Add generated source files and fix thinko in aarch64-asm.c
[binutils-gdb.git] / gdb / break-catch-throw.c
blobd053bd5fbe021c132b2da7f68eb6b5ad1b6519e7
1 /* Everything about catch/throw catchpoints, for GDB.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "breakpoint.h"
24 #include "gdbcmd.h"
25 #include "inferior.h"
26 #include "annotate.h"
27 #include "valprint.h"
28 #include "cli/cli-utils.h"
29 #include "completer.h"
30 #include "gdbsupport/gdb_obstack.h"
31 #include "mi/mi-common.h"
32 #include "linespec.h"
33 #include "probe.h"
34 #include "objfiles.h"
35 #include "cp-abi.h"
36 #include "gdbsupport/gdb_regex.h"
37 #include "cp-support.h"
38 #include "location.h"
39 #include "cli/cli-decode.h"
41 /* Each spot where we may place an exception-related catchpoint has
42 two names: the SDT probe point and the function name. This
43 structure holds both. */
45 struct exception_names
47 /* The name of the probe point to try, in the form accepted by
48 'parse_probes'. */
50 const char *probe;
52 /* The name of the corresponding function. */
54 const char *function;
57 /* Names of the probe points and functions on which to break. This is
58 indexed by exception_event_kind. */
59 static const struct exception_names exception_functions[] =
61 { "-probe-stap libstdcxx:throw", "__cxa_throw" },
62 { "-probe-stap libstdcxx:rethrow", "__cxa_rethrow" },
63 { "-probe-stap libstdcxx:catch", "__cxa_begin_catch" }
66 /* The type of an exception catchpoint. Unlike most catchpoints, this
67 one is implemented with code breakpoints, so it inherits struct
68 code_breakpoint, not struct catchpoint. */
70 struct exception_catchpoint : public code_breakpoint
72 exception_catchpoint (struct gdbarch *gdbarch,
73 bool temp, const char *cond_string_,
74 enum exception_event_kind kind_,
75 std::string &&except_rx)
76 : code_breakpoint (gdbarch, bp_catchpoint, temp, cond_string_),
77 kind (kind_),
78 exception_rx (std::move (except_rx)),
79 pattern (exception_rx.empty ()
80 ? nullptr
81 : new compiled_regex (exception_rx.c_str (), REG_NOSUB,
82 _("invalid type-matching regexp")))
84 pspace = current_program_space;
85 re_set ();
88 void re_set () override;
89 enum print_stop_action print_it (const bpstat *bs) const override;
90 bool print_one (const bp_location **) const override;
91 void print_mention () const override;
92 void print_recreate (struct ui_file *fp) const override;
93 void print_one_detail (struct ui_out *) const override;
94 void check_status (struct bpstat *bs) override;
95 struct bp_location *allocate_location () override;
97 /* The kind of exception catchpoint. */
99 enum exception_event_kind kind;
101 /* If not empty, a string holding the source form of the regular
102 expression to match against. */
104 std::string exception_rx;
106 /* If non-NULL, a compiled regular expression which is used to
107 determine which exceptions to stop on. */
109 std::unique_ptr<compiled_regex> pattern;
112 /* See breakpoint.h. */
114 bool
115 is_exception_catchpoint (breakpoint *bp)
117 return dynamic_cast<exception_catchpoint *> (bp) != nullptr;
122 /* A helper function that fetches exception probe arguments. This
123 fills in *ARG0 (if non-NULL) and *ARG1 (which must be non-NULL).
124 It will throw an exception on any kind of failure. */
126 static void
127 fetch_probe_arguments (struct value **arg0, struct value **arg1)
129 frame_info_ptr frame = get_selected_frame (_("No frame selected"));
130 CORE_ADDR pc = get_frame_pc (frame);
131 struct bound_probe pc_probe;
132 unsigned n_args;
134 pc_probe = find_probe_by_pc (pc);
135 if (pc_probe.prob == NULL)
136 error (_("did not find exception probe (does libstdcxx have SDT probes?)"));
138 if (pc_probe.prob->get_provider () != "libstdcxx"
139 || (pc_probe.prob->get_name () != "catch"
140 && pc_probe.prob->get_name () != "throw"
141 && pc_probe.prob->get_name () != "rethrow"))
142 error (_("not stopped at a C++ exception catchpoint"));
144 n_args = pc_probe.prob->get_argument_count (get_frame_arch (frame));
145 if (n_args < 2)
146 error (_("C++ exception catchpoint has too few arguments"));
148 if (arg0 != NULL)
149 *arg0 = pc_probe.prob->evaluate_argument (0, frame);
150 *arg1 = pc_probe.prob->evaluate_argument (1, frame);
152 if ((arg0 != NULL && *arg0 == NULL) || *arg1 == NULL)
153 error (_("error computing probe argument at c++ exception catchpoint"));
158 /* Implement the 'check_status' method. */
160 void
161 exception_catchpoint::check_status (struct bpstat *bs)
163 std::string type_name;
165 this->breakpoint::check_status (bs);
166 if (!bs->stop)
167 return;
169 if (this->pattern == NULL)
170 return;
172 const char *name = nullptr;
173 gdb::unique_xmalloc_ptr<char> canon;
176 struct value *typeinfo_arg;
178 fetch_probe_arguments (NULL, &typeinfo_arg);
179 type_name = cplus_typename_from_type_info (typeinfo_arg);
181 canon = cp_canonicalize_string (type_name.c_str ());
182 name = (canon != nullptr
183 ? canon.get ()
184 : type_name.c_str ());
186 catch (const gdb_exception_error &e)
188 exception_print (gdb_stderr, e);
191 if (name != nullptr)
193 if (this->pattern->exec (name, 0, NULL, 0) != 0)
194 bs->stop = false;
198 /* Implement the 're_set' method. */
200 void
201 exception_catchpoint::re_set ()
203 std::vector<symtab_and_line> sals;
204 struct program_space *filter_pspace = current_program_space;
206 /* We first try to use the probe interface. */
209 location_spec_up locspec
210 = new_probe_location_spec (exception_functions[kind].probe);
211 sals = parse_probes (locspec.get (), filter_pspace, NULL);
213 catch (const gdb_exception_error &e)
215 /* Using the probe interface failed. Let's fallback to the normal
216 catchpoint mode. */
219 location_spec_up locspec
220 = (new_explicit_location_spec_function
221 (exception_functions[kind].function));
222 sals = this->decode_location_spec (locspec.get (), filter_pspace);
224 catch (const gdb_exception_error &ex)
226 /* NOT_FOUND_ERROR just means the breakpoint will be
227 pending, so let it through. */
228 if (ex.error != NOT_FOUND_ERROR)
229 throw;
233 update_breakpoint_locations (this, filter_pspace, sals, {});
236 enum print_stop_action
237 exception_catchpoint::print_it (const bpstat *bs) const
239 struct ui_out *uiout = current_uiout;
240 int bp_temp;
242 annotate_catchpoint (number);
243 maybe_print_thread_hit_breakpoint (uiout);
245 bp_temp = disposition == disp_del;
246 uiout->text (bp_temp ? "Temporary catchpoint "
247 : "Catchpoint ");
248 print_num_locno (bs, uiout);
249 uiout->text ((kind == EX_EVENT_THROW ? " (exception thrown), "
250 : (kind == EX_EVENT_CATCH ? " (exception caught), "
251 : " (exception rethrown), ")));
252 if (uiout->is_mi_like_p ())
254 uiout->field_string ("reason",
255 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
256 uiout->field_string ("disp", bpdisp_text (disposition));
258 return PRINT_SRC_AND_LOC;
261 bool
262 exception_catchpoint::print_one (const bp_location **last_loc) const
264 struct value_print_options opts;
265 struct ui_out *uiout = current_uiout;
267 get_user_print_options (&opts);
269 if (opts.addressprint)
270 uiout->field_skip ("addr");
271 annotate_field (5);
273 switch (kind)
275 case EX_EVENT_THROW:
276 uiout->field_string ("what", "exception throw");
277 if (uiout->is_mi_like_p ())
278 uiout->field_string ("catch-type", "throw");
279 break;
281 case EX_EVENT_RETHROW:
282 uiout->field_string ("what", "exception rethrow");
283 if (uiout->is_mi_like_p ())
284 uiout->field_string ("catch-type", "rethrow");
285 break;
287 case EX_EVENT_CATCH:
288 uiout->field_string ("what", "exception catch");
289 if (uiout->is_mi_like_p ())
290 uiout->field_string ("catch-type", "catch");
291 break;
294 return true;
297 /* Implement the 'print_one_detail' method. */
299 void
300 exception_catchpoint::print_one_detail (struct ui_out *uiout) const
302 if (!exception_rx.empty ())
304 uiout->text (_("\tmatching: "));
305 uiout->field_string ("regexp", exception_rx);
306 uiout->text ("\n");
310 void
311 exception_catchpoint::print_mention () const
313 struct ui_out *uiout = current_uiout;
314 int bp_temp;
316 bp_temp = disposition == disp_del;
317 uiout->message ("%s %d %s",
318 (bp_temp ? _("Temporary catchpoint ") : _("Catchpoint")),
319 number,
320 (kind == EX_EVENT_THROW
321 ? _("(throw)") : (kind == EX_EVENT_CATCH
322 ? _("(catch)") : _("(rethrow)"))));
325 /* Implement the "print_recreate" method for throw and catch
326 catchpoints. */
328 void
329 exception_catchpoint::print_recreate (struct ui_file *fp) const
331 int bp_temp;
333 bp_temp = disposition == disp_del;
334 gdb_printf (fp, bp_temp ? "tcatch " : "catch ");
335 switch (kind)
337 case EX_EVENT_THROW:
338 gdb_printf (fp, "throw");
339 break;
340 case EX_EVENT_CATCH:
341 gdb_printf (fp, "catch");
342 break;
343 case EX_EVENT_RETHROW:
344 gdb_printf (fp, "rethrow");
345 break;
347 print_recreate_thread (fp);
350 /* Implement the "allocate_location" method for throw and catch
351 catchpoints. */
353 bp_location *
354 exception_catchpoint::allocate_location ()
356 return new bp_location (this, bp_loc_software_breakpoint);
359 static void
360 handle_gnu_v3_exceptions (int tempflag, std::string &&except_rx,
361 const char *cond_string,
362 enum exception_event_kind ex_event, int from_tty)
364 struct gdbarch *gdbarch = get_current_arch ();
366 std::unique_ptr<exception_catchpoint> cp
367 (new exception_catchpoint (gdbarch, tempflag, cond_string,
368 ex_event, std::move (except_rx)));
370 install_breakpoint (0, std::move (cp), 1);
373 /* Look for an "if" token in *STRING. The "if" token must be preceded
374 by whitespace.
376 If there is any non-whitespace text between *STRING and the "if"
377 token, then it is returned in a newly-xmalloc'd string. Otherwise,
378 this returns NULL.
380 STRING is updated to point to the "if" token, if it exists, or to
381 the end of the string. */
383 static std::string
384 extract_exception_regexp (const char **string)
386 const char *start;
387 const char *last, *last_space;
389 start = skip_spaces (*string);
391 last = start;
392 last_space = start;
393 while (*last != '\0')
395 const char *if_token = last;
397 /* Check for the "if". */
398 if (check_for_argument (&if_token, "if", 2))
399 break;
401 /* No "if" token here. Skip to the next word start. */
402 last_space = skip_to_space (last);
403 last = skip_spaces (last_space);
406 *string = last;
407 if (last_space > start)
408 return std::string (start, last_space - start);
409 return std::string ();
412 /* See breakpoint.h. */
414 void
415 catch_exception_event (enum exception_event_kind ex_event,
416 const char *arg, bool tempflag, int from_tty)
418 const char *cond_string = NULL;
420 if (!arg)
421 arg = "";
422 arg = skip_spaces (arg);
424 std::string except_rx = extract_exception_regexp (&arg);
426 cond_string = ep_parse_optional_if_clause (&arg);
428 if ((*arg != '\0') && !isspace (*arg))
429 error (_("Junk at end of arguments."));
431 if (ex_event != EX_EVENT_THROW
432 && ex_event != EX_EVENT_CATCH
433 && ex_event != EX_EVENT_RETHROW)
434 error (_("Unsupported or unknown exception event; cannot catch it"));
436 handle_gnu_v3_exceptions (tempflag, std::move (except_rx), cond_string,
437 ex_event, from_tty);
440 /* Implementation of "catch catch" command. */
442 static void
443 catch_catch_command (const char *arg, int from_tty,
444 struct cmd_list_element *command)
446 bool tempflag = command->context () == CATCH_TEMPORARY;
448 catch_exception_event (EX_EVENT_CATCH, arg, tempflag, from_tty);
451 /* Implementation of "catch throw" command. */
453 static void
454 catch_throw_command (const char *arg, int from_tty,
455 struct cmd_list_element *command)
457 bool tempflag = command->context () == CATCH_TEMPORARY;
459 catch_exception_event (EX_EVENT_THROW, arg, tempflag, from_tty);
462 /* Implementation of "catch rethrow" command. */
464 static void
465 catch_rethrow_command (const char *arg, int from_tty,
466 struct cmd_list_element *command)
468 bool tempflag = command->context () == CATCH_TEMPORARY;
470 catch_exception_event (EX_EVENT_RETHROW, arg, tempflag, from_tty);
475 /* Implement the 'make_value' method for the $_exception
476 internalvar. */
478 static struct value *
479 compute_exception (struct gdbarch *argc, struct internalvar *var, void *ignore)
481 struct value *arg0, *arg1;
482 struct type *obj_type;
484 fetch_probe_arguments (&arg0, &arg1);
486 /* ARG0 is a pointer to the exception object. ARG1 is a pointer to
487 the std::type_info for the exception. Now we find the type from
488 the type_info and cast the result. */
489 obj_type = cplus_type_from_type_info (arg1);
490 return value_ind (value_cast (make_pointer_type (obj_type, NULL), arg0));
493 /* Implementation of the '$_exception' variable. */
495 static const struct internalvar_funcs exception_funcs =
497 compute_exception,
498 NULL,
503 void _initialize_break_catch_throw ();
504 void
505 _initialize_break_catch_throw ()
507 /* Add catch and tcatch sub-commands. */
508 add_catch_command ("catch", _("\
509 Catch an exception, when caught."),
510 catch_catch_command,
511 NULL,
512 CATCH_PERMANENT,
513 CATCH_TEMPORARY);
514 add_catch_command ("throw", _("\
515 Catch an exception, when thrown."),
516 catch_throw_command,
517 NULL,
518 CATCH_PERMANENT,
519 CATCH_TEMPORARY);
520 add_catch_command ("rethrow", _("\
521 Catch an exception, when rethrown."),
522 catch_rethrow_command,
523 NULL,
524 CATCH_PERMANENT,
525 CATCH_TEMPORARY);
527 create_internalvar_type_lazy ("_exception", &exception_funcs, NULL);