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