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 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 ui_out
*saved_uiout
;
64 struct cleanup
*saved_cleanup_chain
;
69 /* Where to go for throw_exception(). */
70 static struct catcher
*current_catcher
;
72 EXCEPTIONS_SIGJMP_BUF
*
73 exceptions_state_mc_init (struct ui_out
*func_uiout
,
74 volatile struct gdb_exception
*exception
,
77 struct catcher
*new_catcher
= XZALLOC (struct catcher
);
79 /* Start with no exception, save it's address. */
80 exception
->reason
= 0;
81 exception
->error
= GDB_NO_ERROR
;
82 exception
->message
= NULL
;
83 new_catcher
->exception
= exception
;
85 new_catcher
->mask
= mask
;
87 /* Override the global ``struct ui_out'' builder. */
88 new_catcher
->saved_uiout
= uiout
;
91 /* Prevent error/quit during FUNC from calling cleanups established
93 new_catcher
->saved_cleanup_chain
= save_cleanups ();
95 /* Push this new catcher on the top. */
96 new_catcher
->prev
= current_catcher
;
97 current_catcher
= new_catcher
;
98 new_catcher
->state
= CATCHER_CREATED
;
100 return &new_catcher
->buf
;
106 struct catcher
*old_catcher
= current_catcher
;
108 current_catcher
= old_catcher
->prev
;
110 /* Restore the cleanup chain, the error/quit messages, and the uiout
111 builder, to their original states. */
113 restore_cleanups (old_catcher
->saved_cleanup_chain
);
115 uiout
= old_catcher
->saved_uiout
;
120 /* Catcher state machine. Returns non-zero if the m/c should be run
121 again, zero if it should abort. */
124 exceptions_state_mc (enum catcher_action action
)
126 switch (current_catcher
->state
)
128 case CATCHER_CREATED
:
132 /* Allow the code to run the catcher. */
133 current_catcher
->state
= CATCHER_RUNNING
;
136 internal_error (__FILE__
, __LINE__
, _("bad state"));
138 case CATCHER_RUNNING
:
142 /* No error/quit has occured. Just clean up. */
146 current_catcher
->state
= CATCHER_RUNNING_1
;
149 current_catcher
->state
= CATCHER_ABORTING
;
150 /* See also throw_exception. */
153 internal_error (__FILE__
, __LINE__
, _("bad switch"));
155 case CATCHER_RUNNING_1
:
159 /* The did a "break" from the inner while loop. */
163 current_catcher
->state
= CATCHER_RUNNING
;
166 current_catcher
->state
= CATCHER_ABORTING
;
167 /* See also throw_exception. */
170 internal_error (__FILE__
, __LINE__
, _("bad switch"));
172 case CATCHER_ABORTING
:
177 struct gdb_exception exception
= *current_catcher
->exception
;
179 if (current_catcher
->mask
& RETURN_MASK (exception
.reason
))
181 /* Exit normally if this catcher can handle this
182 exception. The caller analyses the func return
187 /* The caller didn't request that the event be caught,
188 relay the event to the next containing
191 throw_exception (exception
);
194 internal_error (__FILE__
, __LINE__
, _("bad state"));
197 internal_error (__FILE__
, __LINE__
, _("bad switch"));
202 exceptions_state_mc_action_iter (void)
204 return exceptions_state_mc (CATCH_ITER
);
208 exceptions_state_mc_action_iter_1 (void)
210 return exceptions_state_mc (CATCH_ITER_1
);
213 /* Return EXCEPTION to the nearest containing catch_errors(). */
216 throw_exception (struct gdb_exception exception
)
218 struct thread_info
*tp
= NULL
;
223 if (!ptid_equal (inferior_ptid
, null_ptid
))
224 tp
= find_thread_ptid (inferior_ptid
);
226 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
227 I can think of a reason why that is vital, though). */
230 /* Clear queued breakpoint commands */
231 bpstat_clear_actions (tp
->control
.stop_bpstat
);
234 disable_current_display ();
235 do_cleanups (ALL_CLEANUPS
);
237 /* Jump to the containing catch_errors() call, communicating REASON
238 to that call via setjmp's return value. Note that REASON can't
239 be zero, by definition in defs.h. */
240 exceptions_state_mc (CATCH_THROWING
);
241 *current_catcher
->exception
= exception
;
242 EXCEPTIONS_SIGLONGJMP (current_catcher
->buf
, exception
.reason
);
245 static char *last_message
;
248 deprecated_throw_reason (enum return_reason reason
)
250 struct gdb_exception exception
;
252 memset (&exception
, 0, sizeof exception
);
254 exception
.reason
= reason
;
260 exception
.error
= GENERIC_ERROR
;
263 internal_error (__FILE__
, __LINE__
, _("bad switch"));
266 throw_exception (exception
);
272 struct serial
*gdb_stdout_serial
;
274 if (deprecated_error_begin_hook
)
275 deprecated_error_begin_hook ();
276 target_terminal_ours ();
278 /* We want all output to appear now, before we print the error. We
279 have 3 levels of buffering we have to flush (it's possible that
280 some of these should be changed to flush the lower-level ones
283 /* 1. The _filtered buffer. */
286 /* 2. The stdio buffer. */
287 gdb_flush (gdb_stdout
);
288 gdb_flush (gdb_stderr
);
290 /* 3. The system-level buffer. */
291 gdb_stdout_serial
= serial_fdopen (1);
292 if (gdb_stdout_serial
)
294 serial_drain_output (gdb_stdout_serial
);
295 serial_un_fdopen (gdb_stdout_serial
);
298 annotate_error_begin ();
302 print_exception (struct ui_file
*file
, struct gdb_exception e
)
304 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
305 as that way the MI's behavior is preserved. */
309 for (start
= e
.message
; start
!= NULL
; start
= end
)
311 end
= strchr (start
, '\n');
313 fputs_filtered (start
, file
);
317 ui_file_write (file
, start
, end
- start
);
320 fprintf_filtered (file
, "\n");
322 /* Now append the annotation. */
329 /* Assume that these are all errors. */
333 internal_error (__FILE__
, __LINE__
, _("Bad switch."));
338 exception_print (struct ui_file
*file
, struct gdb_exception e
)
340 if (e
.reason
< 0 && e
.message
!= NULL
)
343 print_exception (file
, e
);
348 exception_fprintf (struct ui_file
*file
, struct gdb_exception e
,
349 const char *prefix
, ...)
351 if (e
.reason
< 0 && e
.message
!= NULL
)
357 /* Print the prefix. */
358 va_start (args
, prefix
);
359 vfprintf_filtered (file
, prefix
, args
);
362 print_exception (file
, e
);
367 print_any_exception (struct ui_file
*file
, const char *prefix
,
368 struct gdb_exception e
)
370 if (e
.reason
< 0 && e
.message
!= NULL
)
372 target_terminal_ours ();
373 wrap_here (""); /* Force out any buffered output */
374 gdb_flush (gdb_stdout
);
375 annotate_error_begin ();
377 /* Print the prefix. */
378 if (prefix
!= NULL
&& prefix
[0] != '\0')
379 fputs_filtered (prefix
, file
);
380 print_exception (file
, e
);
384 static void ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (3, 0)
385 throw_it (enum return_reason reason
, enum errors error
, const char *fmt
,
388 struct gdb_exception e
;
391 /* Save the message. Create the new message before deleting the
392 old, the new message may include the old message text. */
393 new_message
= xstrvprintf (fmt
, ap
);
394 xfree (last_message
);
395 last_message
= new_message
;
397 /* Create the exception. */
400 e
.message
= last_message
;
402 /* Throw the exception. */
407 throw_verror (enum errors error
, const char *fmt
, va_list ap
)
409 throw_it (RETURN_ERROR
, error
, fmt
, ap
);
413 throw_vfatal (const char *fmt
, va_list ap
)
415 throw_it (RETURN_QUIT
, GDB_NO_ERROR
, fmt
, ap
);
419 throw_error (enum errors error
, const char *fmt
, ...)
423 va_start (args
, fmt
);
424 throw_it (RETURN_ERROR
, error
, fmt
, args
);
428 /* Call FUNC(UIOUT, FUNC_ARGS) but wrapped within an exception
429 handler. If an exception (enum return_reason) is thrown using
430 throw_exception() than all cleanups installed since
431 catch_exceptions() was entered are invoked, the (-ve) exception
432 value is then returned by catch_exceptions. If FUNC() returns
433 normally (with a positive or zero return value) then that value is
434 returned by catch_exceptions(). It is an internal_error() for
435 FUNC() to return a negative value.
437 See exceptions.h for further usage details.
439 Must not be called with immediate_quit in effect (bad things might
440 happen, say we got a signal in the middle of a memcpy to quit_return).
441 This is an OK restriction; with very few exceptions immediate_quit can
442 be replaced by judicious use of QUIT. */
444 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
445 error() et.al. could maintain a set of flags that indicate the the
446 current state of each of the longjmp buffers. This would give the
447 longjmp code the chance to detect a longjmp botch (before it gets
448 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
449 code also randomly used a SET_TOP_LEVEL macro that directly
450 initialize the longjmp buffers. */
453 catch_exceptions (struct ui_out
*uiout
,
454 catch_exceptions_ftype
*func
,
458 return catch_exceptions_with_msg (uiout
, func
, func_args
, NULL
, mask
);
462 catch_exception (struct ui_out
*uiout
,
463 catch_exception_ftype
*func
,
467 volatile struct gdb_exception exception
;
469 TRY_CATCH (exception
, mask
)
471 (*func
) (uiout
, func_args
);
477 catch_exceptions_with_msg (struct ui_out
*uiout
,
478 catch_exceptions_ftype
*func
,
483 volatile struct gdb_exception exception
;
484 volatile int val
= 0;
486 TRY_CATCH (exception
, mask
)
488 val
= (*func
) (uiout
, func_args
);
490 print_any_exception (gdb_stderr
, NULL
, exception
);
491 gdb_assert (val
>= 0);
492 gdb_assert (exception
.reason
<= 0);
493 if (exception
.reason
< 0)
495 /* If caller wants a copy of the low-level error message, make
496 one. This is used in the case of a silent error whereby the
497 caller may optionally want to issue the message. */
498 if (gdberrmsg
!= NULL
)
500 if (exception
.message
!= NULL
)
501 *gdberrmsg
= xstrdup (exception
.message
);
505 return exception
.reason
;
510 /* This function is superseded by catch_exceptions(). */
513 catch_errors (catch_errors_ftype
*func
, void *func_args
, char *errstring
,
516 volatile int val
= 0;
517 volatile struct gdb_exception exception
;
519 TRY_CATCH (exception
, mask
)
521 val
= func (func_args
);
523 print_any_exception (gdb_stderr
, errstring
, exception
);
524 if (exception
.reason
!= 0)
530 catch_command_errors (catch_command_errors_ftype
* command
,
531 char *arg
, int from_tty
, return_mask mask
)
533 volatile struct gdb_exception e
;
537 command (arg
, from_tty
);
539 print_any_exception (gdb_stderr
, NULL
, e
);