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