Commit | Line | Data |
---|---|---|
60250e8b AC |
1 | /* Exception (throw catch) mechanism, for GDB, the GNU debugger. |
2 | ||
3 | Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, | |
05ff989b AC |
4 | 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free |
5 | Software Foundation, Inc. | |
60250e8b AC |
6 | |
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 59 Temple Place - Suite 330, | |
22 | Boston, MA 02111-1307, USA. */ | |
23 | ||
24 | #include "defs.h" | |
25 | #include "exceptions.h" | |
26 | #include <setjmp.h> | |
27 | #include "breakpoint.h" | |
28 | #include "target.h" | |
29 | #include "inferior.h" | |
30 | #include "annotate.h" | |
31 | #include "ui-out.h" | |
32 | #include "gdb_assert.h" | |
db5f402d | 33 | #include "gdb_string.h" |
60250e8b | 34 | |
c1043fc2 AC |
35 | const struct exception exception_none = { 0, NO_ERROR, NULL }; |
36 | ||
60250e8b AC |
37 | /* One should use catch_errors rather than manipulating these |
38 | directly. */ | |
39 | #if defined(HAVE_SIGSETJMP) | |
40 | #define SIGJMP_BUF sigjmp_buf | |
41 | #define SIGSETJMP(buf) sigsetjmp((buf), 1) | |
42 | #define SIGLONGJMP(buf,val) siglongjmp((buf), (val)) | |
43 | #else | |
44 | #define SIGJMP_BUF jmp_buf | |
45 | #define SIGSETJMP(buf) setjmp(buf) | |
46 | #define SIGLONGJMP(buf,val) longjmp((buf), (val)) | |
47 | #endif | |
48 | ||
db5f402d AC |
49 | /* Possible catcher states. */ |
50 | enum catcher_state { | |
51 | /* Initial state, a new catcher has just been created. */ | |
52 | CATCHER_CREATED, | |
53 | /* The catch code is running. */ | |
54 | CATCHER_RUNNING, | |
55 | CATCHER_RUNNING_1, | |
56 | /* The catch code threw an exception. */ | |
57 | CATCHER_ABORTING | |
58 | }; | |
59 | ||
60 | /* Possible catcher actions. */ | |
61 | enum catcher_action { | |
62 | CATCH_ITER, | |
63 | CATCH_ITER_1, | |
64 | CATCH_THROWING | |
65 | }; | |
66 | ||
67 | struct catcher | |
68 | { | |
69 | enum catcher_state state; | |
2a78bfb5 | 70 | /* Jump buffer pointing back at the exception handler. */ |
db5f402d | 71 | SIGJMP_BUF buf; |
8a076db9 | 72 | /* Status buffer belonging to the exception handler. */ |
2a78bfb5 | 73 | volatile struct exception *exception; |
8a076db9 AC |
74 | /* Should the error / quit message be printed? Old code assumes |
75 | that this file prints the error/quit message when first reported. | |
76 | New code instead directly handles the printing of error/quit | |
77 | messages. */ | |
78 | int print_message; | |
db5f402d AC |
79 | /* Saved/current state. */ |
80 | int mask; | |
81 | char *saved_error_pre_print; | |
82 | char *saved_quit_pre_print; | |
83 | struct ui_out *saved_uiout; | |
84 | struct cleanup *saved_cleanup_chain; | |
db5f402d AC |
85 | /* Back link. */ |
86 | struct catcher *prev; | |
87 | }; | |
88 | ||
60250e8b | 89 | /* Where to go for throw_exception(). */ |
db5f402d AC |
90 | static struct catcher *current_catcher; |
91 | ||
92 | static SIGJMP_BUF * | |
93 | catcher_init (struct ui_out *func_uiout, | |
94 | char *errstring, | |
2a78bfb5 | 95 | volatile struct exception *exception, |
8a076db9 AC |
96 | return_mask mask, |
97 | int print_message) | |
db5f402d AC |
98 | { |
99 | struct catcher *new_catcher = XZALLOC (struct catcher); | |
100 | ||
2a78bfb5 AC |
101 | /* Start with no exception, save it's address. */ |
102 | exception->reason = 0; | |
103 | exception->error = NO_ERROR; | |
104 | exception->message = NULL; | |
105 | new_catcher->exception = exception; | |
106 | ||
db5f402d | 107 | new_catcher->mask = mask; |
8a076db9 | 108 | new_catcher->print_message = print_message; |
db5f402d AC |
109 | |
110 | /* Override error/quit messages during FUNC. */ | |
111 | new_catcher->saved_error_pre_print = error_pre_print; | |
112 | new_catcher->saved_quit_pre_print = quit_pre_print; | |
113 | if (mask & RETURN_MASK_ERROR) | |
114 | error_pre_print = errstring; | |
115 | if (mask & RETURN_MASK_QUIT) | |
116 | quit_pre_print = errstring; | |
117 | ||
118 | /* Override the global ``struct ui_out'' builder. */ | |
119 | new_catcher->saved_uiout = uiout; | |
120 | uiout = func_uiout; | |
121 | ||
122 | /* Prevent error/quit during FUNC from calling cleanups established | |
123 | prior to here. */ | |
124 | new_catcher->saved_cleanup_chain = save_cleanups (); | |
125 | ||
126 | /* Push this new catcher on the top. */ | |
127 | new_catcher->prev = current_catcher; | |
128 | current_catcher = new_catcher; | |
129 | new_catcher->state = CATCHER_CREATED; | |
130 | ||
131 | return &new_catcher->buf; | |
132 | } | |
133 | ||
134 | static void | |
135 | catcher_pop (void) | |
136 | { | |
137 | struct catcher *old_catcher = current_catcher; | |
138 | current_catcher = old_catcher->prev; | |
139 | ||
140 | /* Restore the cleanup chain, the error/quit messages, and the uiout | |
141 | builder, to their original states. */ | |
142 | ||
143 | restore_cleanups (old_catcher->saved_cleanup_chain); | |
144 | ||
145 | uiout = old_catcher->saved_uiout; | |
146 | ||
147 | quit_pre_print = old_catcher->saved_quit_pre_print; | |
148 | error_pre_print = old_catcher->saved_error_pre_print; | |
149 | ||
150 | xfree (old_catcher); | |
151 | } | |
152 | ||
153 | /* Catcher state machine. Returns non-zero if the m/c should be run | |
154 | again, zero if it should abort. */ | |
155 | ||
156 | int | |
157 | catcher_state_machine (enum catcher_action action) | |
158 | { | |
159 | switch (current_catcher->state) | |
160 | { | |
161 | case CATCHER_CREATED: | |
162 | switch (action) | |
163 | { | |
164 | case CATCH_ITER: | |
165 | /* Allow the code to run the catcher. */ | |
166 | current_catcher->state = CATCHER_RUNNING; | |
167 | return 1; | |
168 | default: | |
169 | internal_error (__FILE__, __LINE__, "bad state"); | |
170 | } | |
171 | case CATCHER_RUNNING: | |
172 | switch (action) | |
173 | { | |
174 | case CATCH_ITER: | |
175 | /* No error/quit has occured. Just clean up. */ | |
176 | catcher_pop (); | |
177 | return 0; | |
178 | case CATCH_ITER_1: | |
179 | current_catcher->state = CATCHER_RUNNING_1; | |
180 | return 1; | |
181 | case CATCH_THROWING: | |
182 | current_catcher->state = CATCHER_ABORTING; | |
183 | /* See also throw_exception. */ | |
184 | return 1; | |
185 | default: | |
186 | internal_error (__FILE__, __LINE__, "bad switch"); | |
187 | } | |
188 | case CATCHER_RUNNING_1: | |
189 | switch (action) | |
190 | { | |
191 | case CATCH_ITER: | |
192 | /* The did a "break" from the inner while loop. */ | |
193 | catcher_pop (); | |
194 | return 0; | |
195 | case CATCH_ITER_1: | |
196 | current_catcher->state = CATCHER_RUNNING; | |
197 | return 0; | |
198 | case CATCH_THROWING: | |
199 | current_catcher->state = CATCHER_ABORTING; | |
200 | /* See also throw_exception. */ | |
201 | return 1; | |
202 | default: | |
203 | internal_error (__FILE__, __LINE__, "bad switch"); | |
204 | } | |
205 | case CATCHER_ABORTING: | |
206 | switch (action) | |
207 | { | |
208 | case CATCH_ITER: | |
209 | { | |
2a78bfb5 AC |
210 | struct exception exception = *current_catcher->exception; |
211 | if (current_catcher->mask & RETURN_MASK (exception.reason)) | |
db5f402d AC |
212 | { |
213 | /* Exit normally if this catcher can handle this | |
214 | exception. The caller analyses the func return | |
215 | values. */ | |
216 | catcher_pop (); | |
217 | return 0; | |
218 | } | |
219 | /* The caller didn't request that the event be caught, | |
220 | relay the event to the next containing | |
221 | catch_errors(). */ | |
222 | catcher_pop (); | |
2a78bfb5 | 223 | throw_exception (exception); |
db5f402d AC |
224 | } |
225 | default: | |
226 | internal_error (__FILE__, __LINE__, "bad state"); | |
227 | } | |
228 | default: | |
229 | internal_error (__FILE__, __LINE__, "bad switch"); | |
230 | } | |
231 | } | |
60250e8b | 232 | |
2a78bfb5 | 233 | /* Return EXCEPTION to the nearest containing catch_errors(). */ |
60250e8b AC |
234 | |
235 | NORETURN void | |
2a78bfb5 | 236 | throw_exception (struct exception exception) |
60250e8b AC |
237 | { |
238 | quit_flag = 0; | |
239 | immediate_quit = 0; | |
240 | ||
241 | /* Perhaps it would be cleaner to do this via the cleanup chain (not sure | |
242 | I can think of a reason why that is vital, though). */ | |
243 | bpstat_clear_actions (stop_bpstat); /* Clear queued breakpoint commands */ | |
244 | ||
245 | disable_current_display (); | |
246 | do_cleanups (ALL_CLEANUPS); | |
247 | if (target_can_async_p () && !target_executing) | |
248 | do_exec_cleanups (ALL_CLEANUPS); | |
249 | if (sync_execution) | |
250 | do_exec_error_cleanups (ALL_CLEANUPS); | |
251 | ||
60250e8b AC |
252 | /* Jump to the containing catch_errors() call, communicating REASON |
253 | to that call via setjmp's return value. Note that REASON can't | |
254 | be zero, by definition in defs.h. */ | |
db5f402d | 255 | catcher_state_machine (CATCH_THROWING); |
2a78bfb5 AC |
256 | *current_catcher->exception = exception; |
257 | SIGLONGJMP (current_catcher->buf, exception.reason); | |
258 | } | |
259 | ||
6b1b7650 AC |
260 | static char *last_message; |
261 | ||
2a78bfb5 AC |
262 | NORETURN void |
263 | throw_reason (enum return_reason reason) | |
264 | { | |
265 | struct exception exception; | |
266 | memset (&exception, 0, sizeof exception); | |
267 | ||
268 | exception.reason = reason; | |
269 | switch (reason) | |
270 | { | |
271 | case RETURN_QUIT: | |
272 | break; | |
273 | case RETURN_ERROR: | |
274 | exception.error = GENERIC_ERROR; | |
2a78bfb5 AC |
275 | break; |
276 | default: | |
277 | internal_error (__FILE__, __LINE__, "bad switch"); | |
278 | } | |
279 | ||
280 | throw_exception (exception); | |
60250e8b AC |
281 | } |
282 | ||
6b1b7650 | 283 | static void |
c6da7a6d | 284 | print_flush (void) |
6b1b7650 | 285 | { |
c6da7a6d AC |
286 | if (deprecated_error_begin_hook) |
287 | deprecated_error_begin_hook (); | |
288 | target_terminal_ours (); | |
289 | wrap_here (""); /* Force out any buffered output */ | |
290 | gdb_flush (gdb_stdout); | |
291 | annotate_error_begin (); | |
6b1b7650 AC |
292 | } |
293 | ||
9cbc821d AC |
294 | static void |
295 | print_exception (struct ui_file *file, struct exception e) | |
296 | { | |
297 | /* KLUGE: cagney/2005-01-13: Write the string out one line at a time | |
298 | as that way the MI's behavior is preserved. */ | |
299 | const char *start; | |
300 | const char *end; | |
301 | for (start = e.message; start != NULL; start = end) | |
302 | { | |
303 | end = strchr (start, '\n'); | |
304 | if (end == NULL) | |
305 | fputs_filtered (start, file); | |
306 | else | |
307 | { | |
308 | end++; | |
309 | ui_file_write (file, start, end - start); | |
310 | } | |
311 | } | |
c6da7a6d | 312 | fprintf_filtered (file, "\n"); |
e48f5bee AC |
313 | |
314 | /* Now append the annotation. */ | |
315 | switch (e.reason) | |
316 | { | |
317 | case RETURN_QUIT: | |
318 | annotate_quit (); | |
319 | break; | |
320 | case RETURN_ERROR: | |
321 | /* Assume that these are all errors. */ | |
322 | annotate_error (); | |
323 | break; | |
324 | default: | |
325 | internal_error (__FILE__, __LINE__, _("Bad switch.")); | |
326 | } | |
9cbc821d AC |
327 | } |
328 | ||
8a076db9 | 329 | void |
9cbc821d | 330 | exception_print (struct ui_file *file, struct exception e) |
8a076db9 AC |
331 | { |
332 | if (e.reason < 0 && e.message != NULL) | |
333 | { | |
c6da7a6d | 334 | print_flush (); |
9cbc821d | 335 | print_exception (file, e); |
9cbc821d AC |
336 | } |
337 | } | |
8a076db9 | 338 | |
9cbc821d AC |
339 | void |
340 | exception_fprintf (struct ui_file *file, struct exception e, | |
341 | const char *prefix, ...) | |
342 | { | |
343 | if (e.reason < 0 && e.message != NULL) | |
344 | { | |
345 | va_list args; | |
c6da7a6d AC |
346 | |
347 | print_flush (); | |
9cbc821d AC |
348 | |
349 | /* Print the prefix. */ | |
350 | va_start (args, prefix); | |
351 | vfprintf_filtered (file, prefix, args); | |
352 | va_end (args); | |
353 | ||
354 | print_exception (file, e); | |
8a076db9 AC |
355 | } |
356 | } | |
357 | ||
e48f5bee AC |
358 | void |
359 | print_any_exception (struct ui_file *file, const char *prefix, | |
360 | struct exception e) | |
361 | { | |
362 | if (e.reason < 0 && e.message != NULL) | |
363 | { | |
364 | target_terminal_ours (); | |
365 | wrap_here (""); /* Force out any buffered output */ | |
366 | gdb_flush (gdb_stdout); | |
367 | annotate_error_begin (); | |
368 | ||
369 | /* Print the prefix. */ | |
370 | if (prefix != NULL && prefix[0] != '\0') | |
371 | fputs_filtered (prefix, file); | |
372 | print_exception (file, e); | |
373 | } | |
374 | } | |
375 | ||
6b1b7650 | 376 | NORETURN static void |
e48f5bee AC |
377 | throw_it (enum return_reason reason, enum errors error, const char *prefix, |
378 | const char *fmt, va_list ap) ATTR_NORETURN; | |
6b1b7650 | 379 | NORETURN static void |
e48f5bee AC |
380 | throw_it (enum return_reason reason, enum errors error, const char *prefix, |
381 | const char *fmt, va_list ap) | |
6b1b7650 | 382 | { |
6b1b7650 | 383 | struct exception e; |
6b1b7650 AC |
384 | |
385 | /* Save the message. */ | |
386 | xfree (last_message); | |
c6da7a6d AC |
387 | last_message = xstrvprintf (fmt, ap); |
388 | ||
389 | /* Create the exception. */ | |
390 | e.reason = reason; | |
391 | e.error = error; | |
392 | e.message = last_message; | |
6b1b7650 | 393 | |
6b1b7650 | 394 | /* Throw the exception. */ |
6b1b7650 AC |
395 | throw_exception (e); |
396 | } | |
397 | ||
398 | NORETURN void | |
399 | throw_verror (enum errors error, const char *fmt, va_list ap) | |
400 | { | |
e48f5bee | 401 | throw_it (RETURN_ERROR, error, error_pre_print, fmt, ap); |
6b1b7650 AC |
402 | } |
403 | ||
404 | NORETURN void | |
405 | throw_vfatal (const char *fmt, va_list ap) | |
406 | { | |
e48f5bee | 407 | throw_it (RETURN_QUIT, NO_ERROR, quit_pre_print, fmt, ap); |
6b1b7650 AC |
408 | } |
409 | ||
410 | NORETURN void | |
05ff989b | 411 | throw_error (enum errors error, const char *fmt, ...) |
6b1b7650 | 412 | { |
05ff989b AC |
413 | va_list args; |
414 | va_start (args, fmt); | |
e48f5bee | 415 | throw_it (RETURN_ERROR, error, error_pre_print, fmt, args); |
05ff989b | 416 | va_end (args); |
6b1b7650 AC |
417 | } |
418 | ||
60250e8b AC |
419 | /* Call FUNC() with args FUNC_UIOUT and FUNC_ARGS, catching any |
420 | errors. Set FUNC_CAUGHT to an ``enum return_reason'' if the | |
421 | function is aborted (using throw_exception() or zero if the | |
422 | function returns normally. Set FUNC_VAL to the value returned by | |
423 | the function or 0 if the function was aborted. | |
424 | ||
425 | Must not be called with immediate_quit in effect (bad things might | |
426 | happen, say we got a signal in the middle of a memcpy to quit_return). | |
427 | This is an OK restriction; with very few exceptions immediate_quit can | |
428 | be replaced by judicious use of QUIT. | |
429 | ||
430 | MASK specifies what to catch; it is normally set to | |
431 | RETURN_MASK_ALL, if for no other reason than that the code which | |
432 | calls catch_errors might not be set up to deal with a quit which | |
433 | isn't caught. But if the code can deal with it, it generally | |
434 | should be RETURN_MASK_ERROR, unless for some reason it is more | |
435 | useful to abort only the portion of the operation inside the | |
436 | catch_errors. Note that quit should return to the command line | |
437 | fairly quickly, even if some further processing is being done. */ | |
438 | ||
439 | /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with | |
440 | error() et.al. could maintain a set of flags that indicate the the | |
441 | current state of each of the longjmp buffers. This would give the | |
442 | longjmp code the chance to detect a longjmp botch (before it gets | |
443 | to longjmperror()). Prior to 1999-11-05 this wasn't possible as | |
444 | code also randomly used a SET_TOP_LEVEL macro that directly | |
445 | initialize the longjmp buffers. */ | |
446 | ||
447 | /* MAYBE: cagney/1999-11-05: Should the catch_errors and cleanups code | |
448 | be consolidated into a single file instead of being distributed | |
449 | between utils.c and top.c? */ | |
450 | ||
60250e8b AC |
451 | int |
452 | catch_exceptions (struct ui_out *uiout, | |
453 | catch_exceptions_ftype *func, | |
454 | void *func_args, | |
60250e8b AC |
455 | return_mask mask) |
456 | { | |
1c3c7ee7 | 457 | return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask); |
60250e8b AC |
458 | } |
459 | ||
2a78bfb5 AC |
460 | struct exception |
461 | catch_exception (struct ui_out *uiout, | |
462 | catch_exception_ftype *func, | |
463 | void *func_args, | |
464 | return_mask mask) | |
465 | { | |
466 | volatile struct exception exception; | |
467 | SIGJMP_BUF *catch; | |
8a076db9 | 468 | catch = catcher_init (uiout, NULL, &exception, mask, 0); |
2a78bfb5 AC |
469 | for (SIGSETJMP ((*catch)); |
470 | catcher_state_machine (CATCH_ITER);) | |
471 | (*func) (uiout, func_args); | |
472 | return exception; | |
473 | } | |
474 | ||
60250e8b AC |
475 | int |
476 | catch_exceptions_with_msg (struct ui_out *uiout, | |
477 | catch_exceptions_ftype *func, | |
478 | void *func_args, | |
60250e8b AC |
479 | char **gdberrmsg, |
480 | return_mask mask) | |
481 | { | |
2a78bfb5 AC |
482 | volatile struct exception exception; |
483 | volatile int val = 0; | |
1c3c7ee7 | 484 | SIGJMP_BUF *catch = catcher_init (uiout, NULL, &exception, mask, 1); |
2a78bfb5 | 485 | for (SIGSETJMP ((*catch)); catcher_state_machine (CATCH_ITER);) |
db5f402d | 486 | val = (*func) (uiout, func_args); |
e48f5bee | 487 | print_any_exception (gdb_stderr, NULL, exception); |
60250e8b | 488 | gdb_assert (val >= 0); |
2a78bfb5 AC |
489 | gdb_assert (exception.reason <= 0); |
490 | if (exception.reason < 0) | |
491 | { | |
492 | /* If caller wants a copy of the low-level error message, make | |
493 | one. This is used in the case of a silent error whereby the | |
494 | caller may optionally want to issue the message. */ | |
495 | if (gdberrmsg != NULL) | |
6b1b7650 AC |
496 | { |
497 | if (exception.message != NULL) | |
498 | *gdberrmsg = xstrdup (exception.message); | |
499 | else | |
500 | *gdberrmsg = NULL; | |
501 | } | |
2a78bfb5 AC |
502 | return exception.reason; |
503 | } | |
60250e8b AC |
504 | return val; |
505 | } | |
506 | ||
60250e8b AC |
507 | int |
508 | catch_errors (catch_errors_ftype *func, void *func_args, char *errstring, | |
509 | return_mask mask) | |
510 | { | |
2a78bfb5 AC |
511 | volatile int val = 0; |
512 | volatile struct exception exception; | |
8a076db9 | 513 | SIGJMP_BUF *catch = catcher_init (uiout, errstring, &exception, mask, 1); |
db5f402d AC |
514 | /* This illustrates how it is possible to nest the mechanism and |
515 | hence catch "break". Of course this doesn't address the need to | |
516 | also catch "return". */ | |
2a78bfb5 AC |
517 | for (SIGSETJMP ((*catch)); catcher_state_machine (CATCH_ITER);) |
518 | val = func (func_args); | |
e48f5bee | 519 | print_any_exception (gdb_stderr, errstring, exception); |
2a78bfb5 | 520 | if (exception.reason != 0) |
60250e8b AC |
521 | return 0; |
522 | return val; | |
523 | } | |
524 | ||
525 | struct captured_command_args | |
526 | { | |
527 | catch_command_errors_ftype *command; | |
528 | char *arg; | |
529 | int from_tty; | |
530 | }; | |
531 | ||
532 | static int | |
533 | do_captured_command (void *data) | |
534 | { | |
535 | struct captured_command_args *context = data; | |
536 | context->command (context->arg, context->from_tty); | |
537 | /* FIXME: cagney/1999-11-07: Technically this do_cleanups() call | |
538 | isn't needed. Instead an assertion check could be made that | |
539 | simply confirmed that the called function correctly cleaned up | |
540 | after itself. Unfortunately, old code (prior to 1999-11-04) in | |
541 | main.c was calling SET_TOP_LEVEL(), calling the command function, | |
542 | and then *always* calling do_cleanups(). For the moment we | |
543 | remain ``bug compatible'' with that old code.. */ | |
544 | do_cleanups (ALL_CLEANUPS); | |
545 | return 1; | |
546 | } | |
547 | ||
548 | int | |
549 | catch_command_errors (catch_command_errors_ftype * command, | |
550 | char *arg, int from_tty, return_mask mask) | |
551 | { | |
552 | struct captured_command_args args; | |
553 | args.command = command; | |
554 | args.arg = arg; | |
555 | args.from_tty = from_tty; | |
556 | return catch_errors (do_captured_command, &args, "", mask); | |
557 | } |