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 /* Return length of current_catcher list. */
74 catcher_list_size (void)
77 struct catcher
*catcher
;
79 for (size
= 0, catcher
= current_catcher
;
81 catcher
= catcher
->prev
)
87 EXCEPTIONS_SIGJMP_BUF
*
88 exceptions_state_mc_init (volatile struct gdb_exception
*exception
,
91 struct catcher
*new_catcher
= XZALLOC (struct catcher
);
93 /* Start with no exception, save it's address. */
94 exception
->reason
= 0;
95 exception
->error
= GDB_NO_ERROR
;
96 exception
->message
= NULL
;
97 new_catcher
->exception
= exception
;
99 new_catcher
->mask
= mask
;
101 /* Prevent error/quit during FUNC from calling cleanups established
103 new_catcher
->saved_cleanup_chain
= save_cleanups ();
105 /* Push this new catcher on the top. */
106 new_catcher
->prev
= current_catcher
;
107 current_catcher
= new_catcher
;
108 new_catcher
->state
= CATCHER_CREATED
;
110 return &new_catcher
->buf
;
116 struct catcher
*old_catcher
= current_catcher
;
118 current_catcher
= old_catcher
->prev
;
120 /* Restore the cleanup chain, the error/quit messages, and the uiout
121 builder, to their original states. */
123 restore_cleanups (old_catcher
->saved_cleanup_chain
);
128 /* Catcher state machine. Returns non-zero if the m/c should be run
129 again, zero if it should abort. */
132 exceptions_state_mc (enum catcher_action action
)
134 switch (current_catcher
->state
)
136 case CATCHER_CREATED
:
140 /* Allow the code to run the catcher. */
141 current_catcher
->state
= CATCHER_RUNNING
;
144 internal_error (__FILE__
, __LINE__
, _("bad state"));
146 case CATCHER_RUNNING
:
150 /* No error/quit has occured. Just clean up. */
154 current_catcher
->state
= CATCHER_RUNNING_1
;
157 current_catcher
->state
= CATCHER_ABORTING
;
158 /* See also throw_exception. */
161 internal_error (__FILE__
, __LINE__
, _("bad switch"));
163 case CATCHER_RUNNING_1
:
167 /* The did a "break" from the inner while loop. */
171 current_catcher
->state
= CATCHER_RUNNING
;
174 current_catcher
->state
= CATCHER_ABORTING
;
175 /* See also throw_exception. */
178 internal_error (__FILE__
, __LINE__
, _("bad switch"));
180 case CATCHER_ABORTING
:
185 struct gdb_exception exception
= *current_catcher
->exception
;
187 if (current_catcher
->mask
& RETURN_MASK (exception
.reason
))
189 /* Exit normally if this catcher can handle this
190 exception. The caller analyses the func return
195 /* The caller didn't request that the event be caught,
196 relay the event to the next containing
199 throw_exception (exception
);
202 internal_error (__FILE__
, __LINE__
, _("bad state"));
205 internal_error (__FILE__
, __LINE__
, _("bad switch"));
210 exceptions_state_mc_action_iter (void)
212 return exceptions_state_mc (CATCH_ITER
);
216 exceptions_state_mc_action_iter_1 (void)
218 return exceptions_state_mc (CATCH_ITER_1
);
221 /* Return EXCEPTION to the nearest containing catch_errors(). */
224 throw_exception (struct gdb_exception exception
)
229 do_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 exceptions_state_mc (CATCH_THROWING
);
235 *current_catcher
->exception
= exception
;
236 EXCEPTIONS_SIGLONGJMP (current_catcher
->buf
, exception
.reason
);
240 deprecated_throw_reason (enum return_reason reason
)
242 struct gdb_exception exception
;
244 memset (&exception
, 0, sizeof exception
);
246 exception
.reason
= reason
;
252 exception
.error
= GENERIC_ERROR
;
255 internal_error (__FILE__
, __LINE__
, _("bad switch"));
258 throw_exception (exception
);
264 struct serial
*gdb_stdout_serial
;
266 if (deprecated_error_begin_hook
)
267 deprecated_error_begin_hook ();
268 target_terminal_ours ();
270 /* We want all output to appear now, before we print the error. We
271 have 3 levels of buffering we have to flush (it's possible that
272 some of these should be changed to flush the lower-level ones
275 /* 1. The _filtered buffer. */
278 /* 2. The stdio buffer. */
279 gdb_flush (gdb_stdout
);
280 gdb_flush (gdb_stderr
);
282 /* 3. The system-level buffer. */
283 gdb_stdout_serial
= serial_fdopen (1);
284 if (gdb_stdout_serial
)
286 serial_drain_output (gdb_stdout_serial
);
287 serial_un_fdopen (gdb_stdout_serial
);
290 annotate_error_begin ();
294 print_exception (struct ui_file
*file
, struct gdb_exception e
)
296 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
297 as that way the MI's behavior is preserved. */
301 for (start
= e
.message
; start
!= NULL
; start
= end
)
303 end
= strchr (start
, '\n');
305 fputs_filtered (start
, file
);
309 ui_file_write (file
, start
, end
- start
);
312 fprintf_filtered (file
, "\n");
314 /* Now append the annotation. */
321 /* Assume that these are all errors. */
325 internal_error (__FILE__
, __LINE__
, _("Bad switch."));
330 exception_print (struct ui_file
*file
, struct gdb_exception e
)
332 if (e
.reason
< 0 && e
.message
!= NULL
)
335 print_exception (file
, e
);
340 exception_fprintf (struct ui_file
*file
, struct gdb_exception e
,
341 const char *prefix
, ...)
343 if (e
.reason
< 0 && e
.message
!= NULL
)
349 /* Print the prefix. */
350 va_start (args
, prefix
);
351 vfprintf_filtered (file
, prefix
, args
);
354 print_exception (file
, e
);
359 print_any_exception (struct ui_file
*file
, const char *prefix
,
360 struct gdb_exception e
)
362 if (e
.reason
< 0 && e
.message
!= NULL
)
364 target_terminal_ours ();
365 wrap_here (""); /* Force out any buffered output. */
366 gdb_flush (gdb_stdout
);
367 annotate_error_begin ();
369 /* Print the prefix. */
370 if (prefix
!= NULL
&& prefix
[0] != '\0')
371 fputs_filtered (prefix
, file
);
372 print_exception (file
, e
);
376 /* A stack of exception messages.
377 This is needed to handle nested calls to throw_it: we don't want to
378 xfree space for a message before it's used.
379 This can happen if we throw an exception during a cleanup:
380 An outer TRY_CATCH may have an exception message it wants to print,
381 but while doing cleanups further calls to throw_it are made.
383 This is indexed by the size of the current_catcher list.
384 It is a dynamically allocated array so that we don't care how deeply
385 GDB nests its TRY_CATCHs. */
386 static char **exception_messages
;
388 /* The number of currently allocated entries in exception_messages. */
389 static int exception_messages_size
;
391 static void ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (3, 0)
392 throw_it (enum return_reason reason
, enum errors error
, const char *fmt
,
395 struct gdb_exception e
;
397 int depth
= catcher_list_size ();
399 gdb_assert (depth
> 0);
401 /* Note: The new message may use an old message's text. */
402 new_message
= xstrvprintf (fmt
, ap
);
404 if (depth
> exception_messages_size
)
406 int old_size
= exception_messages_size
;
408 exception_messages_size
= depth
+ 10;
409 exception_messages
= (char **) xrealloc (exception_messages
,
410 exception_messages_size
412 memset (exception_messages
+ old_size
, 0,
413 (exception_messages_size
- old_size
) * sizeof (char *));
416 xfree (exception_messages
[depth
- 1]);
417 exception_messages
[depth
- 1] = new_message
;
419 /* Create the exception. */
422 e
.message
= new_message
;
424 /* Throw the exception. */
429 throw_verror (enum errors error
, const char *fmt
, va_list ap
)
431 throw_it (RETURN_ERROR
, error
, fmt
, ap
);
435 throw_vfatal (const char *fmt
, va_list ap
)
437 throw_it (RETURN_QUIT
, GDB_NO_ERROR
, fmt
, ap
);
441 throw_error (enum errors error
, const char *fmt
, ...)
445 va_start (args
, fmt
);
446 throw_it (RETURN_ERROR
, error
, fmt
, args
);
450 /* Call FUNC(UIOUT, FUNC_ARGS) but wrapped within an exception
451 handler. If an exception (enum return_reason) is thrown using
452 throw_exception() than all cleanups installed since
453 catch_exceptions() was entered are invoked, the (-ve) exception
454 value is then returned by catch_exceptions. If FUNC() returns
455 normally (with a positive or zero return value) then that value is
456 returned by catch_exceptions(). It is an internal_error() for
457 FUNC() to return a negative value.
459 See exceptions.h for further usage details.
461 Must not be called with immediate_quit in effect (bad things might
462 happen, say we got a signal in the middle of a memcpy to quit_return).
463 This is an OK restriction; with very few exceptions immediate_quit can
464 be replaced by judicious use of QUIT. */
466 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
467 error() et al. could maintain a set of flags that indicate the
468 current state of each of the longjmp buffers. This would give the
469 longjmp code the chance to detect a longjmp botch (before it gets
470 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
471 code also randomly used a SET_TOP_LEVEL macro that directly
472 initialized the longjmp buffers. */
475 catch_exceptions (struct ui_out
*uiout
,
476 catch_exceptions_ftype
*func
,
480 return catch_exceptions_with_msg (uiout
, func
, func_args
, NULL
, mask
);
484 catch_exceptions_with_msg (struct ui_out
*func_uiout
,
485 catch_exceptions_ftype
*func
,
490 volatile struct gdb_exception exception
;
491 volatile int val
= 0;
492 struct ui_out
*saved_uiout
;
494 /* Save and override the global ``struct ui_out'' builder. */
495 saved_uiout
= current_uiout
;
496 current_uiout
= func_uiout
;
498 TRY_CATCH (exception
, RETURN_MASK_ALL
)
500 val
= (*func
) (current_uiout
, func_args
);
503 /* Restore the global builder. */
504 current_uiout
= saved_uiout
;
506 if (exception
.reason
< 0 && (mask
& RETURN_MASK (exception
.reason
)) == 0)
508 /* The caller didn't request that the event be caught.
510 throw_exception (exception
);
513 print_any_exception (gdb_stderr
, NULL
, exception
);
514 gdb_assert (val
>= 0);
515 gdb_assert (exception
.reason
<= 0);
516 if (exception
.reason
< 0)
518 /* If caller wants a copy of the low-level error message, make
519 one. This is used in the case of a silent error whereby the
520 caller may optionally want to issue the message. */
521 if (gdberrmsg
!= NULL
)
523 if (exception
.message
!= NULL
)
524 *gdberrmsg
= xstrdup (exception
.message
);
528 return exception
.reason
;
533 /* This function is superseded by catch_exceptions(). */
536 catch_errors (catch_errors_ftype
*func
, void *func_args
, char *errstring
,
539 volatile int val
= 0;
540 volatile struct gdb_exception exception
;
541 struct ui_out
*saved_uiout
;
543 /* Save the global ``struct ui_out'' builder. */
544 saved_uiout
= current_uiout
;
546 TRY_CATCH (exception
, RETURN_MASK_ALL
)
548 val
= func (func_args
);
551 /* Restore the global builder. */
552 current_uiout
= saved_uiout
;
554 if (exception
.reason
< 0 && (mask
& RETURN_MASK (exception
.reason
)) == 0)
556 /* The caller didn't request that the event be caught.
558 throw_exception (exception
);
561 print_any_exception (gdb_stderr
, errstring
, exception
);
562 if (exception
.reason
!= 0)
568 catch_command_errors (catch_command_errors_ftype
* command
,
569 char *arg
, int from_tty
, return_mask mask
)
571 volatile struct gdb_exception e
;
575 command (arg
, from_tty
);
577 print_any_exception (gdb_stderr
, NULL
, e
);