common-exceptions.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
  2. Copyright (C) 1986-2022 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_COMMON_EXCEPTIONS_H
  15. #define COMMON_COMMON_EXCEPTIONS_H
  16. #include <setjmp.h>
  17. #include <new>
  18. #include <memory>
  19. #include <string>
  20. /* Reasons for calling throw_exceptions(). NOTE: all reason values
  21. must be different from zero. enum value 0 is reserved for internal
  22. use as the return value from an initial setjmp(). */
  23. enum return_reason
  24. {
  25. /* User interrupt. */
  26. RETURN_QUIT = -2,
  27. /* Any other error. */
  28. RETURN_ERROR
  29. };
  30. #define RETURN_MASK(reason) (1 << (int)(-reason))
  31. typedef enum
  32. {
  33. RETURN_MASK_QUIT = RETURN_MASK (RETURN_QUIT),
  34. RETURN_MASK_ERROR = RETURN_MASK (RETURN_ERROR),
  35. RETURN_MASK_ALL = (RETURN_MASK_QUIT | RETURN_MASK_ERROR)
  36. } return_mask;
  37. /* Describe all exceptions. */
  38. enum errors {
  39. GDB_NO_ERROR,
  40. /* Any generic error, the corresponding text is in
  41. exception.message. */
  42. GENERIC_ERROR,
  43. /* Something requested was not found. */
  44. NOT_FOUND_ERROR,
  45. /* Thread library lacks support necessary for finding thread local
  46. storage. */
  47. TLS_NO_LIBRARY_SUPPORT_ERROR,
  48. /* Load module not found while attempting to find thread local storage. */
  49. TLS_LOAD_MODULE_NOT_FOUND_ERROR,
  50. /* Thread local storage has not been allocated yet. */
  51. TLS_NOT_ALLOCATED_YET_ERROR,
  52. /* Something else went wrong while attempting to find thread local
  53. storage. The ``struct gdb_exception'' message field provides
  54. more detail. */
  55. TLS_GENERIC_ERROR,
  56. /* Problem parsing an XML document. */
  57. XML_PARSE_ERROR,
  58. /* Error accessing memory. */
  59. MEMORY_ERROR,
  60. /* Value not available. E.g., a register was not collected in a
  61. traceframe. */
  62. NOT_AVAILABLE_ERROR,
  63. /* Value was optimized out. Note: if the value was a register, this
  64. means the register was not saved in the frame. */
  65. OPTIMIZED_OUT_ERROR,
  66. /* DW_OP_entry_value resolving failed. */
  67. NO_ENTRY_VALUE_ERROR,
  68. /* Target throwing an error has been closed. Current command should be
  69. aborted as the inferior state is no longer valid. */
  70. TARGET_CLOSE_ERROR,
  71. /* An undefined command was executed. */
  72. UNDEFINED_COMMAND_ERROR,
  73. /* Requested feature, method, mechanism, etc. is not supported. */
  74. NOT_SUPPORTED_ERROR,
  75. /* The number of candidates generated during line completion has
  76. reached the user's specified limit. This isn't an error, this exception
  77. is used to halt searching for more completions, but for consistency
  78. "_ERROR" is appended to the name. */
  79. MAX_COMPLETIONS_REACHED_ERROR,
  80. /* Add more errors here. */
  81. NR_ERRORS
  82. };
  83. struct gdb_exception
  84. {
  85. gdb_exception ()
  86. : reason ((enum return_reason) 0),
  87. error (GDB_NO_ERROR)
  88. {
  89. }
  90. gdb_exception (enum return_reason r, enum errors e)
  91. : reason (r),
  92. error (e)
  93. {
  94. }
  95. gdb_exception (enum return_reason r, enum errors e,
  96. const char *fmt, va_list ap)
  97. ATTRIBUTE_PRINTF (4, 0)
  98. : reason (r),
  99. error (e),
  100. message (std::make_shared<std::string> (string_vprintf (fmt, ap)))
  101. {
  102. }
  103. /* The move constructor exists so that we can mark it "noexcept",
  104. which is a good practice for any sort of exception object. */
  105. explicit gdb_exception (gdb_exception &&other) noexcept = default;
  106. /* The copy constructor exists so that we can mark it "noexcept",
  107. which is a good practice for any sort of exception object. */
  108. gdb_exception (const gdb_exception &other) noexcept
  109. : reason (other.reason),
  110. error (other.error),
  111. message (other.message)
  112. {
  113. }
  114. /* The assignment operator exists so that we can mark it "noexcept",
  115. which is a good practice for any sort of exception object. */
  116. gdb_exception &operator= (const gdb_exception &other) noexcept
  117. {
  118. reason = other.reason;
  119. error = other.error;
  120. message = other.message;
  121. return *this;
  122. }
  123. gdb_exception &operator= (gdb_exception &&other) noexcept = default;
  124. /* Return the contents of the exception message, as a C string. The
  125. string remains owned by the exception object. */
  126. const char *what () const noexcept
  127. {
  128. return message->c_str ();
  129. }
  130. /* Compare two exceptions. */
  131. bool operator== (const gdb_exception &other) const
  132. {
  133. const char *msg1 = message == nullptr ? "" : what ();
  134. const char *msg2 = other.message == nullptr ? "" : other.what ();
  135. return (reason == other.reason
  136. && error == other.error
  137. && strcmp (msg1, msg2) == 0);
  138. }
  139. /* Compare two exceptions. */
  140. bool operator!= (const gdb_exception &other) const
  141. {
  142. return !(*this == other);
  143. }
  144. enum return_reason reason;
  145. enum errors error;
  146. std::shared_ptr<std::string> message;
  147. };
  148. /* Functions to drive the sjlj-based exceptions state machine. Though
  149. declared here by necessity, these functions should be considered
  150. internal to the exceptions subsystem and not used other than via
  151. the TRY/CATCH (or TRY_SJLJ/CATCH_SJLJ) macros defined below. */
  152. extern jmp_buf *exceptions_state_mc_init (void);
  153. extern int exceptions_state_mc_action_iter (void);
  154. extern int exceptions_state_mc_action_iter_1 (void);
  155. extern int exceptions_state_mc_catch (struct gdb_exception *, int);
  156. /* Macro to wrap up standard try/catch behavior.
  157. The double loop lets us correctly handle code "break"ing out of the
  158. try catch block. (It works as the "break" only exits the inner
  159. "while" loop, the outer for loop detects this handling it
  160. correctly.) Of course "return" and "goto" are not so lucky.
  161. For instance:
  162. *INDENT-OFF*
  163. TRY_SJLJ
  164. {
  165. }
  166. CATCH_SJLJ (e, RETURN_MASK_ERROR)
  167. {
  168. switch (e.reason)
  169. {
  170. case RETURN_ERROR: ...
  171. }
  172. }
  173. END_CATCH_SJLJ
  174. The SJLJ variants are needed in some cases where gdb exceptions
  175. need to cross third-party library code compiled without exceptions
  176. support (e.g., readline). */
  177. #define TRY_SJLJ \
  178. { \
  179. jmp_buf *buf = \
  180. exceptions_state_mc_init (); \
  181. setjmp (*buf); \
  182. } \
  183. while (exceptions_state_mc_action_iter ()) \
  184. while (exceptions_state_mc_action_iter_1 ())
  185. #define CATCH_SJLJ(EXCEPTION, MASK) \
  186. { \
  187. struct gdb_exception EXCEPTION; \
  188. if (exceptions_state_mc_catch (&(EXCEPTION), MASK))
  189. #define END_CATCH_SJLJ \
  190. }
  191. /* The exception types client code may catch. They're just shims
  192. around gdb_exception that add nothing but type info. Which is used
  193. is selected depending on the MASK argument passed to CATCH. */
  194. struct gdb_exception_error : public gdb_exception
  195. {
  196. gdb_exception_error (enum errors e, const char *fmt, va_list ap)
  197. ATTRIBUTE_PRINTF (3, 0)
  198. : gdb_exception (RETURN_ERROR, e, fmt, ap)
  199. {
  200. }
  201. explicit gdb_exception_error (gdb_exception &&ex) noexcept
  202. : gdb_exception (std::move (ex))
  203. {
  204. gdb_assert (ex.reason == RETURN_ERROR);
  205. }
  206. };
  207. struct gdb_exception_quit : public gdb_exception
  208. {
  209. gdb_exception_quit (const char *fmt, va_list ap)
  210. ATTRIBUTE_PRINTF (2, 0)
  211. : gdb_exception (RETURN_QUIT, GDB_NO_ERROR, fmt, ap)
  212. {
  213. }
  214. explicit gdb_exception_quit (gdb_exception &&ex) noexcept
  215. : gdb_exception (std::move (ex))
  216. {
  217. gdb_assert (ex.reason == RETURN_QUIT);
  218. }
  219. };
  220. /* An exception type that inherits from both std::bad_alloc and a gdb
  221. exception. This is necessary because operator new can only throw
  222. std::bad_alloc, and OTOH, we want exceptions thrown due to memory
  223. allocation error to be caught by all the CATCH/RETURN_MASK_ALL
  224. spread around the codebase. */
  225. struct gdb_quit_bad_alloc
  226. : public gdb_exception_quit,
  227. public std::bad_alloc
  228. {
  229. explicit gdb_quit_bad_alloc (gdb_exception &&ex) noexcept
  230. : gdb_exception_quit (std::move (ex)),
  231. std::bad_alloc ()
  232. {
  233. }
  234. };
  235. /* *INDENT-ON* */
  236. /* Throw an exception (as described by "struct gdb_exception"),
  237. landing in the inner most containing exception handler established
  238. using TRY/CATCH. */
  239. extern void throw_exception (gdb_exception &&exception)
  240. ATTRIBUTE_NORETURN;
  241. /* Throw an exception by executing a LONG JUMP to the inner most
  242. containing exception handler established using TRY_SJLJ. Necessary
  243. in some cases where we need to throw GDB exceptions across
  244. third-party library code (e.g., readline). */
  245. extern void throw_exception_sjlj (const struct gdb_exception &exception)
  246. ATTRIBUTE_NORETURN;
  247. /* Convenience wrappers around throw_exception that throw GDB
  248. errors. */
  249. extern void throw_verror (enum errors, const char *fmt, va_list ap)
  250. ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 0);
  251. extern void throw_vquit (const char *fmt, va_list ap)
  252. ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 0);
  253. extern void throw_error (enum errors error, const char *fmt, ...)
  254. ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 3);
  255. extern void throw_quit (const char *fmt, ...)
  256. ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2);
  257. #endif /* COMMON_COMMON_EXCEPTIONS_H */