| 1 | /* Exception (throw catch) mechanism, for GDB, the GNU debugger. |
| 2 | |
| 3 | Copyright (C) 1986-2017 Free Software Foundation, Inc. |
| 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 | |
| 20 | #ifndef COMMON_EXCEPTIONS_H |
| 21 | #define COMMON_EXCEPTIONS_H |
| 22 | |
| 23 | #include <setjmp.h> |
| 24 | #include <new> |
| 25 | |
| 26 | /* Reasons for calling throw_exceptions(). NOTE: all reason values |
| 27 | must be less than zero. enum value 0 is reserved for internal use |
| 28 | as the return value from an initial setjmp(). The function |
| 29 | catch_exceptions() reserves values >= 0 as legal results from its |
| 30 | wrapped function. */ |
| 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 | |
| 90 | /* DW_OP_entry_value resolving failed. */ |
| 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 | |
| 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 | |
| 109 | /* Add more errors here. */ |
| 110 | NR_ERRORS |
| 111 | }; |
| 112 | |
| 113 | struct gdb_exception |
| 114 | { |
| 115 | enum return_reason reason; |
| 116 | enum errors error; |
| 117 | const char *message; |
| 118 | }; |
| 119 | |
| 120 | /* The different exception mechanisms that TRY/CATCH can map to. */ |
| 121 | |
| 122 | /* Make GDB exceptions use setjmp/longjmp behind the scenes. */ |
| 123 | #define GDB_XCPT_SJMP 1 |
| 124 | |
| 125 | /* Make GDB exceptions use try/catch behind the scenes. */ |
| 126 | #define GDB_XCPT_TRY 2 |
| 127 | |
| 128 | /* Specify this mode to build with TRY/CATCH mapped directly to raw |
| 129 | try/catch. GDB won't work correctly, but building that way catches |
| 130 | code tryin to break/continue out of the try block, along with |
| 131 | spurious code between the TRY and the CATCH block. */ |
| 132 | #define GDB_XCPT_RAW_TRY 3 |
| 133 | |
| 134 | #define GDB_XCPT GDB_XCPT_TRY |
| 135 | |
| 136 | /* Functions to drive the sjlj-based exceptions state machine. Though |
| 137 | declared here by necessity, these functions should be considered |
| 138 | internal to the exceptions subsystem and not used other than via |
| 139 | the TRY/CATCH (or TRY_SJLJ/CATCH_SJLJ) macros defined below. */ |
| 140 | |
| 141 | extern jmp_buf *exceptions_state_mc_init (void); |
| 142 | extern int exceptions_state_mc_action_iter (void); |
| 143 | extern int exceptions_state_mc_action_iter_1 (void); |
| 144 | extern int exceptions_state_mc_catch (struct gdb_exception *, int); |
| 145 | |
| 146 | /* Same, but for the C++ try/catch-based TRY/CATCH mechanism. */ |
| 147 | |
| 148 | #if GDB_XCPT != GDB_XCPT_SJMP |
| 149 | extern void *exception_try_scope_entry (void); |
| 150 | extern void exception_try_scope_exit (void *saved_state); |
| 151 | extern void exception_rethrow (void); |
| 152 | #endif |
| 153 | |
| 154 | /* Macro to wrap up standard try/catch behavior. |
| 155 | |
| 156 | The double loop lets us correctly handle code "break"ing out of the |
| 157 | try catch block. (It works as the "break" only exits the inner |
| 158 | "while" loop, the outer for loop detects this handling it |
| 159 | correctly.) Of course "return" and "goto" are not so lucky. |
| 160 | |
| 161 | For instance: |
| 162 | |
| 163 | *INDENT-OFF* |
| 164 | |
| 165 | TRY |
| 166 | { |
| 167 | } |
| 168 | CATCH (e, RETURN_MASK_ERROR) |
| 169 | { |
| 170 | switch (e.reason) |
| 171 | { |
| 172 | case RETURN_ERROR: ... |
| 173 | } |
| 174 | } |
| 175 | END_CATCH |
| 176 | |
| 177 | Note that the SJLJ version of the macros are actually named |
| 178 | TRY_SJLJ/CATCH_SJLJ in order to make it possible to call them even |
| 179 | when TRY/CATCH are mapped to C++ try/catch. The SJLJ variants are |
| 180 | needed in some cases where gdb exceptions need to cross third-party |
| 181 | library code compiled without exceptions support (e.g., |
| 182 | readline). */ |
| 183 | |
| 184 | #define TRY_SJLJ \ |
| 185 | { \ |
| 186 | jmp_buf *buf = \ |
| 187 | exceptions_state_mc_init (); \ |
| 188 | setjmp (*buf); \ |
| 189 | } \ |
| 190 | while (exceptions_state_mc_action_iter ()) \ |
| 191 | while (exceptions_state_mc_action_iter_1 ()) |
| 192 | |
| 193 | #define CATCH_SJLJ(EXCEPTION, MASK) \ |
| 194 | { \ |
| 195 | struct gdb_exception EXCEPTION; \ |
| 196 | if (exceptions_state_mc_catch (&(EXCEPTION), MASK)) |
| 197 | |
| 198 | #define END_CATCH_SJLJ \ |
| 199 | } |
| 200 | |
| 201 | #if GDB_XCPT == GDB_XCPT_SJMP |
| 202 | |
| 203 | /* If using SJLJ-based exceptions for all exceptions, then provide |
| 204 | standard aliases. */ |
| 205 | |
| 206 | #define TRY TRY_SJLJ |
| 207 | #define CATCH CATCH_SJLJ |
| 208 | #define END_CATCH END_CATCH_SJLJ |
| 209 | |
| 210 | #endif /* GDB_XCPT_SJMP */ |
| 211 | |
| 212 | #if GDB_XCPT == GDB_XCPT_TRY || GDB_XCPT == GDB_XCPT_RAW_TRY |
| 213 | |
| 214 | /* Prevent error/quit during TRY from calling cleanups established |
| 215 | prior to here. This pops out the scope in either case of normal |
| 216 | exit or exception exit. */ |
| 217 | struct exception_try_scope |
| 218 | { |
| 219 | exception_try_scope () |
| 220 | { |
| 221 | saved_state = exception_try_scope_entry (); |
| 222 | } |
| 223 | ~exception_try_scope () |
| 224 | { |
| 225 | exception_try_scope_exit (saved_state); |
| 226 | } |
| 227 | |
| 228 | void *saved_state; |
| 229 | }; |
| 230 | |
| 231 | #if GDB_XCPT == GDB_XCPT_TRY |
| 232 | |
| 233 | /* We still need to wrap TRY/CATCH in C++ so that cleanups and C++ |
| 234 | exceptions can coexist. The TRY blocked is wrapped in a |
| 235 | do/while(0) so that break/continue within the block works the same |
| 236 | as in C. */ |
| 237 | #define TRY \ |
| 238 | try \ |
| 239 | { \ |
| 240 | exception_try_scope exception_try_scope_instance; \ |
| 241 | do \ |
| 242 | { |
| 243 | |
| 244 | #define CATCH(EXCEPTION, MASK) \ |
| 245 | } while (0); \ |
| 246 | } \ |
| 247 | catch (struct gdb_exception ## _ ## MASK &EXCEPTION) |
| 248 | |
| 249 | #define END_CATCH \ |
| 250 | catch (...) \ |
| 251 | { \ |
| 252 | exception_rethrow (); \ |
| 253 | } |
| 254 | |
| 255 | #else |
| 256 | |
| 257 | #define TRY try |
| 258 | #define CATCH(EXCEPTION, MASK) \ |
| 259 | catch (struct gdb_exception ## _ ## MASK &EXCEPTION) |
| 260 | #define END_CATCH |
| 261 | |
| 262 | #endif |
| 263 | |
| 264 | /* The exception types client code may catch. They're just shims |
| 265 | around gdb_exception that add nothing but type info. Which is used |
| 266 | is selected depending on the MASK argument passed to CATCH. */ |
| 267 | |
| 268 | struct gdb_exception_RETURN_MASK_ALL : public gdb_exception |
| 269 | { |
| 270 | }; |
| 271 | |
| 272 | struct gdb_exception_RETURN_MASK_ERROR : public gdb_exception_RETURN_MASK_ALL |
| 273 | { |
| 274 | }; |
| 275 | |
| 276 | struct gdb_exception_RETURN_MASK_QUIT : public gdb_exception_RETURN_MASK_ALL |
| 277 | { |
| 278 | }; |
| 279 | |
| 280 | #endif /* GDB_XCPT_TRY || GDB_XCPT_RAW_TRY */ |
| 281 | |
| 282 | /* An exception type that inherits from both std::bad_alloc and a gdb |
| 283 | exception. This is necessary because operator new can only throw |
| 284 | std::bad_alloc, and OTOH, we want exceptions thrown due to memory |
| 285 | allocation error to be caught by all the CATCH/RETURN_MASK_ALL |
| 286 | spread around the codebase. */ |
| 287 | |
| 288 | struct gdb_quit_bad_alloc |
| 289 | : public gdb_exception_RETURN_MASK_QUIT, |
| 290 | public std::bad_alloc |
| 291 | { |
| 292 | explicit gdb_quit_bad_alloc (gdb_exception ex) |
| 293 | : std::bad_alloc () |
| 294 | { |
| 295 | gdb_exception *self = this; |
| 296 | |
| 297 | *self = ex; |
| 298 | } |
| 299 | }; |
| 300 | |
| 301 | /* *INDENT-ON* */ |
| 302 | |
| 303 | /* Throw an exception (as described by "struct gdb_exception"), |
| 304 | landing in the inner most containing exception handler established |
| 305 | using TRY/CATCH. */ |
| 306 | extern void throw_exception (struct gdb_exception exception) |
| 307 | ATTRIBUTE_NORETURN; |
| 308 | |
| 309 | /* Throw an exception by executing a LONG JUMP to the inner most |
| 310 | containing exception handler established using TRY_SJLJ. Necessary |
| 311 | in some cases where we need to throw GDB exceptions across |
| 312 | third-party library code (e.g., readline). */ |
| 313 | extern void throw_exception_sjlj (struct gdb_exception exception) |
| 314 | ATTRIBUTE_NORETURN; |
| 315 | |
| 316 | /* Convenience wrappers around throw_exception that throw GDB |
| 317 | errors. */ |
| 318 | extern void throw_verror (enum errors, const char *fmt, va_list ap) |
| 319 | ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 0); |
| 320 | extern void throw_vquit (const char *fmt, va_list ap) |
| 321 | ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 0); |
| 322 | extern void throw_error (enum errors error, const char *fmt, ...) |
| 323 | ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 3); |
| 324 | extern void throw_quit (const char *fmt, ...) |
| 325 | ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2); |
| 326 | |
| 327 | /* A pre-defined non-exception. */ |
| 328 | extern const struct gdb_exception exception_none; |
| 329 | |
| 330 | #endif /* COMMON_EXCEPTIONS_H */ |