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"
34 /* One should use catch_errors rather than manipulating these
36 #if defined(HAVE_SIGSETJMP)
37 #define SIGJMP_BUF sigjmp_buf
38 #define SIGSETJMP(buf) sigsetjmp((buf), 1)
39 #define SIGLONGJMP(buf,val) siglongjmp((buf), (val))
41 #define SIGJMP_BUF jmp_buf
42 #define SIGSETJMP(buf) setjmp(buf)
43 #define SIGLONGJMP(buf,val) longjmp((buf), (val))
46 /* Where to go for throw_exception(). */
47 static SIGJMP_BUF
*catch_return
;
49 /* Return for reason REASON to the nearest containing catch_errors(). */
52 throw_exception (enum return_reason reason
)
57 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
58 I can think of a reason why that is vital, though). */
59 bpstat_clear_actions (stop_bpstat
); /* Clear queued breakpoint commands */
61 disable_current_display ();
62 do_cleanups (ALL_CLEANUPS
);
63 if (target_can_async_p () && !target_executing
)
64 do_exec_cleanups (ALL_CLEANUPS
);
66 do_exec_error_cleanups (ALL_CLEANUPS
);
68 if (annotation_level
> 1)
79 /* Jump to the containing catch_errors() call, communicating REASON
80 to that call via setjmp's return value. Note that REASON can't
81 be zero, by definition in defs.h. */
83 (NORETURN
void) SIGLONGJMP (*catch_return
, (int) reason
);
86 /* Call FUNC() with args FUNC_UIOUT and FUNC_ARGS, catching any
87 errors. Set FUNC_CAUGHT to an ``enum return_reason'' if the
88 function is aborted (using throw_exception() or zero if the
89 function returns normally. Set FUNC_VAL to the value returned by
90 the function or 0 if the function was aborted.
92 Must not be called with immediate_quit in effect (bad things might
93 happen, say we got a signal in the middle of a memcpy to quit_return).
94 This is an OK restriction; with very few exceptions immediate_quit can
95 be replaced by judicious use of QUIT.
97 MASK specifies what to catch; it is normally set to
98 RETURN_MASK_ALL, if for no other reason than that the code which
99 calls catch_errors might not be set up to deal with a quit which
100 isn't caught. But if the code can deal with it, it generally
101 should be RETURN_MASK_ERROR, unless for some reason it is more
102 useful to abort only the portion of the operation inside the
103 catch_errors. Note that quit should return to the command line
104 fairly quickly, even if some further processing is being done. */
106 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
107 error() et.al. could maintain a set of flags that indicate the the
108 current state of each of the longjmp buffers. This would give the
109 longjmp code the chance to detect a longjmp botch (before it gets
110 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
111 code also randomly used a SET_TOP_LEVEL macro that directly
112 initialize the longjmp buffers. */
114 /* MAYBE: cagney/1999-11-05: Should the catch_errors and cleanups code
115 be consolidated into a single file instead of being distributed
116 between utils.c and top.c? */
119 catcher (catch_exceptions_ftype
*func
,
120 struct ui_out
*func_uiout
,
123 enum return_reason
*func_caught
,
128 SIGJMP_BUF
*saved_catch
;
130 struct cleanup
*saved_cleanup_chain
;
131 char *saved_error_pre_print
;
132 char *saved_quit_pre_print
;
133 struct ui_out
*saved_uiout
;
135 /* Return value from SIGSETJMP(): enum return_reason if error or
136 quit caught, 0 otherwise. */
139 /* Return value from FUNC(): Hopefully non-zero. Explicitly set to
140 zero if an error quit was caught. */
143 /* Override error/quit messages during FUNC. */
145 saved_error_pre_print
= error_pre_print
;
146 saved_quit_pre_print
= quit_pre_print
;
148 if (mask
& RETURN_MASK_ERROR
)
149 error_pre_print
= errstring
;
150 if (mask
& RETURN_MASK_QUIT
)
151 quit_pre_print
= errstring
;
153 /* Override the global ``struct ui_out'' builder. */
158 /* Prevent error/quit during FUNC from calling cleanups established
161 saved_cleanup_chain
= save_cleanups ();
163 /* Call FUNC, catching error/quit events. */
165 saved_catch
= catch_return
;
166 catch_return
= &catch;
167 caught
= SIGSETJMP (catch);
169 val
= (*func
) (func_uiout
, func_args
);
173 /* If caller wants a copy of the low-level error message, make one.
174 This is used in the case of a silent error whereby the caller
175 may optionally want to issue the message. */
177 *gdberrmsg
= error_last_message ();
179 catch_return
= saved_catch
;
181 /* FIXME: cagney/1999-11-05: A correct FUNC implementation will
182 clean things up (restoring the cleanup chain) to the state they
183 were just prior to the call. Unfortunately, many FUNC's are not
184 that well behaved. This could be fixed by adding either a
185 do_cleanups call (to cover the problem) or an assertion check to
186 detect bad FUNCs code. */
188 /* Restore the cleanup chain, the error/quit messages, and the uiout
189 builder, to their original states. */
191 restore_cleanups (saved_cleanup_chain
);
195 if (mask
& RETURN_MASK_QUIT
)
196 quit_pre_print
= saved_quit_pre_print
;
197 if (mask
& RETURN_MASK_ERROR
)
198 error_pre_print
= saved_error_pre_print
;
200 /* Return normally if no error/quit event occurred or this catcher
201 can handle this exception. The caller analyses the func return
204 if (!caught
|| (mask
& RETURN_MASK (caught
)))
207 *func_caught
= caught
;
211 /* The caller didn't request that the event be caught, relay the
212 event to the next containing catch_errors(). */
214 throw_exception (caught
);
218 catch_exceptions (struct ui_out
*uiout
,
219 catch_exceptions_ftype
*func
,
225 enum return_reason caught
;
226 catcher (func
, uiout
, func_args
, &val
, &caught
, errstring
, NULL
, mask
);
227 gdb_assert (val
>= 0);
228 gdb_assert (caught
<= 0);
235 catch_exceptions_with_msg (struct ui_out
*uiout
,
236 catch_exceptions_ftype
*func
,
243 enum return_reason caught
;
244 catcher (func
, uiout
, func_args
, &val
, &caught
, errstring
, gdberrmsg
, mask
);
245 gdb_assert (val
>= 0);
246 gdb_assert (caught
<= 0);
252 struct catch_errors_args
254 catch_errors_ftype
*func
;
259 do_catch_errors (struct ui_out
*uiout
, void *data
)
261 struct catch_errors_args
*args
= data
;
262 return args
->func (args
->func_args
);
266 catch_errors (catch_errors_ftype
*func
, void *func_args
, char *errstring
,
270 enum return_reason caught
;
271 struct catch_errors_args args
;
273 args
.func_args
= func_args
;
274 catcher (do_catch_errors
, uiout
, &args
, &val
, &caught
, errstring
,
281 struct captured_command_args
283 catch_command_errors_ftype
*command
;
289 do_captured_command (void *data
)
291 struct captured_command_args
*context
= data
;
292 context
->command (context
->arg
, context
->from_tty
);
293 /* FIXME: cagney/1999-11-07: Technically this do_cleanups() call
294 isn't needed. Instead an assertion check could be made that
295 simply confirmed that the called function correctly cleaned up
296 after itself. Unfortunately, old code (prior to 1999-11-04) in
297 main.c was calling SET_TOP_LEVEL(), calling the command function,
298 and then *always* calling do_cleanups(). For the moment we
299 remain ``bug compatible'' with that old code.. */
300 do_cleanups (ALL_CLEANUPS
);
305 catch_command_errors (catch_command_errors_ftype
* command
,
306 char *arg
, int from_tty
, return_mask mask
)
308 struct captured_command_args args
;
309 args
.command
= command
;
311 args
.from_tty
= from_tty
;
312 return catch_errors (do_captured_command
, &args
, "", mask
);