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
28 /* Reasons for calling throw_exceptions(). NOTE: all reason values
29 must be different from zero. enum value 0 is reserved for internal
30 use as the return value from an initial setjmp(). */
36 /* Any other error. */
40 #define RETURN_MASK(reason) (1 << (int)(-reason))
44 RETURN_MASK_QUIT
= RETURN_MASK (RETURN_QUIT
),
45 RETURN_MASK_ERROR
= RETURN_MASK (RETURN_ERROR
),
46 RETURN_MASK_ALL
= (RETURN_MASK_QUIT
| RETURN_MASK_ERROR
)
49 /* Describe all exceptions. */
54 /* Any generic error, the corresponding text is in
58 /* Something requested was not found. */
61 /* Thread library lacks support necessary for finding thread local
63 TLS_NO_LIBRARY_SUPPORT_ERROR
,
65 /* Load module not found while attempting to find thread local storage. */
66 TLS_LOAD_MODULE_NOT_FOUND_ERROR
,
68 /* Thread local storage has not been allocated yet. */
69 TLS_NOT_ALLOCATED_YET_ERROR
,
71 /* Something else went wrong while attempting to find thread local
72 storage. The ``struct gdb_exception'' message field provides
76 /* Problem parsing an XML document. */
79 /* Error accessing memory. */
82 /* Value not available. E.g., a register was not collected in a
86 /* Value was optimized out. Note: if the value was a register, this
87 means the register was not saved in the frame. */
90 /* DW_OP_entry_value resolving failed. */
93 /* Target throwing an error has been closed. Current command should be
94 aborted as the inferior state is no longer valid. */
97 /* An undefined command was executed. */
98 UNDEFINED_COMMAND_ERROR
,
100 /* Requested feature, method, mechanism, etc. is not supported. */
103 /* The number of candidates generated during line completion has
104 reached the user's specified limit. This isn't an error, this exception
105 is used to halt searching for more completions, but for consistency
106 "_ERROR" is appended to the name. */
107 MAX_COMPLETIONS_REACHED_ERROR
,
109 /* Add more errors here. */
116 : reason ((enum return_reason
) 0),
121 gdb_exception (enum return_reason r
, enum errors e
)
127 gdb_exception (enum return_reason r
, enum errors e
,
128 const char *fmt
, va_list ap
)
129 ATTRIBUTE_PRINTF (4, 0)
132 message (std::make_shared
<std::string
> (string_vprintf (fmt
, ap
)))
136 /* The copy constructor exists so that we can mark it "noexcept",
137 which is a good practice for any sort of exception object. */
138 gdb_exception (const gdb_exception
&other
) noexcept
139 : reason (other
.reason
),
141 message (other
.message
)
145 /* The assignment operator exists so that we can mark it "noexcept",
146 which is a good practice for any sort of exception object. */
147 gdb_exception
&operator= (const gdb_exception
&other
) noexcept
149 reason
= other
.reason
;
151 message
= other
.message
;
155 gdb_exception
&operator= (gdb_exception
&&other
) noexcept
= default;
157 /* Return the contents of the exception message, as a C string. The
158 string remains owned by the exception object. */
159 const char *what () const noexcept
161 return message
->c_str ();
164 enum return_reason reason
;
166 std::shared_ptr
<std::string
> message
;
169 /* Functions to drive the sjlj-based exceptions state machine. Though
170 declared here by necessity, these functions should be considered
171 internal to the exceptions subsystem and not used other than via
172 the TRY/CATCH (or TRY_SJLJ/CATCH_SJLJ) macros defined below. */
174 extern jmp_buf *exceptions_state_mc_init (void);
175 extern int exceptions_state_mc_action_iter (void);
176 extern int exceptions_state_mc_action_iter_1 (void);
177 extern int exceptions_state_mc_catch (struct gdb_exception
*, int);
179 /* Macro to wrap up standard try/catch behavior.
181 The double loop lets us correctly handle code "break"ing out of the
182 try catch block. (It works as the "break" only exits the inner
183 "while" loop, the outer for loop detects this handling it
184 correctly.) Of course "return" and "goto" are not so lucky.
193 CATCH_SJLJ (e, RETURN_MASK_ERROR)
197 case RETURN_ERROR: ...
202 The SJLJ variants are needed in some cases where gdb exceptions
203 need to cross third-party library code compiled without exceptions
204 support (e.g., readline). */
209 exceptions_state_mc_init (); \
212 while (exceptions_state_mc_action_iter ()) \
213 while (exceptions_state_mc_action_iter_1 ())
215 #define CATCH_SJLJ(EXCEPTION, MASK) \
217 struct gdb_exception EXCEPTION; \
218 if (exceptions_state_mc_catch (&(EXCEPTION), MASK))
220 #define END_CATCH_SJLJ \
223 /* The exception types client code may catch. They're just shims
224 around gdb_exception that add nothing but type info. Which is used
225 is selected depending on the MASK argument passed to CATCH. */
227 struct gdb_exception_error
: public gdb_exception
229 gdb_exception_error (enum errors e
, const char *fmt
, va_list ap
)
230 ATTRIBUTE_PRINTF (3, 0)
231 : gdb_exception (RETURN_ERROR
, e
, fmt
, ap
)
235 explicit gdb_exception_error (const gdb_exception
&ex
) noexcept
238 gdb_assert (ex
.reason
== RETURN_ERROR
);
242 struct gdb_exception_quit
: public gdb_exception
244 gdb_exception_quit (const char *fmt
, va_list ap
)
245 ATTRIBUTE_PRINTF (2, 0)
246 : gdb_exception (RETURN_QUIT
, GDB_NO_ERROR
, fmt
, ap
)
250 explicit gdb_exception_quit (const gdb_exception
&ex
) noexcept
253 gdb_assert (ex
.reason
== RETURN_QUIT
);
257 /* An exception type that inherits from both std::bad_alloc and a gdb
258 exception. This is necessary because operator new can only throw
259 std::bad_alloc, and OTOH, we want exceptions thrown due to memory
260 allocation error to be caught by all the CATCH/RETURN_MASK_ALL
261 spread around the codebase. */
263 struct gdb_quit_bad_alloc
264 : public gdb_exception_quit
,
265 public std::bad_alloc
267 explicit gdb_quit_bad_alloc (const gdb_exception
&ex
) noexcept
268 : gdb_exception_quit (ex
),
276 /* Throw an exception (as described by "struct gdb_exception"),
277 landing in the inner most containing exception handler established
279 extern void throw_exception (const gdb_exception
&exception
)
282 /* Throw an exception by executing a LONG JUMP to the inner most
283 containing exception handler established using TRY_SJLJ. Necessary
284 in some cases where we need to throw GDB exceptions across
285 third-party library code (e.g., readline). */
286 extern void throw_exception_sjlj (const struct gdb_exception
&exception
)
289 /* Convenience wrappers around throw_exception that throw GDB
291 extern void throw_verror (enum errors
, const char *fmt
, va_list ap
)
292 ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (2, 0);
293 extern void throw_vquit (const char *fmt
, va_list ap
)
294 ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (1, 0);
295 extern void throw_error (enum errors error
, const char *fmt
, ...)
296 ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (2, 3);
297 extern void throw_quit (const char *fmt
, ...)
298 ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (1, 2);
300 #endif /* COMMON_COMMON_EXCEPTIONS_H */