use gnulib's update-copyright script to update copyright years
[deliverable/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 63 struct cleanup *saved_cleanup_chain;
db5f402d
AC
64 /* Back link. */
65 struct catcher *prev;
66};
67
60250e8b 68/* Where to go for throw_exception(). */
db5f402d
AC
69static struct catcher *current_catcher;
70
ef140872
DE
71/* Return length of current_catcher list. */
72
73static int
74catcher_list_size (void)
75{
76 int size;
77 struct catcher *catcher;
78
79 for (size = 0, catcher = current_catcher;
80 catcher != NULL;
81 catcher = catcher->prev)
82 ++size;
83
84 return size;
85}
86
6941d02a 87EXCEPTIONS_SIGJMP_BUF *
f9679975 88exceptions_state_mc_init (volatile struct gdb_exception *exception,
6941d02a 89 return_mask mask)
db5f402d
AC
90{
91 struct catcher *new_catcher = XZALLOC (struct catcher);
92
2a78bfb5
AC
93 /* Start with no exception, save it's address. */
94 exception->reason = 0;
7b871fab 95 exception->error = GDB_NO_ERROR;
2a78bfb5
AC
96 exception->message = NULL;
97 new_catcher->exception = exception;
98
db5f402d
AC
99 new_catcher->mask = mask;
100
db5f402d 101 /* Prevent error/quit during FUNC from calling cleanups established
0963b4bd 102 prior to here. */
db5f402d
AC
103 new_catcher->saved_cleanup_chain = save_cleanups ();
104
105 /* Push this new catcher on the top. */
106 new_catcher->prev = current_catcher;
107 current_catcher = new_catcher;
108 new_catcher->state = CATCHER_CREATED;
109
110 return &new_catcher->buf;
111}
112
113static void
114catcher_pop (void)
115{
116 struct catcher *old_catcher = current_catcher;
d7f9d729 117
db5f402d
AC
118 current_catcher = old_catcher->prev;
119
120 /* Restore the cleanup chain, the error/quit messages, and the uiout
0963b4bd 121 builder, to their original states. */
db5f402d
AC
122
123 restore_cleanups (old_catcher->saved_cleanup_chain);
124
db5f402d
AC
125 xfree (old_catcher);
126}
127
128/* Catcher state machine. Returns non-zero if the m/c should be run
129 again, zero if it should abort. */
130
6941d02a
AC
131static int
132exceptions_state_mc (enum catcher_action action)
db5f402d
AC
133{
134 switch (current_catcher->state)
135 {
136 case CATCHER_CREATED:
137 switch (action)
138 {
139 case CATCH_ITER:
140 /* Allow the code to run the catcher. */
141 current_catcher->state = CATCHER_RUNNING;
142 return 1;
143 default:
e2e0b3e5 144 internal_error (__FILE__, __LINE__, _("bad state"));
db5f402d
AC
145 }
146 case CATCHER_RUNNING:
147 switch (action)
148 {
149 case CATCH_ITER:
150 /* No error/quit has occured. Just clean up. */
151 catcher_pop ();
152 return 0;
153 case CATCH_ITER_1:
154 current_catcher->state = CATCHER_RUNNING_1;
155 return 1;
156 case CATCH_THROWING:
157 current_catcher->state = CATCHER_ABORTING;
158 /* See also throw_exception. */
159 return 1;
160 default:
e2e0b3e5 161 internal_error (__FILE__, __LINE__, _("bad switch"));
db5f402d
AC
162 }
163 case CATCHER_RUNNING_1:
164 switch (action)
165 {
166 case CATCH_ITER:
167 /* The did a "break" from the inner while loop. */
168 catcher_pop ();
169 return 0;
170 case CATCH_ITER_1:
171 current_catcher->state = CATCHER_RUNNING;
172 return 0;
173 case CATCH_THROWING:
174 current_catcher->state = CATCHER_ABORTING;
175 /* See also throw_exception. */
176 return 1;
177 default:
e2e0b3e5 178 internal_error (__FILE__, __LINE__, _("bad switch"));
db5f402d
AC
179 }
180 case CATCHER_ABORTING:
181 switch (action)
182 {
183 case CATCH_ITER:
184 {
71fff37b 185 struct gdb_exception exception = *current_catcher->exception;
d7f9d729 186
2a78bfb5 187 if (current_catcher->mask & RETURN_MASK (exception.reason))
db5f402d
AC
188 {
189 /* Exit normally if this catcher can handle this
190 exception. The caller analyses the func return
191 values. */
192 catcher_pop ();
193 return 0;
194 }
195 /* The caller didn't request that the event be caught,
196 relay the event to the next containing
0963b4bd 197 catch_errors(). */
db5f402d 198 catcher_pop ();
2a78bfb5 199 throw_exception (exception);
db5f402d
AC
200 }
201 default:
e2e0b3e5 202 internal_error (__FILE__, __LINE__, _("bad state"));
db5f402d
AC
203 }
204 default:
e2e0b3e5 205 internal_error (__FILE__, __LINE__, _("bad switch"));
db5f402d
AC
206 }
207}
60250e8b 208
6941d02a
AC
209int
210exceptions_state_mc_action_iter (void)
211{
212 return exceptions_state_mc (CATCH_ITER);
213}
214
215int
216exceptions_state_mc_action_iter_1 (void)
217{
218 return exceptions_state_mc (CATCH_ITER_1);
219}
220
2a78bfb5 221/* Return EXCEPTION to the nearest containing catch_errors(). */
60250e8b 222
c25c4a8b 223void
71fff37b 224throw_exception (struct gdb_exception exception)
60250e8b
AC
225{
226 quit_flag = 0;
227 immediate_quit = 0;
228
60250e8b 229 do_cleanups (ALL_CLEANUPS);
60250e8b 230
60250e8b
AC
231 /* Jump to the containing catch_errors() call, communicating REASON
232 to that call via setjmp's return value. Note that REASON can't
0963b4bd 233 be zero, by definition in defs.h. */
6941d02a 234 exceptions_state_mc (CATCH_THROWING);
2a78bfb5 235 *current_catcher->exception = exception;
6941d02a 236 EXCEPTIONS_SIGLONGJMP (current_catcher->buf, exception.reason);
2a78bfb5
AC
237}
238
c25c4a8b 239void
315a522e 240deprecated_throw_reason (enum return_reason reason)
2a78bfb5 241{
71fff37b 242 struct gdb_exception exception;
d7f9d729 243
2a78bfb5
AC
244 memset (&exception, 0, sizeof exception);
245
246 exception.reason = reason;
247 switch (reason)
248 {
249 case RETURN_QUIT:
250 break;
251 case RETURN_ERROR:
252 exception.error = GENERIC_ERROR;
2a78bfb5
AC
253 break;
254 default:
e2e0b3e5 255 internal_error (__FILE__, __LINE__, _("bad switch"));
2a78bfb5
AC
256 }
257
258 throw_exception (exception);
60250e8b
AC
259}
260
6b1b7650 261static void
c6da7a6d 262print_flush (void)
6b1b7650 263{
e06e2353
AC
264 struct serial *gdb_stdout_serial;
265
c6da7a6d
AC
266 if (deprecated_error_begin_hook)
267 deprecated_error_begin_hook ();
268 target_terminal_ours ();
e06e2353
AC
269
270 /* We want all output to appear now, before we print the error. We
271 have 3 levels of buffering we have to flush (it's possible that
272 some of these should be changed to flush the lower-level ones
273 too): */
274
275 /* 1. The _filtered buffer. */
276 wrap_here ("");
277
278 /* 2. The stdio buffer. */
c6da7a6d 279 gdb_flush (gdb_stdout);
e06e2353
AC
280 gdb_flush (gdb_stderr);
281
282 /* 3. The system-level buffer. */
283 gdb_stdout_serial = serial_fdopen (1);
cade9e54
PB
284 if (gdb_stdout_serial)
285 {
286 serial_drain_output (gdb_stdout_serial);
287 serial_un_fdopen (gdb_stdout_serial);
288 }
e06e2353 289
c6da7a6d 290 annotate_error_begin ();
6b1b7650
AC
291}
292
9cbc821d 293static void
71fff37b 294print_exception (struct ui_file *file, struct gdb_exception e)
9cbc821d
AC
295{
296 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
297 as that way the MI's behavior is preserved. */
298 const char *start;
299 const char *end;
d7f9d729 300
9cbc821d
AC
301 for (start = e.message; start != NULL; start = end)
302 {
303 end = strchr (start, '\n');
304 if (end == NULL)
305 fputs_filtered (start, file);
306 else
307 {
308 end++;
309 ui_file_write (file, start, end - start);
310 }
311 }
c6da7a6d 312 fprintf_filtered (file, "\n");
e48f5bee
AC
313
314 /* Now append the annotation. */
315 switch (e.reason)
316 {
317 case RETURN_QUIT:
318 annotate_quit ();
319 break;
320 case RETURN_ERROR:
321 /* Assume that these are all errors. */
322 annotate_error ();
323 break;
324 default:
325 internal_error (__FILE__, __LINE__, _("Bad switch."));
326 }
9cbc821d
AC
327}
328
8a076db9 329void
71fff37b 330exception_print (struct ui_file *file, struct gdb_exception e)
8a076db9
AC
331{
332 if (e.reason < 0 && e.message != NULL)
333 {
c6da7a6d 334 print_flush ();
9cbc821d 335 print_exception (file, e);
9cbc821d
AC
336 }
337}
8a076db9 338
9cbc821d 339void
71fff37b 340exception_fprintf (struct ui_file *file, struct gdb_exception e,
9cbc821d
AC
341 const char *prefix, ...)
342{
343 if (e.reason < 0 && e.message != NULL)
344 {
345 va_list args;
c6da7a6d
AC
346
347 print_flush ();
9cbc821d
AC
348
349 /* Print the prefix. */
350 va_start (args, prefix);
351 vfprintf_filtered (file, prefix, args);
352 va_end (args);
353
354 print_exception (file, e);
8a076db9
AC
355 }
356}
357
2c0b251b 358static void
e48f5bee 359print_any_exception (struct ui_file *file, const char *prefix,
71fff37b 360 struct gdb_exception e)
e48f5bee
AC
361{
362 if (e.reason < 0 && e.message != NULL)
363 {
364 target_terminal_ours ();
0963b4bd 365 wrap_here (""); /* Force out any buffered output. */
e48f5bee
AC
366 gdb_flush (gdb_stdout);
367 annotate_error_begin ();
368
369 /* Print the prefix. */
370 if (prefix != NULL && prefix[0] != '\0')
371 fputs_filtered (prefix, file);
372 print_exception (file, e);
373 }
374}
375
ef140872
DE
376/* A stack of exception messages.
377 This is needed to handle nested calls to throw_it: we don't want to
378 xfree space for a message before it's used.
379 This can happen if we throw an exception during a cleanup:
380 An outer TRY_CATCH may have an exception message it wants to print,
381 but while doing cleanups further calls to throw_it are made.
382
383 This is indexed by the size of the current_catcher list.
384 It is a dynamically allocated array so that we don't care how deeply
385 GDB nests its TRY_CATCHs. */
386static char **exception_messages;
387
388/* The number of currently allocated entries in exception_messages. */
389static int exception_messages_size;
390
c25c4a8b 391static void ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (3, 0)
3af1e0e3
AC
392throw_it (enum return_reason reason, enum errors error, const char *fmt,
393 va_list ap)
6b1b7650 394{
71fff37b 395 struct gdb_exception e;
17d92a02 396 char *new_message;
ef140872
DE
397 int depth = catcher_list_size ();
398
399 gdb_assert (depth > 0);
6b1b7650 400
ef140872 401 /* Note: The new message may use an old message's text. */
17d92a02 402 new_message = xstrvprintf (fmt, ap);
ef140872
DE
403
404 if (depth > exception_messages_size)
405 {
406 int old_size = exception_messages_size;
407
408 exception_messages_size = depth + 10;
409 exception_messages = (char **) xrealloc (exception_messages,
410 exception_messages_size
411 * sizeof (char *));
412 memset (exception_messages + old_size, 0,
413 (exception_messages_size - old_size) * sizeof (char *));
414 }
415
416 xfree (exception_messages[depth - 1]);
417 exception_messages[depth - 1] = new_message;
c6da7a6d
AC
418
419 /* Create the exception. */
420 e.reason = reason;
421 e.error = error;
ef140872 422 e.message = new_message;
6b1b7650 423
6b1b7650 424 /* Throw the exception. */
6b1b7650
AC
425 throw_exception (e);
426}
427
c25c4a8b 428void
6b1b7650
AC
429throw_verror (enum errors error, const char *fmt, va_list ap)
430{
3af1e0e3 431 throw_it (RETURN_ERROR, error, fmt, ap);
6b1b7650
AC
432}
433
c25c4a8b 434void
6b1b7650
AC
435throw_vfatal (const char *fmt, va_list ap)
436{
7b871fab 437 throw_it (RETURN_QUIT, GDB_NO_ERROR, fmt, ap);
6b1b7650
AC
438}
439
c25c4a8b 440void
05ff989b 441throw_error (enum errors error, const char *fmt, ...)
6b1b7650 442{
05ff989b 443 va_list args;
d7f9d729 444
05ff989b 445 va_start (args, fmt);
3af1e0e3 446 throw_it (RETURN_ERROR, error, fmt, args);
05ff989b 447 va_end (args);
6b1b7650
AC
448}
449
787274f0
DE
450/* Call FUNC(UIOUT, FUNC_ARGS) but wrapped within an exception
451 handler. If an exception (enum return_reason) is thrown using
452 throw_exception() than all cleanups installed since
453 catch_exceptions() was entered are invoked, the (-ve) exception
454 value is then returned by catch_exceptions. If FUNC() returns
455 normally (with a positive or zero return value) then that value is
456 returned by catch_exceptions(). It is an internal_error() for
457 FUNC() to return a negative value.
458
459 See exceptions.h for further usage details.
60250e8b
AC
460
461 Must not be called with immediate_quit in effect (bad things might
462 happen, say we got a signal in the middle of a memcpy to quit_return).
463 This is an OK restriction; with very few exceptions immediate_quit can
787274f0 464 be replaced by judicious use of QUIT. */
60250e8b
AC
465
466/* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
7a9dd1b2 467 error() et al. could maintain a set of flags that indicate the
60250e8b
AC
468 current state of each of the longjmp buffers. This would give the
469 longjmp code the chance to detect a longjmp botch (before it gets
470 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
471 code also randomly used a SET_TOP_LEVEL macro that directly
0963b4bd 472 initialized the longjmp buffers. */
60250e8b 473
60250e8b
AC
474int
475catch_exceptions (struct ui_out *uiout,
476 catch_exceptions_ftype *func,
477 void *func_args,
60250e8b
AC
478 return_mask mask)
479{
1c3c7ee7 480 return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask);
60250e8b
AC
481}
482
483int
f9679975 484catch_exceptions_with_msg (struct ui_out *func_uiout,
60250e8b
AC
485 catch_exceptions_ftype *func,
486 void *func_args,
60250e8b
AC
487 char **gdberrmsg,
488 return_mask mask)
489{
71fff37b 490 volatile struct gdb_exception exception;
2a78bfb5 491 volatile int val = 0;
f9679975 492 struct ui_out *saved_uiout;
d7f9d729 493
f9679975 494 /* Save and override the global ``struct ui_out'' builder. */
79a45e25
PA
495 saved_uiout = current_uiout;
496 current_uiout = func_uiout;
f9679975
PA
497
498 TRY_CATCH (exception, RETURN_MASK_ALL)
6941d02a 499 {
79a45e25 500 val = (*func) (current_uiout, func_args);
6941d02a 501 }
f9679975
PA
502
503 /* Restore the global builder. */
79a45e25 504 current_uiout = saved_uiout;
f9679975
PA
505
506 if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
507 {
508 /* The caller didn't request that the event be caught.
509 Rethrow. */
510 throw_exception (exception);
511 }
512
e48f5bee 513 print_any_exception (gdb_stderr, NULL, exception);
60250e8b 514 gdb_assert (val >= 0);
2a78bfb5
AC
515 gdb_assert (exception.reason <= 0);
516 if (exception.reason < 0)
517 {
518 /* If caller wants a copy of the low-level error message, make
519 one. This is used in the case of a silent error whereby the
520 caller may optionally want to issue the message. */
521 if (gdberrmsg != NULL)
6b1b7650
AC
522 {
523 if (exception.message != NULL)
524 *gdberrmsg = xstrdup (exception.message);
525 else
526 *gdberrmsg = NULL;
527 }
2a78bfb5
AC
528 return exception.reason;
529 }
60250e8b
AC
530 return val;
531}
532
787274f0
DE
533/* This function is superseded by catch_exceptions(). */
534
60250e8b
AC
535int
536catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
537 return_mask mask)
538{
2a78bfb5 539 volatile int val = 0;
71fff37b 540 volatile struct gdb_exception exception;
f9679975 541 struct ui_out *saved_uiout;
d7f9d729 542
f9679975 543 /* Save the global ``struct ui_out'' builder. */
79a45e25 544 saved_uiout = current_uiout;
f9679975
PA
545
546 TRY_CATCH (exception, RETURN_MASK_ALL)
6941d02a
AC
547 {
548 val = func (func_args);
549 }
f9679975
PA
550
551 /* Restore the global builder. */
79a45e25 552 current_uiout = saved_uiout;
f9679975
PA
553
554 if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
555 {
556 /* The caller didn't request that the event be caught.
557 Rethrow. */
558 throw_exception (exception);
559 }
560
e48f5bee 561 print_any_exception (gdb_stderr, errstring, exception);
2a78bfb5 562 if (exception.reason != 0)
60250e8b
AC
563 return 0;
564 return val;
565}
566
60250e8b
AC
567int
568catch_command_errors (catch_command_errors_ftype * command,
569 char *arg, int from_tty, return_mask mask)
570{
71fff37b 571 volatile struct gdb_exception e;
d7f9d729 572
6941d02a
AC
573 TRY_CATCH (e, mask)
574 {
575 command (arg, from_tty);
576 }
5a14cc1a
AC
577 print_any_exception (gdb_stderr, NULL, e);
578 if (e.reason < 0)
579 return 0;
580 return 1;
60250e8b 581}
This page took 0.500184 seconds and 4 git commands to generate.