GDB (xrefs)
Loading...
Searching...
No Matches
break-catch-throw.c
Go to the documentation of this file.
1/* Everything about catch/throw catchpoints, for GDB.
2
3 Copyright (C) 1986-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
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"
40
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. */
44
46{
47 /* The name of the probe point to try, in the form accepted by
48 'parse_probes'. */
49
50 const char *probe;
51
52 /* The name of the corresponding function. */
53
54 const char *function;
55};
56
57/* Names of the probe points and functions on which to break. This is
58 indexed by exception_event_kind. */
59static const struct exception_names exception_functions[] =
60{
61 { "-probe-stap libstdcxx:throw", "__cxa_throw" },
62 { "-probe-stap libstdcxx:rethrow", "__cxa_rethrow" },
63 { "-probe-stap libstdcxx:catch", "__cxa_begin_catch" }
64};
65
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. */
69
71{
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")))
83 {
85 re_set ();
86 }
87
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;
96
97 /* The kind of exception catchpoint. */
98
100
101 /* If not empty, a string holding the source form of the regular
102 expression to match against. */
103
104 std::string exception_rx;
105
106 /* If non-NULL, a compiled regular expression which is used to
107 determine which exceptions to stop on. */
108
109 std::unique_ptr<compiled_regex> pattern;
110};
111
112/* See breakpoint.h. */
113
114bool
116{
117 return dynamic_cast<exception_catchpoint *> (bp) != nullptr;
118}
119
120
121
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. */
125
126static void
127fetch_probe_arguments (struct value **arg0, struct value **arg1)
128{
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;
133
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?)"));
137
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"));
143
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"));
147
148 if (arg0 != NULL)
149 *arg0 = pc_probe.prob->evaluate_argument (0, frame);
150 *arg1 = pc_probe.prob->evaluate_argument (1, frame);
151
152 if ((arg0 != NULL && *arg0 == NULL) || *arg1 == NULL)
153 error (_("error computing probe argument at c++ exception catchpoint"));
154}
155
156
157
158/* Implement the 'check_status' method. */
159
160void
162{
163 std::string type_name;
164
165 this->breakpoint::check_status (bs);
166 if (!bs->stop)
167 return;
168
169 if (this->pattern == NULL)
170 return;
171
172 const char *name = nullptr;
173 gdb::unique_xmalloc_ptr<char> canon;
174 try
175 {
176 struct value *typeinfo_arg;
177
178 fetch_probe_arguments (NULL, &typeinfo_arg);
179 type_name = cplus_typename_from_type_info (typeinfo_arg);
180
181 canon = cp_canonicalize_string (type_name.c_str ());
182 name = (canon != nullptr
183 ? canon.get ()
184 : type_name.c_str ());
185 }
186 catch (const gdb_exception_error &e)
187 {
189 }
190
191 if (name != nullptr)
192 {
193 if (this->pattern->exec (name, 0, NULL, 0) != 0)
194 bs->stop = false;
195 }
196}
197
198/* Implement the 're_set' method. */
199
200void
202{
203 std::vector<symtab_and_line> sals;
204 struct program_space *filter_pspace = current_program_space;
205
206 /* We first try to use the probe interface. */
207 try
208 {
211 sals = parse_probes (locspec.get (), filter_pspace, NULL);
212 }
213 catch (const gdb_exception_error &e)
214 {
215 /* Using the probe interface failed. Let's fallback to the normal
216 catchpoint mode. */
217 try
218 {
221 (exception_functions[kind].function));
222 sals = this->decode_location_spec (locspec.get (), filter_pspace);
223 }
224 catch (const gdb_exception_error &ex)
225 {
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;
230 }
231 }
232
233 update_breakpoint_locations (this, filter_pspace, sals, {});
234}
235
238{
239 struct ui_out *uiout = current_uiout;
240 int bp_temp;
241
244
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 ())
253 {
254 uiout->field_string ("reason",
256 uiout->field_string ("disp", bpdisp_text (disposition));
257 }
258 return PRINT_SRC_AND_LOC;
259}
260
261bool
263{
264 struct value_print_options opts;
265 struct ui_out *uiout = current_uiout;
266
268
269 if (opts.addressprint)
270 uiout->field_skip ("addr");
271 annotate_field (5);
272
273 switch (kind)
274 {
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;
280
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;
286
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;
292 }
293
294 return true;
295}
296
297/* Implement the 'print_one_detail' method. */
298
299void
301{
302 if (!exception_rx.empty ())
303 {
304 uiout->text (_("\tmatching: "));
305 uiout->field_string ("regexp", exception_rx);
306 uiout->text ("\n");
307 }
308}
309
310void
312{
313 struct ui_out *uiout = current_uiout;
314 int bp_temp;
315
316 bp_temp = disposition == disp_del;
317 uiout->message ("%s %d %s",
318 (bp_temp ? _("Temporary catchpoint ") : _("Catchpoint")),
319 number,
321 ? _("(throw)") : (kind == EX_EVENT_CATCH
322 ? _("(catch)") : _("(rethrow)"))));
323}
324
325/* Implement the "print_recreate" method for throw and catch
326 catchpoints. */
327
328void
330{
331 int bp_temp;
332
333 bp_temp = disposition == disp_del;
334 gdb_printf (fp, bp_temp ? "tcatch " : "catch ");
335 switch (kind)
336 {
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;
346 }
348}
349
350/* Implement the "allocate_location" method for throw and catch
351 catchpoints. */
352
358
359static void
360handle_gnu_v3_exceptions (int tempflag, std::string &&except_rx,
361 const char *cond_string,
362 enum exception_event_kind ex_event, int from_tty)
363{
364 struct gdbarch *gdbarch = get_current_arch ();
365
366 std::unique_ptr<exception_catchpoint> cp
367 (new exception_catchpoint (gdbarch, tempflag, cond_string,
368 ex_event, std::move (except_rx)));
369
370 install_breakpoint (0, std::move (cp), 1);
371}
372
373/* Look for an "if" token in *STRING. The "if" token must be preceded
374 by whitespace.
375
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.
379
380 STRING is updated to point to the "if" token, if it exists, or to
381 the end of the string. */
382
383static std::string
384extract_exception_regexp (const char **string)
385{
386 const char *start;
387 const char *last, *last_space;
388
389 start = skip_spaces (*string);
390
391 last = start;
392 last_space = start;
393 while (*last != '\0')
394 {
395 const char *if_token = last;
396
397 /* Check for the "if". */
398 if (check_for_argument (&if_token, "if", 2))
399 break;
400
401 /* No "if" token here. Skip to the next word start. */
402 last_space = skip_to_space (last);
403 last = skip_spaces (last_space);
404 }
405
406 *string = last;
407 if (last_space > start)
408 return std::string (start, last_space - start);
409 return std::string ();
410}
411
412/* See breakpoint.h. */
413
414void
416 const char *arg, bool tempflag, int from_tty)
417{
418 const char *cond_string = NULL;
419
420 if (!arg)
421 arg = "";
422 arg = skip_spaces (arg);
423
424 std::string except_rx = extract_exception_regexp (&arg);
425
426 cond_string = ep_parse_optional_if_clause (&arg);
427
428 if ((*arg != '\0') && !isspace (*arg))
429 error (_("Junk at end of arguments."));
430
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"));
435
436 handle_gnu_v3_exceptions (tempflag, std::move (except_rx), cond_string,
437 ex_event, from_tty);
438}
439
440/* Implementation of "catch catch" command. */
441
442static void
443catch_catch_command (const char *arg, int from_tty,
444 struct cmd_list_element *command)
445{
446 bool tempflag = command->context () == CATCH_TEMPORARY;
447
448 catch_exception_event (EX_EVENT_CATCH, arg, tempflag, from_tty);
449}
450
451/* Implementation of "catch throw" command. */
452
453static void
454catch_throw_command (const char *arg, int from_tty,
455 struct cmd_list_element *command)
456{
457 bool tempflag = command->context () == CATCH_TEMPORARY;
458
459 catch_exception_event (EX_EVENT_THROW, arg, tempflag, from_tty);
460}
461
462/* Implementation of "catch rethrow" command. */
463
464static void
465catch_rethrow_command (const char *arg, int from_tty,
466 struct cmd_list_element *command)
467{
468 bool tempflag = command->context () == CATCH_TEMPORARY;
469
470 catch_exception_event (EX_EVENT_RETHROW, arg, tempflag, from_tty);
471}
472
473
474
475/* Implement the 'make_value' method for the $_exception
476 internalvar. */
477
478static struct value *
479compute_exception (struct gdbarch *argc, struct internalvar *var, void *ignore)
480{
481 struct value *arg0, *arg1;
482 struct type *obj_type;
483
484 fetch_probe_arguments (&arg0, &arg1);
485
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));
491}
492
493/* Implementation of the '$_exception' variable. */
494
496{
498 NULL,
499};
500
501
502
504void
506{
507 /* Add catch and tcatch sub-commands. */
508 add_catch_command ("catch", _("\
509Catch an exception, when caught."),
511 NULL,
514 add_catch_command ("throw", _("\
515Catch an exception, when thrown."),
517 NULL,
520 add_catch_command ("rethrow", _("\
521Catch an exception, when rethrown."),
523 NULL,
526
527 create_internalvar_type_lazy ("_exception", &exception_funcs, NULL);
528}
const char *const name
void annotate_field(int num)
Definition annotate.c:172
void annotate_catchpoint(int num)
Definition annotate.c:82
struct gdbarch * get_current_arch(void)
Definition arch-utils.c:846
const char * ep_parse_optional_if_clause(const char **arg)
static void handle_gnu_v3_exceptions(int tempflag, std::string &&except_rx, const char *cond_string, enum exception_event_kind ex_event, int from_tty)
static void catch_throw_command(const char *arg, int from_tty, struct cmd_list_element *command)
static void catch_catch_command(const char *arg, int from_tty, struct cmd_list_element *command)
static std::string extract_exception_regexp(const char **string)
static struct value * compute_exception(struct gdbarch *argc, struct internalvar *var, void *ignore)
static void fetch_probe_arguments(struct value **arg0, struct value **arg1)
static const struct exception_names exception_functions[]
void catch_exception_event(enum exception_event_kind ex_event, const char *arg, bool tempflag, int from_tty)
bool is_exception_catchpoint(breakpoint *bp)
static void catch_rethrow_command(const char *arg, int from_tty, struct cmd_list_element *command)
void _initialize_break_catch_throw()
static const struct internalvar_funcs exception_funcs
void add_catch_command(const char *name, const char *docstring, cmd_func_ftype *func, completer_ftype *completer, void *user_data_catch, void *user_data_tcatch)
const char * bpdisp_text(enum bpdisp disp)
Definition breakpoint.c:505
breakpoint * install_breakpoint(int internal, std::unique_ptr< breakpoint > &&arg, int update_gll)
void print_num_locno(const bpstat *bs, struct ui_out *uiout)
void update_breakpoint_locations(code_breakpoint *b, struct program_space *filter_pspace, gdb::array_view< const symtab_and_line > sals, gdb::array_view< const symtab_and_line > sals_end)
void maybe_print_thread_hit_breakpoint(struct ui_out *uiout)
#define CATCH_PERMANENT
@ disp_del
Definition breakpoint.h:237
@ bp_catchpoint
Definition breakpoint.h:182
exception_event_kind
Definition breakpoint.h:55
@ EX_EVENT_CATCH
Definition breakpoint.h:58
@ EX_EVENT_THROW
Definition breakpoint.h:56
@ EX_EVENT_RETHROW
Definition breakpoint.h:57
#define CATCH_TEMPORARY
print_stop_action
Definition breakpoint.h:542
@ PRINT_SRC_AND_LOC
Definition breakpoint.h:548
@ bp_loc_software_breakpoint
Definition breakpoint.h:316
Definition probe.h:115
virtual struct value * evaluate_argument(unsigned n, frame_info_ptr frame)=0
const std::string & get_name() const
Definition probe.h:199
virtual unsigned get_argument_count(struct gdbarch *gdbarch)=0
const std::string & get_provider() const
Definition probe.h:205
void field_string(const char *fldname, const char *string, const ui_file_style &style=ui_file_style())
Definition ui-out.c:511
void field_skip(const char *fldname)
Definition ui-out.c:499
void text(const char *string)
Definition ui-out.c:566
bool is_mi_like_p() const
Definition ui-out.c:810
void message(const char *format,...) ATTRIBUTE_PRINTF(2
Definition ui-out.c:774
int check_for_argument(const char **str, const char *arg, int arg_len)
Definition cli-utils.c:421
std::string cplus_typename_from_type_info(struct value *value)
Definition cp-abi.c:216
struct type * cplus_type_from_type_info(struct value *value)
Definition cp-abi.c:206
gdb::unique_xmalloc_ptr< char > cp_canonicalize_string(const char *string)
Definition cp-support.c:627
void exception_print(struct ui_file *file, const struct gdb_exception &e)
Definition exceptions.c:106
CORE_ADDR get_frame_pc(frame_info_ptr frame)
Definition frame.c:2712
struct gdbarch * get_frame_arch(frame_info_ptr this_frame)
Definition frame.c:3027
frame_info_ptr get_selected_frame(const char *message)
Definition frame.c:1888
struct type * make_pointer_type(struct type *type, struct type **typeptr)
Definition gdbtypes.c:369
location_spec_up new_probe_location_spec(std::string &&probe)
Definition location.c:249
std::unique_ptr< location_spec > location_spec_up
Definition location.h:71
static location_spec_up new_explicit_location_spec_function(const char *function_name)
Definition location.h:287
const char * async_reason_lookup(enum async_reply_reason reason)
Definition mi-common.c:50
@ EXEC_ASYNC_BREAKPOINT_HIT
Definition mi-common.h:27
Definition aarch64.h:67
std::vector< symtab_and_line > parse_probes(const location_spec *locspec, struct program_space *search_pspace, struct linespec_result *canonical)
Definition probe.c:117
struct bound_probe find_probe_by_pc(CORE_ADDR pc)
Definition probe.c:243
struct program_space * current_program_space
Definition progspace.c:40
probe * prob
Definition probe.h:255
bool stop
location_spec_up locspec
Definition breakpoint.h:832
virtual void check_status(struct bpstat *bs)
Definition breakpoint.h:737
void print_recreate_thread(struct ui_file *fp) const
program_space * pspace
Definition breakpoint.h:829
bpdisp disposition
Definition breakpoint.h:802
void * context() const
Definition cli-decode.h:109
virtual std::vector< symtab_and_line > decode_location_spec(location_spec *locspec, struct program_space *search_pspace)
bool print_one(const bp_location **) const override
void print_one_detail(struct ui_out *) const override
void print_recreate(struct ui_file *fp) const override
exception_catchpoint(struct gdbarch *gdbarch, bool temp, const char *cond_string_, enum exception_event_kind kind_, std::string &&except_rx)
enum exception_event_kind kind
enum print_stop_action print_it(const bpstat *bs) const override
std::unique_ptr< compiled_regex > pattern
void print_mention() const override
struct bp_location * allocate_location() override
void check_status(struct bpstat *bs) override
const char * function
Definition value.h:130
#define current_uiout
Definition ui-out.h:40
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
#define gdb_stderr
Definition utils.h:187
struct value * value_cast(struct type *type, struct value *arg2)
Definition valops.c:403
struct value * value_ind(struct value *arg1)
Definition valops.c:1630
void get_user_print_options(struct value_print_options *opts)
Definition valprint.c:135
struct internalvar * create_internalvar_type_lazy(const char *name, const struct internalvar_funcs *funcs, void *data)
Definition value.c:1966
#define nullptr
Definition x86-cpuid.h:28