gdb/break-catch-sig.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Everything about signal catchpoints, for GDB.

  2.    Copyright (C) 2011-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "arch-utils.h"
  16. #include <ctype.h>
  17. #include "breakpoint.h"
  18. #include "gdbcmd.h"
  19. #include "inferior.h"
  20. #include "infrun.h"
  21. #include "annotate.h"
  22. #include "valprint.h"
  23. #include "cli/cli-utils.h"
  24. #include "completer.h"
  25. #include "gdb_obstack.h"

  26. #define INTERNAL_SIGNAL(x) ((x) == GDB_SIGNAL_TRAP || (x) == GDB_SIGNAL_INT)

  27. typedef enum gdb_signal gdb_signal_type;

  28. DEF_VEC_I (gdb_signal_type);

  29. /* An instance of this type is used to represent a signal catchpoint.
  30.    It includes a "struct breakpoint" as a kind of base class; users
  31.    downcast to "struct breakpoint *" when needed.  A breakpoint is
  32.    really of this type iff its ops pointer points to
  33.    SIGNAL_CATCHPOINT_OPS.  */

  34. struct signal_catchpoint
  35. {
  36.   /* The base class.  */

  37.   struct breakpoint base;

  38.   /* Signal numbers used for the 'catch signal' feature.  If no signal
  39.      has been specified for filtering, its value is NULL.  Otherwise,
  40.      it holds a list of all signals to be caught.  */

  41.   VEC (gdb_signal_type) *signals_to_be_caught;

  42.   /* If SIGNALS_TO_BE_CAUGHT is NULL, then all "ordinary" signals are
  43.      caught.  If CATCH_ALL is non-zero, then internal signals are
  44.      caught as well.  If SIGNALS_TO_BE_CAUGHT is non-NULL, then this
  45.      field is ignored.  */

  46.   int catch_all;
  47. };

  48. /* The breakpoint_ops structure to be used in signal catchpoints.  */

  49. static struct breakpoint_ops signal_catchpoint_ops;

  50. /* Count of each signal.  */

  51. static unsigned int *signal_catch_counts;



  52. /* A convenience wrapper for gdb_signal_to_name that returns the
  53.    integer value if the name is not known.  */

  54. static const char *
  55. signal_to_name_or_int (enum gdb_signal sig)
  56. {
  57.   const char *result = gdb_signal_to_name (sig);

  58.   if (strcmp (result, "?") == 0)
  59.     result = plongest (sig);

  60.   return result;
  61. }



  62. /* Implement the "dtor" breakpoint_ops method for signal
  63.    catchpoints.  */

  64. static void
  65. signal_catchpoint_dtor (struct breakpoint *b)
  66. {
  67.   struct signal_catchpoint *c = (struct signal_catchpoint *) b;

  68.   VEC_free (gdb_signal_type, c->signals_to_be_caught);

  69.   base_breakpoint_ops.dtor (b);
  70. }

  71. /* Implement the "insert_location" breakpoint_ops method for signal
  72.    catchpoints.  */

  73. static int
  74. signal_catchpoint_insert_location (struct bp_location *bl)
  75. {
  76.   struct signal_catchpoint *c = (void *) bl->owner;
  77.   int i;

  78.   if (c->signals_to_be_caught != NULL)
  79.     {
  80.       gdb_signal_type iter;

  81.       for (i = 0;
  82.            VEC_iterate (gdb_signal_type, c->signals_to_be_caught, i, iter);
  83.            i++)
  84.         ++signal_catch_counts[iter];
  85.     }
  86.   else
  87.     {
  88.       for (i = 0; i < GDB_SIGNAL_LAST; ++i)
  89.         {
  90.           if (c->catch_all || !INTERNAL_SIGNAL (i))
  91.             ++signal_catch_counts[i];
  92.         }
  93.     }

  94.   signal_catch_update (signal_catch_counts);

  95.   return 0;
  96. }

  97. /* Implement the "remove_location" breakpoint_ops method for signal
  98.    catchpoints.  */

  99. static int
  100. signal_catchpoint_remove_location (struct bp_location *bl)
  101. {
  102.   struct signal_catchpoint *c = (void *) bl->owner;
  103.   int i;

  104.   if (c->signals_to_be_caught != NULL)
  105.     {
  106.       gdb_signal_type iter;

  107.       for (i = 0;
  108.            VEC_iterate (gdb_signal_type, c->signals_to_be_caught, i, iter);
  109.            i++)
  110.         {
  111.           gdb_assert (signal_catch_counts[iter] > 0);
  112.           --signal_catch_counts[iter];
  113.         }
  114.     }
  115.   else
  116.     {
  117.       for (i = 0; i < GDB_SIGNAL_LAST; ++i)
  118.         {
  119.           if (c->catch_all || !INTERNAL_SIGNAL (i))
  120.             {
  121.               gdb_assert (signal_catch_counts[i] > 0);
  122.               --signal_catch_counts[i];
  123.             }
  124.         }
  125.     }

  126.   signal_catch_update (signal_catch_counts);

  127.   return 0;
  128. }

  129. /* Implement the "breakpoint_hit" breakpoint_ops method for signal
  130.    catchpoints.  */

  131. static int
  132. signal_catchpoint_breakpoint_hit (const struct bp_location *bl,
  133.                                   struct address_space *aspace,
  134.                                   CORE_ADDR bp_addr,
  135.                                   const struct target_waitstatus *ws)
  136. {
  137.   const struct signal_catchpoint *c = (void *) bl->owner;
  138.   gdb_signal_type signal_number;

  139.   if (ws->kind != TARGET_WAITKIND_STOPPED)
  140.     return 0;

  141.   signal_number = ws->value.sig;

  142.   /* If we are catching specific signals in this breakpoint, then we
  143.      must guarantee that the called signal is the same signal we are
  144.      catching.  */
  145.   if (c->signals_to_be_caught)
  146.     {
  147.       int i;
  148.       gdb_signal_type iter;

  149.       for (i = 0;
  150.            VEC_iterate (gdb_signal_type, c->signals_to_be_caught, i, iter);
  151.            i++)
  152.         if (signal_number == iter)
  153.           return 1;
  154.       /* Not the same.  */
  155.       gdb_assert (!iter);
  156.       return 0;
  157.     }
  158.   else
  159.     return c->catch_all || !INTERNAL_SIGNAL (signal_number);
  160. }

  161. /* Implement the "print_it" breakpoint_ops method for signal
  162.    catchpoints.  */

  163. static enum print_stop_action
  164. signal_catchpoint_print_it (bpstat bs)
  165. {
  166.   struct breakpoint *b = bs->breakpoint_at;
  167.   ptid_t ptid;
  168.   struct target_waitstatus last;
  169.   const char *signal_name;

  170.   get_last_target_status (&ptid, &last);

  171.   signal_name = signal_to_name_or_int (last.value.sig);

  172.   annotate_catchpoint (b->number);

  173.   printf_filtered (_("\nCatchpoint %d (signal %s), "), b->number, signal_name);

  174.   return PRINT_SRC_AND_LOC;
  175. }

  176. /* Implement the "print_one" breakpoint_ops method for signal
  177.    catchpoints.  */

  178. static void
  179. signal_catchpoint_print_one (struct breakpoint *b,
  180.                              struct bp_location **last_loc)
  181. {
  182.   struct signal_catchpoint *c = (void *) b;
  183.   struct value_print_options opts;
  184.   struct ui_out *uiout = current_uiout;

  185.   get_user_print_options (&opts);

  186.   /* Field 4, the address, is omitted (which makes the columns
  187.      not line up too nicely with the headers, but the effect
  188.      is relatively readable).  */
  189.   if (opts.addressprint)
  190.     ui_out_field_skip (uiout, "addr");
  191.   annotate_field (5);

  192.   if (c->signals_to_be_caught
  193.       && VEC_length (gdb_signal_type, c->signals_to_be_caught) > 1)
  194.     ui_out_text (uiout, "signals \"");
  195.   else
  196.     ui_out_text (uiout, "signal \"");

  197.   if (c->signals_to_be_caught)
  198.     {
  199.       int i;
  200.       gdb_signal_type iter;
  201.       struct obstack text;
  202.       struct cleanup *cleanup;

  203.       obstack_init (&text);
  204.       cleanup = make_cleanup_obstack_free (&text);

  205.       for (i = 0;
  206.            VEC_iterate (gdb_signal_type, c->signals_to_be_caught, i, iter);
  207.            i++)
  208.         {
  209.           const char *name = signal_to_name_or_int (iter);

  210.           if (i > 0)
  211.             obstack_grow (&text, " ", 1);
  212.           obstack_grow (&text, name, strlen (name));
  213.         }
  214.       obstack_grow (&text, "", 1);
  215.       ui_out_field_string (uiout, "what", obstack_base (&text));
  216.       do_cleanups (cleanup);
  217.     }
  218.   else
  219.     ui_out_field_string (uiout, "what",
  220.                          c->catch_all ? "<any signal>" : "<standard signals>");
  221.   ui_out_text (uiout, "\" ");

  222.   if (ui_out_is_mi_like_p (uiout))
  223.     ui_out_field_string (uiout, "catch-type", "signal");
  224. }

  225. /* Implement the "print_mention" breakpoint_ops method for signal
  226.    catchpoints.  */

  227. static void
  228. signal_catchpoint_print_mention (struct breakpoint *b)
  229. {
  230.   struct signal_catchpoint *c = (void *) b;

  231.   if (c->signals_to_be_caught)
  232.     {
  233.       int i;
  234.       gdb_signal_type iter;

  235.       if (VEC_length (gdb_signal_type, c->signals_to_be_caught) > 1)
  236.         printf_filtered (_("Catchpoint %d (signals"), b->number);
  237.       else
  238.         printf_filtered (_("Catchpoint %d (signal"), b->number);

  239.       for (i = 0;
  240.            VEC_iterate (gdb_signal_type, c->signals_to_be_caught, i, iter);
  241.            i++)
  242.         {
  243.           const char *name = signal_to_name_or_int (iter);

  244.           printf_filtered (" %s", name);
  245.         }
  246.       printf_filtered (")");
  247.     }
  248.   else if (c->catch_all)
  249.     printf_filtered (_("Catchpoint %d (any signal)"), b->number);
  250.   else
  251.     printf_filtered (_("Catchpoint %d (standard signals)"), b->number);
  252. }

  253. /* Implement the "print_recreate" breakpoint_ops method for signal
  254.    catchpoints.  */

  255. static void
  256. signal_catchpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
  257. {
  258.   struct signal_catchpoint *c = (void *) b;

  259.   fprintf_unfiltered (fp, "catch signal");

  260.   if (c->signals_to_be_caught)
  261.     {
  262.       int i;
  263.       gdb_signal_type iter;

  264.       for (i = 0;
  265.            VEC_iterate (gdb_signal_type, c->signals_to_be_caught, i, iter);
  266.            i++)
  267.         fprintf_unfiltered (fp, " %s", signal_to_name_or_int (iter));
  268.     }
  269.   else if (c->catch_all)
  270.     fprintf_unfiltered (fp, " all");
  271.   fputc_unfiltered ('\n', fp);
  272. }

  273. /* Implement the "explains_signal" breakpoint_ops method for signal
  274.    catchpoints.  */

  275. static int
  276. signal_catchpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
  277. {
  278.   return 1;
  279. }

  280. /* Create a new signal catchpoint.  TEMPFLAG is true if this should be
  281.    a temporary catchpoint.  FILTER is the list of signals to catch; it
  282.    can be NULL, meaning all signals.  CATCH_ALL is a flag indicating
  283.    whether signals used internally by gdb should be caught; it is only
  284.    valid if FILTER is NULL.  If FILTER is NULL and CATCH_ALL is zero,
  285.    then internal signals like SIGTRAP are not caught.  */

  286. static void
  287. create_signal_catchpoint (int tempflag, VEC (gdb_signal_type) *filter,
  288.                           int catch_all)
  289. {
  290.   struct signal_catchpoint *c;
  291.   struct gdbarch *gdbarch = get_current_arch ();

  292.   c = XNEW (struct signal_catchpoint);
  293.   init_catchpoint (&c->base, gdbarch, tempflag, NULL, &signal_catchpoint_ops);
  294.   c->signals_to_be_caught = filter;
  295.   c->catch_all = catch_all;

  296.   install_breakpoint (0, &c->base, 1);
  297. }


  298. /* Splits the argument using space as delimiter.  Returns an xmalloc'd
  299.    filter list, or NULL if no filtering is required.  */

  300. static VEC (gdb_signal_type) *
  301. catch_signal_split_args (char *arg, int *catch_all)
  302. {
  303.   VEC (gdb_signal_type) *result = NULL;
  304.   struct cleanup *cleanup = make_cleanup (VEC_cleanup (gdb_signal_type),
  305.                                           &result);
  306.   int first = 1;

  307.   while (*arg != '\0')
  308.     {
  309.       int num;
  310.       gdb_signal_type signal_number;
  311.       char *one_arg, *endptr;
  312.       struct cleanup *inner_cleanup;

  313.       one_arg = extract_arg (&arg);
  314.       if (one_arg == NULL)
  315.         break;
  316.       inner_cleanup = make_cleanup (xfree, one_arg);

  317.       /* Check for the special flag "all".  */
  318.       if (strcmp (one_arg, "all") == 0)
  319.         {
  320.           arg = skip_spaces (arg);
  321.           if (*arg != '\0' || !first)
  322.             error (_("'all' cannot be caught with other signals"));
  323.           *catch_all = 1;
  324.           gdb_assert (result == NULL);
  325.           do_cleanups (inner_cleanup);
  326.           discard_cleanups (cleanup);
  327.           return NULL;
  328.         }

  329.       first = 0;

  330.       /* Check if the user provided a signal name or a number.  */
  331.       num = (int) strtol (one_arg, &endptr, 0);
  332.       if (*endptr == '\0')
  333.         signal_number = gdb_signal_from_command (num);
  334.       else
  335.         {
  336.           signal_number = gdb_signal_from_name (one_arg);
  337.           if (signal_number == GDB_SIGNAL_UNKNOWN)
  338.             error (_("Unknown signal name '%s'."), one_arg);
  339.         }

  340.       VEC_safe_push (gdb_signal_type, result, signal_number);
  341.       do_cleanups (inner_cleanup);
  342.     }

  343.   discard_cleanups (cleanup);
  344.   return result;
  345. }

  346. /* Implement the "catch signal" command.  */

  347. static void
  348. catch_signal_command (char *arg, int from_tty,
  349.                       struct cmd_list_element *command)
  350. {
  351.   int tempflag, catch_all = 0;
  352.   VEC (gdb_signal_type) *filter;

  353.   tempflag = get_cmd_context (command) == CATCH_TEMPORARY;

  354.   arg = skip_spaces (arg);

  355.   /* The allowed syntax is:
  356.      catch signal
  357.      catch signal <name | number> [<name | number> ... <name | number>]

  358.      Let's check if there's a signal name.  */

  359.   if (arg != NULL)
  360.     filter = catch_signal_split_args (arg, &catch_all);
  361.   else
  362.     filter = NULL;

  363.   create_signal_catchpoint (tempflag, filter, catch_all);
  364. }

  365. static void
  366. initialize_signal_catchpoint_ops (void)
  367. {
  368.   struct breakpoint_ops *ops;

  369.   initialize_breakpoint_ops ();

  370.   ops = &signal_catchpoint_ops;
  371.   *ops = base_breakpoint_ops;
  372.   ops->dtor = signal_catchpoint_dtor;
  373.   ops->insert_location = signal_catchpoint_insert_location;
  374.   ops->remove_location = signal_catchpoint_remove_location;
  375.   ops->breakpoint_hit = signal_catchpoint_breakpoint_hit;
  376.   ops->print_it = signal_catchpoint_print_it;
  377.   ops->print_one = signal_catchpoint_print_one;
  378.   ops->print_mention = signal_catchpoint_print_mention;
  379.   ops->print_recreate = signal_catchpoint_print_recreate;
  380.   ops->explains_signal = signal_catchpoint_explains_signal;
  381. }

  382. initialize_file_ftype _initialize_break_catch_sig;

  383. void
  384. _initialize_break_catch_sig (void)
  385. {
  386.   initialize_signal_catchpoint_ops ();

  387.   signal_catch_counts = XCNEWVEC (unsigned int, GDB_SIGNAL_LAST);

  388.   add_catch_command ("signal", _("\
  389. Catch signals by their names and/or numbers.\n\
  390. Usage: catch signal [[NAME|NUMBER] [NAME|NUMBER]...|all]\n\
  391. Arguments say which signals to catch.  If no arguments\n\
  392. are given, every \"normal\" signal will be caught.\n\
  393. The argument \"all\" means to also catch signals used by GDB.\n\
  394. Arguments, if given, should be one or more signal names\n\
  395. (if your system supports that), or signal numbers."),
  396.                      catch_signal_command,
  397.                      signal_completer,
  398.                      CATCH_PERMANENT,
  399.                      CATCH_TEMPORARY);
  400. }