RISC-V: Don't report warnings when linking different privileged spec objects.
[binutils-gdb.git] / gdb / breakpoint.c
blob9983e905e1b4060161e5a2d37f180b60bd30815c
1 /* Everything about breakpoints, for GDB.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "arch-utils.h"
21 #include <ctype.h>
22 #include "event-top.h"
23 #include "exceptions.h"
24 #include "hashtab.h"
25 #include "symtab.h"
26 #include "frame.h"
27 #include "breakpoint.h"
28 #include "tracepoint.h"
29 #include "gdbtypes.h"
30 #include "expression.h"
31 #include "gdbcore.h"
32 #include "cli/cli-cmds.h"
33 #include "value.h"
34 #include "command.h"
35 #include "inferior.h"
36 #include "infrun.h"
37 #include "gdbthread.h"
38 #include "target.h"
39 #include "language.h"
40 #include "gdb-demangle.h"
41 #include "filenames.h"
42 #include "annotate.h"
43 #include "symfile.h"
44 #include "objfiles.h"
45 #include "source.h"
46 #include "linespec.h"
47 #include "completer.h"
48 #include "ui-out.h"
49 #include "cli/cli-script.h"
50 #include "block.h"
51 #include "solib.h"
52 #include "solist.h"
53 #include "observable.h"
54 #include "memattr.h"
55 #include "ada-lang.h"
56 #include "top.h"
57 #include "ui.h"
58 #include "valprint.h"
59 #include "jit.h"
60 #include "parser-defs.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 #include "break-cond-parse.h"
75 /* readline include files */
76 #include "readline/tilde.h"
78 /* readline defines this. */
79 #undef savestring
81 #include "mi/mi-common.h"
82 #include "extension.h"
83 #include <algorithm>
84 #include "progspace-and-thread.h"
85 #include "gdbsupport/array-view.h"
86 #include <optional>
87 #include "gdbsupport/common-utils.h"
89 /* Prototypes for local functions. */
91 static void map_breakpoint_numbers (const char *,
92 gdb::function_view<void (breakpoint *)>);
94 static void parse_breakpoint_sals (location_spec *locspec,
95 linespec_result *canonical,
96 program_space *search_pspace);
98 static void breakpoint_re_set_one (breakpoint *b,
99 program_space *filter_pspace);
101 static void create_breakpoints_sal (struct gdbarch *,
102 struct linespec_result *,
103 gdb::unique_xmalloc_ptr<char>,
104 gdb::unique_xmalloc_ptr<char>,
105 enum bptype,
106 enum bpdisp, int, int, int,
107 int,
108 int, int, int, unsigned);
110 static int can_use_hardware_watchpoint
111 (const std::vector<value_ref_ptr> &vals);
113 static void mention (const breakpoint *);
115 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
117 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
119 static struct breakpoint *
120 momentary_breakpoint_from_master (struct breakpoint *orig,
121 enum bptype type,
122 int loc_enabled, int thread);
124 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, bool);
126 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
127 CORE_ADDR bpaddr,
128 enum bptype bptype,
129 struct program_space *pspace);
131 static bool watchpoint_locations_match (const struct bp_location *loc1,
132 const struct bp_location *loc2);
134 static bool breakpoint_locations_match (const struct bp_location *loc1,
135 const struct bp_location *loc2,
136 bool sw_hw_bps_match = false);
138 static bool breakpoint_location_address_match (struct bp_location *bl,
139 const struct address_space *aspace,
140 CORE_ADDR addr);
142 static bool breakpoint_location_address_range_overlap (struct bp_location *,
143 const address_space *,
144 CORE_ADDR, int);
146 static int remove_breakpoint (struct bp_location *);
147 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
149 static enum print_stop_action print_bp_stop_message (bpstat *bs);
151 static int hw_breakpoint_used_count (void);
153 static int hw_watchpoint_use_count (struct breakpoint *);
155 static int hw_watchpoint_used_count_others (struct breakpoint *except,
156 enum bptype type,
157 int *other_type_used);
159 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
160 int count);
162 static void decref_bp_location (struct bp_location **loc);
164 static std::vector<symtab_and_line> bkpt_probe_decode_location_spec
165 (struct breakpoint *b,
166 location_spec *locspec,
167 struct program_space *search_pspace);
169 static bool bl_address_is_meaningful (bp_location *loc);
171 static int find_loc_num_by_location (const bp_location *loc);
173 /* update_global_location_list's modes of operation wrt to whether to
174 insert locations now. */
175 enum ugll_insert_mode
177 /* Don't insert any breakpoint locations into the inferior, only
178 remove already-inserted locations that no longer should be
179 inserted. Functions that delete a breakpoint or breakpoints
180 should specify this mode, so that deleting a breakpoint doesn't
181 have the side effect of inserting the locations of other
182 breakpoints that are marked not-inserted, but should_be_inserted
183 returns true on them.
185 This behavior is useful is situations close to tear-down -- e.g.,
186 after an exec, while the target still has execution, but
187 breakpoint shadows of the previous executable image should *NOT*
188 be restored to the new image; or before detaching, where the
189 target still has execution and wants to delete breakpoints from
190 GDB's lists, and all breakpoints had already been removed from
191 the inferior. */
192 UGLL_DONT_INSERT,
194 /* May insert breakpoints iff breakpoints_should_be_inserted_now
195 claims breakpoints should be inserted now. */
196 UGLL_MAY_INSERT,
198 /* Insert locations now, irrespective of
199 breakpoints_should_be_inserted_now. E.g., say all threads are
200 stopped right now, and the user did "continue". We need to
201 insert breakpoints _before_ resuming the target, but
202 UGLL_MAY_INSERT wouldn't insert them, because
203 breakpoints_should_be_inserted_now returns false at that point,
204 as no thread is running yet. */
205 UGLL_INSERT
208 /* Return a textual version of INSERT_MODE. */
210 static const char *
211 ugll_insert_mode_text (ugll_insert_mode insert_mode)
213 /* Make sure the compiler warns if a new ugll_insert_mode enumerator is added
214 but not handled here. */
215 DIAGNOSTIC_PUSH
216 DIAGNOSTIC_ERROR_SWITCH
217 switch (insert_mode)
219 case UGLL_DONT_INSERT:
220 return "UGLL_DONT_INSERT";
221 case UGLL_MAY_INSERT:
222 return "UGLL_MAY_INSERT";
223 case UGLL_INSERT:
224 return "UGLL_INSERT";
226 DIAGNOSTIC_POP
228 gdb_assert_not_reached ("must handle all enum values");
231 /* Return a textual version of REASON. */
233 static const char *
234 remove_bp_reason_str (remove_bp_reason reason)
236 /* Make sure the compiler warns if a new remove_bp_reason enumerator is added
237 but not handled here. */
238 DIAGNOSTIC_PUSH
239 DIAGNOSTIC_ERROR_SWITCH
240 switch (reason)
242 case REMOVE_BREAKPOINT:
243 return "regular remove";
244 case DETACH_BREAKPOINT:
245 return "detach";
247 DIAGNOSTIC_POP
249 gdb_assert_not_reached ("must handle all enum values");
252 /* Return a textual version of breakpoint location BL describing number,
253 location and address. */
255 static std::string
256 breakpoint_location_address_str (const bp_location *bl)
258 std::string str = string_printf ("Breakpoint %d (%s) at address %s",
259 bl->owner->number,
260 host_address_to_string (bl),
261 paddress (bl->gdbarch, bl->address));
263 std::string loc_string = bl->to_string ();
264 if (!loc_string.empty ())
265 str += string_printf (" %s", loc_string.c_str ());
267 return str;
270 static void update_global_location_list (enum ugll_insert_mode);
272 static void update_global_location_list_nothrow (enum ugll_insert_mode);
274 static void insert_breakpoint_locations (void);
276 static void trace_pass_command (const char *, int);
278 static void set_tracepoint_count (int num);
280 static bool is_masked_watchpoint (const struct breakpoint *b);
282 /* Return true if B refers to a static tracepoint set by marker ("-m"),
283 zero otherwise. */
285 static bool strace_marker_p (struct breakpoint *b);
287 static void bkpt_probe_create_sals_from_location_spec
288 (location_spec *locspec,
289 struct linespec_result *canonical,
290 struct program_space *search_pspace);
292 const struct breakpoint_ops code_breakpoint_ops =
294 parse_breakpoint_sals,
295 create_breakpoints_sal,
298 /* Breakpoints set on probes. */
299 static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
301 bkpt_probe_create_sals_from_location_spec,
302 create_breakpoints_sal,
305 /* Tracepoints set on probes. We use the same methods as for breakpoints
306 on probes. */
307 static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
309 bkpt_probe_create_sals_from_location_spec,
310 create_breakpoints_sal,
313 /* Implementation of abstract dtors. These must exist to satisfy the
314 linker. */
316 breakpoint::~breakpoint ()
320 code_breakpoint::~code_breakpoint ()
324 catchpoint::~catchpoint ()
328 /* The structure to be used in regular breakpoints. */
329 struct ordinary_breakpoint : public code_breakpoint
331 using code_breakpoint::code_breakpoint;
333 int resources_needed (const struct bp_location *) override;
334 enum print_stop_action print_it (const bpstat *bs) const override;
335 void print_mention () const override;
336 void print_recreate (struct ui_file *fp) const override;
339 /* Internal breakpoints. These typically have a lifetime the same as
340 the program, and they end up installed on the breakpoint chain with
341 a negative breakpoint number. They're visible in "maint info
342 breakpoints", but not "info breakpoints". */
343 struct internal_breakpoint : public code_breakpoint
345 internal_breakpoint (struct gdbarch *gdbarch,
346 enum bptype type, CORE_ADDR address)
347 : code_breakpoint (gdbarch, type)
349 symtab_and_line sal;
350 sal.pc = address;
351 sal.section = find_pc_overlay (sal.pc);
352 sal.pspace = current_program_space;
353 add_location (sal);
355 pspace = current_program_space;
356 disposition = disp_donttouch;
359 void re_set (program_space *pspace) override;
360 void check_status (struct bpstat *bs) override;
361 enum print_stop_action print_it (const bpstat *bs) const override;
362 void print_mention () const override;
365 /* Momentary breakpoints. These typically have a lifetime of some run
366 control command only, are always thread-specific, and have 0 for
367 breakpoint number. I.e., there can be many momentary breakpoints
368 on the breakpoint chain and they all same the same number (zero).
369 They're visible in "maint info breakpoints", but not "info
370 breakpoints". */
371 struct momentary_breakpoint : public code_breakpoint
373 momentary_breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
374 program_space *pspace_,
375 const struct frame_id &frame_id_,
376 int thread_)
377 : code_breakpoint (gdbarch_, bptype)
379 /* If FRAME_ID is valid, it should be a real frame, not an inlined
380 or tail-called one. */
381 gdb_assert (!frame_id_artificial_p (frame_id));
383 /* Momentary breakpoints are always thread-specific. */
384 gdb_assert (thread_ > 0);
386 pspace = pspace_;
387 enable_state = bp_enabled;
388 disposition = disp_donttouch;
389 frame_id = frame_id_;
390 thread = thread_;
392 /* The inferior should have been set by the parent constructor. */
393 gdb_assert (inferior == -1);
396 void re_set (program_space *pspace) override;
397 void check_status (struct bpstat *bs) override;
398 enum print_stop_action print_it (const bpstat *bs) const override;
399 void print_mention () const override;
402 /* DPrintf breakpoints. */
403 struct dprintf_breakpoint : public ordinary_breakpoint
405 using ordinary_breakpoint::ordinary_breakpoint;
407 void re_set (program_space *pspace) override;
408 int breakpoint_hit (const struct bp_location *bl,
409 const address_space *aspace,
410 CORE_ADDR bp_addr,
411 const target_waitstatus &ws) override;
412 void print_recreate (struct ui_file *fp) const override;
413 void after_condition_true (struct bpstat *bs) override;
416 /* Ranged breakpoints. */
417 struct ranged_breakpoint : public ordinary_breakpoint
419 explicit ranged_breakpoint (struct gdbarch *gdbarch,
420 const symtab_and_line &sal_start,
421 int length,
422 location_spec_up start_locspec,
423 location_spec_up end_locspec)
424 : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
426 bp_location *bl = add_location (sal_start);
427 bl->length = length;
429 disposition = disp_donttouch;
431 locspec = std::move (start_locspec);
432 locspec_range_end = std::move (end_locspec);
435 int breakpoint_hit (const struct bp_location *bl,
436 const address_space *aspace,
437 CORE_ADDR bp_addr,
438 const target_waitstatus &ws) override;
439 int resources_needed (const struct bp_location *) override;
440 enum print_stop_action print_it (const bpstat *bs) const override;
441 bool print_one (const bp_location **) const override;
442 void print_one_detail (struct ui_out *) const override;
443 void print_mention () const override;
444 void print_recreate (struct ui_file *fp) const override;
447 /* Static tracepoints with marker (`-m'). */
448 struct static_marker_tracepoint : public tracepoint
450 using tracepoint::tracepoint;
452 std::vector<symtab_and_line> decode_location_spec
453 (struct location_spec *locspec,
454 struct program_space *search_pspace) override;
457 /* The style in which to perform a dynamic printf. This is a user
458 option because different output options have different tradeoffs;
459 if GDB does the printing, there is better error handling if there
460 is a problem with any of the arguments, but using an inferior
461 function lets you have special-purpose printers and sending of
462 output to the same place as compiled-in print functions. */
464 static const char dprintf_style_gdb[] = "gdb";
465 static const char dprintf_style_call[] = "call";
466 static const char dprintf_style_agent[] = "agent";
467 static const char *const dprintf_style_enums[] = {
468 dprintf_style_gdb,
469 dprintf_style_call,
470 dprintf_style_agent,
471 NULL
473 static const char *dprintf_style = dprintf_style_gdb;
475 /* The function to use for dynamic printf if the preferred style is to
476 call into the inferior. The value is simply a string that is
477 copied into the command, so it can be anything that GDB can
478 evaluate to a callable address, not necessarily a function name. */
480 static std::string dprintf_function = "printf";
482 /* The channel to use for dynamic printf if the preferred style is to
483 call into the inferior; if a nonempty string, it will be passed to
484 the call as the first argument, with the format string as the
485 second. As with the dprintf function, this can be anything that
486 GDB knows how to evaluate, so in addition to common choices like
487 "stderr", this could be an app-specific expression like
488 "mystreams[curlogger]". */
490 static std::string dprintf_channel;
492 /* True if dprintf commands should continue to operate even if GDB
493 has disconnected. */
494 static bool disconnected_dprintf = true;
496 struct command_line *
497 breakpoint_commands (struct breakpoint *b)
499 return b->commands ? b->commands.get () : NULL;
502 /* Flag indicating that a command has proceeded the inferior past the
503 current breakpoint. */
505 static bool breakpoint_proceeded;
507 const char *
508 bpdisp_text (enum bpdisp disp)
510 /* NOTE: the following values are a part of MI protocol and
511 represent values of 'disp' field returned when inferior stops at
512 a breakpoint. */
513 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
515 return bpdisps[(int) disp];
518 /* Prototypes for exported functions. */
519 /* If FALSE, gdb will not use hardware support for watchpoints, even
520 if such is available. */
521 static int can_use_hw_watchpoints;
523 static void
524 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
525 struct cmd_list_element *c,
526 const char *value)
528 gdb_printf (file,
529 _("Debugger's willingness to use "
530 "watchpoint hardware is %s.\n"),
531 value);
534 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
535 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
536 for unrecognized breakpoint locations.
537 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
538 static enum auto_boolean pending_break_support;
539 static void
540 show_pending_break_support (struct ui_file *file, int from_tty,
541 struct cmd_list_element *c,
542 const char *value)
544 gdb_printf (file,
545 _("Debugger's behavior regarding "
546 "pending breakpoints is %s.\n"),
547 value);
550 /* If true, gdb will automatically use hardware breakpoints for breakpoints
551 set with "break" but falling in read-only memory.
552 If false, gdb will warn about such breakpoints, but won't automatically
553 use hardware breakpoints. */
554 static bool automatic_hardware_breakpoints;
555 static void
556 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
557 struct cmd_list_element *c,
558 const char *value)
560 gdb_printf (file,
561 _("Automatic usage of hardware breakpoints is %s.\n"),
562 value);
565 /* If on, GDB keeps breakpoints inserted even if the inferior is
566 stopped, and immediately inserts any new breakpoints as soon as
567 they're created. If off (default), GDB keeps breakpoints off of
568 the target as long as possible. That is, it delays inserting
569 breakpoints until the next resume, and removes them again when the
570 target fully stops. This is a bit safer in case GDB crashes while
571 processing user input. */
572 static bool always_inserted_mode = false;
574 static void
575 show_always_inserted_mode (struct ui_file *file, int from_tty,
576 struct cmd_list_element *c, const char *value)
578 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
579 value);
582 /* See breakpoint.h. */
583 bool debug_breakpoint = false;
585 /* "show debug breakpoint" implementation. */
586 static void
587 show_debug_breakpoint (struct ui_file *file, int from_tty,
588 struct cmd_list_element *c, const char *value)
590 gdb_printf (file, _("Breakpoint location debugging is %s.\n"), value);
593 /* See breakpoint.h. */
596 breakpoints_should_be_inserted_now (void)
598 if (gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
600 /* If breakpoints are global, they should be inserted even if no
601 thread under gdb's control is running, or even if there are
602 no threads under GDB's control yet. */
603 return 1;
605 else
607 if (always_inserted_mode)
609 /* The user wants breakpoints inserted even if all threads
610 are stopped. */
611 return 1;
614 for (inferior *inf : all_inferiors ())
615 if (inf->has_execution ()
616 && threads_are_executing (inf->process_target ()))
617 return 1;
619 /* Don't remove breakpoints yet if, even though all threads are
620 stopped, we still have events to process. */
621 for (thread_info *tp : all_non_exited_threads ())
622 if (tp->resumed () && tp->has_pending_waitstatus ())
623 return 1;
625 return 0;
628 static const char condition_evaluation_both[] = "host or target";
630 /* Modes for breakpoint condition evaluation. */
631 static const char condition_evaluation_auto[] = "auto";
632 static const char condition_evaluation_host[] = "host";
633 static const char condition_evaluation_target[] = "target";
634 static const char *const condition_evaluation_enums[] = {
635 condition_evaluation_auto,
636 condition_evaluation_host,
637 condition_evaluation_target,
638 NULL
641 /* Global that holds the current mode for breakpoint condition evaluation. */
642 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
644 /* Global that we use to display information to the user (gets its value from
645 condition_evaluation_mode_1. */
646 static const char *condition_evaluation_mode = condition_evaluation_auto;
648 /* Translate a condition evaluation mode MODE into either "host"
649 or "target". This is used mostly to translate from "auto" to the
650 real setting that is being used. It returns the translated
651 evaluation mode. */
653 static const char *
654 translate_condition_evaluation_mode (const char *mode)
656 if (mode == condition_evaluation_auto)
658 if (target_supports_evaluation_of_breakpoint_conditions ())
659 return condition_evaluation_target;
660 else
661 return condition_evaluation_host;
663 else
664 return mode;
667 /* Discovers what condition_evaluation_auto translates to. */
669 static const char *
670 breakpoint_condition_evaluation_mode (void)
672 return translate_condition_evaluation_mode (condition_evaluation_mode);
675 /* Return true if GDB should evaluate breakpoint conditions or false
676 otherwise. */
678 static bool
679 gdb_evaluates_breakpoint_condition_p (void)
681 const char *mode = breakpoint_condition_evaluation_mode ();
683 return (mode == condition_evaluation_host);
686 /* Are we executing breakpoint commands? */
687 static int executing_breakpoint_commands;
689 /* Are overlay event breakpoints enabled? */
690 static int overlay_events_enabled;
692 /* See description in breakpoint.h. */
693 bool target_exact_watchpoints = false;
695 /* Chains of all breakpoints defined. */
697 static intrusive_list<breakpoint> breakpoint_chain;
699 /* See breakpoint.h. */
701 breakpoint_range
702 all_breakpoints ()
704 return breakpoint_range (breakpoint_chain.begin (), breakpoint_chain.end ());
707 /* See breakpoint.h. */
709 breakpoint_safe_range
710 all_breakpoints_safe ()
712 return breakpoint_safe_range (all_breakpoints ());
715 /* See breakpoint.h. */
717 tracepoint_range
718 all_tracepoints ()
720 return tracepoint_range (tracepoint_iterator (breakpoint_chain.begin ()),
721 tracepoint_iterator (breakpoint_chain.end ()));
724 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
726 static std::vector<bp_location *> bp_locations;
728 /* See breakpoint.h. */
730 const std::vector<bp_location *> &
731 all_bp_locations ()
733 return bp_locations;
736 /* Range to iterate over breakpoint locations at a given address. */
738 struct bp_locations_at_addr_range
740 using iterator = std::vector<bp_location *>::iterator;
742 bp_locations_at_addr_range (CORE_ADDR addr)
744 struct compare
746 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
747 { return loc->address < addr_; }
749 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
750 { return addr_ < loc->address; }
753 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
754 addr, compare ());
756 m_begin = it_pair.first;
757 m_end = it_pair.second;
760 iterator begin () const
761 { return m_begin; }
763 iterator end () const
764 { return m_end; }
766 private:
767 iterator m_begin;
768 iterator m_end;
771 /* Return a range to iterate over all breakpoint locations exactly at address
772 ADDR.
774 If it's needed to iterate multiple times on the same range, it's possible
775 to save the range in a local variable and use it multiple times:
777 auto range = all_bp_locations_at_addr (addr);
779 for (bp_location *loc : range)
780 // use loc
782 for (bp_location *loc : range)
783 // use loc
785 This saves a bit of time, as it avoids re-doing the binary searches to find
786 the range's boundaries. Just remember not to change the bp_locations vector
787 in the mean time, as it could make the range's iterators stale. */
789 static bp_locations_at_addr_range
790 all_bp_locations_at_addr (CORE_ADDR addr)
792 return bp_locations_at_addr_range (addr);
795 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
796 ADDRESS for the current elements of BP_LOCATIONS which get a valid
797 result from bp_location_has_shadow. You can use it for roughly
798 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
799 an address you need to read. */
801 static CORE_ADDR bp_locations_placed_address_before_address_max;
803 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
804 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
805 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
806 You can use it for roughly limiting the subrange of BP_LOCATIONS to
807 scan for shadow bytes for an address you need to read. */
809 static CORE_ADDR bp_locations_shadow_len_after_address_max;
811 /* The locations that no longer correspond to any breakpoint, unlinked
812 from the bp_locations array, but for which a hit may still be
813 reported by a target. */
814 static std::vector<bp_location *> moribund_locations;
816 /* Number of last breakpoint made. */
818 static int breakpoint_count;
820 /* The value of `breakpoint_count' before the last command that
821 created breakpoints. If the last (break-like) command created more
822 than one breakpoint, then the difference between BREAKPOINT_COUNT
823 and PREV_BREAKPOINT_COUNT is more than one. */
824 static int prev_breakpoint_count;
826 /* Number of last tracepoint made. */
828 static int tracepoint_count;
830 static struct cmd_list_element *breakpoint_set_cmdlist;
831 static struct cmd_list_element *breakpoint_show_cmdlist;
832 struct cmd_list_element *save_cmdlist;
834 /* Return whether a breakpoint is an active enabled breakpoint. */
835 static bool
836 breakpoint_enabled (struct breakpoint *b)
838 return (b->enable_state == bp_enabled);
841 /* Set breakpoint count to NUM. */
843 static void
844 set_breakpoint_count (int num)
846 prev_breakpoint_count = breakpoint_count;
847 breakpoint_count = num;
848 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
851 /* Used by `start_rbreak_breakpoints' below, to record the current
852 breakpoint count before "rbreak" creates any breakpoint. */
853 static int rbreak_start_breakpoint_count;
855 /* Called at the start an "rbreak" command to record the first
856 breakpoint made. */
858 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
860 rbreak_start_breakpoint_count = breakpoint_count;
863 /* Called at the end of an "rbreak" command to record the last
864 breakpoint made. */
866 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
868 prev_breakpoint_count = rbreak_start_breakpoint_count;
871 /* Used in run_command to zero the hit count when a new run starts. */
873 void
874 clear_breakpoint_hit_counts (void)
876 for (breakpoint &b : all_breakpoints ())
877 b.hit_count = 0;
881 /* Return the breakpoint with the specified number, or NULL
882 if the number does not refer to an existing breakpoint. */
884 struct breakpoint *
885 get_breakpoint (int num)
887 for (breakpoint &b : all_breakpoints ())
888 if (b.number == num)
889 return &b;
891 return nullptr;
894 /* Return TRUE if NUM refer to an existing breakpoint that has
895 multiple code locations. */
897 static bool
898 has_multiple_locations (int num)
900 for (breakpoint &b : all_breakpoints ())
901 if (b.number == num)
902 return b.has_multiple_locations ();
904 return false;
909 /* Mark locations as "conditions have changed" in case the target supports
910 evaluating conditions on its side. */
912 static void
913 mark_breakpoint_modified (struct breakpoint *b)
915 /* This is only meaningful if the target is
916 evaluating conditions and if the user has
917 opted for condition evaluation on the target's
918 side. */
919 if (gdb_evaluates_breakpoint_condition_p ()
920 || !target_supports_evaluation_of_breakpoint_conditions ())
921 return;
923 if (!is_breakpoint (b))
924 return;
926 for (bp_location &loc : b->locations ())
927 loc.condition_changed = condition_modified;
930 /* Mark location as "conditions have changed" in case the target supports
931 evaluating conditions on its side. */
933 static void
934 mark_breakpoint_location_modified (struct bp_location *loc)
936 /* This is only meaningful if the target is
937 evaluating conditions and if the user has
938 opted for condition evaluation on the target's
939 side. */
940 if (gdb_evaluates_breakpoint_condition_p ()
941 || !target_supports_evaluation_of_breakpoint_conditions ())
943 return;
945 if (!is_breakpoint (loc->owner))
946 return;
948 loc->condition_changed = condition_modified;
951 /* Sets the condition-evaluation mode using the static global
952 condition_evaluation_mode. */
954 static void
955 set_condition_evaluation_mode (const char *args, int from_tty,
956 struct cmd_list_element *c)
958 const char *old_mode, *new_mode;
960 if ((condition_evaluation_mode_1 == condition_evaluation_target)
961 && !target_supports_evaluation_of_breakpoint_conditions ())
963 condition_evaluation_mode_1 = condition_evaluation_mode;
964 warning (_("Target does not support breakpoint condition evaluation.\n"
965 "Using host evaluation mode instead."));
966 return;
969 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
970 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
972 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
973 settings was "auto". */
974 condition_evaluation_mode = condition_evaluation_mode_1;
976 /* Only update the mode if the user picked a different one. */
977 if (new_mode != old_mode)
979 /* If the user switched to a different evaluation mode, we
980 need to synch the changes with the target as follows:
982 "host" -> "target": Send all (valid) conditions to the target.
983 "target" -> "host": Remove all the conditions from the target.
986 if (new_mode == condition_evaluation_target)
988 /* Mark everything modified and synch conditions with the
989 target. */
990 for (bp_location *loc : all_bp_locations ())
991 mark_breakpoint_location_modified (loc);
993 else
995 /* Manually mark non-duplicate locations to synch conditions
996 with the target. We do this to remove all the conditions the
997 target knows about. */
998 for (bp_location *loc : all_bp_locations ())
999 if (is_breakpoint (loc->owner) && loc->inserted)
1000 loc->needs_update = 1;
1003 /* Do the update. */
1004 update_global_location_list (UGLL_MAY_INSERT);
1007 return;
1010 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
1011 what "auto" is translating to. */
1013 static void
1014 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
1015 struct cmd_list_element *c, const char *value)
1017 if (condition_evaluation_mode == condition_evaluation_auto)
1018 gdb_printf (file,
1019 _("Breakpoint condition evaluation "
1020 "mode is %s (currently %s).\n"),
1021 value,
1022 breakpoint_condition_evaluation_mode ());
1023 else
1024 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
1025 value);
1028 /* Parse COND_STRING in the context of LOC and set as the condition
1029 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
1030 the number of LOC within its owner. In case of parsing error, mark
1031 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
1033 static void
1034 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
1035 int bp_num, int loc_num)
1037 bool has_junk = false;
1040 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
1041 block_for_pc (loc->address), 0);
1042 if (*cond_string != 0)
1043 has_junk = true;
1044 else
1046 loc->cond = std::move (new_exp);
1047 if (loc->disabled_by_cond && loc->enabled)
1048 gdb_printf (_("Breakpoint %d's condition is now valid at "
1049 "location %d, enabling.\n"),
1050 bp_num, loc_num);
1052 loc->disabled_by_cond = false;
1055 catch (const gdb_exception_error &e)
1057 if (loc->enabled)
1059 /* Warn if a user-enabled location is now becoming disabled-by-cond.
1060 BP_NUM is 0 if the breakpoint is being defined for the first
1061 time using the "break ... if ..." command, and non-zero if
1062 already defined. */
1063 if (bp_num != 0)
1064 warning (_("failed to validate condition at location %d.%d, "
1065 "disabling:\n %s"), bp_num, loc_num, e.what ());
1066 else
1067 warning (_("failed to validate condition at location %d, "
1068 "disabling:\n %s"), loc_num, e.what ());
1071 loc->disabled_by_cond = true;
1074 if (has_junk)
1075 error (_("Garbage '%s' follows condition"), cond_string);
1078 /* See breakpoint.h. */
1080 void
1081 notify_breakpoint_modified (breakpoint *b)
1083 interps_notify_breakpoint_modified (b);
1084 gdb::observers::breakpoint_modified.notify (b);
1087 void
1088 set_breakpoint_condition (struct breakpoint *b, const char *exp,
1089 int from_tty, bool force)
1091 if (*exp == 0)
1093 b->cond_string.reset ();
1095 if (is_watchpoint (b))
1096 gdb::checked_static_cast<watchpoint *> (b)->cond_exp.reset ();
1097 else
1099 int loc_num = 1;
1100 for (bp_location &loc : b->locations ())
1102 loc.cond.reset ();
1103 if (loc.disabled_by_cond && loc.enabled)
1104 gdb_printf (_("Breakpoint %d's condition is now valid at "
1105 "location %d, enabling.\n"),
1106 b->number, loc_num);
1107 loc.disabled_by_cond = false;
1108 loc_num++;
1110 /* No need to free the condition agent expression
1111 bytecode (if we have one). We will handle this
1112 when we go through update_global_location_list. */
1116 if (from_tty)
1117 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
1119 else
1121 if (is_watchpoint (b))
1123 innermost_block_tracker tracker;
1124 const char *arg = exp;
1125 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
1126 if (*arg != 0)
1127 error (_("Junk at end of expression"));
1128 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
1129 w->cond_exp = std::move (new_exp);
1130 w->cond_exp_valid_block = tracker.block ();
1132 else
1134 /* Parse and set condition expressions. We make two passes.
1135 In the first, we parse the condition string to see if it
1136 is valid in at least one location. If so, the condition
1137 would be accepted. So we go ahead and set the locations'
1138 conditions. In case no valid case is found, we throw
1139 the error and the condition string will be rejected.
1140 This two-pass approach is taken to avoid setting the
1141 state of locations in case of a reject. */
1142 for (const bp_location &loc : b->locations ())
1146 const char *arg = exp;
1147 parse_exp_1 (&arg, loc.address,
1148 block_for_pc (loc.address), 0);
1149 if (*arg != 0)
1150 error (_("Junk at end of expression"));
1151 break;
1153 catch (const gdb_exception_error &e)
1155 /* Condition string is invalid. If this happens to
1156 be the last loc, abandon (if not forced) or continue
1157 (if forced). */
1158 if (&loc == &b->last_loc () && !force)
1159 throw;
1163 /* If we reach here, the condition is valid at some locations. */
1164 int loc_num = 1;
1165 for (bp_location &loc : b->locations ())
1167 set_breakpoint_location_condition (exp, &loc, b->number, loc_num);
1168 loc_num++;
1172 /* We know that the new condition parsed successfully. The
1173 condition string of the breakpoint can be safely updated. */
1174 b->cond_string = make_unique_xstrdup (exp);
1175 b->condition_not_parsed = 0;
1177 mark_breakpoint_modified (b);
1179 notify_breakpoint_modified (b);
1182 /* See breakpoint.h. */
1184 void
1185 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1186 bool force)
1188 for (breakpoint &b : all_breakpoints ())
1189 if (b.number == bpnum)
1191 /* Check if this breakpoint has a "stop" method implemented in an
1192 extension language. This method and conditions entered into GDB
1193 from the CLI are mutually exclusive. */
1194 const struct extension_language_defn *extlang
1195 = get_breakpoint_cond_ext_lang (&b, EXT_LANG_NONE);
1197 if (extlang != NULL)
1199 error (_("Only one stop condition allowed. There is currently"
1200 " a %s stop condition defined for this breakpoint."),
1201 ext_lang_capitalized_name (extlang));
1203 set_breakpoint_condition (&b, exp, from_tty, force);
1205 if (is_breakpoint (&b))
1206 update_global_location_list (UGLL_MAY_INSERT);
1208 return;
1211 error (_("No breakpoint number %d."), bpnum);
1214 /* The options for the "condition" command. */
1216 struct condition_command_opts
1218 /* For "-force". */
1219 bool force_condition = false;
1222 static const gdb::option::option_def condition_command_option_defs[] = {
1224 gdb::option::flag_option_def<condition_command_opts> {
1225 "force",
1226 [] (condition_command_opts *opts) { return &opts->force_condition; },
1227 N_("Set the condition even if it is invalid for all current locations."),
1232 /* Create an option_def_group for the "condition" options, with
1233 CC_OPTS as context. */
1235 static inline gdb::option::option_def_group
1236 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1238 return {{condition_command_option_defs}, cc_opts};
1241 /* Completion for the "condition" command. */
1243 static void
1244 condition_completer (struct cmd_list_element *cmd,
1245 completion_tracker &tracker,
1246 const char *text, const char * /*word*/)
1248 bool has_no_arguments = (*text == '\0');
1249 condition_command_opts cc_opts;
1250 const auto group = make_condition_command_options_def_group (&cc_opts);
1251 if (gdb::option::complete_options
1252 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1253 return;
1255 text = skip_spaces (text);
1256 const char *space = skip_to_space (text);
1257 if (*space == '\0')
1259 int len;
1261 if (text[0] == '$')
1263 tracker.advance_custom_word_point_by (1);
1264 /* We don't support completion of history indices. */
1265 if (!isdigit (text[1]))
1266 complete_internalvar (tracker, &text[1]);
1267 return;
1270 /* Suggest the "-force" flag if no arguments are given. If
1271 arguments were passed, they either already include the flag,
1272 or we are beyond the point of suggesting it because it's
1273 positionally the first argument. */
1274 if (has_no_arguments)
1275 gdb::option::complete_on_all_options (tracker, group);
1277 /* We're completing the breakpoint number. */
1278 len = strlen (text);
1280 for (breakpoint &b : all_breakpoints ())
1282 char number[50];
1284 xsnprintf (number, sizeof (number), "%d", b.number);
1286 if (strncmp (number, text, len) == 0)
1287 tracker.add_completion (make_unique_xstrdup (number));
1290 return;
1293 /* We're completing the expression part. Skip the breakpoint num. */
1294 const char *exp_start = skip_spaces (space);
1295 tracker.advance_custom_word_point_by (exp_start - text);
1296 text = exp_start;
1297 const char *word = advance_to_expression_complete_word_point (tracker, text);
1298 expression_completer (cmd, tracker, text, word);
1301 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1303 static void
1304 condition_command (const char *arg, int from_tty)
1306 const char *p;
1307 int bnum;
1309 if (arg == 0)
1310 error_no_arg (_("breakpoint number"));
1312 p = arg;
1314 /* Check if the "-force" flag was passed. */
1315 condition_command_opts cc_opts;
1316 const auto group = make_condition_command_options_def_group (&cc_opts);
1317 gdb::option::process_options
1318 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1320 bnum = get_number (&p);
1321 if (bnum == 0)
1322 error (_("Bad breakpoint argument: '%s'"), arg);
1324 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1327 /* Check that COMMAND do not contain commands that are suitable
1328 only for tracepoints and not suitable for ordinary breakpoints.
1329 Throw if any such commands is found. */
1331 static void
1332 check_no_tracepoint_commands (struct command_line *commands)
1334 struct command_line *c;
1336 for (c = commands; c; c = c->next)
1338 if (c->control_type == while_stepping_control)
1339 error (_("The 'while-stepping' command can "
1340 "only be used for tracepoints"));
1342 check_no_tracepoint_commands (c->body_list_0.get ());
1343 check_no_tracepoint_commands (c->body_list_1.get ());
1345 /* Not that command parsing removes leading whitespace and comment
1346 lines and also empty lines. So, we only need to check for
1347 command directly. */
1348 if (strstr (c->line, "collect ") == c->line)
1349 error (_("The 'collect' command can only be used for tracepoints"));
1351 if (strstr (c->line, "teval ") == c->line)
1352 error (_("The 'teval' command can only be used for tracepoints"));
1356 struct longjmp_breakpoint : public momentary_breakpoint
1358 using momentary_breakpoint::momentary_breakpoint;
1360 ~longjmp_breakpoint () override;
1363 /* Encapsulate tests for different types of tracepoints. */
1365 static bool
1366 is_tracepoint_type (bptype type)
1368 return (type == bp_tracepoint
1369 || type == bp_fast_tracepoint
1370 || type == bp_static_tracepoint
1371 || type == bp_static_marker_tracepoint);
1374 /* See breakpoint.h. */
1376 bool
1377 is_tracepoint (const struct breakpoint *b)
1379 return is_tracepoint_type (b->type);
1382 /* Factory function to create an appropriate instance of breakpoint given
1383 TYPE. */
1385 template<typename... Arg>
1386 static std::unique_ptr<code_breakpoint>
1387 new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type,
1388 Arg&&... args)
1390 code_breakpoint *b;
1392 switch (type)
1394 case bp_breakpoint:
1395 case bp_hardware_breakpoint:
1396 b = new ordinary_breakpoint (gdbarch, type,
1397 std::forward<Arg> (args)...);
1398 break;
1400 case bp_fast_tracepoint:
1401 case bp_static_tracepoint:
1402 case bp_tracepoint:
1403 b = new tracepoint (gdbarch, type,
1404 std::forward<Arg> (args)...);
1405 break;
1407 case bp_static_marker_tracepoint:
1408 b = new static_marker_tracepoint (gdbarch, type,
1409 std::forward<Arg> (args)...);
1410 break;
1412 case bp_dprintf:
1413 b = new dprintf_breakpoint (gdbarch, type,
1414 std::forward<Arg> (args)...);
1415 break;
1417 default:
1418 gdb_assert_not_reached ("invalid type");
1421 return std::unique_ptr<code_breakpoint> (b);
1424 /* A helper function that validates that COMMANDS are valid for a
1425 breakpoint. This function will throw an exception if a problem is
1426 found. */
1428 static void
1429 validate_commands_for_breakpoint (struct breakpoint *b,
1430 struct command_line *commands)
1432 if (is_tracepoint (b))
1434 tracepoint *t = gdb::checked_static_cast<tracepoint *> (b);
1435 struct command_line *c;
1436 struct command_line *while_stepping = 0;
1438 /* Reset the while-stepping step count. The previous commands
1439 might have included a while-stepping action, while the new
1440 ones might not. */
1441 t->step_count = 0;
1443 /* We need to verify that each top-level element of commands is
1444 valid for tracepoints, that there's at most one
1445 while-stepping element, and that the while-stepping's body
1446 has valid tracing commands excluding nested while-stepping.
1447 We also need to validate the tracepoint action line in the
1448 context of the tracepoint --- validate_actionline actually
1449 has side effects, like setting the tracepoint's
1450 while-stepping STEP_COUNT, in addition to checking if the
1451 collect/teval actions parse and make sense in the
1452 tracepoint's context. */
1453 for (c = commands; c; c = c->next)
1455 if (c->control_type == while_stepping_control)
1457 if (b->type == bp_fast_tracepoint)
1458 error (_("The 'while-stepping' command "
1459 "cannot be used for fast tracepoint"));
1460 else if (b->type == bp_static_tracepoint
1461 || b->type == bp_static_marker_tracepoint)
1462 error (_("The 'while-stepping' command "
1463 "cannot be used for static tracepoint"));
1465 if (while_stepping)
1466 error (_("The 'while-stepping' command "
1467 "can be used only once"));
1468 else
1469 while_stepping = c;
1472 validate_actionline (c->line, t);
1474 if (while_stepping)
1476 struct command_line *c2;
1478 gdb_assert (while_stepping->body_list_1 == nullptr);
1479 c2 = while_stepping->body_list_0.get ();
1480 for (; c2; c2 = c2->next)
1482 if (c2->control_type == while_stepping_control)
1483 error (_("The 'while-stepping' command cannot be nested"));
1487 else
1489 check_no_tracepoint_commands (commands);
1493 /* Return a vector of all the static tracepoints set at ADDR. The
1494 caller is responsible for releasing the vector. */
1496 std::vector<breakpoint *>
1497 static_tracepoints_here (CORE_ADDR addr)
1499 std::vector<breakpoint *> found;
1501 for (breakpoint &b : all_breakpoints ())
1502 if (b.type == bp_static_tracepoint
1503 || b.type == bp_static_marker_tracepoint)
1505 for (bp_location &loc : b.locations ())
1506 if (loc.address == addr)
1507 found.push_back (&b);
1510 return found;
1513 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1514 validate that only allowed commands are included. */
1516 void
1517 breakpoint_set_commands (struct breakpoint *b,
1518 counted_command_line &&commands)
1520 validate_commands_for_breakpoint (b, commands.get ());
1522 b->commands = std::move (commands);
1523 notify_breakpoint_modified (b);
1526 /* Set the internal `silent' flag on the breakpoint. Note that this
1527 is not the same as the "silent" that may appear in the breakpoint's
1528 commands. */
1530 void
1531 breakpoint_set_silent (struct breakpoint *b, int silent)
1533 int old_silent = b->silent;
1535 b->silent = silent;
1536 if (old_silent != silent)
1537 notify_breakpoint_modified (b);
1540 /* See breakpoint.h. */
1542 void
1543 breakpoint_set_thread (struct breakpoint *b, int thread)
1545 /* THREAD should be -1, meaning no thread restriction, or it should be a
1546 valid global thread-id, which are greater than zero. */
1547 gdb_assert (thread == -1 || thread > 0);
1549 /* It is not valid to set a thread restriction for a breakpoint that
1550 already has task or inferior restriction. */
1551 gdb_assert (thread == -1 || (b->task == -1 && b->inferior == -1));
1553 int old_thread = b->thread;
1554 b->thread = thread;
1555 if (old_thread != thread)
1557 /* If THREAD is in a different program_space than OLD_THREAD, or the
1558 breakpoint has switched to or from being thread-specific, then we
1559 need to re-set the locations of this breakpoint. First, figure
1560 out the program_space for the old and new threads, use a value of
1561 nullptr to indicate the breakpoint is in all program spaces. */
1562 program_space *old_pspace = nullptr;
1563 if (old_thread != -1)
1565 struct thread_info *thr = find_thread_global_id (old_thread);
1566 gdb_assert (thr != nullptr);
1567 old_pspace = thr->inf->pspace;
1570 program_space *new_pspace = nullptr;
1571 if (thread != -1)
1573 struct thread_info *thr = find_thread_global_id (thread);
1574 gdb_assert (thr != nullptr);
1575 new_pspace = thr->inf->pspace;
1578 /* If the program space has changed for this breakpoint, then
1579 re-evaluate it's locations. */
1580 if (old_pspace != new_pspace)
1582 /* The breakpoint is now associated with a completely different
1583 program space. Discard all of the current locations and then
1584 re-set the breakpoint in the new program space, this will
1585 create the new locations. */
1586 b->clear_locations ();
1587 breakpoint_re_set_one (b, new_pspace);
1590 /* If the program space didn't change, or the breakpoint didn't
1591 acquire any new locations after the clear_locations call, then we
1592 need to notify of the breakpoint modification now. */
1593 if (old_pspace == new_pspace || !b->has_locations ())
1594 notify_breakpoint_modified (b);
1598 /* See breakpoint.h. */
1600 void
1601 breakpoint_set_inferior (struct breakpoint *b, int inferior)
1603 /* INFERIOR should be -1, meaning no inferior restriction, or it should
1604 be a valid inferior number, which are greater than zero. */
1605 gdb_assert (inferior == -1 || inferior > 0);
1607 /* It is not valid to set an inferior restriction for a breakpoint that
1608 already has a task or thread restriction. */
1609 gdb_assert (inferior == -1 || (b->task == -1 && b->thread == -1));
1611 int old_inferior = b->inferior;
1612 b->inferior = inferior;
1613 if (old_inferior != inferior)
1615 /* If INFERIOR is in a different program_space than OLD_INFERIOR, or
1616 the breakpoint has switch to or from inferior-specific, then we
1617 need to re-set the locations of this breakpoint. First, figure
1618 out the program_space for the old and new inferiors, use a value
1619 of nullptr to indicate the breakpoint is in all program
1620 spaces. */
1621 program_space *old_pspace = nullptr;
1622 if (old_inferior != -1)
1624 struct inferior *inf = find_inferior_id (old_inferior);
1625 gdb_assert (inf != nullptr);
1626 old_pspace = inf->pspace;
1629 program_space *new_pspace = nullptr;
1630 if (inferior != -1)
1632 struct inferior *inf = find_inferior_id (inferior);
1633 gdb_assert (inf != nullptr);
1634 new_pspace = inf->pspace;
1637 if (old_pspace != new_pspace)
1639 /* The breakpoint is now associated with a completely different
1640 program space. Discard all of the current locations and then
1641 re-set the breakpoint in the new program space, this will
1642 create the new locations. */
1643 b->clear_locations ();
1644 breakpoint_re_set_one (b, new_pspace);
1647 /* If the program space didn't change, or the breakpoint didn't
1648 acquire any new locations after the clear_locations call, then we
1649 need to notify of the breakpoint modification now. */
1650 if (old_pspace == new_pspace || !b->has_locations ())
1651 notify_breakpoint_modified (b);
1655 /* See breakpoint.h. */
1657 void
1658 breakpoint_set_task (struct breakpoint *b, int task)
1660 /* TASK should be -1, meaning no task restriction, or it should be a
1661 valid task-id, which are greater than zero. */
1662 gdb_assert (task == -1 || task > 0);
1664 /* It is not valid to set a task restriction for a breakpoint that
1665 already has a thread or inferior restriction. */
1666 gdb_assert (task == -1 || (b->thread == -1 && b->inferior == -1));
1668 int old_task = b->task;
1669 b->task = task;
1670 if (old_task != task)
1671 notify_breakpoint_modified (b);
1674 static void
1675 commands_command_1 (const char *arg, int from_tty,
1676 struct command_line *control)
1678 counted_command_line cmd;
1679 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1680 NULL after the call to read_command_lines if the user provides an empty
1681 list of command by just typing "end". */
1682 bool cmd_read = false;
1684 std::string new_arg;
1686 if (arg == NULL || !*arg)
1688 /* Argument not explicitly given. Synthesize it. */
1689 if (breakpoint_count - prev_breakpoint_count > 1)
1690 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1691 breakpoint_count);
1692 else if (breakpoint_count > 0)
1693 new_arg = string_printf ("%d", breakpoint_count);
1695 else
1697 /* Create a copy of ARG. This is needed because the "commands"
1698 command may be coming from a script. In that case, the read
1699 line buffer is going to be overwritten in the lambda of
1700 'map_breakpoint_numbers' below when reading the next line
1701 before we are are done parsing the breakpoint numbers. */
1702 new_arg = arg;
1704 arg = new_arg.c_str ();
1706 map_breakpoint_numbers
1707 (arg, [&] (breakpoint *b)
1709 if (!cmd_read)
1711 gdb_assert (cmd == NULL);
1712 if (control != NULL)
1713 cmd = control->body_list_0;
1714 else
1716 std::string str
1717 = string_printf (_("Type commands for breakpoint(s) "
1718 "%s, one per line."),
1719 arg);
1721 auto do_validate = [=] (const char *line)
1723 tracepoint *t
1724 = gdb::checked_static_cast<tracepoint *> (b);
1725 validate_actionline (line, t);
1727 gdb::function_view<void (const char *)> validator;
1728 if (is_tracepoint (b))
1729 validator = do_validate;
1731 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1733 cmd_read = true;
1736 /* If a breakpoint was on the list more than once, we don't need to
1737 do anything. */
1738 if (b->commands != cmd)
1740 validate_commands_for_breakpoint (b, cmd.get ());
1741 b->commands = cmd;
1742 notify_breakpoint_modified (b);
1747 static void
1748 commands_command (const char *arg, int from_tty)
1750 commands_command_1 (arg, from_tty, NULL);
1753 /* Like commands_command, but instead of reading the commands from
1754 input stream, takes them from an already parsed command structure.
1756 This is used by cli-script.c to DTRT with breakpoint commands
1757 that are part of if and while bodies. */
1758 enum command_control_type
1759 commands_from_control_command (const char *arg, struct command_line *cmd)
1761 commands_command_1 (arg, 0, cmd);
1762 return simple_control;
1765 /* Return true if BL->TARGET_INFO contains valid information. */
1767 static bool
1768 bp_location_has_shadow (struct bp_location *bl)
1770 if (bl->loc_type != bp_loc_software_breakpoint)
1771 return false;
1772 if (!bl->inserted)
1773 return false;
1774 if (bl->target_info.shadow_len == 0)
1775 /* BL isn't valid, or doesn't shadow memory. */
1776 return false;
1777 return true;
1780 /* Update BUF, which is LEN bytes read from the target address
1781 MEMADDR, by replacing a memory breakpoint with its shadowed
1782 contents.
1784 If READBUF is not NULL, this buffer must not overlap with the of
1785 the breakpoint location's shadow_contents buffer. Otherwise, a
1786 failed assertion internal error will be raised. */
1788 static void
1789 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1790 const gdb_byte *writebuf_org,
1791 ULONGEST memaddr, LONGEST len,
1792 struct bp_target_info *target_info,
1793 struct gdbarch *gdbarch)
1795 /* Now do full processing of the found relevant range of elements. */
1796 CORE_ADDR bp_addr = 0;
1797 int bp_size = 0;
1798 int bptoffset = 0;
1800 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1801 current_program_space->aspace.get (), 0))
1803 /* The breakpoint is inserted in a different address space. */
1804 return;
1807 /* Addresses and length of the part of the breakpoint that
1808 we need to copy. */
1809 bp_addr = target_info->placed_address;
1810 bp_size = target_info->shadow_len;
1812 if (bp_addr + bp_size <= memaddr)
1814 /* The breakpoint is entirely before the chunk of memory we are
1815 reading. */
1816 return;
1819 if (bp_addr >= memaddr + len)
1821 /* The breakpoint is entirely after the chunk of memory we are
1822 reading. */
1823 return;
1826 /* Offset within shadow_contents. */
1827 if (bp_addr < memaddr)
1829 /* Only copy the second part of the breakpoint. */
1830 bp_size -= memaddr - bp_addr;
1831 bptoffset = memaddr - bp_addr;
1832 bp_addr = memaddr;
1835 if (bp_addr + bp_size > memaddr + len)
1837 /* Only copy the first part of the breakpoint. */
1838 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1841 if (readbuf != NULL)
1843 /* Verify that the readbuf buffer does not overlap with the
1844 shadow_contents buffer. */
1845 gdb_assert (target_info->shadow_contents >= readbuf + len
1846 || readbuf >= (target_info->shadow_contents
1847 + target_info->shadow_len));
1849 /* Update the read buffer with this inserted breakpoint's
1850 shadow. */
1851 memcpy (readbuf + bp_addr - memaddr,
1852 target_info->shadow_contents + bptoffset, bp_size);
1854 else
1856 const unsigned char *bp;
1857 CORE_ADDR addr = target_info->reqstd_address;
1858 int placed_size;
1860 /* Update the shadow with what we want to write to memory. */
1861 memcpy (target_info->shadow_contents + bptoffset,
1862 writebuf_org + bp_addr - memaddr, bp_size);
1864 /* Determine appropriate breakpoint contents and size for this
1865 address. */
1866 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1868 /* Update the final write buffer with this inserted
1869 breakpoint's INSN. */
1870 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1874 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1875 by replacing any memory breakpoints with their shadowed contents.
1877 If READBUF is not NULL, this buffer must not overlap with any of
1878 the breakpoint location's shadow_contents buffers. Otherwise,
1879 a failed assertion internal error will be raised.
1881 The range of shadowed area by each bp_location is:
1882 bl->address - bp_locations_placed_address_before_address_max
1883 up to bl->address + bp_locations_shadow_len_after_address_max
1884 The range we were requested to resolve shadows for is:
1885 memaddr ... memaddr + len
1886 Thus the safe cutoff boundaries for performance optimization are
1887 memaddr + len <= (bl->address
1888 - bp_locations_placed_address_before_address_max)
1889 and:
1890 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1892 void
1893 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1894 const gdb_byte *writebuf_org,
1895 ULONGEST memaddr, LONGEST len)
1897 /* Left boundary, right boundary and median element of our binary
1898 search. */
1899 unsigned bc_l, bc_r, bc;
1901 /* Find BC_L which is a leftmost element which may affect BUF
1902 content. It is safe to report lower value but a failure to
1903 report higher one. */
1905 bc_l = 0;
1906 bc_r = bp_locations.size ();
1907 while (bc_l + 1 < bc_r)
1909 struct bp_location *bl;
1911 bc = (bc_l + bc_r) / 2;
1912 bl = bp_locations[bc];
1914 /* Check first BL->ADDRESS will not overflow due to the added
1915 constant. Then advance the left boundary only if we are sure
1916 the BC element can in no way affect the BUF content (MEMADDR
1917 to MEMADDR + LEN range).
1919 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1920 offset so that we cannot miss a breakpoint with its shadow
1921 range tail still reaching MEMADDR. */
1923 if ((bl->address + bp_locations_shadow_len_after_address_max
1924 >= bl->address)
1925 && (bl->address + bp_locations_shadow_len_after_address_max
1926 <= memaddr))
1927 bc_l = bc;
1928 else
1929 bc_r = bc;
1932 /* Due to the binary search above, we need to make sure we pick the
1933 first location that's at BC_L's address. E.g., if there are
1934 multiple locations at the same address, BC_L may end up pointing
1935 at a duplicate location, and miss the "master"/"inserted"
1936 location. Say, given locations L1, L2 and L3 at addresses A and
1939 L1@A, L2@A, L3@B, ...
1941 BC_L could end up pointing at location L2, while the "master"
1942 location could be L1. Since the `loc->inserted' flag is only set
1943 on "master" locations, we'd forget to restore the shadow of L1
1944 and L2. */
1945 while (bc_l > 0
1946 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1947 bc_l--;
1949 /* Now do full processing of the found relevant range of elements. */
1951 for (bc = bc_l; bc < bp_locations.size (); bc++)
1953 struct bp_location *bl = bp_locations[bc];
1955 /* bp_location array has BL->OWNER always non-NULL. */
1956 if (bl->owner->type == bp_none)
1957 warning (_("reading through apparently deleted breakpoint #%d?"),
1958 bl->owner->number);
1960 /* Performance optimization: any further element can no longer affect BUF
1961 content. */
1963 if (bl->address >= bp_locations_placed_address_before_address_max
1964 && (memaddr + len
1965 <= (bl->address
1966 - bp_locations_placed_address_before_address_max)))
1967 break;
1969 if (!bp_location_has_shadow (bl))
1970 continue;
1972 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1973 memaddr, len, &bl->target_info, bl->gdbarch);
1977 /* See breakpoint.h. */
1979 bool
1980 is_breakpoint (const struct breakpoint *bpt)
1982 return (bpt->type == bp_breakpoint
1983 || bpt->type == bp_hardware_breakpoint
1984 || bpt->type == bp_dprintf);
1987 /* Return true if BPT is of any hardware watchpoint kind. */
1989 static bool
1990 is_hardware_watchpoint (const struct breakpoint *bpt)
1992 return (bpt->type == bp_hardware_watchpoint
1993 || bpt->type == bp_read_watchpoint
1994 || bpt->type == bp_access_watchpoint);
1997 /* See breakpoint.h. */
1999 bool
2000 is_watchpoint (const struct breakpoint *bpt)
2002 return (is_hardware_watchpoint (bpt)
2003 || bpt->type == bp_watchpoint);
2006 /* Returns true if the current thread and its running state are safe
2007 to evaluate or update watchpoint B. Watchpoints on local
2008 expressions need to be evaluated in the context of the thread that
2009 was current when the watchpoint was created, and, that thread needs
2010 to be stopped to be able to select the correct frame context.
2011 Watchpoints on global expressions can be evaluated on any thread,
2012 and in any state. It is presently left to the target allowing
2013 memory accesses when threads are running. */
2015 static bool
2016 watchpoint_in_thread_scope (struct watchpoint *b)
2018 return (b->pspace == current_program_space
2019 && (b->watchpoint_thread == null_ptid
2020 || (inferior_ptid == b->watchpoint_thread
2021 && !inferior_thread ()->executing ())));
2024 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
2025 associated bp_watchpoint_scope breakpoint. */
2027 static void
2028 watchpoint_del_at_next_stop (struct watchpoint *w)
2030 if (w->related_breakpoint != w)
2032 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
2033 gdb_assert (w->related_breakpoint->related_breakpoint == w);
2034 w->related_breakpoint->disposition = disp_del_at_next_stop;
2035 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
2036 w->related_breakpoint = w;
2038 w->disposition = disp_del_at_next_stop;
2039 disable_breakpoint (w);
2042 /* Extract a bitfield value from value VAL using the bit parameters contained in
2043 watchpoint W. */
2045 static struct value *
2046 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
2048 struct value *bit_val;
2050 if (val == NULL)
2051 return NULL;
2053 bit_val = value::allocate (val->type ());
2055 val->unpack_bitfield (bit_val,
2056 w->val_bitpos,
2057 w->val_bitsize,
2058 val->contents_for_printing ().data (),
2059 val->offset ());
2061 return bit_val;
2064 /* Allocate a dummy location and add it to B. This is required
2065 because bpstat_stop_status requires a location to be able to report
2066 stops. */
2068 static void
2069 add_dummy_location (struct breakpoint *b,
2070 struct program_space *pspace)
2072 gdb_assert (!b->has_locations ());
2074 bp_location *loc = new bp_location (b, bp_loc_other);
2075 loc->pspace = pspace;
2076 b->add_location (*loc);
2079 /* Assuming that B is a watchpoint:
2080 - Reparse watchpoint expression, if REPARSE is true
2081 - Evaluate expression and store the result in B->val
2082 - Evaluate the condition if there is one, and store the result
2083 in b->loc->cond.
2084 - Update the list of values that must be watched in B->loc.
2086 If the watchpoint disposition is disp_del_at_next_stop, then do
2087 nothing. If this is local watchpoint that is out of scope, delete
2090 Even with `set breakpoint always-inserted on' the watchpoints are
2091 removed + inserted on each stop here. Normal breakpoints must
2092 never be removed because they might be missed by a running thread
2093 when debugging in non-stop mode. On the other hand, hardware
2094 watchpoints (is_hardware_watchpoint; processed here) are specific
2095 to each LWP since they are stored in each LWP's hardware debug
2096 registers. Therefore, such LWP must be stopped first in order to
2097 be able to modify its hardware watchpoints.
2099 Hardware watchpoints must be reset exactly once after being
2100 presented to the user. It cannot be done sooner, because it would
2101 reset the data used to present the watchpoint hit to the user. And
2102 it must not be done later because it could display the same single
2103 watchpoint hit during multiple GDB stops. Note that the latter is
2104 relevant only to the hardware watchpoint types bp_read_watchpoint
2105 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
2106 not user-visible - its hit is suppressed if the memory content has
2107 not changed.
2109 The following constraints influence the location where we can reset
2110 hardware watchpoints:
2112 * target_stopped_by_watchpoint and target_stopped_data_address are
2113 called several times when GDB stops.
2115 [linux]
2116 * Multiple hardware watchpoints can be hit at the same time,
2117 causing GDB to stop. GDB only presents one hardware watchpoint
2118 hit at a time as the reason for stopping, and all the other hits
2119 are presented later, one after the other, each time the user
2120 requests the execution to be resumed. Execution is not resumed
2121 for the threads still having pending hit event stored in
2122 LWP_INFO->STATUS. While the watchpoint is already removed from
2123 the inferior on the first stop the thread hit event is kept being
2124 reported from its cached value by linux_nat_stopped_data_address
2125 until the real thread resume happens after the watchpoint gets
2126 presented and thus its LWP_INFO->STATUS gets reset.
2128 Therefore the hardware watchpoint hit can get safely reset on the
2129 watchpoint removal from inferior. */
2131 static void
2132 update_watchpoint (struct watchpoint *b, bool reparse)
2134 bool within_current_scope;
2136 /* If this is a local watchpoint, we only want to check if the
2137 watchpoint frame is in scope if the current thread is the thread
2138 that was used to create the watchpoint. */
2139 if (!watchpoint_in_thread_scope (b))
2140 return;
2142 if (b->disposition == disp_del_at_next_stop)
2143 return;
2145 std::optional<scoped_restore_selected_frame> restore_frame;
2147 /* Determine if the watchpoint is within scope. */
2148 if (b->exp_valid_block == NULL)
2149 within_current_scope = true;
2150 else
2152 frame_info_ptr fi = get_current_frame ();
2153 struct gdbarch *frame_arch = get_frame_arch (fi);
2154 CORE_ADDR frame_pc = get_frame_pc (fi);
2156 /* If we're at a point where the stack has been destroyed
2157 (e.g. in a function epilogue), unwinding may not work
2158 properly. Do not attempt to recreate locations at this
2159 point. See similar comments in watchpoint_check. */
2160 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
2161 return;
2163 /* Save the current frame's ID so we can restore it after
2164 evaluating the watchpoint expression on its own frame. */
2165 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
2166 took a frame parameter, so that we didn't have to change the
2167 selected frame. */
2168 restore_frame.emplace ();
2170 fi = frame_find_by_id (b->watchpoint_frame);
2171 within_current_scope = (fi != NULL);
2172 if (within_current_scope)
2173 select_frame (fi);
2176 /* We don't free locations. They are stored in the bp_location array
2177 and update_global_location_list will eventually delete them and
2178 remove breakpoints if needed. */
2179 b->clear_locations ();
2181 if (within_current_scope && reparse)
2183 const char *s;
2185 b->exp.reset ();
2186 s = (b->exp_string_reparse
2187 ? b->exp_string_reparse.get ()
2188 : b->exp_string.get ());
2189 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
2190 /* If the meaning of expression itself changed, the old value is
2191 no longer relevant. We don't want to report a watchpoint hit
2192 to the user when the old value and the new value may actually
2193 be completely different objects. */
2194 b->val = NULL;
2195 b->val_valid = false;
2197 /* Note that unlike with breakpoints, the watchpoint's condition
2198 expression is stored in the breakpoint object, not in the
2199 locations (re)created below. */
2200 if (b->cond_string != NULL)
2202 b->cond_exp.reset ();
2204 s = b->cond_string.get ();
2205 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
2209 /* If we failed to parse the expression, for example because
2210 it refers to a global variable in a not-yet-loaded shared library,
2211 don't try to insert watchpoint. We don't automatically delete
2212 such watchpoint, though, since failure to parse expression
2213 is different from out-of-scope watchpoint. */
2214 if (!target_has_execution ())
2216 /* Without execution, memory can't change. No use to try and
2217 set watchpoint locations. The watchpoint will be reset when
2218 the target gains execution, through breakpoint_re_set. */
2219 if (!can_use_hw_watchpoints)
2221 if (b->works_in_software_mode ())
2222 b->type = bp_watchpoint;
2223 else
2224 error (_("Can't set read/access watchpoint when "
2225 "hardware watchpoints are disabled."));
2228 else if (within_current_scope && b->exp)
2230 std::vector<value_ref_ptr> val_chain;
2231 struct value *v, *result;
2232 struct program_space *wp_pspace;
2234 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
2235 &val_chain, false);
2237 /* Avoid setting b->val if it's already set. The meaning of
2238 b->val is 'the last value' user saw, and we should update
2239 it only if we reported that last value to user. As it
2240 happens, the code that reports it updates b->val directly.
2241 We don't keep track of the memory value for masked
2242 watchpoints. */
2243 if (!b->val_valid && !is_masked_watchpoint (b))
2245 if (b->val_bitsize != 0)
2246 v = extract_bitfield_from_watchpoint_value (b, v);
2247 b->val = release_value (v);
2248 b->val_valid = true;
2251 if (b->exp_valid_block == nullptr)
2252 wp_pspace = current_program_space;
2253 else
2254 wp_pspace = get_frame_program_space (get_selected_frame (NULL));
2256 /* Look at each value on the value chain. */
2257 gdb_assert (!val_chain.empty ());
2258 for (const value_ref_ptr &iter : val_chain)
2260 v = iter.get ();
2262 /* If it's a memory location, and GDB actually needed
2263 its contents to evaluate the expression, then we
2264 must watch it. If the first value returned is
2265 still lazy, that means an error occurred reading it;
2266 watch it anyway in case it becomes readable. */
2267 if (v->lval () == lval_memory
2268 && (v == val_chain[0] || ! v->lazy ()))
2270 struct type *vtype = check_typedef (v->type ());
2272 /* We only watch structs and arrays if user asked
2273 for it explicitly, never if they just happen to
2274 appear in the middle of some value chain. */
2275 if (v == result
2276 || (vtype->code () != TYPE_CODE_STRUCT
2277 && vtype->code () != TYPE_CODE_ARRAY))
2279 CORE_ADDR addr;
2280 enum target_hw_bp_type type;
2281 int bitpos = 0, bitsize = 0;
2283 if (v->bitsize () != 0)
2285 /* Extract the bit parameters out from the bitfield
2286 sub-expression. */
2287 bitpos = v->bitpos ();
2288 bitsize = v->bitsize ();
2290 else if (v == result && b->val_bitsize != 0)
2292 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2293 lvalue whose bit parameters are saved in the fields
2294 VAL_BITPOS and VAL_BITSIZE. */
2295 bitpos = b->val_bitpos;
2296 bitsize = b->val_bitsize;
2299 addr = v->address ();
2300 if (bitsize != 0)
2302 /* Skip the bytes that don't contain the bitfield. */
2303 addr += bitpos / 8;
2306 type = hw_write;
2307 if (b->type == bp_read_watchpoint)
2308 type = hw_read;
2309 else if (b->type == bp_access_watchpoint)
2310 type = hw_access;
2312 bp_location *loc = b->allocate_location ();
2313 loc->gdbarch = v->type ()->arch ();
2314 loc->pspace = wp_pspace;
2315 loc->address
2316 = gdbarch_remove_non_address_bits_watchpoint (loc->gdbarch,
2317 addr);
2318 b->add_location (*loc);
2320 if (bitsize != 0)
2322 /* Just cover the bytes that make up the bitfield. */
2323 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2325 else
2326 loc->length = v->type ()->length ();
2328 loc->watchpoint_type = type;
2333 /* Helper function to bundle possibly emitting a warning along with
2334 changing the type of B to bp_watchpoint. */
2335 auto change_type_to_bp_watchpoint = [] (breakpoint *bp)
2337 /* Only warn for breakpoints that have been assigned a +ve number,
2338 anything else is either an internal watchpoint (which we don't
2339 currently create) or has not yet been finalized, in which case
2340 this change of type will be occurring before the user is told
2341 the type of this watchpoint. */
2342 if (bp->type == bp_hardware_watchpoint && bp->number > 0)
2343 warning (_("watchpoint %d downgraded to software watchpoint"),
2344 bp->number);
2345 bp->type = bp_watchpoint;
2348 /* Change the type of breakpoint between hardware assisted or
2349 an ordinary watchpoint depending on the hardware support and
2350 free hardware slots. Recheck the number of free hardware slots
2351 as the value chain may have changed. */
2353 int reg_cnt;
2354 enum bp_loc_type loc_type;
2356 reg_cnt = can_use_hardware_watchpoint (val_chain);
2358 if (reg_cnt)
2360 int i, target_resources_ok, other_type_used;
2361 enum bptype type;
2363 /* Use an exact watchpoint when there's only one memory region to be
2364 watched, and only one debug register is needed to watch it. */
2365 b->exact = target_exact_watchpoints && reg_cnt == 1;
2367 /* We need to determine how many resources are already
2368 used for all other hardware watchpoints plus this one
2369 to see if we still have enough resources to also fit
2370 this watchpoint in as well. */
2372 /* If this is a software watchpoint, we try to turn it
2373 to a hardware one -- count resources as if B was of
2374 hardware watchpoint type. */
2375 type = b->type;
2376 if (type == bp_watchpoint)
2377 type = bp_hardware_watchpoint;
2379 /* This watchpoint may or may not have been placed on
2380 the list yet at this point (it won't be in the list
2381 if we're trying to create it for the first time,
2382 through watch_command), so always account for it
2383 manually. */
2385 /* Count resources used by all watchpoints except B. */
2386 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2388 /* Add in the resources needed for B. */
2389 i += hw_watchpoint_use_count (b);
2391 target_resources_ok
2392 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2393 if (target_resources_ok <= 0)
2395 bool sw_mode = b->works_in_software_mode ();
2397 if (target_resources_ok == 0 && !sw_mode)
2398 error (_("Target does not support this type of "
2399 "hardware watchpoint."));
2400 else if (target_resources_ok < 0 && !sw_mode)
2401 error (_("There are not enough available hardware "
2402 "resources for this watchpoint."));
2404 /* Downgrade to software watchpoint. */
2405 change_type_to_bp_watchpoint (b);
2407 else
2409 /* If this was a software watchpoint, we've just
2410 found we have enough resources to turn it to a
2411 hardware watchpoint. Otherwise, this is a
2412 nop. */
2413 b->type = type;
2416 else if (!b->works_in_software_mode ())
2418 if (!can_use_hw_watchpoints)
2419 error (_("Can't set read/access watchpoint when "
2420 "hardware watchpoints are disabled."));
2421 else
2422 error (_("Expression cannot be implemented with "
2423 "read/access watchpoint."));
2425 else
2426 change_type_to_bp_watchpoint (b);
2428 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
2429 : bp_loc_hardware_watchpoint);
2431 for (bp_location &bl : b->locations ())
2432 bl.loc_type = loc_type;
2435 /* If a software watchpoint is not watching any memory, then the
2436 above left it without any location set up. But,
2437 bpstat_stop_status requires a location to be able to report
2438 stops, so make sure there's at least a dummy one. */
2439 if (b->type == bp_watchpoint && !b->has_locations ())
2440 add_dummy_location (b, wp_pspace);
2442 else if (!within_current_scope)
2444 gdb_printf (_("\
2445 Watchpoint %d deleted because the program has left the block\n\
2446 in which its expression is valid.\n"),
2447 b->number);
2448 watchpoint_del_at_next_stop (b);
2452 /* Returns true iff breakpoint location should be
2453 inserted in the inferior. We don't differentiate the type of BL's owner
2454 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2455 breakpoint_ops is not defined, because in insert_bp_location,
2456 tracepoint's insert_location will not be called. */
2458 static bool
2459 should_be_inserted (struct bp_location *bl)
2461 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2462 return false;
2464 if (bl->owner->disposition == disp_del_at_next_stop)
2465 return false;
2467 if (!bl->enabled || bl->disabled_by_cond
2468 || bl->shlib_disabled || bl->duplicate)
2469 return false;
2471 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2472 return false;
2474 /* This is set for example, when we're attached to the parent of a
2475 vfork, and have detached from the child. The child is running
2476 free, and we expect it to do an exec or exit, at which point the
2477 OS makes the parent schedulable again (and the target reports
2478 that the vfork is done). Until the child is done with the shared
2479 memory region, do not insert breakpoints in the parent, otherwise
2480 the child could still trip on the parent's breakpoints. Since
2481 the parent is blocked anyway, it won't miss any breakpoint. */
2482 if (bl->pspace->breakpoints_not_allowed)
2483 return false;
2485 /* Don't insert a breakpoint if we're trying to step past its
2486 location, except if the breakpoint is a single-step breakpoint,
2487 and the breakpoint's thread is the thread which is stepping past
2488 a breakpoint. */
2489 if ((bl->loc_type == bp_loc_software_breakpoint
2490 || bl->loc_type == bp_loc_hardware_breakpoint)
2491 && stepping_past_instruction_at (bl->pspace->aspace.get (),
2492 bl->address)
2493 /* The single-step breakpoint may be inserted at the location
2494 we're trying to step if the instruction branches to itself.
2495 However, the instruction won't be executed at all and it may
2496 break the semantics of the instruction, for example, the
2497 instruction is a conditional branch or updates some flags.
2498 We can't fix it unless GDB is able to emulate the instruction
2499 or switch to displaced stepping. */
2500 && !(bl->owner->type == bp_single_step
2501 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2503 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2504 paddress (bl->gdbarch, bl->address));
2505 return false;
2508 /* Don't insert watchpoints if we're trying to step past the
2509 instruction that triggered one. */
2510 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2511 && stepping_past_nonsteppable_watchpoint ())
2513 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2514 "skipping watchpoint at %s:%d",
2515 paddress (bl->gdbarch, bl->address), bl->length);
2516 return false;
2519 return true;
2522 /* Same as should_be_inserted but does the check assuming
2523 that the location is not duplicated. */
2525 static bool
2526 unduplicated_should_be_inserted (struct bp_location *bl)
2528 scoped_restore restore_bl_duplicate
2529 = make_scoped_restore (&bl->duplicate, 0);
2531 return should_be_inserted (bl);
2534 /* Parses a conditional described by an expression COND into an
2535 agent expression bytecode suitable for evaluation
2536 by the bytecode interpreter. Return NULL if there was
2537 any error during parsing. */
2539 static agent_expr_up
2540 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2542 if (cond == NULL)
2543 return NULL;
2545 agent_expr_up aexpr;
2547 /* We don't want to stop processing, so catch any errors
2548 that may show up. */
2551 aexpr = gen_eval_for_expr (scope, cond);
2554 catch (const gdb_exception_error &ex)
2556 /* If we got here, it means the condition could not be parsed to a valid
2557 bytecode expression and thus can't be evaluated on the target's side.
2558 It's no use iterating through the conditions. */
2561 /* We have a valid agent expression. */
2562 return aexpr;
2565 /* Based on location BL, create a list of breakpoint conditions to be
2566 passed on to the target. If we have duplicated locations with different
2567 conditions, we will add such conditions to the list. The idea is that the
2568 target will evaluate the list of conditions and will only notify GDB when
2569 one of them is true. */
2571 static void
2572 build_target_condition_list (struct bp_location *bl)
2574 bool null_condition_or_parse_error = false;
2575 int modified = bl->needs_update;
2577 /* Release conditions left over from a previous insert. */
2578 bl->target_info.conditions.clear ();
2580 /* This is only meaningful if the target is
2581 evaluating conditions and if the user has
2582 opted for condition evaluation on the target's
2583 side. */
2584 if (gdb_evaluates_breakpoint_condition_p ()
2585 || !target_supports_evaluation_of_breakpoint_conditions ())
2586 return;
2588 auto loc_range = all_bp_locations_at_addr (bl->address);
2590 /* Do a first pass to check for locations with no assigned
2591 conditions or conditions that fail to parse to a valid agent
2592 expression bytecode. If any of these happen, then it's no use to
2593 send conditions to the target since this location will always
2594 trigger and generate a response back to GDB. Note we consider
2595 all locations at the same address irrespective of type, i.e.,
2596 even if the locations aren't considered duplicates (e.g.,
2597 software breakpoint and hardware breakpoint at the same
2598 address). */
2599 for (bp_location *loc : loc_range)
2601 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2603 if (modified)
2605 /* Re-parse the conditions since something changed. In that
2606 case we already freed the condition bytecodes (see
2607 force_breakpoint_reinsertion). We just
2608 need to parse the condition to bytecodes again. */
2609 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2610 loc->cond.get ());
2613 /* If we have a NULL bytecode expression, it means something
2614 went wrong or we have a null condition expression. */
2615 if (!loc->cond_bytecode)
2617 null_condition_or_parse_error = true;
2618 break;
2623 /* If any of these happened, it means we will have to evaluate the conditions
2624 for the location's address on gdb's side. It is no use keeping bytecodes
2625 for all the other duplicate locations, thus we free all of them here.
2627 This is so we have a finer control over which locations' conditions are
2628 being evaluated by GDB or the remote stub. */
2629 if (null_condition_or_parse_error)
2631 for (bp_location *loc : loc_range)
2633 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2635 /* Only go as far as the first NULL bytecode is
2636 located. */
2637 if (!loc->cond_bytecode)
2638 return;
2640 loc->cond_bytecode.reset ();
2645 /* No NULL conditions or failed bytecode generation. Build a
2646 condition list for this location's address. If we have software
2647 and hardware locations at the same address, they aren't
2648 considered duplicates, but we still merge all the conditions
2649 anyway, as it's simpler, and doesn't really make a practical
2650 difference. */
2651 for (bp_location *loc : loc_range)
2652 if (loc->cond
2653 && is_breakpoint (loc->owner)
2654 && loc->pspace->num == bl->pspace->num
2655 && loc->owner->enable_state == bp_enabled
2656 && loc->enabled
2657 && !loc->disabled_by_cond)
2659 /* Add the condition to the vector. This will be used later
2660 to send the conditions to the target. */
2661 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2664 return;
2667 /* Parses a command described by string CMD into an agent expression
2668 bytecode suitable for evaluation by the bytecode interpreter.
2669 Return NULL if there was any error during parsing. */
2671 static agent_expr_up
2672 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2674 const char *cmdrest;
2675 const char *format_start, *format_end;
2676 struct gdbarch *gdbarch = get_current_arch ();
2678 if (cmd == NULL)
2679 return NULL;
2681 cmdrest = cmd;
2683 if (*cmdrest == ',')
2684 ++cmdrest;
2685 cmdrest = skip_spaces (cmdrest);
2687 if (*cmdrest++ != '"')
2688 error (_("No format string following the location"));
2690 format_start = cmdrest;
2692 format_pieces fpieces (&cmdrest);
2694 format_end = cmdrest;
2696 if (*cmdrest++ != '"')
2697 error (_("Bad format string, non-terminated '\"'."));
2699 cmdrest = skip_spaces (cmdrest);
2701 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2702 error (_("Invalid argument syntax"));
2704 if (*cmdrest == ',')
2705 cmdrest++;
2706 cmdrest = skip_spaces (cmdrest);
2708 /* For each argument, make an expression. */
2710 std::vector<struct expression *> argvec;
2711 while (*cmdrest != '\0')
2713 const char *cmd1;
2715 cmd1 = cmdrest;
2716 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope),
2717 PARSER_COMMA_TERMINATES);
2718 argvec.push_back (expr.release ());
2719 cmdrest = cmd1;
2720 if (*cmdrest == ',')
2721 ++cmdrest;
2724 agent_expr_up aexpr;
2726 /* We don't want to stop processing, so catch any errors
2727 that may show up. */
2730 aexpr = gen_printf (scope, gdbarch, 0, 0,
2731 format_start, format_end - format_start,
2732 argvec.size (), argvec.data ());
2734 catch (const gdb_exception_error &ex)
2736 /* If we got here, it means the command could not be parsed to a valid
2737 bytecode expression and thus can't be evaluated on the target's side.
2738 It's no use iterating through the other commands. */
2741 /* We have a valid agent expression, return it. */
2742 return aexpr;
2745 /* Based on location BL, create a list of breakpoint commands to be
2746 passed on to the target. If we have duplicated locations with
2747 different commands, we will add any such to the list. */
2749 static void
2750 build_target_command_list (struct bp_location *bl)
2752 bool null_command_or_parse_error = false;
2753 int modified = bl->needs_update;
2755 /* Clear commands left over from a previous insert. */
2756 bl->target_info.tcommands.clear ();
2758 if (!target_can_run_breakpoint_commands ())
2759 return;
2761 /* For now, limit to agent-style dprintf breakpoints. */
2762 if (dprintf_style != dprintf_style_agent)
2763 return;
2765 auto loc_range = all_bp_locations_at_addr (bl->address);
2767 /* For now, if we have any location at the same address that isn't a
2768 dprintf, don't install the target-side commands, as that would
2769 make the breakpoint not be reported to the core, and we'd lose
2770 control. */
2771 for (bp_location *loc : loc_range)
2772 if (is_breakpoint (loc->owner)
2773 && loc->pspace->num == bl->pspace->num
2774 && loc->owner->type != bp_dprintf)
2775 return;
2777 /* Do a first pass to check for locations with no assigned
2778 conditions or conditions that fail to parse to a valid agent expression
2779 bytecode. If any of these happen, then it's no use to send conditions
2780 to the target since this location will always trigger and generate a
2781 response back to GDB. */
2782 for (bp_location *loc : loc_range)
2784 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2786 if (modified)
2788 /* Re-parse the commands since something changed. In that
2789 case we already freed the command bytecodes (see
2790 force_breakpoint_reinsertion). We just
2791 need to parse the command to bytecodes again. */
2792 loc->cmd_bytecode
2793 = parse_cmd_to_aexpr (bl->address,
2794 loc->owner->extra_string.get ());
2797 /* If we have a NULL bytecode expression, it means something
2798 went wrong or we have a null command expression. */
2799 if (!loc->cmd_bytecode)
2801 null_command_or_parse_error = true;
2802 break;
2807 /* If anything failed, then we're not doing target-side commands,
2808 and so clean up. */
2809 if (null_command_or_parse_error)
2811 for (bp_location *loc : loc_range)
2812 if (is_breakpoint (loc->owner)
2813 && loc->pspace->num == bl->pspace->num)
2815 /* Only go as far as the first NULL bytecode is
2816 located. */
2817 if (loc->cmd_bytecode == NULL)
2818 return;
2820 loc->cmd_bytecode.reset ();
2824 /* No NULL commands or failed bytecode generation. Build a command
2825 list for all duplicate locations at this location's address.
2826 Note that here we must care for whether the breakpoint location
2827 types are considered duplicates, otherwise, say, if we have a
2828 software and hardware location at the same address, the target
2829 could end up running the commands twice. For the moment, we only
2830 support targets-side commands with dprintf, but it doesn't hurt
2831 to be pedantically correct in case that changes. */
2832 for (bp_location *loc : loc_range)
2833 if (breakpoint_locations_match (bl, loc)
2834 && loc->owner->extra_string
2835 && is_breakpoint (loc->owner)
2836 && loc->pspace->num == bl->pspace->num
2837 && loc->owner->enable_state == bp_enabled
2838 && loc->enabled
2839 && !loc->disabled_by_cond)
2841 /* Add the command to the vector. This will be used later
2842 to send the commands to the target. */
2843 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2846 bl->target_info.persist = 0;
2847 /* Maybe flag this location as persistent. */
2848 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2849 bl->target_info.persist = 1;
2852 /* Return the kind of breakpoint on address *ADDR. Get the kind
2853 of breakpoint according to ADDR except single-step breakpoint.
2854 Get the kind of single-step breakpoint according to the current
2855 registers state. */
2857 static int
2858 breakpoint_kind (const struct bp_location *bl, CORE_ADDR *addr)
2860 if (bl->owner->type == bp_single_step)
2862 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2863 struct regcache *regcache;
2865 regcache = get_thread_regcache (thr);
2867 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2868 regcache, addr);
2870 else
2871 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2874 /* Rethrow the currently handled exception, if it's a TARGET_CLOSE_ERROR.
2875 E is either the currently handled exception, or a copy, or a sliced copy,
2876 so we can't rethrow that one, but we can use it to inspect the properties
2877 of the currently handled exception. */
2879 static void
2880 rethrow_on_target_close_error (const gdb_exception &e)
2882 if (e.reason == 0)
2883 return;
2884 /* Can't set the breakpoint. */
2886 if (e.error != TARGET_CLOSE_ERROR)
2887 return;
2889 /* If the target has closed then it will have deleted any breakpoints
2890 inserted within the target inferior, as a result any further attempts
2891 to interact with the breakpoint objects is not possible. Just rethrow
2892 the error. Don't use e to rethrow, to prevent object slicing of the
2893 exception. */
2894 throw;
2897 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2898 location. Any error messages are printed to TMP_ERROR_STREAM; and
2899 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2900 Returns 0 for success, 1 if the bp_location type is not supported or
2901 -1 for failure.
2903 NOTE drow/2003-09-09: This routine could be broken down to an
2904 object-style method for each breakpoint or catchpoint type. */
2905 static int
2906 insert_bp_location (struct bp_location *bl,
2907 struct ui_file *tmp_error_stream,
2908 int *disabled_breaks,
2909 int *hw_breakpoint_error,
2910 int *hw_bp_error_explained_already)
2912 gdb_exception bp_excpt;
2914 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2915 return 0;
2917 breakpoint_debug_printf ("%s", breakpoint_location_address_str (bl).c_str ());
2919 /* Note we don't initialize bl->target_info, as that wipes out
2920 the breakpoint location's shadow_contents if the breakpoint
2921 is still inserted at that location. This in turn breaks
2922 target_read_memory which depends on these buffers when
2923 a memory read is requested at the breakpoint location:
2924 Once the target_info has been wiped, we fail to see that
2925 we have a breakpoint inserted at that address and thus
2926 read the breakpoint instead of returning the data saved in
2927 the breakpoint location's shadow contents. */
2928 bl->target_info.reqstd_address = bl->address;
2929 bl->target_info.placed_address_space = bl->pspace->aspace.get ();
2930 bl->target_info.length = bl->length;
2932 /* When working with target-side conditions, we must pass all the conditions
2933 for the same breakpoint address down to the target since GDB will not
2934 insert those locations. With a list of breakpoint conditions, the target
2935 can decide when to stop and notify GDB. */
2937 if (is_breakpoint (bl->owner))
2939 build_target_condition_list (bl);
2940 build_target_command_list (bl);
2941 /* Reset the modification marker. */
2942 bl->needs_update = 0;
2945 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2946 set at a read-only address, then a breakpoint location will have
2947 been changed to hardware breakpoint before we get here. If it is
2948 "off" however, error out before actually trying to insert the
2949 breakpoint, with a nicer error message. */
2950 if (bl->loc_type == bp_loc_software_breakpoint
2951 && !automatic_hardware_breakpoints)
2953 mem_region *mr = lookup_mem_region (bl->address);
2955 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2957 gdb_printf (tmp_error_stream,
2958 _("Cannot insert breakpoint %d.\n"
2959 "Cannot set software breakpoint "
2960 "at read-only address %s\n"),
2961 bl->owner->number,
2962 paddress (bl->gdbarch, bl->address));
2963 return 1;
2967 if (bl->loc_type == bp_loc_software_breakpoint
2968 || bl->loc_type == bp_loc_hardware_breakpoint)
2970 /* First check to see if we have to handle an overlay. */
2971 if (overlay_debugging == ovly_off
2972 || bl->section == NULL
2973 || !(section_is_overlay (bl->section)))
2975 /* No overlay handling: just set the breakpoint. */
2978 int val;
2980 val = bl->owner->insert_location (bl);
2981 if (val)
2982 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2984 catch (gdb_exception &e)
2986 rethrow_on_target_close_error (e);
2987 bp_excpt = std::move (e);
2990 else
2992 /* This breakpoint is in an overlay section.
2993 Shall we set a breakpoint at the LMA? */
2994 if (!overlay_events_enabled)
2996 /* Yes -- overlay event support is not active,
2997 so we must try to set a breakpoint at the LMA.
2998 This will not work for a hardware breakpoint. */
2999 if (bl->loc_type == bp_loc_hardware_breakpoint)
3000 warning (_("hardware breakpoint %d not supported in overlay!"),
3001 bl->owner->number);
3002 else
3004 CORE_ADDR addr = overlay_unmapped_address (bl->address,
3005 bl->section);
3006 /* Set a software (trap) breakpoint at the LMA. */
3007 bl->overlay_target_info = bl->target_info;
3008 bl->overlay_target_info.reqstd_address = addr;
3010 /* No overlay handling: just set the breakpoint. */
3013 int val;
3015 bl->overlay_target_info.kind
3016 = breakpoint_kind (bl, &addr);
3017 bl->overlay_target_info.placed_address = addr;
3018 val = target_insert_breakpoint (bl->gdbarch,
3019 &bl->overlay_target_info);
3020 if (val)
3021 bp_excpt
3022 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
3024 catch (gdb_exception &e)
3026 rethrow_on_target_close_error (e);
3027 bp_excpt = std::move (e);
3030 if (bp_excpt.reason != 0)
3031 gdb_printf (tmp_error_stream,
3032 "Overlay breakpoint %d "
3033 "failed: in ROM?\n",
3034 bl->owner->number);
3037 /* Shall we set a breakpoint at the VMA? */
3038 if (section_is_mapped (bl->section))
3040 /* Yes. This overlay section is mapped into memory. */
3043 int val;
3045 val = bl->owner->insert_location (bl);
3046 if (val)
3047 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
3049 catch (gdb_exception_error &e)
3051 rethrow_on_target_close_error (e);
3052 bp_excpt = std::move (e);
3055 else
3057 /* No. This breakpoint will not be inserted.
3058 No error, but do not mark the bp as 'inserted'. */
3059 return 0;
3063 if (bp_excpt.reason != 0)
3065 /* Can't set the breakpoint. */
3066 gdb_assert (bl->owner != nullptr);
3068 /* In some cases, we might not be able to insert a
3069 breakpoint in a shared library that has already been
3070 removed, but we have not yet processed the shlib unload
3071 event. Unfortunately, some targets that implement
3072 breakpoint insertion themselves can't tell why the
3073 breakpoint insertion failed (e.g., the remote target
3074 doesn't define error codes), so we must treat generic
3075 errors as memory errors. */
3076 if (bp_excpt.reason == RETURN_ERROR
3077 && (bp_excpt.error == GENERIC_ERROR
3078 || bp_excpt.error == MEMORY_ERROR)
3079 && bl->loc_type == bp_loc_software_breakpoint
3080 && (solib_name_from_address (bl->pspace, bl->address)
3081 || shared_objfile_contains_address_p (bl->pspace,
3082 bl->address)))
3084 /* See also: disable_breakpoints_in_shlibs. */
3085 bl->shlib_disabled = 1;
3086 notify_breakpoint_modified (bl->owner);
3087 if (!*disabled_breaks)
3089 gdb_printf (tmp_error_stream,
3090 "Cannot insert breakpoint %d.\n",
3091 bl->owner->number);
3092 gdb_printf (tmp_error_stream,
3093 "Temporarily disabling shared "
3094 "library breakpoints:\n");
3096 *disabled_breaks = 1;
3097 gdb_printf (tmp_error_stream,
3098 "breakpoint #%d\n", bl->owner->number);
3099 return 0;
3101 else
3103 if (bl->loc_type == bp_loc_hardware_breakpoint)
3105 *hw_breakpoint_error = 1;
3106 *hw_bp_error_explained_already = bp_excpt.message != NULL;
3107 gdb_printf (tmp_error_stream,
3108 "Cannot insert hardware breakpoint %d%s",
3109 bl->owner->number,
3110 bp_excpt.message ? ":" : ".\n");
3111 if (bp_excpt.message != NULL)
3112 gdb_printf (tmp_error_stream, "%s.\n",
3113 bp_excpt.what ());
3115 else
3117 if (bp_excpt.message == NULL)
3119 std::string message
3120 = memory_error_message (TARGET_XFER_E_IO,
3121 bl->gdbarch, bl->address);
3123 gdb_printf (tmp_error_stream,
3124 "Cannot insert breakpoint %d.\n"
3125 "%s\n",
3126 bl->owner->number, message.c_str ());
3128 else
3130 gdb_printf (tmp_error_stream,
3131 "Cannot insert breakpoint %d: %s\n",
3132 bl->owner->number,
3133 bp_excpt.what ());
3136 return 1;
3140 else
3141 bl->inserted = 1;
3143 return 0;
3146 else if (bl->loc_type == bp_loc_hardware_watchpoint
3147 && bl->owner->disposition != disp_del_at_next_stop)
3149 int val;
3151 val = bl->owner->insert_location (bl);
3153 /* If trying to set a read-watchpoint, and it turns out it's not
3154 supported, try emulating one with an access watchpoint. */
3155 if (val == 1 && bl->watchpoint_type == hw_read)
3157 /* But don't try to insert it, if there's already another
3158 hw_access location that would be considered a duplicate
3159 of this one. */
3160 for (bp_location *loc : all_bp_locations ())
3161 if (loc != bl
3162 && loc->watchpoint_type == hw_access
3163 && watchpoint_locations_match (bl, loc))
3165 bl->duplicate = 1;
3166 bl->inserted = 1;
3167 bl->target_info = loc->target_info;
3168 bl->watchpoint_type = hw_access;
3169 val = 0;
3170 break;
3173 if (val == 1)
3175 bl->watchpoint_type = hw_access;
3176 val = bl->owner->insert_location (bl);
3178 if (val)
3179 /* Back to the original value. */
3180 bl->watchpoint_type = hw_read;
3184 bl->inserted = (val == 0);
3187 else if (bl->owner->type == bp_catchpoint)
3189 int val;
3191 val = bl->owner->insert_location (bl);
3192 if (val)
3194 bl->owner->enable_state = bp_disabled;
3196 if (val == 1)
3197 warning (_("\
3198 Error inserting catchpoint %d: Your system does not support this type\n\
3199 of catchpoint."), bl->owner->number);
3200 else
3201 warning (_("Error inserting catchpoint %d."), bl->owner->number);
3204 bl->inserted = (val == 0);
3206 /* We've already printed an error message if there was a problem
3207 inserting this catchpoint, and we've disabled the catchpoint,
3208 so just return success. */
3209 return 0;
3212 return 0;
3215 /* This function is called when program space PSPACE is about to be
3216 deleted. It takes care of updating breakpoints to not reference
3217 PSPACE anymore. */
3219 void
3220 breakpoint_program_space_exit (struct program_space *pspace)
3222 /* Remove any breakpoint that was set through this program space. */
3223 for (breakpoint &b : all_breakpoints_safe ())
3224 if (b.pspace == pspace)
3225 delete_breakpoint (&b);
3227 /* Breakpoints set through other program spaces could have locations
3228 bound to PSPACE as well. Remove those. */
3229 for (bp_location *loc : all_bp_locations ())
3230 if (loc->pspace == pspace)
3232 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3233 loc->owner->unadd_location (*loc);
3236 /* Now update the global location list to permanently delete the
3237 removed locations above. */
3238 update_global_location_list (UGLL_DONT_INSERT);
3241 /* Make sure all breakpoints are inserted in inferior.
3242 Throws exception on any error.
3243 A breakpoint that is already inserted won't be inserted
3244 again, so calling this function twice is safe. */
3245 void
3246 insert_breakpoints (void)
3248 for (breakpoint &bpt : all_breakpoints ())
3249 if (is_hardware_watchpoint (&bpt))
3251 watchpoint &w = gdb::checked_static_cast<watchpoint &> (bpt);
3253 update_watchpoint (&w, false /* don't reparse. */);
3256 /* Updating watchpoints creates new locations, so update the global
3257 location list. Explicitly tell ugll to insert locations and
3258 ignore breakpoints_always_inserted_mode. Also,
3259 update_global_location_list tries to "upgrade" software
3260 breakpoints to hardware breakpoints to handle "set breakpoint
3261 auto-hw", so we need to call it even if we don't have new
3262 locations. */
3263 update_global_location_list (UGLL_INSERT);
3266 /* This is used when we need to synch breakpoint conditions between GDB and the
3267 target. It is the case with deleting and disabling of breakpoints when using
3268 always-inserted mode. */
3270 static void
3271 update_inserted_breakpoint_locations (void)
3273 int error_flag = 0;
3274 int val = 0;
3275 int disabled_breaks = 0;
3276 int hw_breakpoint_error = 0;
3277 int hw_bp_details_reported = 0;
3279 string_file tmp_error_stream;
3281 /* Explicitly mark the warning -- this will only be printed if
3282 there was an error. */
3283 tmp_error_stream.puts ("Warning:\n");
3285 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3287 for (bp_location *bl : all_bp_locations ())
3289 /* We only want to update software breakpoints and hardware
3290 breakpoints. */
3291 if (!is_breakpoint (bl->owner))
3292 continue;
3294 /* We only want to update locations that are already inserted
3295 and need updating. This is to avoid unwanted insertion during
3296 deletion of breakpoints. */
3297 if (!bl->inserted || !bl->needs_update)
3298 continue;
3300 switch_to_program_space_and_thread (bl->pspace);
3302 /* For targets that support global breakpoints, there's no need
3303 to select an inferior to insert breakpoint to. In fact, even
3304 if we aren't attached to any process yet, we should still
3305 insert breakpoints. */
3306 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ())
3307 && (inferior_ptid == null_ptid || !target_has_execution ()))
3308 continue;
3310 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3311 &hw_breakpoint_error, &hw_bp_details_reported);
3312 if (val)
3313 error_flag = val;
3316 if (error_flag)
3318 target_terminal::ours_for_output ();
3319 error (("%s"), tmp_error_stream.c_str ());
3323 /* Used when starting or continuing the program. */
3325 static void
3326 insert_breakpoint_locations (void)
3328 int error_flag = 0;
3329 int val = 0;
3330 int disabled_breaks = 0;
3331 int hw_breakpoint_error = 0;
3332 int hw_bp_error_explained_already = 0;
3334 string_file tmp_error_stream;
3336 /* Explicitly mark the warning -- this will only be printed if
3337 there was an error. */
3338 tmp_error_stream.puts ("Warning:\n");
3340 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3342 for (bp_location *bl : all_bp_locations ())
3344 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3345 continue;
3347 /* There is no point inserting thread-specific breakpoints if
3348 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3349 has BL->OWNER always non-NULL. */
3350 if (bl->owner->thread != -1
3351 && !valid_global_thread_id (bl->owner->thread))
3352 continue;
3354 /* Or inferior specific breakpoints if the inferior no longer
3355 exists. */
3356 if (bl->owner->inferior != -1
3357 && !valid_global_inferior_id (bl->owner->inferior))
3358 continue;
3360 switch_to_program_space_and_thread (bl->pspace);
3362 /* For targets that support global breakpoints, there's no need
3363 to select an inferior to insert breakpoint to. In fact, even
3364 if we aren't attached to any process yet, we should still
3365 insert breakpoints. */
3366 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ())
3367 && (inferior_ptid == null_ptid || !target_has_execution ()))
3368 continue;
3370 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3371 &hw_breakpoint_error, &hw_bp_error_explained_already);
3372 if (val)
3373 error_flag = val;
3376 /* If we failed to insert all locations of a watchpoint, remove
3377 them, as half-inserted watchpoint is of limited use. */
3378 for (breakpoint &bpt : all_breakpoints ())
3380 bool some_failed = false;
3382 if (!is_hardware_watchpoint (&bpt))
3383 continue;
3385 if (!breakpoint_enabled (&bpt))
3386 continue;
3388 if (bpt.disposition == disp_del_at_next_stop)
3389 continue;
3391 for (bp_location &loc : bpt.locations ())
3392 if (!loc.inserted && should_be_inserted (&loc))
3394 some_failed = true;
3395 break;
3398 if (some_failed)
3400 for (bp_location &loc : bpt.locations ())
3401 if (loc.inserted)
3402 remove_breakpoint (&loc);
3404 hw_breakpoint_error = 1;
3405 tmp_error_stream.printf ("Could not insert "
3406 "hardware watchpoint %d.\n",
3407 bpt.number);
3408 error_flag = -1;
3412 if (error_flag)
3414 /* If a hardware breakpoint or watchpoint was inserted, add a
3415 message about possibly exhausted resources. */
3416 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3418 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3419 You may have requested too many hardware breakpoints/watchpoints.\n");
3421 target_terminal::ours_for_output ();
3422 error (("%s"), tmp_error_stream.c_str ());
3426 /* Used when the program stops.
3427 Returns zero if successful, or non-zero if there was a problem
3428 removing a breakpoint location. */
3431 remove_breakpoints (void)
3433 int val = 0;
3435 for (bp_location *bl : all_bp_locations ())
3436 if (bl->inserted && !is_tracepoint (bl->owner))
3437 val |= remove_breakpoint (bl);
3439 return val;
3442 /* When a thread exits, remove breakpoints that are related to
3443 that thread. */
3445 static void
3446 remove_threaded_breakpoints (thread_info *tp,
3447 std::optional<ULONGEST> /* exit_code */,
3448 int /* silent */)
3450 for (breakpoint &b : all_breakpoints_safe ())
3452 if (b.thread == tp->global_num && user_breakpoint_p (&b))
3454 gdb_printf (_("\
3455 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3456 b.number, print_thread_id (tp));
3457 delete_breakpoint (&b);
3462 /* Called when inferior INF has been removed from GDB. Remove associated
3463 per-inferior breakpoints. */
3465 static void
3466 remove_inferior_breakpoints (struct inferior *inf)
3468 for (breakpoint &b : all_breakpoints_safe ())
3470 if (b.inferior == inf->num && user_breakpoint_p (&b))
3472 /* Tell the user the breakpoint has been deleted. But only for
3473 breakpoints that would not normally have been deleted at the
3474 next stop anyway. */
3475 if (b.disposition != disp_del
3476 && b.disposition != disp_del_at_next_stop)
3477 gdb_printf (_("\
3478 Inferior-specific breakpoint %d deleted - inferior %d has been removed.\n"),
3479 b.number, inf->num);
3480 delete_breakpoint (&b);
3485 /* See breakpoint.h. */
3487 void
3488 remove_breakpoints_inf (inferior *inf)
3490 int val;
3492 breakpoint_debug_printf ("inf->num = %d", inf->num);
3494 for (bp_location *bl : all_bp_locations ())
3496 if (bl->pspace != inf->pspace)
3497 continue;
3499 if (bl->inserted && !bl->target_info.persist)
3501 val = remove_breakpoint (bl);
3502 if (val != 0)
3503 return;
3508 static int internal_breakpoint_number = -1;
3510 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3511 If INTERNAL is non-zero, the breakpoint number will be populated
3512 from internal_breakpoint_number and that variable decremented.
3513 Otherwise the breakpoint number will be populated from
3514 breakpoint_count and that value incremented. Internal breakpoints
3515 do not set the internal var bpnum. */
3516 static void
3517 set_breakpoint_number (int internal, struct breakpoint *b)
3519 if (internal)
3520 b->number = internal_breakpoint_number--;
3521 else
3523 set_breakpoint_count (breakpoint_count + 1);
3524 b->number = breakpoint_count;
3528 /* Create a TYPE breakpoint on ADDRESS from an object file with GDBARCH. */
3530 static struct breakpoint *
3531 create_internal_breakpoint (struct gdbarch *gdbarch,
3532 CORE_ADDR address, enum bptype type)
3534 std::unique_ptr<internal_breakpoint> b
3535 (new internal_breakpoint (gdbarch, type, address));
3537 b->number = internal_breakpoint_number--;
3539 return add_to_breakpoint_chain (std::move (b));
3542 /* Create a TYPE breakpoint on minimal symbol MSYM from an object file with
3543 GDBARCH. */
3545 static struct breakpoint *
3546 create_internal_breakpoint (struct gdbarch *gdbarch,
3547 bound_minimal_symbol &msym, enum bptype type)
3549 CORE_ADDR address;
3551 address = msym.value_address ();
3553 address = gdbarch_convert_from_func_ptr_addr
3554 (gdbarch, address, current_inferior ()->top_target ());
3556 /* Note that we're not using gdbarch_addr_bits_remove here, because that's
3557 related to addresses in $pc. We're getting the address from the
3558 minimal symbol table. */
3560 /* Is gdbarch_deprecated_function_start_offset needed here? Or is that dealt
3561 with elsewhere? Needs testing on vax. */
3563 if (gdbarch_skip_entrypoint_p (gdbarch))
3564 address = gdbarch_skip_entrypoint (gdbarch, address);
3566 return create_internal_breakpoint (gdbarch, address, type);
3569 static const char *const longjmp_names[] =
3571 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3573 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3575 /* Per-objfile data private to breakpoint.c. */
3576 struct breakpoint_objfile_data
3578 /* Minimal symbol for "_ovly_debug_event" (if any). */
3579 bound_minimal_symbol overlay_msym;
3581 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3582 bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3584 /* True if we have looked for longjmp probes. */
3585 int longjmp_searched = 0;
3587 /* SystemTap probe points for longjmp (if any). These are non-owning
3588 references. */
3589 std::vector<probe *> longjmp_probes;
3591 /* Minimal symbol for "std::terminate()" (if any). */
3592 bound_minimal_symbol terminate_msym;
3594 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3595 bound_minimal_symbol exception_msym;
3597 /* True if we have looked for exception probes. */
3598 int exception_searched = 0;
3600 /* SystemTap probe points for unwinding (if any). These are non-owning
3601 references. */
3602 std::vector<probe *> exception_probes;
3605 static const registry<objfile>::key<breakpoint_objfile_data>
3606 breakpoint_objfile_key;
3608 /* Minimal symbol not found sentinel. */
3609 static struct minimal_symbol msym_not_found;
3611 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3613 static bool
3614 msym_not_found_p (const struct minimal_symbol *msym)
3616 return msym == &msym_not_found;
3619 /* Return per-objfile data needed by breakpoint.c.
3620 Allocate the data if necessary. */
3622 static struct breakpoint_objfile_data *
3623 get_breakpoint_objfile_data (struct objfile *objfile)
3625 struct breakpoint_objfile_data *bp_objfile_data;
3627 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3628 if (bp_objfile_data == NULL)
3629 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3630 return bp_objfile_data;
3633 static void
3634 create_overlay_event_breakpoint (void)
3636 const char *const func_name = "_ovly_debug_event";
3638 for (objfile *objfile : current_program_space->objfiles ())
3640 struct breakpoint *b;
3641 struct breakpoint_objfile_data *bp_objfile_data;
3642 CORE_ADDR addr;
3644 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3646 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3647 continue;
3649 if (bp_objfile_data->overlay_msym.minsym == NULL)
3651 bound_minimal_symbol m
3652 = lookup_minimal_symbol_text (current_program_space, func_name,
3653 objfile);
3654 if (m.minsym == NULL)
3656 /* Avoid future lookups in this objfile. */
3657 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3658 continue;
3660 bp_objfile_data->overlay_msym = m;
3663 addr = bp_objfile_data->overlay_msym.value_address ();
3664 b = create_internal_breakpoint (objfile->arch (), addr,
3665 bp_overlay_event);
3666 b->locspec = new_explicit_location_spec_function (func_name);
3668 if (overlay_debugging == ovly_auto)
3670 b->enable_state = bp_enabled;
3671 overlay_events_enabled = 1;
3673 else
3675 b->enable_state = bp_disabled;
3676 overlay_events_enabled = 0;
3681 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3682 true if a breakpoint was installed. */
3684 static bool
3685 create_longjmp_master_breakpoint_probe (objfile *objfile)
3687 struct gdbarch *gdbarch = objfile->arch ();
3688 struct breakpoint_objfile_data *bp_objfile_data
3689 = get_breakpoint_objfile_data (objfile);
3691 if (!bp_objfile_data->longjmp_searched)
3693 std::vector<probe *> ret
3694 = find_probes_in_objfile (objfile, "libc", "longjmp");
3696 if (!ret.empty ())
3698 /* We are only interested in checking one element. */
3699 probe *p = ret[0];
3701 if (!p->can_evaluate_arguments ())
3703 /* We cannot use the probe interface here,
3704 because it does not know how to evaluate
3705 arguments. */
3706 ret.clear ();
3709 bp_objfile_data->longjmp_probes = ret;
3710 bp_objfile_data->longjmp_searched = 1;
3713 if (bp_objfile_data->longjmp_probes.empty ())
3714 return false;
3716 for (probe *p : bp_objfile_data->longjmp_probes)
3718 struct breakpoint *b;
3720 b = create_internal_breakpoint (gdbarch,
3721 p->get_relocated_address (objfile),
3722 bp_longjmp_master);
3723 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
3724 b->enable_state = bp_disabled;
3727 return true;
3730 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3731 Return true if at least one breakpoint was installed. */
3733 static bool
3734 create_longjmp_master_breakpoint_names (objfile *objfile)
3736 struct gdbarch *gdbarch = objfile->arch ();
3737 if (!gdbarch_get_longjmp_target_p (gdbarch))
3738 return false;
3740 struct breakpoint_objfile_data *bp_objfile_data
3741 = get_breakpoint_objfile_data (objfile);
3742 unsigned int installed_bp = 0;
3744 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3746 struct breakpoint *b;
3747 const char *func_name;
3748 CORE_ADDR addr;
3750 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3751 continue;
3753 func_name = longjmp_names[i];
3754 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3756 bound_minimal_symbol m
3757 = lookup_minimal_symbol_text (objfile->pspace (), func_name,
3758 objfile);
3759 if (m.minsym == NULL)
3761 /* Prevent future lookups in this objfile. */
3762 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3763 continue;
3765 bp_objfile_data->longjmp_msym[i] = m;
3768 addr = bp_objfile_data->longjmp_msym[i].value_address ();
3769 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
3770 b->locspec = new_explicit_location_spec_function (func_name);
3771 b->enable_state = bp_disabled;
3772 installed_bp++;
3775 return installed_bp > 0;
3778 /* Create a master longjmp breakpoint. */
3780 static void
3781 create_longjmp_master_breakpoint (void)
3783 scoped_restore_current_program_space restore_pspace;
3785 for (struct program_space *pspace : program_spaces)
3787 set_current_program_space (pspace);
3789 for (objfile *obj : pspace->objfiles ())
3791 /* Skip separate debug object, it's handled in the loop below. */
3792 if (obj->separate_debug_objfile_backlink != nullptr)
3793 continue;
3795 /* Try a probe kind breakpoint on main objfile. */
3796 if (create_longjmp_master_breakpoint_probe (obj))
3797 continue;
3799 /* Try longjmp_names kind breakpoints on main and separate_debug
3800 objfiles. */
3801 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3802 if (create_longjmp_master_breakpoint_names (debug_objfile))
3803 break;
3808 /* Create a master std::terminate breakpoint. */
3809 static void
3810 create_std_terminate_master_breakpoint (void)
3812 const char *const func_name = "std::terminate()";
3814 scoped_restore_current_program_space restore_pspace;
3815 scoped_restore_current_language save_language (language_cplus);
3817 for (struct program_space *pspace : program_spaces)
3819 set_current_program_space (pspace);
3821 for (objfile *objfile : pspace->objfiles ())
3823 struct breakpoint *b;
3824 struct breakpoint_objfile_data *bp_objfile_data;
3826 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3828 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3829 continue;
3831 if (bp_objfile_data->terminate_msym.minsym == NULL)
3833 bound_minimal_symbol m
3834 = lookup_minimal_symbol (current_program_space, func_name,
3835 objfile);
3836 if (m.minsym == NULL || (m.minsym->type () != mst_text
3837 && m.minsym->type () != mst_file_text))
3839 /* Prevent future lookups in this objfile. */
3840 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3841 continue;
3843 bp_objfile_data->terminate_msym = m;
3846 b = create_internal_breakpoint (objfile->arch (),
3847 bp_objfile_data->terminate_msym,
3848 bp_std_terminate_master);
3849 b->locspec = new_explicit_location_spec_function (func_name);
3850 b->enable_state = bp_disabled;
3855 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3856 probe. Return true if a breakpoint was installed. */
3858 static bool
3859 create_exception_master_breakpoint_probe (objfile *objfile)
3861 struct breakpoint *b;
3862 struct gdbarch *gdbarch;
3863 struct breakpoint_objfile_data *bp_objfile_data;
3865 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3867 /* We prefer the SystemTap probe point if it exists. */
3868 if (!bp_objfile_data->exception_searched)
3870 std::vector<probe *> ret
3871 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3873 if (!ret.empty ())
3875 /* We are only interested in checking one element. */
3876 probe *p = ret[0];
3878 if (!p->can_evaluate_arguments ())
3880 /* We cannot use the probe interface here, because it does
3881 not know how to evaluate arguments. */
3882 ret.clear ();
3885 bp_objfile_data->exception_probes = ret;
3886 bp_objfile_data->exception_searched = 1;
3889 if (bp_objfile_data->exception_probes.empty ())
3890 return false;
3892 gdbarch = objfile->arch ();
3894 for (probe *p : bp_objfile_data->exception_probes)
3896 b = create_internal_breakpoint (gdbarch,
3897 p->get_relocated_address (objfile),
3898 bp_exception_master);
3899 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
3900 b->enable_state = bp_disabled;
3903 return true;
3906 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3907 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3909 static bool
3910 create_exception_master_breakpoint_hook (objfile *objfile)
3912 const char *const func_name = "_Unwind_DebugHook";
3913 struct breakpoint *b;
3914 struct gdbarch *gdbarch;
3915 struct breakpoint_objfile_data *bp_objfile_data;
3917 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3919 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3920 return false;
3922 gdbarch = objfile->arch ();
3924 if (bp_objfile_data->exception_msym.minsym == NULL)
3926 bound_minimal_symbol debug_hook
3927 = lookup_minimal_symbol_text (objfile->pspace (), func_name, objfile);
3928 if (debug_hook.minsym == NULL)
3930 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3931 return false;
3934 bp_objfile_data->exception_msym = debug_hook;
3937 b = create_internal_breakpoint (gdbarch, bp_objfile_data->exception_msym,
3938 bp_exception_master);
3939 b->locspec = new_explicit_location_spec_function (func_name);
3940 b->enable_state = bp_disabled;
3942 return true;
3945 /* Install a master breakpoint on the unwinder's debug hook. */
3947 static void
3948 create_exception_master_breakpoint (void)
3950 for (objfile *obj : current_program_space->objfiles ())
3952 /* Skip separate debug object. */
3953 if (obj->separate_debug_objfile_backlink)
3954 continue;
3956 /* Try a probe kind breakpoint. */
3957 if (create_exception_master_breakpoint_probe (obj))
3958 continue;
3960 /* Iterate over main and separate debug objects and try an
3961 _Unwind_DebugHook kind breakpoint. */
3962 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3963 if (create_exception_master_breakpoint_hook (debug_objfile))
3964 break;
3968 /* Does B have a location spec? */
3970 static bool
3971 breakpoint_location_spec_empty_p (const struct breakpoint *b)
3973 return (b->locspec != nullptr && b->locspec->empty_p ());
3976 void
3977 update_breakpoints_after_exec (void)
3979 /* We're about to delete breakpoints from GDB's lists. If the
3980 INSERTED flag is true, GDB will try to lift the breakpoints by
3981 writing the breakpoints' "shadow contents" back into memory. The
3982 "shadow contents" are NOT valid after an exec, so GDB should not
3983 do that. Instead, the target is responsible from marking
3984 breakpoints out as soon as it detects an exec. We don't do that
3985 here instead, because there may be other attempts to delete
3986 breakpoints after detecting an exec and before reaching here. */
3987 for (bp_location *bploc : all_bp_locations ())
3988 if (bploc->pspace == current_program_space)
3989 gdb_assert (!bploc->inserted);
3991 for (breakpoint &b : all_breakpoints_safe ())
3993 if (b.pspace != current_program_space)
3994 continue;
3996 /* Solib breakpoints must be explicitly reset after an exec(). */
3997 if (b.type == bp_shlib_event)
3999 delete_breakpoint (&b);
4000 continue;
4003 /* JIT breakpoints must be explicitly reset after an exec(). */
4004 if (b.type == bp_jit_event)
4006 delete_breakpoint (&b);
4007 continue;
4010 /* Thread event breakpoints must be set anew after an exec(),
4011 as must overlay event and longjmp master breakpoints. */
4012 if (b.type == bp_thread_event || b.type == bp_overlay_event
4013 || b.type == bp_longjmp_master || b.type == bp_std_terminate_master
4014 || b.type == bp_exception_master)
4016 delete_breakpoint (&b);
4017 continue;
4020 /* Step-resume breakpoints are meaningless after an exec(). */
4021 if (b.type == bp_step_resume || b.type == bp_hp_step_resume)
4023 delete_breakpoint (&b);
4024 continue;
4027 /* Just like single-step breakpoints. */
4028 if (b.type == bp_single_step)
4030 delete_breakpoint (&b);
4031 continue;
4034 /* Longjmp and longjmp-resume breakpoints are also meaningless
4035 after an exec. */
4036 if (b.type == bp_longjmp || b.type == bp_longjmp_resume
4037 || b.type == bp_longjmp_call_dummy
4038 || b.type == bp_exception || b.type == bp_exception_resume)
4040 delete_breakpoint (&b);
4041 continue;
4044 if (b.type == bp_catchpoint)
4046 /* For now, none of the bp_catchpoint breakpoints need to
4047 do anything at this point. In the future, if some of
4048 the catchpoints need to something, we will need to add
4049 a new method, and call this method from here. */
4050 continue;
4053 /* bp_finish is a special case. The only way we ought to be able
4054 to see one of these when an exec() has happened, is if the user
4055 caught a vfork, and then said "finish". Ordinarily a finish just
4056 carries them to the call-site of the current callee, by setting
4057 a temporary bp there and resuming. But in this case, the finish
4058 will carry them entirely through the vfork & exec.
4060 We don't want to allow a bp_finish to remain inserted now. But
4061 we can't safely delete it, 'cause finish_command has a handle to
4062 the bp on a bpstat, and will later want to delete it. There's a
4063 chance (and I've seen it happen) that if we delete the bp_finish
4064 here, that its storage will get reused by the time finish_command
4065 gets 'round to deleting the "use to be a bp_finish" breakpoint.
4066 We really must allow finish_command to delete a bp_finish.
4068 In the absence of a general solution for the "how do we know
4069 it's safe to delete something others may have handles to?"
4070 problem, what we'll do here is just uninsert the bp_finish, and
4071 let finish_command delete it.
4073 (We know the bp_finish is "doomed" in the sense that it's
4074 momentary, and will be deleted as soon as finish_command sees
4075 the inferior stopped. So it doesn't matter that the bp's
4076 address is probably bogus in the new a.out, unlike e.g., the
4077 solib breakpoints.) */
4079 if (b.type == bp_finish)
4081 continue;
4084 /* Without a symbolic address, we have little hope of the
4085 pre-exec() address meaning the same thing in the post-exec()
4086 a.out. */
4087 if (breakpoint_location_spec_empty_p (&b))
4089 delete_breakpoint (&b);
4090 continue;
4096 detach_breakpoints (ptid_t ptid)
4098 int val = 0;
4099 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
4100 struct inferior *inf = current_inferior ();
4102 if (ptid.pid () == inferior_ptid.pid ())
4103 error (_("Cannot detach breakpoints of inferior_ptid"));
4105 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
4106 inferior_ptid = ptid;
4107 for (bp_location *bl : all_bp_locations ())
4109 if (bl->pspace != inf->pspace)
4110 continue;
4112 /* This function must physically remove breakpoints locations
4113 from the specified ptid, without modifying the breakpoint
4114 package's state. Locations of type bp_loc_other and
4115 bp_loc_software_watchpoint are only maintained at GDB side,
4116 so there is no need to remove them. Moreover, removing these
4117 would modify the breakpoint package's state. */
4118 if (bl->loc_type == bp_loc_other
4119 || bl->loc_type == bp_loc_software_watchpoint)
4120 continue;
4122 if (bl->inserted)
4123 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
4126 return val;
4129 /* Remove the breakpoint location BL from the current address space.
4130 Note that this is used to detach breakpoints from a child fork.
4131 When we get here, the child isn't in the inferior list, and neither
4132 do we have objects to represent its address space --- we should
4133 *not* look at bl->pspace->aspace here. */
4135 static int
4136 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
4138 breakpoint_debug_printf ("%s due to %s",
4139 breakpoint_location_address_str (bl).c_str (),
4140 remove_bp_reason_str (reason));
4142 int val;
4144 /* BL is never in moribund_locations by our callers. */
4145 gdb_assert (bl->owner != NULL);
4147 /* The type of none suggests that owner is actually deleted.
4148 This should not ever happen. */
4149 gdb_assert (bl->owner->type != bp_none);
4151 if (bl->loc_type == bp_loc_software_breakpoint
4152 || bl->loc_type == bp_loc_hardware_breakpoint)
4154 /* "Normal" instruction breakpoint: either the standard
4155 trap-instruction bp (bp_breakpoint), or a
4156 bp_hardware_breakpoint. */
4158 /* First check to see if we have to handle an overlay. */
4159 if (overlay_debugging == ovly_off
4160 || bl->section == NULL
4161 || !(section_is_overlay (bl->section)))
4163 /* No overlay handling: just remove the breakpoint. */
4165 /* If we're trying to uninsert a memory breakpoint that we
4166 know is set in a dynamic object that is marked
4167 shlib_disabled, then either the dynamic object was
4168 removed with "remove-symbol-file" or with
4169 "nosharedlibrary". In the former case, we don't know
4170 whether another dynamic object might have loaded over the
4171 breakpoint's address -- the user might well let us know
4172 about it next with add-symbol-file (the whole point of
4173 add-symbol-file is letting the user manually maintain a
4174 list of dynamically loaded objects). If we have the
4175 breakpoint's shadow memory, that is, this is a software
4176 breakpoint managed by GDB, check whether the breakpoint
4177 is still inserted in memory, to avoid overwriting wrong
4178 code with stale saved shadow contents. Note that HW
4179 breakpoints don't have shadow memory, as they're
4180 implemented using a mechanism that is not dependent on
4181 being able to modify the target's memory, and as such
4182 they should always be removed. */
4183 if (bl->shlib_disabled
4184 && bl->target_info.shadow_len != 0
4185 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
4186 val = 0;
4187 else
4188 val = bl->owner->remove_location (bl, reason);
4190 else
4192 /* This breakpoint is in an overlay section.
4193 Did we set a breakpoint at the LMA? */
4194 if (!overlay_events_enabled)
4196 /* Yes -- overlay event support is not active, so we
4197 should have set a breakpoint at the LMA. Remove it.
4199 /* Ignore any failures: if the LMA is in ROM, we will
4200 have already warned when we failed to insert it. */
4201 if (bl->loc_type == bp_loc_hardware_breakpoint)
4202 target_remove_hw_breakpoint (bl->gdbarch,
4203 &bl->overlay_target_info);
4204 else
4205 target_remove_breakpoint (bl->gdbarch,
4206 &bl->overlay_target_info,
4207 reason);
4209 /* Did we set a breakpoint at the VMA?
4210 If so, we will have marked the breakpoint 'inserted'. */
4211 if (bl->inserted)
4213 /* Yes -- remove it. Previously we did not bother to
4214 remove the breakpoint if the section had been
4215 unmapped, but let's not rely on that being safe. We
4216 don't know what the overlay manager might do. */
4218 /* However, we should remove *software* breakpoints only
4219 if the section is still mapped, or else we overwrite
4220 wrong code with the saved shadow contents. */
4221 if (bl->loc_type == bp_loc_hardware_breakpoint
4222 || section_is_mapped (bl->section))
4223 val = bl->owner->remove_location (bl, reason);
4224 else
4225 val = 0;
4227 else
4229 /* No -- not inserted, so no need to remove. No error. */
4230 val = 0;
4234 /* In some cases, we might not be able to remove a breakpoint in
4235 a shared library that has already been removed, but we have
4236 not yet processed the shlib unload event. Similarly for an
4237 unloaded add-symbol-file object - the user might not yet have
4238 had the chance to remove-symbol-file it. shlib_disabled will
4239 be set if the library/object has already been removed, but
4240 the breakpoint hasn't been uninserted yet, e.g., after
4241 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4242 always-inserted mode. */
4243 if (val
4244 && (bl->loc_type == bp_loc_software_breakpoint
4245 && (bl->shlib_disabled
4246 || solib_name_from_address (bl->pspace, bl->address)
4247 || shared_objfile_contains_address_p (bl->pspace,
4248 bl->address))))
4249 val = 0;
4251 if (val)
4252 return val;
4253 bl->inserted = (reason == DETACH_BREAKPOINT);
4255 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4257 bl->inserted = (reason == DETACH_BREAKPOINT);
4258 bl->owner->remove_location (bl, reason);
4260 /* Failure to remove any of the hardware watchpoints comes here. */
4261 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4262 warning (_("Could not remove hardware watchpoint %d."),
4263 bl->owner->number);
4265 else if (bl->owner->type == bp_catchpoint
4266 && breakpoint_enabled (bl->owner)
4267 && !bl->duplicate)
4269 val = bl->owner->remove_location (bl, reason);
4270 if (val)
4271 return val;
4273 bl->inserted = (reason == DETACH_BREAKPOINT);
4276 return 0;
4279 static int
4280 remove_breakpoint (struct bp_location *bl)
4282 /* BL is never in moribund_locations by our callers. */
4283 gdb_assert (bl->owner != NULL);
4285 /* The type of none suggests that owner is actually deleted.
4286 This should not ever happen. */
4287 gdb_assert (bl->owner->type != bp_none);
4289 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4291 switch_to_program_space_and_thread (bl->pspace);
4293 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4296 /* See breakpoint.h. */
4298 void
4299 mark_breakpoints_out (program_space *pspace)
4301 for (bp_location *bl : all_bp_locations ())
4302 if (bl->pspace == pspace)
4303 bl->inserted = 0;
4306 /* See breakpoint.h. */
4308 void
4309 breakpoint_init_inferior (inferior *inf, inf_context context)
4311 /* If breakpoint locations are shared across processes, then there's
4312 nothing to do. */
4313 if (gdbarch_has_global_breakpoints (inf->arch ()))
4314 return;
4316 mark_breakpoints_out (inf->pspace);
4318 for (breakpoint &b : all_breakpoints_safe ())
4320 if (b.has_locations () && b.first_loc ().pspace != inf->pspace)
4321 continue;
4323 switch (b.type)
4325 case bp_call_dummy:
4326 case bp_longjmp_call_dummy:
4328 /* If the call dummy breakpoint is at the entry point it will
4329 cause problems when the inferior is rerun, so we better get
4330 rid of it. */
4332 case bp_watchpoint_scope:
4334 /* Also get rid of scope breakpoints. */
4336 case bp_shlib_event:
4338 /* Also remove solib event breakpoints. Their addresses may
4339 have changed since the last time we ran the program.
4340 Actually we may now be debugging against different target;
4341 and so the solib backend that installed this breakpoint may
4342 not be used in by the target. E.g.,
4344 (gdb) file prog-linux
4345 (gdb) run # native linux target
4347 (gdb) kill
4348 (gdb) file prog-win.exe
4349 (gdb) tar rem :9999 # remote Windows gdbserver.
4352 case bp_step_resume:
4354 /* Also remove step-resume breakpoints. */
4356 case bp_single_step:
4358 /* Also remove single-step breakpoints. */
4360 delete_breakpoint (&b);
4361 break;
4363 case bp_watchpoint:
4364 case bp_hardware_watchpoint:
4365 case bp_read_watchpoint:
4366 case bp_access_watchpoint:
4368 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
4370 /* Likewise for watchpoints on local expressions. */
4371 if (w.exp_valid_block != NULL)
4372 delete_breakpoint (&b);
4373 else
4375 /* Get rid of existing locations, which are no longer
4376 valid. New ones will be created in
4377 update_watchpoint, when the inferior is restarted.
4378 The next update_global_location_list call will
4379 garbage collect them. */
4380 b.clear_locations ();
4382 if (context == inf_starting)
4384 /* Reset val field to force reread of starting value in
4385 insert_breakpoints. */
4386 w.val.reset (nullptr);
4387 w.val_valid = false;
4391 break;
4392 default:
4393 break;
4397 /* Get rid of the moribund locations. */
4398 for (bp_location *bl : moribund_locations)
4399 decref_bp_location (&bl);
4400 moribund_locations.clear ();
4403 /* These functions concern about actual breakpoints inserted in the
4404 target --- to e.g. check if we need to do decr_pc adjustment or if
4405 we need to hop over the bkpt --- so we check for address space
4406 match, not program space. */
4408 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4409 exists at PC. It returns ordinary_breakpoint_here if it's an
4410 ordinary breakpoint, or permanent_breakpoint_here if it's a
4411 permanent breakpoint.
4412 - When continuing from a location with an ordinary breakpoint, we
4413 actually single step once before calling insert_breakpoints.
4414 - When continuing from a location with a permanent breakpoint, we
4415 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4416 the target, to advance the PC past the breakpoint. */
4418 enum breakpoint_here
4419 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4421 bool any_breakpoint_here = false;
4423 for (bp_location *bl : all_bp_locations ())
4425 if (bl->loc_type != bp_loc_software_breakpoint
4426 && bl->loc_type != bp_loc_hardware_breakpoint)
4427 continue;
4429 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4430 if ((breakpoint_enabled (bl->owner)
4431 || bl->permanent)
4432 && breakpoint_location_address_match (bl, aspace, pc))
4434 if (overlay_debugging
4435 && section_is_overlay (bl->section)
4436 && !section_is_mapped (bl->section))
4437 continue; /* unmapped overlay -- can't be a match */
4438 else if (bl->permanent)
4439 return permanent_breakpoint_here;
4440 else
4441 any_breakpoint_here = true;
4445 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4448 /* See breakpoint.h. */
4451 breakpoint_in_range_p (const address_space *aspace,
4452 CORE_ADDR addr, ULONGEST len)
4454 for (bp_location *bl : all_bp_locations ())
4456 if (bl->loc_type != bp_loc_software_breakpoint
4457 && bl->loc_type != bp_loc_hardware_breakpoint)
4458 continue;
4460 if ((breakpoint_enabled (bl->owner)
4461 || bl->permanent)
4462 && breakpoint_location_address_range_overlap (bl, aspace,
4463 addr, len))
4465 if (overlay_debugging
4466 && section_is_overlay (bl->section)
4467 && !section_is_mapped (bl->section))
4469 /* Unmapped overlay -- can't be a match. */
4470 continue;
4473 return 1;
4477 return 0;
4480 /* Return true if there's a moribund breakpoint at PC. */
4483 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4485 for (bp_location *loc : moribund_locations)
4486 if (breakpoint_location_address_match (loc, aspace, pc))
4487 return 1;
4489 return 0;
4492 /* Returns true iff BL is inserted at PC, in address space ASPACE. */
4494 static bool
4495 bp_location_inserted_here_p (const struct bp_location *bl,
4496 const address_space *aspace, CORE_ADDR pc)
4498 if (bl->inserted
4499 && breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
4500 aspace, pc))
4502 /* An unmapped overlay can't be a match. */
4503 return !(overlay_debugging
4504 && section_is_overlay (bl->section)
4505 && !section_is_mapped (bl->section));
4507 return false;
4510 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4513 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4515 for (bp_location *bl : all_bp_locations_at_addr (pc))
4517 if (bl->loc_type != bp_loc_software_breakpoint
4518 && bl->loc_type != bp_loc_hardware_breakpoint)
4519 continue;
4521 if (bp_location_inserted_here_p (bl, aspace, pc))
4522 return 1;
4524 return 0;
4527 /* This function returns non-zero iff there is a software breakpoint
4528 inserted at PC. */
4531 software_breakpoint_inserted_here_p (const address_space *aspace,
4532 CORE_ADDR pc)
4534 for (bp_location *bl : all_bp_locations_at_addr (pc))
4536 if (bl->loc_type != bp_loc_software_breakpoint)
4537 continue;
4539 if (bp_location_inserted_here_p (bl, aspace, pc))
4540 return 1;
4543 return 0;
4546 /* See breakpoint.h. */
4549 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4550 CORE_ADDR pc)
4552 for (bp_location *bl : all_bp_locations_at_addr (pc))
4554 if (bl->loc_type != bp_loc_hardware_breakpoint)
4555 continue;
4557 if (bp_location_inserted_here_p (bl, aspace, pc))
4558 return 1;
4561 return 0;
4565 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4566 CORE_ADDR addr, ULONGEST len)
4568 for (breakpoint &bpt : all_breakpoints ())
4570 if (bpt.type != bp_hardware_watchpoint
4571 && bpt.type != bp_access_watchpoint)
4572 continue;
4574 if (!breakpoint_enabled (&bpt))
4575 continue;
4577 for (bp_location &loc : bpt.locations ())
4578 if (loc.pspace->aspace.get () == aspace && loc.inserted)
4580 CORE_ADDR l, h;
4582 /* Check for intersection. */
4583 l = std::max<CORE_ADDR> (loc.address, addr);
4584 h = std::min<CORE_ADDR> (loc.address + loc.length, addr + len);
4585 if (l < h)
4586 return 1;
4589 return 0;
4592 /* See breakpoint.h. */
4594 bool
4595 is_catchpoint (struct breakpoint *b)
4597 return (b->type == bp_catchpoint);
4600 /* Clear a bpstat so that it says we are not at any breakpoint.
4601 Also free any storage that is part of a bpstat. */
4603 void
4604 bpstat_clear (bpstat **bsp)
4606 bpstat *p;
4607 bpstat *q;
4609 if (bsp == 0)
4610 return;
4611 p = *bsp;
4612 while (p != NULL)
4614 q = p->next;
4615 delete p;
4616 p = q;
4618 *bsp = NULL;
4621 bpstat::bpstat (const bpstat &other)
4622 : next (NULL),
4623 bp_location_at (other.bp_location_at),
4624 breakpoint_at (other.breakpoint_at),
4625 commands (other.commands),
4626 print (other.print),
4627 stop (other.stop),
4628 print_it (other.print_it)
4630 if (other.old_val != NULL)
4631 old_val = release_value (other.old_val->copy ());
4634 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4635 is part of the bpstat is copied as well. */
4637 bpstat *
4638 bpstat_copy (bpstat *bs)
4640 bpstat *p = nullptr;
4641 bpstat *tmp;
4642 bpstat *retval = nullptr;
4644 if (bs == NULL)
4645 return bs;
4647 for (; bs != NULL; bs = bs->next)
4649 tmp = new bpstat (*bs);
4651 if (p == NULL)
4652 /* This is the first thing in the chain. */
4653 retval = tmp;
4654 else
4655 p->next = tmp;
4656 p = tmp;
4658 p->next = NULL;
4659 return retval;
4662 /* Find the bpstat associated with this breakpoint. */
4664 bpstat *
4665 bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
4667 if (bsp == NULL)
4668 return NULL;
4670 for (; bsp != NULL; bsp = bsp->next)
4672 if (bsp->breakpoint_at == breakpoint)
4673 return bsp;
4675 return NULL;
4678 /* See breakpoint.h. */
4680 bool
4681 bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
4683 for (; bsp != NULL; bsp = bsp->next)
4685 if (bsp->breakpoint_at == NULL)
4687 /* A moribund location can never explain a signal other than
4688 GDB_SIGNAL_TRAP. */
4689 if (sig == GDB_SIGNAL_TRAP)
4690 return true;
4692 else
4694 if (bsp->breakpoint_at->explains_signal (sig))
4695 return true;
4699 return false;
4702 /* See breakpoint.h. */
4705 bpstat_num (bpstat **bsp, int *num)
4707 struct breakpoint *b;
4709 if ((*bsp) == NULL)
4710 return 0; /* No more breakpoint values */
4712 /* We assume we'll never have several bpstats that correspond to a
4713 single breakpoint -- otherwise, this function might return the
4714 same number more than once and this will look ugly. */
4715 b = (*bsp)->breakpoint_at;
4716 *bsp = (*bsp)->next;
4717 if (b == NULL)
4718 return -1; /* breakpoint that's been deleted since */
4720 *num = b->number; /* We have its number */
4721 return 1;
4724 /* See breakpoint.h */
4727 bpstat_locno (const bpstat *bs)
4729 const struct breakpoint *b = bs->breakpoint_at;
4730 const struct bp_location *bl = bs->bp_location_at.get ();
4732 if (b != nullptr && b->has_multiple_locations ())
4734 int locno = 1;
4736 for (bp_location &loc : b->locations ())
4738 if (bl == &loc)
4739 return locno;
4741 ++locno;
4744 warning (_("location number not found for breakpoint %d address %s."),
4745 b->number, paddress (bl->gdbarch, bl->address));
4748 return 0;
4751 /* See breakpoint.h. */
4753 void
4754 print_num_locno (const bpstat *bs, struct ui_out *uiout)
4756 struct breakpoint *b = bs->breakpoint_at;
4758 if (b == nullptr)
4759 uiout->text (_("deleted breakpoint"));
4760 else
4762 uiout->field_signed ("bkptno", b->number);
4764 int locno = bpstat_locno (bs);
4765 if (locno != 0)
4766 uiout->message (".%pF", signed_field ("locno", locno));
4770 /* See breakpoint.h. */
4772 void
4773 bpstat_clear_actions (void)
4775 bpstat *bs;
4777 if (inferior_ptid == null_ptid)
4778 return;
4780 thread_info *tp = inferior_thread ();
4781 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4783 bs->commands = NULL;
4784 bs->old_val.reset (nullptr);
4788 /* Called when a command is about to proceed the inferior. */
4790 static void
4791 breakpoint_about_to_proceed (void)
4793 if (inferior_ptid != null_ptid)
4795 struct thread_info *tp = inferior_thread ();
4797 /* Allow inferior function calls in breakpoint commands to not
4798 interrupt the command list. When the call finishes
4799 successfully, the inferior will be standing at the same
4800 breakpoint as if nothing happened. */
4801 if (tp->control.in_infcall)
4802 return;
4805 breakpoint_proceeded = 1;
4808 /* Return true iff CMD as the first line of a command sequence is `silent'
4809 or its equivalent. */
4811 static bool
4812 command_line_is_silent (struct command_line *cmd)
4814 return cmd && (strcmp ("silent", cmd->line) == 0);
4817 /* Sets the $_hit_bpnum and $_hit_locno to bpnum and locno.
4818 A locno 0 is changed to 1 to e.g. let the user do
4819 (gdb) disable $_hit_bpnum.$_hit_locno
4820 for a single location breakpoint. */
4822 static void
4823 set_hit_convenience_vars (int bpnum, int locno)
4825 set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), bpnum);
4826 set_internalvar_integer (lookup_internalvar ("_hit_locno"),
4827 (locno > 0 ? locno : 1));
4830 /* Execute all the commands associated with all the breakpoints at
4831 this location. Any of these commands could cause the process to
4832 proceed beyond this point, etc. We look out for such changes by
4833 checking the global "breakpoint_proceeded" after each command.
4835 Returns true if a breakpoint command resumed the inferior. In that
4836 case, it is the caller's responsibility to recall it again with the
4837 bpstat of the current thread. */
4839 static bool
4840 bpstat_do_actions_1 (bpstat **bsp)
4842 bpstat *bs;
4843 bool again = false;
4845 /* Avoid endless recursion if a `source' command is contained
4846 in bs->commands. */
4847 if (executing_breakpoint_commands)
4848 return false;
4850 scoped_restore save_executing
4851 = make_scoped_restore (&executing_breakpoint_commands, 1);
4853 scoped_restore preventer = prevent_dont_repeat ();
4855 /* This pointer will iterate over the list of bpstat's. */
4856 bs = *bsp;
4858 /* The $_hit_* convenience variables are set before running the
4859 commands of BS. In case we have several bs, after the loop,
4860 we set again the variables to the first printed bpnum and locno.
4861 For multiple breakpoints, this ensures the variables are set to the
4862 breakpoint printed for the user. */
4863 int printed_hit_bpnum = -1;
4864 int printed_hit_locno = -1;
4866 breakpoint_proceeded = 0;
4867 for (; bs != NULL; bs = bs->next)
4869 struct command_line *cmd = NULL;
4871 /* Set the _hit_* convenience variables before running BS's commands. */
4873 const struct breakpoint *b = bs->breakpoint_at;
4874 if (b != nullptr)
4876 int locno = bpstat_locno (bs);
4878 set_hit_convenience_vars (b->number, locno);
4879 if (printed_hit_locno == -1 && bs->print)
4881 printed_hit_bpnum = b->number;
4882 printed_hit_locno = locno;
4887 /* Take ownership of the BSP's command tree, if it has one.
4889 The command tree could legitimately contain commands like
4890 'step' and 'next', which call clear_proceed_status, which
4891 frees the bpstat BS and its command tree. To make sure this doesn't
4892 free the tree we're executing out from under us, we need to
4893 take ownership of the tree ourselves. Since a given bpstat's
4894 commands are only executed once, we don't need to copy it; we
4895 can clear the pointer in the bpstat, and make sure we free
4896 the tree when we're done. */
4897 counted_command_line ccmd = bs->commands;
4898 bs->commands = NULL;
4899 if (ccmd != NULL)
4900 cmd = ccmd.get ();
4901 if (command_line_is_silent (cmd))
4903 /* The action has been already done by bpstat_stop_status. */
4904 cmd = cmd->next;
4907 while (cmd != NULL)
4909 execute_control_command (cmd);
4910 /* After execute_control_command, if breakpoint_proceeded is true,
4911 BS has been freed and cannot be accessed anymore. */
4913 if (breakpoint_proceeded)
4914 break;
4915 else
4916 cmd = cmd->next;
4919 if (breakpoint_proceeded)
4921 if (current_ui->async)
4922 /* If we are in async mode, then the target might be still
4923 running, not stopped at any breakpoint, so nothing for
4924 us to do here -- just return to the event loop. */
4926 else
4927 /* In sync mode, when execute_control_command returns
4928 we're already standing on the next breakpoint.
4929 Breakpoint commands for that stop were not run, since
4930 execute_command does not run breakpoint commands --
4931 only command_line_handler does, but that one is not
4932 involved in execution of breakpoint commands. So, we
4933 can now execute breakpoint commands. It should be
4934 noted that making execute_command do bpstat actions is
4935 not an option -- in this case we'll have recursive
4936 invocation of bpstat for each breakpoint with a
4937 command, and can easily blow up GDB stack. Instead, we
4938 return true, which will trigger the caller to recall us
4939 with the new stop_bpstat. */
4940 again = true;
4941 break;
4945 /* Now that we have executed the commands of all bs, set the _hit_*
4946 convenience variables to the printed values. */
4947 if (printed_hit_locno != -1)
4948 set_hit_convenience_vars (printed_hit_bpnum, printed_hit_locno);
4950 return again;
4953 /* Helper for bpstat_do_actions. Get the current thread, if there's
4954 one, is alive and has execution. Return NULL otherwise. */
4956 static thread_info *
4957 get_bpstat_thread ()
4959 if (inferior_ptid == null_ptid || !target_has_execution ())
4960 return NULL;
4962 thread_info *tp = inferior_thread ();
4963 if (tp->state == THREAD_EXITED || tp->executing ())
4964 return NULL;
4965 return tp;
4968 void
4969 bpstat_do_actions (void)
4971 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4972 thread_info *tp;
4974 /* Do any commands attached to breakpoint we are stopped at. */
4975 while ((tp = get_bpstat_thread ()) != NULL)
4977 /* Since in sync mode, bpstat_do_actions may resume the
4978 inferior, and only return when it is stopped at the next
4979 breakpoint, we keep doing breakpoint actions until it returns
4980 false to indicate the inferior was not resumed. */
4981 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4982 break;
4985 cleanup_if_error.release ();
4988 /* Print out the (old or new) value associated with a watchpoint. */
4990 static void
4991 watchpoint_value_print (struct value *val, struct ui_file *stream)
4993 if (val == NULL)
4994 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4995 else
4997 struct value_print_options opts;
4998 get_user_print_options (&opts);
4999 value_print (val, stream, &opts);
5003 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
5004 debugging multiple threads. */
5006 void
5007 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
5009 if (uiout->is_mi_like_p ())
5010 return;
5012 uiout->text ("\n");
5014 if (show_thread_that_caused_stop ())
5016 struct thread_info *thr = inferior_thread ();
5018 uiout->text ("Thread ");
5019 uiout->field_string ("thread-id", print_thread_id (thr));
5021 const char *name = thread_name (thr);
5022 if (name != NULL)
5024 uiout->text (" \"");
5025 uiout->field_string ("name", name);
5026 uiout->text ("\"");
5029 uiout->text (" hit ");
5033 /* Generic routine for printing messages indicating why we
5034 stopped. The behavior of this function depends on the value
5035 'print_it' in the bpstat structure. Under some circumstances we
5036 may decide not to print anything here and delegate the task to
5037 normal_stop(). */
5039 static enum print_stop_action
5040 print_bp_stop_message (bpstat *bs)
5042 switch (bs->print_it)
5044 case print_it_noop:
5045 /* Nothing should be printed for this bpstat entry. */
5046 return PRINT_UNKNOWN;
5048 case print_it_done:
5049 /* We still want to print the frame, but we already printed the
5050 relevant messages. */
5051 return PRINT_SRC_AND_LOC;
5053 case print_it_normal:
5055 struct breakpoint *b = bs->breakpoint_at;
5057 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
5058 which has since been deleted. */
5059 if (b == NULL)
5060 return PRINT_UNKNOWN;
5062 /* Normal case. Call the breakpoint's print_it method. */
5063 return b->print_it (bs);
5066 default:
5067 internal_error (_("print_bp_stop_message: unrecognized enum value"));
5071 /* See breakpoint.h. */
5073 void
5074 print_solib_event (bool is_catchpoint)
5076 bool any_deleted = !current_program_space->deleted_solibs.empty ();
5077 bool any_added = !current_program_space->added_solibs.empty ();
5079 if (!is_catchpoint)
5081 if (any_added || any_deleted)
5082 current_uiout->text (_("Stopped due to shared library event:\n"));
5083 else
5084 current_uiout->text (_("Stopped due to shared library event (no "
5085 "libraries added or removed)\n"));
5088 if (current_uiout->is_mi_like_p ())
5089 current_uiout->field_string ("reason",
5090 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
5092 if (any_deleted)
5094 current_uiout->text (_(" Inferior unloaded "));
5095 ui_out_emit_list list_emitter (current_uiout, "removed");
5096 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
5098 const std::string &name = current_program_space->deleted_solibs[ix];
5100 if (ix > 0)
5101 current_uiout->text (" ");
5102 current_uiout->field_string ("library", name);
5103 current_uiout->text ("\n");
5107 if (any_added)
5109 current_uiout->text (_(" Inferior loaded "));
5110 ui_out_emit_list list_emitter (current_uiout, "added");
5111 bool first = true;
5112 for (solib *iter : current_program_space->added_solibs)
5114 if (!first)
5115 current_uiout->text (" ");
5116 first = false;
5117 current_uiout->field_string ("library", iter->so_name);
5118 current_uiout->text ("\n");
5123 /* Print a message indicating what happened. This is called from
5124 normal_stop(). The input to this routine is the head of the bpstat
5125 list - a list of the eventpoints that caused this stop. KIND is
5126 the target_waitkind for the stopping event. This
5127 routine calls the generic print routine for printing a message
5128 about reasons for stopping. This will print (for example) the
5129 "Breakpoint n," part of the output. The return value of this
5130 routine is one of:
5132 PRINT_UNKNOWN: Means we printed nothing.
5133 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
5134 code to print the location. An example is
5135 "Breakpoint 1, " which should be followed by
5136 the location.
5137 PRINT_SRC_ONLY: Means we printed something, but there is no need
5138 to also print the location part of the message.
5139 An example is the catch/throw messages, which
5140 don't require a location appended to the end.
5141 PRINT_NOTHING: We have done some printing and we don't need any
5142 further info to be printed. */
5144 enum print_stop_action
5145 bpstat_print (bpstat *bs, target_waitkind kind)
5147 enum print_stop_action val;
5149 /* Maybe another breakpoint in the chain caused us to stop.
5150 (Currently all watchpoints go on the bpstat whether hit or not.
5151 That probably could (should) be changed, provided care is taken
5152 with respect to bpstat_explains_signal). */
5153 for (; bs; bs = bs->next)
5155 val = print_bp_stop_message (bs);
5156 if (val == PRINT_SRC_ONLY
5157 || val == PRINT_SRC_AND_LOC
5158 || val == PRINT_NOTHING)
5159 return val;
5162 /* If we had hit a shared library event breakpoint,
5163 print_bp_stop_message would print out this message. If we hit an
5164 OS-level shared library event, do the same thing. */
5165 if (kind == TARGET_WAITKIND_LOADED)
5167 print_solib_event (false);
5168 return PRINT_NOTHING;
5171 /* We reached the end of the chain, or we got a null BS to start
5172 with and nothing was printed. */
5173 return PRINT_UNKNOWN;
5176 /* Evaluate the boolean expression EXP and return the result. */
5178 static bool
5179 breakpoint_cond_eval (expression *exp)
5181 scoped_value_mark mark;
5182 return value_true (exp->evaluate ());
5185 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
5187 bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
5188 : next (NULL),
5189 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
5190 breakpoint_at (bl->owner),
5191 commands (NULL),
5192 print (0),
5193 stop (0),
5194 print_it (print_it_normal)
5196 **bs_link_pointer = this;
5197 *bs_link_pointer = &next;
5200 bpstat::bpstat ()
5201 : next (NULL),
5202 breakpoint_at (NULL),
5203 commands (NULL),
5204 print (0),
5205 stop (0),
5206 print_it (print_it_normal)
5210 /* The target has stopped with waitstatus WS. Check if any hardware
5211 watchpoints have triggered, according to the target. */
5214 watchpoints_triggered (const target_waitstatus &ws)
5216 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
5217 CORE_ADDR addr;
5219 if (!stopped_by_watchpoint)
5221 /* We were not stopped by a watchpoint. Mark all watchpoints
5222 as not triggered. */
5223 for (breakpoint &b : all_breakpoints ())
5224 if (is_hardware_watchpoint (&b))
5226 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
5228 w.watchpoint_triggered = watch_triggered_no;
5231 return 0;
5234 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
5236 /* We were stopped by a watchpoint, but we don't know where.
5237 Mark all watchpoints as unknown. */
5238 for (breakpoint &b : all_breakpoints ())
5239 if (is_hardware_watchpoint (&b))
5241 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
5243 w.watchpoint_triggered = watch_triggered_unknown;
5246 return 1;
5249 /* The target could report the data address. Mark watchpoints
5250 affected by this data address as triggered, and all others as not
5251 triggered. */
5253 for (breakpoint &b : all_breakpoints ())
5254 if (is_hardware_watchpoint (&b))
5256 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
5258 w.watchpoint_triggered = watch_triggered_no;
5259 for (bp_location &loc : b.locations ())
5261 if (is_masked_watchpoint (&b))
5263 CORE_ADDR newaddr = addr & w.hw_wp_mask;
5264 CORE_ADDR start = loc.address & w.hw_wp_mask;
5266 if (newaddr == start)
5268 w.watchpoint_triggered = watch_triggered_yes;
5269 break;
5272 /* Exact match not required. Within range is sufficient. */
5273 else if (target_watchpoint_addr_within_range
5274 (current_inferior ()->top_target (), addr, loc.address,
5275 loc.length))
5277 w.watchpoint_triggered = watch_triggered_yes;
5278 break;
5283 return 1;
5286 /* Possible return values for watchpoint_check. */
5287 enum wp_check_result
5289 /* The watchpoint has been deleted. */
5290 WP_DELETED = 1,
5292 /* The value has changed. */
5293 WP_VALUE_CHANGED = 2,
5295 /* The value has not changed. */
5296 WP_VALUE_NOT_CHANGED = 3,
5298 /* Ignore this watchpoint, no matter if the value changed or not. */
5299 WP_IGNORE = 4,
5302 #define BP_TEMPFLAG 1
5303 #define BP_HARDWAREFLAG 2
5305 /* Evaluate watchpoint condition expression and check if its value
5306 changed. */
5308 static wp_check_result
5309 watchpoint_check (bpstat *bs)
5311 frame_info_ptr fr;
5312 bool within_current_scope;
5314 /* BS is built from an existing struct breakpoint. */
5315 gdb_assert (bs->breakpoint_at != NULL);
5316 watchpoint *b = gdb::checked_static_cast<watchpoint *> (bs->breakpoint_at);
5318 /* If this is a local watchpoint, we only want to check if the
5319 watchpoint frame is in scope if the current thread is the thread
5320 that was used to create the watchpoint. */
5321 if (!watchpoint_in_thread_scope (b))
5322 return WP_IGNORE;
5324 if (b->exp_valid_block == NULL)
5325 within_current_scope = true;
5326 else
5328 frame_info_ptr frame = get_current_frame ();
5329 struct gdbarch *frame_arch = get_frame_arch (frame);
5330 CORE_ADDR frame_pc = get_frame_pc (frame);
5332 /* stack_frame_destroyed_p() returns a non-zero value if we're
5333 still in the function but the stack frame has already been
5334 invalidated. Since we can't rely on the values of local
5335 variables after the stack has been destroyed, we are treating
5336 the watchpoint in that state as `not changed' without further
5337 checking. Don't mark watchpoints as changed if the current
5338 frame is in an epilogue - even if they are in some other
5339 frame, our view of the stack is likely to be wrong and
5340 frame_find_by_id could error out. */
5341 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5342 return WP_IGNORE;
5344 fr = frame_find_by_id (b->watchpoint_frame);
5345 within_current_scope = (fr != NULL);
5347 /* If we've gotten confused in the unwinder, we might have
5348 returned a frame that can't describe this variable. */
5349 if (within_current_scope)
5351 struct symbol *function;
5353 function = get_frame_function (fr);
5354 if (function == NULL
5355 || !function->value_block ()->contains (b->exp_valid_block))
5356 within_current_scope = false;
5359 if (within_current_scope)
5360 /* If we end up stopping, the current frame will get selected
5361 in normal_stop. So this call to select_frame won't affect
5362 the user. */
5363 select_frame (fr);
5366 if (within_current_scope)
5368 /* We use value_{,free_to_}mark because it could be a *long*
5369 time before we return to the command level and call
5370 free_all_values. We can't call free_all_values because we
5371 might be in the middle of evaluating a function call. */
5373 struct value *mark;
5374 struct value *new_val;
5376 if (is_masked_watchpoint (b))
5377 /* Since we don't know the exact trigger address (from
5378 stopped_data_address), just tell the user we've triggered
5379 a mask watchpoint. */
5380 return WP_VALUE_CHANGED;
5382 mark = value_mark ();
5383 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5384 NULL, NULL, false);
5386 if (b->val_bitsize != 0)
5387 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5389 /* We use value_equal_contents instead of value_equal because
5390 the latter coerces an array to a pointer, thus comparing just
5391 the address of the array instead of its contents. This is
5392 not what we want. */
5393 if ((b->val != NULL) != (new_val != NULL)
5394 || (b->val != NULL && !value_equal_contents (b->val.get (),
5395 new_val)))
5397 bs->old_val = b->val;
5398 b->val = release_value (new_val);
5399 b->val_valid = true;
5400 if (new_val != NULL)
5401 value_free_to_mark (mark);
5402 return WP_VALUE_CHANGED;
5404 else
5406 /* Nothing changed. */
5407 value_free_to_mark (mark);
5408 return WP_VALUE_NOT_CHANGED;
5411 else
5413 /* This seems like the only logical thing to do because
5414 if we temporarily ignored the watchpoint, then when
5415 we reenter the block in which it is valid it contains
5416 garbage (in the case of a function, it may have two
5417 garbage values, one before and one after the prologue).
5418 So we can't even detect the first assignment to it and
5419 watch after that (since the garbage may or may not equal
5420 the first value assigned). */
5421 /* We print all the stop information in
5422 breakpointprint_it, but in this case, by the time we
5423 call breakpoint->print_it this bp will be deleted
5424 already. So we have no choice but print the information
5425 here. */
5427 SWITCH_THRU_ALL_UIS ()
5429 struct ui_out *uiout = current_uiout;
5431 if (uiout->is_mi_like_p ())
5432 uiout->field_string
5433 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5434 uiout->message ("\nWatchpoint %pF deleted because the program has "
5435 "left the block in\n"
5436 "which its expression is valid.\n",
5437 signed_field ("wpnum", b->number));
5440 /* Make sure the watchpoint's commands aren't executed. */
5441 b->commands = NULL;
5442 watchpoint_del_at_next_stop (b);
5444 return WP_DELETED;
5448 /* Return true if it looks like target has stopped due to hitting
5449 breakpoint location BL. This function does not check if we should
5450 stop, only if BL explains the stop. */
5452 static bool
5453 bpstat_check_location (const struct bp_location *bl,
5454 const address_space *aspace, CORE_ADDR bp_addr,
5455 const target_waitstatus &ws)
5457 struct breakpoint *b = bl->owner;
5459 /* BL is from an existing breakpoint. */
5460 gdb_assert (b != NULL);
5462 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
5465 /* Determine if the watched values have actually changed, and we
5466 should stop. If not, set BS->stop to false. */
5468 static void
5469 bpstat_check_watchpoint (bpstat *bs)
5471 const struct bp_location *bl;
5473 /* BS is built for existing struct breakpoint. */
5474 bl = bs->bp_location_at.get ();
5475 gdb_assert (bl != NULL);
5476 watchpoint *b = gdb::checked_static_cast<watchpoint *> (bs->breakpoint_at);
5478 bool must_check_value = false;
5480 if (b->type == bp_watchpoint)
5481 /* For a software watchpoint, we must always check the
5482 watched value. */
5483 must_check_value = true;
5484 else if (b->watchpoint_triggered == watch_triggered_yes)
5485 /* We have a hardware watchpoint (read, write, or access)
5486 and the target earlier reported an address watched by
5487 this watchpoint. */
5488 must_check_value = true;
5489 else if (b->watchpoint_triggered == watch_triggered_unknown
5490 && b->type == bp_hardware_watchpoint)
5491 /* We were stopped by a hardware watchpoint, but the target could
5492 not report the data address. We must check the watchpoint's
5493 value. Access and read watchpoints are out of luck; without
5494 a data address, we can't figure it out. */
5495 must_check_value = true;
5497 if (must_check_value)
5499 wp_check_result e;
5503 e = watchpoint_check (bs);
5505 catch (const gdb_exception_error &ex)
5507 exception_fprintf (gdb_stderr, ex,
5508 "Error evaluating expression "
5509 "for watchpoint %d\n",
5510 b->number);
5512 SWITCH_THRU_ALL_UIS ()
5514 gdb_printf (_("Watchpoint %d deleted.\n"),
5515 b->number);
5517 watchpoint_del_at_next_stop (b);
5518 e = WP_DELETED;
5521 switch (e)
5523 case WP_DELETED:
5524 /* We've already printed what needs to be printed. */
5525 bs->print_it = print_it_done;
5526 /* Stop. */
5527 break;
5528 case WP_IGNORE:
5529 bs->print_it = print_it_noop;
5530 bs->stop = false;
5531 break;
5532 case WP_VALUE_CHANGED:
5533 if (b->type == bp_read_watchpoint)
5535 /* There are two cases to consider here:
5537 1. We're watching the triggered memory for reads.
5538 In that case, trust the target, and always report
5539 the watchpoint hit to the user. Even though
5540 reads don't cause value changes, the value may
5541 have changed since the last time it was read, and
5542 since we're not trapping writes, we will not see
5543 those, and as such we should ignore our notion of
5544 old value.
5546 2. We're watching the triggered memory for both
5547 reads and writes. There are two ways this may
5548 happen:
5550 2.1. This is a target that can't break on data
5551 reads only, but can break on accesses (reads or
5552 writes), such as e.g., x86. We detect this case
5553 at the time we try to insert read watchpoints.
5555 2.2. Otherwise, the target supports read
5556 watchpoints, but, the user set an access or write
5557 watchpoint watching the same memory as this read
5558 watchpoint.
5560 If we're watching memory writes as well as reads,
5561 ignore watchpoint hits when we find that the
5562 value hasn't changed, as reads don't cause
5563 changes. This still gives false positives when
5564 the program writes the same value to memory as
5565 what there was already in memory (we will confuse
5566 it for a read), but it's much better than
5567 nothing. */
5569 int other_write_watchpoint = 0;
5571 if (bl->watchpoint_type == hw_read)
5573 for (breakpoint &other_b : all_breakpoints ())
5574 if (other_b.type == bp_hardware_watchpoint
5575 || other_b.type == bp_access_watchpoint)
5577 watchpoint &other_w =
5578 gdb::checked_static_cast<watchpoint &> (other_b);
5580 if (other_w.watchpoint_triggered
5581 == watch_triggered_yes)
5583 other_write_watchpoint = 1;
5584 break;
5589 if (other_write_watchpoint
5590 || bl->watchpoint_type == hw_access)
5592 /* We're watching the same memory for writes,
5593 and the value changed since the last time we
5594 updated it, so this trap must be for a write.
5595 Ignore it. */
5596 bs->print_it = print_it_noop;
5597 bs->stop = false;
5600 break;
5601 case WP_VALUE_NOT_CHANGED:
5602 if (b->type == bp_hardware_watchpoint
5603 || b->type == bp_watchpoint)
5605 /* Don't stop: write watchpoints shouldn't fire if
5606 the value hasn't changed. */
5607 bs->print_it = print_it_noop;
5608 bs->stop = false;
5610 /* Stop. */
5611 break;
5612 default:
5613 /* Can't happen. */
5614 break;
5617 else /* !must_check_value */
5619 /* This is a case where some watchpoint(s) triggered, but
5620 not at the address of this watchpoint, or else no
5621 watchpoint triggered after all. So don't print
5622 anything for this watchpoint. */
5623 bs->print_it = print_it_noop;
5624 bs->stop = false;
5628 /* For breakpoints that are currently marked as telling gdb to stop,
5629 check conditions (condition proper, frame, thread and ignore count)
5630 of breakpoint referred to by BS. If we should not stop for this
5631 breakpoint, set BS->stop to 0. */
5633 static void
5634 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5636 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
5638 const struct bp_location *bl;
5639 struct breakpoint *b;
5640 /* Assume stop. */
5641 bool condition_result = true;
5642 struct expression *cond;
5644 gdb_assert (bs->stop);
5646 /* BS is built for existing struct breakpoint. */
5647 bl = bs->bp_location_at.get ();
5648 gdb_assert (bl != NULL);
5649 b = bs->breakpoint_at;
5650 gdb_assert (b != NULL);
5652 infrun_debug_printf ("thread = %s, breakpoint %d.%d",
5653 thread->ptid.to_string ().c_str (),
5654 b->number, find_loc_num_by_location (bl));
5656 /* Even if the target evaluated the condition on its end and notified GDB, we
5657 need to do so again since GDB does not know if we stopped due to a
5658 breakpoint or a single step breakpoint. */
5660 if (frame_id_p (b->frame_id)
5661 && b->frame_id != get_stack_frame_id (get_current_frame ()))
5663 infrun_debug_printf ("incorrect frame %s not %s, not stopping",
5664 get_stack_frame_id (get_current_frame ()).to_string ().c_str (),
5665 b->frame_id.to_string ().c_str ());
5666 bs->stop = false;
5667 return;
5670 /* If this is a thread/task-specific breakpoint, don't waste cpu
5671 evaluating the condition if this isn't the specified
5672 thread/task. */
5673 if ((b->thread != -1 && b->thread != thread->global_num)
5674 || (b->inferior != -1 && b->inferior != thread->inf->num)
5675 || (b->task != -1 && b->task != ada_get_task_number (thread)))
5677 infrun_debug_printf ("incorrect thread or task, not stopping");
5678 bs->stop = false;
5679 return;
5682 /* Evaluate extension language breakpoints that have a "stop" method
5683 implemented. */
5684 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5686 if (is_watchpoint (b))
5688 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
5690 cond = w->cond_exp.get ();
5692 else
5693 cond = bl->cond.get ();
5695 if (cond != nullptr && b->disposition != disp_del_at_next_stop)
5697 bool within_current_scope = true;
5699 /* We use scoped_value_mark because it could be a long time
5700 before we return to the command level and call
5701 free_all_values. We can't call free_all_values because we
5702 might be in the middle of evaluating a function call. */
5703 scoped_value_mark mark;
5705 watchpoint *w = nullptr;
5706 if (is_watchpoint (b))
5707 w = gdb::checked_static_cast<watchpoint *> (b);
5709 /* Need to select the frame, with all that implies so that
5710 the conditions will have the right context. Because we
5711 use the frame, we will not see an inlined function's
5712 variables when we arrive at a breakpoint at the start
5713 of the inlined function; the current frame will be the
5714 call site. */
5715 if (w == NULL || w->cond_exp_valid_block == NULL)
5716 select_frame (get_current_frame ());
5717 else
5719 frame_info_ptr frame;
5721 /* For local watchpoint expressions, which particular
5722 instance of a local is being watched matters, so we
5723 keep track of the frame to evaluate the expression
5724 in. To evaluate the condition however, it doesn't
5725 really matter which instantiation of the function
5726 where the condition makes sense triggers the
5727 watchpoint. This allows an expression like "watch
5728 global if q > 10" set in `func', catch writes to
5729 global on all threads that call `func', or catch
5730 writes on all recursive calls of `func' by a single
5731 thread. We simply always evaluate the condition in
5732 the innermost frame that's executing where it makes
5733 sense to evaluate the condition. It seems
5734 intuitive. */
5735 frame = block_innermost_frame (w->cond_exp_valid_block);
5736 if (frame != NULL)
5737 select_frame (frame);
5738 else
5739 within_current_scope = false;
5741 if (within_current_scope)
5745 scoped_restore reset_in_cond_eval
5746 = make_scoped_restore (&thread->control.in_cond_eval, true);
5747 condition_result = breakpoint_cond_eval (cond);
5749 catch (const gdb_exception_error &ex)
5751 int locno = bpstat_locno (bs);
5752 if (locno != 0)
5753 exception_fprintf
5754 (gdb_stderr, ex,
5755 "Error in testing condition for breakpoint %d.%d:\n",
5756 b->number, locno);
5757 else
5758 exception_fprintf
5759 (gdb_stderr, ex,
5760 "Error in testing condition for breakpoint %d:\n",
5761 b->number);
5764 else
5766 warning (_("Watchpoint condition cannot be tested "
5767 "in the current scope"));
5768 /* If we failed to set the right context for this
5769 watchpoint, unconditionally report it. */
5771 /* FIXME-someday, should give breakpoint #. */
5774 if (cond != nullptr && !condition_result)
5776 infrun_debug_printf ("condition_result = false, not stopping");
5777 bs->stop = false;
5778 return;
5780 else if (b->ignore_count > 0)
5782 infrun_debug_printf ("ignore count %d, not stopping",
5783 b->ignore_count);
5784 b->ignore_count--;
5785 bs->stop = false;
5786 /* Increase the hit count even though we don't stop. */
5787 ++(b->hit_count);
5788 notify_breakpoint_modified (b);
5789 return;
5792 if (bs->stop)
5793 infrun_debug_printf ("stopping at this breakpoint");
5794 else
5795 infrun_debug_printf ("not stopping at this breakpoint");
5798 /* Returns true if we need to track moribund locations of LOC's type
5799 on the current target. */
5801 static bool
5802 need_moribund_for_location_type (const struct bp_location *loc)
5804 return ((loc->loc_type == bp_loc_software_breakpoint
5805 && !target_supports_stopped_by_sw_breakpoint ())
5806 || (loc->loc_type == bp_loc_hardware_breakpoint
5807 && !target_supports_stopped_by_hw_breakpoint ()));
5810 /* See breakpoint.h. */
5812 bpstat *
5813 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5814 const target_waitstatus &ws)
5816 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5818 for (breakpoint &b : all_breakpoints ())
5820 if (!breakpoint_enabled (&b))
5821 continue;
5823 for (bp_location &bl : b.locations ())
5825 /* For hardware watchpoints, we look only at the first
5826 location. The watchpoint_check function will work on the
5827 entire expression, not the individual locations. For
5828 read watchpoints, the watchpoints_triggered function has
5829 checked all locations already. */
5830 if (b.type == bp_hardware_watchpoint && &bl != &b.first_loc ())
5831 break;
5833 if (!bl.enabled || bl.disabled_by_cond || bl.shlib_disabled)
5834 continue;
5836 if (!bpstat_check_location (&bl, aspace, bp_addr, ws))
5837 continue;
5839 /* Come here if it's a watchpoint, or if the break address
5840 matches. */
5842 bpstat *bs = new bpstat (&bl, &bs_link); /* Alloc a bpstat to
5843 explain stop. */
5845 /* Assume we stop. Should we find a watchpoint that is not
5846 actually triggered, or if the condition of the breakpoint
5847 evaluates as false, we'll reset 'stop' to 0. */
5848 bs->stop = true;
5849 bs->print = true;
5851 /* If this is a scope breakpoint, mark the associated
5852 watchpoint as triggered so that we will handle the
5853 out-of-scope event. We'll get to the watchpoint next
5854 iteration. */
5855 if (b.type == bp_watchpoint_scope && b.related_breakpoint != &b)
5857 watchpoint *w
5858 = gdb::checked_static_cast<watchpoint *> (b.related_breakpoint);
5860 w->watchpoint_triggered = watch_triggered_yes;
5865 /* Check if a moribund breakpoint explains the stop. */
5866 if (!target_supports_stopped_by_sw_breakpoint ()
5867 || !target_supports_stopped_by_hw_breakpoint ())
5869 for (bp_location *loc : moribund_locations)
5871 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5872 && need_moribund_for_location_type (loc))
5874 bpstat *bs = new bpstat (loc, &bs_link);
5875 /* For hits of moribund locations, we should just proceed. */
5876 bs->stop = false;
5877 bs->print = false;
5878 bs->print_it = print_it_noop;
5883 return bs_head;
5886 /* See breakpoint.h. */
5888 bpstat *
5889 bpstat_stop_status (const address_space *aspace,
5890 CORE_ADDR bp_addr, thread_info *thread,
5891 const target_waitstatus &ws,
5892 bpstat *stop_chain)
5894 struct breakpoint *b = NULL;
5895 /* First item of allocated bpstat's. */
5896 bpstat *bs_head = stop_chain;
5897 bpstat *bs;
5898 int need_remove_insert;
5899 int removed_any;
5901 /* First, build the bpstat chain with locations that explain a
5902 target stop, while being careful to not set the target running,
5903 as that may invalidate locations (in particular watchpoint
5904 locations are recreated). Resuming will happen here with
5905 breakpoint conditions or watchpoint expressions that include
5906 inferior function calls. */
5907 if (bs_head == NULL)
5908 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5910 /* A bit of special processing for shlib breakpoints. We need to
5911 process solib loading here, so that the lists of loaded and
5912 unloaded libraries are correct before we handle "catch load" and
5913 "catch unload". */
5914 for (bs = bs_head; bs != NULL; bs = bs->next)
5916 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5918 handle_solib_event ();
5919 break;
5923 /* Now go through the locations that caused the target to stop, and
5924 check whether we're interested in reporting this stop to higher
5925 layers, or whether we should resume the target transparently. */
5927 removed_any = 0;
5929 for (bs = bs_head; bs != NULL; bs = bs->next)
5931 if (!bs->stop)
5932 continue;
5934 b = bs->breakpoint_at;
5935 b->check_status (bs);
5936 if (bs->stop)
5938 bpstat_check_breakpoint_conditions (bs, thread);
5940 if (bs->stop)
5942 ++(b->hit_count);
5944 /* We will stop here. */
5945 if (b->disposition == disp_disable)
5947 --(b->enable_count);
5948 if (b->enable_count <= 0)
5949 b->enable_state = bp_disabled;
5950 removed_any = 1;
5952 notify_breakpoint_modified (b);
5953 if (b->silent)
5954 bs->print = false;
5955 bs->commands = b->commands;
5956 if (command_line_is_silent (bs->commands
5957 ? bs->commands.get () : NULL))
5958 bs->print = false;
5960 b->after_condition_true (bs);
5965 /* Print nothing for this entry if we don't stop or don't
5966 print. */
5967 if (!bs->stop || !bs->print)
5968 bs->print_it = print_it_noop;
5971 /* If we aren't stopping, the value of some hardware watchpoint may
5972 not have changed, but the intermediate memory locations we are
5973 watching may have. Don't bother if we're stopping; this will get
5974 done later. */
5975 need_remove_insert = 0;
5976 if (! bpstat_causes_stop (bs_head))
5977 for (bs = bs_head; bs != NULL; bs = bs->next)
5978 if (!bs->stop
5979 && bs->breakpoint_at
5980 && is_hardware_watchpoint (bs->breakpoint_at))
5982 watchpoint *w
5983 = gdb::checked_static_cast<watchpoint *> (bs->breakpoint_at);
5985 update_watchpoint (w, false /* don't reparse. */);
5986 need_remove_insert = 1;
5989 if (need_remove_insert)
5990 update_global_location_list (UGLL_MAY_INSERT);
5991 else if (removed_any)
5992 update_global_location_list (UGLL_DONT_INSERT);
5994 return bs_head;
5997 /* See breakpoint.h. */
5999 bpstat *
6000 bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
6001 thread_info *thread, const target_waitstatus &ws)
6003 gdb_assert (!target_stopped_by_watchpoint ());
6005 /* Clear all watchpoints' 'watchpoint_triggered' value from a
6006 previous stop to avoid confusing bpstat_stop_status. */
6007 watchpoints_triggered (ws);
6009 return bpstat_stop_status (aspace, bp_addr, thread, ws);
6012 static void
6013 handle_jit_event (CORE_ADDR address)
6015 struct gdbarch *gdbarch;
6017 infrun_debug_printf ("handling bp_jit_event");
6019 /* Switch terminal for any messages produced by
6020 breakpoint_re_set. */
6021 target_terminal::ours_for_output ();
6023 gdbarch = get_frame_arch (get_current_frame ());
6024 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
6025 thus it is expected that its objectfile can be found through
6026 minimal symbol lookup. If it doesn't work (and assert fails), it
6027 most likely means that `jit_breakpoint_re_set` was changes and this
6028 function needs to be updated too. */
6029 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
6030 gdb_assert (jit_bp_sym.objfile != nullptr);
6031 objfile *objfile = jit_bp_sym.objfile;
6032 if (objfile->separate_debug_objfile_backlink)
6033 objfile = objfile->separate_debug_objfile_backlink;
6034 jit_event_handler (gdbarch, objfile);
6036 target_terminal::inferior ();
6039 /* Prepare WHAT final decision for infrun. */
6041 /* Decide what infrun needs to do with this bpstat. */
6043 struct bpstat_what
6044 bpstat_what (bpstat *bs_head)
6046 struct bpstat_what retval;
6047 bpstat *bs;
6049 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
6050 retval.call_dummy = STOP_NONE;
6051 retval.is_longjmp = false;
6053 for (bs = bs_head; bs != NULL; bs = bs->next)
6055 /* Extract this BS's action. After processing each BS, we check
6056 if its action overrides all we've seem so far. */
6057 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
6058 enum bptype bptype;
6060 if (bs->breakpoint_at == NULL)
6062 /* I suspect this can happen if it was a momentary
6063 breakpoint which has since been deleted. */
6064 bptype = bp_none;
6066 else
6067 bptype = bs->breakpoint_at->type;
6069 switch (bptype)
6071 case bp_none:
6072 break;
6073 case bp_breakpoint:
6074 case bp_hardware_breakpoint:
6075 case bp_single_step:
6076 case bp_until:
6077 case bp_finish:
6078 case bp_shlib_event:
6079 if (bs->stop)
6081 if (bs->print)
6082 this_action = BPSTAT_WHAT_STOP_NOISY;
6083 else
6084 this_action = BPSTAT_WHAT_STOP_SILENT;
6086 else
6087 this_action = BPSTAT_WHAT_SINGLE;
6088 break;
6089 case bp_watchpoint:
6090 case bp_hardware_watchpoint:
6091 case bp_read_watchpoint:
6092 case bp_access_watchpoint:
6093 if (bs->stop)
6095 if (bs->print)
6096 this_action = BPSTAT_WHAT_STOP_NOISY;
6097 else
6098 this_action = BPSTAT_WHAT_STOP_SILENT;
6100 else
6102 /* There was a watchpoint, but we're not stopping.
6103 This requires no further action. */
6105 break;
6106 case bp_longjmp:
6107 case bp_longjmp_call_dummy:
6108 case bp_exception:
6109 if (bs->stop)
6111 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
6112 retval.is_longjmp = bptype != bp_exception;
6114 else
6115 this_action = BPSTAT_WHAT_SINGLE;
6116 break;
6117 case bp_longjmp_resume:
6118 case bp_exception_resume:
6119 if (bs->stop)
6121 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
6122 retval.is_longjmp = bptype == bp_longjmp_resume;
6124 else
6125 this_action = BPSTAT_WHAT_SINGLE;
6126 break;
6127 case bp_step_resume:
6128 if (bs->stop)
6129 this_action = BPSTAT_WHAT_STEP_RESUME;
6130 else
6132 /* It is for the wrong frame. */
6133 this_action = BPSTAT_WHAT_SINGLE;
6135 break;
6136 case bp_hp_step_resume:
6137 if (bs->stop)
6138 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
6139 else
6141 /* It is for the wrong frame. */
6142 this_action = BPSTAT_WHAT_SINGLE;
6144 break;
6145 case bp_watchpoint_scope:
6146 case bp_thread_event:
6147 case bp_overlay_event:
6148 case bp_longjmp_master:
6149 case bp_std_terminate_master:
6150 case bp_exception_master:
6151 this_action = BPSTAT_WHAT_SINGLE;
6152 break;
6153 case bp_catchpoint:
6154 if (bs->stop)
6156 if (bs->print)
6157 this_action = BPSTAT_WHAT_STOP_NOISY;
6158 else
6159 this_action = BPSTAT_WHAT_STOP_SILENT;
6161 else
6163 /* Some catchpoints are implemented with breakpoints.
6164 For those, we need to step over the breakpoint. */
6165 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
6166 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
6167 this_action = BPSTAT_WHAT_SINGLE;
6169 break;
6170 case bp_jit_event:
6171 this_action = BPSTAT_WHAT_SINGLE;
6172 break;
6173 case bp_call_dummy:
6174 /* Make sure the action is stop (silent or noisy),
6175 so infrun.c pops the dummy frame. */
6176 retval.call_dummy = STOP_STACK_DUMMY;
6177 this_action = BPSTAT_WHAT_STOP_SILENT;
6178 break;
6179 case bp_std_terminate:
6180 /* Make sure the action is stop (silent or noisy),
6181 so infrun.c pops the dummy frame. */
6182 retval.call_dummy = STOP_STD_TERMINATE;
6183 this_action = BPSTAT_WHAT_STOP_SILENT;
6184 break;
6185 case bp_tracepoint:
6186 case bp_fast_tracepoint:
6187 case bp_static_tracepoint:
6188 case bp_static_marker_tracepoint:
6189 /* Tracepoint hits should not be reported back to GDB, and
6190 if one got through somehow, it should have been filtered
6191 out already. */
6192 internal_error (_("bpstat_what: tracepoint encountered"));
6193 break;
6194 case bp_gnu_ifunc_resolver:
6195 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
6196 this_action = BPSTAT_WHAT_SINGLE;
6197 break;
6198 case bp_gnu_ifunc_resolver_return:
6199 /* The breakpoint will be removed, execution will restart from the
6200 PC of the former breakpoint. */
6201 this_action = BPSTAT_WHAT_KEEP_CHECKING;
6202 break;
6204 case bp_dprintf:
6205 if (bs->stop)
6206 this_action = BPSTAT_WHAT_STOP_SILENT;
6207 else
6208 this_action = BPSTAT_WHAT_SINGLE;
6209 break;
6211 default:
6212 internal_error (_("bpstat_what: unhandled bptype %d"), (int) bptype);
6215 retval.main_action = std::max (retval.main_action, this_action);
6218 return retval;
6221 void
6222 bpstat_run_callbacks (bpstat *bs_head)
6224 bpstat *bs;
6226 for (bs = bs_head; bs != NULL; bs = bs->next)
6228 struct breakpoint *b = bs->breakpoint_at;
6230 if (b == NULL)
6231 continue;
6232 switch (b->type)
6234 case bp_jit_event:
6235 handle_jit_event (bs->bp_location_at->address);
6236 break;
6237 case bp_gnu_ifunc_resolver:
6238 gnu_ifunc_resolver_stop
6239 (gdb::checked_static_cast<code_breakpoint *> (b));
6240 break;
6241 case bp_gnu_ifunc_resolver_return:
6242 gnu_ifunc_resolver_return_stop
6243 (gdb::checked_static_cast<code_breakpoint *> (b));
6244 break;
6249 /* See breakpoint.h. */
6251 bool
6252 bpstat_should_step ()
6254 for (breakpoint &b : all_breakpoints ())
6255 if (breakpoint_enabled (&b)
6256 && b.type == bp_watchpoint
6257 && b.has_locations ())
6258 return true;
6260 return false;
6263 /* See breakpoint.h. */
6265 bool
6266 bpstat_causes_stop (bpstat *bs)
6268 for (; bs != NULL; bs = bs->next)
6269 if (bs->stop)
6270 return true;
6272 return false;
6277 /* Compute a number of spaces suitable to indent the next line
6278 so it starts at the position corresponding to the table column
6279 named COL_NAME in the currently active table of UIOUT. */
6281 static int
6282 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6284 int i, total_width, width, align;
6285 const char *text;
6287 total_width = 0;
6288 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
6290 if (strcmp (text, col_name) == 0)
6291 return total_width;
6293 total_width += width + 1;
6296 return 0;
6299 /* Determine if the locations of this breakpoint will have their conditions
6300 evaluated by the target, host or a mix of both. Returns the following:
6302 "host": Host evals condition.
6303 "host or target": Host or Target evals condition.
6304 "target": Target evals condition.
6307 static const char *
6308 bp_condition_evaluator (const breakpoint *b)
6310 char host_evals = 0;
6311 char target_evals = 0;
6313 if (!b)
6314 return NULL;
6316 if (!is_breakpoint (b))
6317 return NULL;
6319 if (gdb_evaluates_breakpoint_condition_p ()
6320 || !target_supports_evaluation_of_breakpoint_conditions ())
6321 return condition_evaluation_host;
6323 for (bp_location &bl : b->locations ())
6325 if (bl.cond_bytecode)
6326 target_evals++;
6327 else
6328 host_evals++;
6331 if (host_evals && target_evals)
6332 return condition_evaluation_both;
6333 else if (target_evals)
6334 return condition_evaluation_target;
6335 else
6336 return condition_evaluation_host;
6339 /* Determine the breakpoint location's condition evaluator. This is
6340 similar to bp_condition_evaluator, but for locations. */
6342 static const char *
6343 bp_location_condition_evaluator (const struct bp_location *bl)
6345 if (bl && !is_breakpoint (bl->owner))
6346 return NULL;
6348 if (gdb_evaluates_breakpoint_condition_p ()
6349 || !target_supports_evaluation_of_breakpoint_conditions ())
6350 return condition_evaluation_host;
6352 if (bl && bl->cond_bytecode)
6353 return condition_evaluation_target;
6354 else
6355 return condition_evaluation_host;
6358 /* Print the LOC location out of the list of B->LOC locations. */
6360 static void
6361 print_breakpoint_location (const breakpoint *b, const bp_location *loc)
6363 struct ui_out *uiout = current_uiout;
6365 scoped_restore_current_program_space restore_pspace;
6367 if (loc != NULL && loc->shlib_disabled)
6368 loc = NULL;
6370 if (loc != NULL)
6371 set_current_program_space (loc->pspace);
6373 if (b->display_canonical)
6374 uiout->field_string ("what", b->locspec->to_string ());
6375 else if (loc && loc->symtab)
6377 const struct symbol *sym = loc->symbol;
6379 if (sym)
6381 uiout->text ("in ");
6382 uiout->field_string ("func", sym->print_name (),
6383 function_name_style.style ());
6384 uiout->text (" ");
6385 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6386 uiout->text ("at ");
6388 uiout->field_string ("file",
6389 symtab_to_filename_for_display (loc->symtab),
6390 file_name_style.style ());
6391 uiout->text (":");
6393 if (uiout->is_mi_like_p ())
6394 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6396 uiout->field_signed ("line", loc->line_number,
6397 line_number_style.style ());
6399 else if (loc)
6401 string_file stb;
6403 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6404 demangle, "");
6405 uiout->field_stream ("at", stb);
6407 else
6408 uiout->field_string ("pending", b->locspec->to_string ());
6410 if (loc && is_breakpoint (b)
6411 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6412 && bp_condition_evaluator (b) == condition_evaluation_both)
6414 uiout->text (" (");
6415 uiout->field_string ("evaluated-by",
6416 bp_location_condition_evaluator (loc));
6417 uiout->text (")");
6421 static const char *
6422 bptype_string (enum bptype type)
6424 struct ep_type_description
6426 enum bptype type;
6427 const char *description;
6429 static struct ep_type_description bptypes[] =
6431 {bp_none, "?deleted?"},
6432 {bp_breakpoint, "breakpoint"},
6433 {bp_hardware_breakpoint, "hw breakpoint"},
6434 {bp_single_step, "sw single-step"},
6435 {bp_until, "until"},
6436 {bp_finish, "finish"},
6437 {bp_watchpoint, "watchpoint"},
6438 {bp_hardware_watchpoint, "hw watchpoint"},
6439 {bp_read_watchpoint, "read watchpoint"},
6440 {bp_access_watchpoint, "acc watchpoint"},
6441 {bp_longjmp, "longjmp"},
6442 {bp_longjmp_resume, "longjmp resume"},
6443 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6444 {bp_exception, "exception"},
6445 {bp_exception_resume, "exception resume"},
6446 {bp_step_resume, "step resume"},
6447 {bp_hp_step_resume, "high-priority step resume"},
6448 {bp_watchpoint_scope, "watchpoint scope"},
6449 {bp_call_dummy, "call dummy"},
6450 {bp_std_terminate, "std::terminate"},
6451 {bp_shlib_event, "shlib events"},
6452 {bp_thread_event, "thread events"},
6453 {bp_overlay_event, "overlay events"},
6454 {bp_longjmp_master, "longjmp master"},
6455 {bp_std_terminate_master, "std::terminate master"},
6456 {bp_exception_master, "exception master"},
6457 {bp_catchpoint, "catchpoint"},
6458 {bp_tracepoint, "tracepoint"},
6459 {bp_fast_tracepoint, "fast tracepoint"},
6460 {bp_static_tracepoint, "static tracepoint"},
6461 {bp_static_marker_tracepoint, "static marker tracepoint"},
6462 {bp_dprintf, "dprintf"},
6463 {bp_jit_event, "jit events"},
6464 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6465 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6468 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6469 || ((int) type != bptypes[(int) type].type))
6470 internal_error (_("bptypes table does not describe type #%d."),
6471 (int) type);
6473 return bptypes[(int) type].description;
6476 /* For MI, output a field named 'thread-groups' with a list as the value.
6477 For CLI, prefix the list with the string 'inf'. */
6479 static void
6480 output_thread_groups (struct ui_out *uiout,
6481 const char *field_name,
6482 const std::vector<int> &inf_nums,
6483 int mi_only)
6485 int is_mi = uiout->is_mi_like_p ();
6487 /* For backward compatibility, don't display inferiors in CLI unless
6488 there are several. Always display them for MI. */
6489 if (!is_mi && mi_only)
6490 return;
6492 ui_out_emit_list list_emitter (uiout, field_name);
6494 for (size_t i = 0; i < inf_nums.size (); i++)
6496 if (is_mi)
6498 char mi_group[10];
6500 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6501 uiout->field_string (NULL, mi_group);
6503 else
6505 if (i == 0)
6506 uiout->text (" inf ");
6507 else
6508 uiout->text (", ");
6510 uiout->text (plongest (inf_nums[i]));
6515 /* See breakpoint.h. */
6517 bool fix_breakpoint_script_output_globally = false;
6519 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6520 instead of going via breakpoint_ops::print_one. This makes "maint
6521 info breakpoints" show the software breakpoint locations of
6522 catchpoints, which are considered internal implementation
6523 detail. Returns true if RAW_LOC is false and if the breakpoint's
6524 print_one method did something; false otherwise. */
6526 static bool
6527 print_one_breakpoint_location (struct breakpoint *b,
6528 struct bp_location *loc,
6529 int loc_number,
6530 const bp_location **last_loc,
6531 int allflag, bool raw_loc)
6533 struct command_line *l;
6534 static char bpenables[] = "nynny";
6536 struct ui_out *uiout = current_uiout;
6537 bool header_of_multiple = false;
6538 bool part_of_multiple = (loc != NULL);
6539 struct value_print_options opts;
6541 get_user_print_options (&opts);
6543 gdb_assert (!loc || loc_number != 0);
6544 /* See comment in print_one_breakpoint concerning treatment of
6545 breakpoints with single disabled location. */
6546 if (loc == NULL
6547 && (b->has_locations ()
6548 && (b->has_multiple_locations ()
6549 || !b->first_loc ().enabled || b->first_loc ().disabled_by_cond)))
6550 header_of_multiple = true;
6552 if (loc == NULL && b->has_locations ())
6553 loc = &b->first_loc ();
6555 annotate_record ();
6557 /* 1 */
6558 annotate_field (0);
6559 if (part_of_multiple)
6560 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6561 else
6562 uiout->field_signed ("number", b->number);
6564 /* 2 */
6565 annotate_field (1);
6566 if (part_of_multiple)
6567 uiout->field_skip ("type");
6568 else
6569 uiout->field_string ("type", bptype_string (b->type));
6571 /* 3 */
6572 annotate_field (2);
6573 if (part_of_multiple)
6574 uiout->field_skip ("disp");
6575 else
6576 uiout->field_string ("disp", bpdisp_text (b->disposition));
6578 /* 4 */
6579 annotate_field (3);
6580 if (part_of_multiple)
6582 /* For locations that are disabled because of an invalid
6583 condition, display "N*" on the CLI, where "*" refers to a
6584 footnote below the table. For MI, simply display a "N"
6585 without a footnote. On the CLI, for enabled locations whose
6586 breakpoint is disabled, display "y-". */
6587 auto get_enable_state = [uiout, loc] () -> const char *
6589 if (uiout->is_mi_like_p ())
6591 if (loc->disabled_by_cond)
6592 return "N";
6593 else if (!loc->enabled)
6594 return "n";
6595 else
6596 return "y";
6598 else
6600 if (loc->disabled_by_cond)
6601 return "N*";
6602 else if (!loc->enabled)
6603 return "n";
6604 else if (!breakpoint_enabled (loc->owner))
6605 return "y-";
6606 else
6607 return "y";
6610 uiout->field_string ("enabled", get_enable_state ());
6612 else
6613 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6615 /* 5 and 6 */
6616 bool result = false;
6617 if (!raw_loc && b->print_one (last_loc))
6618 result = true;
6619 else
6621 if (is_watchpoint (b))
6623 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
6625 /* Field 4, the address, is omitted (which makes the columns
6626 not line up too nicely with the headers, but the effect
6627 is relatively readable). */
6628 if (opts.addressprint)
6629 uiout->field_skip ("addr");
6630 annotate_field (5);
6631 uiout->field_string ("what", w->exp_string.get ());
6633 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6634 || is_ada_exception_catchpoint (b))
6636 if (opts.addressprint)
6638 annotate_field (4);
6639 if (header_of_multiple)
6640 uiout->field_string ("addr", "<MULTIPLE>",
6641 metadata_style.style ());
6642 else if (!b->has_locations () || loc->shlib_disabled)
6643 uiout->field_string ("addr", "<PENDING>",
6644 metadata_style.style ());
6645 else
6646 uiout->field_core_addr ("addr",
6647 loc->gdbarch, loc->address);
6649 annotate_field (5);
6650 if (!header_of_multiple)
6651 print_breakpoint_location (b, loc);
6652 if (b->has_locations ())
6653 *last_loc = &b->first_loc ();
6657 if (loc != nullptr && !header_of_multiple && !loc->shlib_disabled)
6659 std::vector<int> inf_nums;
6660 int mi_only = 1;
6662 for (inferior *inf : all_inferiors ())
6664 if (inf->pspace == loc->pspace)
6665 inf_nums.push_back (inf->num);
6668 /* For backward compatibility, don't display inferiors in CLI unless
6669 there are several. Always display for MI. */
6670 if (allflag
6671 || (!gdbarch_has_global_breakpoints (current_inferior ()->arch ())
6672 && (program_spaces.size () > 1
6673 || number_of_inferiors () > 1)
6674 /* LOC is for existing B, it cannot be in
6675 moribund_locations and thus having NULL OWNER. */
6676 && loc->owner->type != bp_catchpoint))
6677 mi_only = 0;
6678 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6681 /* In the MI output, each location of a thread or task specific
6682 breakpoint includes the relevant thread or task ID. This is done for
6683 backwards compatibility reasons.
6685 For the CLI output, the thread/task information is printed on a
6686 separate line, see the 'stop only in thread' and 'stop only in task'
6687 output below. */
6688 if (part_of_multiple && uiout->is_mi_like_p ())
6690 if (b->thread != -1)
6691 uiout->field_signed ("thread", b->thread);
6692 else if (b->task != -1)
6693 uiout->field_signed ("task", b->task);
6694 else if (b->inferior != -1)
6695 uiout->field_signed ("inferior", b->inferior);
6698 uiout->text ("\n");
6700 if (!part_of_multiple)
6701 b->print_one_detail (uiout);
6703 if (part_of_multiple && frame_id_p (b->frame_id))
6705 annotate_field (6);
6706 uiout->text ("\tstop only in stack frame at ");
6707 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6708 the frame ID. */
6709 uiout->field_core_addr ("frame",
6710 b->gdbarch, b->frame_id.stack_addr);
6711 uiout->text ("\n");
6714 if (!part_of_multiple && b->cond_string)
6716 annotate_field (7);
6717 if (is_tracepoint (b))
6718 uiout->text ("\ttrace only if ");
6719 else
6720 uiout->text ("\tstop only if ");
6721 uiout->field_string ("cond", b->cond_string.get ());
6723 /* Print whether the target is doing the breakpoint's condition
6724 evaluation. If GDB is doing the evaluation, don't print anything. */
6725 if (is_breakpoint (b)
6726 && breakpoint_condition_evaluation_mode ()
6727 == condition_evaluation_target)
6729 uiout->message (" (%pF evals)",
6730 string_field ("evaluated-by",
6731 bp_condition_evaluator (b)));
6733 uiout->text ("\n");
6736 if (!part_of_multiple && b->thread != -1)
6738 /* FIXME should make an annotation for this. */
6739 uiout->text ("\tstop only in thread ");
6740 if (uiout->is_mi_like_p ())
6741 uiout->field_signed ("thread", b->thread);
6742 else
6744 struct thread_info *thr = find_thread_global_id (b->thread);
6746 uiout->field_string ("thread", print_thread_id (thr));
6748 uiout->text ("\n");
6751 if (!part_of_multiple && b->task != -1)
6753 uiout->text ("\tstop only in task ");
6754 uiout->field_signed ("task", b->task);
6755 uiout->text ("\n");
6758 if (!part_of_multiple && b->inferior != -1)
6760 uiout->text ("\tstop only in inferior ");
6761 uiout->field_signed ("inferior", b->inferior);
6762 uiout->text ("\n");
6765 if (!part_of_multiple)
6767 if (b->hit_count)
6769 /* FIXME should make an annotation for this. */
6770 if (is_catchpoint (b))
6771 uiout->text ("\tcatchpoint");
6772 else if (is_tracepoint (b))
6773 uiout->text ("\ttracepoint");
6774 else
6775 uiout->text ("\tbreakpoint");
6776 uiout->text (" already hit ");
6777 uiout->field_signed ("times", b->hit_count);
6778 if (b->hit_count == 1)
6779 uiout->text (" time\n");
6780 else
6781 uiout->text (" times\n");
6783 else
6785 /* Output the count also if it is zero, but only if this is mi. */
6786 if (uiout->is_mi_like_p ())
6787 uiout->field_signed ("times", b->hit_count);
6791 if (!part_of_multiple && b->ignore_count)
6793 annotate_field (8);
6794 uiout->message ("\tignore next %pF hits\n",
6795 signed_field ("ignore", b->ignore_count));
6798 /* Note that an enable count of 1 corresponds to "enable once"
6799 behavior, which is reported by the combination of enablement and
6800 disposition, so we don't need to mention it here. */
6801 if (!part_of_multiple && b->enable_count > 1)
6803 annotate_field (8);
6804 uiout->text ("\tdisable after ");
6805 /* Tweak the wording to clarify that ignore and enable counts
6806 are distinct, and have additive effect. */
6807 if (b->ignore_count)
6808 uiout->text ("additional ");
6809 else
6810 uiout->text ("next ");
6811 uiout->field_signed ("enable", b->enable_count);
6812 uiout->text (" hits\n");
6815 if (!part_of_multiple && is_tracepoint (b))
6817 tracepoint *tp = gdb::checked_static_cast<tracepoint *> (b);
6819 if (tp->traceframe_usage)
6821 uiout->text ("\ttrace buffer usage ");
6822 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6823 uiout->text (" bytes\n");
6827 l = b->commands ? b->commands.get () : NULL;
6828 if (!part_of_multiple && l)
6830 annotate_field (9);
6832 bool use_fixed_output =
6833 (uiout->test_flags (fix_breakpoint_script_output)
6834 || fix_breakpoint_script_output_globally);
6836 std::optional<ui_out_emit_tuple> tuple_emitter;
6837 std::optional<ui_out_emit_list> list_emitter;
6839 if (use_fixed_output)
6840 list_emitter.emplace (uiout, "script");
6841 else
6842 tuple_emitter.emplace (uiout, "script");
6844 print_command_lines (uiout, l, 4);
6847 if (is_tracepoint (b))
6849 tracepoint *t = gdb::checked_static_cast<tracepoint *> (b);
6851 if (!part_of_multiple && t->pass_count)
6853 annotate_field (10);
6854 uiout->text ("\tpass count ");
6855 uiout->field_signed ("pass", t->pass_count);
6856 uiout->text (" \n");
6859 /* Don't display it when tracepoint or tracepoint location is
6860 pending. */
6861 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6863 annotate_field (11);
6865 if (uiout->is_mi_like_p ())
6866 uiout->field_string ("installed",
6867 loc->inserted ? "y" : "n");
6868 else
6870 if (loc->inserted)
6871 uiout->text ("\t");
6872 else
6873 uiout->text ("\tnot ");
6874 uiout->text ("installed on target\n");
6879 if (uiout->is_mi_like_p () && !part_of_multiple)
6881 if (is_watchpoint (b))
6883 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
6885 uiout->field_string ("original-location", w->exp_string.get ());
6887 else if (b->locspec != nullptr)
6889 const char *str = b->locspec->to_string ();
6890 if (str != nullptr)
6891 uiout->field_string ("original-location", str);
6895 return result;
6898 /* See breakpoint.h. */
6900 bool fix_multi_location_breakpoint_output_globally = false;
6902 static void
6903 print_one_breakpoint (breakpoint *b, const bp_location **last_loc, int allflag)
6905 struct ui_out *uiout = current_uiout;
6906 bool use_fixed_output
6907 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6908 || fix_multi_location_breakpoint_output_globally);
6910 std::optional<ui_out_emit_tuple> bkpt_tuple_emitter (std::in_place, uiout,
6911 "bkpt");
6912 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6913 allflag, false);
6915 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6916 are outside. */
6917 if (!use_fixed_output)
6918 bkpt_tuple_emitter.reset ();
6920 /* If this breakpoint has custom print function,
6921 it's already printed. Otherwise, print individual
6922 locations, if any. */
6923 if (!printed || allflag)
6925 /* If breakpoint has a single location that is disabled, we
6926 print it as if it had several locations, since otherwise it's
6927 hard to represent "breakpoint enabled, location disabled"
6928 situation.
6930 Note that while hardware watchpoints have several locations
6931 internally, that's not a property exposed to users.
6933 Likewise, while catchpoints may be implemented with
6934 breakpoints (e.g., catch throw), that's not a property
6935 exposed to users. We do however display the internal
6936 breakpoint locations with "maint info breakpoints". */
6937 if (!is_hardware_watchpoint (b)
6938 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6939 || is_ada_exception_catchpoint (b))
6940 && (allflag
6941 || (b->has_locations ()
6942 && (b->has_multiple_locations ()
6943 || !b->first_loc ().enabled
6944 || b->first_loc ().disabled_by_cond))))
6946 std::optional<ui_out_emit_list> locations_list;
6948 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6949 MI record. For later versions, place breakpoint locations in a
6950 list. */
6951 if (uiout->is_mi_like_p () && use_fixed_output)
6952 locations_list.emplace (uiout, "locations");
6954 int n = 1;
6955 for (bp_location &loc : b->locations ())
6957 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6958 print_one_breakpoint_location (b, &loc, n, last_loc,
6959 allflag, allflag);
6960 n++;
6966 static int
6967 breakpoint_address_bits (struct breakpoint *b)
6969 int print_address_bits = 0;
6971 for (bp_location &loc : b->locations ())
6973 if (!bl_address_is_meaningful (&loc))
6974 continue;
6976 int addr_bit = gdbarch_addr_bit (loc.gdbarch);
6977 if (addr_bit > print_address_bits)
6978 print_address_bits = addr_bit;
6981 return print_address_bits;
6984 /* See breakpoint.h. */
6986 void
6987 print_breakpoint (breakpoint *b)
6989 const bp_location *dummy_loc = nullptr;
6990 print_one_breakpoint (b, &dummy_loc, 0);
6993 /* Return true if this breakpoint was set by the user, false if it is
6994 internal or momentary. */
6997 user_breakpoint_p (struct breakpoint *b)
6999 return b->number > 0;
7002 /* See breakpoint.h. */
7005 pending_breakpoint_p (struct breakpoint *b)
7007 return !b->has_locations ();
7010 /* Print information on breakpoints (including watchpoints and tracepoints).
7012 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
7013 understood by number_or_range_parser. Only breakpoints included in this
7014 list are then printed.
7016 If SHOW_INTERNAL is true, print internal breakpoints.
7018 If FILTER is non-NULL, call it on each breakpoint and only include the
7019 ones for which it returns true.
7021 Return the total number of breakpoints listed. */
7023 static int
7024 breakpoint_1 (const char *bp_num_list, bool show_internal,
7025 bool (*filter) (const struct breakpoint *))
7027 const bp_location *last_loc = nullptr;
7028 int nr_printable_breakpoints;
7029 struct value_print_options opts;
7030 int print_address_bits = 0;
7031 int print_type_col_width = 14;
7032 struct ui_out *uiout = current_uiout;
7033 bool has_disabled_by_cond_location = false;
7035 get_user_print_options (&opts);
7037 /* Compute the number of rows in the table, as well as the size
7038 required for address fields. */
7039 nr_printable_breakpoints = 0;
7040 for (breakpoint &b : all_breakpoints ())
7042 /* If we have a filter, only list the breakpoints it accepts. */
7043 if (filter && !filter (&b))
7044 continue;
7046 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
7047 accept. Skip the others. */
7048 if (bp_num_list != NULL && *bp_num_list != '\0')
7050 if (show_internal && parse_and_eval_long (bp_num_list) != b.number)
7051 continue;
7052 if (!show_internal && !number_is_in_list (bp_num_list, b.number))
7053 continue;
7056 if (show_internal || user_breakpoint_p (&b))
7058 int addr_bit, type_len;
7060 addr_bit = breakpoint_address_bits (&b);
7061 if (addr_bit > print_address_bits)
7062 print_address_bits = addr_bit;
7064 type_len = strlen (bptype_string (b.type));
7065 if (type_len > print_type_col_width)
7066 print_type_col_width = type_len;
7068 nr_printable_breakpoints++;
7073 ui_out_emit_table table_emitter (uiout,
7074 opts.addressprint ? 6 : 5,
7075 nr_printable_breakpoints,
7076 "BreakpointTable");
7078 if (nr_printable_breakpoints > 0)
7079 annotate_breakpoints_headers ();
7080 if (nr_printable_breakpoints > 0)
7081 annotate_field (0);
7082 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
7083 if (nr_printable_breakpoints > 0)
7084 annotate_field (1);
7085 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
7086 if (nr_printable_breakpoints > 0)
7087 annotate_field (2);
7088 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
7089 if (nr_printable_breakpoints > 0)
7090 annotate_field (3);
7091 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
7092 if (opts.addressprint)
7094 if (nr_printable_breakpoints > 0)
7095 annotate_field (4);
7096 if (print_address_bits <= 32)
7097 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
7098 else
7099 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
7101 if (nr_printable_breakpoints > 0)
7102 annotate_field (5);
7103 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
7104 uiout->table_body ();
7105 if (nr_printable_breakpoints > 0)
7106 annotate_breakpoints_table ();
7108 for (breakpoint &b : all_breakpoints ())
7110 QUIT;
7111 /* If we have a filter, only list the breakpoints it accepts. */
7112 if (filter && !filter (&b))
7113 continue;
7115 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
7116 accept. Skip the others. */
7118 if (bp_num_list != NULL && *bp_num_list != '\0')
7120 if (show_internal) /* maintenance info breakpoint */
7122 if (parse_and_eval_long (bp_num_list) != b.number)
7123 continue;
7125 else /* all others */
7127 if (!number_is_in_list (bp_num_list, b.number))
7128 continue;
7131 /* We only print out user settable breakpoints unless the
7132 show_internal is set. */
7133 if (show_internal || user_breakpoint_p (&b))
7135 print_one_breakpoint (&b, &last_loc, show_internal);
7136 for (bp_location &loc : b.locations ())
7137 if (loc.disabled_by_cond)
7138 has_disabled_by_cond_location = true;
7143 if (nr_printable_breakpoints == 0)
7145 /* If there's a filter, let the caller decide how to report
7146 empty list. */
7147 if (!filter)
7149 if (bp_num_list == NULL || *bp_num_list == '\0')
7150 uiout->message ("No breakpoints, watchpoints, tracepoints, "
7151 "or catchpoints.\n");
7152 else
7153 uiout->message ("No breakpoint, watchpoint, tracepoint, "
7154 "or catchpoint matching '%s'.\n", bp_num_list);
7157 else
7159 if (last_loc && !server_command)
7160 set_next_address (last_loc->gdbarch, last_loc->address);
7162 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
7163 uiout->message (_("(*): Breakpoint condition is invalid at this "
7164 "location.\n"));
7167 /* FIXME? Should this be moved up so that it is only called when
7168 there have been breakpoints? */
7169 annotate_breakpoints_table_end ();
7171 return nr_printable_breakpoints;
7174 /* Display the value of default-collect in a way that is generally
7175 compatible with the breakpoint list. */
7177 static void
7178 default_collect_info (void)
7180 struct ui_out *uiout = current_uiout;
7182 /* If it has no value (which is frequently the case), say nothing; a
7183 message like "No default-collect." gets in user's face when it's
7184 not wanted. */
7185 if (default_collect.empty ())
7186 return;
7188 /* The following phrase lines up nicely with per-tracepoint collect
7189 actions. */
7190 uiout->text ("default collect ");
7191 uiout->field_string ("default-collect", default_collect);
7192 uiout->text (" \n");
7195 static void
7196 info_breakpoints_command (const char *args, int from_tty)
7198 breakpoint_1 (args, false, NULL);
7200 default_collect_info ();
7203 static void
7204 info_watchpoints_command (const char *args, int from_tty)
7206 int num_printed = breakpoint_1 (args, false, is_watchpoint);
7207 struct ui_out *uiout = current_uiout;
7209 if (num_printed == 0)
7211 if (args == NULL || *args == '\0')
7212 uiout->message ("No watchpoints.\n");
7213 else
7214 uiout->message ("No watchpoint matching '%s'.\n", args);
7218 static void
7219 maintenance_info_breakpoints (const char *args, int from_tty)
7221 breakpoint_1 (args, true, NULL);
7223 default_collect_info ();
7226 static bool
7227 breakpoint_has_pc (struct breakpoint *b,
7228 struct program_space *pspace,
7229 CORE_ADDR pc, struct obj_section *section)
7231 for (bp_location &bl : b->locations ())
7233 if (bl.pspace == pspace
7234 && bl.address == pc
7235 && (!overlay_debugging || bl.section == section))
7236 return true;
7238 return false;
7241 /* See breakpoint.h. */
7243 void
7244 describe_other_breakpoints (struct gdbarch *gdbarch,
7245 struct program_space *pspace, CORE_ADDR pc,
7246 struct obj_section *section, int thread)
7248 int others = 0;
7250 for (breakpoint &b : all_breakpoints ())
7251 others += (user_breakpoint_p (&b)
7252 && breakpoint_has_pc (&b, pspace, pc, section));
7254 if (others > 0)
7256 if (others == 1)
7257 gdb_printf (_("Note: breakpoint "));
7258 else /* if (others == ???) */
7259 gdb_printf (_("Note: breakpoints "));
7260 for (breakpoint &b : all_breakpoints ())
7261 if (user_breakpoint_p (&b)
7262 && breakpoint_has_pc (&b, pspace, pc, section))
7264 others--;
7265 gdb_printf ("%d", b.number);
7266 if (b.thread == -1 && thread != -1)
7267 gdb_printf (" (all threads)");
7268 else if (b.thread != -1)
7270 struct thread_info *thr = find_thread_global_id (b.thread);
7271 gdb_printf (" (thread %s)", print_thread_id (thr));
7273 else if (b.task != -1)
7274 gdb_printf (" (task %d)", b.task);
7275 gdb_printf ("%s%s ",
7276 ((b.enable_state == bp_disabled
7277 || b.enable_state == bp_call_disabled)
7278 ? " (disabled)"
7279 : ""),
7280 (others > 1) ? ","
7281 : ((others == 1) ? " and" : ""));
7283 current_uiout->message (_("also set at pc %ps.\n"),
7284 styled_string (address_style.style (),
7285 paddress (gdbarch, pc)));
7290 /* Return true iff it is meaningful to use the address member of LOC.
7291 For some breakpoint types, the locations' address members are
7292 irrelevant and it makes no sense to attempt to compare them to
7293 other addresses (or use them for any other purpose either).
7295 More specifically, software watchpoints and catchpoints that are
7296 not backed by breakpoints always have a zero valued location
7297 address and we don't want to mark breakpoints of any of these types
7298 to be a duplicate of an actual breakpoint location at address
7299 zero. */
7301 static bool
7302 bl_address_is_meaningful (bp_location *loc)
7304 return loc->loc_type != bp_loc_other;
7307 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7308 true if LOC1 and LOC2 represent the same watchpoint location. */
7310 static bool
7311 watchpoint_locations_match (const struct bp_location *loc1,
7312 const struct bp_location *loc2)
7314 watchpoint *w1 = gdb::checked_static_cast<watchpoint *> (loc1->owner);
7315 watchpoint *w2 = gdb::checked_static_cast<watchpoint *> (loc2->owner);
7317 /* Both of them must exist. */
7318 gdb_assert (w1 != NULL);
7319 gdb_assert (w2 != NULL);
7321 /* If the target can evaluate the condition expression in hardware,
7322 then we we need to insert both watchpoints even if they are at
7323 the same place. Otherwise the watchpoint will only trigger when
7324 the condition of whichever watchpoint was inserted evaluates to
7325 true, not giving a chance for GDB to check the condition of the
7326 other watchpoint. */
7327 if ((w1->cond_exp
7328 && target_can_accel_watchpoint_condition (loc1->address,
7329 loc1->length,
7330 loc1->watchpoint_type,
7331 w1->cond_exp.get ()))
7332 || (w2->cond_exp
7333 && target_can_accel_watchpoint_condition (loc2->address,
7334 loc2->length,
7335 loc2->watchpoint_type,
7336 w2->cond_exp.get ())))
7337 return false;
7339 /* Note that this checks the owner's type, not the location's. In
7340 case the target does not support read watchpoints, but does
7341 support access watchpoints, we'll have bp_read_watchpoint
7342 watchpoints with hw_access locations. Those should be considered
7343 duplicates of hw_read locations. The hw_read locations will
7344 become hw_access locations later. */
7345 return (loc1->owner->type == loc2->owner->type
7346 && loc1->pspace->aspace == loc2->pspace->aspace
7347 && loc1->address == loc2->address
7348 && loc1->length == loc2->length);
7351 /* See breakpoint.h. */
7354 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
7355 const address_space *aspace2, CORE_ADDR addr2)
7357 return ((gdbarch_has_global_breakpoints (current_inferior ()->arch ())
7358 || aspace1 == aspace2)
7359 && addr1 == addr2);
7362 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7363 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7364 matches ASPACE2. On targets that have global breakpoints, the address
7365 space doesn't really matter. */
7367 static bool
7368 breakpoint_address_match_range (const address_space *aspace1,
7369 CORE_ADDR addr1,
7370 int len1, const address_space *aspace2,
7371 CORE_ADDR addr2)
7373 return ((gdbarch_has_global_breakpoints (current_inferior ()->arch ())
7374 || aspace1 == aspace2)
7375 && addr2 >= addr1 && addr2 < addr1 + len1);
7378 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7379 a ranged breakpoint. In most targets, a match happens only if ASPACE
7380 matches the breakpoint's address space. On targets that have global
7381 breakpoints, the address space doesn't really matter. */
7383 static bool
7384 breakpoint_location_address_match (struct bp_location *bl,
7385 const address_space *aspace,
7386 CORE_ADDR addr)
7388 return (breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
7389 aspace, addr)
7390 || (bl->length
7391 && breakpoint_address_match_range (bl->pspace->aspace.get (),
7392 bl->address, bl->length,
7393 aspace, addr)));
7396 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7397 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7398 match happens only if ASPACE matches the breakpoint's address
7399 space. On targets that have global breakpoints, the address space
7400 doesn't really matter. */
7402 static bool
7403 breakpoint_location_address_range_overlap (struct bp_location *bl,
7404 const address_space *aspace,
7405 CORE_ADDR addr, int len)
7407 if (gdbarch_has_global_breakpoints (current_inferior ()->arch ())
7408 || bl->pspace->aspace.get () == aspace)
7410 int bl_len = bl->length != 0 ? bl->length : 1;
7412 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7413 return 1;
7415 return 0;
7418 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7419 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7420 true, otherwise returns false. */
7422 static bool
7423 tracepoint_locations_match (const struct bp_location *loc1,
7424 const struct bp_location *loc2)
7426 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7427 /* Since tracepoint locations are never duplicated with others', tracepoint
7428 locations at the same address of different tracepoints are regarded as
7429 different locations. */
7430 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7431 else
7432 return false;
7435 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7436 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7437 the same location. If SW_HW_BPS_MATCH is true, then software
7438 breakpoint locations and hardware breakpoint locations match,
7439 otherwise they don't. */
7441 static bool
7442 breakpoint_locations_match (const struct bp_location *loc1,
7443 const struct bp_location *loc2,
7444 bool sw_hw_bps_match)
7446 int hw_point1, hw_point2;
7448 /* Both of them must not be in moribund_locations. */
7449 gdb_assert (loc1->owner != NULL);
7450 gdb_assert (loc2->owner != NULL);
7452 hw_point1 = is_hardware_watchpoint (loc1->owner);
7453 hw_point2 = is_hardware_watchpoint (loc2->owner);
7455 if (hw_point1 != hw_point2)
7456 return false;
7457 else if (hw_point1)
7458 return watchpoint_locations_match (loc1, loc2);
7459 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7460 return tracepoint_locations_match (loc1, loc2);
7461 else
7462 /* We compare bp_location.length in order to cover ranged
7463 breakpoints. Keep this in sync with
7464 bp_location_is_less_than. */
7465 return (breakpoint_address_match (loc1->pspace->aspace.get (),
7466 loc1->address,
7467 loc2->pspace->aspace.get (),
7468 loc2->address)
7469 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7470 && loc1->length == loc2->length);
7473 static void
7474 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7475 int bnum, bool have_bnum)
7477 /* The longest string possibly returned by hex_string_custom
7478 is 50 chars. These must be at least that big for safety. */
7479 char astr1[64];
7480 char astr2[64];
7482 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7483 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7484 if (have_bnum)
7485 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7486 bnum, astr1, astr2);
7487 else
7488 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7491 /* Adjust a breakpoint's address to account for architectural
7492 constraints on breakpoint placement. Return the adjusted address.
7493 Note: Very few targets require this kind of adjustment. For most
7494 targets, this function is simply the identity function. */
7496 static CORE_ADDR
7497 adjust_breakpoint_address (struct gdbarch *gdbarch,
7498 CORE_ADDR bpaddr, enum bptype bptype,
7499 struct program_space *pspace)
7501 gdb_assert (pspace != nullptr);
7503 if (bptype == bp_watchpoint
7504 || bptype == bp_hardware_watchpoint
7505 || bptype == bp_read_watchpoint
7506 || bptype == bp_access_watchpoint
7507 || bptype == bp_catchpoint)
7509 /* Watchpoints and the various bp_catch_* eventpoints should not
7510 have their addresses modified. */
7511 return bpaddr;
7513 else if (bptype == bp_single_step)
7515 /* Single-step breakpoints should not have their addresses
7516 modified. If there's any architectural constrain that
7517 applies to this address, then it should have already been
7518 taken into account when the breakpoint was created in the
7519 first place. If we didn't do this, stepping through e.g.,
7520 Thumb-2 IT blocks would break. */
7521 return bpaddr;
7523 else
7525 CORE_ADDR adjusted_bpaddr = bpaddr;
7527 /* Some targets have architectural constraints on the placement
7528 of breakpoint instructions. Obtain the adjusted address. */
7529 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7531 /* Targets that implement this adjustment function will likely
7532 inspect either the symbol table, target memory at BPADDR, or
7533 even state registers, so ensure a suitable thread (and its
7534 associated program space) are currently selected. */
7535 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7536 switch_to_program_space_and_thread (pspace);
7537 adjusted_bpaddr
7538 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7541 adjusted_bpaddr
7542 = gdbarch_remove_non_address_bits_breakpoint (gdbarch, adjusted_bpaddr);
7544 /* An adjusted breakpoint address can significantly alter
7545 a user's expectations. Print a warning if an adjustment
7546 is required. */
7547 if (adjusted_bpaddr != bpaddr)
7548 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, false);
7550 return adjusted_bpaddr;
7554 static bp_loc_type
7555 bp_location_from_bp_type (bptype type)
7557 switch (type)
7559 case bp_breakpoint:
7560 case bp_single_step:
7561 case bp_until:
7562 case bp_finish:
7563 case bp_longjmp:
7564 case bp_longjmp_resume:
7565 case bp_longjmp_call_dummy:
7566 case bp_exception:
7567 case bp_exception_resume:
7568 case bp_step_resume:
7569 case bp_hp_step_resume:
7570 case bp_watchpoint_scope:
7571 case bp_call_dummy:
7572 case bp_std_terminate:
7573 case bp_shlib_event:
7574 case bp_thread_event:
7575 case bp_overlay_event:
7576 case bp_jit_event:
7577 case bp_longjmp_master:
7578 case bp_std_terminate_master:
7579 case bp_exception_master:
7580 case bp_gnu_ifunc_resolver:
7581 case bp_gnu_ifunc_resolver_return:
7582 case bp_dprintf:
7583 return bp_loc_software_breakpoint;
7585 case bp_hardware_breakpoint:
7586 return bp_loc_hardware_breakpoint;
7588 case bp_hardware_watchpoint:
7589 case bp_read_watchpoint:
7590 case bp_access_watchpoint:
7591 return bp_loc_hardware_watchpoint;
7593 case bp_watchpoint:
7594 return bp_loc_software_watchpoint;
7596 case bp_tracepoint:
7597 case bp_fast_tracepoint:
7598 case bp_static_tracepoint:
7599 case bp_static_marker_tracepoint:
7600 return bp_loc_tracepoint;
7602 case bp_catchpoint:
7603 return bp_loc_other;
7605 default:
7606 internal_error (_("unknown breakpoint type"));
7610 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7612 this->owner = owner;
7613 this->cond_bytecode = NULL;
7614 this->shlib_disabled = 0;
7615 this->enabled = 1;
7616 this->disabled_by_cond = false;
7618 this->loc_type = type;
7620 if (this->loc_type == bp_loc_software_breakpoint
7621 || this->loc_type == bp_loc_hardware_breakpoint)
7622 mark_breakpoint_location_modified (this);
7624 incref ();
7627 bp_location::bp_location (breakpoint *owner)
7628 : bp_location::bp_location (owner,
7629 bp_location_from_bp_type (owner->type))
7633 /* See breakpoint.h. */
7635 std::string
7636 bp_location::to_string () const
7638 string_file stb;
7639 ui_out_redirect_pop redir (current_uiout, &stb);
7640 print_breakpoint_location (this->owner, this);
7641 return stb.release ();
7644 /* Decrement reference count. If the reference count reaches 0,
7645 destroy the bp_location. Sets *BLP to NULL. */
7647 static void
7648 decref_bp_location (struct bp_location **blp)
7650 bp_location_ref_policy::decref (*blp);
7651 *blp = NULL;
7654 /* Add breakpoint B at the end of the global breakpoint chain. */
7656 static breakpoint *
7657 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7659 /* Add this breakpoint to the end of the chain so that a list of
7660 breakpoints will come out in order of increasing numbers. */
7662 breakpoint_chain.push_back (*b.release ());
7664 return &breakpoint_chain.back ();
7667 /* Initialize loc->function_name. */
7669 static void
7670 set_breakpoint_location_function (struct bp_location *loc)
7672 gdb_assert (loc->owner != NULL);
7674 if (loc->owner->type == bp_breakpoint
7675 || loc->owner->type == bp_hardware_breakpoint
7676 || is_tracepoint (loc->owner))
7678 const char *function_name;
7680 if (loc->msymbol != NULL
7681 && (loc->msymbol->type () == mst_text_gnu_ifunc
7682 || loc->msymbol->type () == mst_data_gnu_ifunc))
7684 struct breakpoint *b = loc->owner;
7686 function_name = loc->msymbol->linkage_name ();
7688 if (b->type == bp_breakpoint
7689 && b->has_single_location ()
7690 && b->related_breakpoint == b)
7692 /* Create only the whole new breakpoint of this type but do not
7693 mess more complicated breakpoints with multiple locations. */
7694 b->type = bp_gnu_ifunc_resolver;
7695 /* Remember the resolver's address for use by the return
7696 breakpoint. */
7697 loc->related_address = loc->address;
7700 else
7701 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7703 if (function_name)
7704 loc->function_name = make_unique_xstrdup (function_name);
7708 /* Attempt to determine architecture of location identified by SAL. */
7709 struct gdbarch *
7710 get_sal_arch (struct symtab_and_line sal)
7712 if (sal.section)
7713 return sal.section->objfile->arch ();
7714 if (sal.symtab)
7715 return sal.symtab->compunit ()->objfile ()->arch ();
7717 return NULL;
7720 /* Call this routine when stepping and nexting to enable a breakpoint
7721 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7722 initiated the operation. */
7724 void
7725 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7727 int thread = tp->global_num;
7729 /* To avoid having to rescan all objfile symbols at every step,
7730 we maintain a list of continually-inserted but always disabled
7731 longjmp "master" breakpoints. Here, we simply create momentary
7732 clones of those and enable them for the requested thread. */
7733 for (breakpoint &b : all_breakpoints_safe ())
7734 if (b.pspace == current_program_space
7735 && (b.type == bp_longjmp_master
7736 || b.type == bp_exception_master))
7738 bptype type = b.type == bp_longjmp_master ? bp_longjmp : bp_exception;
7739 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7740 after their removal. */
7741 momentary_breakpoint_from_master (&b, type, 1, thread);
7744 tp->initiating_frame = frame;
7747 /* Delete all longjmp breakpoints from THREAD. */
7748 void
7749 delete_longjmp_breakpoint (int thread)
7751 for (breakpoint &b : all_breakpoints_safe ())
7752 if (b.type == bp_longjmp || b.type == bp_exception)
7754 if (b.thread == thread)
7756 gdb_assert (b.inferior == -1);
7757 delete_breakpoint (&b);
7762 void
7763 delete_longjmp_breakpoint_at_next_stop (int thread)
7765 for (breakpoint &b : all_breakpoints_safe ())
7766 if (b.type == bp_longjmp || b.type == bp_exception)
7768 if (b.thread == thread)
7770 gdb_assert (b.inferior == -1);
7771 b.disposition = disp_del_at_next_stop;
7776 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7777 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7778 pointer to any of them. Return NULL if this system cannot place longjmp
7779 breakpoints. */
7781 struct breakpoint *
7782 set_longjmp_breakpoint_for_call_dummy (void)
7784 breakpoint *retval = nullptr;
7786 for (breakpoint &b : all_breakpoints ())
7787 if (b.pspace == current_program_space && b.type == bp_longjmp_master)
7789 int thread = inferior_thread ()->global_num;
7790 breakpoint *new_b
7791 = momentary_breakpoint_from_master (&b, bp_longjmp_call_dummy,
7792 1, thread);
7794 /* Link NEW_B into the chain of RETVAL breakpoints. */
7796 gdb_assert (new_b->related_breakpoint == new_b);
7797 if (retval == NULL)
7798 retval = new_b;
7799 new_b->related_breakpoint = retval;
7800 while (retval->related_breakpoint != new_b->related_breakpoint)
7801 retval = retval->related_breakpoint;
7802 retval->related_breakpoint = new_b;
7805 return retval;
7808 /* Verify all existing dummy frames and their associated breakpoints for
7809 TP. Remove those which can no longer be found in the current frame
7810 stack.
7812 If the unwind fails then there is not sufficient information to discard
7813 dummy frames. In this case, elide the clean up and the dummy frames will
7814 be cleaned up next time this function is called from a location where
7815 unwinding is possible. */
7817 void
7818 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7820 /* We would need to delete breakpoints other than the current one while
7821 iterating, so all_breakpoints_safe is not sufficient to make that safe.
7822 Save all breakpoints to delete in that set and delete them at the end. */
7823 std::unordered_set<breakpoint *> to_delete;
7825 for (struct breakpoint &b : all_breakpoints ())
7827 if (b.type == bp_longjmp_call_dummy && b.thread == tp->global_num)
7829 gdb_assert (b.inferior == -1);
7830 struct breakpoint *dummy_b = b.related_breakpoint;
7832 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7833 chained off b->related_breakpoint. */
7834 while (dummy_b != &b && dummy_b->type != bp_call_dummy)
7835 dummy_b = dummy_b->related_breakpoint;
7837 /* If there was no bp_call_dummy breakpoint then there's nothing
7838 more to do. Or, if the dummy frame associated with the
7839 bp_call_dummy is still on the stack then we need to leave this
7840 bp_call_dummy in place. */
7841 if (dummy_b->type != bp_call_dummy
7842 || frame_find_by_id (dummy_b->frame_id) != NULL)
7843 continue;
7845 /* We didn't find the dummy frame on the stack, this could be
7846 because we have longjmp'd to a stack frame that is previous to
7847 the dummy frame, or it could be because the stack unwind is
7848 broken at some point between the longjmp frame and the dummy
7849 frame.
7851 Next we figure out why the stack unwind stopped. If it looks
7852 like the unwind is complete then we assume the dummy frame has
7853 been jumped over, however, if the unwind stopped for an
7854 unexpected reason then we assume the stack unwind is currently
7855 broken, and that we will (eventually) return to the dummy
7856 frame.
7858 It might be tempting to consider using frame_id_inner here, but
7859 that is not safe. There is no guarantee that the stack frames
7860 we are looking at here are even on the same stack as the
7861 original dummy frame, hence frame_id_inner can't be used. See
7862 the comments on frame_id_inner for more details. */
7863 bool unwind_finished_unexpectedly = false;
7864 for (frame_info_ptr fi = get_current_frame (); fi != nullptr; )
7866 frame_info_ptr prev = get_prev_frame (fi);
7867 if (prev == nullptr)
7869 /* FI is the last stack frame. Why did this frame not
7870 unwind further? */
7871 auto stop_reason = get_frame_unwind_stop_reason (fi);
7872 if (stop_reason != UNWIND_NO_REASON
7873 && stop_reason != UNWIND_OUTERMOST)
7874 unwind_finished_unexpectedly = true;
7876 fi = prev;
7878 if (unwind_finished_unexpectedly)
7879 continue;
7881 dummy_frame_discard (dummy_b->frame_id, tp);
7883 for (breakpoint *related_breakpoint = b.related_breakpoint;
7884 related_breakpoint != &b;
7885 related_breakpoint = related_breakpoint->related_breakpoint)
7886 to_delete.insert (b.related_breakpoint);
7888 to_delete.insert (&b);
7892 for (breakpoint *b : to_delete)
7893 delete_breakpoint (b);
7896 void
7897 enable_overlay_breakpoints (void)
7899 for (breakpoint &b : all_breakpoints ())
7900 if (b.type == bp_overlay_event)
7902 b.enable_state = bp_enabled;
7903 update_global_location_list (UGLL_MAY_INSERT);
7904 overlay_events_enabled = 1;
7908 void
7909 disable_overlay_breakpoints (void)
7911 for (breakpoint &b : all_breakpoints ())
7912 if (b.type == bp_overlay_event)
7914 b.enable_state = bp_disabled;
7915 update_global_location_list (UGLL_DONT_INSERT);
7916 overlay_events_enabled = 0;
7920 /* Set an active std::terminate breakpoint for each std::terminate
7921 master breakpoint. */
7922 void
7923 set_std_terminate_breakpoint (void)
7925 for (breakpoint &b : all_breakpoints_safe ())
7926 if (b.pspace == current_program_space
7927 && b.type == bp_std_terminate_master)
7929 momentary_breakpoint_from_master (&b, bp_std_terminate, 1,
7930 inferior_thread ()->global_num);
7934 /* Delete all the std::terminate breakpoints. */
7935 void
7936 delete_std_terminate_breakpoint (void)
7938 for (breakpoint &b : all_breakpoints_safe ())
7939 if (b.type == bp_std_terminate)
7940 delete_breakpoint (&b);
7943 struct breakpoint *
7944 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7946 struct breakpoint *b;
7948 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
7950 b->enable_state = bp_enabled;
7951 /* locspec has to be used or breakpoint_re_set will delete me. */
7952 b->locspec = new_address_location_spec (b->first_loc ().address, NULL, 0);
7954 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7956 return b;
7959 struct lang_and_radix
7961 enum language lang;
7962 int radix;
7965 /* Create a breakpoint for JIT code registration and unregistration. */
7967 struct breakpoint *
7968 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7970 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
7973 /* Remove JIT code registration and unregistration breakpoint(s). */
7975 void
7976 remove_jit_event_breakpoints (void)
7978 for (breakpoint &b : all_breakpoints_safe ())
7979 if (b.type == bp_jit_event
7980 && b.first_loc ().pspace == current_program_space)
7981 delete_breakpoint (&b);
7984 void
7985 remove_solib_event_breakpoints (void)
7987 for (breakpoint &b : all_breakpoints_safe ())
7988 if (b.type == bp_shlib_event
7989 && b.first_loc ().pspace == current_program_space)
7990 delete_breakpoint (&b);
7993 /* See breakpoint.h. */
7995 void
7996 remove_solib_event_breakpoints_at_next_stop (void)
7998 for (breakpoint &b : all_breakpoints_safe ())
7999 if (b.type == bp_shlib_event
8000 && b.first_loc ().pspace == current_program_space)
8001 b.disposition = disp_del_at_next_stop;
8004 /* Helper for create_solib_event_breakpoint /
8005 create_and_insert_solib_event_breakpoint. Allows specifying which
8006 INSERT_MODE to pass through to update_global_location_list. */
8008 static struct breakpoint *
8009 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
8010 enum ugll_insert_mode insert_mode)
8012 struct breakpoint *b;
8014 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
8015 update_global_location_list_nothrow (insert_mode);
8016 return b;
8019 struct breakpoint *
8020 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
8022 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
8025 /* See breakpoint.h. */
8027 struct breakpoint *
8028 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
8030 struct breakpoint *b;
8032 /* Explicitly tell update_global_location_list to insert
8033 locations. */
8034 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
8035 if (!b->first_loc ().inserted)
8037 delete_breakpoint (b);
8038 return NULL;
8040 return b;
8043 /* See breakpoint.h. */
8045 void
8046 disable_breakpoints_in_shlibs (program_space *pspace)
8048 for (bp_location *loc : all_bp_locations ())
8050 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
8051 struct breakpoint *b = loc->owner;
8053 /* We apply the check to all breakpoints, including disabled for
8054 those with loc->duplicate set. This is so that when breakpoint
8055 becomes enabled, or the duplicate is removed, gdb will try to
8056 insert all breakpoints. If we don't set shlib_disabled here,
8057 we'll try to insert those breakpoints and fail. */
8058 if (((b->type == bp_breakpoint)
8059 || (b->type == bp_jit_event)
8060 || (b->type == bp_hardware_breakpoint)
8061 || (is_tracepoint (b)))
8062 && loc->pspace == pspace
8063 && !loc->shlib_disabled
8064 && solib_name_from_address (loc->pspace, loc->address)
8067 loc->shlib_disabled = 1;
8072 /* Disable any breakpoints and tracepoints that are in SOLIB upon
8073 notification of unloaded_shlib. Only apply to enabled breakpoints,
8074 disabled ones can just stay disabled. */
8076 static void
8077 disable_breakpoints_in_unloaded_shlib (program_space *pspace, const solib &solib)
8079 bool disabled_shlib_breaks = false;
8081 for (bp_location *loc : all_bp_locations ())
8083 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
8084 struct breakpoint *b = loc->owner;
8086 if (pspace == loc->pspace
8087 && !loc->shlib_disabled
8088 && (((b->type == bp_breakpoint
8089 || b->type == bp_jit_event
8090 || b->type == bp_hardware_breakpoint)
8091 && (loc->loc_type == bp_loc_hardware_breakpoint
8092 || loc->loc_type == bp_loc_software_breakpoint))
8093 || is_tracepoint (b))
8094 && solib_contains_address_p (solib, loc->address))
8096 loc->shlib_disabled = 1;
8097 /* At this point, we cannot rely on remove_breakpoint
8098 succeeding so we must mark the breakpoint as not inserted
8099 to prevent future errors occurring in remove_breakpoints. */
8100 loc->inserted = 0;
8102 /* This may cause duplicate notifications for the same breakpoint. */
8103 notify_breakpoint_modified (b);
8105 if (!disabled_shlib_breaks)
8107 target_terminal::ours_for_output ();
8108 warning (_("Temporarily disabling breakpoints "
8109 "for unloaded shared library \"%s\""),
8110 solib.so_name.c_str ());
8112 disabled_shlib_breaks = true;
8117 /* Disable any breakpoints and tracepoints in OBJFILE upon
8118 notification of free_objfile. Only apply to enabled breakpoints,
8119 disabled ones can just stay disabled. */
8121 static void
8122 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
8124 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8125 managed by the user with add-symbol-file/remove-symbol-file.
8126 Similarly to how breakpoints in shared libraries are handled in
8127 response to "nosharedlibrary", mark breakpoints in such modules
8128 shlib_disabled so they end up uninserted on the next global
8129 location list update. Shared libraries not loaded by the user
8130 aren't handled here -- they're already handled in
8131 disable_breakpoints_in_unloaded_shlib, called by solib.c's
8132 solib_unloaded observer. We skip objfiles that are not
8133 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8134 main objfile). */
8135 if ((objfile->flags & OBJF_SHARED) == 0
8136 || (objfile->flags & OBJF_USERLOADED) == 0)
8137 return;
8139 for (breakpoint &b : all_breakpoints ())
8141 bool bp_modified = false;
8143 if (!is_breakpoint (&b) && !is_tracepoint (&b))
8144 continue;
8146 for (bp_location &loc : b.locations ())
8148 CORE_ADDR loc_addr = loc.address;
8150 if (loc.loc_type != bp_loc_hardware_breakpoint
8151 && loc.loc_type != bp_loc_software_breakpoint)
8152 continue;
8154 if (loc.shlib_disabled != 0)
8155 continue;
8157 if (objfile->pspace () != loc.pspace)
8158 continue;
8160 if (is_addr_in_objfile (loc_addr, objfile))
8162 loc.shlib_disabled = 1;
8163 /* At this point, we don't know whether the object was
8164 unmapped from the inferior or not, so leave the
8165 inserted flag alone. We'll handle failure to
8166 uninsert quietly, in case the object was indeed
8167 unmapped. */
8169 mark_breakpoint_location_modified (&loc);
8171 bp_modified = true;
8175 if (bp_modified)
8176 notify_breakpoint_modified (&b);
8180 /* See breakpoint.h. */
8182 breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
8183 bool temp, const char *cond_string_)
8184 : type (bptype),
8185 disposition (temp ? disp_del : disp_donttouch),
8186 gdbarch (gdbarch_),
8187 language (current_language->la_language),
8188 input_radix (::input_radix),
8189 cond_string (cond_string_ != nullptr
8190 ? make_unique_xstrdup (cond_string_)
8191 : nullptr),
8192 related_breakpoint (this)
8196 /* See breakpoint.h. */
8198 catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
8199 const char *cond_string)
8200 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
8202 add_dummy_location (this, current_program_space);
8204 pspace = current_program_space;
8207 /* See breakpoint.h. */
8209 void
8210 catchpoint::re_set (program_space *filter_pspace)
8212 /* All catchpoints are associated with a specific program_space. */
8213 gdb_assert (pspace != nullptr);
8215 /* If only a single program space changed, and it's not the program space
8216 for which this catchpoint applies, then there's nothing to do. */
8217 if (filter_pspace != nullptr && filter_pspace != pspace)
8218 return;
8220 /* Catchpoints have a single dummy location. */
8221 gdb_assert (locations ().size () == 1);
8222 bp_location &bl = m_locations.front ();
8224 if (cond_string == nullptr)
8226 /* It shouldn't be possible to have a parsed condition expression
8227 cached on this location if the catchpoint doesn't have a condition
8228 string set. */
8229 gdb_assert (bl.cond == nullptr);
8231 /* Nothing to re-compute, and the catchpoint cannot change. */
8232 return;
8235 bool previous_disabled_by_cond = bl.disabled_by_cond;
8237 /* Start by marking the location disabled and discarding the previously
8238 computed condition expression. Now if we get an exception, even if
8239 it's a quit exception, we'll leave the location disabled and there
8240 will be no (possibly invalid) expression cached. */
8241 bl.disabled_by_cond = true;
8242 bl.cond = nullptr;
8244 const char *s = cond_string.get ();
8247 switch_to_program_space_and_thread (pspace);
8249 bl.cond = parse_exp_1 (&s, bl.address, block_for_pc (bl.address),
8250 nullptr);
8251 bl.disabled_by_cond = false;
8253 catch (const gdb_exception_error &e)
8255 /* Any exception thrown must be from either the parse_exp_1 or
8256 earlier in the try block. As such the following two asserts
8257 should be true. */
8258 gdb_assert (bl.disabled_by_cond);
8259 gdb_assert (bl.cond == nullptr);
8262 if (previous_disabled_by_cond != bl.disabled_by_cond)
8263 notify_breakpoint_modified (this);
8266 /* Notify interpreters and observers that breakpoint B was created. */
8268 static void
8269 notify_breakpoint_created (breakpoint *b)
8271 interps_notify_breakpoint_created (b);
8272 gdb::observers::breakpoint_created.notify (b);
8275 breakpoint *
8276 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8278 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8279 set_breakpoint_number (internal, b);
8280 if (is_tracepoint (b))
8281 set_tracepoint_count (breakpoint_count);
8282 if (!internal)
8283 mention (b);
8285 notify_breakpoint_created (b);
8287 if (update_gll)
8288 update_global_location_list (UGLL_MAY_INSERT);
8290 return b;
8293 static int
8294 hw_breakpoint_used_count (void)
8296 int i = 0;
8298 for (breakpoint &b : all_breakpoints ())
8299 if (b.type == bp_hardware_breakpoint && breakpoint_enabled (&b))
8300 for (bp_location &bl : b.locations ())
8302 /* Special types of hardware breakpoints may use more than
8303 one register. */
8304 i += b.resources_needed (&bl);
8307 return i;
8310 /* Returns the resources B would use if it were a hardware
8311 watchpoint. */
8313 static int
8314 hw_watchpoint_use_count (struct breakpoint *b)
8316 int i = 0;
8318 if (!breakpoint_enabled (b))
8319 return 0;
8321 for (bp_location &bl : b->locations ())
8323 /* Special types of hardware watchpoints may use more than
8324 one register. */
8325 i += b->resources_needed (&bl);
8328 return i;
8331 /* Returns the sum the used resources of all hardware watchpoints of
8332 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8333 the sum of the used resources of all hardware watchpoints of other
8334 types _not_ TYPE. */
8336 static int
8337 hw_watchpoint_used_count_others (struct breakpoint *except,
8338 enum bptype type, int *other_type_used)
8340 int i = 0;
8342 *other_type_used = 0;
8343 for (breakpoint &b : all_breakpoints ())
8345 if (&b == except)
8346 continue;
8347 if (!breakpoint_enabled (&b))
8348 continue;
8350 if (b.type == type)
8351 i += hw_watchpoint_use_count (&b);
8352 else if (is_hardware_watchpoint (&b))
8353 *other_type_used = 1;
8356 return i;
8359 void
8360 disable_watchpoints_before_interactive_call_start (void)
8362 for (breakpoint &b : all_breakpoints ())
8363 if (is_watchpoint (&b) && breakpoint_enabled (&b))
8365 b.enable_state = bp_call_disabled;
8366 update_global_location_list (UGLL_DONT_INSERT);
8370 void
8371 enable_watchpoints_after_interactive_call_stop (void)
8373 for (breakpoint &b : all_breakpoints ())
8374 if (is_watchpoint (&b) && b.enable_state == bp_call_disabled)
8376 b.enable_state = bp_enabled;
8377 update_global_location_list (UGLL_MAY_INSERT);
8381 void
8382 disable_breakpoints_before_startup (void)
8384 current_program_space->executing_startup = 1;
8385 update_global_location_list (UGLL_DONT_INSERT);
8388 void
8389 enable_breakpoints_after_startup (void)
8391 current_program_space->executing_startup = 0;
8392 breakpoint_re_set ();
8395 /* Allocate a new momentary breakpoint. */
8397 template<typename... Arg>
8398 static momentary_breakpoint *
8399 new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
8400 Arg&&... args)
8402 if (type == bp_longjmp || type == bp_exception)
8403 return new longjmp_breakpoint (gdbarch, type,
8404 std::forward<Arg> (args)...);
8405 else
8406 return new momentary_breakpoint (gdbarch, type,
8407 std::forward<Arg> (args)...);
8410 /* Set a momentary breakpoint of type TYPE at address specified by
8411 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8412 frame. */
8414 breakpoint_up
8415 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8416 struct frame_id frame_id, enum bptype type)
8418 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8419 tail-called one. */
8420 gdb_assert (!frame_id_artificial_p (frame_id));
8422 std::unique_ptr<momentary_breakpoint> b
8423 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
8424 inferior_thread ()->global_num));
8426 b->add_location (sal);
8428 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
8430 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8432 return bp;
8435 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8436 The new breakpoint will have type TYPE, use OPS as its
8437 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8439 static struct breakpoint *
8440 momentary_breakpoint_from_master (struct breakpoint *orig,
8441 enum bptype type,
8442 int loc_enabled,
8443 int thread)
8445 std::unique_ptr<breakpoint> copy
8446 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
8447 orig->frame_id, thread));
8448 const bp_location &orig_loc = orig->first_loc ();
8449 bp_location *copy_loc = copy->allocate_location ();
8450 copy->add_location (*copy_loc);
8451 set_breakpoint_location_function (copy_loc);
8453 copy_loc->gdbarch = orig_loc.gdbarch;
8454 copy_loc->requested_address = orig_loc.requested_address;
8455 copy_loc->address = orig_loc.address;
8456 copy_loc->section = orig_loc.section;
8457 copy_loc->pspace = orig_loc.pspace;
8458 copy_loc->probe = orig_loc.probe;
8459 copy_loc->line_number = orig_loc.line_number;
8460 copy_loc->symtab = orig_loc.symtab;
8461 copy_loc->enabled = loc_enabled;
8463 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
8464 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8465 return b;
8468 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8469 ORIG is NULL. */
8471 struct breakpoint *
8472 clone_momentary_breakpoint (struct breakpoint *orig)
8474 /* If there's nothing to clone, then return nothing. */
8475 if (orig == NULL)
8476 return NULL;
8478 return momentary_breakpoint_from_master (orig, orig->type, 0,
8479 orig->thread);
8482 breakpoint_up
8483 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8484 enum bptype type)
8486 struct symtab_and_line sal;
8488 sal = find_pc_line (pc, 0);
8489 sal.pc = pc;
8490 sal.section = find_pc_overlay (pc);
8491 sal.explicit_pc = 1;
8493 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8497 /* Tell the user we have just set a breakpoint B. */
8499 static void
8500 mention (const breakpoint *b)
8502 b->print_mention ();
8503 current_uiout->text ("\n");
8507 static bool bp_loc_is_permanent (struct bp_location *loc);
8509 /* Handle "set breakpoint auto-hw on".
8511 If the explicitly specified breakpoint type is not hardware
8512 breakpoint, check the memory map to see whether the breakpoint
8513 address is in read-only memory.
8515 - location type is not hardware breakpoint, memory is read-only.
8516 We change the type of the location to hardware breakpoint.
8518 - location type is hardware breakpoint, memory is read-write. This
8519 means we've previously made the location hardware one, but then the
8520 memory map changed, so we undo.
8523 static void
8524 handle_automatic_hardware_breakpoints (bp_location *bl)
8526 if (automatic_hardware_breakpoints
8527 && bl->owner->type != bp_hardware_breakpoint
8528 && (bl->loc_type == bp_loc_software_breakpoint
8529 || bl->loc_type == bp_loc_hardware_breakpoint))
8531 /* When breakpoints are removed, remove_breakpoints will use
8532 location types we've just set here, the only possible problem
8533 is that memory map has changed during running program, but
8534 it's not going to work anyway with current gdb. */
8535 mem_region *mr = lookup_mem_region (bl->address);
8537 if (mr != nullptr)
8539 enum bp_loc_type new_type;
8541 if (mr->attrib.mode != MEM_RW)
8542 new_type = bp_loc_hardware_breakpoint;
8543 else
8544 new_type = bp_loc_software_breakpoint;
8546 if (new_type != bl->loc_type)
8548 static bool said = false;
8550 bl->loc_type = new_type;
8551 if (!said)
8553 gdb_printf (_("Note: automatically using "
8554 "hardware breakpoints for "
8555 "read-only addresses.\n"));
8556 said = true;
8563 bp_location *
8564 code_breakpoint::add_location (const symtab_and_line &sal)
8566 CORE_ADDR adjusted_address;
8567 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8569 if (loc_gdbarch == NULL)
8570 loc_gdbarch = gdbarch;
8572 /* Adjust the breakpoint's address prior to allocating a location.
8573 Once we call allocate_location(), that mostly uninitialized
8574 location will be placed on the location chain. Adjustment of the
8575 breakpoint may cause target_read_memory() to be called and we do
8576 not want its scan of the location chain to find a breakpoint and
8577 location that's only been partially initialized. */
8578 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8579 sal.pc, type,
8580 sal.pspace);
8582 /* Sort the locations by their ADDRESS. */
8583 bp_location *new_loc = this->allocate_location ();
8585 new_loc->requested_address = sal.pc;
8586 new_loc->address = adjusted_address;
8587 new_loc->pspace = sal.pspace;
8588 new_loc->probe.prob = sal.prob;
8589 new_loc->probe.objfile = sal.objfile;
8590 gdb_assert (new_loc->pspace != NULL);
8591 new_loc->section = sal.section;
8592 new_loc->gdbarch = loc_gdbarch;
8593 new_loc->line_number = sal.line;
8594 new_loc->symtab = sal.symtab;
8595 new_loc->symbol = sal.symbol;
8596 new_loc->msymbol = sal.msymbol;
8597 new_loc->objfile = sal.objfile;
8599 breakpoint::add_location (*new_loc);
8601 set_breakpoint_location_function (new_loc);
8603 /* While by definition, permanent breakpoints are already present in the
8604 code, we don't mark the location as inserted. Normally one would expect
8605 that GDB could rely on that breakpoint instruction to stop the program,
8606 thus removing the need to insert its own breakpoint, except that executing
8607 the breakpoint instruction can kill the target instead of reporting a
8608 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8609 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8610 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8611 breakpoint be inserted normally results in QEMU knowing about the GDB
8612 breakpoint, and thus trap before the breakpoint instruction is executed.
8613 (If GDB later needs to continue execution past the permanent breakpoint,
8614 it manually increments the PC, thus avoiding executing the breakpoint
8615 instruction.) */
8616 if (bp_loc_is_permanent (new_loc))
8617 new_loc->permanent = 1;
8619 return new_loc;
8623 /* Return true if LOC is pointing to a permanent breakpoint,
8624 return false otherwise. */
8626 static bool
8627 bp_loc_is_permanent (struct bp_location *loc)
8629 gdb_assert (loc != NULL);
8631 /* If we have a non-breakpoint-backed catchpoint or a software
8632 watchpoint, just return 0. We should not attempt to read from
8633 the addresses the locations of these breakpoint types point to.
8634 gdbarch_program_breakpoint_here_p, below, will attempt to read
8635 memory. */
8636 if (!bl_address_is_meaningful (loc))
8637 return false;
8639 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8640 switch_to_program_space_and_thread (loc->pspace);
8641 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8644 /* Build a command list for the dprintf corresponding to the current
8645 settings of the dprintf style options. */
8647 static void
8648 update_dprintf_command_list (struct breakpoint *b)
8650 gdb_assert (b->type == bp_dprintf);
8651 gdb_assert (b->extra_string != nullptr);
8653 const char *dprintf_args = b->extra_string.get ();
8654 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8656 /* Trying to create a dprintf breakpoint without a format and args
8657 string should be detected at creation time. */
8658 gdb_assert (dprintf_args != nullptr);
8660 dprintf_args = skip_spaces (dprintf_args);
8662 /* Allow a comma, as it may have terminated a location, but don't
8663 insist on it. */
8664 if (*dprintf_args == ',')
8665 ++dprintf_args;
8666 dprintf_args = skip_spaces (dprintf_args);
8668 if (*dprintf_args != '"')
8669 error (_("Bad format string, missing '\"'."));
8671 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8672 printf_line = xstrprintf ("printf %s", dprintf_args);
8673 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8675 if (dprintf_function.empty ())
8676 error (_("No function supplied for dprintf call"));
8678 if (!dprintf_channel.empty ())
8679 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8680 dprintf_function.c_str (),
8681 dprintf_channel.c_str (),
8682 dprintf_args);
8683 else
8684 printf_line = xstrprintf ("call (void) %s (%s)",
8685 dprintf_function.c_str (),
8686 dprintf_args);
8688 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8690 if (target_can_run_breakpoint_commands ())
8691 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8692 else
8694 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8695 printf_line = xstrprintf ("printf %s", dprintf_args);
8698 else
8699 internal_error (_("Invalid dprintf style."));
8701 gdb_assert (printf_line != NULL);
8703 /* Manufacture a printf sequence. */
8704 struct command_line *printf_cmd_line
8705 = new struct command_line (simple_control, printf_line.release ());
8706 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8707 command_lines_deleter ()));
8710 /* Update all dprintf commands, making their command lists reflect
8711 current style settings. */
8713 static void
8714 update_dprintf_commands (const char *args, int from_tty,
8715 struct cmd_list_element *c)
8717 for (breakpoint &b : all_breakpoints ())
8718 if (b.type == bp_dprintf)
8719 update_dprintf_command_list (&b);
8722 code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
8723 enum bptype type_,
8724 gdb::array_view<const symtab_and_line> sals,
8725 location_spec_up &&locspec_,
8726 gdb::unique_xmalloc_ptr<char> filter_,
8727 gdb::unique_xmalloc_ptr<char> cond_string_,
8728 gdb::unique_xmalloc_ptr<char> extra_string_,
8729 enum bpdisp disposition_,
8730 int thread_, int task_, int inferior_,
8731 int ignore_count_,
8732 int from_tty,
8733 int enabled_, unsigned flags,
8734 int display_canonical_)
8735 : breakpoint (gdbarch_, type_)
8737 int i;
8739 if (type == bp_hardware_breakpoint)
8741 int target_resources_ok;
8743 i = hw_breakpoint_used_count ();
8744 target_resources_ok =
8745 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8746 i + 1, 0);
8747 if (target_resources_ok == 0)
8748 error (_("No hardware breakpoint support in the target."));
8749 else if (target_resources_ok < 0)
8750 error (_("Hardware breakpoints used exceeds limit."));
8753 gdb_assert (!sals.empty ());
8755 /* At most one of thread, task, or inferior can be set on any breakpoint. */
8756 gdb_assert (((thread == -1 ? 0 : 1)
8757 + (task == -1 ? 0 : 1)
8758 + (inferior == -1 ? 0 : 1)) <= 1);
8760 thread = thread_;
8761 task = task_;
8762 inferior = inferior_;
8764 cond_string = std::move (cond_string_);
8765 extra_string = std::move (extra_string_);
8766 ignore_count = ignore_count_;
8767 enable_state = enabled_ ? bp_enabled : bp_disabled;
8768 disposition = disposition_;
8770 if (type == bp_static_tracepoint
8771 || type == bp_static_marker_tracepoint)
8773 auto *t = gdb::checked_static_cast<tracepoint *> (this);
8774 struct static_tracepoint_marker marker;
8776 if (strace_marker_p (this))
8778 /* We already know the marker exists, otherwise, we wouldn't
8779 see a sal for it. */
8780 const char *p = &locspec_->to_string ()[3];
8781 const char *endp;
8783 p = skip_spaces (p);
8785 endp = skip_to_space (p);
8787 t->static_trace_marker_id.assign (p, endp - p);
8789 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8790 t->static_trace_marker_id.c_str ());
8792 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8794 t->static_trace_marker_id = std::move (marker.str_id);
8796 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8797 t->static_trace_marker_id.c_str ());
8799 else
8800 warning (_("Couldn't determine the static tracepoint marker to probe"));
8803 for (const auto &sal : sals)
8805 if (from_tty)
8807 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8808 if (loc_gdbarch == nullptr)
8809 loc_gdbarch = gdbarch;
8811 describe_other_breakpoints (loc_gdbarch,
8812 sal.pspace, sal.pc, sal.section, thread);
8815 bp_location *new_loc = add_location (sal);
8816 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8817 new_loc->inserted = 1;
8819 /* Do not set breakpoint locations conditions yet. As locations
8820 are inserted, they get sorted based on their addresses. Let
8821 the list stabilize to have reliable location numbers. */
8824 /* Dynamic printf requires and uses additional arguments on the
8825 command line, otherwise it's an error. */
8826 if (type == bp_dprintf)
8827 update_dprintf_command_list (this);
8828 else
8829 gdb_assert (extra_string == nullptr);
8831 /* The order of the locations is now stable. Set the location
8832 condition using the location's number. */
8833 int loc_num = 1;
8834 for (bp_location &bl : locations ())
8836 if (cond_string != nullptr)
8837 set_breakpoint_location_condition (cond_string.get (), &bl,
8838 number, loc_num);
8840 ++loc_num;
8843 display_canonical = display_canonical_;
8844 if (locspec_ != nullptr)
8845 locspec = std::move (locspec_);
8846 else
8847 locspec = new_address_location_spec (this->first_loc ().address, NULL, 0);
8848 filter = std::move (filter_);
8851 static void
8852 create_breakpoint_sal (struct gdbarch *gdbarch,
8853 gdb::array_view<const symtab_and_line> sals,
8854 location_spec_up &&locspec,
8855 gdb::unique_xmalloc_ptr<char> filter,
8856 gdb::unique_xmalloc_ptr<char> cond_string,
8857 gdb::unique_xmalloc_ptr<char> extra_string,
8858 enum bptype type, enum bpdisp disposition,
8859 int thread, int task, int inferior, int ignore_count,
8860 int from_tty,
8861 int enabled, int internal, unsigned flags,
8862 int display_canonical)
8864 std::unique_ptr<code_breakpoint> b
8865 = new_breakpoint_from_type (gdbarch,
8866 type,
8867 sals,
8868 std::move (locspec),
8869 std::move (filter),
8870 std::move (cond_string),
8871 std::move (extra_string),
8872 disposition,
8873 thread, task, inferior, ignore_count,
8874 from_tty,
8875 enabled, flags,
8876 display_canonical);
8878 install_breakpoint (internal, std::move (b), 0);
8881 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8882 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8883 value. COND_STRING, if not NULL, specified the condition to be
8884 used for all breakpoints. Essentially the only case where
8885 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8886 function. In that case, it's still not possible to specify
8887 separate conditions for different overloaded functions, so
8888 we take just a single condition string.
8890 NOTE: If the function succeeds, the caller is expected to cleanup
8891 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8892 array contents). If the function fails (error() is called), the
8893 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8894 COND and SALS arrays and each of those arrays contents. */
8896 static void
8897 create_breakpoints_sal (struct gdbarch *gdbarch,
8898 struct linespec_result *canonical,
8899 gdb::unique_xmalloc_ptr<char> cond_string,
8900 gdb::unique_xmalloc_ptr<char> extra_string,
8901 enum bptype type, enum bpdisp disposition,
8902 int thread, int task, int inferior,
8903 int ignore_count,
8904 int from_tty,
8905 int enabled, int internal, unsigned flags)
8907 if (canonical->pre_expanded)
8908 gdb_assert (canonical->lsals.size () == 1);
8910 for (const auto &lsal : canonical->lsals)
8912 /* Note that 'location' can be NULL in the case of a plain
8913 'break', without arguments. */
8914 location_spec_up locspec
8915 = (canonical->locspec != nullptr
8916 ? canonical->locspec->clone ()
8917 : nullptr);
8918 gdb::unique_xmalloc_ptr<char> filter_string
8919 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8921 create_breakpoint_sal (gdbarch, lsal.sals,
8922 std::move (locspec),
8923 std::move (filter_string),
8924 std::move (cond_string),
8925 std::move (extra_string),
8926 type, disposition,
8927 thread, task, inferior, ignore_count,
8928 from_tty, enabled, internal, flags,
8929 canonical->special_display);
8933 /* Parse LOCSPEC which is assumed to be a SAL specification possibly
8934 followed by conditionals. On return, SALS contains an array of SAL
8935 addresses found. LOCSPEC points to the end of the SAL (for
8936 linespec locspecs).
8938 The array and the line spec strings are allocated on the heap, it is
8939 the caller's responsibility to free them. */
8941 static void
8942 parse_breakpoint_sals (location_spec *locspec,
8943 struct linespec_result *canonical,
8944 struct program_space *search_pspace)
8946 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8948 const char *spec
8949 = as_linespec_location_spec (locspec)->spec_string.get ();
8951 if (spec == NULL)
8953 /* The last displayed codepoint, if it's valid, is our default
8954 breakpoint address. */
8955 if (last_displayed_sal_is_valid ())
8957 /* Set sal's pspace, pc, symtab, and line to the values
8958 corresponding to the last call to print_frame_info.
8959 Be sure to reinitialize LINE with NOTCURRENT == 0
8960 as the breakpoint line number is inappropriate otherwise.
8961 find_pc_line would adjust PC, re-set it back. */
8962 symtab_and_line sal = get_last_displayed_sal ();
8963 CORE_ADDR pc = sal.pc;
8965 sal = find_pc_line (pc, 0);
8967 /* "break" without arguments is equivalent to "break *PC"
8968 where PC is the last displayed codepoint's address. So
8969 make sure to set sal.explicit_pc to prevent GDB from
8970 trying to expand the list of sals to include all other
8971 instances with the same symtab and line. */
8972 sal.pc = pc;
8973 sal.explicit_pc = 1;
8975 struct linespec_sals lsal;
8976 lsal.sals = {sal};
8977 lsal.canonical = NULL;
8979 canonical->lsals.push_back (std::move (lsal));
8980 return;
8982 else
8983 error (_("No default breakpoint address now."));
8987 /* Force almost all breakpoints to be in terms of the
8988 current_source_symtab (which is decode_line_1's default).
8989 This should produce the results we want almost all of the
8990 time while leaving default_breakpoint_* alone.
8992 ObjC: However, don't match an Objective-C method name which
8993 may have a '+' or '-' succeeded by a '['. */
8994 symtab_and_line cursal
8995 = get_current_source_symtab_and_line (current_program_space);
8996 if (last_displayed_sal_is_valid ())
8998 const char *spec = NULL;
9000 if (locspec->type () == LINESPEC_LOCATION_SPEC)
9001 spec = as_linespec_location_spec (locspec)->spec_string.get ();
9003 if (!cursal.symtab
9004 || (spec != NULL
9005 && strchr ("+-", spec[0]) != NULL
9006 && spec[1] != '['))
9008 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
9009 get_last_displayed_symtab (),
9010 get_last_displayed_line (),
9011 canonical, NULL, NULL);
9012 return;
9016 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
9017 cursal.symtab, cursal.line, canonical, NULL, NULL);
9021 /* Convert each SAL into a real PC. Verify that the PC can be
9022 inserted as a breakpoint. If it can't throw an error. */
9024 static void
9025 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9027 for (auto &sal : sals)
9028 resolve_sal_pc (&sal);
9031 /* Fast tracepoints may have restrictions on valid locations. For
9032 instance, a fast tracepoint using a jump instead of a trap will
9033 likely have to overwrite more bytes than a trap would, and so can
9034 only be placed where the instruction is longer than the jump, or a
9035 multi-instruction sequence does not have a jump into the middle of
9036 it, etc. */
9038 static void
9039 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9040 gdb::array_view<const symtab_and_line> sals)
9042 for (const auto &sal : sals)
9044 struct gdbarch *sarch;
9046 sarch = get_sal_arch (sal);
9047 /* We fall back to GDBARCH if there is no architecture
9048 associated with SAL. */
9049 if (sarch == NULL)
9050 sarch = gdbarch;
9051 std::string msg;
9052 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9053 error (_("May not have a fast tracepoint at %s%s"),
9054 paddress (sarch, sal.pc), msg.c_str ());
9058 /* Decode a static tracepoint marker spec. */
9060 static std::vector<symtab_and_line>
9061 decode_static_tracepoint_spec (const char **arg_p)
9063 const char *p = &(*arg_p)[3];
9064 const char *endp;
9066 p = skip_spaces (p);
9068 endp = skip_to_space (p);
9070 std::string marker_str (p, endp - p);
9072 std::vector<static_tracepoint_marker> markers
9073 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9074 if (markers.empty ())
9075 error (_("No known static tracepoint marker named %s"),
9076 marker_str.c_str ());
9078 std::vector<symtab_and_line> sals;
9079 sals.reserve (markers.size ());
9081 for (const static_tracepoint_marker &marker : markers)
9083 symtab_and_line sal = find_pc_line (marker.address, 0);
9084 sal.pc = marker.address;
9085 sals.push_back (sal);
9088 *arg_p = endp;
9089 return sals;
9092 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9093 according to IS_TRACEPOINT. */
9095 static const struct breakpoint_ops *
9096 breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
9097 bool is_tracepoint)
9099 if (is_tracepoint)
9101 if (locspec_type == PROBE_LOCATION_SPEC)
9102 return &tracepoint_probe_breakpoint_ops;
9103 else
9104 return &code_breakpoint_ops;
9106 else
9108 if (locspec_type == PROBE_LOCATION_SPEC)
9109 return &bkpt_probe_breakpoint_ops;
9110 else
9111 return &code_breakpoint_ops;
9115 /* Return the program space to use as a filter when searching for locations
9116 of a breakpoint specific to THREAD or INFERIOR. If THREAD and INFERIOR
9117 are both -1, meaning all threads/inferiors, then this function returns
9118 nullptr, indicating no program space filtering should be performed.
9119 Otherwise, this function returns the program space for the inferior that
9120 contains THREAD (when THREAD is not -1), or the program space for
9121 INFERIOR (when INFERIOR is not -1). */
9123 static struct program_space *
9124 find_program_space_for_breakpoint (int thread, int inferior)
9126 if (thread != -1)
9128 gdb_assert (inferior == -1);
9130 struct thread_info *thr = find_thread_global_id (thread);
9131 gdb_assert (thr != nullptr);
9132 gdb_assert (thr->inf != nullptr);
9133 return thr->inf->pspace;
9135 else if (inferior != -1)
9137 gdb_assert (thread == -1);
9139 struct inferior *inf = find_inferior_id (inferior);
9140 gdb_assert (inf != nullptr);
9142 return inf->pspace;
9145 return nullptr;
9148 /* See breakpoint.h. */
9150 const struct breakpoint_ops *
9151 breakpoint_ops_for_location_spec (const location_spec *locspec,
9152 bool is_tracepoint)
9154 if (locspec != nullptr)
9155 return (breakpoint_ops_for_location_spec_type
9156 (locspec->type (), is_tracepoint));
9157 return &code_breakpoint_ops;
9160 /* See breakpoint.h. */
9163 create_breakpoint (struct gdbarch *gdbarch,
9164 location_spec *locspec,
9165 const char *cond_string,
9166 int thread, int inferior,
9167 const char *extra_string,
9168 bool force_condition, int parse_extra,
9169 int tempflag, enum bptype type_wanted,
9170 int ignore_count,
9171 enum auto_boolean pending_break_support,
9172 const struct breakpoint_ops *ops,
9173 int from_tty, int enabled, int internal,
9174 unsigned flags)
9176 struct linespec_result canonical;
9177 bool pending = false;
9178 int task = -1;
9179 int prev_bkpt_count = breakpoint_count;
9181 gdb_assert (thread == -1 || thread > 0);
9182 gdb_assert (inferior == -1 || inferior > 0);
9183 gdb_assert (thread == -1 || inferior == -1);
9185 /* If PARSE_EXTRA is true then the thread and inferior details will be
9186 parsed from the EXTRA_STRING, the THREAD and INFERIOR arguments
9187 should be -1. */
9188 gdb_assert (!parse_extra || thread == -1);
9189 gdb_assert (!parse_extra || inferior == -1);
9191 gdb_assert (ops != NULL);
9193 /* If extra_string isn't useful, set it to NULL. */
9194 if (extra_string != NULL && *extra_string == '\0')
9195 extra_string = NULL;
9197 /* A bp_dprintf must always have an accompanying EXTRA_STRING containing
9198 the dprintf format and arguments -- PARSE_EXTRA should always be false
9199 in this case.
9201 For all other breakpoint types, EXTRA_STRING should be nullptr unless
9202 PARSE_EXTRA is true. */
9203 gdb_assert ((type_wanted == bp_dprintf)
9204 ? (extra_string != nullptr && !parse_extra)
9205 : (extra_string == nullptr || parse_extra));
9207 /* Will hold either copies of the similarly named function argument, or
9208 will hold a modified version of the function argument, depending on
9209 the value of PARSE_EXTRA. */
9210 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9211 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9213 if (parse_extra)
9215 /* Parse EXTRA_STRING splitting the parts out. */
9216 create_breakpoint_parse_arg_string (extra_string, &cond_string_copy,
9217 &thread, &inferior, &task,
9218 &extra_string_copy,
9219 &force_condition);
9221 /* We could check that EXTRA_STRING_COPY is empty at this point -- it
9222 should be, as we only get here for things that are not bp_dprintf,
9223 however, we prefer to give the location spec parser a chance to
9224 run first, this means the user will get errors about invalid
9225 location spec instead of an error about garbage at the end of the
9226 command line.
9228 We still do the EXTRA_STRING_COPY is empty check, just later in
9229 this function. */
9231 gdb_assert (thread == -1 || thread > 0);
9232 gdb_assert (task == -1 || task > 0);
9233 gdb_assert (inferior == -1 || inferior > 0);
9235 else
9237 if (cond_string != nullptr)
9238 cond_string_copy.reset (xstrdup (cond_string));
9239 if (extra_string != nullptr)
9240 extra_string_copy.reset (xstrdup (extra_string));
9243 /* Clear these. Updated values are now held in the *_copy locals. */
9244 cond_string = nullptr;
9245 extra_string = nullptr;
9249 struct program_space *search_pspace
9250 = find_program_space_for_breakpoint (thread, inferior);
9251 ops->create_sals_from_location_spec (locspec, &canonical,
9252 search_pspace);
9254 catch (const gdb_exception_error &e)
9256 /* If caller is interested in rc value from parse, set
9257 value. */
9258 if (e.error == NOT_FOUND_ERROR)
9260 /* If pending breakpoint support is turned off, throw
9261 error. */
9263 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9264 throw;
9266 exception_print (gdb_stderr, e);
9268 /* If pending breakpoint support is auto query and the user
9269 selects no, then simply return the error code. */
9270 if (pending_break_support == AUTO_BOOLEAN_AUTO
9271 && !nquery (_("Make %s pending on future shared library load? "),
9272 bptype_string (type_wanted)))
9273 return 0;
9275 /* At this point, either the user was queried about setting
9276 a pending breakpoint and selected yes, or pending
9277 breakpoint behavior is on and thus a pending breakpoint
9278 is defaulted on behalf of the user. */
9279 pending = true;
9281 else
9282 throw;
9285 /* Only bp_dprintf breakpoints should have anything in EXTRA_STRING_COPY
9286 by this point. For all other breakpoints this indicates an error. We
9287 could place this check earlier in the function, but we prefer to see
9288 errors from the location spec parser before we see this error message. */
9289 if (type_wanted != bp_dprintf && extra_string_copy.get () != nullptr)
9290 error (_("Garbage '%s' at end of command"), extra_string_copy.get ());
9292 if (!pending && canonical.lsals.empty ())
9293 return 0;
9295 /* Resolve all line numbers to PC's and verify that the addresses
9296 are ok for the target. */
9297 if (!pending)
9299 for (auto &lsal : canonical.lsals)
9300 breakpoint_sals_to_pc (lsal.sals);
9303 /* Fast tracepoints may have additional restrictions on location. */
9304 if (!pending && type_wanted == bp_fast_tracepoint)
9306 for (const auto &lsal : canonical.lsals)
9307 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9310 /* Verify that condition can be parsed, before setting any
9311 breakpoints. Allocate a separate condition expression for each
9312 breakpoint. */
9313 if (!pending)
9315 /* Check the validity of the condition. We should error out if the
9316 condition is invalid at all of the locations and if it is not
9317 forced. In the PARSE_EXTRA case above, this check is done when
9318 parsing the EXTRA_STRING. */
9319 if (cond_string_copy.get () != nullptr && !force_condition)
9321 int num_failures = 0;
9322 const linespec_sals &lsal = canonical.lsals[0];
9323 for (const auto &sal : lsal.sals)
9325 const char *cond = cond_string_copy.get ();
9328 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9329 /* One success is sufficient to keep going. */
9330 break;
9332 catch (const gdb_exception_error &)
9334 num_failures++;
9335 /* If this is the last sal, error out. */
9336 if (num_failures == lsal.sals.size ())
9337 throw;
9342 ops->create_breakpoints_sal (gdbarch, &canonical,
9343 std::move (cond_string_copy),
9344 std::move (extra_string_copy),
9345 type_wanted,
9346 tempflag ? disp_del : disp_donttouch,
9347 thread, task, inferior, ignore_count,
9348 from_tty, enabled, internal, flags);
9350 else
9352 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
9353 type_wanted);
9354 b->locspec = locspec->clone ();
9356 /* Create a private copy of the condition string. */
9357 b->cond_string = std::move (cond_string_copy);
9359 b->thread = thread;
9360 b->task = task;
9361 b->inferior = inferior;
9363 /* Create a private copy of any extra string. */
9364 b->extra_string = std::move (extra_string_copy);
9366 b->ignore_count = ignore_count;
9367 b->disposition = tempflag ? disp_del : disp_donttouch;
9368 b->condition_not_parsed = 1;
9369 b->enable_state = enabled ? bp_enabled : bp_disabled;
9371 if (b->type == bp_dprintf)
9372 update_dprintf_command_list (b.get ());
9374 install_breakpoint (internal, std::move (b), 0);
9377 if (canonical.lsals.size () > 1)
9379 warning (_("Multiple breakpoints were set.\nUse the "
9380 "\"delete\" command to delete unwanted breakpoints."));
9381 prev_breakpoint_count = prev_bkpt_count;
9384 update_global_location_list (UGLL_MAY_INSERT);
9386 return 1;
9389 /* Set a breakpoint.
9390 ARG is a string describing breakpoint address,
9391 condition, and thread.
9392 FLAG specifies if a breakpoint is hardware on,
9393 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9394 and BP_TEMPFLAG. */
9396 static void
9397 break_command_1 (const char *arg, int flag, int from_tty)
9399 int tempflag = flag & BP_TEMPFLAG;
9400 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9401 ? bp_hardware_breakpoint
9402 : bp_breakpoint);
9404 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9405 const struct breakpoint_ops *ops
9406 = breakpoint_ops_for_location_spec (locspec.get (),
9407 false /* is_tracepoint */);
9409 create_breakpoint (get_current_arch (),
9410 locspec.get (),
9411 NULL,
9412 -1 /* thread */, -1 /* inferior */,
9413 arg, false, 1 /* parse arg */,
9414 tempflag, type_wanted,
9415 0 /* Ignore count */,
9416 pending_break_support,
9417 ops,
9418 from_tty,
9419 1 /* enabled */,
9420 0 /* internal */,
9424 /* Helper function for break_command_1 and disassemble_command. */
9426 void
9427 resolve_sal_pc (struct symtab_and_line *sal)
9429 CORE_ADDR pc;
9431 if (sal->pc == 0 && sal->symtab != NULL)
9433 if (!find_line_pc (sal->symtab, sal->line, &pc))
9434 error (_("No line %d in file \"%s\"."),
9435 sal->line, symtab_to_filename_for_display (sal->symtab));
9436 sal->pc = pc;
9438 /* If this SAL corresponds to a breakpoint inserted using a line
9439 number, then skip the function prologue if necessary. */
9440 if (sal->explicit_line)
9441 skip_prologue_sal (sal);
9444 if (sal->section == 0 && sal->symtab != NULL)
9446 const struct blockvector *bv;
9447 const struct block *b;
9448 struct symbol *sym;
9450 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9451 sal->symtab->compunit ());
9452 if (bv != NULL)
9454 sym = b->linkage_function ();
9455 if (sym != NULL)
9456 sal->section
9457 = sym->obj_section (sal->symtab->compunit ()->objfile ());
9458 else
9460 /* It really is worthwhile to have the section, so we'll
9461 just have to look harder. This case can be executed
9462 if we have line numbers but no functions (as can
9463 happen in assembly source). */
9465 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9466 switch_to_program_space_and_thread (sal->pspace);
9468 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9469 if (msym.minsym)
9470 sal->section = msym.obj_section ();
9476 void
9477 break_command (const char *arg, int from_tty)
9479 break_command_1 (arg, 0, from_tty);
9482 void
9483 tbreak_command (const char *arg, int from_tty)
9485 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9488 static void
9489 hbreak_command (const char *arg, int from_tty)
9491 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9494 static void
9495 thbreak_command (const char *arg, int from_tty)
9497 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9500 /* The dynamic printf command is mostly like a regular breakpoint, but
9501 with a prewired command list consisting of a single output command,
9502 built from extra arguments supplied on the dprintf command
9503 line. */
9505 static void
9506 dprintf_command (const char *arg, int from_tty)
9508 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9510 /* If non-NULL, ARG should have been advanced past the location;
9511 the next character must be ','. */
9512 if (arg == nullptr || arg[0] != ',' || arg[1] == '\0')
9513 error (_("Format string required"));
9514 else
9516 /* Skip the comma. */
9517 ++arg;
9520 create_breakpoint (get_current_arch (),
9521 locspec.get (),
9522 NULL, -1, -1,
9523 arg, false, 0 /* parse arg */,
9524 0, bp_dprintf,
9525 0 /* Ignore count */,
9526 pending_break_support,
9527 &code_breakpoint_ops,
9528 from_tty,
9529 1 /* enabled */,
9530 0 /* internal */,
9534 static void
9535 agent_printf_command (const char *arg, int from_tty)
9537 error (_("May only run agent-printf on the target"));
9540 /* Implement the "breakpoint_hit" method for ranged breakpoints. */
9543 ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9544 const address_space *aspace,
9545 CORE_ADDR bp_addr,
9546 const target_waitstatus &ws)
9548 if (ws.kind () != TARGET_WAITKIND_STOPPED
9549 || ws.sig () != GDB_SIGNAL_TRAP)
9550 return 0;
9552 return breakpoint_address_match_range (bl->pspace->aspace.get (),
9553 bl->address, bl->length, aspace,
9554 bp_addr);
9557 /* Implement the "resources_needed" method for ranged breakpoints. */
9560 ranged_breakpoint::resources_needed (const struct bp_location *bl)
9562 return target_ranged_break_num_registers ();
9565 /* Implement the "print_it" method for ranged breakpoints. */
9567 enum print_stop_action
9568 ranged_breakpoint::print_it (const bpstat *bs) const
9570 struct ui_out *uiout = current_uiout;
9572 gdb_assert (type == bp_hardware_breakpoint);
9574 /* Ranged breakpoints have only one location. */
9575 gdb_assert (this->has_single_location ());
9577 annotate_breakpoint (number);
9579 maybe_print_thread_hit_breakpoint (uiout);
9581 if (disposition == disp_del)
9582 uiout->text ("Temporary ranged breakpoint ");
9583 else
9584 uiout->text ("Ranged breakpoint ");
9585 if (uiout->is_mi_like_p ())
9587 uiout->field_string ("reason",
9588 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9589 uiout->field_string ("disp", bpdisp_text (disposition));
9591 print_num_locno (bs, uiout);
9592 uiout->text (", ");
9594 return PRINT_SRC_AND_LOC;
9597 /* Implement the "print_one" method for ranged breakpoints. */
9599 bool
9600 ranged_breakpoint::print_one (const bp_location **last_loc) const
9602 struct value_print_options opts;
9603 struct ui_out *uiout = current_uiout;
9605 /* Ranged breakpoints have only one location. */
9606 gdb_assert (this->has_single_location ());
9608 get_user_print_options (&opts);
9610 if (opts.addressprint)
9611 /* We don't print the address range here, it will be printed later
9612 by ranged_breakpoint::print_one_detail. */
9613 uiout->field_skip ("addr");
9614 annotate_field (5);
9615 print_breakpoint_location (this, &this->first_loc ());
9616 *last_loc = &this->first_loc ();
9618 return true;
9621 /* Implement the "print_one_detail" method for ranged breakpoints. */
9623 void
9624 ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
9626 CORE_ADDR address_start, address_end;
9627 const bp_location &bl = this->first_loc ();
9628 string_file stb;
9630 address_start = bl.address;
9631 address_end = address_start + bl.length - 1;
9633 uiout->text ("\taddress range: ");
9634 stb.printf ("[%s, %s]",
9635 print_core_address (bl.gdbarch, address_start),
9636 print_core_address (bl.gdbarch, address_end));
9637 uiout->field_stream ("addr", stb);
9638 uiout->text ("\n");
9641 /* Implement the "print_mention" method for ranged breakpoints. */
9643 void
9644 ranged_breakpoint::print_mention () const
9646 const bp_location &bl = this->first_loc ();
9647 struct ui_out *uiout = current_uiout;
9649 gdb_assert (type == bp_hardware_breakpoint);
9651 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9652 number, paddress (bl.gdbarch, bl.address),
9653 paddress (bl.gdbarch, bl.address + bl.length - 1));
9656 /* Implement the "print_recreate" method for ranged breakpoints. */
9658 void
9659 ranged_breakpoint::print_recreate (struct ui_file *fp) const
9661 gdb_printf (fp, "break-range %s, %s",
9662 locspec->to_string (),
9663 locspec_range_end->to_string ());
9664 print_recreate_thread (fp);
9667 /* Find the address where the end of the breakpoint range should be
9668 placed, given the SAL of the end of the range. This is so that if
9669 the user provides a line number, the end of the range is set to the
9670 last instruction of the given line. */
9672 static CORE_ADDR
9673 find_breakpoint_range_end (struct symtab_and_line sal)
9675 CORE_ADDR end;
9677 /* If the user provided a PC value, use it. Otherwise,
9678 find the address of the end of the given location. */
9679 if (sal.explicit_pc)
9680 end = sal.pc;
9681 else
9683 int ret;
9684 CORE_ADDR start;
9686 ret = find_line_pc_range (sal, &start, &end);
9687 if (!ret)
9688 error (_("Could not find location of the end of the range."));
9690 /* find_line_pc_range returns the start of the next line. */
9691 end--;
9694 return end;
9697 /* Implement the "break-range" CLI command. */
9699 static void
9700 break_range_command (const char *arg, int from_tty)
9702 const char *arg_start;
9703 struct linespec_result canonical_start, canonical_end;
9704 int bp_count, can_use_bp, length;
9705 CORE_ADDR end;
9707 /* We don't support software ranged breakpoints. */
9708 if (target_ranged_break_num_registers () < 0)
9709 error (_("This target does not support hardware ranged breakpoints."));
9711 bp_count = hw_breakpoint_used_count ();
9712 bp_count += target_ranged_break_num_registers ();
9713 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9714 bp_count, 0);
9715 if (can_use_bp < 0)
9716 error (_("Hardware breakpoints used exceeds limit."));
9718 arg = skip_spaces (arg);
9719 if (arg == NULL || arg[0] == '\0')
9720 error(_("No address range specified."));
9722 arg_start = arg;
9723 location_spec_up start_locspec
9724 = string_to_location_spec (&arg, current_language);
9725 parse_breakpoint_sals (start_locspec.get (), &canonical_start, nullptr);
9727 if (arg[0] != ',')
9728 error (_("Too few arguments."));
9729 else if (canonical_start.lsals.empty ())
9730 error (_("Could not find location of the beginning of the range."));
9732 const linespec_sals &lsal_start = canonical_start.lsals[0];
9734 if (canonical_start.lsals.size () > 1
9735 || lsal_start.sals.size () != 1)
9736 error (_("Cannot create a ranged breakpoint with multiple locations."));
9738 const symtab_and_line &sal_start = lsal_start.sals[0];
9739 std::string addr_string_start (arg_start, arg - arg_start);
9741 arg++; /* Skip the comma. */
9742 arg = skip_spaces (arg);
9744 /* Parse the end location specification. */
9746 arg_start = arg;
9748 /* We call decode_line_full directly here instead of using
9749 parse_breakpoint_sals because we need to specify the start
9750 location spec's symtab and line as the default symtab and line
9751 for the end of the range. This makes it possible to have ranges
9752 like "foo.c:27, +14", where +14 means 14 lines from the start
9753 location spec. */
9754 location_spec_up end_locspec
9755 = string_to_location_spec (&arg, current_language);
9756 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9757 sal_start.symtab, sal_start.line,
9758 &canonical_end, NULL, NULL);
9760 if (canonical_end.lsals.empty ())
9761 error (_("Could not find location of the end of the range."));
9763 const linespec_sals &lsal_end = canonical_end.lsals[0];
9764 if (canonical_end.lsals.size () > 1
9765 || lsal_end.sals.size () != 1)
9766 error (_("Cannot create a ranged breakpoint with multiple locations."));
9768 const symtab_and_line &sal_end = lsal_end.sals[0];
9770 end = find_breakpoint_range_end (sal_end);
9771 if (sal_start.pc > end)
9772 error (_("Invalid address range, end precedes start."));
9774 length = end - sal_start.pc + 1;
9775 if (length < 0)
9776 /* Length overflowed. */
9777 error (_("Address range too large."));
9778 else if (length == 1)
9780 /* This range is simple enough to be handled by
9781 the `hbreak' command. */
9782 hbreak_command (&addr_string_start[0], 1);
9784 return;
9787 /* Now set up the breakpoint and install it. */
9789 std::unique_ptr<breakpoint> br
9790 (new ranged_breakpoint (get_current_arch (),
9791 sal_start, length,
9792 std::move (start_locspec),
9793 std::move (end_locspec)));
9795 install_breakpoint (false, std::move (br), true);
9798 /* See breakpoint.h. */
9800 watchpoint::~watchpoint ()
9802 /* Make sure to unlink the destroyed watchpoint from the related
9803 breakpoint ring. */
9805 breakpoint *bpt = this;
9806 while (bpt->related_breakpoint != this)
9807 bpt = bpt->related_breakpoint;
9809 bpt->related_breakpoint = this->related_breakpoint;
9812 /* Return non-zero if EXP is verified as constant. Returned zero
9813 means EXP is variable. Also the constant detection may fail for
9814 some constant expressions and in such case still falsely return
9815 zero. */
9817 static bool
9818 watchpoint_exp_is_const (const struct expression *exp)
9820 return exp->op->constant_p ();
9823 /* Implement the "re_set" method for watchpoints. */
9825 void
9826 watchpoint::re_set (struct program_space *pspace)
9828 /* Watchpoint can be either on expression using entirely global
9829 variables, or it can be on local variables.
9831 Watchpoints of the first kind are never auto-deleted, and even
9832 persist across program restarts. Since they can use variables
9833 from shared libraries, we need to reparse expression as libraries
9834 are loaded and unloaded.
9836 Watchpoints on local variables can also change meaning as result
9837 of solib event. For example, if a watchpoint uses both a local
9838 and a global variables in expression, it's a local watchpoint,
9839 but unloading of a shared library will make the expression
9840 invalid. This is not a very common use case, but we still
9841 re-evaluate expression, to avoid surprises to the user.
9843 Note that for local watchpoints, we re-evaluate it only if
9844 watchpoints frame id is still valid. If it's not, it means the
9845 watchpoint is out of scope and will be deleted soon. In fact,
9846 I'm not sure we'll ever be called in this case.
9848 If a local watchpoint's frame id is still valid, then
9849 exp_valid_block is likewise valid, and we can safely use it.
9851 Don't do anything about disabled watchpoints, since they will be
9852 reevaluated again when enabled. */
9853 update_watchpoint (this, true /* reparse */);
9856 /* Implement the "insert" method for hardware watchpoints. */
9859 watchpoint::insert_location (struct bp_location *bl)
9861 int length = exact ? 1 : bl->length;
9863 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9864 cond_exp.get ());
9867 /* Implement the "remove" method for hardware watchpoints. */
9870 watchpoint::remove_location (struct bp_location *bl,
9871 enum remove_bp_reason reason)
9873 int length = exact ? 1 : bl->length;
9875 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9876 cond_exp.get ());
9880 watchpoint::breakpoint_hit (const struct bp_location *bl,
9881 const address_space *aspace, CORE_ADDR bp_addr,
9882 const target_waitstatus &ws)
9884 struct breakpoint *b = bl->owner;
9886 /* Continuable hardware watchpoints are treated as non-existent if the
9887 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9888 some data address). Otherwise gdb won't stop on a break instruction
9889 in the code (not from a breakpoint) when a hardware watchpoint has
9890 been defined. Also skip watchpoints which we know did not trigger
9891 (did not match the data address). */
9892 if (is_hardware_watchpoint (b)
9893 && watchpoint_triggered == watch_triggered_no)
9894 return 0;
9896 return 1;
9899 void
9900 watchpoint::check_status (bpstat *bs)
9902 bpstat_check_watchpoint (bs);
9905 /* Implement the "resources_needed" method for hardware
9906 watchpoints. */
9909 watchpoint::resources_needed (const struct bp_location *bl)
9911 int length = exact? 1 : bl->length;
9913 return target_region_ok_for_hw_watchpoint (bl->address, length);
9916 /* Implement the "works_in_software_mode" method for hardware
9917 watchpoints. */
9919 bool
9920 watchpoint::works_in_software_mode () const
9922 /* Read and access watchpoints only work with hardware support. */
9923 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9926 enum print_stop_action
9927 watchpoint::print_it (const bpstat *bs) const
9929 enum print_stop_action result;
9930 struct ui_out *uiout = current_uiout;
9932 gdb_assert (bs->bp_location_at != NULL);
9934 annotate_watchpoint (this->number);
9935 maybe_print_thread_hit_breakpoint (uiout);
9937 string_file stb;
9939 std::optional<ui_out_emit_tuple> tuple_emitter;
9940 switch (this->type)
9942 case bp_watchpoint:
9943 case bp_hardware_watchpoint:
9944 if (uiout->is_mi_like_p ())
9945 uiout->field_string
9946 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9947 mention (this);
9948 tuple_emitter.emplace (uiout, "value");
9949 uiout->text ("\nOld value = ");
9950 watchpoint_value_print (bs->old_val.get (), &stb);
9951 uiout->field_stream ("old", stb);
9952 uiout->text ("\nNew value = ");
9953 watchpoint_value_print (val.get (), &stb);
9954 uiout->field_stream ("new", stb);
9955 uiout->text ("\n");
9956 /* More than one watchpoint may have been triggered. */
9957 result = PRINT_UNKNOWN;
9958 break;
9960 case bp_read_watchpoint:
9961 if (uiout->is_mi_like_p ())
9962 uiout->field_string
9963 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9964 mention (this);
9965 tuple_emitter.emplace (uiout, "value");
9966 uiout->text ("\nValue = ");
9967 watchpoint_value_print (val.get (), &stb);
9968 uiout->field_stream ("value", stb);
9969 uiout->text ("\n");
9970 result = PRINT_UNKNOWN;
9971 break;
9973 case bp_access_watchpoint:
9974 if (bs->old_val != NULL)
9976 if (uiout->is_mi_like_p ())
9977 uiout->field_string
9978 ("reason",
9979 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9980 mention (this);
9981 tuple_emitter.emplace (uiout, "value");
9982 uiout->text ("\nOld value = ");
9983 watchpoint_value_print (bs->old_val.get (), &stb);
9984 uiout->field_stream ("old", stb);
9985 uiout->text ("\nNew value = ");
9987 else
9989 mention (this);
9990 if (uiout->is_mi_like_p ())
9991 uiout->field_string
9992 ("reason",
9993 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9994 tuple_emitter.emplace (uiout, "value");
9995 uiout->text ("\nValue = ");
9997 watchpoint_value_print (val.get (), &stb);
9998 uiout->field_stream ("new", stb);
9999 uiout->text ("\n");
10000 result = PRINT_UNKNOWN;
10001 break;
10002 default:
10003 result = PRINT_UNKNOWN;
10006 return result;
10009 /* Implement the "print_mention" method for hardware watchpoints. */
10011 void
10012 watchpoint::print_mention () const
10014 struct ui_out *uiout = current_uiout;
10015 const char *tuple_name;
10017 switch (type)
10019 case bp_watchpoint:
10020 uiout->text ("Watchpoint ");
10021 tuple_name = "wpt";
10022 break;
10023 case bp_hardware_watchpoint:
10024 uiout->text ("Hardware watchpoint ");
10025 tuple_name = "wpt";
10026 break;
10027 case bp_read_watchpoint:
10028 uiout->text ("Hardware read watchpoint ");
10029 tuple_name = "hw-rwpt";
10030 break;
10031 case bp_access_watchpoint:
10032 uiout->text ("Hardware access (read/write) watchpoint ");
10033 tuple_name = "hw-awpt";
10034 break;
10035 default:
10036 internal_error (_("Invalid hardware watchpoint type."));
10039 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10040 uiout->field_signed ("number", number);
10041 uiout->text (": ");
10042 uiout->field_string ("exp", exp_string.get ());
10045 /* Implement the "print_recreate" method for watchpoints. */
10047 void
10048 watchpoint::print_recreate (struct ui_file *fp) const
10050 switch (type)
10052 case bp_watchpoint:
10053 case bp_hardware_watchpoint:
10054 gdb_printf (fp, "watch");
10055 break;
10056 case bp_read_watchpoint:
10057 gdb_printf (fp, "rwatch");
10058 break;
10059 case bp_access_watchpoint:
10060 gdb_printf (fp, "awatch");
10061 break;
10062 default:
10063 internal_error (_("Invalid watchpoint type."));
10066 gdb_printf (fp, " %s", exp_string.get ());
10067 print_recreate_thread (fp);
10070 /* Implement the "explains_signal" method for watchpoints. */
10072 bool
10073 watchpoint::explains_signal (enum gdb_signal sig)
10075 /* A software watchpoint cannot cause a signal other than
10076 GDB_SIGNAL_TRAP. */
10077 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10078 return false;
10080 return true;
10083 struct masked_watchpoint : public watchpoint
10085 using watchpoint::watchpoint;
10087 int insert_location (struct bp_location *) override;
10088 int remove_location (struct bp_location *,
10089 enum remove_bp_reason reason) override;
10090 int resources_needed (const struct bp_location *) override;
10091 bool works_in_software_mode () const override;
10092 enum print_stop_action print_it (const bpstat *bs) const override;
10093 void print_one_detail (struct ui_out *) const override;
10094 void print_mention () const override;
10095 void print_recreate (struct ui_file *fp) const override;
10098 /* Implement the "insert" method for masked hardware watchpoints. */
10101 masked_watchpoint::insert_location (struct bp_location *bl)
10103 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
10104 bl->watchpoint_type);
10107 /* Implement the "remove" method for masked hardware watchpoints. */
10110 masked_watchpoint::remove_location (struct bp_location *bl,
10111 enum remove_bp_reason reason)
10113 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
10114 bl->watchpoint_type);
10117 /* Implement the "resources_needed" method for masked hardware
10118 watchpoints. */
10121 masked_watchpoint::resources_needed (const struct bp_location *bl)
10123 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
10126 /* Implement the "works_in_software_mode" method for masked hardware
10127 watchpoints. */
10129 bool
10130 masked_watchpoint::works_in_software_mode () const
10132 return false;
10135 /* Implement the "print_it" method for masked hardware
10136 watchpoints. */
10138 enum print_stop_action
10139 masked_watchpoint::print_it (const bpstat *bs) const
10141 struct ui_out *uiout = current_uiout;
10143 /* Masked watchpoints have only one location. */
10144 gdb_assert (this->has_single_location ());
10146 annotate_watchpoint (this->number);
10147 maybe_print_thread_hit_breakpoint (uiout);
10149 switch (this->type)
10151 case bp_hardware_watchpoint:
10152 if (uiout->is_mi_like_p ())
10153 uiout->field_string
10154 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10155 break;
10157 case bp_read_watchpoint:
10158 if (uiout->is_mi_like_p ())
10159 uiout->field_string
10160 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10161 break;
10163 case bp_access_watchpoint:
10164 if (uiout->is_mi_like_p ())
10165 uiout->field_string
10166 ("reason",
10167 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10168 break;
10169 default:
10170 internal_error (_("Invalid hardware watchpoint type."));
10173 mention (this);
10174 uiout->text (_("\n\
10175 Check the underlying instruction at PC for the memory\n\
10176 address and value which triggered this watchpoint.\n"));
10177 uiout->text ("\n");
10179 /* More than one watchpoint may have been triggered. */
10180 return PRINT_UNKNOWN;
10183 /* Implement the "print_one_detail" method for masked hardware
10184 watchpoints. */
10186 void
10187 masked_watchpoint::print_one_detail (struct ui_out *uiout) const
10189 /* Masked watchpoints have only one location. */
10190 gdb_assert (this->has_single_location ());
10192 uiout->text ("\tmask ");
10193 uiout->field_core_addr ("mask", this->first_loc ().gdbarch, hw_wp_mask);
10194 uiout->text ("\n");
10197 /* Implement the "print_mention" method for masked hardware
10198 watchpoints. */
10200 void
10201 masked_watchpoint::print_mention () const
10203 struct ui_out *uiout = current_uiout;
10204 const char *tuple_name;
10206 switch (type)
10208 case bp_hardware_watchpoint:
10209 uiout->text ("Masked hardware watchpoint ");
10210 tuple_name = "wpt";
10211 break;
10212 case bp_read_watchpoint:
10213 uiout->text ("Masked hardware read watchpoint ");
10214 tuple_name = "hw-rwpt";
10215 break;
10216 case bp_access_watchpoint:
10217 uiout->text ("Masked hardware access (read/write) watchpoint ");
10218 tuple_name = "hw-awpt";
10219 break;
10220 default:
10221 internal_error (_("Invalid hardware watchpoint type."));
10224 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10225 uiout->field_signed ("number", number);
10226 uiout->text (": ");
10227 uiout->field_string ("exp", exp_string.get ());
10230 /* Implement the "print_recreate" method for masked hardware
10231 watchpoints. */
10233 void
10234 masked_watchpoint::print_recreate (struct ui_file *fp) const
10236 switch (type)
10238 case bp_hardware_watchpoint:
10239 gdb_printf (fp, "watch");
10240 break;
10241 case bp_read_watchpoint:
10242 gdb_printf (fp, "rwatch");
10243 break;
10244 case bp_access_watchpoint:
10245 gdb_printf (fp, "awatch");
10246 break;
10247 default:
10248 internal_error (_("Invalid hardware watchpoint type."));
10251 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
10252 phex (hw_wp_mask, sizeof (CORE_ADDR)));
10253 print_recreate_thread (fp);
10256 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10258 static bool
10259 is_masked_watchpoint (const struct breakpoint *b)
10261 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
10264 /* accessflag: hw_write: watch write,
10265 hw_read: watch read,
10266 hw_access: watch access (read or write) */
10267 static void
10268 watch_command_1 (const char *arg, int accessflag, int from_tty,
10269 bool just_location, bool internal)
10271 struct breakpoint *scope_breakpoint = NULL;
10272 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10273 struct value *result;
10274 int saved_bitpos = 0, saved_bitsize = 0;
10275 const char *exp_start = NULL;
10276 const char *exp_end = NULL;
10277 const char *tok, *end_tok;
10278 int toklen = -1;
10279 const char *cond_start = NULL;
10280 const char *cond_end = NULL;
10281 enum bptype bp_type;
10282 int thread = -1;
10283 int inferior = -1;
10284 /* Flag to indicate whether we are going to use masks for
10285 the hardware watchpoint. */
10286 bool use_mask = false;
10287 CORE_ADDR mask = 0;
10288 int task = -1;
10290 /* Make sure that we actually have parameters to parse. */
10291 if (arg != NULL && arg[0] != '\0')
10293 const char *value_start;
10295 exp_end = arg + strlen (arg);
10297 /* Look for "parameter value" pairs at the end
10298 of the arguments string. */
10299 for (tok = exp_end - 1; tok > arg; tok--)
10301 /* Skip whitespace at the end of the argument list. */
10302 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10303 tok--;
10305 /* Find the beginning of the last token.
10306 This is the value of the parameter. */
10307 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10308 tok--;
10309 value_start = tok + 1;
10311 /* Skip whitespace. */
10312 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10313 tok--;
10315 end_tok = tok;
10317 /* Find the beginning of the second to last token.
10318 This is the parameter itself. */
10319 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10320 tok--;
10321 tok++;
10322 toklen = end_tok - tok + 1;
10324 if (toklen == 6 && startswith (tok, "thread"))
10326 struct thread_info *thr;
10327 /* At this point we've found a "thread" token, which means
10328 the user is trying to set a watchpoint that triggers
10329 only in a specific thread. */
10330 const char *endp;
10332 if (thread != -1)
10333 error(_("You can specify only one thread."));
10335 if (task != -1)
10336 error (_("You can specify only one of thread or task."));
10338 if (inferior != -1)
10339 error (_("You can specify only one of inferior or thread."));
10341 /* Extract the thread ID from the next token. */
10342 thr = parse_thread_id (value_start, &endp);
10343 if (value_start == endp)
10344 error (_("Junk after thread keyword."));
10346 thread = thr->global_num;
10348 else if (toklen == 4 && startswith (tok, "task"))
10350 char *tmp;
10352 if (task != -1)
10353 error(_("You can specify only one task."));
10355 if (thread != -1)
10356 error (_("You can specify only one of thread or task."));
10358 if (inferior != -1)
10359 error (_("You can specify only one of inferior or task."));
10361 task = strtol (value_start, &tmp, 0);
10362 if (tmp == value_start)
10363 error (_("Junk after task keyword."));
10364 if (!valid_task_id (task))
10365 error (_("Unknown task %d."), task);
10367 else if (toklen == 8 && startswith (tok, "inferior"))
10369 /* Support for watchpoints will be added in a later commit. */
10370 error (_("Cannot use 'inferior' keyword with watchpoints"));
10372 else if (toklen == 4 && startswith (tok, "mask"))
10374 /* We've found a "mask" token, which means the user wants to
10375 create a hardware watchpoint that is going to have the mask
10376 facility. */
10377 struct value *mask_value;
10379 if (use_mask)
10380 error(_("You can specify only one mask."));
10382 use_mask = just_location = true;
10384 scoped_value_mark mark;
10385 mask_value = parse_to_comma_and_eval (&value_start);
10386 mask = value_as_address (mask_value);
10388 else
10389 /* We didn't recognize what we found. We should stop here. */
10390 break;
10392 /* Truncate the string and get rid of the "parameter value" pair before
10393 the arguments string is parsed by the parse_exp_1 function. */
10394 exp_end = tok;
10397 else
10398 exp_end = arg;
10400 /* Parse the rest of the arguments. From here on out, everything
10401 is in terms of a newly allocated string instead of the original
10402 ARG. */
10403 std::string expression (arg, exp_end - arg);
10404 exp_start = arg = expression.c_str ();
10405 innermost_block_tracker tracker;
10406 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10407 exp_end = arg;
10408 /* Remove trailing whitespace from the expression before saving it.
10409 This makes the eventual display of the expression string a bit
10410 prettier. */
10411 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10412 --exp_end;
10414 /* Checking if the expression is not constant. */
10415 if (watchpoint_exp_is_const (exp.get ()))
10417 int len;
10419 len = exp_end - exp_start;
10420 while (len > 0 && isspace (exp_start[len - 1]))
10421 len--;
10422 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10425 exp_valid_block = tracker.block ();
10426 struct value *mark = value_mark ();
10427 struct value *val_as_value = nullptr;
10428 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10429 just_location);
10431 if (val_as_value != NULL && just_location)
10433 saved_bitpos = val_as_value->bitpos ();
10434 saved_bitsize = val_as_value->bitsize ();
10437 value_ref_ptr val;
10438 if (just_location)
10440 int ret;
10442 exp_valid_block = NULL;
10443 val = release_value (value_addr (result));
10444 value_free_to_mark (mark);
10446 if (use_mask)
10448 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10449 mask);
10450 if (ret == -1)
10451 error (_("This target does not support masked watchpoints."));
10452 else if (ret == -2)
10453 error (_("Invalid mask or memory region."));
10456 else if (val_as_value != NULL)
10457 val = release_value (val_as_value);
10459 tok = skip_spaces (arg);
10460 end_tok = skip_to_space (tok);
10462 toklen = end_tok - tok;
10463 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10465 tok = cond_start = end_tok + 1;
10466 innermost_block_tracker if_tracker;
10467 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10469 /* The watchpoint expression may not be local, but the condition
10470 may still be. E.g.: `watch global if local > 0'. */
10471 cond_exp_valid_block = if_tracker.block ();
10473 cond_end = tok;
10475 if (*tok)
10476 error (_("Junk at end of command."));
10478 frame_info_ptr wp_frame = block_innermost_frame (exp_valid_block);
10480 /* Save this because create_internal_breakpoint below invalidates
10481 'wp_frame'. */
10482 frame_id watchpoint_frame = get_frame_id (wp_frame);
10484 /* Now set up the breakpoint. We create all watchpoints as hardware
10485 watchpoints here even if hardware watchpoints are turned off, a call
10486 to update_watchpoint later in this function will cause the type to
10487 drop back to bp_watchpoint (software watchpoint) if required. */
10489 if (accessflag == hw_read)
10490 bp_type = bp_read_watchpoint;
10491 else if (accessflag == hw_access)
10492 bp_type = bp_access_watchpoint;
10493 else
10494 bp_type = bp_hardware_watchpoint;
10496 std::unique_ptr<watchpoint> w;
10497 if (use_mask)
10498 w = std::make_unique<masked_watchpoint> (nullptr, bp_type);
10499 else
10500 w = std::make_unique<watchpoint> (nullptr, bp_type);
10502 /* At most one of thread or task can be set on a watchpoint. */
10503 gdb_assert (thread == -1 || task == -1);
10504 w->thread = thread;
10505 w->inferior = inferior;
10506 w->task = task;
10507 w->disposition = disp_donttouch;
10508 w->pspace = current_program_space;
10509 w->exp = std::move (exp);
10510 w->exp_valid_block = exp_valid_block;
10511 w->cond_exp_valid_block = cond_exp_valid_block;
10512 if (just_location)
10514 struct type *t = val->type ();
10515 CORE_ADDR addr = value_as_address (val.get ());
10517 w->exp_string_reparse
10518 = current_language->watch_location_expression (t, addr);
10520 w->exp_string = xstrprintf ("-location %.*s",
10521 (int) (exp_end - exp_start), exp_start);
10523 else
10524 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10526 if (use_mask)
10528 w->hw_wp_mask = mask;
10530 else
10532 w->val = val;
10533 w->val_bitpos = saved_bitpos;
10534 w->val_bitsize = saved_bitsize;
10535 w->val_valid = true;
10538 if (cond_start)
10539 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10540 else
10541 w->cond_string = 0;
10543 if (frame_id_p (watchpoint_frame))
10545 w->watchpoint_frame = watchpoint_frame;
10546 w->watchpoint_thread = inferior_ptid;
10548 else
10550 w->watchpoint_frame = null_frame_id;
10551 w->watchpoint_thread = null_ptid;
10554 if (!just_location)
10555 value_free_to_mark (mark);
10557 /* Finally update the new watchpoint. This creates the locations
10558 that should be inserted. */
10559 update_watchpoint (w.get (), true /* reparse */);
10561 /* If the expression is "local", then set up a "watchpoint scope"
10562 breakpoint at the point where we've left the scope of the watchpoint
10563 expression. Create the scope breakpoint before the watchpoint, so
10564 that we will encounter it first in bpstat_stop_status. */
10565 if (exp_valid_block != nullptr && wp_frame != nullptr)
10567 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10569 if (frame_id_p (caller_frame_id))
10571 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10572 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10574 scope_breakpoint
10575 = create_internal_breakpoint (caller_arch, caller_pc,
10576 bp_watchpoint_scope);
10578 /* create_internal_breakpoint could invalidate WP_FRAME. */
10579 wp_frame = nullptr;
10581 scope_breakpoint->enable_state = bp_enabled;
10583 /* Automatically delete the breakpoint when it hits. */
10584 scope_breakpoint->disposition = disp_del;
10586 /* Only break in the proper frame (help with recursion). */
10587 scope_breakpoint->frame_id = caller_frame_id;
10589 /* Set the address at which we will stop. */
10590 bp_location &loc = scope_breakpoint->first_loc ();
10591 loc.gdbarch = caller_arch;
10592 loc.requested_address = caller_pc;
10593 loc.address
10594 = adjust_breakpoint_address (loc.gdbarch, loc.requested_address,
10595 scope_breakpoint->type,
10596 current_program_space);
10600 if (scope_breakpoint != nullptr)
10602 /* The scope breakpoint is related to the watchpoint. We will
10603 need to act on them together. */
10604 w->related_breakpoint = scope_breakpoint;
10605 scope_breakpoint->related_breakpoint = w.get ();
10608 /* Verify that the scope breakpoint comes before the watchpoint in the
10609 breakpoint chain. */
10610 gdb_assert (scope_breakpoint == nullptr
10611 || &breakpoint_chain.back () == scope_breakpoint);
10612 watchpoint *watchpoint_ptr = w.get ();
10613 install_breakpoint (internal, std::move (w), 1);
10614 gdb_assert (&breakpoint_chain.back () == watchpoint_ptr);
10617 /* Return count of debug registers needed to watch the given expression.
10618 If the watchpoint cannot be handled in hardware return zero. */
10620 static int
10621 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10623 int found_memory_cnt = 0;
10625 /* Did the user specifically forbid us to use hardware watchpoints? */
10626 if (!can_use_hw_watchpoints)
10627 return 0;
10629 gdb_assert (!vals.empty ());
10630 struct value *head = vals[0].get ();
10632 /* Make sure that the value of the expression depends only upon
10633 memory contents, and values computed from them within GDB. If we
10634 find any register references or function calls, we can't use a
10635 hardware watchpoint.
10637 The idea here is that evaluating an expression generates a series
10638 of values, one holding the value of every subexpression. (The
10639 expression a*b+c has five subexpressions: a, b, a*b, c, and
10640 a*b+c.) GDB's values hold almost enough information to establish
10641 the criteria given above --- they identify memory lvalues,
10642 register lvalues, computed values, etcetera. So we can evaluate
10643 the expression, and then scan the chain of values that leaves
10644 behind to decide whether we can detect any possible change to the
10645 expression's final value using only hardware watchpoints.
10647 However, I don't think that the values returned by inferior
10648 function calls are special in any way. So this function may not
10649 notice that an expression involving an inferior function call
10650 can't be watched with hardware watchpoints. FIXME. */
10651 for (const value_ref_ptr &iter : vals)
10653 struct value *v = iter.get ();
10655 if (v->lval () == lval_memory)
10657 if (v != head && v->lazy ())
10658 /* A lazy memory lvalue in the chain is one that GDB never
10659 needed to fetch; we either just used its address (e.g.,
10660 `a' in `a.b') or we never needed it at all (e.g., `a'
10661 in `a,b'). This doesn't apply to HEAD; if that is
10662 lazy then it was not readable, but watch it anyway. */
10664 else
10666 /* Ahh, memory we actually used! Check if we can cover
10667 it with hardware watchpoints. */
10668 struct type *vtype = check_typedef (v->type ());
10670 /* We only watch structs and arrays if user asked for it
10671 explicitly, never if they just happen to appear in a
10672 middle of some value chain. */
10673 if (v == head
10674 || (vtype->code () != TYPE_CODE_STRUCT
10675 && vtype->code () != TYPE_CODE_ARRAY))
10677 CORE_ADDR vaddr = v->address ();
10678 int len;
10679 int num_regs;
10681 len = (target_exact_watchpoints
10682 && is_scalar_type_recursive (vtype))?
10683 1 : v->type ()->length ();
10685 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10686 if (!num_regs)
10687 return 0;
10688 else
10689 found_memory_cnt += num_regs;
10693 else if (v->lval () != not_lval && !v->deprecated_modifiable ())
10694 return 0; /* These are values from the history (e.g., $1). */
10695 else if (v->lval () == lval_register)
10696 return 0; /* Cannot watch a register with a HW watchpoint. */
10699 /* The expression itself looks suitable for using a hardware
10700 watchpoint, but give the target machine a chance to reject it. */
10701 return found_memory_cnt;
10704 void
10705 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10707 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10710 /* Options for the watch, awatch, and rwatch commands. */
10712 struct watch_options
10714 /* For -location. */
10715 bool location = false;
10718 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10720 Historically GDB always accepted both '-location' and '-l' flags for
10721 these commands (both flags being synonyms). When converting to the
10722 newer option scheme only '-location' is added here. That's fine (for
10723 backward compatibility) as any non-ambiguous prefix of a flag will be
10724 accepted, so '-l', '-loc', are now all accepted.
10726 What this means is that, if in the future, we add any new flag here
10727 that starts with '-l' then this will break backward compatibility, so
10728 please, don't do that! */
10730 static const gdb::option::option_def watch_option_defs[] = {
10731 gdb::option::flag_option_def<watch_options> {
10732 "location",
10733 [] (watch_options *opt) { return &opt->location; },
10734 N_("\
10735 This evaluates EXPRESSION and watches the memory to which it refers.\n\
10736 -l can be used as a short form of -location."),
10740 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10741 commands. */
10743 static gdb::option::option_def_group
10744 make_watch_options_def_group (watch_options *opts)
10746 return {{watch_option_defs}, opts};
10749 /* A helper function that looks for the "-location" argument and then
10750 calls watch_command_1. */
10752 static void
10753 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10755 watch_options opts;
10756 auto grp = make_watch_options_def_group (&opts);
10757 gdb::option::process_options
10758 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10759 if (arg != nullptr && *arg == '\0')
10760 arg = nullptr;
10762 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10765 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10766 static void
10767 watch_command_completer (struct cmd_list_element *ignore,
10768 completion_tracker &tracker,
10769 const char *text, const char * /*word*/)
10771 const auto group = make_watch_options_def_group (nullptr);
10772 if (gdb::option::complete_options
10773 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10774 return;
10776 const char *word = advance_to_expression_complete_word_point (tracker, text);
10777 expression_completer (ignore, tracker, text, word);
10780 static void
10781 watch_command (const char *arg, int from_tty)
10783 watch_maybe_just_location (arg, hw_write, from_tty);
10786 void
10787 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10789 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10792 static void
10793 rwatch_command (const char *arg, int from_tty)
10795 watch_maybe_just_location (arg, hw_read, from_tty);
10798 void
10799 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
10801 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10804 static void
10805 awatch_command (const char *arg, int from_tty)
10807 watch_maybe_just_location (arg, hw_access, from_tty);
10811 /* Data for the FSM that manages the until(location)/advance commands
10812 in infcmd.c. Here because it uses the mechanisms of
10813 breakpoints. */
10815 struct until_break_fsm : public thread_fsm
10817 /* The thread that was current when the command was executed. */
10818 int thread;
10820 /* The breakpoint set at the return address in the caller frame,
10821 plus breakpoints at all the destination locations. */
10822 std::vector<breakpoint_up> breakpoints;
10824 until_break_fsm (struct interp *cmd_interp, int thread,
10825 std::vector<breakpoint_up> &&breakpoints)
10826 : thread_fsm (cmd_interp),
10827 thread (thread),
10828 breakpoints (std::move (breakpoints))
10832 void clean_up (struct thread_info *thread) override;
10833 bool should_stop (struct thread_info *thread) override;
10834 enum async_reply_reason do_async_reply_reason () override;
10837 /* Implementation of the 'should_stop' FSM method for the
10838 until(location)/advance commands. */
10840 bool
10841 until_break_fsm::should_stop (struct thread_info *tp)
10843 for (const breakpoint_up &bp : breakpoints)
10844 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10845 bp.get ()) != NULL)
10847 set_finished ();
10848 break;
10851 return true;
10854 /* Implementation of the 'clean_up' FSM method for the
10855 until(location)/advance commands. */
10857 void
10858 until_break_fsm::clean_up (struct thread_info *)
10860 /* Clean up our temporary breakpoints. */
10861 breakpoints.clear ();
10862 delete_longjmp_breakpoint (thread);
10865 /* Implementation of the 'async_reply_reason' FSM method for the
10866 until(location)/advance commands. */
10868 enum async_reply_reason
10869 until_break_fsm::do_async_reply_reason ()
10871 return EXEC_ASYNC_LOCATION_REACHED;
10874 void
10875 until_break_command (const char *arg, int from_tty, int anywhere)
10877 frame_info_ptr frame;
10878 struct gdbarch *frame_gdbarch;
10879 struct frame_id stack_frame_id;
10880 struct frame_id caller_frame_id;
10881 int thread;
10882 struct thread_info *tp;
10884 clear_proceed_status (0);
10886 /* Set a breakpoint where the user wants it and at return from
10887 this function. */
10889 location_spec_up locspec = string_to_location_spec (&arg, current_language);
10891 std::vector<symtab_and_line> sals
10892 = (last_displayed_sal_is_valid ()
10893 ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10894 get_last_displayed_symtab (),
10895 get_last_displayed_line ())
10896 : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
10897 NULL, NULL, 0));
10899 if (sals.empty ())
10900 error (_("Couldn't get information on specified line."));
10902 if (*arg)
10903 error (_("Junk at end of arguments."));
10905 tp = inferior_thread ();
10906 thread = tp->global_num;
10908 /* Note linespec handling above invalidates the frame chain.
10909 Installing a breakpoint also invalidates the frame chain (as it
10910 may need to switch threads), so do any frame handling before
10911 that. */
10913 frame = get_selected_frame (NULL);
10914 frame_gdbarch = get_frame_arch (frame);
10915 stack_frame_id = get_stack_frame_id (frame);
10916 caller_frame_id = frame_unwind_caller_id (frame);
10918 /* Keep within the current frame, or in frames called by the current
10919 one. */
10921 std::vector<breakpoint_up> breakpoints;
10923 std::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10925 if (frame_id_p (caller_frame_id))
10927 struct symtab_and_line sal2;
10928 struct gdbarch *caller_gdbarch;
10930 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10931 sal2.pc = frame_unwind_caller_pc (frame);
10932 caller_gdbarch = frame_unwind_caller_arch (frame);
10934 breakpoint_up caller_breakpoint
10935 = set_momentary_breakpoint (caller_gdbarch, sal2,
10936 caller_frame_id, bp_until);
10937 breakpoints.emplace_back (std::move (caller_breakpoint));
10939 set_longjmp_breakpoint (tp, stack_frame_id);
10940 lj_deleter.emplace (thread);
10943 /* If the user told us to continue until a specified location, we
10944 don't specify a frame at which we need to stop. Otherwise,
10945 specify the selected frame, because we want to stop only at the
10946 very same frame. */
10947 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10949 for (symtab_and_line &sal : sals)
10951 resolve_sal_pc (&sal);
10953 breakpoint_up location_breakpoint
10954 = set_momentary_breakpoint (frame_gdbarch, sal,
10955 stop_frame_id, bp_until);
10956 breakpoints.emplace_back (std::move (location_breakpoint));
10959 tp->set_thread_fsm
10960 (std::unique_ptr<thread_fsm>
10961 (new until_break_fsm (command_interp (), tp->global_num,
10962 std::move (breakpoints))));
10964 if (lj_deleter)
10965 lj_deleter->release ();
10967 proceed (-1, GDB_SIGNAL_DEFAULT);
10972 /* Compare two breakpoints and return a strcmp-like result. */
10974 static int
10975 compare_breakpoints (const breakpoint *a, const breakpoint *b)
10977 uintptr_t ua = (uintptr_t) a;
10978 uintptr_t ub = (uintptr_t) b;
10980 if (a->number < b->number)
10981 return -1;
10982 else if (a->number > b->number)
10983 return 1;
10985 /* Now sort by address, in case we see, e..g, two breakpoints with
10986 the number 0. */
10987 if (ua < ub)
10988 return -1;
10989 return ua > ub ? 1 : 0;
10992 /* Delete breakpoints by address or line. */
10994 static void
10995 clear_command (const char *arg, int from_tty)
10997 int default_match;
10999 std::vector<symtab_and_line> decoded_sals;
11000 symtab_and_line last_sal;
11001 gdb::array_view<symtab_and_line> sals;
11002 if (arg)
11004 decoded_sals
11005 = decode_line_with_current_source (arg,
11006 (DECODE_LINE_FUNFIRSTLINE
11007 | DECODE_LINE_LIST_MODE));
11008 default_match = 0;
11009 sals = decoded_sals;
11011 else
11013 /* Set sal's line, symtab, pc, and pspace to the values
11014 corresponding to the last call to print_frame_info. If the
11015 codepoint is not valid, this will set all the fields to 0. */
11016 last_sal = get_last_displayed_sal ();
11017 if (last_sal.symtab == 0)
11018 error (_("No source file specified."));
11020 default_match = 1;
11021 sals = last_sal;
11024 /* We don't call resolve_sal_pc here. That's not as bad as it
11025 seems, because all existing breakpoints typically have both
11026 file/line and pc set. So, if clear is given file/line, we can
11027 match this to existing breakpoint without obtaining pc at all.
11029 We only support clearing given the address explicitly
11030 present in breakpoint table. Say, we've set breakpoint
11031 at file:line. There were several PC values for that file:line,
11032 due to optimization, all in one block.
11034 We've picked one PC value. If "clear" is issued with another
11035 PC corresponding to the same file:line, the breakpoint won't
11036 be cleared. We probably can still clear the breakpoint, but
11037 since the other PC value is never presented to user, user
11038 can only find it by guessing, and it does not seem important
11039 to support that. */
11041 /* For each line spec given, delete bps which correspond to it. Do
11042 it in two passes, solely to preserve the current behavior that
11043 from_tty is forced true if we delete more than one
11044 breakpoint. */
11046 std::vector<struct breakpoint *> found;
11047 for (const auto &sal : sals)
11049 const char *sal_fullname;
11051 /* If exact pc given, clear bpts at that pc.
11052 If line given (pc == 0), clear all bpts on specified line.
11053 If defaulting, clear all bpts on default line
11054 or at default pc.
11056 defaulting sal.pc != 0 tests to do
11058 0 1 pc
11059 1 1 pc _and_ line
11060 0 0 line
11061 1 0 <can't happen> */
11063 sal_fullname = (sal.symtab == NULL
11064 ? NULL : symtab_to_fullname (sal.symtab));
11066 /* Find all matching breakpoints and add them to 'found'. */
11067 for (breakpoint &b : all_breakpoints ())
11069 int match = 0;
11070 /* Are we going to delete b? */
11071 if (b.type != bp_none && !is_watchpoint (&b)
11072 && user_breakpoint_p (&b))
11074 for (bp_location &loc : b.locations ())
11076 /* If the user specified file:line, don't allow a PC
11077 match. This matches historical gdb behavior. */
11078 int pc_match = (!sal.explicit_line
11079 && sal.pc
11080 && (loc.pspace == sal.pspace)
11081 && (loc.address == sal.pc)
11082 && (!section_is_overlay (loc.section)
11083 || loc.section == sal.section));
11084 int line_match = 0;
11086 if ((default_match || sal.explicit_line)
11087 && loc.symtab != NULL
11088 && sal_fullname != NULL
11089 && sal.pspace == loc.pspace
11090 && loc.line_number == sal.line
11091 && filename_cmp (symtab_to_fullname (loc.symtab),
11092 sal_fullname) == 0)
11093 line_match = 1;
11095 if (pc_match || line_match)
11097 match = 1;
11098 break;
11103 if (match)
11104 found.push_back (&b);
11108 /* Now go through the 'found' chain and delete them. */
11109 if (found.empty ())
11111 if (arg)
11112 error (_("No breakpoint at %s."), arg);
11113 else
11114 error (_("No breakpoint at this line."));
11117 /* Remove duplicates from the vec. */
11118 std::sort (found.begin (), found.end (),
11119 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11121 return compare_breakpoints (bp_a, bp_b) < 0;
11123 found.erase (std::unique (found.begin (), found.end (),
11124 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11126 return compare_breakpoints (bp_a, bp_b) == 0;
11128 found.end ());
11130 if (found.size () > 1)
11131 from_tty = 1; /* Always report if deleted more than one. */
11132 if (from_tty)
11134 if (found.size () == 1)
11135 gdb_printf (_("Deleted breakpoint "));
11136 else
11137 gdb_printf (_("Deleted breakpoints "));
11140 for (breakpoint *iter : found)
11142 if (from_tty)
11143 gdb_printf ("%d ", iter->number);
11144 delete_breakpoint (iter);
11146 if (from_tty)
11147 gdb_putc ('\n');
11150 /* Delete breakpoint in BS if they are `delete' breakpoints and
11151 all breakpoints that are marked for deletion, whether hit or not.
11152 This is called after any breakpoint is hit, or after errors. */
11154 void
11155 breakpoint_auto_delete (bpstat *bs)
11157 for (; bs; bs = bs->next)
11158 if (bs->breakpoint_at
11159 && bs->breakpoint_at->disposition == disp_del
11160 && bs->stop)
11161 delete_breakpoint (bs->breakpoint_at);
11163 for (breakpoint &b : all_breakpoints_safe ())
11164 if (b.disposition == disp_del_at_next_stop)
11165 delete_breakpoint (&b);
11168 /* A comparison function for bp_location AP and BP being interfaced to
11169 std::sort. Sort elements primarily by their ADDRESS (no matter what
11170 bl_address_is_meaningful says), secondarily by ordering first
11171 permanent elements and tertiarily just ensuring the array is sorted
11172 stable way despite std::sort being an unstable algorithm. */
11174 static int
11175 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11177 if (a->address != b->address)
11178 return a->address < b->address;
11180 /* Sort locations at the same address by their pspace number, keeping
11181 locations of the same inferior (in a multi-inferior environment)
11182 grouped. */
11184 if (a->pspace->num != b->pspace->num)
11185 return a->pspace->num < b->pspace->num;
11187 /* Sort permanent breakpoints first. */
11188 if (a->permanent != b->permanent)
11189 return a->permanent > b->permanent;
11191 /* Sort by type in order to make duplicate determination easier.
11192 See update_global_location_list. This is kept in sync with
11193 breakpoint_locations_match. */
11194 if (a->loc_type < b->loc_type)
11195 return true;
11197 /* Likewise, for range-breakpoints, sort by length. */
11198 if (a->loc_type == bp_loc_hardware_breakpoint
11199 && b->loc_type == bp_loc_hardware_breakpoint
11200 && a->length < b->length)
11201 return true;
11203 /* Make the internal GDB representation stable across GDB runs
11204 where A and B memory inside GDB can differ. Breakpoint locations of
11205 the same type at the same address can be sorted in arbitrary order. */
11207 if (a->owner->number != b->owner->number)
11208 return a->owner->number < b->owner->number;
11210 return a < b;
11213 /* Set bp_locations_placed_address_before_address_max and
11214 bp_locations_shadow_len_after_address_max according to the current
11215 content of the bp_locations array. */
11217 static void
11218 bp_locations_target_extensions_update (void)
11220 bp_locations_placed_address_before_address_max = 0;
11221 bp_locations_shadow_len_after_address_max = 0;
11223 for (bp_location *bl : all_bp_locations ())
11225 CORE_ADDR start, end, addr;
11227 if (!bp_location_has_shadow (bl))
11228 continue;
11230 start = bl->target_info.placed_address;
11231 end = start + bl->target_info.shadow_len;
11233 gdb_assert (bl->address >= start);
11234 addr = bl->address - start;
11235 if (addr > bp_locations_placed_address_before_address_max)
11236 bp_locations_placed_address_before_address_max = addr;
11238 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11240 gdb_assert (bl->address < end);
11241 addr = end - bl->address;
11242 if (addr > bp_locations_shadow_len_after_address_max)
11243 bp_locations_shadow_len_after_address_max = addr;
11247 /* Download tracepoint locations if they haven't been. */
11249 static void
11250 download_tracepoint_locations (void)
11252 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11254 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11256 for (breakpoint &b : all_tracepoints ())
11258 bool bp_location_downloaded = false;
11260 if ((b.type == bp_fast_tracepoint
11261 ? !may_insert_fast_tracepoints
11262 : !may_insert_tracepoints))
11263 continue;
11265 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11267 if (target_can_download_tracepoint ())
11268 can_download_tracepoint = TRIBOOL_TRUE;
11269 else
11270 can_download_tracepoint = TRIBOOL_FALSE;
11273 if (can_download_tracepoint == TRIBOOL_FALSE)
11274 break;
11276 for (bp_location &bl : b.locations ())
11278 /* In tracepoint, locations are _never_ duplicated, so
11279 should_be_inserted is equivalent to
11280 unduplicated_should_be_inserted. */
11281 if (!should_be_inserted (&bl) || bl.inserted)
11282 continue;
11284 switch_to_program_space_and_thread (bl.pspace);
11286 target_download_tracepoint (&bl);
11288 bl.inserted = 1;
11289 bp_location_downloaded = true;
11292 tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
11293 t.number_on_target = b.number;
11294 if (bp_location_downloaded)
11295 notify_breakpoint_modified (&b);
11299 /* Swap the insertion/duplication state between two locations. */
11301 static void
11302 swap_insertion (struct bp_location *left, struct bp_location *right)
11304 const int left_inserted = left->inserted;
11305 const int left_duplicate = left->duplicate;
11306 const int left_needs_update = left->needs_update;
11307 const struct bp_target_info left_target_info = left->target_info;
11309 /* Locations of tracepoints can never be duplicated. */
11310 if (is_tracepoint (left->owner))
11311 gdb_assert (!left->duplicate);
11312 if (is_tracepoint (right->owner))
11313 gdb_assert (!right->duplicate);
11315 left->inserted = right->inserted;
11316 left->duplicate = right->duplicate;
11317 left->needs_update = right->needs_update;
11318 left->target_info = right->target_info;
11319 right->inserted = left_inserted;
11320 right->duplicate = left_duplicate;
11321 right->needs_update = left_needs_update;
11322 right->target_info = left_target_info;
11325 /* Force the re-insertion of the locations at ADDRESS. This is called
11326 once a new/deleted/modified duplicate location is found and we are evaluating
11327 conditions on the target's side. Such conditions need to be updated on
11328 the target. */
11330 static void
11331 force_breakpoint_reinsertion (struct bp_location *bl)
11333 CORE_ADDR address = 0;
11334 int pspace_num;
11336 address = bl->address;
11337 pspace_num = bl->pspace->num;
11339 /* This is only meaningful if the target is
11340 evaluating conditions and if the user has
11341 opted for condition evaluation on the target's
11342 side. */
11343 if (gdb_evaluates_breakpoint_condition_p ()
11344 || !target_supports_evaluation_of_breakpoint_conditions ())
11345 return;
11347 /* Flag all breakpoint locations with this address and
11348 the same program space as the location
11349 as "its condition has changed". We need to
11350 update the conditions on the target's side. */
11351 for (bp_location *loc : all_bp_locations_at_addr (address))
11353 if (!is_breakpoint (loc->owner)
11354 || pspace_num != loc->pspace->num)
11355 continue;
11357 /* Flag the location appropriately. We use a different state to
11358 let everyone know that we already updated the set of locations
11359 with addr bl->address and program space bl->pspace. This is so
11360 we don't have to keep calling these functions just to mark locations
11361 that have already been marked. */
11362 loc->condition_changed = condition_updated;
11364 /* Free the agent expression bytecode as well. We will compute
11365 it later on. */
11366 loc->cond_bytecode.reset ();
11370 /* Called whether new breakpoints are created, or existing breakpoints
11371 deleted, to update the global location list and recompute which
11372 locations are duplicate of which.
11374 The INSERT_MODE flag determines whether locations may not, may, or
11375 shall be inserted now. See 'enum ugll_insert_mode' for more
11376 info. */
11378 static void
11379 update_global_location_list (enum ugll_insert_mode insert_mode)
11381 /* Last breakpoint location address that was marked for update. */
11382 CORE_ADDR last_addr = 0;
11383 /* Last breakpoint location program space that was marked for update. */
11384 int last_pspace_num = -1;
11386 breakpoint_debug_printf ("insert_mode = %s",
11387 ugll_insert_mode_text (insert_mode));
11389 /* Used in the duplicates detection below. When iterating over all
11390 bp_locations, points to the first bp_location of a given address.
11391 Breakpoints and watchpoints of different types are never
11392 duplicates of each other. Keep one pointer for each type of
11393 breakpoint/watchpoint, so we only need to loop over all locations
11394 once. */
11395 struct bp_location *bp_loc_first; /* breakpoint */
11396 struct bp_location *wp_loc_first; /* hardware watchpoint */
11397 struct bp_location *awp_loc_first; /* access watchpoint */
11398 struct bp_location *rwp_loc_first; /* read watchpoint */
11400 /* Saved former bp_locations array which we compare against the newly
11401 built bp_locations from the current state of ALL_BREAKPOINTS. */
11402 std::vector<bp_location *> old_locations = std::move (bp_locations);
11403 bp_locations.clear ();
11405 for (breakpoint &b : all_breakpoints ())
11406 for (bp_location &loc : b.locations ())
11407 bp_locations.push_back (&loc);
11409 /* See if we need to "upgrade" a software breakpoint to a hardware
11410 breakpoint. Do this before deciding whether locations are
11411 duplicates. Also do this before sorting because sorting order
11412 depends on location type. */
11413 for (bp_location *loc : bp_locations)
11414 if (!loc->inserted && should_be_inserted (loc))
11415 handle_automatic_hardware_breakpoints (loc);
11417 std::sort (bp_locations.begin (), bp_locations.end (),
11418 bp_location_is_less_than);
11420 bp_locations_target_extensions_update ();
11422 /* Identify bp_location instances that are no longer present in the
11423 new list, and therefore should be freed. Note that it's not
11424 necessary that those locations should be removed from inferior --
11425 if there's another location at the same address (previously
11426 marked as duplicate), we don't need to remove/insert the
11427 location.
11429 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11430 and former bp_location array state respectively. */
11432 size_t loc_i = 0;
11433 for (bp_location *old_loc : old_locations)
11435 /* Tells if 'old_loc' is found among the new locations. If
11436 not, we have to free it. */
11437 bool found_object = false;
11438 /* Tells if the location should remain inserted in the target. */
11439 bool keep_in_target = false;
11440 bool removed = false;
11442 /* Skip LOCP entries which will definitely never be needed.
11443 Stop either at or being the one matching OLD_LOC. */
11444 while (loc_i < bp_locations.size ()
11445 && bp_locations[loc_i]->address < old_loc->address)
11446 loc_i++;
11448 for (size_t loc2_i = loc_i;
11449 (loc2_i < bp_locations.size ()
11450 && bp_locations[loc2_i]->address == old_loc->address);
11451 loc2_i++)
11453 /* Check if this is a new/duplicated location or a duplicated
11454 location that had its condition modified. If so, we want to send
11455 its condition to the target if evaluation of conditions is taking
11456 place there. */
11457 if (bp_locations[loc2_i]->condition_changed == condition_modified
11458 && (last_addr != old_loc->address
11459 || last_pspace_num != old_loc->pspace->num))
11461 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11462 last_pspace_num = old_loc->pspace->num;
11465 if (bp_locations[loc2_i] == old_loc)
11466 found_object = true;
11469 /* We have already handled this address, update it so that we don't
11470 have to go through updates again. */
11471 last_addr = old_loc->address;
11473 /* Target-side condition evaluation: Handle deleted locations. */
11474 if (!found_object)
11475 force_breakpoint_reinsertion (old_loc);
11477 /* If this location is no longer present, and inserted, look if
11478 there's maybe a new location at the same address. If so,
11479 mark that one inserted, and don't remove this one. This is
11480 needed so that we don't have a time window where a breakpoint
11481 at certain location is not inserted. */
11483 if (old_loc->inserted)
11485 /* If the location is inserted now, we might have to remove
11486 it. */
11488 if (found_object && should_be_inserted (old_loc))
11490 /* The location is still present in the location list,
11491 and still should be inserted. Don't do anything. */
11492 keep_in_target = true;
11494 else
11496 /* This location still exists, but it won't be kept in the
11497 target since it may have been disabled. We proceed to
11498 remove its target-side condition. */
11500 /* The location is either no longer present, or got
11501 disabled. See if there's another location at the
11502 same address, in which case we don't need to remove
11503 this one from the target. */
11505 /* OLD_LOC comes from existing struct breakpoint. */
11506 if (bl_address_is_meaningful (old_loc))
11508 for (size_t loc2_i = loc_i;
11509 (loc2_i < bp_locations.size ()
11510 && bp_locations[loc2_i]->address == old_loc->address);
11511 loc2_i++)
11513 bp_location *loc2 = bp_locations[loc2_i];
11515 if (loc2 == old_loc)
11516 continue;
11518 if (breakpoint_locations_match (loc2, old_loc))
11520 /* Read watchpoint locations are switched to
11521 access watchpoints, if the former are not
11522 supported, but the latter are. */
11523 if (is_hardware_watchpoint (old_loc->owner))
11525 gdb_assert (is_hardware_watchpoint (loc2->owner));
11526 loc2->watchpoint_type = old_loc->watchpoint_type;
11529 /* loc2 is a duplicated location. We need to check
11530 if it should be inserted in case it will be
11531 unduplicated. */
11532 if (unduplicated_should_be_inserted (loc2))
11534 swap_insertion (old_loc, loc2);
11535 keep_in_target = true;
11536 break;
11543 if (!keep_in_target)
11545 if (remove_breakpoint (old_loc))
11547 /* This is just about all we can do. We could keep
11548 this location on the global list, and try to
11549 remove it next time, but there's no particular
11550 reason why we will succeed next time.
11552 Note that at this point, old_loc->owner is still
11553 valid, as delete_breakpoint frees the breakpoint
11554 only after calling us. */
11555 warning (_("error removing breakpoint %d at %s"),
11556 old_loc->owner->number,
11557 paddress (old_loc->gdbarch, old_loc->address));
11559 removed = true;
11563 if (!found_object)
11565 if (removed && target_is_non_stop_p ()
11566 && need_moribund_for_location_type (old_loc))
11568 /* This location was removed from the target. In
11569 non-stop mode, a race condition is possible where
11570 we've removed a breakpoint, but stop events for that
11571 breakpoint are already queued and will arrive later.
11572 We apply an heuristic to be able to distinguish such
11573 SIGTRAPs from other random SIGTRAPs: we keep this
11574 breakpoint location for a bit, and will retire it
11575 after we see some number of events. The theory here
11576 is that reporting of events should, "on the average",
11577 be fair, so after a while we'll see events from all
11578 threads that have anything of interest, and no longer
11579 need to keep this breakpoint location around. We
11580 don't hold locations forever so to reduce chances of
11581 mistaking a non-breakpoint SIGTRAP for a breakpoint
11582 SIGTRAP.
11584 The heuristic failing can be disastrous on
11585 decr_pc_after_break targets.
11587 On decr_pc_after_break targets, like e.g., x86-linux,
11588 if we fail to recognize a late breakpoint SIGTRAP,
11589 because events_till_retirement has reached 0 too
11590 soon, we'll fail to do the PC adjustment, and report
11591 a random SIGTRAP to the user. When the user resumes
11592 the inferior, it will most likely immediately crash
11593 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11594 corrupted, because of being resumed e.g., in the
11595 middle of a multi-byte instruction, or skipped a
11596 one-byte instruction. This was actually seen happen
11597 on native x86-linux, and should be less rare on
11598 targets that do not support new thread events, like
11599 remote, due to the heuristic depending on
11600 thread_count.
11602 Mistaking a random SIGTRAP for a breakpoint trap
11603 causes similar symptoms (PC adjustment applied when
11604 it shouldn't), but then again, playing with SIGTRAPs
11605 behind the debugger's back is asking for trouble.
11607 Since hardware watchpoint traps are always
11608 distinguishable from other traps, so we don't need to
11609 apply keep hardware watchpoint moribund locations
11610 around. We simply always ignore hardware watchpoint
11611 traps we can no longer explain. */
11613 process_stratum_target *proc_target = nullptr;
11614 for (inferior *inf : all_inferiors ())
11615 if (inf->pspace == old_loc->pspace)
11617 proc_target = inf->process_target ();
11618 break;
11620 if (proc_target != nullptr)
11621 old_loc->events_till_retirement
11622 = 3 * (thread_count (proc_target) + 1);
11623 else
11624 old_loc->events_till_retirement = 1;
11625 old_loc->owner = NULL;
11627 moribund_locations.push_back (old_loc);
11629 else
11631 old_loc->owner = NULL;
11632 decref_bp_location (&old_loc);
11637 /* Rescan breakpoints at the same address and section, marking the
11638 first one as "first" and any others as "duplicates". This is so
11639 that the bpt instruction is only inserted once. If we have a
11640 permanent breakpoint at the same place as BPT, make that one the
11641 official one, and the rest as duplicates. Permanent breakpoints
11642 are sorted first for the same address.
11644 Do the same for hardware watchpoints, but also considering the
11645 watchpoint's type (regular/access/read) and length. */
11647 bp_loc_first = NULL;
11648 wp_loc_first = NULL;
11649 awp_loc_first = NULL;
11650 rwp_loc_first = NULL;
11652 for (bp_location *loc : all_bp_locations ())
11654 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11655 non-NULL. */
11656 struct bp_location **loc_first_p;
11657 breakpoint *b = loc->owner;
11659 if (!unduplicated_should_be_inserted (loc)
11660 || !bl_address_is_meaningful (loc)
11661 /* Don't detect duplicate for tracepoint locations because they are
11662 never duplicated. See the comments in field `duplicate' of
11663 `struct bp_location'. */
11664 || is_tracepoint (b))
11666 /* Clear the condition modification flag. */
11667 loc->condition_changed = condition_unchanged;
11668 continue;
11671 if (b->type == bp_hardware_watchpoint)
11672 loc_first_p = &wp_loc_first;
11673 else if (b->type == bp_read_watchpoint)
11674 loc_first_p = &rwp_loc_first;
11675 else if (b->type == bp_access_watchpoint)
11676 loc_first_p = &awp_loc_first;
11677 else
11678 loc_first_p = &bp_loc_first;
11680 if (*loc_first_p == NULL
11681 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11682 || !breakpoint_locations_match (loc, *loc_first_p))
11684 *loc_first_p = loc;
11685 loc->duplicate = 0;
11687 if (is_breakpoint (loc->owner) && loc->condition_changed)
11689 loc->needs_update = 1;
11690 /* Clear the condition modification flag. */
11691 loc->condition_changed = condition_unchanged;
11693 continue;
11697 /* This and the above ensure the invariant that the first location
11698 is not duplicated, and is the inserted one.
11699 All following are marked as duplicated, and are not inserted. */
11700 if (loc->inserted)
11701 swap_insertion (loc, *loc_first_p);
11702 loc->duplicate = 1;
11704 /* Clear the condition modification flag. */
11705 loc->condition_changed = condition_unchanged;
11708 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11710 if (insert_mode != UGLL_DONT_INSERT)
11711 insert_breakpoint_locations ();
11712 else
11714 /* Even though the caller told us to not insert new
11715 locations, we may still need to update conditions on the
11716 target's side of breakpoints that were already inserted
11717 if the target is evaluating breakpoint conditions. We
11718 only update conditions for locations that are marked
11719 "needs_update". */
11720 update_inserted_breakpoint_locations ();
11724 if (insert_mode != UGLL_DONT_INSERT)
11725 download_tracepoint_locations ();
11728 void
11729 breakpoint_retire_moribund (void)
11731 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11733 struct bp_location *loc = moribund_locations[ix];
11734 if (--(loc->events_till_retirement) == 0)
11736 decref_bp_location (&loc);
11737 unordered_remove (moribund_locations, ix);
11738 --ix;
11743 static void
11744 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11749 update_global_location_list (insert_mode);
11751 catch (const gdb_exception_error &e)
11756 /* Clear BKP from a BPS. */
11758 static void
11759 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
11761 bpstat *bs;
11763 for (bs = bps; bs; bs = bs->next)
11764 if (bs->breakpoint_at == bpt)
11766 bs->breakpoint_at = NULL;
11767 bs->old_val = NULL;
11768 /* bs->commands will be freed later. */
11772 /* Callback for iterate_over_threads. */
11773 static int
11774 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11776 struct breakpoint *bpt = (struct breakpoint *) data;
11778 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11779 return 0;
11782 /* See breakpoint.h. */
11784 void
11785 code_breakpoint::say_where () const
11787 struct value_print_options opts;
11789 get_user_print_options (&opts);
11791 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11792 single string. */
11793 if (!this->has_locations ())
11795 /* For pending locations, the output differs slightly based
11796 on extra_string. If this is non-NULL, it contains either
11797 a condition or dprintf arguments. */
11798 if (extra_string == NULL)
11800 gdb_printf (_(" (%s) pending."), locspec->to_string ());
11802 else if (type == bp_dprintf)
11804 gdb_printf (_(" (%s,%s) pending."),
11805 locspec->to_string (),
11806 extra_string.get ());
11808 else
11810 gdb_printf (_(" (%s %s) pending."),
11811 locspec->to_string (),
11812 extra_string.get ());
11815 else
11817 const bp_location &bl = this->first_loc ();
11818 if (opts.addressprint || bl.symtab == nullptr)
11819 gdb_printf (" at %ps",
11820 styled_string (address_style.style (),
11821 paddress (bl.gdbarch,
11822 bl.address)));
11823 if (bl.symtab != NULL)
11825 /* If there is a single location, we can print the location
11826 more nicely. */
11827 if (!this->has_multiple_locations ())
11829 const char *filename
11830 = symtab_to_filename_for_display (bl.symtab);
11831 gdb_printf (": file %ps, line %ps.",
11832 styled_string (file_name_style.style (),
11833 filename),
11834 styled_string (line_number_style.style (),
11835 pulongest (bl.line_number)));
11837 else
11838 /* This is not ideal, but each location may have a
11839 different file name, and this at least reflects the
11840 real situation somewhat. */
11841 gdb_printf (": %s.", locspec->to_string ());
11844 if (this->has_multiple_locations ())
11846 int n = std::distance (m_locations.begin (), m_locations.end ());
11847 gdb_printf (" (%d locations)", n);
11852 /* See breakpoint.h. */
11854 bp_location_range
11855 breakpoint::locations () const
11857 return bp_location_range (m_locations.begin (), m_locations.end ());
11860 struct bp_location *
11861 breakpoint::allocate_location ()
11863 return new bp_location (this);
11866 /* See breakpoint.h. */
11868 void
11869 breakpoint::add_location (bp_location &loc)
11871 gdb_assert (loc.owner == this);
11872 gdb_assert (!loc.is_linked ());
11874 auto ub = std::upper_bound (m_locations.begin (), m_locations.end (),
11875 loc,
11876 [] (const bp_location &left,
11877 const bp_location &right)
11878 { return left.address < right.address; });
11879 m_locations.insert (ub, loc);
11882 /* See breakpoint.h. */
11884 void
11885 breakpoint::unadd_location (bp_location &loc)
11887 gdb_assert (loc.owner == this);
11888 gdb_assert (loc.is_linked ());
11890 m_locations.erase (m_locations.iterator_to (loc));
11893 #define internal_error_pure_virtual_called() \
11894 gdb_assert_not_reached ("pure virtual function called")
11897 breakpoint::insert_location (struct bp_location *bl)
11899 internal_error_pure_virtual_called ();
11903 breakpoint::remove_location (struct bp_location *bl,
11904 enum remove_bp_reason reason)
11906 internal_error_pure_virtual_called ();
11910 breakpoint::breakpoint_hit (const struct bp_location *bl,
11911 const address_space *aspace,
11912 CORE_ADDR bp_addr,
11913 const target_waitstatus &ws)
11915 internal_error_pure_virtual_called ();
11919 breakpoint::resources_needed (const struct bp_location *bl)
11921 internal_error_pure_virtual_called ();
11924 enum print_stop_action
11925 breakpoint::print_it (const bpstat *bs) const
11927 internal_error_pure_virtual_called ();
11930 void
11931 breakpoint::print_mention () const
11933 internal_error_pure_virtual_called ();
11936 void
11937 breakpoint::print_recreate (struct ui_file *fp) const
11939 internal_error_pure_virtual_called ();
11942 /* Default breakpoint_ops methods. */
11944 void
11945 code_breakpoint::re_set (struct program_space *pspace)
11947 /* FIXME: is this still reachable? */
11948 if (breakpoint_location_spec_empty_p (this))
11950 /* Anything without a location can't be re-set. */
11951 delete_breakpoint (this);
11952 return;
11955 re_set_default (pspace);
11959 code_breakpoint::insert_location (struct bp_location *bl)
11961 CORE_ADDR addr = bl->target_info.reqstd_address;
11963 bl->target_info.kind = breakpoint_kind (bl, &addr);
11964 bl->target_info.placed_address = addr;
11966 int result;
11967 if (bl->loc_type == bp_loc_hardware_breakpoint)
11968 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
11969 else
11970 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11972 if (result == 0 && bl->probe.prob != nullptr)
11974 /* The insertion was successful, now let's set the probe's semaphore
11975 if needed. */
11976 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11979 return result;
11983 code_breakpoint::remove_location (struct bp_location *bl,
11984 enum remove_bp_reason reason)
11986 if (bl->probe.prob != nullptr)
11988 /* Let's clear the semaphore before removing the location. */
11989 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11992 if (bl->loc_type == bp_loc_hardware_breakpoint)
11993 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11994 else
11995 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
11999 code_breakpoint::breakpoint_hit (const struct bp_location *bl,
12000 const address_space *aspace,
12001 CORE_ADDR bp_addr,
12002 const target_waitstatus &ws)
12004 if (ws.kind () != TARGET_WAITKIND_STOPPED
12005 || ws.sig () != GDB_SIGNAL_TRAP)
12006 return 0;
12008 if (!breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
12009 aspace, bp_addr))
12010 return 0;
12012 if (overlay_debugging /* unmapped overlay section */
12013 && section_is_overlay (bl->section)
12014 && !section_is_mapped (bl->section))
12015 return 0;
12017 return 1;
12021 dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
12022 const address_space *aspace,
12023 CORE_ADDR bp_addr,
12024 const target_waitstatus &ws)
12026 if (dprintf_style == dprintf_style_agent
12027 && target_can_run_breakpoint_commands ())
12029 /* An agent-style dprintf never causes a stop. If we see a trap
12030 for this address it must be for a breakpoint that happens to
12031 be set at the same address. */
12032 return 0;
12035 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
12039 ordinary_breakpoint::resources_needed (const struct bp_location *bl)
12041 gdb_assert (type == bp_hardware_breakpoint);
12043 return 1;
12046 enum print_stop_action
12047 ordinary_breakpoint::print_it (const bpstat *bs) const
12049 const struct bp_location *bl;
12050 int bp_temp;
12051 struct ui_out *uiout = current_uiout;
12053 bl = bs->bp_location_at.get ();
12055 bp_temp = disposition == disp_del;
12056 if (bl->address != bl->requested_address)
12057 breakpoint_adjustment_warning (bl->requested_address,
12058 bl->address,
12059 number, true);
12060 annotate_breakpoint (number);
12061 maybe_print_thread_hit_breakpoint (uiout);
12063 if (uiout->is_mi_like_p ())
12065 uiout->field_string ("reason",
12066 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12067 uiout->field_string ("disp", bpdisp_text (disposition));
12070 if (bp_temp)
12071 uiout->text ("Temporary breakpoint ");
12072 else
12073 uiout->text ("Breakpoint ");
12074 print_num_locno (bs, uiout);
12075 uiout->text (", ");
12077 return PRINT_SRC_AND_LOC;
12080 void
12081 ordinary_breakpoint::print_mention () const
12083 if (current_uiout->is_mi_like_p ())
12084 return;
12086 switch (type)
12088 case bp_breakpoint:
12089 case bp_gnu_ifunc_resolver:
12090 if (disposition == disp_del)
12091 gdb_printf (_("Temporary breakpoint"));
12092 else
12093 gdb_printf (_("Breakpoint"));
12094 gdb_printf (_(" %d"), number);
12095 if (type == bp_gnu_ifunc_resolver)
12096 gdb_printf (_(" at gnu-indirect-function resolver"));
12097 break;
12098 case bp_hardware_breakpoint:
12099 gdb_printf (_("Hardware assisted breakpoint %d"), number);
12100 break;
12101 case bp_dprintf:
12102 gdb_printf (_("Dprintf %d"), number);
12103 break;
12106 say_where ();
12109 void
12110 ordinary_breakpoint::print_recreate (struct ui_file *fp) const
12112 if (type == bp_breakpoint && disposition == disp_del)
12113 gdb_printf (fp, "tbreak");
12114 else if (type == bp_breakpoint)
12115 gdb_printf (fp, "break");
12116 else if (type == bp_hardware_breakpoint
12117 && disposition == disp_del)
12118 gdb_printf (fp, "thbreak");
12119 else if (type == bp_hardware_breakpoint)
12120 gdb_printf (fp, "hbreak");
12121 else
12122 internal_error (_("unhandled breakpoint type %d"), (int) type);
12124 gdb_printf (fp, " %s", locspec->to_string ());
12126 /* Print out extra_string if this breakpoint is pending. It might
12127 contain, for example, conditions that were set by the user. */
12128 if (!this->has_locations () && extra_string != NULL)
12129 gdb_printf (fp, " %s", extra_string.get ());
12131 print_recreate_thread (fp);
12134 std::vector<symtab_and_line>
12135 code_breakpoint::decode_location_spec (location_spec *locspec,
12136 program_space *search_pspace)
12138 if (locspec->type () == PROBE_LOCATION_SPEC)
12139 return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
12141 struct linespec_result canonical;
12143 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
12144 NULL, 0, &canonical, multiple_symbols_all,
12145 filter.get ());
12147 /* We should get 0 or 1 resulting SALs. */
12148 gdb_assert (canonical.lsals.size () < 2);
12150 if (!canonical.lsals.empty ())
12152 const linespec_sals &lsal = canonical.lsals[0];
12153 return std::move (lsal.sals);
12155 return {};
12158 /* Virtual table for internal breakpoints. */
12160 void
12161 internal_breakpoint::re_set (struct program_space *pspace)
12163 switch (type)
12165 /* Delete overlay event and longjmp master breakpoints; they
12166 will be reset later by breakpoint_re_set. */
12167 case bp_overlay_event:
12168 case bp_longjmp_master:
12169 case bp_std_terminate_master:
12170 case bp_exception_master:
12171 delete_breakpoint (this);
12172 break;
12174 /* This breakpoint is special, it's set up when the inferior
12175 starts and we really don't want to touch it. */
12176 case bp_shlib_event:
12178 /* Like bp_shlib_event, this breakpoint type is special. Once
12179 it is set up, we do not want to touch it. */
12180 case bp_thread_event:
12181 break;
12185 void
12186 internal_breakpoint::check_status (bpstat *bs)
12188 if (type == bp_shlib_event)
12190 /* If requested, stop when the dynamic linker notifies GDB of
12191 events. This allows the user to get control and place
12192 breakpoints in initializer routines for dynamically loaded
12193 objects (among other things). */
12194 bs->stop = stop_on_solib_events != 0;
12195 bs->print = stop_on_solib_events != 0;
12197 else
12198 bs->stop = false;
12201 enum print_stop_action
12202 internal_breakpoint::print_it (const bpstat *bs) const
12204 switch (type)
12206 case bp_shlib_event:
12207 /* Did we stop because the user set the stop_on_solib_events
12208 variable? (If so, we report this as a generic, "Stopped due
12209 to shlib event" message.) */
12210 print_solib_event (false);
12211 break;
12213 case bp_thread_event:
12214 /* Not sure how we will get here.
12215 GDB should not stop for these breakpoints. */
12216 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
12217 break;
12219 case bp_overlay_event:
12220 /* By analogy with the thread event, GDB should not stop for these. */
12221 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12222 break;
12224 case bp_longjmp_master:
12225 /* These should never be enabled. */
12226 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12227 break;
12229 case bp_std_terminate_master:
12230 /* These should never be enabled. */
12231 gdb_printf (_("std::terminate Master Breakpoint: "
12232 "gdb should not stop!\n"));
12233 break;
12235 case bp_exception_master:
12236 /* These should never be enabled. */
12237 gdb_printf (_("Exception Master Breakpoint: "
12238 "gdb should not stop!\n"));
12239 break;
12242 return PRINT_NOTHING;
12245 void
12246 internal_breakpoint::print_mention () const
12248 /* Nothing to mention. These breakpoints are internal. */
12251 /* Virtual table for momentary breakpoints */
12253 void
12254 momentary_breakpoint::re_set (struct program_space *pspace)
12256 /* Keep temporary breakpoints, which can be encountered when we step
12257 over a dlopen call and solib_add is resetting the breakpoints.
12258 Otherwise these should have been blown away via the cleanup chain
12259 or by breakpoint_init_inferior when we rerun the executable. */
12262 void
12263 momentary_breakpoint::check_status (bpstat *bs)
12265 /* Nothing. The point of these breakpoints is causing a stop. */
12268 enum print_stop_action
12269 momentary_breakpoint::print_it (const bpstat *bs) const
12271 return PRINT_UNKNOWN;
12274 void
12275 momentary_breakpoint::print_mention () const
12277 /* Nothing to mention. These breakpoints are internal. */
12280 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12282 It gets cleared already on the removal of the first one of such placed
12283 breakpoints. This is OK as they get all removed altogether. */
12285 longjmp_breakpoint::~longjmp_breakpoint ()
12287 thread_info *tp = find_thread_global_id (this->thread);
12289 if (tp != NULL)
12290 tp->initiating_frame = null_frame_id;
12293 static void
12294 bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
12295 struct linespec_result *canonical,
12296 struct program_space *search_pspace)
12299 struct linespec_sals lsal;
12301 lsal.sals = parse_probes (locspec, search_pspace, canonical);
12302 lsal.canonical = xstrdup (canonical->locspec->to_string ());
12303 canonical->lsals.push_back (std::move (lsal));
12306 static std::vector<symtab_and_line>
12307 bkpt_probe_decode_location_spec (struct breakpoint *b,
12308 location_spec *locspec,
12309 program_space *search_pspace)
12311 std::vector<symtab_and_line> sals
12312 = parse_probes (locspec, search_pspace, NULL);
12313 if (sals.empty ())
12314 error (_("probe not found"));
12315 return sals;
12319 tracepoint::breakpoint_hit (const struct bp_location *bl,
12320 const address_space *aspace, CORE_ADDR bp_addr,
12321 const target_waitstatus &ws)
12323 /* By definition, the inferior does not report stops at
12324 tracepoints. */
12325 return 0;
12328 void
12329 tracepoint::print_one_detail (struct ui_out *uiout) const
12331 if (!static_trace_marker_id.empty ())
12333 gdb_assert (type == bp_static_tracepoint
12334 || type == bp_static_marker_tracepoint);
12336 uiout->message ("\tmarker id is %pF\n",
12337 string_field ("static-tracepoint-marker-string-id",
12338 static_trace_marker_id.c_str ()));
12342 void
12343 tracepoint::print_mention () const
12345 if (current_uiout->is_mi_like_p ())
12346 return;
12348 switch (type)
12350 case bp_tracepoint:
12351 gdb_printf (_("Tracepoint"));
12352 gdb_printf (_(" %d"), number);
12353 break;
12354 case bp_fast_tracepoint:
12355 gdb_printf (_("Fast tracepoint"));
12356 gdb_printf (_(" %d"), number);
12357 break;
12358 case bp_static_tracepoint:
12359 case bp_static_marker_tracepoint:
12360 gdb_printf (_("Static tracepoint"));
12361 gdb_printf (_(" %d"), number);
12362 break;
12363 default:
12364 internal_error (_("unhandled tracepoint type %d"), (int) type);
12367 say_where ();
12370 void
12371 tracepoint::print_recreate (struct ui_file *fp) const
12373 if (type == bp_fast_tracepoint)
12374 gdb_printf (fp, "ftrace");
12375 else if (type == bp_static_tracepoint
12376 || type == bp_static_marker_tracepoint)
12377 gdb_printf (fp, "strace");
12378 else if (type == bp_tracepoint)
12379 gdb_printf (fp, "trace");
12380 else
12381 internal_error (_("unhandled tracepoint type %d"), (int) type);
12383 gdb_printf (fp, " %s", locspec->to_string ());
12384 print_recreate_thread (fp);
12386 if (pass_count)
12387 gdb_printf (fp, " passcount %d\n", pass_count);
12390 void
12391 dprintf_breakpoint::re_set (struct program_space *pspace)
12393 re_set_default (pspace);
12395 /* 1 - connect to target 1, that can run breakpoint commands.
12396 2 - create a dprintf, which resolves fine.
12397 3 - disconnect from target 1
12398 4 - connect to target 2, that can NOT run breakpoint commands.
12400 After steps #3/#4, you'll want the dprintf command list to
12401 be updated, because target 1 and 2 may well return different
12402 answers for target_can_run_breakpoint_commands().
12403 Given absence of finer grained resetting, we get to do
12404 it all the time. */
12405 update_dprintf_command_list (this);
12408 /* Implement the "print_recreate" method for dprintf. */
12410 void
12411 dprintf_breakpoint::print_recreate (struct ui_file *fp) const
12413 gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
12414 print_recreate_thread (fp);
12417 /* Implement the "after_condition_true" method for dprintf.
12419 dprintf's are implemented with regular commands in their command
12420 list, but we run the commands here instead of before presenting the
12421 stop to the user, as dprintf's don't actually cause a stop. This
12422 also makes it so that the commands of multiple dprintfs at the same
12423 address are all handled. */
12425 void
12426 dprintf_breakpoint::after_condition_true (struct bpstat *bs)
12428 /* dprintf's never cause a stop. This wasn't set in the
12429 check_status hook instead because that would make the dprintf's
12430 condition not be evaluated. */
12431 bs->stop = false;
12433 /* Run the command list here. Take ownership of it instead of
12434 copying. We never want these commands to run later in
12435 bpstat_do_actions, if a breakpoint that causes a stop happens to
12436 be set at same address as this dprintf, or even if running the
12437 commands here throws. */
12438 counted_command_line cmds = std::move (bs->commands);
12439 gdb_assert (cmds != nullptr);
12440 execute_control_commands (cmds.get (), 0);
12443 /* The breakpoint_ops structure to be used on static tracepoints with
12444 markers (`-m'). */
12446 static void
12447 strace_marker_create_sals_from_location_spec
12448 (location_spec *locspec,
12449 struct linespec_result *canonical,
12450 struct program_space *search_pspace)
12452 struct linespec_sals lsal;
12453 const char *arg_start, *arg;
12455 arg = arg_start = as_linespec_location_spec (locspec)->spec_string.get ();
12456 lsal.sals = decode_static_tracepoint_spec (&arg);
12458 std::string str (arg_start, arg - arg_start);
12459 const char *ptr = str.c_str ();
12460 canonical->locspec
12461 = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
12463 lsal.canonical = xstrdup (canonical->locspec->to_string ());
12464 canonical->lsals.push_back (std::move (lsal));
12467 static void
12468 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12469 struct linespec_result *canonical,
12470 gdb::unique_xmalloc_ptr<char> cond_string,
12471 gdb::unique_xmalloc_ptr<char> extra_string,
12472 enum bptype type_wanted,
12473 enum bpdisp disposition,
12474 int thread,
12475 int task, int inferior,
12476 int ignore_count,
12477 int from_tty, int enabled,
12478 int internal, unsigned flags)
12480 const linespec_sals &lsal = canonical->lsals[0];
12482 /* If the user is creating a static tracepoint by marker id
12483 (strace -m MARKER_ID), then store the sals index, so that
12484 breakpoint_re_set can try to match up which of the newly
12485 found markers corresponds to this one, and, don't try to
12486 expand multiple locations for each sal, given than SALS
12487 already should contain all sals for MARKER_ID. */
12489 for (size_t i = 0; i < lsal.sals.size (); i++)
12491 location_spec_up locspec = canonical->locspec->clone ();
12493 std::unique_ptr<tracepoint> tp
12494 (new tracepoint (gdbarch,
12495 type_wanted,
12496 lsal.sals[i],
12497 std::move (locspec),
12498 NULL,
12499 std::move (cond_string),
12500 std::move (extra_string),
12501 disposition,
12502 thread, task, inferior, ignore_count,
12503 from_tty, enabled, flags,
12504 canonical->special_display));
12506 /* Given that its possible to have multiple markers with
12507 the same string id, if the user is creating a static
12508 tracepoint by marker id ("strace -m MARKER_ID"), then
12509 store the sals index, so that breakpoint_re_set can
12510 try to match up which of the newly found markers
12511 corresponds to this one */
12512 tp->static_trace_marker_id_idx = i;
12514 install_breakpoint (internal, std::move (tp), 0);
12518 std::vector<symtab_and_line>
12519 static_marker_tracepoint::decode_location_spec (location_spec *locspec,
12520 program_space *search_pspace)
12522 const char *s = as_linespec_location_spec (locspec)->spec_string.get ();
12524 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12525 if (sals.size () > static_trace_marker_id_idx)
12527 sals[0] = sals[static_trace_marker_id_idx];
12528 sals.resize (1);
12529 return sals;
12531 else
12532 error (_("marker %s not found"), static_trace_marker_id.c_str ());
12535 /* Static tracepoints with marker (`-m'). */
12536 static struct breakpoint_ops strace_marker_breakpoint_ops =
12538 strace_marker_create_sals_from_location_spec,
12539 strace_marker_create_breakpoints_sal,
12542 static bool
12543 strace_marker_p (struct breakpoint *b)
12545 return b->type == bp_static_marker_tracepoint;
12548 /* Notify interpreters and observers that breakpoint B was deleted. */
12550 static void
12551 notify_breakpoint_deleted (breakpoint *b)
12553 interps_notify_breakpoint_deleted (b);
12554 gdb::observers::breakpoint_deleted.notify (b);
12557 /* Delete a breakpoint and clean up all traces of it in the data
12558 structures. */
12560 void
12561 delete_breakpoint (struct breakpoint *bpt)
12563 gdb_assert (bpt != NULL);
12565 /* Has this bp already been deleted? This can happen because
12566 multiple lists can hold pointers to bp's. bpstat lists are
12567 especial culprits.
12569 One example of this happening is a watchpoint's scope bp. When
12570 the scope bp triggers, we notice that the watchpoint is out of
12571 scope, and delete it. We also delete its scope bp. But the
12572 scope bp is marked "auto-deleting", and is already on a bpstat.
12573 That bpstat is then checked for auto-deleting bp's, which are
12574 deleted.
12576 A real solution to this problem might involve reference counts in
12577 bp's, and/or giving them pointers back to their referencing
12578 bpstat's, and teaching delete_breakpoint to only free a bp's
12579 storage when no more references were extent. A cheaper bandaid
12580 was chosen. */
12581 if (bpt->type == bp_none)
12582 return;
12584 /* At least avoid this stale reference until the reference counting
12585 of breakpoints gets resolved. */
12586 if (bpt->related_breakpoint != bpt)
12588 struct breakpoint *related;
12589 struct watchpoint *w;
12591 if (bpt->type == bp_watchpoint_scope)
12592 w = gdb::checked_static_cast<watchpoint *> (bpt->related_breakpoint);
12593 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12594 w = gdb::checked_static_cast<watchpoint *> (bpt);
12595 else
12596 w = NULL;
12597 if (w != NULL)
12598 watchpoint_del_at_next_stop (w);
12600 /* Unlink bpt from the bpt->related_breakpoint ring. */
12601 for (related = bpt; related->related_breakpoint != bpt;
12602 related = related->related_breakpoint);
12603 related->related_breakpoint = bpt->related_breakpoint;
12604 bpt->related_breakpoint = bpt;
12607 /* watch_command_1 creates a watchpoint but only sets its number if
12608 update_watchpoint succeeds in creating its bp_locations. If there's
12609 a problem in that process, we'll be asked to delete the half-created
12610 watchpoint. In that case, don't announce the deletion. */
12611 if (bpt->number)
12612 notify_breakpoint_deleted (bpt);
12614 breakpoint_chain.erase (breakpoint_chain.iterator_to (*bpt));
12616 /* Be sure no bpstat's are pointing at the breakpoint after it's
12617 been freed. */
12618 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12619 in all threads for now. Note that we cannot just remove bpstats
12620 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12621 commands are associated with the bpstat; if we remove it here,
12622 then the later call to bpstat_do_actions (&stop_bpstat); in
12623 event-top.c won't do anything, and temporary breakpoints with
12624 commands won't work. */
12626 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12628 /* Now that breakpoint is removed from breakpoint list, update the
12629 global location list. This will remove locations that used to
12630 belong to this breakpoint. Do this before freeing the breakpoint
12631 itself, since remove_breakpoint looks at location's owner. It
12632 might be better design to have location completely
12633 self-contained, but it's not the case now.
12635 Clear the location linked list first, otherwise, the intrusive_list
12636 destructor accesses the locations after they are freed. */
12637 bpt->clear_locations ();
12638 update_global_location_list (UGLL_DONT_INSERT);
12640 /* On the chance that someone will soon try again to delete this
12641 same bp, we mark it as deleted before freeing its storage. */
12642 bpt->type = bp_none;
12643 delete bpt;
12646 /* Iterator function to call a user-provided callback function once
12647 for each of B and its related breakpoints. */
12649 static void
12650 iterate_over_related_breakpoints (struct breakpoint *b,
12651 gdb::function_view<void (breakpoint *)> function)
12653 struct breakpoint *related;
12655 related = b;
12658 struct breakpoint *next;
12660 /* FUNCTION may delete RELATED. */
12661 next = related->related_breakpoint;
12663 if (next == related)
12665 /* RELATED is the last ring entry. */
12666 function (related);
12668 /* FUNCTION may have deleted it, so we'd never reach back to
12669 B. There's nothing left to do anyway, so just break
12670 out. */
12671 break;
12673 else
12674 function (related);
12676 related = next;
12678 while (related != b);
12681 static void
12682 delete_command (const char *arg, int from_tty)
12684 dont_repeat ();
12686 if (arg == 0)
12688 int breaks_to_delete = 0;
12690 /* Delete all breakpoints, watchpoints, tracepoints, and catchpoints
12691 if no argument. Do not delete internal breakpoints, these have to
12692 be deleted with an explicit breakpoint number argument. */
12693 for (breakpoint &b : all_breakpoints ())
12694 if (user_breakpoint_p (&b))
12696 breaks_to_delete = 1;
12697 break;
12700 /* Ask user only if there are some breakpoints to delete. */
12701 if (!from_tty
12702 || (breaks_to_delete
12703 && query (_("Delete all breakpoints, watchpoints, tracepoints, "
12704 "and catchpoints? "))))
12705 for (breakpoint &b : all_breakpoints_safe ())
12706 if (user_breakpoint_p (&b))
12707 delete_breakpoint (&b);
12709 else
12710 map_breakpoint_numbers
12711 (arg, [&] (breakpoint *br)
12713 iterate_over_related_breakpoints (br, delete_breakpoint);
12717 /* Return true if all locations of B bound to PSPACE are pending. If
12718 PSPACE is NULL, all locations of all program spaces are
12719 considered. */
12721 static bool
12722 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
12724 for (bp_location &loc : b->locations ())
12725 if ((pspace == NULL
12726 || loc.pspace == pspace)
12727 && !loc.shlib_disabled
12728 && !loc.pspace->executing_startup)
12729 return false;
12730 return true;
12733 /* Subroutine of update_breakpoint_locations to simplify it.
12734 Return true if multiple fns in list LOCS have the same name.
12735 Null names are ignored. */
12737 static bool
12738 ambiguous_names_p (const bp_location_range &locs)
12740 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
12741 xcalloc, xfree));
12743 for (const bp_location &l : locs)
12745 const char **slot;
12746 const char *name = l.function_name.get ();
12748 /* Allow for some names to be NULL, ignore them. */
12749 if (name == NULL)
12750 continue;
12752 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
12753 INSERT);
12754 /* NOTE: We can assume slot != NULL here because xcalloc never
12755 returns NULL. */
12756 if (*slot != NULL)
12757 return true;
12758 *slot = name;
12761 return false;
12764 /* When symbols change, it probably means the sources changed as well,
12765 and it might mean the static tracepoint markers are no longer at
12766 the same address or line numbers they used to be at last we
12767 checked. Losing your static tracepoints whenever you rebuild is
12768 undesirable. This function tries to resync/rematch gdb static
12769 tracepoints with the markers on the target, for static tracepoints
12770 that have not been set by marker id. Static tracepoint that have
12771 been set by marker id are reset by marker id in breakpoint_re_set.
12772 The heuristic is:
12774 1) For a tracepoint set at a specific address, look for a marker at
12775 the old PC. If one is found there, assume to be the same marker.
12776 If the name / string id of the marker found is different from the
12777 previous known name, assume that means the user renamed the marker
12778 in the sources, and output a warning.
12780 2) For a tracepoint set at a given line number, look for a marker
12781 at the new address of the old line number. If one is found there,
12782 assume to be the same marker. If the name / string id of the
12783 marker found is different from the previous known name, assume that
12784 means the user renamed the marker in the sources, and output a
12785 warning.
12787 3) If a marker is no longer found at the same address or line, it
12788 may mean the marker no longer exists. But it may also just mean
12789 the code changed a bit. Maybe the user added a few lines of code
12790 that made the marker move up or down (in line number terms). Ask
12791 the target for info about the marker with the string id as we knew
12792 it. If found, update line number and address in the matching
12793 static tracepoint. This will get confused if there's more than one
12794 marker with the same ID (possible in UST, although unadvised
12795 precisely because it confuses tools). */
12797 static struct symtab_and_line
12798 update_static_tracepoint (tracepoint *tp, struct symtab_and_line sal)
12800 struct static_tracepoint_marker marker;
12801 CORE_ADDR pc;
12803 pc = sal.pc;
12804 if (sal.line)
12805 find_line_pc (sal.symtab, sal.line, &pc);
12807 if (target_static_tracepoint_marker_at (pc, &marker))
12809 if (tp->static_trace_marker_id != marker.str_id)
12810 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12811 tp->number, tp->static_trace_marker_id.c_str (),
12812 marker.str_id.c_str ());
12814 tp->static_trace_marker_id = std::move (marker.str_id);
12816 return sal;
12819 /* Old marker wasn't found on target at lineno. Try looking it up
12820 by string ID. */
12821 if (!sal.explicit_pc
12822 && sal.line != 0
12823 && sal.symtab != NULL
12824 && !tp->static_trace_marker_id.empty ())
12826 std::vector<static_tracepoint_marker> markers
12827 = target_static_tracepoint_markers_by_strid
12828 (tp->static_trace_marker_id.c_str ());
12830 if (!markers.empty ())
12832 struct symbol *sym;
12833 struct static_tracepoint_marker *tpmarker;
12834 struct ui_out *uiout = current_uiout;
12836 tpmarker = &markers[0];
12838 tp->static_trace_marker_id = std::move (tpmarker->str_id);
12840 warning (_("marker for static tracepoint %d (%s) not "
12841 "found at previous line number"),
12842 tp->number, tp->static_trace_marker_id.c_str ());
12844 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
12845 sym = find_pc_sect_function (tpmarker->address, NULL);
12846 uiout->text ("Now in ");
12847 if (sym)
12849 uiout->field_string ("func", sym->print_name (),
12850 function_name_style.style ());
12851 uiout->text (" at ");
12853 uiout->field_string ("file",
12854 symtab_to_filename_for_display (sal2.symtab),
12855 file_name_style.style ());
12856 uiout->text (":");
12858 if (uiout->is_mi_like_p ())
12860 const char *fullname = symtab_to_fullname (sal2.symtab);
12862 uiout->field_string ("fullname", fullname);
12865 uiout->field_signed ("line", sal2.line, line_number_style.style ());
12866 uiout->text ("\n");
12868 tp->first_loc ().line_number = sal2.line;
12869 tp->first_loc ().symtab = sym != NULL ? sal2.symtab : NULL;
12871 std::unique_ptr<explicit_location_spec> els
12872 (new explicit_location_spec
12873 (symtab_to_filename_for_display (sal2.symtab)));
12874 els->line_offset.offset = tp->first_loc ().line_number;
12875 els->line_offset.sign = LINE_OFFSET_NONE;
12877 tp->locspec = std::move (els);
12879 /* Might be nice to check if function changed, and warn if
12880 so. */
12883 return sal;
12886 /* Returns true iff location lists A and B are sufficiently same that
12887 we don't need to report breakpoint as changed. */
12889 static bool
12890 locations_are_equal (const bp_location_list &a, const bp_location_range &b)
12892 auto a_iter = a.begin ();
12893 auto b_iter = b.begin ();
12895 for (; a_iter != a.end () && b_iter != b.end (); ++a_iter, ++b_iter)
12897 if (a_iter->address != b_iter->address)
12898 return false;
12900 if (a_iter->shlib_disabled != b_iter->shlib_disabled)
12901 return false;
12903 if (a_iter->enabled != b_iter->enabled)
12904 return false;
12906 if (a_iter->disabled_by_cond != b_iter->disabled_by_cond)
12907 return false;
12909 /* When a breakpoint is set by address, it is not created as
12910 pending; but then during an solib event or the like it may
12911 acquire a symbol. So, check this here. */
12912 if (a_iter->symbol != b_iter->symbol
12913 || a_iter->msymbol != b_iter->msymbol)
12914 return false;
12917 return (a_iter == a.end ()) == (b_iter == b.end ());
12920 /* See breakpoint.h. */
12922 bp_location_list
12923 breakpoint::steal_locations (program_space *pspace)
12925 if (pspace == NULL)
12926 return std::move (m_locations);
12928 bp_location_list ret;
12930 for (auto it = m_locations.begin (); it != m_locations.end (); )
12932 if (it->pspace == pspace)
12934 bp_location &loc = *it;
12935 it = m_locations.erase (it);
12936 ret.push_back (loc);
12938 else
12939 ++it;
12942 return ret;
12945 /* Create new breakpoint locations for B (a hardware or software
12946 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12947 zero, then B is a ranged breakpoint. Only recreates locations for
12948 FILTER_PSPACE. Locations of other program spaces are left
12949 untouched. */
12951 void
12952 update_breakpoint_locations (code_breakpoint *b,
12953 struct program_space *filter_pspace,
12954 gdb::array_view<const symtab_and_line> sals,
12955 gdb::array_view<const symtab_and_line> sals_end)
12957 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
12959 /* Ranged breakpoints have only one start location and one end
12960 location. */
12961 b->enable_state = bp_disabled;
12962 gdb_printf (gdb_stderr,
12963 _("Could not reset ranged breakpoint %d: "
12964 "multiple locations found\n"),
12965 b->number);
12966 return;
12969 /* If there's no new locations, and all existing locations are
12970 pending, don't do anything. This optimizes the common case where
12971 all locations are in the same shared library, that was unloaded.
12972 We'd like to retain the location, so that when the library is
12973 loaded again, we don't loose the enabled/disabled status of the
12974 individual locations. */
12975 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
12976 return;
12978 bp_location_list existing_locations = b->steal_locations (filter_pspace);
12980 for (const auto &sal : sals)
12982 struct bp_location *new_loc;
12984 switch_to_program_space_and_thread (sal.pspace);
12986 new_loc = b->add_location (sal);
12988 /* Reparse conditions, they might contain references to the
12989 old symtab. */
12990 if (b->cond_string != NULL)
12992 const char *s;
12994 s = b->cond_string.get ();
12997 new_loc->cond = parse_exp_1 (&s, sal.pc,
12998 block_for_pc (sal.pc),
13001 catch (const gdb_exception_error &e)
13003 new_loc->disabled_by_cond = true;
13007 if (!sals_end.empty ())
13009 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13011 new_loc->length = end - sals[0].pc + 1;
13015 /* If possible, carry over 'disable' status from existing
13016 breakpoints. */
13018 /* If there are multiple breakpoints with the same function name,
13019 e.g. for inline functions, comparing function names won't work.
13020 Instead compare pc addresses; this is just a heuristic as things
13021 may have moved, but in practice it gives the correct answer
13022 often enough until a better solution is found. */
13023 int have_ambiguous_names = ambiguous_names_p (b->locations ());
13025 for (const bp_location &e : existing_locations)
13027 if ((!e.enabled || e.disabled_by_cond) && e.function_name)
13029 if (have_ambiguous_names)
13031 for (bp_location &l : b->locations ())
13033 /* Ignore software vs hardware location type at
13034 this point, because with "set breakpoint
13035 auto-hw", after a re-set, locations that were
13036 hardware can end up as software, or vice versa.
13037 As mentioned above, this is an heuristic and in
13038 practice should give the correct answer often
13039 enough. */
13040 if (breakpoint_locations_match (&e, &l, true))
13042 l.enabled = e.enabled;
13043 l.disabled_by_cond = e.disabled_by_cond;
13044 break;
13048 else
13050 for (bp_location &l : b->locations ())
13051 if (l.function_name
13052 && strcmp (e.function_name.get (),
13053 l.function_name.get ()) == 0)
13055 l.enabled = e.enabled;
13056 l.disabled_by_cond = e.disabled_by_cond;
13057 break;
13064 if (!locations_are_equal (existing_locations, b->locations ()))
13065 notify_breakpoint_modified (b);
13068 /* Find the SaL locations corresponding to the given LOCSPEC.
13069 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13071 std::vector<symtab_and_line>
13072 code_breakpoint::location_spec_to_sals (location_spec *locspec,
13073 struct program_space *search_pspace,
13074 int *found)
13076 struct gdb_exception exception;
13078 std::vector<symtab_and_line> sals;
13082 sals = decode_location_spec (locspec, search_pspace);
13084 catch (gdb_exception_error &e)
13086 int not_found_and_ok = false;
13088 /* For pending breakpoints, it's expected that parsing will
13089 fail until the right shared library is loaded. User has
13090 already told to create pending breakpoints and don't need
13091 extra messages. If breakpoint is in bp_shlib_disabled
13092 state, then user already saw the message about that
13093 breakpoint being disabled, and don't want to see more
13094 errors. */
13095 if (e.error == NOT_FOUND_ERROR
13096 && (condition_not_parsed
13097 || (this->has_locations ()
13098 && search_pspace != NULL
13099 && this->first_loc ().pspace != search_pspace)
13100 || (this->has_locations () && this->first_loc ().shlib_disabled)
13101 || (this->has_locations ()
13102 && this->first_loc ().pspace->executing_startup)
13103 || enable_state == bp_disabled))
13104 not_found_and_ok = true;
13106 if (!not_found_and_ok)
13108 /* We surely don't want to warn about the same breakpoint
13109 10 times. One solution, implemented here, is disable
13110 the breakpoint on error. Another solution would be to
13111 have separate 'warning emitted' flag. Since this
13112 happens only when a binary has changed, I don't know
13113 which approach is better. */
13114 enable_state = bp_disabled;
13115 throw;
13118 exception = std::move (e);
13121 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13123 for (auto &sal : sals)
13124 resolve_sal_pc (&sal);
13126 if (type == bp_static_tracepoint)
13128 tracepoint *t = gdb::checked_static_cast<tracepoint *> (this);
13129 sals[0] = update_static_tracepoint (t, sals[0]);
13132 *found = 1;
13134 else
13135 *found = 0;
13137 return sals;
13140 /* The default re_set method, for typical hardware or software
13141 breakpoints. Reevaluate the breakpoint and recreate its
13142 locations. */
13144 void
13145 code_breakpoint::re_set_default (struct program_space *filter_pspace)
13147 std::vector<symtab_and_line> expanded, expanded_end;
13149 /* If this breakpoint is thread- or inferior-specific, then find the
13150 program space in which this breakpoint exists. Otherwise, for
13151 breakpoints that are not thread- or inferior-specific, BP_PSPACE will
13152 be nullptr. */
13153 program_space *bp_pspace
13154 = find_program_space_for_breakpoint (this->thread, this->inferior);
13156 /* If this is not a thread or inferior specific breakpoint, or it is a
13157 thread or inferior specific breakpoint but we are looking for new
13158 locations in the program space that the specific thread or inferior is
13159 running, then look for new locations for this breakpoint. */
13160 if (bp_pspace == nullptr || filter_pspace == bp_pspace)
13162 int found;
13163 std::vector<symtab_and_line> sals
13164 = location_spec_to_sals (locspec.get (), filter_pspace, &found);
13165 if (found)
13166 expanded = std::move (sals);
13168 if (locspec_range_end != nullptr)
13170 std::vector<symtab_and_line> sals_end
13171 = location_spec_to_sals (locspec_range_end.get (),
13172 filter_pspace, &found);
13173 if (found)
13174 expanded_end = std::move (sals_end);
13178 /* Update the locations for this breakpoint. For thread-specific
13179 breakpoints this will remove any old locations that are for the wrong
13180 program space -- this can happen if the user changes the thread of a
13181 thread-specific breakpoint. */
13182 update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
13185 /* Re-set breakpoint locations for the current program space.
13186 Locations bound to other program spaces are left untouched. */
13188 void
13189 breakpoint_re_set (void)
13192 scoped_restore_current_language save_language;
13193 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13194 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13196 /* To ::re_set each breakpoint we set the current_language to the
13197 language of the breakpoint before re-evaluating the breakpoint's
13198 location. This change can unfortunately get undone by accident if
13199 the language_mode is set to auto, and we either switch frames, or
13200 more likely in this context, we select the current frame.
13202 We prevent this by temporarily turning the language_mode to
13203 language_mode_manual. We restore it once all breakpoints
13204 have been reset. */
13205 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13206 language_mode = language_mode_manual;
13208 /* Note: we must not try to insert locations until after all
13209 breakpoints have been re-set. Otherwise, e.g., when re-setting
13210 breakpoint 1, we'd insert the locations of breakpoint 2, which
13211 hadn't been re-set yet, and thus may have stale locations. */
13213 for (breakpoint &b : all_breakpoints_safe ())
13217 input_radix = b.input_radix;
13218 set_language (b.language);
13219 b.re_set (current_program_space);
13221 catch (const gdb_exception &ex)
13223 exception_fprintf (gdb_stderr, ex,
13224 "Error in re-setting breakpoint %d: ",
13225 b.number);
13229 jit_breakpoint_re_set ();
13232 create_overlay_event_breakpoint ();
13233 create_longjmp_master_breakpoint ();
13234 create_std_terminate_master_breakpoint ();
13235 create_exception_master_breakpoint ();
13237 /* Now we can insert. */
13238 update_global_location_list (UGLL_MAY_INSERT);
13241 /* Re-set locations for breakpoint B in FILTER_PSPACE. If FILTER_PSPACE is
13242 nullptr then re-set locations for B in all program spaces. Locations
13243 bound to program spaces other than FILTER_PSPACE are left untouched. */
13245 static void
13246 breakpoint_re_set_one (breakpoint *b, program_space *filter_pspace)
13249 scoped_restore_current_language save_language;
13250 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13251 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13253 /* To ::re_set each breakpoint we set the current_language to the
13254 language of the breakpoint before re-evaluating the breakpoint's
13255 location. This change can unfortunately get undone by accident if
13256 the language_mode is set to auto, and we either switch frames, or
13257 more likely in this context, we select the current frame.
13259 We prevent this by temporarily turning the language_mode to
13260 language_mode_manual. We restore it once all breakpoints
13261 have been reset. */
13262 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13263 language_mode = language_mode_manual;
13265 /* Note: we must not try to insert locations until after all
13266 breakpoints have been re-set. Otherwise, e.g., when re-setting
13267 breakpoint 1, we'd insert the locations of breakpoint 2, which
13268 hadn't been re-set yet, and thus may have stale locations. */
13272 input_radix = b->input_radix;
13273 set_language (b->language);
13274 b->re_set (filter_pspace);
13276 catch (const gdb_exception &ex)
13278 exception_fprintf (gdb_stderr, ex,
13279 "Error in re-setting breakpoint %d: ",
13280 b->number);
13284 /* Now we can insert. */
13285 update_global_location_list (UGLL_MAY_INSERT);
13288 /* Reset the thread number of this breakpoint:
13290 - If the breakpoint is for all threads, leave it as-is.
13291 - Else, reset it to the current thread for inferior_ptid. */
13292 void
13293 breakpoint_re_set_thread (struct breakpoint *b)
13295 if (b->thread != -1)
13297 b->thread = inferior_thread ()->global_num;
13299 /* We're being called after following a fork. The new fork is
13300 selected as current, and unless this was a vfork will have a
13301 different program space from the original thread. Reset that
13302 as well. */
13303 b->first_loc ().pspace = current_program_space;
13307 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13308 If from_tty is nonzero, it prints a message to that effect,
13309 which ends with a period (no newline). */
13311 void
13312 set_ignore_count (int bptnum, int count, int from_tty)
13314 if (count < 0)
13315 count = 0;
13317 for (breakpoint &b : all_breakpoints ())
13318 if (b.number == bptnum)
13320 if (is_tracepoint (&b))
13322 if (from_tty && count != 0)
13323 gdb_printf (_("Ignore count ignored for tracepoint %d."),
13324 bptnum);
13325 return;
13328 b.ignore_count = count;
13329 if (from_tty)
13331 if (count == 0)
13332 gdb_printf (_("Will stop next time "
13333 "breakpoint %d is reached."),
13334 bptnum);
13335 else if (count == 1)
13336 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
13337 bptnum);
13338 else
13339 gdb_printf (_("Will ignore next %d "
13340 "crossings of breakpoint %d."),
13341 count, bptnum);
13343 notify_breakpoint_modified (&b);
13344 return;
13347 error (_("No breakpoint number %d."), bptnum);
13350 /* Command to set ignore-count of breakpoint N to COUNT. */
13352 static void
13353 ignore_command (const char *args, int from_tty)
13355 const char *p = args;
13356 int num;
13358 if (p == 0)
13359 error_no_arg (_("a breakpoint number"));
13361 num = get_number (&p);
13362 if (num == 0)
13363 error (_("bad breakpoint number: '%s'"), args);
13364 if (*p == 0)
13365 error (_("Second argument (specified ignore-count) is missing."));
13367 set_ignore_count (num,
13368 longest_to_int (value_as_long (parse_and_eval (p))),
13369 from_tty);
13370 if (from_tty)
13371 gdb_printf ("\n");
13375 /* Call FUNCTION on each of the breakpoints with numbers in the range
13376 defined by BP_NUM_RANGE (an inclusive range). */
13378 static void
13379 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13380 gdb::function_view<void (breakpoint *)> function)
13382 if (bp_num_range.first == 0)
13384 warning (_("bad breakpoint number at or near '%d'"),
13385 bp_num_range.first);
13387 else
13389 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13391 bool match = false;
13393 for (breakpoint &b : all_breakpoints_safe ())
13394 if (b.number == i)
13396 match = true;
13397 function (&b);
13398 break;
13400 if (!match)
13401 gdb_printf (_("No breakpoint number %d.\n"), i);
13406 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13407 ARGS. */
13409 static void
13410 map_breakpoint_numbers (const char *args,
13411 gdb::function_view<void (breakpoint *)> function)
13413 if (args == NULL || *args == '\0')
13414 error_no_arg (_("one or more breakpoint numbers"));
13416 number_or_range_parser parser (args);
13418 while (!parser.finished ())
13420 int num = parser.get_number ();
13421 map_breakpoint_number_range (std::make_pair (num, num), function);
13425 /* Return the breakpoint location structure corresponding to the
13426 BP_NUM and LOC_NUM values. */
13428 static struct bp_location *
13429 find_location_by_number (int bp_num, int loc_num)
13431 breakpoint *b = get_breakpoint (bp_num);
13433 if (!b || b->number != bp_num)
13434 error (_("Bad breakpoint number '%d'"), bp_num);
13436 if (loc_num == 0)
13437 error (_("Bad breakpoint location number '%d'"), loc_num);
13439 int n = 0;
13440 for (bp_location &loc : b->locations ())
13441 if (++n == loc_num)
13442 return &loc;
13444 error (_("Bad breakpoint location number '%d'"), loc_num);
13447 /* Modes of operation for extract_bp_num. */
13448 enum class extract_bp_kind
13450 /* Extracting a breakpoint number. */
13453 /* Extracting a location number. */
13454 loc,
13457 /* Extract a breakpoint or location number (as determined by KIND)
13458 from the string starting at START. TRAILER is a character which
13459 can be found after the number. If you don't want a trailer, use
13460 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13461 string. This always returns a positive integer. */
13463 static int
13464 extract_bp_num (extract_bp_kind kind, const char *start,
13465 int trailer, const char **end_out = NULL)
13467 const char *end = start;
13468 int num = get_number_trailer (&end, trailer);
13469 if (num < 0)
13470 error (kind == extract_bp_kind::bp
13471 ? _("Negative breakpoint number '%.*s'")
13472 : _("Negative breakpoint location number '%.*s'"),
13473 int (end - start), start);
13474 if (num == 0)
13475 error (kind == extract_bp_kind::bp
13476 ? _("Bad breakpoint number '%.*s'")
13477 : _("Bad breakpoint location number '%.*s'"),
13478 int (end - start), start);
13480 if (end_out != NULL)
13481 *end_out = end;
13482 return num;
13485 /* Extract a breakpoint or location range (as determined by KIND) in
13486 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13487 representing the (inclusive) range. The returned pair's elements
13488 are always positive integers. */
13490 static std::pair<int, int>
13491 extract_bp_or_bp_range (extract_bp_kind kind,
13492 const std::string &arg,
13493 std::string::size_type arg_offset)
13495 std::pair<int, int> range;
13496 const char *bp_loc = &arg[arg_offset];
13497 std::string::size_type dash = arg.find ('-', arg_offset);
13498 if (dash != std::string::npos)
13500 /* bp_loc is a range (x-z). */
13501 if (arg.length () == dash + 1)
13502 error (kind == extract_bp_kind::bp
13503 ? _("Bad breakpoint number at or near: '%s'")
13504 : _("Bad breakpoint location number at or near: '%s'"),
13505 bp_loc);
13507 const char *end;
13508 const char *start_first = bp_loc;
13509 const char *start_second = &arg[dash + 1];
13510 range.first = extract_bp_num (kind, start_first, '-');
13511 range.second = extract_bp_num (kind, start_second, '\0', &end);
13513 if (range.first > range.second)
13514 error (kind == extract_bp_kind::bp
13515 ? _("Inverted breakpoint range at '%.*s'")
13516 : _("Inverted breakpoint location range at '%.*s'"),
13517 int (end - start_first), start_first);
13519 else
13521 /* bp_loc is a single value. */
13522 range.first = extract_bp_num (kind, bp_loc, '\0');
13523 range.second = range.first;
13525 return range;
13528 /* Extract the breakpoint/location range specified by ARG. Returns
13529 the breakpoint range in BP_NUM_RANGE, and the location range in
13530 BP_LOC_RANGE.
13532 ARG may be in any of the following forms:
13534 x where 'x' is a breakpoint number.
13535 x-y where 'x' and 'y' specify a breakpoint numbers range.
13536 x.y where 'x' is a breakpoint number and 'y' a location number.
13537 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13538 location number range.
13541 static void
13542 extract_bp_number_and_location (const std::string &arg,
13543 std::pair<int, int> &bp_num_range,
13544 std::pair<int, int> &bp_loc_range)
13546 std::string::size_type dot = arg.find ('.');
13548 if (dot != std::string::npos)
13550 /* Handle 'x.y' and 'x.y-z' cases. */
13552 if (arg.length () == dot + 1 || dot == 0)
13553 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
13555 bp_num_range.first
13556 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13557 bp_num_range.second = bp_num_range.first;
13559 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13560 arg, dot + 1);
13562 else
13564 /* Handle x and x-y cases. */
13566 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
13567 bp_loc_range.first = 0;
13568 bp_loc_range.second = 0;
13572 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13573 specifies whether to enable or disable. */
13575 static void
13576 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13578 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13579 if (loc != NULL)
13581 if (loc->disabled_by_cond && enable)
13582 error (_("Breakpoint %d's condition is invalid at location %d, "
13583 "cannot enable."), bp_num, loc_num);
13585 if (loc->enabled != enable)
13587 loc->enabled = enable;
13588 mark_breakpoint_location_modified (loc);
13590 if (target_supports_enable_disable_tracepoint ()
13591 && current_trace_status ()->running && loc->owner
13592 && is_tracepoint (loc->owner))
13593 target_disable_tracepoint (loc);
13595 update_global_location_list (UGLL_DONT_INSERT);
13597 notify_breakpoint_modified (loc->owner);
13600 /* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
13601 owner. 1-based indexing. -1 signals NOT FOUND. */
13603 static int
13604 find_loc_num_by_location (const bp_location *loc)
13606 if (loc != nullptr && loc->owner != nullptr)
13608 /* Locations use 1-based indexing. */
13609 int loc_num = 1;
13610 for (bp_location &it : loc->owner->locations ())
13612 if (&it == loc)
13613 return loc_num;
13614 loc_num++;
13617 return -1;
13620 /* Enable or disable a breakpoint location LOC. ENABLE
13621 specifies whether to enable or disable. */
13623 void
13624 enable_disable_bp_location (bp_location *loc, bool enable)
13626 if (loc == nullptr)
13627 error (_("Breakpoint location is invalid."));
13629 if (loc->owner == nullptr)
13630 error (_("Breakpoint location does not have an owner breakpoint."));
13632 if (loc->disabled_by_cond && enable)
13634 int loc_num = find_loc_num_by_location (loc);
13635 if (loc_num == -1)
13636 error (_("Breakpoint location LOC_NUM could not be found."));
13637 else
13638 error (_("Breakpoint %d's condition is invalid at location %d, "
13639 "cannot enable."), loc->owner->number, loc_num);
13642 if (loc->enabled != enable)
13644 loc->enabled = enable;
13645 mark_breakpoint_location_modified (loc);
13648 if (target_supports_enable_disable_tracepoint ()
13649 && current_trace_status ()->running && loc->owner
13650 && is_tracepoint (loc->owner))
13651 target_disable_tracepoint (loc);
13653 update_global_location_list (UGLL_DONT_INSERT);
13654 notify_breakpoint_modified (loc->owner);
13657 /* Enable or disable a range of breakpoint locations. BP_NUM is the
13658 number of the breakpoint, and BP_LOC_RANGE specifies the
13659 (inclusive) range of location numbers of that breakpoint to
13660 enable/disable. ENABLE specifies whether to enable or disable the
13661 location. */
13663 static void
13664 enable_disable_breakpoint_location_range (int bp_num,
13665 std::pair<int, int> &bp_loc_range,
13666 bool enable)
13668 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13669 enable_disable_bp_num_loc (bp_num, i, enable);
13672 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13673 If from_tty is nonzero, it prints a message to that effect,
13674 which ends with a period (no newline). */
13676 void
13677 disable_breakpoint (struct breakpoint *bpt)
13679 /* Never disable a watchpoint scope breakpoint; we want to
13680 hit them when we leave scope so we can delete both the
13681 watchpoint and its scope breakpoint at that time. */
13682 if (bpt->type == bp_watchpoint_scope)
13683 return;
13685 bpt->enable_state = bp_disabled;
13687 /* Mark breakpoint locations modified. */
13688 mark_breakpoint_modified (bpt);
13690 if (target_supports_enable_disable_tracepoint ()
13691 && current_trace_status ()->running && is_tracepoint (bpt))
13693 for (bp_location &location : bpt->locations ())
13694 target_disable_tracepoint (&location);
13697 update_global_location_list (UGLL_DONT_INSERT);
13699 notify_breakpoint_modified (bpt);
13702 /* Enable or disable the breakpoint(s) or breakpoint location(s)
13703 specified in ARGS. ARGS may be in any of the formats handled by
13704 extract_bp_number_and_location. ENABLE specifies whether to enable
13705 or disable the breakpoints/locations. */
13707 static void
13708 enable_disable_command (const char *args, int from_tty, bool enable)
13710 if (args == 0)
13712 for (breakpoint &bpt : all_breakpoints ())
13713 if (user_breakpoint_p (&bpt))
13715 if (enable)
13716 enable_breakpoint (&bpt);
13717 else
13718 disable_breakpoint (&bpt);
13721 else
13723 std::string num = extract_arg (&args);
13725 while (!num.empty ())
13727 std::pair<int, int> bp_num_range, bp_loc_range;
13729 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13731 if (bp_loc_range.first == bp_loc_range.second
13732 && (bp_loc_range.first == 0
13733 || (bp_loc_range.first == 1
13734 && bp_num_range.first == bp_num_range.second
13735 && !has_multiple_locations (bp_num_range.first))))
13737 /* Handle breakpoint ids with formats 'x' or 'x-z'
13738 or 'y.1' where y has only one code location. */
13739 map_breakpoint_number_range (bp_num_range,
13740 enable
13741 ? enable_breakpoint
13742 : disable_breakpoint);
13744 else
13746 /* Handle breakpoint ids with formats 'x.y' or
13747 'x.y-z'. */
13748 enable_disable_breakpoint_location_range
13749 (bp_num_range.first, bp_loc_range, enable);
13751 num = extract_arg (&args);
13756 /* The disable command disables the specified breakpoints/locations
13757 (or all defined breakpoints) so they're no longer effective in
13758 stopping the inferior. ARGS may be in any of the forms defined in
13759 extract_bp_number_and_location. */
13761 static void
13762 disable_command (const char *args, int from_tty)
13764 enable_disable_command (args, from_tty, false);
13767 static void
13768 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13769 int count)
13771 int target_resources_ok;
13773 if (bpt->type == bp_hardware_breakpoint)
13775 int i;
13776 i = hw_breakpoint_used_count ();
13777 target_resources_ok =
13778 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13779 i + 1, 0);
13780 if (target_resources_ok == 0)
13781 error (_("No hardware breakpoint support in the target."));
13782 else if (target_resources_ok < 0)
13783 error (_("Hardware breakpoints used exceeds limit."));
13786 if (is_watchpoint (bpt))
13788 /* Initialize it just to avoid a GCC false warning. */
13789 enum enable_state orig_enable_state = bp_disabled;
13793 watchpoint *w = gdb::checked_static_cast<watchpoint *> (bpt);
13795 orig_enable_state = bpt->enable_state;
13796 bpt->enable_state = bp_enabled;
13797 update_watchpoint (w, true /* reparse */);
13799 catch (const gdb_exception_error &e)
13801 bpt->enable_state = orig_enable_state;
13802 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13803 bpt->number);
13804 return;
13808 bpt->enable_state = bp_enabled;
13810 /* Mark breakpoint locations modified. */
13811 mark_breakpoint_modified (bpt);
13813 if (target_supports_enable_disable_tracepoint ()
13814 && current_trace_status ()->running && is_tracepoint (bpt))
13816 for (bp_location &location : bpt->locations ())
13817 target_enable_tracepoint (&location);
13820 bpt->disposition = disposition;
13821 bpt->enable_count = count;
13822 update_global_location_list (UGLL_MAY_INSERT);
13824 notify_breakpoint_modified (bpt);
13828 void
13829 enable_breakpoint (struct breakpoint *bpt)
13831 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13834 /* The enable command enables the specified breakpoints/locations (or
13835 all defined breakpoints) so they once again become (or continue to
13836 be) effective in stopping the inferior. ARGS may be in any of the
13837 forms defined in extract_bp_number_and_location. */
13839 static void
13840 enable_command (const char *args, int from_tty)
13842 enable_disable_command (args, from_tty, true);
13845 static void
13846 enable_once_command (const char *args, int from_tty)
13848 map_breakpoint_numbers
13849 (args, [&] (breakpoint *b)
13851 iterate_over_related_breakpoints
13852 (b, [&] (breakpoint *bpt)
13854 enable_breakpoint_disp (bpt, disp_disable, 1);
13859 static void
13860 enable_count_command (const char *args, int from_tty)
13862 int count;
13864 if (args == NULL)
13865 error_no_arg (_("hit count"));
13867 count = get_number (&args);
13869 map_breakpoint_numbers
13870 (args, [&] (breakpoint *b)
13872 iterate_over_related_breakpoints
13873 (b, [&] (breakpoint *bpt)
13875 enable_breakpoint_disp (bpt, disp_disable, count);
13880 static void
13881 enable_delete_command (const char *args, int from_tty)
13883 map_breakpoint_numbers
13884 (args, [&] (breakpoint *b)
13886 iterate_over_related_breakpoints
13887 (b, [&] (breakpoint *bpt)
13889 enable_breakpoint_disp (bpt, disp_del, 1);
13894 /* Invalidate last known value of any hardware watchpoint if
13895 the memory which that value represents has been written to by
13896 GDB itself. */
13898 static void
13899 invalidate_bp_value_on_memory_change (struct inferior *inferior,
13900 CORE_ADDR addr, ssize_t len,
13901 const bfd_byte *data)
13903 for (breakpoint &bp : all_breakpoints ())
13904 if (bp.enable_state == bp_enabled
13905 && bp.type == bp_hardware_watchpoint)
13907 watchpoint &wp = gdb::checked_static_cast<watchpoint &> (bp);
13909 if (wp.val_valid && wp.val != nullptr)
13911 for (bp_location &loc : bp.locations ())
13912 if (loc.loc_type == bp_loc_hardware_watchpoint
13913 && loc.address + loc.length > addr
13914 && addr + len > loc.address)
13916 wp.val = NULL;
13917 wp.val_valid = false;
13923 /* Create and insert a breakpoint for software single step. */
13925 void
13926 insert_single_step_breakpoint (struct gdbarch *gdbarch,
13927 const address_space *aspace,
13928 CORE_ADDR next_pc)
13930 struct thread_info *tp = inferior_thread ();
13931 struct symtab_and_line sal;
13932 CORE_ADDR pc = next_pc;
13934 if (tp->control.single_step_breakpoints == NULL)
13936 std::unique_ptr<breakpoint> b
13937 (new momentary_breakpoint (gdbarch, bp_single_step,
13938 current_program_space,
13939 null_frame_id,
13940 tp->global_num));
13942 tp->control.single_step_breakpoints
13943 = add_to_breakpoint_chain (std::move (b));
13946 sal = find_pc_line (pc, 0);
13947 sal.pc = pc;
13948 sal.section = find_pc_overlay (pc);
13949 sal.explicit_pc = 1;
13951 auto *ss_bp
13952 = (gdb::checked_static_cast<momentary_breakpoint *>
13953 (tp->control.single_step_breakpoints));
13954 ss_bp->add_location (sal);
13956 update_global_location_list (UGLL_INSERT);
13959 /* Insert single step breakpoints according to the current state. */
13962 insert_single_step_breakpoints (struct gdbarch *gdbarch)
13964 regcache *regcache = get_thread_regcache (inferior_thread ());
13965 std::vector<CORE_ADDR> next_pcs;
13967 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
13969 if (!next_pcs.empty ())
13971 frame_info_ptr frame = get_current_frame ();
13972 const address_space *aspace = get_frame_address_space (frame);
13974 for (CORE_ADDR pc : next_pcs)
13975 insert_single_step_breakpoint (gdbarch, aspace, pc);
13977 return 1;
13979 else
13980 return 0;
13983 /* See breakpoint.h. */
13986 breakpoint_has_location_inserted_here (struct breakpoint *bp,
13987 const address_space *aspace,
13988 CORE_ADDR pc)
13990 for (bp_location &loc : bp->locations ())
13991 if (loc.inserted
13992 && breakpoint_location_address_match (&loc, aspace, pc))
13993 return 1;
13995 return 0;
13998 /* Check whether a software single-step breakpoint is inserted at
13999 PC. */
14002 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14003 CORE_ADDR pc)
14005 for (breakpoint &bpt : all_breakpoints ())
14007 if (bpt.type == bp_single_step
14008 && breakpoint_has_location_inserted_here (&bpt, aspace, pc))
14009 return 1;
14011 return 0;
14014 /* Tracepoint-specific operations. */
14016 /* Set tracepoint count to NUM. */
14017 static void
14018 set_tracepoint_count (int num)
14020 tracepoint_count = num;
14021 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14024 static void
14025 trace_command (const char *arg, int from_tty)
14027 location_spec_up locspec = string_to_location_spec (&arg,
14028 current_language);
14029 const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
14030 (locspec.get (), true /* is_tracepoint */);
14032 create_breakpoint (get_current_arch (),
14033 locspec.get (),
14034 NULL, -1, -1, arg, false, 1 /* parse arg */,
14035 0 /* tempflag */,
14036 bp_tracepoint /* type_wanted */,
14037 0 /* Ignore count */,
14038 pending_break_support,
14039 ops,
14040 from_tty,
14041 1 /* enabled */,
14042 0 /* internal */, 0);
14045 static void
14046 ftrace_command (const char *arg, int from_tty)
14048 location_spec_up locspec = string_to_location_spec (&arg,
14049 current_language);
14050 create_breakpoint (get_current_arch (),
14051 locspec.get (),
14052 NULL, -1, -1, arg, false, 1 /* parse arg */,
14053 0 /* tempflag */,
14054 bp_fast_tracepoint /* type_wanted */,
14055 0 /* Ignore count */,
14056 pending_break_support,
14057 &code_breakpoint_ops,
14058 from_tty,
14059 1 /* enabled */,
14060 0 /* internal */, 0);
14063 /* strace command implementation. Creates a static tracepoint. */
14065 static void
14066 strace_command (const char *arg, int from_tty)
14068 const struct breakpoint_ops *ops;
14069 location_spec_up locspec;
14070 enum bptype type;
14072 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14073 or with a normal static tracepoint. */
14074 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14076 ops = &strace_marker_breakpoint_ops;
14077 locspec = new_linespec_location_spec (&arg,
14078 symbol_name_match_type::FULL);
14079 type = bp_static_marker_tracepoint;
14081 else
14083 ops = &code_breakpoint_ops;
14084 locspec = string_to_location_spec (&arg, current_language);
14085 type = bp_static_tracepoint;
14088 create_breakpoint (get_current_arch (),
14089 locspec.get (),
14090 NULL, -1, -1, arg, false, 1 /* parse arg */,
14091 0 /* tempflag */,
14092 type /* type_wanted */,
14093 0 /* Ignore count */,
14094 pending_break_support,
14095 ops,
14096 from_tty,
14097 1 /* enabled */,
14098 0 /* internal */, 0);
14101 /* Set up a fake reader function that gets command lines from a linked
14102 list that was acquired during tracepoint uploading. */
14104 static struct uploaded_tp *this_utp;
14105 static int next_cmd;
14107 static const char *
14108 read_uploaded_action (std::string &buffer)
14110 char *rslt = nullptr;
14112 if (next_cmd < this_utp->cmd_strings.size ())
14114 rslt = this_utp->cmd_strings[next_cmd].get ();
14115 next_cmd++;
14118 return rslt;
14121 /* Given information about a tracepoint as recorded on a target (which
14122 can be either a live system or a trace file), attempt to create an
14123 equivalent GDB tracepoint. This is not a reliable process, since
14124 the target does not necessarily have all the information used when
14125 the tracepoint was originally defined. */
14127 struct tracepoint *
14128 create_tracepoint_from_upload (struct uploaded_tp *utp)
14130 const char *addr_str;
14131 char small_buf[100];
14132 struct tracepoint *tp;
14134 if (utp->at_string)
14135 addr_str = utp->at_string.get ();
14136 else
14138 /* In the absence of a source location, fall back to raw
14139 address. Since there is no way to confirm that the address
14140 means the same thing as when the trace was started, warn the
14141 user. */
14142 warning (_("Uploaded tracepoint %d has no "
14143 "source location, using raw address"),
14144 utp->number);
14145 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14146 addr_str = small_buf;
14149 /* There's not much we can do with a sequence of bytecodes. */
14150 if (utp->cond && !utp->cond_string)
14151 warning (_("Uploaded tracepoint %d condition "
14152 "has no source form, ignoring it"),
14153 utp->number);
14155 location_spec_up locspec = string_to_location_spec (&addr_str,
14156 current_language);
14159 gdb_assert (addr_str != nullptr);
14160 if (*addr_str != '\0')
14161 error (_("Garbage '%s' at end of location"), addr_str);
14163 if (!create_breakpoint (get_current_arch (),
14164 locspec.get (),
14165 utp->cond_string.get (), -1, -1, addr_str,
14166 false /* force_condition */,
14167 0 /* parse cond/thread */,
14168 0 /* tempflag */,
14169 utp->type /* type_wanted */,
14170 0 /* Ignore count */,
14171 pending_break_support,
14172 &code_breakpoint_ops,
14173 0 /* from_tty */,
14174 utp->enabled /* enabled */,
14175 0 /* internal */,
14176 CREATE_BREAKPOINT_FLAGS_INSERTED))
14177 return NULL;
14179 /* Get the tracepoint we just created. */
14180 tp = get_tracepoint (tracepoint_count);
14181 gdb_assert (tp != NULL);
14183 if (utp->pass > 0)
14185 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14186 tp->number);
14188 trace_pass_command (small_buf, 0);
14191 /* If we have uploaded versions of the original commands, set up a
14192 special-purpose "reader" function and call the usual command line
14193 reader, then pass the result to the breakpoint command-setting
14194 function. */
14195 if (!utp->cmd_strings.empty ())
14197 counted_command_line cmd_list;
14199 this_utp = utp;
14200 next_cmd = 0;
14202 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14204 breakpoint_set_commands (tp, std::move (cmd_list));
14206 else if (!utp->actions.empty ()
14207 || !utp->step_actions.empty ())
14208 warning (_("Uploaded tracepoint %d actions "
14209 "have no source form, ignoring them"),
14210 utp->number);
14212 /* Copy any status information that might be available. */
14213 tp->hit_count = utp->hit_count;
14214 tp->traceframe_usage = utp->traceframe_usage;
14216 return tp;
14219 /* Print information on tracepoint number TPNUM_EXP, or all if
14220 omitted. */
14222 static void
14223 info_tracepoints_command (const char *args, int from_tty)
14225 struct ui_out *uiout = current_uiout;
14226 int num_printed;
14228 num_printed = breakpoint_1 (args, false, is_tracepoint);
14230 if (num_printed == 0)
14232 if (args == NULL || *args == '\0')
14233 uiout->message ("No tracepoints.\n");
14234 else
14235 uiout->message ("No tracepoint matching '%s'.\n", args);
14238 default_collect_info ();
14241 /* The 'enable trace' command enables tracepoints.
14242 Not supported by all targets. */
14243 static void
14244 enable_trace_command (const char *args, int from_tty)
14246 enable_command (args, from_tty);
14249 /* The 'disable trace' command disables tracepoints.
14250 Not supported by all targets. */
14251 static void
14252 disable_trace_command (const char *args, int from_tty)
14254 disable_command (args, from_tty);
14257 /* Remove a tracepoint (or all if no argument). */
14258 static void
14259 delete_trace_command (const char *arg, int from_tty)
14261 dont_repeat ();
14263 if (arg == 0)
14265 int breaks_to_delete = 0;
14267 /* Delete all tracepoints if no argument.
14268 Do not delete internal or call-dummy breakpoints, these
14269 have to be deleted with an explicit breakpoint number
14270 argument. */
14271 for (breakpoint &tp : all_tracepoints ())
14272 if (is_tracepoint (&tp) && user_breakpoint_p (&tp))
14274 breaks_to_delete = 1;
14275 break;
14278 /* Ask user only if there are some breakpoints to delete. */
14279 if (!from_tty
14280 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14282 for (breakpoint &b : all_breakpoints_safe ())
14283 if (is_tracepoint (&b) && user_breakpoint_p (&b))
14284 delete_breakpoint (&b);
14287 else
14288 map_breakpoint_numbers
14289 (arg, [&] (breakpoint *br)
14291 iterate_over_related_breakpoints (br, delete_breakpoint);
14295 /* Helper function for trace_pass_command. */
14297 static void
14298 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14300 tp->pass_count = count;
14301 notify_breakpoint_modified (tp);
14302 if (from_tty)
14303 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
14304 tp->number, count);
14307 /* Set passcount for tracepoint.
14309 First command argument is passcount, second is tracepoint number.
14310 If tracepoint number omitted, apply to most recently defined.
14311 Also accepts special argument "all". */
14313 static void
14314 trace_pass_command (const char *args, int from_tty)
14316 ULONGEST count;
14318 if (args == 0 || *args == 0)
14319 error (_("passcount command requires an "
14320 "argument (count + optional TP num)"));
14322 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14324 args = skip_spaces (args);
14325 if (*args && strncasecmp (args, "all", 3) == 0)
14327 args += 3; /* Skip special argument "all". */
14328 if (*args)
14329 error (_("Junk at end of arguments."));
14331 for (breakpoint &b : all_tracepoints ())
14333 tracepoint &t1 = gdb::checked_static_cast<tracepoint &> (b);
14334 trace_pass_set_count (&t1, count, from_tty);
14337 else if (*args == '\0')
14339 tracepoint *t1 = get_tracepoint_by_number (&args, NULL);
14340 if (t1)
14341 trace_pass_set_count (t1, count, from_tty);
14343 else
14345 number_or_range_parser parser (args);
14346 while (!parser.finished ())
14348 tracepoint *t1 = get_tracepoint_by_number (&args, &parser);
14349 if (t1)
14350 trace_pass_set_count (t1, count, from_tty);
14355 struct tracepoint *
14356 get_tracepoint (int num)
14358 for (breakpoint &t : all_tracepoints ())
14359 if (t.number == num)
14360 return gdb::checked_static_cast<tracepoint *> (&t);
14362 return NULL;
14365 /* Find the tracepoint with the given target-side number (which may be
14366 different from the tracepoint number after disconnecting and
14367 reconnecting). */
14369 struct tracepoint *
14370 get_tracepoint_by_number_on_target (int num)
14372 for (breakpoint &b : all_tracepoints ())
14374 tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
14376 if (t.number_on_target == num)
14377 return &t;
14380 return NULL;
14383 /* Utility: parse a tracepoint number and look it up in the list.
14384 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14385 If the argument is missing, the most recent tracepoint
14386 (tracepoint_count) is returned. */
14388 struct tracepoint *
14389 get_tracepoint_by_number (const char **arg,
14390 number_or_range_parser *parser)
14392 int tpnum;
14393 const char *instring = arg == NULL ? NULL : *arg;
14395 if (parser != NULL)
14397 gdb_assert (!parser->finished ());
14398 tpnum = parser->get_number ();
14400 else if (arg == NULL || *arg == NULL || ! **arg)
14401 tpnum = tracepoint_count;
14402 else
14403 tpnum = get_number (arg);
14405 if (tpnum <= 0)
14407 if (instring && *instring)
14408 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14409 instring);
14410 else
14411 gdb_printf (_("No previous tracepoint\n"));
14412 return NULL;
14415 for (breakpoint &t : all_tracepoints ())
14416 if (t.number == tpnum)
14417 return gdb::checked_static_cast<tracepoint *> (&t);
14419 gdb_printf ("No tracepoint number %d.\n", tpnum);
14420 return NULL;
14423 void
14424 breakpoint::print_recreate_thread (struct ui_file *fp) const
14426 if (thread != -1)
14428 struct thread_info *thr = find_thread_global_id (thread);
14429 gdb_printf (fp, " thread %s", print_full_thread_id (thr));
14432 if (task != -1)
14433 gdb_printf (fp, " task %d", task);
14435 gdb_printf (fp, "\n");
14438 /* Save information on user settable breakpoints (watchpoints, etc) to
14439 a new script file named FILENAME. If FILTER is non-NULL, call it
14440 on each breakpoint and only include the ones for which it returns
14441 true. */
14443 static void
14444 save_breakpoints (const char *filename, int from_tty,
14445 bool (*filter) (const struct breakpoint *))
14447 bool any = false;
14448 int extra_trace_bits = 0;
14450 if (filename == 0 || *filename == 0)
14451 error (_("Argument required (file name in which to save)"));
14453 /* See if we have anything to save. */
14454 for (breakpoint &tp : all_breakpoints ())
14456 /* Skip internal and momentary breakpoints. */
14457 if (!user_breakpoint_p (&tp))
14458 continue;
14460 /* If we have a filter, only save the breakpoints it accepts. */
14461 if (filter && !filter (&tp))
14462 continue;
14464 any = true;
14466 if (is_tracepoint (&tp))
14468 extra_trace_bits = 1;
14470 /* We can stop searching. */
14471 break;
14475 if (!any)
14477 warning (_("Nothing to save."));
14478 return;
14481 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14483 stdio_file fp;
14485 if (!fp.open (expanded_filename.get (), "w"))
14486 error (_("Unable to open file '%s' for saving (%s)"),
14487 expanded_filename.get (), safe_strerror (errno));
14489 if (extra_trace_bits)
14490 save_trace_state_variables (&fp);
14492 for (breakpoint &tp : all_breakpoints ())
14494 /* Skip internal and momentary breakpoints. */
14495 if (!user_breakpoint_p (&tp))
14496 continue;
14498 /* If we have a filter, only save the breakpoints it accepts. */
14499 if (filter && !filter (&tp))
14500 continue;
14502 tp.print_recreate (&fp);
14504 /* Note, we can't rely on tp->number for anything, as we can't
14505 assume the recreated breakpoint numbers will match. Use $bpnum
14506 instead. */
14508 if (tp.cond_string)
14509 fp.printf (" condition $bpnum %s\n", tp.cond_string.get ());
14511 if (tp.ignore_count)
14512 fp.printf (" ignore $bpnum %d\n", tp.ignore_count);
14514 if (tp.type != bp_dprintf && tp.commands)
14516 fp.puts (" commands\n");
14518 ui_out_redirect_pop redir (current_uiout, &fp);
14519 print_command_lines (current_uiout, tp.commands.get (), 2);
14521 fp.puts (" end\n");
14524 if (tp.enable_state == bp_disabled)
14525 fp.puts ("disable $bpnum\n");
14527 /* If this is a multi-location breakpoint, check if the locations
14528 should be individually disabled. Watchpoint locations are
14529 special, and not user visible. */
14530 if (!is_watchpoint (&tp) && tp.has_multiple_locations ())
14532 int n = 1;
14534 for (bp_location &loc : tp.locations ())
14536 if (!loc.enabled)
14537 fp.printf ("disable $bpnum.%d\n", n);
14539 n++;
14544 if (extra_trace_bits && !default_collect.empty ())
14545 fp.printf ("set default-collect %s\n", default_collect.c_str ());
14547 if (from_tty)
14548 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
14551 /* The `save breakpoints' command. */
14553 static void
14554 save_breakpoints_command (const char *args, int from_tty)
14556 save_breakpoints (args, from_tty, NULL);
14559 /* The `save tracepoints' command. */
14561 static void
14562 save_tracepoints_command (const char *args, int from_tty)
14564 save_breakpoints (args, from_tty, is_tracepoint);
14568 /* This help string is used to consolidate all the help string for specifying
14569 locations used by several commands. */
14571 #define LOCATION_SPEC_HELP_STRING \
14572 "Linespecs are colon-separated lists of location parameters, such as\n\
14573 source filename, function name, label name, and line number.\n\
14574 Example: To specify the start of a label named \"the_top\" in the\n\
14575 function \"fact\" in the file \"factorial.c\", use\n\
14576 \"factorial.c:fact:the_top\".\n\
14578 Address locations begin with \"*\" and specify an exact address in the\n\
14579 program. Example: To specify the fourth byte past the start function\n\
14580 \"main\", use \"*main + 4\".\n\
14582 Explicit locations are similar to linespecs but use an option/argument\n\
14583 syntax to specify location parameters.\n\
14584 Example: To specify the start of the label named \"the_top\" in the\n\
14585 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
14586 -function fact -label the_top\".\n\
14588 By default, a specified function is matched against the program's\n\
14589 functions in all scopes. For C++, this means in all namespaces and\n\
14590 classes. For Ada, this means in all packages. E.g., in C++,\n\
14591 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14592 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
14593 specified name as a complete fully-qualified name instead."
14595 /* This help string is used for the break, hbreak, tbreak and thbreak
14596 commands. It is defined as a macro to prevent duplication.
14597 COMMAND should be a string constant containing the name of the
14598 command. */
14600 #define BREAK_ARGS_HELP(command) \
14601 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14602 \t[-force-condition] [if CONDITION]\n\
14603 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14604 probe point. Accepted values are `-probe' (for a generic, automatically\n\
14605 guessed probe type), `-probe-stap' (for a SystemTap probe) or\n\
14606 `-probe-dtrace' (for a DTrace probe).\n\
14607 LOCATION may be a linespec, address, or explicit location as described\n\
14608 below.\n\
14610 With no LOCATION, uses current execution address of the selected\n\
14611 stack frame. This is useful for breaking on return to a stack frame.\n\
14613 THREADNUM is the number from \"info threads\".\n\
14614 CONDITION is a boolean expression.\n\
14616 With the \"-force-condition\" flag, the condition is defined even when\n\
14617 it is invalid for all current locations.\n\
14618 \n" LOCATION_SPEC_HELP_STRING "\n\n\
14619 Multiple breakpoints at one place are permitted, and useful if their\n\
14620 conditions are different.\n\
14622 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14624 /* List of subcommands for "catch". */
14625 static struct cmd_list_element *catch_cmdlist;
14627 /* List of subcommands for "tcatch". */
14628 static struct cmd_list_element *tcatch_cmdlist;
14630 void
14631 add_catch_command (const char *name, const char *docstring,
14632 cmd_func_ftype *func,
14633 completer_ftype *completer,
14634 void *user_data_catch,
14635 void *user_data_tcatch)
14637 struct cmd_list_element *command;
14639 command = add_cmd (name, class_breakpoint, docstring,
14640 &catch_cmdlist);
14641 command->func = func;
14642 command->set_context (user_data_catch);
14643 set_cmd_completer (command, completer);
14645 command = add_cmd (name, class_breakpoint, docstring,
14646 &tcatch_cmdlist);
14647 command->func = func;
14648 command->set_context (user_data_tcatch);
14649 set_cmd_completer (command, completer);
14652 /* False if any of the breakpoint's locations could be a location where
14653 functions have been inlined, true otherwise. */
14655 static bool
14656 is_non_inline_function (struct breakpoint *b)
14658 /* The shared library event breakpoint is set on the address of a
14659 non-inline function. */
14660 return (b->type == bp_shlib_event);
14663 /* Nonzero if the specified PC cannot be a location where functions
14664 have been inlined. */
14667 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
14668 const target_waitstatus &ws)
14670 for (breakpoint &b : all_breakpoints ())
14672 if (!is_non_inline_function (&b))
14673 continue;
14675 for (bp_location &bl : b.locations ())
14677 if (!bl.shlib_disabled
14678 && bpstat_check_location (&bl, aspace, pc, ws))
14679 return 1;
14683 return 0;
14686 /* Remove any references to OBJFILE which is going to be freed. */
14688 void
14689 breakpoint_free_objfile (struct objfile *objfile)
14691 for (bp_location *loc : all_bp_locations ())
14692 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
14693 loc->symtab = NULL;
14696 /* Chain containing all defined "enable breakpoint" subcommands. */
14698 static struct cmd_list_element *enablebreaklist = NULL;
14700 /* See breakpoint.h. */
14702 cmd_list_element *commands_cmd_element = nullptr;
14704 void _initialize_breakpoint ();
14705 void
14706 _initialize_breakpoint ()
14708 struct cmd_list_element *c;
14710 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14711 "breakpoint");
14712 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14713 "breakpoint");
14714 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14715 "breakpoint");
14717 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14718 before a breakpoint is set. */
14719 breakpoint_count = 0;
14721 tracepoint_count = 0;
14723 add_com ("ignore", class_breakpoint, ignore_command, _("\
14724 Set ignore-count of breakpoint number N to COUNT.\n\
14725 Usage is `ignore N COUNT'."));
14727 commands_cmd_element = add_com ("commands", class_breakpoint,
14728 commands_command, _("\
14729 Set commands to be executed when the given breakpoints are hit.\n\
14730 Give a space-separated breakpoint list as argument after \"commands\".\n\
14731 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14732 (e.g. `5-7').\n\
14733 With no argument, the targeted breakpoint is the last one set.\n\
14734 The commands themselves follow starting on the next line.\n\
14735 Type a line containing \"end\" to indicate the end of them.\n\
14736 Give \"silent\" as the first line to make the breakpoint silent;\n\
14737 then no output is printed when it is hit, except what the commands print."));
14739 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14740 static std::string condition_command_help
14741 = gdb::option::build_help (_("\
14742 Specify breakpoint number N to break only if COND is true.\n\
14743 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
14744 is an expression to be evaluated whenever breakpoint N is reached.\n\
14746 Options:\n\
14747 %OPTIONS%"), cc_opts);
14749 c = add_com ("condition", class_breakpoint, condition_command,
14750 condition_command_help.c_str ());
14751 set_cmd_completer_handle_brkchars (c, condition_completer);
14753 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
14754 Set a temporary breakpoint.\n\
14755 Like \"break\" except the breakpoint is only temporary,\n\
14756 so it will be deleted when hit. Equivalent to \"break\" followed\n\
14757 by using \"enable delete\" on the breakpoint number.\n\
14759 BREAK_ARGS_HELP ("tbreak")));
14760 set_cmd_completer (c, location_completer);
14762 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
14763 Set a hardware assisted breakpoint.\n\
14764 Like \"break\" except the breakpoint requires hardware support,\n\
14765 some target hardware may not have this support.\n\
14767 BREAK_ARGS_HELP ("hbreak")));
14768 set_cmd_completer (c, location_completer);
14770 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
14771 Set a temporary hardware assisted breakpoint.\n\
14772 Like \"hbreak\" except the breakpoint is only temporary,\n\
14773 so it will be deleted when hit.\n\
14775 BREAK_ARGS_HELP ("thbreak")));
14776 set_cmd_completer (c, location_completer);
14778 cmd_list_element *enable_cmd
14779 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14780 Enable all or some breakpoints.\n\
14781 Usage: enable [BREAKPOINTNUM]...\n\
14782 Give breakpoint numbers (separated by spaces) as arguments.\n\
14783 With no subcommand, breakpoints are enabled until you command otherwise.\n\
14784 This is used to cancel the effect of the \"disable\" command.\n\
14785 With a subcommand you can enable temporarily."),
14786 &enablelist, 1, &cmdlist);
14788 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
14790 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
14791 Enable all or some breakpoints.\n\
14792 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
14793 Give breakpoint numbers (separated by spaces) as arguments.\n\
14794 This is used to cancel the effect of the \"disable\" command.\n\
14795 May be abbreviated to simply \"enable\"."),
14796 &enablebreaklist, 1, &enablelist);
14798 add_cmd ("once", no_class, enable_once_command, _("\
14799 Enable some breakpoints for one hit.\n\
14800 Usage: enable breakpoints once BREAKPOINTNUM...\n\
14801 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14802 &enablebreaklist);
14804 add_cmd ("delete", no_class, enable_delete_command, _("\
14805 Enable some breakpoints and delete when hit.\n\
14806 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
14807 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14808 &enablebreaklist);
14810 add_cmd ("count", no_class, enable_count_command, _("\
14811 Enable some breakpoints for COUNT hits.\n\
14812 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
14813 If a breakpoint is hit while enabled in this fashion,\n\
14814 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14815 &enablebreaklist);
14817 add_cmd ("delete", no_class, enable_delete_command, _("\
14818 Enable some breakpoints and delete when hit.\n\
14819 Usage: enable delete BREAKPOINTNUM...\n\
14820 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14821 &enablelist);
14823 add_cmd ("once", no_class, enable_once_command, _("\
14824 Enable some breakpoints for one hit.\n\
14825 Usage: enable once BREAKPOINTNUM...\n\
14826 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14827 &enablelist);
14829 add_cmd ("count", no_class, enable_count_command, _("\
14830 Enable some breakpoints for COUNT hits.\n\
14831 Usage: enable count COUNT BREAKPOINTNUM...\n\
14832 If a breakpoint is hit while enabled in this fashion,\n\
14833 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14834 &enablelist);
14836 cmd_list_element *disable_cmd
14837 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14838 Disable all or some breakpoints.\n\
14839 Usage: disable [BREAKPOINTNUM]...\n\
14840 Arguments are breakpoint numbers with spaces in between.\n\
14841 To disable all breakpoints, give no argument.\n\
14842 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
14843 &disablelist, 1, &cmdlist);
14844 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14845 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
14847 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
14848 Disable all or some breakpoints.\n\
14849 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
14850 Arguments are breakpoint numbers with spaces in between.\n\
14851 To disable all breakpoints, give no argument.\n\
14852 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
14853 This command may be abbreviated \"disable\"."),
14854 &disablelist);
14856 cmd_list_element *delete_cmd
14857 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14858 Delete all or some breakpoints.\n\
14859 Usage: delete [BREAKPOINTNUM]...\n\
14860 Arguments are breakpoint numbers with spaces in between.\n\
14861 To delete all breakpoints, watchpoints, tracepoints, and catchpoints,\n\
14862 give no argument.\n\
14864 Also a prefix command for deletion of other GDB objects."),
14865 &deletelist, 1, &cmdlist);
14866 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14867 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
14869 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
14870 Delete all or some breakpoints or auto-display expressions.\n\
14871 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
14872 Arguments are breakpoint numbers with spaces in between.\n\
14873 To delete all breakpoints, watchpoints, tracepoints, and catchpoints,\n\
14874 give no argument.\n\
14875 This command may be abbreviated \"delete\"."),
14876 &deletelist);
14878 cmd_list_element *clear_cmd
14879 = add_com ("clear", class_breakpoint, clear_command, _("\
14880 Clear breakpoint at specified location.\n\
14881 Argument may be a linespec, explicit, or address location as described below.\n\
14883 With no argument, clears all breakpoints in the line that the selected frame\n\
14884 is executing in.\n"
14885 "\n" LOCATION_SPEC_HELP_STRING "\n\n\
14886 See also the \"delete\" command which clears breakpoints by number."));
14887 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
14889 cmd_list_element *break_cmd
14890 = add_com ("break", class_breakpoint, break_command, _("\
14891 Set breakpoint at specified location.\n"
14892 BREAK_ARGS_HELP ("break")));
14893 set_cmd_completer (break_cmd, location_completer);
14895 add_com_alias ("b", break_cmd, class_run, 1);
14896 add_com_alias ("br", break_cmd, class_run, 1);
14897 add_com_alias ("bre", break_cmd, class_run, 1);
14898 add_com_alias ("brea", break_cmd, class_run, 1);
14900 cmd_list_element *info_breakpoints_cmd
14901 = add_info ("breakpoints", info_breakpoints_command, _("\
14902 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
14903 The \"Type\" column indicates one of:\n\
14904 \tbreakpoint - normal breakpoint\n\
14905 \twatchpoint - watchpoint\n\
14906 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14907 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14908 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14909 address and file/line number respectively.\n\
14911 Convenience variable \"$_\" and default examine address for \"x\"\n\
14912 are set to the address of the last breakpoint listed unless the command\n\
14913 is prefixed with \"server \".\n\n\
14914 Convenience variable \"$bpnum\" contains the number of the last\n\
14915 breakpoint set."));
14917 add_info_alias ("b", info_breakpoints_cmd, 1);
14919 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14920 Status of all breakpoints, or breakpoint number NUMBER.\n\
14921 The \"Type\" column indicates one of:\n\
14922 \tbreakpoint - normal breakpoint\n\
14923 \twatchpoint - watchpoint\n\
14924 \tlongjmp - internal breakpoint used to step through longjmp()\n\
14925 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14926 \tuntil - internal breakpoint used by the \"until\" command\n\
14927 \tfinish - internal breakpoint used by the \"finish\" command\n\
14928 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14929 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14930 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14931 address and file/line number respectively.\n\
14933 Convenience variable \"$_\" and default examine address for \"x\"\n\
14934 are set to the address of the last breakpoint listed unless the command\n\
14935 is prefixed with \"server \".\n\n\
14936 Convenience variable \"$bpnum\" contains the number of the last\n\
14937 breakpoint set."),
14938 &maintenanceinfolist);
14940 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
14941 Set catchpoints to catch events."),
14942 &catch_cmdlist,
14943 0/*allow-unknown*/, &cmdlist);
14945 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
14946 Set temporary catchpoints to catch events."),
14947 &tcatch_cmdlist,
14948 0/*allow-unknown*/, &cmdlist);
14950 const auto opts = make_watch_options_def_group (nullptr);
14952 static const std::string watch_help = gdb::option::build_help (_("\
14953 Set a watchpoint for EXPRESSION.\n\
14954 Usage: watch [-location] EXPRESSION\n\
14956 Options:\n\
14957 %OPTIONS%\n\
14959 A watchpoint stops execution of your program whenever the value of\n\
14960 an expression changes."), opts);
14961 c = add_com ("watch", class_breakpoint, watch_command,
14962 watch_help.c_str ());
14963 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14965 static const std::string rwatch_help = gdb::option::build_help (_("\
14966 Set a read watchpoint for EXPRESSION.\n\
14967 Usage: rwatch [-location] EXPRESSION\n\
14969 Options:\n\
14970 %OPTIONS%\n\
14972 A read watchpoint stops execution of your program whenever the value of\n\
14973 an expression is read."), opts);
14974 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14975 rwatch_help.c_str ());
14976 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14978 static const std::string awatch_help = gdb::option::build_help (_("\
14979 Set an access watchpoint for EXPRESSION.\n\
14980 Usage: awatch [-location] EXPRESSION\n\
14982 Options:\n\
14983 %OPTIONS%\n\
14985 An access watchpoint stops execution of your program whenever the value\n\
14986 of an expression is either read or written."), opts);
14987 c = add_com ("awatch", class_breakpoint, awatch_command,
14988 awatch_help.c_str ());
14989 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14991 add_info ("watchpoints", info_watchpoints_command, _("\
14992 Status of specified watchpoints (all watchpoints if no argument)."));
14994 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14995 respond to changes - contrary to the description. */
14996 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14997 &can_use_hw_watchpoints, _("\
14998 Set debugger's willingness to use watchpoint hardware."), _("\
14999 Show debugger's willingness to use watchpoint hardware."), _("\
15000 If zero, gdb will not use hardware for new watchpoints, even if\n\
15001 such is available. (However, any hardware watchpoints that were\n\
15002 created before setting this to nonzero, will continue to use watchpoint\n\
15003 hardware.)"),
15004 NULL,
15005 show_can_use_hw_watchpoints,
15006 &setlist, &showlist);
15008 can_use_hw_watchpoints = 1;
15010 /* Tracepoint manipulation commands. */
15012 cmd_list_element *trace_cmd
15013 = add_com ("trace", class_breakpoint, trace_command, _("\
15014 Set a tracepoint at specified location.\n\
15016 BREAK_ARGS_HELP ("trace") "\n\
15017 Do \"help tracepoints\" for info on other tracepoint commands."));
15018 set_cmd_completer (trace_cmd, location_completer);
15020 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15021 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15022 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15023 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
15025 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15026 Set a fast tracepoint at specified location.\n\
15028 BREAK_ARGS_HELP ("ftrace") "\n\
15029 Do \"help tracepoints\" for info on other tracepoint commands."));
15030 set_cmd_completer (c, location_completer);
15032 c = add_com ("strace", class_breakpoint, strace_command, _("\
15033 Set a static tracepoint at location or marker.\n\
15035 strace [LOCATION] [if CONDITION]\n\
15036 LOCATION may be a linespec, explicit, or address location (described below)\n\
15037 or -m MARKER_ID.\n\n\
15038 If a marker id is specified, probe the marker with that name. With\n\
15039 no LOCATION, uses current execution address of the selected stack frame.\n\
15040 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15041 This collects arbitrary user data passed in the probe point call to the\n\
15042 tracing library. You can inspect it when analyzing the trace buffer,\n\
15043 by printing the $_sdata variable like any other convenience variable.\n\
15045 CONDITION is a boolean expression.\n\
15046 \n" LOCATION_SPEC_HELP_STRING "\n\n\
15047 Multiple tracepoints at one place are permitted, and useful if their\n\
15048 conditions are different.\n\
15050 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15051 Do \"help tracepoints\" for info on other tracepoint commands."));
15052 set_cmd_completer (c, location_completer);
15054 cmd_list_element *info_tracepoints_cmd
15055 = add_info ("tracepoints", info_tracepoints_command, _("\
15056 Status of specified tracepoints (all tracepoints if no argument).\n\
15057 Convenience variable \"$tpnum\" contains the number of the\n\
15058 last tracepoint set."));
15060 add_info_alias ("tp", info_tracepoints_cmd, 1);
15062 cmd_list_element *delete_tracepoints_cmd
15063 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15064 Delete specified tracepoints.\n\
15065 Arguments are tracepoint numbers, separated by spaces.\n\
15066 No argument means delete all tracepoints."),
15067 &deletelist);
15068 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
15070 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15071 Disable specified tracepoints.\n\
15072 Arguments are tracepoint numbers, separated by spaces.\n\
15073 No argument means disable all tracepoints."),
15074 &disablelist);
15075 deprecate_cmd (c, "disable");
15077 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15078 Enable specified tracepoints.\n\
15079 Arguments are tracepoint numbers, separated by spaces.\n\
15080 No argument means enable all tracepoints."),
15081 &enablelist);
15082 deprecate_cmd (c, "enable");
15084 add_com ("passcount", class_trace, trace_pass_command, _("\
15085 Set the passcount for a tracepoint.\n\
15086 The trace will end when the tracepoint has been passed 'count' times.\n\
15087 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15088 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15090 add_basic_prefix_cmd ("save", class_breakpoint,
15091 _("Save breakpoint definitions as a script."),
15092 &save_cmdlist,
15093 0/*allow-unknown*/, &cmdlist);
15095 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15096 Save current breakpoint definitions as a script.\n\
15097 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15098 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15099 session to restore them."),
15100 &save_cmdlist);
15101 set_cmd_completer (c, deprecated_filename_completer);
15103 cmd_list_element *save_tracepoints_cmd
15104 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15105 Save current tracepoint definitions as a script.\n\
15106 Use the 'source' command in another debug session to restore them."),
15107 &save_cmdlist);
15108 set_cmd_completer (save_tracepoints_cmd, deprecated_filename_completer);
15110 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
15111 deprecate_cmd (c, "save tracepoints");
15113 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
15114 _("\
15115 Breakpoint specific settings.\n\
15116 Configure various breakpoint-specific variables such as\n\
15117 pending breakpoint behavior."),
15118 _("\
15119 Breakpoint specific settings.\n\
15120 Configure various breakpoint-specific variables such as\n\
15121 pending breakpoint behavior."),
15122 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
15123 &setlist, &showlist);
15125 add_setshow_auto_boolean_cmd ("pending", no_class,
15126 &pending_break_support, _("\
15127 Set debugger's behavior regarding pending breakpoints."), _("\
15128 Show debugger's behavior regarding pending breakpoints."), _("\
15129 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15130 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15131 an error. If auto, an unrecognized breakpoint location results in a\n\
15132 user-query to see if a pending breakpoint should be created."),
15133 NULL,
15134 show_pending_break_support,
15135 &breakpoint_set_cmdlist,
15136 &breakpoint_show_cmdlist);
15138 pending_break_support = AUTO_BOOLEAN_AUTO;
15140 add_setshow_boolean_cmd ("auto-hw", no_class,
15141 &automatic_hardware_breakpoints, _("\
15142 Set automatic usage of hardware breakpoints."), _("\
15143 Show automatic usage of hardware breakpoints."), _("\
15144 If set, the debugger will automatically use hardware breakpoints for\n\
15145 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15146 a warning will be emitted for such breakpoints."),
15147 NULL,
15148 show_automatic_hardware_breakpoints,
15149 &breakpoint_set_cmdlist,
15150 &breakpoint_show_cmdlist);
15152 add_setshow_boolean_cmd ("always-inserted", class_support,
15153 &always_inserted_mode, _("\
15154 Set mode for inserting breakpoints."), _("\
15155 Show mode for inserting breakpoints."), _("\
15156 When this mode is on, breakpoints are inserted immediately as soon as\n\
15157 they're created, kept inserted even when execution stops, and removed\n\
15158 only when the user deletes them. When this mode is off (the default),\n\
15159 breakpoints are inserted only when execution continues, and removed\n\
15160 when execution stops."),
15161 NULL,
15162 &show_always_inserted_mode,
15163 &breakpoint_set_cmdlist,
15164 &breakpoint_show_cmdlist);
15166 add_setshow_boolean_cmd ("breakpoint", class_maintenance,
15167 &debug_breakpoint, _("\
15168 Set breakpoint location debugging."), _("\
15169 Show breakpoint location debugging."), _("\
15170 When on, breakpoint location specific debugging is enabled."),
15171 NULL,
15172 show_debug_breakpoint,
15173 &setdebuglist, &showdebuglist);
15175 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15176 condition_evaluation_enums,
15177 &condition_evaluation_mode_1, _("\
15178 Set mode of breakpoint condition evaluation."), _("\
15179 Show mode of breakpoint condition evaluation."), _("\
15180 When this is set to \"host\", breakpoint conditions will be\n\
15181 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15182 breakpoint conditions will be downloaded to the target (if the target\n\
15183 supports such feature) and conditions will be evaluated on the target's side.\n\
15184 If this is set to \"auto\" (default), this will be automatically set to\n\
15185 \"target\" if it supports condition evaluation, otherwise it will\n\
15186 be set to \"host\"."),
15187 &set_condition_evaluation_mode,
15188 &show_condition_evaluation_mode,
15189 &breakpoint_set_cmdlist,
15190 &breakpoint_show_cmdlist);
15192 add_com ("break-range", class_breakpoint, break_range_command, _("\
15193 Set a breakpoint for an address range.\n\
15194 break-range START-LOCATION, END-LOCATION\n\
15195 where START-LOCATION and END-LOCATION can be one of the following:\n\
15196 LINENUM, for that line in the current file,\n\
15197 FILE:LINENUM, for that line in that file,\n\
15198 +OFFSET, for that number of lines after the current line\n\
15199 or the start of the range\n\
15200 FUNCTION, for the first line in that function,\n\
15201 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15202 *ADDRESS, for the instruction at that address.\n\
15204 The breakpoint will stop execution of the inferior whenever it executes\n\
15205 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15206 range (including START-LOCATION and END-LOCATION)."));
15208 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15209 Set a dynamic printf at specified location.\n\
15210 dprintf location,format string,arg1,arg2,...\n\
15211 location may be a linespec, explicit, or address location.\n"
15212 "\n" LOCATION_SPEC_HELP_STRING));
15213 set_cmd_completer (c, location_completer);
15215 add_setshow_enum_cmd ("dprintf-style", class_support,
15216 dprintf_style_enums, &dprintf_style, _("\
15217 Set the style of usage for dynamic printf."), _("\
15218 Show the style of usage for dynamic printf."), _("\
15219 This setting chooses how GDB will do a dynamic printf.\n\
15220 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15221 console, as with the \"printf\" command.\n\
15222 If the value is \"call\", the print is done by calling a function in your\n\
15223 program; by default printf(), but you can choose a different function or\n\
15224 output stream by setting dprintf-function and dprintf-channel."),
15225 update_dprintf_commands, NULL,
15226 &setlist, &showlist);
15228 add_setshow_string_cmd ("dprintf-function", class_support,
15229 &dprintf_function, _("\
15230 Set the function to use for dynamic printf."), _("\
15231 Show the function to use for dynamic printf."), NULL,
15232 update_dprintf_commands, NULL,
15233 &setlist, &showlist);
15235 add_setshow_string_cmd ("dprintf-channel", class_support,
15236 &dprintf_channel, _("\
15237 Set the channel to use for dynamic printf."), _("\
15238 Show the channel to use for dynamic printf."), NULL,
15239 update_dprintf_commands, NULL,
15240 &setlist, &showlist);
15242 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15243 &disconnected_dprintf, _("\
15244 Set whether dprintf continues after GDB disconnects."), _("\
15245 Show whether dprintf continues after GDB disconnects."), _("\
15246 Use this to let dprintf commands continue to hit and produce output\n\
15247 even if GDB disconnects or detaches from the target."),
15248 NULL,
15249 NULL,
15250 &setlist, &showlist);
15252 add_com ("agent-printf", class_vars, agent_printf_command, _("\
15253 Target agent only formatted printing, like the C \"printf\" function.\n\
15254 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15255 This supports most C printf format specifications, like %s, %d, etc.\n\
15256 This is useful for formatted output in user-defined commands."));
15258 automatic_hardware_breakpoints = true;
15260 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
15261 "breakpoint");
15262 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
15263 "breakpoint");
15264 gdb::observers::inferior_removed.attach (remove_inferior_breakpoints,
15265 "breakpoint");