PR c++/15176:
[deliverable/binutils-gdb.git] / gdb / break-catch-throw.c
1 /* Everything about catch/throw catchpoints, for GDB.
2
3 Copyright (C) 1986-2013 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 "gdb_obstack.h"
31 #include "mi/mi-common.h"
32 #include "exceptions.h"
33 #include "linespec.h"
34 #include "probe.h"
35 #include "objfiles.h"
36 #include "cp-abi.h"
37
38 /* Enums for exception-handling support. */
39 enum exception_event_kind
40 {
41 EX_EVENT_THROW,
42 EX_EVENT_RETHROW,
43 EX_EVENT_CATCH
44 };
45
46 /* Each spot where we may place an exception-related catchpoint has
47 two names: the SDT probe point and the function name. This
48 structure holds both. */
49
50 struct exception_names
51 {
52 /* The name of the probe point to try, in the form accepted by
53 'parse_probes'. */
54
55 const char *probe;
56
57 /* The name of the corresponding function. */
58
59 const char *function;
60 };
61
62 /* Names of the probe points and functions on which to break. This is
63 indexed by exception_event_kind. */
64 static const struct exception_names exception_functions[] =
65 {
66 { "-probe-stap libstdcxx:throw", "__cxa_throw" },
67 { "-probe-stap libstdcxx:rethrow", "__cxa_rethrow" },
68 { "-probe-stap libstdcxx:catch", "__cxa_begin_catch" }
69 };
70
71 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
72
73 /* The type of an exception catchpoint. */
74
75 struct exception_catchpoint
76 {
77 /* The base class. */
78
79 struct breakpoint base;
80
81 /* The kind of exception catchpoint. */
82
83 enum exception_event_kind kind;
84 };
85
86 /* A helper function that returns a value indicating the kind of the
87 exception catchpoint B. */
88
89 static enum exception_event_kind
90 classify_exception_breakpoint (struct breakpoint *b)
91 {
92 struct exception_catchpoint *cp = (struct exception_catchpoint *) b;
93
94 return cp->kind;
95 }
96
97 /* Implement the 're_set' method. */
98
99 static void
100 re_set_exception_catchpoint (struct breakpoint *self)
101 {
102 struct symtabs_and_lines sals = {0};
103 struct symtabs_and_lines sals_end = {0};
104 volatile struct gdb_exception e;
105 struct cleanup *cleanup;
106 enum exception_event_kind kind = classify_exception_breakpoint (self);
107 int pass;
108
109 for (pass = 0; sals.sals == NULL && pass < 2; ++pass)
110 {
111 TRY_CATCH (e, RETURN_MASK_ERROR)
112 {
113 char *spec;
114
115 if (pass == 0)
116 {
117 spec = ASTRDUP (exception_functions[kind].probe);
118 sals = parse_probes (&spec, NULL);
119 }
120 else
121 {
122 spec = ASTRDUP (exception_functions[kind].function);
123 self->ops->decode_linespec (self, &spec, &sals);
124 }
125 }
126 /* NOT_FOUND_ERROR just means the breakpoint will be pending, so
127 let it through. */
128 if (e.reason < 0 && e.error != NOT_FOUND_ERROR)
129 throw_exception (e);
130 }
131
132 cleanup = make_cleanup (xfree, sals.sals);
133 update_breakpoint_locations (self, sals, sals_end);
134 do_cleanups (cleanup);
135 }
136
137 static enum print_stop_action
138 print_it_exception_catchpoint (bpstat bs)
139 {
140 struct ui_out *uiout = current_uiout;
141 struct breakpoint *b = bs->breakpoint_at;
142 int bp_temp;
143 enum exception_event_kind kind = classify_exception_breakpoint (b);
144
145 annotate_catchpoint (b->number);
146
147 bp_temp = b->disposition == disp_del;
148 ui_out_text (uiout,
149 bp_temp ? "Temporary catchpoint "
150 : "Catchpoint ");
151 if (!ui_out_is_mi_like_p (uiout))
152 ui_out_field_int (uiout, "bkptno", b->number);
153 ui_out_text (uiout,
154 (kind == EX_EVENT_THROW ? " (exception thrown), "
155 : (kind == EX_EVENT_CATCH ? " (exception caught), "
156 : " (exception rethrown), ")));
157 if (ui_out_is_mi_like_p (uiout))
158 {
159 ui_out_field_string (uiout, "reason",
160 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
161 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
162 ui_out_field_int (uiout, "bkptno", b->number);
163 }
164 return PRINT_SRC_AND_LOC;
165 }
166
167 static void
168 print_one_exception_catchpoint (struct breakpoint *b,
169 struct bp_location **last_loc)
170 {
171 struct value_print_options opts;
172 struct ui_out *uiout = current_uiout;
173 enum exception_event_kind kind = classify_exception_breakpoint (b);
174
175 get_user_print_options (&opts);
176 if (opts.addressprint)
177 {
178 annotate_field (4);
179 if (b->loc == NULL || b->loc->shlib_disabled)
180 ui_out_field_string (uiout, "addr", "<PENDING>");
181 else
182 ui_out_field_core_addr (uiout, "addr",
183 b->loc->gdbarch, b->loc->address);
184 }
185 annotate_field (5);
186 if (b->loc)
187 *last_loc = b->loc;
188
189 switch (kind)
190 {
191 case EX_EVENT_THROW:
192 ui_out_field_string (uiout, "what", "exception throw");
193 if (ui_out_is_mi_like_p (uiout))
194 ui_out_field_string (uiout, "catch-type", "throw");
195 break;
196
197 case EX_EVENT_RETHROW:
198 ui_out_field_string (uiout, "what", "exception rethrow");
199 if (ui_out_is_mi_like_p (uiout))
200 ui_out_field_string (uiout, "catch-type", "rethrow");
201 break;
202
203 case EX_EVENT_CATCH:
204 ui_out_field_string (uiout, "what", "exception catch");
205 if (ui_out_is_mi_like_p (uiout))
206 ui_out_field_string (uiout, "catch-type", "catch");
207 break;
208 }
209 }
210
211 static void
212 print_mention_exception_catchpoint (struct breakpoint *b)
213 {
214 struct ui_out *uiout = current_uiout;
215 int bp_temp;
216 enum exception_event_kind kind = classify_exception_breakpoint (b);
217
218 bp_temp = b->disposition == disp_del;
219 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
220 : _("Catchpoint "));
221 ui_out_field_int (uiout, "bkptno", b->number);
222 ui_out_text (uiout, (kind == EX_EVENT_THROW ? _(" (throw)")
223 : (kind == EX_EVENT_CATCH ? _(" (catch)")
224 : _(" (rethrow)"))));
225 }
226
227 /* Implement the "print_recreate" breakpoint_ops method for throw and
228 catch catchpoints. */
229
230 static void
231 print_recreate_exception_catchpoint (struct breakpoint *b,
232 struct ui_file *fp)
233 {
234 int bp_temp;
235 enum exception_event_kind kind = classify_exception_breakpoint (b);
236
237 bp_temp = b->disposition == disp_del;
238 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
239 switch (kind)
240 {
241 case EX_EVENT_THROW:
242 fprintf_unfiltered (fp, "throw");
243 break;
244 case EX_EVENT_CATCH:
245 fprintf_unfiltered (fp, "catch");
246 break;
247 case EX_EVENT_RETHROW:
248 fprintf_unfiltered (fp, "rethrow");
249 break;
250 }
251 print_recreate_thread (b, fp);
252 }
253
254 static void
255 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
256 enum exception_event_kind ex_event, int from_tty)
257 {
258 struct exception_catchpoint *cp;
259
260 cp = XCNEW (struct exception_catchpoint);
261 init_catchpoint (&cp->base, get_current_arch (), tempflag, cond_string,
262 &gnu_v3_exception_catchpoint_ops);
263 /* We need to reset 'type' in order for code in breakpoint.c to do
264 the right thing. */
265 cp->base.type = bp_breakpoint;
266 cp->kind = ex_event;
267
268 re_set_exception_catchpoint (&cp->base);
269
270 install_breakpoint (0, &cp->base, 1);
271 }
272
273 /* Deal with "catch catch", "catch throw", and "catch rethrow"
274 commands. */
275
276 static void
277 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
278 int tempflag, int from_tty)
279 {
280 char *cond_string = NULL;
281
282 if (!arg)
283 arg = "";
284 arg = skip_spaces (arg);
285
286 cond_string = ep_parse_optional_if_clause (&arg);
287
288 if ((*arg != '\0') && !isspace (*arg))
289 error (_("Junk at end of arguments."));
290
291 if (ex_event != EX_EVENT_THROW
292 && ex_event != EX_EVENT_CATCH
293 && ex_event != EX_EVENT_RETHROW)
294 error (_("Unsupported or unknown exception event; cannot catch it"));
295
296 handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty);
297 }
298
299 /* Implementation of "catch catch" command. */
300
301 static void
302 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
303 {
304 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
305
306 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
307 }
308
309 /* Implementation of "catch throw" command. */
310
311 static void
312 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
313 {
314 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
315
316 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
317 }
318
319 /* Implementation of "catch rethrow" command. */
320
321 static void
322 catch_rethrow_command (char *arg, int from_tty,
323 struct cmd_list_element *command)
324 {
325 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
326
327 catch_exception_command_1 (EX_EVENT_RETHROW, arg, tempflag, from_tty);
328 }
329
330 \f
331
332 /* Implement the 'make_value' method for the $_exception
333 internalvar. */
334
335 static struct value *
336 compute_exception (struct gdbarch *argc, struct internalvar *var, void *ignore)
337 {
338 struct frame_info *frame = get_selected_frame (_("No frame selected"));
339 CORE_ADDR pc = get_frame_pc (frame);
340 struct probe *pc_probe;
341 const struct sym_probe_fns *pc_probe_fns;
342 unsigned n_args;
343 struct value *arg0, *arg1;
344 struct type *obj_type;
345
346 pc_probe = find_probe_by_pc (pc);
347 if (pc_probe == NULL
348 || strcmp (pc_probe->provider, "libstdcxx") != 0
349 || (strcmp (pc_probe->name, "catch") != 0
350 && strcmp (pc_probe->name, "throw") != 0
351 && strcmp (pc_probe->name, "rethrow") != 0))
352 error (_("not stopped at a C++ exception catchpoint"));
353
354 gdb_assert (pc_probe->objfile != NULL);
355 gdb_assert (pc_probe->objfile->sf != NULL);
356 gdb_assert (pc_probe->objfile->sf->sym_probe_fns != NULL);
357
358 pc_probe_fns = pc_probe->objfile->sf->sym_probe_fns;
359 n_args = pc_probe_fns->sym_get_probe_argument_count (pc_probe);
360 if (n_args < 2)
361 error (_("C++ exception catchpoint has too few arguments"));
362
363 arg0 = pc_probe_fns->sym_evaluate_probe_argument (pc_probe, 0);
364 arg1 = pc_probe_fns->sym_evaluate_probe_argument (pc_probe, 1);
365
366 if (arg0 == NULL || arg1 == NULL)
367 error (_("error computing probe argument at c++ exception catchpoint"));
368
369 /* ARG0 is a pointer to the exception object. ARG1 is a pointer to
370 the std::type_info for the exception. Now we find the type from
371 the type_info and cast the result. */
372 obj_type = cplus_type_from_type_info (arg1);
373 return value_ind (value_cast (make_pointer_type (obj_type, NULL), arg0));
374 }
375
376 /* Implementation of the '$_exception' variable. */
377
378 static const struct internalvar_funcs exception_funcs =
379 {
380 compute_exception,
381 NULL,
382 NULL
383 };
384
385 \f
386
387 static void
388 initialize_throw_catchpoint_ops (void)
389 {
390 struct breakpoint_ops *ops;
391
392 initialize_breakpoint_ops ();
393
394 /* GNU v3 exception catchpoints. */
395 ops = &gnu_v3_exception_catchpoint_ops;
396 *ops = bkpt_breakpoint_ops;
397 ops->re_set = re_set_exception_catchpoint;
398 ops->print_it = print_it_exception_catchpoint;
399 ops->print_one = print_one_exception_catchpoint;
400 ops->print_mention = print_mention_exception_catchpoint;
401 ops->print_recreate = print_recreate_exception_catchpoint;
402 }
403
404 initialize_file_ftype _initialize_break_catch_throw;
405
406 void
407 _initialize_break_catch_throw (void)
408 {
409 initialize_throw_catchpoint_ops ();
410
411 /* Add catch and tcatch sub-commands. */
412 add_catch_command ("catch", _("\
413 Catch an exception, when caught."),
414 catch_catch_command,
415 NULL,
416 CATCH_PERMANENT,
417 CATCH_TEMPORARY);
418 add_catch_command ("throw", _("\
419 Catch an exception, when thrown."),
420 catch_throw_command,
421 NULL,
422 CATCH_PERMANENT,
423 CATCH_TEMPORARY);
424 add_catch_command ("rethrow", _("\
425 Catch an exception, when rethrown."),
426 catch_rethrow_command,
427 NULL,
428 CATCH_PERMANENT,
429 CATCH_TEMPORARY);
430
431 create_internalvar_type_lazy ("_exception", &exception_funcs, NULL);
432 }
This page took 0.038461 seconds and 4 git commands to generate.