GDB (xrefs)
Loading...
Searching...
No Matches
mi-cmd-break.c
Go to the documentation of this file.
1/* MI Command Set - breakpoint and watchpoint commands.
2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions (a Red Hat company).
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21#include "arch-utils.h"
22#include "mi-cmds.h"
23#include "ui-out.h"
24#include "mi-out.h"
25#include "breakpoint.h"
26#include "mi-getopt.h"
27#include "observable.h"
28#include "mi-main.h"
29#include "mi-cmd-break.h"
30#include "language.h"
31#include "location.h"
32#include "linespec.h"
33#include "gdbsupport/gdb_obstack.h"
34#include <ctype.h>
35#include "tracepoint.h"
36
37enum
38 {
39 FROM_TTY = 0
40 };
41
42/* True if MI breakpoint observers have been registered. */
43
45
46/* Control whether breakpoint_notify may act. */
47
49
50/* Output a single breakpoint, when allowed. */
51
52static void
54{
56 {
57 try
58 {
60 }
61 catch (const gdb_exception_error &ex)
62 {
64 }
65 }
66}
67
74
75/* Arrange for all new breakpoints and catchpoints to be reported to
76 CURRENT_UIOUT until the destructor of the returned scoped_restore
77 is run.
78
79 Note that MI output will be probably invalid if more than one
80 breakpoint is created inside one MI command. */
81
82scoped_restore_tmpl<int>
84{
86 {
88 "mi-cmd-break");
90 }
91
92 return make_scoped_restore (&mi_can_breakpoint_notify, 1);
93}
94
95
96/* Convert arguments in ARGV to a string suitable for parsing by
97 dprintf like "FORMAT",ARG,ARG... and return it. */
98
99static std::string
100mi_argv_to_format (const char *const *argv, int argc)
101{
102 int i;
103 std::string result;
104
105 /* Convert ARGV[0] to format string and save to FORMAT. */
106 result += '\"';
107 for (i = 0; argv[0][i] != '\0'; i++)
108 {
109 switch (argv[0][i])
110 {
111 case '\\':
112 result += "\\\\";
113 break;
114 case '\a':
115 result += "\\a";
116 break;
117 case '\b':
118 result += "\\b";
119 break;
120 case '\f':
121 result += "\\f";
122 break;
123 case '\n':
124 result += "\\n";
125 break;
126 case '\r':
127 result += "\\r";
128 break;
129 case '\t':
130 result += "\\t";
131 break;
132 case '\v':
133 result += "\\v";
134 break;
135 case '"':
136 result += "\\\"";
137 break;
138 default:
139 if (isprint (argv[0][i]))
140 result += argv[0][i];
141 else
142 {
143 char tmp[5];
144
145 xsnprintf (tmp, sizeof (tmp), "\\%o",
146 (unsigned char) argv[0][i]);
147 result += tmp;
148 }
149 break;
150 }
151 }
152 result += '\"';
153
154 /* Append other arguments. */
155 for (i = 1; i < argc; i++)
156 {
157 result += ',';
158 result += argv[i];
159 }
160
161 return result;
162}
163
164/* Insert breakpoint.
165 If dprintf is true, it will insert dprintf.
166 If not, it will insert other type breakpoint. */
167
168static void
169mi_cmd_break_insert_1 (int dprintf, const char *command,
170 const char *const *argv, int argc)
171{
172 const char *address = NULL;
173 int hardware = 0;
174 int temp_p = 0;
175 int thread = -1;
176 int thread_group = -1;
177 int ignore_count = 0;
178 const char *condition = NULL;
179 int pending = 0;
180 int enabled = 1;
181 int tracepoint = 0;
183 enum bptype type_wanted;
185 const struct breakpoint_ops *ops;
186 int is_explicit = 0;
187 std::unique_ptr<explicit_location_spec> explicit_loc
188 (new explicit_location_spec ());
189 std::string extra_string;
190 bool force_condition = false;
191
192 enum opt
193 {
194 HARDWARE_OPT, TEMP_OPT, CONDITION_OPT,
195 IGNORE_COUNT_OPT, THREAD_OPT, THREAD_GROUP_OPT,
196 PENDING_OPT, DISABLE_OPT,
197 TRACEPOINT_OPT,
198 FORCE_CONDITION_OPT,
199 QUALIFIED_OPT,
200 EXPLICIT_SOURCE_OPT, EXPLICIT_FUNC_OPT,
201 EXPLICIT_LABEL_OPT, EXPLICIT_LINE_OPT
202 };
203 static const struct mi_opt opts[] =
204 {
205 {"h", HARDWARE_OPT, 0},
206 {"t", TEMP_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},
220 { 0, 0, 0 }
221 };
222
223 /* Parse arguments. It could be -r or -h or -t, <location> or ``--''
224 to denote the end of the option list. */
225 int oind = 0;
226 const char *oarg;
227
228 while (1)
229 {
230 int opt = mi_getopt ("-break-insert", argc, argv,
231 opts, &oind, &oarg);
232 if (opt < 0)
233 break;
234 switch ((enum opt) opt)
235 {
236 case TEMP_OPT:
237 temp_p = 1;
238 break;
239 case HARDWARE_OPT:
240 hardware = 1;
241 break;
242 case CONDITION_OPT:
243 condition = oarg;
244 break;
245 case IGNORE_COUNT_OPT:
246 ignore_count = atol (oarg);
247 break;
248 case THREAD_OPT:
249 thread = atol (oarg);
250 if (!valid_global_thread_id (thread))
251 error (_("Unknown thread %d."), thread);
252 break;
253 case THREAD_GROUP_OPT:
254 thread_group = mi_parse_thread_group_id (oarg);
255 break;
256 case PENDING_OPT:
257 pending = 1;
258 break;
259 case DISABLE_OPT:
260 enabled = 0;
261 break;
262 case TRACEPOINT_OPT:
263 tracepoint = 1;
264 break;
265 case QUALIFIED_OPT:
266 match_type = symbol_name_match_type::FULL;
267 break;
268 case EXPLICIT_SOURCE_OPT:
269 is_explicit = 1;
270 explicit_loc->source_filename = xstrdup (oarg);
271 break;
272 case EXPLICIT_FUNC_OPT:
273 is_explicit = 1;
274 explicit_loc->function_name = xstrdup (oarg);
275 break;
276 case EXPLICIT_LABEL_OPT:
277 is_explicit = 1;
278 explicit_loc->label_name = xstrdup (oarg);
279 break;
280 case EXPLICIT_LINE_OPT:
281 is_explicit = 1;
282 explicit_loc->line_offset = linespec_parse_line_offset (oarg);
283 break;
284 case FORCE_CONDITION_OPT:
285 force_condition = true;
286 break;
287 }
288 }
289
290 if (oind >= argc && !is_explicit)
291 error (_("-%s-insert: Missing <location>"),
292 dprintf ? "dprintf" : "break");
293 if (dprintf)
294 {
295 int format_num = is_explicit ? oind : oind + 1;
296
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>"));
301
302 extra_string = mi_argv_to_format (argv + format_num, argc - format_num);
303 address = argv[oind];
304 }
305 else
306 {
307 if (is_explicit)
308 {
309 if (oind < argc)
310 error (_("-break-insert: Garbage following explicit location"));
311 }
312 else
313 {
314 if (oind < argc - 1)
315 error (_("-break-insert: Garbage following <location>"));
316 address = argv[oind];
317 }
318 }
319
320 /* Now we have what we need, let's insert the breakpoint! */
321 scoped_restore restore_breakpoint_reporting = setup_breakpoint_reporting ();
322
323 if (tracepoint)
324 {
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);
334 }
335 else if (dprintf)
336 {
337 type_wanted = bp_dprintf;
338 ops = &code_breakpoint_ops;
339 }
340 else
341 {
342 type_wanted = hardware ? bp_hardware_breakpoint : bp_breakpoint;
343 ops = &code_breakpoint_ops;
344 }
345
346 if (is_explicit)
347 {
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");
356
357 explicit_loc->func_name_match_type = match_type;
358
359 locspec = std::move (explicit_loc);
360 }
361 else
362 {
364 match_type);
365 if (*address)
366 error (_("Garbage '%s' at end of location"), address);
367 }
368
369 create_breakpoint (get_current_arch (), locspec.get (), condition,
370 thread, thread_group,
371 extra_string.c_str (),
372 force_condition,
373 0 /* condition and thread are valid. */,
374 temp_p, type_wanted,
375 ignore_count,
377 ops, 0, enabled, 0, 0);
378}
379
380/* Implements the -break-insert command.
381 See the MI manual for the list of possible options. */
382
383void
384mi_cmd_break_insert (const char *command, const char *const *argv, int argc)
385{
386 mi_cmd_break_insert_1 (0, command, argv, argc);
387}
388
389/* Implements the -dprintf-insert command.
390 See the MI manual for the list of possible options. */
391
392void
393mi_cmd_dprintf_insert (const char *command, const char *const *argv, int argc)
394{
395 mi_cmd_break_insert_1 (1, command, argv, argc);
396}
397
398/* Implements the -break-condition command.
399 See the MI manual for the list of options. */
400
401void
402mi_cmd_break_condition (const char *command, const char *const *argv,
403 int argc)
404{
405 enum option
406 {
407 FORCE_CONDITION_OPT,
408 };
409
410 static const struct mi_opt opts[] =
411 {
412 {"-force", FORCE_CONDITION_OPT, 0},
413 { 0, 0, 0 }
414 };
415
416 /* Parse arguments. */
417 int oind = 0;
418 const char *oarg;
419 bool force_condition = false;
420
421 while (true)
422 {
423 int opt = mi_getopt ("-break-condition", argc, argv,
424 opts, &oind, &oarg);
425 if (opt < 0)
426 break;
427
428 switch (opt)
429 {
430 case FORCE_CONDITION_OPT:
431 force_condition = true;
432 break;
433 }
434 }
435
436 /* There must be at least one more arg: a bpnum. */
437 if (oind >= argc)
438 error (_("-break-condition: Missing the <number> argument"));
439
440 int bpnum = atoi (argv[oind]);
441
442 /* The rest form the condition expr. */
443 std::string expr = "";
444 for (int i = oind + 1; i < argc; ++i)
445 {
446 expr += argv[i];
447 if (i + 1 < argc)
448 expr += " ";
449 }
450
451 set_breakpoint_condition (bpnum, expr.c_str (), 0 /* from_tty */,
452 force_condition);
453}
454
461
462void
463mi_cmd_break_passcount (const char *command, const char *const *argv,
464 int argc)
465{
466 int n;
467 int p;
468 struct tracepoint *t;
469
470 if (argc != 2)
471 error (_("Usage: tracepoint-number passcount"));
472
473 n = atoi (argv[0]);
474 p = atoi (argv[1]);
475 t = get_tracepoint (n);
476
477 if (t)
478 {
479 t->pass_count = p;
481 }
482 else
483 {
484 error (_("Could not find tracepoint %d"), n);
485 }
486}
487
488/* Insert a watchpoint. The type of watchpoint is specified by the
489 first argument:
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. */
493
494void
495mi_cmd_break_watch (const char *command, const char *const *argv, int argc)
496{
497 const char *expr = NULL;
498 enum wp_type type = REG_WP;
499 enum opt
500 {
501 READ_OPT, ACCESS_OPT
502 };
503 static const struct mi_opt opts[] =
504 {
505 {"r", READ_OPT, 0},
506 {"a", ACCESS_OPT, 0},
507 { 0, 0, 0 }
508 };
509
510 /* Parse arguments. */
511 int oind = 0;
512 const char *oarg;
513
514 while (1)
515 {
516 int opt = mi_getopt ("-break-watch", argc, argv,
517 opts, &oind, &oarg);
518
519 if (opt < 0)
520 break;
521 switch ((enum opt) opt)
522 {
523 case READ_OPT:
524 type = READ_WP;
525 break;
526 case ACCESS_OPT:
527 type = ACCESS_WP;
528 break;
529 }
530 }
531 if (oind >= argc)
532 error (_("-break-watch: Missing <expression>"));
533 if (oind < argc - 1)
534 error (_("-break-watch: Garbage following <expression>"));
535 expr = argv[oind];
536
537 /* Now we have what we need, let's insert the watchpoint! */
538 switch (type)
539 {
540 case REG_WP:
542 break;
543 case READ_WP:
545 break;
546 case ACCESS_WP:
548 break;
549 default:
550 error (_("-break-watch: Unknown watchpoint type."));
551 }
552}
553
554void
555mi_cmd_break_commands (const char *command, const char *const *argv, int argc)
556{
558 char *endptr;
559 int bnum;
560 struct breakpoint *b;
561
562 if (argc < 1)
563 error (_("USAGE: %s <BKPT> [<COMMAND> [<COMMAND>...]]"), command);
564
565 bnum = strtol (argv[0], &endptr, 0);
566 if (endptr == argv[0])
567 error (_("breakpoint number argument \"%s\" is not a number."),
568 argv[0]);
569 else if (*endptr != '\0')
570 error (_("junk at the end of breakpoint number argument \"%s\"."),
571 argv[0]);
572
573 b = get_breakpoint (bnum);
574 if (b == NULL)
575 error (_("breakpoint %d not found."), bnum);
576
577 int count = 1;
578 auto reader
579 = [&] (std::string &buffer)
580 {
581 const char *result = nullptr;
582 if (count < argc)
583 result = argv[count++];
584 return result;
585 };
586
587 if (is_tracepoint (b))
588 {
589 tracepoint *t = gdb::checked_static_cast<tracepoint *> (b);
591 [=] (const char *line)
592 {
593 validate_actionline (line, t);
594 });
595 }
596 else
597 break_command = read_command_lines_1 (reader, 1, 0);
598
599 breakpoint_set_commands (b, std::move (break_command));
600}
601
struct gdbarch * get_current_arch(void)
Definition arch-utils.c:846
struct breakpoint * get_breakpoint(int num)
Definition breakpoint.c:887
void watch_command_wrapper(const char *arg, int from_tty, bool internal)
void rwatch_command_wrapper(const char *arg, int from_tty, bool internal)
int create_breakpoint(struct gdbarch *gdbarch, location_spec *locspec, const char *cond_string, int thread, int inferior, const char *extra_string, bool force_condition, int parse_extra, int tempflag, enum bptype type_wanted, int ignore_count, enum auto_boolean pending_break_support, const struct breakpoint_ops *ops, int from_tty, int enabled, int internal, unsigned flags)
const struct breakpoint_ops code_breakpoint_ops
Definition breakpoint.c:290
void break_command(const char *arg, int from_tty)
void awatch_command_wrapper(const char *arg, int from_tty, bool internal)
void breakpoint_set_commands(struct breakpoint *b, counted_command_line &&commands)
void notify_breakpoint_modified(breakpoint *b)
struct tracepoint * get_tracepoint(int num)
void set_breakpoint_condition(struct breakpoint *b, const char *exp, int from_tty, bool force)
void print_breakpoint(breakpoint *b)
bool is_tracepoint(const struct breakpoint *b)
const struct breakpoint_ops * breakpoint_ops_for_location_spec(const location_spec *locspec, bool is_tracepoint)
bptype
Definition breakpoint.h:84
@ bp_breakpoint
Definition breakpoint.h:86
@ bp_dprintf
Definition breakpoint.h:195
@ bp_fast_tracepoint
Definition breakpoint.h:185
@ bp_hardware_breakpoint
Definition breakpoint.h:87
@ bp_tracepoint
Definition breakpoint.h:184
counted_command_line read_command_lines_1(read_next_line_ftype read_next_line_func, int parse_commands, gdb::function_view< void(const char *)> validator)
std::shared_ptr< command_line > counted_command_line
Definition cli-script.h:67
@ AUTO_BOOLEAN_TRUE
Definition defs.h:248
@ AUTO_BOOLEAN_FALSE
Definition defs.h:249
void exception_print(struct ui_file *file, const struct gdb_exception &e)
Definition exceptions.c:106
int valid_global_thread_id(int global_id)
Definition thread.c:621
const struct language_defn * current_language
Definition language.c:82
struct line_offset linespec_parse_line_offset(const char *string)
Definition linespec.c:1664
location_spec_up string_to_location_spec_basic(const char **stringp, const struct language_defn *language, symbol_name_match_type match_type)
Definition location.c:785
std::unique_ptr< location_spec > location_spec_up
Definition location.h:71
@ LINE_OFFSET_UNKNOWN
Definition location.h:41
wp_type
@ REG_WP
@ ACCESS_WP
@ READ_WP
static int mi_can_breakpoint_notify
static std::string mi_argv_to_format(const char *const *argv, int argc)
bp_type
@ REGEXP_BP
@ REG_BP
@ HW_BP
@ FROM_TTY
static int mi_breakpoint_observers_installed
scoped_restore_tmpl< int > setup_breakpoint_reporting(void)
static void breakpoint_notify(struct breakpoint *b)
static void mi_cmd_break_insert_1(int dprintf, const char *command, const char *const *argv, int argc)
mi_cmd_argv_ftype mi_cmd_break_condition
mi_cmd_argv_ftype mi_cmd_break_commands
mi_cmd_argv_ftype mi_cmd_dprintf_insert
mi_cmd_argv_ftype mi_cmd_break_insert
mi_cmd_argv_ftype mi_cmd_break_passcount
mi_cmd_argv_ftype mi_cmd_break_watch
int mi_getopt(const char *prefix, int argc, const char *const *argv, const struct mi_opt *opts, int *oind, const char **oarg)
Definition mi-getopt.c:82
int mi_parse_thread_group_id(const char *id)
Definition mi-main.c:2805
Definition ada-exp.h:87
observable< struct breakpoint * > breakpoint_created
int ignore_count
Definition breakpoint.h:813
location_spec_up locspec
Definition breakpoint.h:832
symbol_name_match_type
Definition symtab.h:63
void validate_actionline(const char *line, tracepoint *t)
Definition tracepoint.c:629
#define gdb_stderr
Definition utils.h:187