gdb/testsuite: fix gdb.trace/signal.exp on x86
[binutils-gdb/blckswan.git] / gdb / breakpoint.c
blobed932a19ed7b64542bd5ab202808287b3b20161b
1 /* Everything about breakpoints, for GDB.
3 Copyright (C) 1986-2022 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 "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdbsupport/gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "stack.h"
63 #include "ax-gdb.h"
64 #include "dummy-frame.h"
65 #include "interps.h"
66 #include "gdbsupport/format.h"
67 #include "thread-fsm.h"
68 #include "tid-parse.h"
69 #include "cli/cli-style.h"
70 #include "cli/cli-decode.h"
72 /* readline include files */
73 #include "readline/tilde.h"
75 /* readline defines this. */
76 #undef savestring
78 #include "mi/mi-common.h"
79 #include "extension.h"
80 #include <algorithm>
81 #include "progspace-and-thread.h"
82 #include "gdbsupport/array-view.h"
83 #include "gdbsupport/gdb_optional.h"
85 /* Prototypes for local functions. */
87 static void map_breakpoint_numbers (const char *,
88 gdb::function_view<void (breakpoint *)>);
90 static void breakpoint_re_set_default (code_breakpoint *);
92 static void
93 create_sals_from_location_default (struct event_location *location,
94 struct linespec_result *canonical);
96 static void create_breakpoints_sal (struct gdbarch *,
97 struct linespec_result *,
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
101 enum bpdisp, int, int,
102 int,
103 int, int, int, unsigned);
105 static std::vector<symtab_and_line> decode_location_default
106 (struct breakpoint *b, struct event_location *location,
107 struct program_space *search_pspace);
109 static int can_use_hardware_watchpoint
110 (const std::vector<value_ref_ptr> &vals);
112 static void mention (const breakpoint *);
114 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
116 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
118 static struct breakpoint *
119 momentary_breakpoint_from_master (struct breakpoint *orig,
120 enum bptype type,
121 int loc_enabled, int thread);
123 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
125 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
126 CORE_ADDR bpaddr,
127 enum bptype bptype);
129 static int watchpoint_locations_match (struct bp_location *loc1,
130 struct bp_location *loc2);
132 static int breakpoint_locations_match (struct bp_location *loc1,
133 struct bp_location *loc2,
134 bool sw_hw_bps_match = false);
136 static int breakpoint_location_address_match (struct bp_location *bl,
137 const struct address_space *aspace,
138 CORE_ADDR addr);
140 static int breakpoint_location_address_range_overlap (struct bp_location *,
141 const address_space *,
142 CORE_ADDR, int);
144 static int remove_breakpoint (struct bp_location *);
145 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
147 static enum print_stop_action print_bp_stop_message (bpstat *bs);
149 static int hw_breakpoint_used_count (void);
151 static int hw_watchpoint_use_count (struct breakpoint *);
153 static int hw_watchpoint_used_count_others (struct breakpoint *except,
154 enum bptype type,
155 int *other_type_used);
157 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
158 int count);
160 static void decref_bp_location (struct bp_location **loc);
162 static std::vector<symtab_and_line> bkpt_probe_decode_location
163 (struct breakpoint *b,
164 struct event_location *location,
165 struct program_space *search_pspace);
167 static bool bl_address_is_meaningful (bp_location *loc);
169 /* update_global_location_list's modes of operation wrt to whether to
170 insert locations now. */
171 enum ugll_insert_mode
173 /* Don't insert any breakpoint locations into the inferior, only
174 remove already-inserted locations that no longer should be
175 inserted. Functions that delete a breakpoint or breakpoints
176 should specify this mode, so that deleting a breakpoint doesn't
177 have the side effect of inserting the locations of other
178 breakpoints that are marked not-inserted, but should_be_inserted
179 returns true on them.
181 This behavior is useful is situations close to tear-down -- e.g.,
182 after an exec, while the target still has execution, but
183 breakpoint shadows of the previous executable image should *NOT*
184 be restored to the new image; or before detaching, where the
185 target still has execution and wants to delete breakpoints from
186 GDB's lists, and all breakpoints had already been removed from
187 the inferior. */
188 UGLL_DONT_INSERT,
190 /* May insert breakpoints iff breakpoints_should_be_inserted_now
191 claims breakpoints should be inserted now. */
192 UGLL_MAY_INSERT,
194 /* Insert locations now, irrespective of
195 breakpoints_should_be_inserted_now. E.g., say all threads are
196 stopped right now, and the user did "continue". We need to
197 insert breakpoints _before_ resuming the target, but
198 UGLL_MAY_INSERT wouldn't insert them, because
199 breakpoints_should_be_inserted_now returns false at that point,
200 as no thread is running yet. */
201 UGLL_INSERT
204 static void update_global_location_list (enum ugll_insert_mode);
206 static void update_global_location_list_nothrow (enum ugll_insert_mode);
208 static void insert_breakpoint_locations (void);
210 static void trace_pass_command (const char *, int);
212 static void set_tracepoint_count (int num);
214 static bool is_masked_watchpoint (const struct breakpoint *b);
216 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
217 otherwise. */
219 static int strace_marker_p (struct breakpoint *b);
221 static void bkpt_probe_create_sals_from_location
222 (struct event_location *location,
223 struct linespec_result *canonical);
224 static void tracepoint_probe_create_sals_from_location
225 (struct event_location *location,
226 struct linespec_result *canonical);
228 const struct breakpoint_ops code_breakpoint_ops =
230 create_sals_from_location_default,
231 create_breakpoints_sal,
234 /* Breakpoints set on probes. */
235 static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
237 bkpt_probe_create_sals_from_location,
238 create_breakpoints_sal,
241 /* Tracepoints set on probes. */
242 static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
244 tracepoint_probe_create_sals_from_location,
245 create_breakpoints_sal,
248 /* Implementation of abstract dtors. These must exist to satisfy the
249 linker. */
251 breakpoint::~breakpoint ()
255 code_breakpoint::~code_breakpoint ()
259 catchpoint::~catchpoint ()
263 /* The structure to be used in regular breakpoints. */
264 struct ordinary_breakpoint : public code_breakpoint
266 using code_breakpoint::code_breakpoint;
268 int resources_needed (const struct bp_location *) override;
269 enum print_stop_action print_it (const bpstat *bs) const override;
270 void print_mention () const override;
271 void print_recreate (struct ui_file *fp) const override;
274 /* Internal breakpoints. These typically have a lifetime the same as
275 the program, and they end up installed on the breakpoint chain with
276 a negative breakpoint number. They're visible in "maint info
277 breakpoints", but not "info breakpoints". */
278 struct internal_breakpoint : public code_breakpoint
280 internal_breakpoint (struct gdbarch *gdbarch,
281 enum bptype type, CORE_ADDR address)
282 : code_breakpoint (gdbarch, type)
284 symtab_and_line sal;
285 sal.pc = address;
286 sal.section = find_pc_overlay (sal.pc);
287 sal.pspace = current_program_space;
288 add_location (sal);
290 pspace = current_program_space;
291 disposition = disp_donttouch;
294 void re_set () override;
295 void check_status (struct bpstat *bs) override;
296 enum print_stop_action print_it (const bpstat *bs) const override;
297 void print_mention () const override;
300 /* Momentary breakpoints. These typically have a lifetime of some run
301 control command only, are always thread-specific, and have 0 for
302 breakpoint number. I.e., there can be many momentary breakpoints
303 on the breakpoint chain and they all same the same number (zero).
304 They're visible in "maint info breakpoints", but not "info
305 breakpoints". */
306 struct momentary_breakpoint : public code_breakpoint
308 momentary_breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
309 program_space *pspace_,
310 const struct frame_id &frame_id_,
311 int thread_)
312 : code_breakpoint (gdbarch_, bptype)
314 /* If FRAME_ID is valid, it should be a real frame, not an inlined
315 or tail-called one. */
316 gdb_assert (!frame_id_artificial_p (frame_id));
318 /* Momentary breakpoints are always thread-specific. */
319 gdb_assert (thread_ > 0);
321 pspace = pspace_;
322 enable_state = bp_enabled;
323 disposition = disp_donttouch;
324 frame_id = frame_id_;
325 thread = thread_;
328 void re_set () override;
329 void check_status (struct bpstat *bs) override;
330 enum print_stop_action print_it (const bpstat *bs) const override;
331 void print_mention () const override;
334 /* DPrintf breakpoints. */
335 struct dprintf_breakpoint : public ordinary_breakpoint
337 using ordinary_breakpoint::ordinary_breakpoint;
339 void re_set () override;
340 int breakpoint_hit (const struct bp_location *bl,
341 const address_space *aspace,
342 CORE_ADDR bp_addr,
343 const target_waitstatus &ws) override;
344 void print_recreate (struct ui_file *fp) const override;
345 void after_condition_true (struct bpstat *bs) override;
348 /* Ranged breakpoints. */
349 struct ranged_breakpoint : public ordinary_breakpoint
351 explicit ranged_breakpoint (struct gdbarch *gdbarch,
352 const symtab_and_line &sal_start,
353 int length,
354 event_location_up start_location,
355 event_location_up end_location)
356 : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
358 bp_location *bl = add_location (sal_start);
359 bl->length = length;
361 disposition = disp_donttouch;
363 location = std::move (start_location);
364 location_range_end = std::move (end_location);
367 int breakpoint_hit (const struct bp_location *bl,
368 const address_space *aspace,
369 CORE_ADDR bp_addr,
370 const target_waitstatus &ws) override;
371 int resources_needed (const struct bp_location *) override;
372 enum print_stop_action print_it (const bpstat *bs) const override;
373 bool print_one (bp_location **) const override;
374 void print_one_detail (struct ui_out *) const override;
375 void print_mention () const override;
376 void print_recreate (struct ui_file *fp) const override;
379 /* Static tracepoints with marker (`-m'). */
380 struct static_marker_tracepoint : public tracepoint
382 using tracepoint::tracepoint;
384 std::vector<symtab_and_line> decode_location
385 (struct event_location *location,
386 struct program_space *search_pspace) override;
389 /* The style in which to perform a dynamic printf. This is a user
390 option because different output options have different tradeoffs;
391 if GDB does the printing, there is better error handling if there
392 is a problem with any of the arguments, but using an inferior
393 function lets you have special-purpose printers and sending of
394 output to the same place as compiled-in print functions. */
396 static const char dprintf_style_gdb[] = "gdb";
397 static const char dprintf_style_call[] = "call";
398 static const char dprintf_style_agent[] = "agent";
399 static const char *const dprintf_style_enums[] = {
400 dprintf_style_gdb,
401 dprintf_style_call,
402 dprintf_style_agent,
403 NULL
405 static const char *dprintf_style = dprintf_style_gdb;
407 /* The function to use for dynamic printf if the preferred style is to
408 call into the inferior. The value is simply a string that is
409 copied into the command, so it can be anything that GDB can
410 evaluate to a callable address, not necessarily a function name. */
412 static std::string dprintf_function = "printf";
414 /* The channel to use for dynamic printf if the preferred style is to
415 call into the inferior; if a nonempty string, it will be passed to
416 the call as the first argument, with the format string as the
417 second. As with the dprintf function, this can be anything that
418 GDB knows how to evaluate, so in addition to common choices like
419 "stderr", this could be an app-specific expression like
420 "mystreams[curlogger]". */
422 static std::string dprintf_channel;
424 /* True if dprintf commands should continue to operate even if GDB
425 has disconnected. */
426 static bool disconnected_dprintf = true;
428 struct command_line *
429 breakpoint_commands (struct breakpoint *b)
431 return b->commands ? b->commands.get () : NULL;
434 /* Flag indicating that a command has proceeded the inferior past the
435 current breakpoint. */
437 static bool breakpoint_proceeded;
439 const char *
440 bpdisp_text (enum bpdisp disp)
442 /* NOTE: the following values are a part of MI protocol and
443 represent values of 'disp' field returned when inferior stops at
444 a breakpoint. */
445 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
447 return bpdisps[(int) disp];
450 /* Prototypes for exported functions. */
451 /* If FALSE, gdb will not use hardware support for watchpoints, even
452 if such is available. */
453 static int can_use_hw_watchpoints;
455 static void
456 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
457 struct cmd_list_element *c,
458 const char *value)
460 gdb_printf (file,
461 _("Debugger's willingness to use "
462 "watchpoint hardware is %s.\n"),
463 value);
466 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
467 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
468 for unrecognized breakpoint locations.
469 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
470 static enum auto_boolean pending_break_support;
471 static void
472 show_pending_break_support (struct ui_file *file, int from_tty,
473 struct cmd_list_element *c,
474 const char *value)
476 gdb_printf (file,
477 _("Debugger's behavior regarding "
478 "pending breakpoints is %s.\n"),
479 value);
482 /* If true, gdb will automatically use hardware breakpoints for breakpoints
483 set with "break" but falling in read-only memory.
484 If false, gdb will warn about such breakpoints, but won't automatically
485 use hardware breakpoints. */
486 static bool automatic_hardware_breakpoints;
487 static void
488 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
489 struct cmd_list_element *c,
490 const char *value)
492 gdb_printf (file,
493 _("Automatic usage of hardware breakpoints is %s.\n"),
494 value);
497 /* If on, GDB keeps breakpoints inserted even if the inferior is
498 stopped, and immediately inserts any new breakpoints as soon as
499 they're created. If off (default), GDB keeps breakpoints off of
500 the target as long as possible. That is, it delays inserting
501 breakpoints until the next resume, and removes them again when the
502 target fully stops. This is a bit safer in case GDB crashes while
503 processing user input. */
504 static bool always_inserted_mode = false;
506 static void
507 show_always_inserted_mode (struct ui_file *file, int from_tty,
508 struct cmd_list_element *c, const char *value)
510 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
511 value);
514 /* See breakpoint.h. */
517 breakpoints_should_be_inserted_now (void)
519 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
521 /* If breakpoints are global, they should be inserted even if no
522 thread under gdb's control is running, or even if there are
523 no threads under GDB's control yet. */
524 return 1;
526 else
528 if (always_inserted_mode)
530 /* The user wants breakpoints inserted even if all threads
531 are stopped. */
532 return 1;
535 for (inferior *inf : all_inferiors ())
536 if (inf->has_execution ()
537 && threads_are_executing (inf->process_target ()))
538 return 1;
540 /* Don't remove breakpoints yet if, even though all threads are
541 stopped, we still have events to process. */
542 for (thread_info *tp : all_non_exited_threads ())
543 if (tp->resumed () && tp->has_pending_waitstatus ())
544 return 1;
546 return 0;
549 static const char condition_evaluation_both[] = "host or target";
551 /* Modes for breakpoint condition evaluation. */
552 static const char condition_evaluation_auto[] = "auto";
553 static const char condition_evaluation_host[] = "host";
554 static const char condition_evaluation_target[] = "target";
555 static const char *const condition_evaluation_enums[] = {
556 condition_evaluation_auto,
557 condition_evaluation_host,
558 condition_evaluation_target,
559 NULL
562 /* Global that holds the current mode for breakpoint condition evaluation. */
563 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
565 /* Global that we use to display information to the user (gets its value from
566 condition_evaluation_mode_1. */
567 static const char *condition_evaluation_mode = condition_evaluation_auto;
569 /* Translate a condition evaluation mode MODE into either "host"
570 or "target". This is used mostly to translate from "auto" to the
571 real setting that is being used. It returns the translated
572 evaluation mode. */
574 static const char *
575 translate_condition_evaluation_mode (const char *mode)
577 if (mode == condition_evaluation_auto)
579 if (target_supports_evaluation_of_breakpoint_conditions ())
580 return condition_evaluation_target;
581 else
582 return condition_evaluation_host;
584 else
585 return mode;
588 /* Discovers what condition_evaluation_auto translates to. */
590 static const char *
591 breakpoint_condition_evaluation_mode (void)
593 return translate_condition_evaluation_mode (condition_evaluation_mode);
596 /* Return true if GDB should evaluate breakpoint conditions or false
597 otherwise. */
599 static int
600 gdb_evaluates_breakpoint_condition_p (void)
602 const char *mode = breakpoint_condition_evaluation_mode ();
604 return (mode == condition_evaluation_host);
607 /* Are we executing breakpoint commands? */
608 static int executing_breakpoint_commands;
610 /* Are overlay event breakpoints enabled? */
611 static int overlay_events_enabled;
613 /* See description in breakpoint.h. */
614 bool target_exact_watchpoints = false;
616 /* Walk the following statement or block through all breakpoints.
617 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
618 current breakpoint. */
620 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
621 for (B = breakpoint_chain; \
622 B ? (TMP=B->next, 1): 0; \
623 B = TMP)
625 /* Chains of all breakpoints defined. */
627 static struct breakpoint *breakpoint_chain;
629 /* See breakpoint.h. */
631 breakpoint_range
632 all_breakpoints ()
634 return breakpoint_range (breakpoint_chain);
637 /* See breakpoint.h. */
639 breakpoint_safe_range
640 all_breakpoints_safe ()
642 return breakpoint_safe_range (all_breakpoints ());
645 /* See breakpoint.h. */
647 tracepoint_range
648 all_tracepoints ()
650 return tracepoint_range (breakpoint_chain);
653 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
655 static std::vector<bp_location *> bp_locations;
657 /* See breakpoint.h. */
659 const std::vector<bp_location *> &
660 all_bp_locations ()
662 return bp_locations;
665 /* Range to iterate over breakpoint locations at a given address. */
667 struct bp_locations_at_addr_range
669 using iterator = std::vector<bp_location *>::iterator;
671 bp_locations_at_addr_range (CORE_ADDR addr)
673 struct compare
675 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
676 { return loc->address < addr_; }
678 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
679 { return addr_ < loc->address; }
682 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
683 addr, compare ());
685 m_begin = it_pair.first;
686 m_end = it_pair.second;
689 iterator begin () const
690 { return m_begin; }
692 iterator end () const
693 { return m_end; }
695 private:
696 iterator m_begin;
697 iterator m_end;
700 /* Return a range to iterate over all breakpoint locations exactly at address
701 ADDR.
703 If it's needed to iterate multiple times on the same range, it's possible
704 to save the range in a local variable and use it multiple times:
706 auto range = all_bp_locations_at_addr (addr);
708 for (bp_location *loc : range)
709 // use loc
711 for (bp_location *loc : range)
712 // use loc
714 This saves a bit of time, as it avoids re-doing the binary searches to find
715 the range's boundaries. Just remember not to change the bp_locations vector
716 in the mean time, as it could make the range's iterators stale. */
718 static bp_locations_at_addr_range
719 all_bp_locations_at_addr (CORE_ADDR addr)
721 return bp_locations_at_addr_range (addr);
724 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
725 ADDRESS for the current elements of BP_LOCATIONS which get a valid
726 result from bp_location_has_shadow. You can use it for roughly
727 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
728 an address you need to read. */
730 static CORE_ADDR bp_locations_placed_address_before_address_max;
732 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
733 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
734 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
735 You can use it for roughly limiting the subrange of BP_LOCATIONS to
736 scan for shadow bytes for an address you need to read. */
738 static CORE_ADDR bp_locations_shadow_len_after_address_max;
740 /* The locations that no longer correspond to any breakpoint, unlinked
741 from the bp_locations array, but for which a hit may still be
742 reported by a target. */
743 static std::vector<bp_location *> moribund_locations;
745 /* Number of last breakpoint made. */
747 static int breakpoint_count;
749 /* The value of `breakpoint_count' before the last command that
750 created breakpoints. If the last (break-like) command created more
751 than one breakpoint, then the difference between BREAKPOINT_COUNT
752 and PREV_BREAKPOINT_COUNT is more than one. */
753 static int prev_breakpoint_count;
755 /* Number of last tracepoint made. */
757 static int tracepoint_count;
759 static struct cmd_list_element *breakpoint_set_cmdlist;
760 static struct cmd_list_element *breakpoint_show_cmdlist;
761 struct cmd_list_element *save_cmdlist;
763 /* Return whether a breakpoint is an active enabled breakpoint. */
764 static int
765 breakpoint_enabled (struct breakpoint *b)
767 return (b->enable_state == bp_enabled);
770 /* Set breakpoint count to NUM. */
772 static void
773 set_breakpoint_count (int num)
775 prev_breakpoint_count = breakpoint_count;
776 breakpoint_count = num;
777 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
780 /* Used by `start_rbreak_breakpoints' below, to record the current
781 breakpoint count before "rbreak" creates any breakpoint. */
782 static int rbreak_start_breakpoint_count;
784 /* Called at the start an "rbreak" command to record the first
785 breakpoint made. */
787 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
789 rbreak_start_breakpoint_count = breakpoint_count;
792 /* Called at the end of an "rbreak" command to record the last
793 breakpoint made. */
795 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
797 prev_breakpoint_count = rbreak_start_breakpoint_count;
800 /* Used in run_command to zero the hit count when a new run starts. */
802 void
803 clear_breakpoint_hit_counts (void)
805 for (breakpoint *b : all_breakpoints ())
806 b->hit_count = 0;
810 /* Return the breakpoint with the specified number, or NULL
811 if the number does not refer to an existing breakpoint. */
813 struct breakpoint *
814 get_breakpoint (int num)
816 for (breakpoint *b : all_breakpoints ())
817 if (b->number == num)
818 return b;
820 return nullptr;
825 /* Mark locations as "conditions have changed" in case the target supports
826 evaluating conditions on its side. */
828 static void
829 mark_breakpoint_modified (struct breakpoint *b)
831 /* This is only meaningful if the target is
832 evaluating conditions and if the user has
833 opted for condition evaluation on the target's
834 side. */
835 if (gdb_evaluates_breakpoint_condition_p ()
836 || !target_supports_evaluation_of_breakpoint_conditions ())
837 return;
839 if (!is_breakpoint (b))
840 return;
842 for (bp_location *loc : b->locations ())
843 loc->condition_changed = condition_modified;
846 /* Mark location as "conditions have changed" in case the target supports
847 evaluating conditions on its side. */
849 static void
850 mark_breakpoint_location_modified (struct bp_location *loc)
852 /* This is only meaningful if the target is
853 evaluating conditions and if the user has
854 opted for condition evaluation on the target's
855 side. */
856 if (gdb_evaluates_breakpoint_condition_p ()
857 || !target_supports_evaluation_of_breakpoint_conditions ())
859 return;
861 if (!is_breakpoint (loc->owner))
862 return;
864 loc->condition_changed = condition_modified;
867 /* Sets the condition-evaluation mode using the static global
868 condition_evaluation_mode. */
870 static void
871 set_condition_evaluation_mode (const char *args, int from_tty,
872 struct cmd_list_element *c)
874 const char *old_mode, *new_mode;
876 if ((condition_evaluation_mode_1 == condition_evaluation_target)
877 && !target_supports_evaluation_of_breakpoint_conditions ())
879 condition_evaluation_mode_1 = condition_evaluation_mode;
880 warning (_("Target does not support breakpoint condition evaluation.\n"
881 "Using host evaluation mode instead."));
882 return;
885 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
886 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
888 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
889 settings was "auto". */
890 condition_evaluation_mode = condition_evaluation_mode_1;
892 /* Only update the mode if the user picked a different one. */
893 if (new_mode != old_mode)
895 /* If the user switched to a different evaluation mode, we
896 need to synch the changes with the target as follows:
898 "host" -> "target": Send all (valid) conditions to the target.
899 "target" -> "host": Remove all the conditions from the target.
902 if (new_mode == condition_evaluation_target)
904 /* Mark everything modified and synch conditions with the
905 target. */
906 for (bp_location *loc : all_bp_locations ())
907 mark_breakpoint_location_modified (loc);
909 else
911 /* Manually mark non-duplicate locations to synch conditions
912 with the target. We do this to remove all the conditions the
913 target knows about. */
914 for (bp_location *loc : all_bp_locations ())
915 if (is_breakpoint (loc->owner) && loc->inserted)
916 loc->needs_update = 1;
919 /* Do the update. */
920 update_global_location_list (UGLL_MAY_INSERT);
923 return;
926 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
927 what "auto" is translating to. */
929 static void
930 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
931 struct cmd_list_element *c, const char *value)
933 if (condition_evaluation_mode == condition_evaluation_auto)
934 gdb_printf (file,
935 _("Breakpoint condition evaluation "
936 "mode is %s (currently %s).\n"),
937 value,
938 breakpoint_condition_evaluation_mode ());
939 else
940 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
941 value);
944 /* Parse COND_STRING in the context of LOC and set as the condition
945 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
946 the number of LOC within its owner. In case of parsing error, mark
947 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
949 static void
950 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
951 int bp_num, int loc_num)
953 bool has_junk = false;
956 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
957 block_for_pc (loc->address), 0);
958 if (*cond_string != 0)
959 has_junk = true;
960 else
962 loc->cond = std::move (new_exp);
963 if (loc->disabled_by_cond && loc->enabled)
964 gdb_printf (_("Breakpoint %d's condition is now valid at "
965 "location %d, enabling.\n"),
966 bp_num, loc_num);
968 loc->disabled_by_cond = false;
971 catch (const gdb_exception_error &e)
973 if (loc->enabled)
975 /* Warn if a user-enabled location is now becoming disabled-by-cond.
976 BP_NUM is 0 if the breakpoint is being defined for the first
977 time using the "break ... if ..." command, and non-zero if
978 already defined. */
979 if (bp_num != 0)
980 warning (_("failed to validate condition at location %d.%d, "
981 "disabling:\n %s"), bp_num, loc_num, e.what ());
982 else
983 warning (_("failed to validate condition at location %d, "
984 "disabling:\n %s"), loc_num, e.what ());
987 loc->disabled_by_cond = true;
990 if (has_junk)
991 error (_("Garbage '%s' follows condition"), cond_string);
994 void
995 set_breakpoint_condition (struct breakpoint *b, const char *exp,
996 int from_tty, bool force)
998 if (*exp == 0)
1000 b->cond_string.reset ();
1002 if (is_watchpoint (b))
1003 static_cast<watchpoint *> (b)->cond_exp.reset ();
1004 else
1006 int loc_num = 1;
1007 for (bp_location *loc : b->locations ())
1009 loc->cond.reset ();
1010 if (loc->disabled_by_cond && loc->enabled)
1011 gdb_printf (_("Breakpoint %d's condition is now valid at "
1012 "location %d, enabling.\n"),
1013 b->number, loc_num);
1014 loc->disabled_by_cond = false;
1015 loc_num++;
1017 /* No need to free the condition agent expression
1018 bytecode (if we have one). We will handle this
1019 when we go through update_global_location_list. */
1023 if (from_tty)
1024 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
1026 else
1028 if (is_watchpoint (b))
1030 innermost_block_tracker tracker;
1031 const char *arg = exp;
1032 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
1033 if (*arg != 0)
1034 error (_("Junk at end of expression"));
1035 watchpoint *w = static_cast<watchpoint *> (b);
1036 w->cond_exp = std::move (new_exp);
1037 w->cond_exp_valid_block = tracker.block ();
1039 else
1041 /* Parse and set condition expressions. We make two passes.
1042 In the first, we parse the condition string to see if it
1043 is valid in at least one location. If so, the condition
1044 would be accepted. So we go ahead and set the locations'
1045 conditions. In case no valid case is found, we throw
1046 the error and the condition string will be rejected.
1047 This two-pass approach is taken to avoid setting the
1048 state of locations in case of a reject. */
1049 for (bp_location *loc : b->locations ())
1053 const char *arg = exp;
1054 parse_exp_1 (&arg, loc->address,
1055 block_for_pc (loc->address), 0);
1056 if (*arg != 0)
1057 error (_("Junk at end of expression"));
1058 break;
1060 catch (const gdb_exception_error &e)
1062 /* Condition string is invalid. If this happens to
1063 be the last loc, abandon (if not forced) or continue
1064 (if forced). */
1065 if (loc->next == nullptr && !force)
1066 throw;
1070 /* If we reach here, the condition is valid at some locations. */
1071 int loc_num = 1;
1072 for (bp_location *loc : b->locations ())
1074 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
1075 loc_num++;
1079 /* We know that the new condition parsed successfully. The
1080 condition string of the breakpoint can be safely updated. */
1081 b->cond_string = make_unique_xstrdup (exp);
1082 b->condition_not_parsed = 0;
1084 mark_breakpoint_modified (b);
1086 gdb::observers::breakpoint_modified.notify (b);
1089 /* See breakpoint.h. */
1091 void
1092 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1093 bool force)
1095 for (breakpoint *b : all_breakpoints ())
1096 if (b->number == bpnum)
1098 /* Check if this breakpoint has a "stop" method implemented in an
1099 extension language. This method and conditions entered into GDB
1100 from the CLI are mutually exclusive. */
1101 const struct extension_language_defn *extlang
1102 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1104 if (extlang != NULL)
1106 error (_("Only one stop condition allowed. There is currently"
1107 " a %s stop condition defined for this breakpoint."),
1108 ext_lang_capitalized_name (extlang));
1110 set_breakpoint_condition (b, exp, from_tty, force);
1112 if (is_breakpoint (b))
1113 update_global_location_list (UGLL_MAY_INSERT);
1115 return;
1118 error (_("No breakpoint number %d."), bpnum);
1121 /* The options for the "condition" command. */
1123 struct condition_command_opts
1125 /* For "-force". */
1126 bool force_condition = false;
1129 static const gdb::option::option_def condition_command_option_defs[] = {
1131 gdb::option::flag_option_def<condition_command_opts> {
1132 "force",
1133 [] (condition_command_opts *opts) { return &opts->force_condition; },
1134 N_("Set the condition even if it is invalid for all current locations."),
1139 /* Create an option_def_group for the "condition" options, with
1140 CC_OPTS as context. */
1142 static inline gdb::option::option_def_group
1143 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1145 return {{condition_command_option_defs}, cc_opts};
1148 /* Completion for the "condition" command. */
1150 static void
1151 condition_completer (struct cmd_list_element *cmd,
1152 completion_tracker &tracker,
1153 const char *text, const char * /*word*/)
1155 bool has_no_arguments = (*text == '\0');
1156 condition_command_opts cc_opts;
1157 const auto group = make_condition_command_options_def_group (&cc_opts);
1158 if (gdb::option::complete_options
1159 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1160 return;
1162 text = skip_spaces (text);
1163 const char *space = skip_to_space (text);
1164 if (*space == '\0')
1166 int len;
1168 if (text[0] == '$')
1170 tracker.advance_custom_word_point_by (1);
1171 /* We don't support completion of history indices. */
1172 if (!isdigit (text[1]))
1173 complete_internalvar (tracker, &text[1]);
1174 return;
1177 /* Suggest the "-force" flag if no arguments are given. If
1178 arguments were passed, they either already include the flag,
1179 or we are beyond the point of suggesting it because it's
1180 positionally the first argument. */
1181 if (has_no_arguments)
1182 gdb::option::complete_on_all_options (tracker, group);
1184 /* We're completing the breakpoint number. */
1185 len = strlen (text);
1187 for (breakpoint *b : all_breakpoints ())
1189 char number[50];
1191 xsnprintf (number, sizeof (number), "%d", b->number);
1193 if (strncmp (number, text, len) == 0)
1194 tracker.add_completion (make_unique_xstrdup (number));
1197 return;
1200 /* We're completing the expression part. Skip the breakpoint num. */
1201 const char *exp_start = skip_spaces (space);
1202 tracker.advance_custom_word_point_by (exp_start - text);
1203 text = exp_start;
1204 const char *word = advance_to_expression_complete_word_point (tracker, text);
1205 expression_completer (cmd, tracker, text, word);
1208 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1210 static void
1211 condition_command (const char *arg, int from_tty)
1213 const char *p;
1214 int bnum;
1216 if (arg == 0)
1217 error_no_arg (_("breakpoint number"));
1219 p = arg;
1221 /* Check if the "-force" flag was passed. */
1222 condition_command_opts cc_opts;
1223 const auto group = make_condition_command_options_def_group (&cc_opts);
1224 gdb::option::process_options
1225 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1227 bnum = get_number (&p);
1228 if (bnum == 0)
1229 error (_("Bad breakpoint argument: '%s'"), arg);
1231 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1234 /* Check that COMMAND do not contain commands that are suitable
1235 only for tracepoints and not suitable for ordinary breakpoints.
1236 Throw if any such commands is found. */
1238 static void
1239 check_no_tracepoint_commands (struct command_line *commands)
1241 struct command_line *c;
1243 for (c = commands; c; c = c->next)
1245 if (c->control_type == while_stepping_control)
1246 error (_("The 'while-stepping' command can "
1247 "only be used for tracepoints"));
1249 check_no_tracepoint_commands (c->body_list_0.get ());
1250 check_no_tracepoint_commands (c->body_list_1.get ());
1252 /* Not that command parsing removes leading whitespace and comment
1253 lines and also empty lines. So, we only need to check for
1254 command directly. */
1255 if (strstr (c->line, "collect ") == c->line)
1256 error (_("The 'collect' command can only be used for tracepoints"));
1258 if (strstr (c->line, "teval ") == c->line)
1259 error (_("The 'teval' command can only be used for tracepoints"));
1263 struct longjmp_breakpoint : public momentary_breakpoint
1265 using momentary_breakpoint::momentary_breakpoint;
1267 ~longjmp_breakpoint () override;
1270 /* Encapsulate tests for different types of tracepoints. */
1272 static bool
1273 is_tracepoint_type (bptype type)
1275 return (type == bp_tracepoint
1276 || type == bp_fast_tracepoint
1277 || type == bp_static_tracepoint
1278 || type == bp_static_marker_tracepoint);
1281 /* See breakpoint.h. */
1283 bool
1284 is_tracepoint (const struct breakpoint *b)
1286 return is_tracepoint_type (b->type);
1289 /* Factory function to create an appropriate instance of breakpoint given
1290 TYPE. */
1292 template<typename... Arg>
1293 static std::unique_ptr<code_breakpoint>
1294 new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type,
1295 Arg&&... args)
1297 code_breakpoint *b;
1299 switch (type)
1301 case bp_breakpoint:
1302 case bp_hardware_breakpoint:
1303 b = new ordinary_breakpoint (gdbarch, type,
1304 std::forward<Arg> (args)...);
1305 break;
1307 case bp_fast_tracepoint:
1308 case bp_static_tracepoint:
1309 case bp_tracepoint:
1310 b = new tracepoint (gdbarch, type,
1311 std::forward<Arg> (args)...);
1312 break;
1314 case bp_static_marker_tracepoint:
1315 b = new static_marker_tracepoint (gdbarch, type,
1316 std::forward<Arg> (args)...);
1317 break;
1319 case bp_dprintf:
1320 b = new dprintf_breakpoint (gdbarch, type,
1321 std::forward<Arg> (args)...);
1322 break;
1324 default:
1325 gdb_assert_not_reached ("invalid type");
1328 return std::unique_ptr<code_breakpoint> (b);
1331 /* A helper function that validates that COMMANDS are valid for a
1332 breakpoint. This function will throw an exception if a problem is
1333 found. */
1335 static void
1336 validate_commands_for_breakpoint (struct breakpoint *b,
1337 struct command_line *commands)
1339 if (is_tracepoint (b))
1341 struct tracepoint *t = (struct tracepoint *) b;
1342 struct command_line *c;
1343 struct command_line *while_stepping = 0;
1345 /* Reset the while-stepping step count. The previous commands
1346 might have included a while-stepping action, while the new
1347 ones might not. */
1348 t->step_count = 0;
1350 /* We need to verify that each top-level element of commands is
1351 valid for tracepoints, that there's at most one
1352 while-stepping element, and that the while-stepping's body
1353 has valid tracing commands excluding nested while-stepping.
1354 We also need to validate the tracepoint action line in the
1355 context of the tracepoint --- validate_actionline actually
1356 has side effects, like setting the tracepoint's
1357 while-stepping STEP_COUNT, in addition to checking if the
1358 collect/teval actions parse and make sense in the
1359 tracepoint's context. */
1360 for (c = commands; c; c = c->next)
1362 if (c->control_type == while_stepping_control)
1364 if (b->type == bp_fast_tracepoint)
1365 error (_("The 'while-stepping' command "
1366 "cannot be used for fast tracepoint"));
1367 else if (b->type == bp_static_tracepoint
1368 || b->type == bp_static_marker_tracepoint)
1369 error (_("The 'while-stepping' command "
1370 "cannot be used for static tracepoint"));
1372 if (while_stepping)
1373 error (_("The 'while-stepping' command "
1374 "can be used only once"));
1375 else
1376 while_stepping = c;
1379 validate_actionline (c->line, b);
1381 if (while_stepping)
1383 struct command_line *c2;
1385 gdb_assert (while_stepping->body_list_1 == nullptr);
1386 c2 = while_stepping->body_list_0.get ();
1387 for (; c2; c2 = c2->next)
1389 if (c2->control_type == while_stepping_control)
1390 error (_("The 'while-stepping' command cannot be nested"));
1394 else
1396 check_no_tracepoint_commands (commands);
1400 /* Return a vector of all the static tracepoints set at ADDR. The
1401 caller is responsible for releasing the vector. */
1403 std::vector<breakpoint *>
1404 static_tracepoints_here (CORE_ADDR addr)
1406 std::vector<breakpoint *> found;
1408 for (breakpoint *b : all_breakpoints ())
1409 if (b->type == bp_static_tracepoint
1410 || b->type == bp_static_marker_tracepoint)
1412 for (bp_location *loc : b->locations ())
1413 if (loc->address == addr)
1414 found.push_back (b);
1417 return found;
1420 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1421 validate that only allowed commands are included. */
1423 void
1424 breakpoint_set_commands (struct breakpoint *b,
1425 counted_command_line &&commands)
1427 validate_commands_for_breakpoint (b, commands.get ());
1429 b->commands = std::move (commands);
1430 gdb::observers::breakpoint_modified.notify (b);
1433 /* Set the internal `silent' flag on the breakpoint. Note that this
1434 is not the same as the "silent" that may appear in the breakpoint's
1435 commands. */
1437 void
1438 breakpoint_set_silent (struct breakpoint *b, int silent)
1440 int old_silent = b->silent;
1442 b->silent = silent;
1443 if (old_silent != silent)
1444 gdb::observers::breakpoint_modified.notify (b);
1447 /* Set the thread for this breakpoint. If THREAD is -1, make the
1448 breakpoint work for any thread. */
1450 void
1451 breakpoint_set_thread (struct breakpoint *b, int thread)
1453 int old_thread = b->thread;
1455 b->thread = thread;
1456 if (old_thread != thread)
1457 gdb::observers::breakpoint_modified.notify (b);
1460 /* Set the task for this breakpoint. If TASK is 0, make the
1461 breakpoint work for any task. */
1463 void
1464 breakpoint_set_task (struct breakpoint *b, int task)
1466 int old_task = b->task;
1468 b->task = task;
1469 if (old_task != task)
1470 gdb::observers::breakpoint_modified.notify (b);
1473 static void
1474 commands_command_1 (const char *arg, int from_tty,
1475 struct command_line *control)
1477 counted_command_line cmd;
1478 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1479 NULL after the call to read_command_lines if the user provides an empty
1480 list of command by just typing "end". */
1481 bool cmd_read = false;
1483 std::string new_arg;
1485 if (arg == NULL || !*arg)
1487 /* Argument not explicitly given. Synthesize it. */
1488 if (breakpoint_count - prev_breakpoint_count > 1)
1489 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1490 breakpoint_count);
1491 else if (breakpoint_count > 0)
1492 new_arg = string_printf ("%d", breakpoint_count);
1494 else
1496 /* Create a copy of ARG. This is needed because the "commands"
1497 command may be coming from a script. In that case, the read
1498 line buffer is going to be overwritten in the lambda of
1499 'map_breakpoint_numbers' below when reading the next line
1500 before we are are done parsing the breakpoint numbers. */
1501 new_arg = arg;
1503 arg = new_arg.c_str ();
1505 map_breakpoint_numbers
1506 (arg, [&] (breakpoint *b)
1508 if (!cmd_read)
1510 gdb_assert (cmd == NULL);
1511 if (control != NULL)
1512 cmd = control->body_list_0;
1513 else
1515 std::string str
1516 = string_printf (_("Type commands for breakpoint(s) "
1517 "%s, one per line."),
1518 arg);
1520 auto do_validate = [=] (const char *line)
1522 validate_actionline (line, b);
1524 gdb::function_view<void (const char *)> validator;
1525 if (is_tracepoint (b))
1526 validator = do_validate;
1528 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1530 cmd_read = true;
1533 /* If a breakpoint was on the list more than once, we don't need to
1534 do anything. */
1535 if (b->commands != cmd)
1537 validate_commands_for_breakpoint (b, cmd.get ());
1538 b->commands = cmd;
1539 gdb::observers::breakpoint_modified.notify (b);
1544 static void
1545 commands_command (const char *arg, int from_tty)
1547 commands_command_1 (arg, from_tty, NULL);
1550 /* Like commands_command, but instead of reading the commands from
1551 input stream, takes them from an already parsed command structure.
1553 This is used by cli-script.c to DTRT with breakpoint commands
1554 that are part of if and while bodies. */
1555 enum command_control_type
1556 commands_from_control_command (const char *arg, struct command_line *cmd)
1558 commands_command_1 (arg, 0, cmd);
1559 return simple_control;
1562 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1564 static int
1565 bp_location_has_shadow (struct bp_location *bl)
1567 if (bl->loc_type != bp_loc_software_breakpoint)
1568 return 0;
1569 if (!bl->inserted)
1570 return 0;
1571 if (bl->target_info.shadow_len == 0)
1572 /* BL isn't valid, or doesn't shadow memory. */
1573 return 0;
1574 return 1;
1577 /* Update BUF, which is LEN bytes read from the target address
1578 MEMADDR, by replacing a memory breakpoint with its shadowed
1579 contents.
1581 If READBUF is not NULL, this buffer must not overlap with the of
1582 the breakpoint location's shadow_contents buffer. Otherwise, a
1583 failed assertion internal error will be raised. */
1585 static void
1586 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1587 const gdb_byte *writebuf_org,
1588 ULONGEST memaddr, LONGEST len,
1589 struct bp_target_info *target_info,
1590 struct gdbarch *gdbarch)
1592 /* Now do full processing of the found relevant range of elements. */
1593 CORE_ADDR bp_addr = 0;
1594 int bp_size = 0;
1595 int bptoffset = 0;
1597 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1598 current_program_space->aspace, 0))
1600 /* The breakpoint is inserted in a different address space. */
1601 return;
1604 /* Addresses and length of the part of the breakpoint that
1605 we need to copy. */
1606 bp_addr = target_info->placed_address;
1607 bp_size = target_info->shadow_len;
1609 if (bp_addr + bp_size <= memaddr)
1611 /* The breakpoint is entirely before the chunk of memory we are
1612 reading. */
1613 return;
1616 if (bp_addr >= memaddr + len)
1618 /* The breakpoint is entirely after the chunk of memory we are
1619 reading. */
1620 return;
1623 /* Offset within shadow_contents. */
1624 if (bp_addr < memaddr)
1626 /* Only copy the second part of the breakpoint. */
1627 bp_size -= memaddr - bp_addr;
1628 bptoffset = memaddr - bp_addr;
1629 bp_addr = memaddr;
1632 if (bp_addr + bp_size > memaddr + len)
1634 /* Only copy the first part of the breakpoint. */
1635 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1638 if (readbuf != NULL)
1640 /* Verify that the readbuf buffer does not overlap with the
1641 shadow_contents buffer. */
1642 gdb_assert (target_info->shadow_contents >= readbuf + len
1643 || readbuf >= (target_info->shadow_contents
1644 + target_info->shadow_len));
1646 /* Update the read buffer with this inserted breakpoint's
1647 shadow. */
1648 memcpy (readbuf + bp_addr - memaddr,
1649 target_info->shadow_contents + bptoffset, bp_size);
1651 else
1653 const unsigned char *bp;
1654 CORE_ADDR addr = target_info->reqstd_address;
1655 int placed_size;
1657 /* Update the shadow with what we want to write to memory. */
1658 memcpy (target_info->shadow_contents + bptoffset,
1659 writebuf_org + bp_addr - memaddr, bp_size);
1661 /* Determine appropriate breakpoint contents and size for this
1662 address. */
1663 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1665 /* Update the final write buffer with this inserted
1666 breakpoint's INSN. */
1667 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1671 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1672 by replacing any memory breakpoints with their shadowed contents.
1674 If READBUF is not NULL, this buffer must not overlap with any of
1675 the breakpoint location's shadow_contents buffers. Otherwise,
1676 a failed assertion internal error will be raised.
1678 The range of shadowed area by each bp_location is:
1679 bl->address - bp_locations_placed_address_before_address_max
1680 up to bl->address + bp_locations_shadow_len_after_address_max
1681 The range we were requested to resolve shadows for is:
1682 memaddr ... memaddr + len
1683 Thus the safe cutoff boundaries for performance optimization are
1684 memaddr + len <= (bl->address
1685 - bp_locations_placed_address_before_address_max)
1686 and:
1687 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1689 void
1690 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1691 const gdb_byte *writebuf_org,
1692 ULONGEST memaddr, LONGEST len)
1694 /* Left boundary, right boundary and median element of our binary
1695 search. */
1696 unsigned bc_l, bc_r, bc;
1698 /* Find BC_L which is a leftmost element which may affect BUF
1699 content. It is safe to report lower value but a failure to
1700 report higher one. */
1702 bc_l = 0;
1703 bc_r = bp_locations.size ();
1704 while (bc_l + 1 < bc_r)
1706 struct bp_location *bl;
1708 bc = (bc_l + bc_r) / 2;
1709 bl = bp_locations[bc];
1711 /* Check first BL->ADDRESS will not overflow due to the added
1712 constant. Then advance the left boundary only if we are sure
1713 the BC element can in no way affect the BUF content (MEMADDR
1714 to MEMADDR + LEN range).
1716 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1717 offset so that we cannot miss a breakpoint with its shadow
1718 range tail still reaching MEMADDR. */
1720 if ((bl->address + bp_locations_shadow_len_after_address_max
1721 >= bl->address)
1722 && (bl->address + bp_locations_shadow_len_after_address_max
1723 <= memaddr))
1724 bc_l = bc;
1725 else
1726 bc_r = bc;
1729 /* Due to the binary search above, we need to make sure we pick the
1730 first location that's at BC_L's address. E.g., if there are
1731 multiple locations at the same address, BC_L may end up pointing
1732 at a duplicate location, and miss the "master"/"inserted"
1733 location. Say, given locations L1, L2 and L3 at addresses A and
1736 L1@A, L2@A, L3@B, ...
1738 BC_L could end up pointing at location L2, while the "master"
1739 location could be L1. Since the `loc->inserted' flag is only set
1740 on "master" locations, we'd forget to restore the shadow of L1
1741 and L2. */
1742 while (bc_l > 0
1743 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1744 bc_l--;
1746 /* Now do full processing of the found relevant range of elements. */
1748 for (bc = bc_l; bc < bp_locations.size (); bc++)
1750 struct bp_location *bl = bp_locations[bc];
1752 /* bp_location array has BL->OWNER always non-NULL. */
1753 if (bl->owner->type == bp_none)
1754 warning (_("reading through apparently deleted breakpoint #%d?"),
1755 bl->owner->number);
1757 /* Performance optimization: any further element can no longer affect BUF
1758 content. */
1760 if (bl->address >= bp_locations_placed_address_before_address_max
1761 && (memaddr + len
1762 <= (bl->address
1763 - bp_locations_placed_address_before_address_max)))
1764 break;
1766 if (!bp_location_has_shadow (bl))
1767 continue;
1769 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1770 memaddr, len, &bl->target_info, bl->gdbarch);
1774 /* See breakpoint.h. */
1776 bool
1777 is_breakpoint (const struct breakpoint *bpt)
1779 return (bpt->type == bp_breakpoint
1780 || bpt->type == bp_hardware_breakpoint
1781 || bpt->type == bp_dprintf);
1784 /* Return true if BPT is of any hardware watchpoint kind. */
1786 static bool
1787 is_hardware_watchpoint (const struct breakpoint *bpt)
1789 return (bpt->type == bp_hardware_watchpoint
1790 || bpt->type == bp_read_watchpoint
1791 || bpt->type == bp_access_watchpoint);
1794 /* See breakpoint.h. */
1796 bool
1797 is_watchpoint (const struct breakpoint *bpt)
1799 return (is_hardware_watchpoint (bpt)
1800 || bpt->type == bp_watchpoint);
1803 /* Returns true if the current thread and its running state are safe
1804 to evaluate or update watchpoint B. Watchpoints on local
1805 expressions need to be evaluated in the context of the thread that
1806 was current when the watchpoint was created, and, that thread needs
1807 to be stopped to be able to select the correct frame context.
1808 Watchpoints on global expressions can be evaluated on any thread,
1809 and in any state. It is presently left to the target allowing
1810 memory accesses when threads are running. */
1812 static int
1813 watchpoint_in_thread_scope (struct watchpoint *b)
1815 return (b->pspace == current_program_space
1816 && (b->watchpoint_thread == null_ptid
1817 || (inferior_ptid == b->watchpoint_thread
1818 && !inferior_thread ()->executing ())));
1821 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1822 associated bp_watchpoint_scope breakpoint. */
1824 static void
1825 watchpoint_del_at_next_stop (struct watchpoint *w)
1827 if (w->related_breakpoint != w)
1829 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1830 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1831 w->related_breakpoint->disposition = disp_del_at_next_stop;
1832 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1833 w->related_breakpoint = w;
1835 w->disposition = disp_del_at_next_stop;
1838 /* Extract a bitfield value from value VAL using the bit parameters contained in
1839 watchpoint W. */
1841 static struct value *
1842 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1844 struct value *bit_val;
1846 if (val == NULL)
1847 return NULL;
1849 bit_val = allocate_value (value_type (val));
1851 unpack_value_bitfield (bit_val,
1852 w->val_bitpos,
1853 w->val_bitsize,
1854 value_contents_for_printing (val).data (),
1855 value_offset (val),
1856 val);
1858 return bit_val;
1861 /* Allocate a dummy location and add it to B. This is required
1862 because bpstat_stop_status requires a location to be able to report
1863 stops. */
1865 static void
1866 add_dummy_location (struct breakpoint *b,
1867 struct program_space *pspace)
1869 gdb_assert (b->loc == NULL);
1871 b->loc = new bp_location (b, bp_loc_other);
1872 b->loc->pspace = pspace;
1875 /* Assuming that B is a watchpoint:
1876 - Reparse watchpoint expression, if REPARSE is non-zero
1877 - Evaluate expression and store the result in B->val
1878 - Evaluate the condition if there is one, and store the result
1879 in b->loc->cond.
1880 - Update the list of values that must be watched in B->loc.
1882 If the watchpoint disposition is disp_del_at_next_stop, then do
1883 nothing. If this is local watchpoint that is out of scope, delete
1886 Even with `set breakpoint always-inserted on' the watchpoints are
1887 removed + inserted on each stop here. Normal breakpoints must
1888 never be removed because they might be missed by a running thread
1889 when debugging in non-stop mode. On the other hand, hardware
1890 watchpoints (is_hardware_watchpoint; processed here) are specific
1891 to each LWP since they are stored in each LWP's hardware debug
1892 registers. Therefore, such LWP must be stopped first in order to
1893 be able to modify its hardware watchpoints.
1895 Hardware watchpoints must be reset exactly once after being
1896 presented to the user. It cannot be done sooner, because it would
1897 reset the data used to present the watchpoint hit to the user. And
1898 it must not be done later because it could display the same single
1899 watchpoint hit during multiple GDB stops. Note that the latter is
1900 relevant only to the hardware watchpoint types bp_read_watchpoint
1901 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1902 not user-visible - its hit is suppressed if the memory content has
1903 not changed.
1905 The following constraints influence the location where we can reset
1906 hardware watchpoints:
1908 * target_stopped_by_watchpoint and target_stopped_data_address are
1909 called several times when GDB stops.
1911 [linux]
1912 * Multiple hardware watchpoints can be hit at the same time,
1913 causing GDB to stop. GDB only presents one hardware watchpoint
1914 hit at a time as the reason for stopping, and all the other hits
1915 are presented later, one after the other, each time the user
1916 requests the execution to be resumed. Execution is not resumed
1917 for the threads still having pending hit event stored in
1918 LWP_INFO->STATUS. While the watchpoint is already removed from
1919 the inferior on the first stop the thread hit event is kept being
1920 reported from its cached value by linux_nat_stopped_data_address
1921 until the real thread resume happens after the watchpoint gets
1922 presented and thus its LWP_INFO->STATUS gets reset.
1924 Therefore the hardware watchpoint hit can get safely reset on the
1925 watchpoint removal from inferior. */
1927 static void
1928 update_watchpoint (struct watchpoint *b, int reparse)
1930 int within_current_scope;
1931 struct frame_id saved_frame_id;
1932 int frame_saved;
1934 /* If this is a local watchpoint, we only want to check if the
1935 watchpoint frame is in scope if the current thread is the thread
1936 that was used to create the watchpoint. */
1937 if (!watchpoint_in_thread_scope (b))
1938 return;
1940 if (b->disposition == disp_del_at_next_stop)
1941 return;
1943 frame_saved = 0;
1945 /* Determine if the watchpoint is within scope. */
1946 if (b->exp_valid_block == NULL)
1947 within_current_scope = 1;
1948 else
1950 struct frame_info *fi = get_current_frame ();
1951 struct gdbarch *frame_arch = get_frame_arch (fi);
1952 CORE_ADDR frame_pc = get_frame_pc (fi);
1954 /* If we're at a point where the stack has been destroyed
1955 (e.g. in a function epilogue), unwinding may not work
1956 properly. Do not attempt to recreate locations at this
1957 point. See similar comments in watchpoint_check. */
1958 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1959 return;
1961 /* Save the current frame's ID so we can restore it after
1962 evaluating the watchpoint expression on its own frame. */
1963 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1964 took a frame parameter, so that we didn't have to change the
1965 selected frame. */
1966 frame_saved = 1;
1967 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1969 fi = frame_find_by_id (b->watchpoint_frame);
1970 within_current_scope = (fi != NULL);
1971 if (within_current_scope)
1972 select_frame (fi);
1975 /* We don't free locations. They are stored in the bp_location array
1976 and update_global_location_list will eventually delete them and
1977 remove breakpoints if needed. */
1978 b->loc = NULL;
1980 if (within_current_scope && reparse)
1982 const char *s;
1984 b->exp.reset ();
1985 s = (b->exp_string_reparse
1986 ? b->exp_string_reparse.get ()
1987 : b->exp_string.get ());
1988 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1989 /* If the meaning of expression itself changed, the old value is
1990 no longer relevant. We don't want to report a watchpoint hit
1991 to the user when the old value and the new value may actually
1992 be completely different objects. */
1993 b->val = NULL;
1994 b->val_valid = false;
1996 /* Note that unlike with breakpoints, the watchpoint's condition
1997 expression is stored in the breakpoint object, not in the
1998 locations (re)created below. */
1999 if (b->cond_string != NULL)
2001 b->cond_exp.reset ();
2003 s = b->cond_string.get ();
2004 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
2008 /* If we failed to parse the expression, for example because
2009 it refers to a global variable in a not-yet-loaded shared library,
2010 don't try to insert watchpoint. We don't automatically delete
2011 such watchpoint, though, since failure to parse expression
2012 is different from out-of-scope watchpoint. */
2013 if (!target_has_execution ())
2015 /* Without execution, memory can't change. No use to try and
2016 set watchpoint locations. The watchpoint will be reset when
2017 the target gains execution, through breakpoint_re_set. */
2018 if (!can_use_hw_watchpoints)
2020 if (b->works_in_software_mode ())
2021 b->type = bp_watchpoint;
2022 else
2023 error (_("Can't set read/access watchpoint when "
2024 "hardware watchpoints are disabled."));
2027 else if (within_current_scope && b->exp)
2029 std::vector<value_ref_ptr> val_chain;
2030 struct value *v, *result;
2031 struct program_space *frame_pspace;
2033 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
2034 &val_chain, false);
2036 /* Avoid setting b->val if it's already set. The meaning of
2037 b->val is 'the last value' user saw, and we should update
2038 it only if we reported that last value to user. As it
2039 happens, the code that reports it updates b->val directly.
2040 We don't keep track of the memory value for masked
2041 watchpoints. */
2042 if (!b->val_valid && !is_masked_watchpoint (b))
2044 if (b->val_bitsize != 0)
2045 v = extract_bitfield_from_watchpoint_value (b, v);
2046 b->val = release_value (v);
2047 b->val_valid = true;
2050 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2052 /* Look at each value on the value chain. */
2053 gdb_assert (!val_chain.empty ());
2054 for (const value_ref_ptr &iter : val_chain)
2056 v = iter.get ();
2058 /* If it's a memory location, and GDB actually needed
2059 its contents to evaluate the expression, then we
2060 must watch it. If the first value returned is
2061 still lazy, that means an error occurred reading it;
2062 watch it anyway in case it becomes readable. */
2063 if (VALUE_LVAL (v) == lval_memory
2064 && (v == val_chain[0] || ! value_lazy (v)))
2066 struct type *vtype = check_typedef (value_type (v));
2068 /* We only watch structs and arrays if user asked
2069 for it explicitly, never if they just happen to
2070 appear in the middle of some value chain. */
2071 if (v == result
2072 || (vtype->code () != TYPE_CODE_STRUCT
2073 && vtype->code () != TYPE_CODE_ARRAY))
2075 CORE_ADDR addr;
2076 enum target_hw_bp_type type;
2077 struct bp_location *loc, **tmp;
2078 int bitpos = 0, bitsize = 0;
2080 if (value_bitsize (v) != 0)
2082 /* Extract the bit parameters out from the bitfield
2083 sub-expression. */
2084 bitpos = value_bitpos (v);
2085 bitsize = value_bitsize (v);
2087 else if (v == result && b->val_bitsize != 0)
2089 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2090 lvalue whose bit parameters are saved in the fields
2091 VAL_BITPOS and VAL_BITSIZE. */
2092 bitpos = b->val_bitpos;
2093 bitsize = b->val_bitsize;
2096 addr = value_address (v);
2097 if (bitsize != 0)
2099 /* Skip the bytes that don't contain the bitfield. */
2100 addr += bitpos / 8;
2103 type = hw_write;
2104 if (b->type == bp_read_watchpoint)
2105 type = hw_read;
2106 else if (b->type == bp_access_watchpoint)
2107 type = hw_access;
2109 loc = b->allocate_location ();
2110 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2112 *tmp = loc;
2113 loc->gdbarch = value_type (v)->arch ();
2115 loc->pspace = frame_pspace;
2116 loc->address = address_significant (loc->gdbarch, addr);
2118 if (bitsize != 0)
2120 /* Just cover the bytes that make up the bitfield. */
2121 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2123 else
2124 loc->length = TYPE_LENGTH (value_type (v));
2126 loc->watchpoint_type = type;
2131 /* Change the type of breakpoint between hardware assisted or
2132 an ordinary watchpoint depending on the hardware support
2133 and free hardware slots. REPARSE is set when the inferior
2134 is started. */
2135 if (reparse)
2137 int reg_cnt;
2138 enum bp_loc_type loc_type;
2140 reg_cnt = can_use_hardware_watchpoint (val_chain);
2142 if (reg_cnt)
2144 int i, target_resources_ok, other_type_used;
2145 enum bptype type;
2147 /* Use an exact watchpoint when there's only one memory region to be
2148 watched, and only one debug register is needed to watch it. */
2149 b->exact = target_exact_watchpoints && reg_cnt == 1;
2151 /* We need to determine how many resources are already
2152 used for all other hardware watchpoints plus this one
2153 to see if we still have enough resources to also fit
2154 this watchpoint in as well. */
2156 /* If this is a software watchpoint, we try to turn it
2157 to a hardware one -- count resources as if B was of
2158 hardware watchpoint type. */
2159 type = b->type;
2160 if (type == bp_watchpoint)
2161 type = bp_hardware_watchpoint;
2163 /* This watchpoint may or may not have been placed on
2164 the list yet at this point (it won't be in the list
2165 if we're trying to create it for the first time,
2166 through watch_command), so always account for it
2167 manually. */
2169 /* Count resources used by all watchpoints except B. */
2170 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2172 /* Add in the resources needed for B. */
2173 i += hw_watchpoint_use_count (b);
2175 target_resources_ok
2176 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2177 if (target_resources_ok <= 0)
2179 bool sw_mode = b->works_in_software_mode ();
2181 if (target_resources_ok == 0 && !sw_mode)
2182 error (_("Target does not support this type of "
2183 "hardware watchpoint."));
2184 else if (target_resources_ok < 0 && !sw_mode)
2185 error (_("There are not enough available hardware "
2186 "resources for this watchpoint."));
2188 /* Downgrade to software watchpoint. */
2189 b->type = bp_watchpoint;
2191 else
2193 /* If this was a software watchpoint, we've just
2194 found we have enough resources to turn it to a
2195 hardware watchpoint. Otherwise, this is a
2196 nop. */
2197 b->type = type;
2200 else if (!b->works_in_software_mode ())
2202 if (!can_use_hw_watchpoints)
2203 error (_("Can't set read/access watchpoint when "
2204 "hardware watchpoints are disabled."));
2205 else
2206 error (_("Expression cannot be implemented with "
2207 "read/access watchpoint."));
2209 else
2210 b->type = bp_watchpoint;
2212 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
2213 : bp_loc_hardware_watchpoint);
2214 for (bp_location *bl : b->locations ())
2215 bl->loc_type = loc_type;
2218 /* If a software watchpoint is not watching any memory, then the
2219 above left it without any location set up. But,
2220 bpstat_stop_status requires a location to be able to report
2221 stops, so make sure there's at least a dummy one. */
2222 if (b->type == bp_watchpoint && b->loc == NULL)
2223 add_dummy_location (b, frame_pspace);
2225 else if (!within_current_scope)
2227 gdb_printf (_("\
2228 Watchpoint %d deleted because the program has left the block\n\
2229 in which its expression is valid.\n"),
2230 b->number);
2231 watchpoint_del_at_next_stop (b);
2234 /* Restore the selected frame. */
2235 if (frame_saved)
2236 select_frame (frame_find_by_id (saved_frame_id));
2240 /* Returns 1 iff breakpoint location should be
2241 inserted in the inferior. We don't differentiate the type of BL's owner
2242 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2243 breakpoint_ops is not defined, because in insert_bp_location,
2244 tracepoint's insert_location will not be called. */
2245 static int
2246 should_be_inserted (struct bp_location *bl)
2248 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2249 return 0;
2251 if (bl->owner->disposition == disp_del_at_next_stop)
2252 return 0;
2254 if (!bl->enabled || bl->disabled_by_cond
2255 || bl->shlib_disabled || bl->duplicate)
2256 return 0;
2258 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2259 return 0;
2261 /* This is set for example, when we're attached to the parent of a
2262 vfork, and have detached from the child. The child is running
2263 free, and we expect it to do an exec or exit, at which point the
2264 OS makes the parent schedulable again (and the target reports
2265 that the vfork is done). Until the child is done with the shared
2266 memory region, do not insert breakpoints in the parent, otherwise
2267 the child could still trip on the parent's breakpoints. Since
2268 the parent is blocked anyway, it won't miss any breakpoint. */
2269 if (bl->pspace->breakpoints_not_allowed)
2270 return 0;
2272 /* Don't insert a breakpoint if we're trying to step past its
2273 location, except if the breakpoint is a single-step breakpoint,
2274 and the breakpoint's thread is the thread which is stepping past
2275 a breakpoint. */
2276 if ((bl->loc_type == bp_loc_software_breakpoint
2277 || bl->loc_type == bp_loc_hardware_breakpoint)
2278 && stepping_past_instruction_at (bl->pspace->aspace,
2279 bl->address)
2280 /* The single-step breakpoint may be inserted at the location
2281 we're trying to step if the instruction branches to itself.
2282 However, the instruction won't be executed at all and it may
2283 break the semantics of the instruction, for example, the
2284 instruction is a conditional branch or updates some flags.
2285 We can't fix it unless GDB is able to emulate the instruction
2286 or switch to displaced stepping. */
2287 && !(bl->owner->type == bp_single_step
2288 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2290 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2291 paddress (bl->gdbarch, bl->address));
2292 return 0;
2295 /* Don't insert watchpoints if we're trying to step past the
2296 instruction that triggered one. */
2297 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2298 && stepping_past_nonsteppable_watchpoint ())
2300 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2301 "skipping watchpoint at %s:%d",
2302 paddress (bl->gdbarch, bl->address), bl->length);
2303 return 0;
2306 return 1;
2309 /* Same as should_be_inserted but does the check assuming
2310 that the location is not duplicated. */
2312 static int
2313 unduplicated_should_be_inserted (struct bp_location *bl)
2315 int result;
2316 const int save_duplicate = bl->duplicate;
2318 bl->duplicate = 0;
2319 result = should_be_inserted (bl);
2320 bl->duplicate = save_duplicate;
2321 return result;
2324 /* Parses a conditional described by an expression COND into an
2325 agent expression bytecode suitable for evaluation
2326 by the bytecode interpreter. Return NULL if there was
2327 any error during parsing. */
2329 static agent_expr_up
2330 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2332 if (cond == NULL)
2333 return NULL;
2335 agent_expr_up aexpr;
2337 /* We don't want to stop processing, so catch any errors
2338 that may show up. */
2341 aexpr = gen_eval_for_expr (scope, cond);
2344 catch (const gdb_exception_error &ex)
2346 /* If we got here, it means the condition could not be parsed to a valid
2347 bytecode expression and thus can't be evaluated on the target's side.
2348 It's no use iterating through the conditions. */
2351 /* We have a valid agent expression. */
2352 return aexpr;
2355 /* Based on location BL, create a list of breakpoint conditions to be
2356 passed on to the target. If we have duplicated locations with different
2357 conditions, we will add such conditions to the list. The idea is that the
2358 target will evaluate the list of conditions and will only notify GDB when
2359 one of them is true. */
2361 static void
2362 build_target_condition_list (struct bp_location *bl)
2364 int null_condition_or_parse_error = 0;
2365 int modified = bl->needs_update;
2367 /* Release conditions left over from a previous insert. */
2368 bl->target_info.conditions.clear ();
2370 /* This is only meaningful if the target is
2371 evaluating conditions and if the user has
2372 opted for condition evaluation on the target's
2373 side. */
2374 if (gdb_evaluates_breakpoint_condition_p ()
2375 || !target_supports_evaluation_of_breakpoint_conditions ())
2376 return;
2378 auto loc_range = all_bp_locations_at_addr (bl->address);
2380 /* Do a first pass to check for locations with no assigned
2381 conditions or conditions that fail to parse to a valid agent
2382 expression bytecode. If any of these happen, then it's no use to
2383 send conditions to the target since this location will always
2384 trigger and generate a response back to GDB. Note we consider
2385 all locations at the same address irrespective of type, i.e.,
2386 even if the locations aren't considered duplicates (e.g.,
2387 software breakpoint and hardware breakpoint at the same
2388 address). */
2389 for (bp_location *loc : loc_range)
2391 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2393 if (modified)
2395 /* Re-parse the conditions since something changed. In that
2396 case we already freed the condition bytecodes (see
2397 force_breakpoint_reinsertion). We just
2398 need to parse the condition to bytecodes again. */
2399 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2400 loc->cond.get ());
2403 /* If we have a NULL bytecode expression, it means something
2404 went wrong or we have a null condition expression. */
2405 if (!loc->cond_bytecode)
2407 null_condition_or_parse_error = 1;
2408 break;
2413 /* If any of these happened, it means we will have to evaluate the conditions
2414 for the location's address on gdb's side. It is no use keeping bytecodes
2415 for all the other duplicate locations, thus we free all of them here.
2417 This is so we have a finer control over which locations' conditions are
2418 being evaluated by GDB or the remote stub. */
2419 if (null_condition_or_parse_error)
2421 for (bp_location *loc : loc_range)
2423 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2425 /* Only go as far as the first NULL bytecode is
2426 located. */
2427 if (!loc->cond_bytecode)
2428 return;
2430 loc->cond_bytecode.reset ();
2435 /* No NULL conditions or failed bytecode generation. Build a
2436 condition list for this location's address. If we have software
2437 and hardware locations at the same address, they aren't
2438 considered duplicates, but we still marge all the conditions
2439 anyway, as it's simpler, and doesn't really make a practical
2440 difference. */
2441 for (bp_location *loc : loc_range)
2442 if (loc->cond
2443 && is_breakpoint (loc->owner)
2444 && loc->pspace->num == bl->pspace->num
2445 && loc->owner->enable_state == bp_enabled
2446 && loc->enabled
2447 && !loc->disabled_by_cond)
2449 /* Add the condition to the vector. This will be used later
2450 to send the conditions to the target. */
2451 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2454 return;
2457 /* Parses a command described by string CMD into an agent expression
2458 bytecode suitable for evaluation by the bytecode interpreter.
2459 Return NULL if there was any error during parsing. */
2461 static agent_expr_up
2462 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2464 const char *cmdrest;
2465 const char *format_start, *format_end;
2466 struct gdbarch *gdbarch = get_current_arch ();
2468 if (cmd == NULL)
2469 return NULL;
2471 cmdrest = cmd;
2473 if (*cmdrest == ',')
2474 ++cmdrest;
2475 cmdrest = skip_spaces (cmdrest);
2477 if (*cmdrest++ != '"')
2478 error (_("No format string following the location"));
2480 format_start = cmdrest;
2482 format_pieces fpieces (&cmdrest);
2484 format_end = cmdrest;
2486 if (*cmdrest++ != '"')
2487 error (_("Bad format string, non-terminated '\"'."));
2489 cmdrest = skip_spaces (cmdrest);
2491 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2492 error (_("Invalid argument syntax"));
2494 if (*cmdrest == ',')
2495 cmdrest++;
2496 cmdrest = skip_spaces (cmdrest);
2498 /* For each argument, make an expression. */
2500 std::vector<struct expression *> argvec;
2501 while (*cmdrest != '\0')
2503 const char *cmd1;
2505 cmd1 = cmdrest;
2506 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2507 argvec.push_back (expr.release ());
2508 cmdrest = cmd1;
2509 if (*cmdrest == ',')
2510 ++cmdrest;
2513 agent_expr_up aexpr;
2515 /* We don't want to stop processing, so catch any errors
2516 that may show up. */
2519 aexpr = gen_printf (scope, gdbarch, 0, 0,
2520 format_start, format_end - format_start,
2521 argvec.size (), argvec.data ());
2523 catch (const gdb_exception_error &ex)
2525 /* If we got here, it means the command could not be parsed to a valid
2526 bytecode expression and thus can't be evaluated on the target's side.
2527 It's no use iterating through the other commands. */
2530 /* We have a valid agent expression, return it. */
2531 return aexpr;
2534 /* Based on location BL, create a list of breakpoint commands to be
2535 passed on to the target. If we have duplicated locations with
2536 different commands, we will add any such to the list. */
2538 static void
2539 build_target_command_list (struct bp_location *bl)
2541 int null_command_or_parse_error = 0;
2542 int modified = bl->needs_update;
2544 /* Clear commands left over from a previous insert. */
2545 bl->target_info.tcommands.clear ();
2547 if (!target_can_run_breakpoint_commands ())
2548 return;
2550 /* For now, limit to agent-style dprintf breakpoints. */
2551 if (dprintf_style != dprintf_style_agent)
2552 return;
2554 auto loc_range = all_bp_locations_at_addr (bl->address);
2556 /* For now, if we have any location at the same address that isn't a
2557 dprintf, don't install the target-side commands, as that would
2558 make the breakpoint not be reported to the core, and we'd lose
2559 control. */
2560 for (bp_location *loc : loc_range)
2561 if (is_breakpoint (loc->owner)
2562 && loc->pspace->num == bl->pspace->num
2563 && loc->owner->type != bp_dprintf)
2564 return;
2566 /* Do a first pass to check for locations with no assigned
2567 conditions or conditions that fail to parse to a valid agent expression
2568 bytecode. If any of these happen, then it's no use to send conditions
2569 to the target since this location will always trigger and generate a
2570 response back to GDB. */
2571 for (bp_location *loc : loc_range)
2573 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2575 if (modified)
2577 /* Re-parse the commands since something changed. In that
2578 case we already freed the command bytecodes (see
2579 force_breakpoint_reinsertion). We just
2580 need to parse the command to bytecodes again. */
2581 loc->cmd_bytecode
2582 = parse_cmd_to_aexpr (bl->address,
2583 loc->owner->extra_string.get ());
2586 /* If we have a NULL bytecode expression, it means something
2587 went wrong or we have a null command expression. */
2588 if (!loc->cmd_bytecode)
2590 null_command_or_parse_error = 1;
2591 break;
2596 /* If anything failed, then we're not doing target-side commands,
2597 and so clean up. */
2598 if (null_command_or_parse_error)
2600 for (bp_location *loc : loc_range)
2601 if (is_breakpoint (loc->owner)
2602 && loc->pspace->num == bl->pspace->num)
2604 /* Only go as far as the first NULL bytecode is
2605 located. */
2606 if (loc->cmd_bytecode == NULL)
2607 return;
2609 loc->cmd_bytecode.reset ();
2613 /* No NULL commands or failed bytecode generation. Build a command
2614 list for all duplicate locations at this location's address.
2615 Note that here we must care for whether the breakpoint location
2616 types are considered duplicates, otherwise, say, if we have a
2617 software and hardware location at the same address, the target
2618 could end up running the commands twice. For the moment, we only
2619 support targets-side commands with dprintf, but it doesn't hurt
2620 to be pedantically correct in case that changes. */
2621 for (bp_location *loc : loc_range)
2622 if (breakpoint_locations_match (bl, loc)
2623 && loc->owner->extra_string
2624 && is_breakpoint (loc->owner)
2625 && loc->pspace->num == bl->pspace->num
2626 && loc->owner->enable_state == bp_enabled
2627 && loc->enabled
2628 && !loc->disabled_by_cond)
2630 /* Add the command to the vector. This will be used later
2631 to send the commands to the target. */
2632 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2635 bl->target_info.persist = 0;
2636 /* Maybe flag this location as persistent. */
2637 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2638 bl->target_info.persist = 1;
2641 /* Return the kind of breakpoint on address *ADDR. Get the kind
2642 of breakpoint according to ADDR except single-step breakpoint.
2643 Get the kind of single-step breakpoint according to the current
2644 registers state. */
2646 static int
2647 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2649 if (bl->owner->type == bp_single_step)
2651 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2652 struct regcache *regcache;
2654 regcache = get_thread_regcache (thr);
2656 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2657 regcache, addr);
2659 else
2660 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2663 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2664 location. Any error messages are printed to TMP_ERROR_STREAM; and
2665 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2666 Returns 0 for success, 1 if the bp_location type is not supported or
2667 -1 for failure.
2669 NOTE drow/2003-09-09: This routine could be broken down to an
2670 object-style method for each breakpoint or catchpoint type. */
2671 static int
2672 insert_bp_location (struct bp_location *bl,
2673 struct ui_file *tmp_error_stream,
2674 int *disabled_breaks,
2675 int *hw_breakpoint_error,
2676 int *hw_bp_error_explained_already)
2678 gdb_exception bp_excpt;
2680 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2681 return 0;
2683 /* Note we don't initialize bl->target_info, as that wipes out
2684 the breakpoint location's shadow_contents if the breakpoint
2685 is still inserted at that location. This in turn breaks
2686 target_read_memory which depends on these buffers when
2687 a memory read is requested at the breakpoint location:
2688 Once the target_info has been wiped, we fail to see that
2689 we have a breakpoint inserted at that address and thus
2690 read the breakpoint instead of returning the data saved in
2691 the breakpoint location's shadow contents. */
2692 bl->target_info.reqstd_address = bl->address;
2693 bl->target_info.placed_address_space = bl->pspace->aspace;
2694 bl->target_info.length = bl->length;
2696 /* When working with target-side conditions, we must pass all the conditions
2697 for the same breakpoint address down to the target since GDB will not
2698 insert those locations. With a list of breakpoint conditions, the target
2699 can decide when to stop and notify GDB. */
2701 if (is_breakpoint (bl->owner))
2703 build_target_condition_list (bl);
2704 build_target_command_list (bl);
2705 /* Reset the modification marker. */
2706 bl->needs_update = 0;
2709 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2710 set at a read-only address, then a breakpoint location will have
2711 been changed to hardware breakpoint before we get here. If it is
2712 "off" however, error out before actually trying to insert the
2713 breakpoint, with a nicer error message. */
2714 if (bl->loc_type == bp_loc_software_breakpoint
2715 && !automatic_hardware_breakpoints)
2717 mem_region *mr = lookup_mem_region (bl->address);
2719 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2721 gdb_printf (tmp_error_stream,
2722 _("Cannot insert breakpoint %d.\n"
2723 "Cannot set software breakpoint "
2724 "at read-only address %s\n"),
2725 bl->owner->number,
2726 paddress (bl->gdbarch, bl->address));
2727 return 1;
2731 if (bl->loc_type == bp_loc_software_breakpoint
2732 || bl->loc_type == bp_loc_hardware_breakpoint)
2734 /* First check to see if we have to handle an overlay. */
2735 if (overlay_debugging == ovly_off
2736 || bl->section == NULL
2737 || !(section_is_overlay (bl->section)))
2739 /* No overlay handling: just set the breakpoint. */
2742 int val;
2744 val = bl->owner->insert_location (bl);
2745 if (val)
2746 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2748 catch (gdb_exception &e)
2750 bp_excpt = std::move (e);
2753 else
2755 /* This breakpoint is in an overlay section.
2756 Shall we set a breakpoint at the LMA? */
2757 if (!overlay_events_enabled)
2759 /* Yes -- overlay event support is not active,
2760 so we must try to set a breakpoint at the LMA.
2761 This will not work for a hardware breakpoint. */
2762 if (bl->loc_type == bp_loc_hardware_breakpoint)
2763 warning (_("hardware breakpoint %d not supported in overlay!"),
2764 bl->owner->number);
2765 else
2767 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2768 bl->section);
2769 /* Set a software (trap) breakpoint at the LMA. */
2770 bl->overlay_target_info = bl->target_info;
2771 bl->overlay_target_info.reqstd_address = addr;
2773 /* No overlay handling: just set the breakpoint. */
2776 int val;
2778 bl->overlay_target_info.kind
2779 = breakpoint_kind (bl, &addr);
2780 bl->overlay_target_info.placed_address = addr;
2781 val = target_insert_breakpoint (bl->gdbarch,
2782 &bl->overlay_target_info);
2783 if (val)
2784 bp_excpt
2785 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2787 catch (gdb_exception &e)
2789 bp_excpt = std::move (e);
2792 if (bp_excpt.reason != 0)
2793 gdb_printf (tmp_error_stream,
2794 "Overlay breakpoint %d "
2795 "failed: in ROM?\n",
2796 bl->owner->number);
2799 /* Shall we set a breakpoint at the VMA? */
2800 if (section_is_mapped (bl->section))
2802 /* Yes. This overlay section is mapped into memory. */
2805 int val;
2807 val = bl->owner->insert_location (bl);
2808 if (val)
2809 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2811 catch (gdb_exception &e)
2813 bp_excpt = std::move (e);
2816 else
2818 /* No. This breakpoint will not be inserted.
2819 No error, but do not mark the bp as 'inserted'. */
2820 return 0;
2824 if (bp_excpt.reason != 0)
2826 /* Can't set the breakpoint. */
2828 /* If the target has closed then it will have deleted any
2829 breakpoints inserted within the target inferior, as a result
2830 any further attempts to interact with the breakpoint objects
2831 is not possible. Just rethrow the error. */
2832 if (bp_excpt.error == TARGET_CLOSE_ERROR)
2833 throw bp_excpt;
2834 gdb_assert (bl->owner != nullptr);
2836 /* In some cases, we might not be able to insert a
2837 breakpoint in a shared library that has already been
2838 removed, but we have not yet processed the shlib unload
2839 event. Unfortunately, some targets that implement
2840 breakpoint insertion themselves can't tell why the
2841 breakpoint insertion failed (e.g., the remote target
2842 doesn't define error codes), so we must treat generic
2843 errors as memory errors. */
2844 if (bp_excpt.reason == RETURN_ERROR
2845 && (bp_excpt.error == GENERIC_ERROR
2846 || bp_excpt.error == MEMORY_ERROR)
2847 && bl->loc_type == bp_loc_software_breakpoint
2848 && (solib_name_from_address (bl->pspace, bl->address)
2849 || shared_objfile_contains_address_p (bl->pspace,
2850 bl->address)))
2852 /* See also: disable_breakpoints_in_shlibs. */
2853 bl->shlib_disabled = 1;
2854 gdb::observers::breakpoint_modified.notify (bl->owner);
2855 if (!*disabled_breaks)
2857 gdb_printf (tmp_error_stream,
2858 "Cannot insert breakpoint %d.\n",
2859 bl->owner->number);
2860 gdb_printf (tmp_error_stream,
2861 "Temporarily disabling shared "
2862 "library breakpoints:\n");
2864 *disabled_breaks = 1;
2865 gdb_printf (tmp_error_stream,
2866 "breakpoint #%d\n", bl->owner->number);
2867 return 0;
2869 else
2871 if (bl->loc_type == bp_loc_hardware_breakpoint)
2873 *hw_breakpoint_error = 1;
2874 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2875 gdb_printf (tmp_error_stream,
2876 "Cannot insert hardware breakpoint %d%s",
2877 bl->owner->number,
2878 bp_excpt.message ? ":" : ".\n");
2879 if (bp_excpt.message != NULL)
2880 gdb_printf (tmp_error_stream, "%s.\n",
2881 bp_excpt.what ());
2883 else
2885 if (bp_excpt.message == NULL)
2887 std::string message
2888 = memory_error_message (TARGET_XFER_E_IO,
2889 bl->gdbarch, bl->address);
2891 gdb_printf (tmp_error_stream,
2892 "Cannot insert breakpoint %d.\n"
2893 "%s\n",
2894 bl->owner->number, message.c_str ());
2896 else
2898 gdb_printf (tmp_error_stream,
2899 "Cannot insert breakpoint %d: %s\n",
2900 bl->owner->number,
2901 bp_excpt.what ());
2904 return 1;
2908 else
2909 bl->inserted = 1;
2911 return 0;
2914 else if (bl->loc_type == bp_loc_hardware_watchpoint
2915 /* NOTE drow/2003-09-08: This state only exists for removing
2916 watchpoints. It's not clear that it's necessary... */
2917 && bl->owner->disposition != disp_del_at_next_stop)
2919 int val;
2921 val = bl->owner->insert_location (bl);
2923 /* If trying to set a read-watchpoint, and it turns out it's not
2924 supported, try emulating one with an access watchpoint. */
2925 if (val == 1 && bl->watchpoint_type == hw_read)
2927 /* But don't try to insert it, if there's already another
2928 hw_access location that would be considered a duplicate
2929 of this one. */
2930 for (bp_location *loc : all_bp_locations ())
2931 if (loc != bl
2932 && loc->watchpoint_type == hw_access
2933 && watchpoint_locations_match (bl, loc))
2935 bl->duplicate = 1;
2936 bl->inserted = 1;
2937 bl->target_info = loc->target_info;
2938 bl->watchpoint_type = hw_access;
2939 val = 0;
2940 break;
2943 if (val == 1)
2945 bl->watchpoint_type = hw_access;
2946 val = bl->owner->insert_location (bl);
2948 if (val)
2949 /* Back to the original value. */
2950 bl->watchpoint_type = hw_read;
2954 bl->inserted = (val == 0);
2957 else if (bl->owner->type == bp_catchpoint)
2959 int val;
2961 val = bl->owner->insert_location (bl);
2962 if (val)
2964 bl->owner->enable_state = bp_disabled;
2966 if (val == 1)
2967 warning (_("\
2968 Error inserting catchpoint %d: Your system does not support this type\n\
2969 of catchpoint."), bl->owner->number);
2970 else
2971 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2974 bl->inserted = (val == 0);
2976 /* We've already printed an error message if there was a problem
2977 inserting this catchpoint, and we've disabled the catchpoint,
2978 so just return success. */
2979 return 0;
2982 return 0;
2985 /* This function is called when program space PSPACE is about to be
2986 deleted. It takes care of updating breakpoints to not reference
2987 PSPACE anymore. */
2989 void
2990 breakpoint_program_space_exit (struct program_space *pspace)
2992 /* Remove any breakpoint that was set through this program space. */
2993 for (breakpoint *b : all_breakpoints_safe ())
2994 if (b->pspace == pspace)
2995 delete_breakpoint (b);
2997 /* Breakpoints set through other program spaces could have locations
2998 bound to PSPACE as well. Remove those. */
2999 for (bp_location *loc : all_bp_locations ())
3001 struct bp_location *tmp;
3003 if (loc->pspace == pspace)
3005 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3006 if (loc->owner->loc == loc)
3007 loc->owner->loc = loc->next;
3008 else
3009 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3010 if (tmp->next == loc)
3012 tmp->next = loc->next;
3013 break;
3018 /* Now update the global location list to permanently delete the
3019 removed locations above. */
3020 update_global_location_list (UGLL_DONT_INSERT);
3023 /* Make sure all breakpoints are inserted in inferior.
3024 Throws exception on any error.
3025 A breakpoint that is already inserted won't be inserted
3026 again, so calling this function twice is safe. */
3027 void
3028 insert_breakpoints (void)
3030 for (breakpoint *bpt : all_breakpoints ())
3031 if (is_hardware_watchpoint (bpt))
3033 struct watchpoint *w = (struct watchpoint *) bpt;
3035 update_watchpoint (w, 0 /* don't reparse. */);
3038 /* Updating watchpoints creates new locations, so update the global
3039 location list. Explicitly tell ugll to insert locations and
3040 ignore breakpoints_always_inserted_mode. Also,
3041 update_global_location_list tries to "upgrade" software
3042 breakpoints to hardware breakpoints to handle "set breakpoint
3043 auto-hw", so we need to call it even if we don't have new
3044 locations. */
3045 update_global_location_list (UGLL_INSERT);
3048 /* This is used when we need to synch breakpoint conditions between GDB and the
3049 target. It is the case with deleting and disabling of breakpoints when using
3050 always-inserted mode. */
3052 static void
3053 update_inserted_breakpoint_locations (void)
3055 int error_flag = 0;
3056 int val = 0;
3057 int disabled_breaks = 0;
3058 int hw_breakpoint_error = 0;
3059 int hw_bp_details_reported = 0;
3061 string_file tmp_error_stream;
3063 /* Explicitly mark the warning -- this will only be printed if
3064 there was an error. */
3065 tmp_error_stream.puts ("Warning:\n");
3067 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3069 for (bp_location *bl : all_bp_locations ())
3071 /* We only want to update software breakpoints and hardware
3072 breakpoints. */
3073 if (!is_breakpoint (bl->owner))
3074 continue;
3076 /* We only want to update locations that are already inserted
3077 and need updating. This is to avoid unwanted insertion during
3078 deletion of breakpoints. */
3079 if (!bl->inserted || !bl->needs_update)
3080 continue;
3082 switch_to_program_space_and_thread (bl->pspace);
3084 /* For targets that support global breakpoints, there's no need
3085 to select an inferior to insert breakpoint to. In fact, even
3086 if we aren't attached to any process yet, we should still
3087 insert breakpoints. */
3088 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3089 && (inferior_ptid == null_ptid || !target_has_execution ()))
3090 continue;
3092 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3093 &hw_breakpoint_error, &hw_bp_details_reported);
3094 if (val)
3095 error_flag = val;
3098 if (error_flag)
3100 target_terminal::ours_for_output ();
3101 error_stream (tmp_error_stream);
3105 /* Used when starting or continuing the program. */
3107 static void
3108 insert_breakpoint_locations (void)
3110 int error_flag = 0;
3111 int val = 0;
3112 int disabled_breaks = 0;
3113 int hw_breakpoint_error = 0;
3114 int hw_bp_error_explained_already = 0;
3116 string_file tmp_error_stream;
3118 /* Explicitly mark the warning -- this will only be printed if
3119 there was an error. */
3120 tmp_error_stream.puts ("Warning:\n");
3122 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3124 for (bp_location *bl : all_bp_locations ())
3126 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3127 continue;
3129 /* There is no point inserting thread-specific breakpoints if
3130 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3131 has BL->OWNER always non-NULL. */
3132 if (bl->owner->thread != -1
3133 && !valid_global_thread_id (bl->owner->thread))
3134 continue;
3136 switch_to_program_space_and_thread (bl->pspace);
3138 /* For targets that support global breakpoints, there's no need
3139 to select an inferior to insert breakpoint to. In fact, even
3140 if we aren't attached to any process yet, we should still
3141 insert breakpoints. */
3142 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3143 && (inferior_ptid == null_ptid || !target_has_execution ()))
3144 continue;
3146 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3147 &hw_breakpoint_error, &hw_bp_error_explained_already);
3148 if (val)
3149 error_flag = val;
3152 /* If we failed to insert all locations of a watchpoint, remove
3153 them, as half-inserted watchpoint is of limited use. */
3154 for (breakpoint *bpt : all_breakpoints ())
3156 int some_failed = 0;
3158 if (!is_hardware_watchpoint (bpt))
3159 continue;
3161 if (!breakpoint_enabled (bpt))
3162 continue;
3164 if (bpt->disposition == disp_del_at_next_stop)
3165 continue;
3167 for (bp_location *loc : bpt->locations ())
3168 if (!loc->inserted && should_be_inserted (loc))
3170 some_failed = 1;
3171 break;
3174 if (some_failed)
3176 for (bp_location *loc : bpt->locations ())
3177 if (loc->inserted)
3178 remove_breakpoint (loc);
3180 hw_breakpoint_error = 1;
3181 tmp_error_stream.printf ("Could not insert "
3182 "hardware watchpoint %d.\n",
3183 bpt->number);
3184 error_flag = -1;
3188 if (error_flag)
3190 /* If a hardware breakpoint or watchpoint was inserted, add a
3191 message about possibly exhausted resources. */
3192 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3194 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3195 You may have requested too many hardware breakpoints/watchpoints.\n");
3197 target_terminal::ours_for_output ();
3198 error_stream (tmp_error_stream);
3202 /* Used when the program stops.
3203 Returns zero if successful, or non-zero if there was a problem
3204 removing a breakpoint location. */
3207 remove_breakpoints (void)
3209 int val = 0;
3211 for (bp_location *bl : all_bp_locations ())
3212 if (bl->inserted && !is_tracepoint (bl->owner))
3213 val |= remove_breakpoint (bl);
3215 return val;
3218 /* When a thread exits, remove breakpoints that are related to
3219 that thread. */
3221 static void
3222 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3224 for (breakpoint *b : all_breakpoints_safe ())
3226 if (b->thread == tp->global_num && user_breakpoint_p (b))
3228 b->disposition = disp_del_at_next_stop;
3230 gdb_printf (_("\
3231 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3232 b->number, print_thread_id (tp));
3234 /* Hide it from the user. */
3235 b->number = 0;
3240 /* See breakpoint.h. */
3242 void
3243 remove_breakpoints_inf (inferior *inf)
3245 int val;
3247 for (bp_location *bl : all_bp_locations ())
3249 if (bl->pspace != inf->pspace)
3250 continue;
3252 if (bl->inserted && !bl->target_info.persist)
3254 val = remove_breakpoint (bl);
3255 if (val != 0)
3256 return;
3261 static int internal_breakpoint_number = -1;
3263 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3264 If INTERNAL is non-zero, the breakpoint number will be populated
3265 from internal_breakpoint_number and that variable decremented.
3266 Otherwise the breakpoint number will be populated from
3267 breakpoint_count and that value incremented. Internal breakpoints
3268 do not set the internal var bpnum. */
3269 static void
3270 set_breakpoint_number (int internal, struct breakpoint *b)
3272 if (internal)
3273 b->number = internal_breakpoint_number--;
3274 else
3276 set_breakpoint_count (breakpoint_count + 1);
3277 b->number = breakpoint_count;
3281 static struct breakpoint *
3282 create_internal_breakpoint (struct gdbarch *gdbarch,
3283 CORE_ADDR address, enum bptype type)
3285 std::unique_ptr<internal_breakpoint> b
3286 (new internal_breakpoint (gdbarch, type, address));
3288 b->number = internal_breakpoint_number--;
3290 return add_to_breakpoint_chain (std::move (b));
3293 static const char *const longjmp_names[] =
3295 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3297 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3299 /* Per-objfile data private to breakpoint.c. */
3300 struct breakpoint_objfile_data
3302 /* Minimal symbol for "_ovly_debug_event" (if any). */
3303 struct bound_minimal_symbol overlay_msym;
3305 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3306 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3308 /* True if we have looked for longjmp probes. */
3309 int longjmp_searched = 0;
3311 /* SystemTap probe points for longjmp (if any). These are non-owning
3312 references. */
3313 std::vector<probe *> longjmp_probes;
3315 /* Minimal symbol for "std::terminate()" (if any). */
3316 struct bound_minimal_symbol terminate_msym;
3318 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3319 struct bound_minimal_symbol exception_msym;
3321 /* True if we have looked for exception probes. */
3322 int exception_searched = 0;
3324 /* SystemTap probe points for unwinding (if any). These are non-owning
3325 references. */
3326 std::vector<probe *> exception_probes;
3329 static const struct objfile_key<breakpoint_objfile_data>
3330 breakpoint_objfile_key;
3332 /* Minimal symbol not found sentinel. */
3333 static struct minimal_symbol msym_not_found;
3335 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3337 static int
3338 msym_not_found_p (const struct minimal_symbol *msym)
3340 return msym == &msym_not_found;
3343 /* Return per-objfile data needed by breakpoint.c.
3344 Allocate the data if necessary. */
3346 static struct breakpoint_objfile_data *
3347 get_breakpoint_objfile_data (struct objfile *objfile)
3349 struct breakpoint_objfile_data *bp_objfile_data;
3351 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3352 if (bp_objfile_data == NULL)
3353 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3354 return bp_objfile_data;
3357 static void
3358 create_overlay_event_breakpoint (void)
3360 const char *const func_name = "_ovly_debug_event";
3362 for (objfile *objfile : current_program_space->objfiles ())
3364 struct breakpoint *b;
3365 struct breakpoint_objfile_data *bp_objfile_data;
3366 CORE_ADDR addr;
3367 struct explicit_location explicit_loc;
3369 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3371 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3372 continue;
3374 if (bp_objfile_data->overlay_msym.minsym == NULL)
3376 struct bound_minimal_symbol m;
3378 m = lookup_minimal_symbol_text (func_name, objfile);
3379 if (m.minsym == NULL)
3381 /* Avoid future lookups in this objfile. */
3382 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3383 continue;
3385 bp_objfile_data->overlay_msym = m;
3388 addr = bp_objfile_data->overlay_msym.value_address ();
3389 b = create_internal_breakpoint (objfile->arch (), addr,
3390 bp_overlay_event);
3391 initialize_explicit_location (&explicit_loc);
3392 explicit_loc.function_name = ASTRDUP (func_name);
3393 b->location = new_explicit_location (&explicit_loc);
3395 if (overlay_debugging == ovly_auto)
3397 b->enable_state = bp_enabled;
3398 overlay_events_enabled = 1;
3400 else
3402 b->enable_state = bp_disabled;
3403 overlay_events_enabled = 0;
3408 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3409 true if a breakpoint was installed. */
3411 static bool
3412 create_longjmp_master_breakpoint_probe (objfile *objfile)
3414 struct gdbarch *gdbarch = objfile->arch ();
3415 struct breakpoint_objfile_data *bp_objfile_data
3416 = get_breakpoint_objfile_data (objfile);
3418 if (!bp_objfile_data->longjmp_searched)
3420 std::vector<probe *> ret
3421 = find_probes_in_objfile (objfile, "libc", "longjmp");
3423 if (!ret.empty ())
3425 /* We are only interested in checking one element. */
3426 probe *p = ret[0];
3428 if (!p->can_evaluate_arguments ())
3430 /* We cannot use the probe interface here,
3431 because it does not know how to evaluate
3432 arguments. */
3433 ret.clear ();
3436 bp_objfile_data->longjmp_probes = ret;
3437 bp_objfile_data->longjmp_searched = 1;
3440 if (bp_objfile_data->longjmp_probes.empty ())
3441 return false;
3443 for (probe *p : bp_objfile_data->longjmp_probes)
3445 struct breakpoint *b;
3447 b = create_internal_breakpoint (gdbarch,
3448 p->get_relocated_address (objfile),
3449 bp_longjmp_master);
3450 b->location = new_probe_location ("-probe-stap libc:longjmp");
3451 b->enable_state = bp_disabled;
3454 return true;
3457 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3458 Return true if at least one breakpoint was installed. */
3460 static bool
3461 create_longjmp_master_breakpoint_names (objfile *objfile)
3463 struct gdbarch *gdbarch = objfile->arch ();
3464 if (!gdbarch_get_longjmp_target_p (gdbarch))
3465 return false;
3467 struct breakpoint_objfile_data *bp_objfile_data
3468 = get_breakpoint_objfile_data (objfile);
3469 unsigned int installed_bp = 0;
3471 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3473 struct breakpoint *b;
3474 const char *func_name;
3475 CORE_ADDR addr;
3476 struct explicit_location explicit_loc;
3478 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3479 continue;
3481 func_name = longjmp_names[i];
3482 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3484 struct bound_minimal_symbol m;
3486 m = lookup_minimal_symbol_text (func_name, objfile);
3487 if (m.minsym == NULL)
3489 /* Prevent future lookups in this objfile. */
3490 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3491 continue;
3493 bp_objfile_data->longjmp_msym[i] = m;
3496 addr = bp_objfile_data->longjmp_msym[i].value_address ();
3497 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
3498 initialize_explicit_location (&explicit_loc);
3499 explicit_loc.function_name = ASTRDUP (func_name);
3500 b->location = new_explicit_location (&explicit_loc);
3501 b->enable_state = bp_disabled;
3502 installed_bp++;
3505 return installed_bp > 0;
3508 /* Create a master longjmp breakpoint. */
3510 static void
3511 create_longjmp_master_breakpoint (void)
3513 scoped_restore_current_program_space restore_pspace;
3515 for (struct program_space *pspace : program_spaces)
3517 set_current_program_space (pspace);
3519 for (objfile *obj : current_program_space->objfiles ())
3521 /* Skip separate debug object, it's handled in the loop below. */
3522 if (obj->separate_debug_objfile_backlink != nullptr)
3523 continue;
3525 /* Try a probe kind breakpoint on main objfile. */
3526 if (create_longjmp_master_breakpoint_probe (obj))
3527 continue;
3529 /* Try longjmp_names kind breakpoints on main and separate_debug
3530 objfiles. */
3531 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3532 if (create_longjmp_master_breakpoint_names (debug_objfile))
3533 break;
3538 /* Create a master std::terminate breakpoint. */
3539 static void
3540 create_std_terminate_master_breakpoint (void)
3542 const char *const func_name = "std::terminate()";
3544 scoped_restore_current_program_space restore_pspace;
3546 for (struct program_space *pspace : program_spaces)
3548 CORE_ADDR addr;
3550 set_current_program_space (pspace);
3552 for (objfile *objfile : current_program_space->objfiles ())
3554 struct breakpoint *b;
3555 struct breakpoint_objfile_data *bp_objfile_data;
3556 struct explicit_location explicit_loc;
3558 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3560 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3561 continue;
3563 if (bp_objfile_data->terminate_msym.minsym == NULL)
3565 struct bound_minimal_symbol m;
3567 m = lookup_minimal_symbol (func_name, NULL, objfile);
3568 if (m.minsym == NULL || (m.minsym->type () != mst_text
3569 && m.minsym->type () != mst_file_text))
3571 /* Prevent future lookups in this objfile. */
3572 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3573 continue;
3575 bp_objfile_data->terminate_msym = m;
3578 addr = bp_objfile_data->terminate_msym.value_address ();
3579 b = create_internal_breakpoint (objfile->arch (), addr,
3580 bp_std_terminate_master);
3581 initialize_explicit_location (&explicit_loc);
3582 explicit_loc.function_name = ASTRDUP (func_name);
3583 b->location = new_explicit_location (&explicit_loc);
3584 b->enable_state = bp_disabled;
3589 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3590 probe. Return true if a breakpoint was installed. */
3592 static bool
3593 create_exception_master_breakpoint_probe (objfile *objfile)
3595 struct breakpoint *b;
3596 struct gdbarch *gdbarch;
3597 struct breakpoint_objfile_data *bp_objfile_data;
3599 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3601 /* We prefer the SystemTap probe point if it exists. */
3602 if (!bp_objfile_data->exception_searched)
3604 std::vector<probe *> ret
3605 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3607 if (!ret.empty ())
3609 /* We are only interested in checking one element. */
3610 probe *p = ret[0];
3612 if (!p->can_evaluate_arguments ())
3614 /* We cannot use the probe interface here, because it does
3615 not know how to evaluate arguments. */
3616 ret.clear ();
3619 bp_objfile_data->exception_probes = ret;
3620 bp_objfile_data->exception_searched = 1;
3623 if (bp_objfile_data->exception_probes.empty ())
3624 return false;
3626 gdbarch = objfile->arch ();
3628 for (probe *p : bp_objfile_data->exception_probes)
3630 b = create_internal_breakpoint (gdbarch,
3631 p->get_relocated_address (objfile),
3632 bp_exception_master);
3633 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3634 b->enable_state = bp_disabled;
3637 return true;
3640 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3641 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3643 static bool
3644 create_exception_master_breakpoint_hook (objfile *objfile)
3646 const char *const func_name = "_Unwind_DebugHook";
3647 struct breakpoint *b;
3648 struct gdbarch *gdbarch;
3649 struct breakpoint_objfile_data *bp_objfile_data;
3650 CORE_ADDR addr;
3651 struct explicit_location explicit_loc;
3653 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3655 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3656 return false;
3658 gdbarch = objfile->arch ();
3660 if (bp_objfile_data->exception_msym.minsym == NULL)
3662 struct bound_minimal_symbol debug_hook;
3664 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3665 if (debug_hook.minsym == NULL)
3667 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3668 return false;
3671 bp_objfile_data->exception_msym = debug_hook;
3674 addr = bp_objfile_data->exception_msym.value_address ();
3675 addr = gdbarch_convert_from_func_ptr_addr
3676 (gdbarch, addr, current_inferior ()->top_target ());
3677 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
3678 initialize_explicit_location (&explicit_loc);
3679 explicit_loc.function_name = ASTRDUP (func_name);
3680 b->location = new_explicit_location (&explicit_loc);
3681 b->enable_state = bp_disabled;
3683 return true;
3686 /* Install a master breakpoint on the unwinder's debug hook. */
3688 static void
3689 create_exception_master_breakpoint (void)
3691 for (objfile *obj : current_program_space->objfiles ())
3693 /* Skip separate debug object. */
3694 if (obj->separate_debug_objfile_backlink)
3695 continue;
3697 /* Try a probe kind breakpoint. */
3698 if (create_exception_master_breakpoint_probe (obj))
3699 continue;
3701 /* Iterate over main and separate debug objects and try an
3702 _Unwind_DebugHook kind breakpoint. */
3703 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3704 if (create_exception_master_breakpoint_hook (debug_objfile))
3705 break;
3709 /* Does B have a location spec? */
3711 static int
3712 breakpoint_event_location_empty_p (const struct breakpoint *b)
3714 return b->location != NULL && event_location_empty_p (b->location.get ());
3717 void
3718 update_breakpoints_after_exec (void)
3720 /* We're about to delete breakpoints from GDB's lists. If the
3721 INSERTED flag is true, GDB will try to lift the breakpoints by
3722 writing the breakpoints' "shadow contents" back into memory. The
3723 "shadow contents" are NOT valid after an exec, so GDB should not
3724 do that. Instead, the target is responsible from marking
3725 breakpoints out as soon as it detects an exec. We don't do that
3726 here instead, because there may be other attempts to delete
3727 breakpoints after detecting an exec and before reaching here. */
3728 for (bp_location *bploc : all_bp_locations ())
3729 if (bploc->pspace == current_program_space)
3730 gdb_assert (!bploc->inserted);
3732 for (breakpoint *b : all_breakpoints_safe ())
3734 if (b->pspace != current_program_space)
3735 continue;
3737 /* Solib breakpoints must be explicitly reset after an exec(). */
3738 if (b->type == bp_shlib_event)
3740 delete_breakpoint (b);
3741 continue;
3744 /* JIT breakpoints must be explicitly reset after an exec(). */
3745 if (b->type == bp_jit_event)
3747 delete_breakpoint (b);
3748 continue;
3751 /* Thread event breakpoints must be set anew after an exec(),
3752 as must overlay event and longjmp master breakpoints. */
3753 if (b->type == bp_thread_event || b->type == bp_overlay_event
3754 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3755 || b->type == bp_exception_master)
3757 delete_breakpoint (b);
3758 continue;
3761 /* Step-resume breakpoints are meaningless after an exec(). */
3762 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3764 delete_breakpoint (b);
3765 continue;
3768 /* Just like single-step breakpoints. */
3769 if (b->type == bp_single_step)
3771 delete_breakpoint (b);
3772 continue;
3775 /* Longjmp and longjmp-resume breakpoints are also meaningless
3776 after an exec. */
3777 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3778 || b->type == bp_longjmp_call_dummy
3779 || b->type == bp_exception || b->type == bp_exception_resume)
3781 delete_breakpoint (b);
3782 continue;
3785 if (b->type == bp_catchpoint)
3787 /* For now, none of the bp_catchpoint breakpoints need to
3788 do anything at this point. In the future, if some of
3789 the catchpoints need to something, we will need to add
3790 a new method, and call this method from here. */
3791 continue;
3794 /* bp_finish is a special case. The only way we ought to be able
3795 to see one of these when an exec() has happened, is if the user
3796 caught a vfork, and then said "finish". Ordinarily a finish just
3797 carries them to the call-site of the current callee, by setting
3798 a temporary bp there and resuming. But in this case, the finish
3799 will carry them entirely through the vfork & exec.
3801 We don't want to allow a bp_finish to remain inserted now. But
3802 we can't safely delete it, 'cause finish_command has a handle to
3803 the bp on a bpstat, and will later want to delete it. There's a
3804 chance (and I've seen it happen) that if we delete the bp_finish
3805 here, that its storage will get reused by the time finish_command
3806 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3807 We really must allow finish_command to delete a bp_finish.
3809 In the absence of a general solution for the "how do we know
3810 it's safe to delete something others may have handles to?"
3811 problem, what we'll do here is just uninsert the bp_finish, and
3812 let finish_command delete it.
3814 (We know the bp_finish is "doomed" in the sense that it's
3815 momentary, and will be deleted as soon as finish_command sees
3816 the inferior stopped. So it doesn't matter that the bp's
3817 address is probably bogus in the new a.out, unlike e.g., the
3818 solib breakpoints.) */
3820 if (b->type == bp_finish)
3822 continue;
3825 /* Without a symbolic address, we have little hope of the
3826 pre-exec() address meaning the same thing in the post-exec()
3827 a.out. */
3828 if (breakpoint_event_location_empty_p (b))
3830 delete_breakpoint (b);
3831 continue;
3837 detach_breakpoints (ptid_t ptid)
3839 int val = 0;
3840 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3841 struct inferior *inf = current_inferior ();
3843 if (ptid.pid () == inferior_ptid.pid ())
3844 error (_("Cannot detach breakpoints of inferior_ptid"));
3846 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3847 inferior_ptid = ptid;
3848 for (bp_location *bl : all_bp_locations ())
3850 if (bl->pspace != inf->pspace)
3851 continue;
3853 /* This function must physically remove breakpoints locations
3854 from the specified ptid, without modifying the breakpoint
3855 package's state. Locations of type bp_loc_other and
3856 bp_loc_software_watchpoint are only maintained at GDB side,
3857 so there is no need to remove them. Moreover, removing these
3858 would modify the breakpoint package's state. */
3859 if (bl->loc_type == bp_loc_other
3860 || bl->loc_type == bp_loc_software_watchpoint)
3861 continue;
3863 if (bl->inserted)
3864 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3867 return val;
3870 /* Remove the breakpoint location BL from the current address space.
3871 Note that this is used to detach breakpoints from a child fork.
3872 When we get here, the child isn't in the inferior list, and neither
3873 do we have objects to represent its address space --- we should
3874 *not* look at bl->pspace->aspace here. */
3876 static int
3877 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3879 int val;
3881 /* BL is never in moribund_locations by our callers. */
3882 gdb_assert (bl->owner != NULL);
3884 /* The type of none suggests that owner is actually deleted.
3885 This should not ever happen. */
3886 gdb_assert (bl->owner->type != bp_none);
3888 if (bl->loc_type == bp_loc_software_breakpoint
3889 || bl->loc_type == bp_loc_hardware_breakpoint)
3891 /* "Normal" instruction breakpoint: either the standard
3892 trap-instruction bp (bp_breakpoint), or a
3893 bp_hardware_breakpoint. */
3895 /* First check to see if we have to handle an overlay. */
3896 if (overlay_debugging == ovly_off
3897 || bl->section == NULL
3898 || !(section_is_overlay (bl->section)))
3900 /* No overlay handling: just remove the breakpoint. */
3902 /* If we're trying to uninsert a memory breakpoint that we
3903 know is set in a dynamic object that is marked
3904 shlib_disabled, then either the dynamic object was
3905 removed with "remove-symbol-file" or with
3906 "nosharedlibrary". In the former case, we don't know
3907 whether another dynamic object might have loaded over the
3908 breakpoint's address -- the user might well let us know
3909 about it next with add-symbol-file (the whole point of
3910 add-symbol-file is letting the user manually maintain a
3911 list of dynamically loaded objects). If we have the
3912 breakpoint's shadow memory, that is, this is a software
3913 breakpoint managed by GDB, check whether the breakpoint
3914 is still inserted in memory, to avoid overwriting wrong
3915 code with stale saved shadow contents. Note that HW
3916 breakpoints don't have shadow memory, as they're
3917 implemented using a mechanism that is not dependent on
3918 being able to modify the target's memory, and as such
3919 they should always be removed. */
3920 if (bl->shlib_disabled
3921 && bl->target_info.shadow_len != 0
3922 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3923 val = 0;
3924 else
3925 val = bl->owner->remove_location (bl, reason);
3927 else
3929 /* This breakpoint is in an overlay section.
3930 Did we set a breakpoint at the LMA? */
3931 if (!overlay_events_enabled)
3933 /* Yes -- overlay event support is not active, so we
3934 should have set a breakpoint at the LMA. Remove it.
3936 /* Ignore any failures: if the LMA is in ROM, we will
3937 have already warned when we failed to insert it. */
3938 if (bl->loc_type == bp_loc_hardware_breakpoint)
3939 target_remove_hw_breakpoint (bl->gdbarch,
3940 &bl->overlay_target_info);
3941 else
3942 target_remove_breakpoint (bl->gdbarch,
3943 &bl->overlay_target_info,
3944 reason);
3946 /* Did we set a breakpoint at the VMA?
3947 If so, we will have marked the breakpoint 'inserted'. */
3948 if (bl->inserted)
3950 /* Yes -- remove it. Previously we did not bother to
3951 remove the breakpoint if the section had been
3952 unmapped, but let's not rely on that being safe. We
3953 don't know what the overlay manager might do. */
3955 /* However, we should remove *software* breakpoints only
3956 if the section is still mapped, or else we overwrite
3957 wrong code with the saved shadow contents. */
3958 if (bl->loc_type == bp_loc_hardware_breakpoint
3959 || section_is_mapped (bl->section))
3960 val = bl->owner->remove_location (bl, reason);
3961 else
3962 val = 0;
3964 else
3966 /* No -- not inserted, so no need to remove. No error. */
3967 val = 0;
3971 /* In some cases, we might not be able to remove a breakpoint in
3972 a shared library that has already been removed, but we have
3973 not yet processed the shlib unload event. Similarly for an
3974 unloaded add-symbol-file object - the user might not yet have
3975 had the chance to remove-symbol-file it. shlib_disabled will
3976 be set if the library/object has already been removed, but
3977 the breakpoint hasn't been uninserted yet, e.g., after
3978 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3979 always-inserted mode. */
3980 if (val
3981 && (bl->loc_type == bp_loc_software_breakpoint
3982 && (bl->shlib_disabled
3983 || solib_name_from_address (bl->pspace, bl->address)
3984 || shared_objfile_contains_address_p (bl->pspace,
3985 bl->address))))
3986 val = 0;
3988 if (val)
3989 return val;
3990 bl->inserted = (reason == DETACH_BREAKPOINT);
3992 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3994 bl->inserted = (reason == DETACH_BREAKPOINT);
3995 bl->owner->remove_location (bl, reason);
3997 /* Failure to remove any of the hardware watchpoints comes here. */
3998 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3999 warning (_("Could not remove hardware watchpoint %d."),
4000 bl->owner->number);
4002 else if (bl->owner->type == bp_catchpoint
4003 && breakpoint_enabled (bl->owner)
4004 && !bl->duplicate)
4006 val = bl->owner->remove_location (bl, reason);
4007 if (val)
4008 return val;
4010 bl->inserted = (reason == DETACH_BREAKPOINT);
4013 return 0;
4016 static int
4017 remove_breakpoint (struct bp_location *bl)
4019 /* BL is never in moribund_locations by our callers. */
4020 gdb_assert (bl->owner != NULL);
4022 /* The type of none suggests that owner is actually deleted.
4023 This should not ever happen. */
4024 gdb_assert (bl->owner->type != bp_none);
4026 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4028 switch_to_program_space_and_thread (bl->pspace);
4030 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4033 /* Clear the "inserted" flag in all breakpoints. */
4035 void
4036 mark_breakpoints_out (void)
4038 for (bp_location *bl : all_bp_locations ())
4039 if (bl->pspace == current_program_space)
4040 bl->inserted = 0;
4043 /* Clear the "inserted" flag in all breakpoints and delete any
4044 breakpoints which should go away between runs of the program.
4046 Plus other such housekeeping that has to be done for breakpoints
4047 between runs.
4049 Note: this function gets called at the end of a run (by
4050 generic_mourn_inferior) and when a run begins (by
4051 init_wait_for_inferior). */
4055 void
4056 breakpoint_init_inferior (enum inf_context context)
4058 struct program_space *pspace = current_program_space;
4060 /* If breakpoint locations are shared across processes, then there's
4061 nothing to do. */
4062 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4063 return;
4065 mark_breakpoints_out ();
4067 for (breakpoint *b : all_breakpoints_safe ())
4069 if (b->loc && b->loc->pspace != pspace)
4070 continue;
4072 switch (b->type)
4074 case bp_call_dummy:
4075 case bp_longjmp_call_dummy:
4077 /* If the call dummy breakpoint is at the entry point it will
4078 cause problems when the inferior is rerun, so we better get
4079 rid of it. */
4081 case bp_watchpoint_scope:
4083 /* Also get rid of scope breakpoints. */
4085 case bp_shlib_event:
4087 /* Also remove solib event breakpoints. Their addresses may
4088 have changed since the last time we ran the program.
4089 Actually we may now be debugging against different target;
4090 and so the solib backend that installed this breakpoint may
4091 not be used in by the target. E.g.,
4093 (gdb) file prog-linux
4094 (gdb) run # native linux target
4096 (gdb) kill
4097 (gdb) file prog-win.exe
4098 (gdb) tar rem :9999 # remote Windows gdbserver.
4101 case bp_step_resume:
4103 /* Also remove step-resume breakpoints. */
4105 case bp_single_step:
4107 /* Also remove single-step breakpoints. */
4109 delete_breakpoint (b);
4110 break;
4112 case bp_watchpoint:
4113 case bp_hardware_watchpoint:
4114 case bp_read_watchpoint:
4115 case bp_access_watchpoint:
4117 struct watchpoint *w = (struct watchpoint *) b;
4119 /* Likewise for watchpoints on local expressions. */
4120 if (w->exp_valid_block != NULL)
4121 delete_breakpoint (b);
4122 else
4124 /* Get rid of existing locations, which are no longer
4125 valid. New ones will be created in
4126 update_watchpoint, when the inferior is restarted.
4127 The next update_global_location_list call will
4128 garbage collect them. */
4129 b->loc = NULL;
4131 if (context == inf_starting)
4133 /* Reset val field to force reread of starting value in
4134 insert_breakpoints. */
4135 w->val.reset (nullptr);
4136 w->val_valid = false;
4140 break;
4141 default:
4142 break;
4146 /* Get rid of the moribund locations. */
4147 for (bp_location *bl : moribund_locations)
4148 decref_bp_location (&bl);
4149 moribund_locations.clear ();
4152 /* These functions concern about actual breakpoints inserted in the
4153 target --- to e.g. check if we need to do decr_pc adjustment or if
4154 we need to hop over the bkpt --- so we check for address space
4155 match, not program space. */
4157 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4158 exists at PC. It returns ordinary_breakpoint_here if it's an
4159 ordinary breakpoint, or permanent_breakpoint_here if it's a
4160 permanent breakpoint.
4161 - When continuing from a location with an ordinary breakpoint, we
4162 actually single step once before calling insert_breakpoints.
4163 - When continuing from a location with a permanent breakpoint, we
4164 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4165 the target, to advance the PC past the breakpoint. */
4167 enum breakpoint_here
4168 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4170 int any_breakpoint_here = 0;
4172 for (bp_location *bl : all_bp_locations ())
4174 if (bl->loc_type != bp_loc_software_breakpoint
4175 && bl->loc_type != bp_loc_hardware_breakpoint)
4176 continue;
4178 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4179 if ((breakpoint_enabled (bl->owner)
4180 || bl->permanent)
4181 && breakpoint_location_address_match (bl, aspace, pc))
4183 if (overlay_debugging
4184 && section_is_overlay (bl->section)
4185 && !section_is_mapped (bl->section))
4186 continue; /* unmapped overlay -- can't be a match */
4187 else if (bl->permanent)
4188 return permanent_breakpoint_here;
4189 else
4190 any_breakpoint_here = 1;
4194 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4197 /* See breakpoint.h. */
4200 breakpoint_in_range_p (const address_space *aspace,
4201 CORE_ADDR addr, ULONGEST len)
4203 for (bp_location *bl : all_bp_locations ())
4205 if (bl->loc_type != bp_loc_software_breakpoint
4206 && bl->loc_type != bp_loc_hardware_breakpoint)
4207 continue;
4209 if ((breakpoint_enabled (bl->owner)
4210 || bl->permanent)
4211 && breakpoint_location_address_range_overlap (bl, aspace,
4212 addr, len))
4214 if (overlay_debugging
4215 && section_is_overlay (bl->section)
4216 && !section_is_mapped (bl->section))
4218 /* Unmapped overlay -- can't be a match. */
4219 continue;
4222 return 1;
4226 return 0;
4229 /* Return true if there's a moribund breakpoint at PC. */
4232 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4234 for (bp_location *loc : moribund_locations)
4235 if (breakpoint_location_address_match (loc, aspace, pc))
4236 return 1;
4238 return 0;
4241 /* Returns non-zero iff BL is inserted at PC, in address space
4242 ASPACE. */
4244 static int
4245 bp_location_inserted_here_p (struct bp_location *bl,
4246 const address_space *aspace, CORE_ADDR pc)
4248 if (bl->inserted
4249 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4250 aspace, pc))
4252 if (overlay_debugging
4253 && section_is_overlay (bl->section)
4254 && !section_is_mapped (bl->section))
4255 return 0; /* unmapped overlay -- can't be a match */
4256 else
4257 return 1;
4259 return 0;
4262 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4265 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4267 for (bp_location *bl : all_bp_locations_at_addr (pc))
4269 if (bl->loc_type != bp_loc_software_breakpoint
4270 && bl->loc_type != bp_loc_hardware_breakpoint)
4271 continue;
4273 if (bp_location_inserted_here_p (bl, aspace, pc))
4274 return 1;
4276 return 0;
4279 /* This function returns non-zero iff there is a software breakpoint
4280 inserted at PC. */
4283 software_breakpoint_inserted_here_p (const address_space *aspace,
4284 CORE_ADDR pc)
4286 for (bp_location *bl : all_bp_locations_at_addr (pc))
4288 if (bl->loc_type != bp_loc_software_breakpoint)
4289 continue;
4291 if (bp_location_inserted_here_p (bl, aspace, pc))
4292 return 1;
4295 return 0;
4298 /* See breakpoint.h. */
4301 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4302 CORE_ADDR pc)
4304 for (bp_location *bl : all_bp_locations_at_addr (pc))
4306 if (bl->loc_type != bp_loc_hardware_breakpoint)
4307 continue;
4309 if (bp_location_inserted_here_p (bl, aspace, pc))
4310 return 1;
4313 return 0;
4317 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4318 CORE_ADDR addr, ULONGEST len)
4320 for (breakpoint *bpt : all_breakpoints ())
4322 if (bpt->type != bp_hardware_watchpoint
4323 && bpt->type != bp_access_watchpoint)
4324 continue;
4326 if (!breakpoint_enabled (bpt))
4327 continue;
4329 for (bp_location *loc : bpt->locations ())
4330 if (loc->pspace->aspace == aspace && loc->inserted)
4332 CORE_ADDR l, h;
4334 /* Check for intersection. */
4335 l = std::max<CORE_ADDR> (loc->address, addr);
4336 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4337 if (l < h)
4338 return 1;
4341 return 0;
4344 /* See breakpoint.h. */
4346 bool
4347 is_catchpoint (struct breakpoint *b)
4349 return (b->type == bp_catchpoint);
4352 /* Clear a bpstat so that it says we are not at any breakpoint.
4353 Also free any storage that is part of a bpstat. */
4355 void
4356 bpstat_clear (bpstat **bsp)
4358 bpstat *p;
4359 bpstat *q;
4361 if (bsp == 0)
4362 return;
4363 p = *bsp;
4364 while (p != NULL)
4366 q = p->next;
4367 delete p;
4368 p = q;
4370 *bsp = NULL;
4373 bpstat::bpstat (const bpstat &other)
4374 : next (NULL),
4375 bp_location_at (other.bp_location_at),
4376 breakpoint_at (other.breakpoint_at),
4377 commands (other.commands),
4378 print (other.print),
4379 stop (other.stop),
4380 print_it (other.print_it)
4382 if (other.old_val != NULL)
4383 old_val = release_value (value_copy (other.old_val.get ()));
4386 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4387 is part of the bpstat is copied as well. */
4389 bpstat *
4390 bpstat_copy (bpstat *bs)
4392 bpstat *p = nullptr;
4393 bpstat *tmp;
4394 bpstat *retval = nullptr;
4396 if (bs == NULL)
4397 return bs;
4399 for (; bs != NULL; bs = bs->next)
4401 tmp = new bpstat (*bs);
4403 if (p == NULL)
4404 /* This is the first thing in the chain. */
4405 retval = tmp;
4406 else
4407 p->next = tmp;
4408 p = tmp;
4410 p->next = NULL;
4411 return retval;
4414 /* Find the bpstat associated with this breakpoint. */
4416 bpstat *
4417 bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
4419 if (bsp == NULL)
4420 return NULL;
4422 for (; bsp != NULL; bsp = bsp->next)
4424 if (bsp->breakpoint_at == breakpoint)
4425 return bsp;
4427 return NULL;
4430 /* See breakpoint.h. */
4432 bool
4433 bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
4435 for (; bsp != NULL; bsp = bsp->next)
4437 if (bsp->breakpoint_at == NULL)
4439 /* A moribund location can never explain a signal other than
4440 GDB_SIGNAL_TRAP. */
4441 if (sig == GDB_SIGNAL_TRAP)
4442 return true;
4444 else
4446 if (bsp->breakpoint_at->explains_signal (sig))
4447 return true;
4451 return false;
4454 /* Put in *NUM the breakpoint number of the first breakpoint we are
4455 stopped at. *BSP upon return is a bpstat which points to the
4456 remaining breakpoints stopped at (but which is not guaranteed to be
4457 good for anything but further calls to bpstat_num).
4459 Return 0 if passed a bpstat which does not indicate any breakpoints.
4460 Return -1 if stopped at a breakpoint that has been deleted since
4461 we set it.
4462 Return 1 otherwise. */
4465 bpstat_num (bpstat **bsp, int *num)
4467 struct breakpoint *b;
4469 if ((*bsp) == NULL)
4470 return 0; /* No more breakpoint values */
4472 /* We assume we'll never have several bpstats that correspond to a
4473 single breakpoint -- otherwise, this function might return the
4474 same number more than once and this will look ugly. */
4475 b = (*bsp)->breakpoint_at;
4476 *bsp = (*bsp)->next;
4477 if (b == NULL)
4478 return -1; /* breakpoint that's been deleted since */
4480 *num = b->number; /* We have its number */
4481 return 1;
4484 /* See breakpoint.h. */
4486 void
4487 bpstat_clear_actions (void)
4489 bpstat *bs;
4491 if (inferior_ptid == null_ptid)
4492 return;
4494 thread_info *tp = inferior_thread ();
4495 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4497 bs->commands = NULL;
4498 bs->old_val.reset (nullptr);
4502 /* Called when a command is about to proceed the inferior. */
4504 static void
4505 breakpoint_about_to_proceed (void)
4507 if (inferior_ptid != null_ptid)
4509 struct thread_info *tp = inferior_thread ();
4511 /* Allow inferior function calls in breakpoint commands to not
4512 interrupt the command list. When the call finishes
4513 successfully, the inferior will be standing at the same
4514 breakpoint as if nothing happened. */
4515 if (tp->control.in_infcall)
4516 return;
4519 breakpoint_proceeded = 1;
4522 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4523 or its equivalent. */
4525 static int
4526 command_line_is_silent (struct command_line *cmd)
4528 return cmd && (strcmp ("silent", cmd->line) == 0);
4531 /* Execute all the commands associated with all the breakpoints at
4532 this location. Any of these commands could cause the process to
4533 proceed beyond this point, etc. We look out for such changes by
4534 checking the global "breakpoint_proceeded" after each command.
4536 Returns true if a breakpoint command resumed the inferior. In that
4537 case, it is the caller's responsibility to recall it again with the
4538 bpstat of the current thread. */
4540 static int
4541 bpstat_do_actions_1 (bpstat **bsp)
4543 bpstat *bs;
4544 int again = 0;
4546 /* Avoid endless recursion if a `source' command is contained
4547 in bs->commands. */
4548 if (executing_breakpoint_commands)
4549 return 0;
4551 scoped_restore save_executing
4552 = make_scoped_restore (&executing_breakpoint_commands, 1);
4554 scoped_restore preventer = prevent_dont_repeat ();
4556 /* This pointer will iterate over the list of bpstat's. */
4557 bs = *bsp;
4559 breakpoint_proceeded = 0;
4560 for (; bs != NULL; bs = bs->next)
4562 struct command_line *cmd = NULL;
4564 /* Take ownership of the BSP's command tree, if it has one.
4566 The command tree could legitimately contain commands like
4567 'step' and 'next', which call clear_proceed_status, which
4568 frees stop_bpstat's command tree. To make sure this doesn't
4569 free the tree we're executing out from under us, we need to
4570 take ownership of the tree ourselves. Since a given bpstat's
4571 commands are only executed once, we don't need to copy it; we
4572 can clear the pointer in the bpstat, and make sure we free
4573 the tree when we're done. */
4574 counted_command_line ccmd = bs->commands;
4575 bs->commands = NULL;
4576 if (ccmd != NULL)
4577 cmd = ccmd.get ();
4578 if (command_line_is_silent (cmd))
4580 /* The action has been already done by bpstat_stop_status. */
4581 cmd = cmd->next;
4584 while (cmd != NULL)
4586 execute_control_command (cmd);
4588 if (breakpoint_proceeded)
4589 break;
4590 else
4591 cmd = cmd->next;
4594 if (breakpoint_proceeded)
4596 if (current_ui->async)
4597 /* If we are in async mode, then the target might be still
4598 running, not stopped at any breakpoint, so nothing for
4599 us to do here -- just return to the event loop. */
4601 else
4602 /* In sync mode, when execute_control_command returns
4603 we're already standing on the next breakpoint.
4604 Breakpoint commands for that stop were not run, since
4605 execute_command does not run breakpoint commands --
4606 only command_line_handler does, but that one is not
4607 involved in execution of breakpoint commands. So, we
4608 can now execute breakpoint commands. It should be
4609 noted that making execute_command do bpstat actions is
4610 not an option -- in this case we'll have recursive
4611 invocation of bpstat for each breakpoint with a
4612 command, and can easily blow up GDB stack. Instead, we
4613 return true, which will trigger the caller to recall us
4614 with the new stop_bpstat. */
4615 again = 1;
4616 break;
4619 return again;
4622 /* Helper for bpstat_do_actions. Get the current thread, if there's
4623 one, is alive and has execution. Return NULL otherwise. */
4625 static thread_info *
4626 get_bpstat_thread ()
4628 if (inferior_ptid == null_ptid || !target_has_execution ())
4629 return NULL;
4631 thread_info *tp = inferior_thread ();
4632 if (tp->state == THREAD_EXITED || tp->executing ())
4633 return NULL;
4634 return tp;
4637 void
4638 bpstat_do_actions (void)
4640 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4641 thread_info *tp;
4643 /* Do any commands attached to breakpoint we are stopped at. */
4644 while ((tp = get_bpstat_thread ()) != NULL)
4646 /* Since in sync mode, bpstat_do_actions may resume the
4647 inferior, and only return when it is stopped at the next
4648 breakpoint, we keep doing breakpoint actions until it returns
4649 false to indicate the inferior was not resumed. */
4650 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4651 break;
4654 cleanup_if_error.release ();
4657 /* Print out the (old or new) value associated with a watchpoint. */
4659 static void
4660 watchpoint_value_print (struct value *val, struct ui_file *stream)
4662 if (val == NULL)
4663 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4664 else
4666 struct value_print_options opts;
4667 get_user_print_options (&opts);
4668 value_print (val, stream, &opts);
4672 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4673 debugging multiple threads. */
4675 void
4676 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4678 if (uiout->is_mi_like_p ())
4679 return;
4681 uiout->text ("\n");
4683 if (show_thread_that_caused_stop ())
4685 struct thread_info *thr = inferior_thread ();
4687 uiout->text ("Thread ");
4688 uiout->field_string ("thread-id", print_thread_id (thr));
4690 const char *name = thread_name (thr);
4691 if (name != NULL)
4693 uiout->text (" \"");
4694 uiout->field_string ("name", name);
4695 uiout->text ("\"");
4698 uiout->text (" hit ");
4702 /* Generic routine for printing messages indicating why we
4703 stopped. The behavior of this function depends on the value
4704 'print_it' in the bpstat structure. Under some circumstances we
4705 may decide not to print anything here and delegate the task to
4706 normal_stop(). */
4708 static enum print_stop_action
4709 print_bp_stop_message (bpstat *bs)
4711 switch (bs->print_it)
4713 case print_it_noop:
4714 /* Nothing should be printed for this bpstat entry. */
4715 return PRINT_UNKNOWN;
4716 break;
4718 case print_it_done:
4719 /* We still want to print the frame, but we already printed the
4720 relevant messages. */
4721 return PRINT_SRC_AND_LOC;
4722 break;
4724 case print_it_normal:
4726 struct breakpoint *b = bs->breakpoint_at;
4728 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4729 which has since been deleted. */
4730 if (b == NULL)
4731 return PRINT_UNKNOWN;
4733 /* Normal case. Call the breakpoint's print_it method. */
4734 return b->print_it (bs);
4736 break;
4738 default:
4739 internal_error (__FILE__, __LINE__,
4740 _("print_bp_stop_message: unrecognized enum value"));
4741 break;
4745 /* See breakpoint.h. */
4747 void
4748 print_solib_event (bool is_catchpoint)
4750 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4751 bool any_added = !current_program_space->added_solibs.empty ();
4753 if (!is_catchpoint)
4755 if (any_added || any_deleted)
4756 current_uiout->text (_("Stopped due to shared library event:\n"));
4757 else
4758 current_uiout->text (_("Stopped due to shared library event (no "
4759 "libraries added or removed)\n"));
4762 if (current_uiout->is_mi_like_p ())
4763 current_uiout->field_string ("reason",
4764 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4766 if (any_deleted)
4768 current_uiout->text (_(" Inferior unloaded "));
4769 ui_out_emit_list list_emitter (current_uiout, "removed");
4770 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4772 const std::string &name = current_program_space->deleted_solibs[ix];
4774 if (ix > 0)
4775 current_uiout->text (" ");
4776 current_uiout->field_string ("library", name);
4777 current_uiout->text ("\n");
4781 if (any_added)
4783 current_uiout->text (_(" Inferior loaded "));
4784 ui_out_emit_list list_emitter (current_uiout, "added");
4785 bool first = true;
4786 for (so_list *iter : current_program_space->added_solibs)
4788 if (!first)
4789 current_uiout->text (" ");
4790 first = false;
4791 current_uiout->field_string ("library", iter->so_name);
4792 current_uiout->text ("\n");
4797 /* Print a message indicating what happened. This is called from
4798 normal_stop(). The input to this routine is the head of the bpstat
4799 list - a list of the eventpoints that caused this stop. KIND is
4800 the target_waitkind for the stopping event. This
4801 routine calls the generic print routine for printing a message
4802 about reasons for stopping. This will print (for example) the
4803 "Breakpoint n," part of the output. The return value of this
4804 routine is one of:
4806 PRINT_UNKNOWN: Means we printed nothing.
4807 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4808 code to print the location. An example is
4809 "Breakpoint 1, " which should be followed by
4810 the location.
4811 PRINT_SRC_ONLY: Means we printed something, but there is no need
4812 to also print the location part of the message.
4813 An example is the catch/throw messages, which
4814 don't require a location appended to the end.
4815 PRINT_NOTHING: We have done some printing and we don't need any
4816 further info to be printed. */
4818 enum print_stop_action
4819 bpstat_print (bpstat *bs, int kind)
4821 enum print_stop_action val;
4823 /* Maybe another breakpoint in the chain caused us to stop.
4824 (Currently all watchpoints go on the bpstat whether hit or not.
4825 That probably could (should) be changed, provided care is taken
4826 with respect to bpstat_explains_signal). */
4827 for (; bs; bs = bs->next)
4829 val = print_bp_stop_message (bs);
4830 if (val == PRINT_SRC_ONLY
4831 || val == PRINT_SRC_AND_LOC
4832 || val == PRINT_NOTHING)
4833 return val;
4836 /* If we had hit a shared library event breakpoint,
4837 print_bp_stop_message would print out this message. If we hit an
4838 OS-level shared library event, do the same thing. */
4839 if (kind == TARGET_WAITKIND_LOADED)
4841 print_solib_event (false);
4842 return PRINT_NOTHING;
4845 /* We reached the end of the chain, or we got a null BS to start
4846 with and nothing was printed. */
4847 return PRINT_UNKNOWN;
4850 /* Evaluate the boolean expression EXP and return the result. */
4852 static bool
4853 breakpoint_cond_eval (expression *exp)
4855 struct value *mark = value_mark ();
4856 bool res = value_true (evaluate_expression (exp));
4858 value_free_to_mark (mark);
4859 return res;
4862 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4864 bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
4865 : next (NULL),
4866 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4867 breakpoint_at (bl->owner),
4868 commands (NULL),
4869 print (0),
4870 stop (0),
4871 print_it (print_it_normal)
4873 **bs_link_pointer = this;
4874 *bs_link_pointer = &next;
4877 bpstat::bpstat ()
4878 : next (NULL),
4879 breakpoint_at (NULL),
4880 commands (NULL),
4881 print (0),
4882 stop (0),
4883 print_it (print_it_normal)
4887 /* The target has stopped with waitstatus WS. Check if any hardware
4888 watchpoints have triggered, according to the target. */
4891 watchpoints_triggered (const target_waitstatus &ws)
4893 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4894 CORE_ADDR addr;
4896 if (!stopped_by_watchpoint)
4898 /* We were not stopped by a watchpoint. Mark all watchpoints
4899 as not triggered. */
4900 for (breakpoint *b : all_breakpoints ())
4901 if (is_hardware_watchpoint (b))
4903 struct watchpoint *w = (struct watchpoint *) b;
4905 w->watchpoint_triggered = watch_triggered_no;
4908 return 0;
4911 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4913 /* We were stopped by a watchpoint, but we don't know where.
4914 Mark all watchpoints as unknown. */
4915 for (breakpoint *b : all_breakpoints ())
4916 if (is_hardware_watchpoint (b))
4918 struct watchpoint *w = (struct watchpoint *) b;
4920 w->watchpoint_triggered = watch_triggered_unknown;
4923 return 1;
4926 /* The target could report the data address. Mark watchpoints
4927 affected by this data address as triggered, and all others as not
4928 triggered. */
4930 for (breakpoint *b : all_breakpoints ())
4931 if (is_hardware_watchpoint (b))
4933 struct watchpoint *w = (struct watchpoint *) b;
4935 w->watchpoint_triggered = watch_triggered_no;
4936 for (bp_location *loc : b->locations ())
4938 if (is_masked_watchpoint (b))
4940 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4941 CORE_ADDR start = loc->address & w->hw_wp_mask;
4943 if (newaddr == start)
4945 w->watchpoint_triggered = watch_triggered_yes;
4946 break;
4949 /* Exact match not required. Within range is sufficient. */
4950 else if (target_watchpoint_addr_within_range
4951 (current_inferior ()->top_target (), addr, loc->address,
4952 loc->length))
4954 w->watchpoint_triggered = watch_triggered_yes;
4955 break;
4960 return 1;
4963 /* Possible return values for watchpoint_check. */
4964 enum wp_check_result
4966 /* The watchpoint has been deleted. */
4967 WP_DELETED = 1,
4969 /* The value has changed. */
4970 WP_VALUE_CHANGED = 2,
4972 /* The value has not changed. */
4973 WP_VALUE_NOT_CHANGED = 3,
4975 /* Ignore this watchpoint, no matter if the value changed or not. */
4976 WP_IGNORE = 4,
4979 #define BP_TEMPFLAG 1
4980 #define BP_HARDWAREFLAG 2
4982 /* Evaluate watchpoint condition expression and check if its value
4983 changed. */
4985 static wp_check_result
4986 watchpoint_check (bpstat *bs)
4988 struct watchpoint *b;
4989 struct frame_info *fr;
4990 int within_current_scope;
4992 /* BS is built from an existing struct breakpoint. */
4993 gdb_assert (bs->breakpoint_at != NULL);
4994 b = (struct watchpoint *) bs->breakpoint_at;
4996 /* If this is a local watchpoint, we only want to check if the
4997 watchpoint frame is in scope if the current thread is the thread
4998 that was used to create the watchpoint. */
4999 if (!watchpoint_in_thread_scope (b))
5000 return WP_IGNORE;
5002 if (b->exp_valid_block == NULL)
5003 within_current_scope = 1;
5004 else
5006 struct frame_info *frame = get_current_frame ();
5007 struct gdbarch *frame_arch = get_frame_arch (frame);
5008 CORE_ADDR frame_pc = get_frame_pc (frame);
5010 /* stack_frame_destroyed_p() returns a non-zero value if we're
5011 still in the function but the stack frame has already been
5012 invalidated. Since we can't rely on the values of local
5013 variables after the stack has been destroyed, we are treating
5014 the watchpoint in that state as `not changed' without further
5015 checking. Don't mark watchpoints as changed if the current
5016 frame is in an epilogue - even if they are in some other
5017 frame, our view of the stack is likely to be wrong and
5018 frame_find_by_id could error out. */
5019 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5020 return WP_IGNORE;
5022 fr = frame_find_by_id (b->watchpoint_frame);
5023 within_current_scope = (fr != NULL);
5025 /* If we've gotten confused in the unwinder, we might have
5026 returned a frame that can't describe this variable. */
5027 if (within_current_scope)
5029 struct symbol *function;
5031 function = get_frame_function (fr);
5032 if (function == NULL
5033 || !contained_in (b->exp_valid_block, function->value_block ()))
5034 within_current_scope = 0;
5037 if (within_current_scope)
5038 /* If we end up stopping, the current frame will get selected
5039 in normal_stop. So this call to select_frame won't affect
5040 the user. */
5041 select_frame (fr);
5044 if (within_current_scope)
5046 /* We use value_{,free_to_}mark because it could be a *long*
5047 time before we return to the command level and call
5048 free_all_values. We can't call free_all_values because we
5049 might be in the middle of evaluating a function call. */
5051 struct value *mark;
5052 struct value *new_val;
5054 if (is_masked_watchpoint (b))
5055 /* Since we don't know the exact trigger address (from
5056 stopped_data_address), just tell the user we've triggered
5057 a mask watchpoint. */
5058 return WP_VALUE_CHANGED;
5060 mark = value_mark ();
5061 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5062 NULL, NULL, false);
5064 if (b->val_bitsize != 0)
5065 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5067 /* We use value_equal_contents instead of value_equal because
5068 the latter coerces an array to a pointer, thus comparing just
5069 the address of the array instead of its contents. This is
5070 not what we want. */
5071 if ((b->val != NULL) != (new_val != NULL)
5072 || (b->val != NULL && !value_equal_contents (b->val.get (),
5073 new_val)))
5075 bs->old_val = b->val;
5076 b->val = release_value (new_val);
5077 b->val_valid = true;
5078 if (new_val != NULL)
5079 value_free_to_mark (mark);
5080 return WP_VALUE_CHANGED;
5082 else
5084 /* Nothing changed. */
5085 value_free_to_mark (mark);
5086 return WP_VALUE_NOT_CHANGED;
5089 else
5091 /* This seems like the only logical thing to do because
5092 if we temporarily ignored the watchpoint, then when
5093 we reenter the block in which it is valid it contains
5094 garbage (in the case of a function, it may have two
5095 garbage values, one before and one after the prologue).
5096 So we can't even detect the first assignment to it and
5097 watch after that (since the garbage may or may not equal
5098 the first value assigned). */
5099 /* We print all the stop information in
5100 breakpointprint_it, but in this case, by the time we
5101 call breakpoint->print_it this bp will be deleted
5102 already. So we have no choice but print the information
5103 here. */
5105 SWITCH_THRU_ALL_UIS ()
5107 struct ui_out *uiout = current_uiout;
5109 if (uiout->is_mi_like_p ())
5110 uiout->field_string
5111 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5112 uiout->message ("\nWatchpoint %pF deleted because the program has "
5113 "left the block in\n"
5114 "which its expression is valid.\n",
5115 signed_field ("wpnum", b->number));
5118 /* Make sure the watchpoint's commands aren't executed. */
5119 b->commands = NULL;
5120 watchpoint_del_at_next_stop (b);
5122 return WP_DELETED;
5126 /* Return true if it looks like target has stopped due to hitting
5127 breakpoint location BL. This function does not check if we should
5128 stop, only if BL explains the stop. */
5130 static int
5131 bpstat_check_location (const struct bp_location *bl,
5132 const address_space *aspace, CORE_ADDR bp_addr,
5133 const target_waitstatus &ws)
5135 struct breakpoint *b = bl->owner;
5137 /* BL is from an existing breakpoint. */
5138 gdb_assert (b != NULL);
5140 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
5143 /* Determine if the watched values have actually changed, and we
5144 should stop. If not, set BS->stop to 0. */
5146 static void
5147 bpstat_check_watchpoint (bpstat *bs)
5149 const struct bp_location *bl;
5150 struct watchpoint *b;
5152 /* BS is built for existing struct breakpoint. */
5153 bl = bs->bp_location_at.get ();
5154 gdb_assert (bl != NULL);
5155 b = (struct watchpoint *) bs->breakpoint_at;
5156 gdb_assert (b != NULL);
5159 int must_check_value = 0;
5161 if (b->type == bp_watchpoint)
5162 /* For a software watchpoint, we must always check the
5163 watched value. */
5164 must_check_value = 1;
5165 else if (b->watchpoint_triggered == watch_triggered_yes)
5166 /* We have a hardware watchpoint (read, write, or access)
5167 and the target earlier reported an address watched by
5168 this watchpoint. */
5169 must_check_value = 1;
5170 else if (b->watchpoint_triggered == watch_triggered_unknown
5171 && b->type == bp_hardware_watchpoint)
5172 /* We were stopped by a hardware watchpoint, but the target could
5173 not report the data address. We must check the watchpoint's
5174 value. Access and read watchpoints are out of luck; without
5175 a data address, we can't figure it out. */
5176 must_check_value = 1;
5178 if (must_check_value)
5180 wp_check_result e;
5184 e = watchpoint_check (bs);
5186 catch (const gdb_exception &ex)
5188 exception_fprintf (gdb_stderr, ex,
5189 "Error evaluating expression "
5190 "for watchpoint %d\n",
5191 b->number);
5193 SWITCH_THRU_ALL_UIS ()
5195 gdb_printf (_("Watchpoint %d deleted.\n"),
5196 b->number);
5198 watchpoint_del_at_next_stop (b);
5199 e = WP_DELETED;
5202 switch (e)
5204 case WP_DELETED:
5205 /* We've already printed what needs to be printed. */
5206 bs->print_it = print_it_done;
5207 /* Stop. */
5208 break;
5209 case WP_IGNORE:
5210 bs->print_it = print_it_noop;
5211 bs->stop = 0;
5212 break;
5213 case WP_VALUE_CHANGED:
5214 if (b->type == bp_read_watchpoint)
5216 /* There are two cases to consider here:
5218 1. We're watching the triggered memory for reads.
5219 In that case, trust the target, and always report
5220 the watchpoint hit to the user. Even though
5221 reads don't cause value changes, the value may
5222 have changed since the last time it was read, and
5223 since we're not trapping writes, we will not see
5224 those, and as such we should ignore our notion of
5225 old value.
5227 2. We're watching the triggered memory for both
5228 reads and writes. There are two ways this may
5229 happen:
5231 2.1. This is a target that can't break on data
5232 reads only, but can break on accesses (reads or
5233 writes), such as e.g., x86. We detect this case
5234 at the time we try to insert read watchpoints.
5236 2.2. Otherwise, the target supports read
5237 watchpoints, but, the user set an access or write
5238 watchpoint watching the same memory as this read
5239 watchpoint.
5241 If we're watching memory writes as well as reads,
5242 ignore watchpoint hits when we find that the
5243 value hasn't changed, as reads don't cause
5244 changes. This still gives false positives when
5245 the program writes the same value to memory as
5246 what there was already in memory (we will confuse
5247 it for a read), but it's much better than
5248 nothing. */
5250 int other_write_watchpoint = 0;
5252 if (bl->watchpoint_type == hw_read)
5254 for (breakpoint *other_b : all_breakpoints ())
5255 if (other_b->type == bp_hardware_watchpoint
5256 || other_b->type == bp_access_watchpoint)
5258 struct watchpoint *other_w =
5259 (struct watchpoint *) other_b;
5261 if (other_w->watchpoint_triggered
5262 == watch_triggered_yes)
5264 other_write_watchpoint = 1;
5265 break;
5270 if (other_write_watchpoint
5271 || bl->watchpoint_type == hw_access)
5273 /* We're watching the same memory for writes,
5274 and the value changed since the last time we
5275 updated it, so this trap must be for a write.
5276 Ignore it. */
5277 bs->print_it = print_it_noop;
5278 bs->stop = 0;
5281 break;
5282 case WP_VALUE_NOT_CHANGED:
5283 if (b->type == bp_hardware_watchpoint
5284 || b->type == bp_watchpoint)
5286 /* Don't stop: write watchpoints shouldn't fire if
5287 the value hasn't changed. */
5288 bs->print_it = print_it_noop;
5289 bs->stop = 0;
5291 /* Stop. */
5292 break;
5293 default:
5294 /* Can't happen. */
5295 break;
5298 else /* must_check_value == 0 */
5300 /* This is a case where some watchpoint(s) triggered, but
5301 not at the address of this watchpoint, or else no
5302 watchpoint triggered after all. So don't print
5303 anything for this watchpoint. */
5304 bs->print_it = print_it_noop;
5305 bs->stop = 0;
5310 /* For breakpoints that are currently marked as telling gdb to stop,
5311 check conditions (condition proper, frame, thread and ignore count)
5312 of breakpoint referred to by BS. If we should not stop for this
5313 breakpoint, set BS->stop to 0. */
5315 static void
5316 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5318 const struct bp_location *bl;
5319 struct breakpoint *b;
5320 /* Assume stop. */
5321 bool condition_result = true;
5322 struct expression *cond;
5324 gdb_assert (bs->stop);
5326 /* BS is built for existing struct breakpoint. */
5327 bl = bs->bp_location_at.get ();
5328 gdb_assert (bl != NULL);
5329 b = bs->breakpoint_at;
5330 gdb_assert (b != NULL);
5332 /* Even if the target evaluated the condition on its end and notified GDB, we
5333 need to do so again since GDB does not know if we stopped due to a
5334 breakpoint or a single step breakpoint. */
5336 if (frame_id_p (b->frame_id)
5337 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5339 bs->stop = 0;
5340 return;
5343 /* If this is a thread/task-specific breakpoint, don't waste cpu
5344 evaluating the condition if this isn't the specified
5345 thread/task. */
5346 if ((b->thread != -1 && b->thread != thread->global_num)
5347 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5349 bs->stop = 0;
5350 return;
5353 /* Evaluate extension language breakpoints that have a "stop" method
5354 implemented. */
5355 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5357 if (is_watchpoint (b))
5359 struct watchpoint *w = (struct watchpoint *) b;
5361 cond = w->cond_exp.get ();
5363 else
5364 cond = bl->cond.get ();
5366 if (cond && b->disposition != disp_del_at_next_stop)
5368 int within_current_scope = 1;
5369 struct watchpoint * w;
5371 /* We use value_mark and value_free_to_mark because it could
5372 be a long time before we return to the command level and
5373 call free_all_values. We can't call free_all_values
5374 because we might be in the middle of evaluating a
5375 function call. */
5376 struct value *mark = value_mark ();
5378 if (is_watchpoint (b))
5379 w = (struct watchpoint *) b;
5380 else
5381 w = NULL;
5383 /* Need to select the frame, with all that implies so that
5384 the conditions will have the right context. Because we
5385 use the frame, we will not see an inlined function's
5386 variables when we arrive at a breakpoint at the start
5387 of the inlined function; the current frame will be the
5388 call site. */
5389 if (w == NULL || w->cond_exp_valid_block == NULL)
5390 select_frame (get_current_frame ());
5391 else
5393 struct frame_info *frame;
5395 /* For local watchpoint expressions, which particular
5396 instance of a local is being watched matters, so we
5397 keep track of the frame to evaluate the expression
5398 in. To evaluate the condition however, it doesn't
5399 really matter which instantiation of the function
5400 where the condition makes sense triggers the
5401 watchpoint. This allows an expression like "watch
5402 global if q > 10" set in `func', catch writes to
5403 global on all threads that call `func', or catch
5404 writes on all recursive calls of `func' by a single
5405 thread. We simply always evaluate the condition in
5406 the innermost frame that's executing where it makes
5407 sense to evaluate the condition. It seems
5408 intuitive. */
5409 frame = block_innermost_frame (w->cond_exp_valid_block);
5410 if (frame != NULL)
5411 select_frame (frame);
5412 else
5413 within_current_scope = 0;
5415 if (within_current_scope)
5419 condition_result = breakpoint_cond_eval (cond);
5421 catch (const gdb_exception &ex)
5423 exception_fprintf (gdb_stderr, ex,
5424 "Error in testing breakpoint condition:\n");
5427 else
5429 warning (_("Watchpoint condition cannot be tested "
5430 "in the current scope"));
5431 /* If we failed to set the right context for this
5432 watchpoint, unconditionally report it. */
5434 /* FIXME-someday, should give breakpoint #. */
5435 value_free_to_mark (mark);
5438 if (cond && !condition_result)
5440 bs->stop = 0;
5442 else if (b->ignore_count > 0)
5444 b->ignore_count--;
5445 bs->stop = 0;
5446 /* Increase the hit count even though we don't stop. */
5447 ++(b->hit_count);
5448 gdb::observers::breakpoint_modified.notify (b);
5452 /* Returns true if we need to track moribund locations of LOC's type
5453 on the current target. */
5455 static int
5456 need_moribund_for_location_type (struct bp_location *loc)
5458 return ((loc->loc_type == bp_loc_software_breakpoint
5459 && !target_supports_stopped_by_sw_breakpoint ())
5460 || (loc->loc_type == bp_loc_hardware_breakpoint
5461 && !target_supports_stopped_by_hw_breakpoint ()));
5464 /* See breakpoint.h. */
5466 bpstat *
5467 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5468 const target_waitstatus &ws)
5470 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5472 for (breakpoint *b : all_breakpoints ())
5474 if (!breakpoint_enabled (b))
5475 continue;
5477 for (bp_location *bl : b->locations ())
5479 /* For hardware watchpoints, we look only at the first
5480 location. The watchpoint_check function will work on the
5481 entire expression, not the individual locations. For
5482 read watchpoints, the watchpoints_triggered function has
5483 checked all locations already. */
5484 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5485 break;
5487 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5488 continue;
5490 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5491 continue;
5493 /* Come here if it's a watchpoint, or if the break address
5494 matches. */
5496 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
5497 explain stop. */
5499 /* Assume we stop. Should we find a watchpoint that is not
5500 actually triggered, or if the condition of the breakpoint
5501 evaluates as false, we'll reset 'stop' to 0. */
5502 bs->stop = 1;
5503 bs->print = 1;
5505 /* If this is a scope breakpoint, mark the associated
5506 watchpoint as triggered so that we will handle the
5507 out-of-scope event. We'll get to the watchpoint next
5508 iteration. */
5509 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5511 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5513 w->watchpoint_triggered = watch_triggered_yes;
5518 /* Check if a moribund breakpoint explains the stop. */
5519 if (!target_supports_stopped_by_sw_breakpoint ()
5520 || !target_supports_stopped_by_hw_breakpoint ())
5522 for (bp_location *loc : moribund_locations)
5524 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5525 && need_moribund_for_location_type (loc))
5527 bpstat *bs = new bpstat (loc, &bs_link);
5528 /* For hits of moribund locations, we should just proceed. */
5529 bs->stop = 0;
5530 bs->print = 0;
5531 bs->print_it = print_it_noop;
5536 return bs_head;
5539 /* See breakpoint.h. */
5541 bpstat *
5542 bpstat_stop_status (const address_space *aspace,
5543 CORE_ADDR bp_addr, thread_info *thread,
5544 const target_waitstatus &ws,
5545 bpstat *stop_chain)
5547 struct breakpoint *b = NULL;
5548 /* First item of allocated bpstat's. */
5549 bpstat *bs_head = stop_chain;
5550 bpstat *bs;
5551 int need_remove_insert;
5552 int removed_any;
5554 /* First, build the bpstat chain with locations that explain a
5555 target stop, while being careful to not set the target running,
5556 as that may invalidate locations (in particular watchpoint
5557 locations are recreated). Resuming will happen here with
5558 breakpoint conditions or watchpoint expressions that include
5559 inferior function calls. */
5560 if (bs_head == NULL)
5561 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5563 /* A bit of special processing for shlib breakpoints. We need to
5564 process solib loading here, so that the lists of loaded and
5565 unloaded libraries are correct before we handle "catch load" and
5566 "catch unload". */
5567 for (bs = bs_head; bs != NULL; bs = bs->next)
5569 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5571 handle_solib_event ();
5572 break;
5576 /* Now go through the locations that caused the target to stop, and
5577 check whether we're interested in reporting this stop to higher
5578 layers, or whether we should resume the target transparently. */
5580 removed_any = 0;
5582 for (bs = bs_head; bs != NULL; bs = bs->next)
5584 if (!bs->stop)
5585 continue;
5587 b = bs->breakpoint_at;
5588 b->check_status (bs);
5589 if (bs->stop)
5591 bpstat_check_breakpoint_conditions (bs, thread);
5593 if (bs->stop)
5595 ++(b->hit_count);
5597 /* We will stop here. */
5598 if (b->disposition == disp_disable)
5600 --(b->enable_count);
5601 if (b->enable_count <= 0)
5602 b->enable_state = bp_disabled;
5603 removed_any = 1;
5605 gdb::observers::breakpoint_modified.notify (b);
5606 if (b->silent)
5607 bs->print = 0;
5608 bs->commands = b->commands;
5609 if (command_line_is_silent (bs->commands
5610 ? bs->commands.get () : NULL))
5611 bs->print = 0;
5613 b->after_condition_true (bs);
5618 /* Print nothing for this entry if we don't stop or don't
5619 print. */
5620 if (!bs->stop || !bs->print)
5621 bs->print_it = print_it_noop;
5624 /* If we aren't stopping, the value of some hardware watchpoint may
5625 not have changed, but the intermediate memory locations we are
5626 watching may have. Don't bother if we're stopping; this will get
5627 done later. */
5628 need_remove_insert = 0;
5629 if (! bpstat_causes_stop (bs_head))
5630 for (bs = bs_head; bs != NULL; bs = bs->next)
5631 if (!bs->stop
5632 && bs->breakpoint_at
5633 && is_hardware_watchpoint (bs->breakpoint_at))
5635 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5637 update_watchpoint (w, 0 /* don't reparse. */);
5638 need_remove_insert = 1;
5641 if (need_remove_insert)
5642 update_global_location_list (UGLL_MAY_INSERT);
5643 else if (removed_any)
5644 update_global_location_list (UGLL_DONT_INSERT);
5646 return bs_head;
5649 /* See breakpoint.h. */
5651 bpstat *
5652 bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5653 thread_info *thread, const target_waitstatus &ws)
5655 gdb_assert (!target_stopped_by_watchpoint ());
5657 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5658 previous stop to avoid confusing bpstat_stop_status. */
5659 watchpoints_triggered (ws);
5661 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5664 static void
5665 handle_jit_event (CORE_ADDR address)
5667 struct gdbarch *gdbarch;
5669 infrun_debug_printf ("handling bp_jit_event");
5671 /* Switch terminal for any messages produced by
5672 breakpoint_re_set. */
5673 target_terminal::ours_for_output ();
5675 gdbarch = get_frame_arch (get_current_frame ());
5676 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5677 thus it is expected that its objectfile can be found through
5678 minimal symbol lookup. If it doesn't work (and assert fails), it
5679 most likely means that `jit_breakpoint_re_set` was changes and this
5680 function needs to be updated too. */
5681 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5682 gdb_assert (jit_bp_sym.objfile != nullptr);
5683 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5685 target_terminal::inferior ();
5688 /* Prepare WHAT final decision for infrun. */
5690 /* Decide what infrun needs to do with this bpstat. */
5692 struct bpstat_what
5693 bpstat_what (bpstat *bs_head)
5695 struct bpstat_what retval;
5696 bpstat *bs;
5698 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5699 retval.call_dummy = STOP_NONE;
5700 retval.is_longjmp = false;
5702 for (bs = bs_head; bs != NULL; bs = bs->next)
5704 /* Extract this BS's action. After processing each BS, we check
5705 if its action overrides all we've seem so far. */
5706 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5707 enum bptype bptype;
5709 if (bs->breakpoint_at == NULL)
5711 /* I suspect this can happen if it was a momentary
5712 breakpoint which has since been deleted. */
5713 bptype = bp_none;
5715 else
5716 bptype = bs->breakpoint_at->type;
5718 switch (bptype)
5720 case bp_none:
5721 break;
5722 case bp_breakpoint:
5723 case bp_hardware_breakpoint:
5724 case bp_single_step:
5725 case bp_until:
5726 case bp_finish:
5727 case bp_shlib_event:
5728 if (bs->stop)
5730 if (bs->print)
5731 this_action = BPSTAT_WHAT_STOP_NOISY;
5732 else
5733 this_action = BPSTAT_WHAT_STOP_SILENT;
5735 else
5736 this_action = BPSTAT_WHAT_SINGLE;
5737 break;
5738 case bp_watchpoint:
5739 case bp_hardware_watchpoint:
5740 case bp_read_watchpoint:
5741 case bp_access_watchpoint:
5742 if (bs->stop)
5744 if (bs->print)
5745 this_action = BPSTAT_WHAT_STOP_NOISY;
5746 else
5747 this_action = BPSTAT_WHAT_STOP_SILENT;
5749 else
5751 /* There was a watchpoint, but we're not stopping.
5752 This requires no further action. */
5754 break;
5755 case bp_longjmp:
5756 case bp_longjmp_call_dummy:
5757 case bp_exception:
5758 if (bs->stop)
5760 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5761 retval.is_longjmp = bptype != bp_exception;
5763 else
5764 this_action = BPSTAT_WHAT_SINGLE;
5765 break;
5766 case bp_longjmp_resume:
5767 case bp_exception_resume:
5768 if (bs->stop)
5770 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5771 retval.is_longjmp = bptype == bp_longjmp_resume;
5773 else
5774 this_action = BPSTAT_WHAT_SINGLE;
5775 break;
5776 case bp_step_resume:
5777 if (bs->stop)
5778 this_action = BPSTAT_WHAT_STEP_RESUME;
5779 else
5781 /* It is for the wrong frame. */
5782 this_action = BPSTAT_WHAT_SINGLE;
5784 break;
5785 case bp_hp_step_resume:
5786 if (bs->stop)
5787 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5788 else
5790 /* It is for the wrong frame. */
5791 this_action = BPSTAT_WHAT_SINGLE;
5793 break;
5794 case bp_watchpoint_scope:
5795 case bp_thread_event:
5796 case bp_overlay_event:
5797 case bp_longjmp_master:
5798 case bp_std_terminate_master:
5799 case bp_exception_master:
5800 this_action = BPSTAT_WHAT_SINGLE;
5801 break;
5802 case bp_catchpoint:
5803 if (bs->stop)
5805 if (bs->print)
5806 this_action = BPSTAT_WHAT_STOP_NOISY;
5807 else
5808 this_action = BPSTAT_WHAT_STOP_SILENT;
5810 else
5812 /* Some catchpoints are implemented with breakpoints.
5813 For those, we need to step over the breakpoint. */
5814 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
5815 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
5816 this_action = BPSTAT_WHAT_SINGLE;
5818 break;
5819 case bp_jit_event:
5820 this_action = BPSTAT_WHAT_SINGLE;
5821 break;
5822 case bp_call_dummy:
5823 /* Make sure the action is stop (silent or noisy),
5824 so infrun.c pops the dummy frame. */
5825 retval.call_dummy = STOP_STACK_DUMMY;
5826 this_action = BPSTAT_WHAT_STOP_SILENT;
5827 break;
5828 case bp_std_terminate:
5829 /* Make sure the action is stop (silent or noisy),
5830 so infrun.c pops the dummy frame. */
5831 retval.call_dummy = STOP_STD_TERMINATE;
5832 this_action = BPSTAT_WHAT_STOP_SILENT;
5833 break;
5834 case bp_tracepoint:
5835 case bp_fast_tracepoint:
5836 case bp_static_tracepoint:
5837 case bp_static_marker_tracepoint:
5838 /* Tracepoint hits should not be reported back to GDB, and
5839 if one got through somehow, it should have been filtered
5840 out already. */
5841 internal_error (__FILE__, __LINE__,
5842 _("bpstat_what: tracepoint encountered"));
5843 break;
5844 case bp_gnu_ifunc_resolver:
5845 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5846 this_action = BPSTAT_WHAT_SINGLE;
5847 break;
5848 case bp_gnu_ifunc_resolver_return:
5849 /* The breakpoint will be removed, execution will restart from the
5850 PC of the former breakpoint. */
5851 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5852 break;
5854 case bp_dprintf:
5855 if (bs->stop)
5856 this_action = BPSTAT_WHAT_STOP_SILENT;
5857 else
5858 this_action = BPSTAT_WHAT_SINGLE;
5859 break;
5861 default:
5862 internal_error (__FILE__, __LINE__,
5863 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5866 retval.main_action = std::max (retval.main_action, this_action);
5869 return retval;
5872 void
5873 bpstat_run_callbacks (bpstat *bs_head)
5875 bpstat *bs;
5877 for (bs = bs_head; bs != NULL; bs = bs->next)
5879 struct breakpoint *b = bs->breakpoint_at;
5881 if (b == NULL)
5882 continue;
5883 switch (b->type)
5885 case bp_jit_event:
5886 handle_jit_event (bs->bp_location_at->address);
5887 break;
5888 case bp_gnu_ifunc_resolver:
5889 gnu_ifunc_resolver_stop ((code_breakpoint *) b);
5890 break;
5891 case bp_gnu_ifunc_resolver_return:
5892 gnu_ifunc_resolver_return_stop ((code_breakpoint *) b);
5893 break;
5898 /* See breakpoint.h. */
5900 bool
5901 bpstat_should_step ()
5903 for (breakpoint *b : all_breakpoints ())
5904 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5905 return true;
5907 return false;
5910 /* See breakpoint.h. */
5912 bool
5913 bpstat_causes_stop (bpstat *bs)
5915 for (; bs != NULL; bs = bs->next)
5916 if (bs->stop)
5917 return true;
5919 return false;
5924 /* Compute a number of spaces suitable to indent the next line
5925 so it starts at the position corresponding to the table column
5926 named COL_NAME in the currently active table of UIOUT. */
5928 static int
5929 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5931 int i, total_width, width, align;
5932 const char *text;
5934 total_width = 0;
5935 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5937 if (strcmp (text, col_name) == 0)
5938 return total_width;
5940 total_width += width + 1;
5943 return 0;
5946 /* Determine if the locations of this breakpoint will have their conditions
5947 evaluated by the target, host or a mix of both. Returns the following:
5949 "host": Host evals condition.
5950 "host or target": Host or Target evals condition.
5951 "target": Target evals condition.
5954 static const char *
5955 bp_condition_evaluator (const breakpoint *b)
5957 char host_evals = 0;
5958 char target_evals = 0;
5960 if (!b)
5961 return NULL;
5963 if (!is_breakpoint (b))
5964 return NULL;
5966 if (gdb_evaluates_breakpoint_condition_p ()
5967 || !target_supports_evaluation_of_breakpoint_conditions ())
5968 return condition_evaluation_host;
5970 for (bp_location *bl : b->locations ())
5972 if (bl->cond_bytecode)
5973 target_evals++;
5974 else
5975 host_evals++;
5978 if (host_evals && target_evals)
5979 return condition_evaluation_both;
5980 else if (target_evals)
5981 return condition_evaluation_target;
5982 else
5983 return condition_evaluation_host;
5986 /* Determine the breakpoint location's condition evaluator. This is
5987 similar to bp_condition_evaluator, but for locations. */
5989 static const char *
5990 bp_location_condition_evaluator (struct bp_location *bl)
5992 if (bl && !is_breakpoint (bl->owner))
5993 return NULL;
5995 if (gdb_evaluates_breakpoint_condition_p ()
5996 || !target_supports_evaluation_of_breakpoint_conditions ())
5997 return condition_evaluation_host;
5999 if (bl && bl->cond_bytecode)
6000 return condition_evaluation_target;
6001 else
6002 return condition_evaluation_host;
6005 /* Print the LOC location out of the list of B->LOC locations. */
6007 static void
6008 print_breakpoint_location (const breakpoint *b,
6009 struct bp_location *loc)
6011 struct ui_out *uiout = current_uiout;
6013 scoped_restore_current_program_space restore_pspace;
6015 if (loc != NULL && loc->shlib_disabled)
6016 loc = NULL;
6018 if (loc != NULL)
6019 set_current_program_space (loc->pspace);
6021 if (b->display_canonical)
6022 uiout->field_string ("what", event_location_to_string (b->location.get ()));
6023 else if (loc && loc->symtab)
6025 const struct symbol *sym = loc->symbol;
6027 if (sym)
6029 uiout->text ("in ");
6030 uiout->field_string ("func", sym->print_name (),
6031 function_name_style.style ());
6032 uiout->text (" ");
6033 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6034 uiout->text ("at ");
6036 uiout->field_string ("file",
6037 symtab_to_filename_for_display (loc->symtab),
6038 file_name_style.style ());
6039 uiout->text (":");
6041 if (uiout->is_mi_like_p ())
6042 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6044 uiout->field_signed ("line", loc->line_number);
6046 else if (loc)
6048 string_file stb;
6050 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6051 demangle, "");
6052 uiout->field_stream ("at", stb);
6054 else
6056 uiout->field_string ("pending",
6057 event_location_to_string (b->location.get ()));
6058 /* If extra_string is available, it could be holding a condition
6059 or dprintf arguments. In either case, make sure it is printed,
6060 too, but only for non-MI streams. */
6061 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6063 if (b->type == bp_dprintf)
6064 uiout->text (",");
6065 else
6066 uiout->text (" ");
6067 uiout->text (b->extra_string.get ());
6071 if (loc && is_breakpoint (b)
6072 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6073 && bp_condition_evaluator (b) == condition_evaluation_both)
6075 uiout->text (" (");
6076 uiout->field_string ("evaluated-by",
6077 bp_location_condition_evaluator (loc));
6078 uiout->text (")");
6082 static const char *
6083 bptype_string (enum bptype type)
6085 struct ep_type_description
6087 enum bptype type;
6088 const char *description;
6090 static struct ep_type_description bptypes[] =
6092 {bp_none, "?deleted?"},
6093 {bp_breakpoint, "breakpoint"},
6094 {bp_hardware_breakpoint, "hw breakpoint"},
6095 {bp_single_step, "sw single-step"},
6096 {bp_until, "until"},
6097 {bp_finish, "finish"},
6098 {bp_watchpoint, "watchpoint"},
6099 {bp_hardware_watchpoint, "hw watchpoint"},
6100 {bp_read_watchpoint, "read watchpoint"},
6101 {bp_access_watchpoint, "acc watchpoint"},
6102 {bp_longjmp, "longjmp"},
6103 {bp_longjmp_resume, "longjmp resume"},
6104 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6105 {bp_exception, "exception"},
6106 {bp_exception_resume, "exception resume"},
6107 {bp_step_resume, "step resume"},
6108 {bp_hp_step_resume, "high-priority step resume"},
6109 {bp_watchpoint_scope, "watchpoint scope"},
6110 {bp_call_dummy, "call dummy"},
6111 {bp_std_terminate, "std::terminate"},
6112 {bp_shlib_event, "shlib events"},
6113 {bp_thread_event, "thread events"},
6114 {bp_overlay_event, "overlay events"},
6115 {bp_longjmp_master, "longjmp master"},
6116 {bp_std_terminate_master, "std::terminate master"},
6117 {bp_exception_master, "exception master"},
6118 {bp_catchpoint, "catchpoint"},
6119 {bp_tracepoint, "tracepoint"},
6120 {bp_fast_tracepoint, "fast tracepoint"},
6121 {bp_static_tracepoint, "static tracepoint"},
6122 {bp_static_marker_tracepoint, "static marker tracepoint"},
6123 {bp_dprintf, "dprintf"},
6124 {bp_jit_event, "jit events"},
6125 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6126 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6129 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6130 || ((int) type != bptypes[(int) type].type))
6131 internal_error (__FILE__, __LINE__,
6132 _("bptypes table does not describe type #%d."),
6133 (int) type);
6135 return bptypes[(int) type].description;
6138 /* For MI, output a field named 'thread-groups' with a list as the value.
6139 For CLI, prefix the list with the string 'inf'. */
6141 static void
6142 output_thread_groups (struct ui_out *uiout,
6143 const char *field_name,
6144 const std::vector<int> &inf_nums,
6145 int mi_only)
6147 int is_mi = uiout->is_mi_like_p ();
6149 /* For backward compatibility, don't display inferiors in CLI unless
6150 there are several. Always display them for MI. */
6151 if (!is_mi && mi_only)
6152 return;
6154 ui_out_emit_list list_emitter (uiout, field_name);
6156 for (size_t i = 0; i < inf_nums.size (); i++)
6158 if (is_mi)
6160 char mi_group[10];
6162 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6163 uiout->field_string (NULL, mi_group);
6165 else
6167 if (i == 0)
6168 uiout->text (" inf ");
6169 else
6170 uiout->text (", ");
6172 uiout->text (plongest (inf_nums[i]));
6177 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6178 instead of going via breakpoint_ops::print_one. This makes "maint
6179 info breakpoints" show the software breakpoint locations of
6180 catchpoints, which are considered internal implementation
6181 detail. Returns true if RAW_LOC is false and if the breakpoint's
6182 print_one method did something; false otherwise. */
6184 static bool
6185 print_one_breakpoint_location (struct breakpoint *b,
6186 struct bp_location *loc,
6187 int loc_number,
6188 struct bp_location **last_loc,
6189 int allflag, bool raw_loc)
6191 struct command_line *l;
6192 static char bpenables[] = "nynny";
6194 struct ui_out *uiout = current_uiout;
6195 int header_of_multiple = 0;
6196 int part_of_multiple = (loc != NULL);
6197 struct value_print_options opts;
6199 get_user_print_options (&opts);
6201 gdb_assert (!loc || loc_number != 0);
6202 /* See comment in print_one_breakpoint concerning treatment of
6203 breakpoints with single disabled location. */
6204 if (loc == NULL
6205 && (b->loc != NULL
6206 && (b->loc->next != NULL
6207 || !b->loc->enabled || b->loc->disabled_by_cond)))
6208 header_of_multiple = 1;
6209 if (loc == NULL)
6210 loc = b->loc;
6212 annotate_record ();
6214 /* 1 */
6215 annotate_field (0);
6216 if (part_of_multiple)
6217 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6218 else
6219 uiout->field_signed ("number", b->number);
6221 /* 2 */
6222 annotate_field (1);
6223 if (part_of_multiple)
6224 uiout->field_skip ("type");
6225 else
6226 uiout->field_string ("type", bptype_string (b->type));
6228 /* 3 */
6229 annotate_field (2);
6230 if (part_of_multiple)
6231 uiout->field_skip ("disp");
6232 else
6233 uiout->field_string ("disp", bpdisp_text (b->disposition));
6235 /* 4 */
6236 annotate_field (3);
6237 if (part_of_multiple)
6239 /* For locations that are disabled because of an invalid
6240 condition, display "N*" on the CLI, where "*" refers to a
6241 footnote below the table. For MI, simply display a "N"
6242 without a footnote. On the CLI, for enabled locations whose
6243 breakpoint is disabled, display "y-". */
6244 auto get_enable_state = [uiout, loc] () -> const char *
6246 if (uiout->is_mi_like_p ())
6248 if (loc->disabled_by_cond)
6249 return "N";
6250 else if (!loc->enabled)
6251 return "n";
6252 else
6253 return "y";
6255 else
6257 if (loc->disabled_by_cond)
6258 return "N*";
6259 else if (!loc->enabled)
6260 return "n";
6261 else if (!breakpoint_enabled (loc->owner))
6262 return "y-";
6263 else
6264 return "y";
6267 uiout->field_string ("enabled", get_enable_state ());
6269 else
6270 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6272 /* 5 and 6 */
6273 bool result = false;
6274 if (!raw_loc && b->print_one (last_loc))
6275 result = true;
6276 else
6278 if (is_watchpoint (b))
6280 struct watchpoint *w = (struct watchpoint *) b;
6282 /* Field 4, the address, is omitted (which makes the columns
6283 not line up too nicely with the headers, but the effect
6284 is relatively readable). */
6285 if (opts.addressprint)
6286 uiout->field_skip ("addr");
6287 annotate_field (5);
6288 uiout->field_string ("what", w->exp_string.get ());
6290 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6291 || is_ada_exception_catchpoint (b))
6293 if (opts.addressprint)
6295 annotate_field (4);
6296 if (header_of_multiple)
6297 uiout->field_string ("addr", "<MULTIPLE>",
6298 metadata_style.style ());
6299 else if (b->loc == NULL || loc->shlib_disabled)
6300 uiout->field_string ("addr", "<PENDING>",
6301 metadata_style.style ());
6302 else
6303 uiout->field_core_addr ("addr",
6304 loc->gdbarch, loc->address);
6306 annotate_field (5);
6307 if (!header_of_multiple)
6308 print_breakpoint_location (b, loc);
6309 if (b->loc)
6310 *last_loc = b->loc;
6314 if (loc != NULL && !header_of_multiple)
6316 std::vector<int> inf_nums;
6317 int mi_only = 1;
6319 for (inferior *inf : all_inferiors ())
6321 if (inf->pspace == loc->pspace)
6322 inf_nums.push_back (inf->num);
6325 /* For backward compatibility, don't display inferiors in CLI unless
6326 there are several. Always display for MI. */
6327 if (allflag
6328 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6329 && (program_spaces.size () > 1
6330 || number_of_inferiors () > 1)
6331 /* LOC is for existing B, it cannot be in
6332 moribund_locations and thus having NULL OWNER. */
6333 && loc->owner->type != bp_catchpoint))
6334 mi_only = 0;
6335 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6338 if (!part_of_multiple)
6340 if (b->thread != -1)
6342 /* FIXME: This seems to be redundant and lost here; see the
6343 "stop only in" line a little further down. */
6344 uiout->text (" thread ");
6345 uiout->field_signed ("thread", b->thread);
6347 else if (b->task != 0)
6349 uiout->text (" task ");
6350 uiout->field_signed ("task", b->task);
6354 uiout->text ("\n");
6356 if (!part_of_multiple)
6357 b->print_one_detail (uiout);
6359 if (part_of_multiple && frame_id_p (b->frame_id))
6361 annotate_field (6);
6362 uiout->text ("\tstop only in stack frame at ");
6363 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6364 the frame ID. */
6365 uiout->field_core_addr ("frame",
6366 b->gdbarch, b->frame_id.stack_addr);
6367 uiout->text ("\n");
6370 if (!part_of_multiple && b->cond_string)
6372 annotate_field (7);
6373 if (is_tracepoint (b))
6374 uiout->text ("\ttrace only if ");
6375 else
6376 uiout->text ("\tstop only if ");
6377 uiout->field_string ("cond", b->cond_string.get ());
6379 /* Print whether the target is doing the breakpoint's condition
6380 evaluation. If GDB is doing the evaluation, don't print anything. */
6381 if (is_breakpoint (b)
6382 && breakpoint_condition_evaluation_mode ()
6383 == condition_evaluation_target)
6385 uiout->message (" (%pF evals)",
6386 string_field ("evaluated-by",
6387 bp_condition_evaluator (b)));
6389 uiout->text ("\n");
6392 if (!part_of_multiple && b->thread != -1)
6394 /* FIXME should make an annotation for this. */
6395 uiout->text ("\tstop only in thread ");
6396 if (uiout->is_mi_like_p ())
6397 uiout->field_signed ("thread", b->thread);
6398 else
6400 struct thread_info *thr = find_thread_global_id (b->thread);
6402 uiout->field_string ("thread", print_thread_id (thr));
6404 uiout->text ("\n");
6407 if (!part_of_multiple)
6409 if (b->hit_count)
6411 /* FIXME should make an annotation for this. */
6412 if (is_catchpoint (b))
6413 uiout->text ("\tcatchpoint");
6414 else if (is_tracepoint (b))
6415 uiout->text ("\ttracepoint");
6416 else
6417 uiout->text ("\tbreakpoint");
6418 uiout->text (" already hit ");
6419 uiout->field_signed ("times", b->hit_count);
6420 if (b->hit_count == 1)
6421 uiout->text (" time\n");
6422 else
6423 uiout->text (" times\n");
6425 else
6427 /* Output the count also if it is zero, but only if this is mi. */
6428 if (uiout->is_mi_like_p ())
6429 uiout->field_signed ("times", b->hit_count);
6433 if (!part_of_multiple && b->ignore_count)
6435 annotate_field (8);
6436 uiout->message ("\tignore next %pF hits\n",
6437 signed_field ("ignore", b->ignore_count));
6440 /* Note that an enable count of 1 corresponds to "enable once"
6441 behavior, which is reported by the combination of enablement and
6442 disposition, so we don't need to mention it here. */
6443 if (!part_of_multiple && b->enable_count > 1)
6445 annotate_field (8);
6446 uiout->text ("\tdisable after ");
6447 /* Tweak the wording to clarify that ignore and enable counts
6448 are distinct, and have additive effect. */
6449 if (b->ignore_count)
6450 uiout->text ("additional ");
6451 else
6452 uiout->text ("next ");
6453 uiout->field_signed ("enable", b->enable_count);
6454 uiout->text (" hits\n");
6457 if (!part_of_multiple && is_tracepoint (b))
6459 struct tracepoint *tp = (struct tracepoint *) b;
6461 if (tp->traceframe_usage)
6463 uiout->text ("\ttrace buffer usage ");
6464 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6465 uiout->text (" bytes\n");
6469 l = b->commands ? b->commands.get () : NULL;
6470 if (!part_of_multiple && l)
6472 annotate_field (9);
6473 ui_out_emit_tuple tuple_emitter (uiout, "script");
6474 print_command_lines (uiout, l, 4);
6477 if (is_tracepoint (b))
6479 struct tracepoint *t = (struct tracepoint *) b;
6481 if (!part_of_multiple && t->pass_count)
6483 annotate_field (10);
6484 uiout->text ("\tpass count ");
6485 uiout->field_signed ("pass", t->pass_count);
6486 uiout->text (" \n");
6489 /* Don't display it when tracepoint or tracepoint location is
6490 pending. */
6491 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6493 annotate_field (11);
6495 if (uiout->is_mi_like_p ())
6496 uiout->field_string ("installed",
6497 loc->inserted ? "y" : "n");
6498 else
6500 if (loc->inserted)
6501 uiout->text ("\t");
6502 else
6503 uiout->text ("\tnot ");
6504 uiout->text ("installed on target\n");
6509 if (uiout->is_mi_like_p () && !part_of_multiple)
6511 if (is_watchpoint (b))
6513 struct watchpoint *w = (struct watchpoint *) b;
6515 uiout->field_string ("original-location", w->exp_string.get ());
6517 else if (b->location != NULL
6518 && event_location_to_string (b->location.get ()) != NULL)
6519 uiout->field_string ("original-location",
6520 event_location_to_string (b->location.get ()));
6523 return result;
6526 /* See breakpoint.h. */
6528 bool fix_multi_location_breakpoint_output_globally = false;
6530 static void
6531 print_one_breakpoint (struct breakpoint *b,
6532 struct bp_location **last_loc,
6533 int allflag)
6535 struct ui_out *uiout = current_uiout;
6536 bool use_fixed_output
6537 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6538 || fix_multi_location_breakpoint_output_globally);
6540 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6541 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6542 allflag, false);
6544 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6545 are outside. */
6546 if (!use_fixed_output)
6547 bkpt_tuple_emitter.reset ();
6549 /* If this breakpoint has custom print function,
6550 it's already printed. Otherwise, print individual
6551 locations, if any. */
6552 if (!printed || allflag)
6554 /* If breakpoint has a single location that is disabled, we
6555 print it as if it had several locations, since otherwise it's
6556 hard to represent "breakpoint enabled, location disabled"
6557 situation.
6559 Note that while hardware watchpoints have several locations
6560 internally, that's not a property exposed to users.
6562 Likewise, while catchpoints may be implemented with
6563 breakpoints (e.g., catch throw), that's not a property
6564 exposed to users. We do however display the internal
6565 breakpoint locations with "maint info breakpoints". */
6566 if (!is_hardware_watchpoint (b)
6567 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6568 || is_ada_exception_catchpoint (b))
6569 && (allflag
6570 || (b->loc && (b->loc->next
6571 || !b->loc->enabled
6572 || b->loc->disabled_by_cond))))
6574 gdb::optional<ui_out_emit_list> locations_list;
6576 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6577 MI record. For later versions, place breakpoint locations in a
6578 list. */
6579 if (uiout->is_mi_like_p () && use_fixed_output)
6580 locations_list.emplace (uiout, "locations");
6582 int n = 1;
6583 for (bp_location *loc : b->locations ())
6585 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6586 print_one_breakpoint_location (b, loc, n, last_loc,
6587 allflag, allflag);
6588 n++;
6594 static int
6595 breakpoint_address_bits (struct breakpoint *b)
6597 int print_address_bits = 0;
6599 for (bp_location *loc : b->locations ())
6601 if (!bl_address_is_meaningful (loc))
6602 continue;
6604 int addr_bit = gdbarch_addr_bit (loc->gdbarch);
6605 if (addr_bit > print_address_bits)
6606 print_address_bits = addr_bit;
6609 return print_address_bits;
6612 /* See breakpoint.h. */
6614 void
6615 print_breakpoint (breakpoint *b)
6617 struct bp_location *dummy_loc = NULL;
6618 print_one_breakpoint (b, &dummy_loc, 0);
6621 /* Return true if this breakpoint was set by the user, false if it is
6622 internal or momentary. */
6625 user_breakpoint_p (struct breakpoint *b)
6627 return b->number > 0;
6630 /* See breakpoint.h. */
6633 pending_breakpoint_p (struct breakpoint *b)
6635 return b->loc == NULL;
6638 /* Print information on breakpoints (including watchpoints and tracepoints).
6640 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6641 understood by number_or_range_parser. Only breakpoints included in this
6642 list are then printed.
6644 If SHOW_INTERNAL is true, print internal breakpoints.
6646 If FILTER is non-NULL, call it on each breakpoint and only include the
6647 ones for which it returns true.
6649 Return the total number of breakpoints listed. */
6651 static int
6652 breakpoint_1 (const char *bp_num_list, bool show_internal,
6653 bool (*filter) (const struct breakpoint *))
6655 struct bp_location *last_loc = NULL;
6656 int nr_printable_breakpoints;
6657 struct value_print_options opts;
6658 int print_address_bits = 0;
6659 int print_type_col_width = 14;
6660 struct ui_out *uiout = current_uiout;
6661 bool has_disabled_by_cond_location = false;
6663 get_user_print_options (&opts);
6665 /* Compute the number of rows in the table, as well as the size
6666 required for address fields. */
6667 nr_printable_breakpoints = 0;
6668 for (breakpoint *b : all_breakpoints ())
6670 /* If we have a filter, only list the breakpoints it accepts. */
6671 if (filter && !filter (b))
6672 continue;
6674 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6675 accept. Skip the others. */
6676 if (bp_num_list != NULL && *bp_num_list != '\0')
6678 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6679 continue;
6680 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6681 continue;
6684 if (show_internal || user_breakpoint_p (b))
6686 int addr_bit, type_len;
6688 addr_bit = breakpoint_address_bits (b);
6689 if (addr_bit > print_address_bits)
6690 print_address_bits = addr_bit;
6692 type_len = strlen (bptype_string (b->type));
6693 if (type_len > print_type_col_width)
6694 print_type_col_width = type_len;
6696 nr_printable_breakpoints++;
6701 ui_out_emit_table table_emitter (uiout,
6702 opts.addressprint ? 6 : 5,
6703 nr_printable_breakpoints,
6704 "BreakpointTable");
6706 if (nr_printable_breakpoints > 0)
6707 annotate_breakpoints_headers ();
6708 if (nr_printable_breakpoints > 0)
6709 annotate_field (0);
6710 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6711 if (nr_printable_breakpoints > 0)
6712 annotate_field (1);
6713 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6714 if (nr_printable_breakpoints > 0)
6715 annotate_field (2);
6716 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6717 if (nr_printable_breakpoints > 0)
6718 annotate_field (3);
6719 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6720 if (opts.addressprint)
6722 if (nr_printable_breakpoints > 0)
6723 annotate_field (4);
6724 if (print_address_bits <= 32)
6725 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6726 else
6727 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6729 if (nr_printable_breakpoints > 0)
6730 annotate_field (5);
6731 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6732 uiout->table_body ();
6733 if (nr_printable_breakpoints > 0)
6734 annotate_breakpoints_table ();
6736 for (breakpoint *b : all_breakpoints ())
6738 QUIT;
6739 /* If we have a filter, only list the breakpoints it accepts. */
6740 if (filter && !filter (b))
6741 continue;
6743 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6744 accept. Skip the others. */
6746 if (bp_num_list != NULL && *bp_num_list != '\0')
6748 if (show_internal) /* maintenance info breakpoint */
6750 if (parse_and_eval_long (bp_num_list) != b->number)
6751 continue;
6753 else /* all others */
6755 if (!number_is_in_list (bp_num_list, b->number))
6756 continue;
6759 /* We only print out user settable breakpoints unless the
6760 show_internal is set. */
6761 if (show_internal || user_breakpoint_p (b))
6763 print_one_breakpoint (b, &last_loc, show_internal);
6764 for (bp_location *loc : b->locations ())
6765 if (loc->disabled_by_cond)
6766 has_disabled_by_cond_location = true;
6771 if (nr_printable_breakpoints == 0)
6773 /* If there's a filter, let the caller decide how to report
6774 empty list. */
6775 if (!filter)
6777 if (bp_num_list == NULL || *bp_num_list == '\0')
6778 uiout->message ("No breakpoints or watchpoints.\n");
6779 else
6780 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6781 bp_num_list);
6784 else
6786 if (last_loc && !server_command)
6787 set_next_address (last_loc->gdbarch, last_loc->address);
6789 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6790 uiout->message (_("(*): Breakpoint condition is invalid at this "
6791 "location.\n"));
6794 /* FIXME? Should this be moved up so that it is only called when
6795 there have been breakpoints? */
6796 annotate_breakpoints_table_end ();
6798 return nr_printable_breakpoints;
6801 /* Display the value of default-collect in a way that is generally
6802 compatible with the breakpoint list. */
6804 static void
6805 default_collect_info (void)
6807 struct ui_out *uiout = current_uiout;
6809 /* If it has no value (which is frequently the case), say nothing; a
6810 message like "No default-collect." gets in user's face when it's
6811 not wanted. */
6812 if (default_collect.empty ())
6813 return;
6815 /* The following phrase lines up nicely with per-tracepoint collect
6816 actions. */
6817 uiout->text ("default collect ");
6818 uiout->field_string ("default-collect", default_collect);
6819 uiout->text (" \n");
6822 static void
6823 info_breakpoints_command (const char *args, int from_tty)
6825 breakpoint_1 (args, false, NULL);
6827 default_collect_info ();
6830 static void
6831 info_watchpoints_command (const char *args, int from_tty)
6833 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6834 struct ui_out *uiout = current_uiout;
6836 if (num_printed == 0)
6838 if (args == NULL || *args == '\0')
6839 uiout->message ("No watchpoints.\n");
6840 else
6841 uiout->message ("No watchpoint matching '%s'.\n", args);
6845 static void
6846 maintenance_info_breakpoints (const char *args, int from_tty)
6848 breakpoint_1 (args, true, NULL);
6850 default_collect_info ();
6853 static int
6854 breakpoint_has_pc (struct breakpoint *b,
6855 struct program_space *pspace,
6856 CORE_ADDR pc, struct obj_section *section)
6858 for (bp_location *bl : b->locations ())
6860 if (bl->pspace == pspace
6861 && bl->address == pc
6862 && (!overlay_debugging || bl->section == section))
6863 return 1;
6865 return 0;
6868 /* See breakpoint.h. */
6870 void
6871 describe_other_breakpoints (struct gdbarch *gdbarch,
6872 struct program_space *pspace, CORE_ADDR pc,
6873 struct obj_section *section, int thread)
6875 int others = 0;
6877 for (breakpoint *b : all_breakpoints ())
6878 others += (user_breakpoint_p (b)
6879 && breakpoint_has_pc (b, pspace, pc, section));
6881 if (others > 0)
6883 if (others == 1)
6884 gdb_printf (_("Note: breakpoint "));
6885 else /* if (others == ???) */
6886 gdb_printf (_("Note: breakpoints "));
6887 for (breakpoint *b : all_breakpoints ())
6888 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6890 others--;
6891 gdb_printf ("%d", b->number);
6892 if (b->thread == -1 && thread != -1)
6893 gdb_printf (" (all threads)");
6894 else if (b->thread != -1)
6895 gdb_printf (" (thread %d)", b->thread);
6896 gdb_printf ("%s%s ",
6897 ((b->enable_state == bp_disabled
6898 || b->enable_state == bp_call_disabled)
6899 ? " (disabled)"
6900 : ""),
6901 (others > 1) ? ","
6902 : ((others == 1) ? " and" : ""));
6904 current_uiout->message (_("also set at pc %ps.\n"),
6905 styled_string (address_style.style (),
6906 paddress (gdbarch, pc)));
6911 /* Return true iff it is meaningful to use the address member of LOC.
6912 For some breakpoint types, the locations' address members are
6913 irrelevant and it makes no sense to attempt to compare them to
6914 other addresses (or use them for any other purpose either).
6916 More specifically, software watchpoints and catchpoints that are
6917 not backed by breakpoints always have a zero valued location
6918 address and we don't want to mark breakpoints of any of these types
6919 to be a duplicate of an actual breakpoint location at address
6920 zero. */
6922 static bool
6923 bl_address_is_meaningful (bp_location *loc)
6925 return loc->loc_type != bp_loc_other;
6928 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6929 true if LOC1 and LOC2 represent the same watchpoint location. */
6931 static int
6932 watchpoint_locations_match (struct bp_location *loc1,
6933 struct bp_location *loc2)
6935 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6936 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6938 /* Both of them must exist. */
6939 gdb_assert (w1 != NULL);
6940 gdb_assert (w2 != NULL);
6942 /* If the target can evaluate the condition expression in hardware,
6943 then we we need to insert both watchpoints even if they are at
6944 the same place. Otherwise the watchpoint will only trigger when
6945 the condition of whichever watchpoint was inserted evaluates to
6946 true, not giving a chance for GDB to check the condition of the
6947 other watchpoint. */
6948 if ((w1->cond_exp
6949 && target_can_accel_watchpoint_condition (loc1->address,
6950 loc1->length,
6951 loc1->watchpoint_type,
6952 w1->cond_exp.get ()))
6953 || (w2->cond_exp
6954 && target_can_accel_watchpoint_condition (loc2->address,
6955 loc2->length,
6956 loc2->watchpoint_type,
6957 w2->cond_exp.get ())))
6958 return 0;
6960 /* Note that this checks the owner's type, not the location's. In
6961 case the target does not support read watchpoints, but does
6962 support access watchpoints, we'll have bp_read_watchpoint
6963 watchpoints with hw_access locations. Those should be considered
6964 duplicates of hw_read locations. The hw_read locations will
6965 become hw_access locations later. */
6966 return (loc1->owner->type == loc2->owner->type
6967 && loc1->pspace->aspace == loc2->pspace->aspace
6968 && loc1->address == loc2->address
6969 && loc1->length == loc2->length);
6972 /* See breakpoint.h. */
6975 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6976 const address_space *aspace2, CORE_ADDR addr2)
6978 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6979 || aspace1 == aspace2)
6980 && addr1 == addr2);
6983 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6984 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6985 matches ASPACE2. On targets that have global breakpoints, the address
6986 space doesn't really matter. */
6988 static int
6989 breakpoint_address_match_range (const address_space *aspace1,
6990 CORE_ADDR addr1,
6991 int len1, const address_space *aspace2,
6992 CORE_ADDR addr2)
6994 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6995 || aspace1 == aspace2)
6996 && addr2 >= addr1 && addr2 < addr1 + len1);
6999 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7000 a ranged breakpoint. In most targets, a match happens only if ASPACE
7001 matches the breakpoint's address space. On targets that have global
7002 breakpoints, the address space doesn't really matter. */
7004 static int
7005 breakpoint_location_address_match (struct bp_location *bl,
7006 const address_space *aspace,
7007 CORE_ADDR addr)
7009 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7010 aspace, addr)
7011 || (bl->length
7012 && breakpoint_address_match_range (bl->pspace->aspace,
7013 bl->address, bl->length,
7014 aspace, addr)));
7017 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7018 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7019 match happens only if ASPACE matches the breakpoint's address
7020 space. On targets that have global breakpoints, the address space
7021 doesn't really matter. */
7023 static int
7024 breakpoint_location_address_range_overlap (struct bp_location *bl,
7025 const address_space *aspace,
7026 CORE_ADDR addr, int len)
7028 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7029 || bl->pspace->aspace == aspace)
7031 int bl_len = bl->length != 0 ? bl->length : 1;
7033 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7034 return 1;
7036 return 0;
7039 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7040 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7041 true, otherwise returns false. */
7043 static int
7044 tracepoint_locations_match (struct bp_location *loc1,
7045 struct bp_location *loc2)
7047 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7048 /* Since tracepoint locations are never duplicated with others', tracepoint
7049 locations at the same address of different tracepoints are regarded as
7050 different locations. */
7051 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7052 else
7053 return 0;
7056 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7057 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7058 the same location. If SW_HW_BPS_MATCH is true, then software
7059 breakpoint locations and hardware breakpoint locations match,
7060 otherwise they don't. */
7062 static int
7063 breakpoint_locations_match (struct bp_location *loc1,
7064 struct bp_location *loc2,
7065 bool sw_hw_bps_match)
7067 int hw_point1, hw_point2;
7069 /* Both of them must not be in moribund_locations. */
7070 gdb_assert (loc1->owner != NULL);
7071 gdb_assert (loc2->owner != NULL);
7073 hw_point1 = is_hardware_watchpoint (loc1->owner);
7074 hw_point2 = is_hardware_watchpoint (loc2->owner);
7076 if (hw_point1 != hw_point2)
7077 return 0;
7078 else if (hw_point1)
7079 return watchpoint_locations_match (loc1, loc2);
7080 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7081 return tracepoint_locations_match (loc1, loc2);
7082 else
7083 /* We compare bp_location.length in order to cover ranged
7084 breakpoints. Keep this in sync with
7085 bp_location_is_less_than. */
7086 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7087 loc2->pspace->aspace, loc2->address)
7088 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7089 && loc1->length == loc2->length);
7092 static void
7093 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7094 int bnum, int have_bnum)
7096 /* The longest string possibly returned by hex_string_custom
7097 is 50 chars. These must be at least that big for safety. */
7098 char astr1[64];
7099 char astr2[64];
7101 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7102 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7103 if (have_bnum)
7104 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7105 bnum, astr1, astr2);
7106 else
7107 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7110 /* Adjust a breakpoint's address to account for architectural
7111 constraints on breakpoint placement. Return the adjusted address.
7112 Note: Very few targets require this kind of adjustment. For most
7113 targets, this function is simply the identity function. */
7115 static CORE_ADDR
7116 adjust_breakpoint_address (struct gdbarch *gdbarch,
7117 CORE_ADDR bpaddr, enum bptype bptype)
7119 if (bptype == bp_watchpoint
7120 || bptype == bp_hardware_watchpoint
7121 || bptype == bp_read_watchpoint
7122 || bptype == bp_access_watchpoint
7123 || bptype == bp_catchpoint)
7125 /* Watchpoints and the various bp_catch_* eventpoints should not
7126 have their addresses modified. */
7127 return bpaddr;
7129 else if (bptype == bp_single_step)
7131 /* Single-step breakpoints should not have their addresses
7132 modified. If there's any architectural constrain that
7133 applies to this address, then it should have already been
7134 taken into account when the breakpoint was created in the
7135 first place. If we didn't do this, stepping through e.g.,
7136 Thumb-2 IT blocks would break. */
7137 return bpaddr;
7139 else
7141 CORE_ADDR adjusted_bpaddr = bpaddr;
7143 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7145 /* Some targets have architectural constraints on the placement
7146 of breakpoint instructions. Obtain the adjusted address. */
7147 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7150 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7152 /* An adjusted breakpoint address can significantly alter
7153 a user's expectations. Print a warning if an adjustment
7154 is required. */
7155 if (adjusted_bpaddr != bpaddr)
7156 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7158 return adjusted_bpaddr;
7162 static bp_loc_type
7163 bp_location_from_bp_type (bptype type)
7165 switch (type)
7167 case bp_breakpoint:
7168 case bp_single_step:
7169 case bp_until:
7170 case bp_finish:
7171 case bp_longjmp:
7172 case bp_longjmp_resume:
7173 case bp_longjmp_call_dummy:
7174 case bp_exception:
7175 case bp_exception_resume:
7176 case bp_step_resume:
7177 case bp_hp_step_resume:
7178 case bp_watchpoint_scope:
7179 case bp_call_dummy:
7180 case bp_std_terminate:
7181 case bp_shlib_event:
7182 case bp_thread_event:
7183 case bp_overlay_event:
7184 case bp_jit_event:
7185 case bp_longjmp_master:
7186 case bp_std_terminate_master:
7187 case bp_exception_master:
7188 case bp_gnu_ifunc_resolver:
7189 case bp_gnu_ifunc_resolver_return:
7190 case bp_dprintf:
7191 return bp_loc_software_breakpoint;
7192 case bp_hardware_breakpoint:
7193 return bp_loc_hardware_breakpoint;
7194 case bp_hardware_watchpoint:
7195 case bp_read_watchpoint:
7196 case bp_access_watchpoint:
7197 return bp_loc_hardware_watchpoint;
7198 case bp_watchpoint:
7199 return bp_loc_software_watchpoint;
7200 case bp_catchpoint:
7201 case bp_tracepoint:
7202 case bp_fast_tracepoint:
7203 case bp_static_tracepoint:
7204 case bp_static_marker_tracepoint:
7205 return bp_loc_other;
7206 default:
7207 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7211 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7213 this->owner = owner;
7214 this->cond_bytecode = NULL;
7215 this->shlib_disabled = 0;
7216 this->enabled = 1;
7217 this->disabled_by_cond = false;
7219 this->loc_type = type;
7221 if (this->loc_type == bp_loc_software_breakpoint
7222 || this->loc_type == bp_loc_hardware_breakpoint)
7223 mark_breakpoint_location_modified (this);
7225 incref ();
7228 bp_location::bp_location (breakpoint *owner)
7229 : bp_location::bp_location (owner,
7230 bp_location_from_bp_type (owner->type))
7234 /* Decrement reference count. If the reference count reaches 0,
7235 destroy the bp_location. Sets *BLP to NULL. */
7237 static void
7238 decref_bp_location (struct bp_location **blp)
7240 bp_location_ref_policy::decref (*blp);
7241 *blp = NULL;
7244 /* Add breakpoint B at the end of the global breakpoint chain. */
7246 static breakpoint *
7247 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7249 struct breakpoint *b1;
7250 struct breakpoint *result = b.get ();
7252 /* Add this breakpoint to the end of the chain so that a list of
7253 breakpoints will come out in order of increasing numbers. */
7255 b1 = breakpoint_chain;
7256 if (b1 == 0)
7257 breakpoint_chain = b.release ();
7258 else
7260 while (b1->next)
7261 b1 = b1->next;
7262 b1->next = b.release ();
7265 return result;
7268 /* Initialize loc->function_name. */
7270 static void
7271 set_breakpoint_location_function (struct bp_location *loc)
7273 gdb_assert (loc->owner != NULL);
7275 if (loc->owner->type == bp_breakpoint
7276 || loc->owner->type == bp_hardware_breakpoint
7277 || is_tracepoint (loc->owner))
7279 const char *function_name;
7281 if (loc->msymbol != NULL
7282 && (loc->msymbol->type () == mst_text_gnu_ifunc
7283 || loc->msymbol->type () == mst_data_gnu_ifunc))
7285 struct breakpoint *b = loc->owner;
7287 function_name = loc->msymbol->linkage_name ();
7289 if (b->type == bp_breakpoint && b->loc == loc
7290 && loc->next == NULL && b->related_breakpoint == b)
7292 /* Create only the whole new breakpoint of this type but do not
7293 mess more complicated breakpoints with multiple locations. */
7294 b->type = bp_gnu_ifunc_resolver;
7295 /* Remember the resolver's address for use by the return
7296 breakpoint. */
7297 loc->related_address = loc->address;
7300 else
7301 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7303 if (function_name)
7304 loc->function_name = make_unique_xstrdup (function_name);
7308 /* Attempt to determine architecture of location identified by SAL. */
7309 struct gdbarch *
7310 get_sal_arch (struct symtab_and_line sal)
7312 if (sal.section)
7313 return sal.section->objfile->arch ();
7314 if (sal.symtab)
7315 return sal.symtab->compunit ()->objfile ()->arch ();
7317 return NULL;
7320 /* Call this routine when stepping and nexting to enable a breakpoint
7321 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7322 initiated the operation. */
7324 void
7325 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7327 int thread = tp->global_num;
7329 /* To avoid having to rescan all objfile symbols at every step,
7330 we maintain a list of continually-inserted but always disabled
7331 longjmp "master" breakpoints. Here, we simply create momentary
7332 clones of those and enable them for the requested thread. */
7333 for (breakpoint *b : all_breakpoints_safe ())
7334 if (b->pspace == current_program_space
7335 && (b->type == bp_longjmp_master
7336 || b->type == bp_exception_master))
7338 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7339 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7340 after their removal. */
7341 momentary_breakpoint_from_master (b, type, 1, thread);
7344 tp->initiating_frame = frame;
7347 /* Delete all longjmp breakpoints from THREAD. */
7348 void
7349 delete_longjmp_breakpoint (int thread)
7351 for (breakpoint *b : all_breakpoints_safe ())
7352 if (b->type == bp_longjmp || b->type == bp_exception)
7354 if (b->thread == thread)
7355 delete_breakpoint (b);
7359 void
7360 delete_longjmp_breakpoint_at_next_stop (int thread)
7362 for (breakpoint *b : all_breakpoints_safe ())
7363 if (b->type == bp_longjmp || b->type == bp_exception)
7365 if (b->thread == thread)
7366 b->disposition = disp_del_at_next_stop;
7370 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7371 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7372 pointer to any of them. Return NULL if this system cannot place longjmp
7373 breakpoints. */
7375 struct breakpoint *
7376 set_longjmp_breakpoint_for_call_dummy (void)
7378 breakpoint *retval = nullptr;
7380 for (breakpoint *b : all_breakpoints ())
7381 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7383 int thread = inferior_thread ()->global_num;
7384 breakpoint *new_b
7385 = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7386 1, thread);
7388 /* Link NEW_B into the chain of RETVAL breakpoints. */
7390 gdb_assert (new_b->related_breakpoint == new_b);
7391 if (retval == NULL)
7392 retval = new_b;
7393 new_b->related_breakpoint = retval;
7394 while (retval->related_breakpoint != new_b->related_breakpoint)
7395 retval = retval->related_breakpoint;
7396 retval->related_breakpoint = new_b;
7399 return retval;
7402 /* Verify all existing dummy frames and their associated breakpoints for
7403 TP. Remove those which can no longer be found in the current frame
7404 stack.
7406 If the unwind fails then there is not sufficient information to discard
7407 dummy frames. In this case, elide the clean up and the dummy frames will
7408 be cleaned up next time this function is called from a location where
7409 unwinding is possible. */
7411 void
7412 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7414 struct breakpoint *b, *b_tmp;
7416 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7417 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7419 struct breakpoint *dummy_b = b->related_breakpoint;
7421 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7422 chained off b->related_breakpoint. */
7423 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7424 dummy_b = dummy_b->related_breakpoint;
7426 /* If there was no bp_call_dummy breakpoint then there's nothing
7427 more to do. Or, if the dummy frame associated with the
7428 bp_call_dummy is still on the stack then we need to leave this
7429 bp_call_dummy in place. */
7430 if (dummy_b->type != bp_call_dummy
7431 || frame_find_by_id (dummy_b->frame_id) != NULL)
7432 continue;
7434 /* We didn't find the dummy frame on the stack, this could be
7435 because we have longjmp'd to a stack frame that is previous to
7436 the dummy frame, or it could be because the stack unwind is
7437 broken at some point between the longjmp frame and the dummy
7438 frame.
7440 Next we figure out why the stack unwind stopped. If it looks
7441 like the unwind is complete then we assume the dummy frame has
7442 been jumped over, however, if the unwind stopped for an
7443 unexpected reason then we assume the stack unwind is currently
7444 broken, and that we will (eventually) return to the dummy
7445 frame.
7447 It might be tempting to consider using frame_id_inner here, but
7448 that is not safe. There is no guarantee that the stack frames
7449 we are looking at here are even on the same stack as the
7450 original dummy frame, hence frame_id_inner can't be used. See
7451 the comments on frame_id_inner for more details. */
7452 bool unwind_finished_unexpectedly = false;
7453 for (struct frame_info *fi = get_current_frame (); fi != nullptr; )
7455 struct frame_info *prev = get_prev_frame (fi);
7456 if (prev == nullptr)
7458 /* FI is the last stack frame. Why did this frame not
7459 unwind further? */
7460 auto stop_reason = get_frame_unwind_stop_reason (fi);
7461 if (stop_reason != UNWIND_NO_REASON
7462 && stop_reason != UNWIND_OUTERMOST)
7463 unwind_finished_unexpectedly = true;
7465 fi = prev;
7467 if (unwind_finished_unexpectedly)
7468 continue;
7470 dummy_frame_discard (dummy_b->frame_id, tp);
7472 while (b->related_breakpoint != b)
7474 if (b_tmp == b->related_breakpoint)
7475 b_tmp = b->related_breakpoint->next;
7476 delete_breakpoint (b->related_breakpoint);
7478 delete_breakpoint (b);
7482 void
7483 enable_overlay_breakpoints (void)
7485 for (breakpoint *b : all_breakpoints ())
7486 if (b->type == bp_overlay_event)
7488 b->enable_state = bp_enabled;
7489 update_global_location_list (UGLL_MAY_INSERT);
7490 overlay_events_enabled = 1;
7494 void
7495 disable_overlay_breakpoints (void)
7497 for (breakpoint *b : all_breakpoints ())
7498 if (b->type == bp_overlay_event)
7500 b->enable_state = bp_disabled;
7501 update_global_location_list (UGLL_DONT_INSERT);
7502 overlay_events_enabled = 0;
7506 /* Set an active std::terminate breakpoint for each std::terminate
7507 master breakpoint. */
7508 void
7509 set_std_terminate_breakpoint (void)
7511 for (breakpoint *b : all_breakpoints_safe ())
7512 if (b->pspace == current_program_space
7513 && b->type == bp_std_terminate_master)
7515 momentary_breakpoint_from_master (b, bp_std_terminate, 1,
7516 inferior_thread ()->global_num);
7520 /* Delete all the std::terminate breakpoints. */
7521 void
7522 delete_std_terminate_breakpoint (void)
7524 for (breakpoint *b : all_breakpoints_safe ())
7525 if (b->type == bp_std_terminate)
7526 delete_breakpoint (b);
7529 struct breakpoint *
7530 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7532 struct breakpoint *b;
7534 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
7536 b->enable_state = bp_enabled;
7537 /* location has to be used or breakpoint_re_set will delete me. */
7538 b->location = new_address_location (b->loc->address, NULL, 0);
7540 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7542 return b;
7545 struct lang_and_radix
7547 enum language lang;
7548 int radix;
7551 /* Create a breakpoint for JIT code registration and unregistration. */
7553 struct breakpoint *
7554 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7556 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
7559 /* Remove JIT code registration and unregistration breakpoint(s). */
7561 void
7562 remove_jit_event_breakpoints (void)
7564 for (breakpoint *b : all_breakpoints_safe ())
7565 if (b->type == bp_jit_event
7566 && b->loc->pspace == current_program_space)
7567 delete_breakpoint (b);
7570 void
7571 remove_solib_event_breakpoints (void)
7573 for (breakpoint *b : all_breakpoints_safe ())
7574 if (b->type == bp_shlib_event
7575 && b->loc->pspace == current_program_space)
7576 delete_breakpoint (b);
7579 /* See breakpoint.h. */
7581 void
7582 remove_solib_event_breakpoints_at_next_stop (void)
7584 for (breakpoint *b : all_breakpoints_safe ())
7585 if (b->type == bp_shlib_event
7586 && b->loc->pspace == current_program_space)
7587 b->disposition = disp_del_at_next_stop;
7590 /* Helper for create_solib_event_breakpoint /
7591 create_and_insert_solib_event_breakpoint. Allows specifying which
7592 INSERT_MODE to pass through to update_global_location_list. */
7594 static struct breakpoint *
7595 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7596 enum ugll_insert_mode insert_mode)
7598 struct breakpoint *b;
7600 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
7601 update_global_location_list_nothrow (insert_mode);
7602 return b;
7605 struct breakpoint *
7606 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7608 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7611 /* See breakpoint.h. */
7613 struct breakpoint *
7614 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7616 struct breakpoint *b;
7618 /* Explicitly tell update_global_location_list to insert
7619 locations. */
7620 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7621 if (!b->loc->inserted)
7623 delete_breakpoint (b);
7624 return NULL;
7626 return b;
7629 /* Disable any breakpoints that are on code in shared libraries. Only
7630 apply to enabled breakpoints, disabled ones can just stay disabled. */
7632 void
7633 disable_breakpoints_in_shlibs (void)
7635 for (bp_location *loc : all_bp_locations ())
7637 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7638 struct breakpoint *b = loc->owner;
7640 /* We apply the check to all breakpoints, including disabled for
7641 those with loc->duplicate set. This is so that when breakpoint
7642 becomes enabled, or the duplicate is removed, gdb will try to
7643 insert all breakpoints. If we don't set shlib_disabled here,
7644 we'll try to insert those breakpoints and fail. */
7645 if (((b->type == bp_breakpoint)
7646 || (b->type == bp_jit_event)
7647 || (b->type == bp_hardware_breakpoint)
7648 || (is_tracepoint (b)))
7649 && loc->pspace == current_program_space
7650 && !loc->shlib_disabled
7651 && solib_name_from_address (loc->pspace, loc->address)
7654 loc->shlib_disabled = 1;
7659 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7660 notification of unloaded_shlib. Only apply to enabled breakpoints,
7661 disabled ones can just stay disabled. */
7663 static void
7664 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7666 int disabled_shlib_breaks = 0;
7668 for (bp_location *loc : all_bp_locations ())
7670 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7671 struct breakpoint *b = loc->owner;
7673 if (solib->pspace == loc->pspace
7674 && !loc->shlib_disabled
7675 && (((b->type == bp_breakpoint
7676 || b->type == bp_jit_event
7677 || b->type == bp_hardware_breakpoint)
7678 && (loc->loc_type == bp_loc_hardware_breakpoint
7679 || loc->loc_type == bp_loc_software_breakpoint))
7680 || is_tracepoint (b))
7681 && solib_contains_address_p (solib, loc->address))
7683 loc->shlib_disabled = 1;
7684 /* At this point, we cannot rely on remove_breakpoint
7685 succeeding so we must mark the breakpoint as not inserted
7686 to prevent future errors occurring in remove_breakpoints. */
7687 loc->inserted = 0;
7689 /* This may cause duplicate notifications for the same breakpoint. */
7690 gdb::observers::breakpoint_modified.notify (b);
7692 if (!disabled_shlib_breaks)
7694 target_terminal::ours_for_output ();
7695 warning (_("Temporarily disabling breakpoints "
7696 "for unloaded shared library \"%s\""),
7697 solib->so_name);
7699 disabled_shlib_breaks = 1;
7704 /* Disable any breakpoints and tracepoints in OBJFILE upon
7705 notification of free_objfile. Only apply to enabled breakpoints,
7706 disabled ones can just stay disabled. */
7708 static void
7709 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7711 if (objfile == NULL)
7712 return;
7714 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7715 managed by the user with add-symbol-file/remove-symbol-file.
7716 Similarly to how breakpoints in shared libraries are handled in
7717 response to "nosharedlibrary", mark breakpoints in such modules
7718 shlib_disabled so they end up uninserted on the next global
7719 location list update. Shared libraries not loaded by the user
7720 aren't handled here -- they're already handled in
7721 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7722 solib_unloaded observer. We skip objfiles that are not
7723 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7724 main objfile). */
7725 if ((objfile->flags & OBJF_SHARED) == 0
7726 || (objfile->flags & OBJF_USERLOADED) == 0)
7727 return;
7729 for (breakpoint *b : all_breakpoints ())
7731 int bp_modified = 0;
7733 if (!is_breakpoint (b) && !is_tracepoint (b))
7734 continue;
7736 for (bp_location *loc : b->locations ())
7738 CORE_ADDR loc_addr = loc->address;
7740 if (loc->loc_type != bp_loc_hardware_breakpoint
7741 && loc->loc_type != bp_loc_software_breakpoint)
7742 continue;
7744 if (loc->shlib_disabled != 0)
7745 continue;
7747 if (objfile->pspace != loc->pspace)
7748 continue;
7750 if (loc->loc_type != bp_loc_hardware_breakpoint
7751 && loc->loc_type != bp_loc_software_breakpoint)
7752 continue;
7754 if (is_addr_in_objfile (loc_addr, objfile))
7756 loc->shlib_disabled = 1;
7757 /* At this point, we don't know whether the object was
7758 unmapped from the inferior or not, so leave the
7759 inserted flag alone. We'll handle failure to
7760 uninsert quietly, in case the object was indeed
7761 unmapped. */
7763 mark_breakpoint_location_modified (loc);
7765 bp_modified = 1;
7769 if (bp_modified)
7770 gdb::observers::breakpoint_modified.notify (b);
7774 /* See breakpoint.h. */
7776 breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
7777 bool temp, const char *cond_string_)
7778 : type (bptype),
7779 disposition (temp ? disp_del : disp_donttouch),
7780 gdbarch (gdbarch_),
7781 language (current_language->la_language),
7782 input_radix (::input_radix),
7783 cond_string (cond_string_ != nullptr
7784 ? make_unique_xstrdup (cond_string_)
7785 : nullptr),
7786 related_breakpoint (this)
7790 /* See breakpoint.h. */
7792 catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
7793 const char *cond_string)
7794 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
7796 add_dummy_location (this, current_program_space);
7798 pspace = current_program_space;
7801 void
7802 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
7804 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
7805 set_breakpoint_number (internal, b);
7806 if (is_tracepoint (b))
7807 set_tracepoint_count (breakpoint_count);
7808 if (!internal)
7809 mention (b);
7810 gdb::observers::breakpoint_created.notify (b);
7812 if (update_gll)
7813 update_global_location_list (UGLL_MAY_INSERT);
7816 static int
7817 hw_breakpoint_used_count (void)
7819 int i = 0;
7821 for (breakpoint *b : all_breakpoints ())
7822 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
7823 for (bp_location *bl : b->locations ())
7825 /* Special types of hardware breakpoints may use more than
7826 one register. */
7827 i += b->resources_needed (bl);
7830 return i;
7833 /* Returns the resources B would use if it were a hardware
7834 watchpoint. */
7836 static int
7837 hw_watchpoint_use_count (struct breakpoint *b)
7839 int i = 0;
7841 if (!breakpoint_enabled (b))
7842 return 0;
7844 for (bp_location *bl : b->locations ())
7846 /* Special types of hardware watchpoints may use more than
7847 one register. */
7848 i += b->resources_needed (bl);
7851 return i;
7854 /* Returns the sum the used resources of all hardware watchpoints of
7855 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
7856 the sum of the used resources of all hardware watchpoints of other
7857 types _not_ TYPE. */
7859 static int
7860 hw_watchpoint_used_count_others (struct breakpoint *except,
7861 enum bptype type, int *other_type_used)
7863 int i = 0;
7865 *other_type_used = 0;
7866 for (breakpoint *b : all_breakpoints ())
7868 if (b == except)
7869 continue;
7870 if (!breakpoint_enabled (b))
7871 continue;
7873 if (b->type == type)
7874 i += hw_watchpoint_use_count (b);
7875 else if (is_hardware_watchpoint (b))
7876 *other_type_used = 1;
7879 return i;
7882 void
7883 disable_watchpoints_before_interactive_call_start (void)
7885 for (breakpoint *b : all_breakpoints ())
7886 if (is_watchpoint (b) && breakpoint_enabled (b))
7888 b->enable_state = bp_call_disabled;
7889 update_global_location_list (UGLL_DONT_INSERT);
7893 void
7894 enable_watchpoints_after_interactive_call_stop (void)
7896 for (breakpoint *b : all_breakpoints ())
7897 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
7899 b->enable_state = bp_enabled;
7900 update_global_location_list (UGLL_MAY_INSERT);
7904 void
7905 disable_breakpoints_before_startup (void)
7907 current_program_space->executing_startup = 1;
7908 update_global_location_list (UGLL_DONT_INSERT);
7911 void
7912 enable_breakpoints_after_startup (void)
7914 current_program_space->executing_startup = 0;
7915 breakpoint_re_set ();
7918 /* Allocate a new momentary breakpoint. */
7920 template<typename... Arg>
7921 static momentary_breakpoint *
7922 new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
7923 Arg&&... args)
7925 if (type == bp_longjmp || type == bp_exception)
7926 return new longjmp_breakpoint (gdbarch, type,
7927 std::forward<Arg> (args)...);
7928 else
7929 return new momentary_breakpoint (gdbarch, type,
7930 std::forward<Arg> (args)...);
7933 /* Set a momentary breakpoint of type TYPE at address specified by
7934 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
7935 frame. */
7937 breakpoint_up
7938 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7939 struct frame_id frame_id, enum bptype type)
7941 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
7942 tail-called one. */
7943 gdb_assert (!frame_id_artificial_p (frame_id));
7945 std::unique_ptr<momentary_breakpoint> b
7946 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
7947 inferior_thread ()->global_num));
7949 b->add_location (sal);
7951 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
7953 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7955 return bp;
7958 /* Make a momentary breakpoint based on the master breakpoint ORIG.
7959 The new breakpoint will have type TYPE, use OPS as its
7960 breakpoint_ops, and will set enabled to LOC_ENABLED. */
7962 static struct breakpoint *
7963 momentary_breakpoint_from_master (struct breakpoint *orig,
7964 enum bptype type,
7965 int loc_enabled,
7966 int thread)
7968 std::unique_ptr<breakpoint> copy
7969 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
7970 orig->frame_id, thread));
7971 copy->loc = copy->allocate_location ();
7972 set_breakpoint_location_function (copy->loc);
7974 copy->loc->gdbarch = orig->loc->gdbarch;
7975 copy->loc->requested_address = orig->loc->requested_address;
7976 copy->loc->address = orig->loc->address;
7977 copy->loc->section = orig->loc->section;
7978 copy->loc->pspace = orig->loc->pspace;
7979 copy->loc->probe = orig->loc->probe;
7980 copy->loc->line_number = orig->loc->line_number;
7981 copy->loc->symtab = orig->loc->symtab;
7982 copy->loc->enabled = loc_enabled;
7984 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
7985 update_global_location_list_nothrow (UGLL_DONT_INSERT);
7986 return b;
7989 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
7990 ORIG is NULL. */
7992 struct breakpoint *
7993 clone_momentary_breakpoint (struct breakpoint *orig)
7995 /* If there's nothing to clone, then return nothing. */
7996 if (orig == NULL)
7997 return NULL;
7999 return momentary_breakpoint_from_master (orig, orig->type, 0,
8000 orig->thread);
8003 breakpoint_up
8004 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8005 enum bptype type)
8007 struct symtab_and_line sal;
8009 sal = find_pc_line (pc, 0);
8010 sal.pc = pc;
8011 sal.section = find_pc_overlay (pc);
8012 sal.explicit_pc = 1;
8014 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8018 /* Tell the user we have just set a breakpoint B. */
8020 static void
8021 mention (const breakpoint *b)
8023 b->print_mention ();
8024 current_uiout->text ("\n");
8028 static bool bp_loc_is_permanent (struct bp_location *loc);
8030 /* Handle "set breakpoint auto-hw on".
8032 If the explicitly specified breakpoint type is not hardware
8033 breakpoint, check the memory map to see whether the breakpoint
8034 address is in read-only memory.
8036 - location type is not hardware breakpoint, memory is read-only.
8037 We change the type of the location to hardware breakpoint.
8039 - location type is hardware breakpoint, memory is read-write. This
8040 means we've previously made the location hardware one, but then the
8041 memory map changed, so we undo.
8044 static void
8045 handle_automatic_hardware_breakpoints (bp_location *bl)
8047 if (automatic_hardware_breakpoints
8048 && bl->owner->type != bp_hardware_breakpoint
8049 && (bl->loc_type == bp_loc_software_breakpoint
8050 || bl->loc_type == bp_loc_hardware_breakpoint))
8052 /* When breakpoints are removed, remove_breakpoints will use
8053 location types we've just set here, the only possible problem
8054 is that memory map has changed during running program, but
8055 it's not going to work anyway with current gdb. */
8056 mem_region *mr = lookup_mem_region (bl->address);
8058 if (mr != nullptr)
8060 enum bp_loc_type new_type;
8062 if (mr->attrib.mode != MEM_RW)
8063 new_type = bp_loc_hardware_breakpoint;
8064 else
8065 new_type = bp_loc_software_breakpoint;
8067 if (new_type != bl->loc_type)
8069 static bool said = false;
8071 bl->loc_type = new_type;
8072 if (!said)
8074 gdb_printf (_("Note: automatically using "
8075 "hardware breakpoints for "
8076 "read-only addresses.\n"));
8077 said = true;
8084 bp_location *
8085 code_breakpoint::add_location (const symtab_and_line &sal)
8087 struct bp_location *new_loc, **tmp;
8088 CORE_ADDR adjusted_address;
8089 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8091 if (loc_gdbarch == NULL)
8092 loc_gdbarch = gdbarch;
8094 /* Adjust the breakpoint's address prior to allocating a location.
8095 Once we call allocate_location(), that mostly uninitialized
8096 location will be placed on the location chain. Adjustment of the
8097 breakpoint may cause target_read_memory() to be called and we do
8098 not want its scan of the location chain to find a breakpoint and
8099 location that's only been partially initialized. */
8100 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8101 sal.pc, type);
8103 /* Sort the locations by their ADDRESS. */
8104 new_loc = allocate_location ();
8105 for (tmp = &(loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8106 tmp = &((*tmp)->next))
8108 new_loc->next = *tmp;
8109 *tmp = new_loc;
8111 new_loc->requested_address = sal.pc;
8112 new_loc->address = adjusted_address;
8113 new_loc->pspace = sal.pspace;
8114 new_loc->probe.prob = sal.prob;
8115 new_loc->probe.objfile = sal.objfile;
8116 gdb_assert (new_loc->pspace != NULL);
8117 new_loc->section = sal.section;
8118 new_loc->gdbarch = loc_gdbarch;
8119 new_loc->line_number = sal.line;
8120 new_loc->symtab = sal.symtab;
8121 new_loc->symbol = sal.symbol;
8122 new_loc->msymbol = sal.msymbol;
8123 new_loc->objfile = sal.objfile;
8125 set_breakpoint_location_function (new_loc);
8127 /* While by definition, permanent breakpoints are already present in the
8128 code, we don't mark the location as inserted. Normally one would expect
8129 that GDB could rely on that breakpoint instruction to stop the program,
8130 thus removing the need to insert its own breakpoint, except that executing
8131 the breakpoint instruction can kill the target instead of reporting a
8132 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8133 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8134 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8135 breakpoint be inserted normally results in QEMU knowing about the GDB
8136 breakpoint, and thus trap before the breakpoint instruction is executed.
8137 (If GDB later needs to continue execution past the permanent breakpoint,
8138 it manually increments the PC, thus avoiding executing the breakpoint
8139 instruction.) */
8140 if (bp_loc_is_permanent (new_loc))
8141 new_loc->permanent = 1;
8143 return new_loc;
8147 /* Return true if LOC is pointing to a permanent breakpoint,
8148 return false otherwise. */
8150 static bool
8151 bp_loc_is_permanent (struct bp_location *loc)
8153 gdb_assert (loc != NULL);
8155 /* If we have a non-breakpoint-backed catchpoint or a software
8156 watchpoint, just return 0. We should not attempt to read from
8157 the addresses the locations of these breakpoint types point to.
8158 gdbarch_program_breakpoint_here_p, below, will attempt to read
8159 memory. */
8160 if (!bl_address_is_meaningful (loc))
8161 return false;
8163 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8164 switch_to_program_space_and_thread (loc->pspace);
8165 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8168 /* Build a command list for the dprintf corresponding to the current
8169 settings of the dprintf style options. */
8171 static void
8172 update_dprintf_command_list (struct breakpoint *b)
8174 const char *dprintf_args = b->extra_string.get ();
8175 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8177 if (!dprintf_args)
8178 return;
8180 dprintf_args = skip_spaces (dprintf_args);
8182 /* Allow a comma, as it may have terminated a location, but don't
8183 insist on it. */
8184 if (*dprintf_args == ',')
8185 ++dprintf_args;
8186 dprintf_args = skip_spaces (dprintf_args);
8188 if (*dprintf_args != '"')
8189 error (_("Bad format string, missing '\"'."));
8191 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8192 printf_line = xstrprintf ("printf %s", dprintf_args);
8193 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8195 if (dprintf_function.empty ())
8196 error (_("No function supplied for dprintf call"));
8198 if (!dprintf_channel.empty ())
8199 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8200 dprintf_function.c_str (),
8201 dprintf_channel.c_str (),
8202 dprintf_args);
8203 else
8204 printf_line = xstrprintf ("call (void) %s (%s)",
8205 dprintf_function.c_str (),
8206 dprintf_args);
8208 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8210 if (target_can_run_breakpoint_commands ())
8211 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8212 else
8214 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8215 printf_line = xstrprintf ("printf %s", dprintf_args);
8218 else
8219 internal_error (__FILE__, __LINE__,
8220 _("Invalid dprintf style."));
8222 gdb_assert (printf_line != NULL);
8224 /* Manufacture a printf sequence. */
8225 struct command_line *printf_cmd_line
8226 = new struct command_line (simple_control, printf_line.release ());
8227 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8228 command_lines_deleter ()));
8231 /* Update all dprintf commands, making their command lists reflect
8232 current style settings. */
8234 static void
8235 update_dprintf_commands (const char *args, int from_tty,
8236 struct cmd_list_element *c)
8238 for (breakpoint *b : all_breakpoints ())
8239 if (b->type == bp_dprintf)
8240 update_dprintf_command_list (b);
8243 code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
8244 enum bptype type_,
8245 gdb::array_view<const symtab_and_line> sals,
8246 event_location_up &&location_,
8247 gdb::unique_xmalloc_ptr<char> filter_,
8248 gdb::unique_xmalloc_ptr<char> cond_string_,
8249 gdb::unique_xmalloc_ptr<char> extra_string_,
8250 enum bpdisp disposition_,
8251 int thread_, int task_, int ignore_count_,
8252 int from_tty,
8253 int enabled_, unsigned flags,
8254 int display_canonical_)
8255 : breakpoint (gdbarch_, type_)
8257 int i;
8259 if (type == bp_hardware_breakpoint)
8261 int target_resources_ok;
8263 i = hw_breakpoint_used_count ();
8264 target_resources_ok =
8265 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8266 i + 1, 0);
8267 if (target_resources_ok == 0)
8268 error (_("No hardware breakpoint support in the target."));
8269 else if (target_resources_ok < 0)
8270 error (_("Hardware breakpoints used exceeds limit."));
8273 gdb_assert (!sals.empty ());
8275 thread = thread_;
8276 task = task_;
8278 cond_string = std::move (cond_string_);
8279 extra_string = std::move (extra_string_);
8280 ignore_count = ignore_count_;
8281 enable_state = enabled_ ? bp_enabled : bp_disabled;
8282 disposition = disposition_;
8284 if (type == bp_static_tracepoint
8285 || type == bp_static_marker_tracepoint)
8287 auto *t = static_cast<struct tracepoint *> (this);
8288 struct static_tracepoint_marker marker;
8290 if (strace_marker_p (this))
8292 /* We already know the marker exists, otherwise, we wouldn't
8293 see a sal for it. */
8294 const char *p = &event_location_to_string (location_.get ())[3];
8295 const char *endp;
8297 p = skip_spaces (p);
8299 endp = skip_to_space (p);
8301 t->static_trace_marker_id.assign (p, endp - p);
8303 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8304 t->static_trace_marker_id.c_str ());
8306 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8308 t->static_trace_marker_id = std::move (marker.str_id);
8310 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8311 t->static_trace_marker_id.c_str ());
8313 else
8314 warning (_("Couldn't determine the static tracepoint marker to probe"));
8317 for (const auto &sal : sals)
8319 if (from_tty)
8321 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8322 if (loc_gdbarch == nullptr)
8323 loc_gdbarch = gdbarch;
8325 describe_other_breakpoints (loc_gdbarch,
8326 sal.pspace, sal.pc, sal.section, thread);
8329 bp_location *new_loc = add_location (sal);
8330 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8331 new_loc->inserted = 1;
8333 /* Do not set breakpoint locations conditions yet. As locations
8334 are inserted, they get sorted based on their addresses. Let
8335 the list stabilize to have reliable location numbers. */
8337 /* Dynamic printf requires and uses additional arguments on the
8338 command line, otherwise it's an error. */
8339 if (type == bp_dprintf)
8341 if (extra_string != nullptr)
8342 update_dprintf_command_list (this);
8343 else
8344 error (_("Format string required"));
8346 else if (extra_string != nullptr)
8347 error (_("Garbage '%s' at end of command"), extra_string.get ());
8350 /* The order of the locations is now stable. Set the location
8351 condition using the location's number. */
8352 int loc_num = 1;
8353 for (bp_location *bl : locations ())
8355 if (cond_string != nullptr)
8356 set_breakpoint_location_condition (cond_string.get (), bl,
8357 number, loc_num);
8359 ++loc_num;
8362 display_canonical = display_canonical_;
8363 if (location_ != nullptr)
8364 location = std::move (location_);
8365 else
8366 location = new_address_location (this->loc->address, NULL, 0);
8367 filter = std::move (filter_);
8370 static void
8371 create_breakpoint_sal (struct gdbarch *gdbarch,
8372 gdb::array_view<const symtab_and_line> sals,
8373 event_location_up &&location,
8374 gdb::unique_xmalloc_ptr<char> filter,
8375 gdb::unique_xmalloc_ptr<char> cond_string,
8376 gdb::unique_xmalloc_ptr<char> extra_string,
8377 enum bptype type, enum bpdisp disposition,
8378 int thread, int task, int ignore_count,
8379 int from_tty,
8380 int enabled, int internal, unsigned flags,
8381 int display_canonical)
8383 std::unique_ptr<code_breakpoint> b
8384 = new_breakpoint_from_type (gdbarch,
8385 type,
8386 sals,
8387 std::move (location),
8388 std::move (filter),
8389 std::move (cond_string),
8390 std::move (extra_string),
8391 disposition,
8392 thread, task, ignore_count,
8393 from_tty,
8394 enabled, flags,
8395 display_canonical);
8397 install_breakpoint (internal, std::move (b), 0);
8400 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8401 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8402 value. COND_STRING, if not NULL, specified the condition to be
8403 used for all breakpoints. Essentially the only case where
8404 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8405 function. In that case, it's still not possible to specify
8406 separate conditions for different overloaded functions, so
8407 we take just a single condition string.
8409 NOTE: If the function succeeds, the caller is expected to cleanup
8410 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8411 array contents). If the function fails (error() is called), the
8412 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8413 COND and SALS arrays and each of those arrays contents. */
8415 static void
8416 create_breakpoints_sal (struct gdbarch *gdbarch,
8417 struct linespec_result *canonical,
8418 gdb::unique_xmalloc_ptr<char> cond_string,
8419 gdb::unique_xmalloc_ptr<char> extra_string,
8420 enum bptype type, enum bpdisp disposition,
8421 int thread, int task, int ignore_count,
8422 int from_tty,
8423 int enabled, int internal, unsigned flags)
8425 if (canonical->pre_expanded)
8426 gdb_assert (canonical->lsals.size () == 1);
8428 for (const auto &lsal : canonical->lsals)
8430 /* Note that 'location' can be NULL in the case of a plain
8431 'break', without arguments. */
8432 event_location_up location
8433 = (canonical->location != NULL
8434 ? copy_event_location (canonical->location.get ()) : NULL);
8435 gdb::unique_xmalloc_ptr<char> filter_string
8436 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8438 create_breakpoint_sal (gdbarch, lsal.sals,
8439 std::move (location),
8440 std::move (filter_string),
8441 std::move (cond_string),
8442 std::move (extra_string),
8443 type, disposition,
8444 thread, task, ignore_count,
8445 from_tty, enabled, internal, flags,
8446 canonical->special_display);
8450 /* Parse LOCATION which is assumed to be a SAL specification possibly
8451 followed by conditionals. On return, SALS contains an array of SAL
8452 addresses found. LOCATION points to the end of the SAL (for
8453 linespec locations).
8455 The array and the line spec strings are allocated on the heap, it is
8456 the caller's responsibility to free them. */
8458 static void
8459 parse_breakpoint_sals (struct event_location *location,
8460 struct linespec_result *canonical)
8462 struct symtab_and_line cursal;
8464 if (event_location_type (location) == LINESPEC_LOCATION)
8466 const char *spec = get_linespec_location (location)->spec_string;
8468 if (spec == NULL)
8470 /* The last displayed codepoint, if it's valid, is our default
8471 breakpoint address. */
8472 if (last_displayed_sal_is_valid ())
8474 /* Set sal's pspace, pc, symtab, and line to the values
8475 corresponding to the last call to print_frame_info.
8476 Be sure to reinitialize LINE with NOTCURRENT == 0
8477 as the breakpoint line number is inappropriate otherwise.
8478 find_pc_line would adjust PC, re-set it back. */
8479 symtab_and_line sal = get_last_displayed_sal ();
8480 CORE_ADDR pc = sal.pc;
8482 sal = find_pc_line (pc, 0);
8484 /* "break" without arguments is equivalent to "break *PC"
8485 where PC is the last displayed codepoint's address. So
8486 make sure to set sal.explicit_pc to prevent GDB from
8487 trying to expand the list of sals to include all other
8488 instances with the same symtab and line. */
8489 sal.pc = pc;
8490 sal.explicit_pc = 1;
8492 struct linespec_sals lsal;
8493 lsal.sals = {sal};
8494 lsal.canonical = NULL;
8496 canonical->lsals.push_back (std::move (lsal));
8497 return;
8499 else
8500 error (_("No default breakpoint address now."));
8504 /* Force almost all breakpoints to be in terms of the
8505 current_source_symtab (which is decode_line_1's default).
8506 This should produce the results we want almost all of the
8507 time while leaving default_breakpoint_* alone.
8509 ObjC: However, don't match an Objective-C method name which
8510 may have a '+' or '-' succeeded by a '['. */
8511 cursal = get_current_source_symtab_and_line ();
8512 if (last_displayed_sal_is_valid ())
8514 const char *spec = NULL;
8516 if (event_location_type (location) == LINESPEC_LOCATION)
8517 spec = get_linespec_location (location)->spec_string;
8519 if (!cursal.symtab
8520 || (spec != NULL
8521 && strchr ("+-", spec[0]) != NULL
8522 && spec[1] != '['))
8524 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
8525 get_last_displayed_symtab (),
8526 get_last_displayed_line (),
8527 canonical, NULL, NULL);
8528 return;
8532 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
8533 cursal.symtab, cursal.line, canonical, NULL, NULL);
8537 /* Convert each SAL into a real PC. Verify that the PC can be
8538 inserted as a breakpoint. If it can't throw an error. */
8540 static void
8541 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
8543 for (auto &sal : sals)
8544 resolve_sal_pc (&sal);
8547 /* Fast tracepoints may have restrictions on valid locations. For
8548 instance, a fast tracepoint using a jump instead of a trap will
8549 likely have to overwrite more bytes than a trap would, and so can
8550 only be placed where the instruction is longer than the jump, or a
8551 multi-instruction sequence does not have a jump into the middle of
8552 it, etc. */
8554 static void
8555 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8556 gdb::array_view<const symtab_and_line> sals)
8558 for (const auto &sal : sals)
8560 struct gdbarch *sarch;
8562 sarch = get_sal_arch (sal);
8563 /* We fall back to GDBARCH if there is no architecture
8564 associated with SAL. */
8565 if (sarch == NULL)
8566 sarch = gdbarch;
8567 std::string msg;
8568 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
8569 error (_("May not have a fast tracepoint at %s%s"),
8570 paddress (sarch, sal.pc), msg.c_str ());
8574 /* Given TOK, a string specification of condition and thread, as
8575 accepted by the 'break' command, extract the condition
8576 string and thread number and set *COND_STRING and *THREAD.
8577 PC identifies the context at which the condition should be parsed.
8578 If no condition is found, *COND_STRING is set to NULL.
8579 If no thread is found, *THREAD is set to -1. */
8581 static void
8582 find_condition_and_thread (const char *tok, CORE_ADDR pc,
8583 gdb::unique_xmalloc_ptr<char> *cond_string,
8584 int *thread, int *task,
8585 gdb::unique_xmalloc_ptr<char> *rest)
8587 cond_string->reset ();
8588 *thread = -1;
8589 *task = 0;
8590 rest->reset ();
8591 bool force = false;
8593 while (tok && *tok)
8595 const char *end_tok;
8596 int toklen;
8597 const char *cond_start = NULL;
8598 const char *cond_end = NULL;
8600 tok = skip_spaces (tok);
8602 if ((*tok == '"' || *tok == ',') && rest)
8604 rest->reset (savestring (tok, strlen (tok)));
8605 return;
8608 end_tok = skip_to_space (tok);
8610 toklen = end_tok - tok;
8612 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8614 tok = cond_start = end_tok + 1;
8617 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8619 catch (const gdb_exception_error &)
8621 if (!force)
8622 throw;
8623 else
8624 tok = tok + strlen (tok);
8626 cond_end = tok;
8627 cond_string->reset (savestring (cond_start, cond_end - cond_start));
8629 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8631 tok = tok + toklen;
8632 force = true;
8634 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8636 const char *tmptok;
8637 struct thread_info *thr;
8639 tok = end_tok + 1;
8640 thr = parse_thread_id (tok, &tmptok);
8641 if (tok == tmptok)
8642 error (_("Junk after thread keyword."));
8643 *thread = thr->global_num;
8644 tok = tmptok;
8646 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8648 char *tmptok;
8650 tok = end_tok + 1;
8651 *task = strtol (tok, &tmptok, 0);
8652 if (tok == tmptok)
8653 error (_("Junk after task keyword."));
8654 if (!valid_task_id (*task))
8655 error (_("Unknown task %d."), *task);
8656 tok = tmptok;
8658 else if (rest)
8660 rest->reset (savestring (tok, strlen (tok)));
8661 return;
8663 else
8664 error (_("Junk at end of arguments."));
8668 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
8669 succeeds. The parsed values are written to COND_STRING, THREAD,
8670 TASK, and REST. See the comment of 'find_condition_and_thread'
8671 for the description of these parameters and INPUT. */
8673 static void
8674 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
8675 const char *input,
8676 gdb::unique_xmalloc_ptr<char> *cond_string,
8677 int *thread, int *task,
8678 gdb::unique_xmalloc_ptr<char> *rest)
8680 int num_failures = 0;
8681 for (auto &sal : sals)
8683 gdb::unique_xmalloc_ptr<char> cond;
8684 int thread_id = 0;
8685 int task_id = 0;
8686 gdb::unique_xmalloc_ptr<char> remaining;
8688 /* Here we want to parse 'arg' to separate condition from thread
8689 number. But because parsing happens in a context and the
8690 contexts of sals might be different, try each until there is
8691 success. Finding one successful parse is sufficient for our
8692 goal. When setting the breakpoint we'll re-parse the
8693 condition in the context of each sal. */
8696 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8697 &task_id, &remaining);
8698 *cond_string = std::move (cond);
8699 *thread = thread_id;
8700 *task = task_id;
8701 *rest = std::move (remaining);
8702 break;
8704 catch (const gdb_exception_error &e)
8706 num_failures++;
8707 /* If no sal remains, do not continue. */
8708 if (num_failures == sals.size ())
8709 throw;
8714 /* Decode a static tracepoint marker spec. */
8716 static std::vector<symtab_and_line>
8717 decode_static_tracepoint_spec (const char **arg_p)
8719 const char *p = &(*arg_p)[3];
8720 const char *endp;
8722 p = skip_spaces (p);
8724 endp = skip_to_space (p);
8726 std::string marker_str (p, endp - p);
8728 std::vector<static_tracepoint_marker> markers
8729 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8730 if (markers.empty ())
8731 error (_("No known static tracepoint marker named %s"),
8732 marker_str.c_str ());
8734 std::vector<symtab_and_line> sals;
8735 sals.reserve (markers.size ());
8737 for (const static_tracepoint_marker &marker : markers)
8739 symtab_and_line sal = find_pc_line (marker.address, 0);
8740 sal.pc = marker.address;
8741 sals.push_back (sal);
8744 *arg_p = endp;
8745 return sals;
8748 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8749 according to IS_TRACEPOINT. */
8751 static const struct breakpoint_ops *
8752 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
8753 bool is_tracepoint)
8755 if (is_tracepoint)
8757 if (location_type == PROBE_LOCATION)
8758 return &tracepoint_probe_breakpoint_ops;
8759 else
8760 return &code_breakpoint_ops;
8762 else
8764 if (location_type == PROBE_LOCATION)
8765 return &bkpt_probe_breakpoint_ops;
8766 else
8767 return &code_breakpoint_ops;
8771 /* See breakpoint.h. */
8773 const struct breakpoint_ops *
8774 breakpoint_ops_for_event_location (const struct event_location *location,
8775 bool is_tracepoint)
8777 if (location != nullptr)
8778 return breakpoint_ops_for_event_location_type
8779 (event_location_type (location), is_tracepoint);
8780 return &code_breakpoint_ops;
8783 /* See breakpoint.h. */
8786 create_breakpoint (struct gdbarch *gdbarch,
8787 struct event_location *location,
8788 const char *cond_string,
8789 int thread, const char *extra_string,
8790 bool force_condition, int parse_extra,
8791 int tempflag, enum bptype type_wanted,
8792 int ignore_count,
8793 enum auto_boolean pending_break_support,
8794 const struct breakpoint_ops *ops,
8795 int from_tty, int enabled, int internal,
8796 unsigned flags)
8798 struct linespec_result canonical;
8799 int pending = 0;
8800 int task = 0;
8801 int prev_bkpt_count = breakpoint_count;
8803 gdb_assert (ops != NULL);
8805 /* If extra_string isn't useful, set it to NULL. */
8806 if (extra_string != NULL && *extra_string == '\0')
8807 extra_string = NULL;
8811 ops->create_sals_from_location (location, &canonical);
8813 catch (const gdb_exception_error &e)
8815 /* If caller is interested in rc value from parse, set
8816 value. */
8817 if (e.error == NOT_FOUND_ERROR)
8819 /* If pending breakpoint support is turned off, throw
8820 error. */
8822 if (pending_break_support == AUTO_BOOLEAN_FALSE)
8823 throw;
8825 exception_print (gdb_stderr, e);
8827 /* If pending breakpoint support is auto query and the user
8828 selects no, then simply return the error code. */
8829 if (pending_break_support == AUTO_BOOLEAN_AUTO
8830 && !nquery (_("Make %s pending on future shared library load? "),
8831 bptype_string (type_wanted)))
8832 return 0;
8834 /* At this point, either the user was queried about setting
8835 a pending breakpoint and selected yes, or pending
8836 breakpoint behavior is on and thus a pending breakpoint
8837 is defaulted on behalf of the user. */
8838 pending = 1;
8840 else
8841 throw;
8844 if (!pending && canonical.lsals.empty ())
8845 return 0;
8847 /* Resolve all line numbers to PC's and verify that the addresses
8848 are ok for the target. */
8849 if (!pending)
8851 for (auto &lsal : canonical.lsals)
8852 breakpoint_sals_to_pc (lsal.sals);
8855 /* Fast tracepoints may have additional restrictions on location. */
8856 if (!pending && type_wanted == bp_fast_tracepoint)
8858 for (const auto &lsal : canonical.lsals)
8859 check_fast_tracepoint_sals (gdbarch, lsal.sals);
8862 /* Verify that condition can be parsed, before setting any
8863 breakpoints. Allocate a separate condition expression for each
8864 breakpoint. */
8865 if (!pending)
8867 gdb::unique_xmalloc_ptr<char> cond_string_copy;
8868 gdb::unique_xmalloc_ptr<char> extra_string_copy;
8870 if (parse_extra)
8872 gdb::unique_xmalloc_ptr<char> rest;
8873 gdb::unique_xmalloc_ptr<char> cond;
8875 const linespec_sals &lsal = canonical.lsals[0];
8877 find_condition_and_thread_for_sals (lsal.sals, extra_string,
8878 &cond, &thread, &task, &rest);
8879 cond_string_copy = std::move (cond);
8880 extra_string_copy = std::move (rest);
8882 else
8884 if (type_wanted != bp_dprintf
8885 && extra_string != NULL && *extra_string != '\0')
8886 error (_("Garbage '%s' at end of location"), extra_string);
8888 /* Check the validity of the condition. We should error out
8889 if the condition is invalid at all of the locations and
8890 if it is not forced. In the PARSE_EXTRA case above, this
8891 check is done when parsing the EXTRA_STRING. */
8892 if (cond_string != nullptr && !force_condition)
8894 int num_failures = 0;
8895 const linespec_sals &lsal = canonical.lsals[0];
8896 for (const auto &sal : lsal.sals)
8898 const char *cond = cond_string;
8901 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
8902 /* One success is sufficient to keep going. */
8903 break;
8905 catch (const gdb_exception_error &)
8907 num_failures++;
8908 /* If this is the last sal, error out. */
8909 if (num_failures == lsal.sals.size ())
8910 throw;
8915 /* Create a private copy of condition string. */
8916 if (cond_string)
8917 cond_string_copy.reset (xstrdup (cond_string));
8918 /* Create a private copy of any extra string. */
8919 if (extra_string)
8920 extra_string_copy.reset (xstrdup (extra_string));
8923 ops->create_breakpoints_sal (gdbarch, &canonical,
8924 std::move (cond_string_copy),
8925 std::move (extra_string_copy),
8926 type_wanted,
8927 tempflag ? disp_del : disp_donttouch,
8928 thread, task, ignore_count,
8929 from_tty, enabled, internal, flags);
8931 else
8933 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
8934 type_wanted);
8935 b->location = copy_event_location (location);
8937 if (parse_extra)
8938 b->cond_string = NULL;
8939 else
8941 /* Create a private copy of condition string. */
8942 b->cond_string.reset (cond_string != NULL
8943 ? xstrdup (cond_string)
8944 : NULL);
8945 b->thread = thread;
8948 /* Create a private copy of any extra string. */
8949 b->extra_string.reset (extra_string != NULL
8950 ? xstrdup (extra_string)
8951 : NULL);
8952 b->ignore_count = ignore_count;
8953 b->disposition = tempflag ? disp_del : disp_donttouch;
8954 b->condition_not_parsed = 1;
8955 b->enable_state = enabled ? bp_enabled : bp_disabled;
8956 if ((type_wanted != bp_breakpoint
8957 && type_wanted != bp_hardware_breakpoint) || thread != -1)
8958 b->pspace = current_program_space;
8960 install_breakpoint (internal, std::move (b), 0);
8963 if (canonical.lsals.size () > 1)
8965 warning (_("Multiple breakpoints were set.\nUse the "
8966 "\"delete\" command to delete unwanted breakpoints."));
8967 prev_breakpoint_count = prev_bkpt_count;
8970 update_global_location_list (UGLL_MAY_INSERT);
8972 return 1;
8975 /* Set a breakpoint.
8976 ARG is a string describing breakpoint address,
8977 condition, and thread.
8978 FLAG specifies if a breakpoint is hardware on,
8979 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8980 and BP_TEMPFLAG. */
8982 static void
8983 break_command_1 (const char *arg, int flag, int from_tty)
8985 int tempflag = flag & BP_TEMPFLAG;
8986 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
8987 ? bp_hardware_breakpoint
8988 : bp_breakpoint);
8990 event_location_up location = string_to_event_location (&arg, current_language);
8991 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
8992 (location.get (), false /* is_tracepoint */);
8994 create_breakpoint (get_current_arch (),
8995 location.get (),
8996 NULL, 0, arg, false, 1 /* parse arg */,
8997 tempflag, type_wanted,
8998 0 /* Ignore count */,
8999 pending_break_support,
9000 ops,
9001 from_tty,
9002 1 /* enabled */,
9003 0 /* internal */,
9007 /* Helper function for break_command_1 and disassemble_command. */
9009 void
9010 resolve_sal_pc (struct symtab_and_line *sal)
9012 CORE_ADDR pc;
9014 if (sal->pc == 0 && sal->symtab != NULL)
9016 if (!find_line_pc (sal->symtab, sal->line, &pc))
9017 error (_("No line %d in file \"%s\"."),
9018 sal->line, symtab_to_filename_for_display (sal->symtab));
9019 sal->pc = pc;
9021 /* If this SAL corresponds to a breakpoint inserted using a line
9022 number, then skip the function prologue if necessary. */
9023 if (sal->explicit_line)
9024 skip_prologue_sal (sal);
9027 if (sal->section == 0 && sal->symtab != NULL)
9029 const struct blockvector *bv;
9030 const struct block *b;
9031 struct symbol *sym;
9033 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9034 sal->symtab->compunit ());
9035 if (bv != NULL)
9037 sym = block_linkage_function (b);
9038 if (sym != NULL)
9040 fixup_symbol_section (sym, sal->symtab->compunit ()->objfile ());
9041 sal->section
9042 = sym->obj_section (sal->symtab->compunit ()->objfile ());
9044 else
9046 /* It really is worthwhile to have the section, so we'll
9047 just have to look harder. This case can be executed
9048 if we have line numbers but no functions (as can
9049 happen in assembly source). */
9051 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9052 switch_to_program_space_and_thread (sal->pspace);
9054 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9055 if (msym.minsym)
9056 sal->section = msym.obj_section ();
9062 void
9063 break_command (const char *arg, int from_tty)
9065 break_command_1 (arg, 0, from_tty);
9068 void
9069 tbreak_command (const char *arg, int from_tty)
9071 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9074 static void
9075 hbreak_command (const char *arg, int from_tty)
9077 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9080 static void
9081 thbreak_command (const char *arg, int from_tty)
9083 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9086 /* The dynamic printf command is mostly like a regular breakpoint, but
9087 with a prewired command list consisting of a single output command,
9088 built from extra arguments supplied on the dprintf command
9089 line. */
9091 static void
9092 dprintf_command (const char *arg, int from_tty)
9094 event_location_up location = string_to_event_location (&arg, current_language);
9096 /* If non-NULL, ARG should have been advanced past the location;
9097 the next character must be ','. */
9098 if (arg != NULL)
9100 if (arg[0] != ',' || arg[1] == '\0')
9101 error (_("Format string required"));
9102 else
9104 /* Skip the comma. */
9105 ++arg;
9109 create_breakpoint (get_current_arch (),
9110 location.get (),
9111 NULL, 0, arg, false, 1 /* parse arg */,
9112 0, bp_dprintf,
9113 0 /* Ignore count */,
9114 pending_break_support,
9115 &code_breakpoint_ops,
9116 from_tty,
9117 1 /* enabled */,
9118 0 /* internal */,
9122 static void
9123 agent_printf_command (const char *arg, int from_tty)
9125 error (_("May only run agent-printf on the target"));
9128 /* Implement the "breakpoint_hit" method for ranged breakpoints. */
9131 ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9132 const address_space *aspace,
9133 CORE_ADDR bp_addr,
9134 const target_waitstatus &ws)
9136 if (ws.kind () != TARGET_WAITKIND_STOPPED
9137 || ws.sig () != GDB_SIGNAL_TRAP)
9138 return 0;
9140 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9141 bl->length, aspace, bp_addr);
9144 /* Implement the "resources_needed" method for ranged breakpoints. */
9147 ranged_breakpoint::resources_needed (const struct bp_location *bl)
9149 return target_ranged_break_num_registers ();
9152 /* Implement the "print_it" method for ranged breakpoints. */
9154 enum print_stop_action
9155 ranged_breakpoint::print_it (const bpstat *bs) const
9157 struct bp_location *bl = loc;
9158 struct ui_out *uiout = current_uiout;
9160 gdb_assert (type == bp_hardware_breakpoint);
9162 /* Ranged breakpoints have only one location. */
9163 gdb_assert (bl && bl->next == NULL);
9165 annotate_breakpoint (number);
9167 maybe_print_thread_hit_breakpoint (uiout);
9169 if (disposition == disp_del)
9170 uiout->text ("Temporary ranged breakpoint ");
9171 else
9172 uiout->text ("Ranged breakpoint ");
9173 if (uiout->is_mi_like_p ())
9175 uiout->field_string ("reason",
9176 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9177 uiout->field_string ("disp", bpdisp_text (disposition));
9179 uiout->field_signed ("bkptno", number);
9180 uiout->text (", ");
9182 return PRINT_SRC_AND_LOC;
9185 /* Implement the "print_one" method for ranged breakpoints. */
9187 bool
9188 ranged_breakpoint::print_one (bp_location **last_loc) const
9190 struct bp_location *bl = loc;
9191 struct value_print_options opts;
9192 struct ui_out *uiout = current_uiout;
9194 /* Ranged breakpoints have only one location. */
9195 gdb_assert (bl && bl->next == NULL);
9197 get_user_print_options (&opts);
9199 if (opts.addressprint)
9200 /* We don't print the address range here, it will be printed later
9201 by print_one_detail_ranged_breakpoint. */
9202 uiout->field_skip ("addr");
9203 annotate_field (5);
9204 print_breakpoint_location (this, bl);
9205 *last_loc = bl;
9207 return true;
9210 /* Implement the "print_one_detail" method for ranged breakpoints. */
9212 void
9213 ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
9215 CORE_ADDR address_start, address_end;
9216 struct bp_location *bl = loc;
9217 string_file stb;
9219 gdb_assert (bl);
9221 address_start = bl->address;
9222 address_end = address_start + bl->length - 1;
9224 uiout->text ("\taddress range: ");
9225 stb.printf ("[%s, %s]",
9226 print_core_address (bl->gdbarch, address_start),
9227 print_core_address (bl->gdbarch, address_end));
9228 uiout->field_stream ("addr", stb);
9229 uiout->text ("\n");
9232 /* Implement the "print_mention" method for ranged breakpoints. */
9234 void
9235 ranged_breakpoint::print_mention () const
9237 struct bp_location *bl = loc;
9238 struct ui_out *uiout = current_uiout;
9240 gdb_assert (bl);
9241 gdb_assert (type == bp_hardware_breakpoint);
9243 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9244 number, paddress (bl->gdbarch, bl->address),
9245 paddress (bl->gdbarch, bl->address + bl->length - 1));
9248 /* Implement the "print_recreate" method for ranged breakpoints. */
9250 void
9251 ranged_breakpoint::print_recreate (struct ui_file *fp) const
9253 gdb_printf (fp, "break-range %s, %s",
9254 event_location_to_string (location.get ()),
9255 event_location_to_string (location_range_end.get ()));
9256 print_recreate_thread (fp);
9259 /* Find the address where the end of the breakpoint range should be
9260 placed, given the SAL of the end of the range. This is so that if
9261 the user provides a line number, the end of the range is set to the
9262 last instruction of the given line. */
9264 static CORE_ADDR
9265 find_breakpoint_range_end (struct symtab_and_line sal)
9267 CORE_ADDR end;
9269 /* If the user provided a PC value, use it. Otherwise,
9270 find the address of the end of the given location. */
9271 if (sal.explicit_pc)
9272 end = sal.pc;
9273 else
9275 int ret;
9276 CORE_ADDR start;
9278 ret = find_line_pc_range (sal, &start, &end);
9279 if (!ret)
9280 error (_("Could not find location of the end of the range."));
9282 /* find_line_pc_range returns the start of the next line. */
9283 end--;
9286 return end;
9289 /* Implement the "break-range" CLI command. */
9291 static void
9292 break_range_command (const char *arg, int from_tty)
9294 const char *arg_start;
9295 struct linespec_result canonical_start, canonical_end;
9296 int bp_count, can_use_bp, length;
9297 CORE_ADDR end;
9299 /* We don't support software ranged breakpoints. */
9300 if (target_ranged_break_num_registers () < 0)
9301 error (_("This target does not support hardware ranged breakpoints."));
9303 bp_count = hw_breakpoint_used_count ();
9304 bp_count += target_ranged_break_num_registers ();
9305 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9306 bp_count, 0);
9307 if (can_use_bp < 0)
9308 error (_("Hardware breakpoints used exceeds limit."));
9310 arg = skip_spaces (arg);
9311 if (arg == NULL || arg[0] == '\0')
9312 error(_("No address range specified."));
9314 arg_start = arg;
9315 event_location_up start_location = string_to_event_location (&arg,
9316 current_language);
9317 parse_breakpoint_sals (start_location.get (), &canonical_start);
9319 if (arg[0] != ',')
9320 error (_("Too few arguments."));
9321 else if (canonical_start.lsals.empty ())
9322 error (_("Could not find location of the beginning of the range."));
9324 const linespec_sals &lsal_start = canonical_start.lsals[0];
9326 if (canonical_start.lsals.size () > 1
9327 || lsal_start.sals.size () != 1)
9328 error (_("Cannot create a ranged breakpoint with multiple locations."));
9330 const symtab_and_line &sal_start = lsal_start.sals[0];
9331 std::string addr_string_start (arg_start, arg - arg_start);
9333 arg++; /* Skip the comma. */
9334 arg = skip_spaces (arg);
9336 /* Parse the end location. */
9338 arg_start = arg;
9340 /* We call decode_line_full directly here instead of using
9341 parse_breakpoint_sals because we need to specify the start location's
9342 symtab and line as the default symtab and line for the end of the
9343 range. This makes it possible to have ranges like "foo.c:27, +14",
9344 where +14 means 14 lines from the start location. */
9345 event_location_up end_location = string_to_event_location (&arg,
9346 current_language);
9347 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9348 sal_start.symtab, sal_start.line,
9349 &canonical_end, NULL, NULL);
9351 if (canonical_end.lsals.empty ())
9352 error (_("Could not find location of the end of the range."));
9354 const linespec_sals &lsal_end = canonical_end.lsals[0];
9355 if (canonical_end.lsals.size () > 1
9356 || lsal_end.sals.size () != 1)
9357 error (_("Cannot create a ranged breakpoint with multiple locations."));
9359 const symtab_and_line &sal_end = lsal_end.sals[0];
9361 end = find_breakpoint_range_end (sal_end);
9362 if (sal_start.pc > end)
9363 error (_("Invalid address range, end precedes start."));
9365 length = end - sal_start.pc + 1;
9366 if (length < 0)
9367 /* Length overflowed. */
9368 error (_("Address range too large."));
9369 else if (length == 1)
9371 /* This range is simple enough to be handled by
9372 the `hbreak' command. */
9373 hbreak_command (&addr_string_start[0], 1);
9375 return;
9378 /* Now set up the breakpoint and install it. */
9380 std::unique_ptr<breakpoint> br
9381 (new ranged_breakpoint (get_current_arch (),
9382 sal_start, length,
9383 std::move (start_location),
9384 std::move (end_location)));
9386 install_breakpoint (false, std::move (br), true);
9389 /* Return non-zero if EXP is verified as constant. Returned zero
9390 means EXP is variable. Also the constant detection may fail for
9391 some constant expressions and in such case still falsely return
9392 zero. */
9394 static bool
9395 watchpoint_exp_is_const (const struct expression *exp)
9397 return exp->op->constant_p ();
9400 /* Implement the "re_set" method for watchpoints. */
9402 void
9403 watchpoint::re_set ()
9405 /* Watchpoint can be either on expression using entirely global
9406 variables, or it can be on local variables.
9408 Watchpoints of the first kind are never auto-deleted, and even
9409 persist across program restarts. Since they can use variables
9410 from shared libraries, we need to reparse expression as libraries
9411 are loaded and unloaded.
9413 Watchpoints on local variables can also change meaning as result
9414 of solib event. For example, if a watchpoint uses both a local
9415 and a global variables in expression, it's a local watchpoint,
9416 but unloading of a shared library will make the expression
9417 invalid. This is not a very common use case, but we still
9418 re-evaluate expression, to avoid surprises to the user.
9420 Note that for local watchpoints, we re-evaluate it only if
9421 watchpoints frame id is still valid. If it's not, it means the
9422 watchpoint is out of scope and will be deleted soon. In fact,
9423 I'm not sure we'll ever be called in this case.
9425 If a local watchpoint's frame id is still valid, then
9426 exp_valid_block is likewise valid, and we can safely use it.
9428 Don't do anything about disabled watchpoints, since they will be
9429 reevaluated again when enabled. */
9430 update_watchpoint (this, 1 /* reparse */);
9433 /* Implement the "insert" method for hardware watchpoints. */
9436 watchpoint::insert_location (struct bp_location *bl)
9438 int length = exact ? 1 : bl->length;
9440 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9441 cond_exp.get ());
9444 /* Implement the "remove" method for hardware watchpoints. */
9447 watchpoint::remove_location (struct bp_location *bl,
9448 enum remove_bp_reason reason)
9450 int length = exact ? 1 : bl->length;
9452 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9453 cond_exp.get ());
9457 watchpoint::breakpoint_hit (const struct bp_location *bl,
9458 const address_space *aspace, CORE_ADDR bp_addr,
9459 const target_waitstatus &ws)
9461 struct breakpoint *b = bl->owner;
9463 /* Continuable hardware watchpoints are treated as non-existent if the
9464 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9465 some data address). Otherwise gdb won't stop on a break instruction
9466 in the code (not from a breakpoint) when a hardware watchpoint has
9467 been defined. Also skip watchpoints which we know did not trigger
9468 (did not match the data address). */
9469 if (is_hardware_watchpoint (b)
9470 && watchpoint_triggered == watch_triggered_no)
9471 return 0;
9473 return 1;
9476 void
9477 watchpoint::check_status (bpstat *bs)
9479 gdb_assert (is_watchpoint (bs->breakpoint_at));
9481 bpstat_check_watchpoint (bs);
9484 /* Implement the "resources_needed" method for hardware
9485 watchpoints. */
9488 watchpoint::resources_needed (const struct bp_location *bl)
9490 int length = exact? 1 : bl->length;
9492 return target_region_ok_for_hw_watchpoint (bl->address, length);
9495 /* Implement the "works_in_software_mode" method for hardware
9496 watchpoints. */
9498 bool
9499 watchpoint::works_in_software_mode () const
9501 /* Read and access watchpoints only work with hardware support. */
9502 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9505 enum print_stop_action
9506 watchpoint::print_it (const bpstat *bs) const
9508 struct breakpoint *b;
9509 enum print_stop_action result;
9510 struct ui_out *uiout = current_uiout;
9512 gdb_assert (bs->bp_location_at != NULL);
9514 b = bs->breakpoint_at;
9516 annotate_watchpoint (b->number);
9517 maybe_print_thread_hit_breakpoint (uiout);
9519 string_file stb;
9521 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9522 switch (b->type)
9524 case bp_watchpoint:
9525 case bp_hardware_watchpoint:
9526 if (uiout->is_mi_like_p ())
9527 uiout->field_string
9528 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9529 mention (b);
9530 tuple_emitter.emplace (uiout, "value");
9531 uiout->text ("\nOld value = ");
9532 watchpoint_value_print (bs->old_val.get (), &stb);
9533 uiout->field_stream ("old", stb);
9534 uiout->text ("\nNew value = ");
9535 watchpoint_value_print (val.get (), &stb);
9536 uiout->field_stream ("new", stb);
9537 uiout->text ("\n");
9538 /* More than one watchpoint may have been triggered. */
9539 result = PRINT_UNKNOWN;
9540 break;
9542 case bp_read_watchpoint:
9543 if (uiout->is_mi_like_p ())
9544 uiout->field_string
9545 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9546 mention (b);
9547 tuple_emitter.emplace (uiout, "value");
9548 uiout->text ("\nValue = ");
9549 watchpoint_value_print (val.get (), &stb);
9550 uiout->field_stream ("value", stb);
9551 uiout->text ("\n");
9552 result = PRINT_UNKNOWN;
9553 break;
9555 case bp_access_watchpoint:
9556 if (bs->old_val != NULL)
9558 if (uiout->is_mi_like_p ())
9559 uiout->field_string
9560 ("reason",
9561 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9562 mention (b);
9563 tuple_emitter.emplace (uiout, "value");
9564 uiout->text ("\nOld value = ");
9565 watchpoint_value_print (bs->old_val.get (), &stb);
9566 uiout->field_stream ("old", stb);
9567 uiout->text ("\nNew value = ");
9569 else
9571 mention (b);
9572 if (uiout->is_mi_like_p ())
9573 uiout->field_string
9574 ("reason",
9575 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9576 tuple_emitter.emplace (uiout, "value");
9577 uiout->text ("\nValue = ");
9579 watchpoint_value_print (val.get (), &stb);
9580 uiout->field_stream ("new", stb);
9581 uiout->text ("\n");
9582 result = PRINT_UNKNOWN;
9583 break;
9584 default:
9585 result = PRINT_UNKNOWN;
9588 return result;
9591 /* Implement the "print_mention" method for hardware watchpoints. */
9593 void
9594 watchpoint::print_mention () const
9596 struct ui_out *uiout = current_uiout;
9597 const char *tuple_name;
9599 switch (type)
9601 case bp_watchpoint:
9602 uiout->text ("Watchpoint ");
9603 tuple_name = "wpt";
9604 break;
9605 case bp_hardware_watchpoint:
9606 uiout->text ("Hardware watchpoint ");
9607 tuple_name = "wpt";
9608 break;
9609 case bp_read_watchpoint:
9610 uiout->text ("Hardware read watchpoint ");
9611 tuple_name = "hw-rwpt";
9612 break;
9613 case bp_access_watchpoint:
9614 uiout->text ("Hardware access (read/write) watchpoint ");
9615 tuple_name = "hw-awpt";
9616 break;
9617 default:
9618 internal_error (__FILE__, __LINE__,
9619 _("Invalid hardware watchpoint type."));
9622 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9623 uiout->field_signed ("number", number);
9624 uiout->text (": ");
9625 uiout->field_string ("exp", exp_string.get ());
9628 /* Implement the "print_recreate" method for watchpoints. */
9630 void
9631 watchpoint::print_recreate (struct ui_file *fp) const
9633 switch (type)
9635 case bp_watchpoint:
9636 case bp_hardware_watchpoint:
9637 gdb_printf (fp, "watch");
9638 break;
9639 case bp_read_watchpoint:
9640 gdb_printf (fp, "rwatch");
9641 break;
9642 case bp_access_watchpoint:
9643 gdb_printf (fp, "awatch");
9644 break;
9645 default:
9646 internal_error (__FILE__, __LINE__,
9647 _("Invalid watchpoint type."));
9650 gdb_printf (fp, " %s", exp_string.get ());
9651 print_recreate_thread (fp);
9654 /* Implement the "explains_signal" method for watchpoints. */
9656 bool
9657 watchpoint::explains_signal (enum gdb_signal sig)
9659 /* A software watchpoint cannot cause a signal other than
9660 GDB_SIGNAL_TRAP. */
9661 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
9662 return false;
9664 return true;
9667 struct masked_watchpoint : public watchpoint
9669 using watchpoint::watchpoint;
9671 int insert_location (struct bp_location *) override;
9672 int remove_location (struct bp_location *,
9673 enum remove_bp_reason reason) override;
9674 int resources_needed (const struct bp_location *) override;
9675 bool works_in_software_mode () const override;
9676 enum print_stop_action print_it (const bpstat *bs) const override;
9677 void print_one_detail (struct ui_out *) const override;
9678 void print_mention () const override;
9679 void print_recreate (struct ui_file *fp) const override;
9682 /* Implement the "insert" method for masked hardware watchpoints. */
9685 masked_watchpoint::insert_location (struct bp_location *bl)
9687 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
9688 bl->watchpoint_type);
9691 /* Implement the "remove" method for masked hardware watchpoints. */
9694 masked_watchpoint::remove_location (struct bp_location *bl,
9695 enum remove_bp_reason reason)
9697 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
9698 bl->watchpoint_type);
9701 /* Implement the "resources_needed" method for masked hardware
9702 watchpoints. */
9705 masked_watchpoint::resources_needed (const struct bp_location *bl)
9707 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
9710 /* Implement the "works_in_software_mode" method for masked hardware
9711 watchpoints. */
9713 bool
9714 masked_watchpoint::works_in_software_mode () const
9716 return false;
9719 /* Implement the "print_it" method for masked hardware
9720 watchpoints. */
9722 enum print_stop_action
9723 masked_watchpoint::print_it (const bpstat *bs) const
9725 struct breakpoint *b = bs->breakpoint_at;
9726 struct ui_out *uiout = current_uiout;
9728 /* Masked watchpoints have only one location. */
9729 gdb_assert (b->loc && b->loc->next == NULL);
9731 annotate_watchpoint (b->number);
9732 maybe_print_thread_hit_breakpoint (uiout);
9734 switch (b->type)
9736 case bp_hardware_watchpoint:
9737 if (uiout->is_mi_like_p ())
9738 uiout->field_string
9739 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9740 break;
9742 case bp_read_watchpoint:
9743 if (uiout->is_mi_like_p ())
9744 uiout->field_string
9745 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9746 break;
9748 case bp_access_watchpoint:
9749 if (uiout->is_mi_like_p ())
9750 uiout->field_string
9751 ("reason",
9752 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9753 break;
9754 default:
9755 internal_error (__FILE__, __LINE__,
9756 _("Invalid hardware watchpoint type."));
9759 mention (b);
9760 uiout->text (_("\n\
9761 Check the underlying instruction at PC for the memory\n\
9762 address and value which triggered this watchpoint.\n"));
9763 uiout->text ("\n");
9765 /* More than one watchpoint may have been triggered. */
9766 return PRINT_UNKNOWN;
9769 /* Implement the "print_one_detail" method for masked hardware
9770 watchpoints. */
9772 void
9773 masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9775 /* Masked watchpoints have only one location. */
9776 gdb_assert (loc && loc->next == NULL);
9778 uiout->text ("\tmask ");
9779 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
9780 uiout->text ("\n");
9783 /* Implement the "print_mention" method for masked hardware
9784 watchpoints. */
9786 void
9787 masked_watchpoint::print_mention () const
9789 struct ui_out *uiout = current_uiout;
9790 const char *tuple_name;
9792 switch (type)
9794 case bp_hardware_watchpoint:
9795 uiout->text ("Masked hardware watchpoint ");
9796 tuple_name = "wpt";
9797 break;
9798 case bp_read_watchpoint:
9799 uiout->text ("Masked hardware read watchpoint ");
9800 tuple_name = "hw-rwpt";
9801 break;
9802 case bp_access_watchpoint:
9803 uiout->text ("Masked hardware access (read/write) watchpoint ");
9804 tuple_name = "hw-awpt";
9805 break;
9806 default:
9807 internal_error (__FILE__, __LINE__,
9808 _("Invalid hardware watchpoint type."));
9811 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9812 uiout->field_signed ("number", number);
9813 uiout->text (": ");
9814 uiout->field_string ("exp", exp_string.get ());
9817 /* Implement the "print_recreate" method for masked hardware
9818 watchpoints. */
9820 void
9821 masked_watchpoint::print_recreate (struct ui_file *fp) const
9823 switch (type)
9825 case bp_hardware_watchpoint:
9826 gdb_printf (fp, "watch");
9827 break;
9828 case bp_read_watchpoint:
9829 gdb_printf (fp, "rwatch");
9830 break;
9831 case bp_access_watchpoint:
9832 gdb_printf (fp, "awatch");
9833 break;
9834 default:
9835 internal_error (__FILE__, __LINE__,
9836 _("Invalid hardware watchpoint type."));
9839 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
9840 phex (hw_wp_mask, sizeof (CORE_ADDR)));
9841 print_recreate_thread (fp);
9844 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
9846 static bool
9847 is_masked_watchpoint (const struct breakpoint *b)
9849 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9852 /* accessflag: hw_write: watch write,
9853 hw_read: watch read,
9854 hw_access: watch access (read or write) */
9855 static void
9856 watch_command_1 (const char *arg, int accessflag, int from_tty,
9857 bool just_location, bool internal)
9859 struct breakpoint *scope_breakpoint = NULL;
9860 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
9861 struct value *result;
9862 int saved_bitpos = 0, saved_bitsize = 0;
9863 const char *exp_start = NULL;
9864 const char *exp_end = NULL;
9865 const char *tok, *end_tok;
9866 int toklen = -1;
9867 const char *cond_start = NULL;
9868 const char *cond_end = NULL;
9869 enum bptype bp_type;
9870 int thread = -1;
9871 /* Flag to indicate whether we are going to use masks for
9872 the hardware watchpoint. */
9873 bool use_mask = false;
9874 CORE_ADDR mask = 0;
9875 int task = 0;
9877 /* Make sure that we actually have parameters to parse. */
9878 if (arg != NULL && arg[0] != '\0')
9880 const char *value_start;
9882 exp_end = arg + strlen (arg);
9884 /* Look for "parameter value" pairs at the end
9885 of the arguments string. */
9886 for (tok = exp_end - 1; tok > arg; tok--)
9888 /* Skip whitespace at the end of the argument list. */
9889 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9890 tok--;
9892 /* Find the beginning of the last token.
9893 This is the value of the parameter. */
9894 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9895 tok--;
9896 value_start = tok + 1;
9898 /* Skip whitespace. */
9899 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9900 tok--;
9902 end_tok = tok;
9904 /* Find the beginning of the second to last token.
9905 This is the parameter itself. */
9906 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9907 tok--;
9908 tok++;
9909 toklen = end_tok - tok + 1;
9911 if (toklen == 6 && startswith (tok, "thread"))
9913 struct thread_info *thr;
9914 /* At this point we've found a "thread" token, which means
9915 the user is trying to set a watchpoint that triggers
9916 only in a specific thread. */
9917 const char *endp;
9919 if (thread != -1)
9920 error(_("You can specify only one thread."));
9922 /* Extract the thread ID from the next token. */
9923 thr = parse_thread_id (value_start, &endp);
9925 /* Check if the user provided a valid thread ID. */
9926 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9927 invalid_thread_id_error (value_start);
9929 thread = thr->global_num;
9931 else if (toklen == 4 && startswith (tok, "task"))
9933 char *tmp;
9935 task = strtol (value_start, &tmp, 0);
9936 if (tmp == value_start)
9937 error (_("Junk after task keyword."));
9938 if (!valid_task_id (task))
9939 error (_("Unknown task %d."), task);
9941 else if (toklen == 4 && startswith (tok, "mask"))
9943 /* We've found a "mask" token, which means the user wants to
9944 create a hardware watchpoint that is going to have the mask
9945 facility. */
9946 struct value *mask_value, *mark;
9948 if (use_mask)
9949 error(_("You can specify only one mask."));
9951 use_mask = just_location = true;
9953 mark = value_mark ();
9954 mask_value = parse_to_comma_and_eval (&value_start);
9955 mask = value_as_address (mask_value);
9956 value_free_to_mark (mark);
9958 else
9959 /* We didn't recognize what we found. We should stop here. */
9960 break;
9962 /* Truncate the string and get rid of the "parameter value" pair before
9963 the arguments string is parsed by the parse_exp_1 function. */
9964 exp_end = tok;
9967 else
9968 exp_end = arg;
9970 /* Parse the rest of the arguments. From here on out, everything
9971 is in terms of a newly allocated string instead of the original
9972 ARG. */
9973 std::string expression (arg, exp_end - arg);
9974 exp_start = arg = expression.c_str ();
9975 innermost_block_tracker tracker;
9976 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
9977 exp_end = arg;
9978 /* Remove trailing whitespace from the expression before saving it.
9979 This makes the eventual display of the expression string a bit
9980 prettier. */
9981 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
9982 --exp_end;
9984 /* Checking if the expression is not constant. */
9985 if (watchpoint_exp_is_const (exp.get ()))
9987 int len;
9989 len = exp_end - exp_start;
9990 while (len > 0 && isspace (exp_start[len - 1]))
9991 len--;
9992 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
9995 exp_valid_block = tracker.block ();
9996 struct value *mark = value_mark ();
9997 struct value *val_as_value = nullptr;
9998 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
9999 just_location);
10001 if (val_as_value != NULL && just_location)
10003 saved_bitpos = value_bitpos (val_as_value);
10004 saved_bitsize = value_bitsize (val_as_value);
10007 value_ref_ptr val;
10008 if (just_location)
10010 int ret;
10012 exp_valid_block = NULL;
10013 val = release_value (value_addr (result));
10014 value_free_to_mark (mark);
10016 if (use_mask)
10018 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10019 mask);
10020 if (ret == -1)
10021 error (_("This target does not support masked watchpoints."));
10022 else if (ret == -2)
10023 error (_("Invalid mask or memory region."));
10026 else if (val_as_value != NULL)
10027 val = release_value (val_as_value);
10029 tok = skip_spaces (arg);
10030 end_tok = skip_to_space (tok);
10032 toklen = end_tok - tok;
10033 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10035 tok = cond_start = end_tok + 1;
10036 innermost_block_tracker if_tracker;
10037 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10039 /* The watchpoint expression may not be local, but the condition
10040 may still be. E.g.: `watch global if local > 0'. */
10041 cond_exp_valid_block = if_tracker.block ();
10043 cond_end = tok;
10045 if (*tok)
10046 error (_("Junk at end of command."));
10048 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10050 /* Save this because create_internal_breakpoint below invalidates
10051 'wp_frame'. */
10052 frame_id watchpoint_frame = get_frame_id (wp_frame);
10054 /* If the expression is "local", then set up a "watchpoint scope"
10055 breakpoint at the point where we've left the scope of the watchpoint
10056 expression. Create the scope breakpoint before the watchpoint, so
10057 that we will encounter it first in bpstat_stop_status. */
10058 if (exp_valid_block != NULL && wp_frame != NULL)
10060 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10062 if (frame_id_p (caller_frame_id))
10064 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10065 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10067 scope_breakpoint
10068 = create_internal_breakpoint (caller_arch, caller_pc,
10069 bp_watchpoint_scope);
10071 /* create_internal_breakpoint could invalidate WP_FRAME. */
10072 wp_frame = NULL;
10074 scope_breakpoint->enable_state = bp_enabled;
10076 /* Automatically delete the breakpoint when it hits. */
10077 scope_breakpoint->disposition = disp_del;
10079 /* Only break in the proper frame (help with recursion). */
10080 scope_breakpoint->frame_id = caller_frame_id;
10082 /* Set the address at which we will stop. */
10083 scope_breakpoint->loc->gdbarch = caller_arch;
10084 scope_breakpoint->loc->requested_address = caller_pc;
10085 scope_breakpoint->loc->address
10086 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10087 scope_breakpoint->loc->requested_address,
10088 scope_breakpoint->type);
10092 /* Now set up the breakpoint. We create all watchpoints as hardware
10093 watchpoints here even if hardware watchpoints are turned off, a call
10094 to update_watchpoint later in this function will cause the type to
10095 drop back to bp_watchpoint (software watchpoint) if required. */
10097 if (accessflag == hw_read)
10098 bp_type = bp_read_watchpoint;
10099 else if (accessflag == hw_access)
10100 bp_type = bp_access_watchpoint;
10101 else
10102 bp_type = bp_hardware_watchpoint;
10104 std::unique_ptr<watchpoint> w;
10105 if (use_mask)
10106 w.reset (new masked_watchpoint (nullptr, bp_type));
10107 else
10108 w.reset (new watchpoint (nullptr, bp_type));
10110 w->thread = thread;
10111 w->task = task;
10112 w->disposition = disp_donttouch;
10113 w->pspace = current_program_space;
10114 w->exp = std::move (exp);
10115 w->exp_valid_block = exp_valid_block;
10116 w->cond_exp_valid_block = cond_exp_valid_block;
10117 if (just_location)
10119 struct type *t = value_type (val.get ());
10120 CORE_ADDR addr = value_as_address (val.get ());
10122 w->exp_string_reparse
10123 = current_language->watch_location_expression (t, addr);
10125 w->exp_string = xstrprintf ("-location %.*s",
10126 (int) (exp_end - exp_start), exp_start);
10128 else
10129 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10131 if (use_mask)
10133 w->hw_wp_mask = mask;
10135 else
10137 w->val = val;
10138 w->val_bitpos = saved_bitpos;
10139 w->val_bitsize = saved_bitsize;
10140 w->val_valid = true;
10143 if (cond_start)
10144 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10145 else
10146 w->cond_string = 0;
10148 if (frame_id_p (watchpoint_frame))
10150 w->watchpoint_frame = watchpoint_frame;
10151 w->watchpoint_thread = inferior_ptid;
10153 else
10155 w->watchpoint_frame = null_frame_id;
10156 w->watchpoint_thread = null_ptid;
10159 if (scope_breakpoint != NULL)
10161 /* The scope breakpoint is related to the watchpoint. We will
10162 need to act on them together. */
10163 w->related_breakpoint = scope_breakpoint;
10164 scope_breakpoint->related_breakpoint = w.get ();
10167 if (!just_location)
10168 value_free_to_mark (mark);
10170 /* Finally update the new watchpoint. This creates the locations
10171 that should be inserted. */
10172 update_watchpoint (w.get (), 1);
10174 install_breakpoint (internal, std::move (w), 1);
10177 /* Return count of debug registers needed to watch the given expression.
10178 If the watchpoint cannot be handled in hardware return zero. */
10180 static int
10181 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10183 int found_memory_cnt = 0;
10185 /* Did the user specifically forbid us to use hardware watchpoints? */
10186 if (!can_use_hw_watchpoints)
10187 return 0;
10189 gdb_assert (!vals.empty ());
10190 struct value *head = vals[0].get ();
10192 /* Make sure that the value of the expression depends only upon
10193 memory contents, and values computed from them within GDB. If we
10194 find any register references or function calls, we can't use a
10195 hardware watchpoint.
10197 The idea here is that evaluating an expression generates a series
10198 of values, one holding the value of every subexpression. (The
10199 expression a*b+c has five subexpressions: a, b, a*b, c, and
10200 a*b+c.) GDB's values hold almost enough information to establish
10201 the criteria given above --- they identify memory lvalues,
10202 register lvalues, computed values, etcetera. So we can evaluate
10203 the expression, and then scan the chain of values that leaves
10204 behind to decide whether we can detect any possible change to the
10205 expression's final value using only hardware watchpoints.
10207 However, I don't think that the values returned by inferior
10208 function calls are special in any way. So this function may not
10209 notice that an expression involving an inferior function call
10210 can't be watched with hardware watchpoints. FIXME. */
10211 for (const value_ref_ptr &iter : vals)
10213 struct value *v = iter.get ();
10215 if (VALUE_LVAL (v) == lval_memory)
10217 if (v != head && value_lazy (v))
10218 /* A lazy memory lvalue in the chain is one that GDB never
10219 needed to fetch; we either just used its address (e.g.,
10220 `a' in `a.b') or we never needed it at all (e.g., `a'
10221 in `a,b'). This doesn't apply to HEAD; if that is
10222 lazy then it was not readable, but watch it anyway. */
10224 else
10226 /* Ahh, memory we actually used! Check if we can cover
10227 it with hardware watchpoints. */
10228 struct type *vtype = check_typedef (value_type (v));
10230 /* We only watch structs and arrays if user asked for it
10231 explicitly, never if they just happen to appear in a
10232 middle of some value chain. */
10233 if (v == head
10234 || (vtype->code () != TYPE_CODE_STRUCT
10235 && vtype->code () != TYPE_CODE_ARRAY))
10237 CORE_ADDR vaddr = value_address (v);
10238 int len;
10239 int num_regs;
10241 len = (target_exact_watchpoints
10242 && is_scalar_type_recursive (vtype))?
10243 1 : TYPE_LENGTH (value_type (v));
10245 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10246 if (!num_regs)
10247 return 0;
10248 else
10249 found_memory_cnt += num_regs;
10253 else if (VALUE_LVAL (v) != not_lval
10254 && deprecated_value_modifiable (v) == 0)
10255 return 0; /* These are values from the history (e.g., $1). */
10256 else if (VALUE_LVAL (v) == lval_register)
10257 return 0; /* Cannot watch a register with a HW watchpoint. */
10260 /* The expression itself looks suitable for using a hardware
10261 watchpoint, but give the target machine a chance to reject it. */
10262 return found_memory_cnt;
10265 void
10266 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10268 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10271 /* Options for the watch, awatch, and rwatch commands. */
10273 struct watch_options
10275 /* For -location. */
10276 bool location = false;
10279 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10281 Historically GDB always accepted both '-location' and '-l' flags for
10282 these commands (both flags being synonyms). When converting to the
10283 newer option scheme only '-location' is added here. That's fine (for
10284 backward compatibility) as any non-ambiguous prefix of a flag will be
10285 accepted, so '-l', '-loc', are now all accepted.
10287 What this means is that, if in the future, we add any new flag here
10288 that starts with '-l' then this will break backward compatibility, so
10289 please, don't do that! */
10291 static const gdb::option::option_def watch_option_defs[] = {
10292 gdb::option::flag_option_def<watch_options> {
10293 "location",
10294 [] (watch_options *opt) { return &opt->location; },
10295 N_("\
10296 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10297 -l can be used as a short form of -location."),
10301 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10302 commands. */
10304 static gdb::option::option_def_group
10305 make_watch_options_def_group (watch_options *opts)
10307 return {{watch_option_defs}, opts};
10310 /* A helper function that looks for the "-location" argument and then
10311 calls watch_command_1. */
10313 static void
10314 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10316 watch_options opts;
10317 auto grp = make_watch_options_def_group (&opts);
10318 gdb::option::process_options
10319 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10320 if (arg != nullptr && *arg == '\0')
10321 arg = nullptr;
10323 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10326 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10327 static void
10328 watch_command_completer (struct cmd_list_element *ignore,
10329 completion_tracker &tracker,
10330 const char *text, const char * /*word*/)
10332 const auto group = make_watch_options_def_group (nullptr);
10333 if (gdb::option::complete_options
10334 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10335 return;
10337 const char *word = advance_to_expression_complete_word_point (tracker, text);
10338 expression_completer (ignore, tracker, text, word);
10341 static void
10342 watch_command (const char *arg, int from_tty)
10344 watch_maybe_just_location (arg, hw_write, from_tty);
10347 void
10348 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10350 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10353 static void
10354 rwatch_command (const char *arg, int from_tty)
10356 watch_maybe_just_location (arg, hw_read, from_tty);
10359 void
10360 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
10362 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10365 static void
10366 awatch_command (const char *arg, int from_tty)
10368 watch_maybe_just_location (arg, hw_access, from_tty);
10372 /* Data for the FSM that manages the until(location)/advance commands
10373 in infcmd.c. Here because it uses the mechanisms of
10374 breakpoints. */
10376 struct until_break_fsm : public thread_fsm
10378 /* The thread that was current when the command was executed. */
10379 int thread;
10381 /* The breakpoint set at the return address in the caller frame,
10382 plus breakpoints at all the destination locations. */
10383 std::vector<breakpoint_up> breakpoints;
10385 until_break_fsm (struct interp *cmd_interp, int thread,
10386 std::vector<breakpoint_up> &&breakpoints)
10387 : thread_fsm (cmd_interp),
10388 thread (thread),
10389 breakpoints (std::move (breakpoints))
10393 void clean_up (struct thread_info *thread) override;
10394 bool should_stop (struct thread_info *thread) override;
10395 enum async_reply_reason do_async_reply_reason () override;
10398 /* Implementation of the 'should_stop' FSM method for the
10399 until(location)/advance commands. */
10401 bool
10402 until_break_fsm::should_stop (struct thread_info *tp)
10404 for (const breakpoint_up &bp : breakpoints)
10405 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10406 bp.get ()) != NULL)
10408 set_finished ();
10409 break;
10412 return true;
10415 /* Implementation of the 'clean_up' FSM method for the
10416 until(location)/advance commands. */
10418 void
10419 until_break_fsm::clean_up (struct thread_info *)
10421 /* Clean up our temporary breakpoints. */
10422 breakpoints.clear ();
10423 delete_longjmp_breakpoint (thread);
10426 /* Implementation of the 'async_reply_reason' FSM method for the
10427 until(location)/advance commands. */
10429 enum async_reply_reason
10430 until_break_fsm::do_async_reply_reason ()
10432 return EXEC_ASYNC_LOCATION_REACHED;
10435 void
10436 until_break_command (const char *arg, int from_tty, int anywhere)
10438 struct frame_info *frame;
10439 struct gdbarch *frame_gdbarch;
10440 struct frame_id stack_frame_id;
10441 struct frame_id caller_frame_id;
10442 int thread;
10443 struct thread_info *tp;
10445 clear_proceed_status (0);
10447 /* Set a breakpoint where the user wants it and at return from
10448 this function. */
10450 event_location_up location = string_to_event_location (&arg, current_language);
10452 std::vector<symtab_and_line> sals
10453 = (last_displayed_sal_is_valid ()
10454 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10455 get_last_displayed_symtab (),
10456 get_last_displayed_line ())
10457 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
10458 NULL, NULL, 0));
10460 if (sals.empty ())
10461 error (_("Couldn't get information on specified line."));
10463 if (*arg)
10464 error (_("Junk at end of arguments."));
10466 tp = inferior_thread ();
10467 thread = tp->global_num;
10469 /* Note linespec handling above invalidates the frame chain.
10470 Installing a breakpoint also invalidates the frame chain (as it
10471 may need to switch threads), so do any frame handling before
10472 that. */
10474 frame = get_selected_frame (NULL);
10475 frame_gdbarch = get_frame_arch (frame);
10476 stack_frame_id = get_stack_frame_id (frame);
10477 caller_frame_id = frame_unwind_caller_id (frame);
10479 /* Keep within the current frame, or in frames called by the current
10480 one. */
10482 std::vector<breakpoint_up> breakpoints;
10484 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10486 if (frame_id_p (caller_frame_id))
10488 struct symtab_and_line sal2;
10489 struct gdbarch *caller_gdbarch;
10491 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10492 sal2.pc = frame_unwind_caller_pc (frame);
10493 caller_gdbarch = frame_unwind_caller_arch (frame);
10495 breakpoint_up caller_breakpoint
10496 = set_momentary_breakpoint (caller_gdbarch, sal2,
10497 caller_frame_id, bp_until);
10498 breakpoints.emplace_back (std::move (caller_breakpoint));
10500 set_longjmp_breakpoint (tp, caller_frame_id);
10501 lj_deleter.emplace (thread);
10504 /* set_momentary_breakpoint could invalidate FRAME. */
10505 frame = NULL;
10507 /* If the user told us to continue until a specified location, we
10508 don't specify a frame at which we need to stop. Otherwise,
10509 specify the selected frame, because we want to stop only at the
10510 very same frame. */
10511 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10513 for (symtab_and_line &sal : sals)
10515 resolve_sal_pc (&sal);
10517 breakpoint_up location_breakpoint
10518 = set_momentary_breakpoint (frame_gdbarch, sal,
10519 stop_frame_id, bp_until);
10520 breakpoints.emplace_back (std::move (location_breakpoint));
10523 tp->set_thread_fsm
10524 (std::unique_ptr<thread_fsm>
10525 (new until_break_fsm (command_interp (), tp->global_num,
10526 std::move (breakpoints))));
10528 if (lj_deleter)
10529 lj_deleter->release ();
10531 proceed (-1, GDB_SIGNAL_DEFAULT);
10536 /* Compare two breakpoints and return a strcmp-like result. */
10538 static int
10539 compare_breakpoints (const breakpoint *a, const breakpoint *b)
10541 uintptr_t ua = (uintptr_t) a;
10542 uintptr_t ub = (uintptr_t) b;
10544 if (a->number < b->number)
10545 return -1;
10546 else if (a->number > b->number)
10547 return 1;
10549 /* Now sort by address, in case we see, e..g, two breakpoints with
10550 the number 0. */
10551 if (ua < ub)
10552 return -1;
10553 return ua > ub ? 1 : 0;
10556 /* Delete breakpoints by address or line. */
10558 static void
10559 clear_command (const char *arg, int from_tty)
10561 int default_match;
10563 std::vector<symtab_and_line> decoded_sals;
10564 symtab_and_line last_sal;
10565 gdb::array_view<symtab_and_line> sals;
10566 if (arg)
10568 decoded_sals
10569 = decode_line_with_current_source (arg,
10570 (DECODE_LINE_FUNFIRSTLINE
10571 | DECODE_LINE_LIST_MODE));
10572 default_match = 0;
10573 sals = decoded_sals;
10575 else
10577 /* Set sal's line, symtab, pc, and pspace to the values
10578 corresponding to the last call to print_frame_info. If the
10579 codepoint is not valid, this will set all the fields to 0. */
10580 last_sal = get_last_displayed_sal ();
10581 if (last_sal.symtab == 0)
10582 error (_("No source file specified."));
10584 default_match = 1;
10585 sals = last_sal;
10588 /* We don't call resolve_sal_pc here. That's not as bad as it
10589 seems, because all existing breakpoints typically have both
10590 file/line and pc set. So, if clear is given file/line, we can
10591 match this to existing breakpoint without obtaining pc at all.
10593 We only support clearing given the address explicitly
10594 present in breakpoint table. Say, we've set breakpoint
10595 at file:line. There were several PC values for that file:line,
10596 due to optimization, all in one block.
10598 We've picked one PC value. If "clear" is issued with another
10599 PC corresponding to the same file:line, the breakpoint won't
10600 be cleared. We probably can still clear the breakpoint, but
10601 since the other PC value is never presented to user, user
10602 can only find it by guessing, and it does not seem important
10603 to support that. */
10605 /* For each line spec given, delete bps which correspond to it. Do
10606 it in two passes, solely to preserve the current behavior that
10607 from_tty is forced true if we delete more than one
10608 breakpoint. */
10610 std::vector<struct breakpoint *> found;
10611 for (const auto &sal : sals)
10613 const char *sal_fullname;
10615 /* If exact pc given, clear bpts at that pc.
10616 If line given (pc == 0), clear all bpts on specified line.
10617 If defaulting, clear all bpts on default line
10618 or at default pc.
10620 defaulting sal.pc != 0 tests to do
10622 0 1 pc
10623 1 1 pc _and_ line
10624 0 0 line
10625 1 0 <can't happen> */
10627 sal_fullname = (sal.symtab == NULL
10628 ? NULL : symtab_to_fullname (sal.symtab));
10630 /* Find all matching breakpoints and add them to 'found'. */
10631 for (breakpoint *b : all_breakpoints ())
10633 int match = 0;
10634 /* Are we going to delete b? */
10635 if (b->type != bp_none && !is_watchpoint (b)
10636 && user_breakpoint_p (b))
10638 for (bp_location *loc : b->locations ())
10640 /* If the user specified file:line, don't allow a PC
10641 match. This matches historical gdb behavior. */
10642 int pc_match = (!sal.explicit_line
10643 && sal.pc
10644 && (loc->pspace == sal.pspace)
10645 && (loc->address == sal.pc)
10646 && (!section_is_overlay (loc->section)
10647 || loc->section == sal.section));
10648 int line_match = 0;
10650 if ((default_match || sal.explicit_line)
10651 && loc->symtab != NULL
10652 && sal_fullname != NULL
10653 && sal.pspace == loc->pspace
10654 && loc->line_number == sal.line
10655 && filename_cmp (symtab_to_fullname (loc->symtab),
10656 sal_fullname) == 0)
10657 line_match = 1;
10659 if (pc_match || line_match)
10661 match = 1;
10662 break;
10667 if (match)
10668 found.push_back (b);
10672 /* Now go thru the 'found' chain and delete them. */
10673 if (found.empty ())
10675 if (arg)
10676 error (_("No breakpoint at %s."), arg);
10677 else
10678 error (_("No breakpoint at this line."));
10681 /* Remove duplicates from the vec. */
10682 std::sort (found.begin (), found.end (),
10683 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10685 return compare_breakpoints (bp_a, bp_b) < 0;
10687 found.erase (std::unique (found.begin (), found.end (),
10688 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10690 return compare_breakpoints (bp_a, bp_b) == 0;
10692 found.end ());
10694 if (found.size () > 1)
10695 from_tty = 1; /* Always report if deleted more than one. */
10696 if (from_tty)
10698 if (found.size () == 1)
10699 gdb_printf (_("Deleted breakpoint "));
10700 else
10701 gdb_printf (_("Deleted breakpoints "));
10704 for (breakpoint *iter : found)
10706 if (from_tty)
10707 gdb_printf ("%d ", iter->number);
10708 delete_breakpoint (iter);
10710 if (from_tty)
10711 gdb_putc ('\n');
10714 /* Delete breakpoint in BS if they are `delete' breakpoints and
10715 all breakpoints that are marked for deletion, whether hit or not.
10716 This is called after any breakpoint is hit, or after errors. */
10718 void
10719 breakpoint_auto_delete (bpstat *bs)
10721 for (; bs; bs = bs->next)
10722 if (bs->breakpoint_at
10723 && bs->breakpoint_at->disposition == disp_del
10724 && bs->stop)
10725 delete_breakpoint (bs->breakpoint_at);
10727 for (breakpoint *b : all_breakpoints_safe ())
10728 if (b->disposition == disp_del_at_next_stop)
10729 delete_breakpoint (b);
10732 /* A comparison function for bp_location AP and BP being interfaced to
10733 std::sort. Sort elements primarily by their ADDRESS (no matter what
10734 bl_address_is_meaningful says), secondarily by ordering first
10735 permanent elements and terciarily just ensuring the array is sorted
10736 stable way despite std::sort being an unstable algorithm. */
10738 static int
10739 bp_location_is_less_than (const bp_location *a, const bp_location *b)
10741 if (a->address != b->address)
10742 return a->address < b->address;
10744 /* Sort locations at the same address by their pspace number, keeping
10745 locations of the same inferior (in a multi-inferior environment)
10746 grouped. */
10748 if (a->pspace->num != b->pspace->num)
10749 return a->pspace->num < b->pspace->num;
10751 /* Sort permanent breakpoints first. */
10752 if (a->permanent != b->permanent)
10753 return a->permanent > b->permanent;
10755 /* Sort by type in order to make duplicate determination easier.
10756 See update_global_location_list. This is kept in sync with
10757 breakpoint_locations_match. */
10758 if (a->loc_type < b->loc_type)
10759 return true;
10761 /* Likewise, for range-breakpoints, sort by length. */
10762 if (a->loc_type == bp_loc_hardware_breakpoint
10763 && b->loc_type == bp_loc_hardware_breakpoint
10764 && a->length < b->length)
10765 return true;
10767 /* Make the internal GDB representation stable across GDB runs
10768 where A and B memory inside GDB can differ. Breakpoint locations of
10769 the same type at the same address can be sorted in arbitrary order. */
10771 if (a->owner->number != b->owner->number)
10772 return a->owner->number < b->owner->number;
10774 return a < b;
10777 /* Set bp_locations_placed_address_before_address_max and
10778 bp_locations_shadow_len_after_address_max according to the current
10779 content of the bp_locations array. */
10781 static void
10782 bp_locations_target_extensions_update (void)
10784 bp_locations_placed_address_before_address_max = 0;
10785 bp_locations_shadow_len_after_address_max = 0;
10787 for (bp_location *bl : all_bp_locations ())
10789 CORE_ADDR start, end, addr;
10791 if (!bp_location_has_shadow (bl))
10792 continue;
10794 start = bl->target_info.placed_address;
10795 end = start + bl->target_info.shadow_len;
10797 gdb_assert (bl->address >= start);
10798 addr = bl->address - start;
10799 if (addr > bp_locations_placed_address_before_address_max)
10800 bp_locations_placed_address_before_address_max = addr;
10802 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10804 gdb_assert (bl->address < end);
10805 addr = end - bl->address;
10806 if (addr > bp_locations_shadow_len_after_address_max)
10807 bp_locations_shadow_len_after_address_max = addr;
10811 /* Download tracepoint locations if they haven't been. */
10813 static void
10814 download_tracepoint_locations (void)
10816 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
10818 scoped_restore_current_pspace_and_thread restore_pspace_thread;
10820 for (breakpoint *b : all_tracepoints ())
10822 struct tracepoint *t;
10823 int bp_location_downloaded = 0;
10825 if ((b->type == bp_fast_tracepoint
10826 ? !may_insert_fast_tracepoints
10827 : !may_insert_tracepoints))
10828 continue;
10830 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
10832 if (target_can_download_tracepoint ())
10833 can_download_tracepoint = TRIBOOL_TRUE;
10834 else
10835 can_download_tracepoint = TRIBOOL_FALSE;
10838 if (can_download_tracepoint == TRIBOOL_FALSE)
10839 break;
10841 for (bp_location *bl : b->locations ())
10843 /* In tracepoint, locations are _never_ duplicated, so
10844 should_be_inserted is equivalent to
10845 unduplicated_should_be_inserted. */
10846 if (!should_be_inserted (bl) || bl->inserted)
10847 continue;
10849 switch_to_program_space_and_thread (bl->pspace);
10851 target_download_tracepoint (bl);
10853 bl->inserted = 1;
10854 bp_location_downloaded = 1;
10856 t = (struct tracepoint *) b;
10857 t->number_on_target = b->number;
10858 if (bp_location_downloaded)
10859 gdb::observers::breakpoint_modified.notify (b);
10863 /* Swap the insertion/duplication state between two locations. */
10865 static void
10866 swap_insertion (struct bp_location *left, struct bp_location *right)
10868 const int left_inserted = left->inserted;
10869 const int left_duplicate = left->duplicate;
10870 const int left_needs_update = left->needs_update;
10871 const struct bp_target_info left_target_info = left->target_info;
10873 /* Locations of tracepoints can never be duplicated. */
10874 if (is_tracepoint (left->owner))
10875 gdb_assert (!left->duplicate);
10876 if (is_tracepoint (right->owner))
10877 gdb_assert (!right->duplicate);
10879 left->inserted = right->inserted;
10880 left->duplicate = right->duplicate;
10881 left->needs_update = right->needs_update;
10882 left->target_info = right->target_info;
10883 right->inserted = left_inserted;
10884 right->duplicate = left_duplicate;
10885 right->needs_update = left_needs_update;
10886 right->target_info = left_target_info;
10889 /* Force the re-insertion of the locations at ADDRESS. This is called
10890 once a new/deleted/modified duplicate location is found and we are evaluating
10891 conditions on the target's side. Such conditions need to be updated on
10892 the target. */
10894 static void
10895 force_breakpoint_reinsertion (struct bp_location *bl)
10897 CORE_ADDR address = 0;
10898 int pspace_num;
10900 address = bl->address;
10901 pspace_num = bl->pspace->num;
10903 /* This is only meaningful if the target is
10904 evaluating conditions and if the user has
10905 opted for condition evaluation on the target's
10906 side. */
10907 if (gdb_evaluates_breakpoint_condition_p ()
10908 || !target_supports_evaluation_of_breakpoint_conditions ())
10909 return;
10911 /* Flag all breakpoint locations with this address and
10912 the same program space as the location
10913 as "its condition has changed". We need to
10914 update the conditions on the target's side. */
10915 for (bp_location *loc : all_bp_locations_at_addr (address))
10917 if (!is_breakpoint (loc->owner)
10918 || pspace_num != loc->pspace->num)
10919 continue;
10921 /* Flag the location appropriately. We use a different state to
10922 let everyone know that we already updated the set of locations
10923 with addr bl->address and program space bl->pspace. This is so
10924 we don't have to keep calling these functions just to mark locations
10925 that have already been marked. */
10926 loc->condition_changed = condition_updated;
10928 /* Free the agent expression bytecode as well. We will compute
10929 it later on. */
10930 loc->cond_bytecode.reset ();
10934 /* Called whether new breakpoints are created, or existing breakpoints
10935 deleted, to update the global location list and recompute which
10936 locations are duplicate of which.
10938 The INSERT_MODE flag determines whether locations may not, may, or
10939 shall be inserted now. See 'enum ugll_insert_mode' for more
10940 info. */
10942 static void
10943 update_global_location_list (enum ugll_insert_mode insert_mode)
10945 /* Last breakpoint location address that was marked for update. */
10946 CORE_ADDR last_addr = 0;
10947 /* Last breakpoint location program space that was marked for update. */
10948 int last_pspace_num = -1;
10950 /* Used in the duplicates detection below. When iterating over all
10951 bp_locations, points to the first bp_location of a given address.
10952 Breakpoints and watchpoints of different types are never
10953 duplicates of each other. Keep one pointer for each type of
10954 breakpoint/watchpoint, so we only need to loop over all locations
10955 once. */
10956 struct bp_location *bp_loc_first; /* breakpoint */
10957 struct bp_location *wp_loc_first; /* hardware watchpoint */
10958 struct bp_location *awp_loc_first; /* access watchpoint */
10959 struct bp_location *rwp_loc_first; /* read watchpoint */
10961 /* Saved former bp_locations array which we compare against the newly
10962 built bp_locations from the current state of ALL_BREAKPOINTS. */
10963 std::vector<bp_location *> old_locations = std::move (bp_locations);
10964 bp_locations.clear ();
10966 for (breakpoint *b : all_breakpoints ())
10967 for (bp_location *loc : b->locations ())
10968 bp_locations.push_back (loc);
10970 /* See if we need to "upgrade" a software breakpoint to a hardware
10971 breakpoint. Do this before deciding whether locations are
10972 duplicates. Also do this before sorting because sorting order
10973 depends on location type. */
10974 for (bp_location *loc : bp_locations)
10975 if (!loc->inserted && should_be_inserted (loc))
10976 handle_automatic_hardware_breakpoints (loc);
10978 std::sort (bp_locations.begin (), bp_locations.end (),
10979 bp_location_is_less_than);
10981 bp_locations_target_extensions_update ();
10983 /* Identify bp_location instances that are no longer present in the
10984 new list, and therefore should be freed. Note that it's not
10985 necessary that those locations should be removed from inferior --
10986 if there's another location at the same address (previously
10987 marked as duplicate), we don't need to remove/insert the
10988 location.
10990 LOCP is kept in sync with OLD_LOCP, each pointing to the current
10991 and former bp_location array state respectively. */
10993 size_t loc_i = 0;
10994 for (bp_location *old_loc : old_locations)
10996 /* Tells if 'old_loc' is found among the new locations. If
10997 not, we have to free it. */
10998 int found_object = 0;
10999 /* Tells if the location should remain inserted in the target. */
11000 int keep_in_target = 0;
11001 int removed = 0;
11003 /* Skip LOCP entries which will definitely never be needed.
11004 Stop either at or being the one matching OLD_LOC. */
11005 while (loc_i < bp_locations.size ()
11006 && bp_locations[loc_i]->address < old_loc->address)
11007 loc_i++;
11009 for (size_t loc2_i = loc_i;
11010 (loc2_i < bp_locations.size ()
11011 && bp_locations[loc2_i]->address == old_loc->address);
11012 loc2_i++)
11014 /* Check if this is a new/duplicated location or a duplicated
11015 location that had its condition modified. If so, we want to send
11016 its condition to the target if evaluation of conditions is taking
11017 place there. */
11018 if (bp_locations[loc2_i]->condition_changed == condition_modified
11019 && (last_addr != old_loc->address
11020 || last_pspace_num != old_loc->pspace->num))
11022 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11023 last_pspace_num = old_loc->pspace->num;
11026 if (bp_locations[loc2_i] == old_loc)
11027 found_object = 1;
11030 /* We have already handled this address, update it so that we don't
11031 have to go through updates again. */
11032 last_addr = old_loc->address;
11034 /* Target-side condition evaluation: Handle deleted locations. */
11035 if (!found_object)
11036 force_breakpoint_reinsertion (old_loc);
11038 /* If this location is no longer present, and inserted, look if
11039 there's maybe a new location at the same address. If so,
11040 mark that one inserted, and don't remove this one. This is
11041 needed so that we don't have a time window where a breakpoint
11042 at certain location is not inserted. */
11044 if (old_loc->inserted)
11046 /* If the location is inserted now, we might have to remove
11047 it. */
11049 if (found_object && should_be_inserted (old_loc))
11051 /* The location is still present in the location list,
11052 and still should be inserted. Don't do anything. */
11053 keep_in_target = 1;
11055 else
11057 /* This location still exists, but it won't be kept in the
11058 target since it may have been disabled. We proceed to
11059 remove its target-side condition. */
11061 /* The location is either no longer present, or got
11062 disabled. See if there's another location at the
11063 same address, in which case we don't need to remove
11064 this one from the target. */
11066 /* OLD_LOC comes from existing struct breakpoint. */
11067 if (bl_address_is_meaningful (old_loc))
11069 for (size_t loc2_i = loc_i;
11070 (loc2_i < bp_locations.size ()
11071 && bp_locations[loc2_i]->address == old_loc->address);
11072 loc2_i++)
11074 bp_location *loc2 = bp_locations[loc2_i];
11076 if (loc2 == old_loc)
11077 continue;
11079 if (breakpoint_locations_match (loc2, old_loc))
11081 /* Read watchpoint locations are switched to
11082 access watchpoints, if the former are not
11083 supported, but the latter are. */
11084 if (is_hardware_watchpoint (old_loc->owner))
11086 gdb_assert (is_hardware_watchpoint (loc2->owner));
11087 loc2->watchpoint_type = old_loc->watchpoint_type;
11090 /* loc2 is a duplicated location. We need to check
11091 if it should be inserted in case it will be
11092 unduplicated. */
11093 if (unduplicated_should_be_inserted (loc2))
11095 swap_insertion (old_loc, loc2);
11096 keep_in_target = 1;
11097 break;
11104 if (!keep_in_target)
11106 if (remove_breakpoint (old_loc))
11108 /* This is just about all we can do. We could keep
11109 this location on the global list, and try to
11110 remove it next time, but there's no particular
11111 reason why we will succeed next time.
11113 Note that at this point, old_loc->owner is still
11114 valid, as delete_breakpoint frees the breakpoint
11115 only after calling us. */
11116 gdb_printf (_("warning: Error removing "
11117 "breakpoint %d\n"),
11118 old_loc->owner->number);
11120 removed = 1;
11124 if (!found_object)
11126 if (removed && target_is_non_stop_p ()
11127 && need_moribund_for_location_type (old_loc))
11129 /* This location was removed from the target. In
11130 non-stop mode, a race condition is possible where
11131 we've removed a breakpoint, but stop events for that
11132 breakpoint are already queued and will arrive later.
11133 We apply an heuristic to be able to distinguish such
11134 SIGTRAPs from other random SIGTRAPs: we keep this
11135 breakpoint location for a bit, and will retire it
11136 after we see some number of events. The theory here
11137 is that reporting of events should, "on the average",
11138 be fair, so after a while we'll see events from all
11139 threads that have anything of interest, and no longer
11140 need to keep this breakpoint location around. We
11141 don't hold locations forever so to reduce chances of
11142 mistaking a non-breakpoint SIGTRAP for a breakpoint
11143 SIGTRAP.
11145 The heuristic failing can be disastrous on
11146 decr_pc_after_break targets.
11148 On decr_pc_after_break targets, like e.g., x86-linux,
11149 if we fail to recognize a late breakpoint SIGTRAP,
11150 because events_till_retirement has reached 0 too
11151 soon, we'll fail to do the PC adjustment, and report
11152 a random SIGTRAP to the user. When the user resumes
11153 the inferior, it will most likely immediately crash
11154 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11155 corrupted, because of being resumed e.g., in the
11156 middle of a multi-byte instruction, or skipped a
11157 one-byte instruction. This was actually seen happen
11158 on native x86-linux, and should be less rare on
11159 targets that do not support new thread events, like
11160 remote, due to the heuristic depending on
11161 thread_count.
11163 Mistaking a random SIGTRAP for a breakpoint trap
11164 causes similar symptoms (PC adjustment applied when
11165 it shouldn't), but then again, playing with SIGTRAPs
11166 behind the debugger's back is asking for trouble.
11168 Since hardware watchpoint traps are always
11169 distinguishable from other traps, so we don't need to
11170 apply keep hardware watchpoint moribund locations
11171 around. We simply always ignore hardware watchpoint
11172 traps we can no longer explain. */
11174 process_stratum_target *proc_target = nullptr;
11175 for (inferior *inf : all_inferiors ())
11176 if (inf->pspace == old_loc->pspace)
11178 proc_target = inf->process_target ();
11179 break;
11181 if (proc_target != nullptr)
11182 old_loc->events_till_retirement
11183 = 3 * (thread_count (proc_target) + 1);
11184 else
11185 old_loc->events_till_retirement = 1;
11186 old_loc->owner = NULL;
11188 moribund_locations.push_back (old_loc);
11190 else
11192 old_loc->owner = NULL;
11193 decref_bp_location (&old_loc);
11198 /* Rescan breakpoints at the same address and section, marking the
11199 first one as "first" and any others as "duplicates". This is so
11200 that the bpt instruction is only inserted once. If we have a
11201 permanent breakpoint at the same place as BPT, make that one the
11202 official one, and the rest as duplicates. Permanent breakpoints
11203 are sorted first for the same address.
11205 Do the same for hardware watchpoints, but also considering the
11206 watchpoint's type (regular/access/read) and length. */
11208 bp_loc_first = NULL;
11209 wp_loc_first = NULL;
11210 awp_loc_first = NULL;
11211 rwp_loc_first = NULL;
11213 for (bp_location *loc : all_bp_locations ())
11215 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11216 non-NULL. */
11217 struct bp_location **loc_first_p;
11218 breakpoint *b = loc->owner;
11220 if (!unduplicated_should_be_inserted (loc)
11221 || !bl_address_is_meaningful (loc)
11222 /* Don't detect duplicate for tracepoint locations because they are
11223 never duplicated. See the comments in field `duplicate' of
11224 `struct bp_location'. */
11225 || is_tracepoint (b))
11227 /* Clear the condition modification flag. */
11228 loc->condition_changed = condition_unchanged;
11229 continue;
11232 if (b->type == bp_hardware_watchpoint)
11233 loc_first_p = &wp_loc_first;
11234 else if (b->type == bp_read_watchpoint)
11235 loc_first_p = &rwp_loc_first;
11236 else if (b->type == bp_access_watchpoint)
11237 loc_first_p = &awp_loc_first;
11238 else
11239 loc_first_p = &bp_loc_first;
11241 if (*loc_first_p == NULL
11242 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11243 || !breakpoint_locations_match (loc, *loc_first_p))
11245 *loc_first_p = loc;
11246 loc->duplicate = 0;
11248 if (is_breakpoint (loc->owner) && loc->condition_changed)
11250 loc->needs_update = 1;
11251 /* Clear the condition modification flag. */
11252 loc->condition_changed = condition_unchanged;
11254 continue;
11258 /* This and the above ensure the invariant that the first location
11259 is not duplicated, and is the inserted one.
11260 All following are marked as duplicated, and are not inserted. */
11261 if (loc->inserted)
11262 swap_insertion (loc, *loc_first_p);
11263 loc->duplicate = 1;
11265 /* Clear the condition modification flag. */
11266 loc->condition_changed = condition_unchanged;
11269 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11271 if (insert_mode != UGLL_DONT_INSERT)
11272 insert_breakpoint_locations ();
11273 else
11275 /* Even though the caller told us to not insert new
11276 locations, we may still need to update conditions on the
11277 target's side of breakpoints that were already inserted
11278 if the target is evaluating breakpoint conditions. We
11279 only update conditions for locations that are marked
11280 "needs_update". */
11281 update_inserted_breakpoint_locations ();
11285 if (insert_mode != UGLL_DONT_INSERT)
11286 download_tracepoint_locations ();
11289 void
11290 breakpoint_retire_moribund (void)
11292 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11294 struct bp_location *loc = moribund_locations[ix];
11295 if (--(loc->events_till_retirement) == 0)
11297 decref_bp_location (&loc);
11298 unordered_remove (moribund_locations, ix);
11299 --ix;
11304 static void
11305 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11310 update_global_location_list (insert_mode);
11312 catch (const gdb_exception_error &e)
11317 /* Clear BKP from a BPS. */
11319 static void
11320 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
11322 bpstat *bs;
11324 for (bs = bps; bs; bs = bs->next)
11325 if (bs->breakpoint_at == bpt)
11327 bs->breakpoint_at = NULL;
11328 bs->old_val = NULL;
11329 /* bs->commands will be freed later. */
11333 /* Callback for iterate_over_threads. */
11334 static int
11335 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11337 struct breakpoint *bpt = (struct breakpoint *) data;
11339 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11340 return 0;
11343 /* Helper for breakpoint and tracepoint breakpoint->mention
11344 callbacks. */
11346 static void
11347 say_where (const breakpoint *b)
11349 struct value_print_options opts;
11351 get_user_print_options (&opts);
11353 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11354 single string. */
11355 if (b->loc == NULL)
11357 /* For pending locations, the output differs slightly based
11358 on b->extra_string. If this is non-NULL, it contains either
11359 a condition or dprintf arguments. */
11360 if (b->extra_string == NULL)
11362 gdb_printf (_(" (%s) pending."),
11363 event_location_to_string (b->location.get ()));
11365 else if (b->type == bp_dprintf)
11367 gdb_printf (_(" (%s,%s) pending."),
11368 event_location_to_string (b->location.get ()),
11369 b->extra_string.get ());
11371 else
11373 gdb_printf (_(" (%s %s) pending."),
11374 event_location_to_string (b->location.get ()),
11375 b->extra_string.get ());
11378 else
11380 if (opts.addressprint || b->loc->symtab == NULL)
11381 gdb_printf (" at %ps",
11382 styled_string (address_style.style (),
11383 paddress (b->loc->gdbarch,
11384 b->loc->address)));
11385 if (b->loc->symtab != NULL)
11387 /* If there is a single location, we can print the location
11388 more nicely. */
11389 if (b->loc->next == NULL)
11391 const char *filename
11392 = symtab_to_filename_for_display (b->loc->symtab);
11393 gdb_printf (": file %ps, line %d.",
11394 styled_string (file_name_style.style (),
11395 filename),
11396 b->loc->line_number);
11398 else
11399 /* This is not ideal, but each location may have a
11400 different file name, and this at least reflects the
11401 real situation somewhat. */
11402 gdb_printf (": %s.",
11403 event_location_to_string (b->location.get ()));
11406 if (b->loc->next)
11408 struct bp_location *loc = b->loc;
11409 int n = 0;
11410 for (; loc; loc = loc->next)
11411 ++n;
11412 gdb_printf (" (%d locations)", n);
11417 /* See breakpoint.h. */
11419 bp_location_range breakpoint::locations () const
11421 return bp_location_range (this->loc);
11424 struct bp_location *
11425 breakpoint::allocate_location ()
11427 return new bp_location (this);
11430 #define internal_error_pure_virtual_called() \
11431 gdb_assert_not_reached ("pure virtual function called")
11434 breakpoint::insert_location (struct bp_location *bl)
11436 internal_error_pure_virtual_called ();
11440 breakpoint::remove_location (struct bp_location *bl,
11441 enum remove_bp_reason reason)
11443 internal_error_pure_virtual_called ();
11447 breakpoint::breakpoint_hit (const struct bp_location *bl,
11448 const address_space *aspace,
11449 CORE_ADDR bp_addr,
11450 const target_waitstatus &ws)
11452 internal_error_pure_virtual_called ();
11456 breakpoint::resources_needed (const struct bp_location *bl)
11458 internal_error_pure_virtual_called ();
11461 enum print_stop_action
11462 breakpoint::print_it (const bpstat *bs) const
11464 internal_error_pure_virtual_called ();
11467 void
11468 breakpoint::print_mention () const
11470 internal_error_pure_virtual_called ();
11473 void
11474 breakpoint::print_recreate (struct ui_file *fp) const
11476 internal_error_pure_virtual_called ();
11479 std::vector<symtab_and_line>
11480 breakpoint::decode_location (struct event_location *location,
11481 struct program_space *search_pspace)
11483 internal_error_pure_virtual_called ();
11486 /* Default breakpoint_ops methods. */
11488 void
11489 code_breakpoint::re_set ()
11491 /* FIXME: is this still reachable? */
11492 if (breakpoint_event_location_empty_p (this))
11494 /* Anything without a location can't be re-set. */
11495 delete_breakpoint (this);
11496 return;
11499 breakpoint_re_set_default (this);
11503 code_breakpoint::insert_location (struct bp_location *bl)
11505 CORE_ADDR addr = bl->target_info.reqstd_address;
11507 bl->target_info.kind = breakpoint_kind (bl, &addr);
11508 bl->target_info.placed_address = addr;
11510 int result;
11511 if (bl->loc_type == bp_loc_hardware_breakpoint)
11512 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
11513 else
11514 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11516 if (result == 0 && bl->probe.prob != nullptr)
11518 /* The insertion was successful, now let's set the probe's semaphore
11519 if needed. */
11520 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11523 return result;
11527 code_breakpoint::remove_location (struct bp_location *bl,
11528 enum remove_bp_reason reason)
11530 if (bl->probe.prob != nullptr)
11532 /* Let's clear the semaphore before removing the location. */
11533 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11536 if (bl->loc_type == bp_loc_hardware_breakpoint)
11537 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11538 else
11539 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
11543 code_breakpoint::breakpoint_hit (const struct bp_location *bl,
11544 const address_space *aspace,
11545 CORE_ADDR bp_addr,
11546 const target_waitstatus &ws)
11548 if (ws.kind () != TARGET_WAITKIND_STOPPED
11549 || ws.sig () != GDB_SIGNAL_TRAP)
11550 return 0;
11552 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11553 aspace, bp_addr))
11554 return 0;
11556 if (overlay_debugging /* unmapped overlay section */
11557 && section_is_overlay (bl->section)
11558 && !section_is_mapped (bl->section))
11559 return 0;
11561 return 1;
11565 dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
11566 const address_space *aspace,
11567 CORE_ADDR bp_addr,
11568 const target_waitstatus &ws)
11570 if (dprintf_style == dprintf_style_agent
11571 && target_can_run_breakpoint_commands ())
11573 /* An agent-style dprintf never causes a stop. If we see a trap
11574 for this address it must be for a breakpoint that happens to
11575 be set at the same address. */
11576 return 0;
11579 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
11583 ordinary_breakpoint::resources_needed (const struct bp_location *bl)
11585 gdb_assert (type == bp_hardware_breakpoint);
11587 return 1;
11590 enum print_stop_action
11591 ordinary_breakpoint::print_it (const bpstat *bs) const
11593 const struct bp_location *bl;
11594 int bp_temp;
11595 struct ui_out *uiout = current_uiout;
11597 bl = bs->bp_location_at.get ();
11599 bp_temp = disposition == disp_del;
11600 if (bl->address != bl->requested_address)
11601 breakpoint_adjustment_warning (bl->requested_address,
11602 bl->address,
11603 number, 1);
11604 annotate_breakpoint (number);
11605 maybe_print_thread_hit_breakpoint (uiout);
11607 if (uiout->is_mi_like_p ())
11609 uiout->field_string ("reason",
11610 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11611 uiout->field_string ("disp", bpdisp_text (disposition));
11613 if (bp_temp)
11614 uiout->message ("Temporary breakpoint %pF, ",
11615 signed_field ("bkptno", number));
11616 else
11617 uiout->message ("Breakpoint %pF, ",
11618 signed_field ("bkptno", number));
11620 return PRINT_SRC_AND_LOC;
11623 void
11624 ordinary_breakpoint::print_mention () const
11626 if (current_uiout->is_mi_like_p ())
11627 return;
11629 switch (type)
11631 case bp_breakpoint:
11632 case bp_gnu_ifunc_resolver:
11633 if (disposition == disp_del)
11634 gdb_printf (_("Temporary breakpoint"));
11635 else
11636 gdb_printf (_("Breakpoint"));
11637 gdb_printf (_(" %d"), number);
11638 if (type == bp_gnu_ifunc_resolver)
11639 gdb_printf (_(" at gnu-indirect-function resolver"));
11640 break;
11641 case bp_hardware_breakpoint:
11642 gdb_printf (_("Hardware assisted breakpoint %d"), number);
11643 break;
11644 case bp_dprintf:
11645 gdb_printf (_("Dprintf %d"), number);
11646 break;
11649 say_where (this);
11652 void
11653 ordinary_breakpoint::print_recreate (struct ui_file *fp) const
11655 if (type == bp_breakpoint && disposition == disp_del)
11656 gdb_printf (fp, "tbreak");
11657 else if (type == bp_breakpoint)
11658 gdb_printf (fp, "break");
11659 else if (type == bp_hardware_breakpoint
11660 && disposition == disp_del)
11661 gdb_printf (fp, "thbreak");
11662 else if (type == bp_hardware_breakpoint)
11663 gdb_printf (fp, "hbreak");
11664 else
11665 internal_error (__FILE__, __LINE__,
11666 _("unhandled breakpoint type %d"), (int) type);
11668 gdb_printf (fp, " %s", event_location_to_string (location.get ()));
11670 /* Print out extra_string if this breakpoint is pending. It might
11671 contain, for example, conditions that were set by the user. */
11672 if (loc == NULL && extra_string != NULL)
11673 gdb_printf (fp, " %s", extra_string.get ());
11675 print_recreate_thread (fp);
11678 std::vector<symtab_and_line>
11679 code_breakpoint::decode_location (struct event_location *location,
11680 struct program_space *search_pspace)
11682 if (event_location_type (location) == PROBE_LOCATION)
11683 return bkpt_probe_decode_location (this, location, search_pspace);
11685 return decode_location_default (this, location, search_pspace);
11688 /* Virtual table for internal breakpoints. */
11690 void
11691 internal_breakpoint::re_set ()
11693 switch (type)
11695 /* Delete overlay event and longjmp master breakpoints; they
11696 will be reset later by breakpoint_re_set. */
11697 case bp_overlay_event:
11698 case bp_longjmp_master:
11699 case bp_std_terminate_master:
11700 case bp_exception_master:
11701 delete_breakpoint (this);
11702 break;
11704 /* This breakpoint is special, it's set up when the inferior
11705 starts and we really don't want to touch it. */
11706 case bp_shlib_event:
11708 /* Like bp_shlib_event, this breakpoint type is special. Once
11709 it is set up, we do not want to touch it. */
11710 case bp_thread_event:
11711 break;
11715 void
11716 internal_breakpoint::check_status (bpstat *bs)
11718 if (type == bp_shlib_event)
11720 /* If requested, stop when the dynamic linker notifies GDB of
11721 events. This allows the user to get control and place
11722 breakpoints in initializer routines for dynamically loaded
11723 objects (among other things). */
11724 bs->stop = stop_on_solib_events;
11725 bs->print = stop_on_solib_events;
11727 else
11728 bs->stop = 0;
11731 enum print_stop_action
11732 internal_breakpoint::print_it (const bpstat *bs) const
11734 switch (type)
11736 case bp_shlib_event:
11737 /* Did we stop because the user set the stop_on_solib_events
11738 variable? (If so, we report this as a generic, "Stopped due
11739 to shlib event" message.) */
11740 print_solib_event (false);
11741 break;
11743 case bp_thread_event:
11744 /* Not sure how we will get here.
11745 GDB should not stop for these breakpoints. */
11746 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
11747 break;
11749 case bp_overlay_event:
11750 /* By analogy with the thread event, GDB should not stop for these. */
11751 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
11752 break;
11754 case bp_longjmp_master:
11755 /* These should never be enabled. */
11756 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
11757 break;
11759 case bp_std_terminate_master:
11760 /* These should never be enabled. */
11761 gdb_printf (_("std::terminate Master Breakpoint: "
11762 "gdb should not stop!\n"));
11763 break;
11765 case bp_exception_master:
11766 /* These should never be enabled. */
11767 gdb_printf (_("Exception Master Breakpoint: "
11768 "gdb should not stop!\n"));
11769 break;
11772 return PRINT_NOTHING;
11775 void
11776 internal_breakpoint::print_mention () const
11778 /* Nothing to mention. These breakpoints are internal. */
11781 /* Virtual table for momentary breakpoints */
11783 void
11784 momentary_breakpoint::re_set ()
11786 /* Keep temporary breakpoints, which can be encountered when we step
11787 over a dlopen call and solib_add is resetting the breakpoints.
11788 Otherwise these should have been blown away via the cleanup chain
11789 or by breakpoint_init_inferior when we rerun the executable. */
11792 void
11793 momentary_breakpoint::check_status (bpstat *bs)
11795 /* Nothing. The point of these breakpoints is causing a stop. */
11798 enum print_stop_action
11799 momentary_breakpoint::print_it (const bpstat *bs) const
11801 return PRINT_UNKNOWN;
11804 void
11805 momentary_breakpoint::print_mention () const
11807 /* Nothing to mention. These breakpoints are internal. */
11810 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
11812 It gets cleared already on the removal of the first one of such placed
11813 breakpoints. This is OK as they get all removed altogether. */
11815 longjmp_breakpoint::~longjmp_breakpoint ()
11817 thread_info *tp = find_thread_global_id (this->thread);
11819 if (tp != NULL)
11820 tp->initiating_frame = null_frame_id;
11823 static void
11824 bkpt_probe_create_sals_from_location (struct event_location *location,
11825 struct linespec_result *canonical)
11828 struct linespec_sals lsal;
11830 lsal.sals = parse_probes (location, NULL, canonical);
11831 lsal.canonical
11832 = xstrdup (event_location_to_string (canonical->location.get ()));
11833 canonical->lsals.push_back (std::move (lsal));
11836 static std::vector<symtab_and_line>
11837 bkpt_probe_decode_location (struct breakpoint *b,
11838 struct event_location *location,
11839 struct program_space *search_pspace)
11841 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
11842 if (sals.empty ())
11843 error (_("probe not found"));
11844 return sals;
11848 tracepoint::breakpoint_hit (const struct bp_location *bl,
11849 const address_space *aspace, CORE_ADDR bp_addr,
11850 const target_waitstatus &ws)
11852 /* By definition, the inferior does not report stops at
11853 tracepoints. */
11854 return 0;
11857 void
11858 tracepoint::print_one_detail (struct ui_out *uiout) const
11860 if (!static_trace_marker_id.empty ())
11862 gdb_assert (type == bp_static_tracepoint
11863 || type == bp_static_marker_tracepoint);
11865 uiout->message ("\tmarker id is %pF\n",
11866 string_field ("static-tracepoint-marker-string-id",
11867 static_trace_marker_id.c_str ()));
11871 void
11872 tracepoint::print_mention () const
11874 if (current_uiout->is_mi_like_p ())
11875 return;
11877 switch (type)
11879 case bp_tracepoint:
11880 gdb_printf (_("Tracepoint"));
11881 gdb_printf (_(" %d"), number);
11882 break;
11883 case bp_fast_tracepoint:
11884 gdb_printf (_("Fast tracepoint"));
11885 gdb_printf (_(" %d"), number);
11886 break;
11887 case bp_static_tracepoint:
11888 case bp_static_marker_tracepoint:
11889 gdb_printf (_("Static tracepoint"));
11890 gdb_printf (_(" %d"), number);
11891 break;
11892 default:
11893 internal_error (__FILE__, __LINE__,
11894 _("unhandled tracepoint type %d"), (int) type);
11897 say_where (this);
11900 void
11901 tracepoint::print_recreate (struct ui_file *fp) const
11903 if (type == bp_fast_tracepoint)
11904 gdb_printf (fp, "ftrace");
11905 else if (type == bp_static_tracepoint
11906 || type == bp_static_marker_tracepoint)
11907 gdb_printf (fp, "strace");
11908 else if (type == bp_tracepoint)
11909 gdb_printf (fp, "trace");
11910 else
11911 internal_error (__FILE__, __LINE__,
11912 _("unhandled tracepoint type %d"), (int) type);
11914 gdb_printf (fp, " %s", event_location_to_string (location.get ()));
11915 print_recreate_thread (fp);
11917 if (pass_count)
11918 gdb_printf (fp, " passcount %d\n", pass_count);
11921 /* Virtual table for tracepoints on static probes. */
11923 static void
11924 tracepoint_probe_create_sals_from_location
11925 (struct event_location *location,
11926 struct linespec_result *canonical)
11928 /* We use the same method for breakpoint on probes. */
11929 bkpt_probe_create_sals_from_location (location, canonical);
11932 void
11933 dprintf_breakpoint::re_set ()
11935 breakpoint_re_set_default (this);
11937 /* extra_string should never be non-NULL for dprintf. */
11938 gdb_assert (extra_string != NULL);
11940 /* 1 - connect to target 1, that can run breakpoint commands.
11941 2 - create a dprintf, which resolves fine.
11942 3 - disconnect from target 1
11943 4 - connect to target 2, that can NOT run breakpoint commands.
11945 After steps #3/#4, you'll want the dprintf command list to
11946 be updated, because target 1 and 2 may well return different
11947 answers for target_can_run_breakpoint_commands().
11948 Given absence of finer grained resetting, we get to do
11949 it all the time. */
11950 if (extra_string != NULL)
11951 update_dprintf_command_list (this);
11954 /* Implement the "print_recreate" method for dprintf. */
11956 void
11957 dprintf_breakpoint::print_recreate (struct ui_file *fp) const
11959 gdb_printf (fp, "dprintf %s,%s",
11960 event_location_to_string (location.get ()),
11961 extra_string.get ());
11962 print_recreate_thread (fp);
11965 /* Implement the "after_condition_true" method for dprintf.
11967 dprintf's are implemented with regular commands in their command
11968 list, but we run the commands here instead of before presenting the
11969 stop to the user, as dprintf's don't actually cause a stop. This
11970 also makes it so that the commands of multiple dprintfs at the same
11971 address are all handled. */
11973 void
11974 dprintf_breakpoint::after_condition_true (struct bpstat *bs)
11976 /* dprintf's never cause a stop. This wasn't set in the
11977 check_status hook instead because that would make the dprintf's
11978 condition not be evaluated. */
11979 bs->stop = 0;
11981 /* Run the command list here. Take ownership of it instead of
11982 copying. We never want these commands to run later in
11983 bpstat_do_actions, if a breakpoint that causes a stop happens to
11984 be set at same address as this dprintf, or even if running the
11985 commands here throws. */
11986 counted_command_line cmds = std::move (bs->commands);
11987 gdb_assert (cmds != nullptr);
11988 execute_control_commands (cmds.get (), 0);
11991 /* The breakpoint_ops structure to be used on static tracepoints with
11992 markers (`-m'). */
11994 static void
11995 strace_marker_create_sals_from_location (struct event_location *location,
11996 struct linespec_result *canonical)
11998 struct linespec_sals lsal;
11999 const char *arg_start, *arg;
12001 arg = arg_start = get_linespec_location (location)->spec_string;
12002 lsal.sals = decode_static_tracepoint_spec (&arg);
12004 std::string str (arg_start, arg - arg_start);
12005 const char *ptr = str.c_str ();
12006 canonical->location
12007 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
12009 lsal.canonical
12010 = xstrdup (event_location_to_string (canonical->location.get ()));
12011 canonical->lsals.push_back (std::move (lsal));
12014 static void
12015 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12016 struct linespec_result *canonical,
12017 gdb::unique_xmalloc_ptr<char> cond_string,
12018 gdb::unique_xmalloc_ptr<char> extra_string,
12019 enum bptype type_wanted,
12020 enum bpdisp disposition,
12021 int thread,
12022 int task, int ignore_count,
12023 int from_tty, int enabled,
12024 int internal, unsigned flags)
12026 const linespec_sals &lsal = canonical->lsals[0];
12028 /* If the user is creating a static tracepoint by marker id
12029 (strace -m MARKER_ID), then store the sals index, so that
12030 breakpoint_re_set can try to match up which of the newly
12031 found markers corresponds to this one, and, don't try to
12032 expand multiple locations for each sal, given than SALS
12033 already should contain all sals for MARKER_ID. */
12035 for (size_t i = 0; i < lsal.sals.size (); i++)
12037 event_location_up location
12038 = copy_event_location (canonical->location.get ());
12040 std::unique_ptr<tracepoint> tp
12041 (new tracepoint (gdbarch,
12042 type_wanted,
12043 lsal.sals[i],
12044 std::move (location),
12045 NULL,
12046 std::move (cond_string),
12047 std::move (extra_string),
12048 disposition,
12049 thread, task, ignore_count,
12050 from_tty, enabled, flags,
12051 canonical->special_display));
12053 /* Given that its possible to have multiple markers with
12054 the same string id, if the user is creating a static
12055 tracepoint by marker id ("strace -m MARKER_ID"), then
12056 store the sals index, so that breakpoint_re_set can
12057 try to match up which of the newly found markers
12058 corresponds to this one */
12059 tp->static_trace_marker_id_idx = i;
12061 install_breakpoint (internal, std::move (tp), 0);
12065 std::vector<symtab_and_line>
12066 static_marker_tracepoint::decode_location (struct event_location *location,
12067 struct program_space *search_pspace)
12069 const char *s = get_linespec_location (location)->spec_string;
12071 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12072 if (sals.size () > static_trace_marker_id_idx)
12074 sals[0] = sals[static_trace_marker_id_idx];
12075 sals.resize (1);
12076 return sals;
12078 else
12079 error (_("marker %s not found"), static_trace_marker_id.c_str ());
12082 /* Static tracepoints with marker (`-m'). */
12083 static struct breakpoint_ops strace_marker_breakpoint_ops =
12085 strace_marker_create_sals_from_location,
12086 strace_marker_create_breakpoints_sal,
12089 static int
12090 strace_marker_p (struct breakpoint *b)
12092 return b->type == bp_static_marker_tracepoint;
12095 /* Delete a breakpoint and clean up all traces of it in the data
12096 structures. */
12098 void
12099 delete_breakpoint (struct breakpoint *bpt)
12101 gdb_assert (bpt != NULL);
12103 /* Has this bp already been deleted? This can happen because
12104 multiple lists can hold pointers to bp's. bpstat lists are
12105 especial culprits.
12107 One example of this happening is a watchpoint's scope bp. When
12108 the scope bp triggers, we notice that the watchpoint is out of
12109 scope, and delete it. We also delete its scope bp. But the
12110 scope bp is marked "auto-deleting", and is already on a bpstat.
12111 That bpstat is then checked for auto-deleting bp's, which are
12112 deleted.
12114 A real solution to this problem might involve reference counts in
12115 bp's, and/or giving them pointers back to their referencing
12116 bpstat's, and teaching delete_breakpoint to only free a bp's
12117 storage when no more references were extent. A cheaper bandaid
12118 was chosen. */
12119 if (bpt->type == bp_none)
12120 return;
12122 /* At least avoid this stale reference until the reference counting
12123 of breakpoints gets resolved. */
12124 if (bpt->related_breakpoint != bpt)
12126 struct breakpoint *related;
12127 struct watchpoint *w;
12129 if (bpt->type == bp_watchpoint_scope)
12130 w = (struct watchpoint *) bpt->related_breakpoint;
12131 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12132 w = (struct watchpoint *) bpt;
12133 else
12134 w = NULL;
12135 if (w != NULL)
12136 watchpoint_del_at_next_stop (w);
12138 /* Unlink bpt from the bpt->related_breakpoint ring. */
12139 for (related = bpt; related->related_breakpoint != bpt;
12140 related = related->related_breakpoint);
12141 related->related_breakpoint = bpt->related_breakpoint;
12142 bpt->related_breakpoint = bpt;
12145 /* watch_command_1 creates a watchpoint but only sets its number if
12146 update_watchpoint succeeds in creating its bp_locations. If there's
12147 a problem in that process, we'll be asked to delete the half-created
12148 watchpoint. In that case, don't announce the deletion. */
12149 if (bpt->number)
12150 gdb::observers::breakpoint_deleted.notify (bpt);
12152 if (breakpoint_chain == bpt)
12153 breakpoint_chain = bpt->next;
12155 for (breakpoint *b : all_breakpoints ())
12156 if (b->next == bpt)
12158 b->next = bpt->next;
12159 break;
12162 /* Be sure no bpstat's are pointing at the breakpoint after it's
12163 been freed. */
12164 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12165 in all threads for now. Note that we cannot just remove bpstats
12166 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12167 commands are associated with the bpstat; if we remove it here,
12168 then the later call to bpstat_do_actions (&stop_bpstat); in
12169 event-top.c won't do anything, and temporary breakpoints with
12170 commands won't work. */
12172 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12174 /* Now that breakpoint is removed from breakpoint list, update the
12175 global location list. This will remove locations that used to
12176 belong to this breakpoint. Do this before freeing the breakpoint
12177 itself, since remove_breakpoint looks at location's owner. It
12178 might be better design to have location completely
12179 self-contained, but it's not the case now. */
12180 update_global_location_list (UGLL_DONT_INSERT);
12182 /* On the chance that someone will soon try again to delete this
12183 same bp, we mark it as deleted before freeing its storage. */
12184 bpt->type = bp_none;
12185 delete bpt;
12188 /* Iterator function to call a user-provided callback function once
12189 for each of B and its related breakpoints. */
12191 static void
12192 iterate_over_related_breakpoints (struct breakpoint *b,
12193 gdb::function_view<void (breakpoint *)> function)
12195 struct breakpoint *related;
12197 related = b;
12200 struct breakpoint *next;
12202 /* FUNCTION may delete RELATED. */
12203 next = related->related_breakpoint;
12205 if (next == related)
12207 /* RELATED is the last ring entry. */
12208 function (related);
12210 /* FUNCTION may have deleted it, so we'd never reach back to
12211 B. There's nothing left to do anyway, so just break
12212 out. */
12213 break;
12215 else
12216 function (related);
12218 related = next;
12220 while (related != b);
12223 static void
12224 delete_command (const char *arg, int from_tty)
12226 dont_repeat ();
12228 if (arg == 0)
12230 int breaks_to_delete = 0;
12232 /* Delete all breakpoints if no argument. Do not delete
12233 internal breakpoints, these have to be deleted with an
12234 explicit breakpoint number argument. */
12235 for (breakpoint *b : all_breakpoints ())
12236 if (user_breakpoint_p (b))
12238 breaks_to_delete = 1;
12239 break;
12242 /* Ask user only if there are some breakpoints to delete. */
12243 if (!from_tty
12244 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
12245 for (breakpoint *b : all_breakpoints_safe ())
12246 if (user_breakpoint_p (b))
12247 delete_breakpoint (b);
12249 else
12250 map_breakpoint_numbers
12251 (arg, [&] (breakpoint *br)
12253 iterate_over_related_breakpoints (br, delete_breakpoint);
12257 /* Return true if all locations of B bound to PSPACE are pending. If
12258 PSPACE is NULL, all locations of all program spaces are
12259 considered. */
12261 static int
12262 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
12264 for (bp_location *loc : b->locations ())
12265 if ((pspace == NULL
12266 || loc->pspace == pspace)
12267 && !loc->shlib_disabled
12268 && !loc->pspace->executing_startup)
12269 return 0;
12270 return 1;
12273 /* Subroutine of update_breakpoint_locations to simplify it.
12274 Return non-zero if multiple fns in list LOC have the same name.
12275 Null names are ignored. */
12277 static int
12278 ambiguous_names_p (struct bp_location *loc)
12280 struct bp_location *l;
12281 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
12282 xcalloc, xfree));
12284 for (l = loc; l != NULL; l = l->next)
12286 const char **slot;
12287 const char *name = l->function_name.get ();
12289 /* Allow for some names to be NULL, ignore them. */
12290 if (name == NULL)
12291 continue;
12293 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
12294 INSERT);
12295 /* NOTE: We can assume slot != NULL here because xcalloc never
12296 returns NULL. */
12297 if (*slot != NULL)
12298 return 1;
12299 *slot = name;
12302 return 0;
12305 /* When symbols change, it probably means the sources changed as well,
12306 and it might mean the static tracepoint markers are no longer at
12307 the same address or line numbers they used to be at last we
12308 checked. Losing your static tracepoints whenever you rebuild is
12309 undesirable. This function tries to resync/rematch gdb static
12310 tracepoints with the markers on the target, for static tracepoints
12311 that have not been set by marker id. Static tracepoint that have
12312 been set by marker id are reset by marker id in breakpoint_re_set.
12313 The heuristic is:
12315 1) For a tracepoint set at a specific address, look for a marker at
12316 the old PC. If one is found there, assume to be the same marker.
12317 If the name / string id of the marker found is different from the
12318 previous known name, assume that means the user renamed the marker
12319 in the sources, and output a warning.
12321 2) For a tracepoint set at a given line number, look for a marker
12322 at the new address of the old line number. If one is found there,
12323 assume to be the same marker. If the name / string id of the
12324 marker found is different from the previous known name, assume that
12325 means the user renamed the marker in the sources, and output a
12326 warning.
12328 3) If a marker is no longer found at the same address or line, it
12329 may mean the marker no longer exists. But it may also just mean
12330 the code changed a bit. Maybe the user added a few lines of code
12331 that made the marker move up or down (in line number terms). Ask
12332 the target for info about the marker with the string id as we knew
12333 it. If found, update line number and address in the matching
12334 static tracepoint. This will get confused if there's more than one
12335 marker with the same ID (possible in UST, although unadvised
12336 precisely because it confuses tools). */
12338 static struct symtab_and_line
12339 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12341 struct tracepoint *tp = (struct tracepoint *) b;
12342 struct static_tracepoint_marker marker;
12343 CORE_ADDR pc;
12345 pc = sal.pc;
12346 if (sal.line)
12347 find_line_pc (sal.symtab, sal.line, &pc);
12349 if (target_static_tracepoint_marker_at (pc, &marker))
12351 if (tp->static_trace_marker_id != marker.str_id)
12352 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12353 b->number, tp->static_trace_marker_id.c_str (),
12354 marker.str_id.c_str ());
12356 tp->static_trace_marker_id = std::move (marker.str_id);
12358 return sal;
12361 /* Old marker wasn't found on target at lineno. Try looking it up
12362 by string ID. */
12363 if (!sal.explicit_pc
12364 && sal.line != 0
12365 && sal.symtab != NULL
12366 && !tp->static_trace_marker_id.empty ())
12368 std::vector<static_tracepoint_marker> markers
12369 = target_static_tracepoint_markers_by_strid
12370 (tp->static_trace_marker_id.c_str ());
12372 if (!markers.empty ())
12374 struct symbol *sym;
12375 struct static_tracepoint_marker *tpmarker;
12376 struct ui_out *uiout = current_uiout;
12377 struct explicit_location explicit_loc;
12379 tpmarker = &markers[0];
12381 tp->static_trace_marker_id = std::move (tpmarker->str_id);
12383 warning (_("marker for static tracepoint %d (%s) not "
12384 "found at previous line number"),
12385 b->number, tp->static_trace_marker_id.c_str ());
12387 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
12388 sym = find_pc_sect_function (tpmarker->address, NULL);
12389 uiout->text ("Now in ");
12390 if (sym)
12392 uiout->field_string ("func", sym->print_name (),
12393 function_name_style.style ());
12394 uiout->text (" at ");
12396 uiout->field_string ("file",
12397 symtab_to_filename_for_display (sal2.symtab),
12398 file_name_style.style ());
12399 uiout->text (":");
12401 if (uiout->is_mi_like_p ())
12403 const char *fullname = symtab_to_fullname (sal2.symtab);
12405 uiout->field_string ("fullname", fullname);
12408 uiout->field_signed ("line", sal2.line);
12409 uiout->text ("\n");
12411 b->loc->line_number = sal2.line;
12412 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
12414 b->location.reset (NULL);
12415 initialize_explicit_location (&explicit_loc);
12416 explicit_loc.source_filename
12417 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
12418 explicit_loc.line_offset.offset = b->loc->line_number;
12419 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
12420 b->location = new_explicit_location (&explicit_loc);
12422 /* Might be nice to check if function changed, and warn if
12423 so. */
12426 return sal;
12429 /* Returns 1 iff locations A and B are sufficiently same that
12430 we don't need to report breakpoint as changed. */
12432 static int
12433 locations_are_equal (struct bp_location *a, struct bp_location *b)
12435 while (a && b)
12437 if (a->address != b->address)
12438 return 0;
12440 if (a->shlib_disabled != b->shlib_disabled)
12441 return 0;
12443 if (a->enabled != b->enabled)
12444 return 0;
12446 if (a->disabled_by_cond != b->disabled_by_cond)
12447 return 0;
12449 a = a->next;
12450 b = b->next;
12453 if ((a == NULL) != (b == NULL))
12454 return 0;
12456 return 1;
12459 /* Split all locations of B that are bound to PSPACE out of B's
12460 location list to a separate list and return that list's head. If
12461 PSPACE is NULL, hoist out all locations of B. */
12463 static struct bp_location *
12464 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12466 struct bp_location head;
12467 struct bp_location *i = b->loc;
12468 struct bp_location **i_link = &b->loc;
12469 struct bp_location *hoisted = &head;
12471 if (pspace == NULL)
12473 i = b->loc;
12474 b->loc = NULL;
12475 return i;
12478 head.next = NULL;
12480 while (i != NULL)
12482 if (i->pspace == pspace)
12484 *i_link = i->next;
12485 i->next = NULL;
12486 hoisted->next = i;
12487 hoisted = i;
12489 else
12490 i_link = &i->next;
12491 i = *i_link;
12494 return head.next;
12497 /* Create new breakpoint locations for B (a hardware or software
12498 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12499 zero, then B is a ranged breakpoint. Only recreates locations for
12500 FILTER_PSPACE. Locations of other program spaces are left
12501 untouched. */
12503 void
12504 update_breakpoint_locations (code_breakpoint *b,
12505 struct program_space *filter_pspace,
12506 gdb::array_view<const symtab_and_line> sals,
12507 gdb::array_view<const symtab_and_line> sals_end)
12509 struct bp_location *existing_locations;
12511 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
12513 /* Ranged breakpoints have only one start location and one end
12514 location. */
12515 b->enable_state = bp_disabled;
12516 gdb_printf (gdb_stderr,
12517 _("Could not reset ranged breakpoint %d: "
12518 "multiple locations found\n"),
12519 b->number);
12520 return;
12523 /* If there's no new locations, and all existing locations are
12524 pending, don't do anything. This optimizes the common case where
12525 all locations are in the same shared library, that was unloaded.
12526 We'd like to retain the location, so that when the library is
12527 loaded again, we don't loose the enabled/disabled status of the
12528 individual locations. */
12529 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
12530 return;
12532 existing_locations = hoist_existing_locations (b, filter_pspace);
12534 for (const auto &sal : sals)
12536 struct bp_location *new_loc;
12538 switch_to_program_space_and_thread (sal.pspace);
12540 new_loc = b->add_location (sal);
12542 /* Reparse conditions, they might contain references to the
12543 old symtab. */
12544 if (b->cond_string != NULL)
12546 const char *s;
12548 s = b->cond_string.get ();
12551 new_loc->cond = parse_exp_1 (&s, sal.pc,
12552 block_for_pc (sal.pc),
12555 catch (const gdb_exception_error &e)
12557 new_loc->disabled_by_cond = true;
12561 if (!sals_end.empty ())
12563 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
12565 new_loc->length = end - sals[0].pc + 1;
12569 /* If possible, carry over 'disable' status from existing
12570 breakpoints. */
12572 struct bp_location *e = existing_locations;
12573 /* If there are multiple breakpoints with the same function name,
12574 e.g. for inline functions, comparing function names won't work.
12575 Instead compare pc addresses; this is just a heuristic as things
12576 may have moved, but in practice it gives the correct answer
12577 often enough until a better solution is found. */
12578 int have_ambiguous_names = ambiguous_names_p (b->loc);
12580 for (; e; e = e->next)
12582 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
12584 if (have_ambiguous_names)
12586 for (bp_location *l : b->locations ())
12588 /* Ignore software vs hardware location type at
12589 this point, because with "set breakpoint
12590 auto-hw", after a re-set, locations that were
12591 hardware can end up as software, or vice versa.
12592 As mentioned above, this is an heuristic and in
12593 practice should give the correct answer often
12594 enough. */
12595 if (breakpoint_locations_match (e, l, true))
12597 l->enabled = e->enabled;
12598 l->disabled_by_cond = e->disabled_by_cond;
12599 break;
12603 else
12605 for (bp_location *l : b->locations ())
12606 if (l->function_name
12607 && strcmp (e->function_name.get (),
12608 l->function_name.get ()) == 0)
12610 l->enabled = e->enabled;
12611 l->disabled_by_cond = e->disabled_by_cond;
12612 break;
12619 if (!locations_are_equal (existing_locations, b->loc))
12620 gdb::observers::breakpoint_modified.notify (b);
12623 /* Find the SaL locations corresponding to the given LOCATION.
12624 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12626 static std::vector<symtab_and_line>
12627 location_to_sals (struct breakpoint *b, struct event_location *location,
12628 struct program_space *search_pspace, int *found)
12630 struct gdb_exception exception;
12632 std::vector<symtab_and_line> sals;
12636 sals = b->decode_location (location, search_pspace);
12638 catch (gdb_exception_error &e)
12640 int not_found_and_ok = 0;
12642 /* For pending breakpoints, it's expected that parsing will
12643 fail until the right shared library is loaded. User has
12644 already told to create pending breakpoints and don't need
12645 extra messages. If breakpoint is in bp_shlib_disabled
12646 state, then user already saw the message about that
12647 breakpoint being disabled, and don't want to see more
12648 errors. */
12649 if (e.error == NOT_FOUND_ERROR
12650 && (b->condition_not_parsed
12651 || (b->loc != NULL
12652 && search_pspace != NULL
12653 && b->loc->pspace != search_pspace)
12654 || (b->loc && b->loc->shlib_disabled)
12655 || (b->loc && b->loc->pspace->executing_startup)
12656 || b->enable_state == bp_disabled))
12657 not_found_and_ok = 1;
12659 if (!not_found_and_ok)
12661 /* We surely don't want to warn about the same breakpoint
12662 10 times. One solution, implemented here, is disable
12663 the breakpoint on error. Another solution would be to
12664 have separate 'warning emitted' flag. Since this
12665 happens only when a binary has changed, I don't know
12666 which approach is better. */
12667 b->enable_state = bp_disabled;
12668 throw;
12671 exception = std::move (e);
12674 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
12676 for (auto &sal : sals)
12677 resolve_sal_pc (&sal);
12678 if (b->condition_not_parsed && b->extra_string != NULL)
12680 gdb::unique_xmalloc_ptr<char> cond_string, extra_string;
12681 int thread, task;
12683 find_condition_and_thread_for_sals (sals, b->extra_string.get (),
12684 &cond_string, &thread,
12685 &task, &extra_string);
12686 gdb_assert (b->cond_string == NULL);
12687 if (cond_string)
12688 b->cond_string = std::move (cond_string);
12689 b->thread = thread;
12690 b->task = task;
12691 if (extra_string)
12692 b->extra_string = std::move (extra_string);
12693 b->condition_not_parsed = 0;
12696 if (b->type == bp_static_tracepoint)
12697 sals[0] = update_static_tracepoint (b, sals[0]);
12699 *found = 1;
12701 else
12702 *found = 0;
12704 return sals;
12707 /* The default re_set method, for typical hardware or software
12708 breakpoints. Reevaluate the breakpoint and recreate its
12709 locations. */
12711 static void
12712 breakpoint_re_set_default (code_breakpoint *b)
12714 struct program_space *filter_pspace = current_program_space;
12715 std::vector<symtab_and_line> expanded, expanded_end;
12717 int found;
12718 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
12719 filter_pspace, &found);
12720 if (found)
12721 expanded = std::move (sals);
12723 if (b->location_range_end != NULL)
12725 std::vector<symtab_and_line> sals_end
12726 = location_to_sals (b, b->location_range_end.get (),
12727 filter_pspace, &found);
12728 if (found)
12729 expanded_end = std::move (sals_end);
12732 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
12735 /* Default method for creating SALs from an address string. It basically
12736 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12738 static void
12739 create_sals_from_location_default (struct event_location *location,
12740 struct linespec_result *canonical)
12742 parse_breakpoint_sals (location, canonical);
12745 /* Decode the line represented by S by calling decode_line_full. This is the
12746 default function for the `decode_location' method of breakpoint_ops. */
12748 static std::vector<symtab_and_line>
12749 decode_location_default (struct breakpoint *b,
12750 struct event_location *location,
12751 struct program_space *search_pspace)
12753 struct linespec_result canonical;
12755 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
12756 NULL, 0, &canonical, multiple_symbols_all,
12757 b->filter.get ());
12759 /* We should get 0 or 1 resulting SALs. */
12760 gdb_assert (canonical.lsals.size () < 2);
12762 if (!canonical.lsals.empty ())
12764 const linespec_sals &lsal = canonical.lsals[0];
12765 return std::move (lsal.sals);
12767 return {};
12770 /* Reset a breakpoint. */
12772 static void
12773 breakpoint_re_set_one (breakpoint *b)
12775 input_radix = b->input_radix;
12776 set_language (b->language);
12778 b->re_set ();
12781 /* Re-set breakpoint locations for the current program space.
12782 Locations bound to other program spaces are left untouched. */
12784 void
12785 breakpoint_re_set (void)
12788 scoped_restore_current_language save_language;
12789 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
12790 scoped_restore_current_pspace_and_thread restore_pspace_thread;
12792 /* breakpoint_re_set_one sets the current_language to the language
12793 of the breakpoint it is resetting (see prepare_re_set_context)
12794 before re-evaluating the breakpoint's location. This change can
12795 unfortunately get undone by accident if the language_mode is set
12796 to auto, and we either switch frames, or more likely in this context,
12797 we select the current frame.
12799 We prevent this by temporarily turning the language_mode to
12800 language_mode_manual. We restore it once all breakpoints
12801 have been reset. */
12802 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
12803 language_mode = language_mode_manual;
12805 /* Note: we must not try to insert locations until after all
12806 breakpoints have been re-set. Otherwise, e.g., when re-setting
12807 breakpoint 1, we'd insert the locations of breakpoint 2, which
12808 hadn't been re-set yet, and thus may have stale locations. */
12810 for (breakpoint *b : all_breakpoints_safe ())
12814 breakpoint_re_set_one (b);
12816 catch (const gdb_exception &ex)
12818 exception_fprintf (gdb_stderr, ex,
12819 "Error in re-setting breakpoint %d: ",
12820 b->number);
12824 jit_breakpoint_re_set ();
12827 create_overlay_event_breakpoint ();
12828 create_longjmp_master_breakpoint ();
12829 create_std_terminate_master_breakpoint ();
12830 create_exception_master_breakpoint ();
12832 /* Now we can insert. */
12833 update_global_location_list (UGLL_MAY_INSERT);
12836 /* Reset the thread number of this breakpoint:
12838 - If the breakpoint is for all threads, leave it as-is.
12839 - Else, reset it to the current thread for inferior_ptid. */
12840 void
12841 breakpoint_re_set_thread (struct breakpoint *b)
12843 if (b->thread != -1)
12845 b->thread = inferior_thread ()->global_num;
12847 /* We're being called after following a fork. The new fork is
12848 selected as current, and unless this was a vfork will have a
12849 different program space from the original thread. Reset that
12850 as well. */
12851 b->loc->pspace = current_program_space;
12855 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
12856 If from_tty is nonzero, it prints a message to that effect,
12857 which ends with a period (no newline). */
12859 void
12860 set_ignore_count (int bptnum, int count, int from_tty)
12862 if (count < 0)
12863 count = 0;
12865 for (breakpoint *b : all_breakpoints ())
12866 if (b->number == bptnum)
12868 if (is_tracepoint (b))
12870 if (from_tty && count != 0)
12871 gdb_printf (_("Ignore count ignored for tracepoint %d."),
12872 bptnum);
12873 return;
12876 b->ignore_count = count;
12877 if (from_tty)
12879 if (count == 0)
12880 gdb_printf (_("Will stop next time "
12881 "breakpoint %d is reached."),
12882 bptnum);
12883 else if (count == 1)
12884 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
12885 bptnum);
12886 else
12887 gdb_printf (_("Will ignore next %d "
12888 "crossings of breakpoint %d."),
12889 count, bptnum);
12891 gdb::observers::breakpoint_modified.notify (b);
12892 return;
12895 error (_("No breakpoint number %d."), bptnum);
12898 /* Command to set ignore-count of breakpoint N to COUNT. */
12900 static void
12901 ignore_command (const char *args, int from_tty)
12903 const char *p = args;
12904 int num;
12906 if (p == 0)
12907 error_no_arg (_("a breakpoint number"));
12909 num = get_number (&p);
12910 if (num == 0)
12911 error (_("bad breakpoint number: '%s'"), args);
12912 if (*p == 0)
12913 error (_("Second argument (specified ignore-count) is missing."));
12915 set_ignore_count (num,
12916 longest_to_int (value_as_long (parse_and_eval (p))),
12917 from_tty);
12918 if (from_tty)
12919 gdb_printf ("\n");
12923 /* Call FUNCTION on each of the breakpoints with numbers in the range
12924 defined by BP_NUM_RANGE (an inclusive range). */
12926 static void
12927 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
12928 gdb::function_view<void (breakpoint *)> function)
12930 if (bp_num_range.first == 0)
12932 warning (_("bad breakpoint number at or near '%d'"),
12933 bp_num_range.first);
12935 else
12937 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
12939 bool match = false;
12941 for (breakpoint *b : all_breakpoints_safe ())
12942 if (b->number == i)
12944 match = true;
12945 function (b);
12946 break;
12948 if (!match)
12949 gdb_printf (_("No breakpoint number %d.\n"), i);
12954 /* Call FUNCTION on each of the breakpoints whose numbers are given in
12955 ARGS. */
12957 static void
12958 map_breakpoint_numbers (const char *args,
12959 gdb::function_view<void (breakpoint *)> function)
12961 if (args == NULL || *args == '\0')
12962 error_no_arg (_("one or more breakpoint numbers"));
12964 number_or_range_parser parser (args);
12966 while (!parser.finished ())
12968 int num = parser.get_number ();
12969 map_breakpoint_number_range (std::make_pair (num, num), function);
12973 /* Return the breakpoint location structure corresponding to the
12974 BP_NUM and LOC_NUM values. */
12976 static struct bp_location *
12977 find_location_by_number (int bp_num, int loc_num)
12979 breakpoint *b = get_breakpoint (bp_num);
12981 if (!b || b->number != bp_num)
12982 error (_("Bad breakpoint number '%d'"), bp_num);
12984 if (loc_num == 0)
12985 error (_("Bad breakpoint location number '%d'"), loc_num);
12987 int n = 0;
12988 for (bp_location *loc : b->locations ())
12989 if (++n == loc_num)
12990 return loc;
12992 error (_("Bad breakpoint location number '%d'"), loc_num);
12995 /* Modes of operation for extract_bp_num. */
12996 enum class extract_bp_kind
12998 /* Extracting a breakpoint number. */
13001 /* Extracting a location number. */
13002 loc,
13005 /* Extract a breakpoint or location number (as determined by KIND)
13006 from the string starting at START. TRAILER is a character which
13007 can be found after the number. If you don't want a trailer, use
13008 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13009 string. This always returns a positive integer. */
13011 static int
13012 extract_bp_num (extract_bp_kind kind, const char *start,
13013 int trailer, const char **end_out = NULL)
13015 const char *end = start;
13016 int num = get_number_trailer (&end, trailer);
13017 if (num < 0)
13018 error (kind == extract_bp_kind::bp
13019 ? _("Negative breakpoint number '%.*s'")
13020 : _("Negative breakpoint location number '%.*s'"),
13021 int (end - start), start);
13022 if (num == 0)
13023 error (kind == extract_bp_kind::bp
13024 ? _("Bad breakpoint number '%.*s'")
13025 : _("Bad breakpoint location number '%.*s'"),
13026 int (end - start), start);
13028 if (end_out != NULL)
13029 *end_out = end;
13030 return num;
13033 /* Extract a breakpoint or location range (as determined by KIND) in
13034 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13035 representing the (inclusive) range. The returned pair's elements
13036 are always positive integers. */
13038 static std::pair<int, int>
13039 extract_bp_or_bp_range (extract_bp_kind kind,
13040 const std::string &arg,
13041 std::string::size_type arg_offset)
13043 std::pair<int, int> range;
13044 const char *bp_loc = &arg[arg_offset];
13045 std::string::size_type dash = arg.find ('-', arg_offset);
13046 if (dash != std::string::npos)
13048 /* bp_loc is a range (x-z). */
13049 if (arg.length () == dash + 1)
13050 error (kind == extract_bp_kind::bp
13051 ? _("Bad breakpoint number at or near: '%s'")
13052 : _("Bad breakpoint location number at or near: '%s'"),
13053 bp_loc);
13055 const char *end;
13056 const char *start_first = bp_loc;
13057 const char *start_second = &arg[dash + 1];
13058 range.first = extract_bp_num (kind, start_first, '-');
13059 range.second = extract_bp_num (kind, start_second, '\0', &end);
13061 if (range.first > range.second)
13062 error (kind == extract_bp_kind::bp
13063 ? _("Inverted breakpoint range at '%.*s'")
13064 : _("Inverted breakpoint location range at '%.*s'"),
13065 int (end - start_first), start_first);
13067 else
13069 /* bp_loc is a single value. */
13070 range.first = extract_bp_num (kind, bp_loc, '\0');
13071 range.second = range.first;
13073 return range;
13076 /* Extract the breakpoint/location range specified by ARG. Returns
13077 the breakpoint range in BP_NUM_RANGE, and the location range in
13078 BP_LOC_RANGE.
13080 ARG may be in any of the following forms:
13082 x where 'x' is a breakpoint number.
13083 x-y where 'x' and 'y' specify a breakpoint numbers range.
13084 x.y where 'x' is a breakpoint number and 'y' a location number.
13085 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13086 location number range.
13089 static void
13090 extract_bp_number_and_location (const std::string &arg,
13091 std::pair<int, int> &bp_num_range,
13092 std::pair<int, int> &bp_loc_range)
13094 std::string::size_type dot = arg.find ('.');
13096 if (dot != std::string::npos)
13098 /* Handle 'x.y' and 'x.y-z' cases. */
13100 if (arg.length () == dot + 1 || dot == 0)
13101 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
13103 bp_num_range.first
13104 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13105 bp_num_range.second = bp_num_range.first;
13107 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13108 arg, dot + 1);
13110 else
13112 /* Handle x and x-y cases. */
13114 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
13115 bp_loc_range.first = 0;
13116 bp_loc_range.second = 0;
13120 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13121 specifies whether to enable or disable. */
13123 static void
13124 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13126 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13127 if (loc != NULL)
13129 if (loc->disabled_by_cond && enable)
13130 error (_("Breakpoint %d's condition is invalid at location %d, "
13131 "cannot enable."), bp_num, loc_num);
13133 if (loc->enabled != enable)
13135 loc->enabled = enable;
13136 mark_breakpoint_location_modified (loc);
13138 if (target_supports_enable_disable_tracepoint ()
13139 && current_trace_status ()->running && loc->owner
13140 && is_tracepoint (loc->owner))
13141 target_disable_tracepoint (loc);
13143 update_global_location_list (UGLL_DONT_INSERT);
13145 gdb::observers::breakpoint_modified.notify (loc->owner);
13148 /* Enable or disable a range of breakpoint locations. BP_NUM is the
13149 number of the breakpoint, and BP_LOC_RANGE specifies the
13150 (inclusive) range of location numbers of that breakpoint to
13151 enable/disable. ENABLE specifies whether to enable or disable the
13152 location. */
13154 static void
13155 enable_disable_breakpoint_location_range (int bp_num,
13156 std::pair<int, int> &bp_loc_range,
13157 bool enable)
13159 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13160 enable_disable_bp_num_loc (bp_num, i, enable);
13163 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13164 If from_tty is nonzero, it prints a message to that effect,
13165 which ends with a period (no newline). */
13167 void
13168 disable_breakpoint (struct breakpoint *bpt)
13170 /* Never disable a watchpoint scope breakpoint; we want to
13171 hit them when we leave scope so we can delete both the
13172 watchpoint and its scope breakpoint at that time. */
13173 if (bpt->type == bp_watchpoint_scope)
13174 return;
13176 bpt->enable_state = bp_disabled;
13178 /* Mark breakpoint locations modified. */
13179 mark_breakpoint_modified (bpt);
13181 if (target_supports_enable_disable_tracepoint ()
13182 && current_trace_status ()->running && is_tracepoint (bpt))
13184 for (bp_location *location : bpt->locations ())
13185 target_disable_tracepoint (location);
13188 update_global_location_list (UGLL_DONT_INSERT);
13190 gdb::observers::breakpoint_modified.notify (bpt);
13193 /* Enable or disable the breakpoint(s) or breakpoint location(s)
13194 specified in ARGS. ARGS may be in any of the formats handled by
13195 extract_bp_number_and_location. ENABLE specifies whether to enable
13196 or disable the breakpoints/locations. */
13198 static void
13199 enable_disable_command (const char *args, int from_tty, bool enable)
13201 if (args == 0)
13203 for (breakpoint *bpt : all_breakpoints ())
13204 if (user_breakpoint_p (bpt))
13206 if (enable)
13207 enable_breakpoint (bpt);
13208 else
13209 disable_breakpoint (bpt);
13212 else
13214 std::string num = extract_arg (&args);
13216 while (!num.empty ())
13218 std::pair<int, int> bp_num_range, bp_loc_range;
13220 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13222 if (bp_loc_range.first == bp_loc_range.second
13223 && bp_loc_range.first == 0)
13225 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
13226 map_breakpoint_number_range (bp_num_range,
13227 enable
13228 ? enable_breakpoint
13229 : disable_breakpoint);
13231 else
13233 /* Handle breakpoint ids with formats 'x.y' or
13234 'x.y-z'. */
13235 enable_disable_breakpoint_location_range
13236 (bp_num_range.first, bp_loc_range, enable);
13238 num = extract_arg (&args);
13243 /* The disable command disables the specified breakpoints/locations
13244 (or all defined breakpoints) so they're no longer effective in
13245 stopping the inferior. ARGS may be in any of the forms defined in
13246 extract_bp_number_and_location. */
13248 static void
13249 disable_command (const char *args, int from_tty)
13251 enable_disable_command (args, from_tty, false);
13254 static void
13255 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13256 int count)
13258 int target_resources_ok;
13260 if (bpt->type == bp_hardware_breakpoint)
13262 int i;
13263 i = hw_breakpoint_used_count ();
13264 target_resources_ok =
13265 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13266 i + 1, 0);
13267 if (target_resources_ok == 0)
13268 error (_("No hardware breakpoint support in the target."));
13269 else if (target_resources_ok < 0)
13270 error (_("Hardware breakpoints used exceeds limit."));
13273 if (is_watchpoint (bpt))
13275 /* Initialize it just to avoid a GCC false warning. */
13276 enum enable_state orig_enable_state = bp_disabled;
13280 struct watchpoint *w = (struct watchpoint *) bpt;
13282 orig_enable_state = bpt->enable_state;
13283 bpt->enable_state = bp_enabled;
13284 update_watchpoint (w, 1 /* reparse */);
13286 catch (const gdb_exception &e)
13288 bpt->enable_state = orig_enable_state;
13289 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13290 bpt->number);
13291 return;
13295 bpt->enable_state = bp_enabled;
13297 /* Mark breakpoint locations modified. */
13298 mark_breakpoint_modified (bpt);
13300 if (target_supports_enable_disable_tracepoint ()
13301 && current_trace_status ()->running && is_tracepoint (bpt))
13303 for (bp_location *location : bpt->locations ())
13304 target_enable_tracepoint (location);
13307 bpt->disposition = disposition;
13308 bpt->enable_count = count;
13309 update_global_location_list (UGLL_MAY_INSERT);
13311 gdb::observers::breakpoint_modified.notify (bpt);
13315 void
13316 enable_breakpoint (struct breakpoint *bpt)
13318 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13321 /* The enable command enables the specified breakpoints/locations (or
13322 all defined breakpoints) so they once again become (or continue to
13323 be) effective in stopping the inferior. ARGS may be in any of the
13324 forms defined in extract_bp_number_and_location. */
13326 static void
13327 enable_command (const char *args, int from_tty)
13329 enable_disable_command (args, from_tty, true);
13332 static void
13333 enable_once_command (const char *args, int from_tty)
13335 map_breakpoint_numbers
13336 (args, [&] (breakpoint *b)
13338 iterate_over_related_breakpoints
13339 (b, [&] (breakpoint *bpt)
13341 enable_breakpoint_disp (bpt, disp_disable, 1);
13346 static void
13347 enable_count_command (const char *args, int from_tty)
13349 int count;
13351 if (args == NULL)
13352 error_no_arg (_("hit count"));
13354 count = get_number (&args);
13356 map_breakpoint_numbers
13357 (args, [&] (breakpoint *b)
13359 iterate_over_related_breakpoints
13360 (b, [&] (breakpoint *bpt)
13362 enable_breakpoint_disp (bpt, disp_disable, count);
13367 static void
13368 enable_delete_command (const char *args, int from_tty)
13370 map_breakpoint_numbers
13371 (args, [&] (breakpoint *b)
13373 iterate_over_related_breakpoints
13374 (b, [&] (breakpoint *bpt)
13376 enable_breakpoint_disp (bpt, disp_del, 1);
13381 /* Invalidate last known value of any hardware watchpoint if
13382 the memory which that value represents has been written to by
13383 GDB itself. */
13385 static void
13386 invalidate_bp_value_on_memory_change (struct inferior *inferior,
13387 CORE_ADDR addr, ssize_t len,
13388 const bfd_byte *data)
13390 for (breakpoint *bp : all_breakpoints ())
13391 if (bp->enable_state == bp_enabled
13392 && bp->type == bp_hardware_watchpoint)
13394 struct watchpoint *wp = (struct watchpoint *) bp;
13396 if (wp->val_valid && wp->val != nullptr)
13398 for (bp_location *loc : bp->locations ())
13399 if (loc->loc_type == bp_loc_hardware_watchpoint
13400 && loc->address + loc->length > addr
13401 && addr + len > loc->address)
13403 wp->val = NULL;
13404 wp->val_valid = false;
13410 /* Create and insert a breakpoint for software single step. */
13412 void
13413 insert_single_step_breakpoint (struct gdbarch *gdbarch,
13414 const address_space *aspace,
13415 CORE_ADDR next_pc)
13417 struct thread_info *tp = inferior_thread ();
13418 struct symtab_and_line sal;
13419 CORE_ADDR pc = next_pc;
13421 if (tp->control.single_step_breakpoints == NULL)
13423 std::unique_ptr<breakpoint> b
13424 (new momentary_breakpoint (gdbarch, bp_single_step,
13425 current_program_space,
13426 null_frame_id,
13427 tp->global_num));
13429 tp->control.single_step_breakpoints
13430 = add_to_breakpoint_chain (std::move (b));
13433 sal = find_pc_line (pc, 0);
13434 sal.pc = pc;
13435 sal.section = find_pc_overlay (pc);
13436 sal.explicit_pc = 1;
13438 auto *ss_bp
13439 = static_cast<momentary_breakpoint *> (tp->control.single_step_breakpoints);
13440 ss_bp->add_location (sal);
13442 update_global_location_list (UGLL_INSERT);
13445 /* Insert single step breakpoints according to the current state. */
13448 insert_single_step_breakpoints (struct gdbarch *gdbarch)
13450 struct regcache *regcache = get_current_regcache ();
13451 std::vector<CORE_ADDR> next_pcs;
13453 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
13455 if (!next_pcs.empty ())
13457 struct frame_info *frame = get_current_frame ();
13458 const address_space *aspace = get_frame_address_space (frame);
13460 for (CORE_ADDR pc : next_pcs)
13461 insert_single_step_breakpoint (gdbarch, aspace, pc);
13463 return 1;
13465 else
13466 return 0;
13469 /* See breakpoint.h. */
13472 breakpoint_has_location_inserted_here (struct breakpoint *bp,
13473 const address_space *aspace,
13474 CORE_ADDR pc)
13476 for (bp_location *loc : bp->locations ())
13477 if (loc->inserted
13478 && breakpoint_location_address_match (loc, aspace, pc))
13479 return 1;
13481 return 0;
13484 /* Check whether a software single-step breakpoint is inserted at
13485 PC. */
13488 single_step_breakpoint_inserted_here_p (const address_space *aspace,
13489 CORE_ADDR pc)
13491 for (breakpoint *bpt : all_breakpoints ())
13493 if (bpt->type == bp_single_step
13494 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13495 return 1;
13497 return 0;
13500 /* Tracepoint-specific operations. */
13502 /* Set tracepoint count to NUM. */
13503 static void
13504 set_tracepoint_count (int num)
13506 tracepoint_count = num;
13507 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
13510 static void
13511 trace_command (const char *arg, int from_tty)
13513 event_location_up location = string_to_event_location (&arg,
13514 current_language);
13515 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
13516 (location.get (), true /* is_tracepoint */);
13518 create_breakpoint (get_current_arch (),
13519 location.get (),
13520 NULL, 0, arg, false, 1 /* parse arg */,
13521 0 /* tempflag */,
13522 bp_tracepoint /* type_wanted */,
13523 0 /* Ignore count */,
13524 pending_break_support,
13525 ops,
13526 from_tty,
13527 1 /* enabled */,
13528 0 /* internal */, 0);
13531 static void
13532 ftrace_command (const char *arg, int from_tty)
13534 event_location_up location = string_to_event_location (&arg,
13535 current_language);
13536 create_breakpoint (get_current_arch (),
13537 location.get (),
13538 NULL, 0, arg, false, 1 /* parse arg */,
13539 0 /* tempflag */,
13540 bp_fast_tracepoint /* type_wanted */,
13541 0 /* Ignore count */,
13542 pending_break_support,
13543 &code_breakpoint_ops,
13544 from_tty,
13545 1 /* enabled */,
13546 0 /* internal */, 0);
13549 /* strace command implementation. Creates a static tracepoint. */
13551 static void
13552 strace_command (const char *arg, int from_tty)
13554 const struct breakpoint_ops *ops;
13555 event_location_up location;
13556 enum bptype type;
13558 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13559 or with a normal static tracepoint. */
13560 if (arg && startswith (arg, "-m") && isspace (arg[2]))
13562 ops = &strace_marker_breakpoint_ops;
13563 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
13564 type = bp_static_marker_tracepoint;
13566 else
13568 ops = &code_breakpoint_ops;
13569 location = string_to_event_location (&arg, current_language);
13570 type = bp_static_tracepoint;
13573 create_breakpoint (get_current_arch (),
13574 location.get (),
13575 NULL, 0, arg, false, 1 /* parse arg */,
13576 0 /* tempflag */,
13577 type /* type_wanted */,
13578 0 /* Ignore count */,
13579 pending_break_support,
13580 ops,
13581 from_tty,
13582 1 /* enabled */,
13583 0 /* internal */, 0);
13586 /* Set up a fake reader function that gets command lines from a linked
13587 list that was acquired during tracepoint uploading. */
13589 static struct uploaded_tp *this_utp;
13590 static int next_cmd;
13592 static char *
13593 read_uploaded_action (void)
13595 char *rslt = nullptr;
13597 if (next_cmd < this_utp->cmd_strings.size ())
13599 rslt = this_utp->cmd_strings[next_cmd].get ();
13600 next_cmd++;
13603 return rslt;
13606 /* Given information about a tracepoint as recorded on a target (which
13607 can be either a live system or a trace file), attempt to create an
13608 equivalent GDB tracepoint. This is not a reliable process, since
13609 the target does not necessarily have all the information used when
13610 the tracepoint was originally defined. */
13612 struct tracepoint *
13613 create_tracepoint_from_upload (struct uploaded_tp *utp)
13615 const char *addr_str;
13616 char small_buf[100];
13617 struct tracepoint *tp;
13619 if (utp->at_string)
13620 addr_str = utp->at_string.get ();
13621 else
13623 /* In the absence of a source location, fall back to raw
13624 address. Since there is no way to confirm that the address
13625 means the same thing as when the trace was started, warn the
13626 user. */
13627 warning (_("Uploaded tracepoint %d has no "
13628 "source location, using raw address"),
13629 utp->number);
13630 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
13631 addr_str = small_buf;
13634 /* There's not much we can do with a sequence of bytecodes. */
13635 if (utp->cond && !utp->cond_string)
13636 warning (_("Uploaded tracepoint %d condition "
13637 "has no source form, ignoring it"),
13638 utp->number);
13640 event_location_up location = string_to_event_location (&addr_str,
13641 current_language);
13642 if (!create_breakpoint (get_current_arch (),
13643 location.get (),
13644 utp->cond_string.get (), -1, addr_str,
13645 false /* force_condition */,
13646 0 /* parse cond/thread */,
13647 0 /* tempflag */,
13648 utp->type /* type_wanted */,
13649 0 /* Ignore count */,
13650 pending_break_support,
13651 &code_breakpoint_ops,
13652 0 /* from_tty */,
13653 utp->enabled /* enabled */,
13654 0 /* internal */,
13655 CREATE_BREAKPOINT_FLAGS_INSERTED))
13656 return NULL;
13658 /* Get the tracepoint we just created. */
13659 tp = get_tracepoint (tracepoint_count);
13660 gdb_assert (tp != NULL);
13662 if (utp->pass > 0)
13664 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
13665 tp->number);
13667 trace_pass_command (small_buf, 0);
13670 /* If we have uploaded versions of the original commands, set up a
13671 special-purpose "reader" function and call the usual command line
13672 reader, then pass the result to the breakpoint command-setting
13673 function. */
13674 if (!utp->cmd_strings.empty ())
13676 counted_command_line cmd_list;
13678 this_utp = utp;
13679 next_cmd = 0;
13681 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
13683 breakpoint_set_commands (tp, std::move (cmd_list));
13685 else if (!utp->actions.empty ()
13686 || !utp->step_actions.empty ())
13687 warning (_("Uploaded tracepoint %d actions "
13688 "have no source form, ignoring them"),
13689 utp->number);
13691 /* Copy any status information that might be available. */
13692 tp->hit_count = utp->hit_count;
13693 tp->traceframe_usage = utp->traceframe_usage;
13695 return tp;
13698 /* Print information on tracepoint number TPNUM_EXP, or all if
13699 omitted. */
13701 static void
13702 info_tracepoints_command (const char *args, int from_tty)
13704 struct ui_out *uiout = current_uiout;
13705 int num_printed;
13707 num_printed = breakpoint_1 (args, false, is_tracepoint);
13709 if (num_printed == 0)
13711 if (args == NULL || *args == '\0')
13712 uiout->message ("No tracepoints.\n");
13713 else
13714 uiout->message ("No tracepoint matching '%s'.\n", args);
13717 default_collect_info ();
13720 /* The 'enable trace' command enables tracepoints.
13721 Not supported by all targets. */
13722 static void
13723 enable_trace_command (const char *args, int from_tty)
13725 enable_command (args, from_tty);
13728 /* The 'disable trace' command disables tracepoints.
13729 Not supported by all targets. */
13730 static void
13731 disable_trace_command (const char *args, int from_tty)
13733 disable_command (args, from_tty);
13736 /* Remove a tracepoint (or all if no argument). */
13737 static void
13738 delete_trace_command (const char *arg, int from_tty)
13740 dont_repeat ();
13742 if (arg == 0)
13744 int breaks_to_delete = 0;
13746 /* Delete all breakpoints if no argument.
13747 Do not delete internal or call-dummy breakpoints, these
13748 have to be deleted with an explicit breakpoint number
13749 argument. */
13750 for (breakpoint *tp : all_tracepoints ())
13751 if (is_tracepoint (tp) && user_breakpoint_p (tp))
13753 breaks_to_delete = 1;
13754 break;
13757 /* Ask user only if there are some breakpoints to delete. */
13758 if (!from_tty
13759 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13761 for (breakpoint *b : all_breakpoints_safe ())
13762 if (is_tracepoint (b) && user_breakpoint_p (b))
13763 delete_breakpoint (b);
13766 else
13767 map_breakpoint_numbers
13768 (arg, [&] (breakpoint *br)
13770 iterate_over_related_breakpoints (br, delete_breakpoint);
13774 /* Helper function for trace_pass_command. */
13776 static void
13777 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
13779 tp->pass_count = count;
13780 gdb::observers::breakpoint_modified.notify (tp);
13781 if (from_tty)
13782 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
13783 tp->number, count);
13786 /* Set passcount for tracepoint.
13788 First command argument is passcount, second is tracepoint number.
13789 If tracepoint number omitted, apply to most recently defined.
13790 Also accepts special argument "all". */
13792 static void
13793 trace_pass_command (const char *args, int from_tty)
13795 struct tracepoint *t1;
13796 ULONGEST count;
13798 if (args == 0 || *args == 0)
13799 error (_("passcount command requires an "
13800 "argument (count + optional TP num)"));
13802 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
13804 args = skip_spaces (args);
13805 if (*args && strncasecmp (args, "all", 3) == 0)
13807 args += 3; /* Skip special argument "all". */
13808 if (*args)
13809 error (_("Junk at end of arguments."));
13811 for (breakpoint *b : all_tracepoints ())
13813 t1 = (struct tracepoint *) b;
13814 trace_pass_set_count (t1, count, from_tty);
13817 else if (*args == '\0')
13819 t1 = get_tracepoint_by_number (&args, NULL);
13820 if (t1)
13821 trace_pass_set_count (t1, count, from_tty);
13823 else
13825 number_or_range_parser parser (args);
13826 while (!parser.finished ())
13828 t1 = get_tracepoint_by_number (&args, &parser);
13829 if (t1)
13830 trace_pass_set_count (t1, count, from_tty);
13835 struct tracepoint *
13836 get_tracepoint (int num)
13838 for (breakpoint *t : all_tracepoints ())
13839 if (t->number == num)
13840 return (struct tracepoint *) t;
13842 return NULL;
13845 /* Find the tracepoint with the given target-side number (which may be
13846 different from the tracepoint number after disconnecting and
13847 reconnecting). */
13849 struct tracepoint *
13850 get_tracepoint_by_number_on_target (int num)
13852 for (breakpoint *b : all_tracepoints ())
13854 struct tracepoint *t = (struct tracepoint *) b;
13856 if (t->number_on_target == num)
13857 return t;
13860 return NULL;
13863 /* Utility: parse a tracepoint number and look it up in the list.
13864 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
13865 If the argument is missing, the most recent tracepoint
13866 (tracepoint_count) is returned. */
13868 struct tracepoint *
13869 get_tracepoint_by_number (const char **arg,
13870 number_or_range_parser *parser)
13872 int tpnum;
13873 const char *instring = arg == NULL ? NULL : *arg;
13875 if (parser != NULL)
13877 gdb_assert (!parser->finished ());
13878 tpnum = parser->get_number ();
13880 else if (arg == NULL || *arg == NULL || ! **arg)
13881 tpnum = tracepoint_count;
13882 else
13883 tpnum = get_number (arg);
13885 if (tpnum <= 0)
13887 if (instring && *instring)
13888 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
13889 instring);
13890 else
13891 gdb_printf (_("No previous tracepoint\n"));
13892 return NULL;
13895 for (breakpoint *t : all_tracepoints ())
13896 if (t->number == tpnum)
13897 return (struct tracepoint *) t;
13899 gdb_printf ("No tracepoint number %d.\n", tpnum);
13900 return NULL;
13903 void
13904 breakpoint::print_recreate_thread (struct ui_file *fp) const
13906 if (thread != -1)
13907 gdb_printf (fp, " thread %d", thread);
13909 if (task != 0)
13910 gdb_printf (fp, " task %d", task);
13912 gdb_printf (fp, "\n");
13915 /* Save information on user settable breakpoints (watchpoints, etc) to
13916 a new script file named FILENAME. If FILTER is non-NULL, call it
13917 on each breakpoint and only include the ones for which it returns
13918 true. */
13920 static void
13921 save_breakpoints (const char *filename, int from_tty,
13922 bool (*filter) (const struct breakpoint *))
13924 int any = 0;
13925 int extra_trace_bits = 0;
13927 if (filename == 0 || *filename == 0)
13928 error (_("Argument required (file name in which to save)"));
13930 /* See if we have anything to save. */
13931 for (breakpoint *tp : all_breakpoints ())
13933 /* Skip internal and momentary breakpoints. */
13934 if (!user_breakpoint_p (tp))
13935 continue;
13937 /* If we have a filter, only save the breakpoints it accepts. */
13938 if (filter && !filter (tp))
13939 continue;
13941 any = 1;
13943 if (is_tracepoint (tp))
13945 extra_trace_bits = 1;
13947 /* We can stop searching. */
13948 break;
13952 if (!any)
13954 warning (_("Nothing to save."));
13955 return;
13958 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
13960 stdio_file fp;
13962 if (!fp.open (expanded_filename.get (), "w"))
13963 error (_("Unable to open file '%s' for saving (%s)"),
13964 expanded_filename.get (), safe_strerror (errno));
13966 if (extra_trace_bits)
13967 save_trace_state_variables (&fp);
13969 for (breakpoint *tp : all_breakpoints ())
13971 /* Skip internal and momentary breakpoints. */
13972 if (!user_breakpoint_p (tp))
13973 continue;
13975 /* If we have a filter, only save the breakpoints it accepts. */
13976 if (filter && !filter (tp))
13977 continue;
13979 tp->print_recreate (&fp);
13981 /* Note, we can't rely on tp->number for anything, as we can't
13982 assume the recreated breakpoint numbers will match. Use $bpnum
13983 instead. */
13985 if (tp->cond_string)
13986 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
13988 if (tp->ignore_count)
13989 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
13991 if (tp->type != bp_dprintf && tp->commands)
13993 fp.puts (" commands\n");
13995 current_uiout->redirect (&fp);
13998 print_command_lines (current_uiout, tp->commands.get (), 2);
14000 catch (const gdb_exception &ex)
14002 current_uiout->redirect (NULL);
14003 throw;
14006 current_uiout->redirect (NULL);
14007 fp.puts (" end\n");
14010 if (tp->enable_state == bp_disabled)
14011 fp.puts ("disable $bpnum\n");
14013 /* If this is a multi-location breakpoint, check if the locations
14014 should be individually disabled. Watchpoint locations are
14015 special, and not user visible. */
14016 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14018 int n = 1;
14020 for (bp_location *loc : tp->locations ())
14022 if (!loc->enabled)
14023 fp.printf ("disable $bpnum.%d\n", n);
14025 n++;
14030 if (extra_trace_bits && !default_collect.empty ())
14031 fp.printf ("set default-collect %s\n", default_collect.c_str ());
14033 if (from_tty)
14034 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
14037 /* The `save breakpoints' command. */
14039 static void
14040 save_breakpoints_command (const char *args, int from_tty)
14042 save_breakpoints (args, from_tty, NULL);
14045 /* The `save tracepoints' command. */
14047 static void
14048 save_tracepoints_command (const char *args, int from_tty)
14050 save_breakpoints (args, from_tty, is_tracepoint);
14054 /* This help string is used to consolidate all the help string for specifying
14055 locations used by several commands. */
14057 #define LOCATION_HELP_STRING \
14058 "Linespecs are colon-separated lists of location parameters, such as\n\
14059 source filename, function name, label name, and line number.\n\
14060 Example: To specify the start of a label named \"the_top\" in the\n\
14061 function \"fact\" in the file \"factorial.c\", use\n\
14062 \"factorial.c:fact:the_top\".\n\
14064 Address locations begin with \"*\" and specify an exact address in the\n\
14065 program. Example: To specify the fourth byte past the start function\n\
14066 \"main\", use \"*main + 4\".\n\
14068 Explicit locations are similar to linespecs but use an option/argument\n\
14069 syntax to specify location parameters.\n\
14070 Example: To specify the start of the label named \"the_top\" in the\n\
14071 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
14072 -function fact -label the_top\".\n\
14074 By default, a specified function is matched against the program's\n\
14075 functions in all scopes. For C++, this means in all namespaces and\n\
14076 classes. For Ada, this means in all packages. E.g., in C++,\n\
14077 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14078 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
14079 specified name as a complete fully-qualified name instead."
14081 /* This help string is used for the break, hbreak, tbreak and thbreak
14082 commands. It is defined as a macro to prevent duplication.
14083 COMMAND should be a string constant containing the name of the
14084 command. */
14086 #define BREAK_ARGS_HELP(command) \
14087 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14088 \t[-force-condition] [if CONDITION]\n\
14089 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14090 probe point. Accepted values are `-probe' (for a generic, automatically\n\
14091 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14092 `-probe-dtrace' (for a DTrace probe).\n\
14093 LOCATION may be a linespec, address, or explicit location as described\n\
14094 below.\n\
14096 With no LOCATION, uses current execution address of the selected\n\
14097 stack frame. This is useful for breaking on return to a stack frame.\n\
14099 THREADNUM is the number from \"info threads\".\n\
14100 CONDITION is a boolean expression.\n\
14102 With the \"-force-condition\" flag, the condition is defined even when\n\
14103 it is invalid for all current locations.\n\
14104 \n" LOCATION_HELP_STRING "\n\n\
14105 Multiple breakpoints at one place are permitted, and useful if their\n\
14106 conditions are different.\n\
14108 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14110 /* List of subcommands for "catch". */
14111 static struct cmd_list_element *catch_cmdlist;
14113 /* List of subcommands for "tcatch". */
14114 static struct cmd_list_element *tcatch_cmdlist;
14116 void
14117 add_catch_command (const char *name, const char *docstring,
14118 cmd_func_ftype *func,
14119 completer_ftype *completer,
14120 void *user_data_catch,
14121 void *user_data_tcatch)
14123 struct cmd_list_element *command;
14125 command = add_cmd (name, class_breakpoint, docstring,
14126 &catch_cmdlist);
14127 command->func = func;
14128 command->set_context (user_data_catch);
14129 set_cmd_completer (command, completer);
14131 command = add_cmd (name, class_breakpoint, docstring,
14132 &tcatch_cmdlist);
14133 command->func = func;
14134 command->set_context (user_data_tcatch);
14135 set_cmd_completer (command, completer);
14138 /* Zero if any of the breakpoint's locations could be a location where
14139 functions have been inlined, nonzero otherwise. */
14141 static int
14142 is_non_inline_function (struct breakpoint *b)
14144 /* The shared library event breakpoint is set on the address of a
14145 non-inline function. */
14146 if (b->type == bp_shlib_event)
14147 return 1;
14149 return 0;
14152 /* Nonzero if the specified PC cannot be a location where functions
14153 have been inlined. */
14156 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
14157 const target_waitstatus &ws)
14159 for (breakpoint *b : all_breakpoints ())
14161 if (!is_non_inline_function (b))
14162 continue;
14164 for (bp_location *bl : b->locations ())
14166 if (!bl->shlib_disabled
14167 && bpstat_check_location (bl, aspace, pc, ws))
14168 return 1;
14172 return 0;
14175 /* Remove any references to OBJFILE which is going to be freed. */
14177 void
14178 breakpoint_free_objfile (struct objfile *objfile)
14180 for (bp_location *loc : all_bp_locations ())
14181 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
14182 loc->symtab = NULL;
14185 /* Chain containing all defined "enable breakpoint" subcommands. */
14187 static struct cmd_list_element *enablebreaklist = NULL;
14189 /* See breakpoint.h. */
14191 cmd_list_element *commands_cmd_element = nullptr;
14193 void _initialize_breakpoint ();
14194 void
14195 _initialize_breakpoint ()
14197 struct cmd_list_element *c;
14199 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14200 "breakpoint");
14201 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14202 "breakpoint");
14203 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14204 "breakpoint");
14206 breakpoint_chain = 0;
14207 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14208 before a breakpoint is set. */
14209 breakpoint_count = 0;
14211 tracepoint_count = 0;
14213 add_com ("ignore", class_breakpoint, ignore_command, _("\
14214 Set ignore-count of breakpoint number N to COUNT.\n\
14215 Usage is `ignore N COUNT'."));
14217 commands_cmd_element = add_com ("commands", class_breakpoint,
14218 commands_command, _("\
14219 Set commands to be executed when the given breakpoints are hit.\n\
14220 Give a space-separated breakpoint list as argument after \"commands\".\n\
14221 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14222 (e.g. `5-7').\n\
14223 With no argument, the targeted breakpoint is the last one set.\n\
14224 The commands themselves follow starting on the next line.\n\
14225 Type a line containing \"end\" to indicate the end of them.\n\
14226 Give \"silent\" as the first line to make the breakpoint silent;\n\
14227 then no output is printed when it is hit, except what the commands print."));
14229 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14230 static std::string condition_command_help
14231 = gdb::option::build_help (_("\
14232 Specify breakpoint number N to break only if COND is true.\n\
14233 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
14234 is an expression to be evaluated whenever breakpoint N is reached.\n\
14236 Options:\n\
14237 %OPTIONS%"), cc_opts);
14239 c = add_com ("condition", class_breakpoint, condition_command,
14240 condition_command_help.c_str ());
14241 set_cmd_completer_handle_brkchars (c, condition_completer);
14243 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
14244 Set a temporary breakpoint.\n\
14245 Like \"break\" except the breakpoint is only temporary,\n\
14246 so it will be deleted when hit. Equivalent to \"break\" followed\n\
14247 by using \"enable delete\" on the breakpoint number.\n\
14249 BREAK_ARGS_HELP ("tbreak")));
14250 set_cmd_completer (c, location_completer);
14252 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
14253 Set a hardware assisted breakpoint.\n\
14254 Like \"break\" except the breakpoint requires hardware support,\n\
14255 some target hardware may not have this support.\n\
14257 BREAK_ARGS_HELP ("hbreak")));
14258 set_cmd_completer (c, location_completer);
14260 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
14261 Set a temporary hardware assisted breakpoint.\n\
14262 Like \"hbreak\" except the breakpoint is only temporary,\n\
14263 so it will be deleted when hit.\n\
14265 BREAK_ARGS_HELP ("thbreak")));
14266 set_cmd_completer (c, location_completer);
14268 cmd_list_element *enable_cmd
14269 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14270 Enable all or some breakpoints.\n\
14271 Usage: enable [BREAKPOINTNUM]...\n\
14272 Give breakpoint numbers (separated by spaces) as arguments.\n\
14273 With no subcommand, breakpoints are enabled until you command otherwise.\n\
14274 This is used to cancel the effect of the \"disable\" command.\n\
14275 With a subcommand you can enable temporarily."),
14276 &enablelist, 1, &cmdlist);
14278 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
14280 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
14281 Enable all or some breakpoints.\n\
14282 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
14283 Give breakpoint numbers (separated by spaces) as arguments.\n\
14284 This is used to cancel the effect of the \"disable\" command.\n\
14285 May be abbreviated to simply \"enable\"."),
14286 &enablebreaklist, 1, &enablelist);
14288 add_cmd ("once", no_class, enable_once_command, _("\
14289 Enable some breakpoints for one hit.\n\
14290 Usage: enable breakpoints once BREAKPOINTNUM...\n\
14291 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14292 &enablebreaklist);
14294 add_cmd ("delete", no_class, enable_delete_command, _("\
14295 Enable some breakpoints and delete when hit.\n\
14296 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
14297 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14298 &enablebreaklist);
14300 add_cmd ("count", no_class, enable_count_command, _("\
14301 Enable some breakpoints for COUNT hits.\n\
14302 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
14303 If a breakpoint is hit while enabled in this fashion,\n\
14304 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14305 &enablebreaklist);
14307 add_cmd ("delete", no_class, enable_delete_command, _("\
14308 Enable some breakpoints and delete when hit.\n\
14309 Usage: enable delete BREAKPOINTNUM...\n\
14310 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14311 &enablelist);
14313 add_cmd ("once", no_class, enable_once_command, _("\
14314 Enable some breakpoints for one hit.\n\
14315 Usage: enable once BREAKPOINTNUM...\n\
14316 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14317 &enablelist);
14319 add_cmd ("count", no_class, enable_count_command, _("\
14320 Enable some breakpoints for COUNT hits.\n\
14321 Usage: enable count COUNT BREAKPOINTNUM...\n\
14322 If a breakpoint is hit while enabled in this fashion,\n\
14323 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14324 &enablelist);
14326 cmd_list_element *disable_cmd
14327 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14328 Disable all or some breakpoints.\n\
14329 Usage: disable [BREAKPOINTNUM]...\n\
14330 Arguments are breakpoint numbers with spaces in between.\n\
14331 To disable all breakpoints, give no argument.\n\
14332 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
14333 &disablelist, 1, &cmdlist);
14334 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14335 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
14337 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
14338 Disable all or some breakpoints.\n\
14339 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
14340 Arguments are breakpoint numbers with spaces in between.\n\
14341 To disable all breakpoints, give no argument.\n\
14342 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
14343 This command may be abbreviated \"disable\"."),
14344 &disablelist);
14346 cmd_list_element *delete_cmd
14347 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14348 Delete all or some breakpoints.\n\
14349 Usage: delete [BREAKPOINTNUM]...\n\
14350 Arguments are breakpoint numbers with spaces in between.\n\
14351 To delete all breakpoints, give no argument.\n\
14353 Also a prefix command for deletion of other GDB objects."),
14354 &deletelist, 1, &cmdlist);
14355 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14356 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
14358 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
14359 Delete all or some breakpoints or auto-display expressions.\n\
14360 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
14361 Arguments are breakpoint numbers with spaces in between.\n\
14362 To delete all breakpoints, give no argument.\n\
14363 This command may be abbreviated \"delete\"."),
14364 &deletelist);
14366 cmd_list_element *clear_cmd
14367 = add_com ("clear", class_breakpoint, clear_command, _("\
14368 Clear breakpoint at specified location.\n\
14369 Argument may be a linespec, explicit, or address location as described below.\n\
14371 With no argument, clears all breakpoints in the line that the selected frame\n\
14372 is executing in.\n"
14373 "\n" LOCATION_HELP_STRING "\n\n\
14374 See also the \"delete\" command which clears breakpoints by number."));
14375 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
14377 cmd_list_element *break_cmd
14378 = add_com ("break", class_breakpoint, break_command, _("\
14379 Set breakpoint at specified location.\n"
14380 BREAK_ARGS_HELP ("break")));
14381 set_cmd_completer (break_cmd, location_completer);
14383 add_com_alias ("b", break_cmd, class_run, 1);
14384 add_com_alias ("br", break_cmd, class_run, 1);
14385 add_com_alias ("bre", break_cmd, class_run, 1);
14386 add_com_alias ("brea", break_cmd, class_run, 1);
14388 cmd_list_element *info_breakpoints_cmd
14389 = add_info ("breakpoints", info_breakpoints_command, _("\
14390 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
14391 The \"Type\" column indicates one of:\n\
14392 \tbreakpoint - normal breakpoint\n\
14393 \twatchpoint - watchpoint\n\
14394 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14395 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14396 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14397 address and file/line number respectively.\n\
14399 Convenience variable \"$_\" and default examine address for \"x\"\n\
14400 are set to the address of the last breakpoint listed unless the command\n\
14401 is prefixed with \"server \".\n\n\
14402 Convenience variable \"$bpnum\" contains the number of the last\n\
14403 breakpoint set."));
14405 add_info_alias ("b", info_breakpoints_cmd, 1);
14407 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14408 Status of all breakpoints, or breakpoint number NUMBER.\n\
14409 The \"Type\" column indicates one of:\n\
14410 \tbreakpoint - normal breakpoint\n\
14411 \twatchpoint - watchpoint\n\
14412 \tlongjmp - internal breakpoint used to step through longjmp()\n\
14413 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14414 \tuntil - internal breakpoint used by the \"until\" command\n\
14415 \tfinish - internal breakpoint used by the \"finish\" command\n\
14416 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14417 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14418 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14419 address and file/line number respectively.\n\
14421 Convenience variable \"$_\" and default examine address for \"x\"\n\
14422 are set to the address of the last breakpoint listed unless the command\n\
14423 is prefixed with \"server \".\n\n\
14424 Convenience variable \"$bpnum\" contains the number of the last\n\
14425 breakpoint set."),
14426 &maintenanceinfolist);
14428 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
14429 Set catchpoints to catch events."),
14430 &catch_cmdlist,
14431 0/*allow-unknown*/, &cmdlist);
14433 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
14434 Set temporary catchpoints to catch events."),
14435 &tcatch_cmdlist,
14436 0/*allow-unknown*/, &cmdlist);
14438 const auto opts = make_watch_options_def_group (nullptr);
14440 static const std::string watch_help = gdb::option::build_help (_("\
14441 Set a watchpoint for EXPRESSION.\n\
14442 Usage: watch [-location] EXPRESSION\n\
14444 Options:\n\
14445 %OPTIONS%\n\
14447 A watchpoint stops execution of your program whenever the value of\n\
14448 an expression changes."), opts);
14449 c = add_com ("watch", class_breakpoint, watch_command,
14450 watch_help.c_str ());
14451 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14453 static const std::string rwatch_help = gdb::option::build_help (_("\
14454 Set a read watchpoint for EXPRESSION.\n\
14455 Usage: rwatch [-location] EXPRESSION\n\
14457 Options:\n\
14458 %OPTIONS%\n\
14460 A read watchpoint stops execution of your program whenever the value of\n\
14461 an expression is read."), opts);
14462 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14463 rwatch_help.c_str ());
14464 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14466 static const std::string awatch_help = gdb::option::build_help (_("\
14467 Set an access watchpoint for EXPRESSION.\n\
14468 Usage: awatch [-location] EXPRESSION\n\
14470 Options:\n\
14471 %OPTIONS%\n\
14473 An access watchpoint stops execution of your program whenever the value\n\
14474 of an expression is either read or written."), opts);
14475 c = add_com ("awatch", class_breakpoint, awatch_command,
14476 awatch_help.c_str ());
14477 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14479 add_info ("watchpoints", info_watchpoints_command, _("\
14480 Status of specified watchpoints (all watchpoints if no argument)."));
14482 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14483 respond to changes - contrary to the description. */
14484 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14485 &can_use_hw_watchpoints, _("\
14486 Set debugger's willingness to use watchpoint hardware."), _("\
14487 Show debugger's willingness to use watchpoint hardware."), _("\
14488 If zero, gdb will not use hardware for new watchpoints, even if\n\
14489 such is available. (However, any hardware watchpoints that were\n\
14490 created before setting this to nonzero, will continue to use watchpoint\n\
14491 hardware.)"),
14492 NULL,
14493 show_can_use_hw_watchpoints,
14494 &setlist, &showlist);
14496 can_use_hw_watchpoints = 1;
14498 /* Tracepoint manipulation commands. */
14500 cmd_list_element *trace_cmd
14501 = add_com ("trace", class_breakpoint, trace_command, _("\
14502 Set a tracepoint at specified location.\n\
14504 BREAK_ARGS_HELP ("trace") "\n\
14505 Do \"help tracepoints\" for info on other tracepoint commands."));
14506 set_cmd_completer (trace_cmd, location_completer);
14508 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14509 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14510 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14511 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
14513 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
14514 Set a fast tracepoint at specified location.\n\
14516 BREAK_ARGS_HELP ("ftrace") "\n\
14517 Do \"help tracepoints\" for info on other tracepoint commands."));
14518 set_cmd_completer (c, location_completer);
14520 c = add_com ("strace", class_breakpoint, strace_command, _("\
14521 Set a static tracepoint at location or marker.\n\
14523 strace [LOCATION] [if CONDITION]\n\
14524 LOCATION may be a linespec, explicit, or address location (described below) \n\
14525 or -m MARKER_ID.\n\n\
14526 If a marker id is specified, probe the marker with that name. With\n\
14527 no LOCATION, uses current execution address of the selected stack frame.\n\
14528 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14529 This collects arbitrary user data passed in the probe point call to the\n\
14530 tracing library. You can inspect it when analyzing the trace buffer,\n\
14531 by printing the $_sdata variable like any other convenience variable.\n\
14533 CONDITION is a boolean expression.\n\
14534 \n" LOCATION_HELP_STRING "\n\n\
14535 Multiple tracepoints at one place are permitted, and useful if their\n\
14536 conditions are different.\n\
14538 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14539 Do \"help tracepoints\" for info on other tracepoint commands."));
14540 set_cmd_completer (c, location_completer);
14542 cmd_list_element *info_tracepoints_cmd
14543 = add_info ("tracepoints", info_tracepoints_command, _("\
14544 Status of specified tracepoints (all tracepoints if no argument).\n\
14545 Convenience variable \"$tpnum\" contains the number of the\n\
14546 last tracepoint set."));
14548 add_info_alias ("tp", info_tracepoints_cmd, 1);
14550 cmd_list_element *delete_tracepoints_cmd
14551 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
14552 Delete specified tracepoints.\n\
14553 Arguments are tracepoint numbers, separated by spaces.\n\
14554 No argument means delete all tracepoints."),
14555 &deletelist);
14556 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
14558 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14559 Disable specified tracepoints.\n\
14560 Arguments are tracepoint numbers, separated by spaces.\n\
14561 No argument means disable all tracepoints."),
14562 &disablelist);
14563 deprecate_cmd (c, "disable");
14565 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14566 Enable specified tracepoints.\n\
14567 Arguments are tracepoint numbers, separated by spaces.\n\
14568 No argument means enable all tracepoints."),
14569 &enablelist);
14570 deprecate_cmd (c, "enable");
14572 add_com ("passcount", class_trace, trace_pass_command, _("\
14573 Set the passcount for a tracepoint.\n\
14574 The trace will end when the tracepoint has been passed 'count' times.\n\
14575 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14576 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14578 add_basic_prefix_cmd ("save", class_breakpoint,
14579 _("Save breakpoint definitions as a script."),
14580 &save_cmdlist,
14581 0/*allow-unknown*/, &cmdlist);
14583 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14584 Save current breakpoint definitions as a script.\n\
14585 This includes all types of breakpoints (breakpoints, watchpoints,\n\
14586 catchpoints, tracepoints). Use the 'source' command in another debug\n\
14587 session to restore them."),
14588 &save_cmdlist);
14589 set_cmd_completer (c, filename_completer);
14591 cmd_list_element *save_tracepoints_cmd
14592 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
14593 Save current tracepoint definitions as a script.\n\
14594 Use the 'source' command in another debug session to restore them."),
14595 &save_cmdlist);
14596 set_cmd_completer (save_tracepoints_cmd, filename_completer);
14598 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
14599 deprecate_cmd (c, "save tracepoints");
14601 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14602 _("\
14603 Breakpoint specific settings.\n\
14604 Configure various breakpoint-specific variables such as\n\
14605 pending breakpoint behavior."),
14606 _("\
14607 Breakpoint specific settings.\n\
14608 Configure various breakpoint-specific variables such as\n\
14609 pending breakpoint behavior."),
14610 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14611 &setlist, &showlist);
14613 add_setshow_auto_boolean_cmd ("pending", no_class,
14614 &pending_break_support, _("\
14615 Set debugger's behavior regarding pending breakpoints."), _("\
14616 Show debugger's behavior regarding pending breakpoints."), _("\
14617 If on, an unrecognized breakpoint location will cause gdb to create a\n\
14618 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14619 an error. If auto, an unrecognized breakpoint location results in a\n\
14620 user-query to see if a pending breakpoint should be created."),
14621 NULL,
14622 show_pending_break_support,
14623 &breakpoint_set_cmdlist,
14624 &breakpoint_show_cmdlist);
14626 pending_break_support = AUTO_BOOLEAN_AUTO;
14628 add_setshow_boolean_cmd ("auto-hw", no_class,
14629 &automatic_hardware_breakpoints, _("\
14630 Set automatic usage of hardware breakpoints."), _("\
14631 Show automatic usage of hardware breakpoints."), _("\
14632 If set, the debugger will automatically use hardware breakpoints for\n\
14633 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14634 a warning will be emitted for such breakpoints."),
14635 NULL,
14636 show_automatic_hardware_breakpoints,
14637 &breakpoint_set_cmdlist,
14638 &breakpoint_show_cmdlist);
14640 add_setshow_boolean_cmd ("always-inserted", class_support,
14641 &always_inserted_mode, _("\
14642 Set mode for inserting breakpoints."), _("\
14643 Show mode for inserting breakpoints."), _("\
14644 When this mode is on, breakpoints are inserted immediately as soon as\n\
14645 they're created, kept inserted even when execution stops, and removed\n\
14646 only when the user deletes them. When this mode is off (the default),\n\
14647 breakpoints are inserted only when execution continues, and removed\n\
14648 when execution stops."),
14649 NULL,
14650 &show_always_inserted_mode,
14651 &breakpoint_set_cmdlist,
14652 &breakpoint_show_cmdlist);
14654 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14655 condition_evaluation_enums,
14656 &condition_evaluation_mode_1, _("\
14657 Set mode of breakpoint condition evaluation."), _("\
14658 Show mode of breakpoint condition evaluation."), _("\
14659 When this is set to \"host\", breakpoint conditions will be\n\
14660 evaluated on the host's side by GDB. When it is set to \"target\",\n\
14661 breakpoint conditions will be downloaded to the target (if the target\n\
14662 supports such feature) and conditions will be evaluated on the target's side.\n\
14663 If this is set to \"auto\" (default), this will be automatically set to\n\
14664 \"target\" if it supports condition evaluation, otherwise it will\n\
14665 be set to \"host\"."),
14666 &set_condition_evaluation_mode,
14667 &show_condition_evaluation_mode,
14668 &breakpoint_set_cmdlist,
14669 &breakpoint_show_cmdlist);
14671 add_com ("break-range", class_breakpoint, break_range_command, _("\
14672 Set a breakpoint for an address range.\n\
14673 break-range START-LOCATION, END-LOCATION\n\
14674 where START-LOCATION and END-LOCATION can be one of the following:\n\
14675 LINENUM, for that line in the current file,\n\
14676 FILE:LINENUM, for that line in that file,\n\
14677 +OFFSET, for that number of lines after the current line\n\
14678 or the start of the range\n\
14679 FUNCTION, for the first line in that function,\n\
14680 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14681 *ADDRESS, for the instruction at that address.\n\
14683 The breakpoint will stop execution of the inferior whenever it executes\n\
14684 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14685 range (including START-LOCATION and END-LOCATION)."));
14687 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
14688 Set a dynamic printf at specified location.\n\
14689 dprintf location,format string,arg1,arg2,...\n\
14690 location may be a linespec, explicit, or address location.\n"
14691 "\n" LOCATION_HELP_STRING));
14692 set_cmd_completer (c, location_completer);
14694 add_setshow_enum_cmd ("dprintf-style", class_support,
14695 dprintf_style_enums, &dprintf_style, _("\
14696 Set the style of usage for dynamic printf."), _("\
14697 Show the style of usage for dynamic printf."), _("\
14698 This setting chooses how GDB will do a dynamic printf.\n\
14699 If the value is \"gdb\", then the printing is done by GDB to its own\n\
14700 console, as with the \"printf\" command.\n\
14701 If the value is \"call\", the print is done by calling a function in your\n\
14702 program; by default printf(), but you can choose a different function or\n\
14703 output stream by setting dprintf-function and dprintf-channel."),
14704 update_dprintf_commands, NULL,
14705 &setlist, &showlist);
14707 add_setshow_string_cmd ("dprintf-function", class_support,
14708 &dprintf_function, _("\
14709 Set the function to use for dynamic printf."), _("\
14710 Show the function to use for dynamic printf."), NULL,
14711 update_dprintf_commands, NULL,
14712 &setlist, &showlist);
14714 add_setshow_string_cmd ("dprintf-channel", class_support,
14715 &dprintf_channel, _("\
14716 Set the channel to use for dynamic printf."), _("\
14717 Show the channel to use for dynamic printf."), NULL,
14718 update_dprintf_commands, NULL,
14719 &setlist, &showlist);
14721 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14722 &disconnected_dprintf, _("\
14723 Set whether dprintf continues after GDB disconnects."), _("\
14724 Show whether dprintf continues after GDB disconnects."), _("\
14725 Use this to let dprintf commands continue to hit and produce output\n\
14726 even if GDB disconnects or detaches from the target."),
14727 NULL,
14728 NULL,
14729 &setlist, &showlist);
14731 add_com ("agent-printf", class_vars, agent_printf_command, _("\
14732 Target agent only formatted printing, like the C \"printf\" function.\n\
14733 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14734 This supports most C printf format specifications, like %s, %d, etc.\n\
14735 This is useful for formatted output in user-defined commands."));
14737 automatic_hardware_breakpoints = true;
14739 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14740 "breakpoint");
14741 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
14742 "breakpoint");