break-catch-throw.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /* Everything about catch/throw catchpoints, for GDB.
  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. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include <ctype.h>
  17. #include "breakpoint.h"
  18. #include "gdbcmd.h"
  19. #include "inferior.h"
  20. #include "annotate.h"
  21. #include "valprint.h"
  22. #include "cli/cli-utils.h"
  23. #include "completer.h"
  24. #include "gdbsupport/gdb_obstack.h"
  25. #include "mi/mi-common.h"
  26. #include "linespec.h"
  27. #include "probe.h"
  28. #include "objfiles.h"
  29. #include "cp-abi.h"
  30. #include "gdbsupport/gdb_regex.h"
  31. #include "cp-support.h"
  32. #include "location.h"
  33. #include "cli/cli-decode.h"
  34. /* Each spot where we may place an exception-related catchpoint has
  35. two names: the SDT probe point and the function name. This
  36. structure holds both. */
  37. struct exception_names
  38. {
  39. /* The name of the probe point to try, in the form accepted by
  40. 'parse_probes'. */
  41. const char *probe;
  42. /* The name of the corresponding function. */
  43. const char *function;
  44. };
  45. /* Names of the probe points and functions on which to break. This is
  46. indexed by exception_event_kind. */
  47. static const struct exception_names exception_functions[] =
  48. {
  49. { "-probe-stap libstdcxx:throw", "__cxa_throw" },
  50. { "-probe-stap libstdcxx:rethrow", "__cxa_rethrow" },
  51. { "-probe-stap libstdcxx:catch", "__cxa_begin_catch" }
  52. };
  53. static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
  54. /* The type of an exception catchpoint. */
  55. struct exception_catchpoint : public breakpoint
  56. {
  57. /* The kind of exception catchpoint. */
  58. enum exception_event_kind kind;
  59. /* If not empty, a string holding the source form of the regular
  60. expression to match against. */
  61. std::string exception_rx;
  62. /* If non-NULL, a compiled regular expression which is used to
  63. determine which exceptions to stop on. */
  64. std::unique_ptr<compiled_regex> pattern;
  65. };
  66. /* See breakpoint.h. */
  67. bool
  68. is_exception_catchpoint (breakpoint *bp)
  69. {
  70. return bp->ops == &gnu_v3_exception_catchpoint_ops;
  71. }
  72. /* A helper function that fetches exception probe arguments. This
  73. fills in *ARG0 (if non-NULL) and *ARG1 (which must be non-NULL).
  74. It will throw an exception on any kind of failure. */
  75. static void
  76. fetch_probe_arguments (struct value **arg0, struct value **arg1)
  77. {
  78. struct frame_info *frame = get_selected_frame (_("No frame selected"));
  79. CORE_ADDR pc = get_frame_pc (frame);
  80. struct bound_probe pc_probe;
  81. unsigned n_args;
  82. pc_probe = find_probe_by_pc (pc);
  83. if (pc_probe.prob == NULL)
  84. error (_("did not find exception probe (does libstdcxx have SDT probes?)"));
  85. if (pc_probe.prob->get_provider () != "libstdcxx"
  86. || (pc_probe.prob->get_name () != "catch"
  87. && pc_probe.prob->get_name () != "throw"
  88. && pc_probe.prob->get_name () != "rethrow"))
  89. error (_("not stopped at a C++ exception catchpoint"));
  90. n_args = pc_probe.prob->get_argument_count (get_frame_arch (frame));
  91. if (n_args < 2)
  92. error (_("C++ exception catchpoint has too few arguments"));
  93. if (arg0 != NULL)
  94. *arg0 = pc_probe.prob->evaluate_argument (0, frame);
  95. *arg1 = pc_probe.prob->evaluate_argument (1, frame);
  96. if ((arg0 != NULL && *arg0 == NULL) || *arg1 == NULL)
  97. error (_("error computing probe argument at c++ exception catchpoint"));
  98. }
  99. /* A helper function that returns a value indicating the kind of the
  100. exception catchpoint B. */
  101. static enum exception_event_kind
  102. classify_exception_breakpoint (struct breakpoint *b)
  103. {
  104. struct exception_catchpoint *cp = (struct exception_catchpoint *) b;
  105. return cp->kind;
  106. }
  107. /* Implement the 'check_status' method. */
  108. static void
  109. check_status_exception_catchpoint (struct bpstat *bs)
  110. {
  111. struct exception_catchpoint *self
  112. = (struct exception_catchpoint *) bs->breakpoint_at;
  113. std::string type_name;
  114. bkpt_breakpoint_ops.check_status (bs);
  115. if (bs->stop == 0)
  116. return;
  117. if (self->pattern == NULL)
  118. return;
  119. const char *name = nullptr;
  120. gdb::unique_xmalloc_ptr<char> canon;
  121. try
  122. {
  123. struct value *typeinfo_arg;
  124. fetch_probe_arguments (NULL, &typeinfo_arg);
  125. type_name = cplus_typename_from_type_info (typeinfo_arg);
  126. canon = cp_canonicalize_string (type_name.c_str ());
  127. name = (canon != nullptr
  128. ? canon.get ()
  129. : type_name.c_str ());
  130. }
  131. catch (const gdb_exception_error &e)
  132. {
  133. exception_print (gdb_stderr, e);
  134. }
  135. if (name != nullptr)
  136. {
  137. if (self->pattern->exec (name, 0, NULL, 0) != 0)
  138. bs->stop = 0;
  139. }
  140. }
  141. /* Implement the 're_set' method. */
  142. static void
  143. re_set_exception_catchpoint (struct breakpoint *self)
  144. {
  145. std::vector<symtab_and_line> sals;
  146. enum exception_event_kind kind = classify_exception_breakpoint (self);
  147. struct program_space *filter_pspace = current_program_space;
  148. /* We first try to use the probe interface. */
  149. try
  150. {
  151. event_location_up location
  152. = new_probe_location (exception_functions[kind].probe);
  153. sals = parse_probes (location.get (), filter_pspace, NULL);
  154. }
  155. catch (const gdb_exception_error &e)
  156. {
  157. /* Using the probe interface failed. Let's fallback to the normal
  158. catchpoint mode. */
  159. try
  160. {
  161. struct explicit_location explicit_loc;
  162. initialize_explicit_location (&explicit_loc);
  163. explicit_loc.function_name
  164. = ASTRDUP (exception_functions[kind].function);
  165. event_location_up location = new_explicit_location (&explicit_loc);
  166. sals = self->ops->decode_location (self, location.get (),
  167. filter_pspace);
  168. }
  169. catch (const gdb_exception_error &ex)
  170. {
  171. /* NOT_FOUND_ERROR just means the breakpoint will be
  172. pending, so let it through. */
  173. if (ex.error != NOT_FOUND_ERROR)
  174. throw;
  175. }
  176. }
  177. update_breakpoint_locations (self, filter_pspace, sals, {});
  178. }
  179. static enum print_stop_action
  180. print_it_exception_catchpoint (bpstat *bs)
  181. {
  182. struct ui_out *uiout = current_uiout;
  183. struct breakpoint *b = bs->breakpoint_at;
  184. int bp_temp;
  185. enum exception_event_kind kind = classify_exception_breakpoint (b);
  186. annotate_catchpoint (b->number);
  187. maybe_print_thread_hit_breakpoint (uiout);
  188. bp_temp = b->disposition == disp_del;
  189. uiout->text (bp_temp ? "Temporary catchpoint "
  190. : "Catchpoint ");
  191. uiout->field_signed ("bkptno", b->number);
  192. uiout->text ((kind == EX_EVENT_THROW ? " (exception thrown), "
  193. : (kind == EX_EVENT_CATCH ? " (exception caught), "
  194. : " (exception rethrown), ")));
  195. if (uiout->is_mi_like_p ())
  196. {
  197. uiout->field_string ("reason",
  198. async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
  199. uiout->field_string ("disp", bpdisp_text (b->disposition));
  200. }
  201. return PRINT_SRC_AND_LOC;
  202. }
  203. static void
  204. print_one_exception_catchpoint (struct breakpoint *b,
  205. struct bp_location **last_loc)
  206. {
  207. struct value_print_options opts;
  208. struct ui_out *uiout = current_uiout;
  209. enum exception_event_kind kind = classify_exception_breakpoint (b);
  210. get_user_print_options (&opts);
  211. if (opts.addressprint)
  212. uiout->field_skip ("addr");
  213. annotate_field (5);
  214. switch (kind)
  215. {
  216. case EX_EVENT_THROW:
  217. uiout->field_string ("what", "exception throw");
  218. if (uiout->is_mi_like_p ())
  219. uiout->field_string ("catch-type", "throw");
  220. break;
  221. case EX_EVENT_RETHROW:
  222. uiout->field_string ("what", "exception rethrow");
  223. if (uiout->is_mi_like_p ())
  224. uiout->field_string ("catch-type", "rethrow");
  225. break;
  226. case EX_EVENT_CATCH:
  227. uiout->field_string ("what", "exception catch");
  228. if (uiout->is_mi_like_p ())
  229. uiout->field_string ("catch-type", "catch");
  230. break;
  231. }
  232. }
  233. /* Implement the 'print_one_detail' method. */
  234. static void
  235. print_one_detail_exception_catchpoint (const struct breakpoint *b,
  236. struct ui_out *uiout)
  237. {
  238. const struct exception_catchpoint *cp
  239. = (const struct exception_catchpoint *) b;
  240. if (!cp->exception_rx.empty ())
  241. {
  242. uiout->text (_("\tmatching: "));
  243. uiout->field_string ("regexp", cp->exception_rx);
  244. uiout->text ("\n");
  245. }
  246. }
  247. static void
  248. print_mention_exception_catchpoint (struct breakpoint *b)
  249. {
  250. struct ui_out *uiout = current_uiout;
  251. int bp_temp;
  252. enum exception_event_kind kind = classify_exception_breakpoint (b);
  253. bp_temp = b->disposition == disp_del;
  254. uiout->message ("%s %d %s",
  255. (bp_temp ? _("Temporary catchpoint ") : _("Catchpoint")),
  256. b->number,
  257. (kind == EX_EVENT_THROW
  258. ? _("(throw)") : (kind == EX_EVENT_CATCH
  259. ? _("(catch)") : _("(rethrow)"))));
  260. }
  261. /* Implement the "print_recreate" breakpoint_ops method for throw and
  262. catch catchpoints. */
  263. static void
  264. print_recreate_exception_catchpoint (struct breakpoint *b,
  265. struct ui_file *fp)
  266. {
  267. int bp_temp;
  268. enum exception_event_kind kind = classify_exception_breakpoint (b);
  269. bp_temp = b->disposition == disp_del;
  270. gdb_printf (fp, bp_temp ? "tcatch " : "catch ");
  271. switch (kind)
  272. {
  273. case EX_EVENT_THROW:
  274. gdb_printf (fp, "throw");
  275. break;
  276. case EX_EVENT_CATCH:
  277. gdb_printf (fp, "catch");
  278. break;
  279. case EX_EVENT_RETHROW:
  280. gdb_printf (fp, "rethrow");
  281. break;
  282. }
  283. print_recreate_thread (b, fp);
  284. }
  285. /* Implement the "allocate_location" breakpoint_ops method for throw
  286. and catch catchpoints. */
  287. static bp_location *
  288. allocate_location_exception_catchpoint (breakpoint *self)
  289. {
  290. return new bp_location (self, bp_loc_software_breakpoint);
  291. }
  292. static void
  293. handle_gnu_v3_exceptions (int tempflag, std::string &&except_rx,
  294. const char *cond_string,
  295. enum exception_event_kind ex_event, int from_tty)
  296. {
  297. std::unique_ptr<compiled_regex> pattern;
  298. if (!except_rx.empty ())
  299. {
  300. pattern.reset (new compiled_regex (except_rx.c_str (), REG_NOSUB,
  301. _("invalid type-matching regexp")));
  302. }
  303. std::unique_ptr<exception_catchpoint> cp (new exception_catchpoint ());
  304. init_catchpoint (cp.get (), get_current_arch (), tempflag, cond_string,
  305. &gnu_v3_exception_catchpoint_ops);
  306. cp->kind = ex_event;
  307. cp->exception_rx = std::move (except_rx);
  308. cp->pattern = std::move (pattern);
  309. re_set_exception_catchpoint (cp.get ());
  310. install_breakpoint (0, std::move (cp), 1);
  311. }
  312. /* Look for an "if" token in *STRING. The "if" token must be preceded
  313. by whitespace.
  314. If there is any non-whitespace text between *STRING and the "if"
  315. token, then it is returned in a newly-xmalloc'd string. Otherwise,
  316. this returns NULL.
  317. STRING is updated to point to the "if" token, if it exists, or to
  318. the end of the string. */
  319. static std::string
  320. extract_exception_regexp (const char **string)
  321. {
  322. const char *start;
  323. const char *last, *last_space;
  324. start = skip_spaces (*string);
  325. last = start;
  326. last_space = start;
  327. while (*last != '\0')
  328. {
  329. const char *if_token = last;
  330. /* Check for the "if". */
  331. if (check_for_argument (&if_token, "if", 2))
  332. break;
  333. /* No "if" token here. Skip to the next word start. */
  334. last_space = skip_to_space (last);
  335. last = skip_spaces (last_space);
  336. }
  337. *string = last;
  338. if (last_space > start)
  339. return std::string (start, last_space - start);
  340. return std::string ();
  341. }
  342. /* See breakpoint.h. */
  343. void
  344. catch_exception_event (enum exception_event_kind ex_event,
  345. const char *arg, bool tempflag, int from_tty)
  346. {
  347. const char *cond_string = NULL;
  348. if (!arg)
  349. arg = "";
  350. arg = skip_spaces (arg);
  351. std::string except_rx = extract_exception_regexp (&arg);
  352. cond_string = ep_parse_optional_if_clause (&arg);
  353. if ((*arg != '\0') && !isspace (*arg))
  354. error (_("Junk at end of arguments."));
  355. if (ex_event != EX_EVENT_THROW
  356. && ex_event != EX_EVENT_CATCH
  357. && ex_event != EX_EVENT_RETHROW)
  358. error (_("Unsupported or unknown exception event; cannot catch it"));
  359. handle_gnu_v3_exceptions (tempflag, std::move (except_rx), cond_string,
  360. ex_event, from_tty);
  361. }
  362. /* Implementation of "catch catch" command. */
  363. static void
  364. catch_catch_command (const char *arg, int from_tty,
  365. struct cmd_list_element *command)
  366. {
  367. bool tempflag = command->context () == CATCH_TEMPORARY;
  368. catch_exception_event (EX_EVENT_CATCH, arg, tempflag, from_tty);
  369. }
  370. /* Implementation of "catch throw" command. */
  371. static void
  372. catch_throw_command (const char *arg, int from_tty,
  373. struct cmd_list_element *command)
  374. {
  375. bool tempflag = command->context () == CATCH_TEMPORARY;
  376. catch_exception_event (EX_EVENT_THROW, arg, tempflag, from_tty);
  377. }
  378. /* Implementation of "catch rethrow" command. */
  379. static void
  380. catch_rethrow_command (const char *arg, int from_tty,
  381. struct cmd_list_element *command)
  382. {
  383. bool tempflag = command->context () == CATCH_TEMPORARY;
  384. catch_exception_event (EX_EVENT_RETHROW, arg, tempflag, from_tty);
  385. }
  386. /* Implement the 'make_value' method for the $_exception
  387. internalvar. */
  388. static struct value *
  389. compute_exception (struct gdbarch *argc, struct internalvar *var, void *ignore)
  390. {
  391. struct value *arg0, *arg1;
  392. struct type *obj_type;
  393. fetch_probe_arguments (&arg0, &arg1);
  394. /* ARG0 is a pointer to the exception object. ARG1 is a pointer to
  395. the std::type_info for the exception. Now we find the type from
  396. the type_info and cast the result. */
  397. obj_type = cplus_type_from_type_info (arg1);
  398. return value_ind (value_cast (make_pointer_type (obj_type, NULL), arg0));
  399. }
  400. /* Implementation of the '$_exception' variable. */
  401. static const struct internalvar_funcs exception_funcs =
  402. {
  403. compute_exception,
  404. NULL,
  405. };
  406. static void
  407. initialize_throw_catchpoint_ops (void)
  408. {
  409. struct breakpoint_ops *ops;
  410. initialize_breakpoint_ops ();
  411. /* GNU v3 exception catchpoints. */
  412. ops = &gnu_v3_exception_catchpoint_ops;
  413. *ops = bkpt_breakpoint_ops;
  414. ops->re_set = re_set_exception_catchpoint;
  415. ops->print_it = print_it_exception_catchpoint;
  416. ops->print_one = print_one_exception_catchpoint;
  417. ops->print_mention = print_mention_exception_catchpoint;
  418. ops->print_recreate = print_recreate_exception_catchpoint;
  419. ops->print_one_detail = print_one_detail_exception_catchpoint;
  420. ops->check_status = check_status_exception_catchpoint;
  421. ops->allocate_location = allocate_location_exception_catchpoint;
  422. }
  423. void _initialize_break_catch_throw ();
  424. void
  425. _initialize_break_catch_throw ()
  426. {
  427. initialize_throw_catchpoint_ops ();
  428. /* Add catch and tcatch sub-commands. */
  429. add_catch_command ("catch", _("\
  430. Catch an exception, when caught."),
  431. catch_catch_command,
  432. NULL,
  433. CATCH_PERMANENT,
  434. CATCH_TEMPORARY);
  435. add_catch_command ("throw", _("\
  436. Catch an exception, when thrown."),
  437. catch_throw_command,
  438. NULL,
  439. CATCH_PERMANENT,
  440. CATCH_TEMPORARY);
  441. add_catch_command ("rethrow", _("\
  442. Catch an exception, when rethrown."),
  443. catch_rethrow_command,
  444. NULL,
  445. CATCH_PERMANENT,
  446. CATCH_TEMPORARY);
  447. create_internalvar_type_lazy ("_exception", &exception_funcs, NULL);
  448. }