Include gdb_assert.h in common-defs.h
[binutils-gdb.git] / gdb / exceptions.c
1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2014 Free Software Foundation, Inc.
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
9 the Free Software Foundation; either version 3 of the License, or
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "exceptions.h"
22 #include "breakpoint.h"
23 #include "target.h"
24 #include "inferior.h"
25 #include "annotate.h"
26 #include "ui-out.h"
27 #include <string.h>
28 #include "serial.h"
29 #include "gdbthread.h"
30
31 const struct gdb_exception exception_none = { 0, GDB_NO_ERROR, NULL };
32
33 /* Possible catcher states. */
34 enum catcher_state {
35 /* Initial state, a new catcher has just been created. */
36 CATCHER_CREATED,
37 /* The catch code is running. */
38 CATCHER_RUNNING,
39 CATCHER_RUNNING_1,
40 /* The catch code threw an exception. */
41 CATCHER_ABORTING
42 };
43
44 /* Possible catcher actions. */
45 enum catcher_action {
46 CATCH_ITER,
47 CATCH_ITER_1,
48 CATCH_THROWING
49 };
50
51 struct catcher
52 {
53 enum catcher_state state;
54 /* Jump buffer pointing back at the exception handler. */
55 EXCEPTIONS_SIGJMP_BUF buf;
56 /* Status buffer belonging to the exception handler. */
57 volatile struct gdb_exception *exception;
58 /* Saved/current state. */
59 int mask;
60 struct cleanup *saved_cleanup_chain;
61 /* Back link. */
62 struct catcher *prev;
63 };
64
65 /* Where to go for throw_exception(). */
66 static struct catcher *current_catcher;
67
68 /* Return length of current_catcher list. */
69
70 static int
71 catcher_list_size (void)
72 {
73 int size;
74 struct catcher *catcher;
75
76 for (size = 0, catcher = current_catcher;
77 catcher != NULL;
78 catcher = catcher->prev)
79 ++size;
80
81 return size;
82 }
83
84 EXCEPTIONS_SIGJMP_BUF *
85 exceptions_state_mc_init (volatile struct gdb_exception *exception,
86 return_mask mask)
87 {
88 struct catcher *new_catcher = XCNEW (struct catcher);
89
90 /* Start with no exception, save it's address. */
91 exception->reason = 0;
92 exception->error = GDB_NO_ERROR;
93 exception->message = NULL;
94 new_catcher->exception = exception;
95
96 new_catcher->mask = mask;
97
98 /* Prevent error/quit during FUNC from calling cleanups established
99 prior to here. */
100 new_catcher->saved_cleanup_chain = save_cleanups ();
101
102 /* Push this new catcher on the top. */
103 new_catcher->prev = current_catcher;
104 current_catcher = new_catcher;
105 new_catcher->state = CATCHER_CREATED;
106
107 return &new_catcher->buf;
108 }
109
110 static void
111 catcher_pop (void)
112 {
113 struct catcher *old_catcher = current_catcher;
114
115 current_catcher = old_catcher->prev;
116
117 /* Restore the cleanup chain, the error/quit messages, and the uiout
118 builder, to their original states. */
119
120 restore_cleanups (old_catcher->saved_cleanup_chain);
121
122 xfree (old_catcher);
123 }
124
125 /* Catcher state machine. Returns non-zero if the m/c should be run
126 again, zero if it should abort. */
127
128 static int
129 exceptions_state_mc (enum catcher_action action)
130 {
131 switch (current_catcher->state)
132 {
133 case CATCHER_CREATED:
134 switch (action)
135 {
136 case CATCH_ITER:
137 /* Allow the code to run the catcher. */
138 current_catcher->state = CATCHER_RUNNING;
139 return 1;
140 default:
141 internal_error (__FILE__, __LINE__, _("bad state"));
142 }
143 case CATCHER_RUNNING:
144 switch (action)
145 {
146 case CATCH_ITER:
147 /* No error/quit has occured. Just clean up. */
148 catcher_pop ();
149 return 0;
150 case CATCH_ITER_1:
151 current_catcher->state = CATCHER_RUNNING_1;
152 return 1;
153 case CATCH_THROWING:
154 current_catcher->state = CATCHER_ABORTING;
155 /* See also throw_exception. */
156 return 1;
157 default:
158 internal_error (__FILE__, __LINE__, _("bad switch"));
159 }
160 case CATCHER_RUNNING_1:
161 switch (action)
162 {
163 case CATCH_ITER:
164 /* The did a "break" from the inner while loop. */
165 catcher_pop ();
166 return 0;
167 case CATCH_ITER_1:
168 current_catcher->state = CATCHER_RUNNING;
169 return 0;
170 case CATCH_THROWING:
171 current_catcher->state = CATCHER_ABORTING;
172 /* See also throw_exception. */
173 return 1;
174 default:
175 internal_error (__FILE__, __LINE__, _("bad switch"));
176 }
177 case CATCHER_ABORTING:
178 switch (action)
179 {
180 case CATCH_ITER:
181 {
182 struct gdb_exception exception = *current_catcher->exception;
183
184 if (current_catcher->mask & RETURN_MASK (exception.reason))
185 {
186 /* Exit normally if this catcher can handle this
187 exception. The caller analyses the func return
188 values. */
189 catcher_pop ();
190 return 0;
191 }
192 /* The caller didn't request that the event be caught,
193 relay the event to the next containing
194 catch_errors(). */
195 catcher_pop ();
196 throw_exception (exception);
197 }
198 default:
199 internal_error (__FILE__, __LINE__, _("bad state"));
200 }
201 default:
202 internal_error (__FILE__, __LINE__, _("bad switch"));
203 }
204 }
205
206 int
207 exceptions_state_mc_action_iter (void)
208 {
209 return exceptions_state_mc (CATCH_ITER);
210 }
211
212 int
213 exceptions_state_mc_action_iter_1 (void)
214 {
215 return exceptions_state_mc (CATCH_ITER_1);
216 }
217
218 /* Return EXCEPTION to the nearest containing catch_errors(). */
219
220 void
221 throw_exception (struct gdb_exception exception)
222 {
223 clear_quit_flag ();
224 immediate_quit = 0;
225
226 do_cleanups (all_cleanups ());
227
228 /* Jump to the containing catch_errors() call, communicating REASON
229 to that call via setjmp's return value. Note that REASON can't
230 be zero, by definition in defs.h. */
231 exceptions_state_mc (CATCH_THROWING);
232 *current_catcher->exception = exception;
233 EXCEPTIONS_SIGLONGJMP (current_catcher->buf, exception.reason);
234 }
235
236 static void
237 print_flush (void)
238 {
239 struct serial *gdb_stdout_serial;
240
241 if (deprecated_error_begin_hook)
242 deprecated_error_begin_hook ();
243 target_terminal_ours ();
244
245 /* We want all output to appear now, before we print the error. We
246 have 3 levels of buffering we have to flush (it's possible that
247 some of these should be changed to flush the lower-level ones
248 too): */
249
250 /* 1. The _filtered buffer. */
251 wrap_here ("");
252
253 /* 2. The stdio buffer. */
254 gdb_flush (gdb_stdout);
255 gdb_flush (gdb_stderr);
256
257 /* 3. The system-level buffer. */
258 gdb_stdout_serial = serial_fdopen (1);
259 if (gdb_stdout_serial)
260 {
261 serial_drain_output (gdb_stdout_serial);
262 serial_un_fdopen (gdb_stdout_serial);
263 }
264
265 annotate_error_begin ();
266 }
267
268 static void
269 print_exception (struct ui_file *file, struct gdb_exception e)
270 {
271 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
272 as that way the MI's behavior is preserved. */
273 const char *start;
274 const char *end;
275
276 for (start = e.message; start != NULL; start = end)
277 {
278 end = strchr (start, '\n');
279 if (end == NULL)
280 fputs_filtered (start, file);
281 else
282 {
283 end++;
284 ui_file_write (file, start, end - start);
285 }
286 }
287 fprintf_filtered (file, "\n");
288
289 /* Now append the annotation. */
290 switch (e.reason)
291 {
292 case RETURN_QUIT:
293 annotate_quit ();
294 break;
295 case RETURN_ERROR:
296 /* Assume that these are all errors. */
297 annotate_error ();
298 break;
299 default:
300 internal_error (__FILE__, __LINE__, _("Bad switch."));
301 }
302 }
303
304 void
305 exception_print (struct ui_file *file, struct gdb_exception e)
306 {
307 if (e.reason < 0 && e.message != NULL)
308 {
309 print_flush ();
310 print_exception (file, e);
311 }
312 }
313
314 void
315 exception_fprintf (struct ui_file *file, struct gdb_exception e,
316 const char *prefix, ...)
317 {
318 if (e.reason < 0 && e.message != NULL)
319 {
320 va_list args;
321
322 print_flush ();
323
324 /* Print the prefix. */
325 va_start (args, prefix);
326 vfprintf_filtered (file, prefix, args);
327 va_end (args);
328
329 print_exception (file, e);
330 }
331 }
332
333 /* A stack of exception messages.
334 This is needed to handle nested calls to throw_it: we don't want to
335 xfree space for a message before it's used.
336 This can happen if we throw an exception during a cleanup:
337 An outer TRY_CATCH may have an exception message it wants to print,
338 but while doing cleanups further calls to throw_it are made.
339
340 This is indexed by the size of the current_catcher list.
341 It is a dynamically allocated array so that we don't care how deeply
342 GDB nests its TRY_CATCHs. */
343 static char **exception_messages;
344
345 /* The number of currently allocated entries in exception_messages. */
346 static int exception_messages_size;
347
348 static void ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (3, 0)
349 throw_it (enum return_reason reason, enum errors error, const char *fmt,
350 va_list ap)
351 {
352 struct gdb_exception e;
353 char *new_message;
354 int depth = catcher_list_size ();
355
356 gdb_assert (depth > 0);
357
358 /* Note: The new message may use an old message's text. */
359 new_message = xstrvprintf (fmt, ap);
360
361 if (depth > exception_messages_size)
362 {
363 int old_size = exception_messages_size;
364
365 exception_messages_size = depth + 10;
366 exception_messages = (char **) xrealloc (exception_messages,
367 exception_messages_size
368 * sizeof (char *));
369 memset (exception_messages + old_size, 0,
370 (exception_messages_size - old_size) * sizeof (char *));
371 }
372
373 xfree (exception_messages[depth - 1]);
374 exception_messages[depth - 1] = new_message;
375
376 /* Create the exception. */
377 e.reason = reason;
378 e.error = error;
379 e.message = new_message;
380
381 /* Throw the exception. */
382 throw_exception (e);
383 }
384
385 void
386 throw_verror (enum errors error, const char *fmt, va_list ap)
387 {
388 throw_it (RETURN_ERROR, error, fmt, ap);
389 }
390
391 void
392 throw_vquit (const char *fmt, va_list ap)
393 {
394 throw_it (RETURN_QUIT, GDB_NO_ERROR, fmt, ap);
395 }
396
397 void
398 throw_error (enum errors error, const char *fmt, ...)
399 {
400 va_list args;
401
402 va_start (args, fmt);
403 throw_verror (error, fmt, args);
404 va_end (args);
405 }
406
407 void
408 throw_quit (const char *fmt, ...)
409 {
410 va_list args;
411
412 va_start (args, fmt);
413 throw_vquit (fmt, args);
414 va_end (args);
415 }
416
417 /* Call FUNC(UIOUT, FUNC_ARGS) but wrapped within an exception
418 handler. If an exception (enum return_reason) is thrown using
419 throw_exception() than all cleanups installed since
420 catch_exceptions() was entered are invoked, the (-ve) exception
421 value is then returned by catch_exceptions. If FUNC() returns
422 normally (with a positive or zero return value) then that value is
423 returned by catch_exceptions(). It is an internal_error() for
424 FUNC() to return a negative value.
425
426 See exceptions.h for further usage details.
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 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
434 error() et al. could maintain a set of flags that indicate the
435 current state of each of the longjmp buffers. This would give the
436 longjmp code the chance to detect a longjmp botch (before it gets
437 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
438 code also randomly used a SET_TOP_LEVEL macro that directly
439 initialized the longjmp buffers. */
440
441 int
442 catch_exceptions (struct ui_out *uiout,
443 catch_exceptions_ftype *func,
444 void *func_args,
445 return_mask mask)
446 {
447 return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask);
448 }
449
450 int
451 catch_exceptions_with_msg (struct ui_out *func_uiout,
452 catch_exceptions_ftype *func,
453 void *func_args,
454 char **gdberrmsg,
455 return_mask mask)
456 {
457 volatile struct gdb_exception exception;
458 volatile int val = 0;
459 struct ui_out *saved_uiout;
460
461 /* Save and override the global ``struct ui_out'' builder. */
462 saved_uiout = current_uiout;
463 current_uiout = func_uiout;
464
465 TRY_CATCH (exception, RETURN_MASK_ALL)
466 {
467 val = (*func) (current_uiout, func_args);
468 }
469
470 /* Restore the global builder. */
471 current_uiout = saved_uiout;
472
473 if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
474 {
475 /* The caller didn't request that the event be caught.
476 Rethrow. */
477 throw_exception (exception);
478 }
479
480 exception_print (gdb_stderr, exception);
481 gdb_assert (val >= 0);
482 gdb_assert (exception.reason <= 0);
483 if (exception.reason < 0)
484 {
485 /* If caller wants a copy of the low-level error message, make
486 one. This is used in the case of a silent error whereby the
487 caller may optionally want to issue the message. */
488 if (gdberrmsg != NULL)
489 {
490 if (exception.message != NULL)
491 *gdberrmsg = xstrdup (exception.message);
492 else
493 *gdberrmsg = NULL;
494 }
495 return exception.reason;
496 }
497 return val;
498 }
499
500 /* This function is superseded by catch_exceptions(). */
501
502 int
503 catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
504 return_mask mask)
505 {
506 volatile int val = 0;
507 volatile struct gdb_exception exception;
508 struct ui_out *saved_uiout;
509
510 /* Save the global ``struct ui_out'' builder. */
511 saved_uiout = current_uiout;
512
513 TRY_CATCH (exception, RETURN_MASK_ALL)
514 {
515 val = func (func_args);
516 }
517
518 /* Restore the global builder. */
519 current_uiout = saved_uiout;
520
521 if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
522 {
523 /* The caller didn't request that the event be caught.
524 Rethrow. */
525 throw_exception (exception);
526 }
527
528 exception_fprintf (gdb_stderr, exception, "%s", errstring);
529 if (exception.reason != 0)
530 return 0;
531 return val;
532 }