1 /* MI Command Set - breakpoint and watchpoint commands.
2 Copyright (C) 2000-2024 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/>. */
20 #include "arch-utils.h"
21 #include "exceptions.h"
25 #include "breakpoint.h"
26 #include "mi-getopt.h"
27 #include "observable.h"
29 #include "mi-cmd-break.h"
33 #include "gdbsupport/gdb_obstack.h"
35 #include "tracepoint.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
)
61 catch (const gdb_exception_error
&ex
)
63 exception_print (gdb_stderr
, ex
);
75 /* Arrange for all new breakpoints and catchpoints to be reported to
76 CURRENT_UIOUT until the destructor of the returned scoped_restore
79 Note that MI output will be probably invalid if more than one
80 breakpoint is created inside one MI command. */
82 scoped_restore_tmpl
<int>
83 setup_breakpoint_reporting (void)
85 if (! mi_breakpoint_observers_installed
)
87 gdb::observers::breakpoint_created
.attach (breakpoint_notify
,
89 mi_breakpoint_observers_installed
= 1;
92 return make_scoped_restore (&mi_can_breakpoint_notify
, 1);
96 /* Convert arguments in ARGV to a string suitable for parsing by
97 dprintf like "FORMAT",ARG,ARG... and return it. */
100 mi_argv_to_format (const char *const *argv
, int argc
)
105 /* Convert ARGV[0] to format string and save to FORMAT. */
107 for (i
= 0; argv
[0][i
] != '\0'; i
++)
139 if (isprint (argv
[0][i
]))
140 result
+= argv
[0][i
];
145 xsnprintf (tmp
, sizeof (tmp
), "\\%o",
146 (unsigned char) argv
[0][i
]);
154 /* Append other arguments. */
155 for (i
= 1; i
< argc
; i
++)
164 /* Insert breakpoint.
165 If dprintf is true, it will insert dprintf.
166 If not, it will insert other type breakpoint. */
169 mi_cmd_break_insert_1 (int dprintf
, const char *command
,
170 const char *const *argv
, int argc
)
172 const char *address
= NULL
;
176 int thread_group
= -1;
177 int ignore_count
= 0;
178 const char *condition
= NULL
;
182 symbol_name_match_type match_type
= symbol_name_match_type::WILD
;
183 enum bptype type_wanted
;
184 location_spec_up locspec
;
185 const struct breakpoint_ops
*ops
;
187 std::unique_ptr
<explicit_location_spec
> explicit_loc
188 (new explicit_location_spec ());
189 std::string extra_string
;
190 bool force_condition
= false;
194 HARDWARE_OPT
, TEMP_OPT
, CONDITION_OPT
,
195 IGNORE_COUNT_OPT
, THREAD_OPT
, THREAD_GROUP_OPT
,
196 PENDING_OPT
, DISABLE_OPT
,
200 EXPLICIT_SOURCE_OPT
, EXPLICIT_FUNC_OPT
,
201 EXPLICIT_LABEL_OPT
, EXPLICIT_LINE_OPT
203 static const struct mi_opt opts
[] =
205 {"h", HARDWARE_OPT
, 0},
207 {"c", CONDITION_OPT
, 1},
208 {"i", IGNORE_COUNT_OPT
, 1},
209 {"p", THREAD_OPT
, 1},
210 {"g", THREAD_GROUP_OPT
, 1},
211 {"f", PENDING_OPT
, 0},
212 {"d", DISABLE_OPT
, 0},
213 {"a", TRACEPOINT_OPT
, 0},
214 {"-force-condition", FORCE_CONDITION_OPT
, 0},
215 {"-qualified", QUALIFIED_OPT
, 0},
216 {"-source" , EXPLICIT_SOURCE_OPT
, 1},
217 {"-function", EXPLICIT_FUNC_OPT
, 1},
218 {"-label", EXPLICIT_LABEL_OPT
, 1},
219 {"-line", EXPLICIT_LINE_OPT
, 1},
223 /* Parse arguments. It could be -r or -h or -t, <location> or ``--''
224 to denote the end of the option list. */
230 int opt
= mi_getopt ("-break-insert", argc
, argv
,
234 switch ((enum opt
) opt
)
245 case IGNORE_COUNT_OPT
:
246 ignore_count
= atol (oarg
);
249 thread
= atol (oarg
);
250 if (!valid_global_thread_id (thread
))
251 error (_("Unknown thread %d."), thread
);
253 case THREAD_GROUP_OPT
:
254 thread_group
= mi_parse_thread_group_id (oarg
);
266 match_type
= symbol_name_match_type::FULL
;
268 case EXPLICIT_SOURCE_OPT
:
270 explicit_loc
->source_filename
= make_unique_xstrdup (oarg
);
272 case EXPLICIT_FUNC_OPT
:
274 explicit_loc
->function_name
= make_unique_xstrdup (oarg
);
276 case EXPLICIT_LABEL_OPT
:
278 explicit_loc
->label_name
= make_unique_xstrdup (oarg
);
280 case EXPLICIT_LINE_OPT
:
282 explicit_loc
->line_offset
= linespec_parse_line_offset (oarg
);
284 case FORCE_CONDITION_OPT
:
285 force_condition
= true;
290 if (oind
>= argc
&& !is_explicit
)
291 error (_("-%s-insert: Missing <location>"),
292 dprintf
? "dprintf" : "break");
295 int format_num
= is_explicit
? oind
: oind
+ 1;
297 if (hardware
|| tracepoint
)
298 error (_("-dprintf-insert: does not support -h or -a"));
299 if (format_num
>= argc
)
300 error (_("-dprintf-insert: Missing <format>"));
302 extra_string
= mi_argv_to_format (argv
+ format_num
, argc
- format_num
);
303 address
= argv
[oind
];
310 error (_("-break-insert: Garbage following explicit location"));
315 error (_("-break-insert: Garbage following <location>"));
316 address
= argv
[oind
];
320 /* Now we have what we need, let's insert the breakpoint! */
321 scoped_restore restore_breakpoint_reporting
= setup_breakpoint_reporting ();
325 /* Note that to request a fast tracepoint, the client uses the
326 "hardware" flag, although there's nothing of hardware related to
327 fast tracepoints -- one can implement slow tracepoints with
328 hardware breakpoints, but fast tracepoints are always software.
329 "fast" is a misnomer, actually, "jump" would be more appropriate.
330 A simulator or an emulator could conceivably implement fast
331 regular non-jump based tracepoints. */
332 type_wanted
= hardware
? bp_fast_tracepoint
: bp_tracepoint
;
333 ops
= breakpoint_ops_for_location_spec (nullptr, true);
337 type_wanted
= bp_dprintf
;
338 ops
= &code_breakpoint_ops
;
342 type_wanted
= hardware
? bp_hardware_breakpoint
: bp_breakpoint
;
343 ops
= &code_breakpoint_ops
;
348 /* Error check -- we must have one of the other
349 parameters specified. */
350 if (explicit_loc
->source_filename
!= NULL
351 && explicit_loc
->function_name
== NULL
352 && explicit_loc
->label_name
== NULL
353 && explicit_loc
->line_offset
.sign
== LINE_OFFSET_UNKNOWN
)
354 error (_("-%s-insert: --source option requires --function, --label,"
355 " or --line"), dprintf
? "dprintf" : "break");
357 explicit_loc
->func_name_match_type
= match_type
;
359 locspec
= std::move (explicit_loc
);
363 locspec
= string_to_location_spec_basic (&address
, current_language
,
366 error (_("Garbage '%s' at end of location"), address
);
369 create_breakpoint (get_current_arch (), locspec
.get (), condition
,
370 thread
, thread_group
,
371 extra_string
.c_str (),
373 0 /* condition and thread are valid. */,
376 pending
? AUTO_BOOLEAN_TRUE
: AUTO_BOOLEAN_FALSE
,
377 ops
, 0, enabled
, 0, 0);
380 /* Implements the -break-insert command.
381 See the MI manual for the list of possible options. */
384 mi_cmd_break_insert (const char *command
, const char *const *argv
, int argc
)
386 mi_cmd_break_insert_1 (0, command
, argv
, argc
);
389 /* Implements the -dprintf-insert command.
390 See the MI manual for the list of possible options. */
393 mi_cmd_dprintf_insert (const char *command
, const char *const *argv
, int argc
)
395 mi_cmd_break_insert_1 (1, command
, argv
, argc
);
398 /* Implements the -break-condition command.
399 See the MI manual for the list of options. */
402 mi_cmd_break_condition (const char *command
, const char *const *argv
,
410 static const struct mi_opt opts
[] =
412 {"-force", FORCE_CONDITION_OPT
, 0},
416 /* Parse arguments. */
419 bool force_condition
= false;
423 int opt
= mi_getopt ("-break-condition", argc
, argv
,
430 case FORCE_CONDITION_OPT
:
431 force_condition
= true;
436 /* There must be at least one more arg: a bpnum. */
438 error (_("-break-condition: Missing the <number> argument"));
440 int bpnum
= atoi (argv
[oind
]);
442 /* The rest form the condition expr. */
443 std::string expr
= "";
444 for (int i
= oind
+ 1; i
< argc
; ++i
)
451 set_breakpoint_condition (bpnum
, expr
.c_str (), 0 /* from_tty */,
463 mi_cmd_break_passcount (const char *command
, const char *const *argv
,
468 struct tracepoint
*t
;
471 error (_("Usage: tracepoint-number passcount"));
475 t
= get_tracepoint (n
);
480 notify_breakpoint_modified (t
);
484 error (_("Could not find tracepoint %d"), n
);
488 /* Insert a watchpoint. The type of watchpoint is specified by the
490 -break-watch <expr> --> insert a regular wp.
491 -break-watch -r <expr> --> insert a read watchpoint.
492 -break-watch -a <expr> --> insert an access wp. */
495 mi_cmd_break_watch (const char *command
, const char *const *argv
, int argc
)
497 const char *expr
= NULL
;
498 enum wp_type type
= REG_WP
;
503 static const struct mi_opt opts
[] =
506 {"a", ACCESS_OPT
, 0},
510 /* Parse arguments. */
516 int opt
= mi_getopt ("-break-watch", argc
, argv
,
521 switch ((enum opt
) opt
)
532 error (_("-break-watch: Missing <expression>"));
534 error (_("-break-watch: Garbage following <expression>"));
537 /* Now we have what we need, let's insert the watchpoint! */
541 watch_command_wrapper (expr
, FROM_TTY
, false);
544 rwatch_command_wrapper (expr
, FROM_TTY
, false);
547 awatch_command_wrapper (expr
, FROM_TTY
, false);
550 error (_("-break-watch: Unknown watchpoint type."));
555 mi_cmd_break_commands (const char *command
, const char *const *argv
, int argc
)
557 counted_command_line break_command
;
560 struct breakpoint
*b
;
563 error (_("USAGE: %s <BKPT> [<COMMAND> [<COMMAND>...]]"), command
);
565 bnum
= strtol (argv
[0], &endptr
, 0);
566 if (endptr
== argv
[0])
567 error (_("breakpoint number argument \"%s\" is not a number."),
569 else if (*endptr
!= '\0')
570 error (_("junk at the end of breakpoint number argument \"%s\"."),
573 b
= get_breakpoint (bnum
);
575 error (_("breakpoint %d not found."), bnum
);
579 = [&] (std::string
&buffer
)
581 const char *result
= nullptr;
583 result
= argv
[count
++];
587 if (is_tracepoint (b
))
589 tracepoint
*t
= gdb::checked_static_cast
<tracepoint
*> (b
);
590 break_command
= read_command_lines_1 (reader
, 1,
591 [=] (const char *line
)
593 validate_actionline (line
, t
);
597 break_command
= read_command_lines_1 (reader
, 1, 0);
599 breakpoint_set_commands (b
, std::move (break_command
));