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