Remove path name from test case
[binutils-gdb.git] / gdb / break-catch-throw.c
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
45 struct exception_names
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. */
59 static 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
70 struct exception_catchpoint : public code_breakpoint
71 {
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")))
83 {
84 pspace = current_program_space;
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
99 enum exception_event_kind kind;
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
114 bool
115 is_exception_catchpoint (breakpoint *bp)
116 {
117 return dynamic_cast<exception_catchpoint *> (bp) != nullptr;
118 }
119
120 \f
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
126 static void
127 fetch_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 \f
157
158 /* Implement the 'check_status' method. */
159
160 void
161 exception_catchpoint::check_status (struct bpstat *bs)
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 {
188 exception_print (gdb_stderr, e);
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
200 void
201 exception_catchpoint::re_set ()
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 {
209 location_spec_up locspec
210 = new_probe_location_spec (exception_functions[kind].probe);
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 {
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);
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
236 enum print_stop_action
237 exception_catchpoint::print_it (const bpstat *bs) const
238 {
239 struct ui_out *uiout = current_uiout;
240 int bp_temp;
241
242 annotate_catchpoint (number);
243 maybe_print_thread_hit_breakpoint (uiout);
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",
255 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
256 uiout->field_string ("disp", bpdisp_text (disposition));
257 }
258 return PRINT_SRC_AND_LOC;
259 }
260
261 bool
262 exception_catchpoint::print_one (const bp_location **last_loc) const
263 {
264 struct value_print_options opts;
265 struct ui_out *uiout = current_uiout;
266
267 get_user_print_options (&opts);
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
299 void
300 exception_catchpoint::print_one_detail (struct ui_out *uiout) const
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
310 void
311 exception_catchpoint::print_mention () const
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,
320 (kind == EX_EVENT_THROW
321 ? _("(throw)") : (kind == EX_EVENT_CATCH
322 ? _("(catch)") : _("(rethrow)"))));
323 }
324
325 /* Implement the "print_recreate" method for throw and catch
326 catchpoints. */
327
328 void
329 exception_catchpoint::print_recreate (struct ui_file *fp) const
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 }
347 print_recreate_thread (fp);
348 }
349
350 /* Implement the "allocate_location" method for throw and catch
351 catchpoints. */
352
353 bp_location *
354 exception_catchpoint::allocate_location ()
355 {
356 return new bp_location (this, bp_loc_software_breakpoint);
357 }
358
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)
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
383 static std::string
384 extract_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
414 void
415 catch_exception_event (enum exception_event_kind ex_event,
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
442 static void
443 catch_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
453 static void
454 catch_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
464 static void
465 catch_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 \f
474
475 /* Implement the 'make_value' method for the $_exception
476 internalvar. */
477
478 static struct value *
479 compute_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
495 static const struct internalvar_funcs exception_funcs =
496 {
497 compute_exception,
498 NULL,
499 };
500
501 \f
502
503 void _initialize_break_catch_throw ();
504 void
505 _initialize_break_catch_throw ()
506 {
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);
526
527 create_internalvar_type_lazy ("_exception", &exception_funcs, NULL);
528 }