Commit | Line | Data |
---|---|---|
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" | |
60250e8b AC |
26 | #include "breakpoint.h" |
27 | #include "target.h" | |
28 | #include "inferior.h" | |
29 | #include "annotate.h" | |
30 | #include "ui-out.h" | |
31 | #include "gdb_assert.h" | |
db5f402d | 32 | #include "gdb_string.h" |
e06e2353 | 33 | #include "serial.h" |
60250e8b | 34 | |
71fff37b | 35 | const struct gdb_exception exception_none = { 0, NO_ERROR, NULL }; |
c1043fc2 | 36 | |
db5f402d AC |
37 | /* Possible catcher states. */ |
38 | enum catcher_state { | |
39 | /* Initial state, a new catcher has just been created. */ | |
40 | CATCHER_CREATED, | |
41 | /* The catch code is running. */ | |
42 | CATCHER_RUNNING, | |
43 | CATCHER_RUNNING_1, | |
44 | /* The catch code threw an exception. */ | |
45 | CATCHER_ABORTING | |
46 | }; | |
47 | ||
48 | /* Possible catcher actions. */ | |
49 | enum catcher_action { | |
50 | CATCH_ITER, | |
51 | CATCH_ITER_1, | |
52 | CATCH_THROWING | |
53 | }; | |
54 | ||
55 | struct catcher | |
56 | { | |
57 | enum catcher_state state; | |
2a78bfb5 | 58 | /* Jump buffer pointing back at the exception handler. */ |
6941d02a | 59 | EXCEPTIONS_SIGJMP_BUF buf; |
8a076db9 | 60 | /* Status buffer belonging to the exception handler. */ |
71fff37b | 61 | volatile struct gdb_exception *exception; |
db5f402d AC |
62 | /* Saved/current state. */ |
63 | int mask; | |
db5f402d AC |
64 | struct ui_out *saved_uiout; |
65 | struct cleanup *saved_cleanup_chain; | |
db5f402d AC |
66 | /* Back link. */ |
67 | struct catcher *prev; | |
68 | }; | |
69 | ||
60250e8b | 70 | /* Where to go for throw_exception(). */ |
db5f402d AC |
71 | static struct catcher *current_catcher; |
72 | ||
6941d02a AC |
73 | EXCEPTIONS_SIGJMP_BUF * |
74 | exceptions_state_mc_init (struct ui_out *func_uiout, | |
71fff37b | 75 | volatile struct gdb_exception *exception, |
6941d02a | 76 | return_mask mask) |
db5f402d AC |
77 | { |
78 | struct catcher *new_catcher = XZALLOC (struct catcher); | |
79 | ||
2a78bfb5 AC |
80 | /* Start with no exception, save it's address. */ |
81 | exception->reason = 0; | |
82 | exception->error = NO_ERROR; | |
83 | exception->message = NULL; | |
84 | new_catcher->exception = exception; | |
85 | ||
db5f402d AC |
86 | new_catcher->mask = mask; |
87 | ||
db5f402d AC |
88 | /* Override the global ``struct ui_out'' builder. */ |
89 | new_catcher->saved_uiout = uiout; | |
90 | uiout = func_uiout; | |
91 | ||
92 | /* Prevent error/quit during FUNC from calling cleanups established | |
93 | prior to here. */ | |
94 | new_catcher->saved_cleanup_chain = save_cleanups (); | |
95 | ||
96 | /* Push this new catcher on the top. */ | |
97 | new_catcher->prev = current_catcher; | |
98 | current_catcher = new_catcher; | |
99 | new_catcher->state = CATCHER_CREATED; | |
100 | ||
101 | return &new_catcher->buf; | |
102 | } | |
103 | ||
104 | static void | |
105 | catcher_pop (void) | |
106 | { | |
107 | struct catcher *old_catcher = current_catcher; | |
108 | current_catcher = old_catcher->prev; | |
109 | ||
110 | /* Restore the cleanup chain, the error/quit messages, and the uiout | |
111 | builder, to their original states. */ | |
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 |
123 | static int |
124 | exceptions_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; |
2a78bfb5 | 178 | if (current_catcher->mask & RETURN_MASK (exception.reason)) |
db5f402d AC |
179 | { |
180 | /* Exit normally if this catcher can handle this | |
181 | exception. The caller analyses the func return | |
182 | values. */ | |
183 | catcher_pop (); | |
184 | return 0; | |
185 | } | |
186 | /* The caller didn't request that the event be caught, | |
187 | relay the event to the next containing | |
188 | catch_errors(). */ | |
189 | catcher_pop (); | |
2a78bfb5 | 190 | throw_exception (exception); |
db5f402d AC |
191 | } |
192 | default: | |
e2e0b3e5 | 193 | internal_error (__FILE__, __LINE__, _("bad state")); |
db5f402d AC |
194 | } |
195 | default: | |
e2e0b3e5 | 196 | internal_error (__FILE__, __LINE__, _("bad switch")); |
db5f402d AC |
197 | } |
198 | } | |
60250e8b | 199 | |
6941d02a AC |
200 | int |
201 | exceptions_state_mc_action_iter (void) | |
202 | { | |
203 | return exceptions_state_mc (CATCH_ITER); | |
204 | } | |
205 | ||
206 | int | |
207 | exceptions_state_mc_action_iter_1 (void) | |
208 | { | |
209 | return exceptions_state_mc (CATCH_ITER_1); | |
210 | } | |
211 | ||
2a78bfb5 | 212 | /* Return EXCEPTION to the nearest containing catch_errors(). */ |
60250e8b AC |
213 | |
214 | NORETURN void | |
71fff37b | 215 | throw_exception (struct gdb_exception exception) |
60250e8b AC |
216 | { |
217 | quit_flag = 0; | |
218 | immediate_quit = 0; | |
219 | ||
220 | /* Perhaps it would be cleaner to do this via the cleanup chain (not sure | |
221 | I can think of a reason why that is vital, though). */ | |
222 | bpstat_clear_actions (stop_bpstat); /* Clear queued breakpoint commands */ | |
223 | ||
224 | disable_current_display (); | |
225 | do_cleanups (ALL_CLEANUPS); | |
226 | if (target_can_async_p () && !target_executing) | |
227 | do_exec_cleanups (ALL_CLEANUPS); | |
228 | if (sync_execution) | |
229 | do_exec_error_cleanups (ALL_CLEANUPS); | |
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 |
239 | static char *last_message; |
240 | ||
2a78bfb5 | 241 | NORETURN void |
315a522e | 242 | deprecated_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 | 262 | static void |
c6da7a6d | 263 | print_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 | 294 | static void |
71fff37b | 295 | print_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 | 329 | void |
71fff37b | 330 | exception_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 | 339 | void |
71fff37b | 340 | exception_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 |
358 | void |
359 | print_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 | ||
6b1b7650 | 376 | NORETURN static void |
3af1e0e3 AC |
377 | throw_it (enum return_reason reason, enum errors error, const char *fmt, |
378 | va_list ap) ATTR_NORETURN; | |
6b1b7650 | 379 | NORETURN static void |
3af1e0e3 AC |
380 | throw_it (enum return_reason reason, enum errors error, const char *fmt, |
381 | va_list ap) | |
6b1b7650 | 382 | { |
71fff37b | 383 | struct gdb_exception e; |
17d92a02 | 384 | char *new_message; |
6b1b7650 | 385 | |
17d92a02 AC |
386 | /* Save the message. Create the new message before deleting the |
387 | old, the new message may include the old message text. */ | |
388 | new_message = xstrvprintf (fmt, ap); | |
6b1b7650 | 389 | xfree (last_message); |
17d92a02 | 390 | last_message = new_message; |
c6da7a6d AC |
391 | |
392 | /* Create the exception. */ | |
393 | e.reason = reason; | |
394 | e.error = error; | |
395 | e.message = last_message; | |
6b1b7650 | 396 | |
6b1b7650 | 397 | /* Throw the exception. */ |
6b1b7650 AC |
398 | throw_exception (e); |
399 | } | |
400 | ||
401 | NORETURN void | |
402 | throw_verror (enum errors error, const char *fmt, va_list ap) | |
403 | { | |
3af1e0e3 | 404 | throw_it (RETURN_ERROR, error, fmt, ap); |
6b1b7650 AC |
405 | } |
406 | ||
407 | NORETURN void | |
408 | throw_vfatal (const char *fmt, va_list ap) | |
409 | { | |
3af1e0e3 | 410 | throw_it (RETURN_QUIT, NO_ERROR, fmt, ap); |
6b1b7650 AC |
411 | } |
412 | ||
413 | NORETURN void | |
05ff989b | 414 | throw_error (enum errors error, const char *fmt, ...) |
6b1b7650 | 415 | { |
05ff989b AC |
416 | va_list args; |
417 | va_start (args, fmt); | |
3af1e0e3 | 418 | throw_it (RETURN_ERROR, error, fmt, args); |
05ff989b | 419 | va_end (args); |
6b1b7650 AC |
420 | } |
421 | ||
60250e8b AC |
422 | /* Call FUNC() with args FUNC_UIOUT and FUNC_ARGS, catching any |
423 | errors. Set FUNC_CAUGHT to an ``enum return_reason'' if the | |
424 | function is aborted (using throw_exception() or zero if the | |
425 | function returns normally. Set FUNC_VAL to the value returned by | |
426 | the function or 0 if the function was aborted. | |
427 | ||
428 | Must not be called with immediate_quit in effect (bad things might | |
429 | happen, say we got a signal in the middle of a memcpy to quit_return). | |
430 | This is an OK restriction; with very few exceptions immediate_quit can | |
431 | be replaced by judicious use of QUIT. | |
432 | ||
433 | MASK specifies what to catch; it is normally set to | |
434 | RETURN_MASK_ALL, if for no other reason than that the code which | |
435 | calls catch_errors might not be set up to deal with a quit which | |
436 | isn't caught. But if the code can deal with it, it generally | |
437 | should be RETURN_MASK_ERROR, unless for some reason it is more | |
438 | useful to abort only the portion of the operation inside the | |
439 | catch_errors. Note that quit should return to the command line | |
440 | fairly quickly, even if some further processing is being done. */ | |
441 | ||
442 | /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with | |
443 | error() et.al. could maintain a set of flags that indicate the the | |
444 | current state of each of the longjmp buffers. This would give the | |
445 | longjmp code the chance to detect a longjmp botch (before it gets | |
446 | to longjmperror()). Prior to 1999-11-05 this wasn't possible as | |
447 | code also randomly used a SET_TOP_LEVEL macro that directly | |
448 | initialize the longjmp buffers. */ | |
449 | ||
450 | /* MAYBE: cagney/1999-11-05: Should the catch_errors and cleanups code | |
451 | be consolidated into a single file instead of being distributed | |
452 | between utils.c and top.c? */ | |
453 | ||
60250e8b AC |
454 | int |
455 | catch_exceptions (struct ui_out *uiout, | |
456 | catch_exceptions_ftype *func, | |
457 | void *func_args, | |
60250e8b AC |
458 | return_mask mask) |
459 | { | |
1c3c7ee7 | 460 | return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask); |
60250e8b AC |
461 | } |
462 | ||
71fff37b | 463 | struct gdb_exception |
2a78bfb5 AC |
464 | catch_exception (struct ui_out *uiout, |
465 | catch_exception_ftype *func, | |
466 | void *func_args, | |
467 | return_mask mask) | |
468 | { | |
71fff37b | 469 | volatile struct gdb_exception exception; |
6941d02a AC |
470 | TRY_CATCH (exception, mask) |
471 | { | |
472 | (*func) (uiout, func_args); | |
473 | } | |
2a78bfb5 AC |
474 | return exception; |
475 | } | |
476 | ||
60250e8b AC |
477 | int |
478 | catch_exceptions_with_msg (struct ui_out *uiout, | |
479 | catch_exceptions_ftype *func, | |
480 | void *func_args, | |
60250e8b AC |
481 | char **gdberrmsg, |
482 | return_mask mask) | |
483 | { | |
71fff37b | 484 | volatile struct gdb_exception exception; |
2a78bfb5 | 485 | volatile int val = 0; |
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 | ||
60250e8b AC |
510 | int |
511 | catch_errors (catch_errors_ftype *func, void *func_args, char *errstring, | |
512 | return_mask mask) | |
513 | { | |
2a78bfb5 | 514 | volatile int val = 0; |
71fff37b | 515 | volatile struct gdb_exception exception; |
6941d02a AC |
516 | TRY_CATCH (exception, mask) |
517 | { | |
518 | val = func (func_args); | |
519 | } | |
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 |
526 | int |
527 | catch_command_errors (catch_command_errors_ftype * command, | |
528 | char *arg, int from_tty, return_mask mask) | |
529 | { | |
71fff37b | 530 | volatile struct gdb_exception e; |
6941d02a AC |
531 | TRY_CATCH (e, mask) |
532 | { | |
533 | command (arg, from_tty); | |
534 | } | |
5a14cc1a AC |
535 | print_any_exception (gdb_stderr, NULL, e); |
536 | if (e.reason < 0) | |
537 | return 0; | |
538 | return 1; | |
60250e8b | 539 | } |