Commit | Line | Data |
---|---|---|
60250e8b AC |
1 | /* Exception (throw catch) mechanism, for GDB, the GNU debugger. |
2 | ||
ecd75fc8 | 3 | Copyright (C) 1986-2014 Free Software Foundation, Inc. |
60250e8b AC |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
60250e8b AC |
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 | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
60250e8b AC |
19 | |
20 | #include "defs.h" | |
21 | #include "exceptions.h" | |
60250e8b AC |
22 | #include "breakpoint.h" |
23 | #include "target.h" | |
24 | #include "inferior.h" | |
25 | #include "annotate.h" | |
26 | #include "ui-out.h" | |
e06e2353 | 27 | #include "serial.h" |
347bddb7 | 28 | #include "gdbthread.h" |
60250e8b | 29 | |
7b871fab | 30 | const struct gdb_exception exception_none = { 0, GDB_NO_ERROR, NULL }; |
c1043fc2 | 31 | |
db5f402d AC |
32 | /* Possible catcher states. */ |
33 | enum catcher_state { | |
34 | /* Initial state, a new catcher has just been created. */ | |
35 | CATCHER_CREATED, | |
36 | /* The catch code is running. */ | |
37 | CATCHER_RUNNING, | |
38 | CATCHER_RUNNING_1, | |
39 | /* The catch code threw an exception. */ | |
40 | CATCHER_ABORTING | |
41 | }; | |
42 | ||
43 | /* Possible catcher actions. */ | |
44 | enum catcher_action { | |
45 | CATCH_ITER, | |
46 | CATCH_ITER_1, | |
47 | CATCH_THROWING | |
48 | }; | |
49 | ||
50 | struct catcher | |
51 | { | |
52 | enum catcher_state state; | |
2a78bfb5 | 53 | /* Jump buffer pointing back at the exception handler. */ |
6941d02a | 54 | EXCEPTIONS_SIGJMP_BUF buf; |
8a076db9 | 55 | /* Status buffer belonging to the exception handler. */ |
71fff37b | 56 | volatile struct gdb_exception *exception; |
db5f402d AC |
57 | /* Saved/current state. */ |
58 | int mask; | |
db5f402d | 59 | struct cleanup *saved_cleanup_chain; |
db5f402d AC |
60 | /* Back link. */ |
61 | struct catcher *prev; | |
62 | }; | |
63 | ||
60250e8b | 64 | /* Where to go for throw_exception(). */ |
db5f402d AC |
65 | static struct catcher *current_catcher; |
66 | ||
ef140872 DE |
67 | /* Return length of current_catcher list. */ |
68 | ||
69 | static int | |
70 | catcher_list_size (void) | |
71 | { | |
72 | int size; | |
73 | struct catcher *catcher; | |
74 | ||
75 | for (size = 0, catcher = current_catcher; | |
76 | catcher != NULL; | |
77 | catcher = catcher->prev) | |
78 | ++size; | |
79 | ||
80 | return size; | |
81 | } | |
82 | ||
6941d02a | 83 | EXCEPTIONS_SIGJMP_BUF * |
f9679975 | 84 | exceptions_state_mc_init (volatile struct gdb_exception *exception, |
6941d02a | 85 | return_mask mask) |
db5f402d | 86 | { |
41bf6aca | 87 | struct catcher *new_catcher = XCNEW (struct catcher); |
db5f402d | 88 | |
2a78bfb5 AC |
89 | /* Start with no exception, save it's address. */ |
90 | exception->reason = 0; | |
7b871fab | 91 | exception->error = GDB_NO_ERROR; |
2a78bfb5 AC |
92 | exception->message = NULL; |
93 | new_catcher->exception = exception; | |
94 | ||
db5f402d AC |
95 | new_catcher->mask = mask; |
96 | ||
db5f402d | 97 | /* Prevent error/quit during FUNC from calling cleanups established |
0963b4bd | 98 | prior to here. */ |
db5f402d AC |
99 | new_catcher->saved_cleanup_chain = save_cleanups (); |
100 | ||
101 | /* Push this new catcher on the top. */ | |
102 | new_catcher->prev = current_catcher; | |
103 | current_catcher = new_catcher; | |
104 | new_catcher->state = CATCHER_CREATED; | |
105 | ||
106 | return &new_catcher->buf; | |
107 | } | |
108 | ||
109 | static void | |
110 | catcher_pop (void) | |
111 | { | |
112 | struct catcher *old_catcher = current_catcher; | |
d7f9d729 | 113 | |
db5f402d AC |
114 | current_catcher = old_catcher->prev; |
115 | ||
116 | /* Restore the cleanup chain, the error/quit messages, and the uiout | |
0963b4bd | 117 | builder, to their original states. */ |
db5f402d AC |
118 | |
119 | restore_cleanups (old_catcher->saved_cleanup_chain); | |
120 | ||
db5f402d AC |
121 | xfree (old_catcher); |
122 | } | |
123 | ||
124 | /* Catcher state machine. Returns non-zero if the m/c should be run | |
125 | again, zero if it should abort. */ | |
126 | ||
6941d02a AC |
127 | static int |
128 | exceptions_state_mc (enum catcher_action action) | |
db5f402d AC |
129 | { |
130 | switch (current_catcher->state) | |
131 | { | |
132 | case CATCHER_CREATED: | |
133 | switch (action) | |
134 | { | |
135 | case CATCH_ITER: | |
136 | /* Allow the code to run the catcher. */ | |
137 | current_catcher->state = CATCHER_RUNNING; | |
138 | return 1; | |
139 | default: | |
e2e0b3e5 | 140 | internal_error (__FILE__, __LINE__, _("bad state")); |
db5f402d AC |
141 | } |
142 | case CATCHER_RUNNING: | |
143 | switch (action) | |
144 | { | |
145 | case CATCH_ITER: | |
146 | /* No error/quit has occured. Just clean up. */ | |
147 | catcher_pop (); | |
148 | return 0; | |
149 | case CATCH_ITER_1: | |
150 | current_catcher->state = CATCHER_RUNNING_1; | |
151 | return 1; | |
152 | case CATCH_THROWING: | |
153 | current_catcher->state = CATCHER_ABORTING; | |
154 | /* See also throw_exception. */ | |
155 | return 1; | |
156 | default: | |
e2e0b3e5 | 157 | internal_error (__FILE__, __LINE__, _("bad switch")); |
db5f402d AC |
158 | } |
159 | case CATCHER_RUNNING_1: | |
160 | switch (action) | |
161 | { | |
162 | case CATCH_ITER: | |
163 | /* The did a "break" from the inner while loop. */ | |
164 | catcher_pop (); | |
165 | return 0; | |
166 | case CATCH_ITER_1: | |
167 | current_catcher->state = CATCHER_RUNNING; | |
168 | return 0; | |
169 | case CATCH_THROWING: | |
170 | current_catcher->state = CATCHER_ABORTING; | |
171 | /* See also throw_exception. */ | |
172 | return 1; | |
173 | default: | |
e2e0b3e5 | 174 | internal_error (__FILE__, __LINE__, _("bad switch")); |
db5f402d AC |
175 | } |
176 | case CATCHER_ABORTING: | |
177 | switch (action) | |
178 | { | |
179 | case CATCH_ITER: | |
180 | { | |
71fff37b | 181 | struct gdb_exception exception = *current_catcher->exception; |
d7f9d729 | 182 | |
2a78bfb5 | 183 | if (current_catcher->mask & RETURN_MASK (exception.reason)) |
db5f402d AC |
184 | { |
185 | /* Exit normally if this catcher can handle this | |
186 | exception. The caller analyses the func return | |
187 | values. */ | |
188 | catcher_pop (); | |
189 | return 0; | |
190 | } | |
191 | /* The caller didn't request that the event be caught, | |
192 | relay the event to the next containing | |
0963b4bd | 193 | catch_errors(). */ |
db5f402d | 194 | catcher_pop (); |
2a78bfb5 | 195 | throw_exception (exception); |
db5f402d AC |
196 | } |
197 | default: | |
e2e0b3e5 | 198 | internal_error (__FILE__, __LINE__, _("bad state")); |
db5f402d AC |
199 | } |
200 | default: | |
e2e0b3e5 | 201 | internal_error (__FILE__, __LINE__, _("bad switch")); |
db5f402d AC |
202 | } |
203 | } | |
60250e8b | 204 | |
6941d02a AC |
205 | int |
206 | exceptions_state_mc_action_iter (void) | |
207 | { | |
208 | return exceptions_state_mc (CATCH_ITER); | |
209 | } | |
210 | ||
211 | int | |
212 | exceptions_state_mc_action_iter_1 (void) | |
213 | { | |
214 | return exceptions_state_mc (CATCH_ITER_1); | |
215 | } | |
216 | ||
2a78bfb5 | 217 | /* Return EXCEPTION to the nearest containing catch_errors(). */ |
60250e8b | 218 | |
c25c4a8b | 219 | void |
71fff37b | 220 | throw_exception (struct gdb_exception exception) |
60250e8b | 221 | { |
522002f9 | 222 | clear_quit_flag (); |
60250e8b AC |
223 | immediate_quit = 0; |
224 | ||
6328eb38 | 225 | do_cleanups (all_cleanups ()); |
60250e8b | 226 | |
60250e8b AC |
227 | /* Jump to the containing catch_errors() call, communicating REASON |
228 | to that call via setjmp's return value. Note that REASON can't | |
0963b4bd | 229 | be zero, by definition in defs.h. */ |
6941d02a | 230 | exceptions_state_mc (CATCH_THROWING); |
2a78bfb5 | 231 | *current_catcher->exception = exception; |
6941d02a | 232 | EXCEPTIONS_SIGLONGJMP (current_catcher->buf, exception.reason); |
2a78bfb5 AC |
233 | } |
234 | ||
6b1b7650 | 235 | static void |
c6da7a6d | 236 | print_flush (void) |
6b1b7650 | 237 | { |
e06e2353 AC |
238 | struct serial *gdb_stdout_serial; |
239 | ||
c6da7a6d AC |
240 | if (deprecated_error_begin_hook) |
241 | deprecated_error_begin_hook (); | |
5df43998 GB |
242 | |
243 | if (target_supports_terminal_ours ()) | |
244 | target_terminal_ours (); | |
e06e2353 AC |
245 | |
246 | /* We want all output to appear now, before we print the error. We | |
247 | have 3 levels of buffering we have to flush (it's possible that | |
248 | some of these should be changed to flush the lower-level ones | |
249 | too): */ | |
250 | ||
251 | /* 1. The _filtered buffer. */ | |
5df43998 GB |
252 | if (filtered_printing_initialized ()) |
253 | wrap_here (""); | |
e06e2353 AC |
254 | |
255 | /* 2. The stdio buffer. */ | |
c6da7a6d | 256 | gdb_flush (gdb_stdout); |
e06e2353 AC |
257 | gdb_flush (gdb_stderr); |
258 | ||
259 | /* 3. The system-level buffer. */ | |
260 | gdb_stdout_serial = serial_fdopen (1); | |
cade9e54 PB |
261 | if (gdb_stdout_serial) |
262 | { | |
263 | serial_drain_output (gdb_stdout_serial); | |
264 | serial_un_fdopen (gdb_stdout_serial); | |
265 | } | |
e06e2353 | 266 | |
c6da7a6d | 267 | annotate_error_begin (); |
6b1b7650 AC |
268 | } |
269 | ||
9cbc821d | 270 | static void |
71fff37b | 271 | print_exception (struct ui_file *file, struct gdb_exception e) |
9cbc821d AC |
272 | { |
273 | /* KLUGE: cagney/2005-01-13: Write the string out one line at a time | |
274 | as that way the MI's behavior is preserved. */ | |
275 | const char *start; | |
276 | const char *end; | |
d7f9d729 | 277 | |
9cbc821d AC |
278 | for (start = e.message; start != NULL; start = end) |
279 | { | |
280 | end = strchr (start, '\n'); | |
281 | if (end == NULL) | |
282 | fputs_filtered (start, file); | |
283 | else | |
284 | { | |
285 | end++; | |
286 | ui_file_write (file, start, end - start); | |
287 | } | |
288 | } | |
c6da7a6d | 289 | fprintf_filtered (file, "\n"); |
e48f5bee AC |
290 | |
291 | /* Now append the annotation. */ | |
292 | switch (e.reason) | |
293 | { | |
294 | case RETURN_QUIT: | |
295 | annotate_quit (); | |
296 | break; | |
297 | case RETURN_ERROR: | |
298 | /* Assume that these are all errors. */ | |
299 | annotate_error (); | |
300 | break; | |
301 | default: | |
302 | internal_error (__FILE__, __LINE__, _("Bad switch.")); | |
303 | } | |
9cbc821d AC |
304 | } |
305 | ||
8a076db9 | 306 | void |
71fff37b | 307 | exception_print (struct ui_file *file, struct gdb_exception e) |
8a076db9 AC |
308 | { |
309 | if (e.reason < 0 && e.message != NULL) | |
310 | { | |
c6da7a6d | 311 | print_flush (); |
9cbc821d | 312 | print_exception (file, e); |
9cbc821d AC |
313 | } |
314 | } | |
8a076db9 | 315 | |
9cbc821d | 316 | void |
71fff37b | 317 | exception_fprintf (struct ui_file *file, struct gdb_exception e, |
9cbc821d AC |
318 | const char *prefix, ...) |
319 | { | |
320 | if (e.reason < 0 && e.message != NULL) | |
321 | { | |
322 | va_list args; | |
c6da7a6d AC |
323 | |
324 | print_flush (); | |
9cbc821d AC |
325 | |
326 | /* Print the prefix. */ | |
327 | va_start (args, prefix); | |
328 | vfprintf_filtered (file, prefix, args); | |
329 | va_end (args); | |
330 | ||
331 | print_exception (file, e); | |
8a076db9 AC |
332 | } |
333 | } | |
334 | ||
ef140872 DE |
335 | /* A stack of exception messages. |
336 | This is needed to handle nested calls to throw_it: we don't want to | |
337 | xfree space for a message before it's used. | |
338 | This can happen if we throw an exception during a cleanup: | |
339 | An outer TRY_CATCH may have an exception message it wants to print, | |
340 | but while doing cleanups further calls to throw_it are made. | |
341 | ||
342 | This is indexed by the size of the current_catcher list. | |
343 | It is a dynamically allocated array so that we don't care how deeply | |
344 | GDB nests its TRY_CATCHs. */ | |
345 | static char **exception_messages; | |
346 | ||
347 | /* The number of currently allocated entries in exception_messages. */ | |
348 | static int exception_messages_size; | |
349 | ||
c25c4a8b | 350 | static void ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (3, 0) |
3af1e0e3 AC |
351 | throw_it (enum return_reason reason, enum errors error, const char *fmt, |
352 | va_list ap) | |
6b1b7650 | 353 | { |
71fff37b | 354 | struct gdb_exception e; |
17d92a02 | 355 | char *new_message; |
ef140872 DE |
356 | int depth = catcher_list_size (); |
357 | ||
358 | gdb_assert (depth > 0); | |
6b1b7650 | 359 | |
ef140872 | 360 | /* Note: The new message may use an old message's text. */ |
17d92a02 | 361 | new_message = xstrvprintf (fmt, ap); |
ef140872 DE |
362 | |
363 | if (depth > exception_messages_size) | |
364 | { | |
365 | int old_size = exception_messages_size; | |
366 | ||
367 | exception_messages_size = depth + 10; | |
368 | exception_messages = (char **) xrealloc (exception_messages, | |
369 | exception_messages_size | |
370 | * sizeof (char *)); | |
371 | memset (exception_messages + old_size, 0, | |
372 | (exception_messages_size - old_size) * sizeof (char *)); | |
373 | } | |
374 | ||
375 | xfree (exception_messages[depth - 1]); | |
376 | exception_messages[depth - 1] = new_message; | |
c6da7a6d AC |
377 | |
378 | /* Create the exception. */ | |
379 | e.reason = reason; | |
380 | e.error = error; | |
ef140872 | 381 | e.message = new_message; |
6b1b7650 | 382 | |
6b1b7650 | 383 | /* Throw the exception. */ |
6b1b7650 AC |
384 | throw_exception (e); |
385 | } | |
386 | ||
c25c4a8b | 387 | void |
6b1b7650 AC |
388 | throw_verror (enum errors error, const char *fmt, va_list ap) |
389 | { | |
3af1e0e3 | 390 | throw_it (RETURN_ERROR, error, fmt, ap); |
6b1b7650 AC |
391 | } |
392 | ||
c25c4a8b | 393 | void |
2c51604d | 394 | throw_vquit (const char *fmt, va_list ap) |
6b1b7650 | 395 | { |
7b871fab | 396 | throw_it (RETURN_QUIT, GDB_NO_ERROR, fmt, ap); |
6b1b7650 AC |
397 | } |
398 | ||
c25c4a8b | 399 | void |
05ff989b | 400 | throw_error (enum errors error, const char *fmt, ...) |
6b1b7650 | 401 | { |
05ff989b | 402 | va_list args; |
d7f9d729 | 403 | |
05ff989b | 404 | va_start (args, fmt); |
2c51604d GB |
405 | throw_verror (error, fmt, args); |
406 | va_end (args); | |
407 | } | |
408 | ||
409 | void | |
410 | throw_quit (const char *fmt, ...) | |
411 | { | |
412 | va_list args; | |
413 | ||
414 | va_start (args, fmt); | |
415 | throw_vquit (fmt, args); | |
05ff989b | 416 | va_end (args); |
6b1b7650 AC |
417 | } |
418 | ||
787274f0 DE |
419 | /* Call FUNC(UIOUT, FUNC_ARGS) but wrapped within an exception |
420 | handler. If an exception (enum return_reason) is thrown using | |
421 | throw_exception() than all cleanups installed since | |
422 | catch_exceptions() was entered are invoked, the (-ve) exception | |
423 | value is then returned by catch_exceptions. If FUNC() returns | |
424 | normally (with a positive or zero return value) then that value is | |
425 | returned by catch_exceptions(). It is an internal_error() for | |
426 | FUNC() to return a negative value. | |
427 | ||
428 | See exceptions.h for further usage details. | |
60250e8b AC |
429 | |
430 | Must not be called with immediate_quit in effect (bad things might | |
431 | happen, say we got a signal in the middle of a memcpy to quit_return). | |
432 | This is an OK restriction; with very few exceptions immediate_quit can | |
787274f0 | 433 | be replaced by judicious use of QUIT. */ |
60250e8b AC |
434 | |
435 | /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with | |
7a9dd1b2 | 436 | error() et al. could maintain a set of flags that indicate the |
60250e8b AC |
437 | current state of each of the longjmp buffers. This would give the |
438 | longjmp code the chance to detect a longjmp botch (before it gets | |
439 | to longjmperror()). Prior to 1999-11-05 this wasn't possible as | |
440 | code also randomly used a SET_TOP_LEVEL macro that directly | |
0963b4bd | 441 | initialized the longjmp buffers. */ |
60250e8b | 442 | |
60250e8b AC |
443 | int |
444 | catch_exceptions (struct ui_out *uiout, | |
445 | catch_exceptions_ftype *func, | |
446 | void *func_args, | |
60250e8b AC |
447 | return_mask mask) |
448 | { | |
1c3c7ee7 | 449 | return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask); |
60250e8b AC |
450 | } |
451 | ||
452 | int | |
f9679975 | 453 | catch_exceptions_with_msg (struct ui_out *func_uiout, |
60250e8b AC |
454 | catch_exceptions_ftype *func, |
455 | void *func_args, | |
60250e8b AC |
456 | char **gdberrmsg, |
457 | return_mask mask) | |
458 | { | |
71fff37b | 459 | volatile struct gdb_exception exception; |
2a78bfb5 | 460 | volatile int val = 0; |
f9679975 | 461 | struct ui_out *saved_uiout; |
d7f9d729 | 462 | |
f9679975 | 463 | /* Save and override the global ``struct ui_out'' builder. */ |
79a45e25 PA |
464 | saved_uiout = current_uiout; |
465 | current_uiout = func_uiout; | |
f9679975 PA |
466 | |
467 | TRY_CATCH (exception, RETURN_MASK_ALL) | |
6941d02a | 468 | { |
79a45e25 | 469 | val = (*func) (current_uiout, func_args); |
6941d02a | 470 | } |
f9679975 PA |
471 | |
472 | /* Restore the global builder. */ | |
79a45e25 | 473 | current_uiout = saved_uiout; |
f9679975 PA |
474 | |
475 | if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0) | |
476 | { | |
477 | /* The caller didn't request that the event be caught. | |
478 | Rethrow. */ | |
479 | throw_exception (exception); | |
480 | } | |
481 | ||
feefc97b | 482 | exception_print (gdb_stderr, exception); |
60250e8b | 483 | gdb_assert (val >= 0); |
2a78bfb5 AC |
484 | gdb_assert (exception.reason <= 0); |
485 | if (exception.reason < 0) | |
486 | { | |
487 | /* If caller wants a copy of the low-level error message, make | |
488 | one. This is used in the case of a silent error whereby the | |
489 | caller may optionally want to issue the message. */ | |
490 | if (gdberrmsg != NULL) | |
6b1b7650 AC |
491 | { |
492 | if (exception.message != NULL) | |
493 | *gdberrmsg = xstrdup (exception.message); | |
494 | else | |
495 | *gdberrmsg = NULL; | |
496 | } | |
2a78bfb5 AC |
497 | return exception.reason; |
498 | } | |
60250e8b AC |
499 | return val; |
500 | } | |
501 | ||
787274f0 DE |
502 | /* This function is superseded by catch_exceptions(). */ |
503 | ||
60250e8b AC |
504 | int |
505 | catch_errors (catch_errors_ftype *func, void *func_args, char *errstring, | |
506 | return_mask mask) | |
507 | { | |
2a78bfb5 | 508 | volatile int val = 0; |
71fff37b | 509 | volatile struct gdb_exception exception; |
f9679975 | 510 | struct ui_out *saved_uiout; |
d7f9d729 | 511 | |
f9679975 | 512 | /* Save the global ``struct ui_out'' builder. */ |
79a45e25 | 513 | saved_uiout = current_uiout; |
f9679975 PA |
514 | |
515 | TRY_CATCH (exception, RETURN_MASK_ALL) | |
6941d02a AC |
516 | { |
517 | val = func (func_args); | |
518 | } | |
f9679975 PA |
519 | |
520 | /* Restore the global builder. */ | |
79a45e25 | 521 | current_uiout = saved_uiout; |
f9679975 PA |
522 | |
523 | if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0) | |
524 | { | |
525 | /* The caller didn't request that the event be caught. | |
526 | Rethrow. */ | |
527 | throw_exception (exception); | |
528 | } | |
529 | ||
feefc97b | 530 | exception_fprintf (gdb_stderr, exception, "%s", errstring); |
2a78bfb5 | 531 | if (exception.reason != 0) |
60250e8b AC |
532 | return 0; |
533 | return val; | |
534 | } |