2005-02-02 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, 2005 Free
5 Software 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 #include "serial.h"
35
36 const struct exception exception_none = { 0, NO_ERROR, NULL };
37
38 /* One should use catch_errors rather than manipulating these
39 directly. */
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))
44 #else
45 #define SIGJMP_BUF jmp_buf
46 #define SIGSETJMP(buf) setjmp(buf)
47 #define SIGLONGJMP(buf,val) longjmp((buf), (val))
48 #endif
49
50 /* Possible catcher states. */
51 enum catcher_state {
52 /* Initial state, a new catcher has just been created. */
53 CATCHER_CREATED,
54 /* The catch code is running. */
55 CATCHER_RUNNING,
56 CATCHER_RUNNING_1,
57 /* The catch code threw an exception. */
58 CATCHER_ABORTING
59 };
60
61 /* Possible catcher actions. */
62 enum catcher_action {
63 CATCH_ITER,
64 CATCH_ITER_1,
65 CATCH_THROWING
66 };
67
68 struct catcher
69 {
70 enum catcher_state state;
71 /* Jump buffer pointing back at the exception handler. */
72 SIGJMP_BUF buf;
73 /* Status buffer belonging to the exception handler. */
74 volatile struct exception *exception;
75 /* Saved/current state. */
76 int mask;
77 struct ui_out *saved_uiout;
78 struct cleanup *saved_cleanup_chain;
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 volatile struct exception *exception,
89 return_mask mask)
90 {
91 struct catcher *new_catcher = XZALLOC (struct catcher);
92
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;
98
99 new_catcher->mask = mask;
100
101 /* Override the global ``struct ui_out'' builder. */
102 new_catcher->saved_uiout = uiout;
103 uiout = func_uiout;
104
105 /* Prevent error/quit during FUNC from calling cleanups established
106 prior to here. */
107 new_catcher->saved_cleanup_chain = save_cleanups ();
108
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;
113
114 return &new_catcher->buf;
115 }
116
117 static void
118 catcher_pop (void)
119 {
120 struct catcher *old_catcher = current_catcher;
121 current_catcher = old_catcher->prev;
122
123 /* Restore the cleanup chain, the error/quit messages, and the uiout
124 builder, to their original states. */
125
126 restore_cleanups (old_catcher->saved_cleanup_chain);
127
128 uiout = old_catcher->saved_uiout;
129
130 xfree (old_catcher);
131 }
132
133 /* Catcher state machine. Returns non-zero if the m/c should be run
134 again, zero if it should abort. */
135
136 int
137 catcher_state_machine (enum catcher_action action)
138 {
139 switch (current_catcher->state)
140 {
141 case CATCHER_CREATED:
142 switch (action)
143 {
144 case CATCH_ITER:
145 /* Allow the code to run the catcher. */
146 current_catcher->state = CATCHER_RUNNING;
147 return 1;
148 default:
149 internal_error (__FILE__, __LINE__, "bad state");
150 }
151 case CATCHER_RUNNING:
152 switch (action)
153 {
154 case CATCH_ITER:
155 /* No error/quit has occured. Just clean up. */
156 catcher_pop ();
157 return 0;
158 case CATCH_ITER_1:
159 current_catcher->state = CATCHER_RUNNING_1;
160 return 1;
161 case CATCH_THROWING:
162 current_catcher->state = CATCHER_ABORTING;
163 /* See also throw_exception. */
164 return 1;
165 default:
166 internal_error (__FILE__, __LINE__, "bad switch");
167 }
168 case CATCHER_RUNNING_1:
169 switch (action)
170 {
171 case CATCH_ITER:
172 /* The did a "break" from the inner while loop. */
173 catcher_pop ();
174 return 0;
175 case CATCH_ITER_1:
176 current_catcher->state = CATCHER_RUNNING;
177 return 0;
178 case CATCH_THROWING:
179 current_catcher->state = CATCHER_ABORTING;
180 /* See also throw_exception. */
181 return 1;
182 default:
183 internal_error (__FILE__, __LINE__, "bad switch");
184 }
185 case CATCHER_ABORTING:
186 switch (action)
187 {
188 case CATCH_ITER:
189 {
190 struct exception exception = *current_catcher->exception;
191 if (current_catcher->mask & RETURN_MASK (exception.reason))
192 {
193 /* Exit normally if this catcher can handle this
194 exception. The caller analyses the func return
195 values. */
196 catcher_pop ();
197 return 0;
198 }
199 /* The caller didn't request that the event be caught,
200 relay the event to the next containing
201 catch_errors(). */
202 catcher_pop ();
203 throw_exception (exception);
204 }
205 default:
206 internal_error (__FILE__, __LINE__, "bad state");
207 }
208 default:
209 internal_error (__FILE__, __LINE__, "bad switch");
210 }
211 }
212
213 /* Return EXCEPTION to the nearest containing catch_errors(). */
214
215 NORETURN void
216 throw_exception (struct exception exception)
217 {
218 quit_flag = 0;
219 immediate_quit = 0;
220
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 */
224
225 disable_current_display ();
226 do_cleanups (ALL_CLEANUPS);
227 if (target_can_async_p () && !target_executing)
228 do_exec_cleanups (ALL_CLEANUPS);
229 if (sync_execution)
230 do_exec_error_cleanups (ALL_CLEANUPS);
231
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);
238 }
239
240 static char *last_message;
241
242 NORETURN void
243 deprecated_throw_reason (enum return_reason reason)
244 {
245 struct exception exception;
246 memset (&exception, 0, sizeof exception);
247
248 exception.reason = reason;
249 switch (reason)
250 {
251 case RETURN_QUIT:
252 break;
253 case RETURN_ERROR:
254 exception.error = GENERIC_ERROR;
255 break;
256 default:
257 internal_error (__FILE__, __LINE__, "bad switch");
258 }
259
260 throw_exception (exception);
261 }
262
263 static void
264 print_flush (void)
265 {
266 struct serial *gdb_stdout_serial;
267
268 if (deprecated_error_begin_hook)
269 deprecated_error_begin_hook ();
270 target_terminal_ours ();
271
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
275 too): */
276
277 /* 1. The _filtered buffer. */
278 wrap_here ("");
279
280 /* 2. The stdio buffer. */
281 gdb_flush (gdb_stdout);
282 gdb_flush (gdb_stderr);
283
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);
288
289 annotate_error_begin ();
290 }
291
292 static void
293 print_exception (struct ui_file *file, struct exception e)
294 {
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. */
297 const char *start;
298 const char *end;
299 for (start = e.message; start != NULL; start = end)
300 {
301 end = strchr (start, '\n');
302 if (end == NULL)
303 fputs_filtered (start, file);
304 else
305 {
306 end++;
307 ui_file_write (file, start, end - start);
308 }
309 }
310 fprintf_filtered (file, "\n");
311
312 /* Now append the annotation. */
313 switch (e.reason)
314 {
315 case RETURN_QUIT:
316 annotate_quit ();
317 break;
318 case RETURN_ERROR:
319 /* Assume that these are all errors. */
320 annotate_error ();
321 break;
322 default:
323 internal_error (__FILE__, __LINE__, _("Bad switch."));
324 }
325 }
326
327 void
328 exception_print (struct ui_file *file, struct exception e)
329 {
330 if (e.reason < 0 && e.message != NULL)
331 {
332 print_flush ();
333 print_exception (file, e);
334 }
335 }
336
337 void
338 exception_fprintf (struct ui_file *file, struct exception e,
339 const char *prefix, ...)
340 {
341 if (e.reason < 0 && e.message != NULL)
342 {
343 va_list args;
344
345 print_flush ();
346
347 /* Print the prefix. */
348 va_start (args, prefix);
349 vfprintf_filtered (file, prefix, args);
350 va_end (args);
351
352 print_exception (file, e);
353 }
354 }
355
356 void
357 print_any_exception (struct ui_file *file, const char *prefix,
358 struct exception e)
359 {
360 if (e.reason < 0 && e.message != NULL)
361 {
362 target_terminal_ours ();
363 wrap_here (""); /* Force out any buffered output */
364 gdb_flush (gdb_stdout);
365 annotate_error_begin ();
366
367 /* Print the prefix. */
368 if (prefix != NULL && prefix[0] != '\0')
369 fputs_filtered (prefix, file);
370 print_exception (file, e);
371 }
372 }
373
374 NORETURN static void
375 throw_it (enum return_reason reason, enum errors error, const char *fmt,
376 va_list ap) ATTR_NORETURN;
377 NORETURN static void
378 throw_it (enum return_reason reason, enum errors error, const char *fmt,
379 va_list ap)
380 {
381 struct exception e;
382 char *new_message;
383
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;
389
390 /* Create the exception. */
391 e.reason = reason;
392 e.error = error;
393 e.message = last_message;
394
395 /* Throw the exception. */
396 throw_exception (e);
397 }
398
399 NORETURN void
400 throw_verror (enum errors error, const char *fmt, va_list ap)
401 {
402 throw_it (RETURN_ERROR, error, fmt, ap);
403 }
404
405 NORETURN void
406 throw_vfatal (const char *fmt, va_list ap)
407 {
408 throw_it (RETURN_QUIT, NO_ERROR, fmt, ap);
409 }
410
411 NORETURN void
412 throw_error (enum errors error, const char *fmt, ...)
413 {
414 va_list args;
415 va_start (args, fmt);
416 throw_it (RETURN_ERROR, error, fmt, args);
417 va_end (args);
418 }
419
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.
425
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.
430
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. */
439
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. */
447
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? */
451
452 int
453 catch_exceptions (struct ui_out *uiout,
454 catch_exceptions_ftype *func,
455 void *func_args,
456 return_mask mask)
457 {
458 return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask);
459 }
460
461 struct exception
462 catch_exception (struct ui_out *uiout,
463 catch_exception_ftype *func,
464 void *func_args,
465 return_mask mask)
466 {
467 volatile struct exception exception;
468 SIGJMP_BUF *catch;
469 catch = catcher_init (uiout, &exception, mask);
470 for (SIGSETJMP ((*catch));
471 catcher_state_machine (CATCH_ITER);)
472 (*func) (uiout, func_args);
473 return exception;
474 }
475
476 int
477 catch_exceptions_with_msg (struct ui_out *uiout,
478 catch_exceptions_ftype *func,
479 void *func_args,
480 char **gdberrmsg,
481 return_mask mask)
482 {
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)
492 {
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)
497 {
498 if (exception.message != NULL)
499 *gdberrmsg = xstrdup (exception.message);
500 else
501 *gdberrmsg = NULL;
502 }
503 return exception.reason;
504 }
505 return val;
506 }
507
508 int
509 catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
510 return_mask mask)
511 {
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)
522 return 0;
523 return val;
524 }
525
526 int
527 catch_command_errors (catch_command_errors_ftype * command,
528 char *arg, int from_tty, return_mask mask)
529 {
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);
535 if (e.reason < 0)
536 return 0;
537 return 1;
538 }
This page took 0.041269 seconds and 4 git commands to generate.