Convert dprintf to vtable ops
[binutils-gdb.git] / gdb / break-catch-throw.c
1 /* Everything about catch/throw catchpoints, for GDB.
2
3 Copyright (C) 1986-2022 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. */
67
68 struct exception_catchpoint : public base_breakpoint
69 {
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;
78
79 /* FIXME this is temporary - until ordinary breakpoints have been
80 converted. */
81 int resources_needed (const struct bp_location *) override
82 {
83 return 1;
84 }
85
86 /* The kind of exception catchpoint. */
87
88 enum exception_event_kind kind;
89
90 /* If not empty, a string holding the source form of the regular
91 expression to match against. */
92
93 std::string exception_rx;
94
95 /* If non-NULL, a compiled regular expression which is used to
96 determine which exceptions to stop on. */
97
98 std::unique_ptr<compiled_regex> pattern;
99 };
100
101 /* See breakpoint.h. */
102
103 bool
104 is_exception_catchpoint (breakpoint *bp)
105 {
106 return dynamic_cast<exception_catchpoint *> (bp) != nullptr;
107 }
108
109 \f
110
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. */
114
115 static void
116 fetch_probe_arguments (struct value **arg0, struct value **arg1)
117 {
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;
121 unsigned n_args;
122
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?)"));
126
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"));
132
133 n_args = pc_probe.prob->get_argument_count (get_frame_arch (frame));
134 if (n_args < 2)
135 error (_("C++ exception catchpoint has too few arguments"));
136
137 if (arg0 != NULL)
138 *arg0 = pc_probe.prob->evaluate_argument (0, frame);
139 *arg1 = pc_probe.prob->evaluate_argument (1, frame);
140
141 if ((arg0 != NULL && *arg0 == NULL) || *arg1 == NULL)
142 error (_("error computing probe argument at c++ exception catchpoint"));
143 }
144
145 \f
146
147 /* Implement the 'check_status' method. */
148
149 void
150 exception_catchpoint::check_status (struct bpstat *bs)
151 {
152 struct exception_catchpoint *self
153 = (struct exception_catchpoint *) bs->breakpoint_at;
154 std::string type_name;
155
156 this->breakpoint::check_status (bs);
157 if (bs->stop == 0)
158 return;
159
160 if (self->pattern == NULL)
161 return;
162
163 const char *name = nullptr;
164 gdb::unique_xmalloc_ptr<char> canon;
165 try
166 {
167 struct value *typeinfo_arg;
168
169 fetch_probe_arguments (NULL, &typeinfo_arg);
170 type_name = cplus_typename_from_type_info (typeinfo_arg);
171
172 canon = cp_canonicalize_string (type_name.c_str ());
173 name = (canon != nullptr
174 ? canon.get ()
175 : type_name.c_str ());
176 }
177 catch (const gdb_exception_error &e)
178 {
179 exception_print (gdb_stderr, e);
180 }
181
182 if (name != nullptr)
183 {
184 if (self->pattern->exec (name, 0, NULL, 0) != 0)
185 bs->stop = 0;
186 }
187 }
188
189 /* Implement the 're_set' method. */
190
191 void
192 exception_catchpoint::re_set ()
193 {
194 std::vector<symtab_and_line> sals;
195 struct program_space *filter_pspace = current_program_space;
196
197 /* We first try to use the probe interface. */
198 try
199 {
200 event_location_up location
201 = new_probe_location (exception_functions[kind].probe);
202 sals = parse_probes (location.get (), filter_pspace, NULL);
203 }
204 catch (const gdb_exception_error &e)
205 {
206 /* Using the probe interface failed. Let's fallback to the normal
207 catchpoint mode. */
208 try
209 {
210 struct explicit_location explicit_loc;
211
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);
217 }
218 catch (const gdb_exception_error &ex)
219 {
220 /* NOT_FOUND_ERROR just means the breakpoint will be
221 pending, so let it through. */
222 if (ex.error != NOT_FOUND_ERROR)
223 throw;
224 }
225 }
226
227 update_breakpoint_locations (this, filter_pspace, sals, {});
228 }
229
230 enum print_stop_action
231 exception_catchpoint::print_it (bpstat *bs)
232 {
233 struct ui_out *uiout = current_uiout;
234 int bp_temp;
235
236 annotate_catchpoint (number);
237 maybe_print_thread_hit_breakpoint (uiout);
238
239 bp_temp = disposition == disp_del;
240 uiout->text (bp_temp ? "Temporary catchpoint "
241 : "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 ())
247 {
248 uiout->field_string ("reason",
249 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
250 uiout->field_string ("disp", bpdisp_text (disposition));
251 }
252 return PRINT_SRC_AND_LOC;
253 }
254
255 bool
256 exception_catchpoint::print_one (struct bp_location **last_loc)
257 {
258 struct value_print_options opts;
259 struct ui_out *uiout = current_uiout;
260
261 get_user_print_options (&opts);
262
263 if (opts.addressprint)
264 uiout->field_skip ("addr");
265 annotate_field (5);
266
267 switch (kind)
268 {
269 case EX_EVENT_THROW:
270 uiout->field_string ("what", "exception throw");
271 if (uiout->is_mi_like_p ())
272 uiout->field_string ("catch-type", "throw");
273 break;
274
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");
279 break;
280
281 case EX_EVENT_CATCH:
282 uiout->field_string ("what", "exception catch");
283 if (uiout->is_mi_like_p ())
284 uiout->field_string ("catch-type", "catch");
285 break;
286 }
287
288 return true;
289 }
290
291 /* Implement the 'print_one_detail' method. */
292
293 void
294 exception_catchpoint::print_one_detail (struct ui_out *uiout) const
295 {
296 if (!exception_rx.empty ())
297 {
298 uiout->text (_("\tmatching: "));
299 uiout->field_string ("regexp", exception_rx);
300 uiout->text ("\n");
301 }
302 }
303
304 void
305 exception_catchpoint::print_mention ()
306 {
307 struct ui_out *uiout = current_uiout;
308 int bp_temp;
309
310 bp_temp = disposition == disp_del;
311 uiout->message ("%s %d %s",
312 (bp_temp ? _("Temporary catchpoint ") : _("Catchpoint")),
313 number,
314 (kind == EX_EVENT_THROW
315 ? _("(throw)") : (kind == EX_EVENT_CATCH
316 ? _("(catch)") : _("(rethrow)"))));
317 }
318
319 /* Implement the "print_recreate" method for throw and catch
320 catchpoints. */
321
322 void
323 exception_catchpoint::print_recreate (struct ui_file *fp)
324 {
325 int bp_temp;
326
327 bp_temp = disposition == disp_del;
328 gdb_printf (fp, bp_temp ? "tcatch " : "catch ");
329 switch (kind)
330 {
331 case EX_EVENT_THROW:
332 gdb_printf (fp, "throw");
333 break;
334 case EX_EVENT_CATCH:
335 gdb_printf (fp, "catch");
336 break;
337 case EX_EVENT_RETHROW:
338 gdb_printf (fp, "rethrow");
339 break;
340 }
341 print_recreate_thread (this, fp);
342 }
343
344 /* Implement the "allocate_location" method for throw and catch
345 catchpoints. */
346
347 bp_location *
348 exception_catchpoint::allocate_location ()
349 {
350 return new bp_location (this, bp_loc_software_breakpoint);
351 }
352
353 static void
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)
357 {
358 std::unique_ptr<compiled_regex> pattern;
359
360 if (!except_rx.empty ())
361 {
362 pattern.reset (new compiled_regex (except_rx.c_str (), REG_NOSUB,
363 _("invalid type-matching regexp")));
364 }
365
366 std::unique_ptr<exception_catchpoint> cp (new exception_catchpoint ());
367
368 init_catchpoint (cp.get (), get_current_arch (), tempflag, cond_string,
369 &vtable_breakpoint_ops);
370 cp->kind = ex_event;
371 cp->exception_rx = std::move (except_rx);
372 cp->pattern = std::move (pattern);
373
374 cp->re_set ();
375
376 install_breakpoint (0, std::move (cp), 1);
377 }
378
379 /* Look for an "if" token in *STRING. The "if" token must be preceded
380 by whitespace.
381
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,
384 this returns NULL.
385
386 STRING is updated to point to the "if" token, if it exists, or to
387 the end of the string. */
388
389 static std::string
390 extract_exception_regexp (const char **string)
391 {
392 const char *start;
393 const char *last, *last_space;
394
395 start = skip_spaces (*string);
396
397 last = start;
398 last_space = start;
399 while (*last != '\0')
400 {
401 const char *if_token = last;
402
403 /* Check for the "if". */
404 if (check_for_argument (&if_token, "if", 2))
405 break;
406
407 /* No "if" token here. Skip to the next word start. */
408 last_space = skip_to_space (last);
409 last = skip_spaces (last_space);
410 }
411
412 *string = last;
413 if (last_space > start)
414 return std::string (start, last_space - start);
415 return std::string ();
416 }
417
418 /* See breakpoint.h. */
419
420 void
421 catch_exception_event (enum exception_event_kind ex_event,
422 const char *arg, bool tempflag, int from_tty)
423 {
424 const char *cond_string = NULL;
425
426 if (!arg)
427 arg = "";
428 arg = skip_spaces (arg);
429
430 std::string except_rx = extract_exception_regexp (&arg);
431
432 cond_string = ep_parse_optional_if_clause (&arg);
433
434 if ((*arg != '\0') && !isspace (*arg))
435 error (_("Junk at end of arguments."));
436
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"));
441
442 handle_gnu_v3_exceptions (tempflag, std::move (except_rx), cond_string,
443 ex_event, from_tty);
444 }
445
446 /* Implementation of "catch catch" command. */
447
448 static void
449 catch_catch_command (const char *arg, int from_tty,
450 struct cmd_list_element *command)
451 {
452 bool tempflag = command->context () == CATCH_TEMPORARY;
453
454 catch_exception_event (EX_EVENT_CATCH, arg, tempflag, from_tty);
455 }
456
457 /* Implementation of "catch throw" command. */
458
459 static void
460 catch_throw_command (const char *arg, int from_tty,
461 struct cmd_list_element *command)
462 {
463 bool tempflag = command->context () == CATCH_TEMPORARY;
464
465 catch_exception_event (EX_EVENT_THROW, arg, tempflag, from_tty);
466 }
467
468 /* Implementation of "catch rethrow" command. */
469
470 static void
471 catch_rethrow_command (const char *arg, int from_tty,
472 struct cmd_list_element *command)
473 {
474 bool tempflag = command->context () == CATCH_TEMPORARY;
475
476 catch_exception_event (EX_EVENT_RETHROW, arg, tempflag, from_tty);
477 }
478
479 \f
480
481 /* Implement the 'make_value' method for the $_exception
482 internalvar. */
483
484 static struct value *
485 compute_exception (struct gdbarch *argc, struct internalvar *var, void *ignore)
486 {
487 struct value *arg0, *arg1;
488 struct type *obj_type;
489
490 fetch_probe_arguments (&arg0, &arg1);
491
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));
497 }
498
499 /* Implementation of the '$_exception' variable. */
500
501 static const struct internalvar_funcs exception_funcs =
502 {
503 compute_exception,
504 NULL,
505 };
506
507 \f
508
509 void _initialize_break_catch_throw ();
510 void
511 _initialize_break_catch_throw ()
512 {
513 /* Add catch and tcatch sub-commands. */
514 add_catch_command ("catch", _("\
515 Catch an exception, when caught."),
516 catch_catch_command,
517 NULL,
518 CATCH_PERMANENT,
519 CATCH_TEMPORARY);
520 add_catch_command ("throw", _("\
521 Catch an exception, when thrown."),
522 catch_throw_command,
523 NULL,
524 CATCH_PERMANENT,
525 CATCH_TEMPORARY);
526 add_catch_command ("rethrow", _("\
527 Catch an exception, when rethrown."),
528 catch_rethrow_command,
529 NULL,
530 CATCH_PERMANENT,
531 CATCH_TEMPORARY);
532
533 create_internalvar_type_lazy ("_exception", &exception_funcs, NULL);
534 }