gprofng: fix build with -mx32
[binutils-gdb/blckswan.git] / gdbsupport / common-exceptions.h
blob543afda508a92dc2dcd2152c09b524c4b1fa351e
1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
3 Copyright (C) 1986-2022 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 #ifndef COMMON_COMMON_EXCEPTIONS_H
21 #define COMMON_COMMON_EXCEPTIONS_H
23 #include <setjmp.h>
24 #include <new>
25 #include <memory>
26 #include <string>
27 #include <functional>
29 /* Reasons for calling throw_exceptions(). NOTE: all reason values
30 must be different from zero. enum value 0 is reserved for internal
31 use as the return value from an initial setjmp(). */
33 enum return_reason
35 /* User interrupt. */
36 RETURN_QUIT = -2,
37 /* Any other error. */
38 RETURN_ERROR
41 #define RETURN_MASK(reason) (1 << (int)(-reason))
43 typedef enum
45 RETURN_MASK_QUIT = RETURN_MASK (RETURN_QUIT),
46 RETURN_MASK_ERROR = RETURN_MASK (RETURN_ERROR),
47 RETURN_MASK_ALL = (RETURN_MASK_QUIT | RETURN_MASK_ERROR)
48 } return_mask;
50 /* Describe all exceptions. */
52 enum errors {
53 GDB_NO_ERROR,
55 /* Any generic error, the corresponding text is in
56 exception.message. */
57 GENERIC_ERROR,
59 /* Something requested was not found. */
60 NOT_FOUND_ERROR,
62 /* Thread library lacks support necessary for finding thread local
63 storage. */
64 TLS_NO_LIBRARY_SUPPORT_ERROR,
66 /* Load module not found while attempting to find thread local storage. */
67 TLS_LOAD_MODULE_NOT_FOUND_ERROR,
69 /* Thread local storage has not been allocated yet. */
70 TLS_NOT_ALLOCATED_YET_ERROR,
72 /* Something else went wrong while attempting to find thread local
73 storage. The ``struct gdb_exception'' message field provides
74 more detail. */
75 TLS_GENERIC_ERROR,
77 /* Problem parsing an XML document. */
78 XML_PARSE_ERROR,
80 /* Error accessing memory. */
81 MEMORY_ERROR,
83 /* Value not available. E.g., a register was not collected in a
84 traceframe. */
85 NOT_AVAILABLE_ERROR,
87 /* Value was optimized out. Note: if the value was a register, this
88 means the register was not saved in the frame. */
89 OPTIMIZED_OUT_ERROR,
91 /* DW_OP_entry_value resolving failed. */
92 NO_ENTRY_VALUE_ERROR,
94 /* Target throwing an error has been closed. Current command should be
95 aborted as the inferior state is no longer valid. */
96 TARGET_CLOSE_ERROR,
98 /* An undefined command was executed. */
99 UNDEFINED_COMMAND_ERROR,
101 /* Requested feature, method, mechanism, etc. is not supported. */
102 NOT_SUPPORTED_ERROR,
104 /* The number of candidates generated during line completion has
105 reached the user's specified limit. This isn't an error, this exception
106 is used to halt searching for more completions, but for consistency
107 "_ERROR" is appended to the name. */
108 MAX_COMPLETIONS_REACHED_ERROR,
110 /* Add more errors here. */
111 NR_ERRORS
114 struct gdb_exception
116 gdb_exception ()
117 : reason ((enum return_reason) 0),
118 error (GDB_NO_ERROR)
122 gdb_exception (enum return_reason r, enum errors e)
123 : reason (r),
124 error (e)
128 gdb_exception (enum return_reason r, enum errors e,
129 const char *fmt, va_list ap)
130 ATTRIBUTE_PRINTF (4, 0)
131 : reason (r),
132 error (e),
133 message (std::make_shared<std::string> (string_vprintf (fmt, ap)))
137 /* The move constructor exists so that we can mark it "noexcept",
138 which is a good practice for any sort of exception object. */
139 explicit gdb_exception (gdb_exception &&other) noexcept = default;
141 /* The copy constructor exists so that we can mark it "noexcept",
142 which is a good practice for any sort of exception object. */
143 gdb_exception (const gdb_exception &other) noexcept
144 : reason (other.reason),
145 error (other.error),
146 message (other.message)
150 /* The assignment operator exists so that we can mark it "noexcept",
151 which is a good practice for any sort of exception object. */
152 gdb_exception &operator= (const gdb_exception &other) noexcept
154 reason = other.reason;
155 error = other.error;
156 message = other.message;
157 return *this;
160 gdb_exception &operator= (gdb_exception &&other) noexcept = default;
162 /* Return the contents of the exception message, as a C string. The
163 string remains owned by the exception object. */
164 const char *what () const noexcept
166 return message->c_str ();
169 /* Compare two exceptions. */
170 bool operator== (const gdb_exception &other) const
172 const char *msg1 = message == nullptr ? "" : what ();
173 const char *msg2 = other.message == nullptr ? "" : other.what ();
175 return (reason == other.reason
176 && error == other.error
177 && strcmp (msg1, msg2) == 0);
180 /* Compare two exceptions. */
181 bool operator!= (const gdb_exception &other) const
183 return !(*this == other);
186 enum return_reason reason;
187 enum errors error;
188 std::shared_ptr<std::string> message;
191 namespace std
194 /* Specialization of std::hash for gdb_exception. */
195 template<>
196 struct hash<gdb_exception>
198 size_t operator() (const gdb_exception &exc) const
200 size_t result = exc.reason + exc.error;
201 if (exc.message != nullptr)
202 result += std::hash<std::string> {} (*exc.message);
203 return result;
209 /* Functions to drive the sjlj-based exceptions state machine. Though
210 declared here by necessity, these functions should be considered
211 internal to the exceptions subsystem and not used other than via
212 the TRY/CATCH (or TRY_SJLJ/CATCH_SJLJ) macros defined below. */
214 extern jmp_buf *exceptions_state_mc_init (void);
215 extern int exceptions_state_mc_action_iter (void);
216 extern int exceptions_state_mc_action_iter_1 (void);
217 extern int exceptions_state_mc_catch (struct gdb_exception *, int);
219 /* Macro to wrap up standard try/catch behavior.
221 The double loop lets us correctly handle code "break"ing out of the
222 try catch block. (It works as the "break" only exits the inner
223 "while" loop, the outer for loop detects this handling it
224 correctly.) Of course "return" and "goto" are not so lucky.
226 For instance:
228 *INDENT-OFF*
230 TRY_SJLJ
233 CATCH_SJLJ (e, RETURN_MASK_ERROR)
235 switch (e.reason)
237 case RETURN_ERROR: ...
240 END_CATCH_SJLJ
242 The SJLJ variants are needed in some cases where gdb exceptions
243 need to cross third-party library code compiled without exceptions
244 support (e.g., readline). */
246 #define TRY_SJLJ \
248 jmp_buf *buf = \
249 exceptions_state_mc_init (); \
250 setjmp (*buf); \
252 while (exceptions_state_mc_action_iter ()) \
253 while (exceptions_state_mc_action_iter_1 ())
255 #define CATCH_SJLJ(EXCEPTION, MASK) \
257 struct gdb_exception EXCEPTION; \
258 if (exceptions_state_mc_catch (&(EXCEPTION), MASK))
260 #define END_CATCH_SJLJ \
263 /* The exception types client code may catch. They're just shims
264 around gdb_exception that add nothing but type info. Which is used
265 is selected depending on the MASK argument passed to CATCH. */
267 struct gdb_exception_error : public gdb_exception
269 gdb_exception_error (enum errors e, const char *fmt, va_list ap)
270 ATTRIBUTE_PRINTF (3, 0)
271 : gdb_exception (RETURN_ERROR, e, fmt, ap)
275 explicit gdb_exception_error (gdb_exception &&ex) noexcept
276 : gdb_exception (std::move (ex))
278 gdb_assert (ex.reason == RETURN_ERROR);
282 struct gdb_exception_quit : public gdb_exception
284 gdb_exception_quit (const char *fmt, va_list ap)
285 ATTRIBUTE_PRINTF (2, 0)
286 : gdb_exception (RETURN_QUIT, GDB_NO_ERROR, fmt, ap)
290 explicit gdb_exception_quit (gdb_exception &&ex) noexcept
291 : gdb_exception (std::move (ex))
293 gdb_assert (ex.reason == RETURN_QUIT);
297 /* An exception type that inherits from both std::bad_alloc and a gdb
298 exception. This is necessary because operator new can only throw
299 std::bad_alloc, and OTOH, we want exceptions thrown due to memory
300 allocation error to be caught by all the CATCH/RETURN_MASK_ALL
301 spread around the codebase. */
303 struct gdb_quit_bad_alloc
304 : public gdb_exception_quit,
305 public std::bad_alloc
307 explicit gdb_quit_bad_alloc (gdb_exception &&ex) noexcept
308 : gdb_exception_quit (std::move (ex)),
309 std::bad_alloc ()
314 /* *INDENT-ON* */
316 /* Throw an exception (as described by "struct gdb_exception"),
317 landing in the inner most containing exception handler established
318 using TRY/CATCH. */
319 extern void throw_exception (gdb_exception &&exception)
320 ATTRIBUTE_NORETURN;
322 /* Throw an exception by executing a LONG JUMP to the inner most
323 containing exception handler established using TRY_SJLJ. Necessary
324 in some cases where we need to throw GDB exceptions across
325 third-party library code (e.g., readline). */
326 extern void throw_exception_sjlj (const struct gdb_exception &exception)
327 ATTRIBUTE_NORETURN;
329 /* Convenience wrappers around throw_exception that throw GDB
330 errors. */
331 extern void throw_verror (enum errors, const char *fmt, va_list ap)
332 ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 0);
333 extern void throw_vquit (const char *fmt, va_list ap)
334 ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 0);
335 extern void throw_error (enum errors error, const char *fmt, ...)
336 ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 3);
337 extern void throw_quit (const char *fmt, ...)
338 ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2);
340 #endif /* COMMON_COMMON_EXCEPTIONS_H */