]>
Commit | Line | Data |
---|---|---|
ff55e1b5 GB |
1 | /* Exception (throw catch) mechanism, for GDB, the GNU debugger. |
2 | ||
1d506c26 | 3 | Copyright (C) 1986-2024 Free Software Foundation, Inc. |
ff55e1b5 GB |
4 | |
5 | This file is part of GDB. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. */ | |
19 | ||
1a5c2598 TT |
20 | #ifndef COMMON_COMMON_EXCEPTIONS_H |
21 | #define COMMON_COMMON_EXCEPTIONS_H | |
ff55e1b5 | 22 | |
173981bc | 23 | #include <setjmp.h> |
503b1c39 | 24 | #include <new> |
3d6e9d23 | 25 | #include <memory> |
56be6ea8 | 26 | #include <string> |
85098eeb | 27 | #include <functional> |
ff55e1b5 | 28 | |
24a601dd TV |
29 | #include "gdbsupport/underlying.h" |
30 | ||
ff55e1b5 | 31 | /* Reasons for calling throw_exceptions(). NOTE: all reason values |
65630365 PA |
32 | must be different from zero. enum value 0 is reserved for internal |
33 | use as the return value from an initial setjmp(). */ | |
ff55e1b5 GB |
34 | |
35 | enum return_reason | |
36 | { | |
522044dc KB |
37 | /* SIGTERM sent to GDB. */ |
38 | RETURN_FORCED_QUIT = -3, | |
ff55e1b5 GB |
39 | /* User interrupt. */ |
40 | RETURN_QUIT = -2, | |
41 | /* Any other error. */ | |
42 | RETURN_ERROR | |
43 | }; | |
44 | ||
45 | #define RETURN_MASK(reason) (1 << (int)(-reason)) | |
46 | ||
47 | typedef enum | |
48 | { | |
522044dc | 49 | RETURN_MASK_FORCED_QUIT = RETURN_MASK (RETURN_FORCED_QUIT), |
ff55e1b5 GB |
50 | RETURN_MASK_QUIT = RETURN_MASK (RETURN_QUIT), |
51 | RETURN_MASK_ERROR = RETURN_MASK (RETURN_ERROR), | |
522044dc | 52 | RETURN_MASK_ALL = (RETURN_MASK_FORCED_QUIT | RETURN_MASK_QUIT | RETURN_MASK_ERROR) |
ff55e1b5 GB |
53 | } return_mask; |
54 | ||
55 | /* Describe all exceptions. */ | |
56 | ||
57 | enum errors { | |
58 | GDB_NO_ERROR, | |
59 | ||
60 | /* Any generic error, the corresponding text is in | |
61 | exception.message. */ | |
62 | GENERIC_ERROR, | |
63 | ||
64 | /* Something requested was not found. */ | |
65 | NOT_FOUND_ERROR, | |
66 | ||
67 | /* Thread library lacks support necessary for finding thread local | |
68 | storage. */ | |
69 | TLS_NO_LIBRARY_SUPPORT_ERROR, | |
70 | ||
71 | /* Load module not found while attempting to find thread local storage. */ | |
72 | TLS_LOAD_MODULE_NOT_FOUND_ERROR, | |
73 | ||
74 | /* Thread local storage has not been allocated yet. */ | |
75 | TLS_NOT_ALLOCATED_YET_ERROR, | |
76 | ||
77 | /* Something else went wrong while attempting to find thread local | |
78 | storage. The ``struct gdb_exception'' message field provides | |
79 | more detail. */ | |
80 | TLS_GENERIC_ERROR, | |
81 | ||
82 | /* Problem parsing an XML document. */ | |
83 | XML_PARSE_ERROR, | |
84 | ||
85 | /* Error accessing memory. */ | |
86 | MEMORY_ERROR, | |
87 | ||
88 | /* Value not available. E.g., a register was not collected in a | |
89 | traceframe. */ | |
90 | NOT_AVAILABLE_ERROR, | |
91 | ||
92 | /* Value was optimized out. Note: if the value was a register, this | |
93 | means the register was not saved in the frame. */ | |
94 | OPTIMIZED_OUT_ERROR, | |
95 | ||
216f72a1 | 96 | /* DW_OP_entry_value resolving failed. */ |
ff55e1b5 GB |
97 | NO_ENTRY_VALUE_ERROR, |
98 | ||
99 | /* Target throwing an error has been closed. Current command should be | |
100 | aborted as the inferior state is no longer valid. */ | |
101 | TARGET_CLOSE_ERROR, | |
102 | ||
103 | /* An undefined command was executed. */ | |
104 | UNDEFINED_COMMAND_ERROR, | |
105 | ||
106 | /* Requested feature, method, mechanism, etc. is not supported. */ | |
107 | NOT_SUPPORTED_ERROR, | |
108 | ||
ef0b411a GB |
109 | /* The number of candidates generated during line completion has |
110 | reached the user's specified limit. This isn't an error, this exception | |
111 | is used to halt searching for more completions, but for consistency | |
112 | "_ERROR" is appended to the name. */ | |
113 | MAX_COMPLETIONS_REACHED_ERROR, | |
114 | ||
ff55e1b5 GB |
115 | /* Add more errors here. */ |
116 | NR_ERRORS | |
117 | }; | |
118 | ||
119 | struct gdb_exception | |
120 | { | |
3d6e9d23 TT |
121 | gdb_exception () |
122 | : reason ((enum return_reason) 0), | |
123 | error (GDB_NO_ERROR) | |
124 | { | |
125 | } | |
126 | ||
127 | gdb_exception (enum return_reason r, enum errors e) | |
128 | : reason (r), | |
129 | error (e) | |
130 | { | |
131 | } | |
132 | ||
56be6ea8 PA |
133 | gdb_exception (enum return_reason r, enum errors e, |
134 | const char *fmt, va_list ap) | |
135 | ATTRIBUTE_PRINTF (4, 0) | |
136 | : reason (r), | |
137 | error (e), | |
138 | message (std::make_shared<std::string> (string_vprintf (fmt, ap))) | |
139 | { | |
140 | } | |
141 | ||
94aeb44b TT |
142 | /* The move constructor exists so that we can mark it "noexcept", |
143 | which is a good practice for any sort of exception object. */ | |
144 | explicit gdb_exception (gdb_exception &&other) noexcept = default; | |
145 | ||
3d6e9d23 TT |
146 | /* The copy constructor exists so that we can mark it "noexcept", |
147 | which is a good practice for any sort of exception object. */ | |
148 | gdb_exception (const gdb_exception &other) noexcept | |
149 | : reason (other.reason), | |
150 | error (other.error), | |
151 | message (other.message) | |
152 | { | |
153 | } | |
154 | ||
155 | /* The assignment operator exists so that we can mark it "noexcept", | |
156 | which is a good practice for any sort of exception object. */ | |
157 | gdb_exception &operator= (const gdb_exception &other) noexcept | |
158 | { | |
159 | reason = other.reason; | |
160 | error = other.error; | |
161 | message = other.message; | |
162 | return *this; | |
163 | } | |
164 | ||
c6fdd8b2 TT |
165 | gdb_exception &operator= (gdb_exception &&other) noexcept = default; |
166 | ||
3d6e9d23 TT |
167 | /* Return the contents of the exception message, as a C string. The |
168 | string remains owned by the exception object. */ | |
169 | const char *what () const noexcept | |
170 | { | |
171 | return message->c_str (); | |
172 | } | |
173 | ||
785e5700 TT |
174 | /* Compare two exceptions. */ |
175 | bool operator== (const gdb_exception &other) const | |
176 | { | |
177 | const char *msg1 = message == nullptr ? "" : what (); | |
178 | const char *msg2 = other.message == nullptr ? "" : other.what (); | |
179 | ||
180 | return (reason == other.reason | |
181 | && error == other.error | |
182 | && strcmp (msg1, msg2) == 0); | |
183 | } | |
184 | ||
185 | /* Compare two exceptions. */ | |
186 | bool operator!= (const gdb_exception &other) const | |
187 | { | |
188 | return !(*this == other); | |
189 | } | |
190 | ||
ff55e1b5 GB |
191 | enum return_reason reason; |
192 | enum errors error; | |
3d6e9d23 | 193 | std::shared_ptr<std::string> message; |
ff55e1b5 GB |
194 | }; |
195 | ||
85098eeb TT |
196 | namespace std |
197 | { | |
198 | ||
199 | /* Specialization of std::hash for gdb_exception. */ | |
200 | template<> | |
201 | struct hash<gdb_exception> | |
202 | { | |
203 | size_t operator() (const gdb_exception &exc) const | |
204 | { | |
24a601dd | 205 | size_t result = to_underlying (exc.reason) + to_underlying (exc.error); |
85098eeb TT |
206 | if (exc.message != nullptr) |
207 | result += std::hash<std::string> {} (*exc.message); | |
208 | return result; | |
209 | } | |
210 | }; | |
211 | ||
212 | } | |
213 | ||
89525768 PA |
214 | /* Functions to drive the sjlj-based exceptions state machine. Though |
215 | declared here by necessity, these functions should be considered | |
216 | internal to the exceptions subsystem and not used other than via | |
217 | the TRY/CATCH (or TRY_SJLJ/CATCH_SJLJ) macros defined below. */ | |
ff55e1b5 | 218 | |
173981bc | 219 | extern jmp_buf *exceptions_state_mc_init (void); |
ff55e1b5 GB |
220 | extern int exceptions_state_mc_action_iter (void); |
221 | extern int exceptions_state_mc_action_iter_1 (void); | |
492d29ea | 222 | extern int exceptions_state_mc_catch (struct gdb_exception *, int); |
89525768 | 223 | |
ff55e1b5 GB |
224 | /* Macro to wrap up standard try/catch behavior. |
225 | ||
226 | The double loop lets us correctly handle code "break"ing out of the | |
227 | try catch block. (It works as the "break" only exits the inner | |
228 | "while" loop, the outer for loop detects this handling it | |
229 | correctly.) Of course "return" and "goto" are not so lucky. | |
230 | ||
231 | For instance: | |
232 | ||
233 | *INDENT-OFF* | |
234 | ||
d272eb37 | 235 | TRY_SJLJ |
ff55e1b5 GB |
236 | { |
237 | } | |
d272eb37 | 238 | CATCH_SJLJ (e, RETURN_MASK_ERROR) |
ff55e1b5 | 239 | { |
492d29ea | 240 | switch (e.reason) |
dda83cd7 SM |
241 | { |
242 | case RETURN_ERROR: ... | |
243 | } | |
ff55e1b5 | 244 | } |
d272eb37 | 245 | END_CATCH_SJLJ |
ff55e1b5 | 246 | |
d272eb37 TT |
247 | The SJLJ variants are needed in some cases where gdb exceptions |
248 | need to cross third-party library code compiled without exceptions | |
249 | support (e.g., readline). */ | |
ff55e1b5 | 250 | |
89525768 | 251 | #define TRY_SJLJ \ |
ff55e1b5 | 252 | { \ |
173981bc | 253 | jmp_buf *buf = \ |
492d29ea | 254 | exceptions_state_mc_init (); \ |
173981bc | 255 | setjmp (*buf); \ |
ff55e1b5 GB |
256 | } \ |
257 | while (exceptions_state_mc_action_iter ()) \ | |
258 | while (exceptions_state_mc_action_iter_1 ()) | |
259 | ||
89525768 | 260 | #define CATCH_SJLJ(EXCEPTION, MASK) \ |
492d29ea PA |
261 | { \ |
262 | struct gdb_exception EXCEPTION; \ | |
263 | if (exceptions_state_mc_catch (&(EXCEPTION), MASK)) | |
264 | ||
89525768 | 265 | #define END_CATCH_SJLJ \ |
492d29ea PA |
266 | } |
267 | ||
72df25b2 PA |
268 | /* The exception types client code may catch. They're just shims |
269 | around gdb_exception that add nothing but type info. Which is used | |
270 | is selected depending on the MASK argument passed to CATCH. */ | |
271 | ||
230d2906 | 272 | struct gdb_exception_error : public gdb_exception |
72df25b2 | 273 | { |
56be6ea8 PA |
274 | gdb_exception_error (enum errors e, const char *fmt, va_list ap) |
275 | ATTRIBUTE_PRINTF (3, 0) | |
276 | : gdb_exception (RETURN_ERROR, e, fmt, ap) | |
26003a20 TT |
277 | { |
278 | } | |
279 | ||
94aeb44b TT |
280 | explicit gdb_exception_error (gdb_exception &&ex) noexcept |
281 | : gdb_exception (std::move (ex)) | |
3d6e9d23 | 282 | { |
56be6ea8 | 283 | gdb_assert (ex.reason == RETURN_ERROR); |
3d6e9d23 | 284 | } |
72df25b2 PA |
285 | }; |
286 | ||
230d2906 | 287 | struct gdb_exception_quit : public gdb_exception |
72df25b2 | 288 | { |
56be6ea8 PA |
289 | gdb_exception_quit (const char *fmt, va_list ap) |
290 | ATTRIBUTE_PRINTF (2, 0) | |
291 | : gdb_exception (RETURN_QUIT, GDB_NO_ERROR, fmt, ap) | |
26003a20 TT |
292 | { |
293 | } | |
294 | ||
94aeb44b TT |
295 | explicit gdb_exception_quit (gdb_exception &&ex) noexcept |
296 | : gdb_exception (std::move (ex)) | |
3d6e9d23 | 297 | { |
56be6ea8 | 298 | gdb_assert (ex.reason == RETURN_QUIT); |
3d6e9d23 | 299 | } |
72df25b2 PA |
300 | }; |
301 | ||
522044dc KB |
302 | struct gdb_exception_forced_quit : public gdb_exception |
303 | { | |
304 | gdb_exception_forced_quit (const char *fmt, va_list ap) | |
305 | ATTRIBUTE_PRINTF (2, 0) | |
306 | : gdb_exception (RETURN_FORCED_QUIT, GDB_NO_ERROR, fmt, ap) | |
307 | { | |
308 | } | |
309 | ||
310 | explicit gdb_exception_forced_quit (gdb_exception &&ex) noexcept | |
311 | : gdb_exception (std::move (ex)) | |
312 | { | |
313 | gdb_assert (ex.reason == RETURN_FORCED_QUIT); | |
314 | } | |
315 | }; | |
316 | ||
503b1c39 PA |
317 | /* An exception type that inherits from both std::bad_alloc and a gdb |
318 | exception. This is necessary because operator new can only throw | |
319 | std::bad_alloc, and OTOH, we want exceptions thrown due to memory | |
320 | allocation error to be caught by all the CATCH/RETURN_MASK_ALL | |
321 | spread around the codebase. */ | |
322 | ||
323 | struct gdb_quit_bad_alloc | |
230d2906 | 324 | : public gdb_exception_quit, |
503b1c39 PA |
325 | public std::bad_alloc |
326 | { | |
94aeb44b TT |
327 | explicit gdb_quit_bad_alloc (gdb_exception &&ex) noexcept |
328 | : gdb_exception_quit (std::move (ex)), | |
3d6e9d23 | 329 | std::bad_alloc () |
503b1c39 | 330 | { |
503b1c39 PA |
331 | } |
332 | }; | |
333 | ||
ff55e1b5 GB |
334 | /* *INDENT-ON* */ |
335 | ||
ddb6d633 PA |
336 | /* Throw an exception (as described by "struct gdb_exception"), |
337 | landing in the inner most containing exception handler established | |
338 | using TRY/CATCH. */ | |
94aeb44b | 339 | extern void throw_exception (gdb_exception &&exception) |
ff55e1b5 | 340 | ATTRIBUTE_NORETURN; |
89525768 PA |
341 | |
342 | /* Throw an exception by executing a LONG JUMP to the inner most | |
ddb6d633 PA |
343 | containing exception handler established using TRY_SJLJ. Necessary |
344 | in some cases where we need to throw GDB exceptions across | |
345 | third-party library code (e.g., readline). */ | |
c6fdd8b2 | 346 | extern void throw_exception_sjlj (const struct gdb_exception &exception) |
89525768 PA |
347 | ATTRIBUTE_NORETURN; |
348 | ||
349 | /* Convenience wrappers around throw_exception that throw GDB | |
350 | errors. */ | |
ff55e1b5 GB |
351 | extern void throw_verror (enum errors, const char *fmt, va_list ap) |
352 | ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 0); | |
353 | extern void throw_vquit (const char *fmt, va_list ap) | |
354 | ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 0); | |
355 | extern void throw_error (enum errors error, const char *fmt, ...) | |
356 | ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 3); | |
357 | extern void throw_quit (const char *fmt, ...) | |
358 | ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2); | |
522044dc KB |
359 | extern void throw_forced_quit (const char *fmt, ...) |
360 | ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2); | |
ff55e1b5 | 361 | |
1a5c2598 | 362 | #endif /* COMMON_COMMON_EXCEPTIONS_H */ |