1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
3 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free
5 Software Foundation, Inc.
7 This file is part of GDB.
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.
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.
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. */
25 #include "exceptions.h"
27 #include "breakpoint.h"
32 #include "gdb_assert.h"
33 #include "gdb_string.h"
35 const struct exception exception_none = { 0, NO_ERROR, NULL };
37 /* One should use catch_errors rather than manipulating these
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))
44 #define SIGJMP_BUF jmp_buf
45 #define SIGSETJMP(buf) setjmp(buf)
46 #define SIGLONGJMP(buf,val) longjmp((buf), (val))
49 /* Possible catcher states. */
51 /* Initial state, a new catcher has just been created. */
53 /* The catch code is running. */
56 /* The catch code threw an exception. */
60 /* Possible catcher actions. */
69 enum catcher_state state;
70 /* Jump buffer pointing back at the exception handler. */
72 /* Status buffer belonging to the exception handler. */
73 volatile struct exception *exception;
74 /* Saved/current state. */
76 struct ui_out *saved_uiout;
77 struct cleanup *saved_cleanup_chain;
82 /* Where to go for throw_exception(). */
83 static struct catcher *current_catcher;
86 catcher_init (struct ui_out *func_uiout,
87 volatile struct exception *exception,
90 struct catcher *new_catcher = XZALLOC (struct catcher);
92 /* Start with no exception, save it's address. */
93 exception->reason = 0;
94 exception->error = NO_ERROR;
95 exception->message = NULL;
96 new_catcher->exception = exception;
98 new_catcher->mask = mask;
100 /* Override the global ``struct ui_out'' builder. */
101 new_catcher->saved_uiout = uiout;
104 /* Prevent error/quit during FUNC from calling cleanups established
106 new_catcher->saved_cleanup_chain = save_cleanups ();
108 /* Push this new catcher on the top. */
109 new_catcher->prev = current_catcher;
110 current_catcher = new_catcher;
111 new_catcher->state = CATCHER_CREATED;
113 return &new_catcher->buf;
119 struct catcher *old_catcher = current_catcher;
120 current_catcher = old_catcher->prev;
122 /* Restore the cleanup chain, the error/quit messages, and the uiout
123 builder, to their original states. */
125 restore_cleanups (old_catcher->saved_cleanup_chain);
127 uiout = old_catcher->saved_uiout;
132 /* Catcher state machine. Returns non-zero if the m/c should be run
133 again, zero if it should abort. */
136 catcher_state_machine (enum catcher_action action)
138 switch (current_catcher->state)
140 case CATCHER_CREATED:
144 /* Allow the code to run the catcher. */
145 current_catcher->state = CATCHER_RUNNING;
148 internal_error (__FILE__, __LINE__, "bad state");
150 case CATCHER_RUNNING:
154 /* No error/quit has occured. Just clean up. */
158 current_catcher->state = CATCHER_RUNNING_1;
161 current_catcher->state = CATCHER_ABORTING;
162 /* See also throw_exception. */
165 internal_error (__FILE__, __LINE__, "bad switch");
167 case CATCHER_RUNNING_1:
171 /* The did a "break" from the inner while loop. */
175 current_catcher->state = CATCHER_RUNNING;
178 current_catcher->state = CATCHER_ABORTING;
179 /* See also throw_exception. */
182 internal_error (__FILE__, __LINE__, "bad switch");
184 case CATCHER_ABORTING:
189 struct exception exception = *current_catcher->exception;
190 if (current_catcher->mask & RETURN_MASK (exception.reason))
192 /* Exit normally if this catcher can handle this
193 exception. The caller analyses the func return
198 /* The caller didn't request that the event be caught,
199 relay the event to the next containing
202 throw_exception (exception);
205 internal_error (__FILE__, __LINE__, "bad state");
208 internal_error (__FILE__, __LINE__, "bad switch");
212 /* Return EXCEPTION to the nearest containing catch_errors(). */
215 throw_exception (struct exception exception)
220 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
221 I can think of a reason why that is vital, though). */
222 bpstat_clear_actions (stop_bpstat); /* Clear queued breakpoint commands */
224 disable_current_display ();
225 do_cleanups (ALL_CLEANUPS);
226 if (target_can_async_p () && !target_executing)
227 do_exec_cleanups (ALL_CLEANUPS);
229 do_exec_error_cleanups (ALL_CLEANUPS);
231 /* Jump to the containing catch_errors() call, communicating REASON
232 to that call via setjmp's return value. Note that REASON can't
233 be zero, by definition in defs.h. */
234 catcher_state_machine (CATCH_THROWING);
235 *current_catcher->exception = exception;
236 SIGLONGJMP (current_catcher->buf, exception.reason);
239 static char *last_message;
242 deprecated_throw_reason (enum return_reason reason)
244 struct exception exception;
245 memset (&exception, 0, sizeof exception);
247 exception.reason = reason;
253 exception.error = GENERIC_ERROR;
256 internal_error (__FILE__, __LINE__, "bad switch");
259 throw_exception (exception);
265 if (deprecated_error_begin_hook)
266 deprecated_error_begin_hook ();
267 target_terminal_ours ();
268 wrap_here (""); /* Force out any buffered output */
269 gdb_flush (gdb_stdout);
270 annotate_error_begin ();
274 print_exception (struct ui_file *file, struct exception e)
276 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
277 as that way the MI's behavior is preserved. */
280 for (start = e.message; start != NULL; start = end)
282 end = strchr (start, '\n');
284 fputs_filtered (start, file);
288 ui_file_write (file, start, end - start);
291 fprintf_filtered (file, "\n");
293 /* Now append the annotation. */
300 /* Assume that these are all errors. */
304 internal_error (__FILE__, __LINE__, _("Bad switch."));
309 exception_print (struct ui_file *file, struct exception e)
311 if (e.reason < 0 && e.message != NULL)
314 print_exception (file, e);
319 exception_fprintf (struct ui_file *file, struct exception e,
320 const char *prefix, ...)
322 if (e.reason < 0 && e.message != NULL)
328 /* Print the prefix. */
329 va_start (args, prefix);
330 vfprintf_filtered (file, prefix, args);
333 print_exception (file, e);
338 print_any_exception (struct ui_file *file, const char *prefix,
341 if (e.reason < 0 && e.message != NULL)
343 target_terminal_ours ();
344 wrap_here (""); /* Force out any buffered output */
345 gdb_flush (gdb_stdout);
346 annotate_error_begin ();
348 /* Print the prefix. */
349 if (prefix != NULL && prefix[0] != '\0')
350 fputs_filtered (prefix, file);
351 print_exception (file, e);
356 throw_it (enum return_reason reason, enum errors error, const char *fmt,
357 va_list ap) ATTR_NORETURN;
359 throw_it (enum return_reason reason, enum errors error, const char *fmt,
365 /* Save the message. Create the new message before deleting the
366 old, the new message may include the old message text. */
367 new_message = xstrvprintf (fmt, ap);
368 xfree (last_message);
369 last_message = new_message;
371 /* Create the exception. */
374 e.message = last_message;
376 /* Throw the exception. */
381 throw_verror (enum errors error, const char *fmt, va_list ap)
383 throw_it (RETURN_ERROR, error, fmt, ap);
387 throw_vfatal (const char *fmt, va_list ap)
389 throw_it (RETURN_QUIT, NO_ERROR, fmt, ap);
393 throw_error (enum errors error, const char *fmt, ...)
396 va_start (args, fmt);
397 throw_it (RETURN_ERROR, error, fmt, args);
401 /* Call FUNC() with args FUNC_UIOUT and FUNC_ARGS, catching any
402 errors. Set FUNC_CAUGHT to an ``enum return_reason'' if the
403 function is aborted (using throw_exception() or zero if the
404 function returns normally. Set FUNC_VAL to the value returned by
405 the function or 0 if the function was aborted.
407 Must not be called with immediate_quit in effect (bad things might
408 happen, say we got a signal in the middle of a memcpy to quit_return).
409 This is an OK restriction; with very few exceptions immediate_quit can
410 be replaced by judicious use of QUIT.
412 MASK specifies what to catch; it is normally set to
413 RETURN_MASK_ALL, if for no other reason than that the code which
414 calls catch_errors might not be set up to deal with a quit which
415 isn't caught. But if the code can deal with it, it generally
416 should be RETURN_MASK_ERROR, unless for some reason it is more
417 useful to abort only the portion of the operation inside the
418 catch_errors. Note that quit should return to the command line
419 fairly quickly, even if some further processing is being done. */
421 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
422 error() et.al. could maintain a set of flags that indicate the the
423 current state of each of the longjmp buffers. This would give the
424 longjmp code the chance to detect a longjmp botch (before it gets
425 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
426 code also randomly used a SET_TOP_LEVEL macro that directly
427 initialize the longjmp buffers. */
429 /* MAYBE: cagney/1999-11-05: Should the catch_errors and cleanups code
430 be consolidated into a single file instead of being distributed
431 between utils.c and top.c? */
434 catch_exceptions (struct ui_out *uiout,
435 catch_exceptions_ftype *func,
439 return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask);
443 catch_exception (struct ui_out *uiout,
444 catch_exception_ftype *func,
448 volatile struct exception exception;
450 catch = catcher_init (uiout, &exception, mask);
451 for (SIGSETJMP ((*catch));
452 catcher_state_machine (CATCH_ITER);)
453 (*func) (uiout, func_args);
458 catch_exceptions_with_msg (struct ui_out *uiout,
459 catch_exceptions_ftype *func,
464 volatile struct exception exception;
465 volatile int val = 0;
466 SIGJMP_BUF *catch = catcher_init (uiout, &exception, mask);
467 for (SIGSETJMP ((*catch)); catcher_state_machine (CATCH_ITER);)
468 val = (*func) (uiout, func_args);
469 print_any_exception (gdb_stderr, NULL, exception);
470 gdb_assert (val >= 0);
471 gdb_assert (exception.reason <= 0);
472 if (exception.reason < 0)
474 /* If caller wants a copy of the low-level error message, make
475 one. This is used in the case of a silent error whereby the
476 caller may optionally want to issue the message. */
477 if (gdberrmsg != NULL)
479 if (exception.message != NULL)
480 *gdberrmsg = xstrdup (exception.message);
484 return exception.reason;
490 catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
493 volatile int val = 0;
494 volatile struct exception exception;
495 SIGJMP_BUF *catch = catcher_init (uiout, &exception, mask);
496 /* This illustrates how it is possible to nest the mechanism and
497 hence catch "break". Of course this doesn't address the need to
498 also catch "return". */
499 for (SIGSETJMP ((*catch)); catcher_state_machine (CATCH_ITER);)
500 val = func (func_args);
501 print_any_exception (gdb_stderr, errstring, exception);
502 if (exception.reason != 0)
508 catch_command_errors (catch_command_errors_ftype * command,
509 char *arg, int from_tty, return_mask mask)
511 volatile struct exception e;
512 SIGJMP_BUF *catch = catcher_init (uiout, &e, mask);
513 for (SIGSETJMP ((*catch)); catcher_state_machine (CATCH_ITER);)
514 command (arg, from_tty);
515 print_any_exception (gdb_stderr, NULL, e);