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