1 /* Everything about catch/throw catchpoints, for GDB.
3 Copyright (C) 1986-2022 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/>. */
21 #include "arch-utils.h"
23 #include "breakpoint.h"
28 #include "cli/cli-utils.h"
29 #include "completer.h"
30 #include "gdbsupport/gdb_obstack.h"
31 #include "mi/mi-common.h"
36 #include "gdbsupport/gdb_regex.h"
37 #include "cp-support.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
52 /* The name of the corresponding 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. */
68 struct exception_catchpoint
: public base_breakpoint
70 void re_set () override
;
71 enum print_stop_action
print_it (struct bpstat
*bs
) override
;
72 bool print_one (struct bp_location
**) override
;
73 void print_mention () override
;
74 void print_recreate (struct ui_file
*fp
) override
;
75 void print_one_detail (struct ui_out
*) const override
;
76 void check_status (struct bpstat
*bs
) override
;
77 struct bp_location
*allocate_location () override
;
79 /* FIXME this is temporary - until ordinary breakpoints have been
81 int resources_needed (const struct bp_location
*) override
86 /* The kind of exception catchpoint. */
88 enum exception_event_kind kind
;
90 /* If not empty, a string holding the source form of the regular
91 expression to match against. */
93 std::string exception_rx
;
95 /* If non-NULL, a compiled regular expression which is used to
96 determine which exceptions to stop on. */
98 std::unique_ptr
<compiled_regex
> pattern
;
101 /* See breakpoint.h. */
104 is_exception_catchpoint (breakpoint
*bp
)
106 return dynamic_cast<exception_catchpoint
*> (bp
) != nullptr;
111 /* A helper function that fetches exception probe arguments. This
112 fills in *ARG0 (if non-NULL) and *ARG1 (which must be non-NULL).
113 It will throw an exception on any kind of failure. */
116 fetch_probe_arguments (struct value
**arg0
, struct value
**arg1
)
118 struct frame_info
*frame
= get_selected_frame (_("No frame selected"));
119 CORE_ADDR pc
= get_frame_pc (frame
);
120 struct bound_probe pc_probe
;
123 pc_probe
= find_probe_by_pc (pc
);
124 if (pc_probe
.prob
== NULL
)
125 error (_("did not find exception probe (does libstdcxx have SDT probes?)"));
127 if (pc_probe
.prob
->get_provider () != "libstdcxx"
128 || (pc_probe
.prob
->get_name () != "catch"
129 && pc_probe
.prob
->get_name () != "throw"
130 && pc_probe
.prob
->get_name () != "rethrow"))
131 error (_("not stopped at a C++ exception catchpoint"));
133 n_args
= pc_probe
.prob
->get_argument_count (get_frame_arch (frame
));
135 error (_("C++ exception catchpoint has too few arguments"));
138 *arg0
= pc_probe
.prob
->evaluate_argument (0, frame
);
139 *arg1
= pc_probe
.prob
->evaluate_argument (1, frame
);
141 if ((arg0
!= NULL
&& *arg0
== NULL
) || *arg1
== NULL
)
142 error (_("error computing probe argument at c++ exception catchpoint"));
147 /* Implement the 'check_status' method. */
150 exception_catchpoint::check_status (struct bpstat
*bs
)
152 struct exception_catchpoint
*self
153 = (struct exception_catchpoint
*) bs
->breakpoint_at
;
154 std::string type_name
;
156 this->breakpoint::check_status (bs
);
160 if (self
->pattern
== NULL
)
163 const char *name
= nullptr;
164 gdb::unique_xmalloc_ptr
<char> canon
;
167 struct value
*typeinfo_arg
;
169 fetch_probe_arguments (NULL
, &typeinfo_arg
);
170 type_name
= cplus_typename_from_type_info (typeinfo_arg
);
172 canon
= cp_canonicalize_string (type_name
.c_str ());
173 name
= (canon
!= nullptr
175 : type_name
.c_str ());
177 catch (const gdb_exception_error
&e
)
179 exception_print (gdb_stderr
, e
);
184 if (self
->pattern
->exec (name
, 0, NULL
, 0) != 0)
189 /* Implement the 're_set' method. */
192 exception_catchpoint::re_set ()
194 std::vector
<symtab_and_line
> sals
;
195 struct program_space
*filter_pspace
= current_program_space
;
197 /* We first try to use the probe interface. */
200 event_location_up location
201 = new_probe_location (exception_functions
[kind
].probe
);
202 sals
= parse_probes (location
.get (), filter_pspace
, NULL
);
204 catch (const gdb_exception_error
&e
)
206 /* Using the probe interface failed. Let's fallback to the normal
210 struct explicit_location explicit_loc
;
212 initialize_explicit_location (&explicit_loc
);
213 explicit_loc
.function_name
214 = ASTRDUP (exception_functions
[kind
].function
);
215 event_location_up location
= new_explicit_location (&explicit_loc
);
216 sals
= this->decode_location (location
.get (), filter_pspace
);
218 catch (const gdb_exception_error
&ex
)
220 /* NOT_FOUND_ERROR just means the breakpoint will be
221 pending, so let it through. */
222 if (ex
.error
!= NOT_FOUND_ERROR
)
227 update_breakpoint_locations (this, filter_pspace
, sals
, {});
230 enum print_stop_action
231 exception_catchpoint::print_it (bpstat
*bs
)
233 struct ui_out
*uiout
= current_uiout
;
236 annotate_catchpoint (number
);
237 maybe_print_thread_hit_breakpoint (uiout
);
239 bp_temp
= disposition
== disp_del
;
240 uiout
->text (bp_temp
? "Temporary catchpoint "
242 uiout
->field_signed ("bkptno", number
);
243 uiout
->text ((kind
== EX_EVENT_THROW
? " (exception thrown), "
244 : (kind
== EX_EVENT_CATCH
? " (exception caught), "
245 : " (exception rethrown), ")));
246 if (uiout
->is_mi_like_p ())
248 uiout
->field_string ("reason",
249 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT
));
250 uiout
->field_string ("disp", bpdisp_text (disposition
));
252 return PRINT_SRC_AND_LOC
;
256 exception_catchpoint::print_one (struct bp_location
**last_loc
)
258 struct value_print_options opts
;
259 struct ui_out
*uiout
= current_uiout
;
261 get_user_print_options (&opts
);
263 if (opts
.addressprint
)
264 uiout
->field_skip ("addr");
270 uiout
->field_string ("what", "exception throw");
271 if (uiout
->is_mi_like_p ())
272 uiout
->field_string ("catch-type", "throw");
275 case EX_EVENT_RETHROW
:
276 uiout
->field_string ("what", "exception rethrow");
277 if (uiout
->is_mi_like_p ())
278 uiout
->field_string ("catch-type", "rethrow");
282 uiout
->field_string ("what", "exception catch");
283 if (uiout
->is_mi_like_p ())
284 uiout
->field_string ("catch-type", "catch");
291 /* Implement the 'print_one_detail' method. */
294 exception_catchpoint::print_one_detail (struct ui_out
*uiout
) const
296 if (!exception_rx
.empty ())
298 uiout
->text (_("\tmatching: "));
299 uiout
->field_string ("regexp", exception_rx
);
305 exception_catchpoint::print_mention ()
307 struct ui_out
*uiout
= current_uiout
;
310 bp_temp
= disposition
== disp_del
;
311 uiout
->message ("%s %d %s",
312 (bp_temp
? _("Temporary catchpoint ") : _("Catchpoint")),
314 (kind
== EX_EVENT_THROW
315 ? _("(throw)") : (kind
== EX_EVENT_CATCH
316 ? _("(catch)") : _("(rethrow)"))));
319 /* Implement the "print_recreate" method for throw and catch
323 exception_catchpoint::print_recreate (struct ui_file
*fp
)
327 bp_temp
= disposition
== disp_del
;
328 gdb_printf (fp
, bp_temp
? "tcatch " : "catch ");
332 gdb_printf (fp
, "throw");
335 gdb_printf (fp
, "catch");
337 case EX_EVENT_RETHROW
:
338 gdb_printf (fp
, "rethrow");
341 print_recreate_thread (this, fp
);
344 /* Implement the "allocate_location" method for throw and catch
348 exception_catchpoint::allocate_location ()
350 return new bp_location (this, bp_loc_software_breakpoint
);
354 handle_gnu_v3_exceptions (int tempflag
, std::string
&&except_rx
,
355 const char *cond_string
,
356 enum exception_event_kind ex_event
, int from_tty
)
358 std::unique_ptr
<compiled_regex
> pattern
;
360 if (!except_rx
.empty ())
362 pattern
.reset (new compiled_regex (except_rx
.c_str (), REG_NOSUB
,
363 _("invalid type-matching regexp")));
366 std::unique_ptr
<exception_catchpoint
> cp (new exception_catchpoint ());
368 init_catchpoint (cp
.get (), get_current_arch (), tempflag
, cond_string
,
369 &vtable_breakpoint_ops
);
371 cp
->exception_rx
= std::move (except_rx
);
372 cp
->pattern
= std::move (pattern
);
376 install_breakpoint (0, std::move (cp
), 1);
379 /* Look for an "if" token in *STRING. The "if" token must be preceded
382 If there is any non-whitespace text between *STRING and the "if"
383 token, then it is returned in a newly-xmalloc'd string. Otherwise,
386 STRING is updated to point to the "if" token, if it exists, or to
387 the end of the string. */
390 extract_exception_regexp (const char **string
)
393 const char *last
, *last_space
;
395 start
= skip_spaces (*string
);
399 while (*last
!= '\0')
401 const char *if_token
= last
;
403 /* Check for the "if". */
404 if (check_for_argument (&if_token
, "if", 2))
407 /* No "if" token here. Skip to the next word start. */
408 last_space
= skip_to_space (last
);
409 last
= skip_spaces (last_space
);
413 if (last_space
> start
)
414 return std::string (start
, last_space
- start
);
415 return std::string ();
418 /* See breakpoint.h. */
421 catch_exception_event (enum exception_event_kind ex_event
,
422 const char *arg
, bool tempflag
, int from_tty
)
424 const char *cond_string
= NULL
;
428 arg
= skip_spaces (arg
);
430 std::string except_rx
= extract_exception_regexp (&arg
);
432 cond_string
= ep_parse_optional_if_clause (&arg
);
434 if ((*arg
!= '\0') && !isspace (*arg
))
435 error (_("Junk at end of arguments."));
437 if (ex_event
!= EX_EVENT_THROW
438 && ex_event
!= EX_EVENT_CATCH
439 && ex_event
!= EX_EVENT_RETHROW
)
440 error (_("Unsupported or unknown exception event; cannot catch it"));
442 handle_gnu_v3_exceptions (tempflag
, std::move (except_rx
), cond_string
,
446 /* Implementation of "catch catch" command. */
449 catch_catch_command (const char *arg
, int from_tty
,
450 struct cmd_list_element
*command
)
452 bool tempflag
= command
->context () == CATCH_TEMPORARY
;
454 catch_exception_event (EX_EVENT_CATCH
, arg
, tempflag
, from_tty
);
457 /* Implementation of "catch throw" command. */
460 catch_throw_command (const char *arg
, int from_tty
,
461 struct cmd_list_element
*command
)
463 bool tempflag
= command
->context () == CATCH_TEMPORARY
;
465 catch_exception_event (EX_EVENT_THROW
, arg
, tempflag
, from_tty
);
468 /* Implementation of "catch rethrow" command. */
471 catch_rethrow_command (const char *arg
, int from_tty
,
472 struct cmd_list_element
*command
)
474 bool tempflag
= command
->context () == CATCH_TEMPORARY
;
476 catch_exception_event (EX_EVENT_RETHROW
, arg
, tempflag
, from_tty
);
481 /* Implement the 'make_value' method for the $_exception
484 static struct value
*
485 compute_exception (struct gdbarch
*argc
, struct internalvar
*var
, void *ignore
)
487 struct value
*arg0
, *arg1
;
488 struct type
*obj_type
;
490 fetch_probe_arguments (&arg0
, &arg1
);
492 /* ARG0 is a pointer to the exception object. ARG1 is a pointer to
493 the std::type_info for the exception. Now we find the type from
494 the type_info and cast the result. */
495 obj_type
= cplus_type_from_type_info (arg1
);
496 return value_ind (value_cast (make_pointer_type (obj_type
, NULL
), arg0
));
499 /* Implementation of the '$_exception' variable. */
501 static const struct internalvar_funcs exception_funcs
=
509 void _initialize_break_catch_throw ();
511 _initialize_break_catch_throw ()
513 /* Add catch and tcatch sub-commands. */
514 add_catch_command ("catch", _("\
515 Catch an exception, when caught."),
520 add_catch_command ("throw", _("\
521 Catch an exception, when thrown."),
526 add_catch_command ("rethrow", _("\
527 Catch an exception, when rethrown."),
528 catch_rethrow_command
,
533 create_internalvar_type_lazy ("_exception", &exception_funcs
, NULL
);