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