]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/exceptions.c
2005-01-12 Andrew Cagney <cagney@gnu.org>
[thirdparty/binutils-gdb.git] / gdb / exceptions.c
CommitLineData
60250e8b
AC
1/* Exception (throw catch) mechanism, for GDB, the GNU debugger.
2
3 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
5 Foundation, Inc.
6
7 This file is part of GDB.
8
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.
13
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.
18
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. */
23
24#include "defs.h"
25#include "exceptions.h"
26#include <setjmp.h>
27#include "breakpoint.h"
28#include "target.h"
29#include "inferior.h"
30#include "annotate.h"
31#include "ui-out.h"
32#include "gdb_assert.h"
33
34/* One should use catch_errors rather than manipulating these
35 directly. */
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))
40#else
41#define SIGJMP_BUF jmp_buf
42#define SIGSETJMP(buf) setjmp(buf)
43#define SIGLONGJMP(buf,val) longjmp((buf), (val))
44#endif
45
46/* Where to go for throw_exception(). */
47static SIGJMP_BUF *catch_return;
48
49/* Return for reason REASON to the nearest containing catch_errors(). */
50
51NORETURN void
52throw_exception (enum return_reason reason)
53{
54 quit_flag = 0;
55 immediate_quit = 0;
56
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 */
60
61 disable_current_display ();
62 do_cleanups (ALL_CLEANUPS);
63 if (target_can_async_p () && !target_executing)
64 do_exec_cleanups (ALL_CLEANUPS);
65 if (sync_execution)
66 do_exec_error_cleanups (ALL_CLEANUPS);
67
68 if (annotation_level > 1)
69 switch (reason)
70 {
71 case RETURN_QUIT:
72 annotate_quit ();
73 break;
74 case RETURN_ERROR:
75 annotate_error ();
76 break;
77 }
78
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. */
82
83 (NORETURN void) SIGLONGJMP (*catch_return, (int) reason);
84}
85
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.
91
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.
96
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. */
105
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. */
113
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? */
117
118static void
119catcher (catch_exceptions_ftype *func,
120 struct ui_out *func_uiout,
121 void *func_args,
122 int *func_val,
123 enum return_reason *func_caught,
124 char *errstring,
125 char **gdberrmsg,
126 return_mask mask)
127{
128 SIGJMP_BUF *saved_catch;
129 SIGJMP_BUF 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;
134
135 /* Return value from SIGSETJMP(): enum return_reason if error or
136 quit caught, 0 otherwise. */
137 int caught;
138
139 /* Return value from FUNC(): Hopefully non-zero. Explicitly set to
140 zero if an error quit was caught. */
141 int val;
142
143 /* Override error/quit messages during FUNC. */
144
145 saved_error_pre_print = error_pre_print;
146 saved_quit_pre_print = quit_pre_print;
147
148 if (mask & RETURN_MASK_ERROR)
149 error_pre_print = errstring;
150 if (mask & RETURN_MASK_QUIT)
151 quit_pre_print = errstring;
152
153 /* Override the global ``struct ui_out'' builder. */
154
155 saved_uiout = uiout;
156 uiout = func_uiout;
157
158 /* Prevent error/quit during FUNC from calling cleanups established
159 prior to here. */
160
161 saved_cleanup_chain = save_cleanups ();
162
163 /* Call FUNC, catching error/quit events. */
164
165 saved_catch = catch_return;
166 catch_return = &catch;
167 caught = SIGSETJMP (catch);
168 if (!caught)
169 val = (*func) (func_uiout, func_args);
170 else
171 {
172 val = 0;
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. */
176 if (gdberrmsg)
177 *gdberrmsg = error_last_message ();
178 }
179 catch_return = saved_catch;
180
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. */
187
188 /* Restore the cleanup chain, the error/quit messages, and the uiout
189 builder, to their original states. */
190
191 restore_cleanups (saved_cleanup_chain);
192
193 uiout = saved_uiout;
194
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;
199
200 /* Return normally if no error/quit event occurred or this catcher
201 can handle this exception. The caller analyses the func return
202 values. */
203
204 if (!caught || (mask & RETURN_MASK (caught)))
205 {
206 *func_val = val;
207 *func_caught = caught;
208 return;
209 }
210
211 /* The caller didn't request that the event be caught, relay the
212 event to the next containing catch_errors(). */
213
214 throw_exception (caught);
215}
216
217int
218catch_exceptions (struct ui_out *uiout,
219 catch_exceptions_ftype *func,
220 void *func_args,
221 char *errstring,
222 return_mask mask)
223{
224 int val;
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);
229 if (caught < 0)
230 return caught;
231 return val;
232}
233
234int
235catch_exceptions_with_msg (struct ui_out *uiout,
236 catch_exceptions_ftype *func,
237 void *func_args,
238 char *errstring,
239 char **gdberrmsg,
240 return_mask mask)
241{
242 int val;
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);
247 if (caught < 0)
248 return caught;
249 return val;
250}
251
252struct catch_errors_args
253{
254 catch_errors_ftype *func;
255 void *func_args;
256};
257
258static int
259do_catch_errors (struct ui_out *uiout, void *data)
260{
261 struct catch_errors_args *args = data;
262 return args->func (args->func_args);
263}
264
265int
266catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
267 return_mask mask)
268{
269 int val;
270 enum return_reason caught;
271 struct catch_errors_args args;
272 args.func = func;
273 args.func_args = func_args;
274 catcher (do_catch_errors, uiout, &args, &val, &caught, errstring,
275 NULL, mask);
276 if (caught != 0)
277 return 0;
278 return val;
279}
280
281struct captured_command_args
282 {
283 catch_command_errors_ftype *command;
284 char *arg;
285 int from_tty;
286 };
287
288static int
289do_captured_command (void *data)
290{
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);
301 return 1;
302}
303
304int
305catch_command_errors (catch_command_errors_ftype * command,
306 char *arg, int from_tty, return_mask mask)
307{
308 struct captured_command_args args;
309 args.command = command;
310 args.arg = arg;
311 args.from_tty = from_tty;
312 return catch_errors (do_captured_command, &args, "", mask);
313}