1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 Free 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 3 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, see <http://www.gnu.org/licenses/>. */
23 #include "exceptions.h"
24 #include "breakpoint.h"
29 #include "gdb_assert.h"
30 #include "gdb_string.h"
33 const struct gdb_exception exception_none = { 0, GDB_NO_ERROR, NULL };
35 /* Possible catcher states. */
37 /* Initial state, a new catcher has just been created. */
39 /* The catch code is running. */
42 /* The catch code threw an exception. */
46 /* Possible catcher actions. */
55 enum catcher_state state;
56 /* Jump buffer pointing back at the exception handler. */
57 EXCEPTIONS_SIGJMP_BUF buf;
58 /* Status buffer belonging to the exception handler. */
59 volatile struct gdb_exception *exception;
60 /* Saved/current state. */
62 struct ui_out *saved_uiout;
63 struct cleanup *saved_cleanup_chain;
68 /* Where to go for throw_exception(). */
69 static struct catcher *current_catcher;
71 EXCEPTIONS_SIGJMP_BUF *
72 exceptions_state_mc_init (struct ui_out *func_uiout,
73 volatile struct gdb_exception *exception,
76 struct catcher *new_catcher = XZALLOC (struct catcher);
78 /* Start with no exception, save it's address. */
79 exception->reason = 0;
80 exception->error = GDB_NO_ERROR;
81 exception->message = NULL;
82 new_catcher->exception = exception;
84 new_catcher->mask = mask;
86 /* Override the global ``struct ui_out'' builder. */
87 new_catcher->saved_uiout = uiout;
90 /* Prevent error/quit during FUNC from calling cleanups established
92 new_catcher->saved_cleanup_chain = save_cleanups ();
94 /* Push this new catcher on the top. */
95 new_catcher->prev = current_catcher;
96 current_catcher = new_catcher;
97 new_catcher->state = CATCHER_CREATED;
99 return &new_catcher->buf;
105 struct catcher *old_catcher = current_catcher;
106 current_catcher = old_catcher->prev;
108 /* Restore the cleanup chain, the error/quit messages, and the uiout
109 builder, to their original states. */
111 restore_cleanups (old_catcher->saved_cleanup_chain);
113 uiout = old_catcher->saved_uiout;
118 /* Catcher state machine. Returns non-zero if the m/c should be run
119 again, zero if it should abort. */
122 exceptions_state_mc (enum catcher_action action)
124 switch (current_catcher->state)
126 case CATCHER_CREATED:
130 /* Allow the code to run the catcher. */
131 current_catcher->state = CATCHER_RUNNING;
134 internal_error (__FILE__, __LINE__, _("bad state"));
136 case CATCHER_RUNNING:
140 /* No error/quit has occured. Just clean up. */
144 current_catcher->state = CATCHER_RUNNING_1;
147 current_catcher->state = CATCHER_ABORTING;
148 /* See also throw_exception. */
151 internal_error (__FILE__, __LINE__, _("bad switch"));
153 case CATCHER_RUNNING_1:
157 /* The did a "break" from the inner while loop. */
161 current_catcher->state = CATCHER_RUNNING;
164 current_catcher->state = CATCHER_ABORTING;
165 /* See also throw_exception. */
168 internal_error (__FILE__, __LINE__, _("bad switch"));
170 case CATCHER_ABORTING:
175 struct gdb_exception exception = *current_catcher->exception;
176 if (current_catcher->mask & RETURN_MASK (exception.reason))
178 /* Exit normally if this catcher can handle this
179 exception. The caller analyses the func return
184 /* The caller didn't request that the event be caught,
185 relay the event to the next containing
188 throw_exception (exception);
191 internal_error (__FILE__, __LINE__, _("bad state"));
194 internal_error (__FILE__, __LINE__, _("bad switch"));
199 exceptions_state_mc_action_iter (void)
201 return exceptions_state_mc (CATCH_ITER);
205 exceptions_state_mc_action_iter_1 (void)
207 return exceptions_state_mc (CATCH_ITER_1);
210 /* Return EXCEPTION to the nearest containing catch_errors(). */
213 throw_exception (struct gdb_exception exception)
218 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
219 I can think of a reason why that is vital, though). */
220 bpstat_clear_actions (stop_bpstat); /* Clear queued breakpoint commands */
222 disable_current_display ();
223 do_cleanups (ALL_CLEANUPS);
225 /* Jump to the containing catch_errors() call, communicating REASON
226 to that call via setjmp's return value. Note that REASON can't
227 be zero, by definition in defs.h. */
228 exceptions_state_mc (CATCH_THROWING);
229 *current_catcher->exception = exception;
230 EXCEPTIONS_SIGLONGJMP (current_catcher->buf, exception.reason);
233 static char *last_message;
236 deprecated_throw_reason (enum return_reason reason)
238 struct gdb_exception exception;
239 memset (&exception, 0, sizeof exception);
241 exception.reason = reason;
247 exception.error = GENERIC_ERROR;
250 internal_error (__FILE__, __LINE__, _("bad switch"));
253 throw_exception (exception);
259 struct serial *gdb_stdout_serial;
261 if (deprecated_error_begin_hook)
262 deprecated_error_begin_hook ();
263 target_terminal_ours ();
265 /* We want all output to appear now, before we print the error. We
266 have 3 levels of buffering we have to flush (it's possible that
267 some of these should be changed to flush the lower-level ones
270 /* 1. The _filtered buffer. */
273 /* 2. The stdio buffer. */
274 gdb_flush (gdb_stdout);
275 gdb_flush (gdb_stderr);
277 /* 3. The system-level buffer. */
278 gdb_stdout_serial = serial_fdopen (1);
279 if (gdb_stdout_serial)
281 serial_drain_output (gdb_stdout_serial);
282 serial_un_fdopen (gdb_stdout_serial);
285 annotate_error_begin ();
289 print_exception (struct ui_file *file, struct gdb_exception e)
291 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
292 as that way the MI's behavior is preserved. */
295 for (start = e.message; start != NULL; start = end)
297 end = strchr (start, '\n');
299 fputs_filtered (start, file);
303 ui_file_write (file, start, end - start);
306 fprintf_filtered (file, "\n");
308 /* Now append the annotation. */
315 /* Assume that these are all errors. */
319 internal_error (__FILE__, __LINE__, _("Bad switch."));
324 exception_print (struct ui_file *file, struct gdb_exception e)
326 if (e.reason < 0 && e.message != NULL)
329 print_exception (file, e);
334 exception_fprintf (struct ui_file *file, struct gdb_exception e,
335 const char *prefix, ...)
337 if (e.reason < 0 && e.message != NULL)
343 /* Print the prefix. */
344 va_start (args, prefix);
345 vfprintf_filtered (file, prefix, args);
348 print_exception (file, e);
353 print_any_exception (struct ui_file *file, const char *prefix,
354 struct gdb_exception e)
356 if (e.reason < 0 && e.message != NULL)
358 target_terminal_ours ();
359 wrap_here (""); /* Force out any buffered output */
360 gdb_flush (gdb_stdout);
361 annotate_error_begin ();
363 /* Print the prefix. */
364 if (prefix != NULL && prefix[0] != '\0')
365 fputs_filtered (prefix, file);
366 print_exception (file, e);
370 NORETURN static void ATTR_NORETURN ATTR_FORMAT (printf, 3, 0)
371 throw_it (enum return_reason reason, enum errors error, const char *fmt,
374 struct gdb_exception e;
377 /* Save the message. Create the new message before deleting the
378 old, the new message may include the old message text. */
379 new_message = xstrvprintf (fmt, ap);
380 xfree (last_message);
381 last_message = new_message;
383 /* Create the exception. */
386 e.message = last_message;
388 /* Throw the exception. */
393 throw_verror (enum errors error, const char *fmt, va_list ap)
395 throw_it (RETURN_ERROR, error, fmt, ap);
399 throw_vfatal (const char *fmt, va_list ap)
401 throw_it (RETURN_QUIT, GDB_NO_ERROR, fmt, ap);
405 throw_error (enum errors error, const char *fmt, ...)
408 va_start (args, fmt);
409 throw_it (RETURN_ERROR, error, fmt, args);
413 /* Call FUNC() with args FUNC_UIOUT and FUNC_ARGS, catching any
414 errors. Set FUNC_CAUGHT to an ``enum return_reason'' if the
415 function is aborted (using throw_exception() or zero if the
416 function returns normally. Set FUNC_VAL to the value returned by
417 the function or 0 if the function was aborted.
419 Must not be called with immediate_quit in effect (bad things might
420 happen, say we got a signal in the middle of a memcpy to quit_return).
421 This is an OK restriction; with very few exceptions immediate_quit can
422 be replaced by judicious use of QUIT.
424 MASK specifies what to catch; it is normally set to
425 RETURN_MASK_ALL, if for no other reason than that the code which
426 calls catch_errors might not be set up to deal with a quit which
427 isn't caught. But if the code can deal with it, it generally
428 should be RETURN_MASK_ERROR, unless for some reason it is more
429 useful to abort only the portion of the operation inside the
430 catch_errors. Note that quit should return to the command line
431 fairly quickly, even if some further processing is being done. */
433 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
434 error() et.al. could maintain a set of flags that indicate the the
435 current state of each of the longjmp buffers. This would give the
436 longjmp code the chance to detect a longjmp botch (before it gets
437 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
438 code also randomly used a SET_TOP_LEVEL macro that directly
439 initialize the longjmp buffers. */
441 /* MAYBE: cagney/1999-11-05: Should the catch_errors and cleanups code
442 be consolidated into a single file instead of being distributed
443 between utils.c and top.c? */
446 catch_exceptions (struct ui_out *uiout,
447 catch_exceptions_ftype *func,
451 return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask);
455 catch_exception (struct ui_out *uiout,
456 catch_exception_ftype *func,
460 volatile struct gdb_exception exception;
461 TRY_CATCH (exception, mask)
463 (*func) (uiout, func_args);
469 catch_exceptions_with_msg (struct ui_out *uiout,
470 catch_exceptions_ftype *func,
475 volatile struct gdb_exception exception;
476 volatile int val = 0;
477 TRY_CATCH (exception, mask)
479 val = (*func) (uiout, func_args);
481 print_any_exception (gdb_stderr, NULL, exception);
482 gdb_assert (val >= 0);
483 gdb_assert (exception.reason <= 0);
484 if (exception.reason < 0)
486 /* If caller wants a copy of the low-level error message, make
487 one. This is used in the case of a silent error whereby the
488 caller may optionally want to issue the message. */
489 if (gdberrmsg != NULL)
491 if (exception.message != NULL)
492 *gdberrmsg = xstrdup (exception.message);
496 return exception.reason;
502 catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
505 volatile int val = 0;
506 volatile struct gdb_exception exception;
507 TRY_CATCH (exception, mask)
509 val = func (func_args);
511 print_any_exception (gdb_stderr, errstring, exception);
512 if (exception.reason != 0)
518 catch_command_errors (catch_command_errors_ftype * command,
519 char *arg, int from_tty, return_mask mask)
521 volatile struct gdb_exception e;
524 command (arg, from_tty);
526 print_any_exception (gdb_stderr, NULL, e);