GDB (xrefs)
/tmp/gdb-8.1/gdb/break-catch-sig.c
Go to the documentation of this file.
1 /* Everything about signal catchpoints, for GDB.
2 
3  Copyright (C) 2011-2018 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 "infrun.h"
27 #include "annotate.h"
28 #include "valprint.h"
29 #include "cli/cli-utils.h"
30 #include "completer.h"
31 
32 #include <string>
33 
34 #define INTERNAL_SIGNAL(x) ((x) == GDB_SIGNAL_TRAP || (x) == GDB_SIGNAL_INT)
35 
36 /* An instance of this type is used to represent a signal catchpoint.
37  A breakpoint is really of this type iff its ops pointer points to
38  SIGNAL_CATCHPOINT_OPS. */
39 
41 {
42  /* Signal numbers used for the 'catch signal' feature. If no signal
43  has been specified for filtering, it is empty. Otherwise,
44  it holds a list of all signals to be caught. */
45 
46  std::vector<gdb_signal> signals_to_be_caught;
47 
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. */
52 
53  bool catch_all;
54 };
55 
56 /* The breakpoint_ops structure to be used in signal catchpoints. */
57 
59 
60 /* Count of each signal. */
61 
62 static unsigned int *signal_catch_counts;
63 
64 
65 
66 /* A convenience wrapper for gdb_signal_to_name that returns the
67  integer value if the name is not known. */
68 
69 static const char *
70 signal_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 
81 
82 /* Implement the "insert_location" breakpoint_ops method for signal
83  catchpoints. */
84 
85 static int
87 {
88  struct signal_catchpoint *c = (struct signal_catchpoint *) bl->owner;
89 
90  if (!c->signals_to_be_caught.empty ())
91  {
92  for (gdb_signal iter : c->signals_to_be_caught)
93  ++signal_catch_counts[iter];
94  }
95  else
96  {
97  for (int i = 0; i < GDB_SIGNAL_LAST; ++i)
98  {
99  if (c->catch_all || !INTERNAL_SIGNAL (i))
100  ++signal_catch_counts[i];
101  }
102  }
103 
105 
106  return 0;
107 }
108 
109 /* Implement the "remove_location" breakpoint_ops method for signal
110  catchpoints. */
111 
112 static int
114  enum remove_bp_reason reason)
115 {
116  struct signal_catchpoint *c = (struct signal_catchpoint *) bl->owner;
117 
118  if (!c->signals_to_be_caught.empty ())
119  {
120  for (gdb_signal iter : c->signals_to_be_caught)
121  {
122  gdb_assert (signal_catch_counts[iter] > 0);
123  --signal_catch_counts[iter];
124  }
125  }
126  else
127  {
128  for (int i = 0; i < GDB_SIGNAL_LAST; ++i)
129  {
130  if (c->catch_all || !INTERNAL_SIGNAL (i))
131  {
133  --signal_catch_counts[i];
134  }
135  }
136  }
137 
139 
140  return 0;
141 }
142 
143 /* Implement the "breakpoint_hit" breakpoint_ops method for signal
144  catchpoints. */
145 
146 static int
148  const address_space *aspace,
149  CORE_ADDR bp_addr,
150  const struct target_waitstatus *ws)
151 {
152  const struct signal_catchpoint *c
153  = (const struct signal_catchpoint *) bl->owner;
154  gdb_signal signal_number;
155 
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. */
164  if (!c->signals_to_be_caught.empty ())
165  {
166  for (gdb_signal iter : c->signals_to_be_caught)
167  if (signal_number == iter)
168  return 1;
169  /* Not the same. */
170  return 0;
171  }
172  else
173  return c->catch_all || !INTERNAL_SIGNAL (signal_number);
174 }
175 
176 /* Implement the "print_it" breakpoint_ops method for signal
177  catchpoints. */
178 
179 static enum print_stop_action
181 {
182  struct breakpoint *b = bs->breakpoint_at;
183  ptid_t ptid;
184  struct target_waitstatus last;
185  const char *signal_name;
186  struct ui_out *uiout = current_uiout;
187 
188  get_last_target_status (&ptid, &last);
189 
190  signal_name = signal_to_name_or_int (last.value.sig);
191 
194 
195  printf_filtered (_("Catchpoint %d (signal %s), "), b->number, signal_name);
196 
197  return PRINT_SRC_AND_LOC;
198 }
199 
200 /* Implement the "print_one" breakpoint_ops method for signal
201  catchpoints. */
202 
203 static void
205  struct bp_location **last_loc)
206 {
207  struct signal_catchpoint *c = (struct signal_catchpoint *) b;
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)
217  uiout->field_skip ("addr");
218  annotate_field (5);
219 
220  if (c->signals_to_be_caught.size () > 1)
221  uiout->text ("signals \"");
222  else
223  uiout->text ("signal \"");
224 
225  if (!c->signals_to_be_caught.empty ())
226  {
228 
229  bool first = true;
230  for (gdb_signal iter : c->signals_to_be_caught)
231  {
232  const char *name = signal_to_name_or_int (iter);
233 
234  if (!first)
235  text += " ";
236  first = false;
237 
238  text += name;
239  }
240  uiout->field_string ("what", text.c_str ());
241  }
242  else
243  uiout->field_string ("what",
244  c->catch_all ? "<any signal>" : "<standard signals>");
245  uiout->text ("\" ");
246 
247  if (uiout->is_mi_like_p ())
248  uiout->field_string ("catch-type", "signal");
249 }
250 
251 /* Implement the "print_mention" breakpoint_ops method for signal
252  catchpoints. */
253 
254 static void
256 {
257  struct signal_catchpoint *c = (struct signal_catchpoint *) b;
258 
259  if (!c->signals_to_be_caught.empty ())
260  {
261  if (c->signals_to_be_caught.size () > 1)
262  printf_filtered (_("Catchpoint %d (signals"), b->number);
263  else
264  printf_filtered (_("Catchpoint %d (signal"), b->number);
265 
266  for (gdb_signal iter : c->signals_to_be_caught)
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 
283 static void
285 {
286  struct signal_catchpoint *c = (struct signal_catchpoint *) b;
287 
288  fprintf_unfiltered (fp, "catch signal");
289 
290  if (!c->signals_to_be_caught.empty ())
291  {
292  for (gdb_signal iter : c->signals_to_be_caught)
293  fprintf_unfiltered (fp, " %s", signal_to_name_or_int (iter));
294  }
295  else if (c->catch_all)
296  fprintf_unfiltered (fp, " all");
297  fputc_unfiltered ('\n', fp);
298 }
299 
300 /* Implement the "explains_signal" breakpoint_ops method for signal
301  catchpoints. */
302 
303 static int
304 signal_catchpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
305 {
306  return 1;
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
311  can be empty, meaning all signals. CATCH_ALL is a flag indicating
312  whether signals used internally by gdb should be caught; it is only
313  valid if FILTER is NULL. If FILTER is empty and CATCH_ALL is zero,
314  then internal signals like SIGTRAP are not caught. */
315 
316 static void
317 create_signal_catchpoint (int tempflag, std::vector<gdb_signal> &&filter,
318  bool catch_all)
319 {
320  struct gdbarch *gdbarch = get_current_arch ();
321 
322  std::unique_ptr<signal_catchpoint> c (new signal_catchpoint ());
323  init_catchpoint (c.get (), gdbarch, tempflag, NULL, &signal_catchpoint_ops);
324  c->signals_to_be_caught = std::move (filter);
325  c->catch_all = catch_all;
326 
327  install_breakpoint (0, std::move (c), 1);
328 }
329 
330 
331 /* Splits the argument using space as delimiter. Returns a filter
332  list, which is empty if no filtering is required. */
333 
334 static std::vector<gdb_signal>
335 catch_signal_split_args (const char *arg, bool *catch_all)
336 {
337  std::vector<gdb_signal> result;
338  bool first = true;
339 
340  while (*arg != '\0')
341  {
342  int num;
343  gdb_signal signal_number;
344  char *endptr;
345 
346  std::string one_arg = extract_arg (&arg);
347  if (one_arg.empty ())
348  break;
349 
350  /* Check for the special flag "all". */
351  if (one_arg == "all")
352  {
353  arg = skip_spaces (arg);
354  if (*arg != '\0' || !first)
355  error (_("'all' cannot be caught with other signals"));
356  *catch_all = true;
357  gdb_assert (result.empty ());
358  return result;
359  }
360 
361  first = false;
362 
363  /* Check if the user provided a signal name or a number. */
364  num = (int) strtol (one_arg.c_str (), &endptr, 0);
365  if (*endptr == '\0')
366  signal_number = gdb_signal_from_command (num);
367  else
368  {
369  signal_number = gdb_signal_from_name (one_arg.c_str ());
370  if (signal_number == GDB_SIGNAL_UNKNOWN)
371  error (_("Unknown signal name '%s'."), one_arg.c_str ());
372  }
373 
374  result.push_back (signal_number);
375  }
376 
377  result.shrink_to_fit ();
378  return result;
379 }
380 
381 /* Implement the "catch signal" command. */
382 
383 static void
384 catch_signal_command (const char *arg, int from_tty,
385  struct cmd_list_element *command)
386 {
387  int tempflag;
388  bool catch_all = false;
389  std::vector<gdb_signal> filter;
390 
391  tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
392 
393  arg = skip_spaces (arg);
394 
395  /* The allowed syntax is:
396  catch signal
397  catch signal <name | number> [<name | number> ... <name | number>]
398 
399  Let's check if there's a signal name. */
400 
401  if (arg != NULL)
402  filter = catch_signal_split_args (arg, &catch_all);
403 
404  create_signal_catchpoint (tempflag, std::move (filter), catch_all);
405 }
406 
407 static void
409 {
410  struct breakpoint_ops *ops;
411 
413 
414  ops = &signal_catchpoint_ops;
415  *ops = base_breakpoint_ops;
424 }
425 
426 void
428 {
430 
431  signal_catch_counts = XCNEWVEC (unsigned int, GDB_SIGNAL_LAST);
432 
433  add_catch_command ("signal", _("\
434 Catch signals by their names and/or numbers.\n\
435 Usage: catch signal [[NAME|NUMBER] [NAME|NUMBER]...|all]\n\
436 Arguments say which signals to catch. If no arguments\n\
437 are given, every \"normal\" signal will be caught.\n\
438 The argument \"all\" means to also catch signals used by GDB.\n\
439 Arguments, if given, should be one or more signal names\n\
440 (if your system supports that), or signal numbers."),
445 }
void get_last_target_status(ptid_t *ptidp, struct target_waitstatus *status)
Definition: infrun.c:4037
const char * string
Definition: signals.c:50
void(* print_recreate)(struct breakpoint *, struct ui_file *fp)
Definition: breakpoint.h:593
void * get_cmd_context(struct cmd_list_element *cmd)
Definition: cli-decode.c:148
static unsigned int * signal_catch_counts
void signal_catch_update(const unsigned int *info)
Definition: infrun.c:8430
void field_string(const char *fldname, const char *string)
Definition: ui-out.c:544
bfd_vma CORE_ADDR
Definition: common-types.h:41
enum gdb_signal gdb_signal_from_command(int num)
Definition: infrun.c:8666
if(!(yy_init))
Definition: ada-lex.c:1075
#define CATCH_TEMPORARY
Definition: breakpoint.h:1283
enum print_stop_action(* print_it)(struct bpstats *bs)
Definition: breakpoint.h:568
void install_breakpoint(int internal, std::unique_ptr< breakpoint > &&arg, int update_gll)
Definition: breakpoint.c:8256
void field_skip(const char *fldname)
Definition: ui-out.c:532
void annotate_field(int num)
Definition: annotate.c:177
std::string extract_arg(const char **arg)
Definition: cli-utils.c:259
static void signal_catchpoint_print_one(struct breakpoint *b, struct bp_location **last_loc)
void(* print_mention)(struct breakpoint *)
Definition: breakpoint.h:590
union target_waitstatus::@174 value
static void create_signal_catchpoint(int tempflag, std::vector< gdb_signal > &&filter, bool catch_all)
char * skip_spaces(char *chp)
Definition: common-utils.c:337
#define _(String)
Definition: gdb_locale.h:35
#define INTERNAL_SIGNAL(x)
void text(const char *string)
Definition: ui-out.c:581
static int signal_catchpoint_explains_signal(struct breakpoint *b, enum gdb_signal sig)
void printf_filtered(const char *format,...)
Definition: utils.c:2045
void signal_completer(struct cmd_list_element *ignore, completion_tracker &tracker, const char *text, const char *word)
Definition: completer.c:1671
#define XCNEWVEC(T, N)
Definition: poison.h:157
int(* insert_location)(struct bp_location *)
Definition: breakpoint.h:533
static int signal_catchpoint_remove_location(struct bp_location *bl, enum remove_bp_reason reason)
const char *const name
Definition: aarch64-tdep.c:76
static void catch_signal_command(const char *arg, int from_tty, struct cmd_list_element *command)
int fputc_unfiltered(int c, struct ui_file *stream)
Definition: utils.c:1835
enum gdb_signal sig
Definition: waitstatus.h:114
Definition: ptid.h:35
void fprintf_unfiltered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2018
int(* breakpoint_hit)(const struct bp_location *bl, const address_space *aspace, CORE_ADDR bp_addr, const struct target_waitstatus *ws)
Definition: breakpoint.h:547
#define current_uiout
Definition: ui-out.h:39
#define CATCH_PERMANENT
Definition: breakpoint.h:1282
static const char * signal_to_name_or_int(enum gdb_signal sig)
struct gdbarch * get_current_arch(void)
Definition: arch-utils.c:798
static int signal_catchpoint_breakpoint_hit(const struct bp_location *bl, const address_space *aspace, CORE_ADDR bp_addr, const struct target_waitstatus *ws)
const char * gdb_signal_to_name(enum gdb_signal)
Definition: signals.c:78
int(* explains_signal)(struct breakpoint *, enum gdb_signal)
Definition: breakpoint.h:633
#define gdb_assert(expr)
Definition: gdb_assert.h:32
Definition: ui-out.h:77
struct breakpoint * breakpoint_at
Definition: breakpoint.h:1122
enum gdb_signal gdb_signal_from_name(const char *)
Definition: signals.c:91
print_stop_action
Definition: breakpoint.h:498
static void signal_catchpoint_print_mention(struct breakpoint *b)
static void signal_catchpoint_print_recreate(struct breakpoint *b, struct ui_file *fp)
int(* remove_location)(struct bp_location *, enum remove_bp_reason reason)
Definition: breakpoint.h:539
enum target_waitkind kind
Definition: waitstatus.h:106
void add_catch_command(const char *name, const char *docstring, cmd_const_sfunc_ftype *sfunc, completer_ftype *completer, void *user_data_catch, void *user_data_tcatch)
Definition: breakpoint.c:15317
std::vector< gdb_signal > signals_to_be_caught
static enum print_stop_action signal_catchpoint_print_it(bpstat bs)
void get_user_print_options(struct value_print_options *opts)
Definition: valprint.c:120
struct breakpoint_ops base_breakpoint_ops
Definition: breakpoint.c:12476
void initialize_breakpoint_ops(void)
Definition: breakpoint.c:15414
static int signal_catchpoint_insert_location(struct bp_location *bl)
static std::vector< gdb_signal > catch_signal_split_args(const char *arg, bool *catch_all)
void annotate_catchpoint(int num)
Definition: annotate.c:87
breakpoint * owner
Definition: breakpoint.h:341
static void initialize_signal_catchpoint_ops(void)
void(* print_one)(struct breakpoint *, struct bp_location **)
Definition: breakpoint.h:572
void maybe_print_thread_hit_breakpoint(struct ui_out *uiout)
Definition: breakpoint.c:4532
void error(const char *fmt,...)
Definition: errors.c:38
remove_bp_reason
Definition: breakpoint.h:47
static struct breakpoint_ops signal_catchpoint_ops
void _initialize_break_catch_sig(void)
bool is_mi_like_p()
Definition: ui-out.c:622
void init_catchpoint(struct breakpoint *b, struct gdbarch *gdbarch, int tempflag, const char *cond_string, const struct breakpoint_ops *ops)
Definition: breakpoint.c:8241
char * filter
Definition: breakpoint.h:735