1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988-2012 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "exceptions.h"
22 #include "breakpoint.h"
27 #include "gdb_assert.h"
28 #include "gdb_string.h"
30 #include "gdbthread.h"
32 const struct gdb_exception exception_none
= { 0, GDB_NO_ERROR
, NULL
};
34 /* Possible catcher states. */
36 /* Initial state, a new catcher has just been created. */
38 /* The catch code is running. */
41 /* The catch code threw an exception. */
45 /* Possible catcher actions. */
54 enum catcher_state state
;
55 /* Jump buffer pointing back at the exception handler. */
56 EXCEPTIONS_SIGJMP_BUF buf
;
57 /* Status buffer belonging to the exception handler. */
58 volatile struct gdb_exception
*exception
;
59 /* Saved/current state. */
61 struct cleanup
*saved_cleanup_chain
;
66 /* Where to go for throw_exception(). */
67 static struct catcher
*current_catcher
;
69 /* Return length of current_catcher list. */
72 catcher_list_size (void)
75 struct catcher
*catcher
;
77 for (size
= 0, catcher
= current_catcher
;
79 catcher
= catcher
->prev
)
85 EXCEPTIONS_SIGJMP_BUF
*
86 exceptions_state_mc_init (volatile struct gdb_exception
*exception
,
89 struct catcher
*new_catcher
= XZALLOC (struct catcher
);
91 /* Start with no exception, save it's address. */
92 exception
->reason
= 0;
93 exception
->error
= GDB_NO_ERROR
;
94 exception
->message
= NULL
;
95 new_catcher
->exception
= exception
;
97 new_catcher
->mask
= mask
;
99 /* Prevent error/quit during FUNC from calling cleanups established
101 new_catcher
->saved_cleanup_chain
= save_cleanups ();
103 /* Push this new catcher on the top. */
104 new_catcher
->prev
= current_catcher
;
105 current_catcher
= new_catcher
;
106 new_catcher
->state
= CATCHER_CREATED
;
108 return &new_catcher
->buf
;
114 struct catcher
*old_catcher
= current_catcher
;
116 current_catcher
= old_catcher
->prev
;
118 /* Restore the cleanup chain, the error/quit messages, and the uiout
119 builder, to their original states. */
121 restore_cleanups (old_catcher
->saved_cleanup_chain
);
126 /* Catcher state machine. Returns non-zero if the m/c should be run
127 again, zero if it should abort. */
130 exceptions_state_mc (enum catcher_action action
)
132 switch (current_catcher
->state
)
134 case CATCHER_CREATED
:
138 /* Allow the code to run the catcher. */
139 current_catcher
->state
= CATCHER_RUNNING
;
142 internal_error (__FILE__
, __LINE__
, _("bad state"));
144 case CATCHER_RUNNING
:
148 /* No error/quit has occured. Just clean up. */
152 current_catcher
->state
= CATCHER_RUNNING_1
;
155 current_catcher
->state
= CATCHER_ABORTING
;
156 /* See also throw_exception. */
159 internal_error (__FILE__
, __LINE__
, _("bad switch"));
161 case CATCHER_RUNNING_1
:
165 /* The did a "break" from the inner while loop. */
169 current_catcher
->state
= CATCHER_RUNNING
;
172 current_catcher
->state
= CATCHER_ABORTING
;
173 /* See also throw_exception. */
176 internal_error (__FILE__
, __LINE__
, _("bad switch"));
178 case CATCHER_ABORTING
:
183 struct gdb_exception exception
= *current_catcher
->exception
;
185 if (current_catcher
->mask
& RETURN_MASK (exception
.reason
))
187 /* Exit normally if this catcher can handle this
188 exception. The caller analyses the func return
193 /* The caller didn't request that the event be caught,
194 relay the event to the next containing
197 throw_exception (exception
);
200 internal_error (__FILE__
, __LINE__
, _("bad state"));
203 internal_error (__FILE__
, __LINE__
, _("bad switch"));
208 exceptions_state_mc_action_iter (void)
210 return exceptions_state_mc (CATCH_ITER
);
214 exceptions_state_mc_action_iter_1 (void)
216 return exceptions_state_mc (CATCH_ITER_1
);
219 /* Return EXCEPTION to the nearest containing catch_errors(). */
222 throw_exception (struct gdb_exception exception
)
227 do_cleanups (all_cleanups ());
229 /* Jump to the containing catch_errors() call, communicating REASON
230 to that call via setjmp's return value. Note that REASON can't
231 be zero, by definition in defs.h. */
232 exceptions_state_mc (CATCH_THROWING
);
233 *current_catcher
->exception
= exception
;
234 EXCEPTIONS_SIGLONGJMP (current_catcher
->buf
, exception
.reason
);
238 deprecated_throw_reason (enum return_reason reason
)
240 struct gdb_exception exception
;
242 memset (&exception
, 0, sizeof exception
);
244 exception
.reason
= reason
;
250 exception
.error
= GENERIC_ERROR
;
253 internal_error (__FILE__
, __LINE__
, _("bad switch"));
256 throw_exception (exception
);
262 struct serial
*gdb_stdout_serial
;
264 if (deprecated_error_begin_hook
)
265 deprecated_error_begin_hook ();
266 target_terminal_ours ();
268 /* We want all output to appear now, before we print the error. We
269 have 3 levels of buffering we have to flush (it's possible that
270 some of these should be changed to flush the lower-level ones
273 /* 1. The _filtered buffer. */
276 /* 2. The stdio buffer. */
277 gdb_flush (gdb_stdout
);
278 gdb_flush (gdb_stderr
);
280 /* 3. The system-level buffer. */
281 gdb_stdout_serial
= serial_fdopen (1);
282 if (gdb_stdout_serial
)
284 serial_drain_output (gdb_stdout_serial
);
285 serial_un_fdopen (gdb_stdout_serial
);
288 annotate_error_begin ();
292 print_exception (struct ui_file
*file
, struct gdb_exception e
)
294 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
295 as that way the MI's behavior is preserved. */
299 for (start
= e
.message
; start
!= NULL
; start
= end
)
301 end
= strchr (start
, '\n');
303 fputs_filtered (start
, file
);
307 ui_file_write (file
, start
, end
- start
);
310 fprintf_filtered (file
, "\n");
312 /* Now append the annotation. */
319 /* Assume that these are all errors. */
323 internal_error (__FILE__
, __LINE__
, _("Bad switch."));
328 exception_print (struct ui_file
*file
, struct gdb_exception e
)
330 if (e
.reason
< 0 && e
.message
!= NULL
)
333 print_exception (file
, e
);
338 exception_fprintf (struct ui_file
*file
, struct gdb_exception e
,
339 const char *prefix
, ...)
341 if (e
.reason
< 0 && e
.message
!= NULL
)
347 /* Print the prefix. */
348 va_start (args
, prefix
);
349 vfprintf_filtered (file
, prefix
, args
);
352 print_exception (file
, e
);
357 print_any_exception (struct ui_file
*file
, const char *prefix
,
358 struct gdb_exception e
)
360 if (e
.reason
< 0 && e
.message
!= NULL
)
362 target_terminal_ours ();
363 wrap_here (""); /* Force out any buffered output. */
364 gdb_flush (gdb_stdout
);
365 annotate_error_begin ();
367 /* Print the prefix. */
368 if (prefix
!= NULL
&& prefix
[0] != '\0')
369 fputs_filtered (prefix
, file
);
370 print_exception (file
, e
);
374 /* A stack of exception messages.
375 This is needed to handle nested calls to throw_it: we don't want to
376 xfree space for a message before it's used.
377 This can happen if we throw an exception during a cleanup:
378 An outer TRY_CATCH may have an exception message it wants to print,
379 but while doing cleanups further calls to throw_it are made.
381 This is indexed by the size of the current_catcher list.
382 It is a dynamically allocated array so that we don't care how deeply
383 GDB nests its TRY_CATCHs. */
384 static char **exception_messages
;
386 /* The number of currently allocated entries in exception_messages. */
387 static int exception_messages_size
;
389 static void ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (3, 0)
390 throw_it (enum return_reason reason
, enum errors error
, const char *fmt
,
393 struct gdb_exception e
;
395 int depth
= catcher_list_size ();
397 gdb_assert (depth
> 0);
399 /* Note: The new message may use an old message's text. */
400 new_message
= xstrvprintf (fmt
, ap
);
402 if (depth
> exception_messages_size
)
404 int old_size
= exception_messages_size
;
406 exception_messages_size
= depth
+ 10;
407 exception_messages
= (char **) xrealloc (exception_messages
,
408 exception_messages_size
410 memset (exception_messages
+ old_size
, 0,
411 (exception_messages_size
- old_size
) * sizeof (char *));
414 xfree (exception_messages
[depth
- 1]);
415 exception_messages
[depth
- 1] = new_message
;
417 /* Create the exception. */
420 e
.message
= new_message
;
422 /* Throw the exception. */
427 throw_verror (enum errors error
, const char *fmt
, va_list ap
)
429 throw_it (RETURN_ERROR
, error
, fmt
, ap
);
433 throw_vfatal (const char *fmt
, va_list ap
)
435 throw_it (RETURN_QUIT
, GDB_NO_ERROR
, fmt
, ap
);
439 throw_error (enum errors error
, const char *fmt
, ...)
443 va_start (args
, fmt
);
444 throw_it (RETURN_ERROR
, error
, fmt
, args
);
448 /* Call FUNC(UIOUT, FUNC_ARGS) but wrapped within an exception
449 handler. If an exception (enum return_reason) is thrown using
450 throw_exception() than all cleanups installed since
451 catch_exceptions() was entered are invoked, the (-ve) exception
452 value is then returned by catch_exceptions. If FUNC() returns
453 normally (with a positive or zero return value) then that value is
454 returned by catch_exceptions(). It is an internal_error() for
455 FUNC() to return a negative value.
457 See exceptions.h for further usage details.
459 Must not be called with immediate_quit in effect (bad things might
460 happen, say we got a signal in the middle of a memcpy to quit_return).
461 This is an OK restriction; with very few exceptions immediate_quit can
462 be replaced by judicious use of QUIT. */
464 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
465 error() et al. could maintain a set of flags that indicate the
466 current state of each of the longjmp buffers. This would give the
467 longjmp code the chance to detect a longjmp botch (before it gets
468 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
469 code also randomly used a SET_TOP_LEVEL macro that directly
470 initialized the longjmp buffers. */
473 catch_exceptions (struct ui_out
*uiout
,
474 catch_exceptions_ftype
*func
,
478 return catch_exceptions_with_msg (uiout
, func
, func_args
, NULL
, mask
);
482 catch_exceptions_with_msg (struct ui_out
*func_uiout
,
483 catch_exceptions_ftype
*func
,
488 volatile struct gdb_exception exception
;
489 volatile int val
= 0;
490 struct ui_out
*saved_uiout
;
492 /* Save and override the global ``struct ui_out'' builder. */
493 saved_uiout
= current_uiout
;
494 current_uiout
= func_uiout
;
496 TRY_CATCH (exception
, RETURN_MASK_ALL
)
498 val
= (*func
) (current_uiout
, func_args
);
501 /* Restore the global builder. */
502 current_uiout
= saved_uiout
;
504 if (exception
.reason
< 0 && (mask
& RETURN_MASK (exception
.reason
)) == 0)
506 /* The caller didn't request that the event be caught.
508 throw_exception (exception
);
511 print_any_exception (gdb_stderr
, NULL
, exception
);
512 gdb_assert (val
>= 0);
513 gdb_assert (exception
.reason
<= 0);
514 if (exception
.reason
< 0)
516 /* If caller wants a copy of the low-level error message, make
517 one. This is used in the case of a silent error whereby the
518 caller may optionally want to issue the message. */
519 if (gdberrmsg
!= NULL
)
521 if (exception
.message
!= NULL
)
522 *gdberrmsg
= xstrdup (exception
.message
);
526 return exception
.reason
;
531 /* This function is superseded by catch_exceptions(). */
534 catch_errors (catch_errors_ftype
*func
, void *func_args
, char *errstring
,
537 volatile int val
= 0;
538 volatile struct gdb_exception exception
;
539 struct ui_out
*saved_uiout
;
541 /* Save the global ``struct ui_out'' builder. */
542 saved_uiout
= current_uiout
;
544 TRY_CATCH (exception
, RETURN_MASK_ALL
)
546 val
= func (func_args
);
549 /* Restore the global builder. */
550 current_uiout
= saved_uiout
;
552 if (exception
.reason
< 0 && (mask
& RETURN_MASK (exception
.reason
)) == 0)
554 /* The caller didn't request that the event be caught.
556 throw_exception (exception
);
559 print_any_exception (gdb_stderr
, errstring
, exception
);
560 if (exception
.reason
!= 0)
566 catch_command_errors (catch_command_errors_ftype
* command
,
567 char *arg
, int from_tty
, return_mask mask
)
569 volatile struct gdb_exception e
;
573 command (arg
, from_tty
);
575 print_any_exception (gdb_stderr
, NULL
, e
);