GDB (API)
/home/stan/gdb/src/gdb/exceptions.c
Go to the documentation of this file.
00001 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "exceptions.h"
00022 #include "breakpoint.h"
00023 #include "target.h"
00024 #include "inferior.h"
00025 #include "annotate.h"
00026 #include "ui-out.h"
00027 #include "gdb_assert.h"
00028 #include "gdb_string.h"
00029 #include "serial.h"
00030 #include "gdbthread.h"
00031 
00032 const struct gdb_exception exception_none = { 0, GDB_NO_ERROR, NULL };
00033 
00034 /* Possible catcher states.  */
00035 enum catcher_state {
00036   /* Initial state, a new catcher has just been created.  */
00037   CATCHER_CREATED,
00038   /* The catch code is running.  */
00039   CATCHER_RUNNING,
00040   CATCHER_RUNNING_1,
00041   /* The catch code threw an exception.  */
00042   CATCHER_ABORTING
00043 };
00044 
00045 /* Possible catcher actions.  */
00046 enum catcher_action {
00047   CATCH_ITER,
00048   CATCH_ITER_1,
00049   CATCH_THROWING
00050 };
00051 
00052 struct catcher
00053 {
00054   enum catcher_state state;
00055   /* Jump buffer pointing back at the exception handler.  */
00056   EXCEPTIONS_SIGJMP_BUF buf;
00057   /* Status buffer belonging to the exception handler.  */
00058   volatile struct gdb_exception *exception;
00059   /* Saved/current state.  */
00060   int mask;
00061   struct cleanup *saved_cleanup_chain;
00062   /* Back link.  */
00063   struct catcher *prev;
00064 };
00065 
00066 /* Where to go for throw_exception().  */
00067 static struct catcher *current_catcher;
00068 
00069 /* Return length of current_catcher list.  */
00070 
00071 static int
00072 catcher_list_size (void)
00073 {
00074   int size;
00075   struct catcher *catcher;
00076 
00077   for (size = 0, catcher = current_catcher;
00078        catcher != NULL;
00079        catcher = catcher->prev)
00080     ++size;
00081 
00082   return size;
00083 }
00084 
00085 EXCEPTIONS_SIGJMP_BUF *
00086 exceptions_state_mc_init (volatile struct gdb_exception *exception,
00087                           return_mask mask)
00088 {
00089   struct catcher *new_catcher = XZALLOC (struct catcher);
00090 
00091   /* Start with no exception, save it's address.  */
00092   exception->reason = 0;
00093   exception->error = GDB_NO_ERROR;
00094   exception->message = NULL;
00095   new_catcher->exception = exception;
00096 
00097   new_catcher->mask = mask;
00098 
00099   /* Prevent error/quit during FUNC from calling cleanups established
00100      prior to here.  */
00101   new_catcher->saved_cleanup_chain = save_cleanups ();
00102 
00103   /* Push this new catcher on the top.  */
00104   new_catcher->prev = current_catcher;
00105   current_catcher = new_catcher;
00106   new_catcher->state = CATCHER_CREATED;
00107 
00108   return &new_catcher->buf;
00109 }
00110 
00111 static void
00112 catcher_pop (void)
00113 {
00114   struct catcher *old_catcher = current_catcher;
00115 
00116   current_catcher = old_catcher->prev;
00117 
00118   /* Restore the cleanup chain, the error/quit messages, and the uiout
00119      builder, to their original states.  */
00120 
00121   restore_cleanups (old_catcher->saved_cleanup_chain);
00122 
00123   xfree (old_catcher);
00124 }
00125 
00126 /* Catcher state machine.  Returns non-zero if the m/c should be run
00127    again, zero if it should abort.  */
00128 
00129 static int
00130 exceptions_state_mc (enum catcher_action action)
00131 {
00132   switch (current_catcher->state)
00133     {
00134     case CATCHER_CREATED:
00135       switch (action)
00136         {
00137         case CATCH_ITER:
00138           /* Allow the code to run the catcher.  */
00139           current_catcher->state = CATCHER_RUNNING;
00140           return 1;
00141         default:
00142           internal_error (__FILE__, __LINE__, _("bad state"));
00143         }
00144     case CATCHER_RUNNING:
00145       switch (action)
00146         {
00147         case CATCH_ITER:
00148           /* No error/quit has occured.  Just clean up.  */
00149           catcher_pop ();
00150           return 0;
00151         case CATCH_ITER_1:
00152           current_catcher->state = CATCHER_RUNNING_1;
00153           return 1;
00154         case CATCH_THROWING:
00155           current_catcher->state = CATCHER_ABORTING;
00156           /* See also throw_exception.  */
00157           return 1;
00158         default:
00159           internal_error (__FILE__, __LINE__, _("bad switch"));
00160         }
00161     case CATCHER_RUNNING_1:
00162       switch (action)
00163         {
00164         case CATCH_ITER:
00165           /* The did a "break" from the inner while loop.  */
00166           catcher_pop ();
00167           return 0;
00168         case CATCH_ITER_1:
00169           current_catcher->state = CATCHER_RUNNING;
00170           return 0;
00171         case CATCH_THROWING:
00172           current_catcher->state = CATCHER_ABORTING;
00173           /* See also throw_exception.  */
00174           return 1;
00175         default:
00176           internal_error (__FILE__, __LINE__, _("bad switch"));
00177         }
00178     case CATCHER_ABORTING:
00179       switch (action)
00180         {
00181         case CATCH_ITER:
00182           {
00183             struct gdb_exception exception = *current_catcher->exception;
00184 
00185             if (current_catcher->mask & RETURN_MASK (exception.reason))
00186               {
00187                 /* Exit normally if this catcher can handle this
00188                    exception.  The caller analyses the func return
00189                    values.  */
00190                 catcher_pop ();
00191                 return 0;
00192               }
00193             /* The caller didn't request that the event be caught,
00194                relay the event to the next containing
00195                catch_errors().  */
00196             catcher_pop ();
00197             throw_exception (exception);
00198           }
00199         default:
00200           internal_error (__FILE__, __LINE__, _("bad state"));
00201         }
00202     default:
00203       internal_error (__FILE__, __LINE__, _("bad switch"));
00204     }
00205 }
00206 
00207 int
00208 exceptions_state_mc_action_iter (void)
00209 {
00210   return exceptions_state_mc (CATCH_ITER);
00211 }
00212 
00213 int
00214 exceptions_state_mc_action_iter_1 (void)
00215 {
00216   return exceptions_state_mc (CATCH_ITER_1);
00217 }
00218 
00219 /* Return EXCEPTION to the nearest containing catch_errors().  */
00220 
00221 void
00222 throw_exception (struct gdb_exception exception)
00223 {
00224   clear_quit_flag ();
00225   immediate_quit = 0;
00226 
00227   do_cleanups (all_cleanups ());
00228 
00229   /* Jump to the containing catch_errors() call, communicating REASON
00230      to that call via setjmp's return value.  Note that REASON can't
00231      be zero, by definition in defs.h.  */
00232   exceptions_state_mc (CATCH_THROWING);
00233   *current_catcher->exception = exception;
00234   EXCEPTIONS_SIGLONGJMP (current_catcher->buf, exception.reason);
00235 }
00236 
00237 static void
00238 print_flush (void)
00239 {
00240   struct serial *gdb_stdout_serial;
00241 
00242   if (deprecated_error_begin_hook)
00243     deprecated_error_begin_hook ();
00244   target_terminal_ours ();
00245 
00246   /* We want all output to appear now, before we print the error.  We
00247      have 3 levels of buffering we have to flush (it's possible that
00248      some of these should be changed to flush the lower-level ones
00249      too):  */
00250 
00251   /* 1.  The _filtered buffer.  */
00252   wrap_here ("");
00253 
00254   /* 2.  The stdio buffer.  */
00255   gdb_flush (gdb_stdout);
00256   gdb_flush (gdb_stderr);
00257 
00258   /* 3.  The system-level buffer.  */
00259   gdb_stdout_serial = serial_fdopen (1);
00260   if (gdb_stdout_serial)
00261     {
00262       serial_drain_output (gdb_stdout_serial);
00263       serial_un_fdopen (gdb_stdout_serial);
00264     }
00265 
00266   annotate_error_begin ();
00267 }
00268 
00269 static void
00270 print_exception (struct ui_file *file, struct gdb_exception e)
00271 {
00272   /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
00273      as that way the MI's behavior is preserved.  */
00274   const char *start;
00275   const char *end;
00276 
00277   for (start = e.message; start != NULL; start = end)
00278     {
00279       end = strchr (start, '\n');
00280       if (end == NULL)
00281         fputs_filtered (start, file);
00282       else
00283         {
00284           end++;
00285           ui_file_write (file, start, end - start);
00286         }
00287     }                                       
00288   fprintf_filtered (file, "\n");
00289 
00290   /* Now append the annotation.  */
00291   switch (e.reason)
00292     {
00293     case RETURN_QUIT:
00294       annotate_quit ();
00295       break;
00296     case RETURN_ERROR:
00297       /* Assume that these are all errors.  */
00298       annotate_error ();
00299       break;
00300     default:
00301       internal_error (__FILE__, __LINE__, _("Bad switch."));
00302     }
00303 }
00304 
00305 void
00306 exception_print (struct ui_file *file, struct gdb_exception e)
00307 {
00308   if (e.reason < 0 && e.message != NULL)
00309     {
00310       print_flush ();
00311       print_exception (file, e);
00312     }
00313 }
00314 
00315 void
00316 exception_fprintf (struct ui_file *file, struct gdb_exception e,
00317                    const char *prefix, ...)
00318 {
00319   if (e.reason < 0 && e.message != NULL)
00320     {
00321       va_list args;
00322 
00323       print_flush ();
00324 
00325       /* Print the prefix.  */
00326       va_start (args, prefix);
00327       vfprintf_filtered (file, prefix, args);
00328       va_end (args);
00329 
00330       print_exception (file, e);
00331     }
00332 }
00333 
00334 static void
00335 print_any_exception (struct ui_file *file, const char *prefix,
00336                      struct gdb_exception e)
00337 {
00338   if (e.reason < 0 && e.message != NULL)
00339     {
00340       target_terminal_ours ();
00341       wrap_here ("");           /* Force out any buffered output.  */
00342       gdb_flush (gdb_stdout);
00343       annotate_error_begin ();
00344 
00345       /* Print the prefix.  */
00346       if (prefix != NULL && prefix[0] != '\0')
00347         fputs_filtered (prefix, file);
00348       print_exception (file, e);
00349     }
00350 }
00351 
00352 /* A stack of exception messages.
00353    This is needed to handle nested calls to throw_it: we don't want to
00354    xfree space for a message before it's used.
00355    This can happen if we throw an exception during a cleanup:
00356    An outer TRY_CATCH may have an exception message it wants to print,
00357    but while doing cleanups further calls to throw_it are made.
00358 
00359    This is indexed by the size of the current_catcher list.
00360    It is a dynamically allocated array so that we don't care how deeply
00361    GDB nests its TRY_CATCHs.  */
00362 static char **exception_messages;
00363 
00364 /* The number of currently allocated entries in exception_messages.  */
00365 static int exception_messages_size;
00366 
00367 static void ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (3, 0)
00368 throw_it (enum return_reason reason, enum errors error, const char *fmt,
00369           va_list ap)
00370 {
00371   struct gdb_exception e;
00372   char *new_message;
00373   int depth = catcher_list_size ();
00374 
00375   gdb_assert (depth > 0);
00376 
00377   /* Note: The new message may use an old message's text.  */
00378   new_message = xstrvprintf (fmt, ap);
00379 
00380   if (depth > exception_messages_size)
00381     {
00382       int old_size = exception_messages_size;
00383 
00384       exception_messages_size = depth + 10;
00385       exception_messages = (char **) xrealloc (exception_messages,
00386                                                exception_messages_size
00387                                                * sizeof (char *));
00388       memset (exception_messages + old_size, 0,
00389               (exception_messages_size - old_size) * sizeof (char *));
00390     }
00391 
00392   xfree (exception_messages[depth - 1]);
00393   exception_messages[depth - 1] = new_message;
00394 
00395   /* Create the exception.  */
00396   e.reason = reason;
00397   e.error = error;
00398   e.message = new_message;
00399 
00400   /* Throw the exception.  */
00401   throw_exception (e);
00402 }
00403 
00404 void
00405 throw_verror (enum errors error, const char *fmt, va_list ap)
00406 {
00407   throw_it (RETURN_ERROR, error, fmt, ap);
00408 }
00409 
00410 void
00411 throw_vfatal (const char *fmt, va_list ap)
00412 {
00413   throw_it (RETURN_QUIT, GDB_NO_ERROR, fmt, ap);
00414 }
00415 
00416 void
00417 throw_error (enum errors error, const char *fmt, ...)
00418 {
00419   va_list args;
00420 
00421   va_start (args, fmt);
00422   throw_it (RETURN_ERROR, error, fmt, args);
00423   va_end (args);
00424 }
00425 
00426 /* Call FUNC(UIOUT, FUNC_ARGS) but wrapped within an exception
00427    handler.  If an exception (enum return_reason) is thrown using
00428    throw_exception() than all cleanups installed since
00429    catch_exceptions() was entered are invoked, the (-ve) exception
00430    value is then returned by catch_exceptions.  If FUNC() returns
00431    normally (with a positive or zero return value) then that value is
00432    returned by catch_exceptions().  It is an internal_error() for
00433    FUNC() to return a negative value.
00434 
00435    See exceptions.h for further usage details.
00436 
00437    Must not be called with immediate_quit in effect (bad things might
00438    happen, say we got a signal in the middle of a memcpy to quit_return).
00439    This is an OK restriction; with very few exceptions immediate_quit can
00440    be replaced by judicious use of QUIT.  */
00441 
00442 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
00443    error() et al. could maintain a set of flags that indicate the
00444    current state of each of the longjmp buffers.  This would give the
00445    longjmp code the chance to detect a longjmp botch (before it gets
00446    to longjmperror()).  Prior to 1999-11-05 this wasn't possible as
00447    code also randomly used a SET_TOP_LEVEL macro that directly
00448    initialized the longjmp buffers.  */
00449 
00450 int
00451 catch_exceptions (struct ui_out *uiout,
00452                   catch_exceptions_ftype *func,
00453                   void *func_args,
00454                   return_mask mask)
00455 {
00456   return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask);
00457 }
00458 
00459 int
00460 catch_exceptions_with_msg (struct ui_out *func_uiout,
00461                            catch_exceptions_ftype *func,
00462                            void *func_args,
00463                            char **gdberrmsg,
00464                            return_mask mask)
00465 {
00466   volatile struct gdb_exception exception;
00467   volatile int val = 0;
00468   struct ui_out *saved_uiout;
00469 
00470   /* Save and override the global ``struct ui_out'' builder.  */
00471   saved_uiout = current_uiout;
00472   current_uiout = func_uiout;
00473 
00474   TRY_CATCH (exception, RETURN_MASK_ALL)
00475     {
00476       val = (*func) (current_uiout, func_args);
00477     }
00478 
00479   /* Restore the global builder.  */
00480   current_uiout = saved_uiout;
00481 
00482   if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
00483     {
00484       /* The caller didn't request that the event be caught.
00485          Rethrow.  */
00486       throw_exception (exception);
00487     }
00488 
00489   print_any_exception (gdb_stderr, NULL, exception);
00490   gdb_assert (val >= 0);
00491   gdb_assert (exception.reason <= 0);
00492   if (exception.reason < 0)
00493     {
00494       /* If caller wants a copy of the low-level error message, make
00495          one.  This is used in the case of a silent error whereby the
00496          caller may optionally want to issue the message.  */
00497       if (gdberrmsg != NULL)
00498         {
00499           if (exception.message != NULL)
00500             *gdberrmsg = xstrdup (exception.message);
00501           else
00502             *gdberrmsg = NULL;
00503         }
00504       return exception.reason;
00505     }
00506   return val;
00507 }
00508 
00509 /* This function is superseded by catch_exceptions().  */
00510 
00511 int
00512 catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
00513               return_mask mask)
00514 {
00515   volatile int val = 0;
00516   volatile struct gdb_exception exception;
00517   struct ui_out *saved_uiout;
00518 
00519   /* Save the global ``struct ui_out'' builder.  */
00520   saved_uiout = current_uiout;
00521 
00522   TRY_CATCH (exception, RETURN_MASK_ALL)
00523     {
00524       val = func (func_args);
00525     }
00526 
00527   /* Restore the global builder.  */
00528   current_uiout = saved_uiout;
00529 
00530   if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
00531     {
00532       /* The caller didn't request that the event be caught.
00533          Rethrow.  */
00534       throw_exception (exception);
00535     }
00536 
00537   print_any_exception (gdb_stderr, errstring, exception);
00538   if (exception.reason != 0)
00539     return 0;
00540   return val;
00541 }
00542 
00543 int
00544 catch_command_errors (catch_command_errors_ftype *command,
00545                       char *arg, int from_tty, return_mask mask)
00546 {
00547   volatile struct gdb_exception e;
00548 
00549   TRY_CATCH (e, mask)
00550     {
00551       command (arg, from_tty);
00552     }
00553   print_any_exception (gdb_stderr, NULL, e);
00554   if (e.reason < 0)
00555     return 0;
00556   return 1;
00557 }
00558 
00559 int
00560 catch_command_errors_const (catch_command_errors_const_ftype *command,
00561                             const char *arg, int from_tty, return_mask mask)
00562 {
00563   volatile struct gdb_exception e;
00564 
00565   TRY_CATCH (e, mask)
00566     {
00567       command (arg, from_tty);
00568     }
00569   print_any_exception (gdb_stderr, NULL, e);
00570   if (e.reason < 0)
00571     return 0;
00572   return 1;
00573 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines