* configure.ac: Use AC_CHECK_DECLS instead of gcc_AC_CHECK_DECLS
[deliverable/binutils-gdb.git] / gdb / exceptions.c
CommitLineData
60250e8b
AC
1/* Exception (throw catch) mechanism, for GDB, the GNU debugger.
2
3 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
05ff989b
AC
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free
5 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
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 <setjmp.h>
27#include "breakpoint.h"
28#include "target.h"
29#include "inferior.h"
30#include "annotate.h"
31#include "ui-out.h"
32#include "gdb_assert.h"
db5f402d 33#include "gdb_string.h"
e06e2353 34#include "serial.h"
60250e8b 35
c1043fc2
AC
36const struct exception exception_none = { 0, NO_ERROR, NULL };
37
60250e8b
AC
38/* One should use catch_errors rather than manipulating these
39 directly. */
40#if defined(HAVE_SIGSETJMP)
41#define SIGJMP_BUF sigjmp_buf
42#define SIGSETJMP(buf) sigsetjmp((buf), 1)
43#define SIGLONGJMP(buf,val) siglongjmp((buf), (val))
44#else
45#define SIGJMP_BUF jmp_buf
46#define SIGSETJMP(buf) setjmp(buf)
47#define SIGLONGJMP(buf,val) longjmp((buf), (val))
48#endif
49
db5f402d
AC
50/* Possible catcher states. */
51enum catcher_state {
52 /* Initial state, a new catcher has just been created. */
53 CATCHER_CREATED,
54 /* The catch code is running. */
55 CATCHER_RUNNING,
56 CATCHER_RUNNING_1,
57 /* The catch code threw an exception. */
58 CATCHER_ABORTING
59};
60
61/* Possible catcher actions. */
62enum catcher_action {
63 CATCH_ITER,
64 CATCH_ITER_1,
65 CATCH_THROWING
66};
67
68struct catcher
69{
70 enum catcher_state state;
2a78bfb5 71 /* Jump buffer pointing back at the exception handler. */
db5f402d 72 SIGJMP_BUF buf;
8a076db9 73 /* Status buffer belonging to the exception handler. */
2a78bfb5 74 volatile struct exception *exception;
db5f402d
AC
75 /* Saved/current state. */
76 int mask;
db5f402d
AC
77 struct ui_out *saved_uiout;
78 struct cleanup *saved_cleanup_chain;
db5f402d
AC
79 /* Back link. */
80 struct catcher *prev;
81};
82
60250e8b 83/* Where to go for throw_exception(). */
db5f402d
AC
84static struct catcher *current_catcher;
85
86static SIGJMP_BUF *
87catcher_init (struct ui_out *func_uiout,
2a78bfb5 88 volatile struct exception *exception,
3af1e0e3 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;
95 exception->error = NO_ERROR;
96 exception->message = NULL;
97 new_catcher->exception = exception;
98
db5f402d
AC
99 new_catcher->mask = mask;
100
db5f402d
AC
101 /* Override the global ``struct ui_out'' builder. */
102 new_catcher->saved_uiout = uiout;
103 uiout = func_uiout;
104
105 /* Prevent error/quit during FUNC from calling cleanups established
106 prior to here. */
107 new_catcher->saved_cleanup_chain = save_cleanups ();
108
109 /* Push this new catcher on the top. */
110 new_catcher->prev = current_catcher;
111 current_catcher = new_catcher;
112 new_catcher->state = CATCHER_CREATED;
113
114 return &new_catcher->buf;
115}
116
117static void
118catcher_pop (void)
119{
120 struct catcher *old_catcher = current_catcher;
121 current_catcher = old_catcher->prev;
122
123 /* Restore the cleanup chain, the error/quit messages, and the uiout
124 builder, to their original states. */
125
126 restore_cleanups (old_catcher->saved_cleanup_chain);
127
128 uiout = old_catcher->saved_uiout;
129
db5f402d
AC
130 xfree (old_catcher);
131}
132
133/* Catcher state machine. Returns non-zero if the m/c should be run
134 again, zero if it should abort. */
135
136int
137catcher_state_machine (enum catcher_action action)
138{
139 switch (current_catcher->state)
140 {
141 case CATCHER_CREATED:
142 switch (action)
143 {
144 case CATCH_ITER:
145 /* Allow the code to run the catcher. */
146 current_catcher->state = CATCHER_RUNNING;
147 return 1;
148 default:
149 internal_error (__FILE__, __LINE__, "bad state");
150 }
151 case CATCHER_RUNNING:
152 switch (action)
153 {
154 case CATCH_ITER:
155 /* No error/quit has occured. Just clean up. */
156 catcher_pop ();
157 return 0;
158 case CATCH_ITER_1:
159 current_catcher->state = CATCHER_RUNNING_1;
160 return 1;
161 case CATCH_THROWING:
162 current_catcher->state = CATCHER_ABORTING;
163 /* See also throw_exception. */
164 return 1;
165 default:
166 internal_error (__FILE__, __LINE__, "bad switch");
167 }
168 case CATCHER_RUNNING_1:
169 switch (action)
170 {
171 case CATCH_ITER:
172 /* The did a "break" from the inner while loop. */
173 catcher_pop ();
174 return 0;
175 case CATCH_ITER_1:
176 current_catcher->state = CATCHER_RUNNING;
177 return 0;
178 case CATCH_THROWING:
179 current_catcher->state = CATCHER_ABORTING;
180 /* See also throw_exception. */
181 return 1;
182 default:
183 internal_error (__FILE__, __LINE__, "bad switch");
184 }
185 case CATCHER_ABORTING:
186 switch (action)
187 {
188 case CATCH_ITER:
189 {
2a78bfb5
AC
190 struct exception exception = *current_catcher->exception;
191 if (current_catcher->mask & RETURN_MASK (exception.reason))
db5f402d
AC
192 {
193 /* Exit normally if this catcher can handle this
194 exception. The caller analyses the func return
195 values. */
196 catcher_pop ();
197 return 0;
198 }
199 /* The caller didn't request that the event be caught,
200 relay the event to the next containing
201 catch_errors(). */
202 catcher_pop ();
2a78bfb5 203 throw_exception (exception);
db5f402d
AC
204 }
205 default:
206 internal_error (__FILE__, __LINE__, "bad state");
207 }
208 default:
209 internal_error (__FILE__, __LINE__, "bad switch");
210 }
211}
60250e8b 212
2a78bfb5 213/* Return EXCEPTION to the nearest containing catch_errors(). */
60250e8b
AC
214
215NORETURN void
2a78bfb5 216throw_exception (struct exception exception)
60250e8b
AC
217{
218 quit_flag = 0;
219 immediate_quit = 0;
220
221 /* Perhaps it would be cleaner to do this via the cleanup chain (not sure
222 I can think of a reason why that is vital, though). */
223 bpstat_clear_actions (stop_bpstat); /* Clear queued breakpoint commands */
224
225 disable_current_display ();
226 do_cleanups (ALL_CLEANUPS);
227 if (target_can_async_p () && !target_executing)
228 do_exec_cleanups (ALL_CLEANUPS);
229 if (sync_execution)
230 do_exec_error_cleanups (ALL_CLEANUPS);
231
60250e8b
AC
232 /* Jump to the containing catch_errors() call, communicating REASON
233 to that call via setjmp's return value. Note that REASON can't
234 be zero, by definition in defs.h. */
db5f402d 235 catcher_state_machine (CATCH_THROWING);
2a78bfb5
AC
236 *current_catcher->exception = exception;
237 SIGLONGJMP (current_catcher->buf, exception.reason);
238}
239
6b1b7650
AC
240static char *last_message;
241
2a78bfb5 242NORETURN void
315a522e 243deprecated_throw_reason (enum return_reason reason)
2a78bfb5
AC
244{
245 struct exception exception;
246 memset (&exception, 0, sizeof exception);
247
248 exception.reason = reason;
249 switch (reason)
250 {
251 case RETURN_QUIT:
252 break;
253 case RETURN_ERROR:
254 exception.error = GENERIC_ERROR;
2a78bfb5
AC
255 break;
256 default:
257 internal_error (__FILE__, __LINE__, "bad switch");
258 }
259
260 throw_exception (exception);
60250e8b
AC
261}
262
6b1b7650 263static void
c6da7a6d 264print_flush (void)
6b1b7650 265{
e06e2353
AC
266 struct serial *gdb_stdout_serial;
267
c6da7a6d
AC
268 if (deprecated_error_begin_hook)
269 deprecated_error_begin_hook ();
270 target_terminal_ours ();
e06e2353
AC
271
272 /* We want all output to appear now, before we print the error. We
273 have 3 levels of buffering we have to flush (it's possible that
274 some of these should be changed to flush the lower-level ones
275 too): */
276
277 /* 1. The _filtered buffer. */
278 wrap_here ("");
279
280 /* 2. The stdio buffer. */
c6da7a6d 281 gdb_flush (gdb_stdout);
e06e2353
AC
282 gdb_flush (gdb_stderr);
283
284 /* 3. The system-level buffer. */
285 gdb_stdout_serial = serial_fdopen (1);
286 serial_drain_output (gdb_stdout_serial);
287 serial_un_fdopen (gdb_stdout_serial);
288
c6da7a6d 289 annotate_error_begin ();
6b1b7650
AC
290}
291
9cbc821d
AC
292static void
293print_exception (struct ui_file *file, struct 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 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
9cbc821d 328exception_print (struct ui_file *file, struct 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
AC
337void
338exception_fprintf (struct ui_file *file, struct exception e,
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
e48f5bee
AC
356void
357print_any_exception (struct ui_file *file, const char *prefix,
358 struct exception e)
359{
360 if (e.reason < 0 && e.message != NULL)
361 {
362 target_terminal_ours ();
363 wrap_here (""); /* Force out any buffered output */
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
6b1b7650 374NORETURN static void
3af1e0e3
AC
375throw_it (enum return_reason reason, enum errors error, const char *fmt,
376 va_list ap) ATTR_NORETURN;
6b1b7650 377NORETURN static void
3af1e0e3
AC
378throw_it (enum return_reason reason, enum errors error, const char *fmt,
379 va_list ap)
6b1b7650 380{
6b1b7650 381 struct exception e;
17d92a02 382 char *new_message;
6b1b7650 383
17d92a02
AC
384 /* Save the message. Create the new message before deleting the
385 old, the new message may include the old message text. */
386 new_message = xstrvprintf (fmt, ap);
6b1b7650 387 xfree (last_message);
17d92a02 388 last_message = new_message;
c6da7a6d
AC
389
390 /* Create the exception. */
391 e.reason = reason;
392 e.error = error;
393 e.message = last_message;
6b1b7650 394
6b1b7650 395 /* Throw the exception. */
6b1b7650
AC
396 throw_exception (e);
397}
398
399NORETURN void
400throw_verror (enum errors error, const char *fmt, va_list ap)
401{
3af1e0e3 402 throw_it (RETURN_ERROR, error, fmt, ap);
6b1b7650
AC
403}
404
405NORETURN void
406throw_vfatal (const char *fmt, va_list ap)
407{
3af1e0e3 408 throw_it (RETURN_QUIT, NO_ERROR, fmt, ap);
6b1b7650
AC
409}
410
411NORETURN void
05ff989b 412throw_error (enum errors error, const char *fmt, ...)
6b1b7650 413{
05ff989b
AC
414 va_list args;
415 va_start (args, fmt);
3af1e0e3 416 throw_it (RETURN_ERROR, error, fmt, args);
05ff989b 417 va_end (args);
6b1b7650
AC
418}
419
60250e8b
AC
420/* Call FUNC() with args FUNC_UIOUT and FUNC_ARGS, catching any
421 errors. Set FUNC_CAUGHT to an ``enum return_reason'' if the
422 function is aborted (using throw_exception() or zero if the
423 function returns normally. Set FUNC_VAL to the value returned by
424 the function or 0 if the function was aborted.
425
426 Must not be called with immediate_quit in effect (bad things might
427 happen, say we got a signal in the middle of a memcpy to quit_return).
428 This is an OK restriction; with very few exceptions immediate_quit can
429 be replaced by judicious use of QUIT.
430
431 MASK specifies what to catch; it is normally set to
432 RETURN_MASK_ALL, if for no other reason than that the code which
433 calls catch_errors might not be set up to deal with a quit which
434 isn't caught. But if the code can deal with it, it generally
435 should be RETURN_MASK_ERROR, unless for some reason it is more
436 useful to abort only the portion of the operation inside the
437 catch_errors. Note that quit should return to the command line
438 fairly quickly, even if some further processing is being done. */
439
440/* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
441 error() et.al. could maintain a set of flags that indicate the the
442 current state of each of the longjmp buffers. This would give the
443 longjmp code the chance to detect a longjmp botch (before it gets
444 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
445 code also randomly used a SET_TOP_LEVEL macro that directly
446 initialize the longjmp buffers. */
447
448/* MAYBE: cagney/1999-11-05: Should the catch_errors and cleanups code
449 be consolidated into a single file instead of being distributed
450 between utils.c and top.c? */
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
2a78bfb5
AC
461struct exception
462catch_exception (struct ui_out *uiout,
463 catch_exception_ftype *func,
464 void *func_args,
465 return_mask mask)
466{
467 volatile struct exception exception;
468 SIGJMP_BUF *catch;
6212a5e9 469 catch = catcher_init (uiout, &exception, mask);
2a78bfb5
AC
470 for (SIGSETJMP ((*catch));
471 catcher_state_machine (CATCH_ITER);)
472 (*func) (uiout, func_args);
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{
2a78bfb5
AC
483 volatile struct exception exception;
484 volatile int val = 0;
6212a5e9 485 SIGJMP_BUF *catch = catcher_init (uiout, &exception, mask);
2a78bfb5 486 for (SIGSETJMP ((*catch)); catcher_state_machine (CATCH_ITER);)
db5f402d 487 val = (*func) (uiout, func_args);
e48f5bee 488 print_any_exception (gdb_stderr, NULL, exception);
60250e8b 489 gdb_assert (val >= 0);
2a78bfb5
AC
490 gdb_assert (exception.reason <= 0);
491 if (exception.reason < 0)
492 {
493 /* If caller wants a copy of the low-level error message, make
494 one. This is used in the case of a silent error whereby the
495 caller may optionally want to issue the message. */
496 if (gdberrmsg != NULL)
6b1b7650
AC
497 {
498 if (exception.message != NULL)
499 *gdberrmsg = xstrdup (exception.message);
500 else
501 *gdberrmsg = NULL;
502 }
2a78bfb5
AC
503 return exception.reason;
504 }
60250e8b
AC
505 return val;
506}
507
60250e8b
AC
508int
509catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
510 return_mask mask)
511{
2a78bfb5
AC
512 volatile int val = 0;
513 volatile struct exception exception;
6212a5e9 514 SIGJMP_BUF *catch = catcher_init (uiout, &exception, mask);
db5f402d
AC
515 /* This illustrates how it is possible to nest the mechanism and
516 hence catch "break". Of course this doesn't address the need to
517 also catch "return". */
2a78bfb5
AC
518 for (SIGSETJMP ((*catch)); catcher_state_machine (CATCH_ITER);)
519 val = func (func_args);
e48f5bee 520 print_any_exception (gdb_stderr, errstring, exception);
2a78bfb5 521 if (exception.reason != 0)
60250e8b
AC
522 return 0;
523 return val;
524}
525
60250e8b
AC
526int
527catch_command_errors (catch_command_errors_ftype * command,
528 char *arg, int from_tty, return_mask mask)
529{
5a14cc1a 530 volatile struct exception e;
6212a5e9 531 SIGJMP_BUF *catch = catcher_init (uiout, &e, mask);
5a14cc1a
AC
532 for (SIGSETJMP ((*catch)); catcher_state_machine (CATCH_ITER);)
533 command (arg, from_tty);
534 print_any_exception (gdb_stderr, NULL, e);
535 if (e.reason < 0)
536 return 0;
537 return 1;
60250e8b 538}
This page took 0.047821 seconds and 4 git commands to generate.