"catch catch/throw/rethrow", breakpoint -> catchpoint
[deliverable/binutils-gdb.git] / gdb / break-catch-throw.c
CommitLineData
916703c0
TT
1/* Everything about catch/throw catchpoints, for GDB.
2
42a4f53d 3 Copyright (C) 1986-2019 Free Software Foundation, Inc.
916703c0
TT
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"
d55e5aa6 21#include "arch-utils.h"
4de283e4 22#include <ctype.h>
d55e5aa6 23#include "breakpoint.h"
4de283e4
TT
24#include "gdbcmd.h"
25#include "inferior.h"
26#include "annotate.h"
27#include "valprint.h"
916703c0
TT
28#include "cli/cli-utils.h"
29#include "completer.h"
d55e5aa6 30#include "gdb_obstack.h"
d55e5aa6 31#include "mi/mi-common.h"
4de283e4 32#include "linespec.h"
d55e5aa6 33#include "probe.h"
4de283e4
TT
34#include "objfiles.h"
35#include "cp-abi.h"
36#include "gdb_regex.h"
37#include "cp-support.h"
38#include "location.h"
916703c0 39
fc4746a2
TT
40/* Each spot where we may place an exception-related catchpoint has
41 two names: the SDT probe point and the function name. This
42 structure holds both. */
43
44struct exception_names
45{
46 /* The name of the probe point to try, in the form accepted by
47 'parse_probes'. */
48
49 const char *probe;
50
51 /* The name of the corresponding function. */
52
53 const char *function;
54};
55
56/* Names of the probe points and functions on which to break. This is
57 indexed by exception_event_kind. */
58static const struct exception_names exception_functions[] =
15a73f56 59{
fc4746a2
TT
60 { "-probe-stap libstdcxx:throw", "__cxa_throw" },
61 { "-probe-stap libstdcxx:rethrow", "__cxa_rethrow" },
62 { "-probe-stap libstdcxx:catch", "__cxa_begin_catch" }
15a73f56
TT
63};
64
65static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
66
67/* The type of an exception catchpoint. */
68
c1fc2657 69struct exception_catchpoint : public breakpoint
15a73f56 70{
15a73f56
TT
71 /* The kind of exception catchpoint. */
72
73 enum exception_event_kind kind;
cc16e6c9 74
4fa8aeac
TT
75 /* If not empty, a string holding the source form of the regular
76 expression to match against. */
cc16e6c9 77
4fa8aeac 78 std::string exception_rx;
cc16e6c9 79
2d7cc5c7
PA
80 /* If non-NULL, a compiled regular expression which is used to
81 determine which exceptions to stop on. */
cc16e6c9 82
2d7cc5c7 83 std::unique_ptr<compiled_regex> pattern;
15a73f56
TT
84};
85
cc16e6c9
TT
86\f
87
88/* A helper function that fetches exception probe arguments. This
89 fills in *ARG0 (if non-NULL) and *ARG1 (which must be non-NULL).
90 It will throw an exception on any kind of failure. */
91
92static void
93fetch_probe_arguments (struct value **arg0, struct value **arg1)
94{
95 struct frame_info *frame = get_selected_frame (_("No frame selected"));
96 CORE_ADDR pc = get_frame_pc (frame);
729662a5 97 struct bound_probe pc_probe;
cc16e6c9
TT
98 unsigned n_args;
99
100 pc_probe = find_probe_by_pc (pc);
935676c9
SDJ
101 if (pc_probe.prob == NULL
102 || pc_probe.prob->get_provider () != "libstdcxx"
103 || (pc_probe.prob->get_name () != "catch"
104 && pc_probe.prob->get_name () != "throw"
105 && pc_probe.prob->get_name () != "rethrow"))
cc16e6c9
TT
106 error (_("not stopped at a C++ exception catchpoint"));
107
935676c9 108 n_args = pc_probe.prob->get_argument_count (frame);
cc16e6c9
TT
109 if (n_args < 2)
110 error (_("C++ exception catchpoint has too few arguments"));
111
112 if (arg0 != NULL)
935676c9
SDJ
113 *arg0 = pc_probe.prob->evaluate_argument (0, frame);
114 *arg1 = pc_probe.prob->evaluate_argument (1, frame);
cc16e6c9
TT
115
116 if ((arg0 != NULL && *arg0 == NULL) || *arg1 == NULL)
117 error (_("error computing probe argument at c++ exception catchpoint"));
118}
119
120\f
121
916703c0
TT
122/* A helper function that returns a value indicating the kind of the
123 exception catchpoint B. */
124
125static enum exception_event_kind
126classify_exception_breakpoint (struct breakpoint *b)
127{
15a73f56
TT
128 struct exception_catchpoint *cp = (struct exception_catchpoint *) b;
129
130 return cp->kind;
131}
132
cc16e6c9
TT
133/* Implement the 'check_status' method. */
134
135static void
136check_status_exception_catchpoint (struct bpstats *bs)
137{
138 struct exception_catchpoint *self
139 = (struct exception_catchpoint *) bs->breakpoint_at;
2f408ecb 140 std::string type_name;
cc16e6c9
TT
141
142 bkpt_breakpoint_ops.check_status (bs);
143 if (bs->stop == 0)
144 return;
145
146 if (self->pattern == NULL)
147 return;
148
a70b8144 149 try
cc16e6c9
TT
150 {
151 struct value *typeinfo_arg;
2f408ecb 152 std::string canon;
cc16e6c9
TT
153
154 fetch_probe_arguments (NULL, &typeinfo_arg);
fe978cb0 155 type_name = cplus_typename_from_type_info (typeinfo_arg);
cc16e6c9 156
2f408ecb
PA
157 canon = cp_canonicalize_string (type_name.c_str ());
158 if (!canon.empty ())
159 std::swap (type_name, canon);
cc16e6c9 160 }
230d2906 161 catch (const gdb_exception_error &e)
492d29ea
PA
162 {
163 exception_print (gdb_stderr, e);
164 }
cc16e6c9 165
2f408ecb 166 if (!type_name.empty ())
7556d4a4 167 {
2d7cc5c7 168 if (self->pattern->exec (type_name.c_str (), 0, NULL, 0) != 0)
7556d4a4 169 bs->stop = 0;
7556d4a4 170 }
cc16e6c9
TT
171}
172
15a73f56
TT
173/* Implement the 're_set' method. */
174
175static void
176re_set_exception_catchpoint (struct breakpoint *self)
177{
6c5b2ebe 178 std::vector<symtab_and_line> sals;
15a73f56 179 enum exception_event_kind kind = classify_exception_breakpoint (self);
c2f4122d 180 struct program_space *filter_pspace = current_program_space;
15a73f56 181
1bff71c3 182 /* We first try to use the probe interface. */
a70b8144 183 try
15a73f56 184 {
ffc2605c 185 event_location_up location
5b56227b 186 = new_probe_location (exception_functions[kind].probe);
ffc2605c 187 sals = parse_probes (location.get (), filter_pspace, NULL);
1bff71c3 188 }
230d2906 189 catch (const gdb_exception_error &e)
1bff71c3 190 {
1bff71c3
SDJ
191 /* Using the probe interface failed. Let's fallback to the normal
192 catchpoint mode. */
a70b8144 193 try
fc4746a2 194 {
67994074 195 struct explicit_location explicit_loc;
1bff71c3 196
67994074
KS
197 initialize_explicit_location (&explicit_loc);
198 explicit_loc.function_name
00e52e53 199 = ASTRDUP (exception_functions[kind].function);
ffc2605c 200 event_location_up location = new_explicit_location (&explicit_loc);
6c5b2ebe
PA
201 sals = self->ops->decode_location (self, location.get (),
202 filter_pspace);
fc4746a2 203 }
230d2906 204 catch (const gdb_exception_error &ex)
7556d4a4
PA
205 {
206 /* NOT_FOUND_ERROR just means the breakpoint will be
207 pending, so let it through. */
208 if (ex.error != NOT_FOUND_ERROR)
eedc3f4f 209 throw;
7556d4a4 210 }
15a73f56 211 }
15a73f56 212
6c5b2ebe 213 update_breakpoint_locations (self, filter_pspace, sals, {});
916703c0
TT
214}
215
216static enum print_stop_action
217print_it_exception_catchpoint (bpstat bs)
218{
219 struct ui_out *uiout = current_uiout;
220 struct breakpoint *b = bs->breakpoint_at;
221 int bp_temp;
222 enum exception_event_kind kind = classify_exception_breakpoint (b);
223
224 annotate_catchpoint (b->number);
f303dbd6 225 maybe_print_thread_hit_breakpoint (uiout);
916703c0
TT
226
227 bp_temp = b->disposition == disp_del;
112e8700 228 uiout->text (bp_temp ? "Temporary catchpoint "
916703c0 229 : "Catchpoint ");
2d33446d 230 uiout->field_int ("bkptno", b->number);
112e8700 231 uiout->text ((kind == EX_EVENT_THROW ? " (exception thrown), "
916703c0
TT
232 : (kind == EX_EVENT_CATCH ? " (exception caught), "
233 : " (exception rethrown), ")));
112e8700 234 if (uiout->is_mi_like_p ())
916703c0 235 {
112e8700 236 uiout->field_string ("reason",
916703c0 237 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 238 uiout->field_string ("disp", bpdisp_text (b->disposition));
916703c0
TT
239 }
240 return PRINT_SRC_AND_LOC;
241}
242
243static void
244print_one_exception_catchpoint (struct breakpoint *b,
245 struct bp_location **last_loc)
246{
247 struct value_print_options opts;
248 struct ui_out *uiout = current_uiout;
249 enum exception_event_kind kind = classify_exception_breakpoint (b);
250
251 get_user_print_options (&opts);
cb1e4e32 252
916703c0 253 if (opts.addressprint)
cb1e4e32 254 uiout->field_skip ("addr");
916703c0 255 annotate_field (5);
916703c0
TT
256
257 switch (kind)
258 {
259 case EX_EVENT_THROW:
112e8700
SM
260 uiout->field_string ("what", "exception throw");
261 if (uiout->is_mi_like_p ())
262 uiout->field_string ("catch-type", "throw");
916703c0
TT
263 break;
264
265 case EX_EVENT_RETHROW:
112e8700
SM
266 uiout->field_string ("what", "exception rethrow");
267 if (uiout->is_mi_like_p ())
268 uiout->field_string ("catch-type", "rethrow");
916703c0
TT
269 break;
270
271 case EX_EVENT_CATCH:
112e8700
SM
272 uiout->field_string ("what", "exception catch");
273 if (uiout->is_mi_like_p ())
274 uiout->field_string ("catch-type", "catch");
916703c0
TT
275 break;
276 }
277}
278
cc16e6c9
TT
279/* Implement the 'print_one_detail' method. */
280
281static void
282print_one_detail_exception_catchpoint (const struct breakpoint *b,
283 struct ui_out *uiout)
284{
285 const struct exception_catchpoint *cp
286 = (const struct exception_catchpoint *) b;
287
4fa8aeac 288 if (!cp->exception_rx.empty ())
cc16e6c9 289 {
112e8700 290 uiout->text (_("\tmatching: "));
4fa8aeac 291 uiout->field_string ("regexp", cp->exception_rx.c_str ());
112e8700 292 uiout->text ("\n");
cc16e6c9
TT
293 }
294}
295
916703c0
TT
296static void
297print_mention_exception_catchpoint (struct breakpoint *b)
298{
299 struct ui_out *uiout = current_uiout;
300 int bp_temp;
301 enum exception_event_kind kind = classify_exception_breakpoint (b);
302
303 bp_temp = b->disposition == disp_del;
30056ea0
AB
304 uiout->message ("%s %d %s",
305 (bp_temp ? _("Temporary catchpoint ") : _("Catchpoint")),
306 b->number,
307 (kind == EX_EVENT_THROW
308 ? _("(throw)") : (kind == EX_EVENT_CATCH
309 ? _("(catch)") : _("(rethrow)"))));
916703c0
TT
310}
311
312/* Implement the "print_recreate" breakpoint_ops method for throw and
313 catch catchpoints. */
314
315static void
316print_recreate_exception_catchpoint (struct breakpoint *b,
317 struct ui_file *fp)
318{
319 int bp_temp;
320 enum exception_event_kind kind = classify_exception_breakpoint (b);
321
322 bp_temp = b->disposition == disp_del;
323 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
324 switch (kind)
325 {
326 case EX_EVENT_THROW:
327 fprintf_unfiltered (fp, "throw");
328 break;
329 case EX_EVENT_CATCH:
330 fprintf_unfiltered (fp, "catch");
331 break;
332 case EX_EVENT_RETHROW:
333 fprintf_unfiltered (fp, "rethrow");
334 break;
335 }
336 print_recreate_thread (b, fp);
337}
338
cb1e4e32
PA
339/* Implement the "allocate_location" breakpoint_ops method for throw
340 and catch catchpoints. */
341
342static bp_location *
343allocate_location_exception_catchpoint (breakpoint *self)
344{
345 return new bp_location (self, bp_loc_software_breakpoint);
346}
347
15a73f56 348static void
4fa8aeac 349handle_gnu_v3_exceptions (int tempflag, std::string &&except_rx,
63160a43 350 const char *cond_string,
916703c0
TT
351 enum exception_event_kind ex_event, int from_tty)
352{
2d7cc5c7 353 std::unique_ptr<compiled_regex> pattern;
cc16e6c9 354
4fa8aeac 355 if (!except_rx.empty ())
cc16e6c9 356 {
4fa8aeac 357 pattern.reset (new compiled_regex (except_rx.c_str (), REG_NOSUB,
2d7cc5c7 358 _("invalid type-matching regexp")));
cc16e6c9 359 }
916703c0 360
b22e99fd 361 std::unique_ptr<exception_catchpoint> cp (new exception_catchpoint ());
cc16e6c9 362
c1fc2657 363 init_catchpoint (cp.get (), get_current_arch (), tempflag, cond_string,
15a73f56 364 &gnu_v3_exception_catchpoint_ops);
15a73f56 365 cp->kind = ex_event;
2f5404b3 366 cp->exception_rx = std::move (except_rx);
2d7cc5c7 367 cp->pattern = std::move (pattern);
15a73f56 368
c1fc2657 369 re_set_exception_catchpoint (cp.get ());
15a73f56 370
b270e6f9 371 install_breakpoint (0, std::move (cp), 1);
cc16e6c9
TT
372}
373
374/* Look for an "if" token in *STRING. The "if" token must be preceded
375 by whitespace.
376
377 If there is any non-whitespace text between *STRING and the "if"
378 token, then it is returned in a newly-xmalloc'd string. Otherwise,
379 this returns NULL.
380
381 STRING is updated to point to the "if" token, if it exists, or to
382 the end of the string. */
383
4fa8aeac 384static std::string
63160a43 385extract_exception_regexp (const char **string)
cc16e6c9 386{
63160a43
PA
387 const char *start;
388 const char *last, *last_space;
cc16e6c9 389
f1735a53 390 start = skip_spaces (*string);
cc16e6c9
TT
391
392 last = start;
393 last_space = start;
394 while (*last != '\0')
395 {
63160a43 396 const char *if_token = last;
cc16e6c9
TT
397
398 /* Check for the "if". */
399 if (check_for_argument (&if_token, "if", 2))
400 break;
401
402 /* No "if" token here. Skip to the next word start. */
403 last_space = skip_to_space (last);
f1735a53 404 last = skip_spaces (last_space);
cc16e6c9
TT
405 }
406
407 *string = last;
408 if (last_space > start)
4fa8aeac
TT
409 return std::string (start, last_space - start);
410 return std::string ();
916703c0
TT
411}
412
30056ea0 413/* See breakpoint.h. */
916703c0 414
30056ea0
AB
415void
416catch_exception_event (enum exception_event_kind ex_event,
417 const char *arg, bool tempflag, int from_tty)
916703c0 418{
63160a43 419 const char *cond_string = NULL;
916703c0
TT
420
421 if (!arg)
422 arg = "";
f1735a53 423 arg = skip_spaces (arg);
916703c0 424
4fa8aeac 425 std::string except_rx = extract_exception_regexp (&arg);
cc16e6c9 426
916703c0
TT
427 cond_string = ep_parse_optional_if_clause (&arg);
428
429 if ((*arg != '\0') && !isspace (*arg))
430 error (_("Junk at end of arguments."));
431
432 if (ex_event != EX_EVENT_THROW
433 && ex_event != EX_EVENT_CATCH
434 && ex_event != EX_EVENT_RETHROW)
435 error (_("Unsupported or unknown exception event; cannot catch it"));
436
4fa8aeac 437 handle_gnu_v3_exceptions (tempflag, std::move (except_rx), cond_string,
cc16e6c9 438 ex_event, from_tty);
916703c0
TT
439}
440
441/* Implementation of "catch catch" command. */
442
443static void
eb4c3f4a
TT
444catch_catch_command (const char *arg, int from_tty,
445 struct cmd_list_element *command)
916703c0 446{
30056ea0 447 bool tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
916703c0 448
30056ea0 449 catch_exception_event (EX_EVENT_CATCH, arg, tempflag, from_tty);
916703c0
TT
450}
451
452/* Implementation of "catch throw" command. */
453
454static void
eb4c3f4a
TT
455catch_throw_command (const char *arg, int from_tty,
456 struct cmd_list_element *command)
916703c0 457{
30056ea0 458 bool tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
916703c0 459
30056ea0 460 catch_exception_event (EX_EVENT_THROW, arg, tempflag, from_tty);
916703c0
TT
461}
462
463/* Implementation of "catch rethrow" command. */
464
465static void
eb4c3f4a 466catch_rethrow_command (const char *arg, int from_tty,
916703c0
TT
467 struct cmd_list_element *command)
468{
30056ea0 469 bool tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
916703c0 470
30056ea0 471 catch_exception_event (EX_EVENT_RETHROW, arg, tempflag, from_tty);
916703c0
TT
472}
473
474\f
475
72f1fe8a
TT
476/* Implement the 'make_value' method for the $_exception
477 internalvar. */
478
479static struct value *
480compute_exception (struct gdbarch *argc, struct internalvar *var, void *ignore)
481{
72f1fe8a
TT
482 struct value *arg0, *arg1;
483 struct type *obj_type;
484
cc16e6c9 485 fetch_probe_arguments (&arg0, &arg1);
72f1fe8a
TT
486
487 /* ARG0 is a pointer to the exception object. ARG1 is a pointer to
488 the std::type_info for the exception. Now we find the type from
489 the type_info and cast the result. */
490 obj_type = cplus_type_from_type_info (arg1);
491 return value_ind (value_cast (make_pointer_type (obj_type, NULL), arg0));
492}
493
494/* Implementation of the '$_exception' variable. */
495
496static const struct internalvar_funcs exception_funcs =
497{
498 compute_exception,
499 NULL,
500 NULL
501};
502
503\f
504
916703c0
TT
505static void
506initialize_throw_catchpoint_ops (void)
507{
508 struct breakpoint_ops *ops;
509
510 initialize_breakpoint_ops ();
511
512 /* GNU v3 exception catchpoints. */
513 ops = &gnu_v3_exception_catchpoint_ops;
514 *ops = bkpt_breakpoint_ops;
15a73f56 515 ops->re_set = re_set_exception_catchpoint;
916703c0
TT
516 ops->print_it = print_it_exception_catchpoint;
517 ops->print_one = print_one_exception_catchpoint;
518 ops->print_mention = print_mention_exception_catchpoint;
519 ops->print_recreate = print_recreate_exception_catchpoint;
cc16e6c9
TT
520 ops->print_one_detail = print_one_detail_exception_catchpoint;
521 ops->check_status = check_status_exception_catchpoint;
cb1e4e32 522 ops->allocate_location = allocate_location_exception_catchpoint;
916703c0
TT
523}
524
916703c0
TT
525void
526_initialize_break_catch_throw (void)
527{
528 initialize_throw_catchpoint_ops ();
529
530 /* Add catch and tcatch sub-commands. */
531 add_catch_command ("catch", _("\
532Catch an exception, when caught."),
533 catch_catch_command,
534 NULL,
535 CATCH_PERMANENT,
536 CATCH_TEMPORARY);
537 add_catch_command ("throw", _("\
538Catch an exception, when thrown."),
539 catch_throw_command,
540 NULL,
541 CATCH_PERMANENT,
542 CATCH_TEMPORARY);
543 add_catch_command ("rethrow", _("\
544Catch an exception, when rethrown."),
545 catch_rethrow_command,
546 NULL,
547 CATCH_PERMANENT,
548 CATCH_TEMPORARY);
72f1fe8a
TT
549
550 create_internalvar_type_lazy ("_exception", &exception_funcs, NULL);
916703c0 551}
This page took 0.466692 seconds and 4 git commands to generate.