1 /* MI Command Set - breakpoint and watchpoint commands.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions (a Red Hat company).
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "arch-utils.h"
25 #include "breakpoint.h"
26 #include "mi-getopt.h"
30 #include "mi-cmd-break.h"
34 #include "gdb_obstack.h"
42 /* True if MI breakpoint observers have been registered. */
44 static int mi_breakpoint_observers_installed;
46 /* Control whether breakpoint_notify may act. */
48 static int mi_can_breakpoint_notify;
50 /* Output a single breakpoint, when allowed. */
53 breakpoint_notify (struct breakpoint *b)
55 if (mi_can_breakpoint_notify)
56 gdb_breakpoint_query (current_uiout, b->number, NULL);
66 /* Arrange for all new breakpoints and catchpoints to be reported to
67 CURRENT_UIOUT until the destructor of the returned scoped_restore
70 Note that MI output will be probably invalid if more than one
71 breakpoint is created inside one MI command. */
73 scoped_restore_tmpl<int>
74 setup_breakpoint_reporting (void)
76 if (! mi_breakpoint_observers_installed)
78 observer_attach_breakpoint_created (breakpoint_notify);
79 mi_breakpoint_observers_installed = 1;
82 return make_scoped_restore (&mi_can_breakpoint_notify, 1);
86 /* Convert arguments in ARGV to the string in "format",argv,argv...
90 mi_argv_to_format (char **argv, int argc)
93 struct obstack obstack;
96 obstack_init (&obstack);
98 /* Convert ARGV[OIND + 1] to format string and save to FORMAT. */
99 obstack_1grow (&obstack, '\"');
100 for (i = 0; i < strlen (argv[0]); i++)
105 obstack_grow (&obstack, "\\\\", 2);
108 obstack_grow (&obstack, "\\a", 2);
111 obstack_grow (&obstack, "\\b", 2);
114 obstack_grow (&obstack, "\\f", 2);
117 obstack_grow (&obstack, "\\n", 2);
120 obstack_grow (&obstack, "\\r", 2);
123 obstack_grow (&obstack, "\\t", 2);
126 obstack_grow (&obstack, "\\v", 2);
129 obstack_grow (&obstack, "\\\"", 2);
132 if (isprint (argv[0][i]))
133 obstack_grow (&obstack, argv[0] + i, 1);
138 xsnprintf (tmp, sizeof (tmp), "\\%o",
139 (unsigned char) argv[0][i]);
140 obstack_grow_str (&obstack, tmp);
145 obstack_1grow (&obstack, '\"');
147 /* Apply other argv to FORMAT. */
148 for (i = 1; i < argc; i++)
150 obstack_1grow (&obstack, ',');
151 obstack_grow_str (&obstack, argv[i]);
153 obstack_1grow (&obstack, '\0');
155 ret = xstrdup ((const char *) obstack_finish (&obstack));
156 obstack_free (&obstack, NULL);
161 /* Insert breakpoint.
162 If dprintf is true, it will insert dprintf.
163 If not, it will insert other type breakpoint. */
166 mi_cmd_break_insert_1 (int dprintf, const char *command, char **argv, int argc)
168 char *address = NULL;
172 int ignore_count = 0;
173 char *condition = NULL;
177 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
178 enum bptype type_wanted;
179 event_location_up location;
180 struct breakpoint_ops *ops;
182 struct explicit_location explicit_loc;
183 char *extra_string = NULL;
187 HARDWARE_OPT, TEMP_OPT, CONDITION_OPT,
188 IGNORE_COUNT_OPT, THREAD_OPT, PENDING_OPT, DISABLE_OPT,
190 EXPLICIT_SOURCE_OPT, EXPLICIT_FUNC_OPT,
191 EXPLICIT_LABEL_OPT, EXPLICIT_LINE_OPT
193 static const struct mi_opt opts[] =
195 {"h", HARDWARE_OPT, 0},
197 {"c", CONDITION_OPT, 1},
198 {"i", IGNORE_COUNT_OPT, 1},
199 {"p", THREAD_OPT, 1},
200 {"f", PENDING_OPT, 0},
201 {"d", DISABLE_OPT, 0},
202 {"a", TRACEPOINT_OPT, 0},
203 {"-source" , EXPLICIT_SOURCE_OPT, 1},
204 {"-function", EXPLICIT_FUNC_OPT, 1},
205 {"-label", EXPLICIT_LABEL_OPT, 1},
206 {"-line", EXPLICIT_LINE_OPT, 1},
210 /* Parse arguments. It could be -r or -h or -t, <location> or ``--''
211 to denote the end of the option list. */
215 initialize_explicit_location (&explicit_loc);
219 int opt = mi_getopt ("-break-insert", argc, argv,
223 switch ((enum opt) opt)
234 case IGNORE_COUNT_OPT:
235 ignore_count = atol (oarg);
238 thread = atol (oarg);
249 case EXPLICIT_SOURCE_OPT:
251 explicit_loc.source_filename = oarg;
253 case EXPLICIT_FUNC_OPT:
255 explicit_loc.function_name = oarg;
257 case EXPLICIT_LABEL_OPT:
259 explicit_loc.label_name = oarg;
261 case EXPLICIT_LINE_OPT:
263 explicit_loc.line_offset = linespec_parse_line_offset (oarg);
268 if (oind >= argc && !is_explicit)
269 error (_("-%s-insert: Missing <location>"),
270 dprintf ? "dprintf" : "break");
273 int format_num = is_explicit ? oind : oind + 1;
275 if (hardware || tracepoint)
276 error (_("-dprintf-insert: does not support -h or -a"));
277 if (format_num >= argc)
278 error (_("-dprintf-insert: Missing <format>"));
280 extra_string = mi_argv_to_format (argv + format_num, argc - format_num);
281 make_cleanup (xfree, extra_string);
282 address = argv[oind];
289 error (_("-break-insert: Garbage following explicit location"));
294 error (_("-break-insert: Garbage following <location>"));
295 address = argv[oind];
299 /* Now we have what we need, let's insert the breakpoint! */
300 scoped_restore restore_breakpoint_reporting = setup_breakpoint_reporting ();
304 /* Note that to request a fast tracepoint, the client uses the
305 "hardware" flag, although there's nothing of hardware related to
306 fast tracepoints -- one can implement slow tracepoints with
307 hardware breakpoints, but fast tracepoints are always software.
308 "fast" is a misnomer, actually, "jump" would be more appropriate.
309 A simulator or an emulator could conceivably implement fast
310 regular non-jump based tracepoints. */
311 type_wanted = hardware ? bp_fast_tracepoint : bp_tracepoint;
312 ops = &tracepoint_breakpoint_ops;
316 type_wanted = bp_dprintf;
317 ops = &dprintf_breakpoint_ops;
321 type_wanted = hardware ? bp_hardware_breakpoint : bp_breakpoint;
322 ops = &bkpt_breakpoint_ops;
327 /* Error check -- we must have one of the other
328 parameters specified. */
329 if (explicit_loc.source_filename != NULL
330 && explicit_loc.function_name == NULL
331 && explicit_loc.label_name == NULL
332 && explicit_loc.line_offset.sign == LINE_OFFSET_UNKNOWN)
333 error (_("-%s-insert: --source option requires --function, --label,"
334 " or --line"), dprintf ? "dprintf" : "break");
336 location = new_explicit_location (&explicit_loc);
340 location = string_to_event_location_basic (&address, current_language);
342 error (_("Garbage '%s' at end of location"), address);
345 create_breakpoint (get_current_arch (), location.get (), condition, thread,
347 0 /* condition and thread are valid. */,
350 pending ? AUTO_BOOLEAN_TRUE : AUTO_BOOLEAN_FALSE,
351 ops, 0, enabled, 0, 0);
352 do_cleanups (back_to);
355 /* Implements the -break-insert command.
356 See the MI manual for the list of possible options. */
359 mi_cmd_break_insert (const char *command, char **argv, int argc)
361 mi_cmd_break_insert_1 (0, command, argv, argc);
364 /* Implements the -dprintf-insert command.
365 See the MI manual for the list of possible options. */
368 mi_cmd_dprintf_insert (const char *command, char **argv, int argc)
370 mi_cmd_break_insert_1 (1, command, argv, argc);
381 mi_cmd_break_passcount (const char *command, char **argv, int argc)
385 struct tracepoint *t;
388 error (_("Usage: tracepoint-number passcount"));
392 t = get_tracepoint (n);
397 observer_notify_breakpoint_modified (t);
401 error (_("Could not find tracepoint %d"), n);
405 /* Insert a watchpoint. The type of watchpoint is specified by the
407 -break-watch <expr> --> insert a regular wp.
408 -break-watch -r <expr> --> insert a read watchpoint.
409 -break-watch -a <expr> --> insert an access wp. */
412 mi_cmd_break_watch (const char *command, char **argv, int argc)
415 enum wp_type type = REG_WP;
420 static const struct mi_opt opts[] =
423 {"a", ACCESS_OPT, 0},
427 /* Parse arguments. */
433 int opt = mi_getopt ("-break-watch", argc, argv,
438 switch ((enum opt) opt)
449 error (_("-break-watch: Missing <expression>"));
451 error (_("-break-watch: Garbage following <expression>"));
454 /* Now we have what we need, let's insert the watchpoint! */
458 watch_command_wrapper (expr, FROM_TTY, 0);
461 rwatch_command_wrapper (expr, FROM_TTY, 0);
464 awatch_command_wrapper (expr, FROM_TTY, 0);
467 error (_("-break-watch: Unknown watchpoint type."));
471 /* The mi_read_next_line consults these variable to return successive
472 command lines. While it would be clearer to use a closure pointer,
473 it is not expected that any future code will use read_command_lines_1,
474 therefore no point of overengineering. */
476 static char **mi_command_line_array;
477 static int mi_command_line_array_cnt;
478 static int mi_command_line_array_ptr;
481 mi_read_next_line (void)
483 if (mi_command_line_array_ptr == mi_command_line_array_cnt)
486 return mi_command_line_array[mi_command_line_array_ptr++];
490 mi_cmd_break_commands (const char *command, char **argv, int argc)
492 command_line_up break_command;
495 struct breakpoint *b;
498 error (_("USAGE: %s <BKPT> [<COMMAND> [<COMMAND>...]]"), command);
500 bnum = strtol (argv[0], &endptr, 0);
501 if (endptr == argv[0])
502 error (_("breakpoint number argument \"%s\" is not a number."),
504 else if (*endptr != '\0')
505 error (_("junk at the end of breakpoint number argument \"%s\"."),
508 b = get_breakpoint (bnum);
510 error (_("breakpoint %d not found."), bnum);
512 mi_command_line_array = argv;
513 mi_command_line_array_ptr = 1;
514 mi_command_line_array_cnt = argc;
516 if (is_tracepoint (b))
517 break_command = read_command_lines_1 (mi_read_next_line, 1,
518 check_tracepoint_command, b);
520 break_command = read_command_lines_1 (mi_read_next_line, 1, 0, 0);
522 breakpoint_set_commands (b, std::move (break_command));