1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
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 "arch-utils.h"
21 #include <ctype.h>
22 #include "event-top.h"
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "cli/cli-cmds.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "ui.h"
57 #include "valprint.h"
58 #include "jit.h"
59 #include "parser-defs.h"
60 #include "gdbsupport/gdb_regex.h"
61 #include "probe.h"
62 #include "cli/cli-utils.h"
63 #include "stack.h"
64 #include "ax-gdb.h"
65 #include "dummy-frame.h"
66 #include "interps.h"
67 #include "gdbsupport/format.h"
68 #include "thread-fsm.h"
69 #include "tid-parse.h"
70 #include "cli/cli-style.h"
71 #include "cli/cli-decode.h"
72 #include <unordered_set>
73
74 /* readline include files */
75 #include "readline/tilde.h"
76
77 /* readline defines this. */
78 #undef savestring
79
80 #include "mi/mi-common.h"
81 #include "extension.h"
82 #include <algorithm>
83 #include "progspace-and-thread.h"
84 #include "gdbsupport/array-view.h"
85 #include <optional>
86 #include "gdbsupport/common-utils.h"
87
88 /* Prototypes for local functions. */
89
90 static void map_breakpoint_numbers (const char *,
91 gdb::function_view<void (breakpoint *)>);
92
93 static void
94 create_sals_from_location_spec_default (location_spec *locspec,
95 linespec_result *canonical);
96
97 static void create_breakpoints_sal (struct gdbarch *,
98 struct linespec_result *,
99 gdb::unique_xmalloc_ptr<char>,
100 gdb::unique_xmalloc_ptr<char>,
101 enum bptype,
102 enum bpdisp, int, int, int,
103 int,
104 int, int, int, unsigned);
105
106 static int can_use_hardware_watchpoint
107 (const std::vector<value_ref_ptr> &vals);
108
109 static void mention (const breakpoint *);
110
111 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
112
113 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
114
115 static struct breakpoint *
116 momentary_breakpoint_from_master (struct breakpoint *orig,
117 enum bptype type,
118 int loc_enabled, int thread);
119
120 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, bool);
121
122 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
123 CORE_ADDR bpaddr,
124 enum bptype bptype,
125 struct program_space *pspace);
126
127 static bool watchpoint_locations_match (const struct bp_location *loc1,
128 const struct bp_location *loc2);
129
130 static bool breakpoint_locations_match (const struct bp_location *loc1,
131 const struct bp_location *loc2,
132 bool sw_hw_bps_match = false);
133
134 static bool breakpoint_location_address_match (struct bp_location *bl,
135 const struct address_space *aspace,
136 CORE_ADDR addr);
137
138 static bool breakpoint_location_address_range_overlap (struct bp_location *,
139 const address_space *,
140 CORE_ADDR, int);
141
142 static int remove_breakpoint (struct bp_location *);
143 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
144
145 static enum print_stop_action print_bp_stop_message (bpstat *bs);
146
147 static int hw_breakpoint_used_count (void);
148
149 static int hw_watchpoint_use_count (struct breakpoint *);
150
151 static int hw_watchpoint_used_count_others (struct breakpoint *except,
152 enum bptype type,
153 int *other_type_used);
154
155 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
156 int count);
157
158 static void decref_bp_location (struct bp_location **loc);
159
160 static std::vector<symtab_and_line> bkpt_probe_decode_location_spec
161 (struct breakpoint *b,
162 location_spec *locspec,
163 struct program_space *search_pspace);
164
165 static bool bl_address_is_meaningful (bp_location *loc);
166
167 static int find_loc_num_by_location (const bp_location *loc);
168
169 /* update_global_location_list's modes of operation wrt to whether to
170 insert locations now. */
171 enum ugll_insert_mode
172 {
173 /* Don't insert any breakpoint locations into the inferior, only
174 remove already-inserted locations that no longer should be
175 inserted. Functions that delete a breakpoint or breakpoints
176 should specify this mode, so that deleting a breakpoint doesn't
177 have the side effect of inserting the locations of other
178 breakpoints that are marked not-inserted, but should_be_inserted
179 returns true on them.
180
181 This behavior is useful is situations close to tear-down -- e.g.,
182 after an exec, while the target still has execution, but
183 breakpoint shadows of the previous executable image should *NOT*
184 be restored to the new image; or before detaching, where the
185 target still has execution and wants to delete breakpoints from
186 GDB's lists, and all breakpoints had already been removed from
187 the inferior. */
188 UGLL_DONT_INSERT,
189
190 /* May insert breakpoints iff breakpoints_should_be_inserted_now
191 claims breakpoints should be inserted now. */
192 UGLL_MAY_INSERT,
193
194 /* Insert locations now, irrespective of
195 breakpoints_should_be_inserted_now. E.g., say all threads are
196 stopped right now, and the user did "continue". We need to
197 insert breakpoints _before_ resuming the target, but
198 UGLL_MAY_INSERT wouldn't insert them, because
199 breakpoints_should_be_inserted_now returns false at that point,
200 as no thread is running yet. */
201 UGLL_INSERT
202 };
203
204 /* Return a textual version of INSERT_MODE. */
205
206 static const char *
ugll_insert_mode_text(ugll_insert_mode insert_mode)207 ugll_insert_mode_text (ugll_insert_mode insert_mode)
208 {
209 /* Make sure the compiler warns if a new ugll_insert_mode enumerator is added
210 but not handled here. */
211 DIAGNOSTIC_PUSH
212 DIAGNOSTIC_ERROR_SWITCH
213 switch (insert_mode)
214 {
215 case UGLL_DONT_INSERT:
216 return "UGLL_DONT_INSERT";
217 case UGLL_MAY_INSERT:
218 return "UGLL_MAY_INSERT";
219 case UGLL_INSERT:
220 return "UGLL_INSERT";
221 }
222 DIAGNOSTIC_POP
223
224 gdb_assert_not_reached ("must handle all enum values");
225 }
226
227 /* Return a textual version of REASON. */
228
229 static const char *
remove_bp_reason_str(remove_bp_reason reason)230 remove_bp_reason_str (remove_bp_reason reason)
231 {
232 /* Make sure the compiler warns if a new remove_bp_reason enumerator is added
233 but not handled here. */
234 DIAGNOSTIC_PUSH
235 DIAGNOSTIC_ERROR_SWITCH
236 switch (reason)
237 {
238 case REMOVE_BREAKPOINT:
239 return "regular remove";
240 case DETACH_BREAKPOINT:
241 return "detach";
242 }
243 DIAGNOSTIC_POP
244
245 gdb_assert_not_reached ("must handle all enum values");
246 }
247
248 /* Return a textual version of breakpoint location BL describing number,
249 location and address. */
250
251 static std::string
breakpoint_location_address_str(const bp_location * bl)252 breakpoint_location_address_str (const bp_location *bl)
253 {
254 std::string str = string_printf ("Breakpoint %d (%s) at address %s",
255 bl->owner->number,
256 host_address_to_string (bl),
257 paddress (bl->gdbarch, bl->address));
258
259 std::string loc_string = bl->to_string ();
260 if (!loc_string.empty ())
261 str += string_printf (" %s", loc_string.c_str ());
262
263 return str;
264 }
265
266 static void update_global_location_list (enum ugll_insert_mode);
267
268 static void update_global_location_list_nothrow (enum ugll_insert_mode);
269
270 static void insert_breakpoint_locations (void);
271
272 static void trace_pass_command (const char *, int);
273
274 static void set_tracepoint_count (int num);
275
276 static bool is_masked_watchpoint (const struct breakpoint *b);
277
278 /* Return true if B refers to a static tracepoint set by marker ("-m"),
279 zero otherwise. */
280
281 static bool strace_marker_p (struct breakpoint *b);
282
283 static void bkpt_probe_create_sals_from_location_spec
284 (location_spec *locspec,
285 struct linespec_result *canonical);
286 static void tracepoint_probe_create_sals_from_location_spec
287 (location_spec *locspec,
288 struct linespec_result *canonical);
289
290 const struct breakpoint_ops code_breakpoint_ops =
291 {
292 create_sals_from_location_spec_default,
293 create_breakpoints_sal,
294 };
295
296 /* Breakpoints set on probes. */
297 static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
298 {
299 bkpt_probe_create_sals_from_location_spec,
300 create_breakpoints_sal,
301 };
302
303 /* Tracepoints set on probes. */
304 static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
305 {
306 tracepoint_probe_create_sals_from_location_spec,
307 create_breakpoints_sal,
308 };
309
310 /* Implementation of abstract dtors. These must exist to satisfy the
311 linker. */
312
~breakpoint()313 breakpoint::~breakpoint ()
314 {
315 }
316
~code_breakpoint()317 code_breakpoint::~code_breakpoint ()
318 {
319 }
320
~catchpoint()321 catchpoint::~catchpoint ()
322 {
323 }
324
325 /* The structure to be used in regular breakpoints. */
326 struct ordinary_breakpoint : public code_breakpoint
327 {
328 using code_breakpoint::code_breakpoint;
329
330 int resources_needed (const struct bp_location *) override;
331 enum print_stop_action print_it (const bpstat *bs) const override;
332 void print_mention () const override;
333 void print_recreate (struct ui_file *fp) const override;
334 };
335
336 /* Internal breakpoints. These typically have a lifetime the same as
337 the program, and they end up installed on the breakpoint chain with
338 a negative breakpoint number. They're visible in "maint info
339 breakpoints", but not "info breakpoints". */
340 struct internal_breakpoint : public code_breakpoint
341 {
internal_breakpointinternal_breakpoint342 internal_breakpoint (struct gdbarch *gdbarch,
343 enum bptype type, CORE_ADDR address)
344 : code_breakpoint (gdbarch, type)
345 {
346 symtab_and_line sal;
347 sal.pc = address;
348 sal.section = find_pc_overlay (sal.pc);
349 sal.pspace = current_program_space;
350 add_location (sal);
351
352 pspace = current_program_space;
353 disposition = disp_donttouch;
354 }
355
356 void re_set () override;
357 void check_status (struct bpstat *bs) override;
358 enum print_stop_action print_it (const bpstat *bs) const override;
359 void print_mention () const override;
360 };
361
362 /* Momentary breakpoints. These typically have a lifetime of some run
363 control command only, are always thread-specific, and have 0 for
364 breakpoint number. I.e., there can be many momentary breakpoints
365 on the breakpoint chain and they all same the same number (zero).
366 They're visible in "maint info breakpoints", but not "info
367 breakpoints". */
368 struct momentary_breakpoint : public code_breakpoint
369 {
momentary_breakpointmomentary_breakpoint370 momentary_breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
371 program_space *pspace_,
372 const struct frame_id &frame_id_,
373 int thread_)
374 : code_breakpoint (gdbarch_, bptype)
375 {
376 /* If FRAME_ID is valid, it should be a real frame, not an inlined
377 or tail-called one. */
378 gdb_assert (!frame_id_artificial_p (frame_id));
379
380 /* Momentary breakpoints are always thread-specific. */
381 gdb_assert (thread_ > 0);
382
383 pspace = pspace_;
384 enable_state = bp_enabled;
385 disposition = disp_donttouch;
386 frame_id = frame_id_;
387 thread = thread_;
388
389 /* The inferior should have been set by the parent constructor. */
390 gdb_assert (inferior == -1);
391 }
392
393 void re_set () override;
394 void check_status (struct bpstat *bs) override;
395 enum print_stop_action print_it (const bpstat *bs) const override;
396 void print_mention () const override;
397 };
398
399 /* DPrintf breakpoints. */
400 struct dprintf_breakpoint : public ordinary_breakpoint
401 {
402 using ordinary_breakpoint::ordinary_breakpoint;
403
404 void re_set () override;
405 int breakpoint_hit (const struct bp_location *bl,
406 const address_space *aspace,
407 CORE_ADDR bp_addr,
408 const target_waitstatus &ws) override;
409 void print_recreate (struct ui_file *fp) const override;
410 void after_condition_true (struct bpstat *bs) override;
411 };
412
413 /* Ranged breakpoints. */
414 struct ranged_breakpoint : public ordinary_breakpoint
415 {
ranged_breakpointranged_breakpoint416 explicit ranged_breakpoint (struct gdbarch *gdbarch,
417 const symtab_and_line &sal_start,
418 int length,
419 location_spec_up start_locspec,
420 location_spec_up end_locspec)
421 : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
422 {
423 bp_location *bl = add_location (sal_start);
424 bl->length = length;
425
426 disposition = disp_donttouch;
427
428 locspec = std::move (start_locspec);
429 locspec_range_end = std::move (end_locspec);
430 }
431
432 int breakpoint_hit (const struct bp_location *bl,
433 const address_space *aspace,
434 CORE_ADDR bp_addr,
435 const target_waitstatus &ws) override;
436 int resources_needed (const struct bp_location *) override;
437 enum print_stop_action print_it (const bpstat *bs) const override;
438 bool print_one (const bp_location **) const override;
439 void print_one_detail (struct ui_out *) const override;
440 void print_mention () const override;
441 void print_recreate (struct ui_file *fp) const override;
442 };
443
444 /* Static tracepoints with marker (`-m'). */
445 struct static_marker_tracepoint : public tracepoint
446 {
447 using tracepoint::tracepoint;
448
449 std::vector<symtab_and_line> decode_location_spec
450 (struct location_spec *locspec,
451 struct program_space *search_pspace) override;
452 };
453
454 /* The style in which to perform a dynamic printf. This is a user
455 option because different output options have different tradeoffs;
456 if GDB does the printing, there is better error handling if there
457 is a problem with any of the arguments, but using an inferior
458 function lets you have special-purpose printers and sending of
459 output to the same place as compiled-in print functions. */
460
461 static const char dprintf_style_gdb[] = "gdb";
462 static const char dprintf_style_call[] = "call";
463 static const char dprintf_style_agent[] = "agent";
464 static const char *const dprintf_style_enums[] = {
465 dprintf_style_gdb,
466 dprintf_style_call,
467 dprintf_style_agent,
468 NULL
469 };
470 static const char *dprintf_style = dprintf_style_gdb;
471
472 /* The function to use for dynamic printf if the preferred style is to
473 call into the inferior. The value is simply a string that is
474 copied into the command, so it can be anything that GDB can
475 evaluate to a callable address, not necessarily a function name. */
476
477 static std::string dprintf_function = "printf";
478
479 /* The channel to use for dynamic printf if the preferred style is to
480 call into the inferior; if a nonempty string, it will be passed to
481 the call as the first argument, with the format string as the
482 second. As with the dprintf function, this can be anything that
483 GDB knows how to evaluate, so in addition to common choices like
484 "stderr", this could be an app-specific expression like
485 "mystreams[curlogger]". */
486
487 static std::string dprintf_channel;
488
489 /* True if dprintf commands should continue to operate even if GDB
490 has disconnected. */
491 static bool disconnected_dprintf = true;
492
493 struct command_line *
breakpoint_commands(struct breakpoint * b)494 breakpoint_commands (struct breakpoint *b)
495 {
496 return b->commands ? b->commands.get () : NULL;
497 }
498
499 /* Flag indicating that a command has proceeded the inferior past the
500 current breakpoint. */
501
502 static bool breakpoint_proceeded;
503
504 const char *
bpdisp_text(enum bpdisp disp)505 bpdisp_text (enum bpdisp disp)
506 {
507 /* NOTE: the following values are a part of MI protocol and
508 represent values of 'disp' field returned when inferior stops at
509 a breakpoint. */
510 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
511
512 return bpdisps[(int) disp];
513 }
514
515 /* Prototypes for exported functions. */
516 /* If FALSE, gdb will not use hardware support for watchpoints, even
517 if such is available. */
518 static int can_use_hw_watchpoints;
519
520 static void
show_can_use_hw_watchpoints(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)521 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
522 struct cmd_list_element *c,
523 const char *value)
524 {
525 gdb_printf (file,
526 _("Debugger's willingness to use "
527 "watchpoint hardware is %s.\n"),
528 value);
529 }
530
531 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
532 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
533 for unrecognized breakpoint locations.
534 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
535 static enum auto_boolean pending_break_support;
536 static void
show_pending_break_support(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)537 show_pending_break_support (struct ui_file *file, int from_tty,
538 struct cmd_list_element *c,
539 const char *value)
540 {
541 gdb_printf (file,
542 _("Debugger's behavior regarding "
543 "pending breakpoints is %s.\n"),
544 value);
545 }
546
547 /* If true, gdb will automatically use hardware breakpoints for breakpoints
548 set with "break" but falling in read-only memory.
549 If false, gdb will warn about such breakpoints, but won't automatically
550 use hardware breakpoints. */
551 static bool automatic_hardware_breakpoints;
552 static void
show_automatic_hardware_breakpoints(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)553 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
554 struct cmd_list_element *c,
555 const char *value)
556 {
557 gdb_printf (file,
558 _("Automatic usage of hardware breakpoints is %s.\n"),
559 value);
560 }
561
562 /* If on, GDB keeps breakpoints inserted even if the inferior is
563 stopped, and immediately inserts any new breakpoints as soon as
564 they're created. If off (default), GDB keeps breakpoints off of
565 the target as long as possible. That is, it delays inserting
566 breakpoints until the next resume, and removes them again when the
567 target fully stops. This is a bit safer in case GDB crashes while
568 processing user input. */
569 static bool always_inserted_mode = false;
570
571 static void
show_always_inserted_mode(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)572 show_always_inserted_mode (struct ui_file *file, int from_tty,
573 struct cmd_list_element *c, const char *value)
574 {
575 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
576 value);
577 }
578
579 /* True if breakpoint debug output is enabled. */
580 static bool debug_breakpoint = false;
581
582 /* Print a "breakpoint" debug statement. */
583 #define breakpoint_debug_printf(fmt, ...) \
584 debug_prefixed_printf_cond (debug_breakpoint, "breakpoint", fmt, \
585 ##__VA_ARGS__)
586
587 /* "show debug breakpoint" implementation. */
588 static void
show_debug_breakpoint(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)589 show_debug_breakpoint (struct ui_file *file, int from_tty,
590 struct cmd_list_element *c, const char *value)
591 {
592 gdb_printf (file, _("Breakpoint location debugging is %s.\n"), value);
593 }
594
595 /* See breakpoint.h. */
596
597 int
breakpoints_should_be_inserted_now(void)598 breakpoints_should_be_inserted_now (void)
599 {
600 if (gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
601 {
602 /* If breakpoints are global, they should be inserted even if no
603 thread under gdb's control is running, or even if there are
604 no threads under GDB's control yet. */
605 return 1;
606 }
607 else
608 {
609 if (always_inserted_mode)
610 {
611 /* The user wants breakpoints inserted even if all threads
612 are stopped. */
613 return 1;
614 }
615
616 for (inferior *inf : all_inferiors ())
617 if (inf->has_execution ()
618 && threads_are_executing (inf->process_target ()))
619 return 1;
620
621 /* Don't remove breakpoints yet if, even though all threads are
622 stopped, we still have events to process. */
623 for (thread_info *tp : all_non_exited_threads ())
624 if (tp->resumed () && tp->has_pending_waitstatus ())
625 return 1;
626 }
627 return 0;
628 }
629
630 static const char condition_evaluation_both[] = "host or target";
631
632 /* Modes for breakpoint condition evaluation. */
633 static const char condition_evaluation_auto[] = "auto";
634 static const char condition_evaluation_host[] = "host";
635 static const char condition_evaluation_target[] = "target";
636 static const char *const condition_evaluation_enums[] = {
637 condition_evaluation_auto,
638 condition_evaluation_host,
639 condition_evaluation_target,
640 NULL
641 };
642
643 /* Global that holds the current mode for breakpoint condition evaluation. */
644 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
645
646 /* Global that we use to display information to the user (gets its value from
647 condition_evaluation_mode_1. */
648 static const char *condition_evaluation_mode = condition_evaluation_auto;
649
650 /* Translate a condition evaluation mode MODE into either "host"
651 or "target". This is used mostly to translate from "auto" to the
652 real setting that is being used. It returns the translated
653 evaluation mode. */
654
655 static const char *
translate_condition_evaluation_mode(const char * mode)656 translate_condition_evaluation_mode (const char *mode)
657 {
658 if (mode == condition_evaluation_auto)
659 {
660 if (target_supports_evaluation_of_breakpoint_conditions ())
661 return condition_evaluation_target;
662 else
663 return condition_evaluation_host;
664 }
665 else
666 return mode;
667 }
668
669 /* Discovers what condition_evaluation_auto translates to. */
670
671 static const char *
breakpoint_condition_evaluation_mode(void)672 breakpoint_condition_evaluation_mode (void)
673 {
674 return translate_condition_evaluation_mode (condition_evaluation_mode);
675 }
676
677 /* Return true if GDB should evaluate breakpoint conditions or false
678 otherwise. */
679
680 static bool
gdb_evaluates_breakpoint_condition_p(void)681 gdb_evaluates_breakpoint_condition_p (void)
682 {
683 const char *mode = breakpoint_condition_evaluation_mode ();
684
685 return (mode == condition_evaluation_host);
686 }
687
688 /* Are we executing breakpoint commands? */
689 static int executing_breakpoint_commands;
690
691 /* Are overlay event breakpoints enabled? */
692 static int overlay_events_enabled;
693
694 /* See description in breakpoint.h. */
695 bool target_exact_watchpoints = false;
696
697 /* Chains of all breakpoints defined. */
698
699 static intrusive_list<breakpoint> breakpoint_chain;
700
701 /* See breakpoint.h. */
702
703 breakpoint_range
all_breakpoints()704 all_breakpoints ()
705 {
706 return breakpoint_range (breakpoint_chain.begin (), breakpoint_chain.end ());
707 }
708
709 /* See breakpoint.h. */
710
711 breakpoint_safe_range
all_breakpoints_safe()712 all_breakpoints_safe ()
713 {
714 return breakpoint_safe_range (all_breakpoints ());
715 }
716
717 /* See breakpoint.h. */
718
719 tracepoint_range
all_tracepoints()720 all_tracepoints ()
721 {
722 return tracepoint_range (tracepoint_iterator (breakpoint_chain.begin ()),
723 tracepoint_iterator (breakpoint_chain.end ()));
724 }
725
726 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
727
728 static std::vector<bp_location *> bp_locations;
729
730 /* See breakpoint.h. */
731
732 const std::vector<bp_location *> &
all_bp_locations()733 all_bp_locations ()
734 {
735 return bp_locations;
736 }
737
738 /* Range to iterate over breakpoint locations at a given address. */
739
740 struct bp_locations_at_addr_range
741 {
742 using iterator = std::vector<bp_location *>::iterator;
743
bp_locations_at_addr_rangebp_locations_at_addr_range744 bp_locations_at_addr_range (CORE_ADDR addr)
745 {
746 struct compare
747 {
748 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
749 { return loc->address < addr_; }
750
751 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
752 { return addr_ < loc->address; }
753 };
754
755 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
756 addr, compare ());
757
758 m_begin = it_pair.first;
759 m_end = it_pair.second;
760 }
761
beginbp_locations_at_addr_range762 iterator begin () const
763 { return m_begin; }
764
endbp_locations_at_addr_range765 iterator end () const
766 { return m_end; }
767
768 private:
769 iterator m_begin;
770 iterator m_end;
771 };
772
773 /* Return a range to iterate over all breakpoint locations exactly at address
774 ADDR.
775
776 If it's needed to iterate multiple times on the same range, it's possible
777 to save the range in a local variable and use it multiple times:
778
779 auto range = all_bp_locations_at_addr (addr);
780
781 for (bp_location *loc : range)
782 // use loc
783
784 for (bp_location *loc : range)
785 // use loc
786
787 This saves a bit of time, as it avoids re-doing the binary searches to find
788 the range's boundaries. Just remember not to change the bp_locations vector
789 in the mean time, as it could make the range's iterators stale. */
790
791 static bp_locations_at_addr_range
all_bp_locations_at_addr(CORE_ADDR addr)792 all_bp_locations_at_addr (CORE_ADDR addr)
793 {
794 return bp_locations_at_addr_range (addr);
795 }
796
797 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
798 ADDRESS for the current elements of BP_LOCATIONS which get a valid
799 result from bp_location_has_shadow. You can use it for roughly
800 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
801 an address you need to read. */
802
803 static CORE_ADDR bp_locations_placed_address_before_address_max;
804
805 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
806 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
807 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
808 You can use it for roughly limiting the subrange of BP_LOCATIONS to
809 scan for shadow bytes for an address you need to read. */
810
811 static CORE_ADDR bp_locations_shadow_len_after_address_max;
812
813 /* The locations that no longer correspond to any breakpoint, unlinked
814 from the bp_locations array, but for which a hit may still be
815 reported by a target. */
816 static std::vector<bp_location *> moribund_locations;
817
818 /* Number of last breakpoint made. */
819
820 static int breakpoint_count;
821
822 /* The value of `breakpoint_count' before the last command that
823 created breakpoints. If the last (break-like) command created more
824 than one breakpoint, then the difference between BREAKPOINT_COUNT
825 and PREV_BREAKPOINT_COUNT is more than one. */
826 static int prev_breakpoint_count;
827
828 /* Number of last tracepoint made. */
829
830 static int tracepoint_count;
831
832 static struct cmd_list_element *breakpoint_set_cmdlist;
833 static struct cmd_list_element *breakpoint_show_cmdlist;
834 struct cmd_list_element *save_cmdlist;
835
836 /* Return whether a breakpoint is an active enabled breakpoint. */
837 static bool
breakpoint_enabled(struct breakpoint * b)838 breakpoint_enabled (struct breakpoint *b)
839 {
840 return (b->enable_state == bp_enabled);
841 }
842
843 /* Set breakpoint count to NUM. */
844
845 static void
set_breakpoint_count(int num)846 set_breakpoint_count (int num)
847 {
848 prev_breakpoint_count = breakpoint_count;
849 breakpoint_count = num;
850 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
851 }
852
853 /* Used by `start_rbreak_breakpoints' below, to record the current
854 breakpoint count before "rbreak" creates any breakpoint. */
855 static int rbreak_start_breakpoint_count;
856
857 /* Called at the start an "rbreak" command to record the first
858 breakpoint made. */
859
scoped_rbreak_breakpoints()860 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
861 {
862 rbreak_start_breakpoint_count = breakpoint_count;
863 }
864
865 /* Called at the end of an "rbreak" command to record the last
866 breakpoint made. */
867
~scoped_rbreak_breakpoints()868 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
869 {
870 prev_breakpoint_count = rbreak_start_breakpoint_count;
871 }
872
873 /* Used in run_command to zero the hit count when a new run starts. */
874
875 void
clear_breakpoint_hit_counts(void)876 clear_breakpoint_hit_counts (void)
877 {
878 for (breakpoint &b : all_breakpoints ())
879 b.hit_count = 0;
880 }
881
882
883 /* Return the breakpoint with the specified number, or NULL
884 if the number does not refer to an existing breakpoint. */
885
886 struct breakpoint *
get_breakpoint(int num)887 get_breakpoint (int num)
888 {
889 for (breakpoint &b : all_breakpoints ())
890 if (b.number == num)
891 return &b;
892
893 return nullptr;
894 }
895
896 /* Return TRUE if NUM refer to an existing breakpoint that has
897 multiple code locations. */
898
899 static bool
has_multiple_locations(int num)900 has_multiple_locations (int num)
901 {
902 for (breakpoint &b : all_breakpoints ())
903 if (b.number == num)
904 return b.has_multiple_locations ();
905
906 return false;
907 }
908
909
910
911 /* Mark locations as "conditions have changed" in case the target supports
912 evaluating conditions on its side. */
913
914 static void
mark_breakpoint_modified(struct breakpoint * b)915 mark_breakpoint_modified (struct breakpoint *b)
916 {
917 /* This is only meaningful if the target is
918 evaluating conditions and if the user has
919 opted for condition evaluation on the target's
920 side. */
921 if (gdb_evaluates_breakpoint_condition_p ()
922 || !target_supports_evaluation_of_breakpoint_conditions ())
923 return;
924
925 if (!is_breakpoint (b))
926 return;
927
928 for (bp_location &loc : b->locations ())
929 loc.condition_changed = condition_modified;
930 }
931
932 /* Mark location as "conditions have changed" in case the target supports
933 evaluating conditions on its side. */
934
935 static void
mark_breakpoint_location_modified(struct bp_location * loc)936 mark_breakpoint_location_modified (struct bp_location *loc)
937 {
938 /* This is only meaningful if the target is
939 evaluating conditions and if the user has
940 opted for condition evaluation on the target's
941 side. */
942 if (gdb_evaluates_breakpoint_condition_p ()
943 || !target_supports_evaluation_of_breakpoint_conditions ())
944
945 return;
946
947 if (!is_breakpoint (loc->owner))
948 return;
949
950 loc->condition_changed = condition_modified;
951 }
952
953 /* Sets the condition-evaluation mode using the static global
954 condition_evaluation_mode. */
955
956 static void
set_condition_evaluation_mode(const char * args,int from_tty,struct cmd_list_element * c)957 set_condition_evaluation_mode (const char *args, int from_tty,
958 struct cmd_list_element *c)
959 {
960 const char *old_mode, *new_mode;
961
962 if ((condition_evaluation_mode_1 == condition_evaluation_target)
963 && !target_supports_evaluation_of_breakpoint_conditions ())
964 {
965 condition_evaluation_mode_1 = condition_evaluation_mode;
966 warning (_("Target does not support breakpoint condition evaluation.\n"
967 "Using host evaluation mode instead."));
968 return;
969 }
970
971 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
972 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
973
974 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
975 settings was "auto". */
976 condition_evaluation_mode = condition_evaluation_mode_1;
977
978 /* Only update the mode if the user picked a different one. */
979 if (new_mode != old_mode)
980 {
981 /* If the user switched to a different evaluation mode, we
982 need to synch the changes with the target as follows:
983
984 "host" -> "target": Send all (valid) conditions to the target.
985 "target" -> "host": Remove all the conditions from the target.
986 */
987
988 if (new_mode == condition_evaluation_target)
989 {
990 /* Mark everything modified and synch conditions with the
991 target. */
992 for (bp_location *loc : all_bp_locations ())
993 mark_breakpoint_location_modified (loc);
994 }
995 else
996 {
997 /* Manually mark non-duplicate locations to synch conditions
998 with the target. We do this to remove all the conditions the
999 target knows about. */
1000 for (bp_location *loc : all_bp_locations ())
1001 if (is_breakpoint (loc->owner) && loc->inserted)
1002 loc->needs_update = 1;
1003 }
1004
1005 /* Do the update. */
1006 update_global_location_list (UGLL_MAY_INSERT);
1007 }
1008
1009 return;
1010 }
1011
1012 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
1013 what "auto" is translating to. */
1014
1015 static void
show_condition_evaluation_mode(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1016 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
1017 struct cmd_list_element *c, const char *value)
1018 {
1019 if (condition_evaluation_mode == condition_evaluation_auto)
1020 gdb_printf (file,
1021 _("Breakpoint condition evaluation "
1022 "mode is %s (currently %s).\n"),
1023 value,
1024 breakpoint_condition_evaluation_mode ());
1025 else
1026 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
1027 value);
1028 }
1029
1030 /* Parse COND_STRING in the context of LOC and set as the condition
1031 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
1032 the number of LOC within its owner. In case of parsing error, mark
1033 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
1034
1035 static void
set_breakpoint_location_condition(const char * cond_string,bp_location * loc,int bp_num,int loc_num)1036 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
1037 int bp_num, int loc_num)
1038 {
1039 bool has_junk = false;
1040 try
1041 {
1042 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
1043 block_for_pc (loc->address), 0);
1044 if (*cond_string != 0)
1045 has_junk = true;
1046 else
1047 {
1048 loc->cond = std::move (new_exp);
1049 if (loc->disabled_by_cond && loc->enabled)
1050 gdb_printf (_("Breakpoint %d's condition is now valid at "
1051 "location %d, enabling.\n"),
1052 bp_num, loc_num);
1053
1054 loc->disabled_by_cond = false;
1055 }
1056 }
1057 catch (const gdb_exception_error &e)
1058 {
1059 if (loc->enabled)
1060 {
1061 /* Warn if a user-enabled location is now becoming disabled-by-cond.
1062 BP_NUM is 0 if the breakpoint is being defined for the first
1063 time using the "break ... if ..." command, and non-zero if
1064 already defined. */
1065 if (bp_num != 0)
1066 warning (_("failed to validate condition at location %d.%d, "
1067 "disabling:\n %s"), bp_num, loc_num, e.what ());
1068 else
1069 warning (_("failed to validate condition at location %d, "
1070 "disabling:\n %s"), loc_num, e.what ());
1071 }
1072
1073 loc->disabled_by_cond = true;
1074 }
1075
1076 if (has_junk)
1077 error (_("Garbage '%s' follows condition"), cond_string);
1078 }
1079
1080 /* See breakpoint.h. */
1081
1082 void
notify_breakpoint_modified(breakpoint * b)1083 notify_breakpoint_modified (breakpoint *b)
1084 {
1085 interps_notify_breakpoint_modified (b);
1086 gdb::observers::breakpoint_modified.notify (b);
1087 }
1088
1089 void
set_breakpoint_condition(struct breakpoint * b,const char * exp,int from_tty,bool force)1090 set_breakpoint_condition (struct breakpoint *b, const char *exp,
1091 int from_tty, bool force)
1092 {
1093 if (*exp == 0)
1094 {
1095 b->cond_string.reset ();
1096
1097 if (is_watchpoint (b))
1098 gdb::checked_static_cast<watchpoint *> (b)->cond_exp.reset ();
1099 else
1100 {
1101 int loc_num = 1;
1102 for (bp_location &loc : b->locations ())
1103 {
1104 loc.cond.reset ();
1105 if (loc.disabled_by_cond && loc.enabled)
1106 gdb_printf (_("Breakpoint %d's condition is now valid at "
1107 "location %d, enabling.\n"),
1108 b->number, loc_num);
1109 loc.disabled_by_cond = false;
1110 loc_num++;
1111
1112 /* No need to free the condition agent expression
1113 bytecode (if we have one). We will handle this
1114 when we go through update_global_location_list. */
1115 }
1116 }
1117
1118 if (from_tty)
1119 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
1120 }
1121 else
1122 {
1123 if (is_watchpoint (b))
1124 {
1125 innermost_block_tracker tracker;
1126 const char *arg = exp;
1127 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
1128 if (*arg != 0)
1129 error (_("Junk at end of expression"));
1130 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
1131 w->cond_exp = std::move (new_exp);
1132 w->cond_exp_valid_block = tracker.block ();
1133 }
1134 else
1135 {
1136 /* Parse and set condition expressions. We make two passes.
1137 In the first, we parse the condition string to see if it
1138 is valid in at least one location. If so, the condition
1139 would be accepted. So we go ahead and set the locations'
1140 conditions. In case no valid case is found, we throw
1141 the error and the condition string will be rejected.
1142 This two-pass approach is taken to avoid setting the
1143 state of locations in case of a reject. */
1144 for (const bp_location &loc : b->locations ())
1145 {
1146 try
1147 {
1148 const char *arg = exp;
1149 parse_exp_1 (&arg, loc.address,
1150 block_for_pc (loc.address), 0);
1151 if (*arg != 0)
1152 error (_("Junk at end of expression"));
1153 break;
1154 }
1155 catch (const gdb_exception_error &e)
1156 {
1157 /* Condition string is invalid. If this happens to
1158 be the last loc, abandon (if not forced) or continue
1159 (if forced). */
1160 if (&loc == &b->last_loc () && !force)
1161 throw;
1162 }
1163 }
1164
1165 /* If we reach here, the condition is valid at some locations. */
1166 int loc_num = 1;
1167 for (bp_location &loc : b->locations ())
1168 {
1169 set_breakpoint_location_condition (exp, &loc, b->number, loc_num);
1170 loc_num++;
1171 }
1172 }
1173
1174 /* We know that the new condition parsed successfully. The
1175 condition string of the breakpoint can be safely updated. */
1176 b->cond_string = make_unique_xstrdup (exp);
1177 b->condition_not_parsed = 0;
1178 }
1179 mark_breakpoint_modified (b);
1180
1181 notify_breakpoint_modified (b);
1182 }
1183
1184 /* See breakpoint.h. */
1185
1186 void
set_breakpoint_condition(int bpnum,const char * exp,int from_tty,bool force)1187 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1188 bool force)
1189 {
1190 for (breakpoint &b : all_breakpoints ())
1191 if (b.number == bpnum)
1192 {
1193 /* Check if this breakpoint has a "stop" method implemented in an
1194 extension language. This method and conditions entered into GDB
1195 from the CLI are mutually exclusive. */
1196 const struct extension_language_defn *extlang
1197 = get_breakpoint_cond_ext_lang (&b, EXT_LANG_NONE);
1198
1199 if (extlang != NULL)
1200 {
1201 error (_("Only one stop condition allowed. There is currently"
1202 " a %s stop condition defined for this breakpoint."),
1203 ext_lang_capitalized_name (extlang));
1204 }
1205 set_breakpoint_condition (&b, exp, from_tty, force);
1206
1207 if (is_breakpoint (&b))
1208 update_global_location_list (UGLL_MAY_INSERT);
1209
1210 return;
1211 }
1212
1213 error (_("No breakpoint number %d."), bpnum);
1214 }
1215
1216 /* The options for the "condition" command. */
1217
1218 struct condition_command_opts
1219 {
1220 /* For "-force". */
1221 bool force_condition = false;
1222 };
1223
1224 static const gdb::option::option_def condition_command_option_defs[] = {
1225
1226 gdb::option::flag_option_def<condition_command_opts> {
1227 "force",
1228 [] (condition_command_opts *opts) { return &opts->force_condition; },
1229 N_("Set the condition even if it is invalid for all current locations."),
1230 },
1231
1232 };
1233
1234 /* Create an option_def_group for the "condition" options, with
1235 CC_OPTS as context. */
1236
1237 static inline gdb::option::option_def_group
make_condition_command_options_def_group(condition_command_opts * cc_opts)1238 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1239 {
1240 return {{condition_command_option_defs}, cc_opts};
1241 }
1242
1243 /* Completion for the "condition" command. */
1244
1245 static void
condition_completer(struct cmd_list_element * cmd,completion_tracker & tracker,const char * text,const char *)1246 condition_completer (struct cmd_list_element *cmd,
1247 completion_tracker &tracker,
1248 const char *text, const char * /*word*/)
1249 {
1250 bool has_no_arguments = (*text == '\0');
1251 condition_command_opts cc_opts;
1252 const auto group = make_condition_command_options_def_group (&cc_opts);
1253 if (gdb::option::complete_options
1254 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1255 return;
1256
1257 text = skip_spaces (text);
1258 const char *space = skip_to_space (text);
1259 if (*space == '\0')
1260 {
1261 int len;
1262
1263 if (text[0] == '$')
1264 {
1265 tracker.advance_custom_word_point_by (1);
1266 /* We don't support completion of history indices. */
1267 if (!isdigit (text[1]))
1268 complete_internalvar (tracker, &text[1]);
1269 return;
1270 }
1271
1272 /* Suggest the "-force" flag if no arguments are given. If
1273 arguments were passed, they either already include the flag,
1274 or we are beyond the point of suggesting it because it's
1275 positionally the first argument. */
1276 if (has_no_arguments)
1277 gdb::option::complete_on_all_options (tracker, group);
1278
1279 /* We're completing the breakpoint number. */
1280 len = strlen (text);
1281
1282 for (breakpoint &b : all_breakpoints ())
1283 {
1284 char number[50];
1285
1286 xsnprintf (number, sizeof (number), "%d", b.number);
1287
1288 if (strncmp (number, text, len) == 0)
1289 tracker.add_completion (make_unique_xstrdup (number));
1290 }
1291
1292 return;
1293 }
1294
1295 /* We're completing the expression part. Skip the breakpoint num. */
1296 const char *exp_start = skip_spaces (space);
1297 tracker.advance_custom_word_point_by (exp_start - text);
1298 text = exp_start;
1299 const char *word = advance_to_expression_complete_word_point (tracker, text);
1300 expression_completer (cmd, tracker, text, word);
1301 }
1302
1303 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1304
1305 static void
condition_command(const char * arg,int from_tty)1306 condition_command (const char *arg, int from_tty)
1307 {
1308 const char *p;
1309 int bnum;
1310
1311 if (arg == 0)
1312 error_no_arg (_("breakpoint number"));
1313
1314 p = arg;
1315
1316 /* Check if the "-force" flag was passed. */
1317 condition_command_opts cc_opts;
1318 const auto group = make_condition_command_options_def_group (&cc_opts);
1319 gdb::option::process_options
1320 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1321
1322 bnum = get_number (&p);
1323 if (bnum == 0)
1324 error (_("Bad breakpoint argument: '%s'"), arg);
1325
1326 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1327 }
1328
1329 /* Check that COMMAND do not contain commands that are suitable
1330 only for tracepoints and not suitable for ordinary breakpoints.
1331 Throw if any such commands is found. */
1332
1333 static void
check_no_tracepoint_commands(struct command_line * commands)1334 check_no_tracepoint_commands (struct command_line *commands)
1335 {
1336 struct command_line *c;
1337
1338 for (c = commands; c; c = c->next)
1339 {
1340 if (c->control_type == while_stepping_control)
1341 error (_("The 'while-stepping' command can "
1342 "only be used for tracepoints"));
1343
1344 check_no_tracepoint_commands (c->body_list_0.get ());
1345 check_no_tracepoint_commands (c->body_list_1.get ());
1346
1347 /* Not that command parsing removes leading whitespace and comment
1348 lines and also empty lines. So, we only need to check for
1349 command directly. */
1350 if (strstr (c->line, "collect ") == c->line)
1351 error (_("The 'collect' command can only be used for tracepoints"));
1352
1353 if (strstr (c->line, "teval ") == c->line)
1354 error (_("The 'teval' command can only be used for tracepoints"));
1355 }
1356 }
1357
1358 struct longjmp_breakpoint : public momentary_breakpoint
1359 {
1360 using momentary_breakpoint::momentary_breakpoint;
1361
1362 ~longjmp_breakpoint () override;
1363 };
1364
1365 /* Encapsulate tests for different types of tracepoints. */
1366
1367 static bool
is_tracepoint_type(bptype type)1368 is_tracepoint_type (bptype type)
1369 {
1370 return (type == bp_tracepoint
1371 || type == bp_fast_tracepoint
1372 || type == bp_static_tracepoint
1373 || type == bp_static_marker_tracepoint);
1374 }
1375
1376 /* See breakpoint.h. */
1377
1378 bool
is_tracepoint(const struct breakpoint * b)1379 is_tracepoint (const struct breakpoint *b)
1380 {
1381 return is_tracepoint_type (b->type);
1382 }
1383
1384 /* Factory function to create an appropriate instance of breakpoint given
1385 TYPE. */
1386
1387 template<typename... Arg>
1388 static std::unique_ptr<code_breakpoint>
new_breakpoint_from_type(struct gdbarch * gdbarch,bptype type,Arg &&...args)1389 new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type,
1390 Arg&&... args)
1391 {
1392 code_breakpoint *b;
1393
1394 switch (type)
1395 {
1396 case bp_breakpoint:
1397 case bp_hardware_breakpoint:
1398 b = new ordinary_breakpoint (gdbarch, type,
1399 std::forward<Arg> (args)...);
1400 break;
1401
1402 case bp_fast_tracepoint:
1403 case bp_static_tracepoint:
1404 case bp_tracepoint:
1405 b = new tracepoint (gdbarch, type,
1406 std::forward<Arg> (args)...);
1407 break;
1408
1409 case bp_static_marker_tracepoint:
1410 b = new static_marker_tracepoint (gdbarch, type,
1411 std::forward<Arg> (args)...);
1412 break;
1413
1414 case bp_dprintf:
1415 b = new dprintf_breakpoint (gdbarch, type,
1416 std::forward<Arg> (args)...);
1417 break;
1418
1419 default:
1420 gdb_assert_not_reached ("invalid type");
1421 }
1422
1423 return std::unique_ptr<code_breakpoint> (b);
1424 }
1425
1426 /* A helper function that validates that COMMANDS are valid for a
1427 breakpoint. This function will throw an exception if a problem is
1428 found. */
1429
1430 static void
validate_commands_for_breakpoint(struct breakpoint * b,struct command_line * commands)1431 validate_commands_for_breakpoint (struct breakpoint *b,
1432 struct command_line *commands)
1433 {
1434 if (is_tracepoint (b))
1435 {
1436 tracepoint *t = gdb::checked_static_cast<tracepoint *> (b);
1437 struct command_line *c;
1438 struct command_line *while_stepping = 0;
1439
1440 /* Reset the while-stepping step count. The previous commands
1441 might have included a while-stepping action, while the new
1442 ones might not. */
1443 t->step_count = 0;
1444
1445 /* We need to verify that each top-level element of commands is
1446 valid for tracepoints, that there's at most one
1447 while-stepping element, and that the while-stepping's body
1448 has valid tracing commands excluding nested while-stepping.
1449 We also need to validate the tracepoint action line in the
1450 context of the tracepoint --- validate_actionline actually
1451 has side effects, like setting the tracepoint's
1452 while-stepping STEP_COUNT, in addition to checking if the
1453 collect/teval actions parse and make sense in the
1454 tracepoint's context. */
1455 for (c = commands; c; c = c->next)
1456 {
1457 if (c->control_type == while_stepping_control)
1458 {
1459 if (b->type == bp_fast_tracepoint)
1460 error (_("The 'while-stepping' command "
1461 "cannot be used for fast tracepoint"));
1462 else if (b->type == bp_static_tracepoint
1463 || b->type == bp_static_marker_tracepoint)
1464 error (_("The 'while-stepping' command "
1465 "cannot be used for static tracepoint"));
1466
1467 if (while_stepping)
1468 error (_("The 'while-stepping' command "
1469 "can be used only once"));
1470 else
1471 while_stepping = c;
1472 }
1473
1474 validate_actionline (c->line, t);
1475 }
1476 if (while_stepping)
1477 {
1478 struct command_line *c2;
1479
1480 gdb_assert (while_stepping->body_list_1 == nullptr);
1481 c2 = while_stepping->body_list_0.get ();
1482 for (; c2; c2 = c2->next)
1483 {
1484 if (c2->control_type == while_stepping_control)
1485 error (_("The 'while-stepping' command cannot be nested"));
1486 }
1487 }
1488 }
1489 else
1490 {
1491 check_no_tracepoint_commands (commands);
1492 }
1493 }
1494
1495 /* Return a vector of all the static tracepoints set at ADDR. The
1496 caller is responsible for releasing the vector. */
1497
1498 std::vector<breakpoint *>
static_tracepoints_here(CORE_ADDR addr)1499 static_tracepoints_here (CORE_ADDR addr)
1500 {
1501 std::vector<breakpoint *> found;
1502
1503 for (breakpoint &b : all_breakpoints ())
1504 if (b.type == bp_static_tracepoint
1505 || b.type == bp_static_marker_tracepoint)
1506 {
1507 for (bp_location &loc : b.locations ())
1508 if (loc.address == addr)
1509 found.push_back (&b);
1510 }
1511
1512 return found;
1513 }
1514
1515 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1516 validate that only allowed commands are included. */
1517
1518 void
breakpoint_set_commands(struct breakpoint * b,counted_command_line && commands)1519 breakpoint_set_commands (struct breakpoint *b,
1520 counted_command_line &&commands)
1521 {
1522 validate_commands_for_breakpoint (b, commands.get ());
1523
1524 b->commands = std::move (commands);
1525 notify_breakpoint_modified (b);
1526 }
1527
1528 /* Set the internal `silent' flag on the breakpoint. Note that this
1529 is not the same as the "silent" that may appear in the breakpoint's
1530 commands. */
1531
1532 void
breakpoint_set_silent(struct breakpoint * b,int silent)1533 breakpoint_set_silent (struct breakpoint *b, int silent)
1534 {
1535 int old_silent = b->silent;
1536
1537 b->silent = silent;
1538 if (old_silent != silent)
1539 notify_breakpoint_modified (b);
1540 }
1541
1542 /* See breakpoint.h. */
1543
1544 void
breakpoint_set_thread(struct breakpoint * b,int thread)1545 breakpoint_set_thread (struct breakpoint *b, int thread)
1546 {
1547 /* THREAD should be -1, meaning no thread restriction, or it should be a
1548 valid global thread-id, which are greater than zero. */
1549 gdb_assert (thread == -1 || thread > 0);
1550
1551 /* It is not valid to set a thread restriction for a breakpoint that
1552 already has task or inferior restriction. */
1553 gdb_assert (thread == -1 || (b->task == -1 && b->inferior == -1));
1554
1555 int old_thread = b->thread;
1556 b->thread = thread;
1557 if (old_thread != thread)
1558 notify_breakpoint_modified (b);
1559 }
1560
1561 /* See breakpoint.h. */
1562
1563 void
breakpoint_set_inferior(struct breakpoint * b,int inferior)1564 breakpoint_set_inferior (struct breakpoint *b, int inferior)
1565 {
1566 /* INFERIOR should be -1, meaning no inferior restriction, or it should
1567 be a valid inferior number, which are greater than zero. */
1568 gdb_assert (inferior == -1 || inferior > 0);
1569
1570 /* It is not valid to set an inferior restriction for a breakpoint that
1571 already has a task or thread restriction. */
1572 gdb_assert (inferior == -1 || (b->task == -1 && b->thread == -1));
1573
1574 int old_inferior = b->inferior;
1575 b->inferior = inferior;
1576 if (old_inferior != inferior)
1577 notify_breakpoint_modified (b);
1578 }
1579
1580 /* See breakpoint.h. */
1581
1582 void
breakpoint_set_task(struct breakpoint * b,int task)1583 breakpoint_set_task (struct breakpoint *b, int task)
1584 {
1585 /* TASK should be -1, meaning no task restriction, or it should be a
1586 valid task-id, which are greater than zero. */
1587 gdb_assert (task == -1 || task > 0);
1588
1589 /* It is not valid to set a task restriction for a breakpoint that
1590 already has a thread or inferior restriction. */
1591 gdb_assert (task == -1 || (b->thread == -1 && b->inferior == -1));
1592
1593 int old_task = b->task;
1594 b->task = task;
1595 if (old_task != task)
1596 notify_breakpoint_modified (b);
1597 }
1598
1599 static void
commands_command_1(const char * arg,int from_tty,struct command_line * control)1600 commands_command_1 (const char *arg, int from_tty,
1601 struct command_line *control)
1602 {
1603 counted_command_line cmd;
1604 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1605 NULL after the call to read_command_lines if the user provides an empty
1606 list of command by just typing "end". */
1607 bool cmd_read = false;
1608
1609 std::string new_arg;
1610
1611 if (arg == NULL || !*arg)
1612 {
1613 /* Argument not explicitly given. Synthesize it. */
1614 if (breakpoint_count - prev_breakpoint_count > 1)
1615 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1616 breakpoint_count);
1617 else if (breakpoint_count > 0)
1618 new_arg = string_printf ("%d", breakpoint_count);
1619 }
1620 else
1621 {
1622 /* Create a copy of ARG. This is needed because the "commands"
1623 command may be coming from a script. In that case, the read
1624 line buffer is going to be overwritten in the lambda of
1625 'map_breakpoint_numbers' below when reading the next line
1626 before we are are done parsing the breakpoint numbers. */
1627 new_arg = arg;
1628 }
1629 arg = new_arg.c_str ();
1630
1631 map_breakpoint_numbers
1632 (arg, [&] (breakpoint *b)
1633 {
1634 if (!cmd_read)
1635 {
1636 gdb_assert (cmd == NULL);
1637 if (control != NULL)
1638 cmd = control->body_list_0;
1639 else
1640 {
1641 std::string str
1642 = string_printf (_("Type commands for breakpoint(s) "
1643 "%s, one per line."),
1644 arg);
1645
1646 auto do_validate = [=] (const char *line)
1647 {
1648 tracepoint *t
1649 = gdb::checked_static_cast<tracepoint *> (b);
1650 validate_actionline (line, t);
1651 };
1652 gdb::function_view<void (const char *)> validator;
1653 if (is_tracepoint (b))
1654 validator = do_validate;
1655
1656 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1657 }
1658 cmd_read = true;
1659 }
1660
1661 /* If a breakpoint was on the list more than once, we don't need to
1662 do anything. */
1663 if (b->commands != cmd)
1664 {
1665 validate_commands_for_breakpoint (b, cmd.get ());
1666 b->commands = cmd;
1667 notify_breakpoint_modified (b);
1668 }
1669 });
1670 }
1671
1672 static void
commands_command(const char * arg,int from_tty)1673 commands_command (const char *arg, int from_tty)
1674 {
1675 commands_command_1 (arg, from_tty, NULL);
1676 }
1677
1678 /* Like commands_command, but instead of reading the commands from
1679 input stream, takes them from an already parsed command structure.
1680
1681 This is used by cli-script.c to DTRT with breakpoint commands
1682 that are part of if and while bodies. */
1683 enum command_control_type
commands_from_control_command(const char * arg,struct command_line * cmd)1684 commands_from_control_command (const char *arg, struct command_line *cmd)
1685 {
1686 commands_command_1 (arg, 0, cmd);
1687 return simple_control;
1688 }
1689
1690 /* Return true if BL->TARGET_INFO contains valid information. */
1691
1692 static bool
bp_location_has_shadow(struct bp_location * bl)1693 bp_location_has_shadow (struct bp_location *bl)
1694 {
1695 if (bl->loc_type != bp_loc_software_breakpoint)
1696 return false;
1697 if (!bl->inserted)
1698 return false;
1699 if (bl->target_info.shadow_len == 0)
1700 /* BL isn't valid, or doesn't shadow memory. */
1701 return false;
1702 return true;
1703 }
1704
1705 /* Update BUF, which is LEN bytes read from the target address
1706 MEMADDR, by replacing a memory breakpoint with its shadowed
1707 contents.
1708
1709 If READBUF is not NULL, this buffer must not overlap with the of
1710 the breakpoint location's shadow_contents buffer. Otherwise, a
1711 failed assertion internal error will be raised. */
1712
1713 static void
one_breakpoint_xfer_memory(gdb_byte * readbuf,gdb_byte * writebuf,const gdb_byte * writebuf_org,ULONGEST memaddr,LONGEST len,struct bp_target_info * target_info,struct gdbarch * gdbarch)1714 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1715 const gdb_byte *writebuf_org,
1716 ULONGEST memaddr, LONGEST len,
1717 struct bp_target_info *target_info,
1718 struct gdbarch *gdbarch)
1719 {
1720 /* Now do full processing of the found relevant range of elements. */
1721 CORE_ADDR bp_addr = 0;
1722 int bp_size = 0;
1723 int bptoffset = 0;
1724
1725 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1726 current_program_space->aspace.get (), 0))
1727 {
1728 /* The breakpoint is inserted in a different address space. */
1729 return;
1730 }
1731
1732 /* Addresses and length of the part of the breakpoint that
1733 we need to copy. */
1734 bp_addr = target_info->placed_address;
1735 bp_size = target_info->shadow_len;
1736
1737 if (bp_addr + bp_size <= memaddr)
1738 {
1739 /* The breakpoint is entirely before the chunk of memory we are
1740 reading. */
1741 return;
1742 }
1743
1744 if (bp_addr >= memaddr + len)
1745 {
1746 /* The breakpoint is entirely after the chunk of memory we are
1747 reading. */
1748 return;
1749 }
1750
1751 /* Offset within shadow_contents. */
1752 if (bp_addr < memaddr)
1753 {
1754 /* Only copy the second part of the breakpoint. */
1755 bp_size -= memaddr - bp_addr;
1756 bptoffset = memaddr - bp_addr;
1757 bp_addr = memaddr;
1758 }
1759
1760 if (bp_addr + bp_size > memaddr + len)
1761 {
1762 /* Only copy the first part of the breakpoint. */
1763 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1764 }
1765
1766 if (readbuf != NULL)
1767 {
1768 /* Verify that the readbuf buffer does not overlap with the
1769 shadow_contents buffer. */
1770 gdb_assert (target_info->shadow_contents >= readbuf + len
1771 || readbuf >= (target_info->shadow_contents
1772 + target_info->shadow_len));
1773
1774 /* Update the read buffer with this inserted breakpoint's
1775 shadow. */
1776 memcpy (readbuf + bp_addr - memaddr,
1777 target_info->shadow_contents + bptoffset, bp_size);
1778 }
1779 else
1780 {
1781 const unsigned char *bp;
1782 CORE_ADDR addr = target_info->reqstd_address;
1783 int placed_size;
1784
1785 /* Update the shadow with what we want to write to memory. */
1786 memcpy (target_info->shadow_contents + bptoffset,
1787 writebuf_org + bp_addr - memaddr, bp_size);
1788
1789 /* Determine appropriate breakpoint contents and size for this
1790 address. */
1791 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1792
1793 /* Update the final write buffer with this inserted
1794 breakpoint's INSN. */
1795 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1796 }
1797 }
1798
1799 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1800 by replacing any memory breakpoints with their shadowed contents.
1801
1802 If READBUF is not NULL, this buffer must not overlap with any of
1803 the breakpoint location's shadow_contents buffers. Otherwise,
1804 a failed assertion internal error will be raised.
1805
1806 The range of shadowed area by each bp_location is:
1807 bl->address - bp_locations_placed_address_before_address_max
1808 up to bl->address + bp_locations_shadow_len_after_address_max
1809 The range we were requested to resolve shadows for is:
1810 memaddr ... memaddr + len
1811 Thus the safe cutoff boundaries for performance optimization are
1812 memaddr + len <= (bl->address
1813 - bp_locations_placed_address_before_address_max)
1814 and:
1815 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1816
1817 void
breakpoint_xfer_memory(gdb_byte * readbuf,gdb_byte * writebuf,const gdb_byte * writebuf_org,ULONGEST memaddr,LONGEST len)1818 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1819 const gdb_byte *writebuf_org,
1820 ULONGEST memaddr, LONGEST len)
1821 {
1822 /* Left boundary, right boundary and median element of our binary
1823 search. */
1824 unsigned bc_l, bc_r, bc;
1825
1826 /* Find BC_L which is a leftmost element which may affect BUF
1827 content. It is safe to report lower value but a failure to
1828 report higher one. */
1829
1830 bc_l = 0;
1831 bc_r = bp_locations.size ();
1832 while (bc_l + 1 < bc_r)
1833 {
1834 struct bp_location *bl;
1835
1836 bc = (bc_l + bc_r) / 2;
1837 bl = bp_locations[bc];
1838
1839 /* Check first BL->ADDRESS will not overflow due to the added
1840 constant. Then advance the left boundary only if we are sure
1841 the BC element can in no way affect the BUF content (MEMADDR
1842 to MEMADDR + LEN range).
1843
1844 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1845 offset so that we cannot miss a breakpoint with its shadow
1846 range tail still reaching MEMADDR. */
1847
1848 if ((bl->address + bp_locations_shadow_len_after_address_max
1849 >= bl->address)
1850 && (bl->address + bp_locations_shadow_len_after_address_max
1851 <= memaddr))
1852 bc_l = bc;
1853 else
1854 bc_r = bc;
1855 }
1856
1857 /* Due to the binary search above, we need to make sure we pick the
1858 first location that's at BC_L's address. E.g., if there are
1859 multiple locations at the same address, BC_L may end up pointing
1860 at a duplicate location, and miss the "master"/"inserted"
1861 location. Say, given locations L1, L2 and L3 at addresses A and
1862 B:
1863
1864 L1@A, L2@A, L3@B, ...
1865
1866 BC_L could end up pointing at location L2, while the "master"
1867 location could be L1. Since the `loc->inserted' flag is only set
1868 on "master" locations, we'd forget to restore the shadow of L1
1869 and L2. */
1870 while (bc_l > 0
1871 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1872 bc_l--;
1873
1874 /* Now do full processing of the found relevant range of elements. */
1875
1876 for (bc = bc_l; bc < bp_locations.size (); bc++)
1877 {
1878 struct bp_location *bl = bp_locations[bc];
1879
1880 /* bp_location array has BL->OWNER always non-NULL. */
1881 if (bl->owner->type == bp_none)
1882 warning (_("reading through apparently deleted breakpoint #%d?"),
1883 bl->owner->number);
1884
1885 /* Performance optimization: any further element can no longer affect BUF
1886 content. */
1887
1888 if (bl->address >= bp_locations_placed_address_before_address_max
1889 && (memaddr + len
1890 <= (bl->address
1891 - bp_locations_placed_address_before_address_max)))
1892 break;
1893
1894 if (!bp_location_has_shadow (bl))
1895 continue;
1896
1897 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1898 memaddr, len, &bl->target_info, bl->gdbarch);
1899 }
1900 }
1901
1902 /* See breakpoint.h. */
1903
1904 bool
is_breakpoint(const struct breakpoint * bpt)1905 is_breakpoint (const struct breakpoint *bpt)
1906 {
1907 return (bpt->type == bp_breakpoint
1908 || bpt->type == bp_hardware_breakpoint
1909 || bpt->type == bp_dprintf);
1910 }
1911
1912 /* Return true if BPT is of any hardware watchpoint kind. */
1913
1914 static bool
is_hardware_watchpoint(const struct breakpoint * bpt)1915 is_hardware_watchpoint (const struct breakpoint *bpt)
1916 {
1917 return (bpt->type == bp_hardware_watchpoint
1918 || bpt->type == bp_read_watchpoint
1919 || bpt->type == bp_access_watchpoint);
1920 }
1921
1922 /* See breakpoint.h. */
1923
1924 bool
is_watchpoint(const struct breakpoint * bpt)1925 is_watchpoint (const struct breakpoint *bpt)
1926 {
1927 return (is_hardware_watchpoint (bpt)
1928 || bpt->type == bp_watchpoint);
1929 }
1930
1931 /* Returns true if the current thread and its running state are safe
1932 to evaluate or update watchpoint B. Watchpoints on local
1933 expressions need to be evaluated in the context of the thread that
1934 was current when the watchpoint was created, and, that thread needs
1935 to be stopped to be able to select the correct frame context.
1936 Watchpoints on global expressions can be evaluated on any thread,
1937 and in any state. It is presently left to the target allowing
1938 memory accesses when threads are running. */
1939
1940 static bool
watchpoint_in_thread_scope(struct watchpoint * b)1941 watchpoint_in_thread_scope (struct watchpoint *b)
1942 {
1943 return (b->pspace == current_program_space
1944 && (b->watchpoint_thread == null_ptid
1945 || (inferior_ptid == b->watchpoint_thread
1946 && !inferior_thread ()->executing ())));
1947 }
1948
1949 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1950 associated bp_watchpoint_scope breakpoint. */
1951
1952 static void
watchpoint_del_at_next_stop(struct watchpoint * w)1953 watchpoint_del_at_next_stop (struct watchpoint *w)
1954 {
1955 if (w->related_breakpoint != w)
1956 {
1957 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1958 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1959 w->related_breakpoint->disposition = disp_del_at_next_stop;
1960 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1961 w->related_breakpoint = w;
1962 }
1963 w->disposition = disp_del_at_next_stop;
1964 disable_breakpoint (w);
1965 }
1966
1967 /* Extract a bitfield value from value VAL using the bit parameters contained in
1968 watchpoint W. */
1969
1970 static struct value *
extract_bitfield_from_watchpoint_value(struct watchpoint * w,struct value * val)1971 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1972 {
1973 struct value *bit_val;
1974
1975 if (val == NULL)
1976 return NULL;
1977
1978 bit_val = value::allocate (val->type ());
1979
1980 val->unpack_bitfield (bit_val,
1981 w->val_bitpos,
1982 w->val_bitsize,
1983 val->contents_for_printing ().data (),
1984 val->offset ());
1985
1986 return bit_val;
1987 }
1988
1989 /* Allocate a dummy location and add it to B. This is required
1990 because bpstat_stop_status requires a location to be able to report
1991 stops. */
1992
1993 static void
add_dummy_location(struct breakpoint * b,struct program_space * pspace)1994 add_dummy_location (struct breakpoint *b,
1995 struct program_space *pspace)
1996 {
1997 gdb_assert (!b->has_locations ());
1998
1999 bp_location *loc = new bp_location (b, bp_loc_other);
2000 loc->pspace = pspace;
2001 b->add_location (*loc);
2002 }
2003
2004 /* Assuming that B is a watchpoint:
2005 - Reparse watchpoint expression, if REPARSE is true
2006 - Evaluate expression and store the result in B->val
2007 - Evaluate the condition if there is one, and store the result
2008 in b->loc->cond.
2009 - Update the list of values that must be watched in B->loc.
2010
2011 If the watchpoint disposition is disp_del_at_next_stop, then do
2012 nothing. If this is local watchpoint that is out of scope, delete
2013 it.
2014
2015 Even with `set breakpoint always-inserted on' the watchpoints are
2016 removed + inserted on each stop here. Normal breakpoints must
2017 never be removed because they might be missed by a running thread
2018 when debugging in non-stop mode. On the other hand, hardware
2019 watchpoints (is_hardware_watchpoint; processed here) are specific
2020 to each LWP since they are stored in each LWP's hardware debug
2021 registers. Therefore, such LWP must be stopped first in order to
2022 be able to modify its hardware watchpoints.
2023
2024 Hardware watchpoints must be reset exactly once after being
2025 presented to the user. It cannot be done sooner, because it would
2026 reset the data used to present the watchpoint hit to the user. And
2027 it must not be done later because it could display the same single
2028 watchpoint hit during multiple GDB stops. Note that the latter is
2029 relevant only to the hardware watchpoint types bp_read_watchpoint
2030 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
2031 not user-visible - its hit is suppressed if the memory content has
2032 not changed.
2033
2034 The following constraints influence the location where we can reset
2035 hardware watchpoints:
2036
2037 * target_stopped_by_watchpoint and target_stopped_data_address are
2038 called several times when GDB stops.
2039
2040 [linux]
2041 * Multiple hardware watchpoints can be hit at the same time,
2042 causing GDB to stop. GDB only presents one hardware watchpoint
2043 hit at a time as the reason for stopping, and all the other hits
2044 are presented later, one after the other, each time the user
2045 requests the execution to be resumed. Execution is not resumed
2046 for the threads still having pending hit event stored in
2047 LWP_INFO->STATUS. While the watchpoint is already removed from
2048 the inferior on the first stop the thread hit event is kept being
2049 reported from its cached value by linux_nat_stopped_data_address
2050 until the real thread resume happens after the watchpoint gets
2051 presented and thus its LWP_INFO->STATUS gets reset.
2052
2053 Therefore the hardware watchpoint hit can get safely reset on the
2054 watchpoint removal from inferior. */
2055
2056 static void
update_watchpoint(struct watchpoint * b,bool reparse)2057 update_watchpoint (struct watchpoint *b, bool reparse)
2058 {
2059 bool within_current_scope;
2060
2061 /* If this is a local watchpoint, we only want to check if the
2062 watchpoint frame is in scope if the current thread is the thread
2063 that was used to create the watchpoint. */
2064 if (!watchpoint_in_thread_scope (b))
2065 return;
2066
2067 if (b->disposition == disp_del_at_next_stop)
2068 return;
2069
2070 std::optional<scoped_restore_selected_frame> restore_frame;
2071
2072 /* Determine if the watchpoint is within scope. */
2073 if (b->exp_valid_block == NULL)
2074 within_current_scope = true;
2075 else
2076 {
2077 frame_info_ptr fi = get_current_frame ();
2078 struct gdbarch *frame_arch = get_frame_arch (fi);
2079 CORE_ADDR frame_pc = get_frame_pc (fi);
2080
2081 /* If we're at a point where the stack has been destroyed
2082 (e.g. in a function epilogue), unwinding may not work
2083 properly. Do not attempt to recreate locations at this
2084 point. See similar comments in watchpoint_check. */
2085 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
2086 return;
2087
2088 /* Save the current frame's ID so we can restore it after
2089 evaluating the watchpoint expression on its own frame. */
2090 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
2091 took a frame parameter, so that we didn't have to change the
2092 selected frame. */
2093 restore_frame.emplace ();
2094
2095 fi = frame_find_by_id (b->watchpoint_frame);
2096 within_current_scope = (fi != NULL);
2097 if (within_current_scope)
2098 select_frame (fi);
2099 }
2100
2101 /* We don't free locations. They are stored in the bp_location array
2102 and update_global_location_list will eventually delete them and
2103 remove breakpoints if needed. */
2104 b->clear_locations ();
2105
2106 if (within_current_scope && reparse)
2107 {
2108 const char *s;
2109
2110 b->exp.reset ();
2111 s = (b->exp_string_reparse
2112 ? b->exp_string_reparse.get ()
2113 : b->exp_string.get ());
2114 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
2115 /* If the meaning of expression itself changed, the old value is
2116 no longer relevant. We don't want to report a watchpoint hit
2117 to the user when the old value and the new value may actually
2118 be completely different objects. */
2119 b->val = NULL;
2120 b->val_valid = false;
2121
2122 /* Note that unlike with breakpoints, the watchpoint's condition
2123 expression is stored in the breakpoint object, not in the
2124 locations (re)created below. */
2125 if (b->cond_string != NULL)
2126 {
2127 b->cond_exp.reset ();
2128
2129 s = b->cond_string.get ();
2130 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
2131 }
2132 }
2133
2134 /* If we failed to parse the expression, for example because
2135 it refers to a global variable in a not-yet-loaded shared library,
2136 don't try to insert watchpoint. We don't automatically delete
2137 such watchpoint, though, since failure to parse expression
2138 is different from out-of-scope watchpoint. */
2139 if (!target_has_execution ())
2140 {
2141 /* Without execution, memory can't change. No use to try and
2142 set watchpoint locations. The watchpoint will be reset when
2143 the target gains execution, through breakpoint_re_set. */
2144 if (!can_use_hw_watchpoints)
2145 {
2146 if (b->works_in_software_mode ())
2147 b->type = bp_watchpoint;
2148 else
2149 error (_("Can't set read/access watchpoint when "
2150 "hardware watchpoints are disabled."));
2151 }
2152 }
2153 else if (within_current_scope && b->exp)
2154 {
2155 std::vector<value_ref_ptr> val_chain;
2156 struct value *v, *result;
2157 struct program_space *wp_pspace;
2158
2159 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
2160 &val_chain, false);
2161
2162 /* Avoid setting b->val if it's already set. The meaning of
2163 b->val is 'the last value' user saw, and we should update
2164 it only if we reported that last value to user. As it
2165 happens, the code that reports it updates b->val directly.
2166 We don't keep track of the memory value for masked
2167 watchpoints. */
2168 if (!b->val_valid && !is_masked_watchpoint (b))
2169 {
2170 if (b->val_bitsize != 0)
2171 v = extract_bitfield_from_watchpoint_value (b, v);
2172 b->val = release_value (v);
2173 b->val_valid = true;
2174 }
2175
2176 if (b->exp_valid_block == nullptr)
2177 wp_pspace = current_program_space;
2178 else
2179 wp_pspace = get_frame_program_space (get_selected_frame (NULL));
2180
2181 /* Look at each value on the value chain. */
2182 gdb_assert (!val_chain.empty ());
2183 for (const value_ref_ptr &iter : val_chain)
2184 {
2185 v = iter.get ();
2186
2187 /* If it's a memory location, and GDB actually needed
2188 its contents to evaluate the expression, then we
2189 must watch it. If the first value returned is
2190 still lazy, that means an error occurred reading it;
2191 watch it anyway in case it becomes readable. */
2192 if (v->lval () == lval_memory
2193 && (v == val_chain[0] || ! v->lazy ()))
2194 {
2195 struct type *vtype = check_typedef (v->type ());
2196
2197 /* We only watch structs and arrays if user asked
2198 for it explicitly, never if they just happen to
2199 appear in the middle of some value chain. */
2200 if (v == result
2201 || (vtype->code () != TYPE_CODE_STRUCT
2202 && vtype->code () != TYPE_CODE_ARRAY))
2203 {
2204 CORE_ADDR addr;
2205 enum target_hw_bp_type type;
2206 int bitpos = 0, bitsize = 0;
2207
2208 if (v->bitsize () != 0)
2209 {
2210 /* Extract the bit parameters out from the bitfield
2211 sub-expression. */
2212 bitpos = v->bitpos ();
2213 bitsize = v->bitsize ();
2214 }
2215 else if (v == result && b->val_bitsize != 0)
2216 {
2217 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2218 lvalue whose bit parameters are saved in the fields
2219 VAL_BITPOS and VAL_BITSIZE. */
2220 bitpos = b->val_bitpos;
2221 bitsize = b->val_bitsize;
2222 }
2223
2224 addr = v->address ();
2225 if (bitsize != 0)
2226 {
2227 /* Skip the bytes that don't contain the bitfield. */
2228 addr += bitpos / 8;
2229 }
2230
2231 type = hw_write;
2232 if (b->type == bp_read_watchpoint)
2233 type = hw_read;
2234 else if (b->type == bp_access_watchpoint)
2235 type = hw_access;
2236
2237 bp_location *loc = b->allocate_location ();
2238 loc->gdbarch = v->type ()->arch ();
2239 loc->pspace = wp_pspace;
2240 loc->address
2241 = gdbarch_remove_non_address_bits (loc->gdbarch, addr);
2242 b->add_location (*loc);
2243
2244 if (bitsize != 0)
2245 {
2246 /* Just cover the bytes that make up the bitfield. */
2247 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2248 }
2249 else
2250 loc->length = v->type ()->length ();
2251
2252 loc->watchpoint_type = type;
2253 }
2254 }
2255 }
2256
2257 /* Helper function to bundle possibly emitting a warning along with
2258 changing the type of B to bp_watchpoint. */
2259 auto change_type_to_bp_watchpoint = [] (breakpoint *bp)
2260 {
2261 /* Only warn for breakpoints that have been assigned a +ve number,
2262 anything else is either an internal watchpoint (which we don't
2263 currently create) or has not yet been finalized, in which case
2264 this change of type will be occurring before the user is told
2265 the type of this watchpoint. */
2266 if (bp->type == bp_hardware_watchpoint && bp->number > 0)
2267 warning (_("watchpoint %d downgraded to software watchpoint"),
2268 bp->number);
2269 bp->type = bp_watchpoint;
2270 };
2271
2272 /* Change the type of breakpoint between hardware assisted or
2273 an ordinary watchpoint depending on the hardware support and
2274 free hardware slots. Recheck the number of free hardware slots
2275 as the value chain may have changed. */
2276 {
2277 int reg_cnt;
2278 enum bp_loc_type loc_type;
2279
2280 reg_cnt = can_use_hardware_watchpoint (val_chain);
2281
2282 if (reg_cnt)
2283 {
2284 int i, target_resources_ok, other_type_used;
2285 enum bptype type;
2286
2287 /* Use an exact watchpoint when there's only one memory region to be
2288 watched, and only one debug register is needed to watch it. */
2289 b->exact = target_exact_watchpoints && reg_cnt == 1;
2290
2291 /* We need to determine how many resources are already
2292 used for all other hardware watchpoints plus this one
2293 to see if we still have enough resources to also fit
2294 this watchpoint in as well. */
2295
2296 /* If this is a software watchpoint, we try to turn it
2297 to a hardware one -- count resources as if B was of
2298 hardware watchpoint type. */
2299 type = b->type;
2300 if (type == bp_watchpoint)
2301 type = bp_hardware_watchpoint;
2302
2303 /* This watchpoint may or may not have been placed on
2304 the list yet at this point (it won't be in the list
2305 if we're trying to create it for the first time,
2306 through watch_command), so always account for it
2307 manually. */
2308
2309 /* Count resources used by all watchpoints except B. */
2310 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2311
2312 /* Add in the resources needed for B. */
2313 i += hw_watchpoint_use_count (b);
2314
2315 target_resources_ok
2316 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2317 if (target_resources_ok <= 0)
2318 {
2319 bool sw_mode = b->works_in_software_mode ();
2320
2321 if (target_resources_ok == 0 && !sw_mode)
2322 error (_("Target does not support this type of "
2323 "hardware watchpoint."));
2324 else if (target_resources_ok < 0 && !sw_mode)
2325 error (_("There are not enough available hardware "
2326 "resources for this watchpoint."));
2327
2328 /* Downgrade to software watchpoint. */
2329 change_type_to_bp_watchpoint (b);
2330 }
2331 else
2332 {
2333 /* If this was a software watchpoint, we've just
2334 found we have enough resources to turn it to a
2335 hardware watchpoint. Otherwise, this is a
2336 nop. */
2337 b->type = type;
2338 }
2339 }
2340 else if (!b->works_in_software_mode ())
2341 {
2342 if (!can_use_hw_watchpoints)
2343 error (_("Can't set read/access watchpoint when "
2344 "hardware watchpoints are disabled."));
2345 else
2346 error (_("Expression cannot be implemented with "
2347 "read/access watchpoint."));
2348 }
2349 else
2350 change_type_to_bp_watchpoint (b);
2351
2352 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
2353 : bp_loc_hardware_watchpoint);
2354
2355 for (bp_location &bl : b->locations ())
2356 bl.loc_type = loc_type;
2357 }
2358
2359 /* If a software watchpoint is not watching any memory, then the
2360 above left it without any location set up. But,
2361 bpstat_stop_status requires a location to be able to report
2362 stops, so make sure there's at least a dummy one. */
2363 if (b->type == bp_watchpoint && !b->has_locations ())
2364 add_dummy_location (b, wp_pspace);
2365 }
2366 else if (!within_current_scope)
2367 {
2368 gdb_printf (_("\
2369 Watchpoint %d deleted because the program has left the block\n\
2370 in which its expression is valid.\n"),
2371 b->number);
2372 watchpoint_del_at_next_stop (b);
2373 }
2374 }
2375
2376 /* Returns true iff breakpoint location should be
2377 inserted in the inferior. We don't differentiate the type of BL's owner
2378 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2379 breakpoint_ops is not defined, because in insert_bp_location,
2380 tracepoint's insert_location will not be called. */
2381
2382 static bool
should_be_inserted(struct bp_location * bl)2383 should_be_inserted (struct bp_location *bl)
2384 {
2385 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2386 return false;
2387
2388 if (bl->owner->disposition == disp_del_at_next_stop)
2389 return false;
2390
2391 if (!bl->enabled || bl->disabled_by_cond
2392 || bl->shlib_disabled || bl->duplicate)
2393 return false;
2394
2395 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2396 return false;
2397
2398 /* This is set for example, when we're attached to the parent of a
2399 vfork, and have detached from the child. The child is running
2400 free, and we expect it to do an exec or exit, at which point the
2401 OS makes the parent schedulable again (and the target reports
2402 that the vfork is done). Until the child is done with the shared
2403 memory region, do not insert breakpoints in the parent, otherwise
2404 the child could still trip on the parent's breakpoints. Since
2405 the parent is blocked anyway, it won't miss any breakpoint. */
2406 if (bl->pspace->breakpoints_not_allowed)
2407 return false;
2408
2409 /* Don't insert a breakpoint if we're trying to step past its
2410 location, except if the breakpoint is a single-step breakpoint,
2411 and the breakpoint's thread is the thread which is stepping past
2412 a breakpoint. */
2413 if ((bl->loc_type == bp_loc_software_breakpoint
2414 || bl->loc_type == bp_loc_hardware_breakpoint)
2415 && stepping_past_instruction_at (bl->pspace->aspace.get (),
2416 bl->address)
2417 /* The single-step breakpoint may be inserted at the location
2418 we're trying to step if the instruction branches to itself.
2419 However, the instruction won't be executed at all and it may
2420 break the semantics of the instruction, for example, the
2421 instruction is a conditional branch or updates some flags.
2422 We can't fix it unless GDB is able to emulate the instruction
2423 or switch to displaced stepping. */
2424 && !(bl->owner->type == bp_single_step
2425 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2426 {
2427 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2428 paddress (bl->gdbarch, bl->address));
2429 return false;
2430 }
2431
2432 /* Don't insert watchpoints if we're trying to step past the
2433 instruction that triggered one. */
2434 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2435 && stepping_past_nonsteppable_watchpoint ())
2436 {
2437 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2438 "skipping watchpoint at %s:%d",
2439 paddress (bl->gdbarch, bl->address), bl->length);
2440 return false;
2441 }
2442
2443 return true;
2444 }
2445
2446 /* Same as should_be_inserted but does the check assuming
2447 that the location is not duplicated. */
2448
2449 static bool
unduplicated_should_be_inserted(struct bp_location * bl)2450 unduplicated_should_be_inserted (struct bp_location *bl)
2451 {
2452 scoped_restore restore_bl_duplicate
2453 = make_scoped_restore (&bl->duplicate, 0);
2454
2455 return should_be_inserted (bl);
2456 }
2457
2458 /* Parses a conditional described by an expression COND into an
2459 agent expression bytecode suitable for evaluation
2460 by the bytecode interpreter. Return NULL if there was
2461 any error during parsing. */
2462
2463 static agent_expr_up
parse_cond_to_aexpr(CORE_ADDR scope,struct expression * cond)2464 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2465 {
2466 if (cond == NULL)
2467 return NULL;
2468
2469 agent_expr_up aexpr;
2470
2471 /* We don't want to stop processing, so catch any errors
2472 that may show up. */
2473 try
2474 {
2475 aexpr = gen_eval_for_expr (scope, cond);
2476 }
2477
2478 catch (const gdb_exception_error &ex)
2479 {
2480 /* If we got here, it means the condition could not be parsed to a valid
2481 bytecode expression and thus can't be evaluated on the target's side.
2482 It's no use iterating through the conditions. */
2483 }
2484
2485 /* We have a valid agent expression. */
2486 return aexpr;
2487 }
2488
2489 /* Based on location BL, create a list of breakpoint conditions to be
2490 passed on to the target. If we have duplicated locations with different
2491 conditions, we will add such conditions to the list. The idea is that the
2492 target will evaluate the list of conditions and will only notify GDB when
2493 one of them is true. */
2494
2495 static void
build_target_condition_list(struct bp_location * bl)2496 build_target_condition_list (struct bp_location *bl)
2497 {
2498 bool null_condition_or_parse_error = false;
2499 int modified = bl->needs_update;
2500
2501 /* Release conditions left over from a previous insert. */
2502 bl->target_info.conditions.clear ();
2503
2504 /* This is only meaningful if the target is
2505 evaluating conditions and if the user has
2506 opted for condition evaluation on the target's
2507 side. */
2508 if (gdb_evaluates_breakpoint_condition_p ()
2509 || !target_supports_evaluation_of_breakpoint_conditions ())
2510 return;
2511
2512 auto loc_range = all_bp_locations_at_addr (bl->address);
2513
2514 /* Do a first pass to check for locations with no assigned
2515 conditions or conditions that fail to parse to a valid agent
2516 expression bytecode. If any of these happen, then it's no use to
2517 send conditions to the target since this location will always
2518 trigger and generate a response back to GDB. Note we consider
2519 all locations at the same address irrespective of type, i.e.,
2520 even if the locations aren't considered duplicates (e.g.,
2521 software breakpoint and hardware breakpoint at the same
2522 address). */
2523 for (bp_location *loc : loc_range)
2524 {
2525 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2526 {
2527 if (modified)
2528 {
2529 /* Re-parse the conditions since something changed. In that
2530 case we already freed the condition bytecodes (see
2531 force_breakpoint_reinsertion). We just
2532 need to parse the condition to bytecodes again. */
2533 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2534 loc->cond.get ());
2535 }
2536
2537 /* If we have a NULL bytecode expression, it means something
2538 went wrong or we have a null condition expression. */
2539 if (!loc->cond_bytecode)
2540 {
2541 null_condition_or_parse_error = true;
2542 break;
2543 }
2544 }
2545 }
2546
2547 /* If any of these happened, it means we will have to evaluate the conditions
2548 for the location's address on gdb's side. It is no use keeping bytecodes
2549 for all the other duplicate locations, thus we free all of them here.
2550
2551 This is so we have a finer control over which locations' conditions are
2552 being evaluated by GDB or the remote stub. */
2553 if (null_condition_or_parse_error)
2554 {
2555 for (bp_location *loc : loc_range)
2556 {
2557 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2558 {
2559 /* Only go as far as the first NULL bytecode is
2560 located. */
2561 if (!loc->cond_bytecode)
2562 return;
2563
2564 loc->cond_bytecode.reset ();
2565 }
2566 }
2567 }
2568
2569 /* No NULL conditions or failed bytecode generation. Build a
2570 condition list for this location's address. If we have software
2571 and hardware locations at the same address, they aren't
2572 considered duplicates, but we still merge all the conditions
2573 anyway, as it's simpler, and doesn't really make a practical
2574 difference. */
2575 for (bp_location *loc : loc_range)
2576 if (loc->cond
2577 && is_breakpoint (loc->owner)
2578 && loc->pspace->num == bl->pspace->num
2579 && loc->owner->enable_state == bp_enabled
2580 && loc->enabled
2581 && !loc->disabled_by_cond)
2582 {
2583 /* Add the condition to the vector. This will be used later
2584 to send the conditions to the target. */
2585 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2586 }
2587
2588 return;
2589 }
2590
2591 /* Parses a command described by string CMD into an agent expression
2592 bytecode suitable for evaluation by the bytecode interpreter.
2593 Return NULL if there was any error during parsing. */
2594
2595 static agent_expr_up
parse_cmd_to_aexpr(CORE_ADDR scope,char * cmd)2596 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2597 {
2598 const char *cmdrest;
2599 const char *format_start, *format_end;
2600 struct gdbarch *gdbarch = get_current_arch ();
2601
2602 if (cmd == NULL)
2603 return NULL;
2604
2605 cmdrest = cmd;
2606
2607 if (*cmdrest == ',')
2608 ++cmdrest;
2609 cmdrest = skip_spaces (cmdrest);
2610
2611 if (*cmdrest++ != '"')
2612 error (_("No format string following the location"));
2613
2614 format_start = cmdrest;
2615
2616 format_pieces fpieces (&cmdrest);
2617
2618 format_end = cmdrest;
2619
2620 if (*cmdrest++ != '"')
2621 error (_("Bad format string, non-terminated '\"'."));
2622
2623 cmdrest = skip_spaces (cmdrest);
2624
2625 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2626 error (_("Invalid argument syntax"));
2627
2628 if (*cmdrest == ',')
2629 cmdrest++;
2630 cmdrest = skip_spaces (cmdrest);
2631
2632 /* For each argument, make an expression. */
2633
2634 std::vector<struct expression *> argvec;
2635 while (*cmdrest != '\0')
2636 {
2637 const char *cmd1;
2638
2639 cmd1 = cmdrest;
2640 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope),
2641 PARSER_COMMA_TERMINATES);
2642 argvec.push_back (expr.release ());
2643 cmdrest = cmd1;
2644 if (*cmdrest == ',')
2645 ++cmdrest;
2646 }
2647
2648 agent_expr_up aexpr;
2649
2650 /* We don't want to stop processing, so catch any errors
2651 that may show up. */
2652 try
2653 {
2654 aexpr = gen_printf (scope, gdbarch, 0, 0,
2655 format_start, format_end - format_start,
2656 argvec.size (), argvec.data ());
2657 }
2658 catch (const gdb_exception_error &ex)
2659 {
2660 /* If we got here, it means the command could not be parsed to a valid
2661 bytecode expression and thus can't be evaluated on the target's side.
2662 It's no use iterating through the other commands. */
2663 }
2664
2665 /* We have a valid agent expression, return it. */
2666 return aexpr;
2667 }
2668
2669 /* Based on location BL, create a list of breakpoint commands to be
2670 passed on to the target. If we have duplicated locations with
2671 different commands, we will add any such to the list. */
2672
2673 static void
build_target_command_list(struct bp_location * bl)2674 build_target_command_list (struct bp_location *bl)
2675 {
2676 bool null_command_or_parse_error = false;
2677 int modified = bl->needs_update;
2678
2679 /* Clear commands left over from a previous insert. */
2680 bl->target_info.tcommands.clear ();
2681
2682 if (!target_can_run_breakpoint_commands ())
2683 return;
2684
2685 /* For now, limit to agent-style dprintf breakpoints. */
2686 if (dprintf_style != dprintf_style_agent)
2687 return;
2688
2689 auto loc_range = all_bp_locations_at_addr (bl->address);
2690
2691 /* For now, if we have any location at the same address that isn't a
2692 dprintf, don't install the target-side commands, as that would
2693 make the breakpoint not be reported to the core, and we'd lose
2694 control. */
2695 for (bp_location *loc : loc_range)
2696 if (is_breakpoint (loc->owner)
2697 && loc->pspace->num == bl->pspace->num
2698 && loc->owner->type != bp_dprintf)
2699 return;
2700
2701 /* Do a first pass to check for locations with no assigned
2702 conditions or conditions that fail to parse to a valid agent expression
2703 bytecode. If any of these happen, then it's no use to send conditions
2704 to the target since this location will always trigger and generate a
2705 response back to GDB. */
2706 for (bp_location *loc : loc_range)
2707 {
2708 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2709 {
2710 if (modified)
2711 {
2712 /* Re-parse the commands since something changed. In that
2713 case we already freed the command bytecodes (see
2714 force_breakpoint_reinsertion). We just
2715 need to parse the command to bytecodes again. */
2716 loc->cmd_bytecode
2717 = parse_cmd_to_aexpr (bl->address,
2718 loc->owner->extra_string.get ());
2719 }
2720
2721 /* If we have a NULL bytecode expression, it means something
2722 went wrong or we have a null command expression. */
2723 if (!loc->cmd_bytecode)
2724 {
2725 null_command_or_parse_error = true;
2726 break;
2727 }
2728 }
2729 }
2730
2731 /* If anything failed, then we're not doing target-side commands,
2732 and so clean up. */
2733 if (null_command_or_parse_error)
2734 {
2735 for (bp_location *loc : loc_range)
2736 if (is_breakpoint (loc->owner)
2737 && loc->pspace->num == bl->pspace->num)
2738 {
2739 /* Only go as far as the first NULL bytecode is
2740 located. */
2741 if (loc->cmd_bytecode == NULL)
2742 return;
2743
2744 loc->cmd_bytecode.reset ();
2745 }
2746 }
2747
2748 /* No NULL commands or failed bytecode generation. Build a command
2749 list for all duplicate locations at this location's address.
2750 Note that here we must care for whether the breakpoint location
2751 types are considered duplicates, otherwise, say, if we have a
2752 software and hardware location at the same address, the target
2753 could end up running the commands twice. For the moment, we only
2754 support targets-side commands with dprintf, but it doesn't hurt
2755 to be pedantically correct in case that changes. */
2756 for (bp_location *loc : loc_range)
2757 if (breakpoint_locations_match (bl, loc)
2758 && loc->owner->extra_string
2759 && is_breakpoint (loc->owner)
2760 && loc->pspace->num == bl->pspace->num
2761 && loc->owner->enable_state == bp_enabled
2762 && loc->enabled
2763 && !loc->disabled_by_cond)
2764 {
2765 /* Add the command to the vector. This will be used later
2766 to send the commands to the target. */
2767 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2768 }
2769
2770 bl->target_info.persist = 0;
2771 /* Maybe flag this location as persistent. */
2772 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2773 bl->target_info.persist = 1;
2774 }
2775
2776 /* Return the kind of breakpoint on address *ADDR. Get the kind
2777 of breakpoint according to ADDR except single-step breakpoint.
2778 Get the kind of single-step breakpoint according to the current
2779 registers state. */
2780
2781 static int
breakpoint_kind(const struct bp_location * bl,CORE_ADDR * addr)2782 breakpoint_kind (const struct bp_location *bl, CORE_ADDR *addr)
2783 {
2784 if (bl->owner->type == bp_single_step)
2785 {
2786 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2787 struct regcache *regcache;
2788
2789 regcache = get_thread_regcache (thr);
2790
2791 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2792 regcache, addr);
2793 }
2794 else
2795 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2796 }
2797
2798 /* Rethrow the currently handled exception, if it's a TARGET_CLOSE_ERROR.
2799 E is either the currently handled exception, or a copy, or a sliced copy,
2800 so we can't rethrow that one, but we can use it to inspect the properties
2801 of the currently handled exception. */
2802
2803 static void
rethrow_on_target_close_error(const gdb_exception & e)2804 rethrow_on_target_close_error (const gdb_exception &e)
2805 {
2806 if (e.reason == 0)
2807 return;
2808 /* Can't set the breakpoint. */
2809
2810 if (e.error != TARGET_CLOSE_ERROR)
2811 return;
2812
2813 /* If the target has closed then it will have deleted any breakpoints
2814 inserted within the target inferior, as a result any further attempts
2815 to interact with the breakpoint objects is not possible. Just rethrow
2816 the error. Don't use e to rethrow, to prevent object slicing of the
2817 exception. */
2818 throw;
2819 }
2820
2821 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2822 location. Any error messages are printed to TMP_ERROR_STREAM; and
2823 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2824 Returns 0 for success, 1 if the bp_location type is not supported or
2825 -1 for failure.
2826
2827 NOTE drow/2003-09-09: This routine could be broken down to an
2828 object-style method for each breakpoint or catchpoint type. */
2829 static int
insert_bp_location(struct bp_location * bl,struct ui_file * tmp_error_stream,int * disabled_breaks,int * hw_breakpoint_error,int * hw_bp_error_explained_already)2830 insert_bp_location (struct bp_location *bl,
2831 struct ui_file *tmp_error_stream,
2832 int *disabled_breaks,
2833 int *hw_breakpoint_error,
2834 int *hw_bp_error_explained_already)
2835 {
2836 gdb_exception bp_excpt;
2837
2838 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2839 return 0;
2840
2841 breakpoint_debug_printf ("%s", breakpoint_location_address_str (bl).c_str ());
2842
2843 /* Note we don't initialize bl->target_info, as that wipes out
2844 the breakpoint location's shadow_contents if the breakpoint
2845 is still inserted at that location. This in turn breaks
2846 target_read_memory which depends on these buffers when
2847 a memory read is requested at the breakpoint location:
2848 Once the target_info has been wiped, we fail to see that
2849 we have a breakpoint inserted at that address and thus
2850 read the breakpoint instead of returning the data saved in
2851 the breakpoint location's shadow contents. */
2852 bl->target_info.reqstd_address = bl->address;
2853 bl->target_info.placed_address_space = bl->pspace->aspace.get ();
2854 bl->target_info.length = bl->length;
2855
2856 /* When working with target-side conditions, we must pass all the conditions
2857 for the same breakpoint address down to the target since GDB will not
2858 insert those locations. With a list of breakpoint conditions, the target
2859 can decide when to stop and notify GDB. */
2860
2861 if (is_breakpoint (bl->owner))
2862 {
2863 build_target_condition_list (bl);
2864 build_target_command_list (bl);
2865 /* Reset the modification marker. */
2866 bl->needs_update = 0;
2867 }
2868
2869 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2870 set at a read-only address, then a breakpoint location will have
2871 been changed to hardware breakpoint before we get here. If it is
2872 "off" however, error out before actually trying to insert the
2873 breakpoint, with a nicer error message. */
2874 if (bl->loc_type == bp_loc_software_breakpoint
2875 && !automatic_hardware_breakpoints)
2876 {
2877 mem_region *mr = lookup_mem_region (bl->address);
2878
2879 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2880 {
2881 gdb_printf (tmp_error_stream,
2882 _("Cannot insert breakpoint %d.\n"
2883 "Cannot set software breakpoint "
2884 "at read-only address %s\n"),
2885 bl->owner->number,
2886 paddress (bl->gdbarch, bl->address));
2887 return 1;
2888 }
2889 }
2890
2891 if (bl->loc_type == bp_loc_software_breakpoint
2892 || bl->loc_type == bp_loc_hardware_breakpoint)
2893 {
2894 /* First check to see if we have to handle an overlay. */
2895 if (overlay_debugging == ovly_off
2896 || bl->section == NULL
2897 || !(section_is_overlay (bl->section)))
2898 {
2899 /* No overlay handling: just set the breakpoint. */
2900 try
2901 {
2902 int val;
2903
2904 val = bl->owner->insert_location (bl);
2905 if (val)
2906 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2907 }
2908 catch (gdb_exception &e)
2909 {
2910 rethrow_on_target_close_error (e);
2911 bp_excpt = std::move (e);
2912 }
2913 }
2914 else
2915 {
2916 /* This breakpoint is in an overlay section.
2917 Shall we set a breakpoint at the LMA? */
2918 if (!overlay_events_enabled)
2919 {
2920 /* Yes -- overlay event support is not active,
2921 so we must try to set a breakpoint at the LMA.
2922 This will not work for a hardware breakpoint. */
2923 if (bl->loc_type == bp_loc_hardware_breakpoint)
2924 warning (_("hardware breakpoint %d not supported in overlay!"),
2925 bl->owner->number);
2926 else
2927 {
2928 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2929 bl->section);
2930 /* Set a software (trap) breakpoint at the LMA. */
2931 bl->overlay_target_info = bl->target_info;
2932 bl->overlay_target_info.reqstd_address = addr;
2933
2934 /* No overlay handling: just set the breakpoint. */
2935 try
2936 {
2937 int val;
2938
2939 bl->overlay_target_info.kind
2940 = breakpoint_kind (bl, &addr);
2941 bl->overlay_target_info.placed_address = addr;
2942 val = target_insert_breakpoint (bl->gdbarch,
2943 &bl->overlay_target_info);
2944 if (val)
2945 bp_excpt
2946 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2947 }
2948 catch (gdb_exception &e)
2949 {
2950 rethrow_on_target_close_error (e);
2951 bp_excpt = std::move (e);
2952 }
2953
2954 if (bp_excpt.reason != 0)
2955 gdb_printf (tmp_error_stream,
2956 "Overlay breakpoint %d "
2957 "failed: in ROM?\n",
2958 bl->owner->number);
2959 }
2960 }
2961 /* Shall we set a breakpoint at the VMA? */
2962 if (section_is_mapped (bl->section))
2963 {
2964 /* Yes. This overlay section is mapped into memory. */
2965 try
2966 {
2967 int val;
2968
2969 val = bl->owner->insert_location (bl);
2970 if (val)
2971 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2972 }
2973 catch (gdb_exception_error &e)
2974 {
2975 rethrow_on_target_close_error (e);
2976 bp_excpt = std::move (e);
2977 }
2978 }
2979 else
2980 {
2981 /* No. This breakpoint will not be inserted.
2982 No error, but do not mark the bp as 'inserted'. */
2983 return 0;
2984 }
2985 }
2986
2987 if (bp_excpt.reason != 0)
2988 {
2989 /* Can't set the breakpoint. */
2990 gdb_assert (bl->owner != nullptr);
2991
2992 /* In some cases, we might not be able to insert a
2993 breakpoint in a shared library that has already been
2994 removed, but we have not yet processed the shlib unload
2995 event. Unfortunately, some targets that implement
2996 breakpoint insertion themselves can't tell why the
2997 breakpoint insertion failed (e.g., the remote target
2998 doesn't define error codes), so we must treat generic
2999 errors as memory errors. */
3000 if (bp_excpt.reason == RETURN_ERROR
3001 && (bp_excpt.error == GENERIC_ERROR
3002 || bp_excpt.error == MEMORY_ERROR)
3003 && bl->loc_type == bp_loc_software_breakpoint
3004 && (solib_name_from_address (bl->pspace, bl->address)
3005 || shared_objfile_contains_address_p (bl->pspace,
3006 bl->address)))
3007 {
3008 /* See also: disable_breakpoints_in_shlibs. */
3009 bl->shlib_disabled = 1;
3010 notify_breakpoint_modified (bl->owner);
3011 if (!*disabled_breaks)
3012 {
3013 gdb_printf (tmp_error_stream,
3014 "Cannot insert breakpoint %d.\n",
3015 bl->owner->number);
3016 gdb_printf (tmp_error_stream,
3017 "Temporarily disabling shared "
3018 "library breakpoints:\n");
3019 }
3020 *disabled_breaks = 1;
3021 gdb_printf (tmp_error_stream,
3022 "breakpoint #%d\n", bl->owner->number);
3023 return 0;
3024 }
3025 else
3026 {
3027 if (bl->loc_type == bp_loc_hardware_breakpoint)
3028 {
3029 *hw_breakpoint_error = 1;
3030 *hw_bp_error_explained_already = bp_excpt.message != NULL;
3031 gdb_printf (tmp_error_stream,
3032 "Cannot insert hardware breakpoint %d%s",
3033 bl->owner->number,
3034 bp_excpt.message ? ":" : ".\n");
3035 if (bp_excpt.message != NULL)
3036 gdb_printf (tmp_error_stream, "%s.\n",
3037 bp_excpt.what ());
3038 }
3039 else
3040 {
3041 if (bp_excpt.message == NULL)
3042 {
3043 std::string message
3044 = memory_error_message (TARGET_XFER_E_IO,
3045 bl->gdbarch, bl->address);
3046
3047 gdb_printf (tmp_error_stream,
3048 "Cannot insert breakpoint %d.\n"
3049 "%s\n",
3050 bl->owner->number, message.c_str ());
3051 }
3052 else
3053 {
3054 gdb_printf (tmp_error_stream,
3055 "Cannot insert breakpoint %d: %s\n",
3056 bl->owner->number,
3057 bp_excpt.what ());
3058 }
3059 }
3060 return 1;
3061
3062 }
3063 }
3064 else
3065 bl->inserted = 1;
3066
3067 return 0;
3068 }
3069
3070 else if (bl->loc_type == bp_loc_hardware_watchpoint
3071 && bl->owner->disposition != disp_del_at_next_stop)
3072 {
3073 int val;
3074
3075 val = bl->owner->insert_location (bl);
3076
3077 /* If trying to set a read-watchpoint, and it turns out it's not
3078 supported, try emulating one with an access watchpoint. */
3079 if (val == 1 && bl->watchpoint_type == hw_read)
3080 {
3081 /* But don't try to insert it, if there's already another
3082 hw_access location that would be considered a duplicate
3083 of this one. */
3084 for (bp_location *loc : all_bp_locations ())
3085 if (loc != bl
3086 && loc->watchpoint_type == hw_access
3087 && watchpoint_locations_match (bl, loc))
3088 {
3089 bl->duplicate = 1;
3090 bl->inserted = 1;
3091 bl->target_info = loc->target_info;
3092 bl->watchpoint_type = hw_access;
3093 val = 0;
3094 break;
3095 }
3096
3097 if (val == 1)
3098 {
3099 bl->watchpoint_type = hw_access;
3100 val = bl->owner->insert_location (bl);
3101
3102 if (val)
3103 /* Back to the original value. */
3104 bl->watchpoint_type = hw_read;
3105 }
3106 }
3107
3108 bl->inserted = (val == 0);
3109 }
3110
3111 else if (bl->owner->type == bp_catchpoint)
3112 {
3113 int val;
3114
3115 val = bl->owner->insert_location (bl);
3116 if (val)
3117 {
3118 bl->owner->enable_state = bp_disabled;
3119
3120 if (val == 1)
3121 warning (_("\
3122 Error inserting catchpoint %d: Your system does not support this type\n\
3123 of catchpoint."), bl->owner->number);
3124 else
3125 warning (_("Error inserting catchpoint %d."), bl->owner->number);
3126 }
3127
3128 bl->inserted = (val == 0);
3129
3130 /* We've already printed an error message if there was a problem
3131 inserting this catchpoint, and we've disabled the catchpoint,
3132 so just return success. */
3133 return 0;
3134 }
3135
3136 return 0;
3137 }
3138
3139 /* This function is called when program space PSPACE is about to be
3140 deleted. It takes care of updating breakpoints to not reference
3141 PSPACE anymore. */
3142
3143 void
breakpoint_program_space_exit(struct program_space * pspace)3144 breakpoint_program_space_exit (struct program_space *pspace)
3145 {
3146 /* Remove any breakpoint that was set through this program space. */
3147 for (breakpoint &b : all_breakpoints_safe ())
3148 if (b.pspace == pspace)
3149 delete_breakpoint (&b);
3150
3151 /* Breakpoints set through other program spaces could have locations
3152 bound to PSPACE as well. Remove those. */
3153 for (bp_location *loc : all_bp_locations ())
3154 if (loc->pspace == pspace)
3155 {
3156 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3157 loc->owner->unadd_location (*loc);
3158 }
3159
3160 /* Now update the global location list to permanently delete the
3161 removed locations above. */
3162 update_global_location_list (UGLL_DONT_INSERT);
3163 }
3164
3165 /* Make sure all breakpoints are inserted in inferior.
3166 Throws exception on any error.
3167 A breakpoint that is already inserted won't be inserted
3168 again, so calling this function twice is safe. */
3169 void
insert_breakpoints(void)3170 insert_breakpoints (void)
3171 {
3172 for (breakpoint &bpt : all_breakpoints ())
3173 if (is_hardware_watchpoint (&bpt))
3174 {
3175 watchpoint &w = gdb::checked_static_cast<watchpoint &> (bpt);
3176
3177 update_watchpoint (&w, false /* don't reparse. */);
3178 }
3179
3180 /* Updating watchpoints creates new locations, so update the global
3181 location list. Explicitly tell ugll to insert locations and
3182 ignore breakpoints_always_inserted_mode. Also,
3183 update_global_location_list tries to "upgrade" software
3184 breakpoints to hardware breakpoints to handle "set breakpoint
3185 auto-hw", so we need to call it even if we don't have new
3186 locations. */
3187 update_global_location_list (UGLL_INSERT);
3188 }
3189
3190 /* This is used when we need to synch breakpoint conditions between GDB and the
3191 target. It is the case with deleting and disabling of breakpoints when using
3192 always-inserted mode. */
3193
3194 static void
update_inserted_breakpoint_locations(void)3195 update_inserted_breakpoint_locations (void)
3196 {
3197 int error_flag = 0;
3198 int val = 0;
3199 int disabled_breaks = 0;
3200 int hw_breakpoint_error = 0;
3201 int hw_bp_details_reported = 0;
3202
3203 string_file tmp_error_stream;
3204
3205 /* Explicitly mark the warning -- this will only be printed if
3206 there was an error. */
3207 tmp_error_stream.puts ("Warning:\n");
3208
3209 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3210
3211 for (bp_location *bl : all_bp_locations ())
3212 {
3213 /* We only want to update software breakpoints and hardware
3214 breakpoints. */
3215 if (!is_breakpoint (bl->owner))
3216 continue;
3217
3218 /* We only want to update locations that are already inserted
3219 and need updating. This is to avoid unwanted insertion during
3220 deletion of breakpoints. */
3221 if (!bl->inserted || !bl->needs_update)
3222 continue;
3223
3224 switch_to_program_space_and_thread (bl->pspace);
3225
3226 /* For targets that support global breakpoints, there's no need
3227 to select an inferior to insert breakpoint to. In fact, even
3228 if we aren't attached to any process yet, we should still
3229 insert breakpoints. */
3230 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ())
3231 && (inferior_ptid == null_ptid || !target_has_execution ()))
3232 continue;
3233
3234 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3235 &hw_breakpoint_error, &hw_bp_details_reported);
3236 if (val)
3237 error_flag = val;
3238 }
3239
3240 if (error_flag)
3241 {
3242 target_terminal::ours_for_output ();
3243 error (("%s"), tmp_error_stream.c_str ());
3244 }
3245 }
3246
3247 /* Used when starting or continuing the program. */
3248
3249 static void
insert_breakpoint_locations(void)3250 insert_breakpoint_locations (void)
3251 {
3252 int error_flag = 0;
3253 int val = 0;
3254 int disabled_breaks = 0;
3255 int hw_breakpoint_error = 0;
3256 int hw_bp_error_explained_already = 0;
3257
3258 string_file tmp_error_stream;
3259
3260 /* Explicitly mark the warning -- this will only be printed if
3261 there was an error. */
3262 tmp_error_stream.puts ("Warning:\n");
3263
3264 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3265
3266 for (bp_location *bl : all_bp_locations ())
3267 {
3268 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3269 continue;
3270
3271 /* There is no point inserting thread-specific breakpoints if
3272 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3273 has BL->OWNER always non-NULL. */
3274 if (bl->owner->thread != -1
3275 && !valid_global_thread_id (bl->owner->thread))
3276 continue;
3277
3278 /* Or inferior specific breakpoints if the inferior no longer
3279 exists. */
3280 if (bl->owner->inferior != -1
3281 && !valid_global_inferior_id (bl->owner->inferior))
3282 continue;
3283
3284 switch_to_program_space_and_thread (bl->pspace);
3285
3286 /* For targets that support global breakpoints, there's no need
3287 to select an inferior to insert breakpoint to. In fact, even
3288 if we aren't attached to any process yet, we should still
3289 insert breakpoints. */
3290 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ())
3291 && (inferior_ptid == null_ptid || !target_has_execution ()))
3292 continue;
3293
3294 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3295 &hw_breakpoint_error, &hw_bp_error_explained_already);
3296 if (val)
3297 error_flag = val;
3298 }
3299
3300 /* If we failed to insert all locations of a watchpoint, remove
3301 them, as half-inserted watchpoint is of limited use. */
3302 for (breakpoint &bpt : all_breakpoints ())
3303 {
3304 bool some_failed = false;
3305
3306 if (!is_hardware_watchpoint (&bpt))
3307 continue;
3308
3309 if (!breakpoint_enabled (&bpt))
3310 continue;
3311
3312 if (bpt.disposition == disp_del_at_next_stop)
3313 continue;
3314
3315 for (bp_location &loc : bpt.locations ())
3316 if (!loc.inserted && should_be_inserted (&loc))
3317 {
3318 some_failed = true;
3319 break;
3320 }
3321
3322 if (some_failed)
3323 {
3324 for (bp_location &loc : bpt.locations ())
3325 if (loc.inserted)
3326 remove_breakpoint (&loc);
3327
3328 hw_breakpoint_error = 1;
3329 tmp_error_stream.printf ("Could not insert "
3330 "hardware watchpoint %d.\n",
3331 bpt.number);
3332 error_flag = -1;
3333 }
3334 }
3335
3336 if (error_flag)
3337 {
3338 /* If a hardware breakpoint or watchpoint was inserted, add a
3339 message about possibly exhausted resources. */
3340 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3341 {
3342 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3343 You may have requested too many hardware breakpoints/watchpoints.\n");
3344 }
3345 target_terminal::ours_for_output ();
3346 error (("%s"), tmp_error_stream.c_str ());
3347 }
3348 }
3349
3350 /* Used when the program stops.
3351 Returns zero if successful, or non-zero if there was a problem
3352 removing a breakpoint location. */
3353
3354 int
remove_breakpoints(void)3355 remove_breakpoints (void)
3356 {
3357 int val = 0;
3358
3359 for (bp_location *bl : all_bp_locations ())
3360 if (bl->inserted && !is_tracepoint (bl->owner))
3361 val |= remove_breakpoint (bl);
3362
3363 return val;
3364 }
3365
3366 /* When a thread exits, remove breakpoints that are related to
3367 that thread. */
3368
3369 static void
remove_threaded_breakpoints(thread_info * tp,std::optional<ULONGEST>,int)3370 remove_threaded_breakpoints (thread_info *tp,
3371 std::optional<ULONGEST> /* exit_code */,
3372 int /* silent */)
3373 {
3374 for (breakpoint &b : all_breakpoints_safe ())
3375 {
3376 if (b.thread == tp->global_num && user_breakpoint_p (&b))
3377 {
3378 gdb_printf (_("\
3379 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3380 b.number, print_thread_id (tp));
3381 delete_breakpoint (&b);
3382 }
3383 }
3384 }
3385
3386 /* Called when inferior INF has been removed from GDB. Remove associated
3387 per-inferior breakpoints. */
3388
3389 static void
remove_inferior_breakpoints(struct inferior * inf)3390 remove_inferior_breakpoints (struct inferior *inf)
3391 {
3392 for (breakpoint &b : all_breakpoints_safe ())
3393 {
3394 if (b.inferior == inf->num && user_breakpoint_p (&b))
3395 {
3396 /* Tell the user the breakpoint has been deleted. But only for
3397 breakpoints that would not normally have been deleted at the
3398 next stop anyway. */
3399 if (b.disposition != disp_del
3400 && b.disposition != disp_del_at_next_stop)
3401 gdb_printf (_("\
3402 Inferior-specific breakpoint %d deleted - inferior %d has been removed.\n"),
3403 b.number, inf->num);
3404 delete_breakpoint (&b);
3405 }
3406 }
3407 }
3408
3409 /* See breakpoint.h. */
3410
3411 void
remove_breakpoints_inf(inferior * inf)3412 remove_breakpoints_inf (inferior *inf)
3413 {
3414 int val;
3415
3416 breakpoint_debug_printf ("inf->num = %d", inf->num);
3417
3418 for (bp_location *bl : all_bp_locations ())
3419 {
3420 if (bl->pspace != inf->pspace)
3421 continue;
3422
3423 if (bl->inserted && !bl->target_info.persist)
3424 {
3425 val = remove_breakpoint (bl);
3426 if (val != 0)
3427 return;
3428 }
3429 }
3430 }
3431
3432 static int internal_breakpoint_number = -1;
3433
3434 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3435 If INTERNAL is non-zero, the breakpoint number will be populated
3436 from internal_breakpoint_number and that variable decremented.
3437 Otherwise the breakpoint number will be populated from
3438 breakpoint_count and that value incremented. Internal breakpoints
3439 do not set the internal var bpnum. */
3440 static void
set_breakpoint_number(int internal,struct breakpoint * b)3441 set_breakpoint_number (int internal, struct breakpoint *b)
3442 {
3443 if (internal)
3444 b->number = internal_breakpoint_number--;
3445 else
3446 {
3447 set_breakpoint_count (breakpoint_count + 1);
3448 b->number = breakpoint_count;
3449 }
3450 }
3451
3452 /* Create a TYPE breakpoint on ADDRESS from an object file with GDBARCH. */
3453
3454 static struct breakpoint *
create_internal_breakpoint(struct gdbarch * gdbarch,CORE_ADDR address,enum bptype type)3455 create_internal_breakpoint (struct gdbarch *gdbarch,
3456 CORE_ADDR address, enum bptype type)
3457 {
3458 std::unique_ptr<internal_breakpoint> b
3459 (new internal_breakpoint (gdbarch, type, address));
3460
3461 b->number = internal_breakpoint_number--;
3462
3463 return add_to_breakpoint_chain (std::move (b));
3464 }
3465
3466 /* Create a TYPE breakpoint on minimal symbol MSYM from an object file with
3467 GDBARCH. */
3468
3469 static struct breakpoint *
create_internal_breakpoint(struct gdbarch * gdbarch,struct bound_minimal_symbol & msym,enum bptype type)3470 create_internal_breakpoint (struct gdbarch *gdbarch,
3471 struct bound_minimal_symbol &msym, enum bptype type)
3472 {
3473 CORE_ADDR address;
3474
3475 address = msym.value_address ();
3476
3477 address = gdbarch_convert_from_func_ptr_addr
3478 (gdbarch, address, current_inferior ()->top_target ());
3479
3480 /* Note that we're not using gdbarch_addr_bits_remove here, because that's
3481 related to addresses in $pc. We're getting the address from the
3482 minimal symbol table. */
3483
3484 /* Is gdbarch_deprecated_function_start_offset needed here? Or is that dealt
3485 with elsewhere? Needs testing on vax. */
3486
3487 if (gdbarch_skip_entrypoint_p (gdbarch))
3488 address = gdbarch_skip_entrypoint (gdbarch, address);
3489
3490 return create_internal_breakpoint (gdbarch, address, type);
3491 }
3492
3493 static const char *const longjmp_names[] =
3494 {
3495 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3496 };
3497 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3498
3499 /* Per-objfile data private to breakpoint.c. */
3500 struct breakpoint_objfile_data
3501 {
3502 /* Minimal symbol for "_ovly_debug_event" (if any). */
3503 struct bound_minimal_symbol overlay_msym;
3504
3505 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3506 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3507
3508 /* True if we have looked for longjmp probes. */
3509 int longjmp_searched = 0;
3510
3511 /* SystemTap probe points for longjmp (if any). These are non-owning
3512 references. */
3513 std::vector<probe *> longjmp_probes;
3514
3515 /* Minimal symbol for "std::terminate()" (if any). */
3516 struct bound_minimal_symbol terminate_msym;
3517
3518 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3519 struct bound_minimal_symbol exception_msym;
3520
3521 /* True if we have looked for exception probes. */
3522 int exception_searched = 0;
3523
3524 /* SystemTap probe points for unwinding (if any). These are non-owning
3525 references. */
3526 std::vector<probe *> exception_probes;
3527 };
3528
3529 static const registry<objfile>::key<breakpoint_objfile_data>
3530 breakpoint_objfile_key;
3531
3532 /* Minimal symbol not found sentinel. */
3533 static struct minimal_symbol msym_not_found;
3534
3535 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3536
3537 static bool
msym_not_found_p(const struct minimal_symbol * msym)3538 msym_not_found_p (const struct minimal_symbol *msym)
3539 {
3540 return msym == &msym_not_found;
3541 }
3542
3543 /* Return per-objfile data needed by breakpoint.c.
3544 Allocate the data if necessary. */
3545
3546 static struct breakpoint_objfile_data *
get_breakpoint_objfile_data(struct objfile * objfile)3547 get_breakpoint_objfile_data (struct objfile *objfile)
3548 {
3549 struct breakpoint_objfile_data *bp_objfile_data;
3550
3551 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3552 if (bp_objfile_data == NULL)
3553 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3554 return bp_objfile_data;
3555 }
3556
3557 static void
create_overlay_event_breakpoint(void)3558 create_overlay_event_breakpoint (void)
3559 {
3560 const char *const func_name = "_ovly_debug_event";
3561
3562 for (objfile *objfile : current_program_space->objfiles ())
3563 {
3564 struct breakpoint *b;
3565 struct breakpoint_objfile_data *bp_objfile_data;
3566 CORE_ADDR addr;
3567
3568 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3569
3570 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3571 continue;
3572
3573 if (bp_objfile_data->overlay_msym.minsym == NULL)
3574 {
3575 struct bound_minimal_symbol m;
3576
3577 m = lookup_minimal_symbol_text (func_name, objfile);
3578 if (m.minsym == NULL)
3579 {
3580 /* Avoid future lookups in this objfile. */
3581 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3582 continue;
3583 }
3584 bp_objfile_data->overlay_msym = m;
3585 }
3586
3587 addr = bp_objfile_data->overlay_msym.value_address ();
3588 b = create_internal_breakpoint (objfile->arch (), addr,
3589 bp_overlay_event);
3590 b->locspec = new_explicit_location_spec_function (func_name);
3591
3592 if (overlay_debugging == ovly_auto)
3593 {
3594 b->enable_state = bp_enabled;
3595 overlay_events_enabled = 1;
3596 }
3597 else
3598 {
3599 b->enable_state = bp_disabled;
3600 overlay_events_enabled = 0;
3601 }
3602 }
3603 }
3604
3605 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3606 true if a breakpoint was installed. */
3607
3608 static bool
create_longjmp_master_breakpoint_probe(objfile * objfile)3609 create_longjmp_master_breakpoint_probe (objfile *objfile)
3610 {
3611 struct gdbarch *gdbarch = objfile->arch ();
3612 struct breakpoint_objfile_data *bp_objfile_data
3613 = get_breakpoint_objfile_data (objfile);
3614
3615 if (!bp_objfile_data->longjmp_searched)
3616 {
3617 std::vector<probe *> ret
3618 = find_probes_in_objfile (objfile, "libc", "longjmp");
3619
3620 if (!ret.empty ())
3621 {
3622 /* We are only interested in checking one element. */
3623 probe *p = ret[0];
3624
3625 if (!p->can_evaluate_arguments ())
3626 {
3627 /* We cannot use the probe interface here,
3628 because it does not know how to evaluate
3629 arguments. */
3630 ret.clear ();
3631 }
3632 }
3633 bp_objfile_data->longjmp_probes = ret;
3634 bp_objfile_data->longjmp_searched = 1;
3635 }
3636
3637 if (bp_objfile_data->longjmp_probes.empty ())
3638 return false;
3639
3640 for (probe *p : bp_objfile_data->longjmp_probes)
3641 {
3642 struct breakpoint *b;
3643
3644 b = create_internal_breakpoint (gdbarch,
3645 p->get_relocated_address (objfile),
3646 bp_longjmp_master);
3647 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
3648 b->enable_state = bp_disabled;
3649 }
3650
3651 return true;
3652 }
3653
3654 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3655 Return true if at least one breakpoint was installed. */
3656
3657 static bool
create_longjmp_master_breakpoint_names(objfile * objfile)3658 create_longjmp_master_breakpoint_names (objfile *objfile)
3659 {
3660 struct gdbarch *gdbarch = objfile->arch ();
3661 if (!gdbarch_get_longjmp_target_p (gdbarch))
3662 return false;
3663
3664 struct breakpoint_objfile_data *bp_objfile_data
3665 = get_breakpoint_objfile_data (objfile);
3666 unsigned int installed_bp = 0;
3667
3668 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3669 {
3670 struct breakpoint *b;
3671 const char *func_name;
3672 CORE_ADDR addr;
3673
3674 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3675 continue;
3676
3677 func_name = longjmp_names[i];
3678 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3679 {
3680 struct bound_minimal_symbol m;
3681
3682 m = lookup_minimal_symbol_text (func_name, objfile);
3683 if (m.minsym == NULL)
3684 {
3685 /* Prevent future lookups in this objfile. */
3686 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3687 continue;
3688 }
3689 bp_objfile_data->longjmp_msym[i] = m;
3690 }
3691
3692 addr = bp_objfile_data->longjmp_msym[i].value_address ();
3693 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
3694 b->locspec = new_explicit_location_spec_function (func_name);
3695 b->enable_state = bp_disabled;
3696 installed_bp++;
3697 }
3698
3699 return installed_bp > 0;
3700 }
3701
3702 /* Create a master longjmp breakpoint. */
3703
3704 static void
create_longjmp_master_breakpoint(void)3705 create_longjmp_master_breakpoint (void)
3706 {
3707 scoped_restore_current_program_space restore_pspace;
3708
3709 for (struct program_space *pspace : program_spaces)
3710 {
3711 set_current_program_space (pspace);
3712
3713 for (objfile *obj : current_program_space->objfiles ())
3714 {
3715 /* Skip separate debug object, it's handled in the loop below. */
3716 if (obj->separate_debug_objfile_backlink != nullptr)
3717 continue;
3718
3719 /* Try a probe kind breakpoint on main objfile. */
3720 if (create_longjmp_master_breakpoint_probe (obj))
3721 continue;
3722
3723 /* Try longjmp_names kind breakpoints on main and separate_debug
3724 objfiles. */
3725 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3726 if (create_longjmp_master_breakpoint_names (debug_objfile))
3727 break;
3728 }
3729 }
3730 }
3731
3732 /* Create a master std::terminate breakpoint. */
3733 static void
create_std_terminate_master_breakpoint(void)3734 create_std_terminate_master_breakpoint (void)
3735 {
3736 const char *const func_name = "std::terminate()";
3737
3738 scoped_restore_current_program_space restore_pspace;
3739 scoped_restore_current_language save_language;
3740 set_language (language_cplus);
3741
3742 for (struct program_space *pspace : program_spaces)
3743 {
3744 set_current_program_space (pspace);
3745
3746 for (objfile *objfile : current_program_space->objfiles ())
3747 {
3748 struct breakpoint *b;
3749 struct breakpoint_objfile_data *bp_objfile_data;
3750
3751 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3752
3753 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3754 continue;
3755
3756 if (bp_objfile_data->terminate_msym.minsym == NULL)
3757 {
3758 struct bound_minimal_symbol m;
3759
3760 m = lookup_minimal_symbol (func_name, NULL, objfile);
3761 if (m.minsym == NULL || (m.minsym->type () != mst_text
3762 && m.minsym->type () != mst_file_text))
3763 {
3764 /* Prevent future lookups in this objfile. */
3765 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3766 continue;
3767 }
3768 bp_objfile_data->terminate_msym = m;
3769 }
3770
3771 b = create_internal_breakpoint (objfile->arch (),
3772 bp_objfile_data->terminate_msym,
3773 bp_std_terminate_master);
3774 b->locspec = new_explicit_location_spec_function (func_name);
3775 b->enable_state = bp_disabled;
3776 }
3777 }
3778 }
3779
3780 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3781 probe. Return true if a breakpoint was installed. */
3782
3783 static bool
create_exception_master_breakpoint_probe(objfile * objfile)3784 create_exception_master_breakpoint_probe (objfile *objfile)
3785 {
3786 struct breakpoint *b;
3787 struct gdbarch *gdbarch;
3788 struct breakpoint_objfile_data *bp_objfile_data;
3789
3790 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3791
3792 /* We prefer the SystemTap probe point if it exists. */
3793 if (!bp_objfile_data->exception_searched)
3794 {
3795 std::vector<probe *> ret
3796 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3797
3798 if (!ret.empty ())
3799 {
3800 /* We are only interested in checking one element. */
3801 probe *p = ret[0];
3802
3803 if (!p->can_evaluate_arguments ())
3804 {
3805 /* We cannot use the probe interface here, because it does
3806 not know how to evaluate arguments. */
3807 ret.clear ();
3808 }
3809 }
3810 bp_objfile_data->exception_probes = ret;
3811 bp_objfile_data->exception_searched = 1;
3812 }
3813
3814 if (bp_objfile_data->exception_probes.empty ())
3815 return false;
3816
3817 gdbarch = objfile->arch ();
3818
3819 for (probe *p : bp_objfile_data->exception_probes)
3820 {
3821 b = create_internal_breakpoint (gdbarch,
3822 p->get_relocated_address (objfile),
3823 bp_exception_master);
3824 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
3825 b->enable_state = bp_disabled;
3826 }
3827
3828 return true;
3829 }
3830
3831 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3832 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3833
3834 static bool
create_exception_master_breakpoint_hook(objfile * objfile)3835 create_exception_master_breakpoint_hook (objfile *objfile)
3836 {
3837 const char *const func_name = "_Unwind_DebugHook";
3838 struct breakpoint *b;
3839 struct gdbarch *gdbarch;
3840 struct breakpoint_objfile_data *bp_objfile_data;
3841
3842 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3843
3844 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3845 return false;
3846
3847 gdbarch = objfile->arch ();
3848
3849 if (bp_objfile_data->exception_msym.minsym == NULL)
3850 {
3851 struct bound_minimal_symbol debug_hook;
3852
3853 debug_hook = lookup_minimal_symbol_text (func_name, objfile);
3854 if (debug_hook.minsym == NULL)
3855 {
3856 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3857 return false;
3858 }
3859
3860 bp_objfile_data->exception_msym = debug_hook;
3861 }
3862
3863 b = create_internal_breakpoint (gdbarch, bp_objfile_data->exception_msym,
3864 bp_exception_master);
3865 b->locspec = new_explicit_location_spec_function (func_name);
3866 b->enable_state = bp_disabled;
3867
3868 return true;
3869 }
3870
3871 /* Install a master breakpoint on the unwinder's debug hook. */
3872
3873 static void
create_exception_master_breakpoint(void)3874 create_exception_master_breakpoint (void)
3875 {
3876 for (objfile *obj : current_program_space->objfiles ())
3877 {
3878 /* Skip separate debug object. */
3879 if (obj->separate_debug_objfile_backlink)
3880 continue;
3881
3882 /* Try a probe kind breakpoint. */
3883 if (create_exception_master_breakpoint_probe (obj))
3884 continue;
3885
3886 /* Iterate over main and separate debug objects and try an
3887 _Unwind_DebugHook kind breakpoint. */
3888 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3889 if (create_exception_master_breakpoint_hook (debug_objfile))
3890 break;
3891 }
3892 }
3893
3894 /* Does B have a location spec? */
3895
3896 static bool
breakpoint_location_spec_empty_p(const struct breakpoint * b)3897 breakpoint_location_spec_empty_p (const struct breakpoint *b)
3898 {
3899 return (b->locspec != nullptr && b->locspec->empty_p ());
3900 }
3901
3902 void
update_breakpoints_after_exec(void)3903 update_breakpoints_after_exec (void)
3904 {
3905 /* We're about to delete breakpoints from GDB's lists. If the
3906 INSERTED flag is true, GDB will try to lift the breakpoints by
3907 writing the breakpoints' "shadow contents" back into memory. The
3908 "shadow contents" are NOT valid after an exec, so GDB should not
3909 do that. Instead, the target is responsible from marking
3910 breakpoints out as soon as it detects an exec. We don't do that
3911 here instead, because there may be other attempts to delete
3912 breakpoints after detecting an exec and before reaching here. */
3913 for (bp_location *bploc : all_bp_locations ())
3914 if (bploc->pspace == current_program_space)
3915 gdb_assert (!bploc->inserted);
3916
3917 for (breakpoint &b : all_breakpoints_safe ())
3918 {
3919 if (b.pspace != current_program_space)
3920 continue;
3921
3922 /* Solib breakpoints must be explicitly reset after an exec(). */
3923 if (b.type == bp_shlib_event)
3924 {
3925 delete_breakpoint (&b);
3926 continue;
3927 }
3928
3929 /* JIT breakpoints must be explicitly reset after an exec(). */
3930 if (b.type == bp_jit_event)
3931 {
3932 delete_breakpoint (&b);
3933 continue;
3934 }
3935
3936 /* Thread event breakpoints must be set anew after an exec(),
3937 as must overlay event and longjmp master breakpoints. */
3938 if (b.type == bp_thread_event || b.type == bp_overlay_event
3939 || b.type == bp_longjmp_master || b.type == bp_std_terminate_master
3940 || b.type == bp_exception_master)
3941 {
3942 delete_breakpoint (&b);
3943 continue;
3944 }
3945
3946 /* Step-resume breakpoints are meaningless after an exec(). */
3947 if (b.type == bp_step_resume || b.type == bp_hp_step_resume)
3948 {
3949 delete_breakpoint (&b);
3950 continue;
3951 }
3952
3953 /* Just like single-step breakpoints. */
3954 if (b.type == bp_single_step)
3955 {
3956 delete_breakpoint (&b);
3957 continue;
3958 }
3959
3960 /* Longjmp and longjmp-resume breakpoints are also meaningless
3961 after an exec. */
3962 if (b.type == bp_longjmp || b.type == bp_longjmp_resume
3963 || b.type == bp_longjmp_call_dummy
3964 || b.type == bp_exception || b.type == bp_exception_resume)
3965 {
3966 delete_breakpoint (&b);
3967 continue;
3968 }
3969
3970 if (b.type == bp_catchpoint)
3971 {
3972 /* For now, none of the bp_catchpoint breakpoints need to
3973 do anything at this point. In the future, if some of
3974 the catchpoints need to something, we will need to add
3975 a new method, and call this method from here. */
3976 continue;
3977 }
3978
3979 /* bp_finish is a special case. The only way we ought to be able
3980 to see one of these when an exec() has happened, is if the user
3981 caught a vfork, and then said "finish". Ordinarily a finish just
3982 carries them to the call-site of the current callee, by setting
3983 a temporary bp there and resuming. But in this case, the finish
3984 will carry them entirely through the vfork & exec.
3985
3986 We don't want to allow a bp_finish to remain inserted now. But
3987 we can't safely delete it, 'cause finish_command has a handle to
3988 the bp on a bpstat, and will later want to delete it. There's a
3989 chance (and I've seen it happen) that if we delete the bp_finish
3990 here, that its storage will get reused by the time finish_command
3991 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3992 We really must allow finish_command to delete a bp_finish.
3993
3994 In the absence of a general solution for the "how do we know
3995 it's safe to delete something others may have handles to?"
3996 problem, what we'll do here is just uninsert the bp_finish, and
3997 let finish_command delete it.
3998
3999 (We know the bp_finish is "doomed" in the sense that it's
4000 momentary, and will be deleted as soon as finish_command sees
4001 the inferior stopped. So it doesn't matter that the bp's
4002 address is probably bogus in the new a.out, unlike e.g., the
4003 solib breakpoints.) */
4004
4005 if (b.type == bp_finish)
4006 {
4007 continue;
4008 }
4009
4010 /* Without a symbolic address, we have little hope of the
4011 pre-exec() address meaning the same thing in the post-exec()
4012 a.out. */
4013 if (breakpoint_location_spec_empty_p (&b))
4014 {
4015 delete_breakpoint (&b);
4016 continue;
4017 }
4018 }
4019 }
4020
4021 int
detach_breakpoints(ptid_t ptid)4022 detach_breakpoints (ptid_t ptid)
4023 {
4024 int val = 0;
4025 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
4026 struct inferior *inf = current_inferior ();
4027
4028 if (ptid.pid () == inferior_ptid.pid ())
4029 error (_("Cannot detach breakpoints of inferior_ptid"));
4030
4031 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
4032 inferior_ptid = ptid;
4033 for (bp_location *bl : all_bp_locations ())
4034 {
4035 if (bl->pspace != inf->pspace)
4036 continue;
4037
4038 /* This function must physically remove breakpoints locations
4039 from the specified ptid, without modifying the breakpoint
4040 package's state. Locations of type bp_loc_other and
4041 bp_loc_software_watchpoint are only maintained at GDB side,
4042 so there is no need to remove them. Moreover, removing these
4043 would modify the breakpoint package's state. */
4044 if (bl->loc_type == bp_loc_other
4045 || bl->loc_type == bp_loc_software_watchpoint)
4046 continue;
4047
4048 if (bl->inserted)
4049 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
4050 }
4051
4052 return val;
4053 }
4054
4055 /* Remove the breakpoint location BL from the current address space.
4056 Note that this is used to detach breakpoints from a child fork.
4057 When we get here, the child isn't in the inferior list, and neither
4058 do we have objects to represent its address space --- we should
4059 *not* look at bl->pspace->aspace here. */
4060
4061 static int
remove_breakpoint_1(struct bp_location * bl,enum remove_bp_reason reason)4062 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
4063 {
4064 breakpoint_debug_printf ("%s due to %s",
4065 breakpoint_location_address_str (bl).c_str (),
4066 remove_bp_reason_str (reason));
4067
4068 int val;
4069
4070 /* BL is never in moribund_locations by our callers. */
4071 gdb_assert (bl->owner != NULL);
4072
4073 /* The type of none suggests that owner is actually deleted.
4074 This should not ever happen. */
4075 gdb_assert (bl->owner->type != bp_none);
4076
4077 if (bl->loc_type == bp_loc_software_breakpoint
4078 || bl->loc_type == bp_loc_hardware_breakpoint)
4079 {
4080 /* "Normal" instruction breakpoint: either the standard
4081 trap-instruction bp (bp_breakpoint), or a
4082 bp_hardware_breakpoint. */
4083
4084 /* First check to see if we have to handle an overlay. */
4085 if (overlay_debugging == ovly_off
4086 || bl->section == NULL
4087 || !(section_is_overlay (bl->section)))
4088 {
4089 /* No overlay handling: just remove the breakpoint. */
4090
4091 /* If we're trying to uninsert a memory breakpoint that we
4092 know is set in a dynamic object that is marked
4093 shlib_disabled, then either the dynamic object was
4094 removed with "remove-symbol-file" or with
4095 "nosharedlibrary". In the former case, we don't know
4096 whether another dynamic object might have loaded over the
4097 breakpoint's address -- the user might well let us know
4098 about it next with add-symbol-file (the whole point of
4099 add-symbol-file is letting the user manually maintain a
4100 list of dynamically loaded objects). If we have the
4101 breakpoint's shadow memory, that is, this is a software
4102 breakpoint managed by GDB, check whether the breakpoint
4103 is still inserted in memory, to avoid overwriting wrong
4104 code with stale saved shadow contents. Note that HW
4105 breakpoints don't have shadow memory, as they're
4106 implemented using a mechanism that is not dependent on
4107 being able to modify the target's memory, and as such
4108 they should always be removed. */
4109 if (bl->shlib_disabled
4110 && bl->target_info.shadow_len != 0
4111 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
4112 val = 0;
4113 else
4114 val = bl->owner->remove_location (bl, reason);
4115 }
4116 else
4117 {
4118 /* This breakpoint is in an overlay section.
4119 Did we set a breakpoint at the LMA? */
4120 if (!overlay_events_enabled)
4121 {
4122 /* Yes -- overlay event support is not active, so we
4123 should have set a breakpoint at the LMA. Remove it.
4124 */
4125 /* Ignore any failures: if the LMA is in ROM, we will
4126 have already warned when we failed to insert it. */
4127 if (bl->loc_type == bp_loc_hardware_breakpoint)
4128 target_remove_hw_breakpoint (bl->gdbarch,
4129 &bl->overlay_target_info);
4130 else
4131 target_remove_breakpoint (bl->gdbarch,
4132 &bl->overlay_target_info,
4133 reason);
4134 }
4135 /* Did we set a breakpoint at the VMA?
4136 If so, we will have marked the breakpoint 'inserted'. */
4137 if (bl->inserted)
4138 {
4139 /* Yes -- remove it. Previously we did not bother to
4140 remove the breakpoint if the section had been
4141 unmapped, but let's not rely on that being safe. We
4142 don't know what the overlay manager might do. */
4143
4144 /* However, we should remove *software* breakpoints only
4145 if the section is still mapped, or else we overwrite
4146 wrong code with the saved shadow contents. */
4147 if (bl->loc_type == bp_loc_hardware_breakpoint
4148 || section_is_mapped (bl->section))
4149 val = bl->owner->remove_location (bl, reason);
4150 else
4151 val = 0;
4152 }
4153 else
4154 {
4155 /* No -- not inserted, so no need to remove. No error. */
4156 val = 0;
4157 }
4158 }
4159
4160 /* In some cases, we might not be able to remove a breakpoint in
4161 a shared library that has already been removed, but we have
4162 not yet processed the shlib unload event. Similarly for an
4163 unloaded add-symbol-file object - the user might not yet have
4164 had the chance to remove-symbol-file it. shlib_disabled will
4165 be set if the library/object has already been removed, but
4166 the breakpoint hasn't been uninserted yet, e.g., after
4167 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4168 always-inserted mode. */
4169 if (val
4170 && (bl->loc_type == bp_loc_software_breakpoint
4171 && (bl->shlib_disabled
4172 || solib_name_from_address (bl->pspace, bl->address)
4173 || shared_objfile_contains_address_p (bl->pspace,
4174 bl->address))))
4175 val = 0;
4176
4177 if (val)
4178 return val;
4179 bl->inserted = (reason == DETACH_BREAKPOINT);
4180 }
4181 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4182 {
4183 bl->inserted = (reason == DETACH_BREAKPOINT);
4184 bl->owner->remove_location (bl, reason);
4185
4186 /* Failure to remove any of the hardware watchpoints comes here. */
4187 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4188 warning (_("Could not remove hardware watchpoint %d."),
4189 bl->owner->number);
4190 }
4191 else if (bl->owner->type == bp_catchpoint
4192 && breakpoint_enabled (bl->owner)
4193 && !bl->duplicate)
4194 {
4195 val = bl->owner->remove_location (bl, reason);
4196 if (val)
4197 return val;
4198
4199 bl->inserted = (reason == DETACH_BREAKPOINT);
4200 }
4201
4202 return 0;
4203 }
4204
4205 static int
remove_breakpoint(struct bp_location * bl)4206 remove_breakpoint (struct bp_location *bl)
4207 {
4208 /* BL is never in moribund_locations by our callers. */
4209 gdb_assert (bl->owner != NULL);
4210
4211 /* The type of none suggests that owner is actually deleted.
4212 This should not ever happen. */
4213 gdb_assert (bl->owner->type != bp_none);
4214
4215 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4216
4217 switch_to_program_space_and_thread (bl->pspace);
4218
4219 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4220 }
4221
4222 /* See breakpoint.h. */
4223
4224 void
mark_breakpoints_out(program_space * pspace)4225 mark_breakpoints_out (program_space *pspace)
4226 {
4227 for (bp_location *bl : all_bp_locations ())
4228 if (bl->pspace == pspace)
4229 bl->inserted = 0;
4230 }
4231
4232 /* See breakpoint.h. */
4233
4234 void
breakpoint_init_inferior(inferior * inf,inf_context context)4235 breakpoint_init_inferior (inferior *inf, inf_context context)
4236 {
4237 /* If breakpoint locations are shared across processes, then there's
4238 nothing to do. */
4239 if (gdbarch_has_global_breakpoints (inf->arch ()))
4240 return;
4241
4242 mark_breakpoints_out (inf->pspace);
4243
4244 for (breakpoint &b : all_breakpoints_safe ())
4245 {
4246 if (b.has_locations () && b.first_loc ().pspace != inf->pspace)
4247 continue;
4248
4249 switch (b.type)
4250 {
4251 case bp_call_dummy:
4252 case bp_longjmp_call_dummy:
4253
4254 /* If the call dummy breakpoint is at the entry point it will
4255 cause problems when the inferior is rerun, so we better get
4256 rid of it. */
4257
4258 case bp_watchpoint_scope:
4259
4260 /* Also get rid of scope breakpoints. */
4261
4262 case bp_shlib_event:
4263
4264 /* Also remove solib event breakpoints. Their addresses may
4265 have changed since the last time we ran the program.
4266 Actually we may now be debugging against different target;
4267 and so the solib backend that installed this breakpoint may
4268 not be used in by the target. E.g.,
4269
4270 (gdb) file prog-linux
4271 (gdb) run # native linux target
4272 ...
4273 (gdb) kill
4274 (gdb) file prog-win.exe
4275 (gdb) tar rem :9999 # remote Windows gdbserver.
4276 */
4277
4278 case bp_step_resume:
4279
4280 /* Also remove step-resume breakpoints. */
4281
4282 case bp_single_step:
4283
4284 /* Also remove single-step breakpoints. */
4285
4286 delete_breakpoint (&b);
4287 break;
4288
4289 case bp_watchpoint:
4290 case bp_hardware_watchpoint:
4291 case bp_read_watchpoint:
4292 case bp_access_watchpoint:
4293 {
4294 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
4295
4296 /* Likewise for watchpoints on local expressions. */
4297 if (w.exp_valid_block != NULL)
4298 delete_breakpoint (&b);
4299 else
4300 {
4301 /* Get rid of existing locations, which are no longer
4302 valid. New ones will be created in
4303 update_watchpoint, when the inferior is restarted.
4304 The next update_global_location_list call will
4305 garbage collect them. */
4306 b.clear_locations ();
4307
4308 if (context == inf_starting)
4309 {
4310 /* Reset val field to force reread of starting value in
4311 insert_breakpoints. */
4312 w.val.reset (nullptr);
4313 w.val_valid = false;
4314 }
4315 }
4316 }
4317 break;
4318 default:
4319 break;
4320 }
4321 }
4322
4323 /* Get rid of the moribund locations. */
4324 for (bp_location *bl : moribund_locations)
4325 decref_bp_location (&bl);
4326 moribund_locations.clear ();
4327 }
4328
4329 /* These functions concern about actual breakpoints inserted in the
4330 target --- to e.g. check if we need to do decr_pc adjustment or if
4331 we need to hop over the bkpt --- so we check for address space
4332 match, not program space. */
4333
4334 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4335 exists at PC. It returns ordinary_breakpoint_here if it's an
4336 ordinary breakpoint, or permanent_breakpoint_here if it's a
4337 permanent breakpoint.
4338 - When continuing from a location with an ordinary breakpoint, we
4339 actually single step once before calling insert_breakpoints.
4340 - When continuing from a location with a permanent breakpoint, we
4341 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4342 the target, to advance the PC past the breakpoint. */
4343
4344 enum breakpoint_here
breakpoint_here_p(const address_space * aspace,CORE_ADDR pc)4345 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4346 {
4347 bool any_breakpoint_here = false;
4348
4349 for (bp_location *bl : all_bp_locations ())
4350 {
4351 if (bl->loc_type != bp_loc_software_breakpoint
4352 && bl->loc_type != bp_loc_hardware_breakpoint)
4353 continue;
4354
4355 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4356 if ((breakpoint_enabled (bl->owner)
4357 || bl->permanent)
4358 && breakpoint_location_address_match (bl, aspace, pc))
4359 {
4360 if (overlay_debugging
4361 && section_is_overlay (bl->section)
4362 && !section_is_mapped (bl->section))
4363 continue; /* unmapped overlay -- can't be a match */
4364 else if (bl->permanent)
4365 return permanent_breakpoint_here;
4366 else
4367 any_breakpoint_here = true;
4368 }
4369 }
4370
4371 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4372 }
4373
4374 /* See breakpoint.h. */
4375
4376 int
breakpoint_in_range_p(const address_space * aspace,CORE_ADDR addr,ULONGEST len)4377 breakpoint_in_range_p (const address_space *aspace,
4378 CORE_ADDR addr, ULONGEST len)
4379 {
4380 for (bp_location *bl : all_bp_locations ())
4381 {
4382 if (bl->loc_type != bp_loc_software_breakpoint
4383 && bl->loc_type != bp_loc_hardware_breakpoint)
4384 continue;
4385
4386 if ((breakpoint_enabled (bl->owner)
4387 || bl->permanent)
4388 && breakpoint_location_address_range_overlap (bl, aspace,
4389 addr, len))
4390 {
4391 if (overlay_debugging
4392 && section_is_overlay (bl->section)
4393 && !section_is_mapped (bl->section))
4394 {
4395 /* Unmapped overlay -- can't be a match. */
4396 continue;
4397 }
4398
4399 return 1;
4400 }
4401 }
4402
4403 return 0;
4404 }
4405
4406 /* Return true if there's a moribund breakpoint at PC. */
4407
4408 int
moribund_breakpoint_here_p(const address_space * aspace,CORE_ADDR pc)4409 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4410 {
4411 for (bp_location *loc : moribund_locations)
4412 if (breakpoint_location_address_match (loc, aspace, pc))
4413 return 1;
4414
4415 return 0;
4416 }
4417
4418 /* Returns true iff BL is inserted at PC, in address space ASPACE. */
4419
4420 static bool
bp_location_inserted_here_p(const struct bp_location * bl,const address_space * aspace,CORE_ADDR pc)4421 bp_location_inserted_here_p (const struct bp_location *bl,
4422 const address_space *aspace, CORE_ADDR pc)
4423 {
4424 if (bl->inserted
4425 && breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
4426 aspace, pc))
4427 {
4428 /* An unmapped overlay can't be a match. */
4429 return !(overlay_debugging
4430 && section_is_overlay (bl->section)
4431 && !section_is_mapped (bl->section));
4432 }
4433 return false;
4434 }
4435
4436 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4437
4438 int
breakpoint_inserted_here_p(const address_space * aspace,CORE_ADDR pc)4439 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4440 {
4441 for (bp_location *bl : all_bp_locations_at_addr (pc))
4442 {
4443 if (bl->loc_type != bp_loc_software_breakpoint
4444 && bl->loc_type != bp_loc_hardware_breakpoint)
4445 continue;
4446
4447 if (bp_location_inserted_here_p (bl, aspace, pc))
4448 return 1;
4449 }
4450 return 0;
4451 }
4452
4453 /* This function returns non-zero iff there is a software breakpoint
4454 inserted at PC. */
4455
4456 int
software_breakpoint_inserted_here_p(const address_space * aspace,CORE_ADDR pc)4457 software_breakpoint_inserted_here_p (const address_space *aspace,
4458 CORE_ADDR pc)
4459 {
4460 for (bp_location *bl : all_bp_locations_at_addr (pc))
4461 {
4462 if (bl->loc_type != bp_loc_software_breakpoint)
4463 continue;
4464
4465 if (bp_location_inserted_here_p (bl, aspace, pc))
4466 return 1;
4467 }
4468
4469 return 0;
4470 }
4471
4472 /* See breakpoint.h. */
4473
4474 int
hardware_breakpoint_inserted_here_p(const address_space * aspace,CORE_ADDR pc)4475 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4476 CORE_ADDR pc)
4477 {
4478 for (bp_location *bl : all_bp_locations_at_addr (pc))
4479 {
4480 if (bl->loc_type != bp_loc_hardware_breakpoint)
4481 continue;
4482
4483 if (bp_location_inserted_here_p (bl, aspace, pc))
4484 return 1;
4485 }
4486
4487 return 0;
4488 }
4489
4490 int
hardware_watchpoint_inserted_in_range(const address_space * aspace,CORE_ADDR addr,ULONGEST len)4491 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4492 CORE_ADDR addr, ULONGEST len)
4493 {
4494 for (breakpoint &bpt : all_breakpoints ())
4495 {
4496 if (bpt.type != bp_hardware_watchpoint
4497 && bpt.type != bp_access_watchpoint)
4498 continue;
4499
4500 if (!breakpoint_enabled (&bpt))
4501 continue;
4502
4503 for (bp_location &loc : bpt.locations ())
4504 if (loc.pspace->aspace.get () == aspace && loc.inserted)
4505 {
4506 CORE_ADDR l, h;
4507
4508 /* Check for intersection. */
4509 l = std::max<CORE_ADDR> (loc.address, addr);
4510 h = std::min<CORE_ADDR> (loc.address + loc.length, addr + len);
4511 if (l < h)
4512 return 1;
4513 }
4514 }
4515 return 0;
4516 }
4517
4518 /* See breakpoint.h. */
4519
4520 bool
is_catchpoint(struct breakpoint * b)4521 is_catchpoint (struct breakpoint *b)
4522 {
4523 return (b->type == bp_catchpoint);
4524 }
4525
4526 /* Clear a bpstat so that it says we are not at any breakpoint.
4527 Also free any storage that is part of a bpstat. */
4528
4529 void
bpstat_clear(bpstat ** bsp)4530 bpstat_clear (bpstat **bsp)
4531 {
4532 bpstat *p;
4533 bpstat *q;
4534
4535 if (bsp == 0)
4536 return;
4537 p = *bsp;
4538 while (p != NULL)
4539 {
4540 q = p->next;
4541 delete p;
4542 p = q;
4543 }
4544 *bsp = NULL;
4545 }
4546
bpstat(const bpstat & other)4547 bpstat::bpstat (const bpstat &other)
4548 : next (NULL),
4549 bp_location_at (other.bp_location_at),
4550 breakpoint_at (other.breakpoint_at),
4551 commands (other.commands),
4552 print (other.print),
4553 stop (other.stop),
4554 print_it (other.print_it)
4555 {
4556 if (other.old_val != NULL)
4557 old_val = release_value (other.old_val->copy ());
4558 }
4559
4560 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4561 is part of the bpstat is copied as well. */
4562
4563 bpstat *
bpstat_copy(bpstat * bs)4564 bpstat_copy (bpstat *bs)
4565 {
4566 bpstat *p = nullptr;
4567 bpstat *tmp;
4568 bpstat *retval = nullptr;
4569
4570 if (bs == NULL)
4571 return bs;
4572
4573 for (; bs != NULL; bs = bs->next)
4574 {
4575 tmp = new bpstat (*bs);
4576
4577 if (p == NULL)
4578 /* This is the first thing in the chain. */
4579 retval = tmp;
4580 else
4581 p->next = tmp;
4582 p = tmp;
4583 }
4584 p->next = NULL;
4585 return retval;
4586 }
4587
4588 /* Find the bpstat associated with this breakpoint. */
4589
4590 bpstat *
bpstat_find_breakpoint(bpstat * bsp,struct breakpoint * breakpoint)4591 bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
4592 {
4593 if (bsp == NULL)
4594 return NULL;
4595
4596 for (; bsp != NULL; bsp = bsp->next)
4597 {
4598 if (bsp->breakpoint_at == breakpoint)
4599 return bsp;
4600 }
4601 return NULL;
4602 }
4603
4604 /* See breakpoint.h. */
4605
4606 bool
bpstat_explains_signal(bpstat * bsp,enum gdb_signal sig)4607 bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
4608 {
4609 for (; bsp != NULL; bsp = bsp->next)
4610 {
4611 if (bsp->breakpoint_at == NULL)
4612 {
4613 /* A moribund location can never explain a signal other than
4614 GDB_SIGNAL_TRAP. */
4615 if (sig == GDB_SIGNAL_TRAP)
4616 return true;
4617 }
4618 else
4619 {
4620 if (bsp->breakpoint_at->explains_signal (sig))
4621 return true;
4622 }
4623 }
4624
4625 return false;
4626 }
4627
4628 /* See breakpoint.h. */
4629
4630 int
bpstat_num(bpstat ** bsp,int * num)4631 bpstat_num (bpstat **bsp, int *num)
4632 {
4633 struct breakpoint *b;
4634
4635 if ((*bsp) == NULL)
4636 return 0; /* No more breakpoint values */
4637
4638 /* We assume we'll never have several bpstats that correspond to a
4639 single breakpoint -- otherwise, this function might return the
4640 same number more than once and this will look ugly. */
4641 b = (*bsp)->breakpoint_at;
4642 *bsp = (*bsp)->next;
4643 if (b == NULL)
4644 return -1; /* breakpoint that's been deleted since */
4645
4646 *num = b->number; /* We have its number */
4647 return 1;
4648 }
4649
4650 /* See breakpoint.h */
4651
4652 int
bpstat_locno(const bpstat * bs)4653 bpstat_locno (const bpstat *bs)
4654 {
4655 const struct breakpoint *b = bs->breakpoint_at;
4656 const struct bp_location *bl = bs->bp_location_at.get ();
4657
4658 if (b != nullptr && b->has_multiple_locations ())
4659 {
4660 int locno = 1;
4661
4662 for (bp_location &loc : b->locations ())
4663 {
4664 if (bl == &loc)
4665 return locno;
4666
4667 ++locno;
4668 }
4669
4670 warning (_("location number not found for breakpoint %d address %s."),
4671 b->number, paddress (bl->gdbarch, bl->address));
4672 }
4673
4674 return 0;
4675 }
4676
4677 /* See breakpoint.h. */
4678
4679 void
print_num_locno(const bpstat * bs,struct ui_out * uiout)4680 print_num_locno (const bpstat *bs, struct ui_out *uiout)
4681 {
4682 struct breakpoint *b = bs->breakpoint_at;
4683
4684 if (b == nullptr)
4685 uiout->text (_("deleted breakpoint"));
4686 else
4687 {
4688 uiout->field_signed ("bkptno", b->number);
4689
4690 int locno = bpstat_locno (bs);
4691 if (locno != 0)
4692 uiout->message (".%pF", signed_field ("locno", locno));
4693 }
4694 }
4695
4696 /* See breakpoint.h. */
4697
4698 void
bpstat_clear_actions(void)4699 bpstat_clear_actions (void)
4700 {
4701 bpstat *bs;
4702
4703 if (inferior_ptid == null_ptid)
4704 return;
4705
4706 thread_info *tp = inferior_thread ();
4707 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4708 {
4709 bs->commands = NULL;
4710 bs->old_val.reset (nullptr);
4711 }
4712 }
4713
4714 /* Called when a command is about to proceed the inferior. */
4715
4716 static void
breakpoint_about_to_proceed(void)4717 breakpoint_about_to_proceed (void)
4718 {
4719 if (inferior_ptid != null_ptid)
4720 {
4721 struct thread_info *tp = inferior_thread ();
4722
4723 /* Allow inferior function calls in breakpoint commands to not
4724 interrupt the command list. When the call finishes
4725 successfully, the inferior will be standing at the same
4726 breakpoint as if nothing happened. */
4727 if (tp->control.in_infcall)
4728 return;
4729 }
4730
4731 breakpoint_proceeded = 1;
4732 }
4733
4734 /* Return true iff CMD as the first line of a command sequence is `silent'
4735 or its equivalent. */
4736
4737 static bool
command_line_is_silent(struct command_line * cmd)4738 command_line_is_silent (struct command_line *cmd)
4739 {
4740 return cmd && (strcmp ("silent", cmd->line) == 0);
4741 }
4742
4743 /* Sets the $_hit_bpnum and $_hit_locno to bpnum and locno.
4744 A locno 0 is changed to 1 to e.g. let the user do
4745 (gdb) disable $_hit_bpnum.$_hit_locno
4746 for a single location breakpoint. */
4747
4748 static void
set_hit_convenience_vars(int bpnum,int locno)4749 set_hit_convenience_vars (int bpnum, int locno)
4750 {
4751 set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), bpnum);
4752 set_internalvar_integer (lookup_internalvar ("_hit_locno"),
4753 (locno > 0 ? locno : 1));
4754 }
4755
4756 /* Execute all the commands associated with all the breakpoints at
4757 this location. Any of these commands could cause the process to
4758 proceed beyond this point, etc. We look out for such changes by
4759 checking the global "breakpoint_proceeded" after each command.
4760
4761 Returns true if a breakpoint command resumed the inferior. In that
4762 case, it is the caller's responsibility to recall it again with the
4763 bpstat of the current thread. */
4764
4765 static bool
bpstat_do_actions_1(bpstat ** bsp)4766 bpstat_do_actions_1 (bpstat **bsp)
4767 {
4768 bpstat *bs;
4769 bool again = false;
4770
4771 /* Avoid endless recursion if a `source' command is contained
4772 in bs->commands. */
4773 if (executing_breakpoint_commands)
4774 return false;
4775
4776 scoped_restore save_executing
4777 = make_scoped_restore (&executing_breakpoint_commands, 1);
4778
4779 scoped_restore preventer = prevent_dont_repeat ();
4780
4781 /* This pointer will iterate over the list of bpstat's. */
4782 bs = *bsp;
4783
4784 /* The $_hit_* convenience variables are set before running the
4785 commands of BS. In case we have several bs, after the loop,
4786 we set again the variables to the first printed bpnum and locno.
4787 For multiple breakpoints, this ensures the variables are set to the
4788 breakpoint printed for the user. */
4789 int printed_hit_bpnum = -1;
4790 int printed_hit_locno = -1;
4791
4792 breakpoint_proceeded = 0;
4793 for (; bs != NULL; bs = bs->next)
4794 {
4795 struct command_line *cmd = NULL;
4796
4797 /* Set the _hit_* convenience variables before running BS's commands. */
4798 {
4799 const struct breakpoint *b = bs->breakpoint_at;
4800 if (b != nullptr)
4801 {
4802 int locno = bpstat_locno (bs);
4803
4804 set_hit_convenience_vars (b->number, locno);
4805 if (printed_hit_locno == -1 && bs->print)
4806 {
4807 printed_hit_bpnum = b->number;
4808 printed_hit_locno = locno;
4809 }
4810 }
4811 }
4812
4813 /* Take ownership of the BSP's command tree, if it has one.
4814
4815 The command tree could legitimately contain commands like
4816 'step' and 'next', which call clear_proceed_status, which
4817 frees the bpstat BS and its command tree. To make sure this doesn't
4818 free the tree we're executing out from under us, we need to
4819 take ownership of the tree ourselves. Since a given bpstat's
4820 commands are only executed once, we don't need to copy it; we
4821 can clear the pointer in the bpstat, and make sure we free
4822 the tree when we're done. */
4823 counted_command_line ccmd = bs->commands;
4824 bs->commands = NULL;
4825 if (ccmd != NULL)
4826 cmd = ccmd.get ();
4827 if (command_line_is_silent (cmd))
4828 {
4829 /* The action has been already done by bpstat_stop_status. */
4830 cmd = cmd->next;
4831 }
4832
4833 while (cmd != NULL)
4834 {
4835 execute_control_command (cmd);
4836 /* After execute_control_command, if breakpoint_proceeded is true,
4837 BS has been freed and cannot be accessed anymore. */
4838
4839 if (breakpoint_proceeded)
4840 break;
4841 else
4842 cmd = cmd->next;
4843 }
4844
4845 if (breakpoint_proceeded)
4846 {
4847 if (current_ui->async)
4848 /* If we are in async mode, then the target might be still
4849 running, not stopped at any breakpoint, so nothing for
4850 us to do here -- just return to the event loop. */
4851 ;
4852 else
4853 /* In sync mode, when execute_control_command returns
4854 we're already standing on the next breakpoint.
4855 Breakpoint commands for that stop were not run, since
4856 execute_command does not run breakpoint commands --
4857 only command_line_handler does, but that one is not
4858 involved in execution of breakpoint commands. So, we
4859 can now execute breakpoint commands. It should be
4860 noted that making execute_command do bpstat actions is
4861 not an option -- in this case we'll have recursive
4862 invocation of bpstat for each breakpoint with a
4863 command, and can easily blow up GDB stack. Instead, we
4864 return true, which will trigger the caller to recall us
4865 with the new stop_bpstat. */
4866 again = true;
4867 break;
4868 }
4869 }
4870
4871 /* Now that we have executed the commands of all bs, set the _hit_*
4872 convenience variables to the printed values. */
4873 if (printed_hit_locno != -1)
4874 set_hit_convenience_vars (printed_hit_bpnum, printed_hit_locno);
4875
4876 return again;
4877 }
4878
4879 /* Helper for bpstat_do_actions. Get the current thread, if there's
4880 one, is alive and has execution. Return NULL otherwise. */
4881
4882 static thread_info *
get_bpstat_thread()4883 get_bpstat_thread ()
4884 {
4885 if (inferior_ptid == null_ptid || !target_has_execution ())
4886 return NULL;
4887
4888 thread_info *tp = inferior_thread ();
4889 if (tp->state == THREAD_EXITED || tp->executing ())
4890 return NULL;
4891 return tp;
4892 }
4893
4894 void
bpstat_do_actions(void)4895 bpstat_do_actions (void)
4896 {
4897 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4898 thread_info *tp;
4899
4900 /* Do any commands attached to breakpoint we are stopped at. */
4901 while ((tp = get_bpstat_thread ()) != NULL)
4902 {
4903 /* Since in sync mode, bpstat_do_actions may resume the
4904 inferior, and only return when it is stopped at the next
4905 breakpoint, we keep doing breakpoint actions until it returns
4906 false to indicate the inferior was not resumed. */
4907 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4908 break;
4909 }
4910
4911 cleanup_if_error.release ();
4912 }
4913
4914 /* Print out the (old or new) value associated with a watchpoint. */
4915
4916 static void
watchpoint_value_print(struct value * val,struct ui_file * stream)4917 watchpoint_value_print (struct value *val, struct ui_file *stream)
4918 {
4919 if (val == NULL)
4920 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4921 else
4922 {
4923 struct value_print_options opts;
4924 get_user_print_options (&opts);
4925 value_print (val, stream, &opts);
4926 }
4927 }
4928
4929 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4930 debugging multiple threads. */
4931
4932 void
maybe_print_thread_hit_breakpoint(struct ui_out * uiout)4933 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4934 {
4935 if (uiout->is_mi_like_p ())
4936 return;
4937
4938 uiout->text ("\n");
4939
4940 if (show_thread_that_caused_stop ())
4941 {
4942 struct thread_info *thr = inferior_thread ();
4943
4944 uiout->text ("Thread ");
4945 uiout->field_string ("thread-id", print_thread_id (thr));
4946
4947 const char *name = thread_name (thr);
4948 if (name != NULL)
4949 {
4950 uiout->text (" \"");
4951 uiout->field_string ("name", name);
4952 uiout->text ("\"");
4953 }
4954
4955 uiout->text (" hit ");
4956 }
4957 }
4958
4959 /* Generic routine for printing messages indicating why we
4960 stopped. The behavior of this function depends on the value
4961 'print_it' in the bpstat structure. Under some circumstances we
4962 may decide not to print anything here and delegate the task to
4963 normal_stop(). */
4964
4965 static enum print_stop_action
print_bp_stop_message(bpstat * bs)4966 print_bp_stop_message (bpstat *bs)
4967 {
4968 switch (bs->print_it)
4969 {
4970 case print_it_noop:
4971 /* Nothing should be printed for this bpstat entry. */
4972 return PRINT_UNKNOWN;
4973
4974 case print_it_done:
4975 /* We still want to print the frame, but we already printed the
4976 relevant messages. */
4977 return PRINT_SRC_AND_LOC;
4978
4979 case print_it_normal:
4980 {
4981 struct breakpoint *b = bs->breakpoint_at;
4982
4983 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4984 which has since been deleted. */
4985 if (b == NULL)
4986 return PRINT_UNKNOWN;
4987
4988 /* Normal case. Call the breakpoint's print_it method. */
4989 return b->print_it (bs);
4990 }
4991
4992 default:
4993 internal_error (_("print_bp_stop_message: unrecognized enum value"));
4994 }
4995 }
4996
4997 /* See breakpoint.h. */
4998
4999 void
print_solib_event(bool is_catchpoint)5000 print_solib_event (bool is_catchpoint)
5001 {
5002 bool any_deleted = !current_program_space->deleted_solibs.empty ();
5003 bool any_added = !current_program_space->added_solibs.empty ();
5004
5005 if (!is_catchpoint)
5006 {
5007 if (any_added || any_deleted)
5008 current_uiout->text (_("Stopped due to shared library event:\n"));
5009 else
5010 current_uiout->text (_("Stopped due to shared library event (no "
5011 "libraries added or removed)\n"));
5012 }
5013
5014 if (current_uiout->is_mi_like_p ())
5015 current_uiout->field_string ("reason",
5016 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
5017
5018 if (any_deleted)
5019 {
5020 current_uiout->text (_(" Inferior unloaded "));
5021 ui_out_emit_list list_emitter (current_uiout, "removed");
5022 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
5023 {
5024 const std::string &name = current_program_space->deleted_solibs[ix];
5025
5026 if (ix > 0)
5027 current_uiout->text (" ");
5028 current_uiout->field_string ("library", name);
5029 current_uiout->text ("\n");
5030 }
5031 }
5032
5033 if (any_added)
5034 {
5035 current_uiout->text (_(" Inferior loaded "));
5036 ui_out_emit_list list_emitter (current_uiout, "added");
5037 bool first = true;
5038 for (solib *iter : current_program_space->added_solibs)
5039 {
5040 if (!first)
5041 current_uiout->text (" ");
5042 first = false;
5043 current_uiout->field_string ("library", iter->so_name);
5044 current_uiout->text ("\n");
5045 }
5046 }
5047 }
5048
5049 /* Print a message indicating what happened. This is called from
5050 normal_stop(). The input to this routine is the head of the bpstat
5051 list - a list of the eventpoints that caused this stop. KIND is
5052 the target_waitkind for the stopping event. This
5053 routine calls the generic print routine for printing a message
5054 about reasons for stopping. This will print (for example) the
5055 "Breakpoint n," part of the output. The return value of this
5056 routine is one of:
5057
5058 PRINT_UNKNOWN: Means we printed nothing.
5059 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
5060 code to print the location. An example is
5061 "Breakpoint 1, " which should be followed by
5062 the location.
5063 PRINT_SRC_ONLY: Means we printed something, but there is no need
5064 to also print the location part of the message.
5065 An example is the catch/throw messages, which
5066 don't require a location appended to the end.
5067 PRINT_NOTHING: We have done some printing and we don't need any
5068 further info to be printed. */
5069
5070 enum print_stop_action
bpstat_print(bpstat * bs,target_waitkind kind)5071 bpstat_print (bpstat *bs, target_waitkind kind)
5072 {
5073 enum print_stop_action val;
5074
5075 /* Maybe another breakpoint in the chain caused us to stop.
5076 (Currently all watchpoints go on the bpstat whether hit or not.
5077 That probably could (should) be changed, provided care is taken
5078 with respect to bpstat_explains_signal). */
5079 for (; bs; bs = bs->next)
5080 {
5081 val = print_bp_stop_message (bs);
5082 if (val == PRINT_SRC_ONLY
5083 || val == PRINT_SRC_AND_LOC
5084 || val == PRINT_NOTHING)
5085 return val;
5086 }
5087
5088 /* If we had hit a shared library event breakpoint,
5089 print_bp_stop_message would print out this message. If we hit an
5090 OS-level shared library event, do the same thing. */
5091 if (kind == TARGET_WAITKIND_LOADED)
5092 {
5093 print_solib_event (false);
5094 return PRINT_NOTHING;
5095 }
5096
5097 /* We reached the end of the chain, or we got a null BS to start
5098 with and nothing was printed. */
5099 return PRINT_UNKNOWN;
5100 }
5101
5102 /* Evaluate the boolean expression EXP and return the result. */
5103
5104 static bool
breakpoint_cond_eval(expression * exp)5105 breakpoint_cond_eval (expression *exp)
5106 {
5107 scoped_value_mark mark;
5108 return value_true (exp->evaluate ());
5109 }
5110
5111 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
5112
bpstat(struct bp_location * bl,bpstat *** bs_link_pointer)5113 bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
5114 : next (NULL),
5115 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
5116 breakpoint_at (bl->owner),
5117 commands (NULL),
5118 print (0),
5119 stop (0),
5120 print_it (print_it_normal)
5121 {
5122 **bs_link_pointer = this;
5123 *bs_link_pointer = &next;
5124 }
5125
bpstat()5126 bpstat::bpstat ()
5127 : next (NULL),
5128 breakpoint_at (NULL),
5129 commands (NULL),
5130 print (0),
5131 stop (0),
5132 print_it (print_it_normal)
5133 {
5134 }
5135
5136 /* The target has stopped with waitstatus WS. Check if any hardware
5137 watchpoints have triggered, according to the target. */
5138
5139 int
watchpoints_triggered(const target_waitstatus & ws)5140 watchpoints_triggered (const target_waitstatus &ws)
5141 {
5142 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
5143 CORE_ADDR addr;
5144
5145 if (!stopped_by_watchpoint)
5146 {
5147 /* We were not stopped by a watchpoint. Mark all watchpoints
5148 as not triggered. */
5149 for (breakpoint &b : all_breakpoints ())
5150 if (is_hardware_watchpoint (&b))
5151 {
5152 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
5153
5154 w.watchpoint_triggered = watch_triggered_no;
5155 }
5156
5157 return 0;
5158 }
5159
5160 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
5161 {
5162 /* We were stopped by a watchpoint, but we don't know where.
5163 Mark all watchpoints as unknown. */
5164 for (breakpoint &b : all_breakpoints ())
5165 if (is_hardware_watchpoint (&b))
5166 {
5167 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
5168
5169 w.watchpoint_triggered = watch_triggered_unknown;
5170 }
5171
5172 return 1;
5173 }
5174
5175 /* The target could report the data address. Mark watchpoints
5176 affected by this data address as triggered, and all others as not
5177 triggered. */
5178
5179 for (breakpoint &b : all_breakpoints ())
5180 if (is_hardware_watchpoint (&b))
5181 {
5182 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
5183
5184 w.watchpoint_triggered = watch_triggered_no;
5185 for (bp_location &loc : b.locations ())
5186 {
5187 if (is_masked_watchpoint (&b))
5188 {
5189 CORE_ADDR newaddr = addr & w.hw_wp_mask;
5190 CORE_ADDR start = loc.address & w.hw_wp_mask;
5191
5192 if (newaddr == start)
5193 {
5194 w.watchpoint_triggered = watch_triggered_yes;
5195 break;
5196 }
5197 }
5198 /* Exact match not required. Within range is sufficient. */
5199 else if (target_watchpoint_addr_within_range
5200 (current_inferior ()->top_target (), addr, loc.address,
5201 loc.length))
5202 {
5203 w.watchpoint_triggered = watch_triggered_yes;
5204 break;
5205 }
5206 }
5207 }
5208
5209 return 1;
5210 }
5211
5212 /* Possible return values for watchpoint_check. */
5213 enum wp_check_result
5214 {
5215 /* The watchpoint has been deleted. */
5216 WP_DELETED = 1,
5217
5218 /* The value has changed. */
5219 WP_VALUE_CHANGED = 2,
5220
5221 /* The value has not changed. */
5222 WP_VALUE_NOT_CHANGED = 3,
5223
5224 /* Ignore this watchpoint, no matter if the value changed or not. */
5225 WP_IGNORE = 4,
5226 };
5227
5228 #define BP_TEMPFLAG 1
5229 #define BP_HARDWAREFLAG 2
5230
5231 /* Evaluate watchpoint condition expression and check if its value
5232 changed. */
5233
5234 static wp_check_result
watchpoint_check(bpstat * bs)5235 watchpoint_check (bpstat *bs)
5236 {
5237 frame_info_ptr fr;
5238 bool within_current_scope;
5239
5240 /* BS is built from an existing struct breakpoint. */
5241 gdb_assert (bs->breakpoint_at != NULL);
5242 watchpoint *b = gdb::checked_static_cast<watchpoint *> (bs->breakpoint_at);
5243
5244 /* If this is a local watchpoint, we only want to check if the
5245 watchpoint frame is in scope if the current thread is the thread
5246 that was used to create the watchpoint. */
5247 if (!watchpoint_in_thread_scope (b))
5248 return WP_IGNORE;
5249
5250 if (b->exp_valid_block == NULL)
5251 within_current_scope = true;
5252 else
5253 {
5254 frame_info_ptr frame = get_current_frame ();
5255 struct gdbarch *frame_arch = get_frame_arch (frame);
5256 CORE_ADDR frame_pc = get_frame_pc (frame);
5257
5258 /* stack_frame_destroyed_p() returns a non-zero value if we're
5259 still in the function but the stack frame has already been
5260 invalidated. Since we can't rely on the values of local
5261 variables after the stack has been destroyed, we are treating
5262 the watchpoint in that state as `not changed' without further
5263 checking. Don't mark watchpoints as changed if the current
5264 frame is in an epilogue - even if they are in some other
5265 frame, our view of the stack is likely to be wrong and
5266 frame_find_by_id could error out. */
5267 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5268 return WP_IGNORE;
5269
5270 fr = frame_find_by_id (b->watchpoint_frame);
5271 within_current_scope = (fr != NULL);
5272
5273 /* If we've gotten confused in the unwinder, we might have
5274 returned a frame that can't describe this variable. */
5275 if (within_current_scope)
5276 {
5277 struct symbol *function;
5278
5279 function = get_frame_function (fr);
5280 if (function == NULL
5281 || !function->value_block ()->contains (b->exp_valid_block))
5282 within_current_scope = false;
5283 }
5284
5285 if (within_current_scope)
5286 /* If we end up stopping, the current frame will get selected
5287 in normal_stop. So this call to select_frame won't affect
5288 the user. */
5289 select_frame (fr);
5290 }
5291
5292 if (within_current_scope)
5293 {
5294 /* We use value_{,free_to_}mark because it could be a *long*
5295 time before we return to the command level and call
5296 free_all_values. We can't call free_all_values because we
5297 might be in the middle of evaluating a function call. */
5298
5299 struct value *mark;
5300 struct value *new_val;
5301
5302 if (is_masked_watchpoint (b))
5303 /* Since we don't know the exact trigger address (from
5304 stopped_data_address), just tell the user we've triggered
5305 a mask watchpoint. */
5306 return WP_VALUE_CHANGED;
5307
5308 mark = value_mark ();
5309 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5310 NULL, NULL, false);
5311
5312 if (b->val_bitsize != 0)
5313 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5314
5315 /* We use value_equal_contents instead of value_equal because
5316 the latter coerces an array to a pointer, thus comparing just
5317 the address of the array instead of its contents. This is
5318 not what we want. */
5319 if ((b->val != NULL) != (new_val != NULL)
5320 || (b->val != NULL && !value_equal_contents (b->val.get (),
5321 new_val)))
5322 {
5323 bs->old_val = b->val;
5324 b->val = release_value (new_val);
5325 b->val_valid = true;
5326 if (new_val != NULL)
5327 value_free_to_mark (mark);
5328 return WP_VALUE_CHANGED;
5329 }
5330 else
5331 {
5332 /* Nothing changed. */
5333 value_free_to_mark (mark);
5334 return WP_VALUE_NOT_CHANGED;
5335 }
5336 }
5337 else
5338 {
5339 /* This seems like the only logical thing to do because
5340 if we temporarily ignored the watchpoint, then when
5341 we reenter the block in which it is valid it contains
5342 garbage (in the case of a function, it may have two
5343 garbage values, one before and one after the prologue).
5344 So we can't even detect the first assignment to it and
5345 watch after that (since the garbage may or may not equal
5346 the first value assigned). */
5347 /* We print all the stop information in
5348 breakpointprint_it, but in this case, by the time we
5349 call breakpoint->print_it this bp will be deleted
5350 already. So we have no choice but print the information
5351 here. */
5352
5353 SWITCH_THRU_ALL_UIS ()
5354 {
5355 struct ui_out *uiout = current_uiout;
5356
5357 if (uiout->is_mi_like_p ())
5358 uiout->field_string
5359 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5360 uiout->message ("\nWatchpoint %pF deleted because the program has "
5361 "left the block in\n"
5362 "which its expression is valid.\n",
5363 signed_field ("wpnum", b->number));
5364 }
5365
5366 /* Make sure the watchpoint's commands aren't executed. */
5367 b->commands = NULL;
5368 watchpoint_del_at_next_stop (b);
5369
5370 return WP_DELETED;
5371 }
5372 }
5373
5374 /* Return true if it looks like target has stopped due to hitting
5375 breakpoint location BL. This function does not check if we should
5376 stop, only if BL explains the stop. */
5377
5378 static bool
bpstat_check_location(const struct bp_location * bl,const address_space * aspace,CORE_ADDR bp_addr,const target_waitstatus & ws)5379 bpstat_check_location (const struct bp_location *bl,
5380 const address_space *aspace, CORE_ADDR bp_addr,
5381 const target_waitstatus &ws)
5382 {
5383 struct breakpoint *b = bl->owner;
5384
5385 /* BL is from an existing breakpoint. */
5386 gdb_assert (b != NULL);
5387
5388 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
5389 }
5390
5391 /* Determine if the watched values have actually changed, and we
5392 should stop. If not, set BS->stop to false. */
5393
5394 static void
bpstat_check_watchpoint(bpstat * bs)5395 bpstat_check_watchpoint (bpstat *bs)
5396 {
5397 const struct bp_location *bl;
5398
5399 /* BS is built for existing struct breakpoint. */
5400 bl = bs->bp_location_at.get ();
5401 gdb_assert (bl != NULL);
5402 watchpoint *b = gdb::checked_static_cast<watchpoint *> (bs->breakpoint_at);
5403
5404 bool must_check_value = false;
5405
5406 if (b->type == bp_watchpoint)
5407 /* For a software watchpoint, we must always check the
5408 watched value. */
5409 must_check_value = true;
5410 else if (b->watchpoint_triggered == watch_triggered_yes)
5411 /* We have a hardware watchpoint (read, write, or access)
5412 and the target earlier reported an address watched by
5413 this watchpoint. */
5414 must_check_value = true;
5415 else if (b->watchpoint_triggered == watch_triggered_unknown
5416 && b->type == bp_hardware_watchpoint)
5417 /* We were stopped by a hardware watchpoint, but the target could
5418 not report the data address. We must check the watchpoint's
5419 value. Access and read watchpoints are out of luck; without
5420 a data address, we can't figure it out. */
5421 must_check_value = true;
5422
5423 if (must_check_value)
5424 {
5425 wp_check_result e;
5426
5427 try
5428 {
5429 e = watchpoint_check (bs);
5430 }
5431 catch (const gdb_exception_error &ex)
5432 {
5433 exception_fprintf (gdb_stderr, ex,
5434 "Error evaluating expression "
5435 "for watchpoint %d\n",
5436 b->number);
5437
5438 SWITCH_THRU_ALL_UIS ()
5439 {
5440 gdb_printf (_("Watchpoint %d deleted.\n"),
5441 b->number);
5442 }
5443 watchpoint_del_at_next_stop (b);
5444 e = WP_DELETED;
5445 }
5446
5447 switch (e)
5448 {
5449 case WP_DELETED:
5450 /* We've already printed what needs to be printed. */
5451 bs->print_it = print_it_done;
5452 /* Stop. */
5453 break;
5454 case WP_IGNORE:
5455 bs->print_it = print_it_noop;
5456 bs->stop = false;
5457 break;
5458 case WP_VALUE_CHANGED:
5459 if (b->type == bp_read_watchpoint)
5460 {
5461 /* There are two cases to consider here:
5462
5463 1. We're watching the triggered memory for reads.
5464 In that case, trust the target, and always report
5465 the watchpoint hit to the user. Even though
5466 reads don't cause value changes, the value may
5467 have changed since the last time it was read, and
5468 since we're not trapping writes, we will not see
5469 those, and as such we should ignore our notion of
5470 old value.
5471
5472 2. We're watching the triggered memory for both
5473 reads and writes. There are two ways this may
5474 happen:
5475
5476 2.1. This is a target that can't break on data
5477 reads only, but can break on accesses (reads or
5478 writes), such as e.g., x86. We detect this case
5479 at the time we try to insert read watchpoints.
5480
5481 2.2. Otherwise, the target supports read
5482 watchpoints, but, the user set an access or write
5483 watchpoint watching the same memory as this read
5484 watchpoint.
5485
5486 If we're watching memory writes as well as reads,
5487 ignore watchpoint hits when we find that the
5488 value hasn't changed, as reads don't cause
5489 changes. This still gives false positives when
5490 the program writes the same value to memory as
5491 what there was already in memory (we will confuse
5492 it for a read), but it's much better than
5493 nothing. */
5494
5495 int other_write_watchpoint = 0;
5496
5497 if (bl->watchpoint_type == hw_read)
5498 {
5499 for (breakpoint &other_b : all_breakpoints ())
5500 if (other_b.type == bp_hardware_watchpoint
5501 || other_b.type == bp_access_watchpoint)
5502 {
5503 watchpoint &other_w =
5504 gdb::checked_static_cast<watchpoint &> (other_b);
5505
5506 if (other_w.watchpoint_triggered
5507 == watch_triggered_yes)
5508 {
5509 other_write_watchpoint = 1;
5510 break;
5511 }
5512 }
5513 }
5514
5515 if (other_write_watchpoint
5516 || bl->watchpoint_type == hw_access)
5517 {
5518 /* We're watching the same memory for writes,
5519 and the value changed since the last time we
5520 updated it, so this trap must be for a write.
5521 Ignore it. */
5522 bs->print_it = print_it_noop;
5523 bs->stop = false;
5524 }
5525 }
5526 break;
5527 case WP_VALUE_NOT_CHANGED:
5528 if (b->type == bp_hardware_watchpoint
5529 || b->type == bp_watchpoint)
5530 {
5531 /* Don't stop: write watchpoints shouldn't fire if
5532 the value hasn't changed. */
5533 bs->print_it = print_it_noop;
5534 bs->stop = false;
5535 }
5536 /* Stop. */
5537 break;
5538 default:
5539 /* Can't happen. */
5540 break;
5541 }
5542 }
5543 else /* !must_check_value */
5544 {
5545 /* This is a case where some watchpoint(s) triggered, but
5546 not at the address of this watchpoint, or else no
5547 watchpoint triggered after all. So don't print
5548 anything for this watchpoint. */
5549 bs->print_it = print_it_noop;
5550 bs->stop = false;
5551 }
5552 }
5553
5554 /* For breakpoints that are currently marked as telling gdb to stop,
5555 check conditions (condition proper, frame, thread and ignore count)
5556 of breakpoint referred to by BS. If we should not stop for this
5557 breakpoint, set BS->stop to 0. */
5558
5559 static void
bpstat_check_breakpoint_conditions(bpstat * bs,thread_info * thread)5560 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5561 {
5562 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
5563
5564 const struct bp_location *bl;
5565 struct breakpoint *b;
5566 /* Assume stop. */
5567 bool condition_result = true;
5568 struct expression *cond;
5569
5570 gdb_assert (bs->stop);
5571
5572 /* BS is built for existing struct breakpoint. */
5573 bl = bs->bp_location_at.get ();
5574 gdb_assert (bl != NULL);
5575 b = bs->breakpoint_at;
5576 gdb_assert (b != NULL);
5577
5578 infrun_debug_printf ("thread = %s, breakpoint %d.%d",
5579 thread->ptid.to_string ().c_str (),
5580 b->number, find_loc_num_by_location (bl));
5581
5582 /* Even if the target evaluated the condition on its end and notified GDB, we
5583 need to do so again since GDB does not know if we stopped due to a
5584 breakpoint or a single step breakpoint. */
5585
5586 if (frame_id_p (b->frame_id)
5587 && b->frame_id != get_stack_frame_id (get_current_frame ()))
5588 {
5589 infrun_debug_printf ("incorrect frame %s not %s, not stopping",
5590 get_stack_frame_id (get_current_frame ()).to_string ().c_str (),
5591 b->frame_id.to_string ().c_str ());
5592 bs->stop = false;
5593 return;
5594 }
5595
5596 /* If this is a thread/task-specific breakpoint, don't waste cpu
5597 evaluating the condition if this isn't the specified
5598 thread/task. */
5599 if ((b->thread != -1 && b->thread != thread->global_num)
5600 || (b->inferior != -1 && b->inferior != thread->inf->num)
5601 || (b->task != -1 && b->task != ada_get_task_number (thread)))
5602 {
5603 infrun_debug_printf ("incorrect thread or task, not stopping");
5604 bs->stop = false;
5605 return;
5606 }
5607
5608 /* Evaluate extension language breakpoints that have a "stop" method
5609 implemented. */
5610 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5611
5612 if (is_watchpoint (b))
5613 {
5614 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
5615
5616 cond = w->cond_exp.get ();
5617 }
5618 else
5619 cond = bl->cond.get ();
5620
5621 if (cond != nullptr && b->disposition != disp_del_at_next_stop)
5622 {
5623 bool within_current_scope = true;
5624
5625 /* We use scoped_value_mark because it could be a long time
5626 before we return to the command level and call
5627 free_all_values. We can't call free_all_values because we
5628 might be in the middle of evaluating a function call. */
5629 scoped_value_mark mark;
5630
5631 watchpoint *w = nullptr;
5632 if (is_watchpoint (b))
5633 w = gdb::checked_static_cast<watchpoint *> (b);
5634
5635 /* Need to select the frame, with all that implies so that
5636 the conditions will have the right context. Because we
5637 use the frame, we will not see an inlined function's
5638 variables when we arrive at a breakpoint at the start
5639 of the inlined function; the current frame will be the
5640 call site. */
5641 if (w == NULL || w->cond_exp_valid_block == NULL)
5642 select_frame (get_current_frame ());
5643 else
5644 {
5645 frame_info_ptr frame;
5646
5647 /* For local watchpoint expressions, which particular
5648 instance of a local is being watched matters, so we
5649 keep track of the frame to evaluate the expression
5650 in. To evaluate the condition however, it doesn't
5651 really matter which instantiation of the function
5652 where the condition makes sense triggers the
5653 watchpoint. This allows an expression like "watch
5654 global if q > 10" set in `func', catch writes to
5655 global on all threads that call `func', or catch
5656 writes on all recursive calls of `func' by a single
5657 thread. We simply always evaluate the condition in
5658 the innermost frame that's executing where it makes
5659 sense to evaluate the condition. It seems
5660 intuitive. */
5661 frame = block_innermost_frame (w->cond_exp_valid_block);
5662 if (frame != NULL)
5663 select_frame (frame);
5664 else
5665 within_current_scope = false;
5666 }
5667 if (within_current_scope)
5668 {
5669 try
5670 {
5671 scoped_restore reset_in_cond_eval
5672 = make_scoped_restore (&thread->control.in_cond_eval, true);
5673 condition_result = breakpoint_cond_eval (cond);
5674 }
5675 catch (const gdb_exception_error &ex)
5676 {
5677 int locno = bpstat_locno (bs);
5678 if (locno != 0)
5679 exception_fprintf
5680 (gdb_stderr, ex,
5681 "Error in testing condition for breakpoint %d.%d:\n",
5682 b->number, locno);
5683 else
5684 exception_fprintf
5685 (gdb_stderr, ex,
5686 "Error in testing condition for breakpoint %d:\n",
5687 b->number);
5688 }
5689 }
5690 else
5691 {
5692 warning (_("Watchpoint condition cannot be tested "
5693 "in the current scope"));
5694 /* If we failed to set the right context for this
5695 watchpoint, unconditionally report it. */
5696 }
5697 /* FIXME-someday, should give breakpoint #. */
5698 }
5699
5700 if (cond != nullptr && !condition_result)
5701 {
5702 infrun_debug_printf ("condition_result = false, not stopping");
5703 bs->stop = false;
5704 return;
5705 }
5706 else if (b->ignore_count > 0)
5707 {
5708 infrun_debug_printf ("ignore count %d, not stopping",
5709 b->ignore_count);
5710 b->ignore_count--;
5711 bs->stop = false;
5712 /* Increase the hit count even though we don't stop. */
5713 ++(b->hit_count);
5714 notify_breakpoint_modified (b);
5715 return;
5716 }
5717
5718 if (bs->stop)
5719 infrun_debug_printf ("stopping at this breakpoint");
5720 else
5721 infrun_debug_printf ("not stopping at this breakpoint");
5722 }
5723
5724 /* Returns true if we need to track moribund locations of LOC's type
5725 on the current target. */
5726
5727 static bool
need_moribund_for_location_type(const struct bp_location * loc)5728 need_moribund_for_location_type (const struct bp_location *loc)
5729 {
5730 return ((loc->loc_type == bp_loc_software_breakpoint
5731 && !target_supports_stopped_by_sw_breakpoint ())
5732 || (loc->loc_type == bp_loc_hardware_breakpoint
5733 && !target_supports_stopped_by_hw_breakpoint ()));
5734 }
5735
5736 /* See breakpoint.h. */
5737
5738 bpstat *
build_bpstat_chain(const address_space * aspace,CORE_ADDR bp_addr,const target_waitstatus & ws)5739 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5740 const target_waitstatus &ws)
5741 {
5742 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5743
5744 for (breakpoint &b : all_breakpoints ())
5745 {
5746 if (!breakpoint_enabled (&b))
5747 continue;
5748
5749 for (bp_location &bl : b.locations ())
5750 {
5751 /* For hardware watchpoints, we look only at the first
5752 location. The watchpoint_check function will work on the
5753 entire expression, not the individual locations. For
5754 read watchpoints, the watchpoints_triggered function has
5755 checked all locations already. */
5756 if (b.type == bp_hardware_watchpoint && &bl != &b.first_loc ())
5757 break;
5758
5759 if (!bl.enabled || bl.disabled_by_cond || bl.shlib_disabled)
5760 continue;
5761
5762 if (!bpstat_check_location (&bl, aspace, bp_addr, ws))
5763 continue;
5764
5765 /* Come here if it's a watchpoint, or if the break address
5766 matches. */
5767
5768 bpstat *bs = new bpstat (&bl, &bs_link); /* Alloc a bpstat to
5769 explain stop. */
5770
5771 /* Assume we stop. Should we find a watchpoint that is not
5772 actually triggered, or if the condition of the breakpoint
5773 evaluates as false, we'll reset 'stop' to 0. */
5774 bs->stop = true;
5775 bs->print = true;
5776
5777 /* If this is a scope breakpoint, mark the associated
5778 watchpoint as triggered so that we will handle the
5779 out-of-scope event. We'll get to the watchpoint next
5780 iteration. */
5781 if (b.type == bp_watchpoint_scope && b.related_breakpoint != &b)
5782 {
5783 watchpoint *w
5784 = gdb::checked_static_cast<watchpoint *> (b.related_breakpoint);
5785
5786 w->watchpoint_triggered = watch_triggered_yes;
5787 }
5788 }
5789 }
5790
5791 /* Check if a moribund breakpoint explains the stop. */
5792 if (!target_supports_stopped_by_sw_breakpoint ()
5793 || !target_supports_stopped_by_hw_breakpoint ())
5794 {
5795 for (bp_location *loc : moribund_locations)
5796 {
5797 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5798 && need_moribund_for_location_type (loc))
5799 {
5800 bpstat *bs = new bpstat (loc, &bs_link);
5801 /* For hits of moribund locations, we should just proceed. */
5802 bs->stop = false;
5803 bs->print = false;
5804 bs->print_it = print_it_noop;
5805 }
5806 }
5807 }
5808
5809 return bs_head;
5810 }
5811
5812 /* See breakpoint.h. */
5813
5814 bpstat *
bpstat_stop_status(const address_space * aspace,CORE_ADDR bp_addr,thread_info * thread,const target_waitstatus & ws,bpstat * stop_chain)5815 bpstat_stop_status (const address_space *aspace,
5816 CORE_ADDR bp_addr, thread_info *thread,
5817 const target_waitstatus &ws,
5818 bpstat *stop_chain)
5819 {
5820 struct breakpoint *b = NULL;
5821 /* First item of allocated bpstat's. */
5822 bpstat *bs_head = stop_chain;
5823 bpstat *bs;
5824 int need_remove_insert;
5825 int removed_any;
5826
5827 /* First, build the bpstat chain with locations that explain a
5828 target stop, while being careful to not set the target running,
5829 as that may invalidate locations (in particular watchpoint
5830 locations are recreated). Resuming will happen here with
5831 breakpoint conditions or watchpoint expressions that include
5832 inferior function calls. */
5833 if (bs_head == NULL)
5834 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5835
5836 /* A bit of special processing for shlib breakpoints. We need to
5837 process solib loading here, so that the lists of loaded and
5838 unloaded libraries are correct before we handle "catch load" and
5839 "catch unload". */
5840 for (bs = bs_head; bs != NULL; bs = bs->next)
5841 {
5842 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5843 {
5844 handle_solib_event ();
5845 break;
5846 }
5847 }
5848
5849 /* Now go through the locations that caused the target to stop, and
5850 check whether we're interested in reporting this stop to higher
5851 layers, or whether we should resume the target transparently. */
5852
5853 removed_any = 0;
5854
5855 for (bs = bs_head; bs != NULL; bs = bs->next)
5856 {
5857 if (!bs->stop)
5858 continue;
5859
5860 b = bs->breakpoint_at;
5861 b->check_status (bs);
5862 if (bs->stop)
5863 {
5864 bpstat_check_breakpoint_conditions (bs, thread);
5865
5866 if (bs->stop)
5867 {
5868 ++(b->hit_count);
5869
5870 /* We will stop here. */
5871 if (b->disposition == disp_disable)
5872 {
5873 --(b->enable_count);
5874 if (b->enable_count <= 0)
5875 b->enable_state = bp_disabled;
5876 removed_any = 1;
5877 }
5878 notify_breakpoint_modified (b);
5879 if (b->silent)
5880 bs->print = false;
5881 bs->commands = b->commands;
5882 if (command_line_is_silent (bs->commands
5883 ? bs->commands.get () : NULL))
5884 bs->print = false;
5885
5886 b->after_condition_true (bs);
5887 }
5888
5889 }
5890
5891 /* Print nothing for this entry if we don't stop or don't
5892 print. */
5893 if (!bs->stop || !bs->print)
5894 bs->print_it = print_it_noop;
5895 }
5896
5897 /* If we aren't stopping, the value of some hardware watchpoint may
5898 not have changed, but the intermediate memory locations we are
5899 watching may have. Don't bother if we're stopping; this will get
5900 done later. */
5901 need_remove_insert = 0;
5902 if (! bpstat_causes_stop (bs_head))
5903 for (bs = bs_head; bs != NULL; bs = bs->next)
5904 if (!bs->stop
5905 && bs->breakpoint_at
5906 && is_hardware_watchpoint (bs->breakpoint_at))
5907 {
5908 watchpoint *w
5909 = gdb::checked_static_cast<watchpoint *> (bs->breakpoint_at);
5910
5911 update_watchpoint (w, false /* don't reparse. */);
5912 need_remove_insert = 1;
5913 }
5914
5915 if (need_remove_insert)
5916 update_global_location_list (UGLL_MAY_INSERT);
5917 else if (removed_any)
5918 update_global_location_list (UGLL_DONT_INSERT);
5919
5920 return bs_head;
5921 }
5922
5923 /* See breakpoint.h. */
5924
5925 bpstat *
bpstat_stop_status_nowatch(const address_space * aspace,CORE_ADDR bp_addr,thread_info * thread,const target_waitstatus & ws)5926 bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5927 thread_info *thread, const target_waitstatus &ws)
5928 {
5929 gdb_assert (!target_stopped_by_watchpoint ());
5930
5931 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5932 previous stop to avoid confusing bpstat_stop_status. */
5933 watchpoints_triggered (ws);
5934
5935 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5936 }
5937
5938 static void
handle_jit_event(CORE_ADDR address)5939 handle_jit_event (CORE_ADDR address)
5940 {
5941 struct gdbarch *gdbarch;
5942
5943 infrun_debug_printf ("handling bp_jit_event");
5944
5945 /* Switch terminal for any messages produced by
5946 breakpoint_re_set. */
5947 target_terminal::ours_for_output ();
5948
5949 gdbarch = get_frame_arch (get_current_frame ());
5950 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5951 thus it is expected that its objectfile can be found through
5952 minimal symbol lookup. If it doesn't work (and assert fails), it
5953 most likely means that `jit_breakpoint_re_set` was changes and this
5954 function needs to be updated too. */
5955 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5956 gdb_assert (jit_bp_sym.objfile != nullptr);
5957 objfile *objfile = jit_bp_sym.objfile;
5958 if (objfile->separate_debug_objfile_backlink)
5959 objfile = objfile->separate_debug_objfile_backlink;
5960 jit_event_handler (gdbarch, objfile);
5961
5962 target_terminal::inferior ();
5963 }
5964
5965 /* Prepare WHAT final decision for infrun. */
5966
5967 /* Decide what infrun needs to do with this bpstat. */
5968
5969 struct bpstat_what
bpstat_what(bpstat * bs_head)5970 bpstat_what (bpstat *bs_head)
5971 {
5972 struct bpstat_what retval;
5973 bpstat *bs;
5974
5975 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5976 retval.call_dummy = STOP_NONE;
5977 retval.is_longjmp = false;
5978
5979 for (bs = bs_head; bs != NULL; bs = bs->next)
5980 {
5981 /* Extract this BS's action. After processing each BS, we check
5982 if its action overrides all we've seem so far. */
5983 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5984 enum bptype bptype;
5985
5986 if (bs->breakpoint_at == NULL)
5987 {
5988 /* I suspect this can happen if it was a momentary
5989 breakpoint which has since been deleted. */
5990 bptype = bp_none;
5991 }
5992 else
5993 bptype = bs->breakpoint_at->type;
5994
5995 switch (bptype)
5996 {
5997 case bp_none:
5998 break;
5999 case bp_breakpoint:
6000 case bp_hardware_breakpoint:
6001 case bp_single_step:
6002 case bp_until:
6003 case bp_finish:
6004 case bp_shlib_event:
6005 if (bs->stop)
6006 {
6007 if (bs->print)
6008 this_action = BPSTAT_WHAT_STOP_NOISY;
6009 else
6010 this_action = BPSTAT_WHAT_STOP_SILENT;
6011 }
6012 else
6013 this_action = BPSTAT_WHAT_SINGLE;
6014 break;
6015 case bp_watchpoint:
6016 case bp_hardware_watchpoint:
6017 case bp_read_watchpoint:
6018 case bp_access_watchpoint:
6019 if (bs->stop)
6020 {
6021 if (bs->print)
6022 this_action = BPSTAT_WHAT_STOP_NOISY;
6023 else
6024 this_action = BPSTAT_WHAT_STOP_SILENT;
6025 }
6026 else
6027 {
6028 /* There was a watchpoint, but we're not stopping.
6029 This requires no further action. */
6030 }
6031 break;
6032 case bp_longjmp:
6033 case bp_longjmp_call_dummy:
6034 case bp_exception:
6035 if (bs->stop)
6036 {
6037 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
6038 retval.is_longjmp = bptype != bp_exception;
6039 }
6040 else
6041 this_action = BPSTAT_WHAT_SINGLE;
6042 break;
6043 case bp_longjmp_resume:
6044 case bp_exception_resume:
6045 if (bs->stop)
6046 {
6047 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
6048 retval.is_longjmp = bptype == bp_longjmp_resume;
6049 }
6050 else
6051 this_action = BPSTAT_WHAT_SINGLE;
6052 break;
6053 case bp_step_resume:
6054 if (bs->stop)
6055 this_action = BPSTAT_WHAT_STEP_RESUME;
6056 else
6057 {
6058 /* It is for the wrong frame. */
6059 this_action = BPSTAT_WHAT_SINGLE;
6060 }
6061 break;
6062 case bp_hp_step_resume:
6063 if (bs->stop)
6064 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
6065 else
6066 {
6067 /* It is for the wrong frame. */
6068 this_action = BPSTAT_WHAT_SINGLE;
6069 }
6070 break;
6071 case bp_watchpoint_scope:
6072 case bp_thread_event:
6073 case bp_overlay_event:
6074 case bp_longjmp_master:
6075 case bp_std_terminate_master:
6076 case bp_exception_master:
6077 this_action = BPSTAT_WHAT_SINGLE;
6078 break;
6079 case bp_catchpoint:
6080 if (bs->stop)
6081 {
6082 if (bs->print)
6083 this_action = BPSTAT_WHAT_STOP_NOISY;
6084 else
6085 this_action = BPSTAT_WHAT_STOP_SILENT;
6086 }
6087 else
6088 {
6089 /* Some catchpoints are implemented with breakpoints.
6090 For those, we need to step over the breakpoint. */
6091 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
6092 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
6093 this_action = BPSTAT_WHAT_SINGLE;
6094 }
6095 break;
6096 case bp_jit_event:
6097 this_action = BPSTAT_WHAT_SINGLE;
6098 break;
6099 case bp_call_dummy:
6100 /* Make sure the action is stop (silent or noisy),
6101 so infrun.c pops the dummy frame. */
6102 retval.call_dummy = STOP_STACK_DUMMY;
6103 this_action = BPSTAT_WHAT_STOP_SILENT;
6104 break;
6105 case bp_std_terminate:
6106 /* Make sure the action is stop (silent or noisy),
6107 so infrun.c pops the dummy frame. */
6108 retval.call_dummy = STOP_STD_TERMINATE;
6109 this_action = BPSTAT_WHAT_STOP_SILENT;
6110 break;
6111 case bp_tracepoint:
6112 case bp_fast_tracepoint:
6113 case bp_static_tracepoint:
6114 case bp_static_marker_tracepoint:
6115 /* Tracepoint hits should not be reported back to GDB, and
6116 if one got through somehow, it should have been filtered
6117 out already. */
6118 internal_error (_("bpstat_what: tracepoint encountered"));
6119 break;
6120 case bp_gnu_ifunc_resolver:
6121 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
6122 this_action = BPSTAT_WHAT_SINGLE;
6123 break;
6124 case bp_gnu_ifunc_resolver_return:
6125 /* The breakpoint will be removed, execution will restart from the
6126 PC of the former breakpoint. */
6127 this_action = BPSTAT_WHAT_KEEP_CHECKING;
6128 break;
6129
6130 case bp_dprintf:
6131 if (bs->stop)
6132 this_action = BPSTAT_WHAT_STOP_SILENT;
6133 else
6134 this_action = BPSTAT_WHAT_SINGLE;
6135 break;
6136
6137 default:
6138 internal_error (_("bpstat_what: unhandled bptype %d"), (int) bptype);
6139 }
6140
6141 retval.main_action = std::max (retval.main_action, this_action);
6142 }
6143
6144 return retval;
6145 }
6146
6147 void
bpstat_run_callbacks(bpstat * bs_head)6148 bpstat_run_callbacks (bpstat *bs_head)
6149 {
6150 bpstat *bs;
6151
6152 for (bs = bs_head; bs != NULL; bs = bs->next)
6153 {
6154 struct breakpoint *b = bs->breakpoint_at;
6155
6156 if (b == NULL)
6157 continue;
6158 switch (b->type)
6159 {
6160 case bp_jit_event:
6161 handle_jit_event (bs->bp_location_at->address);
6162 break;
6163 case bp_gnu_ifunc_resolver:
6164 gnu_ifunc_resolver_stop
6165 (gdb::checked_static_cast<code_breakpoint *> (b));
6166 break;
6167 case bp_gnu_ifunc_resolver_return:
6168 gnu_ifunc_resolver_return_stop
6169 (gdb::checked_static_cast<code_breakpoint *> (b));
6170 break;
6171 }
6172 }
6173 }
6174
6175 /* See breakpoint.h. */
6176
6177 bool
bpstat_should_step()6178 bpstat_should_step ()
6179 {
6180 for (breakpoint &b : all_breakpoints ())
6181 if (breakpoint_enabled (&b)
6182 && b.type == bp_watchpoint
6183 && b.has_locations ())
6184 return true;
6185
6186 return false;
6187 }
6188
6189 /* See breakpoint.h. */
6190
6191 bool
bpstat_causes_stop(bpstat * bs)6192 bpstat_causes_stop (bpstat *bs)
6193 {
6194 for (; bs != NULL; bs = bs->next)
6195 if (bs->stop)
6196 return true;
6197
6198 return false;
6199 }
6200
6201
6202
6203 /* Compute a number of spaces suitable to indent the next line
6204 so it starts at the position corresponding to the table column
6205 named COL_NAME in the currently active table of UIOUT. */
6206
6207 static int
wrap_indent_at_field(struct ui_out * uiout,const char * col_name)6208 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6209 {
6210 int i, total_width, width, align;
6211 const char *text;
6212
6213 total_width = 0;
6214 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
6215 {
6216 if (strcmp (text, col_name) == 0)
6217 return total_width;
6218
6219 total_width += width + 1;
6220 }
6221
6222 return 0;
6223 }
6224
6225 /* Determine if the locations of this breakpoint will have their conditions
6226 evaluated by the target, host or a mix of both. Returns the following:
6227
6228 "host": Host evals condition.
6229 "host or target": Host or Target evals condition.
6230 "target": Target evals condition.
6231 */
6232
6233 static const char *
bp_condition_evaluator(const breakpoint * b)6234 bp_condition_evaluator (const breakpoint *b)
6235 {
6236 char host_evals = 0;
6237 char target_evals = 0;
6238
6239 if (!b)
6240 return NULL;
6241
6242 if (!is_breakpoint (b))
6243 return NULL;
6244
6245 if (gdb_evaluates_breakpoint_condition_p ()
6246 || !target_supports_evaluation_of_breakpoint_conditions ())
6247 return condition_evaluation_host;
6248
6249 for (bp_location &bl : b->locations ())
6250 {
6251 if (bl.cond_bytecode)
6252 target_evals++;
6253 else
6254 host_evals++;
6255 }
6256
6257 if (host_evals && target_evals)
6258 return condition_evaluation_both;
6259 else if (target_evals)
6260 return condition_evaluation_target;
6261 else
6262 return condition_evaluation_host;
6263 }
6264
6265 /* Determine the breakpoint location's condition evaluator. This is
6266 similar to bp_condition_evaluator, but for locations. */
6267
6268 static const char *
bp_location_condition_evaluator(const struct bp_location * bl)6269 bp_location_condition_evaluator (const struct bp_location *bl)
6270 {
6271 if (bl && !is_breakpoint (bl->owner))
6272 return NULL;
6273
6274 if (gdb_evaluates_breakpoint_condition_p ()
6275 || !target_supports_evaluation_of_breakpoint_conditions ())
6276 return condition_evaluation_host;
6277
6278 if (bl && bl->cond_bytecode)
6279 return condition_evaluation_target;
6280 else
6281 return condition_evaluation_host;
6282 }
6283
6284 /* Print the LOC location out of the list of B->LOC locations. */
6285
6286 static void
print_breakpoint_location(const breakpoint * b,const bp_location * loc)6287 print_breakpoint_location (const breakpoint *b, const bp_location *loc)
6288 {
6289 struct ui_out *uiout = current_uiout;
6290
6291 scoped_restore_current_program_space restore_pspace;
6292
6293 if (loc != NULL && loc->shlib_disabled)
6294 loc = NULL;
6295
6296 if (loc != NULL)
6297 set_current_program_space (loc->pspace);
6298
6299 if (b->display_canonical)
6300 uiout->field_string ("what", b->locspec->to_string ());
6301 else if (loc && loc->symtab)
6302 {
6303 const struct symbol *sym = loc->symbol;
6304
6305 if (sym)
6306 {
6307 uiout->text ("in ");
6308 uiout->field_string ("func", sym->print_name (),
6309 function_name_style.style ());
6310 uiout->text (" ");
6311 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6312 uiout->text ("at ");
6313 }
6314 uiout->field_string ("file",
6315 symtab_to_filename_for_display (loc->symtab),
6316 file_name_style.style ());
6317 uiout->text (":");
6318
6319 if (uiout->is_mi_like_p ())
6320 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6321
6322 uiout->field_signed ("line", loc->line_number);
6323 }
6324 else if (loc)
6325 {
6326 string_file stb;
6327
6328 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6329 demangle, "");
6330 uiout->field_stream ("at", stb);
6331 }
6332 else
6333 {
6334 uiout->field_string ("pending", b->locspec->to_string ());
6335 /* If extra_string is available, it could be holding a condition
6336 or dprintf arguments. In either case, make sure it is printed,
6337 too, but only for non-MI streams. */
6338 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6339 {
6340 if (b->type == bp_dprintf)
6341 uiout->text (",");
6342 else
6343 uiout->text (" ");
6344 uiout->text (b->extra_string.get ());
6345 }
6346 }
6347
6348 if (loc && is_breakpoint (b)
6349 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6350 && bp_condition_evaluator (b) == condition_evaluation_both)
6351 {
6352 uiout->text (" (");
6353 uiout->field_string ("evaluated-by",
6354 bp_location_condition_evaluator (loc));
6355 uiout->text (")");
6356 }
6357 }
6358
6359 static const char *
bptype_string(enum bptype type)6360 bptype_string (enum bptype type)
6361 {
6362 struct ep_type_description
6363 {
6364 enum bptype type;
6365 const char *description;
6366 };
6367 static struct ep_type_description bptypes[] =
6368 {
6369 {bp_none, "?deleted?"},
6370 {bp_breakpoint, "breakpoint"},
6371 {bp_hardware_breakpoint, "hw breakpoint"},
6372 {bp_single_step, "sw single-step"},
6373 {bp_until, "until"},
6374 {bp_finish, "finish"},
6375 {bp_watchpoint, "watchpoint"},
6376 {bp_hardware_watchpoint, "hw watchpoint"},
6377 {bp_read_watchpoint, "read watchpoint"},
6378 {bp_access_watchpoint, "acc watchpoint"},
6379 {bp_longjmp, "longjmp"},
6380 {bp_longjmp_resume, "longjmp resume"},
6381 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6382 {bp_exception, "exception"},
6383 {bp_exception_resume, "exception resume"},
6384 {bp_step_resume, "step resume"},
6385 {bp_hp_step_resume, "high-priority step resume"},
6386 {bp_watchpoint_scope, "watchpoint scope"},
6387 {bp_call_dummy, "call dummy"},
6388 {bp_std_terminate, "std::terminate"},
6389 {bp_shlib_event, "shlib events"},
6390 {bp_thread_event, "thread events"},
6391 {bp_overlay_event, "overlay events"},
6392 {bp_longjmp_master, "longjmp master"},
6393 {bp_std_terminate_master, "std::terminate master"},
6394 {bp_exception_master, "exception master"},
6395 {bp_catchpoint, "catchpoint"},
6396 {bp_tracepoint, "tracepoint"},
6397 {bp_fast_tracepoint, "fast tracepoint"},
6398 {bp_static_tracepoint, "static tracepoint"},
6399 {bp_static_marker_tracepoint, "static marker tracepoint"},
6400 {bp_dprintf, "dprintf"},
6401 {bp_jit_event, "jit events"},
6402 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6403 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6404 };
6405
6406 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6407 || ((int) type != bptypes[(int) type].type))
6408 internal_error (_("bptypes table does not describe type #%d."),
6409 (int) type);
6410
6411 return bptypes[(int) type].description;
6412 }
6413
6414 /* For MI, output a field named 'thread-groups' with a list as the value.
6415 For CLI, prefix the list with the string 'inf'. */
6416
6417 static void
output_thread_groups(struct ui_out * uiout,const char * field_name,const std::vector<int> & inf_nums,int mi_only)6418 output_thread_groups (struct ui_out *uiout,
6419 const char *field_name,
6420 const std::vector<int> &inf_nums,
6421 int mi_only)
6422 {
6423 int is_mi = uiout->is_mi_like_p ();
6424
6425 /* For backward compatibility, don't display inferiors in CLI unless
6426 there are several. Always display them for MI. */
6427 if (!is_mi && mi_only)
6428 return;
6429
6430 ui_out_emit_list list_emitter (uiout, field_name);
6431
6432 for (size_t i = 0; i < inf_nums.size (); i++)
6433 {
6434 if (is_mi)
6435 {
6436 char mi_group[10];
6437
6438 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6439 uiout->field_string (NULL, mi_group);
6440 }
6441 else
6442 {
6443 if (i == 0)
6444 uiout->text (" inf ");
6445 else
6446 uiout->text (", ");
6447
6448 uiout->text (plongest (inf_nums[i]));
6449 }
6450 }
6451 }
6452
6453 /* See breakpoint.h. */
6454
6455 bool fix_breakpoint_script_output_globally = false;
6456
6457 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6458 instead of going via breakpoint_ops::print_one. This makes "maint
6459 info breakpoints" show the software breakpoint locations of
6460 catchpoints, which are considered internal implementation
6461 detail. Returns true if RAW_LOC is false and if the breakpoint's
6462 print_one method did something; false otherwise. */
6463
6464 static bool
print_one_breakpoint_location(struct breakpoint * b,struct bp_location * loc,int loc_number,const bp_location ** last_loc,int allflag,bool raw_loc)6465 print_one_breakpoint_location (struct breakpoint *b,
6466 struct bp_location *loc,
6467 int loc_number,
6468 const bp_location **last_loc,
6469 int allflag, bool raw_loc)
6470 {
6471 struct command_line *l;
6472 static char bpenables[] = "nynny";
6473
6474 struct ui_out *uiout = current_uiout;
6475 bool header_of_multiple = false;
6476 bool part_of_multiple = (loc != NULL);
6477 struct value_print_options opts;
6478
6479 get_user_print_options (&opts);
6480
6481 gdb_assert (!loc || loc_number != 0);
6482 /* See comment in print_one_breakpoint concerning treatment of
6483 breakpoints with single disabled location. */
6484 if (loc == NULL
6485 && (b->has_locations ()
6486 && (b->has_multiple_locations ()
6487 || !b->first_loc ().enabled || b->first_loc ().disabled_by_cond)))
6488 header_of_multiple = true;
6489
6490 if (loc == NULL && b->has_locations ())
6491 loc = &b->first_loc ();
6492
6493 annotate_record ();
6494
6495 /* 1 */
6496 annotate_field (0);
6497 if (part_of_multiple)
6498 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6499 else
6500 uiout->field_signed ("number", b->number);
6501
6502 /* 2 */
6503 annotate_field (1);
6504 if (part_of_multiple)
6505 uiout->field_skip ("type");
6506 else
6507 uiout->field_string ("type", bptype_string (b->type));
6508
6509 /* 3 */
6510 annotate_field (2);
6511 if (part_of_multiple)
6512 uiout->field_skip ("disp");
6513 else
6514 uiout->field_string ("disp", bpdisp_text (b->disposition));
6515
6516 /* 4 */
6517 annotate_field (3);
6518 if (part_of_multiple)
6519 {
6520 /* For locations that are disabled because of an invalid
6521 condition, display "N*" on the CLI, where "*" refers to a
6522 footnote below the table. For MI, simply display a "N"
6523 without a footnote. On the CLI, for enabled locations whose
6524 breakpoint is disabled, display "y-". */
6525 auto get_enable_state = [uiout, loc] () -> const char *
6526 {
6527 if (uiout->is_mi_like_p ())
6528 {
6529 if (loc->disabled_by_cond)
6530 return "N";
6531 else if (!loc->enabled)
6532 return "n";
6533 else
6534 return "y";
6535 }
6536 else
6537 {
6538 if (loc->disabled_by_cond)
6539 return "N*";
6540 else if (!loc->enabled)
6541 return "n";
6542 else if (!breakpoint_enabled (loc->owner))
6543 return "y-";
6544 else
6545 return "y";
6546 }
6547 };
6548 uiout->field_string ("enabled", get_enable_state ());
6549 }
6550 else
6551 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6552
6553 /* 5 and 6 */
6554 bool result = false;
6555 if (!raw_loc && b->print_one (last_loc))
6556 result = true;
6557 else
6558 {
6559 if (is_watchpoint (b))
6560 {
6561 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
6562
6563 /* Field 4, the address, is omitted (which makes the columns
6564 not line up too nicely with the headers, but the effect
6565 is relatively readable). */
6566 if (opts.addressprint)
6567 uiout->field_skip ("addr");
6568 annotate_field (5);
6569 uiout->field_string ("what", w->exp_string.get ());
6570 }
6571 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6572 || is_ada_exception_catchpoint (b))
6573 {
6574 if (opts.addressprint)
6575 {
6576 annotate_field (4);
6577 if (header_of_multiple)
6578 uiout->field_string ("addr", "<MULTIPLE>",
6579 metadata_style.style ());
6580 else if (!b->has_locations () || loc->shlib_disabled)
6581 uiout->field_string ("addr", "<PENDING>",
6582 metadata_style.style ());
6583 else
6584 uiout->field_core_addr ("addr",
6585 loc->gdbarch, loc->address);
6586 }
6587 annotate_field (5);
6588 if (!header_of_multiple)
6589 print_breakpoint_location (b, loc);
6590 if (b->has_locations ())
6591 *last_loc = &b->first_loc ();
6592 }
6593 }
6594
6595 if (loc != NULL && !header_of_multiple)
6596 {
6597 std::vector<int> inf_nums;
6598 int mi_only = 1;
6599
6600 for (inferior *inf : all_inferiors ())
6601 {
6602 if (inf->pspace == loc->pspace)
6603 inf_nums.push_back (inf->num);
6604 }
6605
6606 /* For backward compatibility, don't display inferiors in CLI unless
6607 there are several. Always display for MI. */
6608 if (allflag
6609 || (!gdbarch_has_global_breakpoints (current_inferior ()->arch ())
6610 && (program_spaces.size () > 1
6611 || number_of_inferiors () > 1)
6612 /* LOC is for existing B, it cannot be in
6613 moribund_locations and thus having NULL OWNER. */
6614 && loc->owner->type != bp_catchpoint))
6615 mi_only = 0;
6616 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6617 }
6618
6619 /* In the MI output, each location of a thread or task specific
6620 breakpoint includes the relevant thread or task ID. This is done for
6621 backwards compatibility reasons.
6622
6623 For the CLI output, the thread/task information is printed on a
6624 separate line, see the 'stop only in thread' and 'stop only in task'
6625 output below. */
6626 if (part_of_multiple && uiout->is_mi_like_p ())
6627 {
6628 if (b->thread != -1)
6629 uiout->field_signed ("thread", b->thread);
6630 else if (b->task != -1)
6631 uiout->field_signed ("task", b->task);
6632 else if (b->inferior != -1)
6633 uiout->field_signed ("inferior", b->inferior);
6634 }
6635
6636 uiout->text ("\n");
6637
6638 if (!part_of_multiple)
6639 b->print_one_detail (uiout);
6640
6641 if (part_of_multiple && frame_id_p (b->frame_id))
6642 {
6643 annotate_field (6);
6644 uiout->text ("\tstop only in stack frame at ");
6645 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6646 the frame ID. */
6647 uiout->field_core_addr ("frame",
6648 b->gdbarch, b->frame_id.stack_addr);
6649 uiout->text ("\n");
6650 }
6651
6652 if (!part_of_multiple && b->cond_string)
6653 {
6654 annotate_field (7);
6655 if (is_tracepoint (b))
6656 uiout->text ("\ttrace only if ");
6657 else
6658 uiout->text ("\tstop only if ");
6659 uiout->field_string ("cond", b->cond_string.get ());
6660
6661 /* Print whether the target is doing the breakpoint's condition
6662 evaluation. If GDB is doing the evaluation, don't print anything. */
6663 if (is_breakpoint (b)
6664 && breakpoint_condition_evaluation_mode ()
6665 == condition_evaluation_target)
6666 {
6667 uiout->message (" (%pF evals)",
6668 string_field ("evaluated-by",
6669 bp_condition_evaluator (b)));
6670 }
6671 uiout->text ("\n");
6672 }
6673
6674 if (!part_of_multiple && b->thread != -1)
6675 {
6676 /* FIXME should make an annotation for this. */
6677 uiout->text ("\tstop only in thread ");
6678 if (uiout->is_mi_like_p ())
6679 uiout->field_signed ("thread", b->thread);
6680 else
6681 {
6682 struct thread_info *thr = find_thread_global_id (b->thread);
6683
6684 uiout->field_string ("thread", print_thread_id (thr));
6685 }
6686 uiout->text ("\n");
6687 }
6688
6689 if (!part_of_multiple && b->task != -1)
6690 {
6691 uiout->text ("\tstop only in task ");
6692 uiout->field_signed ("task", b->task);
6693 uiout->text ("\n");
6694 }
6695
6696 if (!part_of_multiple && b->inferior != -1)
6697 {
6698 uiout->text ("\tstop only in inferior ");
6699 uiout->field_signed ("inferior", b->inferior);
6700 uiout->text ("\n");
6701 }
6702
6703 if (!part_of_multiple)
6704 {
6705 if (b->hit_count)
6706 {
6707 /* FIXME should make an annotation for this. */
6708 if (is_catchpoint (b))
6709 uiout->text ("\tcatchpoint");
6710 else if (is_tracepoint (b))
6711 uiout->text ("\ttracepoint");
6712 else
6713 uiout->text ("\tbreakpoint");
6714 uiout->text (" already hit ");
6715 uiout->field_signed ("times", b->hit_count);
6716 if (b->hit_count == 1)
6717 uiout->text (" time\n");
6718 else
6719 uiout->text (" times\n");
6720 }
6721 else
6722 {
6723 /* Output the count also if it is zero, but only if this is mi. */
6724 if (uiout->is_mi_like_p ())
6725 uiout->field_signed ("times", b->hit_count);
6726 }
6727 }
6728
6729 if (!part_of_multiple && b->ignore_count)
6730 {
6731 annotate_field (8);
6732 uiout->message ("\tignore next %pF hits\n",
6733 signed_field ("ignore", b->ignore_count));
6734 }
6735
6736 /* Note that an enable count of 1 corresponds to "enable once"
6737 behavior, which is reported by the combination of enablement and
6738 disposition, so we don't need to mention it here. */
6739 if (!part_of_multiple && b->enable_count > 1)
6740 {
6741 annotate_field (8);
6742 uiout->text ("\tdisable after ");
6743 /* Tweak the wording to clarify that ignore and enable counts
6744 are distinct, and have additive effect. */
6745 if (b->ignore_count)
6746 uiout->text ("additional ");
6747 else
6748 uiout->text ("next ");
6749 uiout->field_signed ("enable", b->enable_count);
6750 uiout->text (" hits\n");
6751 }
6752
6753 if (!part_of_multiple && is_tracepoint (b))
6754 {
6755 tracepoint *tp = gdb::checked_static_cast<tracepoint *> (b);
6756
6757 if (tp->traceframe_usage)
6758 {
6759 uiout->text ("\ttrace buffer usage ");
6760 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6761 uiout->text (" bytes\n");
6762 }
6763 }
6764
6765 l = b->commands ? b->commands.get () : NULL;
6766 if (!part_of_multiple && l)
6767 {
6768 annotate_field (9);
6769
6770 bool use_fixed_output =
6771 (uiout->test_flags (fix_breakpoint_script_output)
6772 || fix_breakpoint_script_output_globally);
6773
6774 std::optional<ui_out_emit_tuple> tuple_emitter;
6775 std::optional<ui_out_emit_list> list_emitter;
6776
6777 if (use_fixed_output)
6778 list_emitter.emplace (uiout, "script");
6779 else
6780 tuple_emitter.emplace (uiout, "script");
6781
6782 print_command_lines (uiout, l, 4);
6783 }
6784
6785 if (is_tracepoint (b))
6786 {
6787 tracepoint *t = gdb::checked_static_cast<tracepoint *> (b);
6788
6789 if (!part_of_multiple && t->pass_count)
6790 {
6791 annotate_field (10);
6792 uiout->text ("\tpass count ");
6793 uiout->field_signed ("pass", t->pass_count);
6794 uiout->text (" \n");
6795 }
6796
6797 /* Don't display it when tracepoint or tracepoint location is
6798 pending. */
6799 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6800 {
6801 annotate_field (11);
6802
6803 if (uiout->is_mi_like_p ())
6804 uiout->field_string ("installed",
6805 loc->inserted ? "y" : "n");
6806 else
6807 {
6808 if (loc->inserted)
6809 uiout->text ("\t");
6810 else
6811 uiout->text ("\tnot ");
6812 uiout->text ("installed on target\n");
6813 }
6814 }
6815 }
6816
6817 if (uiout->is_mi_like_p () && !part_of_multiple)
6818 {
6819 if (is_watchpoint (b))
6820 {
6821 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
6822
6823 uiout->field_string ("original-location", w->exp_string.get ());
6824 }
6825 else if (b->locspec != nullptr)
6826 {
6827 const char *str = b->locspec->to_string ();
6828 if (str != nullptr)
6829 uiout->field_string ("original-location", str);
6830 }
6831 }
6832
6833 return result;
6834 }
6835
6836 /* See breakpoint.h. */
6837
6838 bool fix_multi_location_breakpoint_output_globally = false;
6839
6840 static void
print_one_breakpoint(breakpoint * b,const bp_location ** last_loc,int allflag)6841 print_one_breakpoint (breakpoint *b, const bp_location **last_loc, int allflag)
6842 {
6843 struct ui_out *uiout = current_uiout;
6844 bool use_fixed_output
6845 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6846 || fix_multi_location_breakpoint_output_globally);
6847
6848 std::optional<ui_out_emit_tuple> bkpt_tuple_emitter (std::in_place, uiout,
6849 "bkpt");
6850 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6851 allflag, false);
6852
6853 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6854 are outside. */
6855 if (!use_fixed_output)
6856 bkpt_tuple_emitter.reset ();
6857
6858 /* If this breakpoint has custom print function,
6859 it's already printed. Otherwise, print individual
6860 locations, if any. */
6861 if (!printed || allflag)
6862 {
6863 /* If breakpoint has a single location that is disabled, we
6864 print it as if it had several locations, since otherwise it's
6865 hard to represent "breakpoint enabled, location disabled"
6866 situation.
6867
6868 Note that while hardware watchpoints have several locations
6869 internally, that's not a property exposed to users.
6870
6871 Likewise, while catchpoints may be implemented with
6872 breakpoints (e.g., catch throw), that's not a property
6873 exposed to users. We do however display the internal
6874 breakpoint locations with "maint info breakpoints". */
6875 if (!is_hardware_watchpoint (b)
6876 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6877 || is_ada_exception_catchpoint (b))
6878 && (allflag
6879 || (b->has_locations ()
6880 && (b->has_multiple_locations ()
6881 || !b->first_loc ().enabled
6882 || b->first_loc ().disabled_by_cond))))
6883 {
6884 std::optional<ui_out_emit_list> locations_list;
6885
6886 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6887 MI record. For later versions, place breakpoint locations in a
6888 list. */
6889 if (uiout->is_mi_like_p () && use_fixed_output)
6890 locations_list.emplace (uiout, "locations");
6891
6892 int n = 1;
6893 for (bp_location &loc : b->locations ())
6894 {
6895 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6896 print_one_breakpoint_location (b, &loc, n, last_loc,
6897 allflag, allflag);
6898 n++;
6899 }
6900 }
6901 }
6902 }
6903
6904 static int
breakpoint_address_bits(struct breakpoint * b)6905 breakpoint_address_bits (struct breakpoint *b)
6906 {
6907 int print_address_bits = 0;
6908
6909 for (bp_location &loc : b->locations ())
6910 {
6911 if (!bl_address_is_meaningful (&loc))
6912 continue;
6913
6914 int addr_bit = gdbarch_addr_bit (loc.gdbarch);
6915 if (addr_bit > print_address_bits)
6916 print_address_bits = addr_bit;
6917 }
6918
6919 return print_address_bits;
6920 }
6921
6922 /* See breakpoint.h. */
6923
6924 void
print_breakpoint(breakpoint * b)6925 print_breakpoint (breakpoint *b)
6926 {
6927 const bp_location *dummy_loc = nullptr;
6928 print_one_breakpoint (b, &dummy_loc, 0);
6929 }
6930
6931 /* Return true if this breakpoint was set by the user, false if it is
6932 internal or momentary. */
6933
6934 int
user_breakpoint_p(struct breakpoint * b)6935 user_breakpoint_p (struct breakpoint *b)
6936 {
6937 return b->number > 0;
6938 }
6939
6940 /* See breakpoint.h. */
6941
6942 int
pending_breakpoint_p(struct breakpoint * b)6943 pending_breakpoint_p (struct breakpoint *b)
6944 {
6945 return !b->has_locations ();
6946 }
6947
6948 /* Print information on breakpoints (including watchpoints and tracepoints).
6949
6950 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6951 understood by number_or_range_parser. Only breakpoints included in this
6952 list are then printed.
6953
6954 If SHOW_INTERNAL is true, print internal breakpoints.
6955
6956 If FILTER is non-NULL, call it on each breakpoint and only include the
6957 ones for which it returns true.
6958
6959 Return the total number of breakpoints listed. */
6960
6961 static int
breakpoint_1(const char * bp_num_list,bool show_internal,bool (* filter)(const struct breakpoint *))6962 breakpoint_1 (const char *bp_num_list, bool show_internal,
6963 bool (*filter) (const struct breakpoint *))
6964 {
6965 const bp_location *last_loc = nullptr;
6966 int nr_printable_breakpoints;
6967 struct value_print_options opts;
6968 int print_address_bits = 0;
6969 int print_type_col_width = 14;
6970 struct ui_out *uiout = current_uiout;
6971 bool has_disabled_by_cond_location = false;
6972
6973 get_user_print_options (&opts);
6974
6975 /* Compute the number of rows in the table, as well as the size
6976 required for address fields. */
6977 nr_printable_breakpoints = 0;
6978 for (breakpoint &b : all_breakpoints ())
6979 {
6980 /* If we have a filter, only list the breakpoints it accepts. */
6981 if (filter && !filter (&b))
6982 continue;
6983
6984 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6985 accept. Skip the others. */
6986 if (bp_num_list != NULL && *bp_num_list != '\0')
6987 {
6988 if (show_internal && parse_and_eval_long (bp_num_list) != b.number)
6989 continue;
6990 if (!show_internal && !number_is_in_list (bp_num_list, b.number))
6991 continue;
6992 }
6993
6994 if (show_internal || user_breakpoint_p (&b))
6995 {
6996 int addr_bit, type_len;
6997
6998 addr_bit = breakpoint_address_bits (&b);
6999 if (addr_bit > print_address_bits)
7000 print_address_bits = addr_bit;
7001
7002 type_len = strlen (bptype_string (b.type));
7003 if (type_len > print_type_col_width)
7004 print_type_col_width = type_len;
7005
7006 nr_printable_breakpoints++;
7007 }
7008 }
7009
7010 {
7011 ui_out_emit_table table_emitter (uiout,
7012 opts.addressprint ? 6 : 5,
7013 nr_printable_breakpoints,
7014 "BreakpointTable");
7015
7016 if (nr_printable_breakpoints > 0)
7017 annotate_breakpoints_headers ();
7018 if (nr_printable_breakpoints > 0)
7019 annotate_field (0);
7020 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
7021 if (nr_printable_breakpoints > 0)
7022 annotate_field (1);
7023 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
7024 if (nr_printable_breakpoints > 0)
7025 annotate_field (2);
7026 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
7027 if (nr_printable_breakpoints > 0)
7028 annotate_field (3);
7029 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
7030 if (opts.addressprint)
7031 {
7032 if (nr_printable_breakpoints > 0)
7033 annotate_field (4);
7034 if (print_address_bits <= 32)
7035 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
7036 else
7037 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
7038 }
7039 if (nr_printable_breakpoints > 0)
7040 annotate_field (5);
7041 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
7042 uiout->table_body ();
7043 if (nr_printable_breakpoints > 0)
7044 annotate_breakpoints_table ();
7045
7046 for (breakpoint &b : all_breakpoints ())
7047 {
7048 QUIT;
7049 /* If we have a filter, only list the breakpoints it accepts. */
7050 if (filter && !filter (&b))
7051 continue;
7052
7053 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
7054 accept. Skip the others. */
7055
7056 if (bp_num_list != NULL && *bp_num_list != '\0')
7057 {
7058 if (show_internal) /* maintenance info breakpoint */
7059 {
7060 if (parse_and_eval_long (bp_num_list) != b.number)
7061 continue;
7062 }
7063 else /* all others */
7064 {
7065 if (!number_is_in_list (bp_num_list, b.number))
7066 continue;
7067 }
7068 }
7069 /* We only print out user settable breakpoints unless the
7070 show_internal is set. */
7071 if (show_internal || user_breakpoint_p (&b))
7072 {
7073 print_one_breakpoint (&b, &last_loc, show_internal);
7074 for (bp_location &loc : b.locations ())
7075 if (loc.disabled_by_cond)
7076 has_disabled_by_cond_location = true;
7077 }
7078 }
7079 }
7080
7081 if (nr_printable_breakpoints == 0)
7082 {
7083 /* If there's a filter, let the caller decide how to report
7084 empty list. */
7085 if (!filter)
7086 {
7087 if (bp_num_list == NULL || *bp_num_list == '\0')
7088 uiout->message ("No breakpoints, watchpoints, tracepoints, "
7089 "or catchpoints.\n");
7090 else
7091 uiout->message ("No breakpoint, watchpoint, tracepoint, "
7092 "or catchpoint matching '%s'.\n", bp_num_list);
7093 }
7094 }
7095 else
7096 {
7097 if (last_loc && !server_command)
7098 set_next_address (last_loc->gdbarch, last_loc->address);
7099
7100 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
7101 uiout->message (_("(*): Breakpoint condition is invalid at this "
7102 "location.\n"));
7103 }
7104
7105 /* FIXME? Should this be moved up so that it is only called when
7106 there have been breakpoints? */
7107 annotate_breakpoints_table_end ();
7108
7109 return nr_printable_breakpoints;
7110 }
7111
7112 /* Display the value of default-collect in a way that is generally
7113 compatible with the breakpoint list. */
7114
7115 static void
default_collect_info(void)7116 default_collect_info (void)
7117 {
7118 struct ui_out *uiout = current_uiout;
7119
7120 /* If it has no value (which is frequently the case), say nothing; a
7121 message like "No default-collect." gets in user's face when it's
7122 not wanted. */
7123 if (default_collect.empty ())
7124 return;
7125
7126 /* The following phrase lines up nicely with per-tracepoint collect
7127 actions. */
7128 uiout->text ("default collect ");
7129 uiout->field_string ("default-collect", default_collect);
7130 uiout->text (" \n");
7131 }
7132
7133 static void
info_breakpoints_command(const char * args,int from_tty)7134 info_breakpoints_command (const char *args, int from_tty)
7135 {
7136 breakpoint_1 (args, false, NULL);
7137
7138 default_collect_info ();
7139 }
7140
7141 static void
info_watchpoints_command(const char * args,int from_tty)7142 info_watchpoints_command (const char *args, int from_tty)
7143 {
7144 int num_printed = breakpoint_1 (args, false, is_watchpoint);
7145 struct ui_out *uiout = current_uiout;
7146
7147 if (num_printed == 0)
7148 {
7149 if (args == NULL || *args == '\0')
7150 uiout->message ("No watchpoints.\n");
7151 else
7152 uiout->message ("No watchpoint matching '%s'.\n", args);
7153 }
7154 }
7155
7156 static void
maintenance_info_breakpoints(const char * args,int from_tty)7157 maintenance_info_breakpoints (const char *args, int from_tty)
7158 {
7159 breakpoint_1 (args, true, NULL);
7160
7161 default_collect_info ();
7162 }
7163
7164 static bool
breakpoint_has_pc(struct breakpoint * b,struct program_space * pspace,CORE_ADDR pc,struct obj_section * section)7165 breakpoint_has_pc (struct breakpoint *b,
7166 struct program_space *pspace,
7167 CORE_ADDR pc, struct obj_section *section)
7168 {
7169 for (bp_location &bl : b->locations ())
7170 {
7171 if (bl.pspace == pspace
7172 && bl.address == pc
7173 && (!overlay_debugging || bl.section == section))
7174 return true;
7175 }
7176 return false;
7177 }
7178
7179 /* See breakpoint.h. */
7180
7181 void
describe_other_breakpoints(struct gdbarch * gdbarch,struct program_space * pspace,CORE_ADDR pc,struct obj_section * section,int thread)7182 describe_other_breakpoints (struct gdbarch *gdbarch,
7183 struct program_space *pspace, CORE_ADDR pc,
7184 struct obj_section *section, int thread)
7185 {
7186 int others = 0;
7187
7188 for (breakpoint &b : all_breakpoints ())
7189 others += (user_breakpoint_p (&b)
7190 && breakpoint_has_pc (&b, pspace, pc, section));
7191
7192 if (others > 0)
7193 {
7194 if (others == 1)
7195 gdb_printf (_("Note: breakpoint "));
7196 else /* if (others == ???) */
7197 gdb_printf (_("Note: breakpoints "));
7198 for (breakpoint &b : all_breakpoints ())
7199 if (user_breakpoint_p (&b)
7200 && breakpoint_has_pc (&b, pspace, pc, section))
7201 {
7202 others--;
7203 gdb_printf ("%d", b.number);
7204 if (b.thread == -1 && thread != -1)
7205 gdb_printf (" (all threads)");
7206 else if (b.thread != -1)
7207 {
7208 struct thread_info *thr = find_thread_global_id (b.thread);
7209 gdb_printf (" (thread %s)", print_thread_id (thr));
7210 }
7211 else if (b.task != -1)
7212 gdb_printf (" (task %d)", b.task);
7213 gdb_printf ("%s%s ",
7214 ((b.enable_state == bp_disabled
7215 || b.enable_state == bp_call_disabled)
7216 ? " (disabled)"
7217 : ""),
7218 (others > 1) ? ","
7219 : ((others == 1) ? " and" : ""));
7220 }
7221 current_uiout->message (_("also set at pc %ps.\n"),
7222 styled_string (address_style.style (),
7223 paddress (gdbarch, pc)));
7224 }
7225 }
7226
7227
7228 /* Return true iff it is meaningful to use the address member of LOC.
7229 For some breakpoint types, the locations' address members are
7230 irrelevant and it makes no sense to attempt to compare them to
7231 other addresses (or use them for any other purpose either).
7232
7233 More specifically, software watchpoints and catchpoints that are
7234 not backed by breakpoints always have a zero valued location
7235 address and we don't want to mark breakpoints of any of these types
7236 to be a duplicate of an actual breakpoint location at address
7237 zero. */
7238
7239 static bool
bl_address_is_meaningful(bp_location * loc)7240 bl_address_is_meaningful (bp_location *loc)
7241 {
7242 return loc->loc_type != bp_loc_other;
7243 }
7244
7245 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7246 true if LOC1 and LOC2 represent the same watchpoint location. */
7247
7248 static bool
watchpoint_locations_match(const struct bp_location * loc1,const struct bp_location * loc2)7249 watchpoint_locations_match (const struct bp_location *loc1,
7250 const struct bp_location *loc2)
7251 {
7252 watchpoint *w1 = gdb::checked_static_cast<watchpoint *> (loc1->owner);
7253 watchpoint *w2 = gdb::checked_static_cast<watchpoint *> (loc2->owner);
7254
7255 /* Both of them must exist. */
7256 gdb_assert (w1 != NULL);
7257 gdb_assert (w2 != NULL);
7258
7259 /* If the target can evaluate the condition expression in hardware,
7260 then we we need to insert both watchpoints even if they are at
7261 the same place. Otherwise the watchpoint will only trigger when
7262 the condition of whichever watchpoint was inserted evaluates to
7263 true, not giving a chance for GDB to check the condition of the
7264 other watchpoint. */
7265 if ((w1->cond_exp
7266 && target_can_accel_watchpoint_condition (loc1->address,
7267 loc1->length,
7268 loc1->watchpoint_type,
7269 w1->cond_exp.get ()))
7270 || (w2->cond_exp
7271 && target_can_accel_watchpoint_condition (loc2->address,
7272 loc2->length,
7273 loc2->watchpoint_type,
7274 w2->cond_exp.get ())))
7275 return false;
7276
7277 /* Note that this checks the owner's type, not the location's. In
7278 case the target does not support read watchpoints, but does
7279 support access watchpoints, we'll have bp_read_watchpoint
7280 watchpoints with hw_access locations. Those should be considered
7281 duplicates of hw_read locations. The hw_read locations will
7282 become hw_access locations later. */
7283 return (loc1->owner->type == loc2->owner->type
7284 && loc1->pspace->aspace == loc2->pspace->aspace
7285 && loc1->address == loc2->address
7286 && loc1->length == loc2->length);
7287 }
7288
7289 /* See breakpoint.h. */
7290
7291 int
breakpoint_address_match(const address_space * aspace1,CORE_ADDR addr1,const address_space * aspace2,CORE_ADDR addr2)7292 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
7293 const address_space *aspace2, CORE_ADDR addr2)
7294 {
7295 return ((gdbarch_has_global_breakpoints (current_inferior ()->arch ())
7296 || aspace1 == aspace2)
7297 && addr1 == addr2);
7298 }
7299
7300 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7301 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7302 matches ASPACE2. On targets that have global breakpoints, the address
7303 space doesn't really matter. */
7304
7305 static bool
breakpoint_address_match_range(const address_space * aspace1,CORE_ADDR addr1,int len1,const address_space * aspace2,CORE_ADDR addr2)7306 breakpoint_address_match_range (const address_space *aspace1,
7307 CORE_ADDR addr1,
7308 int len1, const address_space *aspace2,
7309 CORE_ADDR addr2)
7310 {
7311 return ((gdbarch_has_global_breakpoints (current_inferior ()->arch ())
7312 || aspace1 == aspace2)
7313 && addr2 >= addr1 && addr2 < addr1 + len1);
7314 }
7315
7316 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7317 a ranged breakpoint. In most targets, a match happens only if ASPACE
7318 matches the breakpoint's address space. On targets that have global
7319 breakpoints, the address space doesn't really matter. */
7320
7321 static bool
breakpoint_location_address_match(struct bp_location * bl,const address_space * aspace,CORE_ADDR addr)7322 breakpoint_location_address_match (struct bp_location *bl,
7323 const address_space *aspace,
7324 CORE_ADDR addr)
7325 {
7326 return (breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
7327 aspace, addr)
7328 || (bl->length
7329 && breakpoint_address_match_range (bl->pspace->aspace.get (),
7330 bl->address, bl->length,
7331 aspace, addr)));
7332 }
7333
7334 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7335 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7336 match happens only if ASPACE matches the breakpoint's address
7337 space. On targets that have global breakpoints, the address space
7338 doesn't really matter. */
7339
7340 static bool
breakpoint_location_address_range_overlap(struct bp_location * bl,const address_space * aspace,CORE_ADDR addr,int len)7341 breakpoint_location_address_range_overlap (struct bp_location *bl,
7342 const address_space *aspace,
7343 CORE_ADDR addr, int len)
7344 {
7345 if (gdbarch_has_global_breakpoints (current_inferior ()->arch ())
7346 || bl->pspace->aspace.get () == aspace)
7347 {
7348 int bl_len = bl->length != 0 ? bl->length : 1;
7349
7350 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7351 return 1;
7352 }
7353 return 0;
7354 }
7355
7356 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7357 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7358 true, otherwise returns false. */
7359
7360 static bool
tracepoint_locations_match(const struct bp_location * loc1,const struct bp_location * loc2)7361 tracepoint_locations_match (const struct bp_location *loc1,
7362 const struct bp_location *loc2)
7363 {
7364 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7365 /* Since tracepoint locations are never duplicated with others', tracepoint
7366 locations at the same address of different tracepoints are regarded as
7367 different locations. */
7368 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7369 else
7370 return false;
7371 }
7372
7373 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7374 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7375 the same location. If SW_HW_BPS_MATCH is true, then software
7376 breakpoint locations and hardware breakpoint locations match,
7377 otherwise they don't. */
7378
7379 static bool
breakpoint_locations_match(const struct bp_location * loc1,const struct bp_location * loc2,bool sw_hw_bps_match)7380 breakpoint_locations_match (const struct bp_location *loc1,
7381 const struct bp_location *loc2,
7382 bool sw_hw_bps_match)
7383 {
7384 int hw_point1, hw_point2;
7385
7386 /* Both of them must not be in moribund_locations. */
7387 gdb_assert (loc1->owner != NULL);
7388 gdb_assert (loc2->owner != NULL);
7389
7390 hw_point1 = is_hardware_watchpoint (loc1->owner);
7391 hw_point2 = is_hardware_watchpoint (loc2->owner);
7392
7393 if (hw_point1 != hw_point2)
7394 return false;
7395 else if (hw_point1)
7396 return watchpoint_locations_match (loc1, loc2);
7397 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7398 return tracepoint_locations_match (loc1, loc2);
7399 else
7400 /* We compare bp_location.length in order to cover ranged
7401 breakpoints. Keep this in sync with
7402 bp_location_is_less_than. */
7403 return (breakpoint_address_match (loc1->pspace->aspace.get (),
7404 loc1->address,
7405 loc2->pspace->aspace.get (),
7406 loc2->address)
7407 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7408 && loc1->length == loc2->length);
7409 }
7410
7411 static void
breakpoint_adjustment_warning(CORE_ADDR from_addr,CORE_ADDR to_addr,int bnum,bool have_bnum)7412 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7413 int bnum, bool have_bnum)
7414 {
7415 /* The longest string possibly returned by hex_string_custom
7416 is 50 chars. These must be at least that big for safety. */
7417 char astr1[64];
7418 char astr2[64];
7419
7420 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7421 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7422 if (have_bnum)
7423 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7424 bnum, astr1, astr2);
7425 else
7426 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7427 }
7428
7429 /* Adjust a breakpoint's address to account for architectural
7430 constraints on breakpoint placement. Return the adjusted address.
7431 Note: Very few targets require this kind of adjustment. For most
7432 targets, this function is simply the identity function. */
7433
7434 static CORE_ADDR
adjust_breakpoint_address(struct gdbarch * gdbarch,CORE_ADDR bpaddr,enum bptype bptype,struct program_space * pspace)7435 adjust_breakpoint_address (struct gdbarch *gdbarch,
7436 CORE_ADDR bpaddr, enum bptype bptype,
7437 struct program_space *pspace)
7438 {
7439 gdb_assert (pspace != nullptr);
7440
7441 if (bptype == bp_watchpoint
7442 || bptype == bp_hardware_watchpoint
7443 || bptype == bp_read_watchpoint
7444 || bptype == bp_access_watchpoint
7445 || bptype == bp_catchpoint)
7446 {
7447 /* Watchpoints and the various bp_catch_* eventpoints should not
7448 have their addresses modified. */
7449 return bpaddr;
7450 }
7451 else if (bptype == bp_single_step)
7452 {
7453 /* Single-step breakpoints should not have their addresses
7454 modified. If there's any architectural constrain that
7455 applies to this address, then it should have already been
7456 taken into account when the breakpoint was created in the
7457 first place. If we didn't do this, stepping through e.g.,
7458 Thumb-2 IT blocks would break. */
7459 return bpaddr;
7460 }
7461 else
7462 {
7463 CORE_ADDR adjusted_bpaddr = bpaddr;
7464
7465 /* Some targets have architectural constraints on the placement
7466 of breakpoint instructions. Obtain the adjusted address. */
7467 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7468 {
7469 /* Targets that implement this adjustment function will likely
7470 inspect either the symbol table, target memory at BPADDR, or
7471 even state registers, so ensure a suitable thread (and its
7472 associated program space) are currently selected. */
7473 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7474 switch_to_program_space_and_thread (pspace);
7475 adjusted_bpaddr
7476 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7477 }
7478
7479 adjusted_bpaddr
7480 = gdbarch_remove_non_address_bits (gdbarch, adjusted_bpaddr);
7481
7482 /* An adjusted breakpoint address can significantly alter
7483 a user's expectations. Print a warning if an adjustment
7484 is required. */
7485 if (adjusted_bpaddr != bpaddr)
7486 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, false);
7487
7488 return adjusted_bpaddr;
7489 }
7490 }
7491
7492 static bp_loc_type
bp_location_from_bp_type(bptype type)7493 bp_location_from_bp_type (bptype type)
7494 {
7495 switch (type)
7496 {
7497 case bp_breakpoint:
7498 case bp_single_step:
7499 case bp_until:
7500 case bp_finish:
7501 case bp_longjmp:
7502 case bp_longjmp_resume:
7503 case bp_longjmp_call_dummy:
7504 case bp_exception:
7505 case bp_exception_resume:
7506 case bp_step_resume:
7507 case bp_hp_step_resume:
7508 case bp_watchpoint_scope:
7509 case bp_call_dummy:
7510 case bp_std_terminate:
7511 case bp_shlib_event:
7512 case bp_thread_event:
7513 case bp_overlay_event:
7514 case bp_jit_event:
7515 case bp_longjmp_master:
7516 case bp_std_terminate_master:
7517 case bp_exception_master:
7518 case bp_gnu_ifunc_resolver:
7519 case bp_gnu_ifunc_resolver_return:
7520 case bp_dprintf:
7521 return bp_loc_software_breakpoint;
7522
7523 case bp_hardware_breakpoint:
7524 return bp_loc_hardware_breakpoint;
7525
7526 case bp_hardware_watchpoint:
7527 case bp_read_watchpoint:
7528 case bp_access_watchpoint:
7529 return bp_loc_hardware_watchpoint;
7530
7531 case bp_watchpoint:
7532 return bp_loc_software_watchpoint;
7533
7534 case bp_tracepoint:
7535 case bp_fast_tracepoint:
7536 case bp_static_tracepoint:
7537 case bp_static_marker_tracepoint:
7538 return bp_loc_tracepoint;
7539
7540 case bp_catchpoint:
7541 return bp_loc_other;
7542
7543 default:
7544 internal_error (_("unknown breakpoint type"));
7545 }
7546 }
7547
bp_location(breakpoint * owner,bp_loc_type type)7548 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7549 {
7550 this->owner = owner;
7551 this->cond_bytecode = NULL;
7552 this->shlib_disabled = 0;
7553 this->enabled = 1;
7554 this->disabled_by_cond = false;
7555
7556 this->loc_type = type;
7557
7558 if (this->loc_type == bp_loc_software_breakpoint
7559 || this->loc_type == bp_loc_hardware_breakpoint)
7560 mark_breakpoint_location_modified (this);
7561
7562 incref ();
7563 }
7564
bp_location(breakpoint * owner)7565 bp_location::bp_location (breakpoint *owner)
7566 : bp_location::bp_location (owner,
7567 bp_location_from_bp_type (owner->type))
7568 {
7569 }
7570
7571 /* See breakpoint.h. */
7572
7573 std::string
to_string()7574 bp_location::to_string () const
7575 {
7576 string_file stb;
7577 ui_out_redirect_pop redir (current_uiout, &stb);
7578 print_breakpoint_location (this->owner, this);
7579 return stb.release ();
7580 }
7581
7582 /* Decrement reference count. If the reference count reaches 0,
7583 destroy the bp_location. Sets *BLP to NULL. */
7584
7585 static void
decref_bp_location(struct bp_location ** blp)7586 decref_bp_location (struct bp_location **blp)
7587 {
7588 bp_location_ref_policy::decref (*blp);
7589 *blp = NULL;
7590 }
7591
7592 /* Add breakpoint B at the end of the global breakpoint chain. */
7593
7594 static breakpoint *
add_to_breakpoint_chain(std::unique_ptr<breakpoint> && b)7595 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7596 {
7597 /* Add this breakpoint to the end of the chain so that a list of
7598 breakpoints will come out in order of increasing numbers. */
7599
7600 breakpoint_chain.push_back (*b.release ());
7601
7602 return &breakpoint_chain.back ();
7603 }
7604
7605 /* Initialize loc->function_name. */
7606
7607 static void
set_breakpoint_location_function(struct bp_location * loc)7608 set_breakpoint_location_function (struct bp_location *loc)
7609 {
7610 gdb_assert (loc->owner != NULL);
7611
7612 if (loc->owner->type == bp_breakpoint
7613 || loc->owner->type == bp_hardware_breakpoint
7614 || is_tracepoint (loc->owner))
7615 {
7616 const char *function_name;
7617
7618 if (loc->msymbol != NULL
7619 && (loc->msymbol->type () == mst_text_gnu_ifunc
7620 || loc->msymbol->type () == mst_data_gnu_ifunc))
7621 {
7622 struct breakpoint *b = loc->owner;
7623
7624 function_name = loc->msymbol->linkage_name ();
7625
7626 if (b->type == bp_breakpoint
7627 && b->has_single_location ()
7628 && b->related_breakpoint == b)
7629 {
7630 /* Create only the whole new breakpoint of this type but do not
7631 mess more complicated breakpoints with multiple locations. */
7632 b->type = bp_gnu_ifunc_resolver;
7633 /* Remember the resolver's address for use by the return
7634 breakpoint. */
7635 loc->related_address = loc->address;
7636 }
7637 }
7638 else
7639 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7640
7641 if (function_name)
7642 loc->function_name = make_unique_xstrdup (function_name);
7643 }
7644 }
7645
7646 /* Attempt to determine architecture of location identified by SAL. */
7647 struct gdbarch *
get_sal_arch(struct symtab_and_line sal)7648 get_sal_arch (struct symtab_and_line sal)
7649 {
7650 if (sal.section)
7651 return sal.section->objfile->arch ();
7652 if (sal.symtab)
7653 return sal.symtab->compunit ()->objfile ()->arch ();
7654
7655 return NULL;
7656 }
7657
7658 /* Call this routine when stepping and nexting to enable a breakpoint
7659 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7660 initiated the operation. */
7661
7662 void
set_longjmp_breakpoint(struct thread_info * tp,struct frame_id frame)7663 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7664 {
7665 int thread = tp->global_num;
7666
7667 /* To avoid having to rescan all objfile symbols at every step,
7668 we maintain a list of continually-inserted but always disabled
7669 longjmp "master" breakpoints. Here, we simply create momentary
7670 clones of those and enable them for the requested thread. */
7671 for (breakpoint &b : all_breakpoints_safe ())
7672 if (b.pspace == current_program_space
7673 && (b.type == bp_longjmp_master
7674 || b.type == bp_exception_master))
7675 {
7676 bptype type = b.type == bp_longjmp_master ? bp_longjmp : bp_exception;
7677 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7678 after their removal. */
7679 momentary_breakpoint_from_master (&b, type, 1, thread);
7680 }
7681
7682 tp->initiating_frame = frame;
7683 }
7684
7685 /* Delete all longjmp breakpoints from THREAD. */
7686 void
delete_longjmp_breakpoint(int thread)7687 delete_longjmp_breakpoint (int thread)
7688 {
7689 for (breakpoint &b : all_breakpoints_safe ())
7690 if (b.type == bp_longjmp || b.type == bp_exception)
7691 {
7692 if (b.thread == thread)
7693 {
7694 gdb_assert (b.inferior == -1);
7695 delete_breakpoint (&b);
7696 }
7697 }
7698 }
7699
7700 void
delete_longjmp_breakpoint_at_next_stop(int thread)7701 delete_longjmp_breakpoint_at_next_stop (int thread)
7702 {
7703 for (breakpoint &b : all_breakpoints_safe ())
7704 if (b.type == bp_longjmp || b.type == bp_exception)
7705 {
7706 if (b.thread == thread)
7707 {
7708 gdb_assert (b.inferior == -1);
7709 b.disposition = disp_del_at_next_stop;
7710 }
7711 }
7712 }
7713
7714 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7715 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7716 pointer to any of them. Return NULL if this system cannot place longjmp
7717 breakpoints. */
7718
7719 struct breakpoint *
set_longjmp_breakpoint_for_call_dummy(void)7720 set_longjmp_breakpoint_for_call_dummy (void)
7721 {
7722 breakpoint *retval = nullptr;
7723
7724 for (breakpoint &b : all_breakpoints ())
7725 if (b.pspace == current_program_space && b.type == bp_longjmp_master)
7726 {
7727 int thread = inferior_thread ()->global_num;
7728 breakpoint *new_b
7729 = momentary_breakpoint_from_master (&b, bp_longjmp_call_dummy,
7730 1, thread);
7731
7732 /* Link NEW_B into the chain of RETVAL breakpoints. */
7733
7734 gdb_assert (new_b->related_breakpoint == new_b);
7735 if (retval == NULL)
7736 retval = new_b;
7737 new_b->related_breakpoint = retval;
7738 while (retval->related_breakpoint != new_b->related_breakpoint)
7739 retval = retval->related_breakpoint;
7740 retval->related_breakpoint = new_b;
7741 }
7742
7743 return retval;
7744 }
7745
7746 /* Verify all existing dummy frames and their associated breakpoints for
7747 TP. Remove those which can no longer be found in the current frame
7748 stack.
7749
7750 If the unwind fails then there is not sufficient information to discard
7751 dummy frames. In this case, elide the clean up and the dummy frames will
7752 be cleaned up next time this function is called from a location where
7753 unwinding is possible. */
7754
7755 void
check_longjmp_breakpoint_for_call_dummy(struct thread_info * tp)7756 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7757 {
7758 /* We would need to delete breakpoints other than the current one while
7759 iterating, so all_breakpoints_safe is not sufficient to make that safe.
7760 Save all breakpoints to delete in that set and delete them at the end. */
7761 std::unordered_set<breakpoint *> to_delete;
7762
7763 for (struct breakpoint &b : all_breakpoints ())
7764 {
7765 if (b.type == bp_longjmp_call_dummy && b.thread == tp->global_num)
7766 {
7767 gdb_assert (b.inferior == -1);
7768 struct breakpoint *dummy_b = b.related_breakpoint;
7769
7770 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7771 chained off b->related_breakpoint. */
7772 while (dummy_b != &b && dummy_b->type != bp_call_dummy)
7773 dummy_b = dummy_b->related_breakpoint;
7774
7775 /* If there was no bp_call_dummy breakpoint then there's nothing
7776 more to do. Or, if the dummy frame associated with the
7777 bp_call_dummy is still on the stack then we need to leave this
7778 bp_call_dummy in place. */
7779 if (dummy_b->type != bp_call_dummy
7780 || frame_find_by_id (dummy_b->frame_id) != NULL)
7781 continue;
7782
7783 /* We didn't find the dummy frame on the stack, this could be
7784 because we have longjmp'd to a stack frame that is previous to
7785 the dummy frame, or it could be because the stack unwind is
7786 broken at some point between the longjmp frame and the dummy
7787 frame.
7788
7789 Next we figure out why the stack unwind stopped. If it looks
7790 like the unwind is complete then we assume the dummy frame has
7791 been jumped over, however, if the unwind stopped for an
7792 unexpected reason then we assume the stack unwind is currently
7793 broken, and that we will (eventually) return to the dummy
7794 frame.
7795
7796 It might be tempting to consider using frame_id_inner here, but
7797 that is not safe. There is no guarantee that the stack frames
7798 we are looking at here are even on the same stack as the
7799 original dummy frame, hence frame_id_inner can't be used. See
7800 the comments on frame_id_inner for more details. */
7801 bool unwind_finished_unexpectedly = false;
7802 for (frame_info_ptr fi = get_current_frame (); fi != nullptr; )
7803 {
7804 frame_info_ptr prev = get_prev_frame (fi);
7805 if (prev == nullptr)
7806 {
7807 /* FI is the last stack frame. Why did this frame not
7808 unwind further? */
7809 auto stop_reason = get_frame_unwind_stop_reason (fi);
7810 if (stop_reason != UNWIND_NO_REASON
7811 && stop_reason != UNWIND_OUTERMOST)
7812 unwind_finished_unexpectedly = true;
7813 }
7814 fi = prev;
7815 }
7816 if (unwind_finished_unexpectedly)
7817 continue;
7818
7819 dummy_frame_discard (dummy_b->frame_id, tp);
7820
7821 for (breakpoint *related_breakpoint = b.related_breakpoint;
7822 related_breakpoint != &b;
7823 related_breakpoint = related_breakpoint->related_breakpoint)
7824 to_delete.insert (b.related_breakpoint);
7825
7826 to_delete.insert (&b);
7827 }
7828 }
7829
7830 for (breakpoint *b : to_delete)
7831 delete_breakpoint (b);
7832 }
7833
7834 void
enable_overlay_breakpoints(void)7835 enable_overlay_breakpoints (void)
7836 {
7837 for (breakpoint &b : all_breakpoints ())
7838 if (b.type == bp_overlay_event)
7839 {
7840 b.enable_state = bp_enabled;
7841 update_global_location_list (UGLL_MAY_INSERT);
7842 overlay_events_enabled = 1;
7843 }
7844 }
7845
7846 void
disable_overlay_breakpoints(void)7847 disable_overlay_breakpoints (void)
7848 {
7849 for (breakpoint &b : all_breakpoints ())
7850 if (b.type == bp_overlay_event)
7851 {
7852 b.enable_state = bp_disabled;
7853 update_global_location_list (UGLL_DONT_INSERT);
7854 overlay_events_enabled = 0;
7855 }
7856 }
7857
7858 /* Set an active std::terminate breakpoint for each std::terminate
7859 master breakpoint. */
7860 void
set_std_terminate_breakpoint(void)7861 set_std_terminate_breakpoint (void)
7862 {
7863 for (breakpoint &b : all_breakpoints_safe ())
7864 if (b.pspace == current_program_space
7865 && b.type == bp_std_terminate_master)
7866 {
7867 momentary_breakpoint_from_master (&b, bp_std_terminate, 1,
7868 inferior_thread ()->global_num);
7869 }
7870 }
7871
7872 /* Delete all the std::terminate breakpoints. */
7873 void
delete_std_terminate_breakpoint(void)7874 delete_std_terminate_breakpoint (void)
7875 {
7876 for (breakpoint &b : all_breakpoints_safe ())
7877 if (b.type == bp_std_terminate)
7878 delete_breakpoint (&b);
7879 }
7880
7881 struct breakpoint *
create_thread_event_breakpoint(struct gdbarch * gdbarch,CORE_ADDR address)7882 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7883 {
7884 struct breakpoint *b;
7885
7886 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
7887
7888 b->enable_state = bp_enabled;
7889 /* locspec has to be used or breakpoint_re_set will delete me. */
7890 b->locspec = new_address_location_spec (b->first_loc ().address, NULL, 0);
7891
7892 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7893
7894 return b;
7895 }
7896
7897 struct lang_and_radix
7898 {
7899 enum language lang;
7900 int radix;
7901 };
7902
7903 /* Create a breakpoint for JIT code registration and unregistration. */
7904
7905 struct breakpoint *
create_jit_event_breakpoint(struct gdbarch * gdbarch,CORE_ADDR address)7906 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7907 {
7908 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
7909 }
7910
7911 /* Remove JIT code registration and unregistration breakpoint(s). */
7912
7913 void
remove_jit_event_breakpoints(void)7914 remove_jit_event_breakpoints (void)
7915 {
7916 for (breakpoint &b : all_breakpoints_safe ())
7917 if (b.type == bp_jit_event
7918 && b.first_loc ().pspace == current_program_space)
7919 delete_breakpoint (&b);
7920 }
7921
7922 void
remove_solib_event_breakpoints(void)7923 remove_solib_event_breakpoints (void)
7924 {
7925 for (breakpoint &b : all_breakpoints_safe ())
7926 if (b.type == bp_shlib_event
7927 && b.first_loc ().pspace == current_program_space)
7928 delete_breakpoint (&b);
7929 }
7930
7931 /* See breakpoint.h. */
7932
7933 void
remove_solib_event_breakpoints_at_next_stop(void)7934 remove_solib_event_breakpoints_at_next_stop (void)
7935 {
7936 for (breakpoint &b : all_breakpoints_safe ())
7937 if (b.type == bp_shlib_event
7938 && b.first_loc ().pspace == current_program_space)
7939 b.disposition = disp_del_at_next_stop;
7940 }
7941
7942 /* Helper for create_solib_event_breakpoint /
7943 create_and_insert_solib_event_breakpoint. Allows specifying which
7944 INSERT_MODE to pass through to update_global_location_list. */
7945
7946 static struct breakpoint *
create_solib_event_breakpoint_1(struct gdbarch * gdbarch,CORE_ADDR address,enum ugll_insert_mode insert_mode)7947 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7948 enum ugll_insert_mode insert_mode)
7949 {
7950 struct breakpoint *b;
7951
7952 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
7953 update_global_location_list_nothrow (insert_mode);
7954 return b;
7955 }
7956
7957 struct breakpoint *
create_solib_event_breakpoint(struct gdbarch * gdbarch,CORE_ADDR address)7958 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7959 {
7960 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7961 }
7962
7963 /* See breakpoint.h. */
7964
7965 struct breakpoint *
create_and_insert_solib_event_breakpoint(struct gdbarch * gdbarch,CORE_ADDR address)7966 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7967 {
7968 struct breakpoint *b;
7969
7970 /* Explicitly tell update_global_location_list to insert
7971 locations. */
7972 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7973 if (!b->first_loc ().inserted)
7974 {
7975 delete_breakpoint (b);
7976 return NULL;
7977 }
7978 return b;
7979 }
7980
7981 /* See breakpoint.h. */
7982
7983 void
disable_breakpoints_in_shlibs(program_space * pspace)7984 disable_breakpoints_in_shlibs (program_space *pspace)
7985 {
7986 for (bp_location *loc : all_bp_locations ())
7987 {
7988 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7989 struct breakpoint *b = loc->owner;
7990
7991 /* We apply the check to all breakpoints, including disabled for
7992 those with loc->duplicate set. This is so that when breakpoint
7993 becomes enabled, or the duplicate is removed, gdb will try to
7994 insert all breakpoints. If we don't set shlib_disabled here,
7995 we'll try to insert those breakpoints and fail. */
7996 if (((b->type == bp_breakpoint)
7997 || (b->type == bp_jit_event)
7998 || (b->type == bp_hardware_breakpoint)
7999 || (is_tracepoint (b)))
8000 && loc->pspace == pspace
8001 && !loc->shlib_disabled
8002 && solib_name_from_address (loc->pspace, loc->address)
8003 )
8004 {
8005 loc->shlib_disabled = 1;
8006 }
8007 }
8008 }
8009
8010 /* Disable any breakpoints and tracepoints that are in SOLIB upon
8011 notification of unloaded_shlib. Only apply to enabled breakpoints,
8012 disabled ones can just stay disabled. */
8013
8014 static void
disable_breakpoints_in_unloaded_shlib(program_space * pspace,const solib & solib)8015 disable_breakpoints_in_unloaded_shlib (program_space *pspace, const solib &solib)
8016 {
8017 bool disabled_shlib_breaks = false;
8018
8019 for (bp_location *loc : all_bp_locations ())
8020 {
8021 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
8022 struct breakpoint *b = loc->owner;
8023
8024 if (pspace == loc->pspace
8025 && !loc->shlib_disabled
8026 && (((b->type == bp_breakpoint
8027 || b->type == bp_jit_event
8028 || b->type == bp_hardware_breakpoint)
8029 && (loc->loc_type == bp_loc_hardware_breakpoint
8030 || loc->loc_type == bp_loc_software_breakpoint))
8031 || is_tracepoint (b))
8032 && solib_contains_address_p (solib, loc->address))
8033 {
8034 loc->shlib_disabled = 1;
8035 /* At this point, we cannot rely on remove_breakpoint
8036 succeeding so we must mark the breakpoint as not inserted
8037 to prevent future errors occurring in remove_breakpoints. */
8038 loc->inserted = 0;
8039
8040 /* This may cause duplicate notifications for the same breakpoint. */
8041 notify_breakpoint_modified (b);
8042
8043 if (!disabled_shlib_breaks)
8044 {
8045 target_terminal::ours_for_output ();
8046 warning (_("Temporarily disabling breakpoints "
8047 "for unloaded shared library \"%s\""),
8048 solib.so_name.c_str ());
8049 }
8050 disabled_shlib_breaks = true;
8051 }
8052 }
8053 }
8054
8055 /* Disable any breakpoints and tracepoints in OBJFILE upon
8056 notification of free_objfile. Only apply to enabled breakpoints,
8057 disabled ones can just stay disabled. */
8058
8059 static void
disable_breakpoints_in_freed_objfile(struct objfile * objfile)8060 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
8061 {
8062 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8063 managed by the user with add-symbol-file/remove-symbol-file.
8064 Similarly to how breakpoints in shared libraries are handled in
8065 response to "nosharedlibrary", mark breakpoints in such modules
8066 shlib_disabled so they end up uninserted on the next global
8067 location list update. Shared libraries not loaded by the user
8068 aren't handled here -- they're already handled in
8069 disable_breakpoints_in_unloaded_shlib, called by solib.c's
8070 solib_unloaded observer. We skip objfiles that are not
8071 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8072 main objfile). */
8073 if ((objfile->flags & OBJF_SHARED) == 0
8074 || (objfile->flags & OBJF_USERLOADED) == 0)
8075 return;
8076
8077 for (breakpoint &b : all_breakpoints ())
8078 {
8079 bool bp_modified = false;
8080
8081 if (!is_breakpoint (&b) && !is_tracepoint (&b))
8082 continue;
8083
8084 for (bp_location &loc : b.locations ())
8085 {
8086 CORE_ADDR loc_addr = loc.address;
8087
8088 if (loc.loc_type != bp_loc_hardware_breakpoint
8089 && loc.loc_type != bp_loc_software_breakpoint)
8090 continue;
8091
8092 if (loc.shlib_disabled != 0)
8093 continue;
8094
8095 if (objfile->pspace != loc.pspace)
8096 continue;
8097
8098 if (loc.loc_type != bp_loc_hardware_breakpoint
8099 && loc.loc_type != bp_loc_software_breakpoint)
8100 continue;
8101
8102 if (is_addr_in_objfile (loc_addr, objfile))
8103 {
8104 loc.shlib_disabled = 1;
8105 /* At this point, we don't know whether the object was
8106 unmapped from the inferior or not, so leave the
8107 inserted flag alone. We'll handle failure to
8108 uninsert quietly, in case the object was indeed
8109 unmapped. */
8110
8111 mark_breakpoint_location_modified (&loc);
8112
8113 bp_modified = true;
8114 }
8115 }
8116
8117 if (bp_modified)
8118 notify_breakpoint_modified (&b);
8119 }
8120 }
8121
8122 /* See breakpoint.h. */
8123
breakpoint(struct gdbarch * gdbarch_,enum bptype bptype,bool temp,const char * cond_string_)8124 breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
8125 bool temp, const char *cond_string_)
8126 : type (bptype),
8127 disposition (temp ? disp_del : disp_donttouch),
8128 gdbarch (gdbarch_),
8129 language (current_language->la_language),
8130 input_radix (::input_radix),
8131 cond_string (cond_string_ != nullptr
8132 ? make_unique_xstrdup (cond_string_)
8133 : nullptr),
8134 related_breakpoint (this)
8135 {
8136 }
8137
8138 /* See breakpoint.h. */
8139
catchpoint(struct gdbarch * gdbarch,bool temp,const char * cond_string)8140 catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
8141 const char *cond_string)
8142 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
8143 {
8144 add_dummy_location (this, current_program_space);
8145
8146 pspace = current_program_space;
8147 }
8148
8149 /* Notify interpreters and observers that breakpoint B was created. */
8150
8151 static void
notify_breakpoint_created(breakpoint * b)8152 notify_breakpoint_created (breakpoint *b)
8153 {
8154 interps_notify_breakpoint_created (b);
8155 gdb::observers::breakpoint_created.notify (b);
8156 }
8157
8158 breakpoint *
install_breakpoint(int internal,std::unique_ptr<breakpoint> && arg,int update_gll)8159 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8160 {
8161 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8162 set_breakpoint_number (internal, b);
8163 if (is_tracepoint (b))
8164 set_tracepoint_count (breakpoint_count);
8165 if (!internal)
8166 mention (b);
8167
8168 notify_breakpoint_created (b);
8169
8170 if (update_gll)
8171 update_global_location_list (UGLL_MAY_INSERT);
8172
8173 return b;
8174 }
8175
8176 static int
hw_breakpoint_used_count(void)8177 hw_breakpoint_used_count (void)
8178 {
8179 int i = 0;
8180
8181 for (breakpoint &b : all_breakpoints ())
8182 if (b.type == bp_hardware_breakpoint && breakpoint_enabled (&b))
8183 for (bp_location &bl : b.locations ())
8184 {
8185 /* Special types of hardware breakpoints may use more than
8186 one register. */
8187 i += b.resources_needed (&bl);
8188 }
8189
8190 return i;
8191 }
8192
8193 /* Returns the resources B would use if it were a hardware
8194 watchpoint. */
8195
8196 static int
hw_watchpoint_use_count(struct breakpoint * b)8197 hw_watchpoint_use_count (struct breakpoint *b)
8198 {
8199 int i = 0;
8200
8201 if (!breakpoint_enabled (b))
8202 return 0;
8203
8204 for (bp_location &bl : b->locations ())
8205 {
8206 /* Special types of hardware watchpoints may use more than
8207 one register. */
8208 i += b->resources_needed (&bl);
8209 }
8210
8211 return i;
8212 }
8213
8214 /* Returns the sum the used resources of all hardware watchpoints of
8215 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8216 the sum of the used resources of all hardware watchpoints of other
8217 types _not_ TYPE. */
8218
8219 static int
hw_watchpoint_used_count_others(struct breakpoint * except,enum bptype type,int * other_type_used)8220 hw_watchpoint_used_count_others (struct breakpoint *except,
8221 enum bptype type, int *other_type_used)
8222 {
8223 int i = 0;
8224
8225 *other_type_used = 0;
8226 for (breakpoint &b : all_breakpoints ())
8227 {
8228 if (&b == except)
8229 continue;
8230 if (!breakpoint_enabled (&b))
8231 continue;
8232
8233 if (b.type == type)
8234 i += hw_watchpoint_use_count (&b);
8235 else if (is_hardware_watchpoint (&b))
8236 *other_type_used = 1;
8237 }
8238
8239 return i;
8240 }
8241
8242 void
disable_watchpoints_before_interactive_call_start(void)8243 disable_watchpoints_before_interactive_call_start (void)
8244 {
8245 for (breakpoint &b : all_breakpoints ())
8246 if (is_watchpoint (&b) && breakpoint_enabled (&b))
8247 {
8248 b.enable_state = bp_call_disabled;
8249 update_global_location_list (UGLL_DONT_INSERT);
8250 }
8251 }
8252
8253 void
enable_watchpoints_after_interactive_call_stop(void)8254 enable_watchpoints_after_interactive_call_stop (void)
8255 {
8256 for (breakpoint &b : all_breakpoints ())
8257 if (is_watchpoint (&b) && b.enable_state == bp_call_disabled)
8258 {
8259 b.enable_state = bp_enabled;
8260 update_global_location_list (UGLL_MAY_INSERT);
8261 }
8262 }
8263
8264 void
disable_breakpoints_before_startup(void)8265 disable_breakpoints_before_startup (void)
8266 {
8267 current_program_space->executing_startup = 1;
8268 update_global_location_list (UGLL_DONT_INSERT);
8269 }
8270
8271 void
enable_breakpoints_after_startup(void)8272 enable_breakpoints_after_startup (void)
8273 {
8274 current_program_space->executing_startup = 0;
8275 breakpoint_re_set ();
8276 }
8277
8278 /* Allocate a new momentary breakpoint. */
8279
8280 template<typename... Arg>
8281 static momentary_breakpoint *
new_momentary_breakpoint(struct gdbarch * gdbarch,enum bptype type,Arg &&...args)8282 new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
8283 Arg&&... args)
8284 {
8285 if (type == bp_longjmp || type == bp_exception)
8286 return new longjmp_breakpoint (gdbarch, type,
8287 std::forward<Arg> (args)...);
8288 else
8289 return new momentary_breakpoint (gdbarch, type,
8290 std::forward<Arg> (args)...);
8291 }
8292
8293 /* Set a momentary breakpoint of type TYPE at address specified by
8294 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8295 frame. */
8296
8297 breakpoint_up
set_momentary_breakpoint(struct gdbarch * gdbarch,struct symtab_and_line sal,struct frame_id frame_id,enum bptype type)8298 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8299 struct frame_id frame_id, enum bptype type)
8300 {
8301 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8302 tail-called one. */
8303 gdb_assert (!frame_id_artificial_p (frame_id));
8304
8305 std::unique_ptr<momentary_breakpoint> b
8306 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
8307 inferior_thread ()->global_num));
8308
8309 b->add_location (sal);
8310
8311 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
8312
8313 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8314
8315 return bp;
8316 }
8317
8318 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8319 The new breakpoint will have type TYPE, use OPS as its
8320 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8321
8322 static struct breakpoint *
momentary_breakpoint_from_master(struct breakpoint * orig,enum bptype type,int loc_enabled,int thread)8323 momentary_breakpoint_from_master (struct breakpoint *orig,
8324 enum bptype type,
8325 int loc_enabled,
8326 int thread)
8327 {
8328 std::unique_ptr<breakpoint> copy
8329 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
8330 orig->frame_id, thread));
8331 const bp_location &orig_loc = orig->first_loc ();
8332 bp_location *copy_loc = copy->allocate_location ();
8333 copy->add_location (*copy_loc);
8334 set_breakpoint_location_function (copy_loc);
8335
8336 copy_loc->gdbarch = orig_loc.gdbarch;
8337 copy_loc->requested_address = orig_loc.requested_address;
8338 copy_loc->address = orig_loc.address;
8339 copy_loc->section = orig_loc.section;
8340 copy_loc->pspace = orig_loc.pspace;
8341 copy_loc->probe = orig_loc.probe;
8342 copy_loc->line_number = orig_loc.line_number;
8343 copy_loc->symtab = orig_loc.symtab;
8344 copy_loc->enabled = loc_enabled;
8345
8346 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
8347 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8348 return b;
8349 }
8350
8351 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8352 ORIG is NULL. */
8353
8354 struct breakpoint *
clone_momentary_breakpoint(struct breakpoint * orig)8355 clone_momentary_breakpoint (struct breakpoint *orig)
8356 {
8357 /* If there's nothing to clone, then return nothing. */
8358 if (orig == NULL)
8359 return NULL;
8360
8361 return momentary_breakpoint_from_master (orig, orig->type, 0,
8362 orig->thread);
8363 }
8364
8365 breakpoint_up
set_momentary_breakpoint_at_pc(struct gdbarch * gdbarch,CORE_ADDR pc,enum bptype type)8366 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8367 enum bptype type)
8368 {
8369 struct symtab_and_line sal;
8370
8371 sal = find_pc_line (pc, 0);
8372 sal.pc = pc;
8373 sal.section = find_pc_overlay (pc);
8374 sal.explicit_pc = 1;
8375
8376 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8377 }
8378
8379
8380 /* Tell the user we have just set a breakpoint B. */
8381
8382 static void
mention(const breakpoint * b)8383 mention (const breakpoint *b)
8384 {
8385 b->print_mention ();
8386 current_uiout->text ("\n");
8387 }
8388
8389
8390 static bool bp_loc_is_permanent (struct bp_location *loc);
8391
8392 /* Handle "set breakpoint auto-hw on".
8393
8394 If the explicitly specified breakpoint type is not hardware
8395 breakpoint, check the memory map to see whether the breakpoint
8396 address is in read-only memory.
8397
8398 - location type is not hardware breakpoint, memory is read-only.
8399 We change the type of the location to hardware breakpoint.
8400
8401 - location type is hardware breakpoint, memory is read-write. This
8402 means we've previously made the location hardware one, but then the
8403 memory map changed, so we undo.
8404 */
8405
8406 static void
handle_automatic_hardware_breakpoints(bp_location * bl)8407 handle_automatic_hardware_breakpoints (bp_location *bl)
8408 {
8409 if (automatic_hardware_breakpoints
8410 && bl->owner->type != bp_hardware_breakpoint
8411 && (bl->loc_type == bp_loc_software_breakpoint
8412 || bl->loc_type == bp_loc_hardware_breakpoint))
8413 {
8414 /* When breakpoints are removed, remove_breakpoints will use
8415 location types we've just set here, the only possible problem
8416 is that memory map has changed during running program, but
8417 it's not going to work anyway with current gdb. */
8418 mem_region *mr = lookup_mem_region (bl->address);
8419
8420 if (mr != nullptr)
8421 {
8422 enum bp_loc_type new_type;
8423
8424 if (mr->attrib.mode != MEM_RW)
8425 new_type = bp_loc_hardware_breakpoint;
8426 else
8427 new_type = bp_loc_software_breakpoint;
8428
8429 if (new_type != bl->loc_type)
8430 {
8431 static bool said = false;
8432
8433 bl->loc_type = new_type;
8434 if (!said)
8435 {
8436 gdb_printf (_("Note: automatically using "
8437 "hardware breakpoints for "
8438 "read-only addresses.\n"));
8439 said = true;
8440 }
8441 }
8442 }
8443 }
8444 }
8445
8446 bp_location *
add_location(const symtab_and_line & sal)8447 code_breakpoint::add_location (const symtab_and_line &sal)
8448 {
8449 CORE_ADDR adjusted_address;
8450 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8451
8452 if (loc_gdbarch == NULL)
8453 loc_gdbarch = gdbarch;
8454
8455 /* Adjust the breakpoint's address prior to allocating a location.
8456 Once we call allocate_location(), that mostly uninitialized
8457 location will be placed on the location chain. Adjustment of the
8458 breakpoint may cause target_read_memory() to be called and we do
8459 not want its scan of the location chain to find a breakpoint and
8460 location that's only been partially initialized. */
8461 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8462 sal.pc, type,
8463 sal.pspace);
8464
8465 /* Sort the locations by their ADDRESS. */
8466 bp_location *new_loc = this->allocate_location ();
8467
8468 new_loc->requested_address = sal.pc;
8469 new_loc->address = adjusted_address;
8470 new_loc->pspace = sal.pspace;
8471 new_loc->probe.prob = sal.prob;
8472 new_loc->probe.objfile = sal.objfile;
8473 gdb_assert (new_loc->pspace != NULL);
8474 new_loc->section = sal.section;
8475 new_loc->gdbarch = loc_gdbarch;
8476 new_loc->line_number = sal.line;
8477 new_loc->symtab = sal.symtab;
8478 new_loc->symbol = sal.symbol;
8479 new_loc->msymbol = sal.msymbol;
8480 new_loc->objfile = sal.objfile;
8481
8482 breakpoint::add_location (*new_loc);
8483
8484 set_breakpoint_location_function (new_loc);
8485
8486 /* While by definition, permanent breakpoints are already present in the
8487 code, we don't mark the location as inserted. Normally one would expect
8488 that GDB could rely on that breakpoint instruction to stop the program,
8489 thus removing the need to insert its own breakpoint, except that executing
8490 the breakpoint instruction can kill the target instead of reporting a
8491 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8492 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8493 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8494 breakpoint be inserted normally results in QEMU knowing about the GDB
8495 breakpoint, and thus trap before the breakpoint instruction is executed.
8496 (If GDB later needs to continue execution past the permanent breakpoint,
8497 it manually increments the PC, thus avoiding executing the breakpoint
8498 instruction.) */
8499 if (bp_loc_is_permanent (new_loc))
8500 new_loc->permanent = 1;
8501
8502 return new_loc;
8503 }
8504
8505
8506 /* Return true if LOC is pointing to a permanent breakpoint,
8507 return false otherwise. */
8508
8509 static bool
bp_loc_is_permanent(struct bp_location * loc)8510 bp_loc_is_permanent (struct bp_location *loc)
8511 {
8512 gdb_assert (loc != NULL);
8513
8514 /* If we have a non-breakpoint-backed catchpoint or a software
8515 watchpoint, just return 0. We should not attempt to read from
8516 the addresses the locations of these breakpoint types point to.
8517 gdbarch_program_breakpoint_here_p, below, will attempt to read
8518 memory. */
8519 if (!bl_address_is_meaningful (loc))
8520 return false;
8521
8522 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8523 switch_to_program_space_and_thread (loc->pspace);
8524 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8525 }
8526
8527 /* Build a command list for the dprintf corresponding to the current
8528 settings of the dprintf style options. */
8529
8530 static void
update_dprintf_command_list(struct breakpoint * b)8531 update_dprintf_command_list (struct breakpoint *b)
8532 {
8533 gdb_assert (b->type == bp_dprintf);
8534 gdb_assert (b->extra_string != nullptr);
8535
8536 const char *dprintf_args = b->extra_string.get ();
8537 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8538
8539 /* Trying to create a dprintf breakpoint without a format and args
8540 string should be detected at creation time. */
8541 gdb_assert (dprintf_args != nullptr);
8542
8543 dprintf_args = skip_spaces (dprintf_args);
8544
8545 /* Allow a comma, as it may have terminated a location, but don't
8546 insist on it. */
8547 if (*dprintf_args == ',')
8548 ++dprintf_args;
8549 dprintf_args = skip_spaces (dprintf_args);
8550
8551 if (*dprintf_args != '"')
8552 error (_("Bad format string, missing '\"'."));
8553
8554 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8555 printf_line = xstrprintf ("printf %s", dprintf_args);
8556 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8557 {
8558 if (dprintf_function.empty ())
8559 error (_("No function supplied for dprintf call"));
8560
8561 if (!dprintf_channel.empty ())
8562 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8563 dprintf_function.c_str (),
8564 dprintf_channel.c_str (),
8565 dprintf_args);
8566 else
8567 printf_line = xstrprintf ("call (void) %s (%s)",
8568 dprintf_function.c_str (),
8569 dprintf_args);
8570 }
8571 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8572 {
8573 if (target_can_run_breakpoint_commands ())
8574 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8575 else
8576 {
8577 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8578 printf_line = xstrprintf ("printf %s", dprintf_args);
8579 }
8580 }
8581 else
8582 internal_error (_("Invalid dprintf style."));
8583
8584 gdb_assert (printf_line != NULL);
8585
8586 /* Manufacture a printf sequence. */
8587 struct command_line *printf_cmd_line
8588 = new struct command_line (simple_control, printf_line.release ());
8589 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8590 command_lines_deleter ()));
8591 }
8592
8593 /* Update all dprintf commands, making their command lists reflect
8594 current style settings. */
8595
8596 static void
update_dprintf_commands(const char * args,int from_tty,struct cmd_list_element * c)8597 update_dprintf_commands (const char *args, int from_tty,
8598 struct cmd_list_element *c)
8599 {
8600 for (breakpoint &b : all_breakpoints ())
8601 if (b.type == bp_dprintf)
8602 update_dprintf_command_list (&b);
8603 }
8604
code_breakpoint(struct gdbarch * gdbarch_,enum bptype type_,gdb::array_view<const symtab_and_line> sals,location_spec_up && locspec_,gdb::unique_xmalloc_ptr<char> filter_,gdb::unique_xmalloc_ptr<char> cond_string_,gdb::unique_xmalloc_ptr<char> extra_string_,enum bpdisp disposition_,int thread_,int task_,int inferior_,int ignore_count_,int from_tty,int enabled_,unsigned flags,int display_canonical_)8605 code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
8606 enum bptype type_,
8607 gdb::array_view<const symtab_and_line> sals,
8608 location_spec_up &&locspec_,
8609 gdb::unique_xmalloc_ptr<char> filter_,
8610 gdb::unique_xmalloc_ptr<char> cond_string_,
8611 gdb::unique_xmalloc_ptr<char> extra_string_,
8612 enum bpdisp disposition_,
8613 int thread_, int task_, int inferior_,
8614 int ignore_count_,
8615 int from_tty,
8616 int enabled_, unsigned flags,
8617 int display_canonical_)
8618 : breakpoint (gdbarch_, type_)
8619 {
8620 int i;
8621
8622 if (type == bp_hardware_breakpoint)
8623 {
8624 int target_resources_ok;
8625
8626 i = hw_breakpoint_used_count ();
8627 target_resources_ok =
8628 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8629 i + 1, 0);
8630 if (target_resources_ok == 0)
8631 error (_("No hardware breakpoint support in the target."));
8632 else if (target_resources_ok < 0)
8633 error (_("Hardware breakpoints used exceeds limit."));
8634 }
8635
8636 gdb_assert (!sals.empty ());
8637
8638 /* At most one of thread, task, or inferior can be set on any breakpoint. */
8639 gdb_assert (((thread == -1 ? 0 : 1)
8640 + (task == -1 ? 0 : 1)
8641 + (inferior == -1 ? 0 : 1)) <= 1);
8642
8643 thread = thread_;
8644 task = task_;
8645 inferior = inferior_;
8646
8647 cond_string = std::move (cond_string_);
8648 extra_string = std::move (extra_string_);
8649 ignore_count = ignore_count_;
8650 enable_state = enabled_ ? bp_enabled : bp_disabled;
8651 disposition = disposition_;
8652
8653 if (type == bp_static_tracepoint
8654 || type == bp_static_marker_tracepoint)
8655 {
8656 auto *t = gdb::checked_static_cast<tracepoint *> (this);
8657 struct static_tracepoint_marker marker;
8658
8659 if (strace_marker_p (this))
8660 {
8661 /* We already know the marker exists, otherwise, we wouldn't
8662 see a sal for it. */
8663 const char *p = &locspec_->to_string ()[3];
8664 const char *endp;
8665
8666 p = skip_spaces (p);
8667
8668 endp = skip_to_space (p);
8669
8670 t->static_trace_marker_id.assign (p, endp - p);
8671
8672 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8673 t->static_trace_marker_id.c_str ());
8674 }
8675 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8676 {
8677 t->static_trace_marker_id = std::move (marker.str_id);
8678
8679 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8680 t->static_trace_marker_id.c_str ());
8681 }
8682 else
8683 warning (_("Couldn't determine the static tracepoint marker to probe"));
8684 }
8685
8686 for (const auto &sal : sals)
8687 {
8688 if (from_tty)
8689 {
8690 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8691 if (loc_gdbarch == nullptr)
8692 loc_gdbarch = gdbarch;
8693
8694 describe_other_breakpoints (loc_gdbarch,
8695 sal.pspace, sal.pc, sal.section, thread);
8696 }
8697
8698 bp_location *new_loc = add_location (sal);
8699 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8700 new_loc->inserted = 1;
8701
8702 /* Do not set breakpoint locations conditions yet. As locations
8703 are inserted, they get sorted based on their addresses. Let
8704 the list stabilize to have reliable location numbers. */
8705 }
8706
8707 /* Dynamic printf requires and uses additional arguments on the
8708 command line, otherwise it's an error. */
8709 if (type == bp_dprintf)
8710 update_dprintf_command_list (this);
8711 else if (extra_string != nullptr)
8712 error (_("Garbage '%s' at end of command"), extra_string.get ());
8713
8714 /* The order of the locations is now stable. Set the location
8715 condition using the location's number. */
8716 int loc_num = 1;
8717 for (bp_location &bl : locations ())
8718 {
8719 if (cond_string != nullptr)
8720 set_breakpoint_location_condition (cond_string.get (), &bl,
8721 number, loc_num);
8722
8723 ++loc_num;
8724 }
8725
8726 display_canonical = display_canonical_;
8727 if (locspec_ != nullptr)
8728 locspec = std::move (locspec_);
8729 else
8730 locspec = new_address_location_spec (this->first_loc ().address, NULL, 0);
8731 filter = std::move (filter_);
8732 }
8733
8734 static void
create_breakpoint_sal(struct gdbarch * gdbarch,gdb::array_view<const symtab_and_line> sals,location_spec_up && locspec,gdb::unique_xmalloc_ptr<char> filter,gdb::unique_xmalloc_ptr<char> cond_string,gdb::unique_xmalloc_ptr<char> extra_string,enum bptype type,enum bpdisp disposition,int thread,int task,int inferior,int ignore_count,int from_tty,int enabled,int internal,unsigned flags,int display_canonical)8735 create_breakpoint_sal (struct gdbarch *gdbarch,
8736 gdb::array_view<const symtab_and_line> sals,
8737 location_spec_up &&locspec,
8738 gdb::unique_xmalloc_ptr<char> filter,
8739 gdb::unique_xmalloc_ptr<char> cond_string,
8740 gdb::unique_xmalloc_ptr<char> extra_string,
8741 enum bptype type, enum bpdisp disposition,
8742 int thread, int task, int inferior, int ignore_count,
8743 int from_tty,
8744 int enabled, int internal, unsigned flags,
8745 int display_canonical)
8746 {
8747 std::unique_ptr<code_breakpoint> b
8748 = new_breakpoint_from_type (gdbarch,
8749 type,
8750 sals,
8751 std::move (locspec),
8752 std::move (filter),
8753 std::move (cond_string),
8754 std::move (extra_string),
8755 disposition,
8756 thread, task, inferior, ignore_count,
8757 from_tty,
8758 enabled, flags,
8759 display_canonical);
8760
8761 install_breakpoint (internal, std::move (b), 0);
8762 }
8763
8764 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8765 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8766 value. COND_STRING, if not NULL, specified the condition to be
8767 used for all breakpoints. Essentially the only case where
8768 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8769 function. In that case, it's still not possible to specify
8770 separate conditions for different overloaded functions, so
8771 we take just a single condition string.
8772
8773 NOTE: If the function succeeds, the caller is expected to cleanup
8774 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8775 array contents). If the function fails (error() is called), the
8776 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8777 COND and SALS arrays and each of those arrays contents. */
8778
8779 static void
create_breakpoints_sal(struct gdbarch * gdbarch,struct linespec_result * canonical,gdb::unique_xmalloc_ptr<char> cond_string,gdb::unique_xmalloc_ptr<char> extra_string,enum bptype type,enum bpdisp disposition,int thread,int task,int inferior,int ignore_count,int from_tty,int enabled,int internal,unsigned flags)8780 create_breakpoints_sal (struct gdbarch *gdbarch,
8781 struct linespec_result *canonical,
8782 gdb::unique_xmalloc_ptr<char> cond_string,
8783 gdb::unique_xmalloc_ptr<char> extra_string,
8784 enum bptype type, enum bpdisp disposition,
8785 int thread, int task, int inferior,
8786 int ignore_count,
8787 int from_tty,
8788 int enabled, int internal, unsigned flags)
8789 {
8790 if (canonical->pre_expanded)
8791 gdb_assert (canonical->lsals.size () == 1);
8792
8793 for (const auto &lsal : canonical->lsals)
8794 {
8795 /* Note that 'location' can be NULL in the case of a plain
8796 'break', without arguments. */
8797 location_spec_up locspec
8798 = (canonical->locspec != nullptr
8799 ? canonical->locspec->clone ()
8800 : nullptr);
8801 gdb::unique_xmalloc_ptr<char> filter_string
8802 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8803
8804 create_breakpoint_sal (gdbarch, lsal.sals,
8805 std::move (locspec),
8806 std::move (filter_string),
8807 std::move (cond_string),
8808 std::move (extra_string),
8809 type, disposition,
8810 thread, task, inferior, ignore_count,
8811 from_tty, enabled, internal, flags,
8812 canonical->special_display);
8813 }
8814 }
8815
8816 /* Parse LOCSPEC which is assumed to be a SAL specification possibly
8817 followed by conditionals. On return, SALS contains an array of SAL
8818 addresses found. LOCSPEC points to the end of the SAL (for
8819 linespec locspecs).
8820
8821 The array and the line spec strings are allocated on the heap, it is
8822 the caller's responsibility to free them. */
8823
8824 static void
parse_breakpoint_sals(location_spec * locspec,struct linespec_result * canonical)8825 parse_breakpoint_sals (location_spec *locspec,
8826 struct linespec_result *canonical)
8827 {
8828 struct symtab_and_line cursal;
8829
8830 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8831 {
8832 const char *spec
8833 = as_linespec_location_spec (locspec)->spec_string.get ();
8834
8835 if (spec == NULL)
8836 {
8837 /* The last displayed codepoint, if it's valid, is our default
8838 breakpoint address. */
8839 if (last_displayed_sal_is_valid ())
8840 {
8841 /* Set sal's pspace, pc, symtab, and line to the values
8842 corresponding to the last call to print_frame_info.
8843 Be sure to reinitialize LINE with NOTCURRENT == 0
8844 as the breakpoint line number is inappropriate otherwise.
8845 find_pc_line would adjust PC, re-set it back. */
8846 symtab_and_line sal = get_last_displayed_sal ();
8847 CORE_ADDR pc = sal.pc;
8848
8849 sal = find_pc_line (pc, 0);
8850
8851 /* "break" without arguments is equivalent to "break *PC"
8852 where PC is the last displayed codepoint's address. So
8853 make sure to set sal.explicit_pc to prevent GDB from
8854 trying to expand the list of sals to include all other
8855 instances with the same symtab and line. */
8856 sal.pc = pc;
8857 sal.explicit_pc = 1;
8858
8859 struct linespec_sals lsal;
8860 lsal.sals = {sal};
8861 lsal.canonical = NULL;
8862
8863 canonical->lsals.push_back (std::move (lsal));
8864 return;
8865 }
8866 else
8867 error (_("No default breakpoint address now."));
8868 }
8869 }
8870
8871 /* Force almost all breakpoints to be in terms of the
8872 current_source_symtab (which is decode_line_1's default).
8873 This should produce the results we want almost all of the
8874 time while leaving default_breakpoint_* alone.
8875
8876 ObjC: However, don't match an Objective-C method name which
8877 may have a '+' or '-' succeeded by a '['. */
8878 cursal = get_current_source_symtab_and_line ();
8879 if (last_displayed_sal_is_valid ())
8880 {
8881 const char *spec = NULL;
8882
8883 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8884 spec = as_linespec_location_spec (locspec)->spec_string.get ();
8885
8886 if (!cursal.symtab
8887 || (spec != NULL
8888 && strchr ("+-", spec[0]) != NULL
8889 && spec[1] != '['))
8890 {
8891 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
8892 get_last_displayed_symtab (),
8893 get_last_displayed_line (),
8894 canonical, NULL, NULL);
8895 return;
8896 }
8897 }
8898
8899 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
8900 cursal.symtab, cursal.line, canonical, NULL, NULL);
8901 }
8902
8903
8904 /* Convert each SAL into a real PC. Verify that the PC can be
8905 inserted as a breakpoint. If it can't throw an error. */
8906
8907 static void
breakpoint_sals_to_pc(std::vector<symtab_and_line> & sals)8908 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
8909 {
8910 for (auto &sal : sals)
8911 resolve_sal_pc (&sal);
8912 }
8913
8914 /* Fast tracepoints may have restrictions on valid locations. For
8915 instance, a fast tracepoint using a jump instead of a trap will
8916 likely have to overwrite more bytes than a trap would, and so can
8917 only be placed where the instruction is longer than the jump, or a
8918 multi-instruction sequence does not have a jump into the middle of
8919 it, etc. */
8920
8921 static void
check_fast_tracepoint_sals(struct gdbarch * gdbarch,gdb::array_view<const symtab_and_line> sals)8922 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8923 gdb::array_view<const symtab_and_line> sals)
8924 {
8925 for (const auto &sal : sals)
8926 {
8927 struct gdbarch *sarch;
8928
8929 sarch = get_sal_arch (sal);
8930 /* We fall back to GDBARCH if there is no architecture
8931 associated with SAL. */
8932 if (sarch == NULL)
8933 sarch = gdbarch;
8934 std::string msg;
8935 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
8936 error (_("May not have a fast tracepoint at %s%s"),
8937 paddress (sarch, sal.pc), msg.c_str ());
8938 }
8939 }
8940
8941 /* Given TOK, a string specification of condition and thread, as accepted
8942 by the 'break' command, extract the condition string into *COND_STRING.
8943 If no condition string is found then *COND_STRING is set to nullptr.
8944
8945 If the breakpoint specification has an associated thread, task, or
8946 inferior, these are extracted into *THREAD, *TASK, and *INFERIOR
8947 respectively, otherwise these arguments are set to -1 (for THREAD and
8948 INFERIOR) or 0 (for TASK).
8949
8950 PC identifies the context at which the condition should be parsed. */
8951
8952 static void
find_condition_and_thread(const char * tok,CORE_ADDR pc,gdb::unique_xmalloc_ptr<char> * cond_string,int * thread,int * inferior,int * task,gdb::unique_xmalloc_ptr<char> * rest)8953 find_condition_and_thread (const char *tok, CORE_ADDR pc,
8954 gdb::unique_xmalloc_ptr<char> *cond_string,
8955 int *thread, int *inferior, int *task,
8956 gdb::unique_xmalloc_ptr<char> *rest)
8957 {
8958 cond_string->reset ();
8959 *thread = -1;
8960 *inferior = -1;
8961 *task = -1;
8962 rest->reset ();
8963 bool force = false;
8964
8965 while (tok && *tok)
8966 {
8967 const char *end_tok;
8968 int toklen;
8969 const char *cond_start = NULL;
8970 const char *cond_end = NULL;
8971
8972 tok = skip_spaces (tok);
8973
8974 if ((*tok == '"' || *tok == ',') && rest)
8975 {
8976 rest->reset (savestring (tok, strlen (tok)));
8977 break;
8978 }
8979
8980 end_tok = skip_to_space (tok);
8981
8982 toklen = end_tok - tok;
8983
8984 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8985 {
8986 tok = cond_start = end_tok + 1;
8987 try
8988 {
8989 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8990 }
8991 catch (const gdb_exception_error &)
8992 {
8993 if (!force)
8994 throw;
8995 else
8996 tok = tok + strlen (tok);
8997 }
8998 cond_end = tok;
8999 cond_string->reset (savestring (cond_start, cond_end - cond_start));
9000 }
9001 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9002 {
9003 tok = tok + toklen;
9004 force = true;
9005 }
9006 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9007 {
9008 const char *tmptok;
9009 struct thread_info *thr;
9010
9011 if (*thread != -1)
9012 error(_("You can specify only one thread."));
9013
9014 if (*task != -1)
9015 error (_("You can specify only one of thread or task."));
9016
9017 if (*inferior != -1)
9018 error (_("You can specify only one of inferior or thread."));
9019
9020 tok = end_tok + 1;
9021 thr = parse_thread_id (tok, &tmptok);
9022 if (tok == tmptok)
9023 error (_("Junk after thread keyword."));
9024 *thread = thr->global_num;
9025 tok = tmptok;
9026 }
9027 else if (toklen >= 1 && strncmp (tok, "inferior", toklen) == 0)
9028 {
9029 if (*inferior != -1)
9030 error(_("You can specify only one inferior."));
9031
9032 if (*task != -1)
9033 error (_("You can specify only one of inferior or task."));
9034
9035 if (*thread != -1)
9036 error (_("You can specify only one of inferior or thread."));
9037
9038 char *tmptok;
9039 tok = end_tok + 1;
9040 *inferior = strtol (tok, &tmptok, 0);
9041 if (tok == tmptok)
9042 error (_("Junk after inferior keyword."));
9043 if (!valid_global_inferior_id (*inferior))
9044 error (_("Unknown inferior number %d."), *inferior);
9045 tok = tmptok;
9046 }
9047 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9048 {
9049 char *tmptok;
9050
9051 if (*task != -1)
9052 error(_("You can specify only one task."));
9053
9054 if (*thread != -1)
9055 error (_("You can specify only one of thread or task."));
9056
9057 if (*inferior != -1)
9058 error (_("You can specify only one of inferior or task."));
9059
9060 tok = end_tok + 1;
9061 *task = strtol (tok, &tmptok, 0);
9062 if (tok == tmptok)
9063 error (_("Junk after task keyword."));
9064 if (!valid_task_id (*task))
9065 error (_("Unknown task %d."), *task);
9066 tok = tmptok;
9067 }
9068 else if (rest)
9069 {
9070 rest->reset (savestring (tok, strlen (tok)));
9071 break;
9072 }
9073 else
9074 error (_("Junk at end of arguments."));
9075 }
9076 }
9077
9078 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9079 succeeds. The parsed values are written to COND_STRING, THREAD,
9080 TASK, and REST. See the comment of 'find_condition_and_thread'
9081 for the description of these parameters and INPUT. */
9082
9083 static void
find_condition_and_thread_for_sals(const std::vector<symtab_and_line> & sals,const char * input,gdb::unique_xmalloc_ptr<char> * cond_string,int * thread,int * inferior,int * task,gdb::unique_xmalloc_ptr<char> * rest)9084 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9085 const char *input,
9086 gdb::unique_xmalloc_ptr<char> *cond_string,
9087 int *thread, int *inferior, int *task,
9088 gdb::unique_xmalloc_ptr<char> *rest)
9089 {
9090 int num_failures = 0;
9091 for (auto &sal : sals)
9092 {
9093 gdb::unique_xmalloc_ptr<char> cond;
9094 int thread_id = -1;
9095 int inferior_id = -1;
9096 int task_id = -1;
9097 gdb::unique_xmalloc_ptr<char> remaining;
9098
9099 /* Here we want to parse 'arg' to separate condition from thread
9100 number. But because parsing happens in a context and the
9101 contexts of sals might be different, try each until there is
9102 success. Finding one successful parse is sufficient for our
9103 goal. When setting the breakpoint we'll re-parse the
9104 condition in the context of each sal. */
9105 try
9106 {
9107 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9108 &inferior_id, &task_id, &remaining);
9109 *cond_string = std::move (cond);
9110 /* A value of -1 indicates that these fields are unset. At most
9111 one of these fields should be set (to a value other than -1)
9112 at this point. */
9113 gdb_assert (((thread_id == -1 ? 1 : 0)
9114 + (task_id == -1 ? 1 : 0)
9115 + (inferior_id == -1 ? 1 : 0)) >= 2);
9116 *thread = thread_id;
9117 *inferior = inferior_id;
9118 *task = task_id;
9119 *rest = std::move (remaining);
9120 break;
9121 }
9122 catch (const gdb_exception_error &e)
9123 {
9124 num_failures++;
9125 /* If no sal remains, do not continue. */
9126 if (num_failures == sals.size ())
9127 throw;
9128 }
9129 }
9130 }
9131
9132 /* Decode a static tracepoint marker spec. */
9133
9134 static std::vector<symtab_and_line>
decode_static_tracepoint_spec(const char ** arg_p)9135 decode_static_tracepoint_spec (const char **arg_p)
9136 {
9137 const char *p = &(*arg_p)[3];
9138 const char *endp;
9139
9140 p = skip_spaces (p);
9141
9142 endp = skip_to_space (p);
9143
9144 std::string marker_str (p, endp - p);
9145
9146 std::vector<static_tracepoint_marker> markers
9147 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9148 if (markers.empty ())
9149 error (_("No known static tracepoint marker named %s"),
9150 marker_str.c_str ());
9151
9152 std::vector<symtab_and_line> sals;
9153 sals.reserve (markers.size ());
9154
9155 for (const static_tracepoint_marker &marker : markers)
9156 {
9157 symtab_and_line sal = find_pc_line (marker.address, 0);
9158 sal.pc = marker.address;
9159 sals.push_back (sal);
9160 }
9161
9162 *arg_p = endp;
9163 return sals;
9164 }
9165
9166 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9167 according to IS_TRACEPOINT. */
9168
9169 static const struct breakpoint_ops *
breakpoint_ops_for_location_spec_type(enum location_spec_type locspec_type,bool is_tracepoint)9170 breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
9171 bool is_tracepoint)
9172 {
9173 if (is_tracepoint)
9174 {
9175 if (locspec_type == PROBE_LOCATION_SPEC)
9176 return &tracepoint_probe_breakpoint_ops;
9177 else
9178 return &code_breakpoint_ops;
9179 }
9180 else
9181 {
9182 if (locspec_type == PROBE_LOCATION_SPEC)
9183 return &bkpt_probe_breakpoint_ops;
9184 else
9185 return &code_breakpoint_ops;
9186 }
9187 }
9188
9189 /* See breakpoint.h. */
9190
9191 const struct breakpoint_ops *
breakpoint_ops_for_location_spec(const location_spec * locspec,bool is_tracepoint)9192 breakpoint_ops_for_location_spec (const location_spec *locspec,
9193 bool is_tracepoint)
9194 {
9195 if (locspec != nullptr)
9196 return (breakpoint_ops_for_location_spec_type
9197 (locspec->type (), is_tracepoint));
9198 return &code_breakpoint_ops;
9199 }
9200
9201 /* See breakpoint.h. */
9202
9203 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)9204 create_breakpoint (struct gdbarch *gdbarch,
9205 location_spec *locspec,
9206 const char *cond_string,
9207 int thread, int inferior,
9208 const char *extra_string,
9209 bool force_condition, int parse_extra,
9210 int tempflag, enum bptype type_wanted,
9211 int ignore_count,
9212 enum auto_boolean pending_break_support,
9213 const struct breakpoint_ops *ops,
9214 int from_tty, int enabled, int internal,
9215 unsigned flags)
9216 {
9217 struct linespec_result canonical;
9218 bool pending = false;
9219 int task = -1;
9220 int prev_bkpt_count = breakpoint_count;
9221
9222 gdb_assert (thread == -1 || thread > 0);
9223 gdb_assert (inferior == -1 || inferior > 0);
9224 gdb_assert (thread == -1 || inferior == -1);
9225
9226 /* If PARSE_EXTRA is true then the thread and inferior details will be
9227 parsed from the EXTRA_STRING, the THREAD and INFERIOR arguments
9228 should be -1. */
9229 gdb_assert (!parse_extra || thread == -1);
9230 gdb_assert (!parse_extra || inferior == -1);
9231
9232 gdb_assert (ops != NULL);
9233
9234 /* If extra_string isn't useful, set it to NULL. */
9235 if (extra_string != NULL && *extra_string == '\0')
9236 extra_string = NULL;
9237
9238 /* A bp_dprintf must always have an accompanying EXTRA_STRING containing
9239 the dprintf format and arguments -- PARSE_EXTRA should always be false
9240 in this case.
9241
9242 For all other breakpoint types, EXTRA_STRING should be nullptr unless
9243 PARSE_EXTRA is true. */
9244 gdb_assert ((type_wanted == bp_dprintf)
9245 ? (extra_string != nullptr && !parse_extra)
9246 : (extra_string == nullptr || parse_extra));
9247
9248 try
9249 {
9250 ops->create_sals_from_location_spec (locspec, &canonical);
9251 }
9252 catch (const gdb_exception_error &e)
9253 {
9254 /* If caller is interested in rc value from parse, set
9255 value. */
9256 if (e.error == NOT_FOUND_ERROR)
9257 {
9258 /* If pending breakpoint support is turned off, throw
9259 error. */
9260
9261 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9262 throw;
9263
9264 exception_print (gdb_stderr, e);
9265
9266 /* If pending breakpoint support is auto query and the user
9267 selects no, then simply return the error code. */
9268 if (pending_break_support == AUTO_BOOLEAN_AUTO
9269 && !nquery (_("Make %s pending on future shared library load? "),
9270 bptype_string (type_wanted)))
9271 return 0;
9272
9273 /* At this point, either the user was queried about setting
9274 a pending breakpoint and selected yes, or pending
9275 breakpoint behavior is on and thus a pending breakpoint
9276 is defaulted on behalf of the user. */
9277 pending = true;
9278 }
9279 else
9280 throw;
9281 }
9282
9283 if (!pending && canonical.lsals.empty ())
9284 return 0;
9285
9286 /* Resolve all line numbers to PC's and verify that the addresses
9287 are ok for the target. */
9288 if (!pending)
9289 {
9290 for (auto &lsal : canonical.lsals)
9291 breakpoint_sals_to_pc (lsal.sals);
9292 }
9293
9294 /* Fast tracepoints may have additional restrictions on location. */
9295 if (!pending && type_wanted == bp_fast_tracepoint)
9296 {
9297 for (const auto &lsal : canonical.lsals)
9298 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9299 }
9300
9301 /* Verify that condition can be parsed, before setting any
9302 breakpoints. Allocate a separate condition expression for each
9303 breakpoint. */
9304 if (!pending)
9305 {
9306 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9307 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9308
9309 if (parse_extra)
9310 {
9311 gdb_assert (type_wanted != bp_dprintf);
9312
9313 gdb::unique_xmalloc_ptr<char> rest;
9314 gdb::unique_xmalloc_ptr<char> cond;
9315
9316 const linespec_sals &lsal = canonical.lsals[0];
9317
9318 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9319 &cond, &thread, &inferior,
9320 &task, &rest);
9321
9322 if (rest.get () != nullptr && *(rest.get ()) != '\0')
9323 error (_("Garbage '%s' at end of command"), rest.get ());
9324
9325 cond_string_copy = std::move (cond);
9326 extra_string_copy = std::move (rest);
9327 }
9328 else
9329 {
9330 /* Check the validity of the condition. We should error out
9331 if the condition is invalid at all of the locations and
9332 if it is not forced. In the PARSE_EXTRA case above, this
9333 check is done when parsing the EXTRA_STRING. */
9334 if (cond_string != nullptr && !force_condition)
9335 {
9336 int num_failures = 0;
9337 const linespec_sals &lsal = canonical.lsals[0];
9338 for (const auto &sal : lsal.sals)
9339 {
9340 const char *cond = cond_string;
9341 try
9342 {
9343 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9344 /* One success is sufficient to keep going. */
9345 break;
9346 }
9347 catch (const gdb_exception_error &)
9348 {
9349 num_failures++;
9350 /* If this is the last sal, error out. */
9351 if (num_failures == lsal.sals.size ())
9352 throw;
9353 }
9354 }
9355 }
9356
9357 /* Create a private copy of condition string. */
9358 if (cond_string)
9359 cond_string_copy.reset (xstrdup (cond_string));
9360 /* Create a private copy of any extra string. */
9361 if (extra_string)
9362 extra_string_copy.reset (xstrdup (extra_string));
9363 }
9364
9365 ops->create_breakpoints_sal (gdbarch, &canonical,
9366 std::move (cond_string_copy),
9367 std::move (extra_string_copy),
9368 type_wanted,
9369 tempflag ? disp_del : disp_donttouch,
9370 thread, task, inferior, ignore_count,
9371 from_tty, enabled, internal, flags);
9372 }
9373 else
9374 {
9375 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
9376 type_wanted);
9377 b->locspec = locspec->clone ();
9378
9379 if (parse_extra)
9380 b->cond_string = NULL;
9381 else
9382 {
9383 /* Create a private copy of condition string. */
9384 b->cond_string.reset (cond_string != NULL
9385 ? xstrdup (cond_string)
9386 : NULL);
9387 b->thread = thread;
9388 }
9389
9390 /* Create a private copy of any extra string. */
9391 b->extra_string.reset (extra_string != NULL
9392 ? xstrdup (extra_string)
9393 : NULL);
9394 b->ignore_count = ignore_count;
9395 b->disposition = tempflag ? disp_del : disp_donttouch;
9396 b->condition_not_parsed = 1;
9397 b->enable_state = enabled ? bp_enabled : bp_disabled;
9398 if ((type_wanted != bp_breakpoint
9399 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9400 b->pspace = current_program_space;
9401
9402 install_breakpoint (internal, std::move (b), 0);
9403 }
9404
9405 if (canonical.lsals.size () > 1)
9406 {
9407 warning (_("Multiple breakpoints were set.\nUse the "
9408 "\"delete\" command to delete unwanted breakpoints."));
9409 prev_breakpoint_count = prev_bkpt_count;
9410 }
9411
9412 update_global_location_list (UGLL_MAY_INSERT);
9413
9414 return 1;
9415 }
9416
9417 /* Set a breakpoint.
9418 ARG is a string describing breakpoint address,
9419 condition, and thread.
9420 FLAG specifies if a breakpoint is hardware on,
9421 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9422 and BP_TEMPFLAG. */
9423
9424 static void
break_command_1(const char * arg,int flag,int from_tty)9425 break_command_1 (const char *arg, int flag, int from_tty)
9426 {
9427 int tempflag = flag & BP_TEMPFLAG;
9428 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9429 ? bp_hardware_breakpoint
9430 : bp_breakpoint);
9431
9432 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9433 const struct breakpoint_ops *ops
9434 = breakpoint_ops_for_location_spec (locspec.get (),
9435 false /* is_tracepoint */);
9436
9437 create_breakpoint (get_current_arch (),
9438 locspec.get (),
9439 NULL,
9440 -1 /* thread */, -1 /* inferior */,
9441 arg, false, 1 /* parse arg */,
9442 tempflag, type_wanted,
9443 0 /* Ignore count */,
9444 pending_break_support,
9445 ops,
9446 from_tty,
9447 1 /* enabled */,
9448 0 /* internal */,
9449 0);
9450 }
9451
9452 /* Helper function for break_command_1 and disassemble_command. */
9453
9454 void
resolve_sal_pc(struct symtab_and_line * sal)9455 resolve_sal_pc (struct symtab_and_line *sal)
9456 {
9457 CORE_ADDR pc;
9458
9459 if (sal->pc == 0 && sal->symtab != NULL)
9460 {
9461 if (!find_line_pc (sal->symtab, sal->line, &pc))
9462 error (_("No line %d in file \"%s\"."),
9463 sal->line, symtab_to_filename_for_display (sal->symtab));
9464 sal->pc = pc;
9465
9466 /* If this SAL corresponds to a breakpoint inserted using a line
9467 number, then skip the function prologue if necessary. */
9468 if (sal->explicit_line)
9469 skip_prologue_sal (sal);
9470 }
9471
9472 if (sal->section == 0 && sal->symtab != NULL)
9473 {
9474 const struct blockvector *bv;
9475 const struct block *b;
9476 struct symbol *sym;
9477
9478 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9479 sal->symtab->compunit ());
9480 if (bv != NULL)
9481 {
9482 sym = b->linkage_function ();
9483 if (sym != NULL)
9484 sal->section
9485 = sym->obj_section (sal->symtab->compunit ()->objfile ());
9486 else
9487 {
9488 /* It really is worthwhile to have the section, so we'll
9489 just have to look harder. This case can be executed
9490 if we have line numbers but no functions (as can
9491 happen in assembly source). */
9492
9493 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9494 switch_to_program_space_and_thread (sal->pspace);
9495
9496 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9497 if (msym.minsym)
9498 sal->section = msym.obj_section ();
9499 }
9500 }
9501 }
9502 }
9503
9504 void
break_command(const char * arg,int from_tty)9505 break_command (const char *arg, int from_tty)
9506 {
9507 break_command_1 (arg, 0, from_tty);
9508 }
9509
9510 void
tbreak_command(const char * arg,int from_tty)9511 tbreak_command (const char *arg, int from_tty)
9512 {
9513 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9514 }
9515
9516 static void
hbreak_command(const char * arg,int from_tty)9517 hbreak_command (const char *arg, int from_tty)
9518 {
9519 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9520 }
9521
9522 static void
thbreak_command(const char * arg,int from_tty)9523 thbreak_command (const char *arg, int from_tty)
9524 {
9525 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9526 }
9527
9528 /* The dynamic printf command is mostly like a regular breakpoint, but
9529 with a prewired command list consisting of a single output command,
9530 built from extra arguments supplied on the dprintf command
9531 line. */
9532
9533 static void
dprintf_command(const char * arg,int from_tty)9534 dprintf_command (const char *arg, int from_tty)
9535 {
9536 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9537
9538 /* If non-NULL, ARG should have been advanced past the location;
9539 the next character must be ','. */
9540 if (arg == nullptr || arg[0] != ',' || arg[1] == '\0')
9541 error (_("Format string required"));
9542 else
9543 {
9544 /* Skip the comma. */
9545 ++arg;
9546 }
9547
9548 create_breakpoint (get_current_arch (),
9549 locspec.get (),
9550 NULL, -1, -1,
9551 arg, false, 0 /* parse arg */,
9552 0, bp_dprintf,
9553 0 /* Ignore count */,
9554 pending_break_support,
9555 &code_breakpoint_ops,
9556 from_tty,
9557 1 /* enabled */,
9558 0 /* internal */,
9559 0);
9560 }
9561
9562 static void
agent_printf_command(const char * arg,int from_tty)9563 agent_printf_command (const char *arg, int from_tty)
9564 {
9565 error (_("May only run agent-printf on the target"));
9566 }
9567
9568 /* Implement the "breakpoint_hit" method for ranged breakpoints. */
9569
9570 int
breakpoint_hit(const struct bp_location * bl,const address_space * aspace,CORE_ADDR bp_addr,const target_waitstatus & ws)9571 ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9572 const address_space *aspace,
9573 CORE_ADDR bp_addr,
9574 const target_waitstatus &ws)
9575 {
9576 if (ws.kind () != TARGET_WAITKIND_STOPPED
9577 || ws.sig () != GDB_SIGNAL_TRAP)
9578 return 0;
9579
9580 return breakpoint_address_match_range (bl->pspace->aspace.get (),
9581 bl->address, bl->length, aspace,
9582 bp_addr);
9583 }
9584
9585 /* Implement the "resources_needed" method for ranged breakpoints. */
9586
9587 int
resources_needed(const struct bp_location * bl)9588 ranged_breakpoint::resources_needed (const struct bp_location *bl)
9589 {
9590 return target_ranged_break_num_registers ();
9591 }
9592
9593 /* Implement the "print_it" method for ranged breakpoints. */
9594
9595 enum print_stop_action
print_it(const bpstat * bs)9596 ranged_breakpoint::print_it (const bpstat *bs) const
9597 {
9598 struct ui_out *uiout = current_uiout;
9599
9600 gdb_assert (type == bp_hardware_breakpoint);
9601
9602 /* Ranged breakpoints have only one location. */
9603 gdb_assert (this->has_single_location ());
9604
9605 annotate_breakpoint (number);
9606
9607 maybe_print_thread_hit_breakpoint (uiout);
9608
9609 if (disposition == disp_del)
9610 uiout->text ("Temporary ranged breakpoint ");
9611 else
9612 uiout->text ("Ranged breakpoint ");
9613 if (uiout->is_mi_like_p ())
9614 {
9615 uiout->field_string ("reason",
9616 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9617 uiout->field_string ("disp", bpdisp_text (disposition));
9618 }
9619 print_num_locno (bs, uiout);
9620 uiout->text (", ");
9621
9622 return PRINT_SRC_AND_LOC;
9623 }
9624
9625 /* Implement the "print_one" method for ranged breakpoints. */
9626
9627 bool
print_one(const bp_location ** last_loc)9628 ranged_breakpoint::print_one (const bp_location **last_loc) const
9629 {
9630 struct value_print_options opts;
9631 struct ui_out *uiout = current_uiout;
9632
9633 /* Ranged breakpoints have only one location. */
9634 gdb_assert (this->has_single_location ());
9635
9636 get_user_print_options (&opts);
9637
9638 if (opts.addressprint)
9639 /* We don't print the address range here, it will be printed later
9640 by ranged_breakpoint::print_one_detail. */
9641 uiout->field_skip ("addr");
9642 annotate_field (5);
9643 print_breakpoint_location (this, &this->first_loc ());
9644 *last_loc = &this->first_loc ();
9645
9646 return true;
9647 }
9648
9649 /* Implement the "print_one_detail" method for ranged breakpoints. */
9650
9651 void
print_one_detail(struct ui_out * uiout)9652 ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
9653 {
9654 CORE_ADDR address_start, address_end;
9655 const bp_location &bl = this->first_loc ();
9656 string_file stb;
9657
9658 address_start = bl.address;
9659 address_end = address_start + bl.length - 1;
9660
9661 uiout->text ("\taddress range: ");
9662 stb.printf ("[%s, %s]",
9663 print_core_address (bl.gdbarch, address_start),
9664 print_core_address (bl.gdbarch, address_end));
9665 uiout->field_stream ("addr", stb);
9666 uiout->text ("\n");
9667 }
9668
9669 /* Implement the "print_mention" method for ranged breakpoints. */
9670
9671 void
print_mention()9672 ranged_breakpoint::print_mention () const
9673 {
9674 const bp_location &bl = this->first_loc ();
9675 struct ui_out *uiout = current_uiout;
9676
9677 gdb_assert (type == bp_hardware_breakpoint);
9678
9679 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9680 number, paddress (bl.gdbarch, bl.address),
9681 paddress (bl.gdbarch, bl.address + bl.length - 1));
9682 }
9683
9684 /* Implement the "print_recreate" method for ranged breakpoints. */
9685
9686 void
print_recreate(struct ui_file * fp)9687 ranged_breakpoint::print_recreate (struct ui_file *fp) const
9688 {
9689 gdb_printf (fp, "break-range %s, %s",
9690 locspec->to_string (),
9691 locspec_range_end->to_string ());
9692 print_recreate_thread (fp);
9693 }
9694
9695 /* Find the address where the end of the breakpoint range should be
9696 placed, given the SAL of the end of the range. This is so that if
9697 the user provides a line number, the end of the range is set to the
9698 last instruction of the given line. */
9699
9700 static CORE_ADDR
find_breakpoint_range_end(struct symtab_and_line sal)9701 find_breakpoint_range_end (struct symtab_and_line sal)
9702 {
9703 CORE_ADDR end;
9704
9705 /* If the user provided a PC value, use it. Otherwise,
9706 find the address of the end of the given location. */
9707 if (sal.explicit_pc)
9708 end = sal.pc;
9709 else
9710 {
9711 int ret;
9712 CORE_ADDR start;
9713
9714 ret = find_line_pc_range (sal, &start, &end);
9715 if (!ret)
9716 error (_("Could not find location of the end of the range."));
9717
9718 /* find_line_pc_range returns the start of the next line. */
9719 end--;
9720 }
9721
9722 return end;
9723 }
9724
9725 /* Implement the "break-range" CLI command. */
9726
9727 static void
break_range_command(const char * arg,int from_tty)9728 break_range_command (const char *arg, int from_tty)
9729 {
9730 const char *arg_start;
9731 struct linespec_result canonical_start, canonical_end;
9732 int bp_count, can_use_bp, length;
9733 CORE_ADDR end;
9734
9735 /* We don't support software ranged breakpoints. */
9736 if (target_ranged_break_num_registers () < 0)
9737 error (_("This target does not support hardware ranged breakpoints."));
9738
9739 bp_count = hw_breakpoint_used_count ();
9740 bp_count += target_ranged_break_num_registers ();
9741 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9742 bp_count, 0);
9743 if (can_use_bp < 0)
9744 error (_("Hardware breakpoints used exceeds limit."));
9745
9746 arg = skip_spaces (arg);
9747 if (arg == NULL || arg[0] == '\0')
9748 error(_("No address range specified."));
9749
9750 arg_start = arg;
9751 location_spec_up start_locspec
9752 = string_to_location_spec (&arg, current_language);
9753 parse_breakpoint_sals (start_locspec.get (), &canonical_start);
9754
9755 if (arg[0] != ',')
9756 error (_("Too few arguments."));
9757 else if (canonical_start.lsals.empty ())
9758 error (_("Could not find location of the beginning of the range."));
9759
9760 const linespec_sals &lsal_start = canonical_start.lsals[0];
9761
9762 if (canonical_start.lsals.size () > 1
9763 || lsal_start.sals.size () != 1)
9764 error (_("Cannot create a ranged breakpoint with multiple locations."));
9765
9766 const symtab_and_line &sal_start = lsal_start.sals[0];
9767 std::string addr_string_start (arg_start, arg - arg_start);
9768
9769 arg++; /* Skip the comma. */
9770 arg = skip_spaces (arg);
9771
9772 /* Parse the end location specification. */
9773
9774 arg_start = arg;
9775
9776 /* We call decode_line_full directly here instead of using
9777 parse_breakpoint_sals because we need to specify the start
9778 location spec's symtab and line as the default symtab and line
9779 for the end of the range. This makes it possible to have ranges
9780 like "foo.c:27, +14", where +14 means 14 lines from the start
9781 location spec. */
9782 location_spec_up end_locspec
9783 = string_to_location_spec (&arg, current_language);
9784 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9785 sal_start.symtab, sal_start.line,
9786 &canonical_end, NULL, NULL);
9787
9788 if (canonical_end.lsals.empty ())
9789 error (_("Could not find location of the end of the range."));
9790
9791 const linespec_sals &lsal_end = canonical_end.lsals[0];
9792 if (canonical_end.lsals.size () > 1
9793 || lsal_end.sals.size () != 1)
9794 error (_("Cannot create a ranged breakpoint with multiple locations."));
9795
9796 const symtab_and_line &sal_end = lsal_end.sals[0];
9797
9798 end = find_breakpoint_range_end (sal_end);
9799 if (sal_start.pc > end)
9800 error (_("Invalid address range, end precedes start."));
9801
9802 length = end - sal_start.pc + 1;
9803 if (length < 0)
9804 /* Length overflowed. */
9805 error (_("Address range too large."));
9806 else if (length == 1)
9807 {
9808 /* This range is simple enough to be handled by
9809 the `hbreak' command. */
9810 hbreak_command (&addr_string_start[0], 1);
9811
9812 return;
9813 }
9814
9815 /* Now set up the breakpoint and install it. */
9816
9817 std::unique_ptr<breakpoint> br
9818 (new ranged_breakpoint (get_current_arch (),
9819 sal_start, length,
9820 std::move (start_locspec),
9821 std::move (end_locspec)));
9822
9823 install_breakpoint (false, std::move (br), true);
9824 }
9825
9826 /* See breakpoint.h. */
9827
~watchpoint()9828 watchpoint::~watchpoint ()
9829 {
9830 /* Make sure to unlink the destroyed watchpoint from the related
9831 breakpoint ring. */
9832
9833 breakpoint *bpt = this;
9834 while (bpt->related_breakpoint != this)
9835 bpt = bpt->related_breakpoint;
9836
9837 bpt->related_breakpoint = this->related_breakpoint;
9838 }
9839
9840 /* Return non-zero if EXP is verified as constant. Returned zero
9841 means EXP is variable. Also the constant detection may fail for
9842 some constant expressions and in such case still falsely return
9843 zero. */
9844
9845 static bool
watchpoint_exp_is_const(const struct expression * exp)9846 watchpoint_exp_is_const (const struct expression *exp)
9847 {
9848 return exp->op->constant_p ();
9849 }
9850
9851 /* Implement the "re_set" method for watchpoints. */
9852
9853 void
re_set()9854 watchpoint::re_set ()
9855 {
9856 /* Watchpoint can be either on expression using entirely global
9857 variables, or it can be on local variables.
9858
9859 Watchpoints of the first kind are never auto-deleted, and even
9860 persist across program restarts. Since they can use variables
9861 from shared libraries, we need to reparse expression as libraries
9862 are loaded and unloaded.
9863
9864 Watchpoints on local variables can also change meaning as result
9865 of solib event. For example, if a watchpoint uses both a local
9866 and a global variables in expression, it's a local watchpoint,
9867 but unloading of a shared library will make the expression
9868 invalid. This is not a very common use case, but we still
9869 re-evaluate expression, to avoid surprises to the user.
9870
9871 Note that for local watchpoints, we re-evaluate it only if
9872 watchpoints frame id is still valid. If it's not, it means the
9873 watchpoint is out of scope and will be deleted soon. In fact,
9874 I'm not sure we'll ever be called in this case.
9875
9876 If a local watchpoint's frame id is still valid, then
9877 exp_valid_block is likewise valid, and we can safely use it.
9878
9879 Don't do anything about disabled watchpoints, since they will be
9880 reevaluated again when enabled. */
9881 update_watchpoint (this, true /* reparse */);
9882 }
9883
9884 /* Implement the "insert" method for hardware watchpoints. */
9885
9886 int
insert_location(struct bp_location * bl)9887 watchpoint::insert_location (struct bp_location *bl)
9888 {
9889 int length = exact ? 1 : bl->length;
9890
9891 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9892 cond_exp.get ());
9893 }
9894
9895 /* Implement the "remove" method for hardware watchpoints. */
9896
9897 int
remove_location(struct bp_location * bl,enum remove_bp_reason reason)9898 watchpoint::remove_location (struct bp_location *bl,
9899 enum remove_bp_reason reason)
9900 {
9901 int length = exact ? 1 : bl->length;
9902
9903 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9904 cond_exp.get ());
9905 }
9906
9907 int
breakpoint_hit(const struct bp_location * bl,const address_space * aspace,CORE_ADDR bp_addr,const target_waitstatus & ws)9908 watchpoint::breakpoint_hit (const struct bp_location *bl,
9909 const address_space *aspace, CORE_ADDR bp_addr,
9910 const target_waitstatus &ws)
9911 {
9912 struct breakpoint *b = bl->owner;
9913
9914 /* Continuable hardware watchpoints are treated as non-existent if the
9915 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9916 some data address). Otherwise gdb won't stop on a break instruction
9917 in the code (not from a breakpoint) when a hardware watchpoint has
9918 been defined. Also skip watchpoints which we know did not trigger
9919 (did not match the data address). */
9920 if (is_hardware_watchpoint (b)
9921 && watchpoint_triggered == watch_triggered_no)
9922 return 0;
9923
9924 return 1;
9925 }
9926
9927 void
check_status(bpstat * bs)9928 watchpoint::check_status (bpstat *bs)
9929 {
9930 bpstat_check_watchpoint (bs);
9931 }
9932
9933 /* Implement the "resources_needed" method for hardware
9934 watchpoints. */
9935
9936 int
resources_needed(const struct bp_location * bl)9937 watchpoint::resources_needed (const struct bp_location *bl)
9938 {
9939 int length = exact? 1 : bl->length;
9940
9941 return target_region_ok_for_hw_watchpoint (bl->address, length);
9942 }
9943
9944 /* Implement the "works_in_software_mode" method for hardware
9945 watchpoints. */
9946
9947 bool
works_in_software_mode()9948 watchpoint::works_in_software_mode () const
9949 {
9950 /* Read and access watchpoints only work with hardware support. */
9951 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9952 }
9953
9954 enum print_stop_action
print_it(const bpstat * bs)9955 watchpoint::print_it (const bpstat *bs) const
9956 {
9957 enum print_stop_action result;
9958 struct ui_out *uiout = current_uiout;
9959
9960 gdb_assert (bs->bp_location_at != NULL);
9961
9962 annotate_watchpoint (this->number);
9963 maybe_print_thread_hit_breakpoint (uiout);
9964
9965 string_file stb;
9966
9967 std::optional<ui_out_emit_tuple> tuple_emitter;
9968 switch (this->type)
9969 {
9970 case bp_watchpoint:
9971 case bp_hardware_watchpoint:
9972 if (uiout->is_mi_like_p ())
9973 uiout->field_string
9974 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9975 mention (this);
9976 tuple_emitter.emplace (uiout, "value");
9977 uiout->text ("\nOld value = ");
9978 watchpoint_value_print (bs->old_val.get (), &stb);
9979 uiout->field_stream ("old", stb);
9980 uiout->text ("\nNew value = ");
9981 watchpoint_value_print (val.get (), &stb);
9982 uiout->field_stream ("new", stb);
9983 uiout->text ("\n");
9984 /* More than one watchpoint may have been triggered. */
9985 result = PRINT_UNKNOWN;
9986 break;
9987
9988 case bp_read_watchpoint:
9989 if (uiout->is_mi_like_p ())
9990 uiout->field_string
9991 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9992 mention (this);
9993 tuple_emitter.emplace (uiout, "value");
9994 uiout->text ("\nValue = ");
9995 watchpoint_value_print (val.get (), &stb);
9996 uiout->field_stream ("value", stb);
9997 uiout->text ("\n");
9998 result = PRINT_UNKNOWN;
9999 break;
10000
10001 case bp_access_watchpoint:
10002 if (bs->old_val != NULL)
10003 {
10004 if (uiout->is_mi_like_p ())
10005 uiout->field_string
10006 ("reason",
10007 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10008 mention (this);
10009 tuple_emitter.emplace (uiout, "value");
10010 uiout->text ("\nOld value = ");
10011 watchpoint_value_print (bs->old_val.get (), &stb);
10012 uiout->field_stream ("old", stb);
10013 uiout->text ("\nNew value = ");
10014 }
10015 else
10016 {
10017 mention (this);
10018 if (uiout->is_mi_like_p ())
10019 uiout->field_string
10020 ("reason",
10021 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10022 tuple_emitter.emplace (uiout, "value");
10023 uiout->text ("\nValue = ");
10024 }
10025 watchpoint_value_print (val.get (), &stb);
10026 uiout->field_stream ("new", stb);
10027 uiout->text ("\n");
10028 result = PRINT_UNKNOWN;
10029 break;
10030 default:
10031 result = PRINT_UNKNOWN;
10032 }
10033
10034 return result;
10035 }
10036
10037 /* Implement the "print_mention" method for hardware watchpoints. */
10038
10039 void
print_mention()10040 watchpoint::print_mention () const
10041 {
10042 struct ui_out *uiout = current_uiout;
10043 const char *tuple_name;
10044
10045 switch (type)
10046 {
10047 case bp_watchpoint:
10048 uiout->text ("Watchpoint ");
10049 tuple_name = "wpt";
10050 break;
10051 case bp_hardware_watchpoint:
10052 uiout->text ("Hardware watchpoint ");
10053 tuple_name = "wpt";
10054 break;
10055 case bp_read_watchpoint:
10056 uiout->text ("Hardware read watchpoint ");
10057 tuple_name = "hw-rwpt";
10058 break;
10059 case bp_access_watchpoint:
10060 uiout->text ("Hardware access (read/write) watchpoint ");
10061 tuple_name = "hw-awpt";
10062 break;
10063 default:
10064 internal_error (_("Invalid hardware watchpoint type."));
10065 }
10066
10067 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10068 uiout->field_signed ("number", number);
10069 uiout->text (": ");
10070 uiout->field_string ("exp", exp_string.get ());
10071 }
10072
10073 /* Implement the "print_recreate" method for watchpoints. */
10074
10075 void
print_recreate(struct ui_file * fp)10076 watchpoint::print_recreate (struct ui_file *fp) const
10077 {
10078 switch (type)
10079 {
10080 case bp_watchpoint:
10081 case bp_hardware_watchpoint:
10082 gdb_printf (fp, "watch");
10083 break;
10084 case bp_read_watchpoint:
10085 gdb_printf (fp, "rwatch");
10086 break;
10087 case bp_access_watchpoint:
10088 gdb_printf (fp, "awatch");
10089 break;
10090 default:
10091 internal_error (_("Invalid watchpoint type."));
10092 }
10093
10094 gdb_printf (fp, " %s", exp_string.get ());
10095 print_recreate_thread (fp);
10096 }
10097
10098 /* Implement the "explains_signal" method for watchpoints. */
10099
10100 bool
explains_signal(enum gdb_signal sig)10101 watchpoint::explains_signal (enum gdb_signal sig)
10102 {
10103 /* A software watchpoint cannot cause a signal other than
10104 GDB_SIGNAL_TRAP. */
10105 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10106 return false;
10107
10108 return true;
10109 }
10110
10111 struct masked_watchpoint : public watchpoint
10112 {
10113 using watchpoint::watchpoint;
10114
10115 int insert_location (struct bp_location *) override;
10116 int remove_location (struct bp_location *,
10117 enum remove_bp_reason reason) override;
10118 int resources_needed (const struct bp_location *) override;
10119 bool works_in_software_mode () const override;
10120 enum print_stop_action print_it (const bpstat *bs) const override;
10121 void print_one_detail (struct ui_out *) const override;
10122 void print_mention () const override;
10123 void print_recreate (struct ui_file *fp) const override;
10124 };
10125
10126 /* Implement the "insert" method for masked hardware watchpoints. */
10127
10128 int
insert_location(struct bp_location * bl)10129 masked_watchpoint::insert_location (struct bp_location *bl)
10130 {
10131 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
10132 bl->watchpoint_type);
10133 }
10134
10135 /* Implement the "remove" method for masked hardware watchpoints. */
10136
10137 int
remove_location(struct bp_location * bl,enum remove_bp_reason reason)10138 masked_watchpoint::remove_location (struct bp_location *bl,
10139 enum remove_bp_reason reason)
10140 {
10141 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
10142 bl->watchpoint_type);
10143 }
10144
10145 /* Implement the "resources_needed" method for masked hardware
10146 watchpoints. */
10147
10148 int
resources_needed(const struct bp_location * bl)10149 masked_watchpoint::resources_needed (const struct bp_location *bl)
10150 {
10151 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
10152 }
10153
10154 /* Implement the "works_in_software_mode" method for masked hardware
10155 watchpoints. */
10156
10157 bool
works_in_software_mode()10158 masked_watchpoint::works_in_software_mode () const
10159 {
10160 return false;
10161 }
10162
10163 /* Implement the "print_it" method for masked hardware
10164 watchpoints. */
10165
10166 enum print_stop_action
print_it(const bpstat * bs)10167 masked_watchpoint::print_it (const bpstat *bs) const
10168 {
10169 struct ui_out *uiout = current_uiout;
10170
10171 /* Masked watchpoints have only one location. */
10172 gdb_assert (this->has_single_location ());
10173
10174 annotate_watchpoint (this->number);
10175 maybe_print_thread_hit_breakpoint (uiout);
10176
10177 switch (this->type)
10178 {
10179 case bp_hardware_watchpoint:
10180 if (uiout->is_mi_like_p ())
10181 uiout->field_string
10182 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10183 break;
10184
10185 case bp_read_watchpoint:
10186 if (uiout->is_mi_like_p ())
10187 uiout->field_string
10188 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10189 break;
10190
10191 case bp_access_watchpoint:
10192 if (uiout->is_mi_like_p ())
10193 uiout->field_string
10194 ("reason",
10195 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10196 break;
10197 default:
10198 internal_error (_("Invalid hardware watchpoint type."));
10199 }
10200
10201 mention (this);
10202 uiout->text (_("\n\
10203 Check the underlying instruction at PC for the memory\n\
10204 address and value which triggered this watchpoint.\n"));
10205 uiout->text ("\n");
10206
10207 /* More than one watchpoint may have been triggered. */
10208 return PRINT_UNKNOWN;
10209 }
10210
10211 /* Implement the "print_one_detail" method for masked hardware
10212 watchpoints. */
10213
10214 void
print_one_detail(struct ui_out * uiout)10215 masked_watchpoint::print_one_detail (struct ui_out *uiout) const
10216 {
10217 /* Masked watchpoints have only one location. */
10218 gdb_assert (this->has_single_location ());
10219
10220 uiout->text ("\tmask ");
10221 uiout->field_core_addr ("mask", this->first_loc ().gdbarch, hw_wp_mask);
10222 uiout->text ("\n");
10223 }
10224
10225 /* Implement the "print_mention" method for masked hardware
10226 watchpoints. */
10227
10228 void
print_mention()10229 masked_watchpoint::print_mention () const
10230 {
10231 struct ui_out *uiout = current_uiout;
10232 const char *tuple_name;
10233
10234 switch (type)
10235 {
10236 case bp_hardware_watchpoint:
10237 uiout->text ("Masked hardware watchpoint ");
10238 tuple_name = "wpt";
10239 break;
10240 case bp_read_watchpoint:
10241 uiout->text ("Masked hardware read watchpoint ");
10242 tuple_name = "hw-rwpt";
10243 break;
10244 case bp_access_watchpoint:
10245 uiout->text ("Masked hardware access (read/write) watchpoint ");
10246 tuple_name = "hw-awpt";
10247 break;
10248 default:
10249 internal_error (_("Invalid hardware watchpoint type."));
10250 }
10251
10252 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10253 uiout->field_signed ("number", number);
10254 uiout->text (": ");
10255 uiout->field_string ("exp", exp_string.get ());
10256 }
10257
10258 /* Implement the "print_recreate" method for masked hardware
10259 watchpoints. */
10260
10261 void
print_recreate(struct ui_file * fp)10262 masked_watchpoint::print_recreate (struct ui_file *fp) const
10263 {
10264 switch (type)
10265 {
10266 case bp_hardware_watchpoint:
10267 gdb_printf (fp, "watch");
10268 break;
10269 case bp_read_watchpoint:
10270 gdb_printf (fp, "rwatch");
10271 break;
10272 case bp_access_watchpoint:
10273 gdb_printf (fp, "awatch");
10274 break;
10275 default:
10276 internal_error (_("Invalid hardware watchpoint type."));
10277 }
10278
10279 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
10280 phex (hw_wp_mask, sizeof (CORE_ADDR)));
10281 print_recreate_thread (fp);
10282 }
10283
10284 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10285
10286 static bool
is_masked_watchpoint(const struct breakpoint * b)10287 is_masked_watchpoint (const struct breakpoint *b)
10288 {
10289 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
10290 }
10291
10292 /* accessflag: hw_write: watch write,
10293 hw_read: watch read,
10294 hw_access: watch access (read or write) */
10295 static void
watch_command_1(const char * arg,int accessflag,int from_tty,bool just_location,bool internal)10296 watch_command_1 (const char *arg, int accessflag, int from_tty,
10297 bool just_location, bool internal)
10298 {
10299 struct breakpoint *scope_breakpoint = NULL;
10300 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10301 struct value *result;
10302 int saved_bitpos = 0, saved_bitsize = 0;
10303 const char *exp_start = NULL;
10304 const char *exp_end = NULL;
10305 const char *tok, *end_tok;
10306 int toklen = -1;
10307 const char *cond_start = NULL;
10308 const char *cond_end = NULL;
10309 enum bptype bp_type;
10310 int thread = -1;
10311 int inferior = -1;
10312 /* Flag to indicate whether we are going to use masks for
10313 the hardware watchpoint. */
10314 bool use_mask = false;
10315 CORE_ADDR mask = 0;
10316 int task = -1;
10317
10318 /* Make sure that we actually have parameters to parse. */
10319 if (arg != NULL && arg[0] != '\0')
10320 {
10321 const char *value_start;
10322
10323 exp_end = arg + strlen (arg);
10324
10325 /* Look for "parameter value" pairs at the end
10326 of the arguments string. */
10327 for (tok = exp_end - 1; tok > arg; tok--)
10328 {
10329 /* Skip whitespace at the end of the argument list. */
10330 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10331 tok--;
10332
10333 /* Find the beginning of the last token.
10334 This is the value of the parameter. */
10335 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10336 tok--;
10337 value_start = tok + 1;
10338
10339 /* Skip whitespace. */
10340 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10341 tok--;
10342
10343 end_tok = tok;
10344
10345 /* Find the beginning of the second to last token.
10346 This is the parameter itself. */
10347 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10348 tok--;
10349 tok++;
10350 toklen = end_tok - tok + 1;
10351
10352 if (toklen == 6 && startswith (tok, "thread"))
10353 {
10354 struct thread_info *thr;
10355 /* At this point we've found a "thread" token, which means
10356 the user is trying to set a watchpoint that triggers
10357 only in a specific thread. */
10358 const char *endp;
10359
10360 if (thread != -1)
10361 error(_("You can specify only one thread."));
10362
10363 if (task != -1)
10364 error (_("You can specify only one of thread or task."));
10365
10366 if (inferior != -1)
10367 error (_("You can specify only one of inferior or thread."));
10368
10369 /* Extract the thread ID from the next token. */
10370 thr = parse_thread_id (value_start, &endp);
10371 if (value_start == endp)
10372 error (_("Junk after thread keyword."));
10373
10374 thread = thr->global_num;
10375 }
10376 else if (toklen == 4 && startswith (tok, "task"))
10377 {
10378 char *tmp;
10379
10380 if (task != -1)
10381 error(_("You can specify only one task."));
10382
10383 if (thread != -1)
10384 error (_("You can specify only one of thread or task."));
10385
10386 if (inferior != -1)
10387 error (_("You can specify only one of inferior or task."));
10388
10389 task = strtol (value_start, &tmp, 0);
10390 if (tmp == value_start)
10391 error (_("Junk after task keyword."));
10392 if (!valid_task_id (task))
10393 error (_("Unknown task %d."), task);
10394 }
10395 else if (toklen == 8 && startswith (tok, "inferior"))
10396 {
10397 /* Support for watchpoints will be added in a later commit. */
10398 error (_("Cannot use 'inferior' keyword with watchpoints"));
10399 }
10400 else if (toklen == 4 && startswith (tok, "mask"))
10401 {
10402 /* We've found a "mask" token, which means the user wants to
10403 create a hardware watchpoint that is going to have the mask
10404 facility. */
10405 struct value *mask_value;
10406
10407 if (use_mask)
10408 error(_("You can specify only one mask."));
10409
10410 use_mask = just_location = true;
10411
10412 scoped_value_mark mark;
10413 mask_value = parse_to_comma_and_eval (&value_start);
10414 mask = value_as_address (mask_value);
10415 }
10416 else
10417 /* We didn't recognize what we found. We should stop here. */
10418 break;
10419
10420 /* Truncate the string and get rid of the "parameter value" pair before
10421 the arguments string is parsed by the parse_exp_1 function. */
10422 exp_end = tok;
10423 }
10424 }
10425 else
10426 exp_end = arg;
10427
10428 /* Parse the rest of the arguments. From here on out, everything
10429 is in terms of a newly allocated string instead of the original
10430 ARG. */
10431 std::string expression (arg, exp_end - arg);
10432 exp_start = arg = expression.c_str ();
10433 innermost_block_tracker tracker;
10434 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10435 exp_end = arg;
10436 /* Remove trailing whitespace from the expression before saving it.
10437 This makes the eventual display of the expression string a bit
10438 prettier. */
10439 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10440 --exp_end;
10441
10442 /* Checking if the expression is not constant. */
10443 if (watchpoint_exp_is_const (exp.get ()))
10444 {
10445 int len;
10446
10447 len = exp_end - exp_start;
10448 while (len > 0 && isspace (exp_start[len - 1]))
10449 len--;
10450 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10451 }
10452
10453 exp_valid_block = tracker.block ();
10454 struct value *mark = value_mark ();
10455 struct value *val_as_value = nullptr;
10456 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10457 just_location);
10458
10459 if (val_as_value != NULL && just_location)
10460 {
10461 saved_bitpos = val_as_value->bitpos ();
10462 saved_bitsize = val_as_value->bitsize ();
10463 }
10464
10465 value_ref_ptr val;
10466 if (just_location)
10467 {
10468 int ret;
10469
10470 exp_valid_block = NULL;
10471 val = release_value (value_addr (result));
10472 value_free_to_mark (mark);
10473
10474 if (use_mask)
10475 {
10476 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10477 mask);
10478 if (ret == -1)
10479 error (_("This target does not support masked watchpoints."));
10480 else if (ret == -2)
10481 error (_("Invalid mask or memory region."));
10482 }
10483 }
10484 else if (val_as_value != NULL)
10485 val = release_value (val_as_value);
10486
10487 tok = skip_spaces (arg);
10488 end_tok = skip_to_space (tok);
10489
10490 toklen = end_tok - tok;
10491 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10492 {
10493 tok = cond_start = end_tok + 1;
10494 innermost_block_tracker if_tracker;
10495 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10496
10497 /* The watchpoint expression may not be local, but the condition
10498 may still be. E.g.: `watch global if local > 0'. */
10499 cond_exp_valid_block = if_tracker.block ();
10500
10501 cond_end = tok;
10502 }
10503 if (*tok)
10504 error (_("Junk at end of command."));
10505
10506 frame_info_ptr wp_frame = block_innermost_frame (exp_valid_block);
10507
10508 /* Save this because create_internal_breakpoint below invalidates
10509 'wp_frame'. */
10510 frame_id watchpoint_frame = get_frame_id (wp_frame);
10511
10512 /* If the expression is "local", then set up a "watchpoint scope"
10513 breakpoint at the point where we've left the scope of the watchpoint
10514 expression. Create the scope breakpoint before the watchpoint, so
10515 that we will encounter it first in bpstat_stop_status. */
10516 if (exp_valid_block != NULL && wp_frame != NULL)
10517 {
10518 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10519
10520 if (frame_id_p (caller_frame_id))
10521 {
10522 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10523 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10524
10525 scope_breakpoint
10526 = create_internal_breakpoint (caller_arch, caller_pc,
10527 bp_watchpoint_scope);
10528
10529 /* create_internal_breakpoint could invalidate WP_FRAME. */
10530 wp_frame = NULL;
10531
10532 scope_breakpoint->enable_state = bp_enabled;
10533
10534 /* Automatically delete the breakpoint when it hits. */
10535 scope_breakpoint->disposition = disp_del;
10536
10537 /* Only break in the proper frame (help with recursion). */
10538 scope_breakpoint->frame_id = caller_frame_id;
10539
10540 /* Set the address at which we will stop. */
10541 bp_location &loc = scope_breakpoint->first_loc ();
10542 loc.gdbarch = caller_arch;
10543 loc.requested_address = caller_pc;
10544 loc.address
10545 = adjust_breakpoint_address (loc.gdbarch, loc.requested_address,
10546 scope_breakpoint->type,
10547 current_program_space);
10548 }
10549 }
10550
10551 /* Now set up the breakpoint. We create all watchpoints as hardware
10552 watchpoints here even if hardware watchpoints are turned off, a call
10553 to update_watchpoint later in this function will cause the type to
10554 drop back to bp_watchpoint (software watchpoint) if required. */
10555
10556 if (accessflag == hw_read)
10557 bp_type = bp_read_watchpoint;
10558 else if (accessflag == hw_access)
10559 bp_type = bp_access_watchpoint;
10560 else
10561 bp_type = bp_hardware_watchpoint;
10562
10563 std::unique_ptr<watchpoint> w;
10564 if (use_mask)
10565 w.reset (new masked_watchpoint (nullptr, bp_type));
10566 else
10567 w.reset (new watchpoint (nullptr, bp_type));
10568
10569 /* At most one of thread or task can be set on a watchpoint. */
10570 gdb_assert (thread == -1 || task == -1);
10571 w->thread = thread;
10572 w->inferior = inferior;
10573 w->task = task;
10574 w->disposition = disp_donttouch;
10575 w->pspace = current_program_space;
10576 w->exp = std::move (exp);
10577 w->exp_valid_block = exp_valid_block;
10578 w->cond_exp_valid_block = cond_exp_valid_block;
10579 if (just_location)
10580 {
10581 struct type *t = val->type ();
10582 CORE_ADDR addr = value_as_address (val.get ());
10583
10584 w->exp_string_reparse
10585 = current_language->watch_location_expression (t, addr);
10586
10587 w->exp_string = xstrprintf ("-location %.*s",
10588 (int) (exp_end - exp_start), exp_start);
10589 }
10590 else
10591 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10592
10593 if (use_mask)
10594 {
10595 w->hw_wp_mask = mask;
10596 }
10597 else
10598 {
10599 w->val = val;
10600 w->val_bitpos = saved_bitpos;
10601 w->val_bitsize = saved_bitsize;
10602 w->val_valid = true;
10603 }
10604
10605 if (cond_start)
10606 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10607 else
10608 w->cond_string = 0;
10609
10610 if (frame_id_p (watchpoint_frame))
10611 {
10612 w->watchpoint_frame = watchpoint_frame;
10613 w->watchpoint_thread = inferior_ptid;
10614 }
10615 else
10616 {
10617 w->watchpoint_frame = null_frame_id;
10618 w->watchpoint_thread = null_ptid;
10619 }
10620
10621 if (scope_breakpoint != NULL)
10622 {
10623 /* The scope breakpoint is related to the watchpoint. We will
10624 need to act on them together. */
10625 w->related_breakpoint = scope_breakpoint;
10626 scope_breakpoint->related_breakpoint = w.get ();
10627 }
10628
10629 if (!just_location)
10630 value_free_to_mark (mark);
10631
10632 /* Finally update the new watchpoint. This creates the locations
10633 that should be inserted. */
10634 update_watchpoint (w.get (), true /* reparse */);
10635
10636 install_breakpoint (internal, std::move (w), 1);
10637 }
10638
10639 /* Return count of debug registers needed to watch the given expression.
10640 If the watchpoint cannot be handled in hardware return zero. */
10641
10642 static int
can_use_hardware_watchpoint(const std::vector<value_ref_ptr> & vals)10643 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10644 {
10645 int found_memory_cnt = 0;
10646
10647 /* Did the user specifically forbid us to use hardware watchpoints? */
10648 if (!can_use_hw_watchpoints)
10649 return 0;
10650
10651 gdb_assert (!vals.empty ());
10652 struct value *head = vals[0].get ();
10653
10654 /* Make sure that the value of the expression depends only upon
10655 memory contents, and values computed from them within GDB. If we
10656 find any register references or function calls, we can't use a
10657 hardware watchpoint.
10658
10659 The idea here is that evaluating an expression generates a series
10660 of values, one holding the value of every subexpression. (The
10661 expression a*b+c has five subexpressions: a, b, a*b, c, and
10662 a*b+c.) GDB's values hold almost enough information to establish
10663 the criteria given above --- they identify memory lvalues,
10664 register lvalues, computed values, etcetera. So we can evaluate
10665 the expression, and then scan the chain of values that leaves
10666 behind to decide whether we can detect any possible change to the
10667 expression's final value using only hardware watchpoints.
10668
10669 However, I don't think that the values returned by inferior
10670 function calls are special in any way. So this function may not
10671 notice that an expression involving an inferior function call
10672 can't be watched with hardware watchpoints. FIXME. */
10673 for (const value_ref_ptr &iter : vals)
10674 {
10675 struct value *v = iter.get ();
10676
10677 if (v->lval () == lval_memory)
10678 {
10679 if (v != head && v->lazy ())
10680 /* A lazy memory lvalue in the chain is one that GDB never
10681 needed to fetch; we either just used its address (e.g.,
10682 `a' in `a.b') or we never needed it at all (e.g., `a'
10683 in `a,b'). This doesn't apply to HEAD; if that is
10684 lazy then it was not readable, but watch it anyway. */
10685 ;
10686 else
10687 {
10688 /* Ahh, memory we actually used! Check if we can cover
10689 it with hardware watchpoints. */
10690 struct type *vtype = check_typedef (v->type ());
10691
10692 /* We only watch structs and arrays if user asked for it
10693 explicitly, never if they just happen to appear in a
10694 middle of some value chain. */
10695 if (v == head
10696 || (vtype->code () != TYPE_CODE_STRUCT
10697 && vtype->code () != TYPE_CODE_ARRAY))
10698 {
10699 CORE_ADDR vaddr = v->address ();
10700 int len;
10701 int num_regs;
10702
10703 len = (target_exact_watchpoints
10704 && is_scalar_type_recursive (vtype))?
10705 1 : v->type ()->length ();
10706
10707 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10708 if (!num_regs)
10709 return 0;
10710 else
10711 found_memory_cnt += num_regs;
10712 }
10713 }
10714 }
10715 else if (v->lval () != not_lval && !v->deprecated_modifiable ())
10716 return 0; /* These are values from the history (e.g., $1). */
10717 else if (v->lval () == lval_register)
10718 return 0; /* Cannot watch a register with a HW watchpoint. */
10719 }
10720
10721 /* The expression itself looks suitable for using a hardware
10722 watchpoint, but give the target machine a chance to reject it. */
10723 return found_memory_cnt;
10724 }
10725
10726 void
watch_command_wrapper(const char * arg,int from_tty,bool internal)10727 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10728 {
10729 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10730 }
10731
10732 /* Options for the watch, awatch, and rwatch commands. */
10733
10734 struct watch_options
10735 {
10736 /* For -location. */
10737 bool location = false;
10738 };
10739
10740 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10741
10742 Historically GDB always accepted both '-location' and '-l' flags for
10743 these commands (both flags being synonyms). When converting to the
10744 newer option scheme only '-location' is added here. That's fine (for
10745 backward compatibility) as any non-ambiguous prefix of a flag will be
10746 accepted, so '-l', '-loc', are now all accepted.
10747
10748 What this means is that, if in the future, we add any new flag here
10749 that starts with '-l' then this will break backward compatibility, so
10750 please, don't do that! */
10751
10752 static const gdb::option::option_def watch_option_defs[] = {
10753 gdb::option::flag_option_def<watch_options> {
10754 "location",
10755 [] (watch_options *opt) { return &opt->location; },
10756 N_("\
10757 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10758 -l can be used as a short form of -location."),
10759 },
10760 };
10761
10762 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10763 commands. */
10764
10765 static gdb::option::option_def_group
make_watch_options_def_group(watch_options * opts)10766 make_watch_options_def_group (watch_options *opts)
10767 {
10768 return {{watch_option_defs}, opts};
10769 }
10770
10771 /* A helper function that looks for the "-location" argument and then
10772 calls watch_command_1. */
10773
10774 static void
watch_maybe_just_location(const char * arg,int accessflag,int from_tty)10775 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10776 {
10777 watch_options opts;
10778 auto grp = make_watch_options_def_group (&opts);
10779 gdb::option::process_options
10780 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10781 if (arg != nullptr && *arg == '\0')
10782 arg = nullptr;
10783
10784 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10785 }
10786
10787 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10788 static void
watch_command_completer(struct cmd_list_element * ignore,completion_tracker & tracker,const char * text,const char *)10789 watch_command_completer (struct cmd_list_element *ignore,
10790 completion_tracker &tracker,
10791 const char *text, const char * /*word*/)
10792 {
10793 const auto group = make_watch_options_def_group (nullptr);
10794 if (gdb::option::complete_options
10795 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10796 return;
10797
10798 const char *word = advance_to_expression_complete_word_point (tracker, text);
10799 expression_completer (ignore, tracker, text, word);
10800 }
10801
10802 static void
watch_command(const char * arg,int from_tty)10803 watch_command (const char *arg, int from_tty)
10804 {
10805 watch_maybe_just_location (arg, hw_write, from_tty);
10806 }
10807
10808 void
rwatch_command_wrapper(const char * arg,int from_tty,bool internal)10809 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10810 {
10811 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10812 }
10813
10814 static void
rwatch_command(const char * arg,int from_tty)10815 rwatch_command (const char *arg, int from_tty)
10816 {
10817 watch_maybe_just_location (arg, hw_read, from_tty);
10818 }
10819
10820 void
awatch_command_wrapper(const char * arg,int from_tty,bool internal)10821 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
10822 {
10823 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10824 }
10825
10826 static void
awatch_command(const char * arg,int from_tty)10827 awatch_command (const char *arg, int from_tty)
10828 {
10829 watch_maybe_just_location (arg, hw_access, from_tty);
10830 }
10831
10832
10833 /* Data for the FSM that manages the until(location)/advance commands
10834 in infcmd.c. Here because it uses the mechanisms of
10835 breakpoints. */
10836
10837 struct until_break_fsm : public thread_fsm
10838 {
10839 /* The thread that was current when the command was executed. */
10840 int thread;
10841
10842 /* The breakpoint set at the return address in the caller frame,
10843 plus breakpoints at all the destination locations. */
10844 std::vector<breakpoint_up> breakpoints;
10845
until_break_fsmuntil_break_fsm10846 until_break_fsm (struct interp *cmd_interp, int thread,
10847 std::vector<breakpoint_up> &&breakpoints)
10848 : thread_fsm (cmd_interp),
10849 thread (thread),
10850 breakpoints (std::move (breakpoints))
10851 {
10852 }
10853
10854 void clean_up (struct thread_info *thread) override;
10855 bool should_stop (struct thread_info *thread) override;
10856 enum async_reply_reason do_async_reply_reason () override;
10857 };
10858
10859 /* Implementation of the 'should_stop' FSM method for the
10860 until(location)/advance commands. */
10861
10862 bool
should_stop(struct thread_info * tp)10863 until_break_fsm::should_stop (struct thread_info *tp)
10864 {
10865 for (const breakpoint_up &bp : breakpoints)
10866 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10867 bp.get ()) != NULL)
10868 {
10869 set_finished ();
10870 break;
10871 }
10872
10873 return true;
10874 }
10875
10876 /* Implementation of the 'clean_up' FSM method for the
10877 until(location)/advance commands. */
10878
10879 void
clean_up(struct thread_info *)10880 until_break_fsm::clean_up (struct thread_info *)
10881 {
10882 /* Clean up our temporary breakpoints. */
10883 breakpoints.clear ();
10884 delete_longjmp_breakpoint (thread);
10885 }
10886
10887 /* Implementation of the 'async_reply_reason' FSM method for the
10888 until(location)/advance commands. */
10889
10890 enum async_reply_reason
do_async_reply_reason()10891 until_break_fsm::do_async_reply_reason ()
10892 {
10893 return EXEC_ASYNC_LOCATION_REACHED;
10894 }
10895
10896 void
until_break_command(const char * arg,int from_tty,int anywhere)10897 until_break_command (const char *arg, int from_tty, int anywhere)
10898 {
10899 frame_info_ptr frame;
10900 struct gdbarch *frame_gdbarch;
10901 struct frame_id stack_frame_id;
10902 struct frame_id caller_frame_id;
10903 int thread;
10904 struct thread_info *tp;
10905
10906 clear_proceed_status (0);
10907
10908 /* Set a breakpoint where the user wants it and at return from
10909 this function. */
10910
10911 location_spec_up locspec = string_to_location_spec (&arg, current_language);
10912
10913 std::vector<symtab_and_line> sals
10914 = (last_displayed_sal_is_valid ()
10915 ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10916 get_last_displayed_symtab (),
10917 get_last_displayed_line ())
10918 : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
10919 NULL, NULL, 0));
10920
10921 if (sals.empty ())
10922 error (_("Couldn't get information on specified line."));
10923
10924 if (*arg)
10925 error (_("Junk at end of arguments."));
10926
10927 tp = inferior_thread ();
10928 thread = tp->global_num;
10929
10930 /* Note linespec handling above invalidates the frame chain.
10931 Installing a breakpoint also invalidates the frame chain (as it
10932 may need to switch threads), so do any frame handling before
10933 that. */
10934
10935 frame = get_selected_frame (NULL);
10936 frame_gdbarch = get_frame_arch (frame);
10937 stack_frame_id = get_stack_frame_id (frame);
10938 caller_frame_id = frame_unwind_caller_id (frame);
10939
10940 /* Keep within the current frame, or in frames called by the current
10941 one. */
10942
10943 std::vector<breakpoint_up> breakpoints;
10944
10945 std::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10946
10947 if (frame_id_p (caller_frame_id))
10948 {
10949 struct symtab_and_line sal2;
10950 struct gdbarch *caller_gdbarch;
10951
10952 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10953 sal2.pc = frame_unwind_caller_pc (frame);
10954 caller_gdbarch = frame_unwind_caller_arch (frame);
10955
10956 breakpoint_up caller_breakpoint
10957 = set_momentary_breakpoint (caller_gdbarch, sal2,
10958 caller_frame_id, bp_until);
10959 breakpoints.emplace_back (std::move (caller_breakpoint));
10960
10961 set_longjmp_breakpoint (tp, stack_frame_id);
10962 lj_deleter.emplace (thread);
10963 }
10964
10965 /* If the user told us to continue until a specified location, we
10966 don't specify a frame at which we need to stop. Otherwise,
10967 specify the selected frame, because we want to stop only at the
10968 very same frame. */
10969 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10970
10971 for (symtab_and_line &sal : sals)
10972 {
10973 resolve_sal_pc (&sal);
10974
10975 breakpoint_up location_breakpoint
10976 = set_momentary_breakpoint (frame_gdbarch, sal,
10977 stop_frame_id, bp_until);
10978 breakpoints.emplace_back (std::move (location_breakpoint));
10979 }
10980
10981 tp->set_thread_fsm
10982 (std::unique_ptr<thread_fsm>
10983 (new until_break_fsm (command_interp (), tp->global_num,
10984 std::move (breakpoints))));
10985
10986 if (lj_deleter)
10987 lj_deleter->release ();
10988
10989 proceed (-1, GDB_SIGNAL_DEFAULT);
10990 }
10991
10992
10993
10994 /* Compare two breakpoints and return a strcmp-like result. */
10995
10996 static int
compare_breakpoints(const breakpoint * a,const breakpoint * b)10997 compare_breakpoints (const breakpoint *a, const breakpoint *b)
10998 {
10999 uintptr_t ua = (uintptr_t) a;
11000 uintptr_t ub = (uintptr_t) b;
11001
11002 if (a->number < b->number)
11003 return -1;
11004 else if (a->number > b->number)
11005 return 1;
11006
11007 /* Now sort by address, in case we see, e..g, two breakpoints with
11008 the number 0. */
11009 if (ua < ub)
11010 return -1;
11011 return ua > ub ? 1 : 0;
11012 }
11013
11014 /* Delete breakpoints by address or line. */
11015
11016 static void
clear_command(const char * arg,int from_tty)11017 clear_command (const char *arg, int from_tty)
11018 {
11019 int default_match;
11020
11021 std::vector<symtab_and_line> decoded_sals;
11022 symtab_and_line last_sal;
11023 gdb::array_view<symtab_and_line> sals;
11024 if (arg)
11025 {
11026 decoded_sals
11027 = decode_line_with_current_source (arg,
11028 (DECODE_LINE_FUNFIRSTLINE
11029 | DECODE_LINE_LIST_MODE));
11030 default_match = 0;
11031 sals = decoded_sals;
11032 }
11033 else
11034 {
11035 /* Set sal's line, symtab, pc, and pspace to the values
11036 corresponding to the last call to print_frame_info. If the
11037 codepoint is not valid, this will set all the fields to 0. */
11038 last_sal = get_last_displayed_sal ();
11039 if (last_sal.symtab == 0)
11040 error (_("No source file specified."));
11041
11042 default_match = 1;
11043 sals = last_sal;
11044 }
11045
11046 /* We don't call resolve_sal_pc here. That's not as bad as it
11047 seems, because all existing breakpoints typically have both
11048 file/line and pc set. So, if clear is given file/line, we can
11049 match this to existing breakpoint without obtaining pc at all.
11050
11051 We only support clearing given the address explicitly
11052 present in breakpoint table. Say, we've set breakpoint
11053 at file:line. There were several PC values for that file:line,
11054 due to optimization, all in one block.
11055
11056 We've picked one PC value. If "clear" is issued with another
11057 PC corresponding to the same file:line, the breakpoint won't
11058 be cleared. We probably can still clear the breakpoint, but
11059 since the other PC value is never presented to user, user
11060 can only find it by guessing, and it does not seem important
11061 to support that. */
11062
11063 /* For each line spec given, delete bps which correspond to it. Do
11064 it in two passes, solely to preserve the current behavior that
11065 from_tty is forced true if we delete more than one
11066 breakpoint. */
11067
11068 std::vector<struct breakpoint *> found;
11069 for (const auto &sal : sals)
11070 {
11071 const char *sal_fullname;
11072
11073 /* If exact pc given, clear bpts at that pc.
11074 If line given (pc == 0), clear all bpts on specified line.
11075 If defaulting, clear all bpts on default line
11076 or at default pc.
11077
11078 defaulting sal.pc != 0 tests to do
11079
11080 0 1 pc
11081 1 1 pc _and_ line
11082 0 0 line
11083 1 0 <can't happen> */
11084
11085 sal_fullname = (sal.symtab == NULL
11086 ? NULL : symtab_to_fullname (sal.symtab));
11087
11088 /* Find all matching breakpoints and add them to 'found'. */
11089 for (breakpoint &b : all_breakpoints ())
11090 {
11091 int match = 0;
11092 /* Are we going to delete b? */
11093 if (b.type != bp_none && !is_watchpoint (&b)
11094 && user_breakpoint_p (&b))
11095 {
11096 for (bp_location &loc : b.locations ())
11097 {
11098 /* If the user specified file:line, don't allow a PC
11099 match. This matches historical gdb behavior. */
11100 int pc_match = (!sal.explicit_line
11101 && sal.pc
11102 && (loc.pspace == sal.pspace)
11103 && (loc.address == sal.pc)
11104 && (!section_is_overlay (loc.section)
11105 || loc.section == sal.section));
11106 int line_match = 0;
11107
11108 if ((default_match || sal.explicit_line)
11109 && loc.symtab != NULL
11110 && sal_fullname != NULL
11111 && sal.pspace == loc.pspace
11112 && loc.line_number == sal.line
11113 && filename_cmp (symtab_to_fullname (loc.symtab),
11114 sal_fullname) == 0)
11115 line_match = 1;
11116
11117 if (pc_match || line_match)
11118 {
11119 match = 1;
11120 break;
11121 }
11122 }
11123 }
11124
11125 if (match)
11126 found.push_back (&b);
11127 }
11128 }
11129
11130 /* Now go thru the 'found' chain and delete them. */
11131 if (found.empty ())
11132 {
11133 if (arg)
11134 error (_("No breakpoint at %s."), arg);
11135 else
11136 error (_("No breakpoint at this line."));
11137 }
11138
11139 /* Remove duplicates from the vec. */
11140 std::sort (found.begin (), found.end (),
11141 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11142 {
11143 return compare_breakpoints (bp_a, bp_b) < 0;
11144 });
11145 found.erase (std::unique (found.begin (), found.end (),
11146 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11147 {
11148 return compare_breakpoints (bp_a, bp_b) == 0;
11149 }),
11150 found.end ());
11151
11152 if (found.size () > 1)
11153 from_tty = 1; /* Always report if deleted more than one. */
11154 if (from_tty)
11155 {
11156 if (found.size () == 1)
11157 gdb_printf (_("Deleted breakpoint "));
11158 else
11159 gdb_printf (_("Deleted breakpoints "));
11160 }
11161
11162 for (breakpoint *iter : found)
11163 {
11164 if (from_tty)
11165 gdb_printf ("%d ", iter->number);
11166 delete_breakpoint (iter);
11167 }
11168 if (from_tty)
11169 gdb_putc ('\n');
11170 }
11171
11172 /* Delete breakpoint in BS if they are `delete' breakpoints and
11173 all breakpoints that are marked for deletion, whether hit or not.
11174 This is called after any breakpoint is hit, or after errors. */
11175
11176 void
breakpoint_auto_delete(bpstat * bs)11177 breakpoint_auto_delete (bpstat *bs)
11178 {
11179 for (; bs; bs = bs->next)
11180 if (bs->breakpoint_at
11181 && bs->breakpoint_at->disposition == disp_del
11182 && bs->stop)
11183 delete_breakpoint (bs->breakpoint_at);
11184
11185 for (breakpoint &b : all_breakpoints_safe ())
11186 if (b.disposition == disp_del_at_next_stop)
11187 delete_breakpoint (&b);
11188 }
11189
11190 /* A comparison function for bp_location AP and BP being interfaced to
11191 std::sort. Sort elements primarily by their ADDRESS (no matter what
11192 bl_address_is_meaningful says), secondarily by ordering first
11193 permanent elements and tertiarily just ensuring the array is sorted
11194 stable way despite std::sort being an unstable algorithm. */
11195
11196 static int
bp_location_is_less_than(const bp_location * a,const bp_location * b)11197 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11198 {
11199 if (a->address != b->address)
11200 return a->address < b->address;
11201
11202 /* Sort locations at the same address by their pspace number, keeping
11203 locations of the same inferior (in a multi-inferior environment)
11204 grouped. */
11205
11206 if (a->pspace->num != b->pspace->num)
11207 return a->pspace->num < b->pspace->num;
11208
11209 /* Sort permanent breakpoints first. */
11210 if (a->permanent != b->permanent)
11211 return a->permanent > b->permanent;
11212
11213 /* Sort by type in order to make duplicate determination easier.
11214 See update_global_location_list. This is kept in sync with
11215 breakpoint_locations_match. */
11216 if (a->loc_type < b->loc_type)
11217 return true;
11218
11219 /* Likewise, for range-breakpoints, sort by length. */
11220 if (a->loc_type == bp_loc_hardware_breakpoint
11221 && b->loc_type == bp_loc_hardware_breakpoint
11222 && a->length < b->length)
11223 return true;
11224
11225 /* Make the internal GDB representation stable across GDB runs
11226 where A and B memory inside GDB can differ. Breakpoint locations of
11227 the same type at the same address can be sorted in arbitrary order. */
11228
11229 if (a->owner->number != b->owner->number)
11230 return a->owner->number < b->owner->number;
11231
11232 return a < b;
11233 }
11234
11235 /* Set bp_locations_placed_address_before_address_max and
11236 bp_locations_shadow_len_after_address_max according to the current
11237 content of the bp_locations array. */
11238
11239 static void
bp_locations_target_extensions_update(void)11240 bp_locations_target_extensions_update (void)
11241 {
11242 bp_locations_placed_address_before_address_max = 0;
11243 bp_locations_shadow_len_after_address_max = 0;
11244
11245 for (bp_location *bl : all_bp_locations ())
11246 {
11247 CORE_ADDR start, end, addr;
11248
11249 if (!bp_location_has_shadow (bl))
11250 continue;
11251
11252 start = bl->target_info.placed_address;
11253 end = start + bl->target_info.shadow_len;
11254
11255 gdb_assert (bl->address >= start);
11256 addr = bl->address - start;
11257 if (addr > bp_locations_placed_address_before_address_max)
11258 bp_locations_placed_address_before_address_max = addr;
11259
11260 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11261
11262 gdb_assert (bl->address < end);
11263 addr = end - bl->address;
11264 if (addr > bp_locations_shadow_len_after_address_max)
11265 bp_locations_shadow_len_after_address_max = addr;
11266 }
11267 }
11268
11269 /* Download tracepoint locations if they haven't been. */
11270
11271 static void
download_tracepoint_locations(void)11272 download_tracepoint_locations (void)
11273 {
11274 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11275
11276 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11277
11278 for (breakpoint &b : all_tracepoints ())
11279 {
11280 bool bp_location_downloaded = false;
11281
11282 if ((b.type == bp_fast_tracepoint
11283 ? !may_insert_fast_tracepoints
11284 : !may_insert_tracepoints))
11285 continue;
11286
11287 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11288 {
11289 if (target_can_download_tracepoint ())
11290 can_download_tracepoint = TRIBOOL_TRUE;
11291 else
11292 can_download_tracepoint = TRIBOOL_FALSE;
11293 }
11294
11295 if (can_download_tracepoint == TRIBOOL_FALSE)
11296 break;
11297
11298 for (bp_location &bl : b.locations ())
11299 {
11300 /* In tracepoint, locations are _never_ duplicated, so
11301 should_be_inserted is equivalent to
11302 unduplicated_should_be_inserted. */
11303 if (!should_be_inserted (&bl) || bl.inserted)
11304 continue;
11305
11306 switch_to_program_space_and_thread (bl.pspace);
11307
11308 target_download_tracepoint (&bl);
11309
11310 bl.inserted = 1;
11311 bp_location_downloaded = true;
11312 }
11313
11314 tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
11315 t.number_on_target = b.number;
11316 if (bp_location_downloaded)
11317 notify_breakpoint_modified (&b);
11318 }
11319 }
11320
11321 /* Swap the insertion/duplication state between two locations. */
11322
11323 static void
swap_insertion(struct bp_location * left,struct bp_location * right)11324 swap_insertion (struct bp_location *left, struct bp_location *right)
11325 {
11326 const int left_inserted = left->inserted;
11327 const int left_duplicate = left->duplicate;
11328 const int left_needs_update = left->needs_update;
11329 const struct bp_target_info left_target_info = left->target_info;
11330
11331 /* Locations of tracepoints can never be duplicated. */
11332 if (is_tracepoint (left->owner))
11333 gdb_assert (!left->duplicate);
11334 if (is_tracepoint (right->owner))
11335 gdb_assert (!right->duplicate);
11336
11337 left->inserted = right->inserted;
11338 left->duplicate = right->duplicate;
11339 left->needs_update = right->needs_update;
11340 left->target_info = right->target_info;
11341 right->inserted = left_inserted;
11342 right->duplicate = left_duplicate;
11343 right->needs_update = left_needs_update;
11344 right->target_info = left_target_info;
11345 }
11346
11347 /* Force the re-insertion of the locations at ADDRESS. This is called
11348 once a new/deleted/modified duplicate location is found and we are evaluating
11349 conditions on the target's side. Such conditions need to be updated on
11350 the target. */
11351
11352 static void
force_breakpoint_reinsertion(struct bp_location * bl)11353 force_breakpoint_reinsertion (struct bp_location *bl)
11354 {
11355 CORE_ADDR address = 0;
11356 int pspace_num;
11357
11358 address = bl->address;
11359 pspace_num = bl->pspace->num;
11360
11361 /* This is only meaningful if the target is
11362 evaluating conditions and if the user has
11363 opted for condition evaluation on the target's
11364 side. */
11365 if (gdb_evaluates_breakpoint_condition_p ()
11366 || !target_supports_evaluation_of_breakpoint_conditions ())
11367 return;
11368
11369 /* Flag all breakpoint locations with this address and
11370 the same program space as the location
11371 as "its condition has changed". We need to
11372 update the conditions on the target's side. */
11373 for (bp_location *loc : all_bp_locations_at_addr (address))
11374 {
11375 if (!is_breakpoint (loc->owner)
11376 || pspace_num != loc->pspace->num)
11377 continue;
11378
11379 /* Flag the location appropriately. We use a different state to
11380 let everyone know that we already updated the set of locations
11381 with addr bl->address and program space bl->pspace. This is so
11382 we don't have to keep calling these functions just to mark locations
11383 that have already been marked. */
11384 loc->condition_changed = condition_updated;
11385
11386 /* Free the agent expression bytecode as well. We will compute
11387 it later on. */
11388 loc->cond_bytecode.reset ();
11389 }
11390 }
11391
11392 /* Called whether new breakpoints are created, or existing breakpoints
11393 deleted, to update the global location list and recompute which
11394 locations are duplicate of which.
11395
11396 The INSERT_MODE flag determines whether locations may not, may, or
11397 shall be inserted now. See 'enum ugll_insert_mode' for more
11398 info. */
11399
11400 static void
update_global_location_list(enum ugll_insert_mode insert_mode)11401 update_global_location_list (enum ugll_insert_mode insert_mode)
11402 {
11403 /* Last breakpoint location address that was marked for update. */
11404 CORE_ADDR last_addr = 0;
11405 /* Last breakpoint location program space that was marked for update. */
11406 int last_pspace_num = -1;
11407
11408 breakpoint_debug_printf ("insert_mode = %s",
11409 ugll_insert_mode_text (insert_mode));
11410
11411 /* Used in the duplicates detection below. When iterating over all
11412 bp_locations, points to the first bp_location of a given address.
11413 Breakpoints and watchpoints of different types are never
11414 duplicates of each other. Keep one pointer for each type of
11415 breakpoint/watchpoint, so we only need to loop over all locations
11416 once. */
11417 struct bp_location *bp_loc_first; /* breakpoint */
11418 struct bp_location *wp_loc_first; /* hardware watchpoint */
11419 struct bp_location *awp_loc_first; /* access watchpoint */
11420 struct bp_location *rwp_loc_first; /* read watchpoint */
11421
11422 /* Saved former bp_locations array which we compare against the newly
11423 built bp_locations from the current state of ALL_BREAKPOINTS. */
11424 std::vector<bp_location *> old_locations = std::move (bp_locations);
11425 bp_locations.clear ();
11426
11427 for (breakpoint &b : all_breakpoints ())
11428 for (bp_location &loc : b.locations ())
11429 bp_locations.push_back (&loc);
11430
11431 /* See if we need to "upgrade" a software breakpoint to a hardware
11432 breakpoint. Do this before deciding whether locations are
11433 duplicates. Also do this before sorting because sorting order
11434 depends on location type. */
11435 for (bp_location *loc : bp_locations)
11436 if (!loc->inserted && should_be_inserted (loc))
11437 handle_automatic_hardware_breakpoints (loc);
11438
11439 std::sort (bp_locations.begin (), bp_locations.end (),
11440 bp_location_is_less_than);
11441
11442 bp_locations_target_extensions_update ();
11443
11444 /* Identify bp_location instances that are no longer present in the
11445 new list, and therefore should be freed. Note that it's not
11446 necessary that those locations should be removed from inferior --
11447 if there's another location at the same address (previously
11448 marked as duplicate), we don't need to remove/insert the
11449 location.
11450
11451 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11452 and former bp_location array state respectively. */
11453
11454 size_t loc_i = 0;
11455 for (bp_location *old_loc : old_locations)
11456 {
11457 /* Tells if 'old_loc' is found among the new locations. If
11458 not, we have to free it. */
11459 bool found_object = false;
11460 /* Tells if the location should remain inserted in the target. */
11461 bool keep_in_target = false;
11462 bool removed = false;
11463
11464 /* Skip LOCP entries which will definitely never be needed.
11465 Stop either at or being the one matching OLD_LOC. */
11466 while (loc_i < bp_locations.size ()
11467 && bp_locations[loc_i]->address < old_loc->address)
11468 loc_i++;
11469
11470 for (size_t loc2_i = loc_i;
11471 (loc2_i < bp_locations.size ()
11472 && bp_locations[loc2_i]->address == old_loc->address);
11473 loc2_i++)
11474 {
11475 /* Check if this is a new/duplicated location or a duplicated
11476 location that had its condition modified. If so, we want to send
11477 its condition to the target if evaluation of conditions is taking
11478 place there. */
11479 if (bp_locations[loc2_i]->condition_changed == condition_modified
11480 && (last_addr != old_loc->address
11481 || last_pspace_num != old_loc->pspace->num))
11482 {
11483 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11484 last_pspace_num = old_loc->pspace->num;
11485 }
11486
11487 if (bp_locations[loc2_i] == old_loc)
11488 found_object = true;
11489 }
11490
11491 /* We have already handled this address, update it so that we don't
11492 have to go through updates again. */
11493 last_addr = old_loc->address;
11494
11495 /* Target-side condition evaluation: Handle deleted locations. */
11496 if (!found_object)
11497 force_breakpoint_reinsertion (old_loc);
11498
11499 /* If this location is no longer present, and inserted, look if
11500 there's maybe a new location at the same address. If so,
11501 mark that one inserted, and don't remove this one. This is
11502 needed so that we don't have a time window where a breakpoint
11503 at certain location is not inserted. */
11504
11505 if (old_loc->inserted)
11506 {
11507 /* If the location is inserted now, we might have to remove
11508 it. */
11509
11510 if (found_object && should_be_inserted (old_loc))
11511 {
11512 /* The location is still present in the location list,
11513 and still should be inserted. Don't do anything. */
11514 keep_in_target = true;
11515 }
11516 else
11517 {
11518 /* This location still exists, but it won't be kept in the
11519 target since it may have been disabled. We proceed to
11520 remove its target-side condition. */
11521
11522 /* The location is either no longer present, or got
11523 disabled. See if there's another location at the
11524 same address, in which case we don't need to remove
11525 this one from the target. */
11526
11527 /* OLD_LOC comes from existing struct breakpoint. */
11528 if (bl_address_is_meaningful (old_loc))
11529 {
11530 for (size_t loc2_i = loc_i;
11531 (loc2_i < bp_locations.size ()
11532 && bp_locations[loc2_i]->address == old_loc->address);
11533 loc2_i++)
11534 {
11535 bp_location *loc2 = bp_locations[loc2_i];
11536
11537 if (loc2 == old_loc)
11538 continue;
11539
11540 if (breakpoint_locations_match (loc2, old_loc))
11541 {
11542 /* Read watchpoint locations are switched to
11543 access watchpoints, if the former are not
11544 supported, but the latter are. */
11545 if (is_hardware_watchpoint (old_loc->owner))
11546 {
11547 gdb_assert (is_hardware_watchpoint (loc2->owner));
11548 loc2->watchpoint_type = old_loc->watchpoint_type;
11549 }
11550
11551 /* loc2 is a duplicated location. We need to check
11552 if it should be inserted in case it will be
11553 unduplicated. */
11554 if (unduplicated_should_be_inserted (loc2))
11555 {
11556 swap_insertion (old_loc, loc2);
11557 keep_in_target = true;
11558 break;
11559 }
11560 }
11561 }
11562 }
11563 }
11564
11565 if (!keep_in_target)
11566 {
11567 if (remove_breakpoint (old_loc))
11568 {
11569 /* This is just about all we can do. We could keep
11570 this location on the global list, and try to
11571 remove it next time, but there's no particular
11572 reason why we will succeed next time.
11573
11574 Note that at this point, old_loc->owner is still
11575 valid, as delete_breakpoint frees the breakpoint
11576 only after calling us. */
11577 warning (_("error removing breakpoint %d at %s"),
11578 old_loc->owner->number,
11579 paddress (old_loc->gdbarch, old_loc->address));
11580 }
11581 removed = true;
11582 }
11583 }
11584
11585 if (!found_object)
11586 {
11587 if (removed && target_is_non_stop_p ()
11588 && need_moribund_for_location_type (old_loc))
11589 {
11590 /* This location was removed from the target. In
11591 non-stop mode, a race condition is possible where
11592 we've removed a breakpoint, but stop events for that
11593 breakpoint are already queued and will arrive later.
11594 We apply an heuristic to be able to distinguish such
11595 SIGTRAPs from other random SIGTRAPs: we keep this
11596 breakpoint location for a bit, and will retire it
11597 after we see some number of events. The theory here
11598 is that reporting of events should, "on the average",
11599 be fair, so after a while we'll see events from all
11600 threads that have anything of interest, and no longer
11601 need to keep this breakpoint location around. We
11602 don't hold locations forever so to reduce chances of
11603 mistaking a non-breakpoint SIGTRAP for a breakpoint
11604 SIGTRAP.
11605
11606 The heuristic failing can be disastrous on
11607 decr_pc_after_break targets.
11608
11609 On decr_pc_after_break targets, like e.g., x86-linux,
11610 if we fail to recognize a late breakpoint SIGTRAP,
11611 because events_till_retirement has reached 0 too
11612 soon, we'll fail to do the PC adjustment, and report
11613 a random SIGTRAP to the user. When the user resumes
11614 the inferior, it will most likely immediately crash
11615 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11616 corrupted, because of being resumed e.g., in the
11617 middle of a multi-byte instruction, or skipped a
11618 one-byte instruction. This was actually seen happen
11619 on native x86-linux, and should be less rare on
11620 targets that do not support new thread events, like
11621 remote, due to the heuristic depending on
11622 thread_count.
11623
11624 Mistaking a random SIGTRAP for a breakpoint trap
11625 causes similar symptoms (PC adjustment applied when
11626 it shouldn't), but then again, playing with SIGTRAPs
11627 behind the debugger's back is asking for trouble.
11628
11629 Since hardware watchpoint traps are always
11630 distinguishable from other traps, so we don't need to
11631 apply keep hardware watchpoint moribund locations
11632 around. We simply always ignore hardware watchpoint
11633 traps we can no longer explain. */
11634
11635 process_stratum_target *proc_target = nullptr;
11636 for (inferior *inf : all_inferiors ())
11637 if (inf->pspace == old_loc->pspace)
11638 {
11639 proc_target = inf->process_target ();
11640 break;
11641 }
11642 if (proc_target != nullptr)
11643 old_loc->events_till_retirement
11644 = 3 * (thread_count (proc_target) + 1);
11645 else
11646 old_loc->events_till_retirement = 1;
11647 old_loc->owner = NULL;
11648
11649 moribund_locations.push_back (old_loc);
11650 }
11651 else
11652 {
11653 old_loc->owner = NULL;
11654 decref_bp_location (&old_loc);
11655 }
11656 }
11657 }
11658
11659 /* Rescan breakpoints at the same address and section, marking the
11660 first one as "first" and any others as "duplicates". This is so
11661 that the bpt instruction is only inserted once. If we have a
11662 permanent breakpoint at the same place as BPT, make that one the
11663 official one, and the rest as duplicates. Permanent breakpoints
11664 are sorted first for the same address.
11665
11666 Do the same for hardware watchpoints, but also considering the
11667 watchpoint's type (regular/access/read) and length. */
11668
11669 bp_loc_first = NULL;
11670 wp_loc_first = NULL;
11671 awp_loc_first = NULL;
11672 rwp_loc_first = NULL;
11673
11674 for (bp_location *loc : all_bp_locations ())
11675 {
11676 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11677 non-NULL. */
11678 struct bp_location **loc_first_p;
11679 breakpoint *b = loc->owner;
11680
11681 if (!unduplicated_should_be_inserted (loc)
11682 || !bl_address_is_meaningful (loc)
11683 /* Don't detect duplicate for tracepoint locations because they are
11684 never duplicated. See the comments in field `duplicate' of
11685 `struct bp_location'. */
11686 || is_tracepoint (b))
11687 {
11688 /* Clear the condition modification flag. */
11689 loc->condition_changed = condition_unchanged;
11690 continue;
11691 }
11692
11693 if (b->type == bp_hardware_watchpoint)
11694 loc_first_p = &wp_loc_first;
11695 else if (b->type == bp_read_watchpoint)
11696 loc_first_p = &rwp_loc_first;
11697 else if (b->type == bp_access_watchpoint)
11698 loc_first_p = &awp_loc_first;
11699 else
11700 loc_first_p = &bp_loc_first;
11701
11702 if (*loc_first_p == NULL
11703 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11704 || !breakpoint_locations_match (loc, *loc_first_p))
11705 {
11706 *loc_first_p = loc;
11707 loc->duplicate = 0;
11708
11709 if (is_breakpoint (loc->owner) && loc->condition_changed)
11710 {
11711 loc->needs_update = 1;
11712 /* Clear the condition modification flag. */
11713 loc->condition_changed = condition_unchanged;
11714 }
11715 continue;
11716 }
11717
11718
11719 /* This and the above ensure the invariant that the first location
11720 is not duplicated, and is the inserted one.
11721 All following are marked as duplicated, and are not inserted. */
11722 if (loc->inserted)
11723 swap_insertion (loc, *loc_first_p);
11724 loc->duplicate = 1;
11725
11726 /* Clear the condition modification flag. */
11727 loc->condition_changed = condition_unchanged;
11728 }
11729
11730 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11731 {
11732 if (insert_mode != UGLL_DONT_INSERT)
11733 insert_breakpoint_locations ();
11734 else
11735 {
11736 /* Even though the caller told us to not insert new
11737 locations, we may still need to update conditions on the
11738 target's side of breakpoints that were already inserted
11739 if the target is evaluating breakpoint conditions. We
11740 only update conditions for locations that are marked
11741 "needs_update". */
11742 update_inserted_breakpoint_locations ();
11743 }
11744 }
11745
11746 if (insert_mode != UGLL_DONT_INSERT)
11747 download_tracepoint_locations ();
11748 }
11749
11750 void
breakpoint_retire_moribund(void)11751 breakpoint_retire_moribund (void)
11752 {
11753 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11754 {
11755 struct bp_location *loc = moribund_locations[ix];
11756 if (--(loc->events_till_retirement) == 0)
11757 {
11758 decref_bp_location (&loc);
11759 unordered_remove (moribund_locations, ix);
11760 --ix;
11761 }
11762 }
11763 }
11764
11765 static void
update_global_location_list_nothrow(enum ugll_insert_mode insert_mode)11766 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11767 {
11768
11769 try
11770 {
11771 update_global_location_list (insert_mode);
11772 }
11773 catch (const gdb_exception_error &e)
11774 {
11775 }
11776 }
11777
11778 /* Clear BKP from a BPS. */
11779
11780 static void
bpstat_remove_bp_location(bpstat * bps,struct breakpoint * bpt)11781 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
11782 {
11783 bpstat *bs;
11784
11785 for (bs = bps; bs; bs = bs->next)
11786 if (bs->breakpoint_at == bpt)
11787 {
11788 bs->breakpoint_at = NULL;
11789 bs->old_val = NULL;
11790 /* bs->commands will be freed later. */
11791 }
11792 }
11793
11794 /* Callback for iterate_over_threads. */
11795 static int
bpstat_remove_breakpoint_callback(struct thread_info * th,void * data)11796 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11797 {
11798 struct breakpoint *bpt = (struct breakpoint *) data;
11799
11800 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11801 return 0;
11802 }
11803
11804 /* See breakpoint.h. */
11805
11806 void
say_where()11807 code_breakpoint::say_where () const
11808 {
11809 struct value_print_options opts;
11810
11811 get_user_print_options (&opts);
11812
11813 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11814 single string. */
11815 if (!this->has_locations ())
11816 {
11817 /* For pending locations, the output differs slightly based
11818 on extra_string. If this is non-NULL, it contains either
11819 a condition or dprintf arguments. */
11820 if (extra_string == NULL)
11821 {
11822 gdb_printf (_(" (%s) pending."), locspec->to_string ());
11823 }
11824 else if (type == bp_dprintf)
11825 {
11826 gdb_printf (_(" (%s,%s) pending."),
11827 locspec->to_string (),
11828 extra_string.get ());
11829 }
11830 else
11831 {
11832 gdb_printf (_(" (%s %s) pending."),
11833 locspec->to_string (),
11834 extra_string.get ());
11835 }
11836 }
11837 else
11838 {
11839 const bp_location &bl = this->first_loc ();
11840 if (opts.addressprint || bl.symtab == nullptr)
11841 gdb_printf (" at %ps",
11842 styled_string (address_style.style (),
11843 paddress (bl.gdbarch,
11844 bl.address)));
11845 if (bl.symtab != NULL)
11846 {
11847 /* If there is a single location, we can print the location
11848 more nicely. */
11849 if (!this->has_multiple_locations ())
11850 {
11851 const char *filename
11852 = symtab_to_filename_for_display (bl.symtab);
11853 gdb_printf (": file %ps, line %d.",
11854 styled_string (file_name_style.style (),
11855 filename),
11856 bl.line_number);
11857 }
11858 else
11859 /* This is not ideal, but each location may have a
11860 different file name, and this at least reflects the
11861 real situation somewhat. */
11862 gdb_printf (": %s.", locspec->to_string ());
11863 }
11864
11865 if (this->has_multiple_locations ())
11866 {
11867 int n = std::distance (m_locations.begin (), m_locations.end ());
11868 gdb_printf (" (%d locations)", n);
11869 }
11870 }
11871 }
11872
11873 /* See breakpoint.h. */
11874
locations()11875 bp_location_range breakpoint::locations () const
11876 {
11877 return bp_location_range (m_locations.begin (), m_locations.end ());
11878 }
11879
11880 struct bp_location *
allocate_location()11881 breakpoint::allocate_location ()
11882 {
11883 return new bp_location (this);
11884 }
11885
11886 /* See breakpoint.h. */
11887
11888 void
add_location(bp_location & loc)11889 breakpoint::add_location (bp_location &loc)
11890 {
11891 gdb_assert (loc.owner == this);
11892 gdb_assert (!loc.is_linked ());
11893
11894 auto ub = std::upper_bound (m_locations.begin (), m_locations.end (),
11895 loc,
11896 [] (const bp_location &left,
11897 const bp_location &right)
11898 { return left.address < right.address; });
11899 m_locations.insert (ub, loc);
11900 }
11901
11902 /* See breakpoint.h. */
11903
11904 void
unadd_location(bp_location & loc)11905 breakpoint::unadd_location (bp_location &loc)
11906 {
11907 gdb_assert (loc.owner == this);
11908 gdb_assert (loc.is_linked ());
11909
11910 m_locations.erase (m_locations.iterator_to (loc));
11911 }
11912
11913 #define internal_error_pure_virtual_called() \
11914 gdb_assert_not_reached ("pure virtual function called")
11915
11916 int
insert_location(struct bp_location * bl)11917 breakpoint::insert_location (struct bp_location *bl)
11918 {
11919 internal_error_pure_virtual_called ();
11920 }
11921
11922 int
remove_location(struct bp_location * bl,enum remove_bp_reason reason)11923 breakpoint::remove_location (struct bp_location *bl,
11924 enum remove_bp_reason reason)
11925 {
11926 internal_error_pure_virtual_called ();
11927 }
11928
11929 int
breakpoint_hit(const struct bp_location * bl,const address_space * aspace,CORE_ADDR bp_addr,const target_waitstatus & ws)11930 breakpoint::breakpoint_hit (const struct bp_location *bl,
11931 const address_space *aspace,
11932 CORE_ADDR bp_addr,
11933 const target_waitstatus &ws)
11934 {
11935 internal_error_pure_virtual_called ();
11936 }
11937
11938 int
resources_needed(const struct bp_location * bl)11939 breakpoint::resources_needed (const struct bp_location *bl)
11940 {
11941 internal_error_pure_virtual_called ();
11942 }
11943
11944 enum print_stop_action
print_it(const bpstat * bs)11945 breakpoint::print_it (const bpstat *bs) const
11946 {
11947 internal_error_pure_virtual_called ();
11948 }
11949
11950 void
print_mention()11951 breakpoint::print_mention () const
11952 {
11953 internal_error_pure_virtual_called ();
11954 }
11955
11956 void
print_recreate(struct ui_file * fp)11957 breakpoint::print_recreate (struct ui_file *fp) const
11958 {
11959 internal_error_pure_virtual_called ();
11960 }
11961
11962 /* Default breakpoint_ops methods. */
11963
11964 void
re_set()11965 code_breakpoint::re_set ()
11966 {
11967 /* FIXME: is this still reachable? */
11968 if (breakpoint_location_spec_empty_p (this))
11969 {
11970 /* Anything without a location can't be re-set. */
11971 delete_breakpoint (this);
11972 return;
11973 }
11974
11975 re_set_default ();
11976 }
11977
11978 int
insert_location(struct bp_location * bl)11979 code_breakpoint::insert_location (struct bp_location *bl)
11980 {
11981 CORE_ADDR addr = bl->target_info.reqstd_address;
11982
11983 bl->target_info.kind = breakpoint_kind (bl, &addr);
11984 bl->target_info.placed_address = addr;
11985
11986 int result;
11987 if (bl->loc_type == bp_loc_hardware_breakpoint)
11988 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
11989 else
11990 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11991
11992 if (result == 0 && bl->probe.prob != nullptr)
11993 {
11994 /* The insertion was successful, now let's set the probe's semaphore
11995 if needed. */
11996 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11997 }
11998
11999 return result;
12000 }
12001
12002 int
remove_location(struct bp_location * bl,enum remove_bp_reason reason)12003 code_breakpoint::remove_location (struct bp_location *bl,
12004 enum remove_bp_reason reason)
12005 {
12006 if (bl->probe.prob != nullptr)
12007 {
12008 /* Let's clear the semaphore before removing the location. */
12009 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12010 }
12011
12012 if (bl->loc_type == bp_loc_hardware_breakpoint)
12013 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12014 else
12015 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12016 }
12017
12018 int
breakpoint_hit(const struct bp_location * bl,const address_space * aspace,CORE_ADDR bp_addr,const target_waitstatus & ws)12019 code_breakpoint::breakpoint_hit (const struct bp_location *bl,
12020 const address_space *aspace,
12021 CORE_ADDR bp_addr,
12022 const target_waitstatus &ws)
12023 {
12024 if (ws.kind () != TARGET_WAITKIND_STOPPED
12025 || ws.sig () != GDB_SIGNAL_TRAP)
12026 return 0;
12027
12028 if (!breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
12029 aspace, bp_addr))
12030 return 0;
12031
12032 if (overlay_debugging /* unmapped overlay section */
12033 && section_is_overlay (bl->section)
12034 && !section_is_mapped (bl->section))
12035 return 0;
12036
12037 return 1;
12038 }
12039
12040 int
breakpoint_hit(const struct bp_location * bl,const address_space * aspace,CORE_ADDR bp_addr,const target_waitstatus & ws)12041 dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
12042 const address_space *aspace,
12043 CORE_ADDR bp_addr,
12044 const target_waitstatus &ws)
12045 {
12046 if (dprintf_style == dprintf_style_agent
12047 && target_can_run_breakpoint_commands ())
12048 {
12049 /* An agent-style dprintf never causes a stop. If we see a trap
12050 for this address it must be for a breakpoint that happens to
12051 be set at the same address. */
12052 return 0;
12053 }
12054
12055 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
12056 }
12057
12058 int
resources_needed(const struct bp_location * bl)12059 ordinary_breakpoint::resources_needed (const struct bp_location *bl)
12060 {
12061 gdb_assert (type == bp_hardware_breakpoint);
12062
12063 return 1;
12064 }
12065
12066 enum print_stop_action
print_it(const bpstat * bs)12067 ordinary_breakpoint::print_it (const bpstat *bs) const
12068 {
12069 const struct bp_location *bl;
12070 int bp_temp;
12071 struct ui_out *uiout = current_uiout;
12072
12073 bl = bs->bp_location_at.get ();
12074
12075 bp_temp = disposition == disp_del;
12076 if (bl->address != bl->requested_address)
12077 breakpoint_adjustment_warning (bl->requested_address,
12078 bl->address,
12079 number, true);
12080 annotate_breakpoint (number);
12081 maybe_print_thread_hit_breakpoint (uiout);
12082
12083 if (uiout->is_mi_like_p ())
12084 {
12085 uiout->field_string ("reason",
12086 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12087 uiout->field_string ("disp", bpdisp_text (disposition));
12088 }
12089
12090 if (bp_temp)
12091 uiout->text ("Temporary breakpoint ");
12092 else
12093 uiout->text ("Breakpoint ");
12094 print_num_locno (bs, uiout);
12095 uiout->text (", ");
12096
12097 return PRINT_SRC_AND_LOC;
12098 }
12099
12100 void
print_mention()12101 ordinary_breakpoint::print_mention () const
12102 {
12103 if (current_uiout->is_mi_like_p ())
12104 return;
12105
12106 switch (type)
12107 {
12108 case bp_breakpoint:
12109 case bp_gnu_ifunc_resolver:
12110 if (disposition == disp_del)
12111 gdb_printf (_("Temporary breakpoint"));
12112 else
12113 gdb_printf (_("Breakpoint"));
12114 gdb_printf (_(" %d"), number);
12115 if (type == bp_gnu_ifunc_resolver)
12116 gdb_printf (_(" at gnu-indirect-function resolver"));
12117 break;
12118 case bp_hardware_breakpoint:
12119 gdb_printf (_("Hardware assisted breakpoint %d"), number);
12120 break;
12121 case bp_dprintf:
12122 gdb_printf (_("Dprintf %d"), number);
12123 break;
12124 }
12125
12126 say_where ();
12127 }
12128
12129 void
print_recreate(struct ui_file * fp)12130 ordinary_breakpoint::print_recreate (struct ui_file *fp) const
12131 {
12132 if (type == bp_breakpoint && disposition == disp_del)
12133 gdb_printf (fp, "tbreak");
12134 else if (type == bp_breakpoint)
12135 gdb_printf (fp, "break");
12136 else if (type == bp_hardware_breakpoint
12137 && disposition == disp_del)
12138 gdb_printf (fp, "thbreak");
12139 else if (type == bp_hardware_breakpoint)
12140 gdb_printf (fp, "hbreak");
12141 else
12142 internal_error (_("unhandled breakpoint type %d"), (int) type);
12143
12144 gdb_printf (fp, " %s", locspec->to_string ());
12145
12146 /* Print out extra_string if this breakpoint is pending. It might
12147 contain, for example, conditions that were set by the user. */
12148 if (!this->has_locations () && extra_string != NULL)
12149 gdb_printf (fp, " %s", extra_string.get ());
12150
12151 print_recreate_thread (fp);
12152 }
12153
12154 std::vector<symtab_and_line>
decode_location_spec(location_spec * locspec,program_space * search_pspace)12155 code_breakpoint::decode_location_spec (location_spec *locspec,
12156 program_space *search_pspace)
12157 {
12158 if (locspec->type () == PROBE_LOCATION_SPEC)
12159 return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
12160
12161 struct linespec_result canonical;
12162
12163 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
12164 NULL, 0, &canonical, multiple_symbols_all,
12165 filter.get ());
12166
12167 /* We should get 0 or 1 resulting SALs. */
12168 gdb_assert (canonical.lsals.size () < 2);
12169
12170 if (!canonical.lsals.empty ())
12171 {
12172 const linespec_sals &lsal = canonical.lsals[0];
12173 return std::move (lsal.sals);
12174 }
12175 return {};
12176 }
12177
12178 /* Virtual table for internal breakpoints. */
12179
12180 void
re_set()12181 internal_breakpoint::re_set ()
12182 {
12183 switch (type)
12184 {
12185 /* Delete overlay event and longjmp master breakpoints; they
12186 will be reset later by breakpoint_re_set. */
12187 case bp_overlay_event:
12188 case bp_longjmp_master:
12189 case bp_std_terminate_master:
12190 case bp_exception_master:
12191 delete_breakpoint (this);
12192 break;
12193
12194 /* This breakpoint is special, it's set up when the inferior
12195 starts and we really don't want to touch it. */
12196 case bp_shlib_event:
12197
12198 /* Like bp_shlib_event, this breakpoint type is special. Once
12199 it is set up, we do not want to touch it. */
12200 case bp_thread_event:
12201 break;
12202 }
12203 }
12204
12205 void
check_status(bpstat * bs)12206 internal_breakpoint::check_status (bpstat *bs)
12207 {
12208 if (type == bp_shlib_event)
12209 {
12210 /* If requested, stop when the dynamic linker notifies GDB of
12211 events. This allows the user to get control and place
12212 breakpoints in initializer routines for dynamically loaded
12213 objects (among other things). */
12214 bs->stop = stop_on_solib_events != 0;
12215 bs->print = stop_on_solib_events != 0;
12216 }
12217 else
12218 bs->stop = false;
12219 }
12220
12221 enum print_stop_action
print_it(const bpstat * bs)12222 internal_breakpoint::print_it (const bpstat *bs) const
12223 {
12224 switch (type)
12225 {
12226 case bp_shlib_event:
12227 /* Did we stop because the user set the stop_on_solib_events
12228 variable? (If so, we report this as a generic, "Stopped due
12229 to shlib event" message.) */
12230 print_solib_event (false);
12231 break;
12232
12233 case bp_thread_event:
12234 /* Not sure how we will get here.
12235 GDB should not stop for these breakpoints. */
12236 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
12237 break;
12238
12239 case bp_overlay_event:
12240 /* By analogy with the thread event, GDB should not stop for these. */
12241 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12242 break;
12243
12244 case bp_longjmp_master:
12245 /* These should never be enabled. */
12246 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12247 break;
12248
12249 case bp_std_terminate_master:
12250 /* These should never be enabled. */
12251 gdb_printf (_("std::terminate Master Breakpoint: "
12252 "gdb should not stop!\n"));
12253 break;
12254
12255 case bp_exception_master:
12256 /* These should never be enabled. */
12257 gdb_printf (_("Exception Master Breakpoint: "
12258 "gdb should not stop!\n"));
12259 break;
12260 }
12261
12262 return PRINT_NOTHING;
12263 }
12264
12265 void
print_mention()12266 internal_breakpoint::print_mention () const
12267 {
12268 /* Nothing to mention. These breakpoints are internal. */
12269 }
12270
12271 /* Virtual table for momentary breakpoints */
12272
12273 void
re_set()12274 momentary_breakpoint::re_set ()
12275 {
12276 /* Keep temporary breakpoints, which can be encountered when we step
12277 over a dlopen call and solib_add is resetting the breakpoints.
12278 Otherwise these should have been blown away via the cleanup chain
12279 or by breakpoint_init_inferior when we rerun the executable. */
12280 }
12281
12282 void
check_status(bpstat * bs)12283 momentary_breakpoint::check_status (bpstat *bs)
12284 {
12285 /* Nothing. The point of these breakpoints is causing a stop. */
12286 }
12287
12288 enum print_stop_action
print_it(const bpstat * bs)12289 momentary_breakpoint::print_it (const bpstat *bs) const
12290 {
12291 return PRINT_UNKNOWN;
12292 }
12293
12294 void
print_mention()12295 momentary_breakpoint::print_mention () const
12296 {
12297 /* Nothing to mention. These breakpoints are internal. */
12298 }
12299
12300 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12301
12302 It gets cleared already on the removal of the first one of such placed
12303 breakpoints. This is OK as they get all removed altogether. */
12304
~longjmp_breakpoint()12305 longjmp_breakpoint::~longjmp_breakpoint ()
12306 {
12307 thread_info *tp = find_thread_global_id (this->thread);
12308
12309 if (tp != NULL)
12310 tp->initiating_frame = null_frame_id;
12311 }
12312
12313 static void
bkpt_probe_create_sals_from_location_spec(location_spec * locspec,struct linespec_result * canonical)12314 bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
12315 struct linespec_result *canonical)
12316
12317 {
12318 struct linespec_sals lsal;
12319
12320 lsal.sals = parse_probes (locspec, NULL, canonical);
12321 lsal.canonical = xstrdup (canonical->locspec->to_string ());
12322 canonical->lsals.push_back (std::move (lsal));
12323 }
12324
12325 static std::vector<symtab_and_line>
bkpt_probe_decode_location_spec(struct breakpoint * b,location_spec * locspec,program_space * search_pspace)12326 bkpt_probe_decode_location_spec (struct breakpoint *b,
12327 location_spec *locspec,
12328 program_space *search_pspace)
12329 {
12330 std::vector<symtab_and_line> sals
12331 = parse_probes (locspec, search_pspace, NULL);
12332 if (sals.empty ())
12333 error (_("probe not found"));
12334 return sals;
12335 }
12336
12337 int
breakpoint_hit(const struct bp_location * bl,const address_space * aspace,CORE_ADDR bp_addr,const target_waitstatus & ws)12338 tracepoint::breakpoint_hit (const struct bp_location *bl,
12339 const address_space *aspace, CORE_ADDR bp_addr,
12340 const target_waitstatus &ws)
12341 {
12342 /* By definition, the inferior does not report stops at
12343 tracepoints. */
12344 return 0;
12345 }
12346
12347 void
print_one_detail(struct ui_out * uiout)12348 tracepoint::print_one_detail (struct ui_out *uiout) const
12349 {
12350 if (!static_trace_marker_id.empty ())
12351 {
12352 gdb_assert (type == bp_static_tracepoint
12353 || type == bp_static_marker_tracepoint);
12354
12355 uiout->message ("\tmarker id is %pF\n",
12356 string_field ("static-tracepoint-marker-string-id",
12357 static_trace_marker_id.c_str ()));
12358 }
12359 }
12360
12361 void
print_mention()12362 tracepoint::print_mention () const
12363 {
12364 if (current_uiout->is_mi_like_p ())
12365 return;
12366
12367 switch (type)
12368 {
12369 case bp_tracepoint:
12370 gdb_printf (_("Tracepoint"));
12371 gdb_printf (_(" %d"), number);
12372 break;
12373 case bp_fast_tracepoint:
12374 gdb_printf (_("Fast tracepoint"));
12375 gdb_printf (_(" %d"), number);
12376 break;
12377 case bp_static_tracepoint:
12378 case bp_static_marker_tracepoint:
12379 gdb_printf (_("Static tracepoint"));
12380 gdb_printf (_(" %d"), number);
12381 break;
12382 default:
12383 internal_error (_("unhandled tracepoint type %d"), (int) type);
12384 }
12385
12386 say_where ();
12387 }
12388
12389 void
print_recreate(struct ui_file * fp)12390 tracepoint::print_recreate (struct ui_file *fp) const
12391 {
12392 if (type == bp_fast_tracepoint)
12393 gdb_printf (fp, "ftrace");
12394 else if (type == bp_static_tracepoint
12395 || type == bp_static_marker_tracepoint)
12396 gdb_printf (fp, "strace");
12397 else if (type == bp_tracepoint)
12398 gdb_printf (fp, "trace");
12399 else
12400 internal_error (_("unhandled tracepoint type %d"), (int) type);
12401
12402 gdb_printf (fp, " %s", locspec->to_string ());
12403 print_recreate_thread (fp);
12404
12405 if (pass_count)
12406 gdb_printf (fp, " passcount %d\n", pass_count);
12407 }
12408
12409 /* Virtual table for tracepoints on static probes. */
12410
12411 static void
tracepoint_probe_create_sals_from_location_spec(location_spec * locspec,struct linespec_result * canonical)12412 tracepoint_probe_create_sals_from_location_spec
12413 (location_spec *locspec,
12414 struct linespec_result *canonical)
12415 {
12416 /* We use the same method for breakpoint on probes. */
12417 bkpt_probe_create_sals_from_location_spec (locspec, canonical);
12418 }
12419
12420 void
re_set()12421 dprintf_breakpoint::re_set ()
12422 {
12423 re_set_default ();
12424
12425 /* 1 - connect to target 1, that can run breakpoint commands.
12426 2 - create a dprintf, which resolves fine.
12427 3 - disconnect from target 1
12428 4 - connect to target 2, that can NOT run breakpoint commands.
12429
12430 After steps #3/#4, you'll want the dprintf command list to
12431 be updated, because target 1 and 2 may well return different
12432 answers for target_can_run_breakpoint_commands().
12433 Given absence of finer grained resetting, we get to do
12434 it all the time. */
12435 update_dprintf_command_list (this);
12436 }
12437
12438 /* Implement the "print_recreate" method for dprintf. */
12439
12440 void
print_recreate(struct ui_file * fp)12441 dprintf_breakpoint::print_recreate (struct ui_file *fp) const
12442 {
12443 gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
12444 print_recreate_thread (fp);
12445 }
12446
12447 /* Implement the "after_condition_true" method for dprintf.
12448
12449 dprintf's are implemented with regular commands in their command
12450 list, but we run the commands here instead of before presenting the
12451 stop to the user, as dprintf's don't actually cause a stop. This
12452 also makes it so that the commands of multiple dprintfs at the same
12453 address are all handled. */
12454
12455 void
after_condition_true(struct bpstat * bs)12456 dprintf_breakpoint::after_condition_true (struct bpstat *bs)
12457 {
12458 /* dprintf's never cause a stop. This wasn't set in the
12459 check_status hook instead because that would make the dprintf's
12460 condition not be evaluated. */
12461 bs->stop = false;
12462
12463 /* Run the command list here. Take ownership of it instead of
12464 copying. We never want these commands to run later in
12465 bpstat_do_actions, if a breakpoint that causes a stop happens to
12466 be set at same address as this dprintf, or even if running the
12467 commands here throws. */
12468 counted_command_line cmds = std::move (bs->commands);
12469 gdb_assert (cmds != nullptr);
12470 execute_control_commands (cmds.get (), 0);
12471 }
12472
12473 /* The breakpoint_ops structure to be used on static tracepoints with
12474 markers (`-m'). */
12475
12476 static void
strace_marker_create_sals_from_location_spec(location_spec * locspec,struct linespec_result * canonical)12477 strace_marker_create_sals_from_location_spec (location_spec *locspec,
12478 struct linespec_result *canonical)
12479 {
12480 struct linespec_sals lsal;
12481 const char *arg_start, *arg;
12482
12483 arg = arg_start = as_linespec_location_spec (locspec)->spec_string.get ();
12484 lsal.sals = decode_static_tracepoint_spec (&arg);
12485
12486 std::string str (arg_start, arg - arg_start);
12487 const char *ptr = str.c_str ();
12488 canonical->locspec
12489 = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
12490
12491 lsal.canonical = xstrdup (canonical->locspec->to_string ());
12492 canonical->lsals.push_back (std::move (lsal));
12493 }
12494
12495 static void
strace_marker_create_breakpoints_sal(struct gdbarch * gdbarch,struct linespec_result * canonical,gdb::unique_xmalloc_ptr<char> cond_string,gdb::unique_xmalloc_ptr<char> extra_string,enum bptype type_wanted,enum bpdisp disposition,int thread,int task,int inferior,int ignore_count,int from_tty,int enabled,int internal,unsigned flags)12496 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12497 struct linespec_result *canonical,
12498 gdb::unique_xmalloc_ptr<char> cond_string,
12499 gdb::unique_xmalloc_ptr<char> extra_string,
12500 enum bptype type_wanted,
12501 enum bpdisp disposition,
12502 int thread,
12503 int task, int inferior,
12504 int ignore_count,
12505 int from_tty, int enabled,
12506 int internal, unsigned flags)
12507 {
12508 const linespec_sals &lsal = canonical->lsals[0];
12509
12510 /* If the user is creating a static tracepoint by marker id
12511 (strace -m MARKER_ID), then store the sals index, so that
12512 breakpoint_re_set can try to match up which of the newly
12513 found markers corresponds to this one, and, don't try to
12514 expand multiple locations for each sal, given than SALS
12515 already should contain all sals for MARKER_ID. */
12516
12517 for (size_t i = 0; i < lsal.sals.size (); i++)
12518 {
12519 location_spec_up locspec = canonical->locspec->clone ();
12520
12521 std::unique_ptr<tracepoint> tp
12522 (new tracepoint (gdbarch,
12523 type_wanted,
12524 lsal.sals[i],
12525 std::move (locspec),
12526 NULL,
12527 std::move (cond_string),
12528 std::move (extra_string),
12529 disposition,
12530 thread, task, inferior, ignore_count,
12531 from_tty, enabled, flags,
12532 canonical->special_display));
12533
12534 /* Given that its possible to have multiple markers with
12535 the same string id, if the user is creating a static
12536 tracepoint by marker id ("strace -m MARKER_ID"), then
12537 store the sals index, so that breakpoint_re_set can
12538 try to match up which of the newly found markers
12539 corresponds to this one */
12540 tp->static_trace_marker_id_idx = i;
12541
12542 install_breakpoint (internal, std::move (tp), 0);
12543 }
12544 }
12545
12546 std::vector<symtab_and_line>
decode_location_spec(location_spec * locspec,program_space * search_pspace)12547 static_marker_tracepoint::decode_location_spec (location_spec *locspec,
12548 program_space *search_pspace)
12549 {
12550 const char *s = as_linespec_location_spec (locspec)->spec_string.get ();
12551
12552 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12553 if (sals.size () > static_trace_marker_id_idx)
12554 {
12555 sals[0] = sals[static_trace_marker_id_idx];
12556 sals.resize (1);
12557 return sals;
12558 }
12559 else
12560 error (_("marker %s not found"), static_trace_marker_id.c_str ());
12561 }
12562
12563 /* Static tracepoints with marker (`-m'). */
12564 static struct breakpoint_ops strace_marker_breakpoint_ops =
12565 {
12566 strace_marker_create_sals_from_location_spec,
12567 strace_marker_create_breakpoints_sal,
12568 };
12569
12570 static bool
strace_marker_p(struct breakpoint * b)12571 strace_marker_p (struct breakpoint *b)
12572 {
12573 return b->type == bp_static_marker_tracepoint;
12574 }
12575
12576 /* Notify interpreters and observers that breakpoint B was deleted. */
12577
12578 static void
notify_breakpoint_deleted(breakpoint * b)12579 notify_breakpoint_deleted (breakpoint *b)
12580 {
12581 interps_notify_breakpoint_deleted (b);
12582 gdb::observers::breakpoint_deleted.notify (b);
12583 }
12584
12585 /* Delete a breakpoint and clean up all traces of it in the data
12586 structures. */
12587
12588 void
delete_breakpoint(struct breakpoint * bpt)12589 delete_breakpoint (struct breakpoint *bpt)
12590 {
12591 gdb_assert (bpt != NULL);
12592
12593 /* Has this bp already been deleted? This can happen because
12594 multiple lists can hold pointers to bp's. bpstat lists are
12595 especial culprits.
12596
12597 One example of this happening is a watchpoint's scope bp. When
12598 the scope bp triggers, we notice that the watchpoint is out of
12599 scope, and delete it. We also delete its scope bp. But the
12600 scope bp is marked "auto-deleting", and is already on a bpstat.
12601 That bpstat is then checked for auto-deleting bp's, which are
12602 deleted.
12603
12604 A real solution to this problem might involve reference counts in
12605 bp's, and/or giving them pointers back to their referencing
12606 bpstat's, and teaching delete_breakpoint to only free a bp's
12607 storage when no more references were extent. A cheaper bandaid
12608 was chosen. */
12609 if (bpt->type == bp_none)
12610 return;
12611
12612 /* At least avoid this stale reference until the reference counting
12613 of breakpoints gets resolved. */
12614 if (bpt->related_breakpoint != bpt)
12615 {
12616 struct breakpoint *related;
12617 struct watchpoint *w;
12618
12619 if (bpt->type == bp_watchpoint_scope)
12620 w = gdb::checked_static_cast<watchpoint *> (bpt->related_breakpoint);
12621 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12622 w = gdb::checked_static_cast<watchpoint *> (bpt);
12623 else
12624 w = NULL;
12625 if (w != NULL)
12626 watchpoint_del_at_next_stop (w);
12627
12628 /* Unlink bpt from the bpt->related_breakpoint ring. */
12629 for (related = bpt; related->related_breakpoint != bpt;
12630 related = related->related_breakpoint);
12631 related->related_breakpoint = bpt->related_breakpoint;
12632 bpt->related_breakpoint = bpt;
12633 }
12634
12635 /* watch_command_1 creates a watchpoint but only sets its number if
12636 update_watchpoint succeeds in creating its bp_locations. If there's
12637 a problem in that process, we'll be asked to delete the half-created
12638 watchpoint. In that case, don't announce the deletion. */
12639 if (bpt->number)
12640 notify_breakpoint_deleted (bpt);
12641
12642 breakpoint_chain.erase (breakpoint_chain.iterator_to (*bpt));
12643
12644 /* Be sure no bpstat's are pointing at the breakpoint after it's
12645 been freed. */
12646 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12647 in all threads for now. Note that we cannot just remove bpstats
12648 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12649 commands are associated with the bpstat; if we remove it here,
12650 then the later call to bpstat_do_actions (&stop_bpstat); in
12651 event-top.c won't do anything, and temporary breakpoints with
12652 commands won't work. */
12653
12654 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12655
12656 /* Now that breakpoint is removed from breakpoint list, update the
12657 global location list. This will remove locations that used to
12658 belong to this breakpoint. Do this before freeing the breakpoint
12659 itself, since remove_breakpoint looks at location's owner. It
12660 might be better design to have location completely
12661 self-contained, but it's not the case now.
12662
12663 Clear the location linked list first, otherwise, the intrusive_list
12664 destructor accesses the locations after they are freed. */
12665 bpt->clear_locations ();
12666 update_global_location_list (UGLL_DONT_INSERT);
12667
12668 /* On the chance that someone will soon try again to delete this
12669 same bp, we mark it as deleted before freeing its storage. */
12670 bpt->type = bp_none;
12671 delete bpt;
12672 }
12673
12674 /* Iterator function to call a user-provided callback function once
12675 for each of B and its related breakpoints. */
12676
12677 static void
iterate_over_related_breakpoints(struct breakpoint * b,gdb::function_view<void (breakpoint *)> function)12678 iterate_over_related_breakpoints (struct breakpoint *b,
12679 gdb::function_view<void (breakpoint *)> function)
12680 {
12681 struct breakpoint *related;
12682
12683 related = b;
12684 do
12685 {
12686 struct breakpoint *next;
12687
12688 /* FUNCTION may delete RELATED. */
12689 next = related->related_breakpoint;
12690
12691 if (next == related)
12692 {
12693 /* RELATED is the last ring entry. */
12694 function (related);
12695
12696 /* FUNCTION may have deleted it, so we'd never reach back to
12697 B. There's nothing left to do anyway, so just break
12698 out. */
12699 break;
12700 }
12701 else
12702 function (related);
12703
12704 related = next;
12705 }
12706 while (related != b);
12707 }
12708
12709 static void
delete_command(const char * arg,int from_tty)12710 delete_command (const char *arg, int from_tty)
12711 {
12712 dont_repeat ();
12713
12714 if (arg == 0)
12715 {
12716 int breaks_to_delete = 0;
12717
12718 /* Delete all breakpoints, watchpoints, tracepoints, and catchpoints
12719 if no argument. Do not delete internal breakpoints, these have to
12720 be deleted with an explicit breakpoint number argument. */
12721 for (breakpoint &b : all_breakpoints ())
12722 if (user_breakpoint_p (&b))
12723 {
12724 breaks_to_delete = 1;
12725 break;
12726 }
12727
12728 /* Ask user only if there are some breakpoints to delete. */
12729 if (!from_tty
12730 || (breaks_to_delete
12731 && query (_("Delete all breakpoints, watchpoints, tracepoints, "
12732 "and catchpoints? "))))
12733 for (breakpoint &b : all_breakpoints_safe ())
12734 if (user_breakpoint_p (&b))
12735 delete_breakpoint (&b);
12736 }
12737 else
12738 map_breakpoint_numbers
12739 (arg, [&] (breakpoint *br)
12740 {
12741 iterate_over_related_breakpoints (br, delete_breakpoint);
12742 });
12743 }
12744
12745 /* Return true if all locations of B bound to PSPACE are pending. If
12746 PSPACE is NULL, all locations of all program spaces are
12747 considered. */
12748
12749 static bool
all_locations_are_pending(struct breakpoint * b,struct program_space * pspace)12750 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
12751 {
12752 for (bp_location &loc : b->locations ())
12753 if ((pspace == NULL
12754 || loc.pspace == pspace)
12755 && !loc.shlib_disabled
12756 && !loc.pspace->executing_startup)
12757 return false;
12758 return true;
12759 }
12760
12761 /* Subroutine of update_breakpoint_locations to simplify it.
12762 Return true if multiple fns in list LOCS have the same name.
12763 Null names are ignored. */
12764
12765 static bool
ambiguous_names_p(const bp_location_range & locs)12766 ambiguous_names_p (const bp_location_range &locs)
12767 {
12768 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
12769 xcalloc, xfree));
12770
12771 for (const bp_location &l : locs)
12772 {
12773 const char **slot;
12774 const char *name = l.function_name.get ();
12775
12776 /* Allow for some names to be NULL, ignore them. */
12777 if (name == NULL)
12778 continue;
12779
12780 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
12781 INSERT);
12782 /* NOTE: We can assume slot != NULL here because xcalloc never
12783 returns NULL. */
12784 if (*slot != NULL)
12785 return true;
12786 *slot = name;
12787 }
12788
12789 return false;
12790 }
12791
12792 /* When symbols change, it probably means the sources changed as well,
12793 and it might mean the static tracepoint markers are no longer at
12794 the same address or line numbers they used to be at last we
12795 checked. Losing your static tracepoints whenever you rebuild is
12796 undesirable. This function tries to resync/rematch gdb static
12797 tracepoints with the markers on the target, for static tracepoints
12798 that have not been set by marker id. Static tracepoint that have
12799 been set by marker id are reset by marker id in breakpoint_re_set.
12800 The heuristic is:
12801
12802 1) For a tracepoint set at a specific address, look for a marker at
12803 the old PC. If one is found there, assume to be the same marker.
12804 If the name / string id of the marker found is different from the
12805 previous known name, assume that means the user renamed the marker
12806 in the sources, and output a warning.
12807
12808 2) For a tracepoint set at a given line number, look for a marker
12809 at the new address of the old line number. If one is found there,
12810 assume to be the same marker. If the name / string id of the
12811 marker found is different from the previous known name, assume that
12812 means the user renamed the marker in the sources, and output a
12813 warning.
12814
12815 3) If a marker is no longer found at the same address or line, it
12816 may mean the marker no longer exists. But it may also just mean
12817 the code changed a bit. Maybe the user added a few lines of code
12818 that made the marker move up or down (in line number terms). Ask
12819 the target for info about the marker with the string id as we knew
12820 it. If found, update line number and address in the matching
12821 static tracepoint. This will get confused if there's more than one
12822 marker with the same ID (possible in UST, although unadvised
12823 precisely because it confuses tools). */
12824
12825 static struct symtab_and_line
update_static_tracepoint(tracepoint * tp,struct symtab_and_line sal)12826 update_static_tracepoint (tracepoint *tp, struct symtab_and_line sal)
12827 {
12828 struct static_tracepoint_marker marker;
12829 CORE_ADDR pc;
12830
12831 pc = sal.pc;
12832 if (sal.line)
12833 find_line_pc (sal.symtab, sal.line, &pc);
12834
12835 if (target_static_tracepoint_marker_at (pc, &marker))
12836 {
12837 if (tp->static_trace_marker_id != marker.str_id)
12838 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12839 tp->number, tp->static_trace_marker_id.c_str (),
12840 marker.str_id.c_str ());
12841
12842 tp->static_trace_marker_id = std::move (marker.str_id);
12843
12844 return sal;
12845 }
12846
12847 /* Old marker wasn't found on target at lineno. Try looking it up
12848 by string ID. */
12849 if (!sal.explicit_pc
12850 && sal.line != 0
12851 && sal.symtab != NULL
12852 && !tp->static_trace_marker_id.empty ())
12853 {
12854 std::vector<static_tracepoint_marker> markers
12855 = target_static_tracepoint_markers_by_strid
12856 (tp->static_trace_marker_id.c_str ());
12857
12858 if (!markers.empty ())
12859 {
12860 struct symbol *sym;
12861 struct static_tracepoint_marker *tpmarker;
12862 struct ui_out *uiout = current_uiout;
12863
12864 tpmarker = &markers[0];
12865
12866 tp->static_trace_marker_id = std::move (tpmarker->str_id);
12867
12868 warning (_("marker for static tracepoint %d (%s) not "
12869 "found at previous line number"),
12870 tp->number, tp->static_trace_marker_id.c_str ());
12871
12872 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
12873 sym = find_pc_sect_function (tpmarker->address, NULL);
12874 uiout->text ("Now in ");
12875 if (sym)
12876 {
12877 uiout->field_string ("func", sym->print_name (),
12878 function_name_style.style ());
12879 uiout->text (" at ");
12880 }
12881 uiout->field_string ("file",
12882 symtab_to_filename_for_display (sal2.symtab),
12883 file_name_style.style ());
12884 uiout->text (":");
12885
12886 if (uiout->is_mi_like_p ())
12887 {
12888 const char *fullname = symtab_to_fullname (sal2.symtab);
12889
12890 uiout->field_string ("fullname", fullname);
12891 }
12892
12893 uiout->field_signed ("line", sal2.line);
12894 uiout->text ("\n");
12895
12896 tp->first_loc ().line_number = sal2.line;
12897 tp->first_loc ().symtab = sym != NULL ? sal2.symtab : NULL;
12898
12899 std::unique_ptr<explicit_location_spec> els
12900 (new explicit_location_spec
12901 (symtab_to_filename_for_display (sal2.symtab)));
12902 els->line_offset.offset = tp->first_loc ().line_number;
12903 els->line_offset.sign = LINE_OFFSET_NONE;
12904
12905 tp->locspec = std::move (els);
12906
12907 /* Might be nice to check if function changed, and warn if
12908 so. */
12909 }
12910 }
12911 return sal;
12912 }
12913
12914 /* Returns true iff location lists A and B are sufficiently same that
12915 we don't need to report breakpoint as changed. */
12916
12917 static bool
locations_are_equal(const bp_location_list & a,const bp_location_range & b)12918 locations_are_equal (const bp_location_list &a, const bp_location_range &b)
12919 {
12920 auto a_iter = a.begin ();
12921 auto b_iter = b.begin ();
12922
12923 for (; a_iter != a.end () && b_iter != b.end (); ++a_iter, ++b_iter)
12924 {
12925 if (a_iter->address != b_iter->address)
12926 return false;
12927
12928 if (a_iter->shlib_disabled != b_iter->shlib_disabled)
12929 return false;
12930
12931 if (a_iter->enabled != b_iter->enabled)
12932 return false;
12933
12934 if (a_iter->disabled_by_cond != b_iter->disabled_by_cond)
12935 return false;
12936 }
12937
12938 return (a_iter == a.end ()) == (b_iter == b.end ());
12939 }
12940
12941 /* See breakpoint.h. */
12942
12943 bp_location_list
steal_locations(program_space * pspace)12944 breakpoint::steal_locations (program_space *pspace)
12945 {
12946 if (pspace == NULL)
12947 return std::move (m_locations);
12948
12949 bp_location_list ret;
12950
12951 for (auto it = m_locations.begin (); it != m_locations.end (); )
12952 {
12953 if (it->pspace == pspace)
12954 {
12955 bp_location &loc = *it;
12956 it = m_locations.erase (it);
12957 ret.push_back (loc);
12958 }
12959 else
12960 ++it;
12961 }
12962
12963 return ret;
12964 }
12965
12966 /* Create new breakpoint locations for B (a hardware or software
12967 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12968 zero, then B is a ranged breakpoint. Only recreates locations for
12969 FILTER_PSPACE. Locations of other program spaces are left
12970 untouched. */
12971
12972 void
update_breakpoint_locations(code_breakpoint * b,struct program_space * filter_pspace,gdb::array_view<const symtab_and_line> sals,gdb::array_view<const symtab_and_line> sals_end)12973 update_breakpoint_locations (code_breakpoint *b,
12974 struct program_space *filter_pspace,
12975 gdb::array_view<const symtab_and_line> sals,
12976 gdb::array_view<const symtab_and_line> sals_end)
12977 {
12978 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
12979 {
12980 /* Ranged breakpoints have only one start location and one end
12981 location. */
12982 b->enable_state = bp_disabled;
12983 gdb_printf (gdb_stderr,
12984 _("Could not reset ranged breakpoint %d: "
12985 "multiple locations found\n"),
12986 b->number);
12987 return;
12988 }
12989
12990 /* If there's no new locations, and all existing locations are
12991 pending, don't do anything. This optimizes the common case where
12992 all locations are in the same shared library, that was unloaded.
12993 We'd like to retain the location, so that when the library is
12994 loaded again, we don't loose the enabled/disabled status of the
12995 individual locations. */
12996 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
12997 return;
12998
12999 bp_location_list existing_locations = b->steal_locations (filter_pspace);
13000
13001 for (const auto &sal : sals)
13002 {
13003 struct bp_location *new_loc;
13004
13005 switch_to_program_space_and_thread (sal.pspace);
13006
13007 new_loc = b->add_location (sal);
13008
13009 /* Reparse conditions, they might contain references to the
13010 old symtab. */
13011 if (b->cond_string != NULL)
13012 {
13013 const char *s;
13014
13015 s = b->cond_string.get ();
13016 try
13017 {
13018 new_loc->cond = parse_exp_1 (&s, sal.pc,
13019 block_for_pc (sal.pc),
13020 0);
13021 }
13022 catch (const gdb_exception_error &e)
13023 {
13024 new_loc->disabled_by_cond = true;
13025 }
13026 }
13027
13028 if (!sals_end.empty ())
13029 {
13030 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13031
13032 new_loc->length = end - sals[0].pc + 1;
13033 }
13034 }
13035
13036 /* If possible, carry over 'disable' status from existing
13037 breakpoints. */
13038 {
13039 /* If there are multiple breakpoints with the same function name,
13040 e.g. for inline functions, comparing function names won't work.
13041 Instead compare pc addresses; this is just a heuristic as things
13042 may have moved, but in practice it gives the correct answer
13043 often enough until a better solution is found. */
13044 int have_ambiguous_names = ambiguous_names_p (b->locations ());
13045
13046 for (const bp_location &e : existing_locations)
13047 {
13048 if ((!e.enabled || e.disabled_by_cond) && e.function_name)
13049 {
13050 if (have_ambiguous_names)
13051 {
13052 for (bp_location &l : b->locations ())
13053 {
13054 /* Ignore software vs hardware location type at
13055 this point, because with "set breakpoint
13056 auto-hw", after a re-set, locations that were
13057 hardware can end up as software, or vice versa.
13058 As mentioned above, this is an heuristic and in
13059 practice should give the correct answer often
13060 enough. */
13061 if (breakpoint_locations_match (&e, &l, true))
13062 {
13063 l.enabled = e.enabled;
13064 l.disabled_by_cond = e.disabled_by_cond;
13065 break;
13066 }
13067 }
13068 }
13069 else
13070 {
13071 for (bp_location &l : b->locations ())
13072 if (l.function_name
13073 && strcmp (e.function_name.get (),
13074 l.function_name.get ()) == 0)
13075 {
13076 l.enabled = e.enabled;
13077 l.disabled_by_cond = e.disabled_by_cond;
13078 break;
13079 }
13080 }
13081 }
13082 }
13083 }
13084
13085 if (!locations_are_equal (existing_locations, b->locations ()))
13086 notify_breakpoint_modified (b);
13087 }
13088
13089 /* Find the SaL locations corresponding to the given LOCSPEC.
13090 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13091
13092 std::vector<symtab_and_line>
location_spec_to_sals(location_spec * locspec,struct program_space * search_pspace,int * found)13093 code_breakpoint::location_spec_to_sals (location_spec *locspec,
13094 struct program_space *search_pspace,
13095 int *found)
13096 {
13097 struct gdb_exception exception;
13098
13099 std::vector<symtab_and_line> sals;
13100
13101 try
13102 {
13103 sals = decode_location_spec (locspec, search_pspace);
13104 }
13105 catch (gdb_exception_error &e)
13106 {
13107 int not_found_and_ok = false;
13108
13109 /* For pending breakpoints, it's expected that parsing will
13110 fail until the right shared library is loaded. User has
13111 already told to create pending breakpoints and don't need
13112 extra messages. If breakpoint is in bp_shlib_disabled
13113 state, then user already saw the message about that
13114 breakpoint being disabled, and don't want to see more
13115 errors. */
13116 if (e.error == NOT_FOUND_ERROR
13117 && (condition_not_parsed
13118 || (this->has_locations ()
13119 && search_pspace != NULL
13120 && this->first_loc ().pspace != search_pspace)
13121 || (this->has_locations () && this->first_loc ().shlib_disabled)
13122 || (this->has_locations ()
13123 && this->first_loc ().pspace->executing_startup)
13124 || enable_state == bp_disabled))
13125 not_found_and_ok = true;
13126
13127 if (!not_found_and_ok)
13128 {
13129 /* We surely don't want to warn about the same breakpoint
13130 10 times. One solution, implemented here, is disable
13131 the breakpoint on error. Another solution would be to
13132 have separate 'warning emitted' flag. Since this
13133 happens only when a binary has changed, I don't know
13134 which approach is better. */
13135 enable_state = bp_disabled;
13136 throw;
13137 }
13138
13139 exception = std::move (e);
13140 }
13141
13142 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13143 {
13144 for (auto &sal : sals)
13145 resolve_sal_pc (&sal);
13146 if (condition_not_parsed && extra_string != NULL)
13147 {
13148 gdb::unique_xmalloc_ptr<char> local_cond, local_extra;
13149 int local_thread, local_task, local_inferior;
13150
13151 find_condition_and_thread_for_sals (sals, extra_string.get (),
13152 &local_cond, &local_thread,
13153 &local_inferior,
13154 &local_task, &local_extra);
13155 gdb_assert (cond_string == nullptr);
13156 if (local_cond != nullptr)
13157 cond_string = std::move (local_cond);
13158 thread = local_thread;
13159 task = local_task;
13160 if (local_extra != nullptr)
13161 extra_string = std::move (local_extra);
13162 condition_not_parsed = 0;
13163 }
13164
13165 if (type == bp_static_tracepoint)
13166 {
13167 tracepoint *t = gdb::checked_static_cast<tracepoint *> (this);
13168 sals[0] = update_static_tracepoint (t, sals[0]);
13169 }
13170
13171 *found = 1;
13172 }
13173 else
13174 *found = 0;
13175
13176 return sals;
13177 }
13178
13179 /* The default re_set method, for typical hardware or software
13180 breakpoints. Reevaluate the breakpoint and recreate its
13181 locations. */
13182
13183 void
re_set_default()13184 code_breakpoint::re_set_default ()
13185 {
13186 struct program_space *filter_pspace = current_program_space;
13187 std::vector<symtab_and_line> expanded, expanded_end;
13188
13189 int found;
13190 std::vector<symtab_and_line> sals = location_spec_to_sals (locspec.get (),
13191 filter_pspace,
13192 &found);
13193 if (found)
13194 expanded = std::move (sals);
13195
13196 if (locspec_range_end != nullptr)
13197 {
13198 std::vector<symtab_and_line> sals_end
13199 = location_spec_to_sals (locspec_range_end.get (),
13200 filter_pspace, &found);
13201 if (found)
13202 expanded_end = std::move (sals_end);
13203 }
13204
13205 update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
13206 }
13207
13208 /* Default method for creating SALs from an address string. It basically
13209 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13210
13211 static void
create_sals_from_location_spec_default(location_spec * locspec,struct linespec_result * canonical)13212 create_sals_from_location_spec_default (location_spec *locspec,
13213 struct linespec_result *canonical)
13214 {
13215 parse_breakpoint_sals (locspec, canonical);
13216 }
13217
13218 /* Reset a breakpoint. */
13219
13220 static void
breakpoint_re_set_one(breakpoint * b)13221 breakpoint_re_set_one (breakpoint *b)
13222 {
13223 input_radix = b->input_radix;
13224 set_language (b->language);
13225
13226 b->re_set ();
13227 }
13228
13229 /* Re-set breakpoint locations for the current program space.
13230 Locations bound to other program spaces are left untouched. */
13231
13232 void
breakpoint_re_set(void)13233 breakpoint_re_set (void)
13234 {
13235 {
13236 scoped_restore_current_language save_language;
13237 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13238 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13239
13240 /* breakpoint_re_set_one sets the current_language to the language
13241 of the breakpoint it is resetting (see prepare_re_set_context)
13242 before re-evaluating the breakpoint's location. This change can
13243 unfortunately get undone by accident if the language_mode is set
13244 to auto, and we either switch frames, or more likely in this context,
13245 we select the current frame.
13246
13247 We prevent this by temporarily turning the language_mode to
13248 language_mode_manual. We restore it once all breakpoints
13249 have been reset. */
13250 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13251 language_mode = language_mode_manual;
13252
13253 /* Note: we must not try to insert locations until after all
13254 breakpoints have been re-set. Otherwise, e.g., when re-setting
13255 breakpoint 1, we'd insert the locations of breakpoint 2, which
13256 hadn't been re-set yet, and thus may have stale locations. */
13257
13258 for (breakpoint &b : all_breakpoints_safe ())
13259 {
13260 try
13261 {
13262 breakpoint_re_set_one (&b);
13263 }
13264 catch (const gdb_exception &ex)
13265 {
13266 exception_fprintf (gdb_stderr, ex,
13267 "Error in re-setting breakpoint %d: ",
13268 b.number);
13269 }
13270 }
13271
13272 jit_breakpoint_re_set ();
13273 }
13274
13275 create_overlay_event_breakpoint ();
13276 create_longjmp_master_breakpoint ();
13277 create_std_terminate_master_breakpoint ();
13278 create_exception_master_breakpoint ();
13279
13280 /* Now we can insert. */
13281 update_global_location_list (UGLL_MAY_INSERT);
13282 }
13283
13284 /* Reset the thread number of this breakpoint:
13285
13286 - If the breakpoint is for all threads, leave it as-is.
13287 - Else, reset it to the current thread for inferior_ptid. */
13288 void
breakpoint_re_set_thread(struct breakpoint * b)13289 breakpoint_re_set_thread (struct breakpoint *b)
13290 {
13291 if (b->thread != -1)
13292 {
13293 b->thread = inferior_thread ()->global_num;
13294
13295 /* We're being called after following a fork. The new fork is
13296 selected as current, and unless this was a vfork will have a
13297 different program space from the original thread. Reset that
13298 as well. */
13299 b->first_loc ().pspace = current_program_space;
13300 }
13301 }
13302
13303 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13304 If from_tty is nonzero, it prints a message to that effect,
13305 which ends with a period (no newline). */
13306
13307 void
set_ignore_count(int bptnum,int count,int from_tty)13308 set_ignore_count (int bptnum, int count, int from_tty)
13309 {
13310 if (count < 0)
13311 count = 0;
13312
13313 for (breakpoint &b : all_breakpoints ())
13314 if (b.number == bptnum)
13315 {
13316 if (is_tracepoint (&b))
13317 {
13318 if (from_tty && count != 0)
13319 gdb_printf (_("Ignore count ignored for tracepoint %d."),
13320 bptnum);
13321 return;
13322 }
13323
13324 b.ignore_count = count;
13325 if (from_tty)
13326 {
13327 if (count == 0)
13328 gdb_printf (_("Will stop next time "
13329 "breakpoint %d is reached."),
13330 bptnum);
13331 else if (count == 1)
13332 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
13333 bptnum);
13334 else
13335 gdb_printf (_("Will ignore next %d "
13336 "crossings of breakpoint %d."),
13337 count, bptnum);
13338 }
13339 notify_breakpoint_modified (&b);
13340 return;
13341 }
13342
13343 error (_("No breakpoint number %d."), bptnum);
13344 }
13345
13346 /* Command to set ignore-count of breakpoint N to COUNT. */
13347
13348 static void
ignore_command(const char * args,int from_tty)13349 ignore_command (const char *args, int from_tty)
13350 {
13351 const char *p = args;
13352 int num;
13353
13354 if (p == 0)
13355 error_no_arg (_("a breakpoint number"));
13356
13357 num = get_number (&p);
13358 if (num == 0)
13359 error (_("bad breakpoint number: '%s'"), args);
13360 if (*p == 0)
13361 error (_("Second argument (specified ignore-count) is missing."));
13362
13363 set_ignore_count (num,
13364 longest_to_int (value_as_long (parse_and_eval (p))),
13365 from_tty);
13366 if (from_tty)
13367 gdb_printf ("\n");
13368 }
13369
13370
13371 /* Call FUNCTION on each of the breakpoints with numbers in the range
13372 defined by BP_NUM_RANGE (an inclusive range). */
13373
13374 static void
map_breakpoint_number_range(std::pair<int,int> bp_num_range,gdb::function_view<void (breakpoint *)> function)13375 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13376 gdb::function_view<void (breakpoint *)> function)
13377 {
13378 if (bp_num_range.first == 0)
13379 {
13380 warning (_("bad breakpoint number at or near '%d'"),
13381 bp_num_range.first);
13382 }
13383 else
13384 {
13385 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13386 {
13387 bool match = false;
13388
13389 for (breakpoint &b : all_breakpoints_safe ())
13390 if (b.number == i)
13391 {
13392 match = true;
13393 function (&b);
13394 break;
13395 }
13396 if (!match)
13397 gdb_printf (_("No breakpoint number %d.\n"), i);
13398 }
13399 }
13400 }
13401
13402 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13403 ARGS. */
13404
13405 static void
map_breakpoint_numbers(const char * args,gdb::function_view<void (breakpoint *)> function)13406 map_breakpoint_numbers (const char *args,
13407 gdb::function_view<void (breakpoint *)> function)
13408 {
13409 if (args == NULL || *args == '\0')
13410 error_no_arg (_("one or more breakpoint numbers"));
13411
13412 number_or_range_parser parser (args);
13413
13414 while (!parser.finished ())
13415 {
13416 int num = parser.get_number ();
13417 map_breakpoint_number_range (std::make_pair (num, num), function);
13418 }
13419 }
13420
13421 /* Return the breakpoint location structure corresponding to the
13422 BP_NUM and LOC_NUM values. */
13423
13424 static struct bp_location *
find_location_by_number(int bp_num,int loc_num)13425 find_location_by_number (int bp_num, int loc_num)
13426 {
13427 breakpoint *b = get_breakpoint (bp_num);
13428
13429 if (!b || b->number != bp_num)
13430 error (_("Bad breakpoint number '%d'"), bp_num);
13431
13432 if (loc_num == 0)
13433 error (_("Bad breakpoint location number '%d'"), loc_num);
13434
13435 int n = 0;
13436 for (bp_location &loc : b->locations ())
13437 if (++n == loc_num)
13438 return &loc;
13439
13440 error (_("Bad breakpoint location number '%d'"), loc_num);
13441 }
13442
13443 /* Modes of operation for extract_bp_num. */
13444 enum class extract_bp_kind
13445 {
13446 /* Extracting a breakpoint number. */
13447 bp,
13448
13449 /* Extracting a location number. */
13450 loc,
13451 };
13452
13453 /* Extract a breakpoint or location number (as determined by KIND)
13454 from the string starting at START. TRAILER is a character which
13455 can be found after the number. If you don't want a trailer, use
13456 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13457 string. This always returns a positive integer. */
13458
13459 static int
13460 extract_bp_num (extract_bp_kind kind, const char *start,
13461 int trailer, const char **end_out = NULL)
13462 {
13463 const char *end = start;
13464 int num = get_number_trailer (&end, trailer);
13465 if (num < 0)
13466 error (kind == extract_bp_kind::bp
13467 ? _("Negative breakpoint number '%.*s'")
13468 : _("Negative breakpoint location number '%.*s'"),
13469 int (end - start), start);
13470 if (num == 0)
13471 error (kind == extract_bp_kind::bp
13472 ? _("Bad breakpoint number '%.*s'")
13473 : _("Bad breakpoint location number '%.*s'"),
13474 int (end - start), start);
13475
13476 if (end_out != NULL)
13477 *end_out = end;
13478 return num;
13479 }
13480
13481 /* Extract a breakpoint or location range (as determined by KIND) in
13482 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13483 representing the (inclusive) range. The returned pair's elements
13484 are always positive integers. */
13485
13486 static std::pair<int, int>
extract_bp_or_bp_range(extract_bp_kind kind,const std::string & arg,std::string::size_type arg_offset)13487 extract_bp_or_bp_range (extract_bp_kind kind,
13488 const std::string &arg,
13489 std::string::size_type arg_offset)
13490 {
13491 std::pair<int, int> range;
13492 const char *bp_loc = &arg[arg_offset];
13493 std::string::size_type dash = arg.find ('-', arg_offset);
13494 if (dash != std::string::npos)
13495 {
13496 /* bp_loc is a range (x-z). */
13497 if (arg.length () == dash + 1)
13498 error (kind == extract_bp_kind::bp
13499 ? _("Bad breakpoint number at or near: '%s'")
13500 : _("Bad breakpoint location number at or near: '%s'"),
13501 bp_loc);
13502
13503 const char *end;
13504 const char *start_first = bp_loc;
13505 const char *start_second = &arg[dash + 1];
13506 range.first = extract_bp_num (kind, start_first, '-');
13507 range.second = extract_bp_num (kind, start_second, '\0', &end);
13508
13509 if (range.first > range.second)
13510 error (kind == extract_bp_kind::bp
13511 ? _("Inverted breakpoint range at '%.*s'")
13512 : _("Inverted breakpoint location range at '%.*s'"),
13513 int (end - start_first), start_first);
13514 }
13515 else
13516 {
13517 /* bp_loc is a single value. */
13518 range.first = extract_bp_num (kind, bp_loc, '\0');
13519 range.second = range.first;
13520 }
13521 return range;
13522 }
13523
13524 /* Extract the breakpoint/location range specified by ARG. Returns
13525 the breakpoint range in BP_NUM_RANGE, and the location range in
13526 BP_LOC_RANGE.
13527
13528 ARG may be in any of the following forms:
13529
13530 x where 'x' is a breakpoint number.
13531 x-y where 'x' and 'y' specify a breakpoint numbers range.
13532 x.y where 'x' is a breakpoint number and 'y' a location number.
13533 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13534 location number range.
13535 */
13536
13537 static void
extract_bp_number_and_location(const std::string & arg,std::pair<int,int> & bp_num_range,std::pair<int,int> & bp_loc_range)13538 extract_bp_number_and_location (const std::string &arg,
13539 std::pair<int, int> &bp_num_range,
13540 std::pair<int, int> &bp_loc_range)
13541 {
13542 std::string::size_type dot = arg.find ('.');
13543
13544 if (dot != std::string::npos)
13545 {
13546 /* Handle 'x.y' and 'x.y-z' cases. */
13547
13548 if (arg.length () == dot + 1 || dot == 0)
13549 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
13550
13551 bp_num_range.first
13552 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13553 bp_num_range.second = bp_num_range.first;
13554
13555 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13556 arg, dot + 1);
13557 }
13558 else
13559 {
13560 /* Handle x and x-y cases. */
13561
13562 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
13563 bp_loc_range.first = 0;
13564 bp_loc_range.second = 0;
13565 }
13566 }
13567
13568 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13569 specifies whether to enable or disable. */
13570
13571 static void
enable_disable_bp_num_loc(int bp_num,int loc_num,bool enable)13572 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13573 {
13574 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13575 if (loc != NULL)
13576 {
13577 if (loc->disabled_by_cond && enable)
13578 error (_("Breakpoint %d's condition is invalid at location %d, "
13579 "cannot enable."), bp_num, loc_num);
13580
13581 if (loc->enabled != enable)
13582 {
13583 loc->enabled = enable;
13584 mark_breakpoint_location_modified (loc);
13585 }
13586 if (target_supports_enable_disable_tracepoint ()
13587 && current_trace_status ()->running && loc->owner
13588 && is_tracepoint (loc->owner))
13589 target_disable_tracepoint (loc);
13590 }
13591 update_global_location_list (UGLL_DONT_INSERT);
13592
13593 notify_breakpoint_modified (loc->owner);
13594 }
13595
13596 /* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
13597 owner. 1-based indexing. -1 signals NOT FOUND. */
13598
13599 static int
find_loc_num_by_location(const bp_location * loc)13600 find_loc_num_by_location (const bp_location *loc)
13601 {
13602 if (loc != nullptr && loc->owner != nullptr)
13603 {
13604 /* Locations use 1-based indexing. */
13605 int loc_num = 1;
13606 for (bp_location &it : loc->owner->locations ())
13607 {
13608 if (&it == loc)
13609 return loc_num;
13610 loc_num++;
13611 }
13612 }
13613 return -1;
13614 }
13615
13616 /* Enable or disable a breakpoint location LOC. ENABLE
13617 specifies whether to enable or disable. */
13618
13619 void
enable_disable_bp_location(bp_location * loc,bool enable)13620 enable_disable_bp_location (bp_location *loc, bool enable)
13621 {
13622 if (loc == nullptr)
13623 error (_("Breakpoint location is invalid."));
13624
13625 if (loc->owner == nullptr)
13626 error (_("Breakpoint location does not have an owner breakpoint."));
13627
13628 if (loc->disabled_by_cond && enable)
13629 {
13630 int loc_num = find_loc_num_by_location (loc);
13631 if (loc_num == -1)
13632 error (_("Breakpoint location LOC_NUM could not be found."));
13633 else
13634 error (_("Breakpoint %d's condition is invalid at location %d, "
13635 "cannot enable."), loc->owner->number, loc_num);
13636 }
13637
13638 if (loc->enabled != enable)
13639 {
13640 loc->enabled = enable;
13641 mark_breakpoint_location_modified (loc);
13642 }
13643
13644 if (target_supports_enable_disable_tracepoint ()
13645 && current_trace_status ()->running && loc->owner
13646 && is_tracepoint (loc->owner))
13647 target_disable_tracepoint (loc);
13648
13649 update_global_location_list (UGLL_DONT_INSERT);
13650 notify_breakpoint_modified (loc->owner);
13651 }
13652
13653 /* Enable or disable a range of breakpoint locations. BP_NUM is the
13654 number of the breakpoint, and BP_LOC_RANGE specifies the
13655 (inclusive) range of location numbers of that breakpoint to
13656 enable/disable. ENABLE specifies whether to enable or disable the
13657 location. */
13658
13659 static void
enable_disable_breakpoint_location_range(int bp_num,std::pair<int,int> & bp_loc_range,bool enable)13660 enable_disable_breakpoint_location_range (int bp_num,
13661 std::pair<int, int> &bp_loc_range,
13662 bool enable)
13663 {
13664 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13665 enable_disable_bp_num_loc (bp_num, i, enable);
13666 }
13667
13668 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13669 If from_tty is nonzero, it prints a message to that effect,
13670 which ends with a period (no newline). */
13671
13672 void
disable_breakpoint(struct breakpoint * bpt)13673 disable_breakpoint (struct breakpoint *bpt)
13674 {
13675 /* Never disable a watchpoint scope breakpoint; we want to
13676 hit them when we leave scope so we can delete both the
13677 watchpoint and its scope breakpoint at that time. */
13678 if (bpt->type == bp_watchpoint_scope)
13679 return;
13680
13681 bpt->enable_state = bp_disabled;
13682
13683 /* Mark breakpoint locations modified. */
13684 mark_breakpoint_modified (bpt);
13685
13686 if (target_supports_enable_disable_tracepoint ()
13687 && current_trace_status ()->running && is_tracepoint (bpt))
13688 {
13689 for (bp_location &location : bpt->locations ())
13690 target_disable_tracepoint (&location);
13691 }
13692
13693 update_global_location_list (UGLL_DONT_INSERT);
13694
13695 notify_breakpoint_modified (bpt);
13696 }
13697
13698 /* Enable or disable the breakpoint(s) or breakpoint location(s)
13699 specified in ARGS. ARGS may be in any of the formats handled by
13700 extract_bp_number_and_location. ENABLE specifies whether to enable
13701 or disable the breakpoints/locations. */
13702
13703 static void
enable_disable_command(const char * args,int from_tty,bool enable)13704 enable_disable_command (const char *args, int from_tty, bool enable)
13705 {
13706 if (args == 0)
13707 {
13708 for (breakpoint &bpt : all_breakpoints ())
13709 if (user_breakpoint_p (&bpt))
13710 {
13711 if (enable)
13712 enable_breakpoint (&bpt);
13713 else
13714 disable_breakpoint (&bpt);
13715 }
13716 }
13717 else
13718 {
13719 std::string num = extract_arg (&args);
13720
13721 while (!num.empty ())
13722 {
13723 std::pair<int, int> bp_num_range, bp_loc_range;
13724
13725 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13726
13727 if (bp_loc_range.first == bp_loc_range.second
13728 && (bp_loc_range.first == 0
13729 || (bp_loc_range.first == 1
13730 && bp_num_range.first == bp_num_range.second
13731 && !has_multiple_locations (bp_num_range.first))))
13732 {
13733 /* Handle breakpoint ids with formats 'x' or 'x-z'
13734 or 'y.1' where y has only one code location. */
13735 map_breakpoint_number_range (bp_num_range,
13736 enable
13737 ? enable_breakpoint
13738 : disable_breakpoint);
13739 }
13740 else
13741 {
13742 /* Handle breakpoint ids with formats 'x.y' or
13743 'x.y-z'. */
13744 enable_disable_breakpoint_location_range
13745 (bp_num_range.first, bp_loc_range, enable);
13746 }
13747 num = extract_arg (&args);
13748 }
13749 }
13750 }
13751
13752 /* The disable command disables the specified breakpoints/locations
13753 (or all defined breakpoints) so they're no longer effective in
13754 stopping the inferior. ARGS may be in any of the forms defined in
13755 extract_bp_number_and_location. */
13756
13757 static void
disable_command(const char * args,int from_tty)13758 disable_command (const char *args, int from_tty)
13759 {
13760 enable_disable_command (args, from_tty, false);
13761 }
13762
13763 static void
enable_breakpoint_disp(struct breakpoint * bpt,enum bpdisp disposition,int count)13764 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13765 int count)
13766 {
13767 int target_resources_ok;
13768
13769 if (bpt->type == bp_hardware_breakpoint)
13770 {
13771 int i;
13772 i = hw_breakpoint_used_count ();
13773 target_resources_ok =
13774 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13775 i + 1, 0);
13776 if (target_resources_ok == 0)
13777 error (_("No hardware breakpoint support in the target."));
13778 else if (target_resources_ok < 0)
13779 error (_("Hardware breakpoints used exceeds limit."));
13780 }
13781
13782 if (is_watchpoint (bpt))
13783 {
13784 /* Initialize it just to avoid a GCC false warning. */
13785 enum enable_state orig_enable_state = bp_disabled;
13786
13787 try
13788 {
13789 watchpoint *w = gdb::checked_static_cast<watchpoint *> (bpt);
13790
13791 orig_enable_state = bpt->enable_state;
13792 bpt->enable_state = bp_enabled;
13793 update_watchpoint (w, true /* reparse */);
13794 }
13795 catch (const gdb_exception_error &e)
13796 {
13797 bpt->enable_state = orig_enable_state;
13798 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13799 bpt->number);
13800 return;
13801 }
13802 }
13803
13804 bpt->enable_state = bp_enabled;
13805
13806 /* Mark breakpoint locations modified. */
13807 mark_breakpoint_modified (bpt);
13808
13809 if (target_supports_enable_disable_tracepoint ()
13810 && current_trace_status ()->running && is_tracepoint (bpt))
13811 {
13812 for (bp_location &location : bpt->locations ())
13813 target_enable_tracepoint (&location);
13814 }
13815
13816 bpt->disposition = disposition;
13817 bpt->enable_count = count;
13818 update_global_location_list (UGLL_MAY_INSERT);
13819
13820 notify_breakpoint_modified (bpt);
13821 }
13822
13823
13824 void
enable_breakpoint(struct breakpoint * bpt)13825 enable_breakpoint (struct breakpoint *bpt)
13826 {
13827 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13828 }
13829
13830 /* The enable command enables the specified breakpoints/locations (or
13831 all defined breakpoints) so they once again become (or continue to
13832 be) effective in stopping the inferior. ARGS may be in any of the
13833 forms defined in extract_bp_number_and_location. */
13834
13835 static void
enable_command(const char * args,int from_tty)13836 enable_command (const char *args, int from_tty)
13837 {
13838 enable_disable_command (args, from_tty, true);
13839 }
13840
13841 static void
enable_once_command(const char * args,int from_tty)13842 enable_once_command (const char *args, int from_tty)
13843 {
13844 map_breakpoint_numbers
13845 (args, [&] (breakpoint *b)
13846 {
13847 iterate_over_related_breakpoints
13848 (b, [&] (breakpoint *bpt)
13849 {
13850 enable_breakpoint_disp (bpt, disp_disable, 1);
13851 });
13852 });
13853 }
13854
13855 static void
enable_count_command(const char * args,int from_tty)13856 enable_count_command (const char *args, int from_tty)
13857 {
13858 int count;
13859
13860 if (args == NULL)
13861 error_no_arg (_("hit count"));
13862
13863 count = get_number (&args);
13864
13865 map_breakpoint_numbers
13866 (args, [&] (breakpoint *b)
13867 {
13868 iterate_over_related_breakpoints
13869 (b, [&] (breakpoint *bpt)
13870 {
13871 enable_breakpoint_disp (bpt, disp_disable, count);
13872 });
13873 });
13874 }
13875
13876 static void
enable_delete_command(const char * args,int from_tty)13877 enable_delete_command (const char *args, int from_tty)
13878 {
13879 map_breakpoint_numbers
13880 (args, [&] (breakpoint *b)
13881 {
13882 iterate_over_related_breakpoints
13883 (b, [&] (breakpoint *bpt)
13884 {
13885 enable_breakpoint_disp (bpt, disp_del, 1);
13886 });
13887 });
13888 }
13889
13890 /* Invalidate last known value of any hardware watchpoint if
13891 the memory which that value represents has been written to by
13892 GDB itself. */
13893
13894 static void
invalidate_bp_value_on_memory_change(struct inferior * inferior,CORE_ADDR addr,ssize_t len,const bfd_byte * data)13895 invalidate_bp_value_on_memory_change (struct inferior *inferior,
13896 CORE_ADDR addr, ssize_t len,
13897 const bfd_byte *data)
13898 {
13899 for (breakpoint &bp : all_breakpoints ())
13900 if (bp.enable_state == bp_enabled
13901 && bp.type == bp_hardware_watchpoint)
13902 {
13903 watchpoint &wp = gdb::checked_static_cast<watchpoint &> (bp);
13904
13905 if (wp.val_valid && wp.val != nullptr)
13906 {
13907 for (bp_location &loc : bp.locations ())
13908 if (loc.loc_type == bp_loc_hardware_watchpoint
13909 && loc.address + loc.length > addr
13910 && addr + len > loc.address)
13911 {
13912 wp.val = NULL;
13913 wp.val_valid = false;
13914 }
13915 }
13916 }
13917 }
13918
13919 /* Create and insert a breakpoint for software single step. */
13920
13921 void
insert_single_step_breakpoint(struct gdbarch * gdbarch,const address_space * aspace,CORE_ADDR next_pc)13922 insert_single_step_breakpoint (struct gdbarch *gdbarch,
13923 const address_space *aspace,
13924 CORE_ADDR next_pc)
13925 {
13926 struct thread_info *tp = inferior_thread ();
13927 struct symtab_and_line sal;
13928 CORE_ADDR pc = next_pc;
13929
13930 if (tp->control.single_step_breakpoints == NULL)
13931 {
13932 std::unique_ptr<breakpoint> b
13933 (new momentary_breakpoint (gdbarch, bp_single_step,
13934 current_program_space,
13935 null_frame_id,
13936 tp->global_num));
13937
13938 tp->control.single_step_breakpoints
13939 = add_to_breakpoint_chain (std::move (b));
13940 }
13941
13942 sal = find_pc_line (pc, 0);
13943 sal.pc = pc;
13944 sal.section = find_pc_overlay (pc);
13945 sal.explicit_pc = 1;
13946
13947 auto *ss_bp
13948 = (gdb::checked_static_cast<momentary_breakpoint *>
13949 (tp->control.single_step_breakpoints));
13950 ss_bp->add_location (sal);
13951
13952 update_global_location_list (UGLL_INSERT);
13953 }
13954
13955 /* Insert single step breakpoints according to the current state. */
13956
13957 int
insert_single_step_breakpoints(struct gdbarch * gdbarch)13958 insert_single_step_breakpoints (struct gdbarch *gdbarch)
13959 {
13960 regcache *regcache = get_thread_regcache (inferior_thread ());
13961 std::vector<CORE_ADDR> next_pcs;
13962
13963 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
13964
13965 if (!next_pcs.empty ())
13966 {
13967 frame_info_ptr frame = get_current_frame ();
13968 const address_space *aspace = get_frame_address_space (frame);
13969
13970 for (CORE_ADDR pc : next_pcs)
13971 insert_single_step_breakpoint (gdbarch, aspace, pc);
13972
13973 return 1;
13974 }
13975 else
13976 return 0;
13977 }
13978
13979 /* See breakpoint.h. */
13980
13981 int
breakpoint_has_location_inserted_here(struct breakpoint * bp,const address_space * aspace,CORE_ADDR pc)13982 breakpoint_has_location_inserted_here (struct breakpoint *bp,
13983 const address_space *aspace,
13984 CORE_ADDR pc)
13985 {
13986 for (bp_location &loc : bp->locations ())
13987 if (loc.inserted
13988 && breakpoint_location_address_match (&loc, aspace, pc))
13989 return 1;
13990
13991 return 0;
13992 }
13993
13994 /* Check whether a software single-step breakpoint is inserted at
13995 PC. */
13996
13997 int
single_step_breakpoint_inserted_here_p(const address_space * aspace,CORE_ADDR pc)13998 single_step_breakpoint_inserted_here_p (const address_space *aspace,
13999 CORE_ADDR pc)
14000 {
14001 for (breakpoint &bpt : all_breakpoints ())
14002 {
14003 if (bpt.type == bp_single_step
14004 && breakpoint_has_location_inserted_here (&bpt, aspace, pc))
14005 return 1;
14006 }
14007 return 0;
14008 }
14009
14010 /* Tracepoint-specific operations. */
14011
14012 /* Set tracepoint count to NUM. */
14013 static void
set_tracepoint_count(int num)14014 set_tracepoint_count (int num)
14015 {
14016 tracepoint_count = num;
14017 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14018 }
14019
14020 static void
trace_command(const char * arg,int from_tty)14021 trace_command (const char *arg, int from_tty)
14022 {
14023 location_spec_up locspec = string_to_location_spec (&arg,
14024 current_language);
14025 const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
14026 (locspec.get (), true /* is_tracepoint */);
14027
14028 create_breakpoint (get_current_arch (),
14029 locspec.get (),
14030 NULL, -1, -1, arg, false, 1 /* parse arg */,
14031 0 /* tempflag */,
14032 bp_tracepoint /* type_wanted */,
14033 0 /* Ignore count */,
14034 pending_break_support,
14035 ops,
14036 from_tty,
14037 1 /* enabled */,
14038 0 /* internal */, 0);
14039 }
14040
14041 static void
ftrace_command(const char * arg,int from_tty)14042 ftrace_command (const char *arg, int from_tty)
14043 {
14044 location_spec_up locspec = string_to_location_spec (&arg,
14045 current_language);
14046 create_breakpoint (get_current_arch (),
14047 locspec.get (),
14048 NULL, -1, -1, arg, false, 1 /* parse arg */,
14049 0 /* tempflag */,
14050 bp_fast_tracepoint /* type_wanted */,
14051 0 /* Ignore count */,
14052 pending_break_support,
14053 &code_breakpoint_ops,
14054 from_tty,
14055 1 /* enabled */,
14056 0 /* internal */, 0);
14057 }
14058
14059 /* strace command implementation. Creates a static tracepoint. */
14060
14061 static void
strace_command(const char * arg,int from_tty)14062 strace_command (const char *arg, int from_tty)
14063 {
14064 const struct breakpoint_ops *ops;
14065 location_spec_up locspec;
14066 enum bptype type;
14067
14068 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14069 or with a normal static tracepoint. */
14070 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14071 {
14072 ops = &strace_marker_breakpoint_ops;
14073 locspec = new_linespec_location_spec (&arg,
14074 symbol_name_match_type::FULL);
14075 type = bp_static_marker_tracepoint;
14076 }
14077 else
14078 {
14079 ops = &code_breakpoint_ops;
14080 locspec = string_to_location_spec (&arg, current_language);
14081 type = bp_static_tracepoint;
14082 }
14083
14084 create_breakpoint (get_current_arch (),
14085 locspec.get (),
14086 NULL, -1, -1, arg, false, 1 /* parse arg */,
14087 0 /* tempflag */,
14088 type /* type_wanted */,
14089 0 /* Ignore count */,
14090 pending_break_support,
14091 ops,
14092 from_tty,
14093 1 /* enabled */,
14094 0 /* internal */, 0);
14095 }
14096
14097 /* Set up a fake reader function that gets command lines from a linked
14098 list that was acquired during tracepoint uploading. */
14099
14100 static struct uploaded_tp *this_utp;
14101 static int next_cmd;
14102
14103 static const char *
read_uploaded_action(std::string & buffer)14104 read_uploaded_action (std::string &buffer)
14105 {
14106 char *rslt = nullptr;
14107
14108 if (next_cmd < this_utp->cmd_strings.size ())
14109 {
14110 rslt = this_utp->cmd_strings[next_cmd].get ();
14111 next_cmd++;
14112 }
14113
14114 return rslt;
14115 }
14116
14117 /* Given information about a tracepoint as recorded on a target (which
14118 can be either a live system or a trace file), attempt to create an
14119 equivalent GDB tracepoint. This is not a reliable process, since
14120 the target does not necessarily have all the information used when
14121 the tracepoint was originally defined. */
14122
14123 struct tracepoint *
create_tracepoint_from_upload(struct uploaded_tp * utp)14124 create_tracepoint_from_upload (struct uploaded_tp *utp)
14125 {
14126 const char *addr_str;
14127 char small_buf[100];
14128 struct tracepoint *tp;
14129
14130 if (utp->at_string)
14131 addr_str = utp->at_string.get ();
14132 else
14133 {
14134 /* In the absence of a source location, fall back to raw
14135 address. Since there is no way to confirm that the address
14136 means the same thing as when the trace was started, warn the
14137 user. */
14138 warning (_("Uploaded tracepoint %d has no "
14139 "source location, using raw address"),
14140 utp->number);
14141 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14142 addr_str = small_buf;
14143 }
14144
14145 /* There's not much we can do with a sequence of bytecodes. */
14146 if (utp->cond && !utp->cond_string)
14147 warning (_("Uploaded tracepoint %d condition "
14148 "has no source form, ignoring it"),
14149 utp->number);
14150
14151 location_spec_up locspec = string_to_location_spec (&addr_str,
14152 current_language);
14153
14154
14155 gdb_assert (addr_str != nullptr);
14156 if (*addr_str != '\0')
14157 error (_("Garbage '%s' at end of location"), addr_str);
14158
14159 if (!create_breakpoint (get_current_arch (),
14160 locspec.get (),
14161 utp->cond_string.get (), -1, -1, addr_str,
14162 false /* force_condition */,
14163 0 /* parse cond/thread */,
14164 0 /* tempflag */,
14165 utp->type /* type_wanted */,
14166 0 /* Ignore count */,
14167 pending_break_support,
14168 &code_breakpoint_ops,
14169 0 /* from_tty */,
14170 utp->enabled /* enabled */,
14171 0 /* internal */,
14172 CREATE_BREAKPOINT_FLAGS_INSERTED))
14173 return NULL;
14174
14175 /* Get the tracepoint we just created. */
14176 tp = get_tracepoint (tracepoint_count);
14177 gdb_assert (tp != NULL);
14178
14179 if (utp->pass > 0)
14180 {
14181 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14182 tp->number);
14183
14184 trace_pass_command (small_buf, 0);
14185 }
14186
14187 /* If we have uploaded versions of the original commands, set up a
14188 special-purpose "reader" function and call the usual command line
14189 reader, then pass the result to the breakpoint command-setting
14190 function. */
14191 if (!utp->cmd_strings.empty ())
14192 {
14193 counted_command_line cmd_list;
14194
14195 this_utp = utp;
14196 next_cmd = 0;
14197
14198 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14199
14200 breakpoint_set_commands (tp, std::move (cmd_list));
14201 }
14202 else if (!utp->actions.empty ()
14203 || !utp->step_actions.empty ())
14204 warning (_("Uploaded tracepoint %d actions "
14205 "have no source form, ignoring them"),
14206 utp->number);
14207
14208 /* Copy any status information that might be available. */
14209 tp->hit_count = utp->hit_count;
14210 tp->traceframe_usage = utp->traceframe_usage;
14211
14212 return tp;
14213 }
14214
14215 /* Print information on tracepoint number TPNUM_EXP, or all if
14216 omitted. */
14217
14218 static void
info_tracepoints_command(const char * args,int from_tty)14219 info_tracepoints_command (const char *args, int from_tty)
14220 {
14221 struct ui_out *uiout = current_uiout;
14222 int num_printed;
14223
14224 num_printed = breakpoint_1 (args, false, is_tracepoint);
14225
14226 if (num_printed == 0)
14227 {
14228 if (args == NULL || *args == '\0')
14229 uiout->message ("No tracepoints.\n");
14230 else
14231 uiout->message ("No tracepoint matching '%s'.\n", args);
14232 }
14233
14234 default_collect_info ();
14235 }
14236
14237 /* The 'enable trace' command enables tracepoints.
14238 Not supported by all targets. */
14239 static void
enable_trace_command(const char * args,int from_tty)14240 enable_trace_command (const char *args, int from_tty)
14241 {
14242 enable_command (args, from_tty);
14243 }
14244
14245 /* The 'disable trace' command disables tracepoints.
14246 Not supported by all targets. */
14247 static void
disable_trace_command(const char * args,int from_tty)14248 disable_trace_command (const char *args, int from_tty)
14249 {
14250 disable_command (args, from_tty);
14251 }
14252
14253 /* Remove a tracepoint (or all if no argument). */
14254 static void
delete_trace_command(const char * arg,int from_tty)14255 delete_trace_command (const char *arg, int from_tty)
14256 {
14257 dont_repeat ();
14258
14259 if (arg == 0)
14260 {
14261 int breaks_to_delete = 0;
14262
14263 /* Delete all tracepoints if no argument.
14264 Do not delete internal or call-dummy breakpoints, these
14265 have to be deleted with an explicit breakpoint number
14266 argument. */
14267 for (breakpoint &tp : all_tracepoints ())
14268 if (is_tracepoint (&tp) && user_breakpoint_p (&tp))
14269 {
14270 breaks_to_delete = 1;
14271 break;
14272 }
14273
14274 /* Ask user only if there are some breakpoints to delete. */
14275 if (!from_tty
14276 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14277 {
14278 for (breakpoint &b : all_breakpoints_safe ())
14279 if (is_tracepoint (&b) && user_breakpoint_p (&b))
14280 delete_breakpoint (&b);
14281 }
14282 }
14283 else
14284 map_breakpoint_numbers
14285 (arg, [&] (breakpoint *br)
14286 {
14287 iterate_over_related_breakpoints (br, delete_breakpoint);
14288 });
14289 }
14290
14291 /* Helper function for trace_pass_command. */
14292
14293 static void
trace_pass_set_count(struct tracepoint * tp,int count,int from_tty)14294 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14295 {
14296 tp->pass_count = count;
14297 notify_breakpoint_modified (tp);
14298 if (from_tty)
14299 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
14300 tp->number, count);
14301 }
14302
14303 /* Set passcount for tracepoint.
14304
14305 First command argument is passcount, second is tracepoint number.
14306 If tracepoint number omitted, apply to most recently defined.
14307 Also accepts special argument "all". */
14308
14309 static void
trace_pass_command(const char * args,int from_tty)14310 trace_pass_command (const char *args, int from_tty)
14311 {
14312 ULONGEST count;
14313
14314 if (args == 0 || *args == 0)
14315 error (_("passcount command requires an "
14316 "argument (count + optional TP num)"));
14317
14318 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14319
14320 args = skip_spaces (args);
14321 if (*args && strncasecmp (args, "all", 3) == 0)
14322 {
14323 args += 3; /* Skip special argument "all". */
14324 if (*args)
14325 error (_("Junk at end of arguments."));
14326
14327 for (breakpoint &b : all_tracepoints ())
14328 {
14329 tracepoint &t1 = gdb::checked_static_cast<tracepoint &> (b);
14330 trace_pass_set_count (&t1, count, from_tty);
14331 }
14332 }
14333 else if (*args == '\0')
14334 {
14335 tracepoint *t1 = get_tracepoint_by_number (&args, NULL);
14336 if (t1)
14337 trace_pass_set_count (t1, count, from_tty);
14338 }
14339 else
14340 {
14341 number_or_range_parser parser (args);
14342 while (!parser.finished ())
14343 {
14344 tracepoint *t1 = get_tracepoint_by_number (&args, &parser);
14345 if (t1)
14346 trace_pass_set_count (t1, count, from_tty);
14347 }
14348 }
14349 }
14350
14351 struct tracepoint *
get_tracepoint(int num)14352 get_tracepoint (int num)
14353 {
14354 for (breakpoint &t : all_tracepoints ())
14355 if (t.number == num)
14356 return gdb::checked_static_cast<tracepoint *> (&t);
14357
14358 return NULL;
14359 }
14360
14361 /* Find the tracepoint with the given target-side number (which may be
14362 different from the tracepoint number after disconnecting and
14363 reconnecting). */
14364
14365 struct tracepoint *
get_tracepoint_by_number_on_target(int num)14366 get_tracepoint_by_number_on_target (int num)
14367 {
14368 for (breakpoint &b : all_tracepoints ())
14369 {
14370 tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
14371
14372 if (t.number_on_target == num)
14373 return &t;
14374 }
14375
14376 return NULL;
14377 }
14378
14379 /* Utility: parse a tracepoint number and look it up in the list.
14380 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14381 If the argument is missing, the most recent tracepoint
14382 (tracepoint_count) is returned. */
14383
14384 struct tracepoint *
get_tracepoint_by_number(const char ** arg,number_or_range_parser * parser)14385 get_tracepoint_by_number (const char **arg,
14386 number_or_range_parser *parser)
14387 {
14388 int tpnum;
14389 const char *instring = arg == NULL ? NULL : *arg;
14390
14391 if (parser != NULL)
14392 {
14393 gdb_assert (!parser->finished ());
14394 tpnum = parser->get_number ();
14395 }
14396 else if (arg == NULL || *arg == NULL || ! **arg)
14397 tpnum = tracepoint_count;
14398 else
14399 tpnum = get_number (arg);
14400
14401 if (tpnum <= 0)
14402 {
14403 if (instring && *instring)
14404 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14405 instring);
14406 else
14407 gdb_printf (_("No previous tracepoint\n"));
14408 return NULL;
14409 }
14410
14411 for (breakpoint &t : all_tracepoints ())
14412 if (t.number == tpnum)
14413 return gdb::checked_static_cast<tracepoint *> (&t);
14414
14415 gdb_printf ("No tracepoint number %d.\n", tpnum);
14416 return NULL;
14417 }
14418
14419 void
print_recreate_thread(struct ui_file * fp)14420 breakpoint::print_recreate_thread (struct ui_file *fp) const
14421 {
14422 if (thread != -1)
14423 {
14424 struct thread_info *thr = find_thread_global_id (thread);
14425 gdb_printf (fp, " thread %s", print_full_thread_id (thr));
14426 }
14427
14428 if (task != -1)
14429 gdb_printf (fp, " task %d", task);
14430
14431 gdb_printf (fp, "\n");
14432 }
14433
14434 /* Save information on user settable breakpoints (watchpoints, etc) to
14435 a new script file named FILENAME. If FILTER is non-NULL, call it
14436 on each breakpoint and only include the ones for which it returns
14437 true. */
14438
14439 static void
save_breakpoints(const char * filename,int from_tty,bool (* filter)(const struct breakpoint *))14440 save_breakpoints (const char *filename, int from_tty,
14441 bool (*filter) (const struct breakpoint *))
14442 {
14443 bool any = false;
14444 int extra_trace_bits = 0;
14445
14446 if (filename == 0 || *filename == 0)
14447 error (_("Argument required (file name in which to save)"));
14448
14449 /* See if we have anything to save. */
14450 for (breakpoint &tp : all_breakpoints ())
14451 {
14452 /* Skip internal and momentary breakpoints. */
14453 if (!user_breakpoint_p (&tp))
14454 continue;
14455
14456 /* If we have a filter, only save the breakpoints it accepts. */
14457 if (filter && !filter (&tp))
14458 continue;
14459
14460 any = true;
14461
14462 if (is_tracepoint (&tp))
14463 {
14464 extra_trace_bits = 1;
14465
14466 /* We can stop searching. */
14467 break;
14468 }
14469 }
14470
14471 if (!any)
14472 {
14473 warning (_("Nothing to save."));
14474 return;
14475 }
14476
14477 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14478
14479 stdio_file fp;
14480
14481 if (!fp.open (expanded_filename.get (), "w"))
14482 error (_("Unable to open file '%s' for saving (%s)"),
14483 expanded_filename.get (), safe_strerror (errno));
14484
14485 if (extra_trace_bits)
14486 save_trace_state_variables (&fp);
14487
14488 for (breakpoint &tp : all_breakpoints ())
14489 {
14490 /* Skip internal and momentary breakpoints. */
14491 if (!user_breakpoint_p (&tp))
14492 continue;
14493
14494 /* If we have a filter, only save the breakpoints it accepts. */
14495 if (filter && !filter (&tp))
14496 continue;
14497
14498 tp.print_recreate (&fp);
14499
14500 /* Note, we can't rely on tp->number for anything, as we can't
14501 assume the recreated breakpoint numbers will match. Use $bpnum
14502 instead. */
14503
14504 if (tp.cond_string)
14505 fp.printf (" condition $bpnum %s\n", tp.cond_string.get ());
14506
14507 if (tp.ignore_count)
14508 fp.printf (" ignore $bpnum %d\n", tp.ignore_count);
14509
14510 if (tp.type != bp_dprintf && tp.commands)
14511 {
14512 fp.puts (" commands\n");
14513
14514 ui_out_redirect_pop redir (current_uiout, &fp);
14515 print_command_lines (current_uiout, tp.commands.get (), 2);
14516
14517 fp.puts (" end\n");
14518 }
14519
14520 if (tp.enable_state == bp_disabled)
14521 fp.puts ("disable $bpnum\n");
14522
14523 /* If this is a multi-location breakpoint, check if the locations
14524 should be individually disabled. Watchpoint locations are
14525 special, and not user visible. */
14526 if (!is_watchpoint (&tp) && tp.has_multiple_locations ())
14527 {
14528 int n = 1;
14529
14530 for (bp_location &loc : tp.locations ())
14531 {
14532 if (!loc.enabled)
14533 fp.printf ("disable $bpnum.%d\n", n);
14534
14535 n++;
14536 }
14537 }
14538 }
14539
14540 if (extra_trace_bits && !default_collect.empty ())
14541 fp.printf ("set default-collect %s\n", default_collect.c_str ());
14542
14543 if (from_tty)
14544 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
14545 }
14546
14547 /* The `save breakpoints' command. */
14548
14549 static void
save_breakpoints_command(const char * args,int from_tty)14550 save_breakpoints_command (const char *args, int from_tty)
14551 {
14552 save_breakpoints (args, from_tty, NULL);
14553 }
14554
14555 /* The `save tracepoints' command. */
14556
14557 static void
save_tracepoints_command(const char * args,int from_tty)14558 save_tracepoints_command (const char *args, int from_tty)
14559 {
14560 save_breakpoints (args, from_tty, is_tracepoint);
14561 }
14562
14563
14564 /* This help string is used to consolidate all the help string for specifying
14565 locations used by several commands. */
14566
14567 #define LOCATION_SPEC_HELP_STRING \
14568 "Linespecs are colon-separated lists of location parameters, such as\n\
14569 source filename, function name, label name, and line number.\n\
14570 Example: To specify the start of a label named \"the_top\" in the\n\
14571 function \"fact\" in the file \"factorial.c\", use\n\
14572 \"factorial.c:fact:the_top\".\n\
14573 \n\
14574 Address locations begin with \"*\" and specify an exact address in the\n\
14575 program. Example: To specify the fourth byte past the start function\n\
14576 \"main\", use \"*main + 4\".\n\
14577 \n\
14578 Explicit locations are similar to linespecs but use an option/argument\n\
14579 syntax to specify location parameters.\n\
14580 Example: To specify the start of the label named \"the_top\" in the\n\
14581 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
14582 -function fact -label the_top\".\n\
14583 \n\
14584 By default, a specified function is matched against the program's\n\
14585 functions in all scopes. For C++, this means in all namespaces and\n\
14586 classes. For Ada, this means in all packages. E.g., in C++,\n\
14587 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14588 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
14589 specified name as a complete fully-qualified name instead."
14590
14591 /* This help string is used for the break, hbreak, tbreak and thbreak
14592 commands. It is defined as a macro to prevent duplication.
14593 COMMAND should be a string constant containing the name of the
14594 command. */
14595
14596 #define BREAK_ARGS_HELP(command) \
14597 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14598 \t[-force-condition] [if CONDITION]\n\
14599 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14600 probe point. Accepted values are `-probe' (for a generic, automatically\n\
14601 guessed probe type), `-probe-stap' (for a SystemTap probe) or\n\
14602 `-probe-dtrace' (for a DTrace probe).\n\
14603 LOCATION may be a linespec, address, or explicit location as described\n\
14604 below.\n\
14605 \n\
14606 With no LOCATION, uses current execution address of the selected\n\
14607 stack frame. This is useful for breaking on return to a stack frame.\n\
14608 \n\
14609 THREADNUM is the number from \"info threads\".\n\
14610 CONDITION is a boolean expression.\n\
14611 \n\
14612 With the \"-force-condition\" flag, the condition is defined even when\n\
14613 it is invalid for all current locations.\n\
14614 \n" LOCATION_SPEC_HELP_STRING "\n\n\
14615 Multiple breakpoints at one place are permitted, and useful if their\n\
14616 conditions are different.\n\
14617 \n\
14618 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14619
14620 /* List of subcommands for "catch". */
14621 static struct cmd_list_element *catch_cmdlist;
14622
14623 /* List of subcommands for "tcatch". */
14624 static struct cmd_list_element *tcatch_cmdlist;
14625
14626 void
add_catch_command(const char * name,const char * docstring,cmd_func_ftype * func,completer_ftype * completer,void * user_data_catch,void * user_data_tcatch)14627 add_catch_command (const char *name, const char *docstring,
14628 cmd_func_ftype *func,
14629 completer_ftype *completer,
14630 void *user_data_catch,
14631 void *user_data_tcatch)
14632 {
14633 struct cmd_list_element *command;
14634
14635 command = add_cmd (name, class_breakpoint, docstring,
14636 &catch_cmdlist);
14637 command->func = func;
14638 command->set_context (user_data_catch);
14639 set_cmd_completer (command, completer);
14640
14641 command = add_cmd (name, class_breakpoint, docstring,
14642 &tcatch_cmdlist);
14643 command->func = func;
14644 command->set_context (user_data_tcatch);
14645 set_cmd_completer (command, completer);
14646 }
14647
14648 /* False if any of the breakpoint's locations could be a location where
14649 functions have been inlined, true otherwise. */
14650
14651 static bool
is_non_inline_function(struct breakpoint * b)14652 is_non_inline_function (struct breakpoint *b)
14653 {
14654 /* The shared library event breakpoint is set on the address of a
14655 non-inline function. */
14656 return (b->type == bp_shlib_event);
14657 }
14658
14659 /* Nonzero if the specified PC cannot be a location where functions
14660 have been inlined. */
14661
14662 int
pc_at_non_inline_function(const address_space * aspace,CORE_ADDR pc,const target_waitstatus & ws)14663 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
14664 const target_waitstatus &ws)
14665 {
14666 for (breakpoint &b : all_breakpoints ())
14667 {
14668 if (!is_non_inline_function (&b))
14669 continue;
14670
14671 for (bp_location &bl : b.locations ())
14672 {
14673 if (!bl.shlib_disabled
14674 && bpstat_check_location (&bl, aspace, pc, ws))
14675 return 1;
14676 }
14677 }
14678
14679 return 0;
14680 }
14681
14682 /* Remove any references to OBJFILE which is going to be freed. */
14683
14684 void
breakpoint_free_objfile(struct objfile * objfile)14685 breakpoint_free_objfile (struct objfile *objfile)
14686 {
14687 for (bp_location *loc : all_bp_locations ())
14688 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
14689 loc->symtab = NULL;
14690 }
14691
14692 /* Chain containing all defined "enable breakpoint" subcommands. */
14693
14694 static struct cmd_list_element *enablebreaklist = NULL;
14695
14696 /* See breakpoint.h. */
14697
14698 cmd_list_element *commands_cmd_element = nullptr;
14699
14700 void _initialize_breakpoint ();
14701 void
_initialize_breakpoint()14702 _initialize_breakpoint ()
14703 {
14704 struct cmd_list_element *c;
14705
14706 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14707 "breakpoint");
14708 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14709 "breakpoint");
14710 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14711 "breakpoint");
14712
14713 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14714 before a breakpoint is set. */
14715 breakpoint_count = 0;
14716
14717 tracepoint_count = 0;
14718
14719 add_com ("ignore", class_breakpoint, ignore_command, _("\
14720 Set ignore-count of breakpoint number N to COUNT.\n\
14721 Usage is `ignore N COUNT'."));
14722
14723 commands_cmd_element = add_com ("commands", class_breakpoint,
14724 commands_command, _("\
14725 Set commands to be executed when the given breakpoints are hit.\n\
14726 Give a space-separated breakpoint list as argument after \"commands\".\n\
14727 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14728 (e.g. `5-7').\n\
14729 With no argument, the targeted breakpoint is the last one set.\n\
14730 The commands themselves follow starting on the next line.\n\
14731 Type a line containing \"end\" to indicate the end of them.\n\
14732 Give \"silent\" as the first line to make the breakpoint silent;\n\
14733 then no output is printed when it is hit, except what the commands print."));
14734
14735 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14736 static std::string condition_command_help
14737 = gdb::option::build_help (_("\
14738 Specify breakpoint number N to break only if COND is true.\n\
14739 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
14740 is an expression to be evaluated whenever breakpoint N is reached.\n\
14741 \n\
14742 Options:\n\
14743 %OPTIONS%"), cc_opts);
14744
14745 c = add_com ("condition", class_breakpoint, condition_command,
14746 condition_command_help.c_str ());
14747 set_cmd_completer_handle_brkchars (c, condition_completer);
14748
14749 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
14750 Set a temporary breakpoint.\n\
14751 Like \"break\" except the breakpoint is only temporary,\n\
14752 so it will be deleted when hit. Equivalent to \"break\" followed\n\
14753 by using \"enable delete\" on the breakpoint number.\n\
14754 \n"
14755 BREAK_ARGS_HELP ("tbreak")));
14756 set_cmd_completer (c, location_completer);
14757
14758 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
14759 Set a hardware assisted breakpoint.\n\
14760 Like \"break\" except the breakpoint requires hardware support,\n\
14761 some target hardware may not have this support.\n\
14762 \n"
14763 BREAK_ARGS_HELP ("hbreak")));
14764 set_cmd_completer (c, location_completer);
14765
14766 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
14767 Set a temporary hardware assisted breakpoint.\n\
14768 Like \"hbreak\" except the breakpoint is only temporary,\n\
14769 so it will be deleted when hit.\n\
14770 \n"
14771 BREAK_ARGS_HELP ("thbreak")));
14772 set_cmd_completer (c, location_completer);
14773
14774 cmd_list_element *enable_cmd
14775 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14776 Enable all or some breakpoints.\n\
14777 Usage: enable [BREAKPOINTNUM]...\n\
14778 Give breakpoint numbers (separated by spaces) as arguments.\n\
14779 With no subcommand, breakpoints are enabled until you command otherwise.\n\
14780 This is used to cancel the effect of the \"disable\" command.\n\
14781 With a subcommand you can enable temporarily."),
14782 &enablelist, 1, &cmdlist);
14783
14784 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
14785
14786 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
14787 Enable all or some breakpoints.\n\
14788 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
14789 Give breakpoint numbers (separated by spaces) as arguments.\n\
14790 This is used to cancel the effect of the \"disable\" command.\n\
14791 May be abbreviated to simply \"enable\"."),
14792 &enablebreaklist, 1, &enablelist);
14793
14794 add_cmd ("once", no_class, enable_once_command, _("\
14795 Enable some breakpoints for one hit.\n\
14796 Usage: enable breakpoints once BREAKPOINTNUM...\n\
14797 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14798 &enablebreaklist);
14799
14800 add_cmd ("delete", no_class, enable_delete_command, _("\
14801 Enable some breakpoints and delete when hit.\n\
14802 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
14803 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14804 &enablebreaklist);
14805
14806 add_cmd ("count", no_class, enable_count_command, _("\
14807 Enable some breakpoints for COUNT hits.\n\
14808 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
14809 If a breakpoint is hit while enabled in this fashion,\n\
14810 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14811 &enablebreaklist);
14812
14813 add_cmd ("delete", no_class, enable_delete_command, _("\
14814 Enable some breakpoints and delete when hit.\n\
14815 Usage: enable delete BREAKPOINTNUM...\n\
14816 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14817 &enablelist);
14818
14819 add_cmd ("once", no_class, enable_once_command, _("\
14820 Enable some breakpoints for one hit.\n\
14821 Usage: enable once BREAKPOINTNUM...\n\
14822 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14823 &enablelist);
14824
14825 add_cmd ("count", no_class, enable_count_command, _("\
14826 Enable some breakpoints for COUNT hits.\n\
14827 Usage: enable count COUNT BREAKPOINTNUM...\n\
14828 If a breakpoint is hit while enabled in this fashion,\n\
14829 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14830 &enablelist);
14831
14832 cmd_list_element *disable_cmd
14833 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14834 Disable all or some breakpoints.\n\
14835 Usage: disable [BREAKPOINTNUM]...\n\
14836 Arguments are breakpoint numbers with spaces in between.\n\
14837 To disable all breakpoints, give no argument.\n\
14838 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
14839 &disablelist, 1, &cmdlist);
14840 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14841 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
14842
14843 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
14844 Disable all or some breakpoints.\n\
14845 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
14846 Arguments are breakpoint numbers with spaces in between.\n\
14847 To disable all breakpoints, give no argument.\n\
14848 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
14849 This command may be abbreviated \"disable\"."),
14850 &disablelist);
14851
14852 cmd_list_element *delete_cmd
14853 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14854 Delete all or some breakpoints.\n\
14855 Usage: delete [BREAKPOINTNUM]...\n\
14856 Arguments are breakpoint numbers with spaces in between.\n\
14857 To delete all breakpoints, watchpoints, tracepoints, and catchpoints,\n\
14858 give no argument.\n\
14859 \n\
14860 Also a prefix command for deletion of other GDB objects."),
14861 &deletelist, 1, &cmdlist);
14862 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14863 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
14864
14865 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
14866 Delete all or some breakpoints or auto-display expressions.\n\
14867 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
14868 Arguments are breakpoint numbers with spaces in between.\n\
14869 To delete all breakpoints, watchpoints, tracepoints, and catchpoints,\n\
14870 give no argument.\n\
14871 This command may be abbreviated \"delete\"."),
14872 &deletelist);
14873
14874 cmd_list_element *clear_cmd
14875 = add_com ("clear", class_breakpoint, clear_command, _("\
14876 Clear breakpoint at specified location.\n\
14877 Argument may be a linespec, explicit, or address location as described below.\n\
14878 \n\
14879 With no argument, clears all breakpoints in the line that the selected frame\n\
14880 is executing in.\n"
14881 "\n" LOCATION_SPEC_HELP_STRING "\n\n\
14882 See also the \"delete\" command which clears breakpoints by number."));
14883 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
14884
14885 cmd_list_element *break_cmd
14886 = add_com ("break", class_breakpoint, break_command, _("\
14887 Set breakpoint at specified location.\n"
14888 BREAK_ARGS_HELP ("break")));
14889 set_cmd_completer (break_cmd, location_completer);
14890
14891 add_com_alias ("b", break_cmd, class_run, 1);
14892 add_com_alias ("br", break_cmd, class_run, 1);
14893 add_com_alias ("bre", break_cmd, class_run, 1);
14894 add_com_alias ("brea", break_cmd, class_run, 1);
14895
14896 cmd_list_element *info_breakpoints_cmd
14897 = add_info ("breakpoints", info_breakpoints_command, _("\
14898 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
14899 The \"Type\" column indicates one of:\n\
14900 \tbreakpoint - normal breakpoint\n\
14901 \twatchpoint - watchpoint\n\
14902 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14903 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14904 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14905 address and file/line number respectively.\n\
14906 \n\
14907 Convenience variable \"$_\" and default examine address for \"x\"\n\
14908 are set to the address of the last breakpoint listed unless the command\n\
14909 is prefixed with \"server \".\n\n\
14910 Convenience variable \"$bpnum\" contains the number of the last\n\
14911 breakpoint set."));
14912
14913 add_info_alias ("b", info_breakpoints_cmd, 1);
14914
14915 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14916 Status of all breakpoints, or breakpoint number NUMBER.\n\
14917 The \"Type\" column indicates one of:\n\
14918 \tbreakpoint - normal breakpoint\n\
14919 \twatchpoint - watchpoint\n\
14920 \tlongjmp - internal breakpoint used to step through longjmp()\n\
14921 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14922 \tuntil - internal breakpoint used by the \"until\" command\n\
14923 \tfinish - internal breakpoint used by the \"finish\" command\n\
14924 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14925 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14926 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14927 address and file/line number respectively.\n\
14928 \n\
14929 Convenience variable \"$_\" and default examine address for \"x\"\n\
14930 are set to the address of the last breakpoint listed unless the command\n\
14931 is prefixed with \"server \".\n\n\
14932 Convenience variable \"$bpnum\" contains the number of the last\n\
14933 breakpoint set."),
14934 &maintenanceinfolist);
14935
14936 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
14937 Set catchpoints to catch events."),
14938 &catch_cmdlist,
14939 0/*allow-unknown*/, &cmdlist);
14940
14941 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
14942 Set temporary catchpoints to catch events."),
14943 &tcatch_cmdlist,
14944 0/*allow-unknown*/, &cmdlist);
14945
14946 const auto opts = make_watch_options_def_group (nullptr);
14947
14948 static const std::string watch_help = gdb::option::build_help (_("\
14949 Set a watchpoint for EXPRESSION.\n\
14950 Usage: watch [-location] EXPRESSION\n\
14951 \n\
14952 Options:\n\
14953 %OPTIONS%\n\
14954 \n\
14955 A watchpoint stops execution of your program whenever the value of\n\
14956 an expression changes."), opts);
14957 c = add_com ("watch", class_breakpoint, watch_command,
14958 watch_help.c_str ());
14959 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14960
14961 static const std::string rwatch_help = gdb::option::build_help (_("\
14962 Set a read watchpoint for EXPRESSION.\n\
14963 Usage: rwatch [-location] EXPRESSION\n\
14964 \n\
14965 Options:\n\
14966 %OPTIONS%\n\
14967 \n\
14968 A read watchpoint stops execution of your program whenever the value of\n\
14969 an expression is read."), opts);
14970 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14971 rwatch_help.c_str ());
14972 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14973
14974 static const std::string awatch_help = gdb::option::build_help (_("\
14975 Set an access watchpoint for EXPRESSION.\n\
14976 Usage: awatch [-location] EXPRESSION\n\
14977 \n\
14978 Options:\n\
14979 %OPTIONS%\n\
14980 \n\
14981 An access watchpoint stops execution of your program whenever the value\n\
14982 of an expression is either read or written."), opts);
14983 c = add_com ("awatch", class_breakpoint, awatch_command,
14984 awatch_help.c_str ());
14985 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14986
14987 add_info ("watchpoints", info_watchpoints_command, _("\
14988 Status of specified watchpoints (all watchpoints if no argument)."));
14989
14990 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14991 respond to changes - contrary to the description. */
14992 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14993 &can_use_hw_watchpoints, _("\
14994 Set debugger's willingness to use watchpoint hardware."), _("\
14995 Show debugger's willingness to use watchpoint hardware."), _("\
14996 If zero, gdb will not use hardware for new watchpoints, even if\n\
14997 such is available. (However, any hardware watchpoints that were\n\
14998 created before setting this to nonzero, will continue to use watchpoint\n\
14999 hardware.)"),
15000 NULL,
15001 show_can_use_hw_watchpoints,
15002 &setlist, &showlist);
15003
15004 can_use_hw_watchpoints = 1;
15005
15006 /* Tracepoint manipulation commands. */
15007
15008 cmd_list_element *trace_cmd
15009 = add_com ("trace", class_breakpoint, trace_command, _("\
15010 Set a tracepoint at specified location.\n\
15011 \n"
15012 BREAK_ARGS_HELP ("trace") "\n\
15013 Do \"help tracepoints\" for info on other tracepoint commands."));
15014 set_cmd_completer (trace_cmd, location_completer);
15015
15016 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15017 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15018 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15019 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
15020
15021 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15022 Set a fast tracepoint at specified location.\n\
15023 \n"
15024 BREAK_ARGS_HELP ("ftrace") "\n\
15025 Do \"help tracepoints\" for info on other tracepoint commands."));
15026 set_cmd_completer (c, location_completer);
15027
15028 c = add_com ("strace", class_breakpoint, strace_command, _("\
15029 Set a static tracepoint at location or marker.\n\
15030 \n\
15031 strace [LOCATION] [if CONDITION]\n\
15032 LOCATION may be a linespec, explicit, or address location (described below)\n\
15033 or -m MARKER_ID.\n\n\
15034 If a marker id is specified, probe the marker with that name. With\n\
15035 no LOCATION, uses current execution address of the selected stack frame.\n\
15036 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15037 This collects arbitrary user data passed in the probe point call to the\n\
15038 tracing library. You can inspect it when analyzing the trace buffer,\n\
15039 by printing the $_sdata variable like any other convenience variable.\n\
15040 \n\
15041 CONDITION is a boolean expression.\n\
15042 \n" LOCATION_SPEC_HELP_STRING "\n\n\
15043 Multiple tracepoints at one place are permitted, and useful if their\n\
15044 conditions are different.\n\
15045 \n\
15046 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15047 Do \"help tracepoints\" for info on other tracepoint commands."));
15048 set_cmd_completer (c, location_completer);
15049
15050 cmd_list_element *info_tracepoints_cmd
15051 = add_info ("tracepoints", info_tracepoints_command, _("\
15052 Status of specified tracepoints (all tracepoints if no argument).\n\
15053 Convenience variable \"$tpnum\" contains the number of the\n\
15054 last tracepoint set."));
15055
15056 add_info_alias ("tp", info_tracepoints_cmd, 1);
15057
15058 cmd_list_element *delete_tracepoints_cmd
15059 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15060 Delete specified tracepoints.\n\
15061 Arguments are tracepoint numbers, separated by spaces.\n\
15062 No argument means delete all tracepoints."),
15063 &deletelist);
15064 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
15065
15066 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15067 Disable specified tracepoints.\n\
15068 Arguments are tracepoint numbers, separated by spaces.\n\
15069 No argument means disable all tracepoints."),
15070 &disablelist);
15071 deprecate_cmd (c, "disable");
15072
15073 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15074 Enable specified tracepoints.\n\
15075 Arguments are tracepoint numbers, separated by spaces.\n\
15076 No argument means enable all tracepoints."),
15077 &enablelist);
15078 deprecate_cmd (c, "enable");
15079
15080 add_com ("passcount", class_trace, trace_pass_command, _("\
15081 Set the passcount for a tracepoint.\n\
15082 The trace will end when the tracepoint has been passed 'count' times.\n\
15083 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15084 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15085
15086 add_basic_prefix_cmd ("save", class_breakpoint,
15087 _("Save breakpoint definitions as a script."),
15088 &save_cmdlist,
15089 0/*allow-unknown*/, &cmdlist);
15090
15091 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15092 Save current breakpoint definitions as a script.\n\
15093 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15094 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15095 session to restore them."),
15096 &save_cmdlist);
15097 set_cmd_completer (c, filename_completer);
15098
15099 cmd_list_element *save_tracepoints_cmd
15100 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15101 Save current tracepoint definitions as a script.\n\
15102 Use the 'source' command in another debug session to restore them."),
15103 &save_cmdlist);
15104 set_cmd_completer (save_tracepoints_cmd, filename_completer);
15105
15106 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
15107 deprecate_cmd (c, "save tracepoints");
15108
15109 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
15110 _("\
15111 Breakpoint specific settings.\n\
15112 Configure various breakpoint-specific variables such as\n\
15113 pending breakpoint behavior."),
15114 _("\
15115 Breakpoint specific settings.\n\
15116 Configure various breakpoint-specific variables such as\n\
15117 pending breakpoint behavior."),
15118 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
15119 &setlist, &showlist);
15120
15121 add_setshow_auto_boolean_cmd ("pending", no_class,
15122 &pending_break_support, _("\
15123 Set debugger's behavior regarding pending breakpoints."), _("\
15124 Show debugger's behavior regarding pending breakpoints."), _("\
15125 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15126 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15127 an error. If auto, an unrecognized breakpoint location results in a\n\
15128 user-query to see if a pending breakpoint should be created."),
15129 NULL,
15130 show_pending_break_support,
15131 &breakpoint_set_cmdlist,
15132 &breakpoint_show_cmdlist);
15133
15134 pending_break_support = AUTO_BOOLEAN_AUTO;
15135
15136 add_setshow_boolean_cmd ("auto-hw", no_class,
15137 &automatic_hardware_breakpoints, _("\
15138 Set automatic usage of hardware breakpoints."), _("\
15139 Show automatic usage of hardware breakpoints."), _("\
15140 If set, the debugger will automatically use hardware breakpoints for\n\
15141 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15142 a warning will be emitted for such breakpoints."),
15143 NULL,
15144 show_automatic_hardware_breakpoints,
15145 &breakpoint_set_cmdlist,
15146 &breakpoint_show_cmdlist);
15147
15148 add_setshow_boolean_cmd ("always-inserted", class_support,
15149 &always_inserted_mode, _("\
15150 Set mode for inserting breakpoints."), _("\
15151 Show mode for inserting breakpoints."), _("\
15152 When this mode is on, breakpoints are inserted immediately as soon as\n\
15153 they're created, kept inserted even when execution stops, and removed\n\
15154 only when the user deletes them. When this mode is off (the default),\n\
15155 breakpoints are inserted only when execution continues, and removed\n\
15156 when execution stops."),
15157 NULL,
15158 &show_always_inserted_mode,
15159 &breakpoint_set_cmdlist,
15160 &breakpoint_show_cmdlist);
15161
15162 add_setshow_boolean_cmd ("breakpoint", class_maintenance,
15163 &debug_breakpoint, _("\
15164 Set breakpoint location debugging."), _("\
15165 Show breakpoint location debugging."), _("\
15166 When on, breakpoint location specific debugging is enabled."),
15167 NULL,
15168 show_debug_breakpoint,
15169 &setdebuglist, &showdebuglist);
15170
15171 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15172 condition_evaluation_enums,
15173 &condition_evaluation_mode_1, _("\
15174 Set mode of breakpoint condition evaluation."), _("\
15175 Show mode of breakpoint condition evaluation."), _("\
15176 When this is set to \"host\", breakpoint conditions will be\n\
15177 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15178 breakpoint conditions will be downloaded to the target (if the target\n\
15179 supports such feature) and conditions will be evaluated on the target's side.\n\
15180 If this is set to \"auto\" (default), this will be automatically set to\n\
15181 \"target\" if it supports condition evaluation, otherwise it will\n\
15182 be set to \"host\"."),
15183 &set_condition_evaluation_mode,
15184 &show_condition_evaluation_mode,
15185 &breakpoint_set_cmdlist,
15186 &breakpoint_show_cmdlist);
15187
15188 add_com ("break-range", class_breakpoint, break_range_command, _("\
15189 Set a breakpoint for an address range.\n\
15190 break-range START-LOCATION, END-LOCATION\n\
15191 where START-LOCATION and END-LOCATION can be one of the following:\n\
15192 LINENUM, for that line in the current file,\n\
15193 FILE:LINENUM, for that line in that file,\n\
15194 +OFFSET, for that number of lines after the current line\n\
15195 or the start of the range\n\
15196 FUNCTION, for the first line in that function,\n\
15197 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15198 *ADDRESS, for the instruction at that address.\n\
15199 \n\
15200 The breakpoint will stop execution of the inferior whenever it executes\n\
15201 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15202 range (including START-LOCATION and END-LOCATION)."));
15203
15204 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15205 Set a dynamic printf at specified location.\n\
15206 dprintf location,format string,arg1,arg2,...\n\
15207 location may be a linespec, explicit, or address location.\n"
15208 "\n" LOCATION_SPEC_HELP_STRING));
15209 set_cmd_completer (c, location_completer);
15210
15211 add_setshow_enum_cmd ("dprintf-style", class_support,
15212 dprintf_style_enums, &dprintf_style, _("\
15213 Set the style of usage for dynamic printf."), _("\
15214 Show the style of usage for dynamic printf."), _("\
15215 This setting chooses how GDB will do a dynamic printf.\n\
15216 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15217 console, as with the \"printf\" command.\n\
15218 If the value is \"call\", the print is done by calling a function in your\n\
15219 program; by default printf(), but you can choose a different function or\n\
15220 output stream by setting dprintf-function and dprintf-channel."),
15221 update_dprintf_commands, NULL,
15222 &setlist, &showlist);
15223
15224 add_setshow_string_cmd ("dprintf-function", class_support,
15225 &dprintf_function, _("\
15226 Set the function to use for dynamic printf."), _("\
15227 Show the function to use for dynamic printf."), NULL,
15228 update_dprintf_commands, NULL,
15229 &setlist, &showlist);
15230
15231 add_setshow_string_cmd ("dprintf-channel", class_support,
15232 &dprintf_channel, _("\
15233 Set the channel to use for dynamic printf."), _("\
15234 Show the channel to use for dynamic printf."), NULL,
15235 update_dprintf_commands, NULL,
15236 &setlist, &showlist);
15237
15238 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15239 &disconnected_dprintf, _("\
15240 Set whether dprintf continues after GDB disconnects."), _("\
15241 Show whether dprintf continues after GDB disconnects."), _("\
15242 Use this to let dprintf commands continue to hit and produce output\n\
15243 even if GDB disconnects or detaches from the target."),
15244 NULL,
15245 NULL,
15246 &setlist, &showlist);
15247
15248 add_com ("agent-printf", class_vars, agent_printf_command, _("\
15249 Target agent only formatted printing, like the C \"printf\" function.\n\
15250 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15251 This supports most C printf format specifications, like %s, %d, etc.\n\
15252 This is useful for formatted output in user-defined commands."));
15253
15254 automatic_hardware_breakpoints = true;
15255
15256 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
15257 "breakpoint");
15258 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
15259 "breakpoint");
15260 gdb::observers::inferior_removed.attach (remove_inferior_breakpoints,
15261 "breakpoint");
15262 }
15263