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