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 catchpoint
70 exception_catchpoint (struct gdbarch
*gdbarch
,
71 bool temp
, const char *cond_string
,
72 enum exception_event_kind kind_
,
73 std::string
&&except_rx
)
74 : catchpoint (gdbarch
, temp
, cond_string
),
76 exception_rx (std::move (except_rx
)),
77 pattern (exception_rx
.empty ()
79 : new compiled_regex (exception_rx
.c_str (), REG_NOSUB
,
80 _("invalid type-matching regexp")))
84 void re_set () override
;
85 enum print_stop_action
print_it (const bpstat
*bs
) const override
;
86 bool print_one (bp_location
**) const override
;
87 void print_mention () const override
;
88 void print_recreate (struct ui_file
*fp
) const override
;
89 void print_one_detail (struct ui_out
*) const override
;
90 void check_status (struct bpstat
*bs
) override
;
91 struct bp_location
*allocate_location () override
;
93 /* FIXME this is temporary - until ordinary breakpoints have been
95 int resources_needed (const struct bp_location
*) override
100 /* The kind of exception catchpoint. */
102 enum exception_event_kind kind
;
104 /* If not empty, a string holding the source form of the regular
105 expression to match against. */
107 std::string exception_rx
;
109 /* If non-NULL, a compiled regular expression which is used to
110 determine which exceptions to stop on. */
112 std::unique_ptr
<compiled_regex
> pattern
;
115 /* See breakpoint.h. */
118 is_exception_catchpoint (breakpoint
*bp
)
120 return dynamic_cast<exception_catchpoint
*> (bp
) != nullptr;
125 /* A helper function that fetches exception probe arguments. This
126 fills in *ARG0 (if non-NULL) and *ARG1 (which must be non-NULL).
127 It will throw an exception on any kind of failure. */
130 fetch_probe_arguments (struct value
**arg0
, struct value
**arg1
)
132 struct frame_info
*frame
= get_selected_frame (_("No frame selected"));
133 CORE_ADDR pc
= get_frame_pc (frame
);
134 struct bound_probe pc_probe
;
137 pc_probe
= find_probe_by_pc (pc
);
138 if (pc_probe
.prob
== NULL
)
139 error (_("did not find exception probe (does libstdcxx have SDT probes?)"));
141 if (pc_probe
.prob
->get_provider () != "libstdcxx"
142 || (pc_probe
.prob
->get_name () != "catch"
143 && pc_probe
.prob
->get_name () != "throw"
144 && pc_probe
.prob
->get_name () != "rethrow"))
145 error (_("not stopped at a C++ exception catchpoint"));
147 n_args
= pc_probe
.prob
->get_argument_count (get_frame_arch (frame
));
149 error (_("C++ exception catchpoint has too few arguments"));
152 *arg0
= pc_probe
.prob
->evaluate_argument (0, frame
);
153 *arg1
= pc_probe
.prob
->evaluate_argument (1, frame
);
155 if ((arg0
!= NULL
&& *arg0
== NULL
) || *arg1
== NULL
)
156 error (_("error computing probe argument at c++ exception catchpoint"));
161 /* Implement the 'check_status' method. */
164 exception_catchpoint::check_status (struct bpstat
*bs
)
166 struct exception_catchpoint
*self
167 = (struct exception_catchpoint
*) bs
->breakpoint_at
;
168 std::string type_name
;
170 this->breakpoint::check_status (bs
);
174 if (self
->pattern
== NULL
)
177 const char *name
= nullptr;
178 gdb::unique_xmalloc_ptr
<char> canon
;
181 struct value
*typeinfo_arg
;
183 fetch_probe_arguments (NULL
, &typeinfo_arg
);
184 type_name
= cplus_typename_from_type_info (typeinfo_arg
);
186 canon
= cp_canonicalize_string (type_name
.c_str ());
187 name
= (canon
!= nullptr
189 : type_name
.c_str ());
191 catch (const gdb_exception_error
&e
)
193 exception_print (gdb_stderr
, e
);
198 if (self
->pattern
->exec (name
, 0, NULL
, 0) != 0)
203 /* Implement the 're_set' method. */
206 exception_catchpoint::re_set ()
208 std::vector
<symtab_and_line
> sals
;
209 struct program_space
*filter_pspace
= current_program_space
;
211 /* We first try to use the probe interface. */
214 event_location_up location
215 = new_probe_location (exception_functions
[kind
].probe
);
216 sals
= parse_probes (location
.get (), filter_pspace
, NULL
);
218 catch (const gdb_exception_error
&e
)
220 /* Using the probe interface failed. Let's fallback to the normal
224 struct explicit_location explicit_loc
;
226 initialize_explicit_location (&explicit_loc
);
227 explicit_loc
.function_name
228 = ASTRDUP (exception_functions
[kind
].function
);
229 event_location_up location
= new_explicit_location (&explicit_loc
);
230 sals
= this->decode_location (location
.get (), filter_pspace
);
232 catch (const gdb_exception_error
&ex
)
234 /* NOT_FOUND_ERROR just means the breakpoint will be
235 pending, so let it through. */
236 if (ex
.error
!= NOT_FOUND_ERROR
)
241 update_breakpoint_locations (this, filter_pspace
, sals
, {});
244 enum print_stop_action
245 exception_catchpoint::print_it (const bpstat
*bs
) const
247 struct ui_out
*uiout
= current_uiout
;
250 annotate_catchpoint (number
);
251 maybe_print_thread_hit_breakpoint (uiout
);
253 bp_temp
= disposition
== disp_del
;
254 uiout
->text (bp_temp
? "Temporary catchpoint "
256 uiout
->field_signed ("bkptno", number
);
257 uiout
->text ((kind
== EX_EVENT_THROW
? " (exception thrown), "
258 : (kind
== EX_EVENT_CATCH
? " (exception caught), "
259 : " (exception rethrown), ")));
260 if (uiout
->is_mi_like_p ())
262 uiout
->field_string ("reason",
263 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT
));
264 uiout
->field_string ("disp", bpdisp_text (disposition
));
266 return PRINT_SRC_AND_LOC
;
270 exception_catchpoint::print_one (bp_location
**last_loc
) const
272 struct value_print_options opts
;
273 struct ui_out
*uiout
= current_uiout
;
275 get_user_print_options (&opts
);
277 if (opts
.addressprint
)
278 uiout
->field_skip ("addr");
284 uiout
->field_string ("what", "exception throw");
285 if (uiout
->is_mi_like_p ())
286 uiout
->field_string ("catch-type", "throw");
289 case EX_EVENT_RETHROW
:
290 uiout
->field_string ("what", "exception rethrow");
291 if (uiout
->is_mi_like_p ())
292 uiout
->field_string ("catch-type", "rethrow");
296 uiout
->field_string ("what", "exception catch");
297 if (uiout
->is_mi_like_p ())
298 uiout
->field_string ("catch-type", "catch");
305 /* Implement the 'print_one_detail' method. */
308 exception_catchpoint::print_one_detail (struct ui_out
*uiout
) const
310 if (!exception_rx
.empty ())
312 uiout
->text (_("\tmatching: "));
313 uiout
->field_string ("regexp", exception_rx
);
319 exception_catchpoint::print_mention () const
321 struct ui_out
*uiout
= current_uiout
;
324 bp_temp
= disposition
== disp_del
;
325 uiout
->message ("%s %d %s",
326 (bp_temp
? _("Temporary catchpoint ") : _("Catchpoint")),
328 (kind
== EX_EVENT_THROW
329 ? _("(throw)") : (kind
== EX_EVENT_CATCH
330 ? _("(catch)") : _("(rethrow)"))));
333 /* Implement the "print_recreate" method for throw and catch
337 exception_catchpoint::print_recreate (struct ui_file
*fp
) const
341 bp_temp
= disposition
== disp_del
;
342 gdb_printf (fp
, bp_temp
? "tcatch " : "catch ");
346 gdb_printf (fp
, "throw");
349 gdb_printf (fp
, "catch");
351 case EX_EVENT_RETHROW
:
352 gdb_printf (fp
, "rethrow");
355 print_recreate_thread (fp
);
358 /* Implement the "allocate_location" method for throw and catch
362 exception_catchpoint::allocate_location ()
364 return new bp_location (this, bp_loc_software_breakpoint
);
368 handle_gnu_v3_exceptions (int tempflag
, std::string
&&except_rx
,
369 const char *cond_string
,
370 enum exception_event_kind ex_event
, int from_tty
)
372 struct gdbarch
*gdbarch
= get_current_arch ();
374 std::unique_ptr
<exception_catchpoint
> cp
375 (new exception_catchpoint (gdbarch
, tempflag
, cond_string
,
376 ex_event
, std::move (except_rx
)));
380 install_breakpoint (0, std::move (cp
), 1);
383 /* Look for an "if" token in *STRING. The "if" token must be preceded
386 If there is any non-whitespace text between *STRING and the "if"
387 token, then it is returned in a newly-xmalloc'd string. Otherwise,
390 STRING is updated to point to the "if" token, if it exists, or to
391 the end of the string. */
394 extract_exception_regexp (const char **string
)
397 const char *last
, *last_space
;
399 start
= skip_spaces (*string
);
403 while (*last
!= '\0')
405 const char *if_token
= last
;
407 /* Check for the "if". */
408 if (check_for_argument (&if_token
, "if", 2))
411 /* No "if" token here. Skip to the next word start. */
412 last_space
= skip_to_space (last
);
413 last
= skip_spaces (last_space
);
417 if (last_space
> start
)
418 return std::string (start
, last_space
- start
);
419 return std::string ();
422 /* See breakpoint.h. */
425 catch_exception_event (enum exception_event_kind ex_event
,
426 const char *arg
, bool tempflag
, int from_tty
)
428 const char *cond_string
= NULL
;
432 arg
= skip_spaces (arg
);
434 std::string except_rx
= extract_exception_regexp (&arg
);
436 cond_string
= ep_parse_optional_if_clause (&arg
);
438 if ((*arg
!= '\0') && !isspace (*arg
))
439 error (_("Junk at end of arguments."));
441 if (ex_event
!= EX_EVENT_THROW
442 && ex_event
!= EX_EVENT_CATCH
443 && ex_event
!= EX_EVENT_RETHROW
)
444 error (_("Unsupported or unknown exception event; cannot catch it"));
446 handle_gnu_v3_exceptions (tempflag
, std::move (except_rx
), cond_string
,
450 /* Implementation of "catch catch" command. */
453 catch_catch_command (const char *arg
, int from_tty
,
454 struct cmd_list_element
*command
)
456 bool tempflag
= command
->context () == CATCH_TEMPORARY
;
458 catch_exception_event (EX_EVENT_CATCH
, arg
, tempflag
, from_tty
);
461 /* Implementation of "catch throw" command. */
464 catch_throw_command (const char *arg
, int from_tty
,
465 struct cmd_list_element
*command
)
467 bool tempflag
= command
->context () == CATCH_TEMPORARY
;
469 catch_exception_event (EX_EVENT_THROW
, arg
, tempflag
, from_tty
);
472 /* Implementation of "catch rethrow" command. */
475 catch_rethrow_command (const char *arg
, int from_tty
,
476 struct cmd_list_element
*command
)
478 bool tempflag
= command
->context () == CATCH_TEMPORARY
;
480 catch_exception_event (EX_EVENT_RETHROW
, arg
, tempflag
, from_tty
);
485 /* Implement the 'make_value' method for the $_exception
488 static struct value
*
489 compute_exception (struct gdbarch
*argc
, struct internalvar
*var
, void *ignore
)
491 struct value
*arg0
, *arg1
;
492 struct type
*obj_type
;
494 fetch_probe_arguments (&arg0
, &arg1
);
496 /* ARG0 is a pointer to the exception object. ARG1 is a pointer to
497 the std::type_info for the exception. Now we find the type from
498 the type_info and cast the result. */
499 obj_type
= cplus_type_from_type_info (arg1
);
500 return value_ind (value_cast (make_pointer_type (obj_type
, NULL
), arg0
));
503 /* Implementation of the '$_exception' variable. */
505 static const struct internalvar_funcs exception_funcs
=
513 void _initialize_break_catch_throw ();
515 _initialize_break_catch_throw ()
517 /* Add catch and tcatch sub-commands. */
518 add_catch_command ("catch", _("\
519 Catch an exception, when caught."),
524 add_catch_command ("throw", _("\
525 Catch an exception, when thrown."),
530 add_catch_command ("rethrow", _("\
531 Catch an exception, when rethrown."),
532 catch_rethrow_command
,
537 create_internalvar_type_lazy ("_exception", &exception_funcs
, NULL
);