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