dd05078e2cba7d162f2a09baf307f5375e1efa0b
[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
35 /* Enums for exception-handling support. */
36 enum exception_event_kind
37 {
38 EX_EVENT_THROW,
39 EX_EVENT_RETHROW,
40 EX_EVENT_CATCH
41 };
42
43 /* Names of the ordinary functions on which to break. This is indexed
44 by exception_event_kind. */
45 static const char * const exception_functions[] =
46 {
47 "__cxa_throw",
48 "__cxa_rethrow",
49 "__cxa_begin_catch"
50 };
51
52 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
53
54 /* The type of an exception catchpoint. */
55
56 struct exception_catchpoint
57 {
58 /* The base class. */
59
60 struct breakpoint base;
61
62 /* The kind of exception catchpoint. */
63
64 enum exception_event_kind kind;
65 };
66
67 /* A helper function that returns a value indicating the kind of the
68 exception catchpoint B. */
69
70 static enum exception_event_kind
71 classify_exception_breakpoint (struct breakpoint *b)
72 {
73 struct exception_catchpoint *cp = (struct exception_catchpoint *) b;
74
75 return cp->kind;
76 }
77
78 /* Implement the 're_set' method. */
79
80 static void
81 re_set_exception_catchpoint (struct breakpoint *self)
82 {
83 struct symtabs_and_lines sals = {0};
84 struct symtabs_and_lines sals_end = {0};
85 volatile struct gdb_exception e;
86 struct cleanup *cleanup;
87 enum exception_event_kind kind = classify_exception_breakpoint (self);
88
89 TRY_CATCH (e, RETURN_MASK_ERROR)
90 {
91 char *spec = ASTRDUP (exception_functions[kind]);
92
93 self->ops->decode_linespec (self, &spec, &sals);
94 }
95 /* NOT_FOUND_ERROR just means the breakpoint will be pending, so let
96 it through. */
97 if (e.reason < 0 && e.error != NOT_FOUND_ERROR)
98 throw_exception (e);
99
100 cleanup = make_cleanup (xfree, sals.sals);
101 update_breakpoint_locations (self, sals, sals_end);
102 do_cleanups (cleanup);
103 }
104
105 static enum print_stop_action
106 print_it_exception_catchpoint (bpstat bs)
107 {
108 struct ui_out *uiout = current_uiout;
109 struct breakpoint *b = bs->breakpoint_at;
110 int bp_temp;
111 enum exception_event_kind kind = classify_exception_breakpoint (b);
112
113 annotate_catchpoint (b->number);
114
115 bp_temp = b->disposition == disp_del;
116 ui_out_text (uiout,
117 bp_temp ? "Temporary catchpoint "
118 : "Catchpoint ");
119 if (!ui_out_is_mi_like_p (uiout))
120 ui_out_field_int (uiout, "bkptno", b->number);
121 ui_out_text (uiout,
122 (kind == EX_EVENT_THROW ? " (exception thrown), "
123 : (kind == EX_EVENT_CATCH ? " (exception caught), "
124 : " (exception rethrown), ")));
125 if (ui_out_is_mi_like_p (uiout))
126 {
127 ui_out_field_string (uiout, "reason",
128 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
129 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
130 ui_out_field_int (uiout, "bkptno", b->number);
131 }
132 return PRINT_SRC_AND_LOC;
133 }
134
135 static void
136 print_one_exception_catchpoint (struct breakpoint *b,
137 struct bp_location **last_loc)
138 {
139 struct value_print_options opts;
140 struct ui_out *uiout = current_uiout;
141 enum exception_event_kind kind = classify_exception_breakpoint (b);
142
143 get_user_print_options (&opts);
144 if (opts.addressprint)
145 {
146 annotate_field (4);
147 if (b->loc == NULL || b->loc->shlib_disabled)
148 ui_out_field_string (uiout, "addr", "<PENDING>");
149 else
150 ui_out_field_core_addr (uiout, "addr",
151 b->loc->gdbarch, b->loc->address);
152 }
153 annotate_field (5);
154 if (b->loc)
155 *last_loc = b->loc;
156
157 switch (kind)
158 {
159 case EX_EVENT_THROW:
160 ui_out_field_string (uiout, "what", "exception throw");
161 if (ui_out_is_mi_like_p (uiout))
162 ui_out_field_string (uiout, "catch-type", "throw");
163 break;
164
165 case EX_EVENT_RETHROW:
166 ui_out_field_string (uiout, "what", "exception rethrow");
167 if (ui_out_is_mi_like_p (uiout))
168 ui_out_field_string (uiout, "catch-type", "rethrow");
169 break;
170
171 case EX_EVENT_CATCH:
172 ui_out_field_string (uiout, "what", "exception catch");
173 if (ui_out_is_mi_like_p (uiout))
174 ui_out_field_string (uiout, "catch-type", "catch");
175 break;
176 }
177 }
178
179 static void
180 print_mention_exception_catchpoint (struct breakpoint *b)
181 {
182 struct ui_out *uiout = current_uiout;
183 int bp_temp;
184 enum exception_event_kind kind = classify_exception_breakpoint (b);
185
186 bp_temp = b->disposition == disp_del;
187 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
188 : _("Catchpoint "));
189 ui_out_field_int (uiout, "bkptno", b->number);
190 ui_out_text (uiout, (kind == EX_EVENT_THROW ? _(" (throw)")
191 : (kind == EX_EVENT_CATCH ? _(" (catch)")
192 : _(" (rethrow)"))));
193 }
194
195 /* Implement the "print_recreate" breakpoint_ops method for throw and
196 catch catchpoints. */
197
198 static void
199 print_recreate_exception_catchpoint (struct breakpoint *b,
200 struct ui_file *fp)
201 {
202 int bp_temp;
203 enum exception_event_kind kind = classify_exception_breakpoint (b);
204
205 bp_temp = b->disposition == disp_del;
206 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
207 switch (kind)
208 {
209 case EX_EVENT_THROW:
210 fprintf_unfiltered (fp, "throw");
211 break;
212 case EX_EVENT_CATCH:
213 fprintf_unfiltered (fp, "catch");
214 break;
215 case EX_EVENT_RETHROW:
216 fprintf_unfiltered (fp, "rethrow");
217 break;
218 }
219 print_recreate_thread (b, fp);
220 }
221
222 static void
223 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
224 enum exception_event_kind ex_event, int from_tty)
225 {
226 struct exception_catchpoint *cp;
227
228 cp = XCNEW (struct exception_catchpoint);
229 init_catchpoint (&cp->base, get_current_arch (), tempflag, cond_string,
230 &gnu_v3_exception_catchpoint_ops);
231 /* We need to reset 'type' in order for code in breakpoint.c to do
232 the right thing. */
233 cp->base.type = bp_breakpoint;
234 cp->kind = ex_event;
235
236 re_set_exception_catchpoint (&cp->base);
237
238 install_breakpoint (0, &cp->base, 1);
239 }
240
241 /* Deal with "catch catch", "catch throw", and "catch rethrow"
242 commands. */
243
244 static void
245 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
246 int tempflag, int from_tty)
247 {
248 char *cond_string = NULL;
249
250 if (!arg)
251 arg = "";
252 arg = skip_spaces (arg);
253
254 cond_string = ep_parse_optional_if_clause (&arg);
255
256 if ((*arg != '\0') && !isspace (*arg))
257 error (_("Junk at end of arguments."));
258
259 if (ex_event != EX_EVENT_THROW
260 && ex_event != EX_EVENT_CATCH
261 && ex_event != EX_EVENT_RETHROW)
262 error (_("Unsupported or unknown exception event; cannot catch it"));
263
264 handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty);
265 }
266
267 /* Implementation of "catch catch" command. */
268
269 static void
270 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
271 {
272 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
273
274 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
275 }
276
277 /* Implementation of "catch throw" command. */
278
279 static void
280 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
281 {
282 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
283
284 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
285 }
286
287 /* Implementation of "catch rethrow" command. */
288
289 static void
290 catch_rethrow_command (char *arg, int from_tty,
291 struct cmd_list_element *command)
292 {
293 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
294
295 catch_exception_command_1 (EX_EVENT_RETHROW, arg, tempflag, from_tty);
296 }
297
298 \f
299
300 static void
301 initialize_throw_catchpoint_ops (void)
302 {
303 struct breakpoint_ops *ops;
304
305 initialize_breakpoint_ops ();
306
307 /* GNU v3 exception catchpoints. */
308 ops = &gnu_v3_exception_catchpoint_ops;
309 *ops = bkpt_breakpoint_ops;
310 ops->re_set = re_set_exception_catchpoint;
311 ops->print_it = print_it_exception_catchpoint;
312 ops->print_one = print_one_exception_catchpoint;
313 ops->print_mention = print_mention_exception_catchpoint;
314 ops->print_recreate = print_recreate_exception_catchpoint;
315 }
316
317 initialize_file_ftype _initialize_break_catch_throw;
318
319 void
320 _initialize_break_catch_throw (void)
321 {
322 initialize_throw_catchpoint_ops ();
323
324 /* Add catch and tcatch sub-commands. */
325 add_catch_command ("catch", _("\
326 Catch an exception, when caught."),
327 catch_catch_command,
328 NULL,
329 CATCH_PERMANENT,
330 CATCH_TEMPORARY);
331 add_catch_command ("throw", _("\
332 Catch an exception, when thrown."),
333 catch_throw_command,
334 NULL,
335 CATCH_PERMANENT,
336 CATCH_TEMPORARY);
337 add_catch_command ("rethrow", _("\
338 Catch an exception, when rethrown."),
339 catch_rethrow_command,
340 NULL,
341 CATCH_PERMANENT,
342 CATCH_TEMPORARY);
343 }