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