2005-02-18 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 "breakpoint.h"
27 #include "target.h"
28 #include "inferior.h"
29 #include "annotate.h"
30 #include "ui-out.h"
31 #include "gdb_assert.h"
32 #include "gdb_string.h"
33 #include "serial.h"
34
35 const struct exception exception_none = { 0, NO_ERROR, NULL };
36
37 /* Possible catcher states. */
38 enum catcher_state {
39 /* Initial state, a new catcher has just been created. */
40 CATCHER_CREATED,
41 /* The catch code is running. */
42 CATCHER_RUNNING,
43 CATCHER_RUNNING_1,
44 /* The catch code threw an exception. */
45 CATCHER_ABORTING
46 };
47
48 /* Possible catcher actions. */
49 enum catcher_action {
50 CATCH_ITER,
51 CATCH_ITER_1,
52 CATCH_THROWING
53 };
54
55 struct catcher
56 {
57 enum catcher_state state;
58 /* Jump buffer pointing back at the exception handler. */
59 EXCEPTIONS_SIGJMP_BUF buf;
60 /* Status buffer belonging to the exception handler. */
61 volatile struct exception *exception;
62 /* Saved/current state. */
63 int mask;
64 struct ui_out *saved_uiout;
65 struct cleanup *saved_cleanup_chain;
66 /* Back link. */
67 struct catcher *prev;
68 };
69
70 /* Where to go for throw_exception(). */
71 static struct catcher *current_catcher;
72
73 EXCEPTIONS_SIGJMP_BUF *
74 exceptions_state_mc_init (struct ui_out *func_uiout,
75 volatile struct exception *exception,
76 return_mask mask)
77 {
78 struct catcher *new_catcher = XZALLOC (struct catcher);
79
80 /* Start with no exception, save it's address. */
81 exception->reason = 0;
82 exception->error = NO_ERROR;
83 exception->message = NULL;
84 new_catcher->exception = exception;
85
86 new_catcher->mask = mask;
87
88 /* Override the global ``struct ui_out'' builder. */
89 new_catcher->saved_uiout = uiout;
90 uiout = func_uiout;
91
92 /* Prevent error/quit during FUNC from calling cleanups established
93 prior to here. */
94 new_catcher->saved_cleanup_chain = save_cleanups ();
95
96 /* Push this new catcher on the top. */
97 new_catcher->prev = current_catcher;
98 current_catcher = new_catcher;
99 new_catcher->state = CATCHER_CREATED;
100
101 return &new_catcher->buf;
102 }
103
104 static void
105 catcher_pop (void)
106 {
107 struct catcher *old_catcher = current_catcher;
108 current_catcher = old_catcher->prev;
109
110 /* Restore the cleanup chain, the error/quit messages, and the uiout
111 builder, to their original states. */
112
113 restore_cleanups (old_catcher->saved_cleanup_chain);
114
115 uiout = old_catcher->saved_uiout;
116
117 xfree (old_catcher);
118 }
119
120 /* Catcher state machine. Returns non-zero if the m/c should be run
121 again, zero if it should abort. */
122
123 static int
124 exceptions_state_mc (enum catcher_action action)
125 {
126 switch (current_catcher->state)
127 {
128 case CATCHER_CREATED:
129 switch (action)
130 {
131 case CATCH_ITER:
132 /* Allow the code to run the catcher. */
133 current_catcher->state = CATCHER_RUNNING;
134 return 1;
135 default:
136 internal_error (__FILE__, __LINE__, _("bad state"));
137 }
138 case CATCHER_RUNNING:
139 switch (action)
140 {
141 case CATCH_ITER:
142 /* No error/quit has occured. Just clean up. */
143 catcher_pop ();
144 return 0;
145 case CATCH_ITER_1:
146 current_catcher->state = CATCHER_RUNNING_1;
147 return 1;
148 case CATCH_THROWING:
149 current_catcher->state = CATCHER_ABORTING;
150 /* See also throw_exception. */
151 return 1;
152 default:
153 internal_error (__FILE__, __LINE__, _("bad switch"));
154 }
155 case CATCHER_RUNNING_1:
156 switch (action)
157 {
158 case CATCH_ITER:
159 /* The did a "break" from the inner while loop. */
160 catcher_pop ();
161 return 0;
162 case CATCH_ITER_1:
163 current_catcher->state = CATCHER_RUNNING;
164 return 0;
165 case CATCH_THROWING:
166 current_catcher->state = CATCHER_ABORTING;
167 /* See also throw_exception. */
168 return 1;
169 default:
170 internal_error (__FILE__, __LINE__, _("bad switch"));
171 }
172 case CATCHER_ABORTING:
173 switch (action)
174 {
175 case CATCH_ITER:
176 {
177 struct exception exception = *current_catcher->exception;
178 if (current_catcher->mask & RETURN_MASK (exception.reason))
179 {
180 /* Exit normally if this catcher can handle this
181 exception. The caller analyses the func return
182 values. */
183 catcher_pop ();
184 return 0;
185 }
186 /* The caller didn't request that the event be caught,
187 relay the event to the next containing
188 catch_errors(). */
189 catcher_pop ();
190 throw_exception (exception);
191 }
192 default:
193 internal_error (__FILE__, __LINE__, _("bad state"));
194 }
195 default:
196 internal_error (__FILE__, __LINE__, _("bad switch"));
197 }
198 }
199
200 int
201 exceptions_state_mc_action_iter (void)
202 {
203 return exceptions_state_mc (CATCH_ITER);
204 }
205
206 int
207 exceptions_state_mc_action_iter_1 (void)
208 {
209 return exceptions_state_mc (CATCH_ITER_1);
210 }
211
212 /* Return EXCEPTION to the nearest containing catch_errors(). */
213
214 NORETURN void
215 throw_exception (struct exception exception)
216 {
217 quit_flag = 0;
218 immediate_quit = 0;
219
220 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
221 I can think of a reason why that is vital, though). */
222 bpstat_clear_actions (stop_bpstat); /* Clear queued breakpoint commands */
223
224 disable_current_display ();
225 do_cleanups (ALL_CLEANUPS);
226 if (target_can_async_p () && !target_executing)
227 do_exec_cleanups (ALL_CLEANUPS);
228 if (sync_execution)
229 do_exec_error_cleanups (ALL_CLEANUPS);
230
231 /* Jump to the containing catch_errors() call, communicating REASON
232 to that call via setjmp's return value. Note that REASON can't
233 be zero, by definition in defs.h. */
234 exceptions_state_mc (CATCH_THROWING);
235 *current_catcher->exception = exception;
236 EXCEPTIONS_SIGLONGJMP (current_catcher->buf, exception.reason);
237 }
238
239 static char *last_message;
240
241 NORETURN void
242 deprecated_throw_reason (enum return_reason reason)
243 {
244 struct exception exception;
245 memset (&exception, 0, sizeof exception);
246
247 exception.reason = reason;
248 switch (reason)
249 {
250 case RETURN_QUIT:
251 break;
252 case RETURN_ERROR:
253 exception.error = GENERIC_ERROR;
254 break;
255 default:
256 internal_error (__FILE__, __LINE__, _("bad switch"));
257 }
258
259 throw_exception (exception);
260 }
261
262 static void
263 print_flush (void)
264 {
265 struct serial *gdb_stdout_serial;
266
267 if (deprecated_error_begin_hook)
268 deprecated_error_begin_hook ();
269 target_terminal_ours ();
270
271 /* We want all output to appear now, before we print the error. We
272 have 3 levels of buffering we have to flush (it's possible that
273 some of these should be changed to flush the lower-level ones
274 too): */
275
276 /* 1. The _filtered buffer. */
277 wrap_here ("");
278
279 /* 2. The stdio buffer. */
280 gdb_flush (gdb_stdout);
281 gdb_flush (gdb_stderr);
282
283 /* 3. The system-level buffer. */
284 gdb_stdout_serial = serial_fdopen (1);
285 serial_drain_output (gdb_stdout_serial);
286 serial_un_fdopen (gdb_stdout_serial);
287
288 annotate_error_begin ();
289 }
290
291 static void
292 print_exception (struct ui_file *file, struct exception e)
293 {
294 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
295 as that way the MI's behavior is preserved. */
296 const char *start;
297 const char *end;
298 for (start = e.message; start != NULL; start = end)
299 {
300 end = strchr (start, '\n');
301 if (end == NULL)
302 fputs_filtered (start, file);
303 else
304 {
305 end++;
306 ui_file_write (file, start, end - start);
307 }
308 }
309 fprintf_filtered (file, "\n");
310
311 /* Now append the annotation. */
312 switch (e.reason)
313 {
314 case RETURN_QUIT:
315 annotate_quit ();
316 break;
317 case RETURN_ERROR:
318 /* Assume that these are all errors. */
319 annotate_error ();
320 break;
321 default:
322 internal_error (__FILE__, __LINE__, _("Bad switch."));
323 }
324 }
325
326 void
327 exception_print (struct ui_file *file, struct exception e)
328 {
329 if (e.reason < 0 && e.message != NULL)
330 {
331 print_flush ();
332 print_exception (file, e);
333 }
334 }
335
336 void
337 exception_fprintf (struct ui_file *file, struct exception e,
338 const char *prefix, ...)
339 {
340 if (e.reason < 0 && e.message != NULL)
341 {
342 va_list args;
343
344 print_flush ();
345
346 /* Print the prefix. */
347 va_start (args, prefix);
348 vfprintf_filtered (file, prefix, args);
349 va_end (args);
350
351 print_exception (file, e);
352 }
353 }
354
355 void
356 print_any_exception (struct ui_file *file, const char *prefix,
357 struct exception e)
358 {
359 if (e.reason < 0 && e.message != NULL)
360 {
361 target_terminal_ours ();
362 wrap_here (""); /* Force out any buffered output */
363 gdb_flush (gdb_stdout);
364 annotate_error_begin ();
365
366 /* Print the prefix. */
367 if (prefix != NULL && prefix[0] != '\0')
368 fputs_filtered (prefix, file);
369 print_exception (file, e);
370 }
371 }
372
373 NORETURN static void
374 throw_it (enum return_reason reason, enum errors error, const char *fmt,
375 va_list ap) ATTR_NORETURN;
376 NORETURN static void
377 throw_it (enum return_reason reason, enum errors error, const char *fmt,
378 va_list ap)
379 {
380 struct exception e;
381 char *new_message;
382
383 /* Save the message. Create the new message before deleting the
384 old, the new message may include the old message text. */
385 new_message = xstrvprintf (fmt, ap);
386 xfree (last_message);
387 last_message = new_message;
388
389 /* Create the exception. */
390 e.reason = reason;
391 e.error = error;
392 e.message = last_message;
393
394 /* Throw the exception. */
395 throw_exception (e);
396 }
397
398 NORETURN void
399 throw_verror (enum errors error, const char *fmt, va_list ap)
400 {
401 throw_it (RETURN_ERROR, error, fmt, ap);
402 }
403
404 NORETURN void
405 throw_vfatal (const char *fmt, va_list ap)
406 {
407 throw_it (RETURN_QUIT, NO_ERROR, fmt, ap);
408 }
409
410 NORETURN void
411 throw_error (enum errors error, const char *fmt, ...)
412 {
413 va_list args;
414 va_start (args, fmt);
415 throw_it (RETURN_ERROR, error, fmt, args);
416 va_end (args);
417 }
418
419 /* Call FUNC() with args FUNC_UIOUT and FUNC_ARGS, catching any
420 errors. Set FUNC_CAUGHT to an ``enum return_reason'' if the
421 function is aborted (using throw_exception() or zero if the
422 function returns normally. Set FUNC_VAL to the value returned by
423 the function or 0 if the function was aborted.
424
425 Must not be called with immediate_quit in effect (bad things might
426 happen, say we got a signal in the middle of a memcpy to quit_return).
427 This is an OK restriction; with very few exceptions immediate_quit can
428 be replaced by judicious use of QUIT.
429
430 MASK specifies what to catch; it is normally set to
431 RETURN_MASK_ALL, if for no other reason than that the code which
432 calls catch_errors might not be set up to deal with a quit which
433 isn't caught. But if the code can deal with it, it generally
434 should be RETURN_MASK_ERROR, unless for some reason it is more
435 useful to abort only the portion of the operation inside the
436 catch_errors. Note that quit should return to the command line
437 fairly quickly, even if some further processing is being done. */
438
439 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
440 error() et.al. could maintain a set of flags that indicate the the
441 current state of each of the longjmp buffers. This would give the
442 longjmp code the chance to detect a longjmp botch (before it gets
443 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
444 code also randomly used a SET_TOP_LEVEL macro that directly
445 initialize the longjmp buffers. */
446
447 /* MAYBE: cagney/1999-11-05: Should the catch_errors and cleanups code
448 be consolidated into a single file instead of being distributed
449 between utils.c and top.c? */
450
451 int
452 catch_exceptions (struct ui_out *uiout,
453 catch_exceptions_ftype *func,
454 void *func_args,
455 return_mask mask)
456 {
457 return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask);
458 }
459
460 struct exception
461 catch_exception (struct ui_out *uiout,
462 catch_exception_ftype *func,
463 void *func_args,
464 return_mask mask)
465 {
466 volatile struct exception exception;
467 TRY_CATCH (exception, mask)
468 {
469 (*func) (uiout, func_args);
470 }
471 return exception;
472 }
473
474 int
475 catch_exceptions_with_msg (struct ui_out *uiout,
476 catch_exceptions_ftype *func,
477 void *func_args,
478 char **gdberrmsg,
479 return_mask mask)
480 {
481 volatile struct exception exception;
482 volatile int val = 0;
483 TRY_CATCH (exception, mask)
484 {
485 val = (*func) (uiout, func_args);
486 }
487 print_any_exception (gdb_stderr, NULL, exception);
488 gdb_assert (val >= 0);
489 gdb_assert (exception.reason <= 0);
490 if (exception.reason < 0)
491 {
492 /* If caller wants a copy of the low-level error message, make
493 one. This is used in the case of a silent error whereby the
494 caller may optionally want to issue the message. */
495 if (gdberrmsg != NULL)
496 {
497 if (exception.message != NULL)
498 *gdberrmsg = xstrdup (exception.message);
499 else
500 *gdberrmsg = NULL;
501 }
502 return exception.reason;
503 }
504 return val;
505 }
506
507 int
508 catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
509 return_mask mask)
510 {
511 volatile int val = 0;
512 volatile struct exception exception;
513 TRY_CATCH (exception, mask)
514 {
515 val = func (func_args);
516 }
517 print_any_exception (gdb_stderr, errstring, exception);
518 if (exception.reason != 0)
519 return 0;
520 return val;
521 }
522
523 int
524 catch_command_errors (catch_command_errors_ftype * command,
525 char *arg, int from_tty, return_mask mask)
526 {
527 volatile struct exception e;
528 TRY_CATCH (e, mask)
529 {
530 command (arg, from_tty);
531 }
532 print_any_exception (gdb_stderr, NULL, e);
533 if (e.reason < 0)
534 return 0;
535 return 1;
536 }
This page took 0.041052 seconds and 4 git commands to generate.