Commit | Line | Data |
---|---|---|
ff55e1b5 GB |
1 | /* Exception (throw catch) mechanism, for GDB, the GNU debugger. |
2 | ||
32d0add0 | 3 | Copyright (C) 1986-2015 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 | ||
20 | #ifndef COMMON_EXCEPTIONS_H | |
21 | #define COMMON_EXCEPTIONS_H | |
22 | ||
23 | #include "gdb_setjmp.h" | |
24 | ||
25 | /* Reasons for calling throw_exceptions(). NOTE: all reason values | |
26 | must be less than zero. enum value 0 is reserved for internal use | |
27 | as the return value from an initial setjmp(). The function | |
28 | catch_exceptions() reserves values >= 0 as legal results from its | |
29 | wrapped function. */ | |
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 | ||
89 | /* DW_OP_GNU_entry_value resolving failed. */ | |
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 | ||
102 | /* Add more errors here. */ | |
103 | NR_ERRORS | |
104 | }; | |
105 | ||
106 | struct gdb_exception | |
107 | { | |
108 | enum return_reason reason; | |
109 | enum errors error; | |
110 | const char *message; | |
111 | }; | |
112 | ||
113 | /* Functions to drive the exceptions state machine. Though declared | |
114 | here by necessity, these functions should be considered internal to | |
115 | the exceptions subsystem and not used other than via the TRY_CATCH | |
116 | macro defined below. */ | |
117 | ||
118 | extern SIGJMP_BUF *exceptions_state_mc_init (volatile struct | |
119 | gdb_exception *exception, | |
120 | return_mask mask); | |
121 | extern int exceptions_state_mc_action_iter (void); | |
122 | extern int exceptions_state_mc_action_iter_1 (void); | |
123 | ||
124 | /* Macro to wrap up standard try/catch behavior. | |
125 | ||
126 | The double loop lets us correctly handle code "break"ing out of the | |
127 | try catch block. (It works as the "break" only exits the inner | |
128 | "while" loop, the outer for loop detects this handling it | |
129 | correctly.) Of course "return" and "goto" are not so lucky. | |
130 | ||
131 | For instance: | |
132 | ||
133 | *INDENT-OFF* | |
134 | ||
135 | volatile struct gdb_exception e; | |
136 | TRY_CATCH (e, RETURN_MASK_ERROR) | |
137 | { | |
138 | } | |
139 | switch (e.reason) | |
140 | { | |
141 | case RETURN_ERROR: ... | |
142 | } | |
143 | ||
144 | */ | |
145 | ||
146 | #define TRY_CATCH(EXCEPTION,MASK) \ | |
147 | { \ | |
148 | SIGJMP_BUF *buf = \ | |
149 | exceptions_state_mc_init (&(EXCEPTION), (MASK)); \ | |
150 | SIGSETJMP (*buf); \ | |
151 | } \ | |
152 | while (exceptions_state_mc_action_iter ()) \ | |
153 | while (exceptions_state_mc_action_iter_1 ()) | |
154 | ||
155 | /* *INDENT-ON* */ | |
156 | ||
157 | /* Hook to allow client-specific actions to be performed prior to | |
158 | throwing an exception. This function must be provided by the | |
159 | client, and will be called before any cleanups are run. */ | |
160 | ||
161 | extern void prepare_to_throw_exception (void); | |
162 | ||
163 | /* Throw an exception (as described by "struct gdb_exception"). Will | |
164 | execute a LONG JUMP to the inner most containing exception handler | |
165 | established using catch_exceptions() (or similar). | |
166 | ||
167 | Code normally throws an exception using error() et.al. For various | |
168 | reaons, GDB also contains code that throws an exception directly. | |
169 | For instance, the remote*.c targets contain CNTRL-C signal handlers | |
170 | that propogate the QUIT event up the exception chain. ``This could | |
171 | be a good thing or a dangerous thing.'' -- the Existential | |
172 | Wombat. */ | |
173 | ||
174 | extern void throw_exception (struct gdb_exception exception) | |
175 | ATTRIBUTE_NORETURN; | |
176 | extern void throw_verror (enum errors, const char *fmt, va_list ap) | |
177 | ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 0); | |
178 | extern void throw_vquit (const char *fmt, va_list ap) | |
179 | ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 0); | |
180 | extern void throw_error (enum errors error, const char *fmt, ...) | |
181 | ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 3); | |
182 | extern void throw_quit (const char *fmt, ...) | |
183 | ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2); | |
184 | ||
185 | #endif /* COMMON_EXCEPTIONS_H */ |