gdb/common/common-exceptions.h - gdb

Data types defined

Macros defined

Source code

  1. /* Exception (throw catch) mechanism, for GDB, the GNU debugger.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #ifndef COMMON_EXCEPTIONS_H
  15. #define COMMON_EXCEPTIONS_H

  16. #include "gdb_setjmp.h"

  17. /* Reasons for calling throw_exceptions().  NOTE: all reason values
  18.    must be less than zero.  enum value 0 is reserved for internal use
  19.    as the return value from an initial setjmp().  The function
  20.    catch_exceptions() reserves values >= 0 as legal results from its
  21.    wrapped function.  */

  22. enum return_reason
  23.   {
  24.     /* User interrupt.  */
  25.     RETURN_QUIT = -2,
  26.     /* Any other error.  */
  27.     RETURN_ERROR
  28.   };

  29. #define RETURN_MASK(reason)        (1 << (int)(-reason))

  30. typedef enum
  31. {
  32.   RETURN_MASK_QUIT = RETURN_MASK (RETURN_QUIT),
  33.   RETURN_MASK_ERROR = RETURN_MASK (RETURN_ERROR),
  34.   RETURN_MASK_ALL = (RETURN_MASK_QUIT | RETURN_MASK_ERROR)
  35. } return_mask;

  36. /* Describe all exceptions.  */

  37. enum errors {
  38.   GDB_NO_ERROR,

  39.   /* Any generic error, the corresponding text is in
  40.      exception.message.  */
  41.   GENERIC_ERROR,

  42.   /* Something requested was not found.  */
  43.   NOT_FOUND_ERROR,

  44.   /* Thread library lacks support necessary for finding thread local
  45.      storage.  */
  46.   TLS_NO_LIBRARY_SUPPORT_ERROR,

  47.   /* Load module not found while attempting to find thread local storage.  */
  48.   TLS_LOAD_MODULE_NOT_FOUND_ERROR,

  49.   /* Thread local storage has not been allocated yet.  */
  50.   TLS_NOT_ALLOCATED_YET_ERROR,

  51.   /* Something else went wrong while attempting to find thread local
  52.      storage.  The ``struct gdb_exception'' message field provides
  53.      more detail.  */
  54.   TLS_GENERIC_ERROR,

  55.   /* Problem parsing an XML document.  */
  56.   XML_PARSE_ERROR,

  57.   /* Error accessing memory.  */
  58.   MEMORY_ERROR,

  59.   /* Value not available.  E.g., a register was not collected in a
  60.      traceframe.  */
  61.   NOT_AVAILABLE_ERROR,

  62.   /* Value was optimized out.  Note: if the value was a register, this
  63.      means the register was not saved in the frame.  */
  64.   OPTIMIZED_OUT_ERROR,

  65.   /* DW_OP_GNU_entry_value resolving failed.  */
  66.   NO_ENTRY_VALUE_ERROR,

  67.   /* Target throwing an error has been closed.  Current command should be
  68.      aborted as the inferior state is no longer valid.  */
  69.   TARGET_CLOSE_ERROR,

  70.   /* An undefined command was executed.  */
  71.   UNDEFINED_COMMAND_ERROR,

  72.   /* Requested feature, method, mechanism, etc. is not supported.  */
  73.   NOT_SUPPORTED_ERROR,

  74.   /* Add more errors here.  */
  75.   NR_ERRORS
  76. };

  77. struct gdb_exception
  78. {
  79.   enum return_reason reason;
  80.   enum errors error;
  81.   const char *message;
  82. };

  83. /* Functions to drive the exceptions state machine.  Though declared
  84.    here by necessity, these functions should be considered internal to
  85.    the exceptions subsystem and not used other than via the TRY_CATCH
  86.    macro defined below.  */

  87. extern SIGJMP_BUF *exceptions_state_mc_init (volatile struct
  88.                                              gdb_exception *exception,
  89.                                              return_mask mask);
  90. extern int exceptions_state_mc_action_iter (void);
  91. extern int exceptions_state_mc_action_iter_1 (void);

  92. /* Macro to wrap up standard try/catch behavior.

  93.    The double loop lets us correctly handle code "break"ing out of the
  94.    try catch block.  (It works as the "break" only exits the inner
  95.    "while" loop, the outer for loop detects this handling it
  96.    correctly.)  Of course "return" and "goto" are not so lucky.

  97.    For instance:

  98.    *INDENT-OFF*

  99.    volatile struct gdb_exception e;
  100.    TRY_CATCH (e, RETURN_MASK_ERROR)
  101.      {
  102.      }
  103.    switch (e.reason)
  104.      {
  105.      case RETURN_ERROR: ...
  106.      }

  107.   */

  108. #define TRY_CATCH(EXCEPTION,MASK) \
  109.      { \
  110.        SIGJMP_BUF *buf = \
  111.          exceptions_state_mc_init (&(EXCEPTION), (MASK)); \
  112.        SIGSETJMP (*buf); \
  113.      } \
  114.      while (exceptions_state_mc_action_iter ()) \
  115.        while (exceptions_state_mc_action_iter_1 ())

  116. /* *INDENT-ON* */

  117. /* Hook to allow client-specific actions to be performed prior to
  118.    throwing an exception.  This function must be provided by the
  119.    client, and will be called before any cleanups are run.  */

  120. extern void prepare_to_throw_exception (void);

  121. /* Throw an exception (as described by "struct gdb_exception").  Will
  122.    execute a LONG JUMP to the inner most containing exception handler
  123.    established using catch_exceptions() (or similar).

  124.    Code normally throws an exception using error() et.al.  For various
  125.    reaons, GDB also contains code that throws an exception directly.
  126.    For instance, the remote*.c targets contain CNTRL-C signal handlers
  127.    that propogate the QUIT event up the exception chain.  ``This could
  128.    be a good thing or a dangerous thing.'' -- the Existential
  129.    Wombat.  */

  130. extern void throw_exception (struct gdb_exception exception)
  131.      ATTRIBUTE_NORETURN;
  132. extern void throw_verror (enum errors, const char *fmt, va_list ap)
  133.      ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 0);
  134. extern void throw_vquit (const char *fmt, va_list ap)
  135.      ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 0);
  136. extern void throw_error (enum errors error, const char *fmt, ...)
  137.      ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 3);
  138. extern void throw_quit (const char *fmt, ...)
  139.      ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2);

  140. #endif /* COMMON_EXCEPTIONS_H */