1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
3 Copyright (C) 1986-2015 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_EXCEPTIONS_H
21 #define COMMON_EXCEPTIONS_H
23 #include "gdb_setjmp.h"
25 /* Reasons for calling throw_exceptions(). NOTE: all reason values
26 must be less than zero. enum value 0 is reserved for internal use
27 as the return value from an initial setjmp(). The function
28 catch_exceptions() reserves values >= 0 as legal results from its
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_GNU_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. */
114 enum return_reason reason
;
119 /* Functions to drive the exceptions state machine. Though declared
120 here by necessity, these functions should be considered internal to
121 the exceptions subsystem and not used other than via the TRY/CATCH
122 macros defined below. */
125 extern SIGJMP_BUF
*exceptions_state_mc_init (void);
126 extern int exceptions_state_mc_action_iter (void);
127 extern int exceptions_state_mc_action_iter_1 (void);
128 extern int exceptions_state_mc_catch (struct gdb_exception
*, int);
130 extern void *exception_try_scope_entry (void);
131 extern void exception_try_scope_exit (void *saved_state
);
132 extern void exception_rethrow (void);
135 /* Macro to wrap up standard try/catch behavior.
137 The double loop lets us correctly handle code "break"ing out of the
138 try catch block. (It works as the "break" only exits the inner
139 "while" loop, the outer for loop detects this handling it
140 correctly.) Of course "return" and "goto" are not so lucky.
149 CATCH (e, RETURN_MASK_ERROR)
153 case RETURN_ERROR: ...
165 exceptions_state_mc_init (); \
168 while (exceptions_state_mc_action_iter ()) \
169 while (exceptions_state_mc_action_iter_1 ())
171 #define CATCH(EXCEPTION, MASK) \
173 struct gdb_exception EXCEPTION; \
174 if (exceptions_state_mc_catch (&(EXCEPTION), MASK))
181 /* Prevent error/quit during TRY from calling cleanups established
182 prior to here. This pops out the scope in either case of normal
183 exit or exception exit. */
184 struct exception_try_scope
186 exception_try_scope ()
188 saved_state
= exception_try_scope_entry ();
190 ~exception_try_scope ()
192 exception_try_scope_exit (saved_state
);
198 /* Define this to build with TRY/CATCH mapped directly to raw
199 try/catch. GDB won't work correctly, but building that way catches
200 code tryin to break/continue out of the try block, along with
201 spurious code between the TRY and the CATCH block. */
202 //#define USE_RAW_CXX_TRY
204 #ifndef USE_RAW_CXX_TRY
206 /* We still need to wrap TRY/CATCH in C++ so that cleanups and C++
207 exceptions can coexist. The TRY blocked is wrapped in a
208 do/while(0) so that break/continue within the block works the same
213 exception_try_scope exception_try_scope_instance; \
217 #define CATCH(EXCEPTION, MASK) \
220 catch (struct gdb_exception ## _ ## MASK &EXCEPTION)
225 exception_rethrow (); \
230 #define CATCH(EXCEPTION, MASK) \
231 catch (struct gdb_exception ## _ ## MASK &EXCEPTION)
236 /* The exception types client code may catch. They're just shims
237 around gdb_exception that add nothing but type info. Which is used
238 is selected depending on the MASK argument passed to CATCH. */
240 struct gdb_exception_RETURN_MASK_ALL
: public gdb_exception
244 struct gdb_exception_RETURN_MASK_ERROR
: public gdb_exception_RETURN_MASK_ALL
248 struct gdb_exception_RETURN_MASK_QUIT
: public gdb_exception_RETURN_MASK_ALL
256 /* Hook to allow client-specific actions to be performed prior to
257 throwing an exception. This function must be provided by the
258 client, and will be called before any cleanups are run. */
260 extern void prepare_to_throw_exception (void);
262 /* Throw an exception (as described by "struct gdb_exception"). Will
263 execute a LONG JUMP to the inner most containing exception handler
264 established using catch_exceptions() (or similar).
266 Code normally throws an exception using error() et.al. For various
267 reaons, GDB also contains code that throws an exception directly.
268 For instance, the remote*.c targets contain CNTRL-C signal handlers
269 that propogate the QUIT event up the exception chain. ``This could
270 be a good thing or a dangerous thing.'' -- the Existential
273 extern void throw_exception (struct gdb_exception exception
)
275 extern void throw_verror (enum errors
, const char *fmt
, va_list ap
)
276 ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (2, 0);
277 extern void throw_vquit (const char *fmt
, va_list ap
)
278 ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (1, 0);
279 extern void throw_error (enum errors error
, const char *fmt
, ...)
280 ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (2, 3);
281 extern void throw_quit (const char *fmt
, ...)
282 ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (1, 2);
284 /* A pre-defined non-exception. */
285 extern const struct gdb_exception exception_none
;
287 #endif /* COMMON_EXCEPTIONS_H */