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 2009, 2010, 2011 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"
32 #include "gdbthread.h"
34 const struct gdb_exception exception_none = { 0, GDB_NO_ERROR, NULL };
36 /* Possible catcher states. */
38 /* Initial state, a new catcher has just been created. */
40 /* The catch code is running. */
43 /* The catch code threw an exception. */
47 /* Possible catcher actions. */
56 enum catcher_state state;
57 /* Jump buffer pointing back at the exception handler. */
58 EXCEPTIONS_SIGJMP_BUF buf;
59 /* Status buffer belonging to the exception handler. */
60 volatile struct gdb_exception *exception;
61 /* Saved/current state. */
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 (volatile struct gdb_exception *exception,
75 struct catcher *new_catcher = XZALLOC (struct catcher);
77 /* Start with no exception, save it's address. */
78 exception->reason = 0;
79 exception->error = GDB_NO_ERROR;
80 exception->message = NULL;
81 new_catcher->exception = exception;
83 new_catcher->mask = mask;
85 /* Prevent error/quit during FUNC from calling cleanups established
87 new_catcher->saved_cleanup_chain = save_cleanups ();
89 /* Push this new catcher on the top. */
90 new_catcher->prev = current_catcher;
91 current_catcher = new_catcher;
92 new_catcher->state = CATCHER_CREATED;
94 return &new_catcher->buf;
100 struct catcher *old_catcher = current_catcher;
102 current_catcher = old_catcher->prev;
104 /* Restore the cleanup chain, the error/quit messages, and the uiout
105 builder, to their original states. */
107 restore_cleanups (old_catcher->saved_cleanup_chain);
112 /* Catcher state machine. Returns non-zero if the m/c should be run
113 again, zero if it should abort. */
116 exceptions_state_mc (enum catcher_action action)
118 switch (current_catcher->state)
120 case CATCHER_CREATED:
124 /* Allow the code to run the catcher. */
125 current_catcher->state = CATCHER_RUNNING;
128 internal_error (__FILE__, __LINE__, _("bad state"));
130 case CATCHER_RUNNING:
134 /* No error/quit has occured. Just clean up. */
138 current_catcher->state = CATCHER_RUNNING_1;
141 current_catcher->state = CATCHER_ABORTING;
142 /* See also throw_exception. */
145 internal_error (__FILE__, __LINE__, _("bad switch"));
147 case CATCHER_RUNNING_1:
151 /* The did a "break" from the inner while loop. */
155 current_catcher->state = CATCHER_RUNNING;
158 current_catcher->state = CATCHER_ABORTING;
159 /* See also throw_exception. */
162 internal_error (__FILE__, __LINE__, _("bad switch"));
164 case CATCHER_ABORTING:
169 struct gdb_exception exception = *current_catcher->exception;
171 if (current_catcher->mask & RETURN_MASK (exception.reason))
173 /* Exit normally if this catcher can handle this
174 exception. The caller analyses the func return
179 /* The caller didn't request that the event be caught,
180 relay the event to the next containing
183 throw_exception (exception);
186 internal_error (__FILE__, __LINE__, _("bad state"));
189 internal_error (__FILE__, __LINE__, _("bad switch"));
194 exceptions_state_mc_action_iter (void)
196 return exceptions_state_mc (CATCH_ITER);
200 exceptions_state_mc_action_iter_1 (void)
202 return exceptions_state_mc (CATCH_ITER_1);
205 /* Return EXCEPTION to the nearest containing catch_errors(). */
208 throw_exception (struct gdb_exception exception)
213 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
214 I can think of a reason why that is vital, though). */
215 bpstat_clear_actions ();
217 do_cleanups (ALL_CLEANUPS);
219 /* Jump to the containing catch_errors() call, communicating REASON
220 to that call via setjmp's return value. Note that REASON can't
221 be zero, by definition in defs.h. */
222 exceptions_state_mc (CATCH_THROWING);
223 *current_catcher->exception = exception;
224 EXCEPTIONS_SIGLONGJMP (current_catcher->buf, exception.reason);
227 static char *last_message;
230 deprecated_throw_reason (enum return_reason reason)
232 struct gdb_exception exception;
234 memset (&exception, 0, sizeof exception);
236 exception.reason = reason;
242 exception.error = GENERIC_ERROR;
245 internal_error (__FILE__, __LINE__, _("bad switch"));
248 throw_exception (exception);
254 struct serial *gdb_stdout_serial;
256 if (deprecated_error_begin_hook)
257 deprecated_error_begin_hook ();
258 target_terminal_ours ();
260 /* We want all output to appear now, before we print the error. We
261 have 3 levels of buffering we have to flush (it's possible that
262 some of these should be changed to flush the lower-level ones
265 /* 1. The _filtered buffer. */
268 /* 2. The stdio buffer. */
269 gdb_flush (gdb_stdout);
270 gdb_flush (gdb_stderr);
272 /* 3. The system-level buffer. */
273 gdb_stdout_serial = serial_fdopen (1);
274 if (gdb_stdout_serial)
276 serial_drain_output (gdb_stdout_serial);
277 serial_un_fdopen (gdb_stdout_serial);
280 annotate_error_begin ();
284 print_exception (struct ui_file *file, struct gdb_exception e)
286 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
287 as that way the MI's behavior is preserved. */
291 for (start = e.message; start != NULL; start = end)
293 end = strchr (start, '\n');
295 fputs_filtered (start, file);
299 ui_file_write (file, start, end - start);
302 fprintf_filtered (file, "\n");
304 /* Now append the annotation. */
311 /* Assume that these are all errors. */
315 internal_error (__FILE__, __LINE__, _("Bad switch."));
320 exception_print (struct ui_file *file, struct gdb_exception e)
322 if (e.reason < 0 && e.message != NULL)
325 print_exception (file, e);
330 exception_fprintf (struct ui_file *file, struct gdb_exception e,
331 const char *prefix, ...)
333 if (e.reason < 0 && e.message != NULL)
339 /* Print the prefix. */
340 va_start (args, prefix);
341 vfprintf_filtered (file, prefix, args);
344 print_exception (file, e);
349 print_any_exception (struct ui_file *file, const char *prefix,
350 struct gdb_exception e)
352 if (e.reason < 0 && e.message != NULL)
354 target_terminal_ours ();
355 wrap_here (""); /* Force out any buffered output. */
356 gdb_flush (gdb_stdout);
357 annotate_error_begin ();
359 /* Print the prefix. */
360 if (prefix != NULL && prefix[0] != '\0')
361 fputs_filtered (prefix, file);
362 print_exception (file, e);
366 static void ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (3, 0)
367 throw_it (enum return_reason reason, enum errors error, const char *fmt,
370 struct gdb_exception e;
373 /* Save the message. Create the new message before deleting the
374 old, the new message may include the old message text. */
375 new_message = xstrvprintf (fmt, ap);
376 xfree (last_message);
377 last_message = new_message;
379 /* Create the exception. */
382 e.message = last_message;
384 /* Throw the exception. */
389 throw_verror (enum errors error, const char *fmt, va_list ap)
391 throw_it (RETURN_ERROR, error, fmt, ap);
395 throw_vfatal (const char *fmt, va_list ap)
397 throw_it (RETURN_QUIT, GDB_NO_ERROR, fmt, ap);
401 throw_error (enum errors error, const char *fmt, ...)
405 va_start (args, fmt);
406 throw_it (RETURN_ERROR, error, fmt, args);
410 /* Call FUNC(UIOUT, FUNC_ARGS) but wrapped within an exception
411 handler. If an exception (enum return_reason) is thrown using
412 throw_exception() than all cleanups installed since
413 catch_exceptions() was entered are invoked, the (-ve) exception
414 value is then returned by catch_exceptions. If FUNC() returns
415 normally (with a positive or zero return value) then that value is
416 returned by catch_exceptions(). It is an internal_error() for
417 FUNC() to return a negative value.
419 See exceptions.h for further usage details.
421 Must not be called with immediate_quit in effect (bad things might
422 happen, say we got a signal in the middle of a memcpy to quit_return).
423 This is an OK restriction; with very few exceptions immediate_quit can
424 be replaced by judicious use of QUIT. */
426 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
427 error() et al. could maintain a set of flags that indicate the
428 current state of each of the longjmp buffers. This would give the
429 longjmp code the chance to detect a longjmp botch (before it gets
430 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
431 code also randomly used a SET_TOP_LEVEL macro that directly
432 initialized the longjmp buffers. */
435 catch_exceptions (struct ui_out *uiout,
436 catch_exceptions_ftype *func,
440 return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask);
444 catch_exceptions_with_msg (struct ui_out *func_uiout,
445 catch_exceptions_ftype *func,
450 volatile struct gdb_exception exception;
451 volatile int val = 0;
452 struct ui_out *saved_uiout;
454 /* Save and override the global ``struct ui_out'' builder. */
455 saved_uiout = current_uiout;
456 current_uiout = func_uiout;
458 TRY_CATCH (exception, RETURN_MASK_ALL)
460 val = (*func) (current_uiout, func_args);
463 /* Restore the global builder. */
464 current_uiout = saved_uiout;
466 if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
468 /* The caller didn't request that the event be caught.
470 throw_exception (exception);
473 print_any_exception (gdb_stderr, NULL, exception);
474 gdb_assert (val >= 0);
475 gdb_assert (exception.reason <= 0);
476 if (exception.reason < 0)
478 /* If caller wants a copy of the low-level error message, make
479 one. This is used in the case of a silent error whereby the
480 caller may optionally want to issue the message. */
481 if (gdberrmsg != NULL)
483 if (exception.message != NULL)
484 *gdberrmsg = xstrdup (exception.message);
488 return exception.reason;
493 /* This function is superseded by catch_exceptions(). */
496 catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
499 volatile int val = 0;
500 volatile struct gdb_exception exception;
501 struct ui_out *saved_uiout;
503 /* Save the global ``struct ui_out'' builder. */
504 saved_uiout = current_uiout;
506 TRY_CATCH (exception, RETURN_MASK_ALL)
508 val = func (func_args);
511 /* Restore the global builder. */
512 current_uiout = saved_uiout;
514 if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
516 /* The caller didn't request that the event be caught.
518 throw_exception (exception);
521 print_any_exception (gdb_stderr, errstring, exception);
522 if (exception.reason != 0)
528 catch_command_errors (catch_command_errors_ftype * command,
529 char *arg, int from_tty, return_mask mask)
531 volatile struct gdb_exception e;
535 command (arg, from_tty);
537 print_any_exception (gdb_stderr, NULL, e);