2005-01-12 Andrew Cagney <cagney@gnu.org>
[deliverable/binutils-gdb.git] / gdb / exceptions.c
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 #include "gdb_string.h"
34
35 /* One should use catch_errors rather than manipulating these
36 directly. */
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))
41 #else
42 #define SIGJMP_BUF jmp_buf
43 #define SIGSETJMP(buf) setjmp(buf)
44 #define SIGLONGJMP(buf,val) longjmp((buf), (val))
45 #endif
46
47 /* Possible catcher states. */
48 enum catcher_state {
49 /* Initial state, a new catcher has just been created. */
50 CATCHER_CREATED,
51 /* The catch code is running. */
52 CATCHER_RUNNING,
53 CATCHER_RUNNING_1,
54 /* The catch code threw an exception. */
55 CATCHER_ABORTING
56 };
57
58 /* Possible catcher actions. */
59 enum catcher_action {
60 CATCH_ITER,
61 CATCH_ITER_1,
62 CATCH_THROWING
63 };
64
65 struct catcher
66 {
67 enum catcher_state state;
68 /* Scratch variables used when transitioning a state. */
69 SIGJMP_BUF buf;
70 int reason;
71 int val;
72 /* Saved/current state. */
73 int mask;
74 char *saved_error_pre_print;
75 char *saved_quit_pre_print;
76 struct ui_out *saved_uiout;
77 struct cleanup *saved_cleanup_chain;
78 char **gdberrmsg;
79 /* Back link. */
80 struct catcher *prev;
81 };
82
83 /* Where to go for throw_exception(). */
84 static struct catcher *current_catcher;
85
86 static SIGJMP_BUF *
87 catcher_init (struct ui_out *func_uiout,
88 char *errstring,
89 char **gdberrmsg,
90 return_mask mask)
91 {
92 struct catcher *new_catcher = XZALLOC (struct catcher);
93
94 new_catcher->gdberrmsg = gdberrmsg;
95 new_catcher->mask = mask;
96
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;
104
105 /* Override the global ``struct ui_out'' builder. */
106 new_catcher->saved_uiout = uiout;
107 uiout = func_uiout;
108
109 /* Prevent error/quit during FUNC from calling cleanups established
110 prior to here. */
111 new_catcher->saved_cleanup_chain = save_cleanups ();
112
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;
117
118 return &new_catcher->buf;
119 }
120
121 static void
122 catcher_pop (void)
123 {
124 struct catcher *old_catcher = current_catcher;
125 current_catcher = old_catcher->prev;
126
127 /* Restore the cleanup chain, the error/quit messages, and the uiout
128 builder, to their original states. */
129
130 restore_cleanups (old_catcher->saved_cleanup_chain);
131
132 uiout = old_catcher->saved_uiout;
133
134 quit_pre_print = old_catcher->saved_quit_pre_print;
135 error_pre_print = old_catcher->saved_error_pre_print;
136
137 xfree (old_catcher);
138 }
139
140 /* Catcher state machine. Returns non-zero if the m/c should be run
141 again, zero if it should abort. */
142
143 int
144 catcher_state_machine (enum catcher_action action)
145 {
146 switch (current_catcher->state)
147 {
148 case CATCHER_CREATED:
149 switch (action)
150 {
151 case CATCH_ITER:
152 /* Allow the code to run the catcher. */
153 current_catcher->state = CATCHER_RUNNING;
154 return 1;
155 default:
156 internal_error (__FILE__, __LINE__, "bad state");
157 }
158 case CATCHER_RUNNING:
159 switch (action)
160 {
161 case CATCH_ITER:
162 /* No error/quit has occured. Just clean up. */
163 catcher_pop ();
164 return 0;
165 case CATCH_ITER_1:
166 current_catcher->state = CATCHER_RUNNING_1;
167 return 1;
168 case CATCH_THROWING:
169 current_catcher->state = CATCHER_ABORTING;
170 /* See also throw_exception. */
171 return 1;
172 default:
173 internal_error (__FILE__, __LINE__, "bad switch");
174 }
175 case CATCHER_RUNNING_1:
176 switch (action)
177 {
178 case CATCH_ITER:
179 /* The did a "break" from the inner while loop. */
180 catcher_pop ();
181 return 0;
182 case CATCH_ITER_1:
183 current_catcher->state = CATCHER_RUNNING;
184 return 0;
185 case CATCH_THROWING:
186 current_catcher->state = CATCHER_ABORTING;
187 /* See also throw_exception. */
188 return 1;
189 default:
190 internal_error (__FILE__, __LINE__, "bad switch");
191 }
192 case CATCHER_ABORTING:
193 switch (action)
194 {
195 case CATCH_ITER:
196 {
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
201 message. */
202 if (current_catcher->gdberrmsg != NULL)
203 *(current_catcher->gdberrmsg) = error_last_message ();
204 if (current_catcher->mask & RETURN_MASK (reason))
205 {
206 /* Exit normally if this catcher can handle this
207 exception. The caller analyses the func return
208 values. */
209 catcher_pop ();
210 return 0;
211 }
212 /* The caller didn't request that the event be caught,
213 relay the event to the next containing
214 catch_errors(). */
215 catcher_pop ();
216 throw_exception (reason);
217 }
218 default:
219 internal_error (__FILE__, __LINE__, "bad state");
220 }
221 default:
222 internal_error (__FILE__, __LINE__, "bad switch");
223 }
224 }
225
226 /* Return for reason REASON to the nearest containing catch_errors(). */
227
228 NORETURN void
229 throw_exception (enum return_reason reason)
230 {
231 quit_flag = 0;
232 immediate_quit = 0;
233
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 */
237
238 disable_current_display ();
239 do_cleanups (ALL_CLEANUPS);
240 if (target_can_async_p () && !target_executing)
241 do_exec_cleanups (ALL_CLEANUPS);
242 if (sync_execution)
243 do_exec_error_cleanups (ALL_CLEANUPS);
244
245 if (annotation_level > 1)
246 switch (reason)
247 {
248 case RETURN_QUIT:
249 annotate_quit ();
250 break;
251 case RETURN_ERROR:
252 annotate_error ();
253 break;
254 }
255
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);
262 }
263
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.
269
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.
274
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. */
283
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. */
291
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? */
295
296 int
297 catch_exceptions (struct ui_out *uiout,
298 catch_exceptions_ftype *func,
299 void *func_args,
300 char *errstring,
301 return_mask mask)
302 {
303 return catch_exceptions_with_msg (uiout, func, func_args, errstring,
304 NULL, mask);
305 }
306
307 int
308 catch_exceptions_with_msg (struct ui_out *uiout,
309 catch_exceptions_ftype *func,
310 void *func_args,
311 char *errstring,
312 char **gdberrmsg,
313 return_mask mask)
314 {
315 int val = 0;
316 enum return_reason caught;
317 SIGJMP_BUF *catch;
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);
324 if (caught < 0)
325 return caught;
326 return val;
327 }
328
329 int
330 catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
331 return_mask mask)
332 {
333 int val = 0;
334 enum return_reason caught;
335 SIGJMP_BUF *catch;
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);)
342 {
343 val = func (func_args);
344 break;
345 }
346 if (caught != 0)
347 return 0;
348 return val;
349 }
350
351 struct captured_command_args
352 {
353 catch_command_errors_ftype *command;
354 char *arg;
355 int from_tty;
356 };
357
358 static int
359 do_captured_command (void *data)
360 {
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);
371 return 1;
372 }
373
374 int
375 catch_command_errors (catch_command_errors_ftype * command,
376 char *arg, int from_tty, return_mask mask)
377 {
378 struct captured_command_args args;
379 args.command = command;
380 args.arg = arg;
381 args.from_tty = from_tty;
382 return catch_errors (do_captured_command, &args, "", mask);
383 }
This page took 0.052059 seconds and 5 git commands to generate.