Plumb enum remove_bp_reason all the way to target_remove_breakpoint
[deliverable/binutils-gdb.git] / gdb / break-catch-sig.c
CommitLineData
ab04a2af
TT
1/* Everything about signal catchpoints, for GDB.
2
618f726f 3 Copyright (C) 2011-2016 Free Software Foundation, Inc.
ab04a2af
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"
21#include "arch-utils.h"
22#include <ctype.h>
23#include "breakpoint.h"
24#include "gdbcmd.h"
25#include "inferior.h"
45741a9c 26#include "infrun.h"
ab04a2af
TT
27#include "annotate.h"
28#include "valprint.h"
29#include "cli/cli-utils.h"
30#include "completer.h"
31#include "gdb_obstack.h"
32
33#define INTERNAL_SIGNAL(x) ((x) == GDB_SIGNAL_TRAP || (x) == GDB_SIGNAL_INT)
34
35typedef enum gdb_signal gdb_signal_type;
36
37DEF_VEC_I (gdb_signal_type);
38
39/* An instance of this type is used to represent a signal catchpoint.
40 It includes a "struct breakpoint" as a kind of base class; users
41 downcast to "struct breakpoint *" when needed. A breakpoint is
42 really of this type iff its ops pointer points to
43 SIGNAL_CATCHPOINT_OPS. */
44
45struct signal_catchpoint
46{
47 /* The base class. */
48
49 struct breakpoint base;
50
51 /* Signal numbers used for the 'catch signal' feature. If no signal
52 has been specified for filtering, its value is NULL. Otherwise,
53 it holds a list of all signals to be caught. */
54
55 VEC (gdb_signal_type) *signals_to_be_caught;
56
57 /* If SIGNALS_TO_BE_CAUGHT is NULL, then all "ordinary" signals are
58 caught. If CATCH_ALL is non-zero, then internal signals are
59 caught as well. If SIGNALS_TO_BE_CAUGHT is non-NULL, then this
60 field is ignored. */
61
62 int catch_all;
63};
64
65/* The breakpoint_ops structure to be used in signal catchpoints. */
66
67static struct breakpoint_ops signal_catchpoint_ops;
68
69/* Count of each signal. */
70
71static unsigned int *signal_catch_counts;
72
73\f
74
75/* A convenience wrapper for gdb_signal_to_name that returns the
76 integer value if the name is not known. */
77
78static const char *
79signal_to_name_or_int (enum gdb_signal sig)
80{
81 const char *result = gdb_signal_to_name (sig);
82
83 if (strcmp (result, "?") == 0)
84 result = plongest (sig);
85
86 return result;
87}
88
89\f
90
91/* Implement the "dtor" breakpoint_ops method for signal
92 catchpoints. */
93
94static void
95signal_catchpoint_dtor (struct breakpoint *b)
96{
97 struct signal_catchpoint *c = (struct signal_catchpoint *) b;
98
99 VEC_free (gdb_signal_type, c->signals_to_be_caught);
100
101 base_breakpoint_ops.dtor (b);
102}
103
104/* Implement the "insert_location" breakpoint_ops method for signal
105 catchpoints. */
106
107static int
108signal_catchpoint_insert_location (struct bp_location *bl)
109{
9a3c8263 110 struct signal_catchpoint *c = (struct signal_catchpoint *) bl->owner;
ab04a2af
TT
111 int i;
112
113 if (c->signals_to_be_caught != NULL)
114 {
115 gdb_signal_type iter;
116
117 for (i = 0;
118 VEC_iterate (gdb_signal_type, c->signals_to_be_caught, i, iter);
119 i++)
120 ++signal_catch_counts[iter];
121 }
122 else
123 {
124 for (i = 0; i < GDB_SIGNAL_LAST; ++i)
125 {
126 if (c->catch_all || !INTERNAL_SIGNAL (i))
127 ++signal_catch_counts[i];
128 }
129 }
130
131 signal_catch_update (signal_catch_counts);
132
133 return 0;
134}
135
136/* Implement the "remove_location" breakpoint_ops method for signal
137 catchpoints. */
138
139static int
73971819
PA
140signal_catchpoint_remove_location (struct bp_location *bl,
141 enum remove_bp_reason reason)
ab04a2af 142{
9a3c8263 143 struct signal_catchpoint *c = (struct signal_catchpoint *) bl->owner;
ab04a2af
TT
144 int i;
145
146 if (c->signals_to_be_caught != NULL)
147 {
148 gdb_signal_type iter;
149
150 for (i = 0;
151 VEC_iterate (gdb_signal_type, c->signals_to_be_caught, i, iter);
152 i++)
153 {
154 gdb_assert (signal_catch_counts[iter] > 0);
155 --signal_catch_counts[iter];
156 }
157 }
158 else
159 {
160 for (i = 0; i < GDB_SIGNAL_LAST; ++i)
161 {
162 if (c->catch_all || !INTERNAL_SIGNAL (i))
163 {
164 gdb_assert (signal_catch_counts[i] > 0);
165 --signal_catch_counts[i];
166 }
167 }
168 }
169
170 signal_catch_update (signal_catch_counts);
171
172 return 0;
173}
174
175/* Implement the "breakpoint_hit" breakpoint_ops method for signal
176 catchpoints. */
177
178static int
179signal_catchpoint_breakpoint_hit (const struct bp_location *bl,
180 struct address_space *aspace,
181 CORE_ADDR bp_addr,
182 const struct target_waitstatus *ws)
183{
9a3c8263
SM
184 const struct signal_catchpoint *c
185 = (const struct signal_catchpoint *) bl->owner;
ab04a2af
TT
186 gdb_signal_type signal_number;
187
188 if (ws->kind != TARGET_WAITKIND_STOPPED)
189 return 0;
190
191 signal_number = ws->value.sig;
192
193 /* If we are catching specific signals in this breakpoint, then we
194 must guarantee that the called signal is the same signal we are
195 catching. */
196 if (c->signals_to_be_caught)
197 {
198 int i;
199 gdb_signal_type iter;
200
201 for (i = 0;
202 VEC_iterate (gdb_signal_type, c->signals_to_be_caught, i, iter);
203 i++)
204 if (signal_number == iter)
96f7d3f1 205 return 1;
ab04a2af 206 /* Not the same. */
96f7d3f1
PW
207 gdb_assert (!iter);
208 return 0;
ab04a2af 209 }
96f7d3f1
PW
210 else
211 return c->catch_all || !INTERNAL_SIGNAL (signal_number);
ab04a2af
TT
212}
213
214/* Implement the "print_it" breakpoint_ops method for signal
215 catchpoints. */
216
217static enum print_stop_action
218signal_catchpoint_print_it (bpstat bs)
219{
220 struct breakpoint *b = bs->breakpoint_at;
221 ptid_t ptid;
222 struct target_waitstatus last;
223 const char *signal_name;
f303dbd6 224 struct ui_out *uiout = current_uiout;
ab04a2af
TT
225
226 get_last_target_status (&ptid, &last);
227
228 signal_name = signal_to_name_or_int (last.value.sig);
229
230 annotate_catchpoint (b->number);
f303dbd6 231 maybe_print_thread_hit_breakpoint (uiout);
ab04a2af 232
f303dbd6 233 printf_filtered (_("Catchpoint %d (signal %s), "), b->number, signal_name);
ab04a2af
TT
234
235 return PRINT_SRC_AND_LOC;
236}
237
238/* Implement the "print_one" breakpoint_ops method for signal
239 catchpoints. */
240
241static void
242signal_catchpoint_print_one (struct breakpoint *b,
243 struct bp_location **last_loc)
244{
9a3c8263 245 struct signal_catchpoint *c = (struct signal_catchpoint *) b;
ab04a2af
TT
246 struct value_print_options opts;
247 struct ui_out *uiout = current_uiout;
248
249 get_user_print_options (&opts);
250
251 /* Field 4, the address, is omitted (which makes the columns
252 not line up too nicely with the headers, but the effect
253 is relatively readable). */
254 if (opts.addressprint)
255 ui_out_field_skip (uiout, "addr");
256 annotate_field (5);
257
258 if (c->signals_to_be_caught
259 && VEC_length (gdb_signal_type, c->signals_to_be_caught) > 1)
260 ui_out_text (uiout, "signals \"");
261 else
262 ui_out_text (uiout, "signal \"");
263
264 if (c->signals_to_be_caught)
265 {
266 int i;
267 gdb_signal_type iter;
268 struct obstack text;
269 struct cleanup *cleanup;
270
271 obstack_init (&text);
272 cleanup = make_cleanup_obstack_free (&text);
273
274 for (i = 0;
275 VEC_iterate (gdb_signal_type, c->signals_to_be_caught, i, iter);
276 i++)
277 {
278 const char *name = signal_to_name_or_int (iter);
279
280 if (i > 0)
281 obstack_grow (&text, " ", 1);
282 obstack_grow (&text, name, strlen (name));
283 }
284 obstack_grow (&text, "", 1);
79f33898 285 ui_out_field_string (uiout, "what", (const char *) obstack_base (&text));
ab04a2af
TT
286 do_cleanups (cleanup);
287 }
288 else
289 ui_out_field_string (uiout, "what",
290 c->catch_all ? "<any signal>" : "<standard signals>");
291 ui_out_text (uiout, "\" ");
292
293 if (ui_out_is_mi_like_p (uiout))
294 ui_out_field_string (uiout, "catch-type", "signal");
295}
296
297/* Implement the "print_mention" breakpoint_ops method for signal
298 catchpoints. */
299
300static void
301signal_catchpoint_print_mention (struct breakpoint *b)
302{
9a3c8263 303 struct signal_catchpoint *c = (struct signal_catchpoint *) b;
ab04a2af
TT
304
305 if (c->signals_to_be_caught)
306 {
307 int i;
308 gdb_signal_type iter;
309
310 if (VEC_length (gdb_signal_type, c->signals_to_be_caught) > 1)
311 printf_filtered (_("Catchpoint %d (signals"), b->number);
312 else
313 printf_filtered (_("Catchpoint %d (signal"), b->number);
314
315 for (i = 0;
316 VEC_iterate (gdb_signal_type, c->signals_to_be_caught, i, iter);
317 i++)
318 {
319 const char *name = signal_to_name_or_int (iter);
320
321 printf_filtered (" %s", name);
322 }
323 printf_filtered (")");
324 }
325 else if (c->catch_all)
326 printf_filtered (_("Catchpoint %d (any signal)"), b->number);
327 else
328 printf_filtered (_("Catchpoint %d (standard signals)"), b->number);
329}
330
331/* Implement the "print_recreate" breakpoint_ops method for signal
332 catchpoints. */
333
334static void
335signal_catchpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
336{
9a3c8263 337 struct signal_catchpoint *c = (struct signal_catchpoint *) b;
ab04a2af
TT
338
339 fprintf_unfiltered (fp, "catch signal");
340
341 if (c->signals_to_be_caught)
342 {
343 int i;
344 gdb_signal_type iter;
345
346 for (i = 0;
347 VEC_iterate (gdb_signal_type, c->signals_to_be_caught, i, iter);
348 i++)
349 fprintf_unfiltered (fp, " %s", signal_to_name_or_int (iter));
350 }
351 else if (c->catch_all)
352 fprintf_unfiltered (fp, " all");
c780cc2f 353 fputc_unfiltered ('\n', fp);
ab04a2af
TT
354}
355
356/* Implement the "explains_signal" breakpoint_ops method for signal
357 catchpoints. */
358
47591c29 359static int
427cd150 360signal_catchpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 361{
47591c29 362 return 1;
ab04a2af
TT
363}
364
365/* Create a new signal catchpoint. TEMPFLAG is true if this should be
366 a temporary catchpoint. FILTER is the list of signals to catch; it
367 can be NULL, meaning all signals. CATCH_ALL is a flag indicating
368 whether signals used internally by gdb should be caught; it is only
369 valid if FILTER is NULL. If FILTER is NULL and CATCH_ALL is zero,
370 then internal signals like SIGTRAP are not caught. */
371
372static void
373create_signal_catchpoint (int tempflag, VEC (gdb_signal_type) *filter,
374 int catch_all)
375{
376 struct signal_catchpoint *c;
377 struct gdbarch *gdbarch = get_current_arch ();
378
379 c = XNEW (struct signal_catchpoint);
380 init_catchpoint (&c->base, gdbarch, tempflag, NULL, &signal_catchpoint_ops);
381 c->signals_to_be_caught = filter;
382 c->catch_all = catch_all;
383
384 install_breakpoint (0, &c->base, 1);
385}
386
387
388/* Splits the argument using space as delimiter. Returns an xmalloc'd
389 filter list, or NULL if no filtering is required. */
390
391static VEC (gdb_signal_type) *
392catch_signal_split_args (char *arg, int *catch_all)
393{
394 VEC (gdb_signal_type) *result = NULL;
395 struct cleanup *cleanup = make_cleanup (VEC_cleanup (gdb_signal_type),
396 &result);
397 int first = 1;
398
399 while (*arg != '\0')
400 {
401 int num;
402 gdb_signal_type signal_number;
403 char *one_arg, *endptr;
404 struct cleanup *inner_cleanup;
405
406 one_arg = extract_arg (&arg);
407 if (one_arg == NULL)
408 break;
409 inner_cleanup = make_cleanup (xfree, one_arg);
410
411 /* Check for the special flag "all". */
412 if (strcmp (one_arg, "all") == 0)
413 {
414 arg = skip_spaces (arg);
415 if (*arg != '\0' || !first)
416 error (_("'all' cannot be caught with other signals"));
417 *catch_all = 1;
418 gdb_assert (result == NULL);
419 do_cleanups (inner_cleanup);
420 discard_cleanups (cleanup);
421 return NULL;
422 }
423
424 first = 0;
425
426 /* Check if the user provided a signal name or a number. */
427 num = (int) strtol (one_arg, &endptr, 0);
428 if (*endptr == '\0')
429 signal_number = gdb_signal_from_command (num);
430 else
431 {
432 signal_number = gdb_signal_from_name (one_arg);
433 if (signal_number == GDB_SIGNAL_UNKNOWN)
434 error (_("Unknown signal name '%s'."), one_arg);
435 }
436
437 VEC_safe_push (gdb_signal_type, result, signal_number);
438 do_cleanups (inner_cleanup);
439 }
440
441 discard_cleanups (cleanup);
442 return result;
443}
444
445/* Implement the "catch signal" command. */
446
447static void
448catch_signal_command (char *arg, int from_tty,
449 struct cmd_list_element *command)
450{
451 int tempflag, catch_all = 0;
452 VEC (gdb_signal_type) *filter;
ab04a2af
TT
453
454 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
455
456 arg = skip_spaces (arg);
457
458 /* The allowed syntax is:
459 catch signal
460 catch signal <name | number> [<name | number> ... <name | number>]
461
462 Let's check if there's a signal name. */
463
464 if (arg != NULL)
465 filter = catch_signal_split_args (arg, &catch_all);
466 else
467 filter = NULL;
468
469 create_signal_catchpoint (tempflag, filter, catch_all);
470}
471
472static void
473initialize_signal_catchpoint_ops (void)
474{
475 struct breakpoint_ops *ops;
476
477 initialize_breakpoint_ops ();
478
479 ops = &signal_catchpoint_ops;
480 *ops = base_breakpoint_ops;
481 ops->dtor = signal_catchpoint_dtor;
482 ops->insert_location = signal_catchpoint_insert_location;
483 ops->remove_location = signal_catchpoint_remove_location;
484 ops->breakpoint_hit = signal_catchpoint_breakpoint_hit;
485 ops->print_it = signal_catchpoint_print_it;
486 ops->print_one = signal_catchpoint_print_one;
487 ops->print_mention = signal_catchpoint_print_mention;
488 ops->print_recreate = signal_catchpoint_print_recreate;
489 ops->explains_signal = signal_catchpoint_explains_signal;
490}
491
492initialize_file_ftype _initialize_break_catch_sig;
493
494void
495_initialize_break_catch_sig (void)
496{
497 initialize_signal_catchpoint_ops ();
498
499 signal_catch_counts = XCNEWVEC (unsigned int, GDB_SIGNAL_LAST);
500
501 add_catch_command ("signal", _("\
502Catch signals by their names and/or numbers.\n\
503Usage: catch signal [[NAME|NUMBER] [NAME|NUMBER]...|all]\n\
504Arguments say which signals to catch. If no arguments\n\
505are given, every \"normal\" signal will be caught.\n\
506The argument \"all\" means to also catch signals used by GDB.\n\
507Arguments, if given, should be one or more signal names\n\
508(if your system supports that), or signal numbers."),
509 catch_signal_command,
510 signal_completer,
511 CATCH_PERMANENT,
512 CATCH_TEMPORARY);
513}
This page took 0.296887 seconds and 4 git commands to generate.