gdb/mi/mi-cmd-break.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* MI Command Set - breakpoint and watchpoint commands.
  2.    Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Solutions (a Red Hat company).

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"
  16. #include "arch-utils.h"
  17. #include "mi-cmds.h"
  18. #include "ui-out.h"
  19. #include "mi-out.h"
  20. #include "breakpoint.h"
  21. #include "mi-getopt.h"
  22. #include "gdb.h"
  23. #include "observer.h"
  24. #include "mi-main.h"
  25. #include "mi-cmd-break.h"
  26. #include "gdb_obstack.h"
  27. #include <ctype.h>

  28. enum
  29.   {
  30.     FROM_TTY = 0
  31.   };

  32. /* True if MI breakpoint observers have been registered.  */

  33. static int mi_breakpoint_observers_installed;

  34. /* Control whether breakpoint_notify may act.  */

  35. static int mi_can_breakpoint_notify;

  36. /* Output a single breakpoint, when allowed.  */

  37. static void
  38. breakpoint_notify (struct breakpoint *b)
  39. {
  40.   if (mi_can_breakpoint_notify)
  41.     gdb_breakpoint_query (current_uiout, b->number, NULL);
  42. }

  43. enum bp_type
  44.   {
  45.     REG_BP,
  46.     HW_BP,
  47.     REGEXP_BP
  48.   };

  49. /* Arrange for all new breakpoints and catchpoints to be reported to
  50.    CURRENT_UIOUT until the cleanup returned by this function is run.

  51.    Note that MI output will be probably invalid if more than one
  52.    breakpoint is created inside one MI command.  */

  53. struct cleanup *
  54. setup_breakpoint_reporting (void)
  55. {
  56.   struct cleanup *rev_flag;

  57.   if (! mi_breakpoint_observers_installed)
  58.     {
  59.       observer_attach_breakpoint_created (breakpoint_notify);
  60.       mi_breakpoint_observers_installed = 1;
  61.     }

  62.   rev_flag = make_cleanup_restore_integer (&mi_can_breakpoint_notify);
  63.   mi_can_breakpoint_notify = 1;

  64.   return rev_flag;
  65. }


  66. /* Convert arguments in ARGV to the string in "format",argv,argv...
  67.    and return it.  */

  68. static char *
  69. mi_argv_to_format (char **argv, int argc)
  70. {
  71.   int i;
  72.   struct obstack obstack;
  73.   char *ret;

  74.   obstack_init (&obstack);

  75.   /* Convert ARGV[OIND + 1] to format string and save to FORMAT.  */
  76.   obstack_1grow (&obstack, '\"');
  77.   for (i = 0; i < strlen (argv[0]); i++)
  78.     {
  79.       switch (argv[0][i])
  80.         {
  81.         case '\\':
  82.           obstack_grow (&obstack, "\\\\", 2);
  83.           break;
  84.         case '\a':
  85.           obstack_grow (&obstack, "\\a", 2);
  86.           break;
  87.         case '\b':
  88.           obstack_grow (&obstack, "\\b", 2);
  89.           break;
  90.         case '\f':
  91.           obstack_grow (&obstack, "\\f", 2);
  92.           break;
  93.         case '\n':
  94.           obstack_grow (&obstack, "\\n", 2);
  95.           break;
  96.         case '\r':
  97.           obstack_grow (&obstack, "\\r", 2);
  98.           break;
  99.         case '\t':
  100.           obstack_grow (&obstack, "\\t", 2);
  101.           break;
  102.         case '\v':
  103.           obstack_grow (&obstack, "\\v", 2);
  104.           break;
  105.         case '"':
  106.           obstack_grow (&obstack, "\\\"", 2);
  107.           break;
  108.         default:
  109.           if (isprint (argv[0][i]))
  110.             obstack_grow (&obstack, argv[0] + i, 1);
  111.           else
  112.             {
  113.               char tmp[5];

  114.               xsnprintf (tmp, sizeof (tmp), "\\%o",
  115.                          (unsigned char) argv[0][i]);
  116.               obstack_grow (&obstack, tmp, strlen (tmp));
  117.             }
  118.           break;
  119.         }
  120.     }
  121.   obstack_1grow (&obstack, '\"');

  122.   /* Apply other argv to FORMAT.  */
  123.   for (i = 1; i < argc; i++)
  124.     {
  125.       obstack_1grow (&obstack, ',');
  126.       obstack_grow (&obstack, argv[i], strlen (argv[i]));
  127.     }
  128.   obstack_1grow (&obstack, '\0');

  129.   ret = xstrdup (obstack_finish (&obstack));
  130.   obstack_free (&obstack, NULL);

  131.   return ret;
  132. }

  133. /* Insert breakpoint.
  134.    If dprintf is true, it will insert dprintf.
  135.    If not, it will insert other type breakpoint.  */

  136. static void
  137. mi_cmd_break_insert_1 (int dprintf, char *command, char **argv, int argc)
  138. {
  139.   char *address = NULL;
  140.   int hardware = 0;
  141.   int temp_p = 0;
  142.   int thread = -1;
  143.   int ignore_count = 0;
  144.   char *condition = NULL;
  145.   int pending = 0;
  146.   int enabled = 1;
  147.   int tracepoint = 0;
  148.   struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
  149.   enum bptype type_wanted;
  150.   struct breakpoint_ops *ops;
  151.   char *extra_string = NULL;

  152.   enum opt
  153.     {
  154.       HARDWARE_OPT, TEMP_OPT, CONDITION_OPT,
  155.       IGNORE_COUNT_OPT, THREAD_OPT, PENDING_OPT, DISABLE_OPT,
  156.       TRACEPOINT_OPT,
  157.     };
  158.   static const struct mi_opt opts[] =
  159.   {
  160.     {"h", HARDWARE_OPT, 0},
  161.     {"t", TEMP_OPT, 0},
  162.     {"c", CONDITION_OPT, 1},
  163.     {"i", IGNORE_COUNT_OPT, 1},
  164.     {"p", THREAD_OPT, 1},
  165.     {"f", PENDING_OPT, 0},
  166.     {"d", DISABLE_OPT, 0},
  167.     {"a", TRACEPOINT_OPT, 0},
  168.     { 0, 0, 0 }
  169.   };

  170.   /* Parse arguments. It could be -r or -h or -t, <location> or ``--''
  171.      to denote the end of the option list. */
  172.   int oind = 0;
  173.   char *oarg;

  174.   while (1)
  175.     {
  176.       int opt = mi_getopt ("-break-insert", argc, argv,
  177.                            opts, &oind, &oarg);
  178.       if (opt < 0)
  179.         break;
  180.       switch ((enum opt) opt)
  181.         {
  182.         case TEMP_OPT:
  183.           temp_p = 1;
  184.           break;
  185.         case HARDWARE_OPT:
  186.           hardware = 1;
  187.           break;
  188.         case CONDITION_OPT:
  189.           condition = oarg;
  190.           break;
  191.         case IGNORE_COUNT_OPT:
  192.           ignore_count = atol (oarg);
  193.           break;
  194.         case THREAD_OPT:
  195.           thread = atol (oarg);
  196.           break;
  197.         case PENDING_OPT:
  198.           pending = 1;
  199.           break;
  200.         case DISABLE_OPT:
  201.           enabled = 0;
  202.           break;
  203.         case TRACEPOINT_OPT:
  204.           tracepoint = 1;
  205.           break;
  206.         }
  207.     }

  208.   if (oind >= argc)
  209.     error (_("-%s-insert: Missing <location>"),
  210.            dprintf ? "dprintf" : "break");
  211.   address = argv[oind];
  212.   if (dprintf)
  213.     {
  214.       int format_num = oind + 1;

  215.       if (hardware || tracepoint)
  216.         error (_("-dprintf-insert: does not support -h or -a"));
  217.       if (format_num >= argc)
  218.         error (_("-dprintf-insert: Missing <format>"));

  219.       extra_string = mi_argv_to_format (argv + format_num, argc - format_num);
  220.       make_cleanup (xfree, extra_string);
  221.     }
  222.   else
  223.     {
  224.       if (oind < argc - 1)
  225.         error (_("-break-insert: Garbage following <location>"));
  226.     }

  227.   /* Now we have what we need, let's insert the breakpoint!  */
  228.   setup_breakpoint_reporting ();

  229.   if (tracepoint)
  230.     {
  231.       /* Note that to request a fast tracepoint, the client uses the
  232.          "hardware" flag, although there's nothing of hardware related to
  233.          fast tracepoints -- one can implement slow tracepoints with
  234.          hardware breakpoints, but fast tracepoints are always software.
  235.          "fast" is a misnomer, actually, "jump" would be more appropriate.
  236.          A simulator or an emulator could conceivably implement fast
  237.          regular non-jump based tracepoints.  */
  238.       type_wanted = hardware ? bp_fast_tracepoint : bp_tracepoint;
  239.       ops = &tracepoint_breakpoint_ops;
  240.     }
  241.   else if (dprintf)
  242.     {
  243.       type_wanted = bp_dprintf;
  244.       ops = &dprintf_breakpoint_ops;
  245.     }
  246.   else
  247.     {
  248.       type_wanted = hardware ? bp_hardware_breakpoint : bp_breakpoint;
  249.       ops = &bkpt_breakpoint_ops;
  250.     }

  251.   create_breakpoint (get_current_arch (), address, condition, thread,
  252.                      extra_string,
  253.                      0 /* condition and thread are valid.  */,
  254.                      temp_p, type_wanted,
  255.                      ignore_count,
  256.                      pending ? AUTO_BOOLEAN_TRUE : AUTO_BOOLEAN_FALSE,
  257.                      ops, 0, enabled, 0, 0);
  258.   do_cleanups (back_to);
  259. }

  260. /* Implements the -break-insert command.
  261.    See the MI manual for the list of possible options.  */

  262. void
  263. mi_cmd_break_insert (char *command, char **argv, int argc)
  264. {
  265.   mi_cmd_break_insert_1 (0, command, argv, argc);
  266. }

  267. /* Implements the -dprintf-insert command.
  268.    See the MI manual for the list of possible options.  */

  269. void
  270. mi_cmd_dprintf_insert (char *command, char **argv, int argc)
  271. {
  272.   mi_cmd_break_insert_1 (1, command, argv, argc);
  273. }

  274. enum wp_type
  275. {
  276.   REG_WP,
  277.   READ_WP,
  278.   ACCESS_WP
  279. };

  280. void
  281. mi_cmd_break_passcount (char *command, char **argv, int argc)
  282. {
  283.   int n;
  284.   int p;
  285.   struct tracepoint *t;

  286.   if (argc != 2)
  287.     error (_("Usage: tracepoint-number passcount"));

  288.   n = atoi (argv[0]);
  289.   p = atoi (argv[1]);
  290.   t = get_tracepoint (n);

  291.   if (t)
  292.     {
  293.       t->pass_count = p;
  294.       observer_notify_breakpoint_modified (&t->base);
  295.     }
  296.   else
  297.     {
  298.       error (_("Could not find tracepoint %d"), n);
  299.     }
  300. }

  301. /* Insert a watchpoint. The type of watchpoint is specified by the
  302.    first argument:
  303.    -break-watch <expr> --> insert a regular wp.
  304.    -break-watch -r <expr> --> insert a read watchpoint.
  305.    -break-watch -a <expr> --> insert an access wp.  */

  306. void
  307. mi_cmd_break_watch (char *command, char **argv, int argc)
  308. {
  309.   char *expr = NULL;
  310.   enum wp_type type = REG_WP;
  311.   enum opt
  312.     {
  313.       READ_OPT, ACCESS_OPT
  314.     };
  315.   static const struct mi_opt opts[] =
  316.   {
  317.     {"r", READ_OPT, 0},
  318.     {"a", ACCESS_OPT, 0},
  319.     { 0, 0, 0 }
  320.   };

  321.   /* Parse arguments. */
  322.   int oind = 0;
  323.   char *oarg;

  324.   while (1)
  325.     {
  326.       int opt = mi_getopt ("-break-watch", argc, argv,
  327.                            opts, &oind, &oarg);

  328.       if (opt < 0)
  329.         break;
  330.       switch ((enum opt) opt)
  331.         {
  332.         case READ_OPT:
  333.           type = READ_WP;
  334.           break;
  335.         case ACCESS_OPT:
  336.           type = ACCESS_WP;
  337.           break;
  338.         }
  339.     }
  340.   if (oind >= argc)
  341.     error (_("-break-watch: Missing <expression>"));
  342.   if (oind < argc - 1)
  343.     error (_("-break-watch: Garbage following <expression>"));
  344.   expr = argv[oind];

  345.   /* Now we have what we need, let's insert the watchpoint!  */
  346.   switch (type)
  347.     {
  348.     case REG_WP:
  349.       watch_command_wrapper (expr, FROM_TTY, 0);
  350.       break;
  351.     case READ_WP:
  352.       rwatch_command_wrapper (expr, FROM_TTY, 0);
  353.       break;
  354.     case ACCESS_WP:
  355.       awatch_command_wrapper (expr, FROM_TTY, 0);
  356.       break;
  357.     default:
  358.       error (_("-break-watch: Unknown watchpoint type."));
  359.     }
  360. }

  361. /* The mi_read_next_line consults these variable to return successive
  362.    command lines.  While it would be clearer to use a closure pointer,
  363.    it is not expected that any future code will use read_command_lines_1,
  364.    therefore no point of overengineering.  */

  365. static char **mi_command_line_array;
  366. static int mi_command_line_array_cnt;
  367. static int mi_command_line_array_ptr;

  368. static char *
  369. mi_read_next_line (void)
  370. {
  371.   if (mi_command_line_array_ptr == mi_command_line_array_cnt)
  372.     return NULL;
  373.   else
  374.     return mi_command_line_array[mi_command_line_array_ptr++];
  375. }

  376. void
  377. mi_cmd_break_commands (char *command, char **argv, int argc)
  378. {
  379.   struct command_line *break_command;
  380.   char *endptr;
  381.   int bnum;
  382.   struct breakpoint *b;

  383.   if (argc < 1)
  384.     error (_("USAGE: %s <BKPT> [<COMMAND> [<COMMAND>...]]"), command);

  385.   bnum = strtol (argv[0], &endptr, 0);
  386.   if (endptr == argv[0])
  387.     error (_("breakpoint number argument \"%s\" is not a number."),
  388.            argv[0]);
  389.   else if (*endptr != '\0')
  390.     error (_("junk at the end of breakpoint number argument \"%s\"."),
  391.            argv[0]);

  392.   b = get_breakpoint (bnum);
  393.   if (b == NULL)
  394.     error (_("breakpoint %d not found."), bnum);

  395.   mi_command_line_array = argv;
  396.   mi_command_line_array_ptr = 1;
  397.   mi_command_line_array_cnt = argc;

  398.   if (is_tracepoint (b))
  399.     break_command = read_command_lines_1 (mi_read_next_line, 1,
  400.                                           check_tracepoint_command, b);
  401.   else
  402.     break_command = read_command_lines_1 (mi_read_next_line, 1, 0, 0);

  403.   breakpoint_set_commands (b, break_command);
  404. }