1 /* Error reporting facilities. 2 3 Copyright (C) 1986-2024 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20 #include "errors.h" 21 #if defined (USE_WIN32API) || defined(__CYGWIN__) 22 #include <windows.h> 23 #endif /* USE_WIN32API */ 24 25 /* See gdbsupport/errors.h. */ 26 27 void 28 warning (const char *fmt, ...) 29 { 30 va_list ap; 31 32 va_start (ap, fmt); 33 vwarning (fmt, ap); 34 va_end (ap); 35 } 36 37 /* See gdbsupport/errors.h. */ 38 39 void 40 error (const char *fmt, ...) 41 { 42 va_list ap; 43 44 va_start (ap, fmt); 45 verror (fmt, ap); 46 va_end (ap); 47 } 48 49 /* See gdbsupport/errors.h. */ 50 51 void 52 internal_error_loc (const char *file, int line, const char *fmt, ...) 53 { 54 va_list ap; 55 56 va_start (ap, fmt); 57 internal_verror (file, line, fmt, ap); 58 va_end (ap); 59 } 60 61 /* See gdbsupport/errors.h. */ 62 63 void 64 internal_warning_loc (const char *file, int line, const char *fmt, ...) 65 { 66 va_list ap; 67 68 va_start (ap, fmt); 69 internal_vwarning (file, line, fmt, ap); 70 va_end (ap); 71 } 72 73 /* See errors.h. */ 74 75 std::string 76 perror_string (const char *prefix, int errnum) 77 { 78 const char *err; 79 80 if (errnum != 0) 81 err = safe_strerror (errnum); 82 else 83 err = safe_strerror (errno); 84 return std::string (prefix) + ": " + err; 85 } 86 87 /* See errors.h. */ 88 89 void 90 perror_with_name (const char *string, int errnum) 91 { 92 std::string combined = perror_string (string, errnum); 93 94 error (_("%s."), combined.c_str ()); 95 } 96 97 #if defined (USE_WIN32API) || defined(__CYGWIN__) 98 99 /* See errors.h. */ 100 101 const char * 102 strwinerror (ULONGEST error) 103 { 104 static char buf[1024]; 105 TCHAR *msgbuf; 106 DWORD lasterr = GetLastError (); 107 DWORD chars = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM 108 | FORMAT_MESSAGE_ALLOCATE_BUFFER, 109 NULL, 110 error, 111 0, /* Default language */ 112 (LPTSTR) &msgbuf, 113 0, 114 NULL); 115 if (chars != 0) 116 { 117 /* If there is an \r\n appended, zap it. */ 118 if (chars >= 2 119 && msgbuf[chars - 2] == '\r' 120 && msgbuf[chars - 1] == '\n') 121 { 122 chars -= 2; 123 msgbuf[chars] = 0; 124 } 125 126 if (chars > ARRAY_SIZE (buf) - 1) 127 { 128 chars = ARRAY_SIZE (buf) - 1; 129 msgbuf [chars] = 0; 130 } 131 132 #ifdef UNICODE 133 wcstombs (buf, msgbuf, chars + 1); 134 #else 135 strncpy (buf, msgbuf, chars + 1); 136 #endif 137 LocalFree (msgbuf); 138 } 139 else 140 sprintf (buf, "unknown win32 error (%u)", (unsigned) error); 141 142 SetLastError (lasterr); 143 return buf; 144 } 145 146 /* See errors.h. */ 147 148 void 149 throw_winerror_with_name (const char *string, ULONGEST err) 150 { 151 error (_("%s (error %u): %s"), string, (unsigned) err, strwinerror (err)); 152 } 153 154 #endif /* USE_WIN32API */ 155