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