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 | #include "common-defs.h" | |
21 | #include "common-exceptions.h" | |
ff55e1b5 GB |
22 | |
23 | /* Possible catcher states. */ | |
24 | enum catcher_state { | |
25 | /* Initial state, a new catcher has just been created. */ | |
26 | CATCHER_CREATED, | |
27 | /* The catch code is running. */ | |
28 | CATCHER_RUNNING, | |
29 | CATCHER_RUNNING_1, | |
30 | /* The catch code threw an exception. */ | |
31 | CATCHER_ABORTING | |
32 | }; | |
33 | ||
34 | /* Possible catcher actions. */ | |
35 | enum catcher_action { | |
36 | CATCH_ITER, | |
37 | CATCH_ITER_1, | |
38 | CATCH_THROWING | |
39 | }; | |
40 | ||
41 | struct catcher | |
42 | { | |
43 | enum catcher_state state; | |
44 | /* Jump buffer pointing back at the exception handler. */ | |
45 | SIGJMP_BUF buf; | |
46 | /* Status buffer belonging to the exception handler. */ | |
47 | volatile struct gdb_exception *exception; | |
48 | /* Saved/current state. */ | |
49 | int mask; | |
50 | struct cleanup *saved_cleanup_chain; | |
51 | /* Back link. */ | |
52 | struct catcher *prev; | |
53 | }; | |
54 | ||
55 | /* Where to go for throw_exception(). */ | |
56 | static struct catcher *current_catcher; | |
57 | ||
58 | /* Return length of current_catcher list. */ | |
59 | ||
60 | static int | |
61 | catcher_list_size (void) | |
62 | { | |
63 | int size; | |
64 | struct catcher *catcher; | |
65 | ||
66 | for (size = 0, catcher = current_catcher; | |
67 | catcher != NULL; | |
68 | catcher = catcher->prev) | |
69 | ++size; | |
70 | ||
71 | return size; | |
72 | } | |
73 | ||
74 | SIGJMP_BUF * | |
75 | exceptions_state_mc_init (volatile struct gdb_exception *exception, | |
76 | return_mask mask) | |
77 | { | |
78 | struct catcher *new_catcher = XCNEW (struct catcher); | |
79 | ||
80 | /* Start with no exception, save it's address. */ | |
81 | exception->reason = 0; | |
82 | exception->error = GDB_NO_ERROR; | |
83 | exception->message = NULL; | |
84 | new_catcher->exception = exception; | |
85 | ||
86 | new_catcher->mask = mask; | |
87 | ||
88 | /* Prevent error/quit during FUNC from calling cleanups established | |
89 | prior to here. */ | |
90 | new_catcher->saved_cleanup_chain = save_cleanups (); | |
91 | ||
92 | /* Push this new catcher on the top. */ | |
93 | new_catcher->prev = current_catcher; | |
94 | current_catcher = new_catcher; | |
95 | new_catcher->state = CATCHER_CREATED; | |
96 | ||
97 | return &new_catcher->buf; | |
98 | } | |
99 | ||
100 | static void | |
101 | catcher_pop (void) | |
102 | { | |
103 | struct catcher *old_catcher = current_catcher; | |
104 | ||
105 | current_catcher = old_catcher->prev; | |
106 | ||
107 | /* Restore the cleanup chain, the error/quit messages, and the uiout | |
108 | builder, to their original states. */ | |
109 | ||
110 | restore_cleanups (old_catcher->saved_cleanup_chain); | |
111 | ||
112 | xfree (old_catcher); | |
113 | } | |
114 | ||
115 | /* Catcher state machine. Returns non-zero if the m/c should be run | |
116 | again, zero if it should abort. */ | |
117 | ||
118 | static int | |
119 | exceptions_state_mc (enum catcher_action action) | |
120 | { | |
121 | switch (current_catcher->state) | |
122 | { | |
123 | case CATCHER_CREATED: | |
124 | switch (action) | |
125 | { | |
126 | case CATCH_ITER: | |
127 | /* Allow the code to run the catcher. */ | |
128 | current_catcher->state = CATCHER_RUNNING; | |
129 | return 1; | |
130 | default: | |
131 | internal_error (__FILE__, __LINE__, _("bad state")); | |
132 | } | |
133 | case CATCHER_RUNNING: | |
134 | switch (action) | |
135 | { | |
136 | case CATCH_ITER: | |
137 | /* No error/quit has occured. Just clean up. */ | |
138 | catcher_pop (); | |
139 | return 0; | |
140 | case CATCH_ITER_1: | |
141 | current_catcher->state = CATCHER_RUNNING_1; | |
142 | return 1; | |
143 | case CATCH_THROWING: | |
144 | current_catcher->state = CATCHER_ABORTING; | |
145 | /* See also throw_exception. */ | |
146 | return 1; | |
147 | default: | |
148 | internal_error (__FILE__, __LINE__, _("bad switch")); | |
149 | } | |
150 | case CATCHER_RUNNING_1: | |
151 | switch (action) | |
152 | { | |
153 | case CATCH_ITER: | |
154 | /* The did a "break" from the inner while loop. */ | |
155 | catcher_pop (); | |
156 | return 0; | |
157 | case CATCH_ITER_1: | |
158 | current_catcher->state = CATCHER_RUNNING; | |
159 | return 0; | |
160 | case CATCH_THROWING: | |
161 | current_catcher->state = CATCHER_ABORTING; | |
162 | /* See also throw_exception. */ | |
163 | return 1; | |
164 | default: | |
165 | internal_error (__FILE__, __LINE__, _("bad switch")); | |
166 | } | |
167 | case CATCHER_ABORTING: | |
168 | switch (action) | |
169 | { | |
170 | case CATCH_ITER: | |
171 | { | |
172 | struct gdb_exception exception = *current_catcher->exception; | |
173 | ||
174 | if (current_catcher->mask & RETURN_MASK (exception.reason)) | |
175 | { | |
176 | /* Exit normally if this catcher can handle this | |
177 | exception. The caller analyses the func return | |
178 | values. */ | |
179 | catcher_pop (); | |
180 | return 0; | |
181 | } | |
182 | /* The caller didn't request that the event be caught, | |
183 | relay the event to the next containing | |
184 | catch_errors(). */ | |
185 | catcher_pop (); | |
186 | throw_exception (exception); | |
187 | } | |
188 | default: | |
189 | internal_error (__FILE__, __LINE__, _("bad state")); | |
190 | } | |
191 | default: | |
192 | internal_error (__FILE__, __LINE__, _("bad switch")); | |
193 | } | |
194 | } | |
195 | ||
196 | int | |
197 | exceptions_state_mc_action_iter (void) | |
198 | { | |
199 | return exceptions_state_mc (CATCH_ITER); | |
200 | } | |
201 | ||
202 | int | |
203 | exceptions_state_mc_action_iter_1 (void) | |
204 | { | |
205 | return exceptions_state_mc (CATCH_ITER_1); | |
206 | } | |
207 | ||
208 | /* Return EXCEPTION to the nearest containing catch_errors(). */ | |
209 | ||
210 | void | |
211 | throw_exception (struct gdb_exception exception) | |
212 | { | |
213 | prepare_to_throw_exception (); | |
214 | ||
215 | do_cleanups (all_cleanups ()); | |
216 | ||
217 | /* Jump to the containing catch_errors() call, communicating REASON | |
218 | to that call via setjmp's return value. Note that REASON can't | |
219 | be zero, by definition in defs.h. */ | |
220 | exceptions_state_mc (CATCH_THROWING); | |
221 | *current_catcher->exception = exception; | |
222 | SIGLONGJMP (current_catcher->buf, exception.reason); | |
223 | } | |
224 | ||
225 | /* A stack of exception messages. | |
226 | This is needed to handle nested calls to throw_it: we don't want to | |
227 | xfree space for a message before it's used. | |
228 | This can happen if we throw an exception during a cleanup: | |
229 | An outer TRY_CATCH may have an exception message it wants to print, | |
230 | but while doing cleanups further calls to throw_it are made. | |
231 | ||
232 | This is indexed by the size of the current_catcher list. | |
233 | It is a dynamically allocated array so that we don't care how deeply | |
234 | GDB nests its TRY_CATCHs. */ | |
235 | static char **exception_messages; | |
236 | ||
237 | /* The number of currently allocated entries in exception_messages. */ | |
238 | static int exception_messages_size; | |
239 | ||
240 | static void ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (3, 0) | |
241 | throw_it (enum return_reason reason, enum errors error, const char *fmt, | |
242 | va_list ap) | |
243 | { | |
244 | struct gdb_exception e; | |
245 | char *new_message; | |
246 | int depth = catcher_list_size (); | |
247 | ||
248 | gdb_assert (depth > 0); | |
249 | ||
250 | /* Note: The new message may use an old message's text. */ | |
251 | new_message = xstrvprintf (fmt, ap); | |
252 | ||
253 | if (depth > exception_messages_size) | |
254 | { | |
255 | int old_size = exception_messages_size; | |
256 | ||
257 | exception_messages_size = depth + 10; | |
258 | exception_messages = (char **) xrealloc (exception_messages, | |
259 | exception_messages_size | |
260 | * sizeof (char *)); | |
261 | memset (exception_messages + old_size, 0, | |
262 | (exception_messages_size - old_size) * sizeof (char *)); | |
263 | } | |
264 | ||
265 | xfree (exception_messages[depth - 1]); | |
266 | exception_messages[depth - 1] = new_message; | |
267 | ||
268 | /* Create the exception. */ | |
269 | e.reason = reason; | |
270 | e.error = error; | |
271 | e.message = new_message; | |
272 | ||
273 | /* Throw the exception. */ | |
274 | throw_exception (e); | |
275 | } | |
276 | ||
277 | void | |
278 | throw_verror (enum errors error, const char *fmt, va_list ap) | |
279 | { | |
280 | throw_it (RETURN_ERROR, error, fmt, ap); | |
281 | } | |
282 | ||
283 | void | |
284 | throw_vquit (const char *fmt, va_list ap) | |
285 | { | |
286 | throw_it (RETURN_QUIT, GDB_NO_ERROR, fmt, ap); | |
287 | } | |
288 | ||
289 | void | |
290 | throw_error (enum errors error, const char *fmt, ...) | |
291 | { | |
292 | va_list args; | |
293 | ||
294 | va_start (args, fmt); | |
295 | throw_verror (error, fmt, args); | |
296 | va_end (args); | |
297 | } | |
298 | ||
299 | void | |
300 | throw_quit (const char *fmt, ...) | |
301 | { | |
302 | va_list args; | |
303 | ||
304 | va_start (args, fmt); | |
305 | throw_vquit (fmt, args); | |
306 | va_end (args); | |
307 | } |