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