More updated translations
[binutils-gdb.git] / gdb / breakpoint.c
blobe7fdeca91ff6a3c4ec48b80f9b0e9063fc6c7176
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 "gdbsupport/gdb_vecs.h"
25 #include "gdbsupport/unordered_set.h"
26 #include "symtab.h"
27 #include "frame.h"
28 #include "breakpoint.h"
29 #include "tracepoint.h"
30 #include "gdbtypes.h"
31 #include "expression.h"
32 #include "gdbcore.h"
33 #include "cli/cli-cmds.h"
34 #include "value.h"
35 #include "command.h"
36 #include "inferior.h"
37 #include "infrun.h"
38 #include "gdbthread.h"
39 #include "target.h"
40 #include "language.h"
41 #include "gdb-demangle.h"
42 #include "filenames.h"
43 #include "annotate.h"
44 #include "symfile.h"
45 #include "objfiles.h"
46 #include "source.h"
47 #include "linespec.h"
48 #include "completer.h"
49 #include "ui-out.h"
50 #include "cli/cli-script.h"
51 #include "block.h"
52 #include "solib.h"
53 #include "solist.h"
54 #include "observable.h"
55 #include "memattr.h"
56 #include "ada-lang.h"
57 #include "top.h"
58 #include "ui.h"
59 #include "valprint.h"
60 #include "jit.h"
61 #include "parser-defs.h"
62 #include "probe.h"
63 #include "cli/cli-utils.h"
64 #include "stack.h"
65 #include "ax-gdb.h"
66 #include "dummy-frame.h"
67 #include "interps.h"
68 #include "gdbsupport/format.h"
69 #include "thread-fsm.h"
70 #include "tid-parse.h"
71 #include "cli/cli-style.h"
72 #include "cli/cli-decode.h"
73 #include <unordered_set>
74 #include "break-cond-parse.h"
76 /* readline include files */
77 #include "readline/tilde.h"
79 /* readline defines this. */
80 #undef savestring
82 #include "mi/mi-common.h"
83 #include "extension.h"
84 #include <algorithm>
85 #include "progspace-and-thread.h"
86 #include "gdbsupport/array-view.h"
87 #include <optional>
88 #include "gdbsupport/common-utils.h"
90 /* Prototypes for local functions. */
92 static void map_breakpoint_numbers (const char *,
93 gdb::function_view<void (breakpoint *)>);
95 static void parse_breakpoint_sals (location_spec *locspec,
96 linespec_result *canonical,
97 program_space *search_pspace);
99 static void breakpoint_re_set_one (breakpoint *b,
100 program_space *filter_pspace);
102 static void create_breakpoints_sal (struct gdbarch *,
103 struct linespec_result *,
104 gdb::unique_xmalloc_ptr<char>,
105 gdb::unique_xmalloc_ptr<char>,
106 enum bptype,
107 enum bpdisp, int, int, int,
108 int,
109 int, int, int, unsigned);
111 static int can_use_hardware_watchpoint
112 (const std::vector<value_ref_ptr> &vals);
114 static void mention (const breakpoint *);
116 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
118 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
120 static struct breakpoint *
121 momentary_breakpoint_from_master (struct breakpoint *orig,
122 enum bptype type,
123 int loc_enabled, int thread);
125 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, bool);
127 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
128 CORE_ADDR bpaddr,
129 enum bptype bptype,
130 struct program_space *pspace);
132 static bool watchpoint_locations_match (const struct bp_location *loc1,
133 const struct bp_location *loc2);
135 static bool breakpoint_locations_match (const struct bp_location *loc1,
136 const struct bp_location *loc2,
137 bool sw_hw_bps_match = false);
139 static bool breakpoint_location_address_match (struct bp_location *bl,
140 const struct address_space *aspace,
141 CORE_ADDR addr);
143 static bool breakpoint_location_address_range_overlap (struct bp_location *,
144 const address_space *,
145 CORE_ADDR, int);
147 static int remove_breakpoint (struct bp_location *);
148 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
150 static enum print_stop_action print_bp_stop_message (bpstat *bs);
152 static int hw_breakpoint_used_count (void);
154 static int hw_watchpoint_use_count (struct breakpoint *);
156 static int hw_watchpoint_used_count_others (struct breakpoint *except,
157 enum bptype type,
158 int *other_type_used);
160 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
161 int count);
163 static void decref_bp_location (struct bp_location **loc);
165 static std::vector<symtab_and_line> bkpt_probe_decode_location_spec
166 (struct breakpoint *b,
167 location_spec *locspec,
168 struct program_space *search_pspace);
170 static bool bl_address_is_meaningful (const bp_location *loc);
172 static int find_loc_num_by_location (const bp_location *loc);
174 /* update_global_location_list's modes of operation wrt to whether to
175 insert locations now. */
176 enum ugll_insert_mode
178 /* Don't insert any breakpoint locations into the inferior, only
179 remove already-inserted locations that no longer should be
180 inserted. Functions that delete a breakpoint or breakpoints
181 should specify this mode, so that deleting a breakpoint doesn't
182 have the side effect of inserting the locations of other
183 breakpoints that are marked not-inserted, but should_be_inserted
184 returns true on them.
186 This behavior is useful is situations close to tear-down -- e.g.,
187 after an exec, while the target still has execution, but
188 breakpoint shadows of the previous executable image should *NOT*
189 be restored to the new image; or before detaching, where the
190 target still has execution and wants to delete breakpoints from
191 GDB's lists, and all breakpoints had already been removed from
192 the inferior. */
193 UGLL_DONT_INSERT,
195 /* May insert breakpoints iff breakpoints_should_be_inserted_now
196 claims breakpoints should be inserted now. */
197 UGLL_MAY_INSERT,
199 /* Insert locations now, irrespective of
200 breakpoints_should_be_inserted_now. E.g., say all threads are
201 stopped right now, and the user did "continue". We need to
202 insert breakpoints _before_ resuming the target, but
203 UGLL_MAY_INSERT wouldn't insert them, because
204 breakpoints_should_be_inserted_now returns false at that point,
205 as no thread is running yet. */
206 UGLL_INSERT
209 /* Return a textual version of INSERT_MODE. */
211 static const char *
212 ugll_insert_mode_text (ugll_insert_mode insert_mode)
214 /* Make sure the compiler warns if a new ugll_insert_mode enumerator is added
215 but not handled here. */
216 DIAGNOSTIC_PUSH
217 DIAGNOSTIC_ERROR_SWITCH
218 switch (insert_mode)
220 case UGLL_DONT_INSERT:
221 return "UGLL_DONT_INSERT";
222 case UGLL_MAY_INSERT:
223 return "UGLL_MAY_INSERT";
224 case UGLL_INSERT:
225 return "UGLL_INSERT";
227 DIAGNOSTIC_POP
229 gdb_assert_not_reached ("must handle all enum values");
232 /* Return a textual version of REASON. */
234 static const char *
235 remove_bp_reason_str (remove_bp_reason reason)
237 /* Make sure the compiler warns if a new remove_bp_reason enumerator is added
238 but not handled here. */
239 DIAGNOSTIC_PUSH
240 DIAGNOSTIC_ERROR_SWITCH
241 switch (reason)
243 case REMOVE_BREAKPOINT:
244 return "regular remove";
245 case DETACH_BREAKPOINT:
246 return "detach";
248 DIAGNOSTIC_POP
250 gdb_assert_not_reached ("must handle all enum values");
253 /* Return a textual version of breakpoint location BL describing number,
254 location and address. */
256 static std::string
257 breakpoint_location_address_str (const bp_location *bl)
259 std::string str = string_printf ("Breakpoint %d (%s) at address %s",
260 bl->owner->number,
261 host_address_to_string (bl),
262 paddress (bl->gdbarch, bl->address));
264 std::string loc_string = bl->to_string ();
265 if (!loc_string.empty ())
266 str += string_printf (" %s", loc_string.c_str ());
268 return str;
271 static void update_global_location_list (enum ugll_insert_mode);
273 static void update_global_location_list_nothrow (enum ugll_insert_mode);
275 static void insert_breakpoint_locations (void);
277 static void trace_pass_command (const char *, int);
279 static void set_tracepoint_count (int num);
281 static bool is_masked_watchpoint (const struct breakpoint *b);
283 /* Return true if B refers to a static tracepoint set by marker ("-m"),
284 zero otherwise. */
286 static bool strace_marker_p (struct breakpoint *b);
288 static void bkpt_probe_create_sals_from_location_spec
289 (location_spec *locspec,
290 struct linespec_result *canonical,
291 struct program_space *search_pspace);
293 const struct breakpoint_ops code_breakpoint_ops =
295 parse_breakpoint_sals,
296 create_breakpoints_sal,
299 /* Breakpoints set on probes. */
300 static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
302 bkpt_probe_create_sals_from_location_spec,
303 create_breakpoints_sal,
306 /* Tracepoints set on probes. We use the same methods as for breakpoints
307 on probes. */
308 static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
310 bkpt_probe_create_sals_from_location_spec,
311 create_breakpoints_sal,
314 /* Implementation of abstract dtors. These must exist to satisfy the
315 linker. */
317 breakpoint::~breakpoint ()
321 code_breakpoint::~code_breakpoint ()
325 catchpoint::~catchpoint ()
329 /* The structure to be used in regular breakpoints. */
330 struct ordinary_breakpoint : public code_breakpoint
332 using code_breakpoint::code_breakpoint;
334 int resources_needed (const struct bp_location *) override;
335 enum print_stop_action print_it (const bpstat *bs) const override;
336 void print_mention () const override;
337 void print_recreate (struct ui_file *fp) const override;
340 /* Internal breakpoints. These typically have a lifetime the same as
341 the program, and they end up installed on the breakpoint chain with
342 a negative breakpoint number. They're visible in "maint info
343 breakpoints", but not "info breakpoints". */
344 struct internal_breakpoint : public code_breakpoint
346 internal_breakpoint (struct gdbarch *gdbarch,
347 enum bptype type, CORE_ADDR address)
348 : code_breakpoint (gdbarch, type)
350 symtab_and_line sal;
351 sal.pc = address;
352 sal.section = find_pc_overlay (sal.pc);
353 sal.pspace = current_program_space;
354 add_location (sal);
356 pspace = current_program_space;
357 disposition = disp_donttouch;
360 void re_set (program_space *pspace) override;
361 void check_status (struct bpstat *bs) override;
362 enum print_stop_action print_it (const bpstat *bs) const override;
363 void print_mention () const override;
366 /* Momentary breakpoints. These typically have a lifetime of some run
367 control command only, are always thread-specific, and have 0 for
368 breakpoint number. I.e., there can be many momentary breakpoints
369 on the breakpoint chain and they all same the same number (zero).
370 They're visible in "maint info breakpoints", but not "info
371 breakpoints". */
372 struct momentary_breakpoint : public code_breakpoint
374 momentary_breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
375 program_space *pspace_,
376 const struct frame_id &frame_id_,
377 int thread_)
378 : code_breakpoint (gdbarch_, bptype)
380 /* If FRAME_ID is valid, it should be a real frame, not an inlined
381 or tail-called one. */
382 gdb_assert (!frame_id_artificial_p (frame_id));
384 /* Momentary breakpoints are always thread-specific. */
385 gdb_assert (thread_ > 0);
387 pspace = pspace_;
388 enable_state = bp_enabled;
389 disposition = disp_donttouch;
390 frame_id = frame_id_;
391 thread = thread_;
393 /* The inferior should have been set by the parent constructor. */
394 gdb_assert (inferior == -1);
397 void re_set (program_space *pspace) override;
398 void check_status (struct bpstat *bs) override;
399 enum print_stop_action print_it (const bpstat *bs) const override;
400 void print_mention () const override;
403 /* DPrintf breakpoints. */
404 struct dprintf_breakpoint : public ordinary_breakpoint
406 using ordinary_breakpoint::ordinary_breakpoint;
408 void re_set (program_space *pspace) override;
409 int breakpoint_hit (const struct bp_location *bl,
410 const address_space *aspace,
411 CORE_ADDR bp_addr,
412 const target_waitstatus &ws) override;
413 void print_recreate (struct ui_file *fp) const override;
414 void after_condition_true (struct bpstat *bs) override;
417 /* Ranged breakpoints. */
418 struct ranged_breakpoint : public ordinary_breakpoint
420 explicit ranged_breakpoint (struct gdbarch *gdbarch,
421 const symtab_and_line &sal_start,
422 int length,
423 location_spec_up start_locspec,
424 location_spec_up end_locspec)
425 : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
427 bp_location *bl = add_location (sal_start);
428 bl->length = length;
430 disposition = disp_donttouch;
432 locspec = std::move (start_locspec);
433 locspec_range_end = std::move (end_locspec);
436 int breakpoint_hit (const struct bp_location *bl,
437 const address_space *aspace,
438 CORE_ADDR bp_addr,
439 const target_waitstatus &ws) override;
440 int resources_needed (const struct bp_location *) override;
441 enum print_stop_action print_it (const bpstat *bs) const override;
442 bool print_one (const bp_location **) const override;
443 void print_one_detail (struct ui_out *) const override;
444 void print_mention () const override;
445 void print_recreate (struct ui_file *fp) const override;
448 /* Static tracepoints with marker (`-m'). */
449 struct static_marker_tracepoint : public tracepoint
451 using tracepoint::tracepoint;
453 std::vector<symtab_and_line> decode_location_spec
454 (struct location_spec *locspec,
455 struct program_space *search_pspace) override;
458 /* The style in which to perform a dynamic printf. This is a user
459 option because different output options have different tradeoffs;
460 if GDB does the printing, there is better error handling if there
461 is a problem with any of the arguments, but using an inferior
462 function lets you have special-purpose printers and sending of
463 output to the same place as compiled-in print functions. */
465 static const char dprintf_style_gdb[] = "gdb";
466 static const char dprintf_style_call[] = "call";
467 static const char dprintf_style_agent[] = "agent";
468 static const char *const dprintf_style_enums[] = {
469 dprintf_style_gdb,
470 dprintf_style_call,
471 dprintf_style_agent,
472 NULL
474 static const char *dprintf_style = dprintf_style_gdb;
476 /* The function to use for dynamic printf if the preferred style is to
477 call into the inferior. The value is simply a string that is
478 copied into the command, so it can be anything that GDB can
479 evaluate to a callable address, not necessarily a function name. */
481 static std::string dprintf_function = "printf";
483 /* The channel to use for dynamic printf if the preferred style is to
484 call into the inferior; if a nonempty string, it will be passed to
485 the call as the first argument, with the format string as the
486 second. As with the dprintf function, this can be anything that
487 GDB knows how to evaluate, so in addition to common choices like
488 "stderr", this could be an app-specific expression like
489 "mystreams[curlogger]". */
491 static std::string dprintf_channel;
493 /* True if dprintf commands should continue to operate even if GDB
494 has disconnected. */
495 static bool disconnected_dprintf = true;
497 struct command_line *
498 breakpoint_commands (struct breakpoint *b)
500 return b->commands ? b->commands.get () : NULL;
503 /* Flag indicating that a command has proceeded the inferior past the
504 current breakpoint. */
506 static bool breakpoint_proceeded;
508 const char *
509 bpdisp_text (enum bpdisp disp)
511 /* NOTE: the following values are a part of MI protocol and
512 represent values of 'disp' field returned when inferior stops at
513 a breakpoint. */
514 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
516 return bpdisps[(int) disp];
519 /* Prototypes for exported functions. */
520 /* If FALSE, gdb will not use hardware support for watchpoints, even
521 if such is available. */
522 static int can_use_hw_watchpoints;
524 static void
525 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
526 struct cmd_list_element *c,
527 const char *value)
529 gdb_printf (file,
530 _("Debugger's willingness to use "
531 "watchpoint hardware is %s.\n"),
532 value);
535 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
536 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
537 for unrecognized breakpoint locations.
538 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
539 static enum auto_boolean pending_break_support;
540 static void
541 show_pending_break_support (struct ui_file *file, int from_tty,
542 struct cmd_list_element *c,
543 const char *value)
545 gdb_printf (file,
546 _("Debugger's behavior regarding "
547 "pending breakpoints is %s.\n"),
548 value);
551 /* If true, gdb will automatically use hardware breakpoints for breakpoints
552 set with "break" but falling in read-only memory.
553 If false, gdb will warn about such breakpoints, but won't automatically
554 use hardware breakpoints. */
555 static bool automatic_hardware_breakpoints;
556 static void
557 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
558 struct cmd_list_element *c,
559 const char *value)
561 gdb_printf (file,
562 _("Automatic usage of hardware breakpoints is %s.\n"),
563 value);
566 /* If on, GDB keeps breakpoints inserted even if the inferior is
567 stopped, and immediately inserts any new breakpoints as soon as
568 they're created. If off (default), GDB keeps breakpoints off of
569 the target as long as possible. That is, it delays inserting
570 breakpoints until the next resume, and removes them again when the
571 target fully stops. This is a bit safer in case GDB crashes while
572 processing user input. */
573 static bool always_inserted_mode = false;
575 static void
576 show_always_inserted_mode (struct ui_file *file, int from_tty,
577 struct cmd_list_element *c, const char *value)
579 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
580 value);
583 /* See breakpoint.h. */
584 bool debug_breakpoint = false;
586 /* "show debug breakpoint" implementation. */
587 static void
588 show_debug_breakpoint (struct ui_file *file, int from_tty,
589 struct cmd_list_element *c, const char *value)
591 gdb_printf (file, _("Breakpoint location debugging is %s.\n"), value);
594 /* See breakpoint.h. */
597 breakpoints_should_be_inserted_now (void)
599 if (gdbarch_has_global_breakpoints (current_inferior ()->arch ()))
601 /* If breakpoints are global, they should be inserted even if no
602 thread under gdb's control is running, or even if there are
603 no threads under GDB's control yet. */
604 return 1;
606 else
608 if (always_inserted_mode)
610 /* The user wants breakpoints inserted even if all threads
611 are stopped. */
612 return 1;
615 for (inferior *inf : all_inferiors ())
616 if (inf->has_execution ()
617 && threads_are_executing (inf->process_target ()))
618 return 1;
620 /* Don't remove breakpoints yet if, even though all threads are
621 stopped, we still have events to process. */
622 for (thread_info *tp : all_non_exited_threads ())
623 if (tp->resumed () && tp->has_pending_waitstatus ())
624 return 1;
626 return 0;
629 static const char condition_evaluation_both[] = "host or target";
631 /* Modes for breakpoint condition evaluation. */
632 static const char condition_evaluation_auto[] = "auto";
633 static const char condition_evaluation_host[] = "host";
634 static const char condition_evaluation_target[] = "target";
635 static const char *const condition_evaluation_enums[] = {
636 condition_evaluation_auto,
637 condition_evaluation_host,
638 condition_evaluation_target,
639 NULL
642 /* Global that holds the current mode for breakpoint condition evaluation. */
643 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
645 /* Global that we use to display information to the user (gets its value from
646 condition_evaluation_mode_1. */
647 static const char *condition_evaluation_mode = condition_evaluation_auto;
649 /* Translate a condition evaluation mode MODE into either "host"
650 or "target". This is used mostly to translate from "auto" to the
651 real setting that is being used. It returns the translated
652 evaluation mode. */
654 static const char *
655 translate_condition_evaluation_mode (const char *mode)
657 if (mode == condition_evaluation_auto)
659 if (target_supports_evaluation_of_breakpoint_conditions ())
660 return condition_evaluation_target;
661 else
662 return condition_evaluation_host;
664 else
665 return mode;
668 /* Discovers what condition_evaluation_auto translates to. */
670 static const char *
671 breakpoint_condition_evaluation_mode (void)
673 return translate_condition_evaluation_mode (condition_evaluation_mode);
676 /* Return true if GDB should evaluate breakpoint conditions or false
677 otherwise. */
679 static bool
680 gdb_evaluates_breakpoint_condition_p (void)
682 const char *mode = breakpoint_condition_evaluation_mode ();
684 return (mode == condition_evaluation_host);
687 /* Are we executing breakpoint commands? */
688 static int executing_breakpoint_commands;
690 /* Are overlay event breakpoints enabled? */
691 static int overlay_events_enabled;
693 /* See description in breakpoint.h. */
694 bool target_exact_watchpoints = false;
696 /* Chains of all breakpoints defined. */
698 static intrusive_list<breakpoint> breakpoint_chain;
700 /* See breakpoint.h. */
702 breakpoint_range
703 all_breakpoints ()
705 return breakpoint_range (breakpoint_chain.begin (), breakpoint_chain.end ());
708 /* See breakpoint.h. */
710 breakpoint_safe_range
711 all_breakpoints_safe ()
713 return breakpoint_safe_range (all_breakpoints ());
716 /* See breakpoint.h. */
718 tracepoint_range
719 all_tracepoints ()
721 return tracepoint_range (tracepoint_iterator (breakpoint_chain.begin ()),
722 tracepoint_iterator (breakpoint_chain.end ()));
725 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
727 static std::vector<bp_location *> bp_locations;
729 /* See breakpoint.h. */
731 const std::vector<bp_location *> &
732 all_bp_locations ()
734 return bp_locations;
737 /* Range to iterate over breakpoint locations at a given address. */
739 struct bp_locations_at_addr_range
741 using iterator = std::vector<bp_location *>::iterator;
743 bp_locations_at_addr_range (CORE_ADDR addr)
745 struct compare
747 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
748 { return loc->address < addr_; }
750 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
751 { return addr_ < loc->address; }
754 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
755 addr, compare ());
757 m_begin = it_pair.first;
758 m_end = it_pair.second;
761 iterator begin () const
762 { return m_begin; }
764 iterator end () const
765 { return m_end; }
767 private:
768 iterator m_begin;
769 iterator m_end;
772 /* Return a range to iterate over all breakpoint locations exactly at address
773 ADDR.
775 If it's needed to iterate multiple times on the same range, it's possible
776 to save the range in a local variable and use it multiple times:
778 auto range = all_bp_locations_at_addr (addr);
780 for (bp_location *loc : range)
781 // use loc
783 for (bp_location *loc : range)
784 // use loc
786 This saves a bit of time, as it avoids re-doing the binary searches to find
787 the range's boundaries. Just remember not to change the bp_locations vector
788 in the mean time, as it could make the range's iterators stale. */
790 static bp_locations_at_addr_range
791 all_bp_locations_at_addr (CORE_ADDR addr)
793 return bp_locations_at_addr_range (addr);
796 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
797 ADDRESS for the current elements of BP_LOCATIONS which get a valid
798 result from bp_location_has_shadow. You can use it for roughly
799 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
800 an address you need to read. */
802 static CORE_ADDR bp_locations_placed_address_before_address_max;
804 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
805 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
806 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
807 You can use it for roughly limiting the subrange of BP_LOCATIONS to
808 scan for shadow bytes for an address you need to read. */
810 static CORE_ADDR bp_locations_shadow_len_after_address_max;
812 /* The locations that no longer correspond to any breakpoint, unlinked
813 from the bp_locations array, but for which a hit may still be
814 reported by a target. */
815 static std::vector<bp_location *> moribund_locations;
817 /* Number of last breakpoint made. */
819 static int breakpoint_count;
821 /* The value of `breakpoint_count' before the last command that
822 created breakpoints. If the last (break-like) command created more
823 than one breakpoint, then the difference between BREAKPOINT_COUNT
824 and PREV_BREAKPOINT_COUNT is more than one. */
825 static int prev_breakpoint_count;
827 /* Number of last tracepoint made. */
829 static int tracepoint_count;
831 static struct cmd_list_element *breakpoint_set_cmdlist;
832 static struct cmd_list_element *breakpoint_show_cmdlist;
833 struct cmd_list_element *save_cmdlist;
835 /* Return whether a breakpoint is an active enabled breakpoint. */
836 static bool
837 breakpoint_enabled (struct breakpoint *b)
839 return (b->enable_state == bp_enabled);
842 /* Set breakpoint count to NUM. */
844 static void
845 set_breakpoint_count (int num)
847 prev_breakpoint_count = breakpoint_count;
848 breakpoint_count = num;
849 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
852 /* Used by `start_rbreak_breakpoints' below, to record the current
853 breakpoint count before "rbreak" creates any breakpoint. */
854 static int rbreak_start_breakpoint_count;
856 /* Called at the start an "rbreak" command to record the first
857 breakpoint made. */
859 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
861 rbreak_start_breakpoint_count = breakpoint_count;
864 /* Called at the end of an "rbreak" command to record the last
865 breakpoint made. */
867 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
869 prev_breakpoint_count = rbreak_start_breakpoint_count;
872 /* See breakpoint.h. */
875 scoped_rbreak_breakpoints::first_breakpoint () const
877 return rbreak_start_breakpoint_count + 1;
880 /* See breakpoint.h. */
883 scoped_rbreak_breakpoints::last_breakpoint () const
885 return (rbreak_start_breakpoint_count == breakpoint_count
886 ? -1
887 : breakpoint_count);
890 /* Used in run_command to zero the hit count when a new run starts. */
892 void
893 clear_breakpoint_hit_counts (void)
895 for (breakpoint &b : all_breakpoints ())
896 b.hit_count = 0;
900 /* Return the breakpoint with the specified number, or NULL
901 if the number does not refer to an existing breakpoint. */
903 struct breakpoint *
904 get_breakpoint (int num)
906 for (breakpoint &b : all_breakpoints ())
907 if (b.number == num)
908 return &b;
910 return nullptr;
913 /* Return TRUE if NUM refer to an existing breakpoint that has
914 multiple code locations. */
916 static bool
917 has_multiple_locations (int num)
919 for (breakpoint &b : all_breakpoints ())
920 if (b.number == num)
921 return b.has_multiple_locations ();
923 return false;
928 /* Mark locations as "conditions have changed" in case the target supports
929 evaluating conditions on its side. */
931 static void
932 mark_breakpoint_modified (struct breakpoint *b)
934 /* This is only meaningful if the target is
935 evaluating conditions and if the user has
936 opted for condition evaluation on the target's
937 side. */
938 if (gdb_evaluates_breakpoint_condition_p ()
939 || !target_supports_evaluation_of_breakpoint_conditions ())
940 return;
942 if (!is_breakpoint (b))
943 return;
945 for (bp_location &loc : b->locations ())
946 loc.condition_changed = condition_modified;
949 /* Mark location as "conditions have changed" in case the target supports
950 evaluating conditions on its side. */
952 static void
953 mark_breakpoint_location_modified (struct bp_location *loc)
955 /* This is only meaningful if the target is
956 evaluating conditions and if the user has
957 opted for condition evaluation on the target's
958 side. */
959 if (gdb_evaluates_breakpoint_condition_p ()
960 || !target_supports_evaluation_of_breakpoint_conditions ())
962 return;
964 if (!is_breakpoint (loc->owner))
965 return;
967 loc->condition_changed = condition_modified;
970 /* Sets the condition-evaluation mode using the static global
971 condition_evaluation_mode. */
973 static void
974 set_condition_evaluation_mode (const char *args, int from_tty,
975 struct cmd_list_element *c)
977 const char *old_mode, *new_mode;
979 if ((condition_evaluation_mode_1 == condition_evaluation_target)
980 && !target_supports_evaluation_of_breakpoint_conditions ())
982 condition_evaluation_mode_1 = condition_evaluation_mode;
983 warning (_("Target does not support breakpoint condition evaluation.\n"
984 "Using host evaluation mode instead."));
985 return;
988 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
989 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
991 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
992 settings was "auto". */
993 condition_evaluation_mode = condition_evaluation_mode_1;
995 /* Only update the mode if the user picked a different one. */
996 if (new_mode != old_mode)
998 /* If the user switched to a different evaluation mode, we
999 need to synch the changes with the target as follows:
1001 "host" -> "target": Send all (valid) conditions to the target.
1002 "target" -> "host": Remove all the conditions from the target.
1005 if (new_mode == condition_evaluation_target)
1007 /* Mark everything modified and synch conditions with the
1008 target. */
1009 for (bp_location *loc : all_bp_locations ())
1010 mark_breakpoint_location_modified (loc);
1012 else
1014 /* Manually mark non-duplicate locations to synch conditions
1015 with the target. We do this to remove all the conditions the
1016 target knows about. */
1017 for (bp_location *loc : all_bp_locations ())
1018 if (is_breakpoint (loc->owner) && loc->inserted)
1019 loc->needs_update = 1;
1022 /* Do the update. */
1023 update_global_location_list (UGLL_MAY_INSERT);
1026 return;
1029 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
1030 what "auto" is translating to. */
1032 static void
1033 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
1034 struct cmd_list_element *c, const char *value)
1036 if (condition_evaluation_mode == condition_evaluation_auto)
1037 gdb_printf (file,
1038 _("Breakpoint condition evaluation "
1039 "mode is %s (currently %s).\n"),
1040 value,
1041 breakpoint_condition_evaluation_mode ());
1042 else
1043 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
1044 value);
1047 /* Parse COND_STRING in the context of LOC and set as the condition
1048 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
1049 the number of LOC within its owner. In case of parsing error, mark
1050 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
1052 static void
1053 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
1054 int bp_num, int loc_num)
1056 bool has_junk = false;
1059 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
1060 block_for_pc (loc->address), 0);
1061 if (*cond_string != 0)
1062 has_junk = true;
1063 else
1065 loc->cond = std::move (new_exp);
1066 if (loc->disabled_by_cond && loc->enabled)
1067 gdb_printf (_("Breakpoint %d's condition is now valid at "
1068 "location %d, enabling.\n"),
1069 bp_num, loc_num);
1071 loc->disabled_by_cond = false;
1074 catch (const gdb_exception_error &e)
1076 if (loc->enabled)
1078 /* Warn if a user-enabled location is now becoming disabled-by-cond.
1079 BP_NUM is 0 if the breakpoint is being defined for the first
1080 time using the "break ... if ..." command, and non-zero if
1081 already defined. */
1082 if (bp_num != 0)
1083 warning (_("failed to validate condition at location %d.%d, "
1084 "disabling:\n %s"), bp_num, loc_num, e.what ());
1085 else
1086 warning (_("failed to validate condition at location %d, "
1087 "disabling:\n %s"), loc_num, e.what ());
1090 loc->disabled_by_cond = true;
1093 if (has_junk)
1094 error (_("Garbage '%s' follows condition"), cond_string);
1097 /* See breakpoint.h. */
1099 void
1100 notify_breakpoint_modified (breakpoint *b)
1102 interps_notify_breakpoint_modified (b);
1103 gdb::observers::breakpoint_modified.notify (b);
1106 void
1107 set_breakpoint_condition (struct breakpoint *b, const char *exp,
1108 int from_tty, bool force)
1110 if (*exp == 0)
1112 b->cond_string.reset ();
1114 if (is_watchpoint (b))
1115 gdb::checked_static_cast<watchpoint *> (b)->cond_exp.reset ();
1116 else
1118 int loc_num = 1;
1119 for (bp_location &loc : b->locations ())
1121 loc.cond.reset ();
1122 if (loc.disabled_by_cond && loc.enabled)
1123 gdb_printf (_("Breakpoint %d's condition is now valid at "
1124 "location %d, enabling.\n"),
1125 b->number, loc_num);
1126 loc.disabled_by_cond = false;
1127 loc_num++;
1129 /* No need to free the condition agent expression
1130 bytecode (if we have one). We will handle this
1131 when we go through update_global_location_list. */
1135 if (from_tty)
1136 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
1138 else
1140 if (is_watchpoint (b))
1142 innermost_block_tracker tracker;
1143 const char *arg = exp;
1144 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
1145 if (*arg != 0)
1146 error (_("Junk at end of expression"));
1147 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
1148 w->cond_exp = std::move (new_exp);
1149 w->cond_exp_valid_block = tracker.block ();
1151 else
1153 /* Parse and set condition expressions. We make two passes.
1154 In the first, we parse the condition string to see if it
1155 is valid in at least one location. If so, the condition
1156 would be accepted. So we go ahead and set the locations'
1157 conditions. In case no valid case is found, we throw
1158 the error and the condition string will be rejected.
1159 This two-pass approach is taken to avoid setting the
1160 state of locations in case of a reject. */
1161 for (const bp_location &loc : b->locations ())
1165 const char *arg = exp;
1166 parse_exp_1 (&arg, loc.address,
1167 block_for_pc (loc.address), 0);
1168 if (*arg != 0)
1169 error (_("Junk at end of expression"));
1170 break;
1172 catch (const gdb_exception_error &e)
1174 /* Condition string is invalid. If this happens to
1175 be the last loc, abandon (if not forced) or continue
1176 (if forced). */
1177 if (&loc == &b->last_loc () && !force)
1178 throw;
1182 /* If we reach here, the condition is valid at some locations. */
1183 int loc_num = 1;
1184 for (bp_location &loc : b->locations ())
1186 set_breakpoint_location_condition (exp, &loc, b->number, loc_num);
1187 loc_num++;
1191 /* We know that the new condition parsed successfully. The
1192 condition string of the breakpoint can be safely updated. */
1193 b->cond_string = make_unique_xstrdup (exp);
1194 b->condition_not_parsed = 0;
1196 mark_breakpoint_modified (b);
1198 notify_breakpoint_modified (b);
1201 /* See breakpoint.h. */
1203 void
1204 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1205 bool force)
1207 for (breakpoint &b : all_breakpoints ())
1208 if (b.number == bpnum)
1210 /* Check if this breakpoint has a "stop" method implemented in an
1211 extension language. This method and conditions entered into GDB
1212 from the CLI are mutually exclusive. */
1213 const struct extension_language_defn *extlang
1214 = get_breakpoint_cond_ext_lang (&b, EXT_LANG_NONE);
1216 if (extlang != NULL)
1218 error (_("Only one stop condition allowed. There is currently"
1219 " a %s stop condition defined for this breakpoint."),
1220 ext_lang_capitalized_name (extlang));
1222 set_breakpoint_condition (&b, exp, from_tty, force);
1224 if (is_breakpoint (&b))
1225 update_global_location_list (UGLL_MAY_INSERT);
1227 return;
1230 error (_("No breakpoint number %d."), bpnum);
1233 /* The options for the "condition" command. */
1235 struct condition_command_opts
1237 /* For "-force". */
1238 bool force_condition = false;
1241 static const gdb::option::option_def condition_command_option_defs[] = {
1243 gdb::option::flag_option_def<condition_command_opts> {
1244 "force",
1245 [] (condition_command_opts *opts) { return &opts->force_condition; },
1246 N_("Set the condition even if it is invalid for all current locations."),
1251 /* Create an option_def_group for the "condition" options, with
1252 CC_OPTS as context. */
1254 static inline gdb::option::option_def_group
1255 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1257 return {{condition_command_option_defs}, cc_opts};
1260 /* Completion for the "condition" command. */
1262 static void
1263 condition_completer (struct cmd_list_element *cmd,
1264 completion_tracker &tracker,
1265 const char *text, const char * /*word*/)
1267 bool has_no_arguments = (*text == '\0');
1268 condition_command_opts cc_opts;
1269 const auto group = make_condition_command_options_def_group (&cc_opts);
1270 if (gdb::option::complete_options
1271 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1272 return;
1274 text = skip_spaces (text);
1275 const char *space = skip_to_space (text);
1276 if (*space == '\0')
1278 int len;
1280 if (text[0] == '$')
1282 tracker.advance_custom_word_point_by (1);
1283 /* We don't support completion of history indices. */
1284 if (!isdigit (text[1]))
1285 complete_internalvar (tracker, &text[1]);
1286 return;
1289 /* Suggest the "-force" flag if no arguments are given. If
1290 arguments were passed, they either already include the flag,
1291 or we are beyond the point of suggesting it because it's
1292 positionally the first argument. */
1293 if (has_no_arguments)
1294 gdb::option::complete_on_all_options (tracker, group);
1296 /* We're completing the breakpoint number. */
1297 len = strlen (text);
1299 for (breakpoint &b : all_breakpoints ())
1301 char number[50];
1303 xsnprintf (number, sizeof (number), "%d", b.number);
1305 if (strncmp (number, text, len) == 0)
1306 tracker.add_completion (make_unique_xstrdup (number));
1309 return;
1312 /* We're completing the expression part. Skip the breakpoint num. */
1313 const char *exp_start = skip_spaces (space);
1314 tracker.advance_custom_word_point_by (exp_start - text);
1315 text = exp_start;
1316 const char *word = advance_to_expression_complete_word_point (tracker, text);
1317 expression_completer (cmd, tracker, text, word);
1320 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1322 static void
1323 condition_command (const char *arg, int from_tty)
1325 const char *p;
1326 int bnum;
1328 if (arg == 0)
1329 error_no_arg (_("breakpoint number"));
1331 p = arg;
1333 /* Check if the "-force" flag was passed. */
1334 condition_command_opts cc_opts;
1335 const auto group = make_condition_command_options_def_group (&cc_opts);
1336 gdb::option::process_options
1337 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1339 bnum = get_number (&p);
1340 if (bnum == 0)
1341 error (_("Bad breakpoint argument: '%s'"), arg);
1343 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1346 /* Check that COMMAND do not contain commands that are suitable
1347 only for tracepoints and not suitable for ordinary breakpoints.
1348 Throw if any such commands is found. */
1350 static void
1351 check_no_tracepoint_commands (struct command_line *commands)
1353 struct command_line *c;
1355 for (c = commands; c; c = c->next)
1357 if (c->control_type == while_stepping_control)
1358 error (_("The 'while-stepping' command can "
1359 "only be used for tracepoints"));
1361 check_no_tracepoint_commands (c->body_list_0.get ());
1362 check_no_tracepoint_commands (c->body_list_1.get ());
1364 /* Not that command parsing removes leading whitespace and comment
1365 lines and also empty lines. So, we only need to check for
1366 command directly. */
1367 if (strstr (c->line, "collect ") == c->line)
1368 error (_("The 'collect' command can only be used for tracepoints"));
1370 if (strstr (c->line, "teval ") == c->line)
1371 error (_("The 'teval' command can only be used for tracepoints"));
1375 struct longjmp_breakpoint : public momentary_breakpoint
1377 using momentary_breakpoint::momentary_breakpoint;
1379 ~longjmp_breakpoint () override;
1382 /* Encapsulate tests for different types of tracepoints. */
1384 static bool
1385 is_tracepoint_type (bptype type)
1387 return (type == bp_tracepoint
1388 || type == bp_fast_tracepoint
1389 || type == bp_static_tracepoint
1390 || type == bp_static_marker_tracepoint);
1393 /* See breakpoint.h. */
1395 bool
1396 is_tracepoint (const struct breakpoint *b)
1398 return is_tracepoint_type (b->type);
1401 /* Factory function to create an appropriate instance of breakpoint given
1402 TYPE. */
1404 template<typename... Arg>
1405 static std::unique_ptr<code_breakpoint>
1406 new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type,
1407 Arg&&... args)
1409 code_breakpoint *b;
1411 switch (type)
1413 case bp_breakpoint:
1414 case bp_hardware_breakpoint:
1415 b = new ordinary_breakpoint (gdbarch, type,
1416 std::forward<Arg> (args)...);
1417 break;
1419 case bp_fast_tracepoint:
1420 case bp_static_tracepoint:
1421 case bp_tracepoint:
1422 b = new tracepoint (gdbarch, type,
1423 std::forward<Arg> (args)...);
1424 break;
1426 case bp_static_marker_tracepoint:
1427 b = new static_marker_tracepoint (gdbarch, type,
1428 std::forward<Arg> (args)...);
1429 break;
1431 case bp_dprintf:
1432 b = new dprintf_breakpoint (gdbarch, type,
1433 std::forward<Arg> (args)...);
1434 break;
1436 default:
1437 gdb_assert_not_reached ("invalid type");
1440 return std::unique_ptr<code_breakpoint> (b);
1443 /* A helper function that validates that COMMANDS are valid for a
1444 breakpoint. This function will throw an exception if a problem is
1445 found. */
1447 static void
1448 validate_commands_for_breakpoint (struct breakpoint *b,
1449 struct command_line *commands)
1451 if (is_tracepoint (b))
1453 tracepoint *t = gdb::checked_static_cast<tracepoint *> (b);
1454 struct command_line *c;
1455 struct command_line *while_stepping = 0;
1457 /* Reset the while-stepping step count. The previous commands
1458 might have included a while-stepping action, while the new
1459 ones might not. */
1460 t->step_count = 0;
1462 /* We need to verify that each top-level element of commands is
1463 valid for tracepoints, that there's at most one
1464 while-stepping element, and that the while-stepping's body
1465 has valid tracing commands excluding nested while-stepping.
1466 We also need to validate the tracepoint action line in the
1467 context of the tracepoint --- validate_actionline actually
1468 has side effects, like setting the tracepoint's
1469 while-stepping STEP_COUNT, in addition to checking if the
1470 collect/teval actions parse and make sense in the
1471 tracepoint's context. */
1472 for (c = commands; c; c = c->next)
1474 if (c->control_type == while_stepping_control)
1476 if (b->type == bp_fast_tracepoint)
1477 error (_("The 'while-stepping' command "
1478 "cannot be used for fast tracepoint"));
1479 else if (b->type == bp_static_tracepoint
1480 || b->type == bp_static_marker_tracepoint)
1481 error (_("The 'while-stepping' command "
1482 "cannot be used for static tracepoint"));
1484 if (while_stepping)
1485 error (_("The 'while-stepping' command "
1486 "can be used only once"));
1487 else
1488 while_stepping = c;
1491 validate_actionline (c->line, t);
1493 if (while_stepping)
1495 struct command_line *c2;
1497 gdb_assert (while_stepping->body_list_1 == nullptr);
1498 c2 = while_stepping->body_list_0.get ();
1499 for (; c2; c2 = c2->next)
1501 if (c2->control_type == while_stepping_control)
1502 error (_("The 'while-stepping' command cannot be nested"));
1506 else
1508 check_no_tracepoint_commands (commands);
1512 /* Return a vector of all the static tracepoints set at ADDR. The
1513 caller is responsible for releasing the vector. */
1515 std::vector<breakpoint *>
1516 static_tracepoints_here (CORE_ADDR addr)
1518 std::vector<breakpoint *> found;
1520 for (breakpoint &b : all_breakpoints ())
1521 if (b.type == bp_static_tracepoint
1522 || b.type == bp_static_marker_tracepoint)
1524 for (bp_location &loc : b.locations ())
1525 if (loc.address == addr)
1526 found.push_back (&b);
1529 return found;
1532 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1533 validate that only allowed commands are included. */
1535 void
1536 breakpoint_set_commands (struct breakpoint *b,
1537 counted_command_line &&commands)
1539 validate_commands_for_breakpoint (b, commands.get ());
1541 b->commands = std::move (commands);
1542 notify_breakpoint_modified (b);
1545 /* Set the internal `silent' flag on the breakpoint. Note that this
1546 is not the same as the "silent" that may appear in the breakpoint's
1547 commands. */
1549 void
1550 breakpoint_set_silent (struct breakpoint *b, int silent)
1552 int old_silent = b->silent;
1554 b->silent = silent;
1555 if (old_silent != silent)
1556 notify_breakpoint_modified (b);
1559 /* See breakpoint.h. */
1561 void
1562 breakpoint_set_thread (struct breakpoint *b, int thread)
1564 /* THREAD should be -1, meaning no thread restriction, or it should be a
1565 valid global thread-id, which are greater than zero. */
1566 gdb_assert (thread == -1 || thread > 0);
1568 /* It is not valid to set a thread restriction for a breakpoint that
1569 already has task or inferior restriction. */
1570 gdb_assert (thread == -1 || (b->task == -1 && b->inferior == -1));
1572 int old_thread = b->thread;
1573 b->thread = thread;
1574 if (old_thread != thread)
1576 /* If THREAD is in a different program_space than OLD_THREAD, or the
1577 breakpoint has switched to or from being thread-specific, then we
1578 need to re-set the locations of this breakpoint. First, figure
1579 out the program_space for the old and new threads, use a value of
1580 nullptr to indicate the breakpoint is in all program spaces. */
1581 program_space *old_pspace = nullptr;
1582 if (old_thread != -1)
1584 struct thread_info *thr = find_thread_global_id (old_thread);
1585 gdb_assert (thr != nullptr);
1586 old_pspace = thr->inf->pspace;
1589 program_space *new_pspace = nullptr;
1590 if (thread != -1)
1592 struct thread_info *thr = find_thread_global_id (thread);
1593 gdb_assert (thr != nullptr);
1594 new_pspace = thr->inf->pspace;
1597 /* If the program space has changed for this breakpoint, then
1598 re-evaluate it's locations. */
1599 if (old_pspace != new_pspace)
1601 /* The breakpoint is now associated with a completely different
1602 program space. Discard all of the current locations and then
1603 re-set the breakpoint in the new program space, this will
1604 create the new locations. */
1605 b->clear_locations ();
1606 breakpoint_re_set_one (b, new_pspace);
1609 /* If the program space didn't change, or the breakpoint didn't
1610 acquire any new locations after the clear_locations call, then we
1611 need to notify of the breakpoint modification now. */
1612 if (old_pspace == new_pspace || !b->has_locations ())
1613 notify_breakpoint_modified (b);
1617 /* See breakpoint.h. */
1619 void
1620 breakpoint_set_inferior (struct breakpoint *b, int inferior)
1622 /* INFERIOR should be -1, meaning no inferior restriction, or it should
1623 be a valid inferior number, which are greater than zero. */
1624 gdb_assert (inferior == -1 || inferior > 0);
1626 /* It is not valid to set an inferior restriction for a breakpoint that
1627 already has a task or thread restriction. */
1628 gdb_assert (inferior == -1 || (b->task == -1 && b->thread == -1));
1630 int old_inferior = b->inferior;
1631 b->inferior = inferior;
1632 if (old_inferior != inferior)
1634 /* If INFERIOR is in a different program_space than OLD_INFERIOR, or
1635 the breakpoint has switch to or from inferior-specific, then we
1636 need to re-set the locations of this breakpoint. First, figure
1637 out the program_space for the old and new inferiors, use a value
1638 of nullptr to indicate the breakpoint is in all program
1639 spaces. */
1640 program_space *old_pspace = nullptr;
1641 if (old_inferior != -1)
1643 struct inferior *inf = find_inferior_id (old_inferior);
1644 gdb_assert (inf != nullptr);
1645 old_pspace = inf->pspace;
1648 program_space *new_pspace = nullptr;
1649 if (inferior != -1)
1651 struct inferior *inf = find_inferior_id (inferior);
1652 gdb_assert (inf != nullptr);
1653 new_pspace = inf->pspace;
1656 if (old_pspace != new_pspace)
1658 /* The breakpoint is now associated with a completely different
1659 program space. Discard all of the current locations and then
1660 re-set the breakpoint in the new program space, this will
1661 create the new locations. */
1662 b->clear_locations ();
1663 breakpoint_re_set_one (b, new_pspace);
1666 /* If the program space didn't change, or the breakpoint didn't
1667 acquire any new locations after the clear_locations call, then we
1668 need to notify of the breakpoint modification now. */
1669 if (old_pspace == new_pspace || !b->has_locations ())
1670 notify_breakpoint_modified (b);
1674 /* See breakpoint.h. */
1676 void
1677 breakpoint_set_task (struct breakpoint *b, int task)
1679 /* TASK should be -1, meaning no task restriction, or it should be a
1680 valid task-id, which are greater than zero. */
1681 gdb_assert (task == -1 || task > 0);
1683 /* It is not valid to set a task restriction for a breakpoint that
1684 already has a thread or inferior restriction. */
1685 gdb_assert (task == -1 || (b->thread == -1 && b->inferior == -1));
1687 int old_task = b->task;
1688 b->task = task;
1689 if (old_task != task)
1690 notify_breakpoint_modified (b);
1693 static void
1694 commands_command_1 (const char *arg, int from_tty,
1695 struct command_line *control)
1697 counted_command_line cmd;
1698 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1699 NULL after the call to read_command_lines if the user provides an empty
1700 list of command by just typing "end". */
1701 bool cmd_read = false;
1703 std::string new_arg;
1705 if (arg == NULL || !*arg)
1707 /* Argument not explicitly given. Synthesize it. */
1708 if (breakpoint_count - prev_breakpoint_count > 1)
1709 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1710 breakpoint_count);
1711 else if (breakpoint_count > 0)
1712 new_arg = string_printf ("%d", breakpoint_count);
1714 else
1716 /* Create a copy of ARG. This is needed because the "commands"
1717 command may be coming from a script. In that case, the read
1718 line buffer is going to be overwritten in the lambda of
1719 'map_breakpoint_numbers' below when reading the next line
1720 before we are are done parsing the breakpoint numbers. */
1721 new_arg = arg;
1723 arg = new_arg.c_str ();
1725 map_breakpoint_numbers
1726 (arg, [&] (breakpoint *b)
1728 if (!cmd_read)
1730 gdb_assert (cmd == NULL);
1731 if (control != NULL)
1732 cmd = control->body_list_0;
1733 else
1735 std::string str
1736 = string_printf (_("Type commands for breakpoint(s) "
1737 "%s, one per line."),
1738 arg);
1740 auto do_validate = [=] (const char *line)
1742 tracepoint *t
1743 = gdb::checked_static_cast<tracepoint *> (b);
1744 validate_actionline (line, t);
1746 gdb::function_view<void (const char *)> validator;
1747 if (is_tracepoint (b))
1748 validator = do_validate;
1750 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1752 cmd_read = true;
1755 /* If a breakpoint was on the list more than once, we don't need to
1756 do anything. */
1757 if (b->commands != cmd)
1759 validate_commands_for_breakpoint (b, cmd.get ());
1760 b->commands = cmd;
1761 notify_breakpoint_modified (b);
1766 static void
1767 commands_command (const char *arg, int from_tty)
1769 commands_command_1 (arg, from_tty, NULL);
1772 /* Like commands_command, but instead of reading the commands from
1773 input stream, takes them from an already parsed command structure.
1775 This is used by cli-script.c to DTRT with breakpoint commands
1776 that are part of if and while bodies. */
1777 enum command_control_type
1778 commands_from_control_command (const char *arg, struct command_line *cmd)
1780 commands_command_1 (arg, 0, cmd);
1781 return simple_control;
1784 /* Return true if BL->TARGET_INFO contains valid information. */
1786 static bool
1787 bp_location_has_shadow (struct bp_location *bl)
1789 if (bl->loc_type != bp_loc_software_breakpoint)
1790 return false;
1791 if (!bl->inserted)
1792 return false;
1793 if (bl->target_info.shadow_len == 0)
1794 /* BL isn't valid, or doesn't shadow memory. */
1795 return false;
1796 return true;
1799 /* Update BUF, which is LEN bytes read from the target address
1800 MEMADDR, by replacing a memory breakpoint with its shadowed
1801 contents.
1803 If READBUF is not NULL, this buffer must not overlap with the of
1804 the breakpoint location's shadow_contents buffer. Otherwise, a
1805 failed assertion internal error will be raised. */
1807 static void
1808 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1809 const gdb_byte *writebuf_org,
1810 ULONGEST memaddr, LONGEST len,
1811 struct bp_target_info *target_info,
1812 struct gdbarch *gdbarch)
1814 /* Now do full processing of the found relevant range of elements. */
1815 CORE_ADDR bp_addr = 0;
1816 int bp_size = 0;
1817 int bptoffset = 0;
1819 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1820 current_program_space->aspace.get (), 0))
1822 /* The breakpoint is inserted in a different address space. */
1823 return;
1826 /* Addresses and length of the part of the breakpoint that
1827 we need to copy. */
1828 bp_addr = target_info->placed_address;
1829 bp_size = target_info->shadow_len;
1831 if (bp_addr + bp_size <= memaddr)
1833 /* The breakpoint is entirely before the chunk of memory we are
1834 reading. */
1835 return;
1838 if (bp_addr >= memaddr + len)
1840 /* The breakpoint is entirely after the chunk of memory we are
1841 reading. */
1842 return;
1845 /* Offset within shadow_contents. */
1846 if (bp_addr < memaddr)
1848 /* Only copy the second part of the breakpoint. */
1849 bp_size -= memaddr - bp_addr;
1850 bptoffset = memaddr - bp_addr;
1851 bp_addr = memaddr;
1854 if (bp_addr + bp_size > memaddr + len)
1856 /* Only copy the first part of the breakpoint. */
1857 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1860 if (readbuf != NULL)
1862 /* Verify that the readbuf buffer does not overlap with the
1863 shadow_contents buffer. */
1864 gdb_assert (target_info->shadow_contents >= readbuf + len
1865 || readbuf >= (target_info->shadow_contents
1866 + target_info->shadow_len));
1868 /* Update the read buffer with this inserted breakpoint's
1869 shadow. */
1870 memcpy (readbuf + bp_addr - memaddr,
1871 target_info->shadow_contents + bptoffset, bp_size);
1873 else
1875 const unsigned char *bp;
1876 CORE_ADDR addr = target_info->reqstd_address;
1877 int placed_size;
1879 /* Update the shadow with what we want to write to memory. */
1880 memcpy (target_info->shadow_contents + bptoffset,
1881 writebuf_org + bp_addr - memaddr, bp_size);
1883 /* Determine appropriate breakpoint contents and size for this
1884 address. */
1885 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1887 /* Update the final write buffer with this inserted
1888 breakpoint's INSN. */
1889 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1893 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1894 by replacing any memory breakpoints with their shadowed contents.
1896 If READBUF is not NULL, this buffer must not overlap with any of
1897 the breakpoint location's shadow_contents buffers. Otherwise,
1898 a failed assertion internal error will be raised.
1900 The range of shadowed area by each bp_location is:
1901 bl->address - bp_locations_placed_address_before_address_max
1902 up to bl->address + bp_locations_shadow_len_after_address_max
1903 The range we were requested to resolve shadows for is:
1904 memaddr ... memaddr + len
1905 Thus the safe cutoff boundaries for performance optimization are
1906 memaddr + len <= (bl->address
1907 - bp_locations_placed_address_before_address_max)
1908 and:
1909 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1911 void
1912 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1913 const gdb_byte *writebuf_org,
1914 ULONGEST memaddr, LONGEST len)
1916 /* Left boundary, right boundary and median element of our binary
1917 search. */
1918 unsigned bc_l, bc_r, bc;
1920 /* Find BC_L which is a leftmost element which may affect BUF
1921 content. It is safe to report lower value but a failure to
1922 report higher one. */
1924 bc_l = 0;
1925 bc_r = bp_locations.size ();
1926 while (bc_l + 1 < bc_r)
1928 struct bp_location *bl;
1930 bc = (bc_l + bc_r) / 2;
1931 bl = bp_locations[bc];
1933 /* Check first BL->ADDRESS will not overflow due to the added
1934 constant. Then advance the left boundary only if we are sure
1935 the BC element can in no way affect the BUF content (MEMADDR
1936 to MEMADDR + LEN range).
1938 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1939 offset so that we cannot miss a breakpoint with its shadow
1940 range tail still reaching MEMADDR. */
1942 if ((bl->address + bp_locations_shadow_len_after_address_max
1943 >= bl->address)
1944 && (bl->address + bp_locations_shadow_len_after_address_max
1945 <= memaddr))
1946 bc_l = bc;
1947 else
1948 bc_r = bc;
1951 /* Due to the binary search above, we need to make sure we pick the
1952 first location that's at BC_L's address. E.g., if there are
1953 multiple locations at the same address, BC_L may end up pointing
1954 at a duplicate location, and miss the "master"/"inserted"
1955 location. Say, given locations L1, L2 and L3 at addresses A and
1958 L1@A, L2@A, L3@B, ...
1960 BC_L could end up pointing at location L2, while the "master"
1961 location could be L1. Since the `loc->inserted' flag is only set
1962 on "master" locations, we'd forget to restore the shadow of L1
1963 and L2. */
1964 while (bc_l > 0
1965 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1966 bc_l--;
1968 /* Now do full processing of the found relevant range of elements. */
1970 for (bc = bc_l; bc < bp_locations.size (); bc++)
1972 struct bp_location *bl = bp_locations[bc];
1974 /* bp_location array has BL->OWNER always non-NULL. */
1975 if (bl->owner->type == bp_none)
1976 warning (_("reading through apparently deleted breakpoint #%d?"),
1977 bl->owner->number);
1979 /* Performance optimization: any further element can no longer affect BUF
1980 content. */
1982 if (bl->address >= bp_locations_placed_address_before_address_max
1983 && (memaddr + len
1984 <= (bl->address
1985 - bp_locations_placed_address_before_address_max)))
1986 break;
1988 if (!bp_location_has_shadow (bl))
1989 continue;
1991 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1992 memaddr, len, &bl->target_info, bl->gdbarch);
1996 /* See breakpoint.h. */
1998 bool
1999 is_breakpoint (const struct breakpoint *bpt)
2001 return (bpt->type == bp_breakpoint
2002 || bpt->type == bp_hardware_breakpoint
2003 || bpt->type == bp_dprintf);
2006 /* Return true if BPT is of any hardware watchpoint kind. */
2008 static bool
2009 is_hardware_watchpoint (const struct breakpoint *bpt)
2011 return (bpt->type == bp_hardware_watchpoint
2012 || bpt->type == bp_read_watchpoint
2013 || bpt->type == bp_access_watchpoint);
2016 /* See breakpoint.h. */
2018 bool
2019 is_watchpoint (const struct breakpoint *bpt)
2021 return (is_hardware_watchpoint (bpt)
2022 || bpt->type == bp_watchpoint);
2025 /* Returns true if the current thread and its running state are safe
2026 to evaluate or update watchpoint B. Watchpoints on local
2027 expressions need to be evaluated in the context of the thread that
2028 was current when the watchpoint was created, and, that thread needs
2029 to be stopped to be able to select the correct frame context.
2030 Watchpoints on global expressions can be evaluated on any thread,
2031 and in any state. It is presently left to the target allowing
2032 memory accesses when threads are running. */
2034 static bool
2035 watchpoint_in_thread_scope (struct watchpoint *b)
2037 return (b->pspace == current_program_space
2038 && (b->watchpoint_thread == null_ptid
2039 || (inferior_ptid == b->watchpoint_thread
2040 && !inferior_thread ()->executing ())));
2043 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
2044 associated bp_watchpoint_scope breakpoint. */
2046 static void
2047 watchpoint_del_at_next_stop (struct watchpoint *w)
2049 if (w->related_breakpoint != w)
2051 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
2052 gdb_assert (w->related_breakpoint->related_breakpoint == w);
2053 w->related_breakpoint->disposition = disp_del_at_next_stop;
2054 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
2055 w->related_breakpoint = w;
2057 w->disposition = disp_del_at_next_stop;
2058 disable_breakpoint (w);
2061 /* Extract a bitfield value from value VAL using the bit parameters contained in
2062 watchpoint W. */
2064 static struct value *
2065 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
2067 struct value *bit_val;
2069 if (val == NULL)
2070 return NULL;
2072 bit_val = value::allocate (val->type ());
2074 val->unpack_bitfield (bit_val,
2075 w->val_bitpos,
2076 w->val_bitsize,
2077 val->contents_for_printing ().data (),
2078 val->offset ());
2080 return bit_val;
2083 /* Allocate a dummy location and add it to B. This is required
2084 because bpstat_stop_status requires a location to be able to report
2085 stops. */
2087 static void
2088 add_dummy_location (struct breakpoint *b,
2089 struct program_space *pspace)
2091 gdb_assert (!b->has_locations ());
2093 bp_location *loc = new bp_location (b, bp_loc_other);
2094 loc->pspace = pspace;
2095 b->add_location (*loc);
2098 /* Assuming that B is a watchpoint:
2099 - Reparse watchpoint expression, if REPARSE is true
2100 - Evaluate expression and store the result in B->val
2101 - Evaluate the condition if there is one, and store the result
2102 in b->loc->cond.
2103 - Update the list of values that must be watched in B->loc.
2105 If the watchpoint disposition is disp_del_at_next_stop, then do
2106 nothing. If this is local watchpoint that is out of scope, delete
2109 Even with `set breakpoint always-inserted on' the watchpoints are
2110 removed + inserted on each stop here. Normal breakpoints must
2111 never be removed because they might be missed by a running thread
2112 when debugging in non-stop mode. On the other hand, hardware
2113 watchpoints (is_hardware_watchpoint; processed here) are specific
2114 to each LWP since they are stored in each LWP's hardware debug
2115 registers. Therefore, such LWP must be stopped first in order to
2116 be able to modify its hardware watchpoints.
2118 Hardware watchpoints must be reset exactly once after being
2119 presented to the user. It cannot be done sooner, because it would
2120 reset the data used to present the watchpoint hit to the user. And
2121 it must not be done later because it could display the same single
2122 watchpoint hit during multiple GDB stops. Note that the latter is
2123 relevant only to the hardware watchpoint types bp_read_watchpoint
2124 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
2125 not user-visible - its hit is suppressed if the memory content has
2126 not changed.
2128 The following constraints influence the location where we can reset
2129 hardware watchpoints:
2131 * target_stopped_by_watchpoint and target_stopped_data_address are
2132 called several times when GDB stops.
2134 [linux]
2135 * Multiple hardware watchpoints can be hit at the same time,
2136 causing GDB to stop. GDB only presents one hardware watchpoint
2137 hit at a time as the reason for stopping, and all the other hits
2138 are presented later, one after the other, each time the user
2139 requests the execution to be resumed. Execution is not resumed
2140 for the threads still having pending hit event stored in
2141 LWP_INFO->STATUS. While the watchpoint is already removed from
2142 the inferior on the first stop the thread hit event is kept being
2143 reported from its cached value by linux_nat_stopped_data_address
2144 until the real thread resume happens after the watchpoint gets
2145 presented and thus its LWP_INFO->STATUS gets reset.
2147 Therefore the hardware watchpoint hit can get safely reset on the
2148 watchpoint removal from inferior. */
2150 static void
2151 update_watchpoint (struct watchpoint *b, bool reparse)
2153 bool within_current_scope;
2155 /* If this is a local watchpoint, we only want to check if the
2156 watchpoint frame is in scope if the current thread is the thread
2157 that was used to create the watchpoint. */
2158 if (!watchpoint_in_thread_scope (b))
2159 return;
2161 if (b->disposition == disp_del_at_next_stop)
2162 return;
2164 std::optional<scoped_restore_selected_frame> restore_frame;
2166 /* Determine if the watchpoint is within scope. */
2167 if (b->exp_valid_block == NULL)
2168 within_current_scope = true;
2169 else
2171 frame_info_ptr fi = get_current_frame ();
2172 struct gdbarch *frame_arch = get_frame_arch (fi);
2173 CORE_ADDR frame_pc = get_frame_pc (fi);
2175 /* If we're at a point where the stack has been destroyed
2176 (e.g. in a function epilogue), unwinding may not work
2177 properly. Do not attempt to recreate locations at this
2178 point. See similar comments in watchpoint_check. */
2179 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
2180 return;
2182 /* Save the current frame's ID so we can restore it after
2183 evaluating the watchpoint expression on its own frame. */
2184 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
2185 took a frame parameter, so that we didn't have to change the
2186 selected frame. */
2187 restore_frame.emplace ();
2189 fi = frame_find_by_id (b->watchpoint_frame);
2190 within_current_scope = (fi != NULL);
2191 if (within_current_scope)
2192 select_frame (fi);
2195 /* We don't free locations. They are stored in the bp_location array
2196 and update_global_location_list will eventually delete them and
2197 remove breakpoints if needed. */
2198 b->clear_locations ();
2200 if (within_current_scope && reparse)
2202 const char *s;
2204 b->exp.reset ();
2205 s = (b->exp_string_reparse
2206 ? b->exp_string_reparse.get ()
2207 : b->exp_string.get ());
2208 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
2209 /* If the meaning of expression itself changed, the old value is
2210 no longer relevant. We don't want to report a watchpoint hit
2211 to the user when the old value and the new value may actually
2212 be completely different objects. */
2213 b->val = NULL;
2214 b->val_valid = false;
2216 /* Note that unlike with breakpoints, the watchpoint's condition
2217 expression is stored in the breakpoint object, not in the
2218 locations (re)created below. */
2219 if (b->cond_string != NULL)
2221 b->cond_exp.reset ();
2223 s = b->cond_string.get ();
2224 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
2228 /* If we failed to parse the expression, for example because
2229 it refers to a global variable in a not-yet-loaded shared library,
2230 don't try to insert watchpoint. We don't automatically delete
2231 such watchpoint, though, since failure to parse expression
2232 is different from out-of-scope watchpoint. */
2233 if (!target_has_execution ())
2235 /* Without execution, memory can't change. No use to try and
2236 set watchpoint locations. The watchpoint will be reset when
2237 the target gains execution, through breakpoint_re_set. */
2238 if (!can_use_hw_watchpoints)
2240 if (b->works_in_software_mode ())
2241 b->type = bp_watchpoint;
2242 else
2243 error (_("Can't set read/access watchpoint when "
2244 "hardware watchpoints are disabled."));
2247 else if (within_current_scope && b->exp)
2249 std::vector<value_ref_ptr> val_chain;
2250 struct value *v, *result;
2251 struct program_space *wp_pspace;
2253 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
2254 &val_chain, false);
2256 /* Avoid setting b->val if it's already set. The meaning of
2257 b->val is 'the last value' user saw, and we should update
2258 it only if we reported that last value to user. As it
2259 happens, the code that reports it updates b->val directly.
2260 We don't keep track of the memory value for masked
2261 watchpoints. */
2262 if (!b->val_valid && !is_masked_watchpoint (b))
2264 if (b->val_bitsize != 0)
2265 v = extract_bitfield_from_watchpoint_value (b, v);
2266 b->val = release_value (v);
2267 b->val_valid = true;
2270 if (b->exp_valid_block == nullptr)
2271 wp_pspace = current_program_space;
2272 else
2273 wp_pspace = get_frame_program_space (get_selected_frame (NULL));
2275 /* Look at each value on the value chain. */
2276 gdb_assert (!val_chain.empty ());
2277 for (const value_ref_ptr &iter : val_chain)
2279 v = iter.get ();
2281 /* If it's a memory location, and GDB actually needed
2282 its contents to evaluate the expression, then we
2283 must watch it. If the first value returned is
2284 still lazy, that means an error occurred reading it;
2285 watch it anyway in case it becomes readable. */
2286 if (v->lval () == lval_memory
2287 && (v == val_chain[0] || ! v->lazy ()))
2289 struct type *vtype = check_typedef (v->type ());
2291 /* We only watch structs and arrays if user asked
2292 for it explicitly, never if they just happen to
2293 appear in the middle of some value chain. */
2294 if (v == result
2295 || (vtype->code () != TYPE_CODE_STRUCT
2296 && vtype->code () != TYPE_CODE_ARRAY))
2298 CORE_ADDR addr;
2299 enum target_hw_bp_type type;
2300 int bitpos = 0, bitsize = 0;
2302 if (v->bitsize () != 0)
2304 /* Extract the bit parameters out from the bitfield
2305 sub-expression. */
2306 bitpos = v->bitpos ();
2307 bitsize = v->bitsize ();
2309 else if (v == result && b->val_bitsize != 0)
2311 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2312 lvalue whose bit parameters are saved in the fields
2313 VAL_BITPOS and VAL_BITSIZE. */
2314 bitpos = b->val_bitpos;
2315 bitsize = b->val_bitsize;
2318 addr = v->address ();
2319 if (bitsize != 0)
2321 /* Skip the bytes that don't contain the bitfield. */
2322 addr += bitpos / 8;
2325 type = hw_write;
2326 if (b->type == bp_read_watchpoint)
2327 type = hw_read;
2328 else if (b->type == bp_access_watchpoint)
2329 type = hw_access;
2331 bp_location *loc = b->allocate_location ();
2332 loc->gdbarch = v->type ()->arch ();
2333 loc->pspace = wp_pspace;
2334 loc->address
2335 = gdbarch_remove_non_address_bits_watchpoint (loc->gdbarch,
2336 addr);
2337 b->add_location (*loc);
2339 if (bitsize != 0)
2341 /* Just cover the bytes that make up the bitfield. */
2342 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2344 else
2345 loc->length = v->type ()->length ();
2347 loc->watchpoint_type = type;
2352 /* Helper function to bundle possibly emitting a warning along with
2353 changing the type of B to bp_watchpoint. */
2354 auto change_type_to_bp_watchpoint = [] (breakpoint *bp)
2356 /* Only warn for breakpoints that have been assigned a +ve number,
2357 anything else is either an internal watchpoint (which we don't
2358 currently create) or has not yet been finalized, in which case
2359 this change of type will be occurring before the user is told
2360 the type of this watchpoint. */
2361 if (bp->type == bp_hardware_watchpoint && bp->number > 0)
2362 warning (_("watchpoint %d downgraded to software watchpoint"),
2363 bp->number);
2364 bp->type = bp_watchpoint;
2367 /* Change the type of breakpoint between hardware assisted or
2368 an ordinary watchpoint depending on the hardware support and
2369 free hardware slots. Recheck the number of free hardware slots
2370 as the value chain may have changed. */
2372 int reg_cnt;
2373 enum bp_loc_type loc_type;
2375 reg_cnt = can_use_hardware_watchpoint (val_chain);
2377 if (reg_cnt)
2379 int i, target_resources_ok, other_type_used;
2380 enum bptype type;
2382 /* Use an exact watchpoint when there's only one memory region to be
2383 watched, and only one debug register is needed to watch it. */
2384 b->exact = target_exact_watchpoints && reg_cnt == 1;
2386 /* We need to determine how many resources are already
2387 used for all other hardware watchpoints plus this one
2388 to see if we still have enough resources to also fit
2389 this watchpoint in as well. */
2391 /* If this is a software watchpoint, we try to turn it
2392 to a hardware one -- count resources as if B was of
2393 hardware watchpoint type. */
2394 type = b->type;
2395 if (type == bp_watchpoint)
2396 type = bp_hardware_watchpoint;
2398 /* This watchpoint may or may not have been placed on
2399 the list yet at this point (it won't be in the list
2400 if we're trying to create it for the first time,
2401 through watch_command), so always account for it
2402 manually. */
2404 /* Count resources used by all watchpoints except B. */
2405 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2407 /* Add in the resources needed for B. */
2408 i += hw_watchpoint_use_count (b);
2410 target_resources_ok
2411 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2412 if (target_resources_ok <= 0)
2414 bool sw_mode = b->works_in_software_mode ();
2416 if (target_resources_ok == 0 && !sw_mode)
2417 error (_("Target does not support this type of "
2418 "hardware watchpoint."));
2419 else if (target_resources_ok < 0 && !sw_mode)
2420 error (_("There are not enough available hardware "
2421 "resources for this watchpoint."));
2423 /* Downgrade to software watchpoint. */
2424 change_type_to_bp_watchpoint (b);
2426 else
2428 /* If this was a software watchpoint, we've just
2429 found we have enough resources to turn it to a
2430 hardware watchpoint. Otherwise, this is a
2431 nop. */
2432 b->type = type;
2435 else if (!b->works_in_software_mode ())
2437 if (!can_use_hw_watchpoints)
2438 error (_("Can't set read/access watchpoint when "
2439 "hardware watchpoints are disabled."));
2440 else
2441 error (_("Expression cannot be implemented with "
2442 "read/access watchpoint."));
2444 else
2445 change_type_to_bp_watchpoint (b);
2447 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
2448 : bp_loc_hardware_watchpoint);
2450 for (bp_location &bl : b->locations ())
2451 bl.loc_type = loc_type;
2454 /* If a software watchpoint is not watching any memory, then the
2455 above left it without any location set up. But,
2456 bpstat_stop_status requires a location to be able to report
2457 stops, so make sure there's at least a dummy one. */
2458 if (b->type == bp_watchpoint && !b->has_locations ())
2459 add_dummy_location (b, wp_pspace);
2461 else if (!within_current_scope)
2463 gdb_printf (_("\
2464 Watchpoint %d deleted because the program has left the block\n\
2465 in which its expression is valid.\n"),
2466 b->number);
2467 watchpoint_del_at_next_stop (b);
2471 /* Returns true iff breakpoint location should be
2472 inserted in the inferior. We don't differentiate the type of BL's owner
2473 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2474 breakpoint_ops is not defined, because in insert_bp_location,
2475 tracepoint's insert_location will not be called. */
2477 static bool
2478 should_be_inserted (struct bp_location *bl)
2480 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2481 return false;
2483 if (bl->owner->disposition == disp_del_at_next_stop)
2484 return false;
2486 if (!bl->enabled || bl->disabled_by_cond
2487 || bl->shlib_disabled || bl->duplicate)
2488 return false;
2490 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2491 return false;
2493 /* This is set for example, when we're attached to the parent of a
2494 vfork, and have detached from the child. The child is running
2495 free, and we expect it to do an exec or exit, at which point the
2496 OS makes the parent schedulable again (and the target reports
2497 that the vfork is done). Until the child is done with the shared
2498 memory region, do not insert breakpoints in the parent, otherwise
2499 the child could still trip on the parent's breakpoints. Since
2500 the parent is blocked anyway, it won't miss any breakpoint. */
2501 if (bl->pspace->breakpoints_not_allowed)
2502 return false;
2504 /* Don't insert a breakpoint if we're trying to step past its
2505 location, except if the breakpoint is a single-step breakpoint,
2506 and the breakpoint's thread is the thread which is stepping past
2507 a breakpoint. */
2508 if ((bl->loc_type == bp_loc_software_breakpoint
2509 || bl->loc_type == bp_loc_hardware_breakpoint)
2510 && stepping_past_instruction_at (bl->pspace->aspace.get (),
2511 bl->address)
2512 /* The single-step breakpoint may be inserted at the location
2513 we're trying to step if the instruction branches to itself.
2514 However, the instruction won't be executed at all and it may
2515 break the semantics of the instruction, for example, the
2516 instruction is a conditional branch or updates some flags.
2517 We can't fix it unless GDB is able to emulate the instruction
2518 or switch to displaced stepping. */
2519 && !(bl->owner->type == bp_single_step
2520 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2522 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2523 paddress (bl->gdbarch, bl->address));
2524 return false;
2527 /* Don't insert watchpoints if we're trying to step past the
2528 instruction that triggered one. */
2529 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2530 && stepping_past_nonsteppable_watchpoint ())
2532 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2533 "skipping watchpoint at %s:%d",
2534 paddress (bl->gdbarch, bl->address), bl->length);
2535 return false;
2538 return true;
2541 /* Same as should_be_inserted but does the check assuming
2542 that the location is not duplicated. */
2544 static bool
2545 unduplicated_should_be_inserted (struct bp_location *bl)
2547 scoped_restore restore_bl_duplicate
2548 = make_scoped_restore (&bl->duplicate, 0);
2550 return should_be_inserted (bl);
2553 /* Parses a conditional described by an expression COND into an
2554 agent expression bytecode suitable for evaluation
2555 by the bytecode interpreter. Return NULL if there was
2556 any error during parsing. */
2558 static agent_expr_up
2559 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2561 if (cond == NULL)
2562 return NULL;
2564 agent_expr_up aexpr;
2566 /* We don't want to stop processing, so catch any errors
2567 that may show up. */
2570 aexpr = gen_eval_for_expr (scope, cond);
2573 catch (const gdb_exception_error &ex)
2575 /* If we got here, it means the condition could not be parsed to a valid
2576 bytecode expression and thus can't be evaluated on the target's side.
2577 It's no use iterating through the conditions. */
2580 /* We have a valid agent expression. */
2581 return aexpr;
2584 /* Based on location BL, create a list of breakpoint conditions to be
2585 passed on to the target. If we have duplicated locations with different
2586 conditions, we will add such conditions to the list. The idea is that the
2587 target will evaluate the list of conditions and will only notify GDB when
2588 one of them is true. */
2590 static void
2591 build_target_condition_list (struct bp_location *bl)
2593 bool null_condition_or_parse_error = false;
2594 int modified = bl->needs_update;
2596 /* Release conditions left over from a previous insert. */
2597 bl->target_info.conditions.clear ();
2599 /* This is only meaningful if the target is
2600 evaluating conditions and if the user has
2601 opted for condition evaluation on the target's
2602 side. */
2603 if (gdb_evaluates_breakpoint_condition_p ()
2604 || !target_supports_evaluation_of_breakpoint_conditions ())
2605 return;
2607 auto loc_range = all_bp_locations_at_addr (bl->address);
2609 /* Do a first pass to check for locations with no assigned
2610 conditions or conditions that fail to parse to a valid agent
2611 expression bytecode. If any of these happen, then it's no use to
2612 send conditions to the target since this location will always
2613 trigger and generate a response back to GDB. Note we consider
2614 all locations at the same address irrespective of type, i.e.,
2615 even if the locations aren't considered duplicates (e.g.,
2616 software breakpoint and hardware breakpoint at the same
2617 address). */
2618 for (bp_location *loc : loc_range)
2620 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2622 if (modified)
2624 /* Re-parse the conditions since something changed. In that
2625 case we already freed the condition bytecodes (see
2626 force_breakpoint_reinsertion). We just
2627 need to parse the condition to bytecodes again. */
2628 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2629 loc->cond.get ());
2632 /* If we have a NULL bytecode expression, it means something
2633 went wrong or we have a null condition expression. */
2634 if (!loc->cond_bytecode)
2636 null_condition_or_parse_error = true;
2637 break;
2642 /* If any of these happened, it means we will have to evaluate the conditions
2643 for the location's address on gdb's side. It is no use keeping bytecodes
2644 for all the other duplicate locations, thus we free all of them here.
2646 This is so we have a finer control over which locations' conditions are
2647 being evaluated by GDB or the remote stub. */
2648 if (null_condition_or_parse_error)
2650 for (bp_location *loc : loc_range)
2652 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2654 /* Only go as far as the first NULL bytecode is
2655 located. */
2656 if (!loc->cond_bytecode)
2657 return;
2659 loc->cond_bytecode.reset ();
2664 /* No NULL conditions or failed bytecode generation. Build a
2665 condition list for this location's address. If we have software
2666 and hardware locations at the same address, they aren't
2667 considered duplicates, but we still merge all the conditions
2668 anyway, as it's simpler, and doesn't really make a practical
2669 difference. */
2670 for (bp_location *loc : loc_range)
2671 if (loc->cond
2672 && is_breakpoint (loc->owner)
2673 && loc->pspace->num == bl->pspace->num
2674 && loc->owner->enable_state == bp_enabled
2675 && loc->enabled
2676 && !loc->disabled_by_cond)
2678 /* Add the condition to the vector. This will be used later
2679 to send the conditions to the target. */
2680 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2683 return;
2686 /* Parses a command described by string CMD into an agent expression
2687 bytecode suitable for evaluation by the bytecode interpreter.
2688 Return NULL if there was any error during parsing. */
2690 static agent_expr_up
2691 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2693 const char *cmdrest;
2694 const char *format_start, *format_end;
2695 struct gdbarch *gdbarch = get_current_arch ();
2697 if (cmd == NULL)
2698 return NULL;
2700 cmdrest = cmd;
2702 if (*cmdrest == ',')
2703 ++cmdrest;
2704 cmdrest = skip_spaces (cmdrest);
2706 if (*cmdrest++ != '"')
2707 error (_("No format string following the location"));
2709 format_start = cmdrest;
2711 format_pieces fpieces (&cmdrest);
2713 format_end = cmdrest;
2715 if (*cmdrest++ != '"')
2716 error (_("Bad format string, non-terminated '\"'."));
2718 cmdrest = skip_spaces (cmdrest);
2720 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2721 error (_("Invalid argument syntax"));
2723 if (*cmdrest == ',')
2724 cmdrest++;
2725 cmdrest = skip_spaces (cmdrest);
2727 /* For each argument, make an expression. */
2729 std::vector<struct expression *> argvec;
2730 while (*cmdrest != '\0')
2732 const char *cmd1;
2734 cmd1 = cmdrest;
2735 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope),
2736 PARSER_COMMA_TERMINATES);
2737 argvec.push_back (expr.release ());
2738 cmdrest = cmd1;
2739 if (*cmdrest == ',')
2740 ++cmdrest;
2743 agent_expr_up aexpr;
2745 /* We don't want to stop processing, so catch any errors
2746 that may show up. */
2749 aexpr = gen_printf (scope, gdbarch, 0, 0,
2750 format_start, format_end - format_start,
2751 argvec.size (), argvec.data ());
2753 catch (const gdb_exception_error &ex)
2755 /* If we got here, it means the command could not be parsed to a valid
2756 bytecode expression and thus can't be evaluated on the target's side.
2757 It's no use iterating through the other commands. */
2760 /* We have a valid agent expression, return it. */
2761 return aexpr;
2764 /* Based on location BL, create a list of breakpoint commands to be
2765 passed on to the target. If we have duplicated locations with
2766 different commands, we will add any such to the list. */
2768 static void
2769 build_target_command_list (struct bp_location *bl)
2771 bool null_command_or_parse_error = false;
2772 int modified = bl->needs_update;
2774 /* Clear commands left over from a previous insert. */
2775 bl->target_info.tcommands.clear ();
2777 if (!target_can_run_breakpoint_commands ())
2778 return;
2780 /* For now, limit to agent-style dprintf breakpoints. */
2781 if (dprintf_style != dprintf_style_agent)
2782 return;
2784 auto loc_range = all_bp_locations_at_addr (bl->address);
2786 /* For now, if we have any location at the same address that isn't a
2787 dprintf, don't install the target-side commands, as that would
2788 make the breakpoint not be reported to the core, and we'd lose
2789 control. */
2790 for (bp_location *loc : loc_range)
2791 if (is_breakpoint (loc->owner)
2792 && loc->pspace->num == bl->pspace->num
2793 && loc->owner->type != bp_dprintf)
2794 return;
2796 /* Do a first pass to check for locations with no assigned
2797 conditions or conditions that fail to parse to a valid agent expression
2798 bytecode. If any of these happen, then it's no use to send conditions
2799 to the target since this location will always trigger and generate a
2800 response back to GDB. */
2801 for (bp_location *loc : loc_range)
2803 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2805 if (modified)
2807 /* Re-parse the commands since something changed. In that
2808 case we already freed the command bytecodes (see
2809 force_breakpoint_reinsertion). We just
2810 need to parse the command to bytecodes again. */
2811 loc->cmd_bytecode
2812 = parse_cmd_to_aexpr (bl->address,
2813 loc->owner->extra_string.get ());
2816 /* If we have a NULL bytecode expression, it means something
2817 went wrong or we have a null command expression. */
2818 if (!loc->cmd_bytecode)
2820 null_command_or_parse_error = true;
2821 break;
2826 /* If anything failed, then we're not doing target-side commands,
2827 and so clean up. */
2828 if (null_command_or_parse_error)
2830 for (bp_location *loc : loc_range)
2831 if (is_breakpoint (loc->owner)
2832 && loc->pspace->num == bl->pspace->num)
2834 /* Only go as far as the first NULL bytecode is
2835 located. */
2836 if (loc->cmd_bytecode == NULL)
2837 return;
2839 loc->cmd_bytecode.reset ();
2843 /* No NULL commands or failed bytecode generation. Build a command
2844 list for all duplicate locations at this location's address.
2845 Note that here we must care for whether the breakpoint location
2846 types are considered duplicates, otherwise, say, if we have a
2847 software and hardware location at the same address, the target
2848 could end up running the commands twice. For the moment, we only
2849 support targets-side commands with dprintf, but it doesn't hurt
2850 to be pedantically correct in case that changes. */
2851 for (bp_location *loc : loc_range)
2852 if (breakpoint_locations_match (bl, loc)
2853 && loc->owner->extra_string
2854 && is_breakpoint (loc->owner)
2855 && loc->pspace->num == bl->pspace->num
2856 && loc->owner->enable_state == bp_enabled
2857 && loc->enabled
2858 && !loc->disabled_by_cond)
2860 /* Add the command to the vector. This will be used later
2861 to send the commands to the target. */
2862 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2865 bl->target_info.persist = 0;
2866 /* Maybe flag this location as persistent. */
2867 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2868 bl->target_info.persist = 1;
2871 /* Return the kind of breakpoint on address *ADDR. Get the kind
2872 of breakpoint according to ADDR except single-step breakpoint.
2873 Get the kind of single-step breakpoint according to the current
2874 registers state. */
2876 static int
2877 breakpoint_kind (const struct bp_location *bl, CORE_ADDR *addr)
2879 if (bl->owner->type == bp_single_step)
2881 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2882 struct regcache *regcache;
2884 regcache = get_thread_regcache (thr);
2886 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2887 regcache, addr);
2889 else
2890 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2893 /* Rethrow the currently handled exception, if it's a TARGET_CLOSE_ERROR.
2894 E is either the currently handled exception, or a copy, or a sliced copy,
2895 so we can't rethrow that one, but we can use it to inspect the properties
2896 of the currently handled exception. */
2898 static void
2899 rethrow_on_target_close_error (const gdb_exception &e)
2901 if (e.reason == 0)
2902 return;
2903 /* Can't set the breakpoint. */
2905 if (e.error != TARGET_CLOSE_ERROR)
2906 return;
2908 /* If the target has closed then it will have deleted any breakpoints
2909 inserted within the target inferior, as a result any further attempts
2910 to interact with the breakpoint objects is not possible. Just rethrow
2911 the error. Don't use e to rethrow, to prevent object slicing of the
2912 exception. */
2913 throw;
2916 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2917 location. Any error messages are printed to TMP_ERROR_STREAM; and
2918 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2919 Returns 0 for success, 1 if the bp_location type is not supported or
2920 -1 for failure.
2922 NOTE drow/2003-09-09: This routine could be broken down to an
2923 object-style method for each breakpoint or catchpoint type. */
2924 static int
2925 insert_bp_location (struct bp_location *bl,
2926 struct ui_file *tmp_error_stream,
2927 int *disabled_breaks,
2928 int *hw_breakpoint_error,
2929 int *hw_bp_error_explained_already)
2931 gdb_exception bp_excpt;
2933 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2934 return 0;
2936 breakpoint_debug_printf ("%s", breakpoint_location_address_str (bl).c_str ());
2938 /* Note we don't initialize bl->target_info, as that wipes out
2939 the breakpoint location's shadow_contents if the breakpoint
2940 is still inserted at that location. This in turn breaks
2941 target_read_memory which depends on these buffers when
2942 a memory read is requested at the breakpoint location:
2943 Once the target_info has been wiped, we fail to see that
2944 we have a breakpoint inserted at that address and thus
2945 read the breakpoint instead of returning the data saved in
2946 the breakpoint location's shadow contents. */
2947 bl->target_info.reqstd_address = bl->address;
2948 bl->target_info.placed_address_space = bl->pspace->aspace.get ();
2949 bl->target_info.length = bl->length;
2951 /* When working with target-side conditions, we must pass all the conditions
2952 for the same breakpoint address down to the target since GDB will not
2953 insert those locations. With a list of breakpoint conditions, the target
2954 can decide when to stop and notify GDB. */
2956 if (is_breakpoint (bl->owner))
2958 build_target_condition_list (bl);
2959 build_target_command_list (bl);
2960 /* Reset the modification marker. */
2961 bl->needs_update = 0;
2964 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2965 set at a read-only address, then a breakpoint location will have
2966 been changed to hardware breakpoint before we get here. If it is
2967 "off" however, error out before actually trying to insert the
2968 breakpoint, with a nicer error message. */
2969 if (bl->loc_type == bp_loc_software_breakpoint
2970 && !automatic_hardware_breakpoints)
2972 mem_region *mr = lookup_mem_region (bl->address);
2974 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2976 gdb_printf (tmp_error_stream,
2977 _("Cannot insert breakpoint %d.\n"
2978 "Cannot set software breakpoint "
2979 "at read-only address %s\n"),
2980 bl->owner->number,
2981 paddress (bl->gdbarch, bl->address));
2982 return 1;
2986 if (bl->loc_type == bp_loc_software_breakpoint
2987 || bl->loc_type == bp_loc_hardware_breakpoint)
2989 /* First check to see if we have to handle an overlay. */
2990 if (overlay_debugging == ovly_off
2991 || bl->section == NULL
2992 || !(section_is_overlay (bl->section)))
2994 /* No overlay handling: just set the breakpoint. */
2997 int val;
2999 val = bl->owner->insert_location (bl);
3000 if (val)
3001 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
3003 catch (gdb_exception &e)
3005 rethrow_on_target_close_error (e);
3006 bp_excpt = std::move (e);
3009 else
3011 /* This breakpoint is in an overlay section.
3012 Shall we set a breakpoint at the LMA? */
3013 if (!overlay_events_enabled)
3015 /* Yes -- overlay event support is not active,
3016 so we must try to set a breakpoint at the LMA.
3017 This will not work for a hardware breakpoint. */
3018 if (bl->loc_type == bp_loc_hardware_breakpoint)
3019 warning (_("hardware breakpoint %d not supported in overlay!"),
3020 bl->owner->number);
3021 else
3023 CORE_ADDR addr = overlay_unmapped_address (bl->address,
3024 bl->section);
3025 /* Set a software (trap) breakpoint at the LMA. */
3026 bl->overlay_target_info = bl->target_info;
3027 bl->overlay_target_info.reqstd_address = addr;
3029 /* No overlay handling: just set the breakpoint. */
3032 int val;
3034 bl->overlay_target_info.kind
3035 = breakpoint_kind (bl, &addr);
3036 bl->overlay_target_info.placed_address = addr;
3037 val = target_insert_breakpoint (bl->gdbarch,
3038 &bl->overlay_target_info);
3039 if (val)
3040 bp_excpt
3041 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
3043 catch (gdb_exception &e)
3045 rethrow_on_target_close_error (e);
3046 bp_excpt = std::move (e);
3049 if (bp_excpt.reason != 0)
3050 gdb_printf (tmp_error_stream,
3051 "Overlay breakpoint %d "
3052 "failed: in ROM?\n",
3053 bl->owner->number);
3056 /* Shall we set a breakpoint at the VMA? */
3057 if (section_is_mapped (bl->section))
3059 /* Yes. This overlay section is mapped into memory. */
3062 int val;
3064 val = bl->owner->insert_location (bl);
3065 if (val)
3066 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
3068 catch (gdb_exception_error &e)
3070 rethrow_on_target_close_error (e);
3071 bp_excpt = std::move (e);
3074 else
3076 /* No. This breakpoint will not be inserted.
3077 No error, but do not mark the bp as 'inserted'. */
3078 return 0;
3082 if (bp_excpt.reason != 0)
3084 /* Can't set the breakpoint. */
3085 gdb_assert (bl->owner != nullptr);
3087 /* In some cases, we might not be able to insert a
3088 breakpoint in a shared library that has already been
3089 removed, but we have not yet processed the shlib unload
3090 event. Unfortunately, some targets that implement
3091 breakpoint insertion themselves can't tell why the
3092 breakpoint insertion failed (e.g., the remote target
3093 doesn't define error codes), so we must treat generic
3094 errors as memory errors. */
3095 if (bp_excpt.reason == RETURN_ERROR
3096 && (bp_excpt.error == GENERIC_ERROR
3097 || bp_excpt.error == MEMORY_ERROR)
3098 && bl->loc_type == bp_loc_software_breakpoint
3099 && (solib_name_from_address (bl->pspace, bl->address)
3100 || shared_objfile_contains_address_p (bl->pspace,
3101 bl->address)))
3103 /* See also: disable_breakpoints_in_shlibs. */
3104 bl->shlib_disabled = 1;
3105 notify_breakpoint_modified (bl->owner);
3106 if (!*disabled_breaks)
3108 gdb_printf (tmp_error_stream,
3109 "Cannot insert breakpoint %d.\n",
3110 bl->owner->number);
3111 gdb_printf (tmp_error_stream,
3112 "Temporarily disabling shared "
3113 "library breakpoints:\n");
3115 *disabled_breaks = 1;
3116 gdb_printf (tmp_error_stream,
3117 "breakpoint #%d\n", bl->owner->number);
3118 return 0;
3120 else
3122 if (bl->loc_type == bp_loc_hardware_breakpoint)
3124 *hw_breakpoint_error = 1;
3125 *hw_bp_error_explained_already = bp_excpt.message != NULL;
3126 gdb_printf (tmp_error_stream,
3127 "Cannot insert hardware breakpoint %d%s",
3128 bl->owner->number,
3129 bp_excpt.message ? ":" : ".\n");
3130 if (bp_excpt.message != NULL)
3131 gdb_printf (tmp_error_stream, "%s.\n",
3132 bp_excpt.what ());
3134 else
3136 if (bp_excpt.message == NULL)
3138 std::string message
3139 = memory_error_message (TARGET_XFER_E_IO,
3140 bl->gdbarch, bl->address);
3142 gdb_printf (tmp_error_stream,
3143 "Cannot insert breakpoint %d.\n"
3144 "%s\n",
3145 bl->owner->number, message.c_str ());
3147 else
3149 gdb_printf (tmp_error_stream,
3150 "Cannot insert breakpoint %d: %s\n",
3151 bl->owner->number,
3152 bp_excpt.what ());
3155 return 1;
3159 else
3160 bl->inserted = 1;
3162 return 0;
3165 else if (bl->loc_type == bp_loc_hardware_watchpoint
3166 && bl->owner->disposition != disp_del_at_next_stop)
3168 int val;
3170 val = bl->owner->insert_location (bl);
3172 /* If trying to set a read-watchpoint, and it turns out it's not
3173 supported, try emulating one with an access watchpoint. */
3174 if (val == 1 && bl->watchpoint_type == hw_read)
3176 /* But don't try to insert it, if there's already another
3177 hw_access location that would be considered a duplicate
3178 of this one. */
3179 for (bp_location *loc : all_bp_locations ())
3180 if (loc != bl
3181 && loc->watchpoint_type == hw_access
3182 && watchpoint_locations_match (bl, loc))
3184 bl->duplicate = 1;
3185 bl->inserted = 1;
3186 bl->target_info = loc->target_info;
3187 bl->watchpoint_type = hw_access;
3188 val = 0;
3189 break;
3192 if (val == 1)
3194 bl->watchpoint_type = hw_access;
3195 val = bl->owner->insert_location (bl);
3197 if (val)
3198 /* Back to the original value. */
3199 bl->watchpoint_type = hw_read;
3203 bl->inserted = (val == 0);
3206 else if (bl->owner->type == bp_catchpoint)
3208 int val;
3210 val = bl->owner->insert_location (bl);
3211 if (val)
3213 bl->owner->enable_state = bp_disabled;
3215 if (val == 1)
3216 warning (_("\
3217 Error inserting catchpoint %d: Your system does not support this type\n\
3218 of catchpoint."), bl->owner->number);
3219 else
3220 warning (_("Error inserting catchpoint %d."), bl->owner->number);
3223 bl->inserted = (val == 0);
3225 /* We've already printed an error message if there was a problem
3226 inserting this catchpoint, and we've disabled the catchpoint,
3227 so just return success. */
3228 return 0;
3231 return 0;
3234 /* This function is called when program space PSPACE is about to be
3235 deleted. It takes care of updating breakpoints to not reference
3236 PSPACE anymore. */
3238 void
3239 breakpoint_program_space_exit (struct program_space *pspace)
3241 /* Remove any breakpoint that was set through this program space. */
3242 for (breakpoint &b : all_breakpoints_safe ())
3243 if (b.pspace == pspace)
3244 delete_breakpoint (&b);
3246 /* Breakpoints set through other program spaces could have locations
3247 bound to PSPACE as well. Remove those. */
3248 for (bp_location *loc : all_bp_locations ())
3249 if (loc->pspace == pspace)
3251 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3252 loc->owner->unadd_location (*loc);
3255 /* Now update the global location list to permanently delete the
3256 removed locations above. */
3257 update_global_location_list (UGLL_DONT_INSERT);
3260 /* Make sure all breakpoints are inserted in inferior.
3261 Throws exception on any error.
3262 A breakpoint that is already inserted won't be inserted
3263 again, so calling this function twice is safe. */
3264 void
3265 insert_breakpoints (void)
3267 for (breakpoint &bpt : all_breakpoints ())
3268 if (is_hardware_watchpoint (&bpt))
3270 watchpoint &w = gdb::checked_static_cast<watchpoint &> (bpt);
3272 update_watchpoint (&w, false /* don't reparse. */);
3275 /* Updating watchpoints creates new locations, so update the global
3276 location list. Explicitly tell ugll to insert locations and
3277 ignore breakpoints_always_inserted_mode. Also,
3278 update_global_location_list tries to "upgrade" software
3279 breakpoints to hardware breakpoints to handle "set breakpoint
3280 auto-hw", so we need to call it even if we don't have new
3281 locations. */
3282 update_global_location_list (UGLL_INSERT);
3285 /* This is used when we need to synch breakpoint conditions between GDB and the
3286 target. It is the case with deleting and disabling of breakpoints when using
3287 always-inserted mode. */
3289 static void
3290 update_inserted_breakpoint_locations (void)
3292 int error_flag = 0;
3293 int val = 0;
3294 int disabled_breaks = 0;
3295 int hw_breakpoint_error = 0;
3296 int hw_bp_details_reported = 0;
3298 string_file tmp_error_stream;
3300 /* Explicitly mark the warning -- this will only be printed if
3301 there was an error. */
3302 tmp_error_stream.puts ("Warning:\n");
3304 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3306 for (bp_location *bl : all_bp_locations ())
3308 /* We only want to update software breakpoints and hardware
3309 breakpoints. */
3310 if (!is_breakpoint (bl->owner))
3311 continue;
3313 /* We only want to update locations that are already inserted
3314 and need updating. This is to avoid unwanted insertion during
3315 deletion of breakpoints. */
3316 if (!bl->inserted || !bl->needs_update)
3317 continue;
3319 switch_to_program_space_and_thread (bl->pspace);
3321 /* For targets that support global breakpoints, there's no need
3322 to select an inferior to insert breakpoint to. In fact, even
3323 if we aren't attached to any process yet, we should still
3324 insert breakpoints. */
3325 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ())
3326 && (inferior_ptid == null_ptid || !target_has_execution ()))
3327 continue;
3329 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3330 &hw_breakpoint_error, &hw_bp_details_reported);
3331 if (val)
3332 error_flag = val;
3335 if (error_flag)
3337 target_terminal::ours_for_output ();
3338 error (("%s"), tmp_error_stream.c_str ());
3342 /* Used when starting or continuing the program. */
3344 static void
3345 insert_breakpoint_locations (void)
3347 int error_flag = 0;
3348 int val = 0;
3349 int disabled_breaks = 0;
3350 int hw_breakpoint_error = 0;
3351 int hw_bp_error_explained_already = 0;
3353 string_file tmp_error_stream;
3355 /* Explicitly mark the warning -- this will only be printed if
3356 there was an error. */
3357 tmp_error_stream.puts ("Warning:\n");
3359 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3361 for (bp_location *bl : all_bp_locations ())
3363 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3364 continue;
3366 /* There is no point inserting thread-specific breakpoints if
3367 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3368 has BL->OWNER always non-NULL. */
3369 if (bl->owner->thread != -1
3370 && !valid_global_thread_id (bl->owner->thread))
3371 continue;
3373 /* Or inferior specific breakpoints if the inferior no longer
3374 exists. */
3375 if (bl->owner->inferior != -1
3376 && !valid_global_inferior_id (bl->owner->inferior))
3377 continue;
3379 switch_to_program_space_and_thread (bl->pspace);
3381 /* For targets that support global breakpoints, there's no need
3382 to select an inferior to insert breakpoint to. In fact, even
3383 if we aren't attached to any process yet, we should still
3384 insert breakpoints. */
3385 if (!gdbarch_has_global_breakpoints (current_inferior ()->arch ())
3386 && (inferior_ptid == null_ptid || !target_has_execution ()))
3387 continue;
3389 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3390 &hw_breakpoint_error, &hw_bp_error_explained_already);
3391 if (val)
3392 error_flag = val;
3395 /* If we failed to insert all locations of a watchpoint, remove
3396 them, as half-inserted watchpoint is of limited use. */
3397 for (breakpoint &bpt : all_breakpoints ())
3399 bool some_failed = false;
3401 if (!is_hardware_watchpoint (&bpt))
3402 continue;
3404 if (!breakpoint_enabled (&bpt))
3405 continue;
3407 if (bpt.disposition == disp_del_at_next_stop)
3408 continue;
3410 for (bp_location &loc : bpt.locations ())
3411 if (!loc.inserted && should_be_inserted (&loc))
3413 some_failed = true;
3414 break;
3417 if (some_failed)
3419 for (bp_location &loc : bpt.locations ())
3420 if (loc.inserted)
3421 remove_breakpoint (&loc);
3423 hw_breakpoint_error = 1;
3424 tmp_error_stream.printf ("Could not insert "
3425 "hardware watchpoint %d.\n",
3426 bpt.number);
3427 error_flag = -1;
3431 if (error_flag)
3433 /* If a hardware breakpoint or watchpoint was inserted, add a
3434 message about possibly exhausted resources. */
3435 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3437 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3438 You may have requested too many hardware breakpoints/watchpoints.\n");
3440 target_terminal::ours_for_output ();
3441 error (("%s"), tmp_error_stream.c_str ());
3445 /* Used when the program stops.
3446 Returns zero if successful, or non-zero if there was a problem
3447 removing a breakpoint location. */
3450 remove_breakpoints (void)
3452 int val = 0;
3454 for (bp_location *bl : all_bp_locations ())
3455 if (bl->inserted && !is_tracepoint (bl->owner))
3456 val |= remove_breakpoint (bl);
3458 return val;
3461 /* When a thread exits, remove breakpoints that are related to
3462 that thread. */
3464 static void
3465 remove_threaded_breakpoints (thread_info *tp,
3466 std::optional<ULONGEST> /* exit_code */,
3467 int /* silent */)
3469 for (breakpoint &b : all_breakpoints_safe ())
3471 if (b.thread == tp->global_num && user_breakpoint_p (&b))
3473 gdb_printf (_("\
3474 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3475 b.number, print_thread_id (tp));
3476 delete_breakpoint (&b);
3481 /* Called when inferior INF has been removed from GDB. Remove associated
3482 per-inferior breakpoints. */
3484 static void
3485 remove_inferior_breakpoints (struct inferior *inf)
3487 for (breakpoint &b : all_breakpoints_safe ())
3489 if (b.inferior == inf->num && user_breakpoint_p (&b))
3491 /* Tell the user the breakpoint has been deleted. But only for
3492 breakpoints that would not normally have been deleted at the
3493 next stop anyway. */
3494 if (b.disposition != disp_del
3495 && b.disposition != disp_del_at_next_stop)
3496 gdb_printf (_("\
3497 Inferior-specific breakpoint %d deleted - inferior %d has been removed.\n"),
3498 b.number, inf->num);
3499 delete_breakpoint (&b);
3504 /* See breakpoint.h. */
3506 void
3507 remove_breakpoints_inf (inferior *inf)
3509 int val;
3511 breakpoint_debug_printf ("inf->num = %d", inf->num);
3513 for (bp_location *bl : all_bp_locations ())
3515 if (bl->pspace != inf->pspace)
3516 continue;
3518 if (bl->inserted && !bl->target_info.persist)
3520 val = remove_breakpoint (bl);
3521 if (val != 0)
3522 return;
3527 static int internal_breakpoint_number = -1;
3529 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3530 If INTERNAL is non-zero, the breakpoint number will be populated
3531 from internal_breakpoint_number and that variable decremented.
3532 Otherwise the breakpoint number will be populated from
3533 breakpoint_count and that value incremented. Internal breakpoints
3534 do not set the internal var bpnum. */
3535 static void
3536 set_breakpoint_number (int internal, struct breakpoint *b)
3538 if (internal)
3539 b->number = internal_breakpoint_number--;
3540 else
3542 set_breakpoint_count (breakpoint_count + 1);
3543 b->number = breakpoint_count;
3547 /* Create a TYPE breakpoint on ADDRESS from an object file with GDBARCH. */
3549 static struct breakpoint *
3550 create_internal_breakpoint (struct gdbarch *gdbarch,
3551 CORE_ADDR address, enum bptype type)
3553 std::unique_ptr<internal_breakpoint> b
3554 (new internal_breakpoint (gdbarch, type, address));
3556 b->number = internal_breakpoint_number--;
3558 return add_to_breakpoint_chain (std::move (b));
3561 /* Create a TYPE breakpoint on minimal symbol MSYM from an object file with
3562 GDBARCH. */
3564 static struct breakpoint *
3565 create_internal_breakpoint (struct gdbarch *gdbarch,
3566 bound_minimal_symbol &msym, enum bptype type)
3568 CORE_ADDR address;
3570 address = msym.value_address ();
3572 address = gdbarch_convert_from_func_ptr_addr
3573 (gdbarch, address, current_inferior ()->top_target ());
3575 /* Note that we're not using gdbarch_addr_bits_remove here, because that's
3576 related to addresses in $pc. We're getting the address from the
3577 minimal symbol table. */
3579 /* Is gdbarch_deprecated_function_start_offset needed here? Or is that dealt
3580 with elsewhere? Needs testing on vax. */
3582 if (gdbarch_skip_entrypoint_p (gdbarch))
3583 address = gdbarch_skip_entrypoint (gdbarch, address);
3585 return create_internal_breakpoint (gdbarch, address, type);
3588 static const char *const longjmp_names[] =
3590 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3592 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3594 /* Per-objfile data private to breakpoint.c. */
3595 struct breakpoint_objfile_data
3597 /* Minimal symbol for "_ovly_debug_event" (if any). */
3598 bound_minimal_symbol overlay_msym;
3600 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3601 bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3603 /* True if we have looked for longjmp probes. */
3604 int longjmp_searched = 0;
3606 /* SystemTap probe points for longjmp (if any). These are non-owning
3607 references. */
3608 std::vector<probe *> longjmp_probes;
3610 /* Minimal symbol for "std::terminate()" (if any). */
3611 bound_minimal_symbol terminate_msym;
3613 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3614 bound_minimal_symbol exception_msym;
3616 /* True if we have looked for exception probes. */
3617 int exception_searched = 0;
3619 /* SystemTap probe points for unwinding (if any). These are non-owning
3620 references. */
3621 std::vector<probe *> exception_probes;
3624 static const registry<objfile>::key<breakpoint_objfile_data>
3625 breakpoint_objfile_key;
3627 /* Minimal symbol not found sentinel. */
3628 static struct minimal_symbol msym_not_found;
3630 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3632 static bool
3633 msym_not_found_p (const struct minimal_symbol *msym)
3635 return msym == &msym_not_found;
3638 /* Return per-objfile data needed by breakpoint.c.
3639 Allocate the data if necessary. */
3641 static struct breakpoint_objfile_data *
3642 get_breakpoint_objfile_data (struct objfile *objfile)
3644 struct breakpoint_objfile_data *bp_objfile_data;
3646 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3647 if (bp_objfile_data == NULL)
3648 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3649 return bp_objfile_data;
3652 static void
3653 create_overlay_event_breakpoint (void)
3655 const char *const func_name = "_ovly_debug_event";
3657 for (objfile *objfile : current_program_space->objfiles ())
3659 struct breakpoint *b;
3660 struct breakpoint_objfile_data *bp_objfile_data;
3661 CORE_ADDR addr;
3663 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3665 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3666 continue;
3668 if (bp_objfile_data->overlay_msym.minsym == NULL)
3670 bound_minimal_symbol m
3671 = lookup_minimal_symbol_text (current_program_space, func_name,
3672 objfile);
3673 if (m.minsym == NULL)
3675 /* Avoid future lookups in this objfile. */
3676 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3677 continue;
3679 bp_objfile_data->overlay_msym = m;
3682 addr = bp_objfile_data->overlay_msym.value_address ();
3683 b = create_internal_breakpoint (objfile->arch (), addr,
3684 bp_overlay_event);
3685 b->locspec = new_explicit_location_spec_function (func_name);
3687 if (overlay_debugging == ovly_auto)
3689 b->enable_state = bp_enabled;
3690 overlay_events_enabled = 1;
3692 else
3694 b->enable_state = bp_disabled;
3695 overlay_events_enabled = 0;
3700 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3701 true if a breakpoint was installed. */
3703 static bool
3704 create_longjmp_master_breakpoint_probe (objfile *objfile)
3706 struct gdbarch *gdbarch = objfile->arch ();
3707 struct breakpoint_objfile_data *bp_objfile_data
3708 = get_breakpoint_objfile_data (objfile);
3710 if (!bp_objfile_data->longjmp_searched)
3712 std::vector<probe *> ret
3713 = find_probes_in_objfile (objfile, "libc", "longjmp");
3715 if (!ret.empty ())
3717 /* We are only interested in checking one element. */
3718 probe *p = ret[0];
3720 if (!p->can_evaluate_arguments ())
3722 /* We cannot use the probe interface here,
3723 because it does not know how to evaluate
3724 arguments. */
3725 ret.clear ();
3728 bp_objfile_data->longjmp_probes = ret;
3729 bp_objfile_data->longjmp_searched = 1;
3732 if (bp_objfile_data->longjmp_probes.empty ())
3733 return false;
3735 for (probe *p : bp_objfile_data->longjmp_probes)
3737 struct breakpoint *b;
3739 b = create_internal_breakpoint (gdbarch,
3740 p->get_relocated_address (objfile),
3741 bp_longjmp_master);
3742 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
3743 b->enable_state = bp_disabled;
3746 return true;
3749 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3750 Return true if at least one breakpoint was installed. */
3752 static bool
3753 create_longjmp_master_breakpoint_names (objfile *objfile)
3755 struct gdbarch *gdbarch = objfile->arch ();
3756 if (!gdbarch_get_longjmp_target_p (gdbarch))
3757 return false;
3759 struct breakpoint_objfile_data *bp_objfile_data
3760 = get_breakpoint_objfile_data (objfile);
3761 unsigned int installed_bp = 0;
3763 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3765 struct breakpoint *b;
3766 const char *func_name;
3767 CORE_ADDR addr;
3769 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3770 continue;
3772 func_name = longjmp_names[i];
3773 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3775 bound_minimal_symbol m
3776 = lookup_minimal_symbol_text (objfile->pspace (), func_name,
3777 objfile);
3778 if (m.minsym == NULL)
3780 /* Prevent future lookups in this objfile. */
3781 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3782 continue;
3784 bp_objfile_data->longjmp_msym[i] = m;
3787 addr = bp_objfile_data->longjmp_msym[i].value_address ();
3788 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
3789 b->locspec = new_explicit_location_spec_function (func_name);
3790 b->enable_state = bp_disabled;
3791 installed_bp++;
3794 return installed_bp > 0;
3797 /* Create a master longjmp breakpoint. */
3799 static void
3800 create_longjmp_master_breakpoint (void)
3802 scoped_restore_current_program_space restore_pspace;
3804 for (struct program_space *pspace : program_spaces)
3806 set_current_program_space (pspace);
3808 for (objfile *obj : pspace->objfiles ())
3810 /* Skip separate debug object, it's handled in the loop below. */
3811 if (obj->separate_debug_objfile_backlink != nullptr)
3812 continue;
3814 /* Try a probe kind breakpoint on main objfile. */
3815 if (create_longjmp_master_breakpoint_probe (obj))
3816 continue;
3818 /* Try longjmp_names kind breakpoints on main and separate_debug
3819 objfiles. */
3820 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3821 if (create_longjmp_master_breakpoint_names (debug_objfile))
3822 break;
3827 /* Create a master std::terminate breakpoint. */
3828 static void
3829 create_std_terminate_master_breakpoint (void)
3831 const char *const func_name = "std::terminate()";
3833 scoped_restore_current_program_space restore_pspace;
3834 scoped_restore_current_language save_language (language_cplus);
3836 for (struct program_space *pspace : program_spaces)
3838 set_current_program_space (pspace);
3840 for (objfile *objfile : pspace->objfiles ())
3842 struct breakpoint *b;
3843 struct breakpoint_objfile_data *bp_objfile_data;
3845 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3847 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3848 continue;
3850 if (bp_objfile_data->terminate_msym.minsym == NULL)
3852 bound_minimal_symbol m
3853 = lookup_minimal_symbol (current_program_space, func_name,
3854 objfile);
3855 if (m.minsym == NULL || (m.minsym->type () != mst_text
3856 && m.minsym->type () != mst_file_text))
3858 /* Prevent future lookups in this objfile. */
3859 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3860 continue;
3862 bp_objfile_data->terminate_msym = m;
3865 b = create_internal_breakpoint (objfile->arch (),
3866 bp_objfile_data->terminate_msym,
3867 bp_std_terminate_master);
3868 b->locspec = new_explicit_location_spec_function (func_name);
3869 b->enable_state = bp_disabled;
3874 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3875 probe. Return true if a breakpoint was installed. */
3877 static bool
3878 create_exception_master_breakpoint_probe (objfile *objfile)
3880 struct breakpoint *b;
3881 struct gdbarch *gdbarch;
3882 struct breakpoint_objfile_data *bp_objfile_data;
3884 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3886 /* We prefer the SystemTap probe point if it exists. */
3887 if (!bp_objfile_data->exception_searched)
3889 std::vector<probe *> ret
3890 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3892 if (!ret.empty ())
3894 /* We are only interested in checking one element. */
3895 probe *p = ret[0];
3897 if (!p->can_evaluate_arguments ())
3899 /* We cannot use the probe interface here, because it does
3900 not know how to evaluate arguments. */
3901 ret.clear ();
3904 bp_objfile_data->exception_probes = ret;
3905 bp_objfile_data->exception_searched = 1;
3908 if (bp_objfile_data->exception_probes.empty ())
3909 return false;
3911 gdbarch = objfile->arch ();
3913 for (probe *p : bp_objfile_data->exception_probes)
3915 b = create_internal_breakpoint (gdbarch,
3916 p->get_relocated_address (objfile),
3917 bp_exception_master);
3918 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
3919 b->enable_state = bp_disabled;
3922 return true;
3925 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3926 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3928 static bool
3929 create_exception_master_breakpoint_hook (objfile *objfile)
3931 const char *const func_name = "_Unwind_DebugHook";
3932 struct breakpoint *b;
3933 struct gdbarch *gdbarch;
3934 struct breakpoint_objfile_data *bp_objfile_data;
3936 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3938 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3939 return false;
3941 gdbarch = objfile->arch ();
3943 if (bp_objfile_data->exception_msym.minsym == NULL)
3945 bound_minimal_symbol debug_hook
3946 = lookup_minimal_symbol_text (objfile->pspace (), func_name, objfile);
3947 if (debug_hook.minsym == NULL)
3949 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3950 return false;
3953 bp_objfile_data->exception_msym = debug_hook;
3956 b = create_internal_breakpoint (gdbarch, bp_objfile_data->exception_msym,
3957 bp_exception_master);
3958 b->locspec = new_explicit_location_spec_function (func_name);
3959 b->enable_state = bp_disabled;
3961 return true;
3964 /* Install a master breakpoint on the unwinder's debug hook. */
3966 static void
3967 create_exception_master_breakpoint (void)
3969 for (objfile *obj : current_program_space->objfiles ())
3971 /* Skip separate debug object. */
3972 if (obj->separate_debug_objfile_backlink)
3973 continue;
3975 /* Try a probe kind breakpoint. */
3976 if (create_exception_master_breakpoint_probe (obj))
3977 continue;
3979 /* Iterate over main and separate debug objects and try an
3980 _Unwind_DebugHook kind breakpoint. */
3981 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3982 if (create_exception_master_breakpoint_hook (debug_objfile))
3983 break;
3987 /* Does B have a location spec? */
3989 static bool
3990 breakpoint_location_spec_empty_p (const struct breakpoint *b)
3992 return (b->locspec != nullptr && b->locspec->empty_p ());
3995 void
3996 update_breakpoints_after_exec (void)
3998 /* We're about to delete breakpoints from GDB's lists. If the
3999 INSERTED flag is true, GDB will try to lift the breakpoints by
4000 writing the breakpoints' "shadow contents" back into memory. The
4001 "shadow contents" are NOT valid after an exec, so GDB should not
4002 do that. Instead, the target is responsible from marking
4003 breakpoints out as soon as it detects an exec. We don't do that
4004 here instead, because there may be other attempts to delete
4005 breakpoints after detecting an exec and before reaching here. */
4006 for (bp_location *bploc : all_bp_locations ())
4007 if (bploc->pspace == current_program_space)
4008 gdb_assert (!bploc->inserted);
4010 for (breakpoint &b : all_breakpoints_safe ())
4012 if (b.pspace != current_program_space)
4013 continue;
4015 /* Solib breakpoints must be explicitly reset after an exec(). */
4016 if (b.type == bp_shlib_event)
4018 delete_breakpoint (&b);
4019 continue;
4022 /* JIT breakpoints must be explicitly reset after an exec(). */
4023 if (b.type == bp_jit_event)
4025 delete_breakpoint (&b);
4026 continue;
4029 /* Thread event breakpoints must be set anew after an exec(),
4030 as must overlay event and longjmp master breakpoints. */
4031 if (b.type == bp_thread_event || b.type == bp_overlay_event
4032 || b.type == bp_longjmp_master || b.type == bp_std_terminate_master
4033 || b.type == bp_exception_master)
4035 delete_breakpoint (&b);
4036 continue;
4039 /* Step-resume breakpoints are meaningless after an exec(). */
4040 if (b.type == bp_step_resume || b.type == bp_hp_step_resume)
4042 delete_breakpoint (&b);
4043 continue;
4046 /* Just like single-step breakpoints. */
4047 if (b.type == bp_single_step)
4049 delete_breakpoint (&b);
4050 continue;
4053 /* Longjmp and longjmp-resume breakpoints are also meaningless
4054 after an exec. */
4055 if (b.type == bp_longjmp || b.type == bp_longjmp_resume
4056 || b.type == bp_longjmp_call_dummy
4057 || b.type == bp_exception || b.type == bp_exception_resume)
4059 delete_breakpoint (&b);
4060 continue;
4063 if (b.type == bp_catchpoint)
4065 /* For now, none of the bp_catchpoint breakpoints need to
4066 do anything at this point. In the future, if some of
4067 the catchpoints need to something, we will need to add
4068 a new method, and call this method from here. */
4069 continue;
4072 /* bp_finish is a special case. The only way we ought to be able
4073 to see one of these when an exec() has happened, is if the user
4074 caught a vfork, and then said "finish". Ordinarily a finish just
4075 carries them to the call-site of the current callee, by setting
4076 a temporary bp there and resuming. But in this case, the finish
4077 will carry them entirely through the vfork & exec.
4079 We don't want to allow a bp_finish to remain inserted now. But
4080 we can't safely delete it, 'cause finish_command has a handle to
4081 the bp on a bpstat, and will later want to delete it. There's a
4082 chance (and I've seen it happen) that if we delete the bp_finish
4083 here, that its storage will get reused by the time finish_command
4084 gets 'round to deleting the "use to be a bp_finish" breakpoint.
4085 We really must allow finish_command to delete a bp_finish.
4087 In the absence of a general solution for the "how do we know
4088 it's safe to delete something others may have handles to?"
4089 problem, what we'll do here is just uninsert the bp_finish, and
4090 let finish_command delete it.
4092 (We know the bp_finish is "doomed" in the sense that it's
4093 momentary, and will be deleted as soon as finish_command sees
4094 the inferior stopped. So it doesn't matter that the bp's
4095 address is probably bogus in the new a.out, unlike e.g., the
4096 solib breakpoints.) */
4098 if (b.type == bp_finish)
4100 continue;
4103 /* Without a symbolic address, we have little hope of the
4104 pre-exec() address meaning the same thing in the post-exec()
4105 a.out. */
4106 if (breakpoint_location_spec_empty_p (&b))
4108 delete_breakpoint (&b);
4109 continue;
4115 detach_breakpoints (ptid_t ptid)
4117 int val = 0;
4118 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
4119 struct inferior *inf = current_inferior ();
4121 if (ptid.pid () == inferior_ptid.pid ())
4122 error (_("Cannot detach breakpoints of inferior_ptid"));
4124 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
4125 inferior_ptid = ptid;
4126 for (bp_location *bl : all_bp_locations ())
4128 if (bl->pspace != inf->pspace)
4129 continue;
4131 /* This function must physically remove breakpoints locations
4132 from the specified ptid, without modifying the breakpoint
4133 package's state. Locations of type bp_loc_other and
4134 bp_loc_software_watchpoint are only maintained at GDB side,
4135 so there is no need to remove them. Moreover, removing these
4136 would modify the breakpoint package's state. */
4137 if (bl->loc_type == bp_loc_other
4138 || bl->loc_type == bp_loc_software_watchpoint)
4139 continue;
4141 if (bl->inserted)
4142 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
4145 return val;
4148 /* Remove the breakpoint location BL from the current address space.
4149 Note that this is used to detach breakpoints from a child fork.
4150 When we get here, the child isn't in the inferior list, and neither
4151 do we have objects to represent its address space --- we should
4152 *not* look at bl->pspace->aspace here. */
4154 static int
4155 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
4157 breakpoint_debug_printf ("%s due to %s",
4158 breakpoint_location_address_str (bl).c_str (),
4159 remove_bp_reason_str (reason));
4161 int val;
4163 /* BL is never in moribund_locations by our callers. */
4164 gdb_assert (bl->owner != NULL);
4166 /* The type of none suggests that owner is actually deleted.
4167 This should not ever happen. */
4168 gdb_assert (bl->owner->type != bp_none);
4170 if (bl->loc_type == bp_loc_software_breakpoint
4171 || bl->loc_type == bp_loc_hardware_breakpoint)
4173 /* "Normal" instruction breakpoint: either the standard
4174 trap-instruction bp (bp_breakpoint), or a
4175 bp_hardware_breakpoint. */
4177 /* First check to see if we have to handle an overlay. */
4178 if (overlay_debugging == ovly_off
4179 || bl->section == NULL
4180 || !(section_is_overlay (bl->section)))
4182 /* No overlay handling: just remove the breakpoint. */
4184 /* If we're trying to uninsert a memory breakpoint that we
4185 know is set in a dynamic object that is marked
4186 shlib_disabled, then either the dynamic object was
4187 removed with "remove-symbol-file" or with
4188 "nosharedlibrary". In the former case, we don't know
4189 whether another dynamic object might have loaded over the
4190 breakpoint's address -- the user might well let us know
4191 about it next with add-symbol-file (the whole point of
4192 add-symbol-file is letting the user manually maintain a
4193 list of dynamically loaded objects). If we have the
4194 breakpoint's shadow memory, that is, this is a software
4195 breakpoint managed by GDB, check whether the breakpoint
4196 is still inserted in memory, to avoid overwriting wrong
4197 code with stale saved shadow contents. Note that HW
4198 breakpoints don't have shadow memory, as they're
4199 implemented using a mechanism that is not dependent on
4200 being able to modify the target's memory, and as such
4201 they should always be removed. */
4202 if (bl->shlib_disabled
4203 && bl->target_info.shadow_len != 0
4204 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
4205 val = 0;
4206 else
4207 val = bl->owner->remove_location (bl, reason);
4209 else
4211 /* This breakpoint is in an overlay section.
4212 Did we set a breakpoint at the LMA? */
4213 if (!overlay_events_enabled)
4215 /* Yes -- overlay event support is not active, so we
4216 should have set a breakpoint at the LMA. Remove it.
4218 /* Ignore any failures: if the LMA is in ROM, we will
4219 have already warned when we failed to insert it. */
4220 if (bl->loc_type == bp_loc_hardware_breakpoint)
4221 target_remove_hw_breakpoint (bl->gdbarch,
4222 &bl->overlay_target_info);
4223 else
4224 target_remove_breakpoint (bl->gdbarch,
4225 &bl->overlay_target_info,
4226 reason);
4228 /* Did we set a breakpoint at the VMA?
4229 If so, we will have marked the breakpoint 'inserted'. */
4230 if (bl->inserted)
4232 /* Yes -- remove it. Previously we did not bother to
4233 remove the breakpoint if the section had been
4234 unmapped, but let's not rely on that being safe. We
4235 don't know what the overlay manager might do. */
4237 /* However, we should remove *software* breakpoints only
4238 if the section is still mapped, or else we overwrite
4239 wrong code with the saved shadow contents. */
4240 if (bl->loc_type == bp_loc_hardware_breakpoint
4241 || section_is_mapped (bl->section))
4242 val = bl->owner->remove_location (bl, reason);
4243 else
4244 val = 0;
4246 else
4248 /* No -- not inserted, so no need to remove. No error. */
4249 val = 0;
4253 /* In some cases, we might not be able to remove a breakpoint in
4254 a shared library that has already been removed, but we have
4255 not yet processed the shlib unload event. Similarly for an
4256 unloaded add-symbol-file object - the user might not yet have
4257 had the chance to remove-symbol-file it. shlib_disabled will
4258 be set if the library/object has already been removed, but
4259 the breakpoint hasn't been uninserted yet, e.g., after
4260 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4261 always-inserted mode. */
4262 if (val
4263 && (bl->loc_type == bp_loc_software_breakpoint
4264 && (bl->shlib_disabled
4265 || solib_name_from_address (bl->pspace, bl->address)
4266 || shared_objfile_contains_address_p (bl->pspace,
4267 bl->address))))
4268 val = 0;
4270 if (val)
4271 return val;
4272 bl->inserted = (reason == DETACH_BREAKPOINT);
4274 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4276 bl->inserted = (reason == DETACH_BREAKPOINT);
4277 bl->owner->remove_location (bl, reason);
4279 /* Failure to remove any of the hardware watchpoints comes here. */
4280 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4281 warning (_("Could not remove hardware watchpoint %d."),
4282 bl->owner->number);
4284 else if (bl->owner->type == bp_catchpoint
4285 && breakpoint_enabled (bl->owner)
4286 && !bl->duplicate)
4288 val = bl->owner->remove_location (bl, reason);
4289 if (val)
4290 return val;
4292 bl->inserted = (reason == DETACH_BREAKPOINT);
4295 return 0;
4298 static int
4299 remove_breakpoint (struct bp_location *bl)
4301 /* BL is never in moribund_locations by our callers. */
4302 gdb_assert (bl->owner != NULL);
4304 /* The type of none suggests that owner is actually deleted.
4305 This should not ever happen. */
4306 gdb_assert (bl->owner->type != bp_none);
4308 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4310 switch_to_program_space_and_thread (bl->pspace);
4312 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4315 /* See breakpoint.h. */
4317 void
4318 mark_breakpoints_out (program_space *pspace)
4320 for (bp_location *bl : all_bp_locations ())
4321 if (bl->pspace == pspace)
4322 bl->inserted = 0;
4325 /* See breakpoint.h. */
4327 void
4328 breakpoint_init_inferior (inferior *inf, inf_context context)
4330 /* If breakpoint locations are shared across processes, then there's
4331 nothing to do. */
4332 if (gdbarch_has_global_breakpoints (inf->arch ()))
4333 return;
4335 mark_breakpoints_out (inf->pspace);
4337 for (breakpoint &b : all_breakpoints_safe ())
4339 if (b.has_locations () && b.first_loc ().pspace != inf->pspace)
4340 continue;
4342 switch (b.type)
4344 case bp_call_dummy:
4345 case bp_longjmp_call_dummy:
4347 /* If the call dummy breakpoint is at the entry point it will
4348 cause problems when the inferior is rerun, so we better get
4349 rid of it. */
4351 case bp_watchpoint_scope:
4353 /* Also get rid of scope breakpoints. */
4355 case bp_shlib_event:
4357 /* Also remove solib event breakpoints. Their addresses may
4358 have changed since the last time we ran the program.
4359 Actually we may now be debugging against different target;
4360 and so the solib backend that installed this breakpoint may
4361 not be used in by the target. E.g.,
4363 (gdb) file prog-linux
4364 (gdb) run # native linux target
4366 (gdb) kill
4367 (gdb) file prog-win.exe
4368 (gdb) tar rem :9999 # remote Windows gdbserver.
4371 case bp_step_resume:
4373 /* Also remove step-resume breakpoints. */
4375 case bp_single_step:
4377 /* Also remove single-step breakpoints. */
4379 delete_breakpoint (&b);
4380 break;
4382 case bp_watchpoint:
4383 case bp_hardware_watchpoint:
4384 case bp_read_watchpoint:
4385 case bp_access_watchpoint:
4387 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
4389 /* Likewise for watchpoints on local expressions. */
4390 if (w.exp_valid_block != NULL)
4391 delete_breakpoint (&b);
4392 else
4394 /* Get rid of existing locations, which are no longer
4395 valid. New ones will be created in
4396 update_watchpoint, when the inferior is restarted.
4397 The next update_global_location_list call will
4398 garbage collect them. */
4399 b.clear_locations ();
4401 if (context == inf_starting)
4403 /* Reset val field to force reread of starting value in
4404 insert_breakpoints. */
4405 w.val.reset (nullptr);
4406 w.val_valid = false;
4410 break;
4411 default:
4412 break;
4416 /* Get rid of the moribund locations. */
4417 for (bp_location *bl : moribund_locations)
4418 decref_bp_location (&bl);
4419 moribund_locations.clear ();
4422 /* These functions concern about actual breakpoints inserted in the
4423 target --- to e.g. check if we need to do decr_pc adjustment or if
4424 we need to hop over the bkpt --- so we check for address space
4425 match, not program space. */
4427 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4428 exists at PC. It returns ordinary_breakpoint_here if it's an
4429 ordinary breakpoint, or permanent_breakpoint_here if it's a
4430 permanent breakpoint.
4431 - When continuing from a location with an ordinary breakpoint, we
4432 actually single step once before calling insert_breakpoints.
4433 - When continuing from a location with a permanent breakpoint, we
4434 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4435 the target, to advance the PC past the breakpoint. */
4437 enum breakpoint_here
4438 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4440 bool any_breakpoint_here = false;
4442 for (bp_location *bl : all_bp_locations ())
4444 if (bl->loc_type != bp_loc_software_breakpoint
4445 && bl->loc_type != bp_loc_hardware_breakpoint)
4446 continue;
4448 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4449 if ((breakpoint_enabled (bl->owner)
4450 || bl->permanent)
4451 && breakpoint_location_address_match (bl, aspace, pc))
4453 if (overlay_debugging
4454 && section_is_overlay (bl->section)
4455 && !section_is_mapped (bl->section))
4456 continue; /* unmapped overlay -- can't be a match */
4457 else if (bl->permanent)
4458 return permanent_breakpoint_here;
4459 else
4460 any_breakpoint_here = true;
4464 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4467 /* See breakpoint.h. */
4470 breakpoint_in_range_p (const address_space *aspace,
4471 CORE_ADDR addr, ULONGEST len)
4473 for (bp_location *bl : all_bp_locations ())
4475 if (bl->loc_type != bp_loc_software_breakpoint
4476 && bl->loc_type != bp_loc_hardware_breakpoint)
4477 continue;
4479 if ((breakpoint_enabled (bl->owner)
4480 || bl->permanent)
4481 && breakpoint_location_address_range_overlap (bl, aspace,
4482 addr, len))
4484 if (overlay_debugging
4485 && section_is_overlay (bl->section)
4486 && !section_is_mapped (bl->section))
4488 /* Unmapped overlay -- can't be a match. */
4489 continue;
4492 return 1;
4496 return 0;
4499 /* Return true if there's a moribund breakpoint at PC. */
4502 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4504 for (bp_location *loc : moribund_locations)
4505 if (breakpoint_location_address_match (loc, aspace, pc))
4506 return 1;
4508 return 0;
4511 /* Returns true iff BL is inserted at PC, in address space ASPACE. */
4513 static bool
4514 bp_location_inserted_here_p (const struct bp_location *bl,
4515 const address_space *aspace, CORE_ADDR pc)
4517 if (bl->inserted
4518 && breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
4519 aspace, pc))
4521 /* An unmapped overlay can't be a match. */
4522 return !(overlay_debugging
4523 && section_is_overlay (bl->section)
4524 && !section_is_mapped (bl->section));
4526 return false;
4529 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4532 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4534 for (bp_location *bl : all_bp_locations_at_addr (pc))
4536 if (bl->loc_type != bp_loc_software_breakpoint
4537 && bl->loc_type != bp_loc_hardware_breakpoint)
4538 continue;
4540 if (bp_location_inserted_here_p (bl, aspace, pc))
4541 return 1;
4543 return 0;
4546 /* This function returns non-zero iff there is a software breakpoint
4547 inserted at PC. */
4550 software_breakpoint_inserted_here_p (const address_space *aspace,
4551 CORE_ADDR pc)
4553 for (bp_location *bl : all_bp_locations_at_addr (pc))
4555 if (bl->loc_type != bp_loc_software_breakpoint)
4556 continue;
4558 if (bp_location_inserted_here_p (bl, aspace, pc))
4559 return 1;
4562 return 0;
4565 /* See breakpoint.h. */
4568 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4569 CORE_ADDR pc)
4571 for (bp_location *bl : all_bp_locations_at_addr (pc))
4573 if (bl->loc_type != bp_loc_hardware_breakpoint)
4574 continue;
4576 if (bp_location_inserted_here_p (bl, aspace, pc))
4577 return 1;
4580 return 0;
4584 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4585 CORE_ADDR addr, ULONGEST len)
4587 for (breakpoint &bpt : all_breakpoints ())
4589 if (bpt.type != bp_hardware_watchpoint
4590 && bpt.type != bp_access_watchpoint)
4591 continue;
4593 if (!breakpoint_enabled (&bpt))
4594 continue;
4596 for (bp_location &loc : bpt.locations ())
4597 if (loc.pspace->aspace.get () == aspace && loc.inserted)
4599 CORE_ADDR l, h;
4601 /* Check for intersection. */
4602 l = std::max<CORE_ADDR> (loc.address, addr);
4603 h = std::min<CORE_ADDR> (loc.address + loc.length, addr + len);
4604 if (l < h)
4605 return 1;
4608 return 0;
4611 /* See breakpoint.h. */
4613 bool
4614 is_catchpoint (struct breakpoint *b)
4616 return (b->type == bp_catchpoint);
4619 /* Clear a bpstat so that it says we are not at any breakpoint.
4620 Also free any storage that is part of a bpstat. */
4622 void
4623 bpstat_clear (bpstat **bsp)
4625 bpstat *p;
4626 bpstat *q;
4628 if (bsp == 0)
4629 return;
4630 p = *bsp;
4631 while (p != NULL)
4633 q = p->next;
4634 delete p;
4635 p = q;
4637 *bsp = NULL;
4640 bpstat::bpstat (const bpstat &other)
4641 : next (NULL),
4642 bp_location_at (other.bp_location_at),
4643 breakpoint_at (other.breakpoint_at),
4644 commands (other.commands),
4645 print (other.print),
4646 stop (other.stop),
4647 print_it (other.print_it)
4649 if (other.old_val != NULL)
4650 old_val = release_value (other.old_val->copy ());
4653 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4654 is part of the bpstat is copied as well. */
4656 bpstat *
4657 bpstat_copy (bpstat *bs)
4659 bpstat *p = nullptr;
4660 bpstat *tmp;
4661 bpstat *retval = nullptr;
4663 if (bs == NULL)
4664 return bs;
4666 for (; bs != NULL; bs = bs->next)
4668 tmp = new bpstat (*bs);
4670 if (p == NULL)
4671 /* This is the first thing in the chain. */
4672 retval = tmp;
4673 else
4674 p->next = tmp;
4675 p = tmp;
4677 p->next = NULL;
4678 return retval;
4681 /* Find the bpstat associated with this breakpoint. */
4683 bpstat *
4684 bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
4686 if (bsp == NULL)
4687 return NULL;
4689 for (; bsp != NULL; bsp = bsp->next)
4691 if (bsp->breakpoint_at == breakpoint)
4692 return bsp;
4694 return NULL;
4697 /* See breakpoint.h. */
4699 bool
4700 bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
4702 for (; bsp != NULL; bsp = bsp->next)
4704 if (bsp->breakpoint_at == NULL)
4706 /* A moribund location can never explain a signal other than
4707 GDB_SIGNAL_TRAP. */
4708 if (sig == GDB_SIGNAL_TRAP)
4709 return true;
4711 else
4713 if (bsp->breakpoint_at->explains_signal (sig))
4714 return true;
4718 return false;
4721 /* See breakpoint.h. */
4724 bpstat_num (bpstat **bsp, int *num)
4726 struct breakpoint *b;
4728 if ((*bsp) == NULL)
4729 return 0; /* No more breakpoint values */
4731 /* We assume we'll never have several bpstats that correspond to a
4732 single breakpoint -- otherwise, this function might return the
4733 same number more than once and this will look ugly. */
4734 b = (*bsp)->breakpoint_at;
4735 *bsp = (*bsp)->next;
4736 if (b == NULL)
4737 return -1; /* breakpoint that's been deleted since */
4739 *num = b->number; /* We have its number */
4740 return 1;
4743 /* See breakpoint.h */
4746 bpstat_locno (const bpstat *bs)
4748 const struct breakpoint *b = bs->breakpoint_at;
4749 const struct bp_location *bl = bs->bp_location_at.get ();
4751 if (b != nullptr && b->has_multiple_locations ())
4753 int locno = 1;
4755 for (bp_location &loc : b->locations ())
4757 if (bl == &loc)
4758 return locno;
4760 ++locno;
4763 warning (_("location number not found for breakpoint %d address %s."),
4764 b->number, paddress (bl->gdbarch, bl->address));
4767 return 0;
4770 /* See breakpoint.h. */
4772 void
4773 print_num_locno (const bpstat *bs, struct ui_out *uiout)
4775 struct breakpoint *b = bs->breakpoint_at;
4777 if (b == nullptr)
4778 uiout->text (_("deleted breakpoint"));
4779 else
4781 uiout->field_signed ("bkptno", b->number);
4783 int locno = bpstat_locno (bs);
4784 if (locno != 0)
4785 uiout->message (".%pF", signed_field ("locno", locno));
4789 /* See breakpoint.h. */
4791 void
4792 bpstat_clear_actions (void)
4794 bpstat *bs;
4796 if (inferior_ptid == null_ptid)
4797 return;
4799 thread_info *tp = inferior_thread ();
4800 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4802 bs->commands = NULL;
4803 bs->old_val.reset (nullptr);
4807 /* Called when a command is about to proceed the inferior. */
4809 static void
4810 breakpoint_about_to_proceed (void)
4812 if (inferior_ptid != null_ptid)
4814 struct thread_info *tp = inferior_thread ();
4816 /* Allow inferior function calls in breakpoint commands to not
4817 interrupt the command list. When the call finishes
4818 successfully, the inferior will be standing at the same
4819 breakpoint as if nothing happened. */
4820 if (tp->control.in_infcall)
4821 return;
4824 breakpoint_proceeded = 1;
4827 /* Return true iff CMD as the first line of a command sequence is `silent'
4828 or its equivalent. */
4830 static bool
4831 command_line_is_silent (struct command_line *cmd)
4833 return cmd && (strcmp ("silent", cmd->line) == 0);
4836 /* Sets the $_hit_bpnum and $_hit_locno to bpnum and locno.
4837 A locno 0 is changed to 1 to e.g. let the user do
4838 (gdb) disable $_hit_bpnum.$_hit_locno
4839 for a single location breakpoint. */
4841 static void
4842 set_hit_convenience_vars (int bpnum, int locno)
4844 set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), bpnum);
4845 set_internalvar_integer (lookup_internalvar ("_hit_locno"),
4846 (locno > 0 ? locno : 1));
4849 /* Execute all the commands associated with all the breakpoints at
4850 this location. Any of these commands could cause the process to
4851 proceed beyond this point, etc. We look out for such changes by
4852 checking the global "breakpoint_proceeded" after each command.
4854 Returns true if a breakpoint command resumed the inferior. In that
4855 case, it is the caller's responsibility to recall it again with the
4856 bpstat of the current thread. */
4858 static bool
4859 bpstat_do_actions_1 (bpstat **bsp)
4861 bpstat *bs;
4862 bool again = false;
4864 /* Avoid endless recursion if a `source' command is contained
4865 in bs->commands. */
4866 if (executing_breakpoint_commands)
4867 return false;
4869 scoped_restore save_executing
4870 = make_scoped_restore (&executing_breakpoint_commands, 1);
4872 scoped_restore preventer = prevent_dont_repeat ();
4874 /* This pointer will iterate over the list of bpstat's. */
4875 bs = *bsp;
4877 /* The $_hit_* convenience variables are set before running the
4878 commands of BS. In case we have several bs, after the loop,
4879 we set again the variables to the first printed bpnum and locno.
4880 For multiple breakpoints, this ensures the variables are set to the
4881 breakpoint printed for the user. */
4882 int printed_hit_bpnum = -1;
4883 int printed_hit_locno = -1;
4885 breakpoint_proceeded = 0;
4886 for (; bs != NULL; bs = bs->next)
4888 struct command_line *cmd = NULL;
4890 /* Set the _hit_* convenience variables before running BS's commands. */
4892 const struct breakpoint *b = bs->breakpoint_at;
4893 if (b != nullptr)
4895 int locno = bpstat_locno (bs);
4897 set_hit_convenience_vars (b->number, locno);
4898 if (printed_hit_locno == -1 && bs->print)
4900 printed_hit_bpnum = b->number;
4901 printed_hit_locno = locno;
4906 /* Take ownership of the BSP's command tree, if it has one.
4908 The command tree could legitimately contain commands like
4909 'step' and 'next', which call clear_proceed_status, which
4910 frees the bpstat BS and its command tree. To make sure this doesn't
4911 free the tree we're executing out from under us, we need to
4912 take ownership of the tree ourselves. Since a given bpstat's
4913 commands are only executed once, we don't need to copy it; we
4914 can clear the pointer in the bpstat, and make sure we free
4915 the tree when we're done. */
4916 counted_command_line ccmd = bs->commands;
4917 bs->commands = NULL;
4918 if (ccmd != NULL)
4919 cmd = ccmd.get ();
4920 if (command_line_is_silent (cmd))
4922 /* The action has been already done by bpstat_stop_status. */
4923 cmd = cmd->next;
4926 while (cmd != NULL)
4928 execute_control_command (cmd);
4929 /* After execute_control_command, if breakpoint_proceeded is true,
4930 BS has been freed and cannot be accessed anymore. */
4932 if (breakpoint_proceeded)
4933 break;
4934 else
4935 cmd = cmd->next;
4938 if (breakpoint_proceeded)
4940 if (current_ui->async)
4941 /* If we are in async mode, then the target might be still
4942 running, not stopped at any breakpoint, so nothing for
4943 us to do here -- just return to the event loop. */
4945 else
4946 /* In sync mode, when execute_control_command returns
4947 we're already standing on the next breakpoint.
4948 Breakpoint commands for that stop were not run, since
4949 execute_command does not run breakpoint commands --
4950 only command_line_handler does, but that one is not
4951 involved in execution of breakpoint commands. So, we
4952 can now execute breakpoint commands. It should be
4953 noted that making execute_command do bpstat actions is
4954 not an option -- in this case we'll have recursive
4955 invocation of bpstat for each breakpoint with a
4956 command, and can easily blow up GDB stack. Instead, we
4957 return true, which will trigger the caller to recall us
4958 with the new stop_bpstat. */
4959 again = true;
4960 break;
4964 /* Now that we have executed the commands of all bs, set the _hit_*
4965 convenience variables to the printed values. */
4966 if (printed_hit_locno != -1)
4967 set_hit_convenience_vars (printed_hit_bpnum, printed_hit_locno);
4969 return again;
4972 /* Helper for bpstat_do_actions. Get the current thread, if there's
4973 one, is alive and has execution. Return NULL otherwise. */
4975 static thread_info *
4976 get_bpstat_thread ()
4978 if (inferior_ptid == null_ptid || !target_has_execution ())
4979 return NULL;
4981 thread_info *tp = inferior_thread ();
4982 if (tp->state == THREAD_EXITED || tp->executing ())
4983 return NULL;
4984 return tp;
4987 void
4988 bpstat_do_actions (void)
4990 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4991 thread_info *tp;
4993 /* Do any commands attached to breakpoint we are stopped at. */
4994 while ((tp = get_bpstat_thread ()) != NULL)
4996 /* Since in sync mode, bpstat_do_actions may resume the
4997 inferior, and only return when it is stopped at the next
4998 breakpoint, we keep doing breakpoint actions until it returns
4999 false to indicate the inferior was not resumed. */
5000 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
5001 break;
5004 cleanup_if_error.release ();
5007 /* Print out the (old or new) value associated with a watchpoint. */
5009 static void
5010 watchpoint_value_print (struct value *val, struct ui_file *stream)
5012 if (val == NULL)
5013 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
5014 else
5016 struct value_print_options opts;
5017 get_user_print_options (&opts);
5018 value_print (val, stream, &opts);
5022 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
5023 debugging multiple threads. */
5025 void
5026 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
5028 if (uiout->is_mi_like_p ())
5029 return;
5031 uiout->text ("\n");
5033 if (show_thread_that_caused_stop ())
5035 struct thread_info *thr = inferior_thread ();
5037 uiout->text ("Thread ");
5038 uiout->field_string ("thread-id", print_thread_id (thr));
5040 const char *name = thread_name (thr);
5041 if (name != NULL)
5043 uiout->text (" \"");
5044 uiout->field_string ("name", name);
5045 uiout->text ("\"");
5048 uiout->text (" hit ");
5052 /* Generic routine for printing messages indicating why we
5053 stopped. The behavior of this function depends on the value
5054 'print_it' in the bpstat structure. Under some circumstances we
5055 may decide not to print anything here and delegate the task to
5056 normal_stop(). */
5058 static enum print_stop_action
5059 print_bp_stop_message (bpstat *bs)
5061 switch (bs->print_it)
5063 case print_it_noop:
5064 /* Nothing should be printed for this bpstat entry. */
5065 return PRINT_UNKNOWN;
5067 case print_it_done:
5068 /* We still want to print the frame, but we already printed the
5069 relevant messages. */
5070 return PRINT_SRC_AND_LOC;
5072 case print_it_normal:
5074 struct breakpoint *b = bs->breakpoint_at;
5076 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
5077 which has since been deleted. */
5078 if (b == NULL)
5079 return PRINT_UNKNOWN;
5081 /* Normal case. Call the breakpoint's print_it method. */
5082 return b->print_it (bs);
5085 default:
5086 internal_error (_("print_bp_stop_message: unrecognized enum value"));
5090 /* See breakpoint.h. */
5092 void
5093 print_solib_event (bool is_catchpoint)
5095 bool any_deleted = !current_program_space->deleted_solibs.empty ();
5096 bool any_added = !current_program_space->added_solibs.empty ();
5098 if (!is_catchpoint)
5100 if (any_added || any_deleted)
5101 current_uiout->text (_("Stopped due to shared library event:\n"));
5102 else
5103 current_uiout->text (_("Stopped due to shared library event (no "
5104 "libraries added or removed)\n"));
5107 if (current_uiout->is_mi_like_p ())
5108 current_uiout->field_string ("reason",
5109 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
5111 if (any_deleted)
5113 current_uiout->text (_(" Inferior unloaded "));
5114 ui_out_emit_list list_emitter (current_uiout, "removed");
5115 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
5117 const std::string &name = current_program_space->deleted_solibs[ix];
5119 if (ix > 0)
5120 current_uiout->text (" ");
5121 current_uiout->field_string ("library", name);
5122 current_uiout->text ("\n");
5126 if (any_added)
5128 current_uiout->text (_(" Inferior loaded "));
5129 ui_out_emit_list list_emitter (current_uiout, "added");
5130 bool first = true;
5131 for (solib *iter : current_program_space->added_solibs)
5133 if (!first)
5134 current_uiout->text (" ");
5135 first = false;
5136 current_uiout->field_string ("library", iter->so_name);
5137 current_uiout->text ("\n");
5142 /* Print a message indicating what happened. This is called from
5143 normal_stop(). The input to this routine is the head of the bpstat
5144 list - a list of the eventpoints that caused this stop. KIND is
5145 the target_waitkind for the stopping event. This
5146 routine calls the generic print routine for printing a message
5147 about reasons for stopping. This will print (for example) the
5148 "Breakpoint n," part of the output. The return value of this
5149 routine is one of:
5151 PRINT_UNKNOWN: Means we printed nothing.
5152 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
5153 code to print the location. An example is
5154 "Breakpoint 1, " which should be followed by
5155 the location.
5156 PRINT_SRC_ONLY: Means we printed something, but there is no need
5157 to also print the location part of the message.
5158 An example is the catch/throw messages, which
5159 don't require a location appended to the end.
5160 PRINT_NOTHING: We have done some printing and we don't need any
5161 further info to be printed. */
5163 enum print_stop_action
5164 bpstat_print (bpstat *bs, target_waitkind kind)
5166 enum print_stop_action val;
5168 /* Maybe another breakpoint in the chain caused us to stop.
5169 (Currently all watchpoints go on the bpstat whether hit or not.
5170 That probably could (should) be changed, provided care is taken
5171 with respect to bpstat_explains_signal). */
5172 for (; bs; bs = bs->next)
5174 val = print_bp_stop_message (bs);
5175 if (val == PRINT_SRC_ONLY
5176 || val == PRINT_SRC_AND_LOC
5177 || val == PRINT_NOTHING)
5178 return val;
5181 /* If we had hit a shared library event breakpoint,
5182 print_bp_stop_message would print out this message. If we hit an
5183 OS-level shared library event, do the same thing. */
5184 if (kind == TARGET_WAITKIND_LOADED)
5186 print_solib_event (false);
5187 return PRINT_NOTHING;
5190 /* We reached the end of the chain, or we got a null BS to start
5191 with and nothing was printed. */
5192 return PRINT_UNKNOWN;
5195 /* Evaluate the boolean expression EXP and return the result. */
5197 static bool
5198 breakpoint_cond_eval (expression *exp)
5200 scoped_value_mark mark;
5201 return value_true (exp->evaluate ());
5204 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
5206 bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
5207 : next (NULL),
5208 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
5209 breakpoint_at (bl->owner),
5210 commands (NULL),
5211 print (0),
5212 stop (0),
5213 print_it (print_it_normal)
5215 **bs_link_pointer = this;
5216 *bs_link_pointer = &next;
5219 bpstat::bpstat ()
5220 : next (NULL),
5221 breakpoint_at (NULL),
5222 commands (NULL),
5223 print (0),
5224 stop (0),
5225 print_it (print_it_normal)
5229 /* The target has stopped with waitstatus WS. Check if any hardware
5230 watchpoints have triggered, according to the target. */
5233 watchpoints_triggered (const target_waitstatus &ws)
5235 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
5236 CORE_ADDR addr;
5238 if (!stopped_by_watchpoint)
5240 /* We were not stopped by a watchpoint. Mark all watchpoints
5241 as not triggered. */
5242 for (breakpoint &b : all_breakpoints ())
5243 if (is_hardware_watchpoint (&b))
5245 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
5247 w.watchpoint_triggered = watch_triggered_no;
5250 return 0;
5253 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
5255 /* We were stopped by a watchpoint, but we don't know where.
5256 Mark all watchpoints as unknown. */
5257 for (breakpoint &b : all_breakpoints ())
5258 if (is_hardware_watchpoint (&b))
5260 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
5262 w.watchpoint_triggered = watch_triggered_unknown;
5265 return 1;
5268 /* The target could report the data address. Mark watchpoints
5269 affected by this data address as triggered, and all others as not
5270 triggered. */
5272 for (breakpoint &b : all_breakpoints ())
5273 if (is_hardware_watchpoint (&b))
5275 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
5277 w.watchpoint_triggered = watch_triggered_no;
5278 for (bp_location &loc : b.locations ())
5280 if (is_masked_watchpoint (&b))
5282 CORE_ADDR newaddr = addr & w.hw_wp_mask;
5283 CORE_ADDR start = loc.address & w.hw_wp_mask;
5285 if (newaddr == start)
5287 w.watchpoint_triggered = watch_triggered_yes;
5288 break;
5291 /* Exact match not required. Within range is sufficient. */
5292 else if (target_watchpoint_addr_within_range
5293 (current_inferior ()->top_target (), addr, loc.address,
5294 loc.length))
5296 w.watchpoint_triggered = watch_triggered_yes;
5297 break;
5302 return 1;
5305 /* Possible return values for watchpoint_check. */
5306 enum wp_check_result
5308 /* The watchpoint has been deleted. */
5309 WP_DELETED = 1,
5311 /* The value has changed. */
5312 WP_VALUE_CHANGED = 2,
5314 /* The value has not changed. */
5315 WP_VALUE_NOT_CHANGED = 3,
5317 /* Ignore this watchpoint, no matter if the value changed or not. */
5318 WP_IGNORE = 4,
5321 #define BP_TEMPFLAG 1
5322 #define BP_HARDWAREFLAG 2
5324 /* Evaluate watchpoint condition expression and check if its value
5325 changed. */
5327 static wp_check_result
5328 watchpoint_check (bpstat *bs)
5330 frame_info_ptr fr;
5331 bool within_current_scope;
5333 /* BS is built from an existing struct breakpoint. */
5334 gdb_assert (bs->breakpoint_at != NULL);
5335 watchpoint *b = gdb::checked_static_cast<watchpoint *> (bs->breakpoint_at);
5337 /* If this is a local watchpoint, we only want to check if the
5338 watchpoint frame is in scope if the current thread is the thread
5339 that was used to create the watchpoint. */
5340 if (!watchpoint_in_thread_scope (b))
5341 return WP_IGNORE;
5343 if (b->exp_valid_block == NULL)
5344 within_current_scope = true;
5345 else
5347 frame_info_ptr frame = get_current_frame ();
5348 struct gdbarch *frame_arch = get_frame_arch (frame);
5349 CORE_ADDR frame_pc = get_frame_pc (frame);
5351 /* stack_frame_destroyed_p() returns a non-zero value if we're
5352 still in the function but the stack frame has already been
5353 invalidated. Since we can't rely on the values of local
5354 variables after the stack has been destroyed, we are treating
5355 the watchpoint in that state as `not changed' without further
5356 checking. Don't mark watchpoints as changed if the current
5357 frame is in an epilogue - even if they are in some other
5358 frame, our view of the stack is likely to be wrong and
5359 frame_find_by_id could error out. */
5360 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5361 return WP_IGNORE;
5363 fr = frame_find_by_id (b->watchpoint_frame);
5364 within_current_scope = (fr != NULL);
5366 /* If we've gotten confused in the unwinder, we might have
5367 returned a frame that can't describe this variable. */
5368 if (within_current_scope)
5370 struct symbol *function;
5372 function = get_frame_function (fr);
5373 if (function == NULL
5374 || !function->value_block ()->contains (b->exp_valid_block))
5375 within_current_scope = false;
5378 if (within_current_scope)
5379 /* If we end up stopping, the current frame will get selected
5380 in normal_stop. So this call to select_frame won't affect
5381 the user. */
5382 select_frame (fr);
5385 if (within_current_scope)
5387 /* We use value_{,free_to_}mark because it could be a *long*
5388 time before we return to the command level and call
5389 free_all_values. We can't call free_all_values because we
5390 might be in the middle of evaluating a function call. */
5392 struct value *mark;
5393 struct value *new_val;
5395 if (is_masked_watchpoint (b))
5396 /* Since we don't know the exact trigger address (from
5397 stopped_data_address), just tell the user we've triggered
5398 a mask watchpoint. */
5399 return WP_VALUE_CHANGED;
5401 mark = value_mark ();
5402 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5403 NULL, NULL, false);
5405 if (b->val_bitsize != 0)
5406 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5408 /* We use value_equal_contents instead of value_equal because
5409 the latter coerces an array to a pointer, thus comparing just
5410 the address of the array instead of its contents. This is
5411 not what we want. */
5412 if ((b->val != NULL) != (new_val != NULL)
5413 || (b->val != NULL && !value_equal_contents (b->val.get (),
5414 new_val)))
5416 bs->old_val = b->val;
5417 b->val = release_value (new_val);
5418 b->val_valid = true;
5419 if (new_val != NULL)
5420 value_free_to_mark (mark);
5421 return WP_VALUE_CHANGED;
5423 else
5425 /* Nothing changed. */
5426 value_free_to_mark (mark);
5427 return WP_VALUE_NOT_CHANGED;
5430 else
5432 /* This seems like the only logical thing to do because
5433 if we temporarily ignored the watchpoint, then when
5434 we reenter the block in which it is valid it contains
5435 garbage (in the case of a function, it may have two
5436 garbage values, one before and one after the prologue).
5437 So we can't even detect the first assignment to it and
5438 watch after that (since the garbage may or may not equal
5439 the first value assigned). */
5440 /* We print all the stop information in
5441 breakpointprint_it, but in this case, by the time we
5442 call breakpoint->print_it this bp will be deleted
5443 already. So we have no choice but print the information
5444 here. */
5446 SWITCH_THRU_ALL_UIS ()
5448 struct ui_out *uiout = current_uiout;
5450 if (uiout->is_mi_like_p ())
5451 uiout->field_string
5452 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5453 uiout->message ("\nWatchpoint %pF deleted because the program has "
5454 "left the block in\n"
5455 "which its expression is valid.\n",
5456 signed_field ("wpnum", b->number));
5459 /* Make sure the watchpoint's commands aren't executed. */
5460 b->commands = NULL;
5461 watchpoint_del_at_next_stop (b);
5463 return WP_DELETED;
5467 /* Return true if it looks like target has stopped due to hitting
5468 breakpoint location BL. This function does not check if we should
5469 stop, only if BL explains the stop. */
5471 static bool
5472 bpstat_check_location (const struct bp_location *bl,
5473 const address_space *aspace, CORE_ADDR bp_addr,
5474 const target_waitstatus &ws)
5476 struct breakpoint *b = bl->owner;
5478 /* BL is from an existing breakpoint. */
5479 gdb_assert (b != NULL);
5481 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
5484 /* Determine if the watched values have actually changed, and we
5485 should stop. If not, set BS->stop to false. */
5487 static void
5488 bpstat_check_watchpoint (bpstat *bs)
5490 const struct bp_location *bl;
5492 /* BS is built for existing struct breakpoint. */
5493 bl = bs->bp_location_at.get ();
5494 gdb_assert (bl != NULL);
5495 watchpoint *b = gdb::checked_static_cast<watchpoint *> (bs->breakpoint_at);
5497 bool must_check_value = false;
5499 if (b->type == bp_watchpoint)
5500 /* For a software watchpoint, we must always check the
5501 watched value. */
5502 must_check_value = true;
5503 else if (b->watchpoint_triggered == watch_triggered_yes)
5504 /* We have a hardware watchpoint (read, write, or access)
5505 and the target earlier reported an address watched by
5506 this watchpoint. */
5507 must_check_value = true;
5508 else if (b->watchpoint_triggered == watch_triggered_unknown
5509 && b->type == bp_hardware_watchpoint)
5510 /* We were stopped by a hardware watchpoint, but the target could
5511 not report the data address. We must check the watchpoint's
5512 value. Access and read watchpoints are out of luck; without
5513 a data address, we can't figure it out. */
5514 must_check_value = true;
5516 if (must_check_value)
5518 wp_check_result e;
5522 e = watchpoint_check (bs);
5524 catch (const gdb_exception_error &ex)
5526 exception_fprintf (gdb_stderr, ex,
5527 "Error evaluating expression "
5528 "for watchpoint %d\n",
5529 b->number);
5531 SWITCH_THRU_ALL_UIS ()
5533 gdb_printf (_("Watchpoint %d deleted.\n"),
5534 b->number);
5536 watchpoint_del_at_next_stop (b);
5537 e = WP_DELETED;
5540 switch (e)
5542 case WP_DELETED:
5543 /* We've already printed what needs to be printed. */
5544 bs->print_it = print_it_done;
5545 /* Stop. */
5546 break;
5547 case WP_IGNORE:
5548 bs->print_it = print_it_noop;
5549 bs->stop = false;
5550 break;
5551 case WP_VALUE_CHANGED:
5552 if (b->type == bp_read_watchpoint)
5554 /* There are two cases to consider here:
5556 1. We're watching the triggered memory for reads.
5557 In that case, trust the target, and always report
5558 the watchpoint hit to the user. Even though
5559 reads don't cause value changes, the value may
5560 have changed since the last time it was read, and
5561 since we're not trapping writes, we will not see
5562 those, and as such we should ignore our notion of
5563 old value.
5565 2. We're watching the triggered memory for both
5566 reads and writes. There are two ways this may
5567 happen:
5569 2.1. This is a target that can't break on data
5570 reads only, but can break on accesses (reads or
5571 writes), such as e.g., x86. We detect this case
5572 at the time we try to insert read watchpoints.
5574 2.2. Otherwise, the target supports read
5575 watchpoints, but, the user set an access or write
5576 watchpoint watching the same memory as this read
5577 watchpoint.
5579 If we're watching memory writes as well as reads,
5580 ignore watchpoint hits when we find that the
5581 value hasn't changed, as reads don't cause
5582 changes. This still gives false positives when
5583 the program writes the same value to memory as
5584 what there was already in memory (we will confuse
5585 it for a read), but it's much better than
5586 nothing. */
5588 int other_write_watchpoint = 0;
5590 if (bl->watchpoint_type == hw_read)
5592 for (breakpoint &other_b : all_breakpoints ())
5593 if (other_b.type == bp_hardware_watchpoint
5594 || other_b.type == bp_access_watchpoint)
5596 watchpoint &other_w =
5597 gdb::checked_static_cast<watchpoint &> (other_b);
5599 if (other_w.watchpoint_triggered
5600 == watch_triggered_yes)
5602 other_write_watchpoint = 1;
5603 break;
5608 if (other_write_watchpoint
5609 || bl->watchpoint_type == hw_access)
5611 /* We're watching the same memory for writes,
5612 and the value changed since the last time we
5613 updated it, so this trap must be for a write.
5614 Ignore it. */
5615 bs->print_it = print_it_noop;
5616 bs->stop = false;
5619 break;
5620 case WP_VALUE_NOT_CHANGED:
5621 if (b->type == bp_hardware_watchpoint
5622 || b->type == bp_watchpoint)
5624 /* Don't stop: write watchpoints shouldn't fire if
5625 the value hasn't changed. */
5626 bs->print_it = print_it_noop;
5627 bs->stop = false;
5629 /* Stop. */
5630 break;
5631 default:
5632 /* Can't happen. */
5633 break;
5636 else /* !must_check_value */
5638 /* This is a case where some watchpoint(s) triggered, but
5639 not at the address of this watchpoint, or else no
5640 watchpoint triggered after all. So don't print
5641 anything for this watchpoint. */
5642 bs->print_it = print_it_noop;
5643 bs->stop = false;
5647 /* For breakpoints that are currently marked as telling gdb to stop,
5648 check conditions (condition proper, frame, thread and ignore count)
5649 of breakpoint referred to by BS. If we should not stop for this
5650 breakpoint, set BS->stop to 0. */
5652 static void
5653 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5655 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
5657 const struct bp_location *bl;
5658 struct breakpoint *b;
5659 /* Assume stop. */
5660 bool condition_result = true;
5661 struct expression *cond;
5663 gdb_assert (bs->stop);
5665 /* BS is built for existing struct breakpoint. */
5666 bl = bs->bp_location_at.get ();
5667 gdb_assert (bl != NULL);
5668 b = bs->breakpoint_at;
5669 gdb_assert (b != NULL);
5671 infrun_debug_printf ("thread = %s, breakpoint %d.%d",
5672 thread->ptid.to_string ().c_str (),
5673 b->number, find_loc_num_by_location (bl));
5675 /* Even if the target evaluated the condition on its end and notified GDB, we
5676 need to do so again since GDB does not know if we stopped due to a
5677 breakpoint or a single step breakpoint. */
5679 if (frame_id_p (b->frame_id)
5680 && b->frame_id != get_stack_frame_id (get_current_frame ()))
5682 infrun_debug_printf ("incorrect frame %s not %s, not stopping",
5683 get_stack_frame_id (get_current_frame ()).to_string ().c_str (),
5684 b->frame_id.to_string ().c_str ());
5685 bs->stop = false;
5686 return;
5689 /* If this is a thread/task-specific breakpoint, don't waste cpu
5690 evaluating the condition if this isn't the specified
5691 thread/task. */
5692 if ((b->thread != -1 && b->thread != thread->global_num)
5693 || (b->inferior != -1 && b->inferior != thread->inf->num)
5694 || (b->task != -1 && b->task != ada_get_task_number (thread)))
5696 infrun_debug_printf ("incorrect thread or task, not stopping");
5697 bs->stop = false;
5698 return;
5701 /* Evaluate extension language breakpoints that have a "stop" method
5702 implemented. */
5703 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5705 if (is_watchpoint (b))
5707 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
5709 cond = w->cond_exp.get ();
5711 else
5712 cond = bl->cond.get ();
5714 if (cond != nullptr && b->disposition != disp_del_at_next_stop)
5716 bool within_current_scope = true;
5718 /* We use scoped_value_mark because it could be a long time
5719 before we return to the command level and call
5720 free_all_values. We can't call free_all_values because we
5721 might be in the middle of evaluating a function call. */
5722 scoped_value_mark mark;
5724 watchpoint *w = nullptr;
5725 if (is_watchpoint (b))
5726 w = gdb::checked_static_cast<watchpoint *> (b);
5728 /* Need to select the frame, with all that implies so that
5729 the conditions will have the right context. Because we
5730 use the frame, we will not see an inlined function's
5731 variables when we arrive at a breakpoint at the start
5732 of the inlined function; the current frame will be the
5733 call site. */
5734 if (w == NULL || w->cond_exp_valid_block == NULL)
5735 select_frame (get_current_frame ());
5736 else
5738 frame_info_ptr frame;
5740 /* For local watchpoint expressions, which particular
5741 instance of a local is being watched matters, so we
5742 keep track of the frame to evaluate the expression
5743 in. To evaluate the condition however, it doesn't
5744 really matter which instantiation of the function
5745 where the condition makes sense triggers the
5746 watchpoint. This allows an expression like "watch
5747 global if q > 10" set in `func', catch writes to
5748 global on all threads that call `func', or catch
5749 writes on all recursive calls of `func' by a single
5750 thread. We simply always evaluate the condition in
5751 the innermost frame that's executing where it makes
5752 sense to evaluate the condition. It seems
5753 intuitive. */
5754 frame = block_innermost_frame (w->cond_exp_valid_block);
5755 if (frame != NULL)
5756 select_frame (frame);
5757 else
5758 within_current_scope = false;
5760 if (within_current_scope)
5764 scoped_restore reset_in_cond_eval
5765 = make_scoped_restore (&thread->control.in_cond_eval, true);
5766 condition_result = breakpoint_cond_eval (cond);
5768 catch (const gdb_exception_error &ex)
5770 int locno = bpstat_locno (bs);
5771 if (locno != 0)
5772 exception_fprintf
5773 (gdb_stderr, ex,
5774 "Error in testing condition for breakpoint %d.%d:\n",
5775 b->number, locno);
5776 else
5777 exception_fprintf
5778 (gdb_stderr, ex,
5779 "Error in testing condition for breakpoint %d:\n",
5780 b->number);
5783 else
5785 warning (_("Watchpoint condition cannot be tested "
5786 "in the current scope"));
5787 /* If we failed to set the right context for this
5788 watchpoint, unconditionally report it. */
5790 /* FIXME-someday, should give breakpoint #. */
5793 if (cond != nullptr && !condition_result)
5795 infrun_debug_printf ("condition_result = false, not stopping");
5796 bs->stop = false;
5797 return;
5799 else if (b->ignore_count > 0)
5801 infrun_debug_printf ("ignore count %d, not stopping",
5802 b->ignore_count);
5803 b->ignore_count--;
5804 bs->stop = false;
5805 /* Increase the hit count even though we don't stop. */
5806 ++(b->hit_count);
5807 notify_breakpoint_modified (b);
5808 return;
5811 if (bs->stop)
5812 infrun_debug_printf ("stopping at this breakpoint");
5813 else
5814 infrun_debug_printf ("not stopping at this breakpoint");
5817 /* Returns true if we need to track moribund locations of LOC's type
5818 on the current target. */
5820 static bool
5821 need_moribund_for_location_type (const struct bp_location *loc)
5823 return ((loc->loc_type == bp_loc_software_breakpoint
5824 && !target_supports_stopped_by_sw_breakpoint ())
5825 || (loc->loc_type == bp_loc_hardware_breakpoint
5826 && !target_supports_stopped_by_hw_breakpoint ()));
5829 /* See breakpoint.h. */
5831 bpstat *
5832 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5833 const target_waitstatus &ws)
5835 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5837 for (breakpoint &b : all_breakpoints ())
5839 if (!breakpoint_enabled (&b))
5840 continue;
5842 for (bp_location &bl : b.locations ())
5844 /* For hardware watchpoints, we look only at the first
5845 location. The watchpoint_check function will work on the
5846 entire expression, not the individual locations. For
5847 read watchpoints, the watchpoints_triggered function has
5848 checked all locations already. */
5849 if (b.type == bp_hardware_watchpoint && &bl != &b.first_loc ())
5850 break;
5852 if (!bl.enabled || bl.disabled_by_cond || bl.shlib_disabled)
5853 continue;
5855 if (!bpstat_check_location (&bl, aspace, bp_addr, ws))
5856 continue;
5858 /* Come here if it's a watchpoint, or if the break address
5859 matches. */
5861 bpstat *bs = new bpstat (&bl, &bs_link); /* Alloc a bpstat to
5862 explain stop. */
5864 /* Assume we stop. Should we find a watchpoint that is not
5865 actually triggered, or if the condition of the breakpoint
5866 evaluates as false, we'll reset 'stop' to 0. */
5867 bs->stop = true;
5868 bs->print = true;
5870 /* If this is a scope breakpoint, mark the associated
5871 watchpoint as triggered so that we will handle the
5872 out-of-scope event. We'll get to the watchpoint next
5873 iteration. */
5874 if (b.type == bp_watchpoint_scope && b.related_breakpoint != &b)
5876 watchpoint *w
5877 = gdb::checked_static_cast<watchpoint *> (b.related_breakpoint);
5879 w->watchpoint_triggered = watch_triggered_yes;
5884 /* Check if a moribund breakpoint explains the stop. */
5885 if (!target_supports_stopped_by_sw_breakpoint ()
5886 || !target_supports_stopped_by_hw_breakpoint ())
5888 for (bp_location *loc : moribund_locations)
5890 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5891 && need_moribund_for_location_type (loc))
5893 bpstat *bs = new bpstat (loc, &bs_link);
5894 /* For hits of moribund locations, we should just proceed. */
5895 bs->stop = false;
5896 bs->print = false;
5897 bs->print_it = print_it_noop;
5902 return bs_head;
5905 /* See breakpoint.h. */
5907 bpstat *
5908 bpstat_stop_status (const address_space *aspace,
5909 CORE_ADDR bp_addr, thread_info *thread,
5910 const target_waitstatus &ws,
5911 bpstat *stop_chain)
5913 struct breakpoint *b = NULL;
5914 /* First item of allocated bpstat's. */
5915 bpstat *bs_head = stop_chain;
5916 bpstat *bs;
5917 int need_remove_insert;
5918 int removed_any;
5920 /* First, build the bpstat chain with locations that explain a
5921 target stop, while being careful to not set the target running,
5922 as that may invalidate locations (in particular watchpoint
5923 locations are recreated). Resuming will happen here with
5924 breakpoint conditions or watchpoint expressions that include
5925 inferior function calls. */
5926 if (bs_head == NULL)
5927 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5929 /* A bit of special processing for shlib breakpoints. We need to
5930 process solib loading here, so that the lists of loaded and
5931 unloaded libraries are correct before we handle "catch load" and
5932 "catch unload". */
5933 for (bs = bs_head; bs != NULL; bs = bs->next)
5935 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5937 handle_solib_event ();
5938 break;
5942 /* Now go through the locations that caused the target to stop, and
5943 check whether we're interested in reporting this stop to higher
5944 layers, or whether we should resume the target transparently. */
5946 removed_any = 0;
5948 for (bs = bs_head; bs != NULL; bs = bs->next)
5950 if (!bs->stop)
5951 continue;
5953 b = bs->breakpoint_at;
5954 b->check_status (bs);
5955 if (bs->stop)
5957 bpstat_check_breakpoint_conditions (bs, thread);
5959 if (bs->stop)
5961 ++(b->hit_count);
5963 /* We will stop here. */
5964 if (b->disposition == disp_disable)
5966 --(b->enable_count);
5967 if (b->enable_count <= 0)
5968 b->enable_state = bp_disabled;
5969 removed_any = 1;
5971 notify_breakpoint_modified (b);
5972 if (b->silent)
5973 bs->print = false;
5974 bs->commands = b->commands;
5975 if (command_line_is_silent (bs->commands
5976 ? bs->commands.get () : NULL))
5977 bs->print = false;
5979 b->after_condition_true (bs);
5984 /* Print nothing for this entry if we don't stop or don't
5985 print. */
5986 if (!bs->stop || !bs->print)
5987 bs->print_it = print_it_noop;
5990 /* If we aren't stopping, the value of some hardware watchpoint may
5991 not have changed, but the intermediate memory locations we are
5992 watching may have. Don't bother if we're stopping; this will get
5993 done later. */
5994 need_remove_insert = 0;
5995 if (! bpstat_causes_stop (bs_head))
5996 for (bs = bs_head; bs != NULL; bs = bs->next)
5997 if (!bs->stop
5998 && bs->breakpoint_at
5999 && is_hardware_watchpoint (bs->breakpoint_at))
6001 watchpoint *w
6002 = gdb::checked_static_cast<watchpoint *> (bs->breakpoint_at);
6004 update_watchpoint (w, false /* don't reparse. */);
6005 need_remove_insert = 1;
6008 if (need_remove_insert)
6009 update_global_location_list (UGLL_MAY_INSERT);
6010 else if (removed_any)
6011 update_global_location_list (UGLL_DONT_INSERT);
6013 return bs_head;
6016 /* See breakpoint.h. */
6018 bpstat *
6019 bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
6020 thread_info *thread, const target_waitstatus &ws)
6022 gdb_assert (!target_stopped_by_watchpoint ());
6024 /* Clear all watchpoints' 'watchpoint_triggered' value from a
6025 previous stop to avoid confusing bpstat_stop_status. */
6026 watchpoints_triggered (ws);
6028 return bpstat_stop_status (aspace, bp_addr, thread, ws);
6031 static void
6032 handle_jit_event (CORE_ADDR address)
6034 struct gdbarch *gdbarch;
6036 infrun_debug_printf ("handling bp_jit_event");
6038 /* Switch terminal for any messages produced by
6039 breakpoint_re_set. */
6040 target_terminal::ours_for_output ();
6042 gdbarch = get_frame_arch (get_current_frame ());
6043 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
6044 thus it is expected that its objectfile can be found through
6045 minimal symbol lookup. If it doesn't work (and assert fails), it
6046 most likely means that `jit_breakpoint_re_set` was changes and this
6047 function needs to be updated too. */
6048 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
6049 gdb_assert (jit_bp_sym.objfile != nullptr);
6050 objfile *objfile = jit_bp_sym.objfile;
6051 if (objfile->separate_debug_objfile_backlink)
6052 objfile = objfile->separate_debug_objfile_backlink;
6053 jit_event_handler (gdbarch, objfile);
6055 target_terminal::inferior ();
6058 /* Prepare WHAT final decision for infrun. */
6060 /* Decide what infrun needs to do with this bpstat. */
6062 struct bpstat_what
6063 bpstat_what (bpstat *bs_head)
6065 struct bpstat_what retval;
6066 bpstat *bs;
6068 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
6069 retval.call_dummy = STOP_NONE;
6070 retval.is_longjmp = false;
6072 for (bs = bs_head; bs != NULL; bs = bs->next)
6074 /* Extract this BS's action. After processing each BS, we check
6075 if its action overrides all we've seem so far. */
6076 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
6077 enum bptype bptype;
6079 if (bs->breakpoint_at == NULL)
6081 /* I suspect this can happen if it was a momentary
6082 breakpoint which has since been deleted. */
6083 bptype = bp_none;
6085 else
6086 bptype = bs->breakpoint_at->type;
6088 switch (bptype)
6090 case bp_none:
6091 break;
6092 case bp_breakpoint:
6093 case bp_hardware_breakpoint:
6094 case bp_single_step:
6095 case bp_until:
6096 case bp_finish:
6097 case bp_shlib_event:
6098 if (bs->stop)
6100 if (bs->print)
6101 this_action = BPSTAT_WHAT_STOP_NOISY;
6102 else
6103 this_action = BPSTAT_WHAT_STOP_SILENT;
6105 else
6106 this_action = BPSTAT_WHAT_SINGLE;
6107 break;
6108 case bp_watchpoint:
6109 case bp_hardware_watchpoint:
6110 case bp_read_watchpoint:
6111 case bp_access_watchpoint:
6112 if (bs->stop)
6114 if (bs->print)
6115 this_action = BPSTAT_WHAT_STOP_NOISY;
6116 else
6117 this_action = BPSTAT_WHAT_STOP_SILENT;
6119 else
6121 /* There was a watchpoint, but we're not stopping.
6122 This requires no further action. */
6124 break;
6125 case bp_longjmp:
6126 case bp_longjmp_call_dummy:
6127 case bp_exception:
6128 if (bs->stop)
6130 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
6131 retval.is_longjmp = bptype != bp_exception;
6133 else
6134 this_action = BPSTAT_WHAT_SINGLE;
6135 break;
6136 case bp_longjmp_resume:
6137 case bp_exception_resume:
6138 if (bs->stop)
6140 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
6141 retval.is_longjmp = bptype == bp_longjmp_resume;
6143 else
6144 this_action = BPSTAT_WHAT_SINGLE;
6145 break;
6146 case bp_step_resume:
6147 if (bs->stop)
6148 this_action = BPSTAT_WHAT_STEP_RESUME;
6149 else
6151 /* It is for the wrong frame. */
6152 this_action = BPSTAT_WHAT_SINGLE;
6154 break;
6155 case bp_hp_step_resume:
6156 if (bs->stop)
6157 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
6158 else
6160 /* It is for the wrong frame. */
6161 this_action = BPSTAT_WHAT_SINGLE;
6163 break;
6164 case bp_watchpoint_scope:
6165 case bp_thread_event:
6166 case bp_overlay_event:
6167 case bp_longjmp_master:
6168 case bp_std_terminate_master:
6169 case bp_exception_master:
6170 this_action = BPSTAT_WHAT_SINGLE;
6171 break;
6172 case bp_catchpoint:
6173 if (bs->stop)
6175 if (bs->print)
6176 this_action = BPSTAT_WHAT_STOP_NOISY;
6177 else
6178 this_action = BPSTAT_WHAT_STOP_SILENT;
6180 else
6182 /* Some catchpoints are implemented with breakpoints.
6183 For those, we need to step over the breakpoint. */
6184 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
6185 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
6186 this_action = BPSTAT_WHAT_SINGLE;
6188 break;
6189 case bp_jit_event:
6190 this_action = BPSTAT_WHAT_SINGLE;
6191 break;
6192 case bp_call_dummy:
6193 /* Make sure the action is stop (silent or noisy),
6194 so infrun.c pops the dummy frame. */
6195 retval.call_dummy = STOP_STACK_DUMMY;
6196 this_action = BPSTAT_WHAT_STOP_SILENT;
6197 break;
6198 case bp_std_terminate:
6199 /* Make sure the action is stop (silent or noisy),
6200 so infrun.c pops the dummy frame. */
6201 retval.call_dummy = STOP_STD_TERMINATE;
6202 this_action = BPSTAT_WHAT_STOP_SILENT;
6203 break;
6204 case bp_tracepoint:
6205 case bp_fast_tracepoint:
6206 case bp_static_tracepoint:
6207 case bp_static_marker_tracepoint:
6208 /* Tracepoint hits should not be reported back to GDB, and
6209 if one got through somehow, it should have been filtered
6210 out already. */
6211 internal_error (_("bpstat_what: tracepoint encountered"));
6212 break;
6213 case bp_gnu_ifunc_resolver:
6214 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
6215 this_action = BPSTAT_WHAT_SINGLE;
6216 break;
6217 case bp_gnu_ifunc_resolver_return:
6218 /* The breakpoint will be removed, execution will restart from the
6219 PC of the former breakpoint. */
6220 this_action = BPSTAT_WHAT_KEEP_CHECKING;
6221 break;
6223 case bp_dprintf:
6224 if (bs->stop)
6225 this_action = BPSTAT_WHAT_STOP_SILENT;
6226 else
6227 this_action = BPSTAT_WHAT_SINGLE;
6228 break;
6230 default:
6231 internal_error (_("bpstat_what: unhandled bptype %d"), (int) bptype);
6234 retval.main_action = std::max (retval.main_action, this_action);
6237 return retval;
6240 void
6241 bpstat_run_callbacks (bpstat *bs_head)
6243 bpstat *bs;
6245 for (bs = bs_head; bs != NULL; bs = bs->next)
6247 struct breakpoint *b = bs->breakpoint_at;
6249 if (b == NULL)
6250 continue;
6251 switch (b->type)
6253 case bp_jit_event:
6254 handle_jit_event (bs->bp_location_at->address);
6255 break;
6256 case bp_gnu_ifunc_resolver:
6257 gnu_ifunc_resolver_stop
6258 (gdb::checked_static_cast<code_breakpoint *> (b));
6259 break;
6260 case bp_gnu_ifunc_resolver_return:
6261 gnu_ifunc_resolver_return_stop
6262 (gdb::checked_static_cast<code_breakpoint *> (b));
6263 break;
6268 /* See breakpoint.h. */
6270 bool
6271 bpstat_should_step ()
6273 for (breakpoint &b : all_breakpoints ())
6274 if (breakpoint_enabled (&b)
6275 && b.type == bp_watchpoint
6276 && b.has_locations ())
6277 return true;
6279 return false;
6282 /* See breakpoint.h. */
6284 bool
6285 bpstat_causes_stop (bpstat *bs)
6287 for (; bs != NULL; bs = bs->next)
6288 if (bs->stop)
6289 return true;
6291 return false;
6296 /* Compute a number of spaces suitable to indent the next line
6297 so it starts at the position corresponding to the table column
6298 named COL_NAME in the currently active table of UIOUT. */
6300 static int
6301 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6303 int i, total_width, width, align;
6304 const char *text;
6306 total_width = 0;
6307 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
6309 if (strcmp (text, col_name) == 0)
6310 return total_width;
6312 total_width += width + 1;
6315 return 0;
6318 /* Determine if the locations of this breakpoint will have their conditions
6319 evaluated by the target, host or a mix of both. Returns the following:
6321 "host": Host evals condition.
6322 "host or target": Host or Target evals condition.
6323 "target": Target evals condition.
6326 static const char *
6327 bp_condition_evaluator (const breakpoint *b)
6329 char host_evals = 0;
6330 char target_evals = 0;
6332 if (!b)
6333 return NULL;
6335 if (!is_breakpoint (b))
6336 return NULL;
6338 if (gdb_evaluates_breakpoint_condition_p ()
6339 || !target_supports_evaluation_of_breakpoint_conditions ())
6340 return condition_evaluation_host;
6342 for (bp_location &bl : b->locations ())
6344 if (bl.cond_bytecode)
6345 target_evals++;
6346 else
6347 host_evals++;
6350 if (host_evals && target_evals)
6351 return condition_evaluation_both;
6352 else if (target_evals)
6353 return condition_evaluation_target;
6354 else
6355 return condition_evaluation_host;
6358 /* Determine the breakpoint location's condition evaluator. This is
6359 similar to bp_condition_evaluator, but for locations. */
6361 static const char *
6362 bp_location_condition_evaluator (const struct bp_location *bl)
6364 if (bl && !is_breakpoint (bl->owner))
6365 return NULL;
6367 if (gdb_evaluates_breakpoint_condition_p ()
6368 || !target_supports_evaluation_of_breakpoint_conditions ())
6369 return condition_evaluation_host;
6371 if (bl && bl->cond_bytecode)
6372 return condition_evaluation_target;
6373 else
6374 return condition_evaluation_host;
6377 /* Print the LOC location out of the list of B->LOC locations. */
6379 static void
6380 print_breakpoint_location (const breakpoint *b, const bp_location *loc)
6382 struct ui_out *uiout = current_uiout;
6384 scoped_restore_current_program_space restore_pspace;
6386 if (loc != NULL && loc->shlib_disabled)
6387 loc = NULL;
6389 if (loc != NULL)
6390 set_current_program_space (loc->pspace);
6392 if (b->display_canonical)
6393 uiout->field_string ("what", b->locspec->to_string ());
6394 else if (loc && loc->symtab)
6396 const struct symbol *sym = loc->symbol;
6398 if (sym)
6400 uiout->text ("in ");
6401 uiout->field_string ("func", sym->print_name (),
6402 function_name_style.style ());
6403 uiout->text (" ");
6404 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6405 uiout->text ("at ");
6407 uiout->field_string ("file",
6408 symtab_to_filename_for_display (loc->symtab),
6409 file_name_style.style ());
6410 uiout->text (":");
6412 if (uiout->is_mi_like_p ())
6413 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6415 uiout->field_signed ("line", loc->line_number,
6416 line_number_style.style ());
6418 else if (loc)
6420 string_file stb;
6422 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6423 demangle, "");
6424 uiout->field_stream ("at", stb);
6426 else
6427 uiout->field_string ("pending", b->locspec->to_string ());
6429 if (loc && is_breakpoint (b)
6430 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6431 && bp_condition_evaluator (b) == condition_evaluation_both)
6433 uiout->text (" (");
6434 uiout->field_string ("evaluated-by",
6435 bp_location_condition_evaluator (loc));
6436 uiout->text (")");
6440 static const char *
6441 bptype_string (enum bptype type)
6443 struct ep_type_description
6445 enum bptype type;
6446 const char *description;
6448 static struct ep_type_description bptypes[] =
6450 {bp_none, "?deleted?"},
6451 {bp_breakpoint, "breakpoint"},
6452 {bp_hardware_breakpoint, "hw breakpoint"},
6453 {bp_single_step, "sw single-step"},
6454 {bp_until, "until"},
6455 {bp_finish, "finish"},
6456 {bp_watchpoint, "watchpoint"},
6457 {bp_hardware_watchpoint, "hw watchpoint"},
6458 {bp_read_watchpoint, "read watchpoint"},
6459 {bp_access_watchpoint, "acc watchpoint"},
6460 {bp_longjmp, "longjmp"},
6461 {bp_longjmp_resume, "longjmp resume"},
6462 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6463 {bp_exception, "exception"},
6464 {bp_exception_resume, "exception resume"},
6465 {bp_step_resume, "step resume"},
6466 {bp_hp_step_resume, "high-priority step resume"},
6467 {bp_watchpoint_scope, "watchpoint scope"},
6468 {bp_call_dummy, "call dummy"},
6469 {bp_std_terminate, "std::terminate"},
6470 {bp_shlib_event, "shlib events"},
6471 {bp_thread_event, "thread events"},
6472 {bp_overlay_event, "overlay events"},
6473 {bp_longjmp_master, "longjmp master"},
6474 {bp_std_terminate_master, "std::terminate master"},
6475 {bp_exception_master, "exception master"},
6476 {bp_catchpoint, "catchpoint"},
6477 {bp_tracepoint, "tracepoint"},
6478 {bp_fast_tracepoint, "fast tracepoint"},
6479 {bp_static_tracepoint, "static tracepoint"},
6480 {bp_static_marker_tracepoint, "static marker tracepoint"},
6481 {bp_dprintf, "dprintf"},
6482 {bp_jit_event, "jit events"},
6483 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6484 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6487 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6488 || ((int) type != bptypes[(int) type].type))
6489 internal_error (_("bptypes table does not describe type #%d."),
6490 (int) type);
6492 return bptypes[(int) type].description;
6495 /* For MI, output a field named 'thread-groups' with a list as the value.
6496 For CLI, prefix the list with the string 'inf'. */
6498 static void
6499 output_thread_groups (struct ui_out *uiout,
6500 const char *field_name,
6501 const std::vector<int> &inf_nums,
6502 int mi_only)
6504 int is_mi = uiout->is_mi_like_p ();
6506 /* For backward compatibility, don't display inferiors in CLI unless
6507 there are several. Always display them for MI. */
6508 if (!is_mi && mi_only)
6509 return;
6511 ui_out_emit_list list_emitter (uiout, field_name);
6513 for (size_t i = 0; i < inf_nums.size (); i++)
6515 if (is_mi)
6517 char mi_group[10];
6519 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6520 uiout->field_string (NULL, mi_group);
6522 else
6524 if (i == 0)
6525 uiout->text (" inf ");
6526 else
6527 uiout->text (", ");
6529 uiout->text (plongest (inf_nums[i]));
6534 /* See breakpoint.h. */
6536 bool fix_breakpoint_script_output_globally = false;
6538 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6539 instead of going via breakpoint_ops::print_one. This makes "maint
6540 info breakpoints" show the software breakpoint locations of
6541 catchpoints, which are considered internal implementation
6542 detail. Returns true if RAW_LOC is false and if the breakpoint's
6543 print_one method did something; false otherwise. */
6545 static bool
6546 print_one_breakpoint_location (struct breakpoint *b,
6547 struct bp_location *loc,
6548 int loc_number,
6549 const bp_location **last_loc,
6550 int allflag, bool raw_loc)
6552 struct command_line *l;
6553 static char bpenables[] = "nynny";
6555 struct ui_out *uiout = current_uiout;
6556 bool header_of_multiple = false;
6557 bool part_of_multiple = (loc != NULL);
6558 struct value_print_options opts;
6560 get_user_print_options (&opts);
6562 gdb_assert (!loc || loc_number != 0);
6563 /* See comment in print_one_breakpoint concerning treatment of
6564 breakpoints with single disabled location. */
6565 if (loc == NULL
6566 && (b->has_locations ()
6567 && (b->has_multiple_locations ()
6568 || !b->first_loc ().enabled || b->first_loc ().disabled_by_cond)))
6569 header_of_multiple = true;
6571 if (loc == NULL && b->has_locations ())
6572 loc = &b->first_loc ();
6574 annotate_record ();
6576 /* 1 */
6577 annotate_field (0);
6578 if (part_of_multiple)
6579 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6580 else
6581 uiout->field_signed ("number", b->number);
6583 /* 2 */
6584 annotate_field (1);
6585 if (part_of_multiple)
6586 uiout->field_skip ("type");
6587 else
6588 uiout->field_string ("type", bptype_string (b->type));
6590 /* 3 */
6591 annotate_field (2);
6592 if (part_of_multiple)
6593 uiout->field_skip ("disp");
6594 else
6595 uiout->field_string ("disp", bpdisp_text (b->disposition));
6597 /* 4 */
6598 annotate_field (3);
6599 if (part_of_multiple)
6601 /* For locations that are disabled because of an invalid
6602 condition, display "N*" on the CLI, where "*" refers to a
6603 footnote below the table. For MI, simply display a "N"
6604 without a footnote. On the CLI, for enabled locations whose
6605 breakpoint is disabled, display "y-". */
6606 auto get_enable_state = [uiout, loc] () -> const char *
6608 if (uiout->is_mi_like_p ())
6610 if (loc->disabled_by_cond)
6611 return "N";
6612 else if (!loc->enabled)
6613 return "n";
6614 else
6615 return "y";
6617 else
6619 if (loc->disabled_by_cond)
6620 return "N*";
6621 else if (!loc->enabled)
6622 return "n";
6623 else if (!breakpoint_enabled (loc->owner))
6624 return "y-";
6625 else
6626 return "y";
6629 uiout->field_string ("enabled", get_enable_state ());
6631 else
6632 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6634 /* 5 and 6 */
6635 bool result = false;
6636 if (!raw_loc && b->print_one (last_loc))
6637 result = true;
6638 else
6640 if (is_watchpoint (b))
6642 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
6644 /* Field 4, the address, is omitted (which makes the columns
6645 not line up too nicely with the headers, but the effect
6646 is relatively readable). */
6647 if (opts.addressprint)
6648 uiout->field_skip ("addr");
6649 annotate_field (5);
6650 uiout->field_string ("what", w->exp_string.get ());
6652 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6653 || is_ada_exception_catchpoint (b))
6655 if (opts.addressprint)
6657 annotate_field (4);
6658 if (header_of_multiple)
6659 uiout->field_string ("addr", "<MULTIPLE>",
6660 metadata_style.style ());
6661 else if (!b->has_locations () || loc->shlib_disabled)
6662 uiout->field_string ("addr", "<PENDING>",
6663 metadata_style.style ());
6664 else
6665 uiout->field_core_addr ("addr",
6666 loc->gdbarch, loc->address);
6668 annotate_field (5);
6669 if (!header_of_multiple)
6670 print_breakpoint_location (b, loc);
6671 if (b->has_locations ())
6672 *last_loc = &b->first_loc ();
6676 if (loc != nullptr && !header_of_multiple && !loc->shlib_disabled)
6678 std::vector<int> inf_nums;
6679 int mi_only = 1;
6681 for (inferior *inf : all_inferiors ())
6683 if (inf->pspace == loc->pspace)
6684 inf_nums.push_back (inf->num);
6687 /* For backward compatibility, don't display inferiors in CLI unless
6688 there are several. Always display for MI. */
6689 if (allflag
6690 || (!gdbarch_has_global_breakpoints (current_inferior ()->arch ())
6691 && (program_spaces.size () > 1
6692 || number_of_inferiors () > 1)
6693 /* LOC is for existing B, it cannot be in
6694 moribund_locations and thus having NULL OWNER. */
6695 && loc->owner->type != bp_catchpoint))
6696 mi_only = 0;
6697 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6700 /* In the MI output, each location of a thread or task specific
6701 breakpoint includes the relevant thread or task ID. This is done for
6702 backwards compatibility reasons.
6704 For the CLI output, the thread/task information is printed on a
6705 separate line, see the 'stop only in thread' and 'stop only in task'
6706 output below. */
6707 if (part_of_multiple && uiout->is_mi_like_p ())
6709 if (b->thread != -1)
6710 uiout->field_signed ("thread", b->thread);
6711 else if (b->task != -1)
6712 uiout->field_signed ("task", b->task);
6713 else if (b->inferior != -1)
6714 uiout->field_signed ("inferior", b->inferior);
6717 uiout->text ("\n");
6719 if (!part_of_multiple)
6720 b->print_one_detail (uiout);
6722 if (part_of_multiple && frame_id_p (b->frame_id))
6724 annotate_field (6);
6725 uiout->text ("\tstop only in stack frame at ");
6726 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6727 the frame ID. */
6728 uiout->field_core_addr ("frame",
6729 b->gdbarch, b->frame_id.stack_addr);
6730 uiout->text ("\n");
6733 if (!part_of_multiple && b->cond_string)
6735 annotate_field (7);
6736 if (is_tracepoint (b))
6737 uiout->text ("\ttrace only if ");
6738 else
6739 uiout->text ("\tstop only if ");
6740 uiout->field_string ("cond", b->cond_string.get ());
6742 /* Print whether the target is doing the breakpoint's condition
6743 evaluation. If GDB is doing the evaluation, don't print anything. */
6744 if (is_breakpoint (b)
6745 && breakpoint_condition_evaluation_mode ()
6746 == condition_evaluation_target)
6748 uiout->message (" (%pF evals)",
6749 string_field ("evaluated-by",
6750 bp_condition_evaluator (b)));
6752 uiout->text ("\n");
6755 if (!part_of_multiple && b->thread != -1)
6757 /* FIXME should make an annotation for this. */
6758 uiout->text ("\tstop only in thread ");
6759 if (uiout->is_mi_like_p ())
6760 uiout->field_signed ("thread", b->thread);
6761 else
6763 struct thread_info *thr = find_thread_global_id (b->thread);
6765 uiout->field_string ("thread", print_thread_id (thr));
6767 uiout->text ("\n");
6770 if (!part_of_multiple && b->task != -1)
6772 uiout->text ("\tstop only in task ");
6773 uiout->field_signed ("task", b->task);
6774 uiout->text ("\n");
6777 if (!part_of_multiple && b->inferior != -1)
6779 uiout->text ("\tstop only in inferior ");
6780 uiout->field_signed ("inferior", b->inferior);
6781 uiout->text ("\n");
6784 if (!part_of_multiple)
6786 if (b->hit_count)
6788 /* FIXME should make an annotation for this. */
6789 if (is_catchpoint (b))
6790 uiout->text ("\tcatchpoint");
6791 else if (is_tracepoint (b))
6792 uiout->text ("\ttracepoint");
6793 else
6794 uiout->text ("\tbreakpoint");
6795 uiout->text (" already hit ");
6796 uiout->field_signed ("times", b->hit_count);
6797 if (b->hit_count == 1)
6798 uiout->text (" time\n");
6799 else
6800 uiout->text (" times\n");
6802 else
6804 /* Output the count also if it is zero, but only if this is mi. */
6805 if (uiout->is_mi_like_p ())
6806 uiout->field_signed ("times", b->hit_count);
6810 if (!part_of_multiple && b->ignore_count)
6812 annotate_field (8);
6813 uiout->message ("\tignore next %pF hits\n",
6814 signed_field ("ignore", b->ignore_count));
6817 /* Note that an enable count of 1 corresponds to "enable once"
6818 behavior, which is reported by the combination of enablement and
6819 disposition, so we don't need to mention it here. */
6820 if (!part_of_multiple && b->enable_count > 1)
6822 annotate_field (8);
6823 uiout->text ("\tdisable after ");
6824 /* Tweak the wording to clarify that ignore and enable counts
6825 are distinct, and have additive effect. */
6826 if (b->ignore_count)
6827 uiout->text ("additional ");
6828 else
6829 uiout->text ("next ");
6830 uiout->field_signed ("enable", b->enable_count);
6831 uiout->text (" hits\n");
6834 if (!part_of_multiple && is_tracepoint (b))
6836 tracepoint *tp = gdb::checked_static_cast<tracepoint *> (b);
6838 if (tp->traceframe_usage)
6840 uiout->text ("\ttrace buffer usage ");
6841 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6842 uiout->text (" bytes\n");
6846 l = b->commands ? b->commands.get () : NULL;
6847 if (!part_of_multiple && l)
6849 annotate_field (9);
6851 bool use_fixed_output =
6852 (uiout->test_flags (fix_breakpoint_script_output)
6853 || fix_breakpoint_script_output_globally);
6855 std::optional<ui_out_emit_tuple> tuple_emitter;
6856 std::optional<ui_out_emit_list> list_emitter;
6858 if (use_fixed_output)
6859 list_emitter.emplace (uiout, "script");
6860 else
6861 tuple_emitter.emplace (uiout, "script");
6863 print_command_lines (uiout, l, 4);
6866 if (is_tracepoint (b))
6868 tracepoint *t = gdb::checked_static_cast<tracepoint *> (b);
6870 if (!part_of_multiple && t->pass_count)
6872 annotate_field (10);
6873 uiout->text ("\tpass count ");
6874 uiout->field_signed ("pass", t->pass_count);
6875 uiout->text (" \n");
6878 /* Don't display it when tracepoint or tracepoint location is
6879 pending. */
6880 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6882 annotate_field (11);
6884 if (uiout->is_mi_like_p ())
6885 uiout->field_string ("installed",
6886 loc->inserted ? "y" : "n");
6887 else
6889 if (loc->inserted)
6890 uiout->text ("\t");
6891 else
6892 uiout->text ("\tnot ");
6893 uiout->text ("installed on target\n");
6898 if (uiout->is_mi_like_p () && !part_of_multiple)
6900 if (is_watchpoint (b))
6902 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
6904 uiout->field_string ("original-location", w->exp_string.get ());
6906 else if (b->locspec != nullptr)
6908 const char *str = b->locspec->to_string ();
6909 if (str != nullptr)
6910 uiout->field_string ("original-location", str);
6914 return result;
6917 /* See breakpoint.h. */
6919 bool fix_multi_location_breakpoint_output_globally = false;
6921 static void
6922 print_one_breakpoint (breakpoint *b, const bp_location **last_loc, int allflag)
6924 struct ui_out *uiout = current_uiout;
6925 bool use_fixed_output
6926 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6927 || fix_multi_location_breakpoint_output_globally);
6929 std::optional<ui_out_emit_tuple> bkpt_tuple_emitter (std::in_place, uiout,
6930 "bkpt");
6931 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6932 allflag, false);
6934 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6935 are outside. */
6936 if (!use_fixed_output)
6937 bkpt_tuple_emitter.reset ();
6939 /* If this breakpoint has custom print function,
6940 it's already printed. Otherwise, print individual
6941 locations, if any. */
6942 if (!printed || allflag)
6944 /* If breakpoint has a single location that is disabled, we
6945 print it as if it had several locations, since otherwise it's
6946 hard to represent "breakpoint enabled, location disabled"
6947 situation.
6949 Note that while hardware watchpoints have several locations
6950 internally, that's not a property exposed to users.
6952 Likewise, while catchpoints may be implemented with
6953 breakpoints (e.g., catch throw), that's not a property
6954 exposed to users. We do however display the internal
6955 breakpoint locations with "maint info breakpoints". */
6956 if (!is_hardware_watchpoint (b)
6957 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6958 || is_ada_exception_catchpoint (b))
6959 && (allflag
6960 || (b->has_locations ()
6961 && (b->has_multiple_locations ()
6962 || !b->first_loc ().enabled
6963 || b->first_loc ().disabled_by_cond))))
6965 std::optional<ui_out_emit_list> locations_list;
6967 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6968 MI record. For later versions, place breakpoint locations in a
6969 list. */
6970 if (uiout->is_mi_like_p () && use_fixed_output)
6971 locations_list.emplace (uiout, "locations");
6973 int n = 1;
6974 for (bp_location &loc : b->locations ())
6976 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6977 print_one_breakpoint_location (b, &loc, n, last_loc,
6978 allflag, allflag);
6979 n++;
6985 static int
6986 breakpoint_address_bits (struct breakpoint *b)
6988 int print_address_bits = 0;
6990 for (bp_location &loc : b->locations ())
6992 if (!bl_address_is_meaningful (&loc))
6993 continue;
6995 int addr_bit = gdbarch_addr_bit (loc.gdbarch);
6996 if (addr_bit > print_address_bits)
6997 print_address_bits = addr_bit;
7000 return print_address_bits;
7003 /* See breakpoint.h. */
7005 void
7006 print_breakpoint (breakpoint *b)
7008 const bp_location *dummy_loc = nullptr;
7009 print_one_breakpoint (b, &dummy_loc, 0);
7012 /* Return true if this breakpoint was set by the user, false if it is
7013 internal or momentary. */
7016 user_breakpoint_p (const breakpoint *b)
7018 return b->number > 0;
7021 /* See breakpoint.h. */
7024 pending_breakpoint_p (const breakpoint *b)
7026 return !b->has_locations ();
7029 /* Print information on breakpoints (including watchpoints and tracepoints).
7031 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
7032 understood by number_or_range_parser. Only breakpoints included in this
7033 list are then printed.
7035 If SHOW_INTERNAL is true, print internal breakpoints.
7037 If FILTER is non-NULL, call it on each breakpoint and only include the
7038 ones for which it returns true.
7040 Return the total number of breakpoints listed. */
7042 static int
7043 breakpoint_1 (const char *bp_num_list, bool show_internal,
7044 bool (*filter) (const struct breakpoint *))
7046 const bp_location *last_loc = nullptr;
7047 int nr_printable_breakpoints;
7048 struct value_print_options opts;
7049 int print_address_bits = 0;
7050 int print_type_col_width = 14;
7051 struct ui_out *uiout = current_uiout;
7052 bool has_disabled_by_cond_location = false;
7054 get_user_print_options (&opts);
7056 /* Compute the number of rows in the table, as well as the size
7057 required for address fields. */
7058 nr_printable_breakpoints = 0;
7059 for (breakpoint &b : all_breakpoints ())
7061 /* If we have a filter, only list the breakpoints it accepts. */
7062 if (filter && !filter (&b))
7063 continue;
7065 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
7066 accept. Skip the others. */
7067 if (bp_num_list != NULL && *bp_num_list != '\0')
7069 if (show_internal && parse_and_eval_long (bp_num_list) != b.number)
7070 continue;
7071 if (!show_internal && !number_is_in_list (bp_num_list, b.number))
7072 continue;
7075 if (show_internal || user_breakpoint_p (&b))
7077 int addr_bit, type_len;
7079 addr_bit = breakpoint_address_bits (&b);
7080 if (addr_bit > print_address_bits)
7081 print_address_bits = addr_bit;
7083 type_len = strlen (bptype_string (b.type));
7084 if (type_len > print_type_col_width)
7085 print_type_col_width = type_len;
7087 nr_printable_breakpoints++;
7092 ui_out_emit_table table_emitter (uiout,
7093 opts.addressprint ? 6 : 5,
7094 nr_printable_breakpoints,
7095 "BreakpointTable");
7097 if (nr_printable_breakpoints > 0)
7098 annotate_breakpoints_headers ();
7099 if (nr_printable_breakpoints > 0)
7100 annotate_field (0);
7101 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
7102 if (nr_printable_breakpoints > 0)
7103 annotate_field (1);
7104 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
7105 if (nr_printable_breakpoints > 0)
7106 annotate_field (2);
7107 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
7108 if (nr_printable_breakpoints > 0)
7109 annotate_field (3);
7110 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
7111 if (opts.addressprint)
7113 if (nr_printable_breakpoints > 0)
7114 annotate_field (4);
7115 if (print_address_bits <= 32)
7116 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
7117 else
7118 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
7120 if (nr_printable_breakpoints > 0)
7121 annotate_field (5);
7122 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
7123 uiout->table_body ();
7124 if (nr_printable_breakpoints > 0)
7125 annotate_breakpoints_table ();
7127 for (breakpoint &b : all_breakpoints ())
7129 QUIT;
7130 /* If we have a filter, only list the breakpoints it accepts. */
7131 if (filter && !filter (&b))
7132 continue;
7134 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
7135 accept. Skip the others. */
7137 if (bp_num_list != NULL && *bp_num_list != '\0')
7139 if (show_internal) /* maintenance info breakpoint */
7141 if (parse_and_eval_long (bp_num_list) != b.number)
7142 continue;
7144 else /* all others */
7146 if (!number_is_in_list (bp_num_list, b.number))
7147 continue;
7150 /* We only print out user settable breakpoints unless the
7151 show_internal is set. */
7152 if (show_internal || user_breakpoint_p (&b))
7154 print_one_breakpoint (&b, &last_loc, show_internal);
7155 for (bp_location &loc : b.locations ())
7156 if (loc.disabled_by_cond)
7157 has_disabled_by_cond_location = true;
7162 if (nr_printable_breakpoints == 0)
7164 /* If there's a filter, let the caller decide how to report
7165 empty list. */
7166 if (!filter)
7168 if (bp_num_list == NULL || *bp_num_list == '\0')
7169 uiout->message ("No breakpoints, watchpoints, tracepoints, "
7170 "or catchpoints.\n");
7171 else
7172 uiout->message ("No breakpoint, watchpoint, tracepoint, "
7173 "or catchpoint matching '%s'.\n", bp_num_list);
7176 else
7178 if (last_loc && !server_command)
7179 set_next_address (last_loc->gdbarch, last_loc->address);
7181 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
7182 uiout->message (_("(*): Breakpoint condition is invalid at this "
7183 "location.\n"));
7186 /* FIXME? Should this be moved up so that it is only called when
7187 there have been breakpoints? */
7188 annotate_breakpoints_table_end ();
7190 return nr_printable_breakpoints;
7193 /* Display the value of default-collect in a way that is generally
7194 compatible with the breakpoint list. */
7196 static void
7197 default_collect_info (void)
7199 struct ui_out *uiout = current_uiout;
7201 /* If it has no value (which is frequently the case), say nothing; a
7202 message like "No default-collect." gets in user's face when it's
7203 not wanted. */
7204 if (default_collect.empty ())
7205 return;
7207 /* The following phrase lines up nicely with per-tracepoint collect
7208 actions. */
7209 uiout->text ("default collect ");
7210 uiout->field_string ("default-collect", default_collect);
7211 uiout->text (" \n");
7214 static void
7215 info_breakpoints_command (const char *args, int from_tty)
7217 breakpoint_1 (args, false, NULL);
7219 default_collect_info ();
7222 static void
7223 info_watchpoints_command (const char *args, int from_tty)
7225 int num_printed = breakpoint_1 (args, false, is_watchpoint);
7226 struct ui_out *uiout = current_uiout;
7228 if (num_printed == 0)
7230 if (args == NULL || *args == '\0')
7231 uiout->message ("No watchpoints.\n");
7232 else
7233 uiout->message ("No watchpoint matching '%s'.\n", args);
7237 static void
7238 maintenance_info_breakpoints (const char *args, int from_tty)
7240 breakpoint_1 (args, true, NULL);
7242 default_collect_info ();
7245 static bool
7246 breakpoint_has_pc (struct breakpoint *b,
7247 struct program_space *pspace,
7248 CORE_ADDR pc, struct obj_section *section)
7250 for (bp_location &bl : b->locations ())
7252 if (bl.pspace == pspace
7253 && bl.address == pc
7254 && (!overlay_debugging || bl.section == section))
7255 return true;
7257 return false;
7260 /* See breakpoint.h. */
7262 void
7263 describe_other_breakpoints (struct gdbarch *gdbarch,
7264 struct program_space *pspace, CORE_ADDR pc,
7265 struct obj_section *section, int thread)
7267 int others = 0;
7269 for (breakpoint &b : all_breakpoints ())
7270 others += (user_breakpoint_p (&b)
7271 && breakpoint_has_pc (&b, pspace, pc, section));
7273 if (others > 0)
7275 if (others == 1)
7276 gdb_printf (_("Note: breakpoint "));
7277 else /* if (others == ???) */
7278 gdb_printf (_("Note: breakpoints "));
7279 for (breakpoint &b : all_breakpoints ())
7280 if (user_breakpoint_p (&b)
7281 && breakpoint_has_pc (&b, pspace, pc, section))
7283 others--;
7284 gdb_printf ("%d", b.number);
7285 if (b.thread == -1 && thread != -1)
7286 gdb_printf (" (all threads)");
7287 else if (b.thread != -1)
7289 struct thread_info *thr = find_thread_global_id (b.thread);
7290 gdb_printf (" (thread %s)", print_thread_id (thr));
7292 else if (b.task != -1)
7293 gdb_printf (" (task %d)", b.task);
7294 gdb_printf ("%s%s ",
7295 ((b.enable_state == bp_disabled
7296 || b.enable_state == bp_call_disabled)
7297 ? " (disabled)"
7298 : ""),
7299 (others > 1) ? ","
7300 : ((others == 1) ? " and" : ""));
7302 current_uiout->message (_("also set at pc %ps.\n"),
7303 styled_string (address_style.style (),
7304 paddress (gdbarch, pc)));
7309 /* Return true iff it is meaningful to use the address member of LOC.
7310 For some breakpoint types, the locations' address members are
7311 irrelevant and it makes no sense to attempt to compare them to
7312 other addresses (or use them for any other purpose either).
7314 More specifically, software watchpoints and catchpoints that are
7315 not backed by breakpoints always have a zero valued location
7316 address and we don't want to mark breakpoints of any of these types
7317 to be a duplicate of an actual breakpoint location at address
7318 zero. */
7320 static bool
7321 bl_address_is_meaningful (const bp_location *loc)
7323 return loc->loc_type != bp_loc_other;
7326 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7327 true if LOC1 and LOC2 represent the same watchpoint location. */
7329 static bool
7330 watchpoint_locations_match (const struct bp_location *loc1,
7331 const struct bp_location *loc2)
7333 watchpoint *w1 = gdb::checked_static_cast<watchpoint *> (loc1->owner);
7334 watchpoint *w2 = gdb::checked_static_cast<watchpoint *> (loc2->owner);
7336 /* Both of them must exist. */
7337 gdb_assert (w1 != NULL);
7338 gdb_assert (w2 != NULL);
7340 /* If the target can evaluate the condition expression in hardware,
7341 then we we need to insert both watchpoints even if they are at
7342 the same place. Otherwise the watchpoint will only trigger when
7343 the condition of whichever watchpoint was inserted evaluates to
7344 true, not giving a chance for GDB to check the condition of the
7345 other watchpoint. */
7346 if ((w1->cond_exp
7347 && target_can_accel_watchpoint_condition (loc1->address,
7348 loc1->length,
7349 loc1->watchpoint_type,
7350 w1->cond_exp.get ()))
7351 || (w2->cond_exp
7352 && target_can_accel_watchpoint_condition (loc2->address,
7353 loc2->length,
7354 loc2->watchpoint_type,
7355 w2->cond_exp.get ())))
7356 return false;
7358 /* Note that this checks the owner's type, not the location's. In
7359 case the target does not support read watchpoints, but does
7360 support access watchpoints, we'll have bp_read_watchpoint
7361 watchpoints with hw_access locations. Those should be considered
7362 duplicates of hw_read locations. The hw_read locations will
7363 become hw_access locations later. */
7364 return (loc1->owner->type == loc2->owner->type
7365 && loc1->pspace->aspace == loc2->pspace->aspace
7366 && loc1->address == loc2->address
7367 && loc1->length == loc2->length);
7370 /* See breakpoint.h. */
7373 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
7374 const address_space *aspace2, CORE_ADDR addr2)
7376 return ((gdbarch_has_global_breakpoints (current_inferior ()->arch ())
7377 || aspace1 == aspace2)
7378 && addr1 == addr2);
7381 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7382 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7383 matches ASPACE2. On targets that have global breakpoints, the address
7384 space doesn't really matter. */
7386 static bool
7387 breakpoint_address_match_range (const address_space *aspace1,
7388 CORE_ADDR addr1,
7389 int len1, const address_space *aspace2,
7390 CORE_ADDR addr2)
7392 return ((gdbarch_has_global_breakpoints (current_inferior ()->arch ())
7393 || aspace1 == aspace2)
7394 && addr2 >= addr1 && addr2 < addr1 + len1);
7397 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7398 a ranged breakpoint. In most targets, a match happens only if ASPACE
7399 matches the breakpoint's address space. On targets that have global
7400 breakpoints, the address space doesn't really matter. */
7402 static bool
7403 breakpoint_location_address_match (struct bp_location *bl,
7404 const address_space *aspace,
7405 CORE_ADDR addr)
7407 return (breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
7408 aspace, addr)
7409 || (bl->length
7410 && breakpoint_address_match_range (bl->pspace->aspace.get (),
7411 bl->address, bl->length,
7412 aspace, addr)));
7415 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7416 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7417 match happens only if ASPACE matches the breakpoint's address
7418 space. On targets that have global breakpoints, the address space
7419 doesn't really matter. */
7421 static bool
7422 breakpoint_location_address_range_overlap (struct bp_location *bl,
7423 const address_space *aspace,
7424 CORE_ADDR addr, int len)
7426 if (gdbarch_has_global_breakpoints (current_inferior ()->arch ())
7427 || bl->pspace->aspace.get () == aspace)
7429 int bl_len = bl->length != 0 ? bl->length : 1;
7431 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7432 return 1;
7434 return 0;
7437 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7438 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7439 true, otherwise returns false. */
7441 static bool
7442 tracepoint_locations_match (const struct bp_location *loc1,
7443 const struct bp_location *loc2)
7445 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7446 /* Since tracepoint locations are never duplicated with others', tracepoint
7447 locations at the same address of different tracepoints are regarded as
7448 different locations. */
7449 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7450 else
7451 return false;
7454 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7455 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7456 the same location. If SW_HW_BPS_MATCH is true, then software
7457 breakpoint locations and hardware breakpoint locations match,
7458 otherwise they don't. */
7460 static bool
7461 breakpoint_locations_match (const struct bp_location *loc1,
7462 const struct bp_location *loc2,
7463 bool sw_hw_bps_match)
7465 int hw_point1, hw_point2;
7467 /* Both of them must not be in moribund_locations. */
7468 gdb_assert (loc1->owner != NULL);
7469 gdb_assert (loc2->owner != NULL);
7471 hw_point1 = is_hardware_watchpoint (loc1->owner);
7472 hw_point2 = is_hardware_watchpoint (loc2->owner);
7474 if (hw_point1 != hw_point2)
7475 return false;
7476 else if (hw_point1)
7477 return watchpoint_locations_match (loc1, loc2);
7478 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7479 return tracepoint_locations_match (loc1, loc2);
7480 else
7481 /* We compare bp_location.length in order to cover ranged
7482 breakpoints. Keep this in sync with
7483 bp_location_is_less_than. */
7484 return (breakpoint_address_match (loc1->pspace->aspace.get (),
7485 loc1->address,
7486 loc2->pspace->aspace.get (),
7487 loc2->address)
7488 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7489 && loc1->length == loc2->length);
7492 static void
7493 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7494 int bnum, bool have_bnum)
7496 /* The longest string possibly returned by hex_string_custom
7497 is 50 chars. These must be at least that big for safety. */
7498 char astr1[64];
7499 char astr2[64];
7501 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7502 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7503 if (have_bnum)
7504 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7505 bnum, astr1, astr2);
7506 else
7507 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7510 /* Adjust a breakpoint's address to account for architectural
7511 constraints on breakpoint placement. Return the adjusted address.
7512 Note: Very few targets require this kind of adjustment. For most
7513 targets, this function is simply the identity function. */
7515 static CORE_ADDR
7516 adjust_breakpoint_address (struct gdbarch *gdbarch,
7517 CORE_ADDR bpaddr, enum bptype bptype,
7518 struct program_space *pspace)
7520 gdb_assert (pspace != nullptr);
7522 if (bptype == bp_watchpoint
7523 || bptype == bp_hardware_watchpoint
7524 || bptype == bp_read_watchpoint
7525 || bptype == bp_access_watchpoint
7526 || bptype == bp_catchpoint)
7528 /* Watchpoints and the various bp_catch_* eventpoints should not
7529 have their addresses modified. */
7530 return bpaddr;
7532 else if (bptype == bp_single_step)
7534 /* Single-step breakpoints should not have their addresses
7535 modified. If there's any architectural constrain that
7536 applies to this address, then it should have already been
7537 taken into account when the breakpoint was created in the
7538 first place. If we didn't do this, stepping through e.g.,
7539 Thumb-2 IT blocks would break. */
7540 return bpaddr;
7542 else
7544 CORE_ADDR adjusted_bpaddr = bpaddr;
7546 /* Some targets have architectural constraints on the placement
7547 of breakpoint instructions. Obtain the adjusted address. */
7548 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7550 /* Targets that implement this adjustment function will likely
7551 inspect either the symbol table, target memory at BPADDR, or
7552 even state registers, so ensure a suitable thread (and its
7553 associated program space) are currently selected. */
7554 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7555 switch_to_program_space_and_thread (pspace);
7556 adjusted_bpaddr
7557 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7560 adjusted_bpaddr
7561 = gdbarch_remove_non_address_bits_breakpoint (gdbarch, adjusted_bpaddr);
7563 /* An adjusted breakpoint address can significantly alter
7564 a user's expectations. Print a warning if an adjustment
7565 is required. */
7566 if (adjusted_bpaddr != bpaddr)
7567 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, false);
7569 return adjusted_bpaddr;
7573 static bp_loc_type
7574 bp_location_from_bp_type (bptype type)
7576 switch (type)
7578 case bp_breakpoint:
7579 case bp_single_step:
7580 case bp_until:
7581 case bp_finish:
7582 case bp_longjmp:
7583 case bp_longjmp_resume:
7584 case bp_longjmp_call_dummy:
7585 case bp_exception:
7586 case bp_exception_resume:
7587 case bp_step_resume:
7588 case bp_hp_step_resume:
7589 case bp_watchpoint_scope:
7590 case bp_call_dummy:
7591 case bp_std_terminate:
7592 case bp_shlib_event:
7593 case bp_thread_event:
7594 case bp_overlay_event:
7595 case bp_jit_event:
7596 case bp_longjmp_master:
7597 case bp_std_terminate_master:
7598 case bp_exception_master:
7599 case bp_gnu_ifunc_resolver:
7600 case bp_gnu_ifunc_resolver_return:
7601 case bp_dprintf:
7602 return bp_loc_software_breakpoint;
7604 case bp_hardware_breakpoint:
7605 return bp_loc_hardware_breakpoint;
7607 case bp_hardware_watchpoint:
7608 case bp_read_watchpoint:
7609 case bp_access_watchpoint:
7610 return bp_loc_hardware_watchpoint;
7612 case bp_watchpoint:
7613 return bp_loc_software_watchpoint;
7615 case bp_tracepoint:
7616 case bp_fast_tracepoint:
7617 case bp_static_tracepoint:
7618 case bp_static_marker_tracepoint:
7619 return bp_loc_tracepoint;
7621 case bp_catchpoint:
7622 return bp_loc_other;
7624 default:
7625 internal_error (_("unknown breakpoint type"));
7629 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7631 this->owner = owner;
7632 this->cond_bytecode = NULL;
7633 this->shlib_disabled = 0;
7634 this->enabled = 1;
7635 this->disabled_by_cond = false;
7637 this->loc_type = type;
7639 if (this->loc_type == bp_loc_software_breakpoint
7640 || this->loc_type == bp_loc_hardware_breakpoint)
7641 mark_breakpoint_location_modified (this);
7643 incref ();
7646 bp_location::bp_location (breakpoint *owner)
7647 : bp_location::bp_location (owner,
7648 bp_location_from_bp_type (owner->type))
7652 /* See breakpoint.h. */
7654 std::string
7655 bp_location::to_string () const
7657 string_file stb;
7658 ui_out_redirect_pop redir (current_uiout, &stb);
7659 print_breakpoint_location (this->owner, this);
7660 return stb.release ();
7663 /* Decrement reference count. If the reference count reaches 0,
7664 destroy the bp_location. Sets *BLP to NULL. */
7666 static void
7667 decref_bp_location (struct bp_location **blp)
7669 bp_location_ref_policy::decref (*blp);
7670 *blp = NULL;
7673 /* Add breakpoint B at the end of the global breakpoint chain. */
7675 static breakpoint *
7676 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7678 /* Add this breakpoint to the end of the chain so that a list of
7679 breakpoints will come out in order of increasing numbers. */
7681 breakpoint_chain.push_back (*b.release ());
7683 return &breakpoint_chain.back ();
7686 /* Initialize loc->function_name. */
7688 static void
7689 set_breakpoint_location_function (struct bp_location *loc)
7691 gdb_assert (loc->owner != NULL);
7693 if (loc->owner->type == bp_breakpoint
7694 || loc->owner->type == bp_hardware_breakpoint
7695 || is_tracepoint (loc->owner))
7697 const char *function_name;
7699 if (loc->msymbol != NULL
7700 && (loc->msymbol->type () == mst_text_gnu_ifunc
7701 || loc->msymbol->type () == mst_data_gnu_ifunc))
7703 struct breakpoint *b = loc->owner;
7705 function_name = loc->msymbol->linkage_name ();
7707 if (b->type == bp_breakpoint
7708 && b->has_single_location ()
7709 && b->related_breakpoint == b)
7711 /* Create only the whole new breakpoint of this type but do not
7712 mess more complicated breakpoints with multiple locations. */
7713 b->type = bp_gnu_ifunc_resolver;
7714 /* Remember the resolver's address for use by the return
7715 breakpoint. */
7716 loc->related_address = loc->address;
7719 else
7720 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7722 if (function_name)
7723 loc->function_name = make_unique_xstrdup (function_name);
7727 /* Attempt to determine architecture of location identified by SAL. */
7728 struct gdbarch *
7729 get_sal_arch (struct symtab_and_line sal)
7731 if (sal.section)
7732 return sal.section->objfile->arch ();
7733 if (sal.symtab)
7734 return sal.symtab->compunit ()->objfile ()->arch ();
7736 return NULL;
7739 /* Call this routine when stepping and nexting to enable a breakpoint
7740 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7741 initiated the operation. */
7743 void
7744 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7746 int thread = tp->global_num;
7748 /* To avoid having to rescan all objfile symbols at every step,
7749 we maintain a list of continually-inserted but always disabled
7750 longjmp "master" breakpoints. Here, we simply create momentary
7751 clones of those and enable them for the requested thread. */
7752 for (breakpoint &b : all_breakpoints_safe ())
7753 if (b.pspace == current_program_space
7754 && (b.type == bp_longjmp_master
7755 || b.type == bp_exception_master))
7757 bptype type = b.type == bp_longjmp_master ? bp_longjmp : bp_exception;
7758 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7759 after their removal. */
7760 momentary_breakpoint_from_master (&b, type, 1, thread);
7763 tp->initiating_frame = frame;
7766 /* Delete all longjmp breakpoints from THREAD. */
7767 void
7768 delete_longjmp_breakpoint (int thread)
7770 for (breakpoint &b : all_breakpoints_safe ())
7771 if (b.type == bp_longjmp || b.type == bp_exception)
7773 if (b.thread == thread)
7775 gdb_assert (b.inferior == -1);
7776 delete_breakpoint (&b);
7781 void
7782 delete_longjmp_breakpoint_at_next_stop (int thread)
7784 for (breakpoint &b : all_breakpoints_safe ())
7785 if (b.type == bp_longjmp || b.type == bp_exception)
7787 if (b.thread == thread)
7789 gdb_assert (b.inferior == -1);
7790 b.disposition = disp_del_at_next_stop;
7795 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7796 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7797 pointer to any of them. Return NULL if this system cannot place longjmp
7798 breakpoints. */
7800 struct breakpoint *
7801 set_longjmp_breakpoint_for_call_dummy (void)
7803 breakpoint *retval = nullptr;
7805 for (breakpoint &b : all_breakpoints ())
7806 if (b.pspace == current_program_space && b.type == bp_longjmp_master)
7808 int thread = inferior_thread ()->global_num;
7809 breakpoint *new_b
7810 = momentary_breakpoint_from_master (&b, bp_longjmp_call_dummy,
7811 1, thread);
7813 /* Link NEW_B into the chain of RETVAL breakpoints. */
7815 gdb_assert (new_b->related_breakpoint == new_b);
7816 if (retval == NULL)
7817 retval = new_b;
7818 new_b->related_breakpoint = retval;
7819 while (retval->related_breakpoint != new_b->related_breakpoint)
7820 retval = retval->related_breakpoint;
7821 retval->related_breakpoint = new_b;
7824 return retval;
7827 /* Verify all existing dummy frames and their associated breakpoints for
7828 TP. Remove those which can no longer be found in the current frame
7829 stack.
7831 If the unwind fails then there is not sufficient information to discard
7832 dummy frames. In this case, elide the clean up and the dummy frames will
7833 be cleaned up next time this function is called from a location where
7834 unwinding is possible. */
7836 void
7837 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7839 /* We would need to delete breakpoints other than the current one while
7840 iterating, so all_breakpoints_safe is not sufficient to make that safe.
7841 Save all breakpoints to delete in that set and delete them at the end. */
7842 std::unordered_set<breakpoint *> to_delete;
7844 for (struct breakpoint &b : all_breakpoints ())
7846 if (b.type == bp_longjmp_call_dummy && b.thread == tp->global_num)
7848 gdb_assert (b.inferior == -1);
7849 struct breakpoint *dummy_b = b.related_breakpoint;
7851 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7852 chained off b->related_breakpoint. */
7853 while (dummy_b != &b && dummy_b->type != bp_call_dummy)
7854 dummy_b = dummy_b->related_breakpoint;
7856 /* If there was no bp_call_dummy breakpoint then there's nothing
7857 more to do. Or, if the dummy frame associated with the
7858 bp_call_dummy is still on the stack then we need to leave this
7859 bp_call_dummy in place. */
7860 if (dummy_b->type != bp_call_dummy
7861 || frame_find_by_id (dummy_b->frame_id) != NULL)
7862 continue;
7864 /* We didn't find the dummy frame on the stack, this could be
7865 because we have longjmp'd to a stack frame that is previous to
7866 the dummy frame, or it could be because the stack unwind is
7867 broken at some point between the longjmp frame and the dummy
7868 frame.
7870 Next we figure out why the stack unwind stopped. If it looks
7871 like the unwind is complete then we assume the dummy frame has
7872 been jumped over, however, if the unwind stopped for an
7873 unexpected reason then we assume the stack unwind is currently
7874 broken, and that we will (eventually) return to the dummy
7875 frame.
7877 It might be tempting to consider using frame_id_inner here, but
7878 that is not safe. There is no guarantee that the stack frames
7879 we are looking at here are even on the same stack as the
7880 original dummy frame, hence frame_id_inner can't be used. See
7881 the comments on frame_id_inner for more details. */
7882 bool unwind_finished_unexpectedly = false;
7883 for (frame_info_ptr fi = get_current_frame (); fi != nullptr; )
7885 frame_info_ptr prev = get_prev_frame (fi);
7886 if (prev == nullptr)
7888 /* FI is the last stack frame. Why did this frame not
7889 unwind further? */
7890 auto stop_reason = get_frame_unwind_stop_reason (fi);
7891 if (stop_reason != UNWIND_NO_REASON
7892 && stop_reason != UNWIND_OUTERMOST)
7893 unwind_finished_unexpectedly = true;
7895 fi = prev;
7897 if (unwind_finished_unexpectedly)
7898 continue;
7900 dummy_frame_discard (dummy_b->frame_id, tp);
7902 for (breakpoint *related_breakpoint = b.related_breakpoint;
7903 related_breakpoint != &b;
7904 related_breakpoint = related_breakpoint->related_breakpoint)
7905 to_delete.insert (b.related_breakpoint);
7907 to_delete.insert (&b);
7911 for (breakpoint *b : to_delete)
7912 delete_breakpoint (b);
7915 void
7916 enable_overlay_breakpoints (void)
7918 for (breakpoint &b : all_breakpoints ())
7919 if (b.type == bp_overlay_event)
7921 b.enable_state = bp_enabled;
7922 update_global_location_list (UGLL_MAY_INSERT);
7923 overlay_events_enabled = 1;
7927 void
7928 disable_overlay_breakpoints (void)
7930 for (breakpoint &b : all_breakpoints ())
7931 if (b.type == bp_overlay_event)
7933 b.enable_state = bp_disabled;
7934 update_global_location_list (UGLL_DONT_INSERT);
7935 overlay_events_enabled = 0;
7939 /* Set an active std::terminate breakpoint for each std::terminate
7940 master breakpoint. */
7941 void
7942 set_std_terminate_breakpoint (void)
7944 for (breakpoint &b : all_breakpoints_safe ())
7945 if (b.pspace == current_program_space
7946 && b.type == bp_std_terminate_master)
7948 momentary_breakpoint_from_master (&b, bp_std_terminate, 1,
7949 inferior_thread ()->global_num);
7953 /* Delete all the std::terminate breakpoints. */
7954 void
7955 delete_std_terminate_breakpoint (void)
7957 for (breakpoint &b : all_breakpoints_safe ())
7958 if (b.type == bp_std_terminate)
7959 delete_breakpoint (&b);
7962 struct breakpoint *
7963 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7965 struct breakpoint *b;
7967 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
7969 b->enable_state = bp_enabled;
7970 /* locspec has to be used or breakpoint_re_set will delete me. */
7971 b->locspec = new_address_location_spec (b->first_loc ().address, NULL, 0);
7973 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7975 return b;
7978 struct lang_and_radix
7980 enum language lang;
7981 int radix;
7984 /* Create a breakpoint for JIT code registration and unregistration. */
7986 struct breakpoint *
7987 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7989 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
7992 /* Remove JIT code registration and unregistration breakpoint(s). */
7994 void
7995 remove_jit_event_breakpoints (void)
7997 for (breakpoint &b : all_breakpoints_safe ())
7998 if (b.type == bp_jit_event
7999 && b.first_loc ().pspace == current_program_space)
8000 delete_breakpoint (&b);
8003 void
8004 remove_solib_event_breakpoints (void)
8006 for (breakpoint &b : all_breakpoints_safe ())
8007 if (b.type == bp_shlib_event
8008 && b.first_loc ().pspace == current_program_space)
8009 delete_breakpoint (&b);
8012 /* See breakpoint.h. */
8014 void
8015 remove_solib_event_breakpoints_at_next_stop (void)
8017 for (breakpoint &b : all_breakpoints_safe ())
8018 if (b.type == bp_shlib_event
8019 && b.first_loc ().pspace == current_program_space)
8020 b.disposition = disp_del_at_next_stop;
8023 /* Helper for create_solib_event_breakpoint /
8024 create_and_insert_solib_event_breakpoint. Allows specifying which
8025 INSERT_MODE to pass through to update_global_location_list. */
8027 static struct breakpoint *
8028 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
8029 enum ugll_insert_mode insert_mode)
8031 struct breakpoint *b;
8033 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
8034 update_global_location_list_nothrow (insert_mode);
8035 return b;
8038 struct breakpoint *
8039 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
8041 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
8044 /* See breakpoint.h. */
8046 struct breakpoint *
8047 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
8049 struct breakpoint *b;
8051 /* Explicitly tell update_global_location_list to insert
8052 locations. */
8053 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
8054 if (!b->first_loc ().inserted)
8056 delete_breakpoint (b);
8057 return NULL;
8059 return b;
8062 /* See breakpoint.h. */
8064 void
8065 disable_breakpoints_in_shlibs (program_space *pspace)
8067 for (bp_location *loc : all_bp_locations ())
8069 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
8070 struct breakpoint *b = loc->owner;
8072 /* We apply the check to all breakpoints, including disabled for
8073 those with loc->duplicate set. This is so that when breakpoint
8074 becomes enabled, or the duplicate is removed, gdb will try to
8075 insert all breakpoints. If we don't set shlib_disabled here,
8076 we'll try to insert those breakpoints and fail. */
8077 if (((b->type == bp_breakpoint)
8078 || (b->type == bp_jit_event)
8079 || (b->type == bp_hardware_breakpoint)
8080 || (is_tracepoint (b)))
8081 && loc->pspace == pspace
8082 && !loc->shlib_disabled
8083 && solib_name_from_address (loc->pspace, loc->address)
8086 loc->shlib_disabled = 1;
8091 /* Disable any breakpoints and tracepoints that are in SOLIB upon
8092 notification of unloaded_shlib. Only apply to enabled breakpoints,
8093 disabled ones can just stay disabled. */
8095 static void
8096 disable_breakpoints_in_unloaded_shlib (program_space *pspace, const solib &solib)
8098 bool disabled_shlib_breaks = false;
8100 for (bp_location *loc : all_bp_locations ())
8102 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
8103 struct breakpoint *b = loc->owner;
8105 if (pspace == loc->pspace
8106 && !loc->shlib_disabled
8107 && (((b->type == bp_breakpoint
8108 || b->type == bp_jit_event
8109 || b->type == bp_hardware_breakpoint)
8110 && (loc->loc_type == bp_loc_hardware_breakpoint
8111 || loc->loc_type == bp_loc_software_breakpoint))
8112 || is_tracepoint (b))
8113 && solib_contains_address_p (solib, loc->address))
8115 loc->shlib_disabled = 1;
8116 /* At this point, we cannot rely on remove_breakpoint
8117 succeeding so we must mark the breakpoint as not inserted
8118 to prevent future errors occurring in remove_breakpoints. */
8119 loc->inserted = 0;
8121 /* This may cause duplicate notifications for the same breakpoint. */
8122 notify_breakpoint_modified (b);
8124 if (!disabled_shlib_breaks)
8126 target_terminal::ours_for_output ();
8127 warning (_("Temporarily disabling breakpoints "
8128 "for unloaded shared library \"%s\""),
8129 solib.so_name.c_str ());
8131 disabled_shlib_breaks = true;
8136 /* Disable any breakpoints and tracepoints in OBJFILE upon
8137 notification of free_objfile. Only apply to enabled breakpoints,
8138 disabled ones can just stay disabled. */
8140 static void
8141 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
8143 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8144 managed by the user with add-symbol-file/remove-symbol-file.
8145 Similarly to how breakpoints in shared libraries are handled in
8146 response to "nosharedlibrary", mark breakpoints in such modules
8147 shlib_disabled so they end up uninserted on the next global
8148 location list update. Shared libraries not loaded by the user
8149 aren't handled here -- they're already handled in
8150 disable_breakpoints_in_unloaded_shlib, called by solib.c's
8151 solib_unloaded observer. We skip objfiles that are not
8152 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8153 main objfile). */
8154 if ((objfile->flags & OBJF_SHARED) == 0
8155 || (objfile->flags & OBJF_USERLOADED) == 0)
8156 return;
8158 for (breakpoint &b : all_breakpoints ())
8160 bool bp_modified = false;
8162 if (!is_breakpoint (&b) && !is_tracepoint (&b))
8163 continue;
8165 for (bp_location &loc : b.locations ())
8167 CORE_ADDR loc_addr = loc.address;
8169 if (loc.loc_type != bp_loc_hardware_breakpoint
8170 && loc.loc_type != bp_loc_software_breakpoint)
8171 continue;
8173 if (loc.shlib_disabled != 0)
8174 continue;
8176 if (objfile->pspace () != loc.pspace)
8177 continue;
8179 if (is_addr_in_objfile (loc_addr, objfile))
8181 loc.shlib_disabled = 1;
8182 /* At this point, we don't know whether the object was
8183 unmapped from the inferior or not, so leave the
8184 inserted flag alone. We'll handle failure to
8185 uninsert quietly, in case the object was indeed
8186 unmapped. */
8188 mark_breakpoint_location_modified (&loc);
8190 bp_modified = true;
8194 if (bp_modified)
8195 notify_breakpoint_modified (&b);
8199 /* See breakpoint.h. */
8201 breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
8202 bool temp, const char *cond_string_)
8203 : type (bptype),
8204 disposition (temp ? disp_del : disp_donttouch),
8205 gdbarch (gdbarch_),
8206 language (current_language->la_language),
8207 input_radix (::input_radix),
8208 cond_string (cond_string_ != nullptr
8209 ? make_unique_xstrdup (cond_string_)
8210 : nullptr),
8211 related_breakpoint (this)
8215 /* See breakpoint.h. */
8217 catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
8218 const char *cond_string)
8219 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
8221 add_dummy_location (this, current_program_space);
8223 pspace = current_program_space;
8226 /* See breakpoint.h. */
8228 void
8229 catchpoint::re_set (program_space *filter_pspace)
8231 /* All catchpoints are associated with a specific program_space. */
8232 gdb_assert (pspace != nullptr);
8234 /* If only a single program space changed, and it's not the program space
8235 for which this catchpoint applies, then there's nothing to do. */
8236 if (filter_pspace != nullptr && filter_pspace != pspace)
8237 return;
8239 /* Catchpoints have a single dummy location. */
8240 gdb_assert (locations ().size () == 1);
8241 bp_location &bl = m_locations.front ();
8243 if (cond_string == nullptr)
8245 /* It shouldn't be possible to have a parsed condition expression
8246 cached on this location if the catchpoint doesn't have a condition
8247 string set. */
8248 gdb_assert (bl.cond == nullptr);
8250 /* Nothing to re-compute, and the catchpoint cannot change. */
8251 return;
8254 bool previous_disabled_by_cond = bl.disabled_by_cond;
8256 /* Start by marking the location disabled and discarding the previously
8257 computed condition expression. Now if we get an exception, even if
8258 it's a quit exception, we'll leave the location disabled and there
8259 will be no (possibly invalid) expression cached. */
8260 bl.disabled_by_cond = true;
8261 bl.cond = nullptr;
8263 const char *s = cond_string.get ();
8266 switch_to_program_space_and_thread (pspace);
8268 bl.cond = parse_exp_1 (&s, bl.address, block_for_pc (bl.address),
8269 nullptr);
8270 bl.disabled_by_cond = false;
8272 catch (const gdb_exception_error &e)
8274 /* Any exception thrown must be from either the parse_exp_1 or
8275 earlier in the try block. As such the following two asserts
8276 should be true. */
8277 gdb_assert (bl.disabled_by_cond);
8278 gdb_assert (bl.cond == nullptr);
8281 if (previous_disabled_by_cond != bl.disabled_by_cond)
8282 notify_breakpoint_modified (this);
8285 /* Notify interpreters and observers that breakpoint B was created. */
8287 static void
8288 notify_breakpoint_created (breakpoint *b)
8290 interps_notify_breakpoint_created (b);
8291 gdb::observers::breakpoint_created.notify (b);
8294 breakpoint *
8295 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8297 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8298 set_breakpoint_number (internal, b);
8299 if (is_tracepoint (b))
8300 set_tracepoint_count (breakpoint_count);
8301 if (!internal)
8302 mention (b);
8304 notify_breakpoint_created (b);
8306 if (update_gll)
8307 update_global_location_list (UGLL_MAY_INSERT);
8309 return b;
8312 static int
8313 hw_breakpoint_used_count (void)
8315 int i = 0;
8317 for (breakpoint &b : all_breakpoints ())
8318 if (b.type == bp_hardware_breakpoint && breakpoint_enabled (&b))
8319 for (bp_location &bl : b.locations ())
8321 /* Special types of hardware breakpoints may use more than
8322 one register. */
8323 i += b.resources_needed (&bl);
8326 return i;
8329 /* Returns the resources B would use if it were a hardware
8330 watchpoint. */
8332 static int
8333 hw_watchpoint_use_count (struct breakpoint *b)
8335 int i = 0;
8337 if (!breakpoint_enabled (b))
8338 return 0;
8340 for (bp_location &bl : b->locations ())
8342 /* Special types of hardware watchpoints may use more than
8343 one register. */
8344 i += b->resources_needed (&bl);
8347 return i;
8350 /* Returns the sum the used resources of all hardware watchpoints of
8351 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8352 the sum of the used resources of all hardware watchpoints of other
8353 types _not_ TYPE. */
8355 static int
8356 hw_watchpoint_used_count_others (struct breakpoint *except,
8357 enum bptype type, int *other_type_used)
8359 int i = 0;
8361 *other_type_used = 0;
8362 for (breakpoint &b : all_breakpoints ())
8364 if (&b == except)
8365 continue;
8366 if (!breakpoint_enabled (&b))
8367 continue;
8369 if (b.type == type)
8370 i += hw_watchpoint_use_count (&b);
8371 else if (is_hardware_watchpoint (&b))
8372 *other_type_used = 1;
8375 return i;
8378 void
8379 disable_watchpoints_before_interactive_call_start (void)
8381 for (breakpoint &b : all_breakpoints ())
8382 if (is_watchpoint (&b) && breakpoint_enabled (&b))
8384 b.enable_state = bp_call_disabled;
8385 update_global_location_list (UGLL_DONT_INSERT);
8389 void
8390 enable_watchpoints_after_interactive_call_stop (void)
8392 for (breakpoint &b : all_breakpoints ())
8393 if (is_watchpoint (&b) && b.enable_state == bp_call_disabled)
8395 b.enable_state = bp_enabled;
8396 update_global_location_list (UGLL_MAY_INSERT);
8400 void
8401 disable_breakpoints_before_startup (void)
8403 current_program_space->executing_startup = 1;
8404 update_global_location_list (UGLL_DONT_INSERT);
8407 void
8408 enable_breakpoints_after_startup (void)
8410 current_program_space->executing_startup = 0;
8411 breakpoint_re_set ();
8414 /* Allocate a new momentary breakpoint. */
8416 template<typename... Arg>
8417 static momentary_breakpoint *
8418 new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
8419 Arg&&... args)
8421 if (type == bp_longjmp || type == bp_exception)
8422 return new longjmp_breakpoint (gdbarch, type,
8423 std::forward<Arg> (args)...);
8424 else
8425 return new momentary_breakpoint (gdbarch, type,
8426 std::forward<Arg> (args)...);
8429 /* Set a momentary breakpoint of type TYPE at address specified by
8430 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8431 frame. */
8433 breakpoint_up
8434 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8435 struct frame_id frame_id, enum bptype type)
8437 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8438 tail-called one. */
8439 gdb_assert (!frame_id_artificial_p (frame_id));
8441 std::unique_ptr<momentary_breakpoint> b
8442 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
8443 inferior_thread ()->global_num));
8445 b->add_location (sal);
8447 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
8449 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8451 return bp;
8454 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8455 The new breakpoint will have type TYPE, use OPS as its
8456 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8458 static struct breakpoint *
8459 momentary_breakpoint_from_master (struct breakpoint *orig,
8460 enum bptype type,
8461 int loc_enabled,
8462 int thread)
8464 std::unique_ptr<breakpoint> copy
8465 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
8466 orig->frame_id, thread));
8467 const bp_location &orig_loc = orig->first_loc ();
8468 bp_location *copy_loc = copy->allocate_location ();
8469 copy->add_location (*copy_loc);
8470 set_breakpoint_location_function (copy_loc);
8472 copy_loc->gdbarch = orig_loc.gdbarch;
8473 copy_loc->requested_address = orig_loc.requested_address;
8474 copy_loc->address = orig_loc.address;
8475 copy_loc->section = orig_loc.section;
8476 copy_loc->pspace = orig_loc.pspace;
8477 copy_loc->probe = orig_loc.probe;
8478 copy_loc->line_number = orig_loc.line_number;
8479 copy_loc->symtab = orig_loc.symtab;
8480 copy_loc->enabled = loc_enabled;
8482 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
8483 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8484 return b;
8487 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8488 ORIG is NULL. */
8490 struct breakpoint *
8491 clone_momentary_breakpoint (struct breakpoint *orig)
8493 /* If there's nothing to clone, then return nothing. */
8494 if (orig == NULL)
8495 return NULL;
8497 return momentary_breakpoint_from_master (orig, orig->type, 0,
8498 orig->thread);
8501 breakpoint_up
8502 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8503 enum bptype type)
8505 struct symtab_and_line sal;
8507 sal = find_pc_line (pc, 0);
8508 sal.pc = pc;
8509 sal.section = find_pc_overlay (pc);
8510 sal.explicit_pc = 1;
8512 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8516 /* Tell the user we have just set a breakpoint B. */
8518 static void
8519 mention (const breakpoint *b)
8521 b->print_mention ();
8522 current_uiout->text ("\n");
8526 static bool bp_loc_is_permanent (struct bp_location *loc);
8528 /* Handle "set breakpoint auto-hw on".
8530 If the explicitly specified breakpoint type is not hardware
8531 breakpoint, check the memory map to see whether the breakpoint
8532 address is in read-only memory.
8534 - location type is not hardware breakpoint, memory is read-only.
8535 We change the type of the location to hardware breakpoint.
8537 - location type is hardware breakpoint, memory is read-write. This
8538 means we've previously made the location hardware one, but then the
8539 memory map changed, so we undo.
8542 static void
8543 handle_automatic_hardware_breakpoints (bp_location *bl)
8545 if (automatic_hardware_breakpoints
8546 && bl->owner->type != bp_hardware_breakpoint
8547 && (bl->loc_type == bp_loc_software_breakpoint
8548 || bl->loc_type == bp_loc_hardware_breakpoint))
8550 /* When breakpoints are removed, remove_breakpoints will use
8551 location types we've just set here, the only possible problem
8552 is that memory map has changed during running program, but
8553 it's not going to work anyway with current gdb. */
8554 mem_region *mr = lookup_mem_region (bl->address);
8556 if (mr != nullptr)
8558 enum bp_loc_type new_type;
8560 if (mr->attrib.mode != MEM_RW)
8561 new_type = bp_loc_hardware_breakpoint;
8562 else
8563 new_type = bp_loc_software_breakpoint;
8565 if (new_type != bl->loc_type)
8567 static bool said = false;
8569 bl->loc_type = new_type;
8570 if (!said)
8572 gdb_printf (_("Note: automatically using "
8573 "hardware breakpoints for "
8574 "read-only addresses.\n"));
8575 said = true;
8582 bp_location *
8583 code_breakpoint::add_location (const symtab_and_line &sal)
8585 CORE_ADDR adjusted_address;
8586 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8588 if (loc_gdbarch == NULL)
8589 loc_gdbarch = gdbarch;
8591 /* Adjust the breakpoint's address prior to allocating a location.
8592 Once we call allocate_location(), that mostly uninitialized
8593 location will be placed on the location chain. Adjustment of the
8594 breakpoint may cause target_read_memory() to be called and we do
8595 not want its scan of the location chain to find a breakpoint and
8596 location that's only been partially initialized. */
8597 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8598 sal.pc, type,
8599 sal.pspace);
8601 /* Sort the locations by their ADDRESS. */
8602 bp_location *new_loc = this->allocate_location ();
8604 new_loc->requested_address = sal.pc;
8605 new_loc->address = adjusted_address;
8606 new_loc->pspace = sal.pspace;
8607 new_loc->probe.prob = sal.prob;
8608 new_loc->probe.objfile = sal.objfile;
8609 gdb_assert (new_loc->pspace != NULL);
8610 new_loc->section = sal.section;
8611 new_loc->gdbarch = loc_gdbarch;
8612 new_loc->line_number = sal.line;
8613 new_loc->symtab = sal.symtab;
8614 new_loc->symbol = sal.symbol;
8615 new_loc->msymbol = sal.msymbol;
8617 breakpoint::add_location (*new_loc);
8619 set_breakpoint_location_function (new_loc);
8621 /* While by definition, permanent breakpoints are already present in the
8622 code, we don't mark the location as inserted. Normally one would expect
8623 that GDB could rely on that breakpoint instruction to stop the program,
8624 thus removing the need to insert its own breakpoint, except that executing
8625 the breakpoint instruction can kill the target instead of reporting a
8626 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8627 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8628 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8629 breakpoint be inserted normally results in QEMU knowing about the GDB
8630 breakpoint, and thus trap before the breakpoint instruction is executed.
8631 (If GDB later needs to continue execution past the permanent breakpoint,
8632 it manually increments the PC, thus avoiding executing the breakpoint
8633 instruction.) */
8634 if (bp_loc_is_permanent (new_loc))
8635 new_loc->permanent = 1;
8637 return new_loc;
8641 /* Return true if LOC is pointing to a permanent breakpoint,
8642 return false otherwise. */
8644 static bool
8645 bp_loc_is_permanent (struct bp_location *loc)
8647 gdb_assert (loc != NULL);
8649 /* If we have a non-breakpoint-backed catchpoint or a software
8650 watchpoint, just return 0. We should not attempt to read from
8651 the addresses the locations of these breakpoint types point to.
8652 gdbarch_program_breakpoint_here_p, below, will attempt to read
8653 memory. */
8654 if (!bl_address_is_meaningful (loc))
8655 return false;
8657 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8658 switch_to_program_space_and_thread (loc->pspace);
8659 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8662 /* Build a command list for the dprintf corresponding to the current
8663 settings of the dprintf style options. */
8665 static void
8666 update_dprintf_command_list (struct breakpoint *b)
8668 gdb_assert (b->type == bp_dprintf);
8669 gdb_assert (b->extra_string != nullptr);
8671 const char *dprintf_args = b->extra_string.get ();
8672 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8674 /* Trying to create a dprintf breakpoint without a format and args
8675 string should be detected at creation time. */
8676 gdb_assert (dprintf_args != nullptr);
8678 dprintf_args = skip_spaces (dprintf_args);
8680 /* Allow a comma, as it may have terminated a location, but don't
8681 insist on it. */
8682 if (*dprintf_args == ',')
8683 ++dprintf_args;
8684 dprintf_args = skip_spaces (dprintf_args);
8686 if (*dprintf_args != '"')
8687 error (_("Bad format string, missing '\"'."));
8689 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8690 printf_line = xstrprintf ("printf %s", dprintf_args);
8691 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8693 if (dprintf_function.empty ())
8694 error (_("No function supplied for dprintf call"));
8696 if (!dprintf_channel.empty ())
8697 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8698 dprintf_function.c_str (),
8699 dprintf_channel.c_str (),
8700 dprintf_args);
8701 else
8702 printf_line = xstrprintf ("call (void) %s (%s)",
8703 dprintf_function.c_str (),
8704 dprintf_args);
8706 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8708 if (target_can_run_breakpoint_commands ())
8709 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8710 else
8712 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8713 printf_line = xstrprintf ("printf %s", dprintf_args);
8716 else
8717 internal_error (_("Invalid dprintf style."));
8719 gdb_assert (printf_line != NULL);
8721 /* Manufacture a printf sequence. */
8722 struct command_line *printf_cmd_line
8723 = new struct command_line (simple_control, printf_line.release ());
8724 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8725 command_lines_deleter ()));
8728 /* Update all dprintf commands, making their command lists reflect
8729 current style settings. */
8731 static void
8732 update_dprintf_commands (const char *args, int from_tty,
8733 struct cmd_list_element *c)
8735 for (breakpoint &b : all_breakpoints ())
8736 if (b.type == bp_dprintf)
8737 update_dprintf_command_list (&b);
8740 code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
8741 enum bptype type_,
8742 gdb::array_view<const symtab_and_line> sals,
8743 location_spec_up &&locspec_,
8744 gdb::unique_xmalloc_ptr<char> filter_,
8745 gdb::unique_xmalloc_ptr<char> cond_string_,
8746 gdb::unique_xmalloc_ptr<char> extra_string_,
8747 enum bpdisp disposition_,
8748 int thread_, int task_, int inferior_,
8749 int ignore_count_,
8750 int from_tty,
8751 int enabled_, unsigned flags,
8752 int display_canonical_)
8753 : breakpoint (gdbarch_, type_)
8755 int i;
8757 if (type == bp_hardware_breakpoint)
8759 int target_resources_ok;
8761 i = hw_breakpoint_used_count ();
8762 target_resources_ok =
8763 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8764 i + 1, 0);
8765 if (target_resources_ok == 0)
8766 error (_("No hardware breakpoint support in the target."));
8767 else if (target_resources_ok < 0)
8768 error (_("Hardware breakpoints used exceeds limit."));
8771 gdb_assert (!sals.empty ());
8773 /* At most one of thread, task, or inferior can be set on any breakpoint. */
8774 gdb_assert (((thread == -1 ? 0 : 1)
8775 + (task == -1 ? 0 : 1)
8776 + (inferior == -1 ? 0 : 1)) <= 1);
8778 thread = thread_;
8779 task = task_;
8780 inferior = inferior_;
8782 cond_string = std::move (cond_string_);
8783 extra_string = std::move (extra_string_);
8784 ignore_count = ignore_count_;
8785 enable_state = enabled_ ? bp_enabled : bp_disabled;
8786 disposition = disposition_;
8788 if (type == bp_static_tracepoint
8789 || type == bp_static_marker_tracepoint)
8791 auto *t = gdb::checked_static_cast<tracepoint *> (this);
8792 struct static_tracepoint_marker marker;
8794 if (strace_marker_p (this))
8796 /* We already know the marker exists, otherwise, we wouldn't
8797 see a sal for it. */
8798 const char *p = &locspec_->to_string ()[3];
8799 const char *endp;
8801 p = skip_spaces (p);
8803 endp = skip_to_space (p);
8805 t->static_trace_marker_id.assign (p, endp - p);
8807 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8808 t->static_trace_marker_id.c_str ());
8810 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8812 t->static_trace_marker_id = std::move (marker.str_id);
8814 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8815 t->static_trace_marker_id.c_str ());
8817 else
8818 warning (_("Couldn't determine the static tracepoint marker to probe"));
8821 for (const auto &sal : sals)
8823 if (from_tty)
8825 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8826 if (loc_gdbarch == nullptr)
8827 loc_gdbarch = gdbarch;
8829 describe_other_breakpoints (loc_gdbarch,
8830 sal.pspace, sal.pc, sal.section, thread);
8833 bp_location *new_loc = add_location (sal);
8834 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8835 new_loc->inserted = 1;
8837 /* Do not set breakpoint locations conditions yet. As locations
8838 are inserted, they get sorted based on their addresses. Let
8839 the list stabilize to have reliable location numbers. */
8842 /* Dynamic printf requires and uses additional arguments on the
8843 command line, otherwise it's an error. */
8844 if (type == bp_dprintf)
8845 update_dprintf_command_list (this);
8846 else
8847 gdb_assert (extra_string == nullptr);
8849 /* The order of the locations is now stable. Set the location
8850 condition using the location's number. */
8851 int loc_num = 1;
8852 for (bp_location &bl : locations ())
8854 if (cond_string != nullptr)
8855 set_breakpoint_location_condition (cond_string.get (), &bl,
8856 number, loc_num);
8858 ++loc_num;
8861 display_canonical = display_canonical_;
8862 if (locspec_ != nullptr)
8863 locspec = std::move (locspec_);
8864 else
8865 locspec = new_address_location_spec (this->first_loc ().address, NULL, 0);
8866 filter = std::move (filter_);
8869 static void
8870 create_breakpoint_sal (struct gdbarch *gdbarch,
8871 gdb::array_view<const symtab_and_line> sals,
8872 location_spec_up &&locspec,
8873 gdb::unique_xmalloc_ptr<char> filter,
8874 gdb::unique_xmalloc_ptr<char> cond_string,
8875 gdb::unique_xmalloc_ptr<char> extra_string,
8876 enum bptype type, enum bpdisp disposition,
8877 int thread, int task, int inferior, int ignore_count,
8878 int from_tty,
8879 int enabled, int internal, unsigned flags,
8880 int display_canonical)
8882 std::unique_ptr<code_breakpoint> b
8883 = new_breakpoint_from_type (gdbarch,
8884 type,
8885 sals,
8886 std::move (locspec),
8887 std::move (filter),
8888 std::move (cond_string),
8889 std::move (extra_string),
8890 disposition,
8891 thread, task, inferior, ignore_count,
8892 from_tty,
8893 enabled, flags,
8894 display_canonical);
8896 install_breakpoint (internal, std::move (b), 0);
8899 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8900 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8901 value. COND_STRING, if not NULL, specified the condition to be
8902 used for all breakpoints. Essentially the only case where
8903 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8904 function. In that case, it's still not possible to specify
8905 separate conditions for different overloaded functions, so
8906 we take just a single condition string.
8908 NOTE: If the function succeeds, the caller is expected to cleanup
8909 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8910 array contents). If the function fails (error() is called), the
8911 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8912 COND and SALS arrays and each of those arrays contents. */
8914 static void
8915 create_breakpoints_sal (struct gdbarch *gdbarch,
8916 struct linespec_result *canonical,
8917 gdb::unique_xmalloc_ptr<char> cond_string,
8918 gdb::unique_xmalloc_ptr<char> extra_string,
8919 enum bptype type, enum bpdisp disposition,
8920 int thread, int task, int inferior,
8921 int ignore_count,
8922 int from_tty,
8923 int enabled, int internal, unsigned flags)
8925 if (canonical->pre_expanded)
8926 gdb_assert (canonical->lsals.size () == 1);
8928 for (const auto &lsal : canonical->lsals)
8930 /* Note that 'location' can be NULL in the case of a plain
8931 'break', without arguments. */
8932 location_spec_up locspec
8933 = (canonical->locspec != nullptr
8934 ? canonical->locspec->clone ()
8935 : nullptr);
8936 gdb::unique_xmalloc_ptr<char> filter_string
8937 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8939 create_breakpoint_sal (gdbarch, lsal.sals,
8940 std::move (locspec),
8941 std::move (filter_string),
8942 std::move (cond_string),
8943 std::move (extra_string),
8944 type, disposition,
8945 thread, task, inferior, ignore_count,
8946 from_tty, enabled, internal, flags,
8947 canonical->special_display);
8951 /* Parse LOCSPEC which is assumed to be a SAL specification possibly
8952 followed by conditionals. On return, SALS contains an array of SAL
8953 addresses found. LOCSPEC points to the end of the SAL (for
8954 linespec locspecs).
8956 The array and the line spec strings are allocated on the heap, it is
8957 the caller's responsibility to free them. */
8959 static void
8960 parse_breakpoint_sals (location_spec *locspec,
8961 struct linespec_result *canonical,
8962 struct program_space *search_pspace)
8964 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8966 const char *spec
8967 = as_linespec_location_spec (locspec)->spec_string.get ();
8969 if (spec == NULL)
8971 /* The last displayed codepoint, if it's valid, is our default
8972 breakpoint address. */
8973 if (last_displayed_sal_is_valid ())
8975 /* Set sal's pspace, pc, symtab, and line to the values
8976 corresponding to the last call to print_frame_info.
8977 Be sure to reinitialize LINE with NOTCURRENT == 0
8978 as the breakpoint line number is inappropriate otherwise.
8979 find_pc_line would adjust PC, re-set it back. */
8980 symtab_and_line sal = get_last_displayed_sal ();
8981 CORE_ADDR pc = sal.pc;
8983 sal = find_pc_line (pc, 0);
8985 /* "break" without arguments is equivalent to "break *PC"
8986 where PC is the last displayed codepoint's address. So
8987 make sure to set sal.explicit_pc to prevent GDB from
8988 trying to expand the list of sals to include all other
8989 instances with the same symtab and line. */
8990 sal.pc = pc;
8991 sal.explicit_pc = 1;
8993 struct linespec_sals lsal;
8994 lsal.sals = {sal};
8995 lsal.canonical = NULL;
8997 canonical->lsals.push_back (std::move (lsal));
8998 return;
9000 else
9001 error (_("No default breakpoint address now."));
9005 /* Force almost all breakpoints to be in terms of the
9006 current_source_symtab (which is decode_line_1's default).
9007 This should produce the results we want almost all of the
9008 time while leaving default_breakpoint_* alone.
9010 ObjC: However, don't match an Objective-C method name which
9011 may have a '+' or '-' succeeded by a '['. */
9012 symtab_and_line cursal
9013 = get_current_source_symtab_and_line (current_program_space);
9014 if (last_displayed_sal_is_valid ())
9016 const char *spec = NULL;
9018 if (locspec->type () == LINESPEC_LOCATION_SPEC)
9019 spec = as_linespec_location_spec (locspec)->spec_string.get ();
9021 if (!cursal.symtab
9022 || (spec != NULL
9023 && strchr ("+-", spec[0]) != NULL
9024 && spec[1] != '['))
9026 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
9027 get_last_displayed_symtab (),
9028 get_last_displayed_line (),
9029 canonical, NULL, NULL);
9030 return;
9034 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
9035 cursal.symtab, cursal.line, canonical, NULL, NULL);
9039 /* Convert each SAL into a real PC. Verify that the PC can be
9040 inserted as a breakpoint. If it can't throw an error. */
9042 static void
9043 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
9045 for (auto &sal : sals)
9046 resolve_sal_pc (&sal);
9049 /* Fast tracepoints may have restrictions on valid locations. For
9050 instance, a fast tracepoint using a jump instead of a trap will
9051 likely have to overwrite more bytes than a trap would, and so can
9052 only be placed where the instruction is longer than the jump, or a
9053 multi-instruction sequence does not have a jump into the middle of
9054 it, etc. */
9056 static void
9057 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9058 gdb::array_view<const symtab_and_line> sals)
9060 for (const auto &sal : sals)
9062 struct gdbarch *sarch;
9064 sarch = get_sal_arch (sal);
9065 /* We fall back to GDBARCH if there is no architecture
9066 associated with SAL. */
9067 if (sarch == NULL)
9068 sarch = gdbarch;
9069 std::string msg;
9070 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
9071 error (_("May not have a fast tracepoint at %s%s"),
9072 paddress (sarch, sal.pc), msg.c_str ());
9076 /* Decode a static tracepoint marker spec. */
9078 static std::vector<symtab_and_line>
9079 decode_static_tracepoint_spec (const char **arg_p)
9081 const char *p = &(*arg_p)[3];
9082 const char *endp;
9084 p = skip_spaces (p);
9086 endp = skip_to_space (p);
9088 std::string marker_str (p, endp - p);
9090 std::vector<static_tracepoint_marker> markers
9091 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9092 if (markers.empty ())
9093 error (_("No known static tracepoint marker named %s"),
9094 marker_str.c_str ());
9096 std::vector<symtab_and_line> sals;
9097 sals.reserve (markers.size ());
9099 for (const static_tracepoint_marker &marker : markers)
9101 symtab_and_line sal = find_pc_line (marker.address, 0);
9102 sal.pc = marker.address;
9103 sals.push_back (sal);
9106 *arg_p = endp;
9107 return sals;
9110 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9111 according to IS_TRACEPOINT. */
9113 static const struct breakpoint_ops *
9114 breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
9115 bool is_tracepoint)
9117 if (is_tracepoint)
9119 if (locspec_type == PROBE_LOCATION_SPEC)
9120 return &tracepoint_probe_breakpoint_ops;
9121 else
9122 return &code_breakpoint_ops;
9124 else
9126 if (locspec_type == PROBE_LOCATION_SPEC)
9127 return &bkpt_probe_breakpoint_ops;
9128 else
9129 return &code_breakpoint_ops;
9133 /* Return the program space to use as a filter when searching for locations
9134 of a breakpoint specific to THREAD or INFERIOR. If THREAD and INFERIOR
9135 are both -1, meaning all threads/inferiors, then this function returns
9136 nullptr, indicating no program space filtering should be performed.
9137 Otherwise, this function returns the program space for the inferior that
9138 contains THREAD (when THREAD is not -1), or the program space for
9139 INFERIOR (when INFERIOR is not -1). */
9141 static struct program_space *
9142 find_program_space_for_breakpoint (int thread, int inferior)
9144 if (thread != -1)
9146 gdb_assert (inferior == -1);
9148 struct thread_info *thr = find_thread_global_id (thread);
9149 gdb_assert (thr != nullptr);
9150 gdb_assert (thr->inf != nullptr);
9151 return thr->inf->pspace;
9153 else if (inferior != -1)
9155 gdb_assert (thread == -1);
9157 struct inferior *inf = find_inferior_id (inferior);
9158 gdb_assert (inf != nullptr);
9160 return inf->pspace;
9163 return nullptr;
9166 /* See breakpoint.h. */
9168 const struct breakpoint_ops *
9169 breakpoint_ops_for_location_spec (const location_spec *locspec,
9170 bool is_tracepoint)
9172 if (locspec != nullptr)
9173 return (breakpoint_ops_for_location_spec_type
9174 (locspec->type (), is_tracepoint));
9175 return &code_breakpoint_ops;
9178 /* See breakpoint.h. */
9181 create_breakpoint (struct gdbarch *gdbarch,
9182 location_spec *locspec,
9183 const char *cond_string,
9184 int thread, int inferior,
9185 const char *extra_string,
9186 bool force_condition, int parse_extra,
9187 int tempflag, enum bptype type_wanted,
9188 int ignore_count,
9189 enum auto_boolean pending_break_support,
9190 const struct breakpoint_ops *ops,
9191 int from_tty, int enabled, int internal,
9192 unsigned flags)
9194 struct linespec_result canonical;
9195 bool pending = false;
9196 int task = -1;
9197 int prev_bkpt_count = breakpoint_count;
9199 gdb_assert (thread == -1 || thread > 0);
9200 gdb_assert (inferior == -1 || inferior > 0);
9201 gdb_assert (thread == -1 || inferior == -1);
9203 /* If PARSE_EXTRA is true then the thread and inferior details will be
9204 parsed from the EXTRA_STRING, the THREAD and INFERIOR arguments
9205 should be -1. */
9206 gdb_assert (!parse_extra || thread == -1);
9207 gdb_assert (!parse_extra || inferior == -1);
9209 gdb_assert (ops != NULL);
9211 /* If extra_string isn't useful, set it to NULL. */
9212 if (extra_string != NULL && *extra_string == '\0')
9213 extra_string = NULL;
9215 /* A bp_dprintf must always have an accompanying EXTRA_STRING containing
9216 the dprintf format and arguments -- PARSE_EXTRA should always be false
9217 in this case.
9219 For all other breakpoint types, EXTRA_STRING should be nullptr unless
9220 PARSE_EXTRA is true. */
9221 gdb_assert ((type_wanted == bp_dprintf)
9222 ? (extra_string != nullptr && !parse_extra)
9223 : (extra_string == nullptr || parse_extra));
9225 /* Will hold either copies of the similarly named function argument, or
9226 will hold a modified version of the function argument, depending on
9227 the value of PARSE_EXTRA. */
9228 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9229 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9231 if (parse_extra)
9233 /* Parse EXTRA_STRING splitting the parts out. */
9234 create_breakpoint_parse_arg_string (extra_string, &cond_string_copy,
9235 &thread, &inferior, &task,
9236 &extra_string_copy,
9237 &force_condition);
9239 /* We could check that EXTRA_STRING_COPY is empty at this point -- it
9240 should be, as we only get here for things that are not bp_dprintf,
9241 however, we prefer to give the location spec parser a chance to
9242 run first, this means the user will get errors about invalid
9243 location spec instead of an error about garbage at the end of the
9244 command line.
9246 We still do the EXTRA_STRING_COPY is empty check, just later in
9247 this function. */
9249 gdb_assert (thread == -1 || thread > 0);
9250 gdb_assert (task == -1 || task > 0);
9251 gdb_assert (inferior == -1 || inferior > 0);
9253 else
9255 if (cond_string != nullptr)
9256 cond_string_copy.reset (xstrdup (cond_string));
9257 if (extra_string != nullptr)
9258 extra_string_copy.reset (xstrdup (extra_string));
9261 /* Clear these. Updated values are now held in the *_copy locals. */
9262 cond_string = nullptr;
9263 extra_string = nullptr;
9267 struct program_space *search_pspace
9268 = find_program_space_for_breakpoint (thread, inferior);
9269 ops->create_sals_from_location_spec (locspec, &canonical,
9270 search_pspace);
9272 catch (const gdb_exception_error &e)
9274 /* If caller is interested in rc value from parse, set
9275 value. */
9276 if (e.error == NOT_FOUND_ERROR)
9278 /* If pending breakpoint support is turned off, throw
9279 error. */
9281 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9282 throw;
9284 exception_print (gdb_stderr, e);
9286 /* If pending breakpoint support is auto query and the user
9287 selects no, then simply return the error code. */
9288 if (pending_break_support == AUTO_BOOLEAN_AUTO
9289 && !nquery (_("Make %s pending on future shared library load? "),
9290 bptype_string (type_wanted)))
9291 return 0;
9293 /* At this point, either the user was queried about setting
9294 a pending breakpoint and selected yes, or pending
9295 breakpoint behavior is on and thus a pending breakpoint
9296 is defaulted on behalf of the user. */
9297 pending = true;
9299 else
9300 throw;
9303 /* Only bp_dprintf breakpoints should have anything in EXTRA_STRING_COPY
9304 by this point. For all other breakpoints this indicates an error. We
9305 could place this check earlier in the function, but we prefer to see
9306 errors from the location spec parser before we see this error message. */
9307 if (type_wanted != bp_dprintf && extra_string_copy.get () != nullptr)
9308 error (_("Garbage '%s' at end of command"), extra_string_copy.get ());
9310 if (!pending && canonical.lsals.empty ())
9311 return 0;
9313 /* Resolve all line numbers to PC's and verify that the addresses
9314 are ok for the target. */
9315 if (!pending)
9317 for (auto &lsal : canonical.lsals)
9318 breakpoint_sals_to_pc (lsal.sals);
9321 /* Fast tracepoints may have additional restrictions on location. */
9322 if (!pending && type_wanted == bp_fast_tracepoint)
9324 for (const auto &lsal : canonical.lsals)
9325 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9328 /* Verify that condition can be parsed, before setting any
9329 breakpoints. Allocate a separate condition expression for each
9330 breakpoint. */
9331 if (!pending)
9333 /* Check the validity of the condition. We should error out if the
9334 condition is invalid at all of the locations and if it is not
9335 forced. In the PARSE_EXTRA case above, this check is done when
9336 parsing the EXTRA_STRING. */
9337 if (cond_string_copy.get () != nullptr && !force_condition)
9339 int num_failures = 0;
9340 const linespec_sals &lsal = canonical.lsals[0];
9341 for (const auto &sal : lsal.sals)
9343 const char *cond = cond_string_copy.get ();
9346 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9347 /* One success is sufficient to keep going. */
9348 break;
9350 catch (const gdb_exception_error &)
9352 num_failures++;
9353 /* If this is the last sal, error out. */
9354 if (num_failures == lsal.sals.size ())
9355 throw;
9360 ops->create_breakpoints_sal (gdbarch, &canonical,
9361 std::move (cond_string_copy),
9362 std::move (extra_string_copy),
9363 type_wanted,
9364 tempflag ? disp_del : disp_donttouch,
9365 thread, task, inferior, ignore_count,
9366 from_tty, enabled, internal, flags);
9368 else
9370 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
9371 type_wanted);
9372 b->locspec = locspec->clone ();
9374 /* Create a private copy of the condition string. */
9375 b->cond_string = std::move (cond_string_copy);
9377 b->thread = thread;
9378 b->task = task;
9379 b->inferior = inferior;
9381 /* Create a private copy of any extra string. */
9382 b->extra_string = std::move (extra_string_copy);
9384 b->ignore_count = ignore_count;
9385 b->disposition = tempflag ? disp_del : disp_donttouch;
9386 b->condition_not_parsed = 1;
9387 b->enable_state = enabled ? bp_enabled : bp_disabled;
9389 if (b->type == bp_dprintf)
9390 update_dprintf_command_list (b.get ());
9392 install_breakpoint (internal, std::move (b), 0);
9395 if (canonical.lsals.size () > 1)
9397 warning (_("Multiple breakpoints were set.\nUse the "
9398 "\"%ps\" command to delete unwanted breakpoints."),
9399 styled_string (command_style.style (), "delete"));
9400 prev_breakpoint_count = prev_bkpt_count;
9403 update_global_location_list (UGLL_MAY_INSERT);
9405 return 1;
9408 /* Set a breakpoint.
9409 ARG is a string describing breakpoint address,
9410 condition, and thread.
9411 FLAG specifies if a breakpoint is hardware on,
9412 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9413 and BP_TEMPFLAG. */
9415 static void
9416 break_command_1 (const char *arg, int flag, int from_tty)
9418 int tempflag = flag & BP_TEMPFLAG;
9419 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9420 ? bp_hardware_breakpoint
9421 : bp_breakpoint);
9423 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9424 const struct breakpoint_ops *ops
9425 = breakpoint_ops_for_location_spec (locspec.get (),
9426 false /* is_tracepoint */);
9428 create_breakpoint (get_current_arch (),
9429 locspec.get (),
9430 NULL,
9431 -1 /* thread */, -1 /* inferior */,
9432 arg, false, 1 /* parse arg */,
9433 tempflag, type_wanted,
9434 0 /* Ignore count */,
9435 pending_break_support,
9436 ops,
9437 from_tty,
9438 1 /* enabled */,
9439 0 /* internal */,
9443 /* Helper function for break_command_1 and disassemble_command. */
9445 void
9446 resolve_sal_pc (struct symtab_and_line *sal)
9448 CORE_ADDR pc;
9450 if (sal->pc == 0 && sal->symtab != NULL)
9452 if (!find_line_pc (sal->symtab, sal->line, &pc))
9453 error (_("No line %d in file \"%s\"."),
9454 sal->line, symtab_to_filename_for_display (sal->symtab));
9455 sal->pc = pc;
9457 /* If this SAL corresponds to a breakpoint inserted using a line
9458 number, then skip the function prologue if necessary. */
9459 if (sal->explicit_line)
9460 skip_prologue_sal (sal);
9463 if (sal->section == 0 && sal->symtab != NULL)
9465 const struct blockvector *bv;
9466 const struct block *b;
9467 struct symbol *sym;
9469 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9470 sal->symtab->compunit ());
9471 if (bv != NULL)
9473 sym = b->linkage_function ();
9474 if (sym != NULL)
9475 sal->section
9476 = sym->obj_section (sal->symtab->compunit ()->objfile ());
9477 else
9479 /* It really is worthwhile to have the section, so we'll
9480 just have to look harder. This case can be executed
9481 if we have line numbers but no functions (as can
9482 happen in assembly source). */
9484 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9485 switch_to_program_space_and_thread (sal->pspace);
9487 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9488 if (msym.minsym)
9489 sal->section = msym.obj_section ();
9495 void
9496 break_command (const char *arg, int from_tty)
9498 break_command_1 (arg, 0, from_tty);
9501 void
9502 tbreak_command (const char *arg, int from_tty)
9504 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9507 static void
9508 hbreak_command (const char *arg, int from_tty)
9510 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9513 static void
9514 thbreak_command (const char *arg, int from_tty)
9516 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9519 /* The dynamic printf command is mostly like a regular breakpoint, but
9520 with a prewired command list consisting of a single output command,
9521 built from extra arguments supplied on the dprintf command
9522 line. */
9524 static void
9525 dprintf_command (const char *arg, int from_tty)
9527 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9529 /* If non-NULL, ARG should have been advanced past the location;
9530 the next character must be ','. */
9531 if (arg == nullptr || arg[0] != ',' || arg[1] == '\0')
9532 error (_("Format string required"));
9533 else
9535 /* Skip the comma. */
9536 ++arg;
9539 create_breakpoint (get_current_arch (),
9540 locspec.get (),
9541 NULL, -1, -1,
9542 arg, false, 0 /* parse arg */,
9543 0, bp_dprintf,
9544 0 /* Ignore count */,
9545 pending_break_support,
9546 &code_breakpoint_ops,
9547 from_tty,
9548 1 /* enabled */,
9549 0 /* internal */,
9553 static void
9554 agent_printf_command (const char *arg, int from_tty)
9556 error (_("May only run agent-printf on the target"));
9559 /* Implement the "breakpoint_hit" method for ranged breakpoints. */
9562 ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9563 const address_space *aspace,
9564 CORE_ADDR bp_addr,
9565 const target_waitstatus &ws)
9567 if (ws.kind () != TARGET_WAITKIND_STOPPED
9568 || ws.sig () != GDB_SIGNAL_TRAP)
9569 return 0;
9571 return breakpoint_address_match_range (bl->pspace->aspace.get (),
9572 bl->address, bl->length, aspace,
9573 bp_addr);
9576 /* Implement the "resources_needed" method for ranged breakpoints. */
9579 ranged_breakpoint::resources_needed (const struct bp_location *bl)
9581 return target_ranged_break_num_registers ();
9584 /* Implement the "print_it" method for ranged breakpoints. */
9586 enum print_stop_action
9587 ranged_breakpoint::print_it (const bpstat *bs) const
9589 struct ui_out *uiout = current_uiout;
9591 gdb_assert (type == bp_hardware_breakpoint);
9593 /* Ranged breakpoints have only one location. */
9594 gdb_assert (this->has_single_location ());
9596 annotate_breakpoint (number);
9598 maybe_print_thread_hit_breakpoint (uiout);
9600 if (disposition == disp_del)
9601 uiout->text ("Temporary ranged breakpoint ");
9602 else
9603 uiout->text ("Ranged breakpoint ");
9604 if (uiout->is_mi_like_p ())
9606 uiout->field_string ("reason",
9607 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9608 uiout->field_string ("disp", bpdisp_text (disposition));
9610 print_num_locno (bs, uiout);
9611 uiout->text (", ");
9613 return PRINT_SRC_AND_LOC;
9616 /* Implement the "print_one" method for ranged breakpoints. */
9618 bool
9619 ranged_breakpoint::print_one (const bp_location **last_loc) const
9621 struct value_print_options opts;
9622 struct ui_out *uiout = current_uiout;
9624 /* Ranged breakpoints have only one location. */
9625 gdb_assert (this->has_single_location ());
9627 get_user_print_options (&opts);
9629 if (opts.addressprint)
9630 /* We don't print the address range here, it will be printed later
9631 by ranged_breakpoint::print_one_detail. */
9632 uiout->field_skip ("addr");
9633 annotate_field (5);
9634 print_breakpoint_location (this, &this->first_loc ());
9635 *last_loc = &this->first_loc ();
9637 return true;
9640 /* Implement the "print_one_detail" method for ranged breakpoints. */
9642 void
9643 ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
9645 CORE_ADDR address_start, address_end;
9646 const bp_location &bl = this->first_loc ();
9647 string_file stb;
9649 address_start = bl.address;
9650 address_end = address_start + bl.length - 1;
9652 uiout->text ("\taddress range: ");
9653 stb.printf ("[%s, %s]",
9654 print_core_address (bl.gdbarch, address_start),
9655 print_core_address (bl.gdbarch, address_end));
9656 uiout->field_stream ("addr", stb);
9657 uiout->text ("\n");
9660 /* Implement the "print_mention" method for ranged breakpoints. */
9662 void
9663 ranged_breakpoint::print_mention () const
9665 const bp_location &bl = this->first_loc ();
9666 struct ui_out *uiout = current_uiout;
9668 gdb_assert (type == bp_hardware_breakpoint);
9670 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9671 number, paddress (bl.gdbarch, bl.address),
9672 paddress (bl.gdbarch, bl.address + bl.length - 1));
9675 /* Implement the "print_recreate" method for ranged breakpoints. */
9677 void
9678 ranged_breakpoint::print_recreate (struct ui_file *fp) const
9680 gdb_printf (fp, "break-range %s, %s",
9681 locspec->to_string (),
9682 locspec_range_end->to_string ());
9683 print_recreate_thread (fp);
9686 /* Find the address where the end of the breakpoint range should be
9687 placed, given the SAL of the end of the range. This is so that if
9688 the user provides a line number, the end of the range is set to the
9689 last instruction of the given line. */
9691 static CORE_ADDR
9692 find_breakpoint_range_end (struct symtab_and_line sal)
9694 CORE_ADDR end;
9696 /* If the user provided a PC value, use it. Otherwise,
9697 find the address of the end of the given location. */
9698 if (sal.explicit_pc)
9699 end = sal.pc;
9700 else
9702 int ret;
9703 CORE_ADDR start;
9705 ret = find_line_pc_range (sal, &start, &end);
9706 if (!ret)
9707 error (_("Could not find location of the end of the range."));
9709 /* find_line_pc_range returns the start of the next line. */
9710 end--;
9713 return end;
9716 /* Implement the "break-range" CLI command. */
9718 static void
9719 break_range_command (const char *arg, int from_tty)
9721 const char *arg_start;
9722 struct linespec_result canonical_start, canonical_end;
9723 int bp_count, can_use_bp, length;
9724 CORE_ADDR end;
9726 /* We don't support software ranged breakpoints. */
9727 if (target_ranged_break_num_registers () < 0)
9728 error (_("This target does not support hardware ranged breakpoints."));
9730 bp_count = hw_breakpoint_used_count ();
9731 bp_count += target_ranged_break_num_registers ();
9732 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9733 bp_count, 0);
9734 if (can_use_bp < 0)
9735 error (_("Hardware breakpoints used exceeds limit."));
9737 arg = skip_spaces (arg);
9738 if (arg == NULL || arg[0] == '\0')
9739 error(_("No address range specified."));
9741 arg_start = arg;
9742 location_spec_up start_locspec
9743 = string_to_location_spec (&arg, current_language);
9744 parse_breakpoint_sals (start_locspec.get (), &canonical_start, nullptr);
9746 if (arg[0] != ',')
9747 error (_("Too few arguments."));
9748 else if (canonical_start.lsals.empty ())
9749 error (_("Could not find location of the beginning of the range."));
9751 const linespec_sals &lsal_start = canonical_start.lsals[0];
9753 if (canonical_start.lsals.size () > 1
9754 || lsal_start.sals.size () != 1)
9755 error (_("Cannot create a ranged breakpoint with multiple locations."));
9757 const symtab_and_line &sal_start = lsal_start.sals[0];
9758 std::string addr_string_start (arg_start, arg - arg_start);
9760 arg++; /* Skip the comma. */
9761 arg = skip_spaces (arg);
9763 /* Parse the end location specification. */
9765 arg_start = arg;
9767 /* We call decode_line_full directly here instead of using
9768 parse_breakpoint_sals because we need to specify the start
9769 location spec's symtab and line as the default symtab and line
9770 for the end of the range. This makes it possible to have ranges
9771 like "foo.c:27, +14", where +14 means 14 lines from the start
9772 location spec. */
9773 location_spec_up end_locspec
9774 = string_to_location_spec (&arg, current_language);
9775 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9776 sal_start.symtab, sal_start.line,
9777 &canonical_end, NULL, NULL);
9779 if (canonical_end.lsals.empty ())
9780 error (_("Could not find location of the end of the range."));
9782 const linespec_sals &lsal_end = canonical_end.lsals[0];
9783 if (canonical_end.lsals.size () > 1
9784 || lsal_end.sals.size () != 1)
9785 error (_("Cannot create a ranged breakpoint with multiple locations."));
9787 const symtab_and_line &sal_end = lsal_end.sals[0];
9789 end = find_breakpoint_range_end (sal_end);
9790 if (sal_start.pc > end)
9791 error (_("Invalid address range, end precedes start."));
9793 length = end - sal_start.pc + 1;
9794 if (length < 0)
9795 /* Length overflowed. */
9796 error (_("Address range too large."));
9797 else if (length == 1)
9799 /* This range is simple enough to be handled by
9800 the `hbreak' command. */
9801 hbreak_command (&addr_string_start[0], 1);
9803 return;
9806 /* Now set up the breakpoint and install it. */
9808 std::unique_ptr<breakpoint> br
9809 (new ranged_breakpoint (get_current_arch (),
9810 sal_start, length,
9811 std::move (start_locspec),
9812 std::move (end_locspec)));
9814 install_breakpoint (false, std::move (br), true);
9817 /* See breakpoint.h. */
9819 watchpoint::~watchpoint ()
9821 /* Make sure to unlink the destroyed watchpoint from the related
9822 breakpoint ring. */
9824 breakpoint *bpt = this;
9825 while (bpt->related_breakpoint != this)
9826 bpt = bpt->related_breakpoint;
9828 bpt->related_breakpoint = this->related_breakpoint;
9831 /* Return non-zero if EXP is verified as constant. Returned zero
9832 means EXP is variable. Also the constant detection may fail for
9833 some constant expressions and in such case still falsely return
9834 zero. */
9836 static bool
9837 watchpoint_exp_is_const (const struct expression *exp)
9839 return exp->op->constant_p ();
9842 /* Implement the "re_set" method for watchpoints. */
9844 void
9845 watchpoint::re_set (struct program_space *pspace)
9847 /* Watchpoint can be either on expression using entirely global
9848 variables, or it can be on local variables.
9850 Watchpoints of the first kind are never auto-deleted, and even
9851 persist across program restarts. Since they can use variables
9852 from shared libraries, we need to reparse expression as libraries
9853 are loaded and unloaded.
9855 Watchpoints on local variables can also change meaning as result
9856 of solib event. For example, if a watchpoint uses both a local
9857 and a global variables in expression, it's a local watchpoint,
9858 but unloading of a shared library will make the expression
9859 invalid. This is not a very common use case, but we still
9860 re-evaluate expression, to avoid surprises to the user.
9862 Note that for local watchpoints, we re-evaluate it only if
9863 watchpoints frame id is still valid. If it's not, it means the
9864 watchpoint is out of scope and will be deleted soon. In fact,
9865 I'm not sure we'll ever be called in this case.
9867 If a local watchpoint's frame id is still valid, then
9868 exp_valid_block is likewise valid, and we can safely use it.
9870 Don't do anything about disabled watchpoints, since they will be
9871 reevaluated again when enabled. */
9872 update_watchpoint (this, true /* reparse */);
9875 /* Implement the "insert" method for hardware watchpoints. */
9878 watchpoint::insert_location (struct bp_location *bl)
9880 int length = exact ? 1 : bl->length;
9882 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9883 cond_exp.get ());
9886 /* Implement the "remove" method for hardware watchpoints. */
9889 watchpoint::remove_location (struct bp_location *bl,
9890 enum remove_bp_reason reason)
9892 int length = exact ? 1 : bl->length;
9894 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9895 cond_exp.get ());
9899 watchpoint::breakpoint_hit (const struct bp_location *bl,
9900 const address_space *aspace, CORE_ADDR bp_addr,
9901 const target_waitstatus &ws)
9903 struct breakpoint *b = bl->owner;
9905 /* Continuable hardware watchpoints are treated as non-existent if the
9906 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9907 some data address). Otherwise gdb won't stop on a break instruction
9908 in the code (not from a breakpoint) when a hardware watchpoint has
9909 been defined. Also skip watchpoints which we know did not trigger
9910 (did not match the data address). */
9911 if (is_hardware_watchpoint (b)
9912 && watchpoint_triggered == watch_triggered_no)
9913 return 0;
9915 return 1;
9918 void
9919 watchpoint::check_status (bpstat *bs)
9921 bpstat_check_watchpoint (bs);
9924 /* Implement the "resources_needed" method for hardware
9925 watchpoints. */
9928 watchpoint::resources_needed (const struct bp_location *bl)
9930 int length = exact? 1 : bl->length;
9932 return target_region_ok_for_hw_watchpoint (bl->address, length);
9935 /* Implement the "works_in_software_mode" method for hardware
9936 watchpoints. */
9938 bool
9939 watchpoint::works_in_software_mode () const
9941 /* Read and access watchpoints only work with hardware support. */
9942 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9945 enum print_stop_action
9946 watchpoint::print_it (const bpstat *bs) const
9948 enum print_stop_action result;
9949 struct ui_out *uiout = current_uiout;
9951 gdb_assert (bs->bp_location_at != NULL);
9953 annotate_watchpoint (this->number);
9954 maybe_print_thread_hit_breakpoint (uiout);
9956 string_file stb;
9958 std::optional<ui_out_emit_tuple> tuple_emitter;
9959 switch (this->type)
9961 case bp_watchpoint:
9962 case bp_hardware_watchpoint:
9963 if (uiout->is_mi_like_p ())
9964 uiout->field_string
9965 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9966 mention (this);
9967 tuple_emitter.emplace (uiout, "value");
9968 uiout->text ("\nOld value = ");
9969 watchpoint_value_print (bs->old_val.get (), &stb);
9970 uiout->field_stream ("old", stb);
9971 uiout->text ("\nNew value = ");
9972 watchpoint_value_print (val.get (), &stb);
9973 uiout->field_stream ("new", stb);
9974 uiout->text ("\n");
9975 /* More than one watchpoint may have been triggered. */
9976 result = PRINT_UNKNOWN;
9977 break;
9979 case bp_read_watchpoint:
9980 if (uiout->is_mi_like_p ())
9981 uiout->field_string
9982 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9983 mention (this);
9984 tuple_emitter.emplace (uiout, "value");
9985 uiout->text ("\nValue = ");
9986 watchpoint_value_print (val.get (), &stb);
9987 uiout->field_stream ("value", stb);
9988 uiout->text ("\n");
9989 result = PRINT_UNKNOWN;
9990 break;
9992 case bp_access_watchpoint:
9993 if (bs->old_val != NULL)
9995 if (uiout->is_mi_like_p ())
9996 uiout->field_string
9997 ("reason",
9998 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9999 mention (this);
10000 tuple_emitter.emplace (uiout, "value");
10001 uiout->text ("\nOld value = ");
10002 watchpoint_value_print (bs->old_val.get (), &stb);
10003 uiout->field_stream ("old", stb);
10004 uiout->text ("\nNew value = ");
10006 else
10008 mention (this);
10009 if (uiout->is_mi_like_p ())
10010 uiout->field_string
10011 ("reason",
10012 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10013 tuple_emitter.emplace (uiout, "value");
10014 uiout->text ("\nValue = ");
10016 watchpoint_value_print (val.get (), &stb);
10017 uiout->field_stream ("new", stb);
10018 uiout->text ("\n");
10019 result = PRINT_UNKNOWN;
10020 break;
10021 default:
10022 result = PRINT_UNKNOWN;
10025 return result;
10028 /* Implement the "print_mention" method for hardware watchpoints. */
10030 void
10031 watchpoint::print_mention () const
10033 struct ui_out *uiout = current_uiout;
10034 const char *tuple_name;
10036 switch (type)
10038 case bp_watchpoint:
10039 uiout->text ("Watchpoint ");
10040 tuple_name = "wpt";
10041 break;
10042 case bp_hardware_watchpoint:
10043 uiout->text ("Hardware watchpoint ");
10044 tuple_name = "wpt";
10045 break;
10046 case bp_read_watchpoint:
10047 uiout->text ("Hardware read watchpoint ");
10048 tuple_name = "hw-rwpt";
10049 break;
10050 case bp_access_watchpoint:
10051 uiout->text ("Hardware access (read/write) watchpoint ");
10052 tuple_name = "hw-awpt";
10053 break;
10054 default:
10055 internal_error (_("Invalid hardware watchpoint type."));
10058 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10059 uiout->field_signed ("number", number);
10060 uiout->text (": ");
10061 uiout->field_string ("exp", exp_string.get ());
10064 /* Implement the "print_recreate" method for watchpoints. */
10066 void
10067 watchpoint::print_recreate (struct ui_file *fp) const
10069 switch (type)
10071 case bp_watchpoint:
10072 case bp_hardware_watchpoint:
10073 gdb_printf (fp, "watch");
10074 break;
10075 case bp_read_watchpoint:
10076 gdb_printf (fp, "rwatch");
10077 break;
10078 case bp_access_watchpoint:
10079 gdb_printf (fp, "awatch");
10080 break;
10081 default:
10082 internal_error (_("Invalid watchpoint type."));
10085 gdb_printf (fp, " %s", exp_string.get ());
10086 print_recreate_thread (fp);
10089 /* Implement the "explains_signal" method for watchpoints. */
10091 bool
10092 watchpoint::explains_signal (enum gdb_signal sig)
10094 /* A software watchpoint cannot cause a signal other than
10095 GDB_SIGNAL_TRAP. */
10096 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10097 return false;
10099 return true;
10102 struct masked_watchpoint : public watchpoint
10104 using watchpoint::watchpoint;
10106 int insert_location (struct bp_location *) override;
10107 int remove_location (struct bp_location *,
10108 enum remove_bp_reason reason) override;
10109 int resources_needed (const struct bp_location *) override;
10110 bool works_in_software_mode () const override;
10111 enum print_stop_action print_it (const bpstat *bs) const override;
10112 void print_one_detail (struct ui_out *) const override;
10113 void print_mention () const override;
10114 void print_recreate (struct ui_file *fp) const override;
10117 /* Implement the "insert" method for masked hardware watchpoints. */
10120 masked_watchpoint::insert_location (struct bp_location *bl)
10122 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
10123 bl->watchpoint_type);
10126 /* Implement the "remove" method for masked hardware watchpoints. */
10129 masked_watchpoint::remove_location (struct bp_location *bl,
10130 enum remove_bp_reason reason)
10132 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
10133 bl->watchpoint_type);
10136 /* Implement the "resources_needed" method for masked hardware
10137 watchpoints. */
10140 masked_watchpoint::resources_needed (const struct bp_location *bl)
10142 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
10145 /* Implement the "works_in_software_mode" method for masked hardware
10146 watchpoints. */
10148 bool
10149 masked_watchpoint::works_in_software_mode () const
10151 return false;
10154 /* Implement the "print_it" method for masked hardware
10155 watchpoints. */
10157 enum print_stop_action
10158 masked_watchpoint::print_it (const bpstat *bs) const
10160 struct ui_out *uiout = current_uiout;
10162 /* Masked watchpoints have only one location. */
10163 gdb_assert (this->has_single_location ());
10165 annotate_watchpoint (this->number);
10166 maybe_print_thread_hit_breakpoint (uiout);
10168 switch (this->type)
10170 case bp_hardware_watchpoint:
10171 if (uiout->is_mi_like_p ())
10172 uiout->field_string
10173 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10174 break;
10176 case bp_read_watchpoint:
10177 if (uiout->is_mi_like_p ())
10178 uiout->field_string
10179 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10180 break;
10182 case bp_access_watchpoint:
10183 if (uiout->is_mi_like_p ())
10184 uiout->field_string
10185 ("reason",
10186 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10187 break;
10188 default:
10189 internal_error (_("Invalid hardware watchpoint type."));
10192 mention (this);
10193 uiout->text (_("\n\
10194 Check the underlying instruction at PC for the memory\n\
10195 address and value which triggered this watchpoint.\n"));
10196 uiout->text ("\n");
10198 /* More than one watchpoint may have been triggered. */
10199 return PRINT_UNKNOWN;
10202 /* Implement the "print_one_detail" method for masked hardware
10203 watchpoints. */
10205 void
10206 masked_watchpoint::print_one_detail (struct ui_out *uiout) const
10208 /* Masked watchpoints have only one location. */
10209 gdb_assert (this->has_single_location ());
10211 uiout->text ("\tmask ");
10212 uiout->field_core_addr ("mask", this->first_loc ().gdbarch, hw_wp_mask);
10213 uiout->text ("\n");
10216 /* Implement the "print_mention" method for masked hardware
10217 watchpoints. */
10219 void
10220 masked_watchpoint::print_mention () const
10222 struct ui_out *uiout = current_uiout;
10223 const char *tuple_name;
10225 switch (type)
10227 case bp_hardware_watchpoint:
10228 uiout->text ("Masked hardware watchpoint ");
10229 tuple_name = "wpt";
10230 break;
10231 case bp_read_watchpoint:
10232 uiout->text ("Masked hardware read watchpoint ");
10233 tuple_name = "hw-rwpt";
10234 break;
10235 case bp_access_watchpoint:
10236 uiout->text ("Masked hardware access (read/write) watchpoint ");
10237 tuple_name = "hw-awpt";
10238 break;
10239 default:
10240 internal_error (_("Invalid hardware watchpoint type."));
10243 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10244 uiout->field_signed ("number", number);
10245 uiout->text (": ");
10246 uiout->field_string ("exp", exp_string.get ());
10249 /* Implement the "print_recreate" method for masked hardware
10250 watchpoints. */
10252 void
10253 masked_watchpoint::print_recreate (struct ui_file *fp) const
10255 switch (type)
10257 case bp_hardware_watchpoint:
10258 gdb_printf (fp, "watch");
10259 break;
10260 case bp_read_watchpoint:
10261 gdb_printf (fp, "rwatch");
10262 break;
10263 case bp_access_watchpoint:
10264 gdb_printf (fp, "awatch");
10265 break;
10266 default:
10267 internal_error (_("Invalid hardware watchpoint type."));
10270 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
10271 phex (hw_wp_mask, sizeof (CORE_ADDR)));
10272 print_recreate_thread (fp);
10275 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10277 static bool
10278 is_masked_watchpoint (const struct breakpoint *b)
10280 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
10283 /* accessflag: hw_write: watch write,
10284 hw_read: watch read,
10285 hw_access: watch access (read or write) */
10286 static void
10287 watch_command_1 (const char *arg, int accessflag, int from_tty,
10288 bool just_location, bool internal)
10290 struct breakpoint *scope_breakpoint = NULL;
10291 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10292 struct value *result;
10293 int saved_bitpos = 0, saved_bitsize = 0;
10294 const char *exp_start = NULL;
10295 const char *exp_end = NULL;
10296 const char *tok, *end_tok;
10297 int toklen = -1;
10298 const char *cond_start = NULL;
10299 const char *cond_end = NULL;
10300 enum bptype bp_type;
10301 int thread = -1;
10302 int inferior = -1;
10303 /* Flag to indicate whether we are going to use masks for
10304 the hardware watchpoint. */
10305 bool use_mask = false;
10306 CORE_ADDR mask = 0;
10307 int task = -1;
10309 /* Make sure that we actually have parameters to parse. */
10310 if (arg != NULL && arg[0] != '\0')
10312 const char *value_start;
10314 exp_end = arg + strlen (arg);
10316 /* Look for "parameter value" pairs at the end
10317 of the arguments string. */
10318 for (tok = exp_end - 1; tok > arg; tok--)
10320 /* Skip whitespace at the end of the argument list. */
10321 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10322 tok--;
10324 /* Find the beginning of the last token.
10325 This is the value of the parameter. */
10326 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10327 tok--;
10328 value_start = tok + 1;
10330 /* Skip whitespace. */
10331 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10332 tok--;
10334 end_tok = tok;
10336 /* Find the beginning of the second to last token.
10337 This is the parameter itself. */
10338 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10339 tok--;
10340 tok++;
10341 toklen = end_tok - tok + 1;
10343 if (toklen == 6 && startswith (tok, "thread"))
10345 struct thread_info *thr;
10346 /* At this point we've found a "thread" token, which means
10347 the user is trying to set a watchpoint that triggers
10348 only in a specific thread. */
10349 const char *endp;
10351 if (thread != -1)
10352 error(_("You can specify only one thread."));
10354 if (task != -1)
10355 error (_("You can specify only one of thread or task."));
10357 if (inferior != -1)
10358 error (_("You can specify only one of inferior or thread."));
10360 /* Extract the thread ID from the next token. */
10361 thr = parse_thread_id (value_start, &endp);
10362 if (value_start == endp)
10363 error (_("Junk after thread keyword."));
10365 thread = thr->global_num;
10367 else if (toklen == 4 && startswith (tok, "task"))
10369 char *tmp;
10371 if (task != -1)
10372 error(_("You can specify only one task."));
10374 if (thread != -1)
10375 error (_("You can specify only one of thread or task."));
10377 if (inferior != -1)
10378 error (_("You can specify only one of inferior or task."));
10380 task = strtol (value_start, &tmp, 0);
10381 if (tmp == value_start)
10382 error (_("Junk after task keyword."));
10383 if (!valid_task_id (task))
10384 error (_("Unknown task %d."), task);
10386 else if (toklen == 8 && startswith (tok, "inferior"))
10388 /* Support for watchpoints will be added in a later commit. */
10389 error (_("Cannot use 'inferior' keyword with watchpoints"));
10391 else if (toklen == 4 && startswith (tok, "mask"))
10393 /* We've found a "mask" token, which means the user wants to
10394 create a hardware watchpoint that is going to have the mask
10395 facility. */
10396 struct value *mask_value;
10398 if (use_mask)
10399 error(_("You can specify only one mask."));
10401 use_mask = just_location = true;
10403 scoped_value_mark mark;
10404 mask_value = parse_to_comma_and_eval (&value_start);
10405 mask = value_as_address (mask_value);
10407 else
10408 /* We didn't recognize what we found. We should stop here. */
10409 break;
10411 /* Truncate the string and get rid of the "parameter value" pair before
10412 the arguments string is parsed by the parse_exp_1 function. */
10413 exp_end = tok;
10416 else
10417 exp_end = arg;
10419 /* Parse the rest of the arguments. From here on out, everything
10420 is in terms of a newly allocated string instead of the original
10421 ARG. */
10422 std::string expression (arg, exp_end - arg);
10423 exp_start = arg = expression.c_str ();
10424 innermost_block_tracker tracker;
10425 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10426 exp_end = arg;
10427 /* Remove trailing whitespace from the expression before saving it.
10428 This makes the eventual display of the expression string a bit
10429 prettier. */
10430 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10431 --exp_end;
10433 /* Checking if the expression is not constant. */
10434 if (watchpoint_exp_is_const (exp.get ()))
10436 int len;
10438 len = exp_end - exp_start;
10439 while (len > 0 && isspace (exp_start[len - 1]))
10440 len--;
10441 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10444 exp_valid_block = tracker.block ();
10445 struct value *mark = value_mark ();
10446 struct value *val_as_value = nullptr;
10447 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10448 just_location);
10450 if (val_as_value != NULL && just_location)
10452 saved_bitpos = val_as_value->bitpos ();
10453 saved_bitsize = val_as_value->bitsize ();
10456 value_ref_ptr val;
10457 if (just_location)
10459 int ret;
10461 exp_valid_block = NULL;
10462 val = release_value (value_addr (result));
10463 value_free_to_mark (mark);
10465 if (use_mask)
10467 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10468 mask);
10469 if (ret == -1)
10470 error (_("This target does not support masked watchpoints."));
10471 else if (ret == -2)
10472 error (_("Invalid mask or memory region."));
10475 else if (val_as_value != NULL)
10476 val = release_value (val_as_value);
10478 tok = skip_spaces (arg);
10479 end_tok = skip_to_space (tok);
10481 toklen = end_tok - tok;
10482 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10484 tok = cond_start = end_tok + 1;
10485 innermost_block_tracker if_tracker;
10486 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10488 /* The watchpoint expression may not be local, but the condition
10489 may still be. E.g.: `watch global if local > 0'. */
10490 cond_exp_valid_block = if_tracker.block ();
10492 cond_end = tok;
10494 if (*tok)
10495 error (_("Junk at end of command."));
10497 frame_info_ptr wp_frame = block_innermost_frame (exp_valid_block);
10499 /* Save this because create_internal_breakpoint below invalidates
10500 'wp_frame'. */
10501 frame_id watchpoint_frame = get_frame_id (wp_frame);
10503 /* Now set up the breakpoint. We create all watchpoints as hardware
10504 watchpoints here even if hardware watchpoints are turned off, a call
10505 to update_watchpoint later in this function will cause the type to
10506 drop back to bp_watchpoint (software watchpoint) if required. */
10508 if (accessflag == hw_read)
10509 bp_type = bp_read_watchpoint;
10510 else if (accessflag == hw_access)
10511 bp_type = bp_access_watchpoint;
10512 else
10513 bp_type = bp_hardware_watchpoint;
10515 std::unique_ptr<watchpoint> w;
10516 if (use_mask)
10517 w = std::make_unique<masked_watchpoint> (nullptr, bp_type);
10518 else
10519 w = std::make_unique<watchpoint> (nullptr, bp_type);
10521 /* At most one of thread or task can be set on a watchpoint. */
10522 gdb_assert (thread == -1 || task == -1);
10523 w->thread = thread;
10524 w->inferior = inferior;
10525 w->task = task;
10526 w->disposition = disp_donttouch;
10527 w->pspace = current_program_space;
10528 w->exp = std::move (exp);
10529 w->exp_valid_block = exp_valid_block;
10530 w->cond_exp_valid_block = cond_exp_valid_block;
10531 if (just_location)
10533 struct type *t = val->type ();
10534 CORE_ADDR addr = value_as_address (val.get ());
10536 w->exp_string_reparse
10537 = current_language->watch_location_expression (t, addr);
10539 w->exp_string = xstrprintf ("-location %.*s",
10540 (int) (exp_end - exp_start), exp_start);
10542 else
10543 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10545 if (use_mask)
10547 w->hw_wp_mask = mask;
10549 else
10551 w->val = val;
10552 w->val_bitpos = saved_bitpos;
10553 w->val_bitsize = saved_bitsize;
10554 w->val_valid = true;
10557 if (cond_start)
10558 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10559 else
10560 w->cond_string = 0;
10562 if (frame_id_p (watchpoint_frame))
10564 w->watchpoint_frame = watchpoint_frame;
10565 w->watchpoint_thread = inferior_ptid;
10567 else
10569 w->watchpoint_frame = null_frame_id;
10570 w->watchpoint_thread = null_ptid;
10573 if (!just_location)
10574 value_free_to_mark (mark);
10576 /* Finally update the new watchpoint. This creates the locations
10577 that should be inserted. */
10578 update_watchpoint (w.get (), true /* reparse */);
10580 /* If the expression is "local", then set up a "watchpoint scope"
10581 breakpoint at the point where we've left the scope of the watchpoint
10582 expression. Create the scope breakpoint before the watchpoint, so
10583 that we will encounter it first in bpstat_stop_status. */
10584 if (exp_valid_block != nullptr && wp_frame != nullptr)
10586 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10588 if (frame_id_p (caller_frame_id))
10590 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10591 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10593 scope_breakpoint
10594 = create_internal_breakpoint (caller_arch, caller_pc,
10595 bp_watchpoint_scope);
10597 /* create_internal_breakpoint could invalidate WP_FRAME. */
10598 wp_frame = nullptr;
10600 scope_breakpoint->enable_state = bp_enabled;
10602 /* Automatically delete the breakpoint when it hits. */
10603 scope_breakpoint->disposition = disp_del;
10605 /* Only break in the proper frame (help with recursion). */
10606 scope_breakpoint->frame_id = caller_frame_id;
10608 /* Set the address at which we will stop. */
10609 bp_location &loc = scope_breakpoint->first_loc ();
10610 loc.gdbarch = caller_arch;
10611 loc.requested_address = caller_pc;
10612 loc.address
10613 = adjust_breakpoint_address (loc.gdbarch, loc.requested_address,
10614 scope_breakpoint->type,
10615 current_program_space);
10619 if (scope_breakpoint != nullptr)
10621 /* The scope breakpoint is related to the watchpoint. We will
10622 need to act on them together. */
10623 w->related_breakpoint = scope_breakpoint;
10624 scope_breakpoint->related_breakpoint = w.get ();
10627 /* Verify that the scope breakpoint comes before the watchpoint in the
10628 breakpoint chain. */
10629 gdb_assert (scope_breakpoint == nullptr
10630 || &breakpoint_chain.back () == scope_breakpoint);
10631 watchpoint *watchpoint_ptr = w.get ();
10632 install_breakpoint (internal, std::move (w), 1);
10633 gdb_assert (&breakpoint_chain.back () == watchpoint_ptr);
10636 /* Return count of debug registers needed to watch the given expression.
10637 If the watchpoint cannot be handled in hardware return zero. */
10639 static int
10640 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10642 int found_memory_cnt = 0;
10644 /* Did the user specifically forbid us to use hardware watchpoints? */
10645 if (!can_use_hw_watchpoints)
10646 return 0;
10648 gdb_assert (!vals.empty ());
10649 struct value *head = vals[0].get ();
10651 /* Make sure that the value of the expression depends only upon
10652 memory contents, and values computed from them within GDB. If we
10653 find any register references or function calls, we can't use a
10654 hardware watchpoint.
10656 The idea here is that evaluating an expression generates a series
10657 of values, one holding the value of every subexpression. (The
10658 expression a*b+c has five subexpressions: a, b, a*b, c, and
10659 a*b+c.) GDB's values hold almost enough information to establish
10660 the criteria given above --- they identify memory lvalues,
10661 register lvalues, computed values, etcetera. So we can evaluate
10662 the expression, and then scan the chain of values that leaves
10663 behind to decide whether we can detect any possible change to the
10664 expression's final value using only hardware watchpoints.
10666 However, I don't think that the values returned by inferior
10667 function calls are special in any way. So this function may not
10668 notice that an expression involving an inferior function call
10669 can't be watched with hardware watchpoints. FIXME. */
10670 for (const value_ref_ptr &iter : vals)
10672 struct value *v = iter.get ();
10674 if (v->lval () == lval_memory)
10676 if (v != head && v->lazy ())
10677 /* A lazy memory lvalue in the chain is one that GDB never
10678 needed to fetch; we either just used its address (e.g.,
10679 `a' in `a.b') or we never needed it at all (e.g., `a'
10680 in `a,b'). This doesn't apply to HEAD; if that is
10681 lazy then it was not readable, but watch it anyway. */
10683 else
10685 /* Ahh, memory we actually used! Check if we can cover
10686 it with hardware watchpoints. */
10687 struct type *vtype = check_typedef (v->type ());
10689 /* We only watch structs and arrays if user asked for it
10690 explicitly, never if they just happen to appear in a
10691 middle of some value chain. */
10692 if (v == head
10693 || (vtype->code () != TYPE_CODE_STRUCT
10694 && vtype->code () != TYPE_CODE_ARRAY))
10696 CORE_ADDR vaddr = v->address ();
10697 int len;
10698 int num_regs;
10700 len = (target_exact_watchpoints
10701 && is_scalar_type_recursive (vtype))?
10702 1 : v->type ()->length ();
10704 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10705 if (!num_regs)
10706 return 0;
10707 else
10708 found_memory_cnt += num_regs;
10712 else if (v->lval () != not_lval && !v->deprecated_modifiable ())
10713 return 0; /* These are values from the history (e.g., $1). */
10714 else if (v->lval () == lval_register)
10715 return 0; /* Cannot watch a register with a HW watchpoint. */
10718 /* The expression itself looks suitable for using a hardware
10719 watchpoint, but give the target machine a chance to reject it. */
10720 return found_memory_cnt;
10723 void
10724 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10726 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10729 /* Options for the watch, awatch, and rwatch commands. */
10731 struct watch_options
10733 /* For -location. */
10734 bool location = false;
10737 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10739 Historically GDB always accepted both '-location' and '-l' flags for
10740 these commands (both flags being synonyms). When converting to the
10741 newer option scheme only '-location' is added here. That's fine (for
10742 backward compatibility) as any non-ambiguous prefix of a flag will be
10743 accepted, so '-l', '-loc', are now all accepted.
10745 What this means is that, if in the future, we add any new flag here
10746 that starts with '-l' then this will break backward compatibility, so
10747 please, don't do that! */
10749 static const gdb::option::option_def watch_option_defs[] = {
10750 gdb::option::flag_option_def<watch_options> {
10751 "location",
10752 [] (watch_options *opt) { return &opt->location; },
10753 N_("\
10754 This evaluates EXPRESSION and watches the memory to which it refers.\n\
10755 -l can be used as a short form of -location."),
10759 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10760 commands. */
10762 static gdb::option::option_def_group
10763 make_watch_options_def_group (watch_options *opts)
10765 return {{watch_option_defs}, opts};
10768 /* A helper function that looks for the "-location" argument and then
10769 calls watch_command_1. */
10771 static void
10772 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10774 watch_options opts;
10775 auto grp = make_watch_options_def_group (&opts);
10776 gdb::option::process_options
10777 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10778 if (arg != nullptr && *arg == '\0')
10779 arg = nullptr;
10781 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10784 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10785 static void
10786 watch_command_completer (struct cmd_list_element *ignore,
10787 completion_tracker &tracker,
10788 const char *text, const char * /*word*/)
10790 const auto group = make_watch_options_def_group (nullptr);
10791 if (gdb::option::complete_options
10792 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10793 return;
10795 const char *word = advance_to_expression_complete_word_point (tracker, text);
10796 expression_completer (ignore, tracker, text, word);
10799 static void
10800 watch_command (const char *arg, int from_tty)
10802 watch_maybe_just_location (arg, hw_write, from_tty);
10805 void
10806 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10808 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10811 static void
10812 rwatch_command (const char *arg, int from_tty)
10814 watch_maybe_just_location (arg, hw_read, from_tty);
10817 void
10818 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
10820 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10823 static void
10824 awatch_command (const char *arg, int from_tty)
10826 watch_maybe_just_location (arg, hw_access, from_tty);
10830 /* Data for the FSM that manages the until(location)/advance commands
10831 in infcmd.c. Here because it uses the mechanisms of
10832 breakpoints. */
10834 struct until_break_fsm : public thread_fsm
10836 /* The thread that was current when the command was executed. */
10837 int thread;
10839 /* The breakpoint set at the return address in the caller frame,
10840 plus breakpoints at all the destination locations. */
10841 std::vector<breakpoint_up> breakpoints;
10843 until_break_fsm (struct interp *cmd_interp, int thread,
10844 std::vector<breakpoint_up> &&breakpoints)
10845 : thread_fsm (cmd_interp),
10846 thread (thread),
10847 breakpoints (std::move (breakpoints))
10851 void clean_up (struct thread_info *thread) override;
10852 bool should_stop (struct thread_info *thread) override;
10853 enum async_reply_reason do_async_reply_reason () override;
10856 /* Implementation of the 'should_stop' FSM method for the
10857 until(location)/advance commands. */
10859 bool
10860 until_break_fsm::should_stop (struct thread_info *tp)
10862 for (const breakpoint_up &bp : breakpoints)
10863 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10864 bp.get ()) != NULL)
10866 set_finished ();
10867 break;
10870 return true;
10873 /* Implementation of the 'clean_up' FSM method for the
10874 until(location)/advance commands. */
10876 void
10877 until_break_fsm::clean_up (struct thread_info *)
10879 /* Clean up our temporary breakpoints. */
10880 breakpoints.clear ();
10881 delete_longjmp_breakpoint (thread);
10884 /* Implementation of the 'async_reply_reason' FSM method for the
10885 until(location)/advance commands. */
10887 enum async_reply_reason
10888 until_break_fsm::do_async_reply_reason ()
10890 return EXEC_ASYNC_LOCATION_REACHED;
10893 void
10894 until_break_command (const char *arg, int from_tty, int anywhere)
10896 frame_info_ptr frame;
10897 struct gdbarch *frame_gdbarch;
10898 struct frame_id stack_frame_id;
10899 struct frame_id caller_frame_id;
10900 int thread;
10901 struct thread_info *tp;
10903 clear_proceed_status (0);
10905 /* Set a breakpoint where the user wants it and at return from
10906 this function. */
10908 location_spec_up locspec = string_to_location_spec (&arg, current_language);
10910 std::vector<symtab_and_line> sals
10911 = (last_displayed_sal_is_valid ()
10912 ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10913 get_last_displayed_symtab (),
10914 get_last_displayed_line ())
10915 : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
10916 NULL, NULL, 0));
10918 if (sals.empty ())
10919 error (_("Couldn't get information on specified line."));
10921 if (*arg)
10922 error (_("Junk at end of arguments."));
10924 tp = inferior_thread ();
10925 thread = tp->global_num;
10927 /* Note linespec handling above invalidates the frame chain.
10928 Installing a breakpoint also invalidates the frame chain (as it
10929 may need to switch threads), so do any frame handling before
10930 that. */
10932 frame = get_selected_frame (NULL);
10933 frame_gdbarch = get_frame_arch (frame);
10934 stack_frame_id = get_stack_frame_id (frame);
10935 caller_frame_id = frame_unwind_caller_id (frame);
10937 /* Keep within the current frame, or in frames called by the current
10938 one. */
10940 std::vector<breakpoint_up> breakpoints;
10942 std::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10944 if (frame_id_p (caller_frame_id))
10946 struct symtab_and_line sal2;
10947 struct gdbarch *caller_gdbarch;
10949 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10950 sal2.pc = frame_unwind_caller_pc (frame);
10951 caller_gdbarch = frame_unwind_caller_arch (frame);
10953 breakpoint_up caller_breakpoint
10954 = set_momentary_breakpoint (caller_gdbarch, sal2,
10955 caller_frame_id, bp_until);
10956 breakpoints.emplace_back (std::move (caller_breakpoint));
10958 set_longjmp_breakpoint (tp, stack_frame_id);
10959 lj_deleter.emplace (thread);
10962 /* If the user told us to continue until a specified location, we
10963 don't specify a frame at which we need to stop. Otherwise,
10964 specify the selected frame, because we want to stop only at the
10965 very same frame. */
10966 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10968 for (symtab_and_line &sal : sals)
10970 resolve_sal_pc (&sal);
10972 breakpoint_up location_breakpoint
10973 = set_momentary_breakpoint (frame_gdbarch, sal,
10974 stop_frame_id, bp_until);
10975 breakpoints.emplace_back (std::move (location_breakpoint));
10978 tp->set_thread_fsm
10979 (std::unique_ptr<thread_fsm>
10980 (new until_break_fsm (command_interp (), tp->global_num,
10981 std::move (breakpoints))));
10983 if (lj_deleter)
10984 lj_deleter->release ();
10986 proceed (-1, GDB_SIGNAL_DEFAULT);
10991 /* Compare two breakpoints and return a strcmp-like result. */
10993 static int
10994 compare_breakpoints (const breakpoint *a, const breakpoint *b)
10996 uintptr_t ua = (uintptr_t) a;
10997 uintptr_t ub = (uintptr_t) b;
10999 if (a->number < b->number)
11000 return -1;
11001 else if (a->number > b->number)
11002 return 1;
11004 /* Now sort by address, in case we see, e..g, two breakpoints with
11005 the number 0. */
11006 if (ua < ub)
11007 return -1;
11008 return ua > ub ? 1 : 0;
11011 /* Delete breakpoints by address or line. */
11013 static void
11014 clear_command (const char *arg, int from_tty)
11016 int default_match;
11018 std::vector<symtab_and_line> decoded_sals;
11019 symtab_and_line last_sal;
11020 gdb::array_view<symtab_and_line> sals;
11021 if (arg)
11023 decoded_sals
11024 = decode_line_with_current_source (arg,
11025 (DECODE_LINE_FUNFIRSTLINE
11026 | DECODE_LINE_LIST_MODE));
11027 default_match = 0;
11028 sals = decoded_sals;
11030 else
11032 /* Set sal's line, symtab, pc, and pspace to the values
11033 corresponding to the last call to print_frame_info. If the
11034 codepoint is not valid, this will set all the fields to 0. */
11035 last_sal = get_last_displayed_sal ();
11036 if (last_sal.symtab == 0)
11037 error (_("No source file specified."));
11039 default_match = 1;
11040 sals = last_sal;
11043 /* We don't call resolve_sal_pc here. That's not as bad as it
11044 seems, because all existing breakpoints typically have both
11045 file/line and pc set. So, if clear is given file/line, we can
11046 match this to existing breakpoint without obtaining pc at all.
11048 We only support clearing given the address explicitly
11049 present in breakpoint table. Say, we've set breakpoint
11050 at file:line. There were several PC values for that file:line,
11051 due to optimization, all in one block.
11053 We've picked one PC value. If "clear" is issued with another
11054 PC corresponding to the same file:line, the breakpoint won't
11055 be cleared. We probably can still clear the breakpoint, but
11056 since the other PC value is never presented to user, user
11057 can only find it by guessing, and it does not seem important
11058 to support that. */
11060 /* For each line spec given, delete bps which correspond to it. Do
11061 it in two passes, solely to preserve the current behavior that
11062 from_tty is forced true if we delete more than one
11063 breakpoint. */
11065 std::vector<struct breakpoint *> found;
11066 for (const auto &sal : sals)
11068 const char *sal_fullname;
11070 /* If exact pc given, clear bpts at that pc.
11071 If line given (pc == 0), clear all bpts on specified line.
11072 If defaulting, clear all bpts on default line
11073 or at default pc.
11075 defaulting sal.pc != 0 tests to do
11077 0 1 pc
11078 1 1 pc _and_ line
11079 0 0 line
11080 1 0 <can't happen> */
11082 sal_fullname = (sal.symtab == NULL
11083 ? NULL : symtab_to_fullname (sal.symtab));
11085 /* Find all matching breakpoints and add them to 'found'. */
11086 for (breakpoint &b : all_breakpoints ())
11088 int match = 0;
11089 /* Are we going to delete b? */
11090 if (b.type != bp_none && !is_watchpoint (&b)
11091 && user_breakpoint_p (&b))
11093 for (bp_location &loc : b.locations ())
11095 /* If the user specified file:line, don't allow a PC
11096 match. This matches historical gdb behavior. */
11097 int pc_match = (!sal.explicit_line
11098 && sal.pc
11099 && (loc.pspace == sal.pspace)
11100 && (loc.address == sal.pc)
11101 && (!section_is_overlay (loc.section)
11102 || loc.section == sal.section));
11103 int line_match = 0;
11105 if ((default_match || sal.explicit_line)
11106 && loc.symtab != NULL
11107 && sal_fullname != NULL
11108 && sal.pspace == loc.pspace
11109 && loc.line_number == sal.line
11110 && filename_cmp (symtab_to_fullname (loc.symtab),
11111 sal_fullname) == 0)
11112 line_match = 1;
11114 if (pc_match || line_match)
11116 match = 1;
11117 break;
11122 if (match)
11123 found.push_back (&b);
11127 /* Now go through the 'found' chain and delete them. */
11128 if (found.empty ())
11130 if (arg)
11131 error (_("No breakpoint at %s."), arg);
11132 else
11133 error (_("No breakpoint at this line."));
11136 /* Remove duplicates from the vec. */
11137 std::sort (found.begin (), found.end (),
11138 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11140 return compare_breakpoints (bp_a, bp_b) < 0;
11142 found.erase (std::unique (found.begin (), found.end (),
11143 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11145 return compare_breakpoints (bp_a, bp_b) == 0;
11147 found.end ());
11149 if (found.size () > 1)
11150 from_tty = 1; /* Always report if deleted more than one. */
11151 if (from_tty)
11153 if (found.size () == 1)
11154 gdb_printf (_("Deleted breakpoint "));
11155 else
11156 gdb_printf (_("Deleted breakpoints "));
11159 for (breakpoint *iter : found)
11161 if (from_tty)
11162 gdb_printf ("%d ", iter->number);
11163 delete_breakpoint (iter);
11165 if (from_tty)
11166 gdb_putc ('\n');
11169 /* Delete breakpoint in BS if they are `delete' breakpoints and
11170 all breakpoints that are marked for deletion, whether hit or not.
11171 This is called after any breakpoint is hit, or after errors. */
11173 void
11174 breakpoint_auto_delete (bpstat *bs)
11176 for (; bs; bs = bs->next)
11177 if (bs->breakpoint_at
11178 && bs->breakpoint_at->disposition == disp_del
11179 && bs->stop)
11180 delete_breakpoint (bs->breakpoint_at);
11182 for (breakpoint &b : all_breakpoints_safe ())
11183 if (b.disposition == disp_del_at_next_stop)
11184 delete_breakpoint (&b);
11187 /* A comparison function for bp_location AP and BP being interfaced to
11188 std::sort. Sort elements primarily by their ADDRESS (no matter what
11189 bl_address_is_meaningful says), secondarily by ordering first
11190 permanent elements and tertiarily just ensuring the array is sorted
11191 stable way despite std::sort being an unstable algorithm. */
11193 static int
11194 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11196 if (a->address != b->address)
11197 return a->address < b->address;
11199 /* Sort locations at the same address by their pspace number, keeping
11200 locations of the same inferior (in a multi-inferior environment)
11201 grouped. */
11203 if (a->pspace->num != b->pspace->num)
11204 return a->pspace->num < b->pspace->num;
11206 /* Sort permanent breakpoints first. */
11207 if (a->permanent != b->permanent)
11208 return a->permanent > b->permanent;
11210 /* Sort by type in order to make duplicate determination easier.
11211 See update_global_location_list. This is kept in sync with
11212 breakpoint_locations_match. */
11213 if (a->loc_type < b->loc_type)
11214 return true;
11216 /* Likewise, for range-breakpoints, sort by length. */
11217 if (a->loc_type == bp_loc_hardware_breakpoint
11218 && b->loc_type == bp_loc_hardware_breakpoint
11219 && a->length < b->length)
11220 return true;
11222 /* Make the internal GDB representation stable across GDB runs
11223 where A and B memory inside GDB can differ. Breakpoint locations of
11224 the same type at the same address can be sorted in arbitrary order. */
11226 if (a->owner->number != b->owner->number)
11227 return a->owner->number < b->owner->number;
11229 return a < b;
11232 /* Set bp_locations_placed_address_before_address_max and
11233 bp_locations_shadow_len_after_address_max according to the current
11234 content of the bp_locations array. */
11236 static void
11237 bp_locations_target_extensions_update (void)
11239 bp_locations_placed_address_before_address_max = 0;
11240 bp_locations_shadow_len_after_address_max = 0;
11242 for (bp_location *bl : all_bp_locations ())
11244 CORE_ADDR start, end, addr;
11246 if (!bp_location_has_shadow (bl))
11247 continue;
11249 start = bl->target_info.placed_address;
11250 end = start + bl->target_info.shadow_len;
11252 gdb_assert (bl->address >= start);
11253 addr = bl->address - start;
11254 if (addr > bp_locations_placed_address_before_address_max)
11255 bp_locations_placed_address_before_address_max = addr;
11257 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11259 gdb_assert (bl->address < end);
11260 addr = end - bl->address;
11261 if (addr > bp_locations_shadow_len_after_address_max)
11262 bp_locations_shadow_len_after_address_max = addr;
11266 /* Download tracepoint locations if they haven't been. */
11268 static void
11269 download_tracepoint_locations (void)
11271 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11273 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11275 for (breakpoint &b : all_tracepoints ())
11277 bool bp_location_downloaded = false;
11279 if ((b.type == bp_fast_tracepoint
11280 ? !may_insert_fast_tracepoints
11281 : !may_insert_tracepoints))
11282 continue;
11284 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11286 if (target_can_download_tracepoint ())
11287 can_download_tracepoint = TRIBOOL_TRUE;
11288 else
11289 can_download_tracepoint = TRIBOOL_FALSE;
11292 if (can_download_tracepoint == TRIBOOL_FALSE)
11293 break;
11295 for (bp_location &bl : b.locations ())
11297 /* In tracepoint, locations are _never_ duplicated, so
11298 should_be_inserted is equivalent to
11299 unduplicated_should_be_inserted. */
11300 if (!should_be_inserted (&bl) || bl.inserted)
11301 continue;
11303 switch_to_program_space_and_thread (bl.pspace);
11305 target_download_tracepoint (&bl);
11307 bl.inserted = 1;
11308 bp_location_downloaded = true;
11311 tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
11312 t.number_on_target = b.number;
11313 if (bp_location_downloaded)
11314 notify_breakpoint_modified (&b);
11318 /* Swap the insertion/duplication state between two locations. */
11320 static void
11321 swap_insertion (struct bp_location *left, struct bp_location *right)
11323 const int left_inserted = left->inserted;
11324 const int left_duplicate = left->duplicate;
11325 const int left_needs_update = left->needs_update;
11326 const struct bp_target_info left_target_info = left->target_info;
11328 /* Locations of tracepoints can never be duplicated. */
11329 if (is_tracepoint (left->owner))
11330 gdb_assert (!left->duplicate);
11331 if (is_tracepoint (right->owner))
11332 gdb_assert (!right->duplicate);
11334 left->inserted = right->inserted;
11335 left->duplicate = right->duplicate;
11336 left->needs_update = right->needs_update;
11337 left->target_info = right->target_info;
11338 right->inserted = left_inserted;
11339 right->duplicate = left_duplicate;
11340 right->needs_update = left_needs_update;
11341 right->target_info = left_target_info;
11344 /* Force the re-insertion of the locations at ADDRESS. This is called
11345 once a new/deleted/modified duplicate location is found and we are evaluating
11346 conditions on the target's side. Such conditions need to be updated on
11347 the target. */
11349 static void
11350 force_breakpoint_reinsertion (struct bp_location *bl)
11352 CORE_ADDR address = 0;
11353 int pspace_num;
11355 address = bl->address;
11356 pspace_num = bl->pspace->num;
11358 /* This is only meaningful if the target is
11359 evaluating conditions and if the user has
11360 opted for condition evaluation on the target's
11361 side. */
11362 if (gdb_evaluates_breakpoint_condition_p ()
11363 || !target_supports_evaluation_of_breakpoint_conditions ())
11364 return;
11366 /* Flag all breakpoint locations with this address and
11367 the same program space as the location
11368 as "its condition has changed". We need to
11369 update the conditions on the target's side. */
11370 for (bp_location *loc : all_bp_locations_at_addr (address))
11372 if (!is_breakpoint (loc->owner)
11373 || pspace_num != loc->pspace->num)
11374 continue;
11376 /* Flag the location appropriately. We use a different state to
11377 let everyone know that we already updated the set of locations
11378 with addr bl->address and program space bl->pspace. This is so
11379 we don't have to keep calling these functions just to mark locations
11380 that have already been marked. */
11381 loc->condition_changed = condition_updated;
11383 /* Free the agent expression bytecode as well. We will compute
11384 it later on. */
11385 loc->cond_bytecode.reset ();
11389 /* Called whether new breakpoints are created, or existing breakpoints
11390 deleted, to update the global location list and recompute which
11391 locations are duplicate of which.
11393 The INSERT_MODE flag determines whether locations may not, may, or
11394 shall be inserted now. See 'enum ugll_insert_mode' for more
11395 info. */
11397 static void
11398 update_global_location_list (enum ugll_insert_mode insert_mode)
11400 /* Last breakpoint location address that was marked for update. */
11401 CORE_ADDR last_addr = 0;
11402 /* Last breakpoint location program space that was marked for update. */
11403 int last_pspace_num = -1;
11405 breakpoint_debug_printf ("insert_mode = %s",
11406 ugll_insert_mode_text (insert_mode));
11408 /* Used in the duplicates detection below. When iterating over all
11409 bp_locations, points to the first bp_location of a given address.
11410 Breakpoints and watchpoints of different types are never
11411 duplicates of each other. Keep one pointer for each type of
11412 breakpoint/watchpoint, so we only need to loop over all locations
11413 once. */
11414 struct bp_location *bp_loc_first; /* breakpoint */
11415 struct bp_location *wp_loc_first; /* hardware watchpoint */
11416 struct bp_location *awp_loc_first; /* access watchpoint */
11417 struct bp_location *rwp_loc_first; /* read watchpoint */
11419 /* Saved former bp_locations array which we compare against the newly
11420 built bp_locations from the current state of ALL_BREAKPOINTS. */
11421 std::vector<bp_location *> old_locations = std::move (bp_locations);
11422 bp_locations.clear ();
11424 for (breakpoint &b : all_breakpoints ())
11425 for (bp_location &loc : b.locations ())
11426 bp_locations.push_back (&loc);
11428 /* See if we need to "upgrade" a software breakpoint to a hardware
11429 breakpoint. Do this before deciding whether locations are
11430 duplicates. Also do this before sorting because sorting order
11431 depends on location type. */
11432 for (bp_location *loc : bp_locations)
11433 if (!loc->inserted && should_be_inserted (loc))
11434 handle_automatic_hardware_breakpoints (loc);
11436 std::sort (bp_locations.begin (), bp_locations.end (),
11437 bp_location_is_less_than);
11439 bp_locations_target_extensions_update ();
11441 /* Identify bp_location instances that are no longer present in the
11442 new list, and therefore should be freed. Note that it's not
11443 necessary that those locations should be removed from inferior --
11444 if there's another location at the same address (previously
11445 marked as duplicate), we don't need to remove/insert the
11446 location.
11448 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11449 and former bp_location array state respectively. */
11451 size_t loc_i = 0;
11452 for (bp_location *old_loc : old_locations)
11454 /* Tells if 'old_loc' is found among the new locations. If
11455 not, we have to free it. */
11456 bool found_object = false;
11457 /* Tells if the location should remain inserted in the target. */
11458 bool keep_in_target = false;
11459 bool removed = false;
11461 /* Skip LOCP entries which will definitely never be needed.
11462 Stop either at or being the one matching OLD_LOC. */
11463 while (loc_i < bp_locations.size ()
11464 && bp_locations[loc_i]->address < old_loc->address)
11465 loc_i++;
11467 for (size_t loc2_i = loc_i;
11468 (loc2_i < bp_locations.size ()
11469 && bp_locations[loc2_i]->address == old_loc->address);
11470 loc2_i++)
11472 /* Check if this is a new/duplicated location or a duplicated
11473 location that had its condition modified. If so, we want to send
11474 its condition to the target if evaluation of conditions is taking
11475 place there. */
11476 if (bp_locations[loc2_i]->condition_changed == condition_modified
11477 && (last_addr != old_loc->address
11478 || last_pspace_num != old_loc->pspace->num))
11480 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11481 last_pspace_num = old_loc->pspace->num;
11484 if (bp_locations[loc2_i] == old_loc)
11485 found_object = true;
11488 /* We have already handled this address, update it so that we don't
11489 have to go through updates again. */
11490 last_addr = old_loc->address;
11492 /* Target-side condition evaluation: Handle deleted locations. */
11493 if (!found_object)
11494 force_breakpoint_reinsertion (old_loc);
11496 /* If this location is no longer present, and inserted, look if
11497 there's maybe a new location at the same address. If so,
11498 mark that one inserted, and don't remove this one. This is
11499 needed so that we don't have a time window where a breakpoint
11500 at certain location is not inserted. */
11502 if (old_loc->inserted)
11504 /* If the location is inserted now, we might have to remove
11505 it. */
11507 if (found_object && should_be_inserted (old_loc))
11509 /* The location is still present in the location list,
11510 and still should be inserted. Don't do anything. */
11511 keep_in_target = true;
11513 else
11515 /* This location still exists, but it won't be kept in the
11516 target since it may have been disabled. We proceed to
11517 remove its target-side condition. */
11519 /* The location is either no longer present, or got
11520 disabled. See if there's another location at the
11521 same address, in which case we don't need to remove
11522 this one from the target. */
11524 /* OLD_LOC comes from existing struct breakpoint. */
11525 if (bl_address_is_meaningful (old_loc))
11527 for (size_t loc2_i = loc_i;
11528 (loc2_i < bp_locations.size ()
11529 && bp_locations[loc2_i]->address == old_loc->address);
11530 loc2_i++)
11532 bp_location *loc2 = bp_locations[loc2_i];
11534 if (loc2 == old_loc)
11535 continue;
11537 if (breakpoint_locations_match (loc2, old_loc))
11539 /* Read watchpoint locations are switched to
11540 access watchpoints, if the former are not
11541 supported, but the latter are. */
11542 if (is_hardware_watchpoint (old_loc->owner))
11544 gdb_assert (is_hardware_watchpoint (loc2->owner));
11545 loc2->watchpoint_type = old_loc->watchpoint_type;
11548 /* loc2 is a duplicated location. We need to check
11549 if it should be inserted in case it will be
11550 unduplicated. */
11551 if (unduplicated_should_be_inserted (loc2))
11553 swap_insertion (old_loc, loc2);
11554 keep_in_target = true;
11555 break;
11562 if (!keep_in_target)
11564 if (remove_breakpoint (old_loc))
11566 /* This is just about all we can do. We could keep
11567 this location on the global list, and try to
11568 remove it next time, but there's no particular
11569 reason why we will succeed next time.
11571 Note that at this point, old_loc->owner is still
11572 valid, as delete_breakpoint frees the breakpoint
11573 only after calling us. */
11574 warning (_("error removing breakpoint %d at %s"),
11575 old_loc->owner->number,
11576 paddress (old_loc->gdbarch, old_loc->address));
11578 removed = true;
11582 if (!found_object)
11584 if (removed && target_is_non_stop_p ()
11585 && need_moribund_for_location_type (old_loc))
11587 /* This location was removed from the target. In
11588 non-stop mode, a race condition is possible where
11589 we've removed a breakpoint, but stop events for that
11590 breakpoint are already queued and will arrive later.
11591 We apply an heuristic to be able to distinguish such
11592 SIGTRAPs from other random SIGTRAPs: we keep this
11593 breakpoint location for a bit, and will retire it
11594 after we see some number of events. The theory here
11595 is that reporting of events should, "on the average",
11596 be fair, so after a while we'll see events from all
11597 threads that have anything of interest, and no longer
11598 need to keep this breakpoint location around. We
11599 don't hold locations forever so to reduce chances of
11600 mistaking a non-breakpoint SIGTRAP for a breakpoint
11601 SIGTRAP.
11603 The heuristic failing can be disastrous on
11604 decr_pc_after_break targets.
11606 On decr_pc_after_break targets, like e.g., x86-linux,
11607 if we fail to recognize a late breakpoint SIGTRAP,
11608 because events_till_retirement has reached 0 too
11609 soon, we'll fail to do the PC adjustment, and report
11610 a random SIGTRAP to the user. When the user resumes
11611 the inferior, it will most likely immediately crash
11612 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11613 corrupted, because of being resumed e.g., in the
11614 middle of a multi-byte instruction, or skipped a
11615 one-byte instruction. This was actually seen happen
11616 on native x86-linux, and should be less rare on
11617 targets that do not support new thread events, like
11618 remote, due to the heuristic depending on
11619 thread_count.
11621 Mistaking a random SIGTRAP for a breakpoint trap
11622 causes similar symptoms (PC adjustment applied when
11623 it shouldn't), but then again, playing with SIGTRAPs
11624 behind the debugger's back is asking for trouble.
11626 Since hardware watchpoint traps are always
11627 distinguishable from other traps, so we don't need to
11628 apply keep hardware watchpoint moribund locations
11629 around. We simply always ignore hardware watchpoint
11630 traps we can no longer explain. */
11632 process_stratum_target *proc_target = nullptr;
11633 for (inferior *inf : all_inferiors ())
11634 if (inf->pspace == old_loc->pspace)
11636 proc_target = inf->process_target ();
11637 break;
11639 if (proc_target != nullptr)
11640 old_loc->events_till_retirement
11641 = 3 * (thread_count (proc_target) + 1);
11642 else
11643 old_loc->events_till_retirement = 1;
11644 old_loc->owner = NULL;
11646 moribund_locations.push_back (old_loc);
11648 else
11650 old_loc->owner = NULL;
11651 decref_bp_location (&old_loc);
11656 /* Rescan breakpoints at the same address and section, marking the
11657 first one as "first" and any others as "duplicates". This is so
11658 that the bpt instruction is only inserted once. If we have a
11659 permanent breakpoint at the same place as BPT, make that one the
11660 official one, and the rest as duplicates. Permanent breakpoints
11661 are sorted first for the same address.
11663 Do the same for hardware watchpoints, but also considering the
11664 watchpoint's type (regular/access/read) and length. */
11666 bp_loc_first = NULL;
11667 wp_loc_first = NULL;
11668 awp_loc_first = NULL;
11669 rwp_loc_first = NULL;
11671 for (bp_location *loc : all_bp_locations ())
11673 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11674 non-NULL. */
11675 struct bp_location **loc_first_p;
11676 breakpoint *b = loc->owner;
11678 if (!unduplicated_should_be_inserted (loc)
11679 || !bl_address_is_meaningful (loc)
11680 /* Don't detect duplicate for tracepoint locations because they are
11681 never duplicated. See the comments in field `duplicate' of
11682 `struct bp_location'. */
11683 || is_tracepoint (b))
11685 /* Clear the condition modification flag. */
11686 loc->condition_changed = condition_unchanged;
11687 continue;
11690 if (b->type == bp_hardware_watchpoint)
11691 loc_first_p = &wp_loc_first;
11692 else if (b->type == bp_read_watchpoint)
11693 loc_first_p = &rwp_loc_first;
11694 else if (b->type == bp_access_watchpoint)
11695 loc_first_p = &awp_loc_first;
11696 else
11697 loc_first_p = &bp_loc_first;
11699 if (*loc_first_p == NULL
11700 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11701 || !breakpoint_locations_match (loc, *loc_first_p))
11703 *loc_first_p = loc;
11704 loc->duplicate = 0;
11706 if (is_breakpoint (loc->owner) && loc->condition_changed)
11708 loc->needs_update = 1;
11709 /* Clear the condition modification flag. */
11710 loc->condition_changed = condition_unchanged;
11712 continue;
11716 /* This and the above ensure the invariant that the first location
11717 is not duplicated, and is the inserted one.
11718 All following are marked as duplicated, and are not inserted. */
11719 if (loc->inserted)
11720 swap_insertion (loc, *loc_first_p);
11721 loc->duplicate = 1;
11723 /* Clear the condition modification flag. */
11724 loc->condition_changed = condition_unchanged;
11727 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11729 if (insert_mode != UGLL_DONT_INSERT)
11730 insert_breakpoint_locations ();
11731 else
11733 /* Even though the caller told us to not insert new
11734 locations, we may still need to update conditions on the
11735 target's side of breakpoints that were already inserted
11736 if the target is evaluating breakpoint conditions. We
11737 only update conditions for locations that are marked
11738 "needs_update". */
11739 update_inserted_breakpoint_locations ();
11743 if (insert_mode != UGLL_DONT_INSERT)
11744 download_tracepoint_locations ();
11747 void
11748 breakpoint_retire_moribund (void)
11750 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11752 struct bp_location *loc = moribund_locations[ix];
11753 if (--(loc->events_till_retirement) == 0)
11755 decref_bp_location (&loc);
11756 unordered_remove (moribund_locations, ix);
11757 --ix;
11762 static void
11763 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11768 update_global_location_list (insert_mode);
11770 catch (const gdb_exception_error &e)
11775 /* Clear BKP from a BPS. */
11777 static void
11778 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
11780 bpstat *bs;
11782 for (bs = bps; bs; bs = bs->next)
11783 if (bs->breakpoint_at == bpt)
11785 bs->breakpoint_at = NULL;
11786 bs->old_val = NULL;
11787 /* bs->commands will be freed later. */
11791 /* Callback for iterate_over_threads. */
11792 static int
11793 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11795 struct breakpoint *bpt = (struct breakpoint *) data;
11797 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11798 return 0;
11801 /* See breakpoint.h. */
11803 void
11804 code_breakpoint::say_where () const
11806 struct value_print_options opts;
11808 get_user_print_options (&opts);
11810 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11811 single string. */
11812 if (!this->has_locations ())
11814 /* For pending locations, the output differs slightly based
11815 on extra_string. If this is non-NULL, it contains either
11816 a condition or dprintf arguments. */
11817 if (extra_string == NULL)
11819 gdb_printf (_(" (%s) pending."), locspec->to_string ());
11821 else if (type == bp_dprintf)
11823 gdb_printf (_(" (%s,%s) pending."),
11824 locspec->to_string (),
11825 extra_string.get ());
11827 else
11829 gdb_printf (_(" (%s %s) pending."),
11830 locspec->to_string (),
11831 extra_string.get ());
11834 else
11836 const bp_location &bl = this->first_loc ();
11837 if (opts.addressprint || bl.symtab == nullptr)
11838 gdb_printf (" at %ps",
11839 styled_string (address_style.style (),
11840 paddress (bl.gdbarch,
11841 bl.address)));
11842 if (bl.symtab != NULL)
11844 /* If there is a single location, we can print the location
11845 more nicely. */
11846 if (!this->has_multiple_locations ())
11848 const char *filename
11849 = symtab_to_filename_for_display (bl.symtab);
11850 gdb_printf (": file %ps, line %ps.",
11851 styled_string (file_name_style.style (),
11852 filename),
11853 styled_string (line_number_style.style (),
11854 pulongest (bl.line_number)));
11856 else
11857 /* This is not ideal, but each location may have a
11858 different file name, and this at least reflects the
11859 real situation somewhat. */
11860 gdb_printf (": %s.", locspec->to_string ());
11863 if (this->has_multiple_locations ())
11865 int n = std::distance (m_locations.begin (), m_locations.end ());
11866 gdb_printf (" (%d locations)", n);
11871 /* See breakpoint.h. */
11873 bp_location_range
11874 breakpoint::locations () const
11876 return bp_location_range (m_locations.begin (), m_locations.end ());
11879 struct bp_location *
11880 breakpoint::allocate_location ()
11882 return new bp_location (this);
11885 /* See breakpoint.h. */
11887 void
11888 breakpoint::add_location (bp_location &loc)
11890 gdb_assert (loc.owner == this);
11891 gdb_assert (!loc.is_linked ());
11893 auto ub = std::upper_bound (m_locations.begin (), m_locations.end (),
11894 loc,
11895 [] (const bp_location &left,
11896 const bp_location &right)
11897 { return left.address < right.address; });
11898 m_locations.insert (ub, loc);
11901 /* See breakpoint.h. */
11903 void
11904 breakpoint::unadd_location (bp_location &loc)
11906 gdb_assert (loc.owner == this);
11907 gdb_assert (loc.is_linked ());
11909 m_locations.erase (m_locations.iterator_to (loc));
11912 #define internal_error_pure_virtual_called() \
11913 gdb_assert_not_reached ("pure virtual function called")
11916 breakpoint::insert_location (struct bp_location *bl)
11918 internal_error_pure_virtual_called ();
11922 breakpoint::remove_location (struct bp_location *bl,
11923 enum remove_bp_reason reason)
11925 internal_error_pure_virtual_called ();
11929 breakpoint::breakpoint_hit (const struct bp_location *bl,
11930 const address_space *aspace,
11931 CORE_ADDR bp_addr,
11932 const target_waitstatus &ws)
11934 internal_error_pure_virtual_called ();
11938 breakpoint::resources_needed (const struct bp_location *bl)
11940 internal_error_pure_virtual_called ();
11943 enum print_stop_action
11944 breakpoint::print_it (const bpstat *bs) const
11946 internal_error_pure_virtual_called ();
11949 void
11950 breakpoint::print_mention () const
11952 internal_error_pure_virtual_called ();
11955 void
11956 breakpoint::print_recreate (struct ui_file *fp) const
11958 internal_error_pure_virtual_called ();
11961 /* Default breakpoint_ops methods. */
11963 void
11964 code_breakpoint::re_set (struct program_space *pspace)
11966 /* FIXME: is this still reachable? */
11967 if (breakpoint_location_spec_empty_p (this))
11969 /* Anything without a location can't be re-set. */
11970 delete_breakpoint (this);
11971 return;
11974 re_set_default (pspace);
11978 code_breakpoint::insert_location (struct bp_location *bl)
11980 CORE_ADDR addr = bl->target_info.reqstd_address;
11982 bl->target_info.kind = breakpoint_kind (bl, &addr);
11983 bl->target_info.placed_address = addr;
11985 int result;
11986 if (bl->loc_type == bp_loc_hardware_breakpoint)
11987 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
11988 else
11989 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11991 if (result == 0 && bl->probe.prob != nullptr)
11993 /* The insertion was successful, now let's set the probe's semaphore
11994 if needed. */
11995 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11998 return result;
12002 code_breakpoint::remove_location (struct bp_location *bl,
12003 enum remove_bp_reason reason)
12005 if (bl->probe.prob != nullptr)
12007 /* Let's clear the semaphore before removing the location. */
12008 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12011 if (bl->loc_type == bp_loc_hardware_breakpoint)
12012 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12013 else
12014 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
12018 code_breakpoint::breakpoint_hit (const struct bp_location *bl,
12019 const address_space *aspace,
12020 CORE_ADDR bp_addr,
12021 const target_waitstatus &ws)
12023 if (ws.kind () != TARGET_WAITKIND_STOPPED
12024 || ws.sig () != GDB_SIGNAL_TRAP)
12025 return 0;
12027 if (!breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
12028 aspace, bp_addr))
12029 return 0;
12031 if (overlay_debugging /* unmapped overlay section */
12032 && section_is_overlay (bl->section)
12033 && !section_is_mapped (bl->section))
12034 return 0;
12036 return 1;
12040 dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
12041 const address_space *aspace,
12042 CORE_ADDR bp_addr,
12043 const target_waitstatus &ws)
12045 if (dprintf_style == dprintf_style_agent
12046 && target_can_run_breakpoint_commands ())
12048 /* An agent-style dprintf never causes a stop. If we see a trap
12049 for this address it must be for a breakpoint that happens to
12050 be set at the same address. */
12051 return 0;
12054 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
12058 ordinary_breakpoint::resources_needed (const struct bp_location *bl)
12060 gdb_assert (type == bp_hardware_breakpoint);
12062 return 1;
12065 enum print_stop_action
12066 ordinary_breakpoint::print_it (const bpstat *bs) const
12068 const struct bp_location *bl;
12069 int bp_temp;
12070 struct ui_out *uiout = current_uiout;
12072 bl = bs->bp_location_at.get ();
12074 bp_temp = disposition == disp_del;
12075 if (bl->address != bl->requested_address)
12076 breakpoint_adjustment_warning (bl->requested_address,
12077 bl->address,
12078 number, true);
12079 annotate_breakpoint (number);
12080 maybe_print_thread_hit_breakpoint (uiout);
12082 if (uiout->is_mi_like_p ())
12084 uiout->field_string ("reason",
12085 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12086 uiout->field_string ("disp", bpdisp_text (disposition));
12089 if (bp_temp)
12090 uiout->text ("Temporary breakpoint ");
12091 else
12092 uiout->text ("Breakpoint ");
12093 print_num_locno (bs, uiout);
12094 uiout->text (", ");
12096 return PRINT_SRC_AND_LOC;
12099 void
12100 ordinary_breakpoint::print_mention () const
12102 if (current_uiout->is_mi_like_p ())
12103 return;
12105 switch (type)
12107 case bp_breakpoint:
12108 case bp_gnu_ifunc_resolver:
12109 if (disposition == disp_del)
12110 gdb_printf (_("Temporary breakpoint"));
12111 else
12112 gdb_printf (_("Breakpoint"));
12113 gdb_printf (_(" %d"), number);
12114 if (type == bp_gnu_ifunc_resolver)
12115 gdb_printf (_(" at gnu-indirect-function resolver"));
12116 break;
12117 case bp_hardware_breakpoint:
12118 gdb_printf (_("Hardware assisted breakpoint %d"), number);
12119 break;
12120 case bp_dprintf:
12121 gdb_printf (_("Dprintf %d"), number);
12122 break;
12125 say_where ();
12128 void
12129 ordinary_breakpoint::print_recreate (struct ui_file *fp) const
12131 if (type == bp_breakpoint && disposition == disp_del)
12132 gdb_printf (fp, "tbreak");
12133 else if (type == bp_breakpoint)
12134 gdb_printf (fp, "break");
12135 else if (type == bp_hardware_breakpoint
12136 && disposition == disp_del)
12137 gdb_printf (fp, "thbreak");
12138 else if (type == bp_hardware_breakpoint)
12139 gdb_printf (fp, "hbreak");
12140 else
12141 internal_error (_("unhandled breakpoint type %d"), (int) type);
12143 gdb_printf (fp, " %s", locspec->to_string ());
12145 /* Print out extra_string if this breakpoint is pending. It might
12146 contain, for example, conditions that were set by the user. */
12147 if (!this->has_locations () && extra_string != NULL)
12148 gdb_printf (fp, " %s", extra_string.get ());
12150 print_recreate_thread (fp);
12153 std::vector<symtab_and_line>
12154 code_breakpoint::decode_location_spec (location_spec *locspec,
12155 program_space *search_pspace)
12157 if (locspec->type () == PROBE_LOCATION_SPEC)
12158 return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
12160 struct linespec_result canonical;
12162 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
12163 NULL, 0, &canonical, multiple_symbols_all,
12164 filter.get ());
12166 /* We should get 0 or 1 resulting SALs. */
12167 gdb_assert (canonical.lsals.size () < 2);
12169 if (!canonical.lsals.empty ())
12171 const linespec_sals &lsal = canonical.lsals[0];
12172 return std::move (lsal.sals);
12174 return {};
12177 /* Virtual table for internal breakpoints. */
12179 void
12180 internal_breakpoint::re_set (struct program_space *pspace)
12182 switch (type)
12184 /* Delete overlay event and longjmp master breakpoints; they
12185 will be reset later by breakpoint_re_set. */
12186 case bp_overlay_event:
12187 case bp_longjmp_master:
12188 case bp_std_terminate_master:
12189 case bp_exception_master:
12190 delete_breakpoint (this);
12191 break;
12193 /* This breakpoint is special, it's set up when the inferior
12194 starts and we really don't want to touch it. */
12195 case bp_shlib_event:
12197 /* Like bp_shlib_event, this breakpoint type is special. Once
12198 it is set up, we do not want to touch it. */
12199 case bp_thread_event:
12200 break;
12204 void
12205 internal_breakpoint::check_status (bpstat *bs)
12207 if (type == bp_shlib_event)
12209 /* If requested, stop when the dynamic linker notifies GDB of
12210 events. This allows the user to get control and place
12211 breakpoints in initializer routines for dynamically loaded
12212 objects (among other things). */
12213 bs->stop = stop_on_solib_events != 0;
12214 bs->print = stop_on_solib_events != 0;
12216 else
12217 bs->stop = false;
12220 enum print_stop_action
12221 internal_breakpoint::print_it (const bpstat *bs) const
12223 switch (type)
12225 case bp_shlib_event:
12226 /* Did we stop because the user set the stop_on_solib_events
12227 variable? (If so, we report this as a generic, "Stopped due
12228 to shlib event" message.) */
12229 print_solib_event (false);
12230 break;
12232 case bp_thread_event:
12233 /* Not sure how we will get here.
12234 GDB should not stop for these breakpoints. */
12235 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
12236 break;
12238 case bp_overlay_event:
12239 /* By analogy with the thread event, GDB should not stop for these. */
12240 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12241 break;
12243 case bp_longjmp_master:
12244 /* These should never be enabled. */
12245 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12246 break;
12248 case bp_std_terminate_master:
12249 /* These should never be enabled. */
12250 gdb_printf (_("std::terminate Master Breakpoint: "
12251 "gdb should not stop!\n"));
12252 break;
12254 case bp_exception_master:
12255 /* These should never be enabled. */
12256 gdb_printf (_("Exception Master Breakpoint: "
12257 "gdb should not stop!\n"));
12258 break;
12261 return PRINT_NOTHING;
12264 void
12265 internal_breakpoint::print_mention () const
12267 /* Nothing to mention. These breakpoints are internal. */
12270 /* Virtual table for momentary breakpoints */
12272 void
12273 momentary_breakpoint::re_set (struct program_space *pspace)
12275 /* Keep temporary breakpoints, which can be encountered when we step
12276 over a dlopen call and solib_add is resetting the breakpoints.
12277 Otherwise these should have been blown away via the cleanup chain
12278 or by breakpoint_init_inferior when we rerun the executable. */
12281 void
12282 momentary_breakpoint::check_status (bpstat *bs)
12284 /* Nothing. The point of these breakpoints is causing a stop. */
12287 enum print_stop_action
12288 momentary_breakpoint::print_it (const bpstat *bs) const
12290 return PRINT_UNKNOWN;
12293 void
12294 momentary_breakpoint::print_mention () const
12296 /* Nothing to mention. These breakpoints are internal. */
12299 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12301 It gets cleared already on the removal of the first one of such placed
12302 breakpoints. This is OK as they get all removed altogether. */
12304 longjmp_breakpoint::~longjmp_breakpoint ()
12306 thread_info *tp = find_thread_global_id (this->thread);
12308 if (tp != NULL)
12309 tp->initiating_frame = null_frame_id;
12312 static void
12313 bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
12314 struct linespec_result *canonical,
12315 struct program_space *search_pspace)
12318 struct linespec_sals lsal;
12320 lsal.sals = parse_probes (locspec, search_pspace, canonical);
12321 lsal.canonical = xstrdup (canonical->locspec->to_string ());
12322 canonical->lsals.push_back (std::move (lsal));
12325 static std::vector<symtab_and_line>
12326 bkpt_probe_decode_location_spec (struct breakpoint *b,
12327 location_spec *locspec,
12328 program_space *search_pspace)
12330 std::vector<symtab_and_line> sals
12331 = parse_probes (locspec, search_pspace, NULL);
12332 if (sals.empty ())
12333 error (_("probe not found"));
12334 return sals;
12338 tracepoint::breakpoint_hit (const struct bp_location *bl,
12339 const address_space *aspace, CORE_ADDR bp_addr,
12340 const target_waitstatus &ws)
12342 /* By definition, the inferior does not report stops at
12343 tracepoints. */
12344 return 0;
12347 void
12348 tracepoint::print_one_detail (struct ui_out *uiout) const
12350 if (!static_trace_marker_id.empty ())
12352 gdb_assert (type == bp_static_tracepoint
12353 || type == bp_static_marker_tracepoint);
12355 uiout->message ("\tmarker id is %pF\n",
12356 string_field ("static-tracepoint-marker-string-id",
12357 static_trace_marker_id.c_str ()));
12361 void
12362 tracepoint::print_mention () const
12364 if (current_uiout->is_mi_like_p ())
12365 return;
12367 switch (type)
12369 case bp_tracepoint:
12370 gdb_printf (_("Tracepoint"));
12371 gdb_printf (_(" %d"), number);
12372 break;
12373 case bp_fast_tracepoint:
12374 gdb_printf (_("Fast tracepoint"));
12375 gdb_printf (_(" %d"), number);
12376 break;
12377 case bp_static_tracepoint:
12378 case bp_static_marker_tracepoint:
12379 gdb_printf (_("Static tracepoint"));
12380 gdb_printf (_(" %d"), number);
12381 break;
12382 default:
12383 internal_error (_("unhandled tracepoint type %d"), (int) type);
12386 say_where ();
12389 void
12390 tracepoint::print_recreate (struct ui_file *fp) const
12392 if (type == bp_fast_tracepoint)
12393 gdb_printf (fp, "ftrace");
12394 else if (type == bp_static_tracepoint
12395 || type == bp_static_marker_tracepoint)
12396 gdb_printf (fp, "strace");
12397 else if (type == bp_tracepoint)
12398 gdb_printf (fp, "trace");
12399 else
12400 internal_error (_("unhandled tracepoint type %d"), (int) type);
12402 gdb_printf (fp, " %s", locspec->to_string ());
12403 print_recreate_thread (fp);
12405 if (pass_count)
12406 gdb_printf (fp, " passcount %d\n", pass_count);
12409 void
12410 dprintf_breakpoint::re_set (struct program_space *pspace)
12412 re_set_default (pspace);
12414 /* 1 - connect to target 1, that can run breakpoint commands.
12415 2 - create a dprintf, which resolves fine.
12416 3 - disconnect from target 1
12417 4 - connect to target 2, that can NOT run breakpoint commands.
12419 After steps #3/#4, you'll want the dprintf command list to
12420 be updated, because target 1 and 2 may well return different
12421 answers for target_can_run_breakpoint_commands().
12422 Given absence of finer grained resetting, we get to do
12423 it all the time. */
12424 update_dprintf_command_list (this);
12427 /* Implement the "print_recreate" method for dprintf. */
12429 void
12430 dprintf_breakpoint::print_recreate (struct ui_file *fp) const
12432 gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
12433 print_recreate_thread (fp);
12436 /* Implement the "after_condition_true" method for dprintf.
12438 dprintf's are implemented with regular commands in their command
12439 list, but we run the commands here instead of before presenting the
12440 stop to the user, as dprintf's don't actually cause a stop. This
12441 also makes it so that the commands of multiple dprintfs at the same
12442 address are all handled. */
12444 void
12445 dprintf_breakpoint::after_condition_true (struct bpstat *bs)
12447 /* dprintf's never cause a stop. This wasn't set in the
12448 check_status hook instead because that would make the dprintf's
12449 condition not be evaluated. */
12450 bs->stop = false;
12452 /* Run the command list here. Take ownership of it instead of
12453 copying. We never want these commands to run later in
12454 bpstat_do_actions, if a breakpoint that causes a stop happens to
12455 be set at same address as this dprintf, or even if running the
12456 commands here throws. */
12457 counted_command_line cmds = std::move (bs->commands);
12458 gdb_assert (cmds != nullptr);
12459 execute_control_commands (cmds.get (), 0);
12462 /* The breakpoint_ops structure to be used on static tracepoints with
12463 markers (`-m'). */
12465 static void
12466 strace_marker_create_sals_from_location_spec
12467 (location_spec *locspec,
12468 struct linespec_result *canonical,
12469 struct program_space *search_pspace)
12471 struct linespec_sals lsal;
12472 const char *arg_start, *arg;
12474 arg = arg_start = as_linespec_location_spec (locspec)->spec_string.get ();
12475 lsal.sals = decode_static_tracepoint_spec (&arg);
12477 std::string str (arg_start, arg - arg_start);
12478 const char *ptr = str.c_str ();
12479 canonical->locspec
12480 = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
12482 lsal.canonical = xstrdup (canonical->locspec->to_string ());
12483 canonical->lsals.push_back (std::move (lsal));
12486 static void
12487 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12488 struct linespec_result *canonical,
12489 gdb::unique_xmalloc_ptr<char> cond_string,
12490 gdb::unique_xmalloc_ptr<char> extra_string,
12491 enum bptype type_wanted,
12492 enum bpdisp disposition,
12493 int thread,
12494 int task, int inferior,
12495 int ignore_count,
12496 int from_tty, int enabled,
12497 int internal, unsigned flags)
12499 const linespec_sals &lsal = canonical->lsals[0];
12501 /* If the user is creating a static tracepoint by marker id
12502 (strace -m MARKER_ID), then store the sals index, so that
12503 breakpoint_re_set can try to match up which of the newly
12504 found markers corresponds to this one, and, don't try to
12505 expand multiple locations for each sal, given than SALS
12506 already should contain all sals for MARKER_ID. */
12508 for (size_t i = 0; i < lsal.sals.size (); i++)
12510 location_spec_up locspec = canonical->locspec->clone ();
12512 std::unique_ptr<tracepoint> tp
12513 (new tracepoint (gdbarch,
12514 type_wanted,
12515 lsal.sals[i],
12516 std::move (locspec),
12517 NULL,
12518 std::move (cond_string),
12519 std::move (extra_string),
12520 disposition,
12521 thread, task, inferior, ignore_count,
12522 from_tty, enabled, flags,
12523 canonical->special_display));
12525 /* Given that its possible to have multiple markers with
12526 the same string id, if the user is creating a static
12527 tracepoint by marker id ("strace -m MARKER_ID"), then
12528 store the sals index, so that breakpoint_re_set can
12529 try to match up which of the newly found markers
12530 corresponds to this one */
12531 tp->static_trace_marker_id_idx = i;
12533 install_breakpoint (internal, std::move (tp), 0);
12537 std::vector<symtab_and_line>
12538 static_marker_tracepoint::decode_location_spec (location_spec *locspec,
12539 program_space *search_pspace)
12541 const char *s = as_linespec_location_spec (locspec)->spec_string.get ();
12543 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12544 if (sals.size () > static_trace_marker_id_idx)
12546 sals[0] = sals[static_trace_marker_id_idx];
12547 sals.resize (1);
12548 return sals;
12550 else
12551 error (_("marker %s not found"), static_trace_marker_id.c_str ());
12554 /* Static tracepoints with marker (`-m'). */
12555 static struct breakpoint_ops strace_marker_breakpoint_ops =
12557 strace_marker_create_sals_from_location_spec,
12558 strace_marker_create_breakpoints_sal,
12561 static bool
12562 strace_marker_p (struct breakpoint *b)
12564 return b->type == bp_static_marker_tracepoint;
12567 /* Notify interpreters and observers that breakpoint B was deleted. */
12569 static void
12570 notify_breakpoint_deleted (breakpoint *b)
12572 interps_notify_breakpoint_deleted (b);
12573 gdb::observers::breakpoint_deleted.notify (b);
12576 /* Delete a breakpoint and clean up all traces of it in the data
12577 structures. */
12579 void
12580 delete_breakpoint (struct breakpoint *bpt)
12582 gdb_assert (bpt != NULL);
12584 /* Has this bp already been deleted? This can happen because
12585 multiple lists can hold pointers to bp's. bpstat lists are
12586 especial culprits.
12588 One example of this happening is a watchpoint's scope bp. When
12589 the scope bp triggers, we notice that the watchpoint is out of
12590 scope, and delete it. We also delete its scope bp. But the
12591 scope bp is marked "auto-deleting", and is already on a bpstat.
12592 That bpstat is then checked for auto-deleting bp's, which are
12593 deleted.
12595 A real solution to this problem might involve reference counts in
12596 bp's, and/or giving them pointers back to their referencing
12597 bpstat's, and teaching delete_breakpoint to only free a bp's
12598 storage when no more references were extent. A cheaper bandaid
12599 was chosen. */
12600 if (bpt->type == bp_none)
12601 return;
12603 /* At least avoid this stale reference until the reference counting
12604 of breakpoints gets resolved. */
12605 if (bpt->related_breakpoint != bpt)
12607 struct breakpoint *related;
12608 struct watchpoint *w;
12610 if (bpt->type == bp_watchpoint_scope)
12611 w = gdb::checked_static_cast<watchpoint *> (bpt->related_breakpoint);
12612 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12613 w = gdb::checked_static_cast<watchpoint *> (bpt);
12614 else
12615 w = NULL;
12616 if (w != NULL)
12617 watchpoint_del_at_next_stop (w);
12619 /* Unlink bpt from the bpt->related_breakpoint ring. */
12620 for (related = bpt; related->related_breakpoint != bpt;
12621 related = related->related_breakpoint);
12622 related->related_breakpoint = bpt->related_breakpoint;
12623 bpt->related_breakpoint = bpt;
12626 /* watch_command_1 creates a watchpoint but only sets its number if
12627 update_watchpoint succeeds in creating its bp_locations. If there's
12628 a problem in that process, we'll be asked to delete the half-created
12629 watchpoint. In that case, don't announce the deletion. */
12630 if (bpt->number)
12631 notify_breakpoint_deleted (bpt);
12633 breakpoint_chain.erase (breakpoint_chain.iterator_to (*bpt));
12635 /* Be sure no bpstat's are pointing at the breakpoint after it's
12636 been freed. */
12637 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12638 in all threads for now. Note that we cannot just remove bpstats
12639 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12640 commands are associated with the bpstat; if we remove it here,
12641 then the later call to bpstat_do_actions (&stop_bpstat); in
12642 event-top.c won't do anything, and temporary breakpoints with
12643 commands won't work. */
12645 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12647 /* Now that breakpoint is removed from breakpoint list, update the
12648 global location list. This will remove locations that used to
12649 belong to this breakpoint. Do this before freeing the breakpoint
12650 itself, since remove_breakpoint looks at location's owner. It
12651 might be better design to have location completely
12652 self-contained, but it's not the case now.
12654 Clear the location linked list first, otherwise, the intrusive_list
12655 destructor accesses the locations after they are freed. */
12656 bpt->clear_locations ();
12657 update_global_location_list (UGLL_DONT_INSERT);
12659 /* On the chance that someone will soon try again to delete this
12660 same bp, we mark it as deleted before freeing its storage. */
12661 bpt->type = bp_none;
12662 delete bpt;
12665 /* Iterator function to call a user-provided callback function once
12666 for each of B and its related breakpoints. */
12668 static void
12669 iterate_over_related_breakpoints (struct breakpoint *b,
12670 gdb::function_view<void (breakpoint *)> function)
12672 struct breakpoint *related;
12674 related = b;
12677 struct breakpoint *next;
12679 /* FUNCTION may delete RELATED. */
12680 next = related->related_breakpoint;
12682 if (next == related)
12684 /* RELATED is the last ring entry. */
12685 function (related);
12687 /* FUNCTION may have deleted it, so we'd never reach back to
12688 B. There's nothing left to do anyway, so just break
12689 out. */
12690 break;
12692 else
12693 function (related);
12695 related = next;
12697 while (related != b);
12700 static void
12701 delete_command (const char *arg, int from_tty)
12703 dont_repeat ();
12705 if (arg == 0)
12707 int breaks_to_delete = 0;
12709 /* Delete all breakpoints, watchpoints, tracepoints, and catchpoints
12710 if no argument. Do not delete internal breakpoints, these have to
12711 be deleted with an explicit breakpoint number argument. */
12712 for (breakpoint &b : all_breakpoints ())
12713 if (user_breakpoint_p (&b))
12715 breaks_to_delete = 1;
12716 break;
12719 /* Ask user only if there are some breakpoints to delete. */
12720 if (!from_tty
12721 || (breaks_to_delete
12722 && query (_("Delete all breakpoints, watchpoints, tracepoints, "
12723 "and catchpoints? "))))
12724 for (breakpoint &b : all_breakpoints_safe ())
12725 if (user_breakpoint_p (&b))
12726 delete_breakpoint (&b);
12728 else
12729 map_breakpoint_numbers
12730 (arg, [&] (breakpoint *br)
12732 iterate_over_related_breakpoints (br, delete_breakpoint);
12736 /* Return true if all locations of B bound to PSPACE are pending. If
12737 PSPACE is NULL, all locations of all program spaces are
12738 considered. */
12740 static bool
12741 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
12743 for (bp_location &loc : b->locations ())
12744 if ((pspace == NULL
12745 || loc.pspace == pspace)
12746 && !loc.shlib_disabled
12747 && !loc.pspace->executing_startup)
12748 return false;
12749 return true;
12752 /* Subroutine of update_breakpoint_locations to simplify it.
12753 Return true if multiple fns in list LOCS have the same name.
12754 Null names are ignored. */
12756 static bool
12757 ambiguous_names_p (const bp_location_range &locs)
12759 gdb::unordered_set<std::string_view> htab;
12761 for (const bp_location &l : locs)
12763 const char *name = l.function_name.get ();
12765 /* Allow for some names to be NULL, ignore them. */
12766 if (name == NULL)
12767 continue;
12769 if (!htab.insert (name).second)
12770 return true;
12773 return false;
12776 /* When symbols change, it probably means the sources changed as well,
12777 and it might mean the static tracepoint markers are no longer at
12778 the same address or line numbers they used to be at last we
12779 checked. Losing your static tracepoints whenever you rebuild is
12780 undesirable. This function tries to resync/rematch gdb static
12781 tracepoints with the markers on the target, for static tracepoints
12782 that have not been set by marker id. Static tracepoint that have
12783 been set by marker id are reset by marker id in breakpoint_re_set.
12784 The heuristic is:
12786 1) For a tracepoint set at a specific address, look for a marker at
12787 the old PC. If one is found there, assume to be the same marker.
12788 If the name / string id of the marker found is different from the
12789 previous known name, assume that means the user renamed the marker
12790 in the sources, and output a warning.
12792 2) For a tracepoint set at a given line number, look for a marker
12793 at the new address of the old line number. If one is found there,
12794 assume to be the same marker. If the name / string id of the
12795 marker found is different from the previous known name, assume that
12796 means the user renamed the marker in the sources, and output a
12797 warning.
12799 3) If a marker is no longer found at the same address or line, it
12800 may mean the marker no longer exists. But it may also just mean
12801 the code changed a bit. Maybe the user added a few lines of code
12802 that made the marker move up or down (in line number terms). Ask
12803 the target for info about the marker with the string id as we knew
12804 it. If found, update line number and address in the matching
12805 static tracepoint. This will get confused if there's more than one
12806 marker with the same ID (possible in UST, although unadvised
12807 precisely because it confuses tools). */
12809 static struct symtab_and_line
12810 update_static_tracepoint (tracepoint *tp, struct symtab_and_line sal)
12812 struct static_tracepoint_marker marker;
12813 CORE_ADDR pc;
12815 pc = sal.pc;
12816 if (sal.line)
12817 find_line_pc (sal.symtab, sal.line, &pc);
12819 if (target_static_tracepoint_marker_at (pc, &marker))
12821 if (tp->static_trace_marker_id != marker.str_id)
12822 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12823 tp->number, tp->static_trace_marker_id.c_str (),
12824 marker.str_id.c_str ());
12826 tp->static_trace_marker_id = std::move (marker.str_id);
12828 return sal;
12831 /* Old marker wasn't found on target at lineno. Try looking it up
12832 by string ID. */
12833 if (!sal.explicit_pc
12834 && sal.line != 0
12835 && sal.symtab != NULL
12836 && !tp->static_trace_marker_id.empty ())
12838 std::vector<static_tracepoint_marker> markers
12839 = target_static_tracepoint_markers_by_strid
12840 (tp->static_trace_marker_id.c_str ());
12842 if (!markers.empty ())
12844 struct symbol *sym;
12845 struct static_tracepoint_marker *tpmarker;
12846 struct ui_out *uiout = current_uiout;
12848 tpmarker = &markers[0];
12850 tp->static_trace_marker_id = std::move (tpmarker->str_id);
12852 warning (_("marker for static tracepoint %d (%s) not "
12853 "found at previous line number"),
12854 tp->number, tp->static_trace_marker_id.c_str ());
12856 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
12857 sym = find_pc_sect_function (tpmarker->address, NULL);
12858 uiout->text ("Now in ");
12859 if (sym)
12861 uiout->field_string ("func", sym->print_name (),
12862 function_name_style.style ());
12863 uiout->text (" at ");
12865 uiout->field_string ("file",
12866 symtab_to_filename_for_display (sal2.symtab),
12867 file_name_style.style ());
12868 uiout->text (":");
12870 if (uiout->is_mi_like_p ())
12872 const char *fullname = symtab_to_fullname (sal2.symtab);
12874 uiout->field_string ("fullname", fullname);
12877 uiout->field_signed ("line", sal2.line, line_number_style.style ());
12878 uiout->text ("\n");
12880 tp->first_loc ().line_number = sal2.line;
12881 tp->first_loc ().symtab = sym != NULL ? sal2.symtab : NULL;
12883 std::unique_ptr<explicit_location_spec> els
12884 (new explicit_location_spec
12885 (symtab_to_filename_for_display (sal2.symtab)));
12886 els->line_offset.offset = tp->first_loc ().line_number;
12887 els->line_offset.sign = LINE_OFFSET_NONE;
12889 tp->locspec = std::move (els);
12891 /* Might be nice to check if function changed, and warn if
12892 so. */
12895 return sal;
12898 /* Returns true iff location lists A and B are sufficiently same that
12899 we don't need to report breakpoint as changed. */
12901 static bool
12902 locations_are_equal (const bp_location_list &a, const bp_location_range &b)
12904 auto a_iter = a.begin ();
12905 auto b_iter = b.begin ();
12907 for (; a_iter != a.end () && b_iter != b.end (); ++a_iter, ++b_iter)
12909 if (a_iter->address != b_iter->address)
12910 return false;
12912 if (a_iter->shlib_disabled != b_iter->shlib_disabled)
12913 return false;
12915 if (a_iter->enabled != b_iter->enabled)
12916 return false;
12918 if (a_iter->disabled_by_cond != b_iter->disabled_by_cond)
12919 return false;
12921 /* When a breakpoint is set by address, it is not created as
12922 pending; but then during an solib event or the like it may
12923 acquire a symbol. So, check this here. */
12924 if (a_iter->symbol != b_iter->symbol
12925 || a_iter->msymbol != b_iter->msymbol)
12926 return false;
12929 return (a_iter == a.end ()) == (b_iter == b.end ());
12932 /* See breakpoint.h. */
12934 bp_location_list
12935 breakpoint::steal_locations (program_space *pspace)
12937 if (pspace == NULL)
12938 return std::move (m_locations);
12940 bp_location_list ret;
12942 for (auto it = m_locations.begin (); it != m_locations.end (); )
12944 if (it->pspace == pspace)
12946 bp_location &loc = *it;
12947 it = m_locations.erase (it);
12948 ret.push_back (loc);
12950 else
12951 ++it;
12954 return ret;
12957 /* Create new breakpoint locations for B (a hardware or software
12958 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12959 zero, then B is a ranged breakpoint. Only recreates locations for
12960 FILTER_PSPACE. Locations of other program spaces are left
12961 untouched. */
12963 void
12964 update_breakpoint_locations (code_breakpoint *b,
12965 struct program_space *filter_pspace,
12966 gdb::array_view<const symtab_and_line> sals,
12967 gdb::array_view<const symtab_and_line> sals_end)
12969 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
12971 /* Ranged breakpoints have only one start location and one end
12972 location. */
12973 b->enable_state = bp_disabled;
12974 gdb_printf (gdb_stderr,
12975 _("Could not reset ranged breakpoint %d: "
12976 "multiple locations found\n"),
12977 b->number);
12978 return;
12981 /* If there's no new locations, and all existing locations are
12982 pending, don't do anything. This optimizes the common case where
12983 all locations are in the same shared library, that was unloaded.
12984 We'd like to retain the location, so that when the library is
12985 loaded again, we don't loose the enabled/disabled status of the
12986 individual locations. */
12987 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
12988 return;
12990 bp_location_list existing_locations = b->steal_locations (filter_pspace);
12992 for (const auto &sal : sals)
12994 struct bp_location *new_loc;
12996 switch_to_program_space_and_thread (sal.pspace);
12998 new_loc = b->add_location (sal);
13000 /* Reparse conditions, they might contain references to the
13001 old symtab. */
13002 if (b->cond_string != NULL)
13004 const char *s;
13006 s = b->cond_string.get ();
13009 new_loc->cond = parse_exp_1 (&s, sal.pc,
13010 block_for_pc (sal.pc),
13013 catch (const gdb_exception_error &e)
13015 new_loc->disabled_by_cond = true;
13019 if (!sals_end.empty ())
13021 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13023 new_loc->length = end - sals[0].pc + 1;
13027 /* If possible, carry over 'disable' status from existing
13028 breakpoints.
13030 If there are multiple breakpoints with the same function name,
13031 e.g. for inline functions, comparing function names won't work.
13032 Instead compare pc addresses; this is just a heuristic as things
13033 may have moved, but in practice it gives the correct answer
13034 often enough until a better solution is found. */
13035 bool have_ambiguous_names = ambiguous_names_p (b->locations ());
13037 for (const bp_location &e : existing_locations)
13039 if (e.function_name == nullptr
13040 || (e.enabled && !e.disabled_by_cond))
13041 continue;
13043 if (have_ambiguous_names)
13045 for (bp_location &l : b->locations ())
13047 /* Ignore software vs hardware location type at
13048 this point, because with "set breakpoint
13049 auto-hw", after a re-set, locations that were
13050 hardware can end up as software, or vice versa.
13051 As mentioned above, this is an heuristic and in
13052 practice should give the correct answer often
13053 enough. */
13054 if (breakpoint_locations_match (&e, &l, true))
13056 l.enabled = e.enabled;
13057 l.disabled_by_cond = e.disabled_by_cond;
13058 break;
13062 else
13064 for (bp_location &l : b->locations ())
13065 if (l.function_name
13066 && strcmp (e.function_name.get (),
13067 l.function_name.get ()) == 0)
13069 l.enabled = e.enabled;
13070 l.disabled_by_cond = e.disabled_by_cond;
13071 break;
13076 if (!locations_are_equal (existing_locations, b->locations ()))
13077 notify_breakpoint_modified (b);
13080 /* Find the SaL locations corresponding to the given LOCSPEC.
13081 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13083 std::vector<symtab_and_line>
13084 code_breakpoint::location_spec_to_sals (location_spec *locspec,
13085 struct program_space *search_pspace,
13086 int *found)
13088 struct gdb_exception exception;
13090 std::vector<symtab_and_line> sals;
13094 sals = decode_location_spec (locspec, search_pspace);
13096 catch (gdb_exception_error &e)
13098 int not_found_and_ok = false;
13100 /* For pending breakpoints, it's expected that parsing will
13101 fail until the right shared library is loaded. User has
13102 already told to create pending breakpoints and don't need
13103 extra messages. If breakpoint is in bp_shlib_disabled
13104 state, then user already saw the message about that
13105 breakpoint being disabled, and don't want to see more
13106 errors. */
13107 if (e.error == NOT_FOUND_ERROR
13108 && (condition_not_parsed
13109 || (this->has_locations ()
13110 && search_pspace != NULL
13111 && this->first_loc ().pspace != search_pspace)
13112 || (this->has_locations () && this->first_loc ().shlib_disabled)
13113 || (this->has_locations ()
13114 && this->first_loc ().pspace->executing_startup)
13115 || enable_state == bp_disabled))
13116 not_found_and_ok = true;
13118 if (!not_found_and_ok)
13120 /* We surely don't want to warn about the same breakpoint
13121 10 times. One solution, implemented here, is disable
13122 the breakpoint on error. Another solution would be to
13123 have separate 'warning emitted' flag. Since this
13124 happens only when a binary has changed, I don't know
13125 which approach is better. */
13126 enable_state = bp_disabled;
13127 throw;
13130 exception = std::move (e);
13133 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13135 for (auto &sal : sals)
13136 resolve_sal_pc (&sal);
13138 if (type == bp_static_tracepoint)
13140 tracepoint *t = gdb::checked_static_cast<tracepoint *> (this);
13141 sals[0] = update_static_tracepoint (t, sals[0]);
13144 *found = 1;
13146 else
13147 *found = 0;
13149 return sals;
13152 /* The default re_set method, for typical hardware or software
13153 breakpoints. Reevaluate the breakpoint and recreate its
13154 locations. */
13156 void
13157 code_breakpoint::re_set_default (struct program_space *filter_pspace)
13159 std::vector<symtab_and_line> expanded, expanded_end;
13161 /* If this breakpoint is thread- or inferior-specific, then find the
13162 program space in which this breakpoint exists. Otherwise, for
13163 breakpoints that are not thread- or inferior-specific, BP_PSPACE will
13164 be nullptr. */
13165 program_space *bp_pspace
13166 = find_program_space_for_breakpoint (this->thread, this->inferior);
13168 /* If this is not a thread or inferior specific breakpoint, or it is a
13169 thread or inferior specific breakpoint but we are looking for new
13170 locations in the program space that the specific thread or inferior is
13171 running, then look for new locations for this breakpoint. */
13172 if (bp_pspace == nullptr || filter_pspace == bp_pspace)
13174 int found;
13175 std::vector<symtab_and_line> sals
13176 = location_spec_to_sals (locspec.get (), filter_pspace, &found);
13177 if (found)
13178 expanded = std::move (sals);
13180 if (locspec_range_end != nullptr)
13182 std::vector<symtab_and_line> sals_end
13183 = location_spec_to_sals (locspec_range_end.get (),
13184 filter_pspace, &found);
13185 if (found)
13186 expanded_end = std::move (sals_end);
13190 /* Update the locations for this breakpoint. For thread-specific
13191 breakpoints this will remove any old locations that are for the wrong
13192 program space -- this can happen if the user changes the thread of a
13193 thread-specific breakpoint. */
13194 update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
13197 /* Re-set breakpoint locations for the current program space.
13198 Locations bound to other program spaces are left untouched. */
13200 void
13201 breakpoint_re_set (void)
13204 scoped_restore_current_language save_language;
13205 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13206 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13208 /* To ::re_set each breakpoint we set the current_language to the
13209 language of the breakpoint before re-evaluating the breakpoint's
13210 location. This change can unfortunately get undone by accident if
13211 the language_mode is set to auto, and we either switch frames, or
13212 more likely in this context, we select the current frame.
13214 We prevent this by temporarily turning the language_mode to
13215 language_mode_manual. We restore it once all breakpoints
13216 have been reset. */
13217 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13218 language_mode = language_mode_manual;
13220 /* Note: we must not try to insert locations until after all
13221 breakpoints have been re-set. Otherwise, e.g., when re-setting
13222 breakpoint 1, we'd insert the locations of breakpoint 2, which
13223 hadn't been re-set yet, and thus may have stale locations. */
13225 for (breakpoint &b : all_breakpoints_safe ())
13229 input_radix = b.input_radix;
13230 set_language (b.language);
13231 b.re_set (current_program_space);
13233 catch (const gdb_exception &ex)
13235 exception_fprintf (gdb_stderr, ex,
13236 "Error in re-setting breakpoint %d: ",
13237 b.number);
13241 jit_breakpoint_re_set ();
13244 create_overlay_event_breakpoint ();
13245 create_longjmp_master_breakpoint ();
13246 create_std_terminate_master_breakpoint ();
13247 create_exception_master_breakpoint ();
13249 /* Now we can insert. */
13250 update_global_location_list (UGLL_MAY_INSERT);
13253 /* Re-set locations for breakpoint B in FILTER_PSPACE. If FILTER_PSPACE is
13254 nullptr then re-set locations for B in all program spaces. Locations
13255 bound to program spaces other than FILTER_PSPACE are left untouched. */
13257 static void
13258 breakpoint_re_set_one (breakpoint *b, program_space *filter_pspace)
13261 scoped_restore_current_language save_language;
13262 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13263 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13265 /* To ::re_set each breakpoint we set the current_language to the
13266 language of the breakpoint before re-evaluating the breakpoint's
13267 location. This change can unfortunately get undone by accident if
13268 the language_mode is set to auto, and we either switch frames, or
13269 more likely in this context, we select the current frame.
13271 We prevent this by temporarily turning the language_mode to
13272 language_mode_manual. We restore it once all breakpoints
13273 have been reset. */
13274 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13275 language_mode = language_mode_manual;
13277 /* Note: we must not try to insert locations until after all
13278 breakpoints have been re-set. Otherwise, e.g., when re-setting
13279 breakpoint 1, we'd insert the locations of breakpoint 2, which
13280 hadn't been re-set yet, and thus may have stale locations. */
13284 input_radix = b->input_radix;
13285 set_language (b->language);
13286 b->re_set (filter_pspace);
13288 catch (const gdb_exception &ex)
13290 exception_fprintf (gdb_stderr, ex,
13291 "Error in re-setting breakpoint %d: ",
13292 b->number);
13296 /* Now we can insert. */
13297 update_global_location_list (UGLL_MAY_INSERT);
13300 /* Reset the thread number of this breakpoint:
13302 - If the breakpoint is for all threads, leave it as-is.
13303 - Else, reset it to the current thread for inferior_ptid. */
13304 void
13305 breakpoint_re_set_thread (struct breakpoint *b)
13307 if (b->thread != -1)
13309 b->thread = inferior_thread ()->global_num;
13311 /* We're being called after following a fork. The new fork is
13312 selected as current, and unless this was a vfork will have a
13313 different program space from the original thread. Reset that
13314 as well. */
13315 b->first_loc ().pspace = current_program_space;
13319 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13320 If from_tty is nonzero, it prints a message to that effect,
13321 which ends with a period (no newline). */
13323 void
13324 set_ignore_count (int bptnum, int count, int from_tty)
13326 if (count < 0)
13327 count = 0;
13329 for (breakpoint &b : all_breakpoints ())
13330 if (b.number == bptnum)
13332 if (is_tracepoint (&b))
13334 if (from_tty && count != 0)
13335 gdb_printf (_("Ignore count ignored for tracepoint %d."),
13336 bptnum);
13337 return;
13340 b.ignore_count = count;
13341 if (from_tty)
13343 if (count == 0)
13344 gdb_printf (_("Will stop next time "
13345 "breakpoint %d is reached."),
13346 bptnum);
13347 else if (count == 1)
13348 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
13349 bptnum);
13350 else
13351 gdb_printf (_("Will ignore next %d "
13352 "crossings of breakpoint %d."),
13353 count, bptnum);
13355 notify_breakpoint_modified (&b);
13356 return;
13359 error (_("No breakpoint number %d."), bptnum);
13362 /* Command to set ignore-count of breakpoint N to COUNT. */
13364 static void
13365 ignore_command (const char *args, int from_tty)
13367 const char *p = args;
13368 int num;
13370 if (p == 0)
13371 error_no_arg (_("a breakpoint number"));
13373 num = get_number (&p);
13374 if (num == 0)
13375 error (_("bad breakpoint number: '%s'"), args);
13376 if (*p == 0)
13377 error (_("Second argument (specified ignore-count) is missing."));
13379 set_ignore_count (num,
13380 longest_to_int (value_as_long (parse_and_eval (p))),
13381 from_tty);
13382 if (from_tty)
13383 gdb_printf ("\n");
13387 /* Call FUNCTION on each of the breakpoints with numbers in the range
13388 defined by BP_NUM_RANGE (an inclusive range). */
13390 static void
13391 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13392 gdb::function_view<void (breakpoint *)> function)
13394 if (bp_num_range.first == 0)
13396 warning (_("bad breakpoint number at or near '%d'"),
13397 bp_num_range.first);
13399 else
13401 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13403 bool match = false;
13405 for (breakpoint &b : all_breakpoints_safe ())
13406 if (b.number == i)
13408 match = true;
13409 function (&b);
13410 break;
13412 if (!match)
13413 gdb_printf (_("No breakpoint number %d.\n"), i);
13418 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13419 ARGS. */
13421 static void
13422 map_breakpoint_numbers (const char *args,
13423 gdb::function_view<void (breakpoint *)> function)
13425 if (args == NULL || *args == '\0')
13426 error_no_arg (_("one or more breakpoint numbers"));
13428 number_or_range_parser parser (args);
13430 while (!parser.finished ())
13432 int num = parser.get_number ();
13433 map_breakpoint_number_range (std::make_pair (num, num), function);
13437 /* Return the breakpoint location structure corresponding to the
13438 BP_NUM and LOC_NUM values. */
13440 static struct bp_location *
13441 find_location_by_number (int bp_num, int loc_num)
13443 breakpoint *b = get_breakpoint (bp_num);
13445 if (!b || b->number != bp_num)
13446 error (_("Bad breakpoint number '%d'"), bp_num);
13448 if (loc_num == 0)
13449 error (_("Bad breakpoint location number '%d'"), loc_num);
13451 int n = 0;
13452 for (bp_location &loc : b->locations ())
13453 if (++n == loc_num)
13454 return &loc;
13456 error (_("Bad breakpoint location number '%d'"), loc_num);
13459 /* Modes of operation for extract_bp_num. */
13460 enum class extract_bp_kind
13462 /* Extracting a breakpoint number. */
13465 /* Extracting a location number. */
13466 loc,
13469 /* Extract a breakpoint or location number (as determined by KIND)
13470 from the string starting at START. TRAILER is a character which
13471 can be found after the number. If you don't want a trailer, use
13472 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13473 string. This always returns a positive integer. */
13475 static int
13476 extract_bp_num (extract_bp_kind kind, const char *start,
13477 int trailer, const char **end_out = NULL)
13479 const char *end = start;
13480 int num = get_number_trailer (&end, trailer);
13481 if (num < 0)
13482 error (kind == extract_bp_kind::bp
13483 ? _("Negative breakpoint number '%.*s'")
13484 : _("Negative breakpoint location number '%.*s'"),
13485 int (end - start), start);
13486 if (num == 0)
13487 error (kind == extract_bp_kind::bp
13488 ? _("Bad breakpoint number '%.*s'")
13489 : _("Bad breakpoint location number '%.*s'"),
13490 int (end - start), start);
13492 if (end_out != NULL)
13493 *end_out = end;
13494 return num;
13497 /* Extract a breakpoint or location range (as determined by KIND) in
13498 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13499 representing the (inclusive) range. The returned pair's elements
13500 are always positive integers. */
13502 static std::pair<int, int>
13503 extract_bp_or_bp_range (extract_bp_kind kind,
13504 const std::string &arg,
13505 std::string::size_type arg_offset)
13507 std::pair<int, int> range;
13508 const char *bp_loc = &arg[arg_offset];
13509 std::string::size_type dash = arg.find ('-', arg_offset);
13510 if (dash != std::string::npos)
13512 /* bp_loc is a range (x-z). */
13513 if (arg.length () == dash + 1)
13514 error (kind == extract_bp_kind::bp
13515 ? _("Bad breakpoint number at or near: '%s'")
13516 : _("Bad breakpoint location number at or near: '%s'"),
13517 bp_loc);
13519 const char *end;
13520 const char *start_first = bp_loc;
13521 const char *start_second = &arg[dash + 1];
13522 range.first = extract_bp_num (kind, start_first, '-');
13523 range.second = extract_bp_num (kind, start_second, '\0', &end);
13525 if (range.first > range.second)
13526 error (kind == extract_bp_kind::bp
13527 ? _("Inverted breakpoint range at '%.*s'")
13528 : _("Inverted breakpoint location range at '%.*s'"),
13529 int (end - start_first), start_first);
13531 else
13533 /* bp_loc is a single value. */
13534 range.first = extract_bp_num (kind, bp_loc, '\0');
13535 range.second = range.first;
13537 return range;
13540 /* Extract the breakpoint/location range specified by ARG. Returns
13541 the breakpoint range in BP_NUM_RANGE, and the location range in
13542 BP_LOC_RANGE.
13544 ARG may be in any of the following forms:
13546 x where 'x' is a breakpoint number.
13547 x-y where 'x' and 'y' specify a breakpoint numbers range.
13548 x.y where 'x' is a breakpoint number and 'y' a location number.
13549 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13550 location number range.
13553 static void
13554 extract_bp_number_and_location (const std::string &arg,
13555 std::pair<int, int> &bp_num_range,
13556 std::pair<int, int> &bp_loc_range)
13558 std::string::size_type dot = arg.find ('.');
13560 if (dot != std::string::npos)
13562 /* Handle 'x.y' and 'x.y-z' cases. */
13564 if (arg.length () == dot + 1 || dot == 0)
13565 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
13567 bp_num_range.first
13568 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13569 bp_num_range.second = bp_num_range.first;
13571 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13572 arg, dot + 1);
13574 else
13576 /* Handle x and x-y cases. */
13578 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
13579 bp_loc_range.first = 0;
13580 bp_loc_range.second = 0;
13584 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13585 specifies whether to enable or disable. */
13587 static void
13588 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13590 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13591 if (loc != NULL)
13593 if (loc->disabled_by_cond && enable)
13594 error (_("Breakpoint %d's condition is invalid at location %d, "
13595 "cannot enable."), bp_num, loc_num);
13597 if (loc->enabled != enable)
13599 loc->enabled = enable;
13600 mark_breakpoint_location_modified (loc);
13602 if (target_supports_enable_disable_tracepoint ()
13603 && current_trace_status ()->running && loc->owner
13604 && is_tracepoint (loc->owner))
13605 target_disable_tracepoint (loc);
13607 update_global_location_list (UGLL_DONT_INSERT);
13609 notify_breakpoint_modified (loc->owner);
13612 /* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
13613 owner. 1-based indexing. -1 signals NOT FOUND. */
13615 static int
13616 find_loc_num_by_location (const bp_location *loc)
13618 if (loc != nullptr && loc->owner != nullptr)
13620 /* Locations use 1-based indexing. */
13621 int loc_num = 1;
13622 for (bp_location &it : loc->owner->locations ())
13624 if (&it == loc)
13625 return loc_num;
13626 loc_num++;
13629 return -1;
13632 /* Enable or disable a breakpoint location LOC. ENABLE
13633 specifies whether to enable or disable. */
13635 void
13636 enable_disable_bp_location (bp_location *loc, bool enable)
13638 if (loc == nullptr)
13639 error (_("Breakpoint location is invalid."));
13641 if (loc->owner == nullptr)
13642 error (_("Breakpoint location does not have an owner breakpoint."));
13644 if (loc->disabled_by_cond && enable)
13646 int loc_num = find_loc_num_by_location (loc);
13647 if (loc_num == -1)
13648 error (_("Breakpoint location LOC_NUM could not be found."));
13649 else
13650 error (_("Breakpoint %d's condition is invalid at location %d, "
13651 "cannot enable."), loc->owner->number, loc_num);
13654 if (loc->enabled != enable)
13656 loc->enabled = enable;
13657 mark_breakpoint_location_modified (loc);
13660 if (target_supports_enable_disable_tracepoint ()
13661 && current_trace_status ()->running && loc->owner
13662 && is_tracepoint (loc->owner))
13663 target_disable_tracepoint (loc);
13665 update_global_location_list (UGLL_DONT_INSERT);
13666 notify_breakpoint_modified (loc->owner);
13669 /* Enable or disable a range of breakpoint locations. BP_NUM is the
13670 number of the breakpoint, and BP_LOC_RANGE specifies the
13671 (inclusive) range of location numbers of that breakpoint to
13672 enable/disable. ENABLE specifies whether to enable or disable the
13673 location. */
13675 static void
13676 enable_disable_breakpoint_location_range (int bp_num,
13677 std::pair<int, int> &bp_loc_range,
13678 bool enable)
13680 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13681 enable_disable_bp_num_loc (bp_num, i, enable);
13684 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13685 If from_tty is nonzero, it prints a message to that effect,
13686 which ends with a period (no newline). */
13688 void
13689 disable_breakpoint (struct breakpoint *bpt)
13691 /* Never disable a watchpoint scope breakpoint; we want to
13692 hit them when we leave scope so we can delete both the
13693 watchpoint and its scope breakpoint at that time. */
13694 if (bpt->type == bp_watchpoint_scope)
13695 return;
13697 bpt->enable_state = bp_disabled;
13699 /* Mark breakpoint locations modified. */
13700 mark_breakpoint_modified (bpt);
13702 if (target_supports_enable_disable_tracepoint ()
13703 && current_trace_status ()->running && is_tracepoint (bpt))
13705 for (bp_location &location : bpt->locations ())
13706 target_disable_tracepoint (&location);
13709 update_global_location_list (UGLL_DONT_INSERT);
13711 notify_breakpoint_modified (bpt);
13714 /* Enable or disable the breakpoint(s) or breakpoint location(s)
13715 specified in ARGS. ARGS may be in any of the formats handled by
13716 extract_bp_number_and_location. ENABLE specifies whether to enable
13717 or disable the breakpoints/locations. */
13719 static void
13720 enable_disable_command (const char *args, int from_tty, bool enable)
13722 if (args == 0)
13724 for (breakpoint &bpt : all_breakpoints ())
13725 if (user_breakpoint_p (&bpt))
13727 if (enable)
13728 enable_breakpoint (&bpt);
13729 else
13730 disable_breakpoint (&bpt);
13733 else
13735 std::string num = extract_arg (&args);
13737 while (!num.empty ())
13739 std::pair<int, int> bp_num_range, bp_loc_range;
13741 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13743 if (bp_loc_range.first == bp_loc_range.second
13744 && (bp_loc_range.first == 0
13745 || (bp_loc_range.first == 1
13746 && bp_num_range.first == bp_num_range.second
13747 && !has_multiple_locations (bp_num_range.first))))
13749 /* Handle breakpoint ids with formats 'x' or 'x-z'
13750 or 'y.1' where y has only one code location. */
13751 map_breakpoint_number_range (bp_num_range,
13752 enable
13753 ? enable_breakpoint
13754 : disable_breakpoint);
13756 else
13758 /* Handle breakpoint ids with formats 'x.y' or
13759 'x.y-z'. */
13760 enable_disable_breakpoint_location_range
13761 (bp_num_range.first, bp_loc_range, enable);
13763 num = extract_arg (&args);
13768 /* The disable command disables the specified breakpoints/locations
13769 (or all defined breakpoints) so they're no longer effective in
13770 stopping the inferior. ARGS may be in any of the forms defined in
13771 extract_bp_number_and_location. */
13773 static void
13774 disable_command (const char *args, int from_tty)
13776 enable_disable_command (args, from_tty, false);
13779 static void
13780 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13781 int count)
13783 int target_resources_ok;
13785 if (bpt->type == bp_hardware_breakpoint)
13787 int i;
13788 i = hw_breakpoint_used_count ();
13789 target_resources_ok =
13790 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13791 i + 1, 0);
13792 if (target_resources_ok == 0)
13793 error (_("No hardware breakpoint support in the target."));
13794 else if (target_resources_ok < 0)
13795 error (_("Hardware breakpoints used exceeds limit."));
13798 if (is_watchpoint (bpt))
13800 /* Initialize it just to avoid a GCC false warning. */
13801 enum enable_state orig_enable_state = bp_disabled;
13805 watchpoint *w = gdb::checked_static_cast<watchpoint *> (bpt);
13807 orig_enable_state = bpt->enable_state;
13808 bpt->enable_state = bp_enabled;
13809 update_watchpoint (w, true /* reparse */);
13811 catch (const gdb_exception_error &e)
13813 bpt->enable_state = orig_enable_state;
13814 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13815 bpt->number);
13816 return;
13820 bpt->enable_state = bp_enabled;
13822 /* Mark breakpoint locations modified. */
13823 mark_breakpoint_modified (bpt);
13825 if (target_supports_enable_disable_tracepoint ()
13826 && current_trace_status ()->running && is_tracepoint (bpt))
13828 for (bp_location &location : bpt->locations ())
13829 target_enable_tracepoint (&location);
13832 bpt->disposition = disposition;
13833 bpt->enable_count = count;
13834 update_global_location_list (UGLL_MAY_INSERT);
13836 notify_breakpoint_modified (bpt);
13840 void
13841 enable_breakpoint (struct breakpoint *bpt)
13843 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13846 /* The enable command enables the specified breakpoints/locations (or
13847 all defined breakpoints) so they once again become (or continue to
13848 be) effective in stopping the inferior. ARGS may be in any of the
13849 forms defined in extract_bp_number_and_location. */
13851 static void
13852 enable_command (const char *args, int from_tty)
13854 enable_disable_command (args, from_tty, true);
13857 static void
13858 enable_once_command (const char *args, int from_tty)
13860 map_breakpoint_numbers
13861 (args, [&] (breakpoint *b)
13863 iterate_over_related_breakpoints
13864 (b, [&] (breakpoint *bpt)
13866 enable_breakpoint_disp (bpt, disp_disable, 1);
13871 static void
13872 enable_count_command (const char *args, int from_tty)
13874 int count;
13876 if (args == NULL)
13877 error_no_arg (_("hit count"));
13879 count = get_number (&args);
13881 map_breakpoint_numbers
13882 (args, [&] (breakpoint *b)
13884 iterate_over_related_breakpoints
13885 (b, [&] (breakpoint *bpt)
13887 enable_breakpoint_disp (bpt, disp_disable, count);
13892 static void
13893 enable_delete_command (const char *args, int from_tty)
13895 map_breakpoint_numbers
13896 (args, [&] (breakpoint *b)
13898 iterate_over_related_breakpoints
13899 (b, [&] (breakpoint *bpt)
13901 enable_breakpoint_disp (bpt, disp_del, 1);
13906 /* Invalidate last known value of any hardware watchpoint if
13907 the memory which that value represents has been written to by
13908 GDB itself. */
13910 static void
13911 invalidate_bp_value_on_memory_change (struct inferior *inferior,
13912 CORE_ADDR addr, ssize_t len,
13913 const bfd_byte *data)
13915 for (breakpoint &bp : all_breakpoints ())
13916 if (bp.enable_state == bp_enabled
13917 && bp.type == bp_hardware_watchpoint)
13919 watchpoint &wp = gdb::checked_static_cast<watchpoint &> (bp);
13921 if (wp.val_valid && wp.val != nullptr)
13923 for (bp_location &loc : bp.locations ())
13924 if (loc.loc_type == bp_loc_hardware_watchpoint
13925 && loc.address + loc.length > addr
13926 && addr + len > loc.address)
13928 wp.val = NULL;
13929 wp.val_valid = false;
13935 /* Create and insert a breakpoint for software single step. */
13937 void
13938 insert_single_step_breakpoint (struct gdbarch *gdbarch,
13939 const address_space *aspace,
13940 CORE_ADDR next_pc)
13942 struct thread_info *tp = inferior_thread ();
13943 struct symtab_and_line sal;
13944 CORE_ADDR pc = next_pc;
13946 if (tp->control.single_step_breakpoints == NULL)
13948 std::unique_ptr<breakpoint> b
13949 (new momentary_breakpoint (gdbarch, bp_single_step,
13950 current_program_space,
13951 null_frame_id,
13952 tp->global_num));
13954 tp->control.single_step_breakpoints
13955 = add_to_breakpoint_chain (std::move (b));
13958 sal = find_pc_line (pc, 0);
13959 sal.pc = pc;
13960 sal.section = find_pc_overlay (pc);
13961 sal.explicit_pc = 1;
13963 auto *ss_bp
13964 = (gdb::checked_static_cast<momentary_breakpoint *>
13965 (tp->control.single_step_breakpoints));
13966 ss_bp->add_location (sal);
13968 update_global_location_list (UGLL_INSERT);
13971 /* Insert single step breakpoints according to the current state. */
13974 insert_single_step_breakpoints (struct gdbarch *gdbarch)
13976 regcache *regcache = get_thread_regcache (inferior_thread ());
13977 std::vector<CORE_ADDR> next_pcs;
13979 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
13981 if (!next_pcs.empty ())
13983 frame_info_ptr frame = get_current_frame ();
13984 const address_space *aspace = get_frame_address_space (frame);
13986 for (CORE_ADDR pc : next_pcs)
13987 insert_single_step_breakpoint (gdbarch, aspace, pc);
13989 return 1;
13991 else
13992 return 0;
13995 /* See breakpoint.h. */
13998 breakpoint_has_location_inserted_here (struct breakpoint *bp,
13999 const address_space *aspace,
14000 CORE_ADDR pc)
14002 for (bp_location &loc : bp->locations ())
14003 if (loc.inserted
14004 && breakpoint_location_address_match (&loc, aspace, pc))
14005 return 1;
14007 return 0;
14010 /* Check whether a software single-step breakpoint is inserted at
14011 PC. */
14014 single_step_breakpoint_inserted_here_p (const address_space *aspace,
14015 CORE_ADDR pc)
14017 for (breakpoint &bpt : all_breakpoints ())
14019 if (bpt.type == bp_single_step
14020 && breakpoint_has_location_inserted_here (&bpt, aspace, pc))
14021 return 1;
14023 return 0;
14026 /* Tracepoint-specific operations. */
14028 /* Set tracepoint count to NUM. */
14029 static void
14030 set_tracepoint_count (int num)
14032 tracepoint_count = num;
14033 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14036 static void
14037 trace_command (const char *arg, int from_tty)
14039 location_spec_up locspec = string_to_location_spec (&arg,
14040 current_language);
14041 const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
14042 (locspec.get (), true /* is_tracepoint */);
14044 create_breakpoint (get_current_arch (),
14045 locspec.get (),
14046 NULL, -1, -1, arg, false, 1 /* parse arg */,
14047 0 /* tempflag */,
14048 bp_tracepoint /* type_wanted */,
14049 0 /* Ignore count */,
14050 pending_break_support,
14051 ops,
14052 from_tty,
14053 1 /* enabled */,
14054 0 /* internal */, 0);
14057 static void
14058 ftrace_command (const char *arg, int from_tty)
14060 location_spec_up locspec = string_to_location_spec (&arg,
14061 current_language);
14062 create_breakpoint (get_current_arch (),
14063 locspec.get (),
14064 NULL, -1, -1, arg, false, 1 /* parse arg */,
14065 0 /* tempflag */,
14066 bp_fast_tracepoint /* type_wanted */,
14067 0 /* Ignore count */,
14068 pending_break_support,
14069 &code_breakpoint_ops,
14070 from_tty,
14071 1 /* enabled */,
14072 0 /* internal */, 0);
14075 /* strace command implementation. Creates a static tracepoint. */
14077 static void
14078 strace_command (const char *arg, int from_tty)
14080 const struct breakpoint_ops *ops;
14081 location_spec_up locspec;
14082 enum bptype type;
14084 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14085 or with a normal static tracepoint. */
14086 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14088 ops = &strace_marker_breakpoint_ops;
14089 locspec = new_linespec_location_spec (&arg,
14090 symbol_name_match_type::FULL);
14091 type = bp_static_marker_tracepoint;
14093 else
14095 ops = &code_breakpoint_ops;
14096 locspec = string_to_location_spec (&arg, current_language);
14097 type = bp_static_tracepoint;
14100 create_breakpoint (get_current_arch (),
14101 locspec.get (),
14102 NULL, -1, -1, arg, false, 1 /* parse arg */,
14103 0 /* tempflag */,
14104 type /* type_wanted */,
14105 0 /* Ignore count */,
14106 pending_break_support,
14107 ops,
14108 from_tty,
14109 1 /* enabled */,
14110 0 /* internal */, 0);
14113 /* Set up a fake reader function that gets command lines from a linked
14114 list that was acquired during tracepoint uploading. */
14116 static struct uploaded_tp *this_utp;
14117 static int next_cmd;
14119 static const char *
14120 read_uploaded_action (std::string &buffer)
14122 char *rslt = nullptr;
14124 if (next_cmd < this_utp->cmd_strings.size ())
14126 rslt = this_utp->cmd_strings[next_cmd].get ();
14127 next_cmd++;
14130 return rslt;
14133 /* Given information about a tracepoint as recorded on a target (which
14134 can be either a live system or a trace file), attempt to create an
14135 equivalent GDB tracepoint. This is not a reliable process, since
14136 the target does not necessarily have all the information used when
14137 the tracepoint was originally defined. */
14139 struct tracepoint *
14140 create_tracepoint_from_upload (struct uploaded_tp *utp)
14142 const char *addr_str;
14143 char small_buf[100];
14144 struct tracepoint *tp;
14146 if (utp->at_string)
14147 addr_str = utp->at_string.get ();
14148 else
14150 /* In the absence of a source location, fall back to raw
14151 address. Since there is no way to confirm that the address
14152 means the same thing as when the trace was started, warn the
14153 user. */
14154 warning (_("Uploaded tracepoint %d has no "
14155 "source location, using raw address"),
14156 utp->number);
14157 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14158 addr_str = small_buf;
14161 /* There's not much we can do with a sequence of bytecodes. */
14162 if (utp->cond && !utp->cond_string)
14163 warning (_("Uploaded tracepoint %d condition "
14164 "has no source form, ignoring it"),
14165 utp->number);
14167 location_spec_up locspec = string_to_location_spec (&addr_str,
14168 current_language);
14171 gdb_assert (addr_str != nullptr);
14172 if (*addr_str != '\0')
14173 error (_("Garbage '%s' at end of location"), addr_str);
14175 if (!create_breakpoint (get_current_arch (),
14176 locspec.get (),
14177 utp->cond_string.get (), -1, -1, addr_str,
14178 false /* force_condition */,
14179 0 /* parse cond/thread */,
14180 0 /* tempflag */,
14181 utp->type /* type_wanted */,
14182 0 /* Ignore count */,
14183 pending_break_support,
14184 &code_breakpoint_ops,
14185 0 /* from_tty */,
14186 utp->enabled /* enabled */,
14187 0 /* internal */,
14188 CREATE_BREAKPOINT_FLAGS_INSERTED))
14189 return NULL;
14191 /* Get the tracepoint we just created. */
14192 tp = get_tracepoint (tracepoint_count);
14193 gdb_assert (tp != NULL);
14195 if (utp->pass > 0)
14197 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14198 tp->number);
14200 trace_pass_command (small_buf, 0);
14203 /* If we have uploaded versions of the original commands, set up a
14204 special-purpose "reader" function and call the usual command line
14205 reader, then pass the result to the breakpoint command-setting
14206 function. */
14207 if (!utp->cmd_strings.empty ())
14209 counted_command_line cmd_list;
14211 this_utp = utp;
14212 next_cmd = 0;
14214 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14216 breakpoint_set_commands (tp, std::move (cmd_list));
14218 else if (!utp->actions.empty ()
14219 || !utp->step_actions.empty ())
14220 warning (_("Uploaded tracepoint %d actions "
14221 "have no source form, ignoring them"),
14222 utp->number);
14224 /* Copy any status information that might be available. */
14225 tp->hit_count = utp->hit_count;
14226 tp->traceframe_usage = utp->traceframe_usage;
14228 return tp;
14231 /* Print information on tracepoint number TPNUM_EXP, or all if
14232 omitted. */
14234 static void
14235 info_tracepoints_command (const char *args, int from_tty)
14237 struct ui_out *uiout = current_uiout;
14238 int num_printed;
14240 num_printed = breakpoint_1 (args, false, is_tracepoint);
14242 if (num_printed == 0)
14244 if (args == NULL || *args == '\0')
14245 uiout->message ("No tracepoints.\n");
14246 else
14247 uiout->message ("No tracepoint matching '%s'.\n", args);
14250 default_collect_info ();
14253 /* The 'enable trace' command enables tracepoints.
14254 Not supported by all targets. */
14255 static void
14256 enable_trace_command (const char *args, int from_tty)
14258 enable_command (args, from_tty);
14261 /* The 'disable trace' command disables tracepoints.
14262 Not supported by all targets. */
14263 static void
14264 disable_trace_command (const char *args, int from_tty)
14266 disable_command (args, from_tty);
14269 /* Remove a tracepoint (or all if no argument). */
14270 static void
14271 delete_trace_command (const char *arg, int from_tty)
14273 dont_repeat ();
14275 if (arg == 0)
14277 int breaks_to_delete = 0;
14279 /* Delete all tracepoints if no argument.
14280 Do not delete internal or call-dummy breakpoints, these
14281 have to be deleted with an explicit breakpoint number
14282 argument. */
14283 for (breakpoint &tp : all_tracepoints ())
14284 if (is_tracepoint (&tp) && user_breakpoint_p (&tp))
14286 breaks_to_delete = 1;
14287 break;
14290 /* Ask user only if there are some breakpoints to delete. */
14291 if (!from_tty
14292 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14294 for (breakpoint &b : all_breakpoints_safe ())
14295 if (is_tracepoint (&b) && user_breakpoint_p (&b))
14296 delete_breakpoint (&b);
14299 else
14300 map_breakpoint_numbers
14301 (arg, [&] (breakpoint *br)
14303 iterate_over_related_breakpoints (br, delete_breakpoint);
14307 /* Helper function for trace_pass_command. */
14309 static void
14310 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14312 tp->pass_count = count;
14313 notify_breakpoint_modified (tp);
14314 if (from_tty)
14315 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
14316 tp->number, count);
14319 /* Set passcount for tracepoint.
14321 First command argument is passcount, second is tracepoint number.
14322 If tracepoint number omitted, apply to most recently defined.
14323 Also accepts special argument "all". */
14325 static void
14326 trace_pass_command (const char *args, int from_tty)
14328 ULONGEST count;
14330 if (args == 0 || *args == 0)
14331 error (_("passcount command requires an "
14332 "argument (count + optional TP num)"));
14334 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14336 args = skip_spaces (args);
14337 if (*args && strncasecmp (args, "all", 3) == 0)
14339 args += 3; /* Skip special argument "all". */
14340 if (*args)
14341 error (_("Junk at end of arguments."));
14343 for (breakpoint &b : all_tracepoints ())
14345 tracepoint &t1 = gdb::checked_static_cast<tracepoint &> (b);
14346 trace_pass_set_count (&t1, count, from_tty);
14349 else if (*args == '\0')
14351 tracepoint *t1 = get_tracepoint_by_number (&args, NULL);
14352 if (t1)
14353 trace_pass_set_count (t1, count, from_tty);
14355 else
14357 number_or_range_parser parser (args);
14358 while (!parser.finished ())
14360 tracepoint *t1 = get_tracepoint_by_number (&args, &parser);
14361 if (t1)
14362 trace_pass_set_count (t1, count, from_tty);
14367 struct tracepoint *
14368 get_tracepoint (int num)
14370 for (breakpoint &t : all_tracepoints ())
14371 if (t.number == num)
14372 return gdb::checked_static_cast<tracepoint *> (&t);
14374 return NULL;
14377 /* Find the tracepoint with the given target-side number (which may be
14378 different from the tracepoint number after disconnecting and
14379 reconnecting). */
14381 struct tracepoint *
14382 get_tracepoint_by_number_on_target (int num)
14384 for (breakpoint &b : all_tracepoints ())
14386 tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
14388 if (t.number_on_target == num)
14389 return &t;
14392 return NULL;
14395 /* Utility: parse a tracepoint number and look it up in the list.
14396 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14397 If the argument is missing, the most recent tracepoint
14398 (tracepoint_count) is returned. */
14400 struct tracepoint *
14401 get_tracepoint_by_number (const char **arg,
14402 number_or_range_parser *parser)
14404 int tpnum;
14405 const char *instring = arg == NULL ? NULL : *arg;
14407 if (parser != NULL)
14409 gdb_assert (!parser->finished ());
14410 tpnum = parser->get_number ();
14412 else if (arg == NULL || *arg == NULL || ! **arg)
14413 tpnum = tracepoint_count;
14414 else
14415 tpnum = get_number (arg);
14417 if (tpnum <= 0)
14419 if (instring && *instring)
14420 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14421 instring);
14422 else
14423 gdb_printf (_("No previous tracepoint\n"));
14424 return NULL;
14427 for (breakpoint &t : all_tracepoints ())
14428 if (t.number == tpnum)
14429 return gdb::checked_static_cast<tracepoint *> (&t);
14431 gdb_printf ("No tracepoint number %d.\n", tpnum);
14432 return NULL;
14435 void
14436 breakpoint::print_recreate_thread (struct ui_file *fp) const
14438 if (thread != -1)
14440 struct thread_info *thr = find_thread_global_id (thread);
14441 gdb_printf (fp, " thread %s", print_full_thread_id (thr));
14444 if (task != -1)
14445 gdb_printf (fp, " task %d", task);
14447 gdb_printf (fp, "\n");
14450 /* Save information on user settable breakpoints (watchpoints, etc) to
14451 a new script file named FILENAME. If FILTER is non-NULL, call it
14452 on each breakpoint and only include the ones for which it returns
14453 true. */
14455 static void
14456 save_breakpoints (const char *filename, int from_tty,
14457 bool (*filter) (const struct breakpoint *))
14459 bool any = false;
14460 int extra_trace_bits = 0;
14462 if (filename == 0 || *filename == 0)
14463 error (_("Argument required (file name in which to save)"));
14465 /* See if we have anything to save. */
14466 for (breakpoint &tp : all_breakpoints ())
14468 /* Skip internal and momentary breakpoints. */
14469 if (!user_breakpoint_p (&tp))
14470 continue;
14472 /* If we have a filter, only save the breakpoints it accepts. */
14473 if (filter && !filter (&tp))
14474 continue;
14476 any = true;
14478 if (is_tracepoint (&tp))
14480 extra_trace_bits = 1;
14482 /* We can stop searching. */
14483 break;
14487 if (!any)
14489 warning (_("Nothing to save."));
14490 return;
14493 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14495 stdio_file fp;
14497 if (!fp.open (expanded_filename.get (), "w"))
14498 error (_("Unable to open file '%s' for saving (%s)"),
14499 expanded_filename.get (), safe_strerror (errno));
14501 if (extra_trace_bits)
14502 save_trace_state_variables (&fp);
14504 for (breakpoint &tp : all_breakpoints ())
14506 /* Skip internal and momentary breakpoints. */
14507 if (!user_breakpoint_p (&tp))
14508 continue;
14510 /* If we have a filter, only save the breakpoints it accepts. */
14511 if (filter && !filter (&tp))
14512 continue;
14514 tp.print_recreate (&fp);
14516 /* Note, we can't rely on tp->number for anything, as we can't
14517 assume the recreated breakpoint numbers will match. Use $bpnum
14518 instead. */
14520 if (tp.cond_string)
14521 fp.printf (" condition $bpnum %s\n", tp.cond_string.get ());
14523 if (tp.ignore_count)
14524 fp.printf (" ignore $bpnum %d\n", tp.ignore_count);
14526 if (tp.type != bp_dprintf && tp.commands)
14528 fp.puts (" commands\n");
14530 ui_out_redirect_pop redir (current_uiout, &fp);
14531 print_command_lines (current_uiout, tp.commands.get (), 2);
14533 fp.puts (" end\n");
14536 if (tp.enable_state == bp_disabled)
14537 fp.puts ("disable $bpnum\n");
14539 /* If this is a multi-location breakpoint, check if the locations
14540 should be individually disabled. Watchpoint locations are
14541 special, and not user visible. */
14542 if (!is_watchpoint (&tp) && tp.has_multiple_locations ())
14544 int n = 1;
14546 for (bp_location &loc : tp.locations ())
14548 if (!loc.enabled)
14549 fp.printf ("disable $bpnum.%d\n", n);
14551 n++;
14556 if (extra_trace_bits && !default_collect.empty ())
14557 fp.printf ("set default-collect %s\n", default_collect.c_str ());
14559 if (from_tty)
14560 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
14563 /* The `save breakpoints' command. */
14565 static void
14566 save_breakpoints_command (const char *args, int from_tty)
14568 save_breakpoints (args, from_tty, NULL);
14571 /* The `save tracepoints' command. */
14573 static void
14574 save_tracepoints_command (const char *args, int from_tty)
14576 save_breakpoints (args, from_tty, is_tracepoint);
14580 /* This help string is used to consolidate all the help string for specifying
14581 locations used by several commands. */
14583 #define LOCATION_SPEC_HELP_STRING \
14584 "Linespecs are colon-separated lists of location parameters, such as\n\
14585 source filename, function name, label name, and line number.\n\
14586 Example: To specify the start of a label named \"the_top\" in the\n\
14587 function \"fact\" in the file \"factorial.c\", use\n\
14588 \"factorial.c:fact:the_top\".\n\
14590 Address locations begin with \"*\" and specify an exact address in the\n\
14591 program. Example: To specify the fourth byte past the start function\n\
14592 \"main\", use \"*main + 4\".\n\
14594 Explicit locations are similar to linespecs but use an option/argument\n\
14595 syntax to specify location parameters.\n\
14596 Example: To specify the start of the label named \"the_top\" in the\n\
14597 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
14598 -function fact -label the_top\".\n\
14600 By default, a specified function is matched against the program's\n\
14601 functions in all scopes. For C++, this means in all namespaces and\n\
14602 classes. For Ada, this means in all packages. E.g., in C++,\n\
14603 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14604 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
14605 specified name as a complete fully-qualified name instead."
14607 /* This help string is used for the break, hbreak, tbreak and thbreak
14608 commands. It is defined as a macro to prevent duplication.
14609 COMMAND should be a string constant containing the name of the
14610 command. */
14612 #define BREAK_ARGS_HELP(command) \
14613 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14614 \t[-force-condition] [if CONDITION]\n\
14615 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14616 probe point. Accepted values are `-probe' (for a generic, automatically\n\
14617 guessed probe type), `-probe-stap' (for a SystemTap probe) or\n\
14618 `-probe-dtrace' (for a DTrace probe).\n\
14619 LOCATION may be a linespec, address, or explicit location as described\n\
14620 below.\n\
14622 With no LOCATION, uses current execution address of the selected\n\
14623 stack frame. This is useful for breaking on return to a stack frame.\n\
14625 THREADNUM is the number from \"info threads\".\n\
14626 CONDITION is a boolean expression.\n\
14628 With the \"-force-condition\" flag, the condition is defined even when\n\
14629 it is invalid for all current locations.\n\
14630 \n" LOCATION_SPEC_HELP_STRING "\n\n\
14631 Multiple breakpoints at one place are permitted, and useful if their\n\
14632 conditions are different.\n\
14634 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14636 /* List of subcommands for "catch". */
14637 static struct cmd_list_element *catch_cmdlist;
14639 /* List of subcommands for "tcatch". */
14640 static struct cmd_list_element *tcatch_cmdlist;
14642 void
14643 add_catch_command (const char *name, const char *docstring,
14644 cmd_func_ftype *func,
14645 completer_ftype *completer,
14646 void *user_data_catch,
14647 void *user_data_tcatch)
14649 struct cmd_list_element *command;
14651 command = add_cmd (name, class_breakpoint, docstring,
14652 &catch_cmdlist);
14653 command->func = func;
14654 command->set_context (user_data_catch);
14655 set_cmd_completer (command, completer);
14657 command = add_cmd (name, class_breakpoint, docstring,
14658 &tcatch_cmdlist);
14659 command->func = func;
14660 command->set_context (user_data_tcatch);
14661 set_cmd_completer (command, completer);
14664 /* False if any of the breakpoint's locations could be a location where
14665 functions have been inlined, true otherwise. */
14667 static bool
14668 is_non_inline_function (struct breakpoint *b)
14670 /* The shared library event breakpoint is set on the address of a
14671 non-inline function. */
14672 return (b->type == bp_shlib_event);
14675 /* Nonzero if the specified PC cannot be a location where functions
14676 have been inlined. */
14679 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
14680 const target_waitstatus &ws)
14682 for (breakpoint &b : all_breakpoints ())
14684 if (!is_non_inline_function (&b))
14685 continue;
14687 for (bp_location &bl : b.locations ())
14689 if (!bl.shlib_disabled
14690 && bpstat_check_location (&bl, aspace, pc, ws))
14691 return 1;
14695 return 0;
14698 /* Remove any references to OBJFILE which is going to be freed. */
14700 void
14701 breakpoint_free_objfile (struct objfile *objfile)
14703 for (bp_location *loc : all_bp_locations ())
14705 if (loc->symtab != nullptr
14706 && loc->symtab->compunit ()->objfile () == objfile)
14708 loc->symtab = nullptr;
14709 loc->symbol = nullptr;
14710 loc->msymbol = nullptr;
14713 if (loc->section != nullptr
14714 && loc->section->objfile == objfile)
14716 /* If symtab was set then it should have already been cleared.
14717 But if bp_location::msymbol was set then the symbol and symtab
14718 might already have been nullptr. */
14719 gdb_assert (loc->symtab == nullptr);
14720 loc->section = nullptr;
14721 loc->symbol = nullptr;
14722 loc->msymbol = nullptr;
14725 if (loc->probe.objfile == objfile)
14726 loc->probe = bound_probe ();
14730 /* Chain containing all defined "enable breakpoint" subcommands. */
14732 static struct cmd_list_element *enablebreaklist = NULL;
14734 /* See breakpoint.h. */
14736 cmd_list_element *commands_cmd_element = nullptr;
14738 void _initialize_breakpoint ();
14739 void
14740 _initialize_breakpoint ()
14742 struct cmd_list_element *c;
14744 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14745 "breakpoint");
14746 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14747 "breakpoint");
14748 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14749 "breakpoint");
14751 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14752 before a breakpoint is set. */
14753 breakpoint_count = 0;
14755 tracepoint_count = 0;
14757 add_com ("ignore", class_breakpoint, ignore_command, _("\
14758 Set ignore-count of breakpoint number N to COUNT.\n\
14759 Usage is `ignore N COUNT'."));
14761 commands_cmd_element = add_com ("commands", class_breakpoint,
14762 commands_command, _("\
14763 Set commands to be executed when the given breakpoints are hit.\n\
14764 Give a space-separated breakpoint list as argument after \"commands\".\n\
14765 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14766 (e.g. `5-7').\n\
14767 With no argument, the targeted breakpoint is the last one set.\n\
14768 The commands themselves follow starting on the next line.\n\
14769 Type a line containing \"end\" to indicate the end of them.\n\
14770 Give \"silent\" as the first line to make the breakpoint silent;\n\
14771 then no output is printed when it is hit, except what the commands print."));
14773 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14774 static std::string condition_command_help
14775 = gdb::option::build_help (_("\
14776 Specify breakpoint number N to break only if COND is true.\n\
14777 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
14778 is an expression to be evaluated whenever breakpoint N is reached.\n\
14780 Options:\n\
14781 %OPTIONS%"), cc_opts);
14783 c = add_com ("condition", class_breakpoint, condition_command,
14784 condition_command_help.c_str ());
14785 set_cmd_completer_handle_brkchars (c, condition_completer);
14787 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
14788 Set a temporary breakpoint.\n\
14789 Like \"break\" except the breakpoint is only temporary,\n\
14790 so it will be deleted when hit. Equivalent to \"break\" followed\n\
14791 by using \"enable delete\" on the breakpoint number.\n\
14793 BREAK_ARGS_HELP ("tbreak")));
14794 set_cmd_completer (c, location_completer);
14796 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
14797 Set a hardware assisted breakpoint.\n\
14798 Like \"break\" except the breakpoint requires hardware support,\n\
14799 some target hardware may not have this support.\n\
14801 BREAK_ARGS_HELP ("hbreak")));
14802 set_cmd_completer (c, location_completer);
14804 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
14805 Set a temporary hardware assisted breakpoint.\n\
14806 Like \"hbreak\" except the breakpoint is only temporary,\n\
14807 so it will be deleted when hit.\n\
14809 BREAK_ARGS_HELP ("thbreak")));
14810 set_cmd_completer (c, location_completer);
14812 cmd_list_element *enable_cmd
14813 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14814 Enable all or some breakpoints.\n\
14815 Usage: enable [BREAKPOINTNUM]...\n\
14816 Give breakpoint numbers (separated by spaces) as arguments.\n\
14817 With no subcommand, breakpoints are enabled until you command otherwise.\n\
14818 This is used to cancel the effect of the \"disable\" command.\n\
14819 With a subcommand you can enable temporarily."),
14820 &enablelist, 1, &cmdlist);
14822 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
14824 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
14825 Enable all or some breakpoints.\n\
14826 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
14827 Give breakpoint numbers (separated by spaces) as arguments.\n\
14828 This is used to cancel the effect of the \"disable\" command.\n\
14829 May be abbreviated to simply \"enable\"."),
14830 &enablebreaklist, 1, &enablelist);
14832 add_cmd ("once", no_class, enable_once_command, _("\
14833 Enable some breakpoints for one hit.\n\
14834 Usage: enable breakpoints once BREAKPOINTNUM...\n\
14835 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14836 &enablebreaklist);
14838 add_cmd ("delete", no_class, enable_delete_command, _("\
14839 Enable some breakpoints and delete when hit.\n\
14840 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
14841 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14842 &enablebreaklist);
14844 add_cmd ("count", no_class, enable_count_command, _("\
14845 Enable some breakpoints for COUNT hits.\n\
14846 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
14847 If a breakpoint is hit while enabled in this fashion,\n\
14848 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14849 &enablebreaklist);
14851 add_cmd ("delete", no_class, enable_delete_command, _("\
14852 Enable some breakpoints and delete when hit.\n\
14853 Usage: enable delete BREAKPOINTNUM...\n\
14854 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14855 &enablelist);
14857 add_cmd ("once", no_class, enable_once_command, _("\
14858 Enable some breakpoints for one hit.\n\
14859 Usage: enable once BREAKPOINTNUM...\n\
14860 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14861 &enablelist);
14863 add_cmd ("count", no_class, enable_count_command, _("\
14864 Enable some breakpoints for COUNT hits.\n\
14865 Usage: enable count COUNT BREAKPOINTNUM...\n\
14866 If a breakpoint is hit while enabled in this fashion,\n\
14867 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14868 &enablelist);
14870 cmd_list_element *disable_cmd
14871 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14872 Disable all or some breakpoints.\n\
14873 Usage: disable [BREAKPOINTNUM]...\n\
14874 Arguments are breakpoint numbers with spaces in between.\n\
14875 To disable all breakpoints, give no argument.\n\
14876 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
14877 &disablelist, 1, &cmdlist);
14878 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14879 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
14881 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
14882 Disable all or some breakpoints.\n\
14883 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
14884 Arguments are breakpoint numbers with spaces in between.\n\
14885 To disable all breakpoints, give no argument.\n\
14886 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
14887 This command may be abbreviated \"disable\"."),
14888 &disablelist);
14890 cmd_list_element *delete_cmd
14891 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14892 Delete all or some breakpoints.\n\
14893 Usage: delete [BREAKPOINTNUM]...\n\
14894 Arguments are breakpoint numbers with spaces in between.\n\
14895 To delete all breakpoints, watchpoints, tracepoints, and catchpoints,\n\
14896 give no argument.\n\
14898 Also a prefix command for deletion of other GDB objects."),
14899 &deletelist, 1, &cmdlist);
14900 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14901 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
14903 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
14904 Delete all or some breakpoints or auto-display expressions.\n\
14905 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
14906 Arguments are breakpoint numbers with spaces in between.\n\
14907 To delete all breakpoints, watchpoints, tracepoints, and catchpoints,\n\
14908 give no argument.\n\
14909 This command may be abbreviated \"delete\"."),
14910 &deletelist);
14912 cmd_list_element *clear_cmd
14913 = add_com ("clear", class_breakpoint, clear_command, _("\
14914 Clear breakpoint at specified location.\n\
14915 Argument may be a linespec, explicit, or address location as described below.\n\
14917 With no argument, clears all breakpoints in the line that the selected frame\n\
14918 is executing in.\n"
14919 "\n" LOCATION_SPEC_HELP_STRING "\n\n\
14920 See also the \"delete\" command which clears breakpoints by number."));
14921 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
14923 cmd_list_element *break_cmd
14924 = add_com ("break", class_breakpoint, break_command, _("\
14925 Set breakpoint at specified location.\n"
14926 BREAK_ARGS_HELP ("break")));
14927 set_cmd_completer (break_cmd, location_completer);
14929 add_com_alias ("b", break_cmd, class_run, 1);
14930 add_com_alias ("br", break_cmd, class_run, 1);
14931 add_com_alias ("bre", break_cmd, class_run, 1);
14932 add_com_alias ("brea", break_cmd, class_run, 1);
14934 cmd_list_element *info_breakpoints_cmd
14935 = add_info ("breakpoints", info_breakpoints_command, _("\
14936 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
14937 The \"Type\" column indicates one of:\n\
14938 \tbreakpoint - normal breakpoint\n\
14939 \twatchpoint - watchpoint\n\
14940 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14941 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14942 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14943 address and file/line number respectively.\n\
14945 Convenience variable \"$_\" and default examine address for \"x\"\n\
14946 are set to the address of the last breakpoint listed unless the command\n\
14947 is prefixed with \"server \".\n\n\
14948 Convenience variable \"$bpnum\" contains the number of the last\n\
14949 breakpoint set."));
14951 add_info_alias ("b", info_breakpoints_cmd, 1);
14953 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14954 Status of all breakpoints, or breakpoint number NUMBER.\n\
14955 The \"Type\" column indicates one of:\n\
14956 \tbreakpoint - normal breakpoint\n\
14957 \twatchpoint - watchpoint\n\
14958 \tlongjmp - internal breakpoint used to step through longjmp()\n\
14959 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14960 \tuntil - internal breakpoint used by the \"until\" command\n\
14961 \tfinish - internal breakpoint used by the \"finish\" command\n\
14962 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14963 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14964 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14965 address and file/line number respectively.\n\
14967 Convenience variable \"$_\" and default examine address for \"x\"\n\
14968 are set to the address of the last breakpoint listed unless the command\n\
14969 is prefixed with \"server \".\n\n\
14970 Convenience variable \"$bpnum\" contains the number of the last\n\
14971 breakpoint set."),
14972 &maintenanceinfolist);
14974 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
14975 Set catchpoints to catch events."),
14976 &catch_cmdlist,
14977 0/*allow-unknown*/, &cmdlist);
14979 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
14980 Set temporary catchpoints to catch events."),
14981 &tcatch_cmdlist,
14982 0/*allow-unknown*/, &cmdlist);
14984 const auto opts = make_watch_options_def_group (nullptr);
14986 static const std::string watch_help = gdb::option::build_help (_("\
14987 Set a watchpoint for EXPRESSION.\n\
14988 Usage: watch [-location] EXPRESSION\n\
14990 Options:\n\
14991 %OPTIONS%\n\
14993 A watchpoint stops execution of your program whenever the value of\n\
14994 an expression changes."), opts);
14995 c = add_com ("watch", class_breakpoint, watch_command,
14996 watch_help.c_str ());
14997 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14999 static const std::string rwatch_help = gdb::option::build_help (_("\
15000 Set a read watchpoint for EXPRESSION.\n\
15001 Usage: rwatch [-location] EXPRESSION\n\
15003 Options:\n\
15004 %OPTIONS%\n\
15006 A read watchpoint stops execution of your program whenever the value of\n\
15007 an expression is read."), opts);
15008 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15009 rwatch_help.c_str ());
15010 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15012 static const std::string awatch_help = gdb::option::build_help (_("\
15013 Set an access watchpoint for EXPRESSION.\n\
15014 Usage: awatch [-location] EXPRESSION\n\
15016 Options:\n\
15017 %OPTIONS%\n\
15019 An access watchpoint stops execution of your program whenever the value\n\
15020 of an expression is either read or written."), opts);
15021 c = add_com ("awatch", class_breakpoint, awatch_command,
15022 awatch_help.c_str ());
15023 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15025 add_info ("watchpoints", info_watchpoints_command, _("\
15026 Status of specified watchpoints (all watchpoints if no argument)."));
15028 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15029 respond to changes - contrary to the description. */
15030 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15031 &can_use_hw_watchpoints, _("\
15032 Set debugger's willingness to use watchpoint hardware."), _("\
15033 Show debugger's willingness to use watchpoint hardware."), _("\
15034 If zero, gdb will not use hardware for new watchpoints, even if\n\
15035 such is available. (However, any hardware watchpoints that were\n\
15036 created before setting this to nonzero, will continue to use watchpoint\n\
15037 hardware.)"),
15038 NULL,
15039 show_can_use_hw_watchpoints,
15040 &setlist, &showlist);
15042 can_use_hw_watchpoints = 1;
15044 /* Tracepoint manipulation commands. */
15046 cmd_list_element *trace_cmd
15047 = add_com ("trace", class_breakpoint, trace_command, _("\
15048 Set a tracepoint at specified location.\n\
15050 BREAK_ARGS_HELP ("trace") "\n\
15051 Do \"help tracepoints\" for info on other tracepoint commands."));
15052 set_cmd_completer (trace_cmd, location_completer);
15054 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15055 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15056 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15057 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
15059 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15060 Set a fast tracepoint at specified location.\n\
15062 BREAK_ARGS_HELP ("ftrace") "\n\
15063 Do \"help tracepoints\" for info on other tracepoint commands."));
15064 set_cmd_completer (c, location_completer);
15066 c = add_com ("strace", class_breakpoint, strace_command, _("\
15067 Set a static tracepoint at location or marker.\n\
15069 strace [LOCATION] [if CONDITION]\n\
15070 LOCATION may be a linespec, explicit, or address location (described below)\n\
15071 or -m MARKER_ID.\n\n\
15072 If a marker id is specified, probe the marker with that name. With\n\
15073 no LOCATION, uses current execution address of the selected stack frame.\n\
15074 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15075 This collects arbitrary user data passed in the probe point call to the\n\
15076 tracing library. You can inspect it when analyzing the trace buffer,\n\
15077 by printing the $_sdata variable like any other convenience variable.\n\
15079 CONDITION is a boolean expression.\n\
15080 \n" LOCATION_SPEC_HELP_STRING "\n\n\
15081 Multiple tracepoints at one place are permitted, and useful if their\n\
15082 conditions are different.\n\
15084 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15085 Do \"help tracepoints\" for info on other tracepoint commands."));
15086 set_cmd_completer (c, location_completer);
15088 cmd_list_element *info_tracepoints_cmd
15089 = add_info ("tracepoints", info_tracepoints_command, _("\
15090 Status of specified tracepoints (all tracepoints if no argument).\n\
15091 Convenience variable \"$tpnum\" contains the number of the\n\
15092 last tracepoint set."));
15094 add_info_alias ("tp", info_tracepoints_cmd, 1);
15096 cmd_list_element *delete_tracepoints_cmd
15097 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15098 Delete specified tracepoints.\n\
15099 Arguments are tracepoint numbers, separated by spaces.\n\
15100 No argument means delete all tracepoints."),
15101 &deletelist);
15102 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
15104 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15105 Disable specified tracepoints.\n\
15106 Arguments are tracepoint numbers, separated by spaces.\n\
15107 No argument means disable all tracepoints."),
15108 &disablelist);
15109 deprecate_cmd (c, "disable");
15111 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15112 Enable specified tracepoints.\n\
15113 Arguments are tracepoint numbers, separated by spaces.\n\
15114 No argument means enable all tracepoints."),
15115 &enablelist);
15116 deprecate_cmd (c, "enable");
15118 add_com ("passcount", class_trace, trace_pass_command, _("\
15119 Set the passcount for a tracepoint.\n\
15120 The trace will end when the tracepoint has been passed 'count' times.\n\
15121 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15122 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15124 add_basic_prefix_cmd ("save", class_breakpoint,
15125 _("Save breakpoint definitions as a script."),
15126 &save_cmdlist,
15127 0/*allow-unknown*/, &cmdlist);
15129 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15130 Save current breakpoint definitions as a script.\n\
15131 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15132 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15133 session to restore them."),
15134 &save_cmdlist);
15135 set_cmd_completer (c, deprecated_filename_completer);
15137 cmd_list_element *save_tracepoints_cmd
15138 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15139 Save current tracepoint definitions as a script.\n\
15140 Use the 'source' command in another debug session to restore them."),
15141 &save_cmdlist);
15142 set_cmd_completer (save_tracepoints_cmd, deprecated_filename_completer);
15144 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
15145 deprecate_cmd (c, "save tracepoints");
15147 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
15148 _("\
15149 Breakpoint specific settings.\n\
15150 Configure various breakpoint-specific variables such as\n\
15151 pending breakpoint behavior."),
15152 _("\
15153 Breakpoint specific settings.\n\
15154 Configure various breakpoint-specific variables such as\n\
15155 pending breakpoint behavior."),
15156 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
15157 &setlist, &showlist);
15159 add_setshow_auto_boolean_cmd ("pending", no_class,
15160 &pending_break_support, _("\
15161 Set debugger's behavior regarding pending breakpoints."), _("\
15162 Show debugger's behavior regarding pending breakpoints."), _("\
15163 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15164 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15165 an error. If auto, an unrecognized breakpoint location results in a\n\
15166 user-query to see if a pending breakpoint should be created."),
15167 NULL,
15168 show_pending_break_support,
15169 &breakpoint_set_cmdlist,
15170 &breakpoint_show_cmdlist);
15172 pending_break_support = AUTO_BOOLEAN_AUTO;
15174 add_setshow_boolean_cmd ("auto-hw", no_class,
15175 &automatic_hardware_breakpoints, _("\
15176 Set automatic usage of hardware breakpoints."), _("\
15177 Show automatic usage of hardware breakpoints."), _("\
15178 If set, the debugger will automatically use hardware breakpoints for\n\
15179 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15180 a warning will be emitted for such breakpoints."),
15181 NULL,
15182 show_automatic_hardware_breakpoints,
15183 &breakpoint_set_cmdlist,
15184 &breakpoint_show_cmdlist);
15186 add_setshow_boolean_cmd ("always-inserted", class_support,
15187 &always_inserted_mode, _("\
15188 Set mode for inserting breakpoints."), _("\
15189 Show mode for inserting breakpoints."), _("\
15190 When this mode is on, breakpoints are inserted immediately as soon as\n\
15191 they're created, kept inserted even when execution stops, and removed\n\
15192 only when the user deletes them. When this mode is off (the default),\n\
15193 breakpoints are inserted only when execution continues, and removed\n\
15194 when execution stops."),
15195 NULL,
15196 &show_always_inserted_mode,
15197 &breakpoint_set_cmdlist,
15198 &breakpoint_show_cmdlist);
15200 add_setshow_boolean_cmd ("breakpoint", class_maintenance,
15201 &debug_breakpoint, _("\
15202 Set breakpoint location debugging."), _("\
15203 Show breakpoint location debugging."), _("\
15204 When on, breakpoint location specific debugging is enabled."),
15205 NULL,
15206 show_debug_breakpoint,
15207 &setdebuglist, &showdebuglist);
15209 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15210 condition_evaluation_enums,
15211 &condition_evaluation_mode_1, _("\
15212 Set mode of breakpoint condition evaluation."), _("\
15213 Show mode of breakpoint condition evaluation."), _("\
15214 When this is set to \"host\", breakpoint conditions will be\n\
15215 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15216 breakpoint conditions will be downloaded to the target (if the target\n\
15217 supports such feature) and conditions will be evaluated on the target's side.\n\
15218 If this is set to \"auto\" (default), this will be automatically set to\n\
15219 \"target\" if it supports condition evaluation, otherwise it will\n\
15220 be set to \"host\"."),
15221 &set_condition_evaluation_mode,
15222 &show_condition_evaluation_mode,
15223 &breakpoint_set_cmdlist,
15224 &breakpoint_show_cmdlist);
15226 add_com ("break-range", class_breakpoint, break_range_command, _("\
15227 Set a breakpoint for an address range.\n\
15228 break-range START-LOCATION, END-LOCATION\n\
15229 where START-LOCATION and END-LOCATION can be one of the following:\n\
15230 LINENUM, for that line in the current file,\n\
15231 FILE:LINENUM, for that line in that file,\n\
15232 +OFFSET, for that number of lines after the current line\n\
15233 or the start of the range\n\
15234 FUNCTION, for the first line in that function,\n\
15235 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15236 *ADDRESS, for the instruction at that address.\n\
15238 The breakpoint will stop execution of the inferior whenever it executes\n\
15239 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15240 range (including START-LOCATION and END-LOCATION)."));
15242 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15243 Set a dynamic printf at specified location.\n\
15244 dprintf location,format string,arg1,arg2,...\n\
15245 location may be a linespec, explicit, or address location.\n"
15246 "\n" LOCATION_SPEC_HELP_STRING));
15247 set_cmd_completer (c, location_completer);
15249 add_setshow_enum_cmd ("dprintf-style", class_support,
15250 dprintf_style_enums, &dprintf_style, _("\
15251 Set the style of usage for dynamic printf."), _("\
15252 Show the style of usage for dynamic printf."), _("\
15253 This setting chooses how GDB will do a dynamic printf.\n\
15254 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15255 console, as with the \"printf\" command.\n\
15256 If the value is \"call\", the print is done by calling a function in your\n\
15257 program; by default printf(), but you can choose a different function or\n\
15258 output stream by setting dprintf-function and dprintf-channel."),
15259 update_dprintf_commands, NULL,
15260 &setlist, &showlist);
15262 add_setshow_string_cmd ("dprintf-function", class_support,
15263 &dprintf_function, _("\
15264 Set the function to use for dynamic printf."), _("\
15265 Show the function to use for dynamic printf."), NULL,
15266 update_dprintf_commands, NULL,
15267 &setlist, &showlist);
15269 add_setshow_string_cmd ("dprintf-channel", class_support,
15270 &dprintf_channel, _("\
15271 Set the channel to use for dynamic printf."), _("\
15272 Show the channel to use for dynamic printf."), NULL,
15273 update_dprintf_commands, NULL,
15274 &setlist, &showlist);
15276 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15277 &disconnected_dprintf, _("\
15278 Set whether dprintf continues after GDB disconnects."), _("\
15279 Show whether dprintf continues after GDB disconnects."), _("\
15280 Use this to let dprintf commands continue to hit and produce output\n\
15281 even if GDB disconnects or detaches from the target."),
15282 NULL,
15283 NULL,
15284 &setlist, &showlist);
15286 add_com ("agent-printf", class_vars, agent_printf_command, _("\
15287 Target agent only formatted printing, like the C \"printf\" function.\n\
15288 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15289 This supports most C printf format specifications, like %s, %d, etc.\n\
15290 This is useful for formatted output in user-defined commands."));
15292 automatic_hardware_breakpoints = true;
15294 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
15295 "breakpoint");
15296 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
15297 "breakpoint");
15298 gdb::observers::inferior_removed.attach (remove_inferior_breakpoints,
15299 "breakpoint");