1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
3 Copyright (C) 1986-2014 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"
28 #include "gdbthread.h"
30 const struct gdb_exception exception_none
= { 0, GDB_NO_ERROR
, NULL
};
32 /* Possible catcher states. */
34 /* Initial state, a new catcher has just been created. */
36 /* The catch code is running. */
39 /* The catch code threw an exception. */
43 /* Possible catcher actions. */
52 enum catcher_state state
;
53 /* Jump buffer pointing back at the exception handler. */
55 /* Status buffer belonging to the exception handler. */
56 volatile struct gdb_exception
*exception
;
57 /* Saved/current state. */
59 struct cleanup
*saved_cleanup_chain
;
64 /* Where to go for throw_exception(). */
65 static struct catcher
*current_catcher
;
67 /* Return length of current_catcher list. */
70 catcher_list_size (void)
73 struct catcher
*catcher
;
75 for (size
= 0, catcher
= current_catcher
;
77 catcher
= catcher
->prev
)
84 exceptions_state_mc_init (volatile struct gdb_exception
*exception
,
87 struct catcher
*new_catcher
= XCNEW (struct catcher
);
89 /* Start with no exception, save it's address. */
90 exception
->reason
= 0;
91 exception
->error
= GDB_NO_ERROR
;
92 exception
->message
= NULL
;
93 new_catcher
->exception
= exception
;
95 new_catcher
->mask
= mask
;
97 /* Prevent error/quit during FUNC from calling cleanups established
99 new_catcher
->saved_cleanup_chain
= save_cleanups ();
101 /* Push this new catcher on the top. */
102 new_catcher
->prev
= current_catcher
;
103 current_catcher
= new_catcher
;
104 new_catcher
->state
= CATCHER_CREATED
;
106 return &new_catcher
->buf
;
112 struct catcher
*old_catcher
= current_catcher
;
114 current_catcher
= old_catcher
->prev
;
116 /* Restore the cleanup chain, the error/quit messages, and the uiout
117 builder, to their original states. */
119 restore_cleanups (old_catcher
->saved_cleanup_chain
);
124 /* Catcher state machine. Returns non-zero if the m/c should be run
125 again, zero if it should abort. */
128 exceptions_state_mc (enum catcher_action action
)
130 switch (current_catcher
->state
)
132 case CATCHER_CREATED
:
136 /* Allow the code to run the catcher. */
137 current_catcher
->state
= CATCHER_RUNNING
;
140 internal_error (__FILE__
, __LINE__
, _("bad state"));
142 case CATCHER_RUNNING
:
146 /* No error/quit has occured. Just clean up. */
150 current_catcher
->state
= CATCHER_RUNNING_1
;
153 current_catcher
->state
= CATCHER_ABORTING
;
154 /* See also throw_exception. */
157 internal_error (__FILE__
, __LINE__
, _("bad switch"));
159 case CATCHER_RUNNING_1
:
163 /* The did a "break" from the inner while loop. */
167 current_catcher
->state
= CATCHER_RUNNING
;
170 current_catcher
->state
= CATCHER_ABORTING
;
171 /* See also throw_exception. */
174 internal_error (__FILE__
, __LINE__
, _("bad switch"));
176 case CATCHER_ABORTING
:
181 struct gdb_exception exception
= *current_catcher
->exception
;
183 if (current_catcher
->mask
& RETURN_MASK (exception
.reason
))
185 /* Exit normally if this catcher can handle this
186 exception. The caller analyses the func return
191 /* The caller didn't request that the event be caught,
192 relay the event to the next containing
195 throw_exception (exception
);
198 internal_error (__FILE__
, __LINE__
, _("bad state"));
201 internal_error (__FILE__
, __LINE__
, _("bad switch"));
206 exceptions_state_mc_action_iter (void)
208 return exceptions_state_mc (CATCH_ITER
);
212 exceptions_state_mc_action_iter_1 (void)
214 return exceptions_state_mc (CATCH_ITER_1
);
217 /* Return EXCEPTION to the nearest containing catch_errors(). */
220 throw_exception (struct gdb_exception exception
)
225 do_cleanups (all_cleanups ());
227 /* Jump to the containing catch_errors() call, communicating REASON
228 to that call via setjmp's return value. Note that REASON can't
229 be zero, by definition in defs.h. */
230 exceptions_state_mc (CATCH_THROWING
);
231 *current_catcher
->exception
= exception
;
232 SIGLONGJMP (current_catcher
->buf
, exception
.reason
);
238 struct serial
*gdb_stdout_serial
;
240 if (deprecated_error_begin_hook
)
241 deprecated_error_begin_hook ();
243 if (target_supports_terminal_ours ())
244 target_terminal_ours ();
246 /* We want all output to appear now, before we print the error. We
247 have 3 levels of buffering we have to flush (it's possible that
248 some of these should be changed to flush the lower-level ones
251 /* 1. The _filtered buffer. */
252 if (filtered_printing_initialized ())
255 /* 2. The stdio buffer. */
256 gdb_flush (gdb_stdout
);
257 gdb_flush (gdb_stderr
);
259 /* 3. The system-level buffer. */
260 gdb_stdout_serial
= serial_fdopen (1);
261 if (gdb_stdout_serial
)
263 serial_drain_output (gdb_stdout_serial
);
264 serial_un_fdopen (gdb_stdout_serial
);
267 annotate_error_begin ();
271 print_exception (struct ui_file
*file
, struct gdb_exception e
)
273 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
274 as that way the MI's behavior is preserved. */
278 for (start
= e
.message
; start
!= NULL
; start
= end
)
280 end
= strchr (start
, '\n');
282 fputs_filtered (start
, file
);
286 ui_file_write (file
, start
, end
- start
);
289 fprintf_filtered (file
, "\n");
291 /* Now append the annotation. */
298 /* Assume that these are all errors. */
302 internal_error (__FILE__
, __LINE__
, _("Bad switch."));
307 exception_print (struct ui_file
*file
, struct gdb_exception e
)
309 if (e
.reason
< 0 && e
.message
!= NULL
)
312 print_exception (file
, e
);
317 exception_fprintf (struct ui_file
*file
, struct gdb_exception e
,
318 const char *prefix
, ...)
320 if (e
.reason
< 0 && e
.message
!= NULL
)
326 /* Print the prefix. */
327 va_start (args
, prefix
);
328 vfprintf_filtered (file
, prefix
, args
);
331 print_exception (file
, e
);
335 /* A stack of exception messages.
336 This is needed to handle nested calls to throw_it: we don't want to
337 xfree space for a message before it's used.
338 This can happen if we throw an exception during a cleanup:
339 An outer TRY_CATCH may have an exception message it wants to print,
340 but while doing cleanups further calls to throw_it are made.
342 This is indexed by the size of the current_catcher list.
343 It is a dynamically allocated array so that we don't care how deeply
344 GDB nests its TRY_CATCHs. */
345 static char **exception_messages
;
347 /* The number of currently allocated entries in exception_messages. */
348 static int exception_messages_size
;
350 static void ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (3, 0)
351 throw_it (enum return_reason reason
, enum errors error
, const char *fmt
,
354 struct gdb_exception e
;
356 int depth
= catcher_list_size ();
358 gdb_assert (depth
> 0);
360 /* Note: The new message may use an old message's text. */
361 new_message
= xstrvprintf (fmt
, ap
);
363 if (depth
> exception_messages_size
)
365 int old_size
= exception_messages_size
;
367 exception_messages_size
= depth
+ 10;
368 exception_messages
= (char **) xrealloc (exception_messages
,
369 exception_messages_size
371 memset (exception_messages
+ old_size
, 0,
372 (exception_messages_size
- old_size
) * sizeof (char *));
375 xfree (exception_messages
[depth
- 1]);
376 exception_messages
[depth
- 1] = new_message
;
378 /* Create the exception. */
381 e
.message
= new_message
;
383 /* Throw the exception. */
388 throw_verror (enum errors error
, const char *fmt
, va_list ap
)
390 throw_it (RETURN_ERROR
, error
, fmt
, ap
);
394 throw_vquit (const char *fmt
, va_list ap
)
396 throw_it (RETURN_QUIT
, GDB_NO_ERROR
, fmt
, ap
);
400 throw_error (enum errors error
, const char *fmt
, ...)
404 va_start (args
, fmt
);
405 throw_verror (error
, fmt
, args
);
410 throw_quit (const char *fmt
, ...)
414 va_start (args
, fmt
);
415 throw_vquit (fmt
, args
);
419 /* Call FUNC(UIOUT, FUNC_ARGS) but wrapped within an exception
420 handler. If an exception (enum return_reason) is thrown using
421 throw_exception() than all cleanups installed since
422 catch_exceptions() was entered are invoked, the (-ve) exception
423 value is then returned by catch_exceptions. If FUNC() returns
424 normally (with a positive or zero return value) then that value is
425 returned by catch_exceptions(). It is an internal_error() for
426 FUNC() to return a negative value.
428 See exceptions.h for further usage details.
430 Must not be called with immediate_quit in effect (bad things might
431 happen, say we got a signal in the middle of a memcpy to quit_return).
432 This is an OK restriction; with very few exceptions immediate_quit can
433 be replaced by judicious use of QUIT. */
435 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
436 error() et al. could maintain a set of flags that indicate the
437 current state of each of the longjmp buffers. This would give the
438 longjmp code the chance to detect a longjmp botch (before it gets
439 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
440 code also randomly used a SET_TOP_LEVEL macro that directly
441 initialized the longjmp buffers. */
444 catch_exceptions (struct ui_out
*uiout
,
445 catch_exceptions_ftype
*func
,
449 return catch_exceptions_with_msg (uiout
, func
, func_args
, NULL
, mask
);
453 catch_exceptions_with_msg (struct ui_out
*func_uiout
,
454 catch_exceptions_ftype
*func
,
459 volatile struct gdb_exception exception
;
460 volatile int val
= 0;
461 struct ui_out
*saved_uiout
;
463 /* Save and override the global ``struct ui_out'' builder. */
464 saved_uiout
= current_uiout
;
465 current_uiout
= func_uiout
;
467 TRY_CATCH (exception
, RETURN_MASK_ALL
)
469 val
= (*func
) (current_uiout
, func_args
);
472 /* Restore the global builder. */
473 current_uiout
= saved_uiout
;
475 if (exception
.reason
< 0 && (mask
& RETURN_MASK (exception
.reason
)) == 0)
477 /* The caller didn't request that the event be caught.
479 throw_exception (exception
);
482 exception_print (gdb_stderr
, exception
);
483 gdb_assert (val
>= 0);
484 gdb_assert (exception
.reason
<= 0);
485 if (exception
.reason
< 0)
487 /* If caller wants a copy of the low-level error message, make
488 one. This is used in the case of a silent error whereby the
489 caller may optionally want to issue the message. */
490 if (gdberrmsg
!= NULL
)
492 if (exception
.message
!= NULL
)
493 *gdberrmsg
= xstrdup (exception
.message
);
497 return exception
.reason
;
502 /* This function is superseded by catch_exceptions(). */
505 catch_errors (catch_errors_ftype
*func
, void *func_args
, char *errstring
,
508 volatile int val
= 0;
509 volatile struct gdb_exception exception
;
510 struct ui_out
*saved_uiout
;
512 /* Save the global ``struct ui_out'' builder. */
513 saved_uiout
= current_uiout
;
515 TRY_CATCH (exception
, RETURN_MASK_ALL
)
517 val
= func (func_args
);
520 /* Restore the global builder. */
521 current_uiout
= saved_uiout
;
523 if (exception
.reason
< 0 && (mask
& RETURN_MASK (exception
.reason
)) == 0)
525 /* The caller didn't request that the event be caught.
527 throw_exception (exception
);
530 exception_fprintf (gdb_stderr
, exception
, "%s", errstring
);
531 if (exception
.reason
!= 0)