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" | |
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" |
60250e8b | 34 | |
c1043fc2 AC |
35 | const struct exception exception_none = { 0, NO_ERROR, NULL }; |
36 | ||
60250e8b AC |
37 | /* One should use catch_errors rather than manipulating these |
38 | directly. */ | |
39 | #if defined(HAVE_SIGSETJMP) | |
40 | #define SIGJMP_BUF sigjmp_buf | |
41 | #define SIGSETJMP(buf) sigsetjmp((buf), 1) | |
42 | #define SIGLONGJMP(buf,val) siglongjmp((buf), (val)) | |
43 | #else | |
44 | #define SIGJMP_BUF jmp_buf | |
45 | #define SIGSETJMP(buf) setjmp(buf) | |
46 | #define SIGLONGJMP(buf,val) longjmp((buf), (val)) | |
47 | #endif | |
48 | ||
db5f402d AC |
49 | /* Possible catcher states. */ |
50 | enum catcher_state { | |
51 | /* Initial state, a new catcher has just been created. */ | |
52 | CATCHER_CREATED, | |
53 | /* The catch code is running. */ | |
54 | CATCHER_RUNNING, | |
55 | CATCHER_RUNNING_1, | |
56 | /* The catch code threw an exception. */ | |
57 | CATCHER_ABORTING | |
58 | }; | |
59 | ||
60 | /* Possible catcher actions. */ | |
61 | enum catcher_action { | |
62 | CATCH_ITER, | |
63 | CATCH_ITER_1, | |
64 | CATCH_THROWING | |
65 | }; | |
66 | ||
67 | struct catcher | |
68 | { | |
69 | enum catcher_state state; | |
2a78bfb5 | 70 | /* Jump buffer pointing back at the exception handler. */ |
db5f402d | 71 | SIGJMP_BUF buf; |
8a076db9 | 72 | /* Status buffer belonging to the exception handler. */ |
2a78bfb5 | 73 | volatile struct exception *exception; |
db5f402d AC |
74 | /* Saved/current state. */ |
75 | int mask; | |
db5f402d AC |
76 | struct ui_out *saved_uiout; |
77 | struct cleanup *saved_cleanup_chain; | |
db5f402d AC |
78 | /* Back link. */ |
79 | struct catcher *prev; | |
80 | }; | |
81 | ||
60250e8b | 82 | /* Where to go for throw_exception(). */ |
db5f402d AC |
83 | static struct catcher *current_catcher; |
84 | ||
85 | static SIGJMP_BUF * | |
86 | catcher_init (struct ui_out *func_uiout, | |
2a78bfb5 | 87 | volatile struct exception *exception, |
3af1e0e3 | 88 | return_mask mask) |
db5f402d AC |
89 | { |
90 | struct catcher *new_catcher = XZALLOC (struct catcher); | |
91 | ||
2a78bfb5 AC |
92 | /* Start with no exception, save it's address. */ |
93 | exception->reason = 0; | |
94 | exception->error = NO_ERROR; | |
95 | exception->message = NULL; | |
96 | new_catcher->exception = exception; | |
97 | ||
db5f402d AC |
98 | new_catcher->mask = mask; |
99 | ||
db5f402d AC |
100 | /* Override the global ``struct ui_out'' builder. */ |
101 | new_catcher->saved_uiout = uiout; | |
102 | uiout = func_uiout; | |
103 | ||
104 | /* Prevent error/quit during FUNC from calling cleanups established | |
105 | prior to here. */ | |
106 | new_catcher->saved_cleanup_chain = save_cleanups (); | |
107 | ||
108 | /* Push this new catcher on the top. */ | |
109 | new_catcher->prev = current_catcher; | |
110 | current_catcher = new_catcher; | |
111 | new_catcher->state = CATCHER_CREATED; | |
112 | ||
113 | return &new_catcher->buf; | |
114 | } | |
115 | ||
116 | static void | |
117 | catcher_pop (void) | |
118 | { | |
119 | struct catcher *old_catcher = current_catcher; | |
120 | current_catcher = old_catcher->prev; | |
121 | ||
122 | /* Restore the cleanup chain, the error/quit messages, and the uiout | |
123 | builder, to their original states. */ | |
124 | ||
125 | restore_cleanups (old_catcher->saved_cleanup_chain); | |
126 | ||
127 | uiout = old_catcher->saved_uiout; | |
128 | ||
db5f402d AC |
129 | xfree (old_catcher); |
130 | } | |
131 | ||
132 | /* Catcher state machine. Returns non-zero if the m/c should be run | |
133 | again, zero if it should abort. */ | |
134 | ||
135 | int | |
136 | catcher_state_machine (enum catcher_action action) | |
137 | { | |
138 | switch (current_catcher->state) | |
139 | { | |
140 | case CATCHER_CREATED: | |
141 | switch (action) | |
142 | { | |
143 | case CATCH_ITER: | |
144 | /* Allow the code to run the catcher. */ | |
145 | current_catcher->state = CATCHER_RUNNING; | |
146 | return 1; | |
147 | default: | |
148 | internal_error (__FILE__, __LINE__, "bad state"); | |
149 | } | |
150 | case CATCHER_RUNNING: | |
151 | switch (action) | |
152 | { | |
153 | case CATCH_ITER: | |
154 | /* No error/quit has occured. Just clean up. */ | |
155 | catcher_pop (); | |
156 | return 0; | |
157 | case CATCH_ITER_1: | |
158 | current_catcher->state = CATCHER_RUNNING_1; | |
159 | return 1; | |
160 | case CATCH_THROWING: | |
161 | current_catcher->state = CATCHER_ABORTING; | |
162 | /* See also throw_exception. */ | |
163 | return 1; | |
164 | default: | |
165 | internal_error (__FILE__, __LINE__, "bad switch"); | |
166 | } | |
167 | case CATCHER_RUNNING_1: | |
168 | switch (action) | |
169 | { | |
170 | case CATCH_ITER: | |
171 | /* The did a "break" from the inner while loop. */ | |
172 | catcher_pop (); | |
173 | return 0; | |
174 | case CATCH_ITER_1: | |
175 | current_catcher->state = CATCHER_RUNNING; | |
176 | return 0; | |
177 | case CATCH_THROWING: | |
178 | current_catcher->state = CATCHER_ABORTING; | |
179 | /* See also throw_exception. */ | |
180 | return 1; | |
181 | default: | |
182 | internal_error (__FILE__, __LINE__, "bad switch"); | |
183 | } | |
184 | case CATCHER_ABORTING: | |
185 | switch (action) | |
186 | { | |
187 | case CATCH_ITER: | |
188 | { | |
2a78bfb5 AC |
189 | struct exception exception = *current_catcher->exception; |
190 | if (current_catcher->mask & RETURN_MASK (exception.reason)) | |
db5f402d AC |
191 | { |
192 | /* Exit normally if this catcher can handle this | |
193 | exception. The caller analyses the func return | |
194 | values. */ | |
195 | catcher_pop (); | |
196 | return 0; | |
197 | } | |
198 | /* The caller didn't request that the event be caught, | |
199 | relay the event to the next containing | |
200 | catch_errors(). */ | |
201 | catcher_pop (); | |
2a78bfb5 | 202 | throw_exception (exception); |
db5f402d AC |
203 | } |
204 | default: | |
205 | internal_error (__FILE__, __LINE__, "bad state"); | |
206 | } | |
207 | default: | |
208 | internal_error (__FILE__, __LINE__, "bad switch"); | |
209 | } | |
210 | } | |
60250e8b | 211 | |
2a78bfb5 | 212 | /* Return EXCEPTION to the nearest containing catch_errors(). */ |
60250e8b AC |
213 | |
214 | NORETURN void | |
2a78bfb5 | 215 | throw_exception (struct 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. */ | |
db5f402d | 234 | catcher_state_machine (CATCH_THROWING); |
2a78bfb5 AC |
235 | *current_catcher->exception = exception; |
236 | SIGLONGJMP (current_catcher->buf, exception.reason); | |
237 | } | |
238 | ||
6b1b7650 AC |
239 | static char *last_message; |
240 | ||
2a78bfb5 | 241 | NORETURN void |
315a522e | 242 | deprecated_throw_reason (enum return_reason reason) |
2a78bfb5 AC |
243 | { |
244 | struct exception exception; | |
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: | |
256 | internal_error (__FILE__, __LINE__, "bad switch"); | |
257 | } | |
258 | ||
259 | throw_exception (exception); | |
60250e8b AC |
260 | } |
261 | ||
6b1b7650 | 262 | static void |
c6da7a6d | 263 | print_flush (void) |
6b1b7650 | 264 | { |
c6da7a6d AC |
265 | if (deprecated_error_begin_hook) |
266 | deprecated_error_begin_hook (); | |
267 | target_terminal_ours (); | |
268 | wrap_here (""); /* Force out any buffered output */ | |
269 | gdb_flush (gdb_stdout); | |
270 | annotate_error_begin (); | |
6b1b7650 AC |
271 | } |
272 | ||
9cbc821d AC |
273 | static void |
274 | print_exception (struct ui_file *file, struct exception e) | |
275 | { | |
276 | /* KLUGE: cagney/2005-01-13: Write the string out one line at a time | |
277 | as that way the MI's behavior is preserved. */ | |
278 | const char *start; | |
279 | const char *end; | |
280 | for (start = e.message; start != NULL; start = end) | |
281 | { | |
282 | end = strchr (start, '\n'); | |
283 | if (end == NULL) | |
284 | fputs_filtered (start, file); | |
285 | else | |
286 | { | |
287 | end++; | |
288 | ui_file_write (file, start, end - start); | |
289 | } | |
290 | } | |
c6da7a6d | 291 | fprintf_filtered (file, "\n"); |
e48f5bee AC |
292 | |
293 | /* Now append the annotation. */ | |
294 | switch (e.reason) | |
295 | { | |
296 | case RETURN_QUIT: | |
297 | annotate_quit (); | |
298 | break; | |
299 | case RETURN_ERROR: | |
300 | /* Assume that these are all errors. */ | |
301 | annotate_error (); | |
302 | break; | |
303 | default: | |
304 | internal_error (__FILE__, __LINE__, _("Bad switch.")); | |
305 | } | |
9cbc821d AC |
306 | } |
307 | ||
8a076db9 | 308 | void |
9cbc821d | 309 | exception_print (struct ui_file *file, struct exception e) |
8a076db9 AC |
310 | { |
311 | if (e.reason < 0 && e.message != NULL) | |
312 | { | |
c6da7a6d | 313 | print_flush (); |
9cbc821d | 314 | print_exception (file, e); |
9cbc821d AC |
315 | } |
316 | } | |
8a076db9 | 317 | |
9cbc821d AC |
318 | void |
319 | exception_fprintf (struct ui_file *file, struct exception e, | |
320 | const char *prefix, ...) | |
321 | { | |
322 | if (e.reason < 0 && e.message != NULL) | |
323 | { | |
324 | va_list args; | |
c6da7a6d AC |
325 | |
326 | print_flush (); | |
9cbc821d AC |
327 | |
328 | /* Print the prefix. */ | |
329 | va_start (args, prefix); | |
330 | vfprintf_filtered (file, prefix, args); | |
331 | va_end (args); | |
332 | ||
333 | print_exception (file, e); | |
8a076db9 AC |
334 | } |
335 | } | |
336 | ||
e48f5bee AC |
337 | void |
338 | print_any_exception (struct ui_file *file, const char *prefix, | |
339 | struct exception e) | |
340 | { | |
341 | if (e.reason < 0 && e.message != NULL) | |
342 | { | |
343 | target_terminal_ours (); | |
344 | wrap_here (""); /* Force out any buffered output */ | |
345 | gdb_flush (gdb_stdout); | |
346 | annotate_error_begin (); | |
347 | ||
348 | /* Print the prefix. */ | |
349 | if (prefix != NULL && prefix[0] != '\0') | |
350 | fputs_filtered (prefix, file); | |
351 | print_exception (file, e); | |
352 | } | |
353 | } | |
354 | ||
6b1b7650 | 355 | NORETURN static void |
3af1e0e3 AC |
356 | throw_it (enum return_reason reason, enum errors error, const char *fmt, |
357 | va_list ap) ATTR_NORETURN; | |
6b1b7650 | 358 | NORETURN static void |
3af1e0e3 AC |
359 | throw_it (enum return_reason reason, enum errors error, const char *fmt, |
360 | va_list ap) | |
6b1b7650 | 361 | { |
6b1b7650 | 362 | struct exception e; |
17d92a02 | 363 | char *new_message; |
6b1b7650 | 364 | |
17d92a02 AC |
365 | /* Save the message. Create the new message before deleting the |
366 | old, the new message may include the old message text. */ | |
367 | new_message = xstrvprintf (fmt, ap); | |
6b1b7650 | 368 | xfree (last_message); |
17d92a02 | 369 | last_message = new_message; |
c6da7a6d AC |
370 | |
371 | /* Create the exception. */ | |
372 | e.reason = reason; | |
373 | e.error = error; | |
374 | e.message = last_message; | |
6b1b7650 | 375 | |
6b1b7650 | 376 | /* Throw the exception. */ |
6b1b7650 AC |
377 | throw_exception (e); |
378 | } | |
379 | ||
380 | NORETURN void | |
381 | throw_verror (enum errors error, const char *fmt, va_list ap) | |
382 | { | |
3af1e0e3 | 383 | throw_it (RETURN_ERROR, error, fmt, ap); |
6b1b7650 AC |
384 | } |
385 | ||
386 | NORETURN void | |
387 | throw_vfatal (const char *fmt, va_list ap) | |
388 | { | |
3af1e0e3 | 389 | throw_it (RETURN_QUIT, NO_ERROR, fmt, ap); |
6b1b7650 AC |
390 | } |
391 | ||
392 | NORETURN void | |
05ff989b | 393 | throw_error (enum errors error, const char *fmt, ...) |
6b1b7650 | 394 | { |
05ff989b AC |
395 | va_list args; |
396 | va_start (args, fmt); | |
3af1e0e3 | 397 | throw_it (RETURN_ERROR, error, fmt, args); |
05ff989b | 398 | va_end (args); |
6b1b7650 AC |
399 | } |
400 | ||
60250e8b AC |
401 | /* Call FUNC() with args FUNC_UIOUT and FUNC_ARGS, catching any |
402 | errors. Set FUNC_CAUGHT to an ``enum return_reason'' if the | |
403 | function is aborted (using throw_exception() or zero if the | |
404 | function returns normally. Set FUNC_VAL to the value returned by | |
405 | the function or 0 if the function was aborted. | |
406 | ||
407 | Must not be called with immediate_quit in effect (bad things might | |
408 | happen, say we got a signal in the middle of a memcpy to quit_return). | |
409 | This is an OK restriction; with very few exceptions immediate_quit can | |
410 | be replaced by judicious use of QUIT. | |
411 | ||
412 | MASK specifies what to catch; it is normally set to | |
413 | RETURN_MASK_ALL, if for no other reason than that the code which | |
414 | calls catch_errors might not be set up to deal with a quit which | |
415 | isn't caught. But if the code can deal with it, it generally | |
416 | should be RETURN_MASK_ERROR, unless for some reason it is more | |
417 | useful to abort only the portion of the operation inside the | |
418 | catch_errors. Note that quit should return to the command line | |
419 | fairly quickly, even if some further processing is being done. */ | |
420 | ||
421 | /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with | |
422 | error() et.al. could maintain a set of flags that indicate the the | |
423 | current state of each of the longjmp buffers. This would give the | |
424 | longjmp code the chance to detect a longjmp botch (before it gets | |
425 | to longjmperror()). Prior to 1999-11-05 this wasn't possible as | |
426 | code also randomly used a SET_TOP_LEVEL macro that directly | |
427 | initialize the longjmp buffers. */ | |
428 | ||
429 | /* MAYBE: cagney/1999-11-05: Should the catch_errors and cleanups code | |
430 | be consolidated into a single file instead of being distributed | |
431 | between utils.c and top.c? */ | |
432 | ||
60250e8b AC |
433 | int |
434 | catch_exceptions (struct ui_out *uiout, | |
435 | catch_exceptions_ftype *func, | |
436 | void *func_args, | |
60250e8b AC |
437 | return_mask mask) |
438 | { | |
1c3c7ee7 | 439 | return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask); |
60250e8b AC |
440 | } |
441 | ||
2a78bfb5 AC |
442 | struct exception |
443 | catch_exception (struct ui_out *uiout, | |
444 | catch_exception_ftype *func, | |
445 | void *func_args, | |
446 | return_mask mask) | |
447 | { | |
448 | volatile struct exception exception; | |
449 | SIGJMP_BUF *catch; | |
6212a5e9 | 450 | catch = catcher_init (uiout, &exception, mask); |
2a78bfb5 AC |
451 | for (SIGSETJMP ((*catch)); |
452 | catcher_state_machine (CATCH_ITER);) | |
453 | (*func) (uiout, func_args); | |
454 | return exception; | |
455 | } | |
456 | ||
60250e8b AC |
457 | int |
458 | catch_exceptions_with_msg (struct ui_out *uiout, | |
459 | catch_exceptions_ftype *func, | |
460 | void *func_args, | |
60250e8b AC |
461 | char **gdberrmsg, |
462 | return_mask mask) | |
463 | { | |
2a78bfb5 AC |
464 | volatile struct exception exception; |
465 | volatile int val = 0; | |
6212a5e9 | 466 | SIGJMP_BUF *catch = catcher_init (uiout, &exception, mask); |
2a78bfb5 | 467 | for (SIGSETJMP ((*catch)); catcher_state_machine (CATCH_ITER);) |
db5f402d | 468 | val = (*func) (uiout, func_args); |
e48f5bee | 469 | print_any_exception (gdb_stderr, NULL, exception); |
60250e8b | 470 | gdb_assert (val >= 0); |
2a78bfb5 AC |
471 | gdb_assert (exception.reason <= 0); |
472 | if (exception.reason < 0) | |
473 | { | |
474 | /* If caller wants a copy of the low-level error message, make | |
475 | one. This is used in the case of a silent error whereby the | |
476 | caller may optionally want to issue the message. */ | |
477 | if (gdberrmsg != NULL) | |
6b1b7650 AC |
478 | { |
479 | if (exception.message != NULL) | |
480 | *gdberrmsg = xstrdup (exception.message); | |
481 | else | |
482 | *gdberrmsg = NULL; | |
483 | } | |
2a78bfb5 AC |
484 | return exception.reason; |
485 | } | |
60250e8b AC |
486 | return val; |
487 | } | |
488 | ||
60250e8b AC |
489 | int |
490 | catch_errors (catch_errors_ftype *func, void *func_args, char *errstring, | |
491 | return_mask mask) | |
492 | { | |
2a78bfb5 AC |
493 | volatile int val = 0; |
494 | volatile struct exception exception; | |
6212a5e9 | 495 | SIGJMP_BUF *catch = catcher_init (uiout, &exception, mask); |
db5f402d AC |
496 | /* This illustrates how it is possible to nest the mechanism and |
497 | hence catch "break". Of course this doesn't address the need to | |
498 | also catch "return". */ | |
2a78bfb5 AC |
499 | for (SIGSETJMP ((*catch)); catcher_state_machine (CATCH_ITER);) |
500 | val = func (func_args); | |
e48f5bee | 501 | print_any_exception (gdb_stderr, errstring, exception); |
2a78bfb5 | 502 | if (exception.reason != 0) |
60250e8b AC |
503 | return 0; |
504 | return val; | |
505 | } | |
506 | ||
60250e8b AC |
507 | int |
508 | catch_command_errors (catch_command_errors_ftype * command, | |
509 | char *arg, int from_tty, return_mask mask) | |
510 | { | |
5a14cc1a | 511 | volatile struct exception e; |
6212a5e9 | 512 | SIGJMP_BUF *catch = catcher_init (uiout, &e, mask); |
5a14cc1a AC |
513 | for (SIGSETJMP ((*catch)); catcher_state_machine (CATCH_ITER);) |
514 | command (arg, from_tty); | |
515 | print_any_exception (gdb_stderr, NULL, e); | |
516 | if (e.reason < 0) | |
517 | return 0; | |
518 | return 1; | |
60250e8b | 519 | } |