1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
3 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free
5 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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 #include "exceptions.h"
27 #include "breakpoint.h"
32 #include "gdb_assert.h"
33 #include "gdb_string.h"
36 const struct exception exception_none
= { 0, NO_ERROR
, NULL
};
38 /* One should use catch_errors rather than manipulating these
40 #if defined(HAVE_SIGSETJMP)
41 #define SIGJMP_BUF sigjmp_buf
42 #define SIGSETJMP(buf) sigsetjmp((buf), 1)
43 #define SIGLONGJMP(buf,val) siglongjmp((buf), (val))
45 #define SIGJMP_BUF jmp_buf
46 #define SIGSETJMP(buf) setjmp(buf)
47 #define SIGLONGJMP(buf,val) longjmp((buf), (val))
50 /* Possible catcher states. */
52 /* Initial state, a new catcher has just been created. */
54 /* The catch code is running. */
57 /* The catch code threw an exception. */
61 /* Possible catcher actions. */
70 enum catcher_state state
;
71 /* Jump buffer pointing back at the exception handler. */
73 /* Status buffer belonging to the exception handler. */
74 volatile struct exception
*exception
;
75 /* Saved/current state. */
77 struct ui_out
*saved_uiout
;
78 struct cleanup
*saved_cleanup_chain
;
83 /* Where to go for throw_exception(). */
84 static struct catcher
*current_catcher
;
87 catcher_init (struct ui_out
*func_uiout
,
88 volatile struct 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
= NO_ERROR
;
96 exception
->message
= NULL
;
97 new_catcher
->exception
= exception
;
99 new_catcher
->mask
= mask
;
101 /* Override the global ``struct ui_out'' builder. */
102 new_catcher
->saved_uiout
= uiout
;
105 /* Prevent error/quit during FUNC from calling cleanups established
107 new_catcher
->saved_cleanup_chain
= save_cleanups ();
109 /* Push this new catcher on the top. */
110 new_catcher
->prev
= current_catcher
;
111 current_catcher
= new_catcher
;
112 new_catcher
->state
= CATCHER_CREATED
;
114 return &new_catcher
->buf
;
120 struct catcher
*old_catcher
= current_catcher
;
121 current_catcher
= old_catcher
->prev
;
123 /* Restore the cleanup chain, the error/quit messages, and the uiout
124 builder, to their original states. */
126 restore_cleanups (old_catcher
->saved_cleanup_chain
);
128 uiout
= old_catcher
->saved_uiout
;
133 /* Catcher state machine. Returns non-zero if the m/c should be run
134 again, zero if it should abort. */
137 catcher_state_machine (enum catcher_action action
)
139 switch (current_catcher
->state
)
141 case CATCHER_CREATED
:
145 /* Allow the code to run the catcher. */
146 current_catcher
->state
= CATCHER_RUNNING
;
149 internal_error (__FILE__
, __LINE__
, "bad state");
151 case CATCHER_RUNNING
:
155 /* No error/quit has occured. Just clean up. */
159 current_catcher
->state
= CATCHER_RUNNING_1
;
162 current_catcher
->state
= CATCHER_ABORTING
;
163 /* See also throw_exception. */
166 internal_error (__FILE__
, __LINE__
, "bad switch");
168 case CATCHER_RUNNING_1
:
172 /* The did a "break" from the inner while loop. */
176 current_catcher
->state
= CATCHER_RUNNING
;
179 current_catcher
->state
= CATCHER_ABORTING
;
180 /* See also throw_exception. */
183 internal_error (__FILE__
, __LINE__
, "bad switch");
185 case CATCHER_ABORTING
:
190 struct exception exception
= *current_catcher
->exception
;
191 if (current_catcher
->mask
& RETURN_MASK (exception
.reason
))
193 /* Exit normally if this catcher can handle this
194 exception. The caller analyses the func return
199 /* The caller didn't request that the event be caught,
200 relay the event to the next containing
203 throw_exception (exception
);
206 internal_error (__FILE__
, __LINE__
, "bad state");
209 internal_error (__FILE__
, __LINE__
, "bad switch");
213 /* Return EXCEPTION to the nearest containing catch_errors(). */
216 throw_exception (struct exception exception
)
221 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
222 I can think of a reason why that is vital, though). */
223 bpstat_clear_actions (stop_bpstat
); /* Clear queued breakpoint commands */
225 disable_current_display ();
226 do_cleanups (ALL_CLEANUPS
);
227 if (target_can_async_p () && !target_executing
)
228 do_exec_cleanups (ALL_CLEANUPS
);
230 do_exec_error_cleanups (ALL_CLEANUPS
);
232 /* Jump to the containing catch_errors() call, communicating REASON
233 to that call via setjmp's return value. Note that REASON can't
234 be zero, by definition in defs.h. */
235 catcher_state_machine (CATCH_THROWING
);
236 *current_catcher
->exception
= exception
;
237 SIGLONGJMP (current_catcher
->buf
, exception
.reason
);
240 static char *last_message
;
243 deprecated_throw_reason (enum return_reason reason
)
245 struct exception exception
;
246 memset (&exception
, 0, sizeof exception
);
248 exception
.reason
= reason
;
254 exception
.error
= GENERIC_ERROR
;
257 internal_error (__FILE__
, __LINE__
, "bad switch");
260 throw_exception (exception
);
266 struct serial
*gdb_stdout_serial
;
268 if (deprecated_error_begin_hook
)
269 deprecated_error_begin_hook ();
270 target_terminal_ours ();
272 /* We want all output to appear now, before we print the error. We
273 have 3 levels of buffering we have to flush (it's possible that
274 some of these should be changed to flush the lower-level ones
277 /* 1. The _filtered buffer. */
280 /* 2. The stdio buffer. */
281 gdb_flush (gdb_stdout
);
282 gdb_flush (gdb_stderr
);
284 /* 3. The system-level buffer. */
285 gdb_stdout_serial
= serial_fdopen (1);
286 serial_drain_output (gdb_stdout_serial
);
287 serial_un_fdopen (gdb_stdout_serial
);
289 annotate_error_begin ();
293 print_exception (struct ui_file
*file
, struct exception e
)
295 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
296 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 exception e
)
330 if (e
.reason
< 0 && e
.message
!= NULL
)
333 print_exception (file
, e
);
338 exception_fprintf (struct ui_file
*file
, struct 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
,
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
);
375 throw_it (enum return_reason reason
, enum errors error
, const char *fmt
,
376 va_list ap
) ATTR_NORETURN
;
378 throw_it (enum return_reason reason
, enum errors error
, const char *fmt
,
384 /* Save the message. Create the new message before deleting the
385 old, the new message may include the old message text. */
386 new_message
= xstrvprintf (fmt
, ap
);
387 xfree (last_message
);
388 last_message
= new_message
;
390 /* Create the exception. */
393 e
.message
= last_message
;
395 /* Throw the exception. */
400 throw_verror (enum errors error
, const char *fmt
, va_list ap
)
402 throw_it (RETURN_ERROR
, error
, fmt
, ap
);
406 throw_vfatal (const char *fmt
, va_list ap
)
408 throw_it (RETURN_QUIT
, NO_ERROR
, fmt
, ap
);
412 throw_error (enum errors error
, const char *fmt
, ...)
415 va_start (args
, fmt
);
416 throw_it (RETURN_ERROR
, error
, fmt
, args
);
420 /* Call FUNC() with args FUNC_UIOUT and FUNC_ARGS, catching any
421 errors. Set FUNC_CAUGHT to an ``enum return_reason'' if the
422 function is aborted (using throw_exception() or zero if the
423 function returns normally. Set FUNC_VAL to the value returned by
424 the function or 0 if the function was aborted.
426 Must not be called with immediate_quit in effect (bad things might
427 happen, say we got a signal in the middle of a memcpy to quit_return).
428 This is an OK restriction; with very few exceptions immediate_quit can
429 be replaced by judicious use of QUIT.
431 MASK specifies what to catch; it is normally set to
432 RETURN_MASK_ALL, if for no other reason than that the code which
433 calls catch_errors might not be set up to deal with a quit which
434 isn't caught. But if the code can deal with it, it generally
435 should be RETURN_MASK_ERROR, unless for some reason it is more
436 useful to abort only the portion of the operation inside the
437 catch_errors. Note that quit should return to the command line
438 fairly quickly, even if some further processing is being done. */
440 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
441 error() et.al. could maintain a set of flags that indicate the the
442 current state of each of the longjmp buffers. This would give the
443 longjmp code the chance to detect a longjmp botch (before it gets
444 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
445 code also randomly used a SET_TOP_LEVEL macro that directly
446 initialize the longjmp buffers. */
448 /* MAYBE: cagney/1999-11-05: Should the catch_errors and cleanups code
449 be consolidated into a single file instead of being distributed
450 between utils.c and top.c? */
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 exception exception
;
469 catch = catcher_init (uiout
, &exception
, mask
);
470 for (SIGSETJMP ((*catch));
471 catcher_state_machine (CATCH_ITER
);)
472 (*func
) (uiout
, func_args
);
477 catch_exceptions_with_msg (struct ui_out
*uiout
,
478 catch_exceptions_ftype
*func
,
483 volatile struct exception exception
;
484 volatile int val
= 0;
485 SIGJMP_BUF
*catch = catcher_init (uiout
, &exception
, mask
);
486 for (SIGSETJMP ((*catch)); catcher_state_machine (CATCH_ITER
);)
487 val
= (*func
) (uiout
, func_args
);
488 print_any_exception (gdb_stderr
, NULL
, exception
);
489 gdb_assert (val
>= 0);
490 gdb_assert (exception
.reason
<= 0);
491 if (exception
.reason
< 0)
493 /* If caller wants a copy of the low-level error message, make
494 one. This is used in the case of a silent error whereby the
495 caller may optionally want to issue the message. */
496 if (gdberrmsg
!= NULL
)
498 if (exception
.message
!= NULL
)
499 *gdberrmsg
= xstrdup (exception
.message
);
503 return exception
.reason
;
509 catch_errors (catch_errors_ftype
*func
, void *func_args
, char *errstring
,
512 volatile int val
= 0;
513 volatile struct exception exception
;
514 SIGJMP_BUF
*catch = catcher_init (uiout
, &exception
, mask
);
515 /* This illustrates how it is possible to nest the mechanism and
516 hence catch "break". Of course this doesn't address the need to
517 also catch "return". */
518 for (SIGSETJMP ((*catch)); catcher_state_machine (CATCH_ITER
);)
519 val
= func (func_args
);
520 print_any_exception (gdb_stderr
, errstring
, exception
);
521 if (exception
.reason
!= 0)
527 catch_command_errors (catch_command_errors_ftype
* command
,
528 char *arg
, int from_tty
, return_mask mask
)
530 volatile struct exception e
;
531 SIGJMP_BUF
*catch = catcher_init (uiout
, &e
, mask
);
532 for (SIGSETJMP ((*catch)); catcher_state_machine (CATCH_ITER
);)
533 command (arg
, from_tty
);
534 print_any_exception (gdb_stderr
, NULL
, e
);