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