1 /* MI Command Set - breakpoint and watchpoint commands.
2 Copyright (C) 2000-2013 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 "gdb_string.h"
27 #include "mi-getopt.h"
29 #include "exceptions.h"
32 #include "mi-cmd-break.h"
33 #include "gdb_obstack.h"
41 /* True if MI breakpoint observers have been registered. */
43 static int mi_breakpoint_observers_installed
;
45 /* Control whether breakpoint_notify may act. */
47 static int mi_can_breakpoint_notify
;
49 /* Output a single breakpoint, when allowed. */
52 breakpoint_notify (struct breakpoint
*b
)
54 if (mi_can_breakpoint_notify
)
55 gdb_breakpoint_query (current_uiout
, b
->number
, NULL
);
65 /* Arrange for all new breakpoints and catchpoints to be reported to
66 CURRENT_UIOUT until the cleanup returned by this function is run.
68 Note that MI output will be probably invalid if more than one
69 breakpoint is created inside one MI command. */
72 setup_breakpoint_reporting (void)
74 struct cleanup
*rev_flag
;
76 if (! mi_breakpoint_observers_installed
)
78 observer_attach_breakpoint_created (breakpoint_notify
);
79 mi_breakpoint_observers_installed
= 1;
82 rev_flag
= make_cleanup_restore_integer (&mi_can_breakpoint_notify
);
83 mi_can_breakpoint_notify
= 1;
89 /* Convert arguments in ARGV to the string in "format",argv,argv...
93 mi_argv_to_format (char **argv
, int argc
)
96 struct obstack obstack
;
99 obstack_init (&obstack
);
101 /* Convert ARGV[OIND + 1] to format string and save to FORMAT. */
102 obstack_1grow (&obstack
, '\"');
103 for (i
= 0; i
< strlen (argv
[0]); i
++)
108 obstack_grow (&obstack
, "\\\\", 2);
111 obstack_grow (&obstack
, "\\a", 2);
114 obstack_grow (&obstack
, "\\b", 2);
117 obstack_grow (&obstack
, "\\f", 2);
120 obstack_grow (&obstack
, "\\n", 2);
123 obstack_grow (&obstack
, "\\r", 2);
126 obstack_grow (&obstack
, "\\t", 2);
129 obstack_grow (&obstack
, "\\v", 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 (&obstack
, tmp
, strlen (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 (&obstack
, argv
[i
], strlen (argv
[i
]));
153 obstack_1grow (&obstack
, '\0');
155 ret
= xstrdup (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
, 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 struct breakpoint_ops
*ops
;
180 char *extra_string
= NULL
;
184 HARDWARE_OPT
, TEMP_OPT
, CONDITION_OPT
,
185 IGNORE_COUNT_OPT
, THREAD_OPT
, PENDING_OPT
, DISABLE_OPT
,
188 static const struct mi_opt opts
[] =
190 {"h", HARDWARE_OPT
, 0},
192 {"c", CONDITION_OPT
, 1},
193 {"i", IGNORE_COUNT_OPT
, 1},
194 {"p", THREAD_OPT
, 1},
195 {"f", PENDING_OPT
, 0},
196 {"d", DISABLE_OPT
, 0},
197 {"a", TRACEPOINT_OPT
, 0},
201 /* Parse arguments. It could be -r or -h or -t, <location> or ``--''
202 to denote the end of the option list. */
208 int opt
= mi_getopt ("-break-insert", argc
, argv
,
212 switch ((enum opt
) opt
)
223 case IGNORE_COUNT_OPT
:
224 ignore_count
= atol (oarg
);
227 thread
= atol (oarg
);
242 error (_("-%s-insert: Missing <location>"),
243 dprintf
? "dprintf" : "break");
244 address
= argv
[oind
];
247 int format_num
= oind
+ 1;
249 if (hardware
|| tracepoint
)
250 error (_("-dprintf-insert: does not support -h or -a"));
251 if (format_num
>= argc
)
252 error (_("-dprintf-insert: Missing <format>"));
254 extra_string
= mi_argv_to_format (argv
+ format_num
, argc
- format_num
);
255 make_cleanup (xfree
, extra_string
);
260 error (_("-break-insert: Garbage following <location>"));
263 /* Now we have what we need, let's insert the breakpoint! */
264 setup_breakpoint_reporting ();
268 /* Note that to request a fast tracepoint, the client uses the
269 "hardware" flag, although there's nothing of hardware related to
270 fast tracepoints -- one can implement slow tracepoints with
271 hardware breakpoints, but fast tracepoints are always software.
272 "fast" is a misnomer, actually, "jump" would be more appropriate.
273 A simulator or an emulator could conceivably implement fast
274 regular non-jump based tracepoints. */
275 type_wanted
= hardware
? bp_fast_tracepoint
: bp_tracepoint
;
276 ops
= &tracepoint_breakpoint_ops
;
280 type_wanted
= bp_dprintf
;
281 ops
= &dprintf_breakpoint_ops
;
285 type_wanted
= hardware
? bp_hardware_breakpoint
: bp_breakpoint
;
286 ops
= &bkpt_breakpoint_ops
;
289 create_breakpoint (get_current_arch (), address
, condition
, thread
,
291 0 /* condition and thread are valid. */,
294 pending
? AUTO_BOOLEAN_TRUE
: AUTO_BOOLEAN_FALSE
,
295 ops
, 0, enabled
, 0, 0);
296 do_cleanups (back_to
);
299 /* Implements the -break-insert command.
300 See the MI manual for the list of possible options. */
303 mi_cmd_break_insert (char *command
, char **argv
, int argc
)
305 mi_cmd_break_insert_1 (0, command
, argv
, argc
);
308 /* Implements the -dprintf-insert command.
309 See the MI manual for the list of possible options. */
312 mi_cmd_dprintf_insert (char *command
, char **argv
, int argc
)
314 mi_cmd_break_insert_1 (1, command
, argv
, argc
);
325 mi_cmd_break_passcount (char *command
, char **argv
, int argc
)
329 struct tracepoint
*t
;
332 error (_("Usage: tracepoint-number passcount"));
336 t
= get_tracepoint (n
);
341 observer_notify_breakpoint_modified (&t
->base
);
345 error (_("Could not find tracepoint %d"), n
);
349 /* Insert a watchpoint. The type of watchpoint is specified by the
351 -break-watch <expr> --> insert a regular wp.
352 -break-watch -r <expr> --> insert a read watchpoint.
353 -break-watch -a <expr> --> insert an access wp. */
356 mi_cmd_break_watch (char *command
, char **argv
, int argc
)
359 enum wp_type type
= REG_WP
;
364 static const struct mi_opt opts
[] =
367 {"a", ACCESS_OPT
, 0},
371 /* Parse arguments. */
377 int opt
= mi_getopt ("-break-watch", argc
, argv
,
382 switch ((enum opt
) opt
)
393 error (_("-break-watch: Missing <expression>"));
395 error (_("-break-watch: Garbage following <expression>"));
398 /* Now we have what we need, let's insert the watchpoint! */
402 watch_command_wrapper (expr
, FROM_TTY
, 0);
405 rwatch_command_wrapper (expr
, FROM_TTY
, 0);
408 awatch_command_wrapper (expr
, FROM_TTY
, 0);
411 error (_("-break-watch: Unknown watchpoint type."));
415 /* The mi_read_next_line consults these variable to return successive
416 command lines. While it would be clearer to use a closure pointer,
417 it is not expected that any future code will use read_command_lines_1,
418 therefore no point of overengineering. */
420 static char **mi_command_line_array
;
421 static int mi_command_line_array_cnt
;
422 static int mi_command_line_array_ptr
;
425 mi_read_next_line (void)
427 if (mi_command_line_array_ptr
== mi_command_line_array_cnt
)
430 return mi_command_line_array
[mi_command_line_array_ptr
++];
434 mi_cmd_break_commands (char *command
, char **argv
, int argc
)
436 struct command_line
*break_command
;
439 struct breakpoint
*b
;
442 error (_("USAGE: %s <BKPT> [<COMMAND> [<COMMAND>...]]"), command
);
444 bnum
= strtol (argv
[0], &endptr
, 0);
445 if (endptr
== argv
[0])
446 error (_("breakpoint number argument \"%s\" is not a number."),
448 else if (*endptr
!= '\0')
449 error (_("junk at the end of breakpoint number argument \"%s\"."),
452 b
= get_breakpoint (bnum
);
454 error (_("breakpoint %d not found."), bnum
);
456 mi_command_line_array
= argv
;
457 mi_command_line_array_ptr
= 1;
458 mi_command_line_array_cnt
= argc
;
460 if (is_tracepoint (b
))
461 break_command
= read_command_lines_1 (mi_read_next_line
, 1,
462 check_tracepoint_command
, b
);
464 break_command
= read_command_lines_1 (mi_read_next_line
, 1, 0, 0);
466 breakpoint_set_commands (b
, break_command
);