testsuite: skip confirmation in 'gdb_reinitialize_dir'
[binutils-gdb.git] / gdbsupport / errors.cc
blobc11229aa0d7b6bcaee033b99449f9bcef39056ee
1 /* Error reporting facilities.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "errors.h"
21 #if defined (USE_WIN32API) || defined(__CYGWIN__)
22 #include <windows.h>
23 #endif /* USE_WIN32API */
25 /* See gdbsupport/errors.h. */
27 void
28 warning (const char *fmt, ...)
30 va_list ap;
32 va_start (ap, fmt);
33 vwarning (fmt, ap);
34 va_end (ap);
37 /* See gdbsupport/errors.h. */
39 void
40 error (const char *fmt, ...)
42 va_list ap;
44 va_start (ap, fmt);
45 verror (fmt, ap);
46 va_end (ap);
49 /* See gdbsupport/errors.h. */
51 void
52 internal_error_loc (const char *file, int line, const char *fmt, ...)
54 va_list ap;
56 va_start (ap, fmt);
57 internal_verror (file, line, fmt, ap);
58 va_end (ap);
61 /* See gdbsupport/errors.h. */
63 void
64 internal_warning_loc (const char *file, int line, const char *fmt, ...)
66 va_list ap;
68 va_start (ap, fmt);
69 internal_vwarning (file, line, fmt, ap);
70 va_end (ap);
73 /* See errors.h. */
75 std::string
76 perror_string (const char *prefix, int errnum)
78 const char *err;
80 if (errnum != 0)
81 err = safe_strerror (errnum);
82 else
83 err = safe_strerror (errno);
84 return std::string (prefix) + ": " + err;
87 /* See errors.h. */
89 void
90 perror_with_name (const char *string, int errnum)
92 std::string combined = perror_string (string, errnum);
94 error (_("%s."), combined.c_str ());
97 #if defined (USE_WIN32API) || defined(__CYGWIN__)
99 /* See errors.h. */
101 const char *
102 strwinerror (ULONGEST error)
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,
114 NULL);
115 if (chars != 0)
117 /* If there is an \r\n appended, zap it. */
118 if (chars >= 2
119 && msgbuf[chars - 2] == '\r'
120 && msgbuf[chars - 1] == '\n')
122 chars -= 2;
123 msgbuf[chars] = 0;
126 if (chars > ARRAY_SIZE (buf) - 1)
128 chars = ARRAY_SIZE (buf) - 1;
129 msgbuf [chars] = 0;
132 #ifdef UNICODE
133 wcstombs (buf, msgbuf, chars + 1);
134 #else
135 strncpy (buf, msgbuf, chars + 1);
136 #endif
137 LocalFree (msgbuf);
139 else
140 sprintf (buf, "unknown win32 error (%u)", (unsigned) error);
142 SetLastError (lasterr);
143 return buf;
146 /* See errors.h. */
148 void
149 throw_winerror_with_name (const char *string, ULONGEST err)
151 error (_("%s (error %u): %s"), string, (unsigned) err, strwinerror (err));
154 #endif /* USE_WIN32API */