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