1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
3 Copyright (C) 1986-2019 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
27 /* Reasons for calling throw_exceptions(). NOTE: all reason values
28 must be different from zero. enum value 0 is reserved for internal
29 use as the return value from an initial setjmp(). */
35 /* Any other error. */
39 #define RETURN_MASK(reason) (1 << (int)(-reason))
43 RETURN_MASK_QUIT
= RETURN_MASK (RETURN_QUIT
),
44 RETURN_MASK_ERROR
= RETURN_MASK (RETURN_ERROR
),
45 RETURN_MASK_ALL
= (RETURN_MASK_QUIT
| RETURN_MASK_ERROR
)
48 /* Describe all exceptions. */
53 /* Any generic error, the corresponding text is in
57 /* Something requested was not found. */
60 /* Thread library lacks support necessary for finding thread local
62 TLS_NO_LIBRARY_SUPPORT_ERROR
,
64 /* Load module not found while attempting to find thread local storage. */
65 TLS_LOAD_MODULE_NOT_FOUND_ERROR
,
67 /* Thread local storage has not been allocated yet. */
68 TLS_NOT_ALLOCATED_YET_ERROR
,
70 /* Something else went wrong while attempting to find thread local
71 storage. The ``struct gdb_exception'' message field provides
75 /* Problem parsing an XML document. */
78 /* Error accessing memory. */
81 /* Value not available. E.g., a register was not collected in a
85 /* Value was optimized out. Note: if the value was a register, this
86 means the register was not saved in the frame. */
89 /* DW_OP_entry_value resolving failed. */
92 /* Target throwing an error has been closed. Current command should be
93 aborted as the inferior state is no longer valid. */
96 /* An undefined command was executed. */
97 UNDEFINED_COMMAND_ERROR
,
99 /* Requested feature, method, mechanism, etc. is not supported. */
102 /* The number of candidates generated during line completion has
103 reached the user's specified limit. This isn't an error, this exception
104 is used to halt searching for more completions, but for consistency
105 "_ERROR" is appended to the name. */
106 MAX_COMPLETIONS_REACHED_ERROR
,
108 /* Add more errors here. */
115 : reason ((enum return_reason
) 0),
120 gdb_exception (enum return_reason r
, enum errors e
)
126 /* The copy constructor exists so that we can mark it "noexcept",
127 which is a good practice for any sort of exception object. */
128 gdb_exception (const gdb_exception
&other
) noexcept
129 : reason (other
.reason
),
131 message (other
.message
)
135 /* The assignment operator exists so that we can mark it "noexcept",
136 which is a good practice for any sort of exception object. */
137 gdb_exception
&operator= (const gdb_exception
&other
) noexcept
139 reason
= other
.reason
;
141 message
= other
.message
;
145 /* Return the contents of the exception message, as a C string. The
146 string remains owned by the exception object. */
147 const char *what () const noexcept
149 return message
->c_str ();
152 enum return_reason reason
;
154 std::shared_ptr
<std::string
> message
;
157 /* Functions to drive the sjlj-based exceptions state machine. Though
158 declared here by necessity, these functions should be considered
159 internal to the exceptions subsystem and not used other than via
160 the TRY/CATCH (or TRY_SJLJ/CATCH_SJLJ) macros defined below. */
162 extern jmp_buf *exceptions_state_mc_init (void);
163 extern int exceptions_state_mc_action_iter (void);
164 extern int exceptions_state_mc_action_iter_1 (void);
165 extern int exceptions_state_mc_catch (struct gdb_exception
*, int);
167 /* For the C++ try/catch-based TRY/CATCH mechanism. */
169 extern void exception_rethrow (void) ATTRIBUTE_NORETURN
;
171 /* Macro to wrap up standard try/catch behavior.
173 The double loop lets us correctly handle code "break"ing out of the
174 try catch block. (It works as the "break" only exits the inner
175 "while" loop, the outer for loop detects this handling it
176 correctly.) Of course "return" and "goto" are not so lucky.
185 CATCH (e, RETURN_MASK_ERROR)
189 case RETURN_ERROR: ...
194 Note that the SJLJ version of the macros are actually named
195 TRY_SJLJ/CATCH_SJLJ in order to make it possible to call them even
196 when TRY/CATCH are mapped to C++ try/catch. The SJLJ variants are
197 needed in some cases where gdb exceptions need to cross third-party
198 library code compiled without exceptions support (e.g.,
204 exceptions_state_mc_init (); \
207 while (exceptions_state_mc_action_iter ()) \
208 while (exceptions_state_mc_action_iter_1 ())
210 #define CATCH_SJLJ(EXCEPTION, MASK) \
212 struct gdb_exception EXCEPTION; \
213 if (exceptions_state_mc_catch (&(EXCEPTION), MASK))
215 #define END_CATCH_SJLJ \
218 /* We still need to wrap TRY/CATCH in C++ so that cleanups and C++
219 exceptions can coexist.
221 The TRY blocked is wrapped in a do/while(0) so that break/continue
222 within the block works the same as in C.
224 END_CATCH makes sure that even if the CATCH block doesn't want to
225 catch the exception, we stop at every frame in the unwind chain to
226 run its cleanups, which may e.g., have pointers to stack variables
227 that are going to be destroyed.
229 There's an outer scope around the whole TRY/END_CATCH in order to
230 cause a compilation error if you forget to add the END_CATCH at the
231 end a TRY/CATCH construct. */
240 #define CATCH(EXCEPTION, MASK) \
243 catch (struct gdb_exception ## _ ## MASK &EXCEPTION)
248 exception_rethrow (); \
252 /* The exception types client code may catch. They're just shims
253 around gdb_exception that add nothing but type info. Which is used
254 is selected depending on the MASK argument passed to CATCH. */
256 struct gdb_exception_error
: public gdb_exception
258 explicit gdb_exception_error (const gdb_exception
&ex
) noexcept
264 struct gdb_exception_quit
: public gdb_exception
266 explicit gdb_exception_quit (const gdb_exception
&ex
) noexcept
272 /* An exception type that inherits from both std::bad_alloc and a gdb
273 exception. This is necessary because operator new can only throw
274 std::bad_alloc, and OTOH, we want exceptions thrown due to memory
275 allocation error to be caught by all the CATCH/RETURN_MASK_ALL
276 spread around the codebase. */
278 struct gdb_quit_bad_alloc
279 : public gdb_exception_quit
,
280 public std::bad_alloc
282 explicit gdb_quit_bad_alloc (const gdb_exception
&ex
) noexcept
283 : gdb_exception_quit (ex
),
291 /* Throw an exception (as described by "struct gdb_exception"),
292 landing in the inner most containing exception handler established
294 extern void throw_exception (struct gdb_exception exception
)
297 /* Throw an exception by executing a LONG JUMP to the inner most
298 containing exception handler established using TRY_SJLJ. Necessary
299 in some cases where we need to throw GDB exceptions across
300 third-party library code (e.g., readline). */
301 extern void throw_exception_sjlj (struct gdb_exception exception
)
304 /* Convenience wrappers around throw_exception that throw GDB
306 extern void throw_verror (enum errors
, const char *fmt
, va_list ap
)
307 ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (2, 0);
308 extern void throw_vquit (const char *fmt
, va_list ap
)
309 ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (1, 0);
310 extern void throw_error (enum errors error
, const char *fmt
, ...)
311 ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (2, 3);
312 extern void throw_quit (const char *fmt
, ...)
313 ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (1, 2);
315 /* A pre-defined non-exception. */
316 extern const struct gdb_exception exception_none
;
318 #endif /* COMMON_COMMON_EXCEPTIONS_H */