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 Free Software
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"
35 /* One should use catch_errors rather than manipulating these
37 #if defined(HAVE_SIGSETJMP)
38 #define SIGJMP_BUF sigjmp_buf
39 #define SIGSETJMP(buf) sigsetjmp((buf), 1)
40 #define SIGLONGJMP(buf,val) siglongjmp((buf), (val))
42 #define SIGJMP_BUF jmp_buf
43 #define SIGSETJMP(buf) setjmp(buf)
44 #define SIGLONGJMP(buf,val) longjmp((buf), (val))
47 /* Possible catcher states. */
49 /* Initial state, a new catcher has just been created. */
51 /* The catch code is running. */
54 /* The catch code threw an exception. */
58 /* Possible catcher actions. */
67 enum catcher_state state
;
68 /* Scratch variables used when transitioning a state. */
72 /* Saved/current state. */
74 char *saved_error_pre_print
;
75 char *saved_quit_pre_print
;
76 struct ui_out
*saved_uiout
;
77 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
,
92 struct catcher
*new_catcher
= XZALLOC (struct catcher
);
94 new_catcher
->gdberrmsg
= gdberrmsg
;
95 new_catcher
->mask
= mask
;
97 /* Override error/quit messages during FUNC. */
98 new_catcher
->saved_error_pre_print
= error_pre_print
;
99 new_catcher
->saved_quit_pre_print
= quit_pre_print
;
100 if (mask
& RETURN_MASK_ERROR
)
101 error_pre_print
= errstring
;
102 if (mask
& RETURN_MASK_QUIT
)
103 quit_pre_print
= errstring
;
105 /* Override the global ``struct ui_out'' builder. */
106 new_catcher
->saved_uiout
= uiout
;
109 /* Prevent error/quit during FUNC from calling cleanups established
111 new_catcher
->saved_cleanup_chain
= save_cleanups ();
113 /* Push this new catcher on the top. */
114 new_catcher
->prev
= current_catcher
;
115 current_catcher
= new_catcher
;
116 new_catcher
->state
= CATCHER_CREATED
;
118 return &new_catcher
->buf
;
124 struct catcher
*old_catcher
= current_catcher
;
125 current_catcher
= old_catcher
->prev
;
127 /* Restore the cleanup chain, the error/quit messages, and the uiout
128 builder, to their original states. */
130 restore_cleanups (old_catcher
->saved_cleanup_chain
);
132 uiout
= old_catcher
->saved_uiout
;
134 quit_pre_print
= old_catcher
->saved_quit_pre_print
;
135 error_pre_print
= old_catcher
->saved_error_pre_print
;
140 /* Catcher state machine. Returns non-zero if the m/c should be run
141 again, zero if it should abort. */
144 catcher_state_machine (enum catcher_action action
)
146 switch (current_catcher
->state
)
148 case CATCHER_CREATED
:
152 /* Allow the code to run the catcher. */
153 current_catcher
->state
= CATCHER_RUNNING
;
156 internal_error (__FILE__
, __LINE__
, "bad state");
158 case CATCHER_RUNNING
:
162 /* No error/quit has occured. Just clean up. */
166 current_catcher
->state
= CATCHER_RUNNING_1
;
169 current_catcher
->state
= CATCHER_ABORTING
;
170 /* See also throw_exception. */
173 internal_error (__FILE__
, __LINE__
, "bad switch");
175 case CATCHER_RUNNING_1
:
179 /* The did a "break" from the inner while loop. */
183 current_catcher
->state
= CATCHER_RUNNING
;
186 current_catcher
->state
= CATCHER_ABORTING
;
187 /* See also throw_exception. */
190 internal_error (__FILE__
, __LINE__
, "bad switch");
192 case CATCHER_ABORTING
:
197 int reason
= current_catcher
->reason
;
198 /* If caller wants a copy of the low-level error message,
199 make one. This is used in the case of a silent error
200 whereby the caller may optionally want to issue the
202 if (current_catcher
->gdberrmsg
!= NULL
)
203 *(current_catcher
->gdberrmsg
) = error_last_message ();
204 if (current_catcher
->mask
& RETURN_MASK (reason
))
206 /* Exit normally if this catcher can handle this
207 exception. The caller analyses the func return
212 /* The caller didn't request that the event be caught,
213 relay the event to the next containing
216 throw_exception (reason
);
219 internal_error (__FILE__
, __LINE__
, "bad state");
222 internal_error (__FILE__
, __LINE__
, "bad switch");
226 /* Return for reason REASON to the nearest containing catch_errors(). */
229 throw_exception (enum return_reason reason
)
234 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
235 I can think of a reason why that is vital, though). */
236 bpstat_clear_actions (stop_bpstat
); /* Clear queued breakpoint commands */
238 disable_current_display ();
239 do_cleanups (ALL_CLEANUPS
);
240 if (target_can_async_p () && !target_executing
)
241 do_exec_cleanups (ALL_CLEANUPS
);
243 do_exec_error_cleanups (ALL_CLEANUPS
);
245 if (annotation_level
> 1)
256 /* Jump to the containing catch_errors() call, communicating REASON
257 to that call via setjmp's return value. Note that REASON can't
258 be zero, by definition in defs.h. */
259 catcher_state_machine (CATCH_THROWING
);
260 current_catcher
->reason
= reason
;
261 SIGLONGJMP (current_catcher
->buf
, current_catcher
->reason
);
264 /* Call FUNC() with args FUNC_UIOUT and FUNC_ARGS, catching any
265 errors. Set FUNC_CAUGHT to an ``enum return_reason'' if the
266 function is aborted (using throw_exception() or zero if the
267 function returns normally. Set FUNC_VAL to the value returned by
268 the function or 0 if the function was aborted.
270 Must not be called with immediate_quit in effect (bad things might
271 happen, say we got a signal in the middle of a memcpy to quit_return).
272 This is an OK restriction; with very few exceptions immediate_quit can
273 be replaced by judicious use of QUIT.
275 MASK specifies what to catch; it is normally set to
276 RETURN_MASK_ALL, if for no other reason than that the code which
277 calls catch_errors might not be set up to deal with a quit which
278 isn't caught. But if the code can deal with it, it generally
279 should be RETURN_MASK_ERROR, unless for some reason it is more
280 useful to abort only the portion of the operation inside the
281 catch_errors. Note that quit should return to the command line
282 fairly quickly, even if some further processing is being done. */
284 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
285 error() et.al. could maintain a set of flags that indicate the the
286 current state of each of the longjmp buffers. This would give the
287 longjmp code the chance to detect a longjmp botch (before it gets
288 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
289 code also randomly used a SET_TOP_LEVEL macro that directly
290 initialize the longjmp buffers. */
292 /* MAYBE: cagney/1999-11-05: Should the catch_errors and cleanups code
293 be consolidated into a single file instead of being distributed
294 between utils.c and top.c? */
297 catch_exceptions (struct ui_out
*uiout
,
298 catch_exceptions_ftype
*func
,
303 return catch_exceptions_with_msg (uiout
, func
, func_args
, errstring
,
308 catch_exceptions_with_msg (struct ui_out
*uiout
,
309 catch_exceptions_ftype
*func
,
316 enum return_reason caught
;
318 catch = catcher_init (uiout
, errstring
, gdberrmsg
, mask
);
319 for (caught
= SIGSETJMP ((*catch));
320 catcher_state_machine (CATCH_ITER
);)
321 val
= (*func
) (uiout
, func_args
);
322 gdb_assert (val
>= 0);
323 gdb_assert (caught
<= 0);
330 catch_errors (catch_errors_ftype
*func
, void *func_args
, char *errstring
,
334 enum return_reason caught
;
336 catch = catcher_init (uiout
, errstring
, NULL
, mask
);
337 /* This illustrates how it is possible to nest the mechanism and
338 hence catch "break". Of course this doesn't address the need to
339 also catch "return". */
340 for (caught
= SIGSETJMP ((*catch)); catcher_state_machine (CATCH_ITER
);)
341 for (; catcher_state_machine (CATCH_ITER_1
);)
343 val
= func (func_args
);
351 struct captured_command_args
353 catch_command_errors_ftype
*command
;
359 do_captured_command (void *data
)
361 struct captured_command_args
*context
= data
;
362 context
->command (context
->arg
, context
->from_tty
);
363 /* FIXME: cagney/1999-11-07: Technically this do_cleanups() call
364 isn't needed. Instead an assertion check could be made that
365 simply confirmed that the called function correctly cleaned up
366 after itself. Unfortunately, old code (prior to 1999-11-04) in
367 main.c was calling SET_TOP_LEVEL(), calling the command function,
368 and then *always* calling do_cleanups(). For the moment we
369 remain ``bug compatible'' with that old code.. */
370 do_cleanups (ALL_CLEANUPS
);
375 catch_command_errors (catch_command_errors_ftype
* command
,
376 char *arg
, int from_tty
, return_mask mask
)
378 struct captured_command_args args
;
379 args
.command
= command
;
381 args
.from_tty
= from_tty
;
382 return catch_errors (do_captured_command
, &args
, "", mask
);