* thread.c (make_cleanup_restore_current_thread): Make it
[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,
9b254dd1 4 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
7b871fab 5 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"
60250e8b 32
7b871fab 33const struct gdb_exception exception_none = { 0, GDB_NO_ERROR, NULL };
c1043fc2 34
db5f402d
AC
35/* Possible catcher states. */
36enum catcher_state {
37 /* Initial state, a new catcher has just been created. */
38 CATCHER_CREATED,
39 /* The catch code is running. */
40 CATCHER_RUNNING,
41 CATCHER_RUNNING_1,
42 /* The catch code threw an exception. */
43 CATCHER_ABORTING
44};
45
46/* Possible catcher actions. */
47enum catcher_action {
48 CATCH_ITER,
49 CATCH_ITER_1,
50 CATCH_THROWING
51};
52
53struct catcher
54{
55 enum catcher_state state;
2a78bfb5 56 /* Jump buffer pointing back at the exception handler. */
6941d02a 57 EXCEPTIONS_SIGJMP_BUF buf;
8a076db9 58 /* Status buffer belonging to the exception handler. */
71fff37b 59 volatile struct gdb_exception *exception;
db5f402d
AC
60 /* Saved/current state. */
61 int mask;
db5f402d
AC
62 struct ui_out *saved_uiout;
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
6941d02a
AC
71EXCEPTIONS_SIGJMP_BUF *
72exceptions_state_mc_init (struct ui_out *func_uiout,
71fff37b 73 volatile struct gdb_exception *exception,
6941d02a 74 return_mask mask)
db5f402d
AC
75{
76 struct catcher *new_catcher = XZALLOC (struct catcher);
77
2a78bfb5
AC
78 /* Start with no exception, save it's address. */
79 exception->reason = 0;
7b871fab 80 exception->error = GDB_NO_ERROR;
2a78bfb5
AC
81 exception->message = NULL;
82 new_catcher->exception = exception;
83
db5f402d
AC
84 new_catcher->mask = mask;
85
db5f402d
AC
86 /* Override the global ``struct ui_out'' builder. */
87 new_catcher->saved_uiout = uiout;
88 uiout = func_uiout;
89
90 /* Prevent error/quit during FUNC from calling cleanups established
91 prior to here. */
92 new_catcher->saved_cleanup_chain = save_cleanups ();
93
94 /* Push this new catcher on the top. */
95 new_catcher->prev = current_catcher;
96 current_catcher = new_catcher;
97 new_catcher->state = CATCHER_CREATED;
98
99 return &new_catcher->buf;
100}
101
102static void
103catcher_pop (void)
104{
105 struct catcher *old_catcher = current_catcher;
106 current_catcher = old_catcher->prev;
107
108 /* Restore the cleanup chain, the error/quit messages, and the uiout
109 builder, to their original states. */
110
111 restore_cleanups (old_catcher->saved_cleanup_chain);
112
113 uiout = old_catcher->saved_uiout;
114
db5f402d
AC
115 xfree (old_catcher);
116}
117
118/* Catcher state machine. Returns non-zero if the m/c should be run
119 again, zero if it should abort. */
120
6941d02a
AC
121static int
122exceptions_state_mc (enum catcher_action action)
db5f402d
AC
123{
124 switch (current_catcher->state)
125 {
126 case CATCHER_CREATED:
127 switch (action)
128 {
129 case CATCH_ITER:
130 /* Allow the code to run the catcher. */
131 current_catcher->state = CATCHER_RUNNING;
132 return 1;
133 default:
e2e0b3e5 134 internal_error (__FILE__, __LINE__, _("bad state"));
db5f402d
AC
135 }
136 case CATCHER_RUNNING:
137 switch (action)
138 {
139 case CATCH_ITER:
140 /* No error/quit has occured. Just clean up. */
141 catcher_pop ();
142 return 0;
143 case CATCH_ITER_1:
144 current_catcher->state = CATCHER_RUNNING_1;
145 return 1;
146 case CATCH_THROWING:
147 current_catcher->state = CATCHER_ABORTING;
148 /* See also throw_exception. */
149 return 1;
150 default:
e2e0b3e5 151 internal_error (__FILE__, __LINE__, _("bad switch"));
db5f402d
AC
152 }
153 case CATCHER_RUNNING_1:
154 switch (action)
155 {
156 case CATCH_ITER:
157 /* The did a "break" from the inner while loop. */
158 catcher_pop ();
159 return 0;
160 case CATCH_ITER_1:
161 current_catcher->state = CATCHER_RUNNING;
162 return 0;
163 case CATCH_THROWING:
164 current_catcher->state = CATCHER_ABORTING;
165 /* See also throw_exception. */
166 return 1;
167 default:
e2e0b3e5 168 internal_error (__FILE__, __LINE__, _("bad switch"));
db5f402d
AC
169 }
170 case CATCHER_ABORTING:
171 switch (action)
172 {
173 case CATCH_ITER:
174 {
71fff37b 175 struct gdb_exception exception = *current_catcher->exception;
2a78bfb5 176 if (current_catcher->mask & RETURN_MASK (exception.reason))
db5f402d
AC
177 {
178 /* Exit normally if this catcher can handle this
179 exception. The caller analyses the func return
180 values. */
181 catcher_pop ();
182 return 0;
183 }
184 /* The caller didn't request that the event be caught,
185 relay the event to the next containing
186 catch_errors(). */
187 catcher_pop ();
2a78bfb5 188 throw_exception (exception);
db5f402d
AC
189 }
190 default:
e2e0b3e5 191 internal_error (__FILE__, __LINE__, _("bad state"));
db5f402d
AC
192 }
193 default:
e2e0b3e5 194 internal_error (__FILE__, __LINE__, _("bad switch"));
db5f402d
AC
195 }
196}
60250e8b 197
6941d02a
AC
198int
199exceptions_state_mc_action_iter (void)
200{
201 return exceptions_state_mc (CATCH_ITER);
202}
203
204int
205exceptions_state_mc_action_iter_1 (void)
206{
207 return exceptions_state_mc (CATCH_ITER_1);
208}
209
2a78bfb5 210/* Return EXCEPTION to the nearest containing catch_errors(). */
60250e8b
AC
211
212NORETURN void
71fff37b 213throw_exception (struct gdb_exception exception)
60250e8b
AC
214{
215 quit_flag = 0;
216 immediate_quit = 0;
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 bpstat_clear_actions (stop_bpstat); /* Clear queued breakpoint commands */
221
222 disable_current_display ();
223 do_cleanups (ALL_CLEANUPS);
32c1e744
VP
224 /* When we implement non-stop mode, this should be redone. If we get
225 exception in a command pertaining to one thread, or maybe even not
226 involving inferior at all, we should not do exec cleanups for all
227 threads. */
60250e8b
AC
228 if (target_can_async_p () && !target_executing)
229 do_exec_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
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
6b1b7650
AC
239static char *last_message;
240
2a78bfb5 241NORETURN void
315a522e 242deprecated_throw_reason (enum return_reason reason)
2a78bfb5 243{
71fff37b 244 struct gdb_exception exception;
2a78bfb5
AC
245 memset (&exception, 0, sizeof exception);
246
247 exception.reason = reason;
248 switch (reason)
249 {
250 case RETURN_QUIT:
251 break;
252 case RETURN_ERROR:
253 exception.error = GENERIC_ERROR;
2a78bfb5
AC
254 break;
255 default:
e2e0b3e5 256 internal_error (__FILE__, __LINE__, _("bad switch"));
2a78bfb5
AC
257 }
258
259 throw_exception (exception);
60250e8b
AC
260}
261
6b1b7650 262static void
c6da7a6d 263print_flush (void)
6b1b7650 264{
e06e2353
AC
265 struct serial *gdb_stdout_serial;
266
c6da7a6d
AC
267 if (deprecated_error_begin_hook)
268 deprecated_error_begin_hook ();
269 target_terminal_ours ();
e06e2353
AC
270
271 /* We want all output to appear now, before we print the error. We
272 have 3 levels of buffering we have to flush (it's possible that
273 some of these should be changed to flush the lower-level ones
274 too): */
275
276 /* 1. The _filtered buffer. */
277 wrap_here ("");
278
279 /* 2. The stdio buffer. */
c6da7a6d 280 gdb_flush (gdb_stdout);
e06e2353
AC
281 gdb_flush (gdb_stderr);
282
283 /* 3. The system-level buffer. */
284 gdb_stdout_serial = serial_fdopen (1);
cade9e54
PB
285 if (gdb_stdout_serial)
286 {
287 serial_drain_output (gdb_stdout_serial);
288 serial_un_fdopen (gdb_stdout_serial);
289 }
e06e2353 290
c6da7a6d 291 annotate_error_begin ();
6b1b7650
AC
292}
293
9cbc821d 294static void
71fff37b 295print_exception (struct ui_file *file, struct gdb_exception e)
9cbc821d
AC
296{
297 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
298 as that way the MI's behavior is preserved. */
299 const char *start;
300 const char *end;
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
e48f5bee
AC
358void
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 ();
365 wrap_here (""); /* Force out any buffered output */
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
bee0189a 376NORETURN static void ATTR_NORETURN ATTR_FORMAT (printf, 3, 0)
3af1e0e3
AC
377throw_it (enum return_reason reason, enum errors error, const char *fmt,
378 va_list ap)
6b1b7650 379{
71fff37b 380 struct gdb_exception e;
17d92a02 381 char *new_message;
6b1b7650 382
17d92a02
AC
383 /* Save the message. Create the new message before deleting the
384 old, the new message may include the old message text. */
385 new_message = xstrvprintf (fmt, ap);
6b1b7650 386 xfree (last_message);
17d92a02 387 last_message = new_message;
c6da7a6d
AC
388
389 /* Create the exception. */
390 e.reason = reason;
391 e.error = error;
392 e.message = last_message;
6b1b7650 393
6b1b7650 394 /* Throw the exception. */
6b1b7650
AC
395 throw_exception (e);
396}
397
398NORETURN void
399throw_verror (enum errors error, const char *fmt, va_list ap)
400{
3af1e0e3 401 throw_it (RETURN_ERROR, error, fmt, ap);
6b1b7650
AC
402}
403
404NORETURN void
405throw_vfatal (const char *fmt, va_list ap)
406{
7b871fab 407 throw_it (RETURN_QUIT, GDB_NO_ERROR, fmt, ap);
6b1b7650
AC
408}
409
410NORETURN void
05ff989b 411throw_error (enum errors error, const char *fmt, ...)
6b1b7650 412{
05ff989b
AC
413 va_list args;
414 va_start (args, fmt);
3af1e0e3 415 throw_it (RETURN_ERROR, error, fmt, args);
05ff989b 416 va_end (args);
6b1b7650
AC
417}
418
60250e8b
AC
419/* Call FUNC() with args FUNC_UIOUT and FUNC_ARGS, catching any
420 errors. Set FUNC_CAUGHT to an ``enum return_reason'' if the
421 function is aborted (using throw_exception() or zero if the
422 function returns normally. Set FUNC_VAL to the value returned by
423 the function or 0 if the function was aborted.
424
425 Must not be called with immediate_quit in effect (bad things might
426 happen, say we got a signal in the middle of a memcpy to quit_return).
427 This is an OK restriction; with very few exceptions immediate_quit can
428 be replaced by judicious use of QUIT.
429
430 MASK specifies what to catch; it is normally set to
431 RETURN_MASK_ALL, if for no other reason than that the code which
432 calls catch_errors might not be set up to deal with a quit which
433 isn't caught. But if the code can deal with it, it generally
434 should be RETURN_MASK_ERROR, unless for some reason it is more
435 useful to abort only the portion of the operation inside the
436 catch_errors. Note that quit should return to the command line
437 fairly quickly, even if some further processing is being done. */
438
439/* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
440 error() et.al. could maintain a set of flags that indicate the the
441 current state of each of the longjmp buffers. This would give the
442 longjmp code the chance to detect a longjmp botch (before it gets
443 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
444 code also randomly used a SET_TOP_LEVEL macro that directly
445 initialize the longjmp buffers. */
446
447/* MAYBE: cagney/1999-11-05: Should the catch_errors and cleanups code
448 be consolidated into a single file instead of being distributed
449 between utils.c and top.c? */
450
60250e8b
AC
451int
452catch_exceptions (struct ui_out *uiout,
453 catch_exceptions_ftype *func,
454 void *func_args,
60250e8b
AC
455 return_mask mask)
456{
1c3c7ee7 457 return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask);
60250e8b
AC
458}
459
71fff37b 460struct gdb_exception
2a78bfb5
AC
461catch_exception (struct ui_out *uiout,
462 catch_exception_ftype *func,
463 void *func_args,
464 return_mask mask)
465{
71fff37b 466 volatile struct gdb_exception exception;
6941d02a
AC
467 TRY_CATCH (exception, mask)
468 {
469 (*func) (uiout, func_args);
470 }
2a78bfb5
AC
471 return exception;
472}
473
60250e8b
AC
474int
475catch_exceptions_with_msg (struct ui_out *uiout,
476 catch_exceptions_ftype *func,
477 void *func_args,
60250e8b
AC
478 char **gdberrmsg,
479 return_mask mask)
480{
71fff37b 481 volatile struct gdb_exception exception;
2a78bfb5 482 volatile int val = 0;
6941d02a
AC
483 TRY_CATCH (exception, mask)
484 {
485 val = (*func) (uiout, func_args);
486 }
e48f5bee 487 print_any_exception (gdb_stderr, NULL, exception);
60250e8b 488 gdb_assert (val >= 0);
2a78bfb5
AC
489 gdb_assert (exception.reason <= 0);
490 if (exception.reason < 0)
491 {
492 /* If caller wants a copy of the low-level error message, make
493 one. This is used in the case of a silent error whereby the
494 caller may optionally want to issue the message. */
495 if (gdberrmsg != NULL)
6b1b7650
AC
496 {
497 if (exception.message != NULL)
498 *gdberrmsg = xstrdup (exception.message);
499 else
500 *gdberrmsg = NULL;
501 }
2a78bfb5
AC
502 return exception.reason;
503 }
60250e8b
AC
504 return val;
505}
506
60250e8b
AC
507int
508catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
509 return_mask mask)
510{
2a78bfb5 511 volatile int val = 0;
71fff37b 512 volatile struct gdb_exception exception;
6941d02a
AC
513 TRY_CATCH (exception, mask)
514 {
515 val = func (func_args);
516 }
e48f5bee 517 print_any_exception (gdb_stderr, errstring, exception);
2a78bfb5 518 if (exception.reason != 0)
60250e8b
AC
519 return 0;
520 return val;
521}
522
60250e8b
AC
523int
524catch_command_errors (catch_command_errors_ftype * command,
525 char *arg, int from_tty, return_mask mask)
526{
71fff37b 527 volatile struct gdb_exception e;
6941d02a
AC
528 TRY_CATCH (e, mask)
529 {
530 command (arg, from_tty);
531 }
5a14cc1a
AC
532 print_any_exception (gdb_stderr, NULL, e);
533 if (e.reason < 0)
534 return 0;
535 return 1;
60250e8b 536}
This page took 0.229721 seconds and 4 git commands to generate.