add_location_to_breakpoint -> breakpoint::add_location
[binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "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"
71
72 /* readline include files */
73 #include "readline/tilde.h"
74
75 /* readline defines this. */
76 #undef savestring
77
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"
84
85 /* Prototypes for local functions. */
86
87 static void map_breakpoint_numbers (const char *,
88 gdb::function_view<void (breakpoint *)>);
89
90 static void breakpoint_re_set_default (struct breakpoint *);
91
92 static void
93 create_sals_from_location_default (struct event_location *location,
94 struct linespec_result *canonical);
95
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 const struct breakpoint_ops *,
104 int, int, int, unsigned);
105
106 static std::vector<symtab_and_line> decode_location_default
107 (struct breakpoint *b, struct event_location *location,
108 struct program_space *search_pspace);
109
110 static int can_use_hardware_watchpoint
111 (const std::vector<value_ref_ptr> &vals);
112
113 static void mention (const breakpoint *);
114
115 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
116 enum bptype);
117 /* This function is used in gdbtk sources and thus can not be made
118 static. */
119 static struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
120 struct symtab_and_line,
121 enum bptype);
122
123 static struct breakpoint *
124 momentary_breakpoint_from_master (struct breakpoint *orig,
125 enum bptype type,
126 int loc_enabled);
127
128 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
129
130 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
131 CORE_ADDR bpaddr,
132 enum bptype bptype);
133
134 static void describe_other_breakpoints (struct gdbarch *,
135 struct program_space *, CORE_ADDR,
136 struct obj_section *, int);
137
138 static int watchpoint_locations_match (struct bp_location *loc1,
139 struct bp_location *loc2);
140
141 static int breakpoint_locations_match (struct bp_location *loc1,
142 struct bp_location *loc2,
143 bool sw_hw_bps_match = false);
144
145 static int breakpoint_location_address_match (struct bp_location *bl,
146 const struct address_space *aspace,
147 CORE_ADDR addr);
148
149 static int breakpoint_location_address_range_overlap (struct bp_location *,
150 const address_space *,
151 CORE_ADDR, int);
152
153 static int remove_breakpoint (struct bp_location *);
154 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
155
156 static enum print_stop_action print_bp_stop_message (bpstat *bs);
157
158 static int hw_breakpoint_used_count (void);
159
160 static int hw_watchpoint_use_count (struct breakpoint *);
161
162 static int hw_watchpoint_used_count_others (struct breakpoint *except,
163 enum bptype type,
164 int *other_type_used);
165
166 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
167 int count);
168
169 static void decref_bp_location (struct bp_location **loc);
170
171 static std::vector<symtab_and_line> bkpt_probe_decode_location
172 (struct breakpoint *b,
173 struct event_location *location,
174 struct program_space *search_pspace);
175
176 /* update_global_location_list's modes of operation wrt to whether to
177 insert locations now. */
178 enum ugll_insert_mode
179 {
180 /* Don't insert any breakpoint locations into the inferior, only
181 remove already-inserted locations that no longer should be
182 inserted. Functions that delete a breakpoint or breakpoints
183 should specify this mode, so that deleting a breakpoint doesn't
184 have the side effect of inserting the locations of other
185 breakpoints that are marked not-inserted, but should_be_inserted
186 returns true on them.
187
188 This behavior is useful is situations close to tear-down -- e.g.,
189 after an exec, while the target still has execution, but
190 breakpoint shadows of the previous executable image should *NOT*
191 be restored to the new image; or before detaching, where the
192 target still has execution and wants to delete breakpoints from
193 GDB's lists, and all breakpoints had already been removed from
194 the inferior. */
195 UGLL_DONT_INSERT,
196
197 /* May insert breakpoints iff breakpoints_should_be_inserted_now
198 claims breakpoints should be inserted now. */
199 UGLL_MAY_INSERT,
200
201 /* Insert locations now, irrespective of
202 breakpoints_should_be_inserted_now. E.g., say all threads are
203 stopped right now, and the user did "continue". We need to
204 insert breakpoints _before_ resuming the target, but
205 UGLL_MAY_INSERT wouldn't insert them, because
206 breakpoints_should_be_inserted_now returns false at that point,
207 as no thread is running yet. */
208 UGLL_INSERT
209 };
210
211 static void update_global_location_list (enum ugll_insert_mode);
212
213 static void update_global_location_list_nothrow (enum ugll_insert_mode);
214
215 static void insert_breakpoint_locations (void);
216
217 static void trace_pass_command (const char *, int);
218
219 static void set_tracepoint_count (int num);
220
221 static bool is_masked_watchpoint (const struct breakpoint *b);
222
223 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
224 otherwise. */
225
226 static int strace_marker_p (struct breakpoint *b);
227
228 static void bkpt_probe_create_sals_from_location
229 (struct event_location *location,
230 struct linespec_result *canonical);
231 static void tracepoint_probe_create_sals_from_location
232 (struct event_location *location,
233 struct linespec_result *canonical);
234
235 const struct breakpoint_ops base_breakpoint_ops =
236 {
237 create_sals_from_location_default,
238 create_breakpoints_sal,
239 };
240
241 /* Breakpoints set on probes. */
242 static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
243 {
244 bkpt_probe_create_sals_from_location,
245 create_breakpoints_sal,
246 };
247
248 /* Tracepoints set on probes. */
249 static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
250 {
251 tracepoint_probe_create_sals_from_location,
252 create_breakpoints_sal,
253 };
254
255 /* The structure to be used in regular breakpoints. */
256 struct ordinary_breakpoint : public base_breakpoint
257 {
258 using base_breakpoint::base_breakpoint;
259
260 int resources_needed (const struct bp_location *) override;
261 enum print_stop_action print_it (const bpstat *bs) const override;
262 void print_mention () const override;
263 void print_recreate (struct ui_file *fp) const override;
264 };
265
266 /* Internal breakpoints. */
267 struct internal_breakpoint : public base_breakpoint
268 {
269 using base_breakpoint::base_breakpoint;
270
271 void re_set () override;
272 void check_status (struct bpstat *bs) override;
273 enum print_stop_action print_it (const bpstat *bs) const override;
274 void print_mention () const override;
275 };
276
277 /* Momentary breakpoints. */
278 struct momentary_breakpoint : public base_breakpoint
279 {
280 using base_breakpoint::base_breakpoint;
281
282 void re_set () override;
283 void check_status (struct bpstat *bs) override;
284 enum print_stop_action print_it (const bpstat *bs) const override;
285 void print_mention () const override;
286 };
287
288 /* DPrintf breakpoints. */
289 struct dprintf_breakpoint : public ordinary_breakpoint
290 {
291 using ordinary_breakpoint::ordinary_breakpoint;
292
293 void re_set () override;
294 int breakpoint_hit (const struct bp_location *bl,
295 const address_space *aspace,
296 CORE_ADDR bp_addr,
297 const target_waitstatus &ws) override;
298 void print_recreate (struct ui_file *fp) const override;
299 void after_condition_true (struct bpstat *bs) override;
300 };
301
302 /* Ranged breakpoints. */
303 struct ranged_breakpoint : public ordinary_breakpoint
304 {
305 explicit ranged_breakpoint (struct gdbarch *gdbarch)
306 : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
307 {
308 }
309
310 int breakpoint_hit (const struct bp_location *bl,
311 const address_space *aspace,
312 CORE_ADDR bp_addr,
313 const target_waitstatus &ws) override;
314 int resources_needed (const struct bp_location *) override;
315 enum print_stop_action print_it (const bpstat *bs) const override;
316 bool print_one (bp_location **) const override;
317 void print_one_detail (struct ui_out *) const override;
318 void print_mention () const override;
319 void print_recreate (struct ui_file *fp) const override;
320 };
321
322 /* Static tracepoints with marker (`-m'). */
323 struct static_marker_tracepoint : public tracepoint
324 {
325 using tracepoint::tracepoint;
326
327 std::vector<symtab_and_line> decode_location
328 (struct event_location *location,
329 struct program_space *search_pspace) override;
330 };
331
332 /* The style in which to perform a dynamic printf. This is a user
333 option because different output options have different tradeoffs;
334 if GDB does the printing, there is better error handling if there
335 is a problem with any of the arguments, but using an inferior
336 function lets you have special-purpose printers and sending of
337 output to the same place as compiled-in print functions. */
338
339 static const char dprintf_style_gdb[] = "gdb";
340 static const char dprintf_style_call[] = "call";
341 static const char dprintf_style_agent[] = "agent";
342 static const char *const dprintf_style_enums[] = {
343 dprintf_style_gdb,
344 dprintf_style_call,
345 dprintf_style_agent,
346 NULL
347 };
348 static const char *dprintf_style = dprintf_style_gdb;
349
350 /* The function to use for dynamic printf if the preferred style is to
351 call into the inferior. The value is simply a string that is
352 copied into the command, so it can be anything that GDB can
353 evaluate to a callable address, not necessarily a function name. */
354
355 static std::string dprintf_function = "printf";
356
357 /* The channel to use for dynamic printf if the preferred style is to
358 call into the inferior; if a nonempty string, it will be passed to
359 the call as the first argument, with the format string as the
360 second. As with the dprintf function, this can be anything that
361 GDB knows how to evaluate, so in addition to common choices like
362 "stderr", this could be an app-specific expression like
363 "mystreams[curlogger]". */
364
365 static std::string dprintf_channel;
366
367 /* True if dprintf commands should continue to operate even if GDB
368 has disconnected. */
369 static bool disconnected_dprintf = true;
370
371 struct command_line *
372 breakpoint_commands (struct breakpoint *b)
373 {
374 return b->commands ? b->commands.get () : NULL;
375 }
376
377 /* Flag indicating that a command has proceeded the inferior past the
378 current breakpoint. */
379
380 static bool breakpoint_proceeded;
381
382 const char *
383 bpdisp_text (enum bpdisp disp)
384 {
385 /* NOTE: the following values are a part of MI protocol and
386 represent values of 'disp' field returned when inferior stops at
387 a breakpoint. */
388 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
389
390 return bpdisps[(int) disp];
391 }
392
393 /* Prototypes for exported functions. */
394 /* If FALSE, gdb will not use hardware support for watchpoints, even
395 if such is available. */
396 static int can_use_hw_watchpoints;
397
398 static void
399 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
400 struct cmd_list_element *c,
401 const char *value)
402 {
403 gdb_printf (file,
404 _("Debugger's willingness to use "
405 "watchpoint hardware is %s.\n"),
406 value);
407 }
408
409 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
410 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
411 for unrecognized breakpoint locations.
412 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
413 static enum auto_boolean pending_break_support;
414 static void
415 show_pending_break_support (struct ui_file *file, int from_tty,
416 struct cmd_list_element *c,
417 const char *value)
418 {
419 gdb_printf (file,
420 _("Debugger's behavior regarding "
421 "pending breakpoints is %s.\n"),
422 value);
423 }
424
425 /* If true, gdb will automatically use hardware breakpoints for breakpoints
426 set with "break" but falling in read-only memory.
427 If false, gdb will warn about such breakpoints, but won't automatically
428 use hardware breakpoints. */
429 static bool automatic_hardware_breakpoints;
430 static void
431 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
432 struct cmd_list_element *c,
433 const char *value)
434 {
435 gdb_printf (file,
436 _("Automatic usage of hardware breakpoints is %s.\n"),
437 value);
438 }
439
440 /* If on, GDB keeps breakpoints inserted even if the inferior is
441 stopped, and immediately inserts any new breakpoints as soon as
442 they're created. If off (default), GDB keeps breakpoints off of
443 the target as long as possible. That is, it delays inserting
444 breakpoints until the next resume, and removes them again when the
445 target fully stops. This is a bit safer in case GDB crashes while
446 processing user input. */
447 static bool always_inserted_mode = false;
448
449 static void
450 show_always_inserted_mode (struct ui_file *file, int from_tty,
451 struct cmd_list_element *c, const char *value)
452 {
453 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
454 value);
455 }
456
457 /* See breakpoint.h. */
458
459 int
460 breakpoints_should_be_inserted_now (void)
461 {
462 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
463 {
464 /* If breakpoints are global, they should be inserted even if no
465 thread under gdb's control is running, or even if there are
466 no threads under GDB's control yet. */
467 return 1;
468 }
469 else
470 {
471 if (always_inserted_mode)
472 {
473 /* The user wants breakpoints inserted even if all threads
474 are stopped. */
475 return 1;
476 }
477
478 for (inferior *inf : all_inferiors ())
479 if (inf->has_execution ()
480 && threads_are_executing (inf->process_target ()))
481 return 1;
482
483 /* Don't remove breakpoints yet if, even though all threads are
484 stopped, we still have events to process. */
485 for (thread_info *tp : all_non_exited_threads ())
486 if (tp->resumed () && tp->has_pending_waitstatus ())
487 return 1;
488 }
489 return 0;
490 }
491
492 static const char condition_evaluation_both[] = "host or target";
493
494 /* Modes for breakpoint condition evaluation. */
495 static const char condition_evaluation_auto[] = "auto";
496 static const char condition_evaluation_host[] = "host";
497 static const char condition_evaluation_target[] = "target";
498 static const char *const condition_evaluation_enums[] = {
499 condition_evaluation_auto,
500 condition_evaluation_host,
501 condition_evaluation_target,
502 NULL
503 };
504
505 /* Global that holds the current mode for breakpoint condition evaluation. */
506 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
507
508 /* Global that we use to display information to the user (gets its value from
509 condition_evaluation_mode_1. */
510 static const char *condition_evaluation_mode = condition_evaluation_auto;
511
512 /* Translate a condition evaluation mode MODE into either "host"
513 or "target". This is used mostly to translate from "auto" to the
514 real setting that is being used. It returns the translated
515 evaluation mode. */
516
517 static const char *
518 translate_condition_evaluation_mode (const char *mode)
519 {
520 if (mode == condition_evaluation_auto)
521 {
522 if (target_supports_evaluation_of_breakpoint_conditions ())
523 return condition_evaluation_target;
524 else
525 return condition_evaluation_host;
526 }
527 else
528 return mode;
529 }
530
531 /* Discovers what condition_evaluation_auto translates to. */
532
533 static const char *
534 breakpoint_condition_evaluation_mode (void)
535 {
536 return translate_condition_evaluation_mode (condition_evaluation_mode);
537 }
538
539 /* Return true if GDB should evaluate breakpoint conditions or false
540 otherwise. */
541
542 static int
543 gdb_evaluates_breakpoint_condition_p (void)
544 {
545 const char *mode = breakpoint_condition_evaluation_mode ();
546
547 return (mode == condition_evaluation_host);
548 }
549
550 /* Are we executing breakpoint commands? */
551 static int executing_breakpoint_commands;
552
553 /* Are overlay event breakpoints enabled? */
554 static int overlay_events_enabled;
555
556 /* See description in breakpoint.h. */
557 bool target_exact_watchpoints = false;
558
559 /* Walk the following statement or block through all breakpoints.
560 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
561 current breakpoint. */
562
563 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
564 for (B = breakpoint_chain; \
565 B ? (TMP=B->next, 1): 0; \
566 B = TMP)
567
568 /* Chains of all breakpoints defined. */
569
570 static struct breakpoint *breakpoint_chain;
571
572 /* See breakpoint.h. */
573
574 breakpoint_range
575 all_breakpoints ()
576 {
577 return breakpoint_range (breakpoint_chain);
578 }
579
580 /* See breakpoint.h. */
581
582 breakpoint_safe_range
583 all_breakpoints_safe ()
584 {
585 return breakpoint_safe_range (all_breakpoints ());
586 }
587
588 /* See breakpoint.h. */
589
590 tracepoint_range
591 all_tracepoints ()
592 {
593 return tracepoint_range (breakpoint_chain);
594 }
595
596 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
597
598 static std::vector<bp_location *> bp_locations;
599
600 /* See breakpoint.h. */
601
602 const std::vector<bp_location *> &
603 all_bp_locations ()
604 {
605 return bp_locations;
606 }
607
608 /* Range to iterate over breakpoint locations at a given address. */
609
610 struct bp_locations_at_addr_range
611 {
612 using iterator = std::vector<bp_location *>::iterator;
613
614 bp_locations_at_addr_range (CORE_ADDR addr)
615 {
616 struct compare
617 {
618 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
619 { return loc->address < addr_; }
620
621 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
622 { return addr_ < loc->address; }
623 };
624
625 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
626 addr, compare ());
627
628 m_begin = it_pair.first;
629 m_end = it_pair.second;
630 }
631
632 iterator begin () const
633 { return m_begin; }
634
635 iterator end () const
636 { return m_end; }
637
638 private:
639 iterator m_begin;
640 iterator m_end;
641 };
642
643 /* Return a range to iterate over all breakpoint locations exactly at address
644 ADDR.
645
646 If it's needed to iterate multiple times on the same range, it's possible
647 to save the range in a local variable and use it multiple times:
648
649 auto range = all_bp_locations_at_addr (addr);
650
651 for (bp_location *loc : range)
652 // use loc
653
654 for (bp_location *loc : range)
655 // use loc
656
657 This saves a bit of time, as it avoids re-doing the binary searches to find
658 the range's boundaries. Just remember not to change the bp_locations vector
659 in the mean time, as it could make the range's iterators stale. */
660
661 static bp_locations_at_addr_range
662 all_bp_locations_at_addr (CORE_ADDR addr)
663 {
664 return bp_locations_at_addr_range (addr);
665 }
666
667 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
668 ADDRESS for the current elements of BP_LOCATIONS which get a valid
669 result from bp_location_has_shadow. You can use it for roughly
670 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
671 an address you need to read. */
672
673 static CORE_ADDR bp_locations_placed_address_before_address_max;
674
675 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
676 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
677 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
678 You can use it for roughly limiting the subrange of BP_LOCATIONS to
679 scan for shadow bytes for an address you need to read. */
680
681 static CORE_ADDR bp_locations_shadow_len_after_address_max;
682
683 /* The locations that no longer correspond to any breakpoint, unlinked
684 from the bp_locations array, but for which a hit may still be
685 reported by a target. */
686 static std::vector<bp_location *> moribund_locations;
687
688 /* Number of last breakpoint made. */
689
690 static int breakpoint_count;
691
692 /* The value of `breakpoint_count' before the last command that
693 created breakpoints. If the last (break-like) command created more
694 than one breakpoint, then the difference between BREAKPOINT_COUNT
695 and PREV_BREAKPOINT_COUNT is more than one. */
696 static int prev_breakpoint_count;
697
698 /* Number of last tracepoint made. */
699
700 static int tracepoint_count;
701
702 static struct cmd_list_element *breakpoint_set_cmdlist;
703 static struct cmd_list_element *breakpoint_show_cmdlist;
704 struct cmd_list_element *save_cmdlist;
705
706 /* Return whether a breakpoint is an active enabled breakpoint. */
707 static int
708 breakpoint_enabled (struct breakpoint *b)
709 {
710 return (b->enable_state == bp_enabled);
711 }
712
713 /* Set breakpoint count to NUM. */
714
715 static void
716 set_breakpoint_count (int num)
717 {
718 prev_breakpoint_count = breakpoint_count;
719 breakpoint_count = num;
720 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
721 }
722
723 /* Used by `start_rbreak_breakpoints' below, to record the current
724 breakpoint count before "rbreak" creates any breakpoint. */
725 static int rbreak_start_breakpoint_count;
726
727 /* Called at the start an "rbreak" command to record the first
728 breakpoint made. */
729
730 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
731 {
732 rbreak_start_breakpoint_count = breakpoint_count;
733 }
734
735 /* Called at the end of an "rbreak" command to record the last
736 breakpoint made. */
737
738 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
739 {
740 prev_breakpoint_count = rbreak_start_breakpoint_count;
741 }
742
743 /* Used in run_command to zero the hit count when a new run starts. */
744
745 void
746 clear_breakpoint_hit_counts (void)
747 {
748 for (breakpoint *b : all_breakpoints ())
749 b->hit_count = 0;
750 }
751
752 \f
753 /* Return the breakpoint with the specified number, or NULL
754 if the number does not refer to an existing breakpoint. */
755
756 struct breakpoint *
757 get_breakpoint (int num)
758 {
759 for (breakpoint *b : all_breakpoints ())
760 if (b->number == num)
761 return b;
762
763 return nullptr;
764 }
765
766 \f
767
768 /* Mark locations as "conditions have changed" in case the target supports
769 evaluating conditions on its side. */
770
771 static void
772 mark_breakpoint_modified (struct breakpoint *b)
773 {
774 /* This is only meaningful if the target is
775 evaluating conditions and if the user has
776 opted for condition evaluation on the target's
777 side. */
778 if (gdb_evaluates_breakpoint_condition_p ()
779 || !target_supports_evaluation_of_breakpoint_conditions ())
780 return;
781
782 if (!is_breakpoint (b))
783 return;
784
785 for (bp_location *loc : b->locations ())
786 loc->condition_changed = condition_modified;
787 }
788
789 /* Mark location as "conditions have changed" in case the target supports
790 evaluating conditions on its side. */
791
792 static void
793 mark_breakpoint_location_modified (struct bp_location *loc)
794 {
795 /* This is only meaningful if the target is
796 evaluating conditions and if the user has
797 opted for condition evaluation on the target's
798 side. */
799 if (gdb_evaluates_breakpoint_condition_p ()
800 || !target_supports_evaluation_of_breakpoint_conditions ())
801
802 return;
803
804 if (!is_breakpoint (loc->owner))
805 return;
806
807 loc->condition_changed = condition_modified;
808 }
809
810 /* Sets the condition-evaluation mode using the static global
811 condition_evaluation_mode. */
812
813 static void
814 set_condition_evaluation_mode (const char *args, int from_tty,
815 struct cmd_list_element *c)
816 {
817 const char *old_mode, *new_mode;
818
819 if ((condition_evaluation_mode_1 == condition_evaluation_target)
820 && !target_supports_evaluation_of_breakpoint_conditions ())
821 {
822 condition_evaluation_mode_1 = condition_evaluation_mode;
823 warning (_("Target does not support breakpoint condition evaluation.\n"
824 "Using host evaluation mode instead."));
825 return;
826 }
827
828 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
829 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
830
831 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
832 settings was "auto". */
833 condition_evaluation_mode = condition_evaluation_mode_1;
834
835 /* Only update the mode if the user picked a different one. */
836 if (new_mode != old_mode)
837 {
838 /* If the user switched to a different evaluation mode, we
839 need to synch the changes with the target as follows:
840
841 "host" -> "target": Send all (valid) conditions to the target.
842 "target" -> "host": Remove all the conditions from the target.
843 */
844
845 if (new_mode == condition_evaluation_target)
846 {
847 /* Mark everything modified and synch conditions with the
848 target. */
849 for (bp_location *loc : all_bp_locations ())
850 mark_breakpoint_location_modified (loc);
851 }
852 else
853 {
854 /* Manually mark non-duplicate locations to synch conditions
855 with the target. We do this to remove all the conditions the
856 target knows about. */
857 for (bp_location *loc : all_bp_locations ())
858 if (is_breakpoint (loc->owner) && loc->inserted)
859 loc->needs_update = 1;
860 }
861
862 /* Do the update. */
863 update_global_location_list (UGLL_MAY_INSERT);
864 }
865
866 return;
867 }
868
869 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
870 what "auto" is translating to. */
871
872 static void
873 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
874 struct cmd_list_element *c, const char *value)
875 {
876 if (condition_evaluation_mode == condition_evaluation_auto)
877 gdb_printf (file,
878 _("Breakpoint condition evaluation "
879 "mode is %s (currently %s).\n"),
880 value,
881 breakpoint_condition_evaluation_mode ());
882 else
883 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
884 value);
885 }
886
887 /* Parse COND_STRING in the context of LOC and set as the condition
888 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
889 the number of LOC within its owner. In case of parsing error, mark
890 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
891
892 static void
893 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
894 int bp_num, int loc_num)
895 {
896 bool has_junk = false;
897 try
898 {
899 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
900 block_for_pc (loc->address), 0);
901 if (*cond_string != 0)
902 has_junk = true;
903 else
904 {
905 loc->cond = std::move (new_exp);
906 if (loc->disabled_by_cond && loc->enabled)
907 gdb_printf (_("Breakpoint %d's condition is now valid at "
908 "location %d, enabling.\n"),
909 bp_num, loc_num);
910
911 loc->disabled_by_cond = false;
912 }
913 }
914 catch (const gdb_exception_error &e)
915 {
916 if (loc->enabled)
917 {
918 /* Warn if a user-enabled location is now becoming disabled-by-cond.
919 BP_NUM is 0 if the breakpoint is being defined for the first
920 time using the "break ... if ..." command, and non-zero if
921 already defined. */
922 if (bp_num != 0)
923 warning (_("failed to validate condition at location %d.%d, "
924 "disabling:\n %s"), bp_num, loc_num, e.what ());
925 else
926 warning (_("failed to validate condition at location %d, "
927 "disabling:\n %s"), loc_num, e.what ());
928 }
929
930 loc->disabled_by_cond = true;
931 }
932
933 if (has_junk)
934 error (_("Garbage '%s' follows condition"), cond_string);
935 }
936
937 void
938 set_breakpoint_condition (struct breakpoint *b, const char *exp,
939 int from_tty, bool force)
940 {
941 if (*exp == 0)
942 {
943 b->cond_string.reset ();
944
945 if (is_watchpoint (b))
946 static_cast<watchpoint *> (b)->cond_exp.reset ();
947 else
948 {
949 int loc_num = 1;
950 for (bp_location *loc : b->locations ())
951 {
952 loc->cond.reset ();
953 if (loc->disabled_by_cond && loc->enabled)
954 gdb_printf (_("Breakpoint %d's condition is now valid at "
955 "location %d, enabling.\n"),
956 b->number, loc_num);
957 loc->disabled_by_cond = false;
958 loc_num++;
959
960 /* No need to free the condition agent expression
961 bytecode (if we have one). We will handle this
962 when we go through update_global_location_list. */
963 }
964 }
965
966 if (from_tty)
967 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
968 }
969 else
970 {
971 if (is_watchpoint (b))
972 {
973 innermost_block_tracker tracker;
974 const char *arg = exp;
975 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
976 if (*arg != 0)
977 error (_("Junk at end of expression"));
978 watchpoint *w = static_cast<watchpoint *> (b);
979 w->cond_exp = std::move (new_exp);
980 w->cond_exp_valid_block = tracker.block ();
981 }
982 else
983 {
984 /* Parse and set condition expressions. We make two passes.
985 In the first, we parse the condition string to see if it
986 is valid in at least one location. If so, the condition
987 would be accepted. So we go ahead and set the locations'
988 conditions. In case no valid case is found, we throw
989 the error and the condition string will be rejected.
990 This two-pass approach is taken to avoid setting the
991 state of locations in case of a reject. */
992 for (bp_location *loc : b->locations ())
993 {
994 try
995 {
996 const char *arg = exp;
997 parse_exp_1 (&arg, loc->address,
998 block_for_pc (loc->address), 0);
999 if (*arg != 0)
1000 error (_("Junk at end of expression"));
1001 break;
1002 }
1003 catch (const gdb_exception_error &e)
1004 {
1005 /* Condition string is invalid. If this happens to
1006 be the last loc, abandon (if not forced) or continue
1007 (if forced). */
1008 if (loc->next == nullptr && !force)
1009 throw;
1010 }
1011 }
1012
1013 /* If we reach here, the condition is valid at some locations. */
1014 int loc_num = 1;
1015 for (bp_location *loc : b->locations ())
1016 {
1017 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
1018 loc_num++;
1019 }
1020 }
1021
1022 /* We know that the new condition parsed successfully. The
1023 condition string of the breakpoint can be safely updated. */
1024 b->cond_string = make_unique_xstrdup (exp);
1025 b->condition_not_parsed = 0;
1026 }
1027 mark_breakpoint_modified (b);
1028
1029 gdb::observers::breakpoint_modified.notify (b);
1030 }
1031
1032 /* See breakpoint.h. */
1033
1034 void
1035 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1036 bool force)
1037 {
1038 for (breakpoint *b : all_breakpoints ())
1039 if (b->number == bpnum)
1040 {
1041 /* Check if this breakpoint has a "stop" method implemented in an
1042 extension language. This method and conditions entered into GDB
1043 from the CLI are mutually exclusive. */
1044 const struct extension_language_defn *extlang
1045 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1046
1047 if (extlang != NULL)
1048 {
1049 error (_("Only one stop condition allowed. There is currently"
1050 " a %s stop condition defined for this breakpoint."),
1051 ext_lang_capitalized_name (extlang));
1052 }
1053 set_breakpoint_condition (b, exp, from_tty, force);
1054
1055 if (is_breakpoint (b))
1056 update_global_location_list (UGLL_MAY_INSERT);
1057
1058 return;
1059 }
1060
1061 error (_("No breakpoint number %d."), bpnum);
1062 }
1063
1064 /* The options for the "condition" command. */
1065
1066 struct condition_command_opts
1067 {
1068 /* For "-force". */
1069 bool force_condition = false;
1070 };
1071
1072 static const gdb::option::option_def condition_command_option_defs[] = {
1073
1074 gdb::option::flag_option_def<condition_command_opts> {
1075 "force",
1076 [] (condition_command_opts *opts) { return &opts->force_condition; },
1077 N_("Set the condition even if it is invalid for all current locations."),
1078 },
1079
1080 };
1081
1082 /* Create an option_def_group for the "condition" options, with
1083 CC_OPTS as context. */
1084
1085 static inline gdb::option::option_def_group
1086 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1087 {
1088 return {{condition_command_option_defs}, cc_opts};
1089 }
1090
1091 /* Completion for the "condition" command. */
1092
1093 static void
1094 condition_completer (struct cmd_list_element *cmd,
1095 completion_tracker &tracker,
1096 const char *text, const char * /*word*/)
1097 {
1098 bool has_no_arguments = (*text == '\0');
1099 condition_command_opts cc_opts;
1100 const auto group = make_condition_command_options_def_group (&cc_opts);
1101 if (gdb::option::complete_options
1102 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1103 return;
1104
1105 text = skip_spaces (text);
1106 const char *space = skip_to_space (text);
1107 if (*space == '\0')
1108 {
1109 int len;
1110
1111 if (text[0] == '$')
1112 {
1113 tracker.advance_custom_word_point_by (1);
1114 /* We don't support completion of history indices. */
1115 if (!isdigit (text[1]))
1116 complete_internalvar (tracker, &text[1]);
1117 return;
1118 }
1119
1120 /* Suggest the "-force" flag if no arguments are given. If
1121 arguments were passed, they either already include the flag,
1122 or we are beyond the point of suggesting it because it's
1123 positionally the first argument. */
1124 if (has_no_arguments)
1125 gdb::option::complete_on_all_options (tracker, group);
1126
1127 /* We're completing the breakpoint number. */
1128 len = strlen (text);
1129
1130 for (breakpoint *b : all_breakpoints ())
1131 {
1132 char number[50];
1133
1134 xsnprintf (number, sizeof (number), "%d", b->number);
1135
1136 if (strncmp (number, text, len) == 0)
1137 tracker.add_completion (make_unique_xstrdup (number));
1138 }
1139
1140 return;
1141 }
1142
1143 /* We're completing the expression part. Skip the breakpoint num. */
1144 const char *exp_start = skip_spaces (space);
1145 tracker.advance_custom_word_point_by (exp_start - text);
1146 text = exp_start;
1147 const char *word = advance_to_expression_complete_word_point (tracker, text);
1148 expression_completer (cmd, tracker, text, word);
1149 }
1150
1151 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1152
1153 static void
1154 condition_command (const char *arg, int from_tty)
1155 {
1156 const char *p;
1157 int bnum;
1158
1159 if (arg == 0)
1160 error_no_arg (_("breakpoint number"));
1161
1162 p = arg;
1163
1164 /* Check if the "-force" flag was passed. */
1165 condition_command_opts cc_opts;
1166 const auto group = make_condition_command_options_def_group (&cc_opts);
1167 gdb::option::process_options
1168 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1169
1170 bnum = get_number (&p);
1171 if (bnum == 0)
1172 error (_("Bad breakpoint argument: '%s'"), arg);
1173
1174 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1175 }
1176
1177 /* Check that COMMAND do not contain commands that are suitable
1178 only for tracepoints and not suitable for ordinary breakpoints.
1179 Throw if any such commands is found. */
1180
1181 static void
1182 check_no_tracepoint_commands (struct command_line *commands)
1183 {
1184 struct command_line *c;
1185
1186 for (c = commands; c; c = c->next)
1187 {
1188 if (c->control_type == while_stepping_control)
1189 error (_("The 'while-stepping' command can "
1190 "only be used for tracepoints"));
1191
1192 check_no_tracepoint_commands (c->body_list_0.get ());
1193 check_no_tracepoint_commands (c->body_list_1.get ());
1194
1195 /* Not that command parsing removes leading whitespace and comment
1196 lines and also empty lines. So, we only need to check for
1197 command directly. */
1198 if (strstr (c->line, "collect ") == c->line)
1199 error (_("The 'collect' command can only be used for tracepoints"));
1200
1201 if (strstr (c->line, "teval ") == c->line)
1202 error (_("The 'teval' command can only be used for tracepoints"));
1203 }
1204 }
1205
1206 struct longjmp_breakpoint : public momentary_breakpoint
1207 {
1208 using momentary_breakpoint::momentary_breakpoint;
1209
1210 ~longjmp_breakpoint () override;
1211 };
1212
1213 /* Encapsulate tests for different types of tracepoints. */
1214
1215 static bool
1216 is_tracepoint_type (bptype type)
1217 {
1218 return (type == bp_tracepoint
1219 || type == bp_fast_tracepoint
1220 || type == bp_static_tracepoint
1221 || type == bp_static_marker_tracepoint);
1222 }
1223
1224 /* See breakpoint.h. */
1225
1226 bool
1227 is_tracepoint (const struct breakpoint *b)
1228 {
1229 return is_tracepoint_type (b->type);
1230 }
1231
1232 /* Factory function to create an appropriate instance of breakpoint given
1233 TYPE. */
1234
1235 static std::unique_ptr<breakpoint>
1236 new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type)
1237 {
1238 breakpoint *b;
1239
1240 switch (type)
1241 {
1242 case bp_breakpoint:
1243 case bp_hardware_breakpoint:
1244 b = new ordinary_breakpoint (gdbarch, type);
1245 break;
1246
1247 case bp_fast_tracepoint:
1248 case bp_static_tracepoint:
1249 case bp_tracepoint:
1250 b = new tracepoint (gdbarch, type);
1251 break;
1252
1253 case bp_static_marker_tracepoint:
1254 b = new static_marker_tracepoint (gdbarch, type);
1255 break;
1256
1257 case bp_dprintf:
1258 b = new dprintf_breakpoint (gdbarch, type);
1259 break;
1260
1261 case bp_overlay_event:
1262 case bp_longjmp_master:
1263 case bp_std_terminate_master:
1264 case bp_exception_master:
1265 case bp_thread_event:
1266 case bp_jit_event:
1267 case bp_shlib_event:
1268 b = new internal_breakpoint (gdbarch, type);
1269 break;
1270
1271 case bp_longjmp:
1272 case bp_exception:
1273 b = new longjmp_breakpoint (gdbarch, type);
1274 break;
1275
1276 case bp_watchpoint_scope:
1277 case bp_finish:
1278 case bp_gnu_ifunc_resolver_return:
1279 case bp_step_resume:
1280 case bp_hp_step_resume:
1281 case bp_longjmp_resume:
1282 case bp_longjmp_call_dummy:
1283 case bp_exception_resume:
1284 case bp_call_dummy:
1285 case bp_until:
1286 case bp_std_terminate:
1287 b = new momentary_breakpoint (gdbarch, type);
1288 break;
1289
1290 default:
1291 gdb_assert_not_reached ("invalid type");
1292 }
1293
1294 return std::unique_ptr<breakpoint> (b);
1295 }
1296
1297 /* A helper function that validates that COMMANDS are valid for a
1298 breakpoint. This function will throw an exception if a problem is
1299 found. */
1300
1301 static void
1302 validate_commands_for_breakpoint (struct breakpoint *b,
1303 struct command_line *commands)
1304 {
1305 if (is_tracepoint (b))
1306 {
1307 struct tracepoint *t = (struct tracepoint *) b;
1308 struct command_line *c;
1309 struct command_line *while_stepping = 0;
1310
1311 /* Reset the while-stepping step count. The previous commands
1312 might have included a while-stepping action, while the new
1313 ones might not. */
1314 t->step_count = 0;
1315
1316 /* We need to verify that each top-level element of commands is
1317 valid for tracepoints, that there's at most one
1318 while-stepping element, and that the while-stepping's body
1319 has valid tracing commands excluding nested while-stepping.
1320 We also need to validate the tracepoint action line in the
1321 context of the tracepoint --- validate_actionline actually
1322 has side effects, like setting the tracepoint's
1323 while-stepping STEP_COUNT, in addition to checking if the
1324 collect/teval actions parse and make sense in the
1325 tracepoint's context. */
1326 for (c = commands; c; c = c->next)
1327 {
1328 if (c->control_type == while_stepping_control)
1329 {
1330 if (b->type == bp_fast_tracepoint)
1331 error (_("The 'while-stepping' command "
1332 "cannot be used for fast tracepoint"));
1333 else if (b->type == bp_static_tracepoint
1334 || b->type == bp_static_marker_tracepoint)
1335 error (_("The 'while-stepping' command "
1336 "cannot be used for static tracepoint"));
1337
1338 if (while_stepping)
1339 error (_("The 'while-stepping' command "
1340 "can be used only once"));
1341 else
1342 while_stepping = c;
1343 }
1344
1345 validate_actionline (c->line, b);
1346 }
1347 if (while_stepping)
1348 {
1349 struct command_line *c2;
1350
1351 gdb_assert (while_stepping->body_list_1 == nullptr);
1352 c2 = while_stepping->body_list_0.get ();
1353 for (; c2; c2 = c2->next)
1354 {
1355 if (c2->control_type == while_stepping_control)
1356 error (_("The 'while-stepping' command cannot be nested"));
1357 }
1358 }
1359 }
1360 else
1361 {
1362 check_no_tracepoint_commands (commands);
1363 }
1364 }
1365
1366 /* Return a vector of all the static tracepoints set at ADDR. The
1367 caller is responsible for releasing the vector. */
1368
1369 std::vector<breakpoint *>
1370 static_tracepoints_here (CORE_ADDR addr)
1371 {
1372 std::vector<breakpoint *> found;
1373
1374 for (breakpoint *b : all_breakpoints ())
1375 if (b->type == bp_static_tracepoint
1376 || b->type == bp_static_marker_tracepoint)
1377 {
1378 for (bp_location *loc : b->locations ())
1379 if (loc->address == addr)
1380 found.push_back (b);
1381 }
1382
1383 return found;
1384 }
1385
1386 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1387 validate that only allowed commands are included. */
1388
1389 void
1390 breakpoint_set_commands (struct breakpoint *b,
1391 counted_command_line &&commands)
1392 {
1393 validate_commands_for_breakpoint (b, commands.get ());
1394
1395 b->commands = std::move (commands);
1396 gdb::observers::breakpoint_modified.notify (b);
1397 }
1398
1399 /* Set the internal `silent' flag on the breakpoint. Note that this
1400 is not the same as the "silent" that may appear in the breakpoint's
1401 commands. */
1402
1403 void
1404 breakpoint_set_silent (struct breakpoint *b, int silent)
1405 {
1406 int old_silent = b->silent;
1407
1408 b->silent = silent;
1409 if (old_silent != silent)
1410 gdb::observers::breakpoint_modified.notify (b);
1411 }
1412
1413 /* Set the thread for this breakpoint. If THREAD is -1, make the
1414 breakpoint work for any thread. */
1415
1416 void
1417 breakpoint_set_thread (struct breakpoint *b, int thread)
1418 {
1419 int old_thread = b->thread;
1420
1421 b->thread = thread;
1422 if (old_thread != thread)
1423 gdb::observers::breakpoint_modified.notify (b);
1424 }
1425
1426 /* Set the task for this breakpoint. If TASK is 0, make the
1427 breakpoint work for any task. */
1428
1429 void
1430 breakpoint_set_task (struct breakpoint *b, int task)
1431 {
1432 int old_task = b->task;
1433
1434 b->task = task;
1435 if (old_task != task)
1436 gdb::observers::breakpoint_modified.notify (b);
1437 }
1438
1439 static void
1440 commands_command_1 (const char *arg, int from_tty,
1441 struct command_line *control)
1442 {
1443 counted_command_line cmd;
1444 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1445 NULL after the call to read_command_lines if the user provides an empty
1446 list of command by just typing "end". */
1447 bool cmd_read = false;
1448
1449 std::string new_arg;
1450
1451 if (arg == NULL || !*arg)
1452 {
1453 /* Argument not explicitly given. Synthesize it. */
1454 if (breakpoint_count - prev_breakpoint_count > 1)
1455 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1456 breakpoint_count);
1457 else if (breakpoint_count > 0)
1458 new_arg = string_printf ("%d", breakpoint_count);
1459 }
1460 else
1461 {
1462 /* Create a copy of ARG. This is needed because the "commands"
1463 command may be coming from a script. In that case, the read
1464 line buffer is going to be overwritten in the lambda of
1465 'map_breakpoint_numbers' below when reading the next line
1466 before we are are done parsing the breakpoint numbers. */
1467 new_arg = arg;
1468 }
1469 arg = new_arg.c_str ();
1470
1471 map_breakpoint_numbers
1472 (arg, [&] (breakpoint *b)
1473 {
1474 if (!cmd_read)
1475 {
1476 gdb_assert (cmd == NULL);
1477 if (control != NULL)
1478 cmd = control->body_list_0;
1479 else
1480 {
1481 std::string str
1482 = string_printf (_("Type commands for breakpoint(s) "
1483 "%s, one per line."),
1484 arg);
1485
1486 auto do_validate = [=] (const char *line)
1487 {
1488 validate_actionline (line, b);
1489 };
1490 gdb::function_view<void (const char *)> validator;
1491 if (is_tracepoint (b))
1492 validator = do_validate;
1493
1494 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1495 }
1496 cmd_read = true;
1497 }
1498
1499 /* If a breakpoint was on the list more than once, we don't need to
1500 do anything. */
1501 if (b->commands != cmd)
1502 {
1503 validate_commands_for_breakpoint (b, cmd.get ());
1504 b->commands = cmd;
1505 gdb::observers::breakpoint_modified.notify (b);
1506 }
1507 });
1508 }
1509
1510 static void
1511 commands_command (const char *arg, int from_tty)
1512 {
1513 commands_command_1 (arg, from_tty, NULL);
1514 }
1515
1516 /* Like commands_command, but instead of reading the commands from
1517 input stream, takes them from an already parsed command structure.
1518
1519 This is used by cli-script.c to DTRT with breakpoint commands
1520 that are part of if and while bodies. */
1521 enum command_control_type
1522 commands_from_control_command (const char *arg, struct command_line *cmd)
1523 {
1524 commands_command_1 (arg, 0, cmd);
1525 return simple_control;
1526 }
1527
1528 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1529
1530 static int
1531 bp_location_has_shadow (struct bp_location *bl)
1532 {
1533 if (bl->loc_type != bp_loc_software_breakpoint)
1534 return 0;
1535 if (!bl->inserted)
1536 return 0;
1537 if (bl->target_info.shadow_len == 0)
1538 /* BL isn't valid, or doesn't shadow memory. */
1539 return 0;
1540 return 1;
1541 }
1542
1543 /* Update BUF, which is LEN bytes read from the target address
1544 MEMADDR, by replacing a memory breakpoint with its shadowed
1545 contents.
1546
1547 If READBUF is not NULL, this buffer must not overlap with the of
1548 the breakpoint location's shadow_contents buffer. Otherwise, a
1549 failed assertion internal error will be raised. */
1550
1551 static void
1552 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1553 const gdb_byte *writebuf_org,
1554 ULONGEST memaddr, LONGEST len,
1555 struct bp_target_info *target_info,
1556 struct gdbarch *gdbarch)
1557 {
1558 /* Now do full processing of the found relevant range of elements. */
1559 CORE_ADDR bp_addr = 0;
1560 int bp_size = 0;
1561 int bptoffset = 0;
1562
1563 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1564 current_program_space->aspace, 0))
1565 {
1566 /* The breakpoint is inserted in a different address space. */
1567 return;
1568 }
1569
1570 /* Addresses and length of the part of the breakpoint that
1571 we need to copy. */
1572 bp_addr = target_info->placed_address;
1573 bp_size = target_info->shadow_len;
1574
1575 if (bp_addr + bp_size <= memaddr)
1576 {
1577 /* The breakpoint is entirely before the chunk of memory we are
1578 reading. */
1579 return;
1580 }
1581
1582 if (bp_addr >= memaddr + len)
1583 {
1584 /* The breakpoint is entirely after the chunk of memory we are
1585 reading. */
1586 return;
1587 }
1588
1589 /* Offset within shadow_contents. */
1590 if (bp_addr < memaddr)
1591 {
1592 /* Only copy the second part of the breakpoint. */
1593 bp_size -= memaddr - bp_addr;
1594 bptoffset = memaddr - bp_addr;
1595 bp_addr = memaddr;
1596 }
1597
1598 if (bp_addr + bp_size > memaddr + len)
1599 {
1600 /* Only copy the first part of the breakpoint. */
1601 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1602 }
1603
1604 if (readbuf != NULL)
1605 {
1606 /* Verify that the readbuf buffer does not overlap with the
1607 shadow_contents buffer. */
1608 gdb_assert (target_info->shadow_contents >= readbuf + len
1609 || readbuf >= (target_info->shadow_contents
1610 + target_info->shadow_len));
1611
1612 /* Update the read buffer with this inserted breakpoint's
1613 shadow. */
1614 memcpy (readbuf + bp_addr - memaddr,
1615 target_info->shadow_contents + bptoffset, bp_size);
1616 }
1617 else
1618 {
1619 const unsigned char *bp;
1620 CORE_ADDR addr = target_info->reqstd_address;
1621 int placed_size;
1622
1623 /* Update the shadow with what we want to write to memory. */
1624 memcpy (target_info->shadow_contents + bptoffset,
1625 writebuf_org + bp_addr - memaddr, bp_size);
1626
1627 /* Determine appropriate breakpoint contents and size for this
1628 address. */
1629 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1630
1631 /* Update the final write buffer with this inserted
1632 breakpoint's INSN. */
1633 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1634 }
1635 }
1636
1637 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1638 by replacing any memory breakpoints with their shadowed contents.
1639
1640 If READBUF is not NULL, this buffer must not overlap with any of
1641 the breakpoint location's shadow_contents buffers. Otherwise,
1642 a failed assertion internal error will be raised.
1643
1644 The range of shadowed area by each bp_location is:
1645 bl->address - bp_locations_placed_address_before_address_max
1646 up to bl->address + bp_locations_shadow_len_after_address_max
1647 The range we were requested to resolve shadows for is:
1648 memaddr ... memaddr + len
1649 Thus the safe cutoff boundaries for performance optimization are
1650 memaddr + len <= (bl->address
1651 - bp_locations_placed_address_before_address_max)
1652 and:
1653 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1654
1655 void
1656 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1657 const gdb_byte *writebuf_org,
1658 ULONGEST memaddr, LONGEST len)
1659 {
1660 /* Left boundary, right boundary and median element of our binary
1661 search. */
1662 unsigned bc_l, bc_r, bc;
1663
1664 /* Find BC_L which is a leftmost element which may affect BUF
1665 content. It is safe to report lower value but a failure to
1666 report higher one. */
1667
1668 bc_l = 0;
1669 bc_r = bp_locations.size ();
1670 while (bc_l + 1 < bc_r)
1671 {
1672 struct bp_location *bl;
1673
1674 bc = (bc_l + bc_r) / 2;
1675 bl = bp_locations[bc];
1676
1677 /* Check first BL->ADDRESS will not overflow due to the added
1678 constant. Then advance the left boundary only if we are sure
1679 the BC element can in no way affect the BUF content (MEMADDR
1680 to MEMADDR + LEN range).
1681
1682 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1683 offset so that we cannot miss a breakpoint with its shadow
1684 range tail still reaching MEMADDR. */
1685
1686 if ((bl->address + bp_locations_shadow_len_after_address_max
1687 >= bl->address)
1688 && (bl->address + bp_locations_shadow_len_after_address_max
1689 <= memaddr))
1690 bc_l = bc;
1691 else
1692 bc_r = bc;
1693 }
1694
1695 /* Due to the binary search above, we need to make sure we pick the
1696 first location that's at BC_L's address. E.g., if there are
1697 multiple locations at the same address, BC_L may end up pointing
1698 at a duplicate location, and miss the "master"/"inserted"
1699 location. Say, given locations L1, L2 and L3 at addresses A and
1700 B:
1701
1702 L1@A, L2@A, L3@B, ...
1703
1704 BC_L could end up pointing at location L2, while the "master"
1705 location could be L1. Since the `loc->inserted' flag is only set
1706 on "master" locations, we'd forget to restore the shadow of L1
1707 and L2. */
1708 while (bc_l > 0
1709 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1710 bc_l--;
1711
1712 /* Now do full processing of the found relevant range of elements. */
1713
1714 for (bc = bc_l; bc < bp_locations.size (); bc++)
1715 {
1716 struct bp_location *bl = bp_locations[bc];
1717
1718 /* bp_location array has BL->OWNER always non-NULL. */
1719 if (bl->owner->type == bp_none)
1720 warning (_("reading through apparently deleted breakpoint #%d?"),
1721 bl->owner->number);
1722
1723 /* Performance optimization: any further element can no longer affect BUF
1724 content. */
1725
1726 if (bl->address >= bp_locations_placed_address_before_address_max
1727 && (memaddr + len
1728 <= (bl->address
1729 - bp_locations_placed_address_before_address_max)))
1730 break;
1731
1732 if (!bp_location_has_shadow (bl))
1733 continue;
1734
1735 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1736 memaddr, len, &bl->target_info, bl->gdbarch);
1737 }
1738 }
1739
1740 /* See breakpoint.h. */
1741
1742 bool
1743 is_breakpoint (const struct breakpoint *bpt)
1744 {
1745 return (bpt->type == bp_breakpoint
1746 || bpt->type == bp_hardware_breakpoint
1747 || bpt->type == bp_dprintf);
1748 }
1749
1750 /* Return true if BPT is of any hardware watchpoint kind. */
1751
1752 static bool
1753 is_hardware_watchpoint (const struct breakpoint *bpt)
1754 {
1755 return (bpt->type == bp_hardware_watchpoint
1756 || bpt->type == bp_read_watchpoint
1757 || bpt->type == bp_access_watchpoint);
1758 }
1759
1760 /* See breakpoint.h. */
1761
1762 bool
1763 is_watchpoint (const struct breakpoint *bpt)
1764 {
1765 return (is_hardware_watchpoint (bpt)
1766 || bpt->type == bp_watchpoint);
1767 }
1768
1769 /* Returns true if the current thread and its running state are safe
1770 to evaluate or update watchpoint B. Watchpoints on local
1771 expressions need to be evaluated in the context of the thread that
1772 was current when the watchpoint was created, and, that thread needs
1773 to be stopped to be able to select the correct frame context.
1774 Watchpoints on global expressions can be evaluated on any thread,
1775 and in any state. It is presently left to the target allowing
1776 memory accesses when threads are running. */
1777
1778 static int
1779 watchpoint_in_thread_scope (struct watchpoint *b)
1780 {
1781 return (b->pspace == current_program_space
1782 && (b->watchpoint_thread == null_ptid
1783 || (inferior_ptid == b->watchpoint_thread
1784 && !inferior_thread ()->executing ())));
1785 }
1786
1787 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1788 associated bp_watchpoint_scope breakpoint. */
1789
1790 static void
1791 watchpoint_del_at_next_stop (struct watchpoint *w)
1792 {
1793 if (w->related_breakpoint != w)
1794 {
1795 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1796 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1797 w->related_breakpoint->disposition = disp_del_at_next_stop;
1798 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1799 w->related_breakpoint = w;
1800 }
1801 w->disposition = disp_del_at_next_stop;
1802 }
1803
1804 /* Extract a bitfield value from value VAL using the bit parameters contained in
1805 watchpoint W. */
1806
1807 static struct value *
1808 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1809 {
1810 struct value *bit_val;
1811
1812 if (val == NULL)
1813 return NULL;
1814
1815 bit_val = allocate_value (value_type (val));
1816
1817 unpack_value_bitfield (bit_val,
1818 w->val_bitpos,
1819 w->val_bitsize,
1820 value_contents_for_printing (val).data (),
1821 value_offset (val),
1822 val);
1823
1824 return bit_val;
1825 }
1826
1827 /* Allocate a dummy location and add it to B, which must be a software
1828 watchpoint. This is required because even if a software watchpoint
1829 is not watching any memory, bpstat_stop_status requires a location
1830 to be able to report stops. */
1831
1832 static void
1833 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1834 struct program_space *pspace)
1835 {
1836 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1837
1838 b->loc = b->allocate_location ();
1839 b->loc->pspace = pspace;
1840 b->loc->address = -1;
1841 b->loc->length = -1;
1842 }
1843
1844 /* Returns true if B is a software watchpoint that is not watching any
1845 memory (e.g., "watch $pc"). */
1846
1847 static bool
1848 is_no_memory_software_watchpoint (struct breakpoint *b)
1849 {
1850 return (b->type == bp_watchpoint
1851 && b->loc != NULL
1852 && b->loc->next == NULL
1853 && b->loc->address == -1
1854 && b->loc->length == -1);
1855 }
1856
1857 /* Assuming that B is a watchpoint:
1858 - Reparse watchpoint expression, if REPARSE is non-zero
1859 - Evaluate expression and store the result in B->val
1860 - Evaluate the condition if there is one, and store the result
1861 in b->loc->cond.
1862 - Update the list of values that must be watched in B->loc.
1863
1864 If the watchpoint disposition is disp_del_at_next_stop, then do
1865 nothing. If this is local watchpoint that is out of scope, delete
1866 it.
1867
1868 Even with `set breakpoint always-inserted on' the watchpoints are
1869 removed + inserted on each stop here. Normal breakpoints must
1870 never be removed because they might be missed by a running thread
1871 when debugging in non-stop mode. On the other hand, hardware
1872 watchpoints (is_hardware_watchpoint; processed here) are specific
1873 to each LWP since they are stored in each LWP's hardware debug
1874 registers. Therefore, such LWP must be stopped first in order to
1875 be able to modify its hardware watchpoints.
1876
1877 Hardware watchpoints must be reset exactly once after being
1878 presented to the user. It cannot be done sooner, because it would
1879 reset the data used to present the watchpoint hit to the user. And
1880 it must not be done later because it could display the same single
1881 watchpoint hit during multiple GDB stops. Note that the latter is
1882 relevant only to the hardware watchpoint types bp_read_watchpoint
1883 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1884 not user-visible - its hit is suppressed if the memory content has
1885 not changed.
1886
1887 The following constraints influence the location where we can reset
1888 hardware watchpoints:
1889
1890 * target_stopped_by_watchpoint and target_stopped_data_address are
1891 called several times when GDB stops.
1892
1893 [linux]
1894 * Multiple hardware watchpoints can be hit at the same time,
1895 causing GDB to stop. GDB only presents one hardware watchpoint
1896 hit at a time as the reason for stopping, and all the other hits
1897 are presented later, one after the other, each time the user
1898 requests the execution to be resumed. Execution is not resumed
1899 for the threads still having pending hit event stored in
1900 LWP_INFO->STATUS. While the watchpoint is already removed from
1901 the inferior on the first stop the thread hit event is kept being
1902 reported from its cached value by linux_nat_stopped_data_address
1903 until the real thread resume happens after the watchpoint gets
1904 presented and thus its LWP_INFO->STATUS gets reset.
1905
1906 Therefore the hardware watchpoint hit can get safely reset on the
1907 watchpoint removal from inferior. */
1908
1909 static void
1910 update_watchpoint (struct watchpoint *b, int reparse)
1911 {
1912 int within_current_scope;
1913 struct frame_id saved_frame_id;
1914 int frame_saved;
1915
1916 /* If this is a local watchpoint, we only want to check if the
1917 watchpoint frame is in scope if the current thread is the thread
1918 that was used to create the watchpoint. */
1919 if (!watchpoint_in_thread_scope (b))
1920 return;
1921
1922 if (b->disposition == disp_del_at_next_stop)
1923 return;
1924
1925 frame_saved = 0;
1926
1927 /* Determine if the watchpoint is within scope. */
1928 if (b->exp_valid_block == NULL)
1929 within_current_scope = 1;
1930 else
1931 {
1932 struct frame_info *fi = get_current_frame ();
1933 struct gdbarch *frame_arch = get_frame_arch (fi);
1934 CORE_ADDR frame_pc = get_frame_pc (fi);
1935
1936 /* If we're at a point where the stack has been destroyed
1937 (e.g. in a function epilogue), unwinding may not work
1938 properly. Do not attempt to recreate locations at this
1939 point. See similar comments in watchpoint_check. */
1940 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1941 return;
1942
1943 /* Save the current frame's ID so we can restore it after
1944 evaluating the watchpoint expression on its own frame. */
1945 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1946 took a frame parameter, so that we didn't have to change the
1947 selected frame. */
1948 frame_saved = 1;
1949 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1950
1951 fi = frame_find_by_id (b->watchpoint_frame);
1952 within_current_scope = (fi != NULL);
1953 if (within_current_scope)
1954 select_frame (fi);
1955 }
1956
1957 /* We don't free locations. They are stored in the bp_location array
1958 and update_global_location_list will eventually delete them and
1959 remove breakpoints if needed. */
1960 b->loc = NULL;
1961
1962 if (within_current_scope && reparse)
1963 {
1964 const char *s;
1965
1966 b->exp.reset ();
1967 s = (b->exp_string_reparse
1968 ? b->exp_string_reparse.get ()
1969 : b->exp_string.get ());
1970 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1971 /* If the meaning of expression itself changed, the old value is
1972 no longer relevant. We don't want to report a watchpoint hit
1973 to the user when the old value and the new value may actually
1974 be completely different objects. */
1975 b->val = NULL;
1976 b->val_valid = false;
1977
1978 /* Note that unlike with breakpoints, the watchpoint's condition
1979 expression is stored in the breakpoint object, not in the
1980 locations (re)created below. */
1981 if (b->cond_string != NULL)
1982 {
1983 b->cond_exp.reset ();
1984
1985 s = b->cond_string.get ();
1986 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1987 }
1988 }
1989
1990 /* If we failed to parse the expression, for example because
1991 it refers to a global variable in a not-yet-loaded shared library,
1992 don't try to insert watchpoint. We don't automatically delete
1993 such watchpoint, though, since failure to parse expression
1994 is different from out-of-scope watchpoint. */
1995 if (!target_has_execution ())
1996 {
1997 /* Without execution, memory can't change. No use to try and
1998 set watchpoint locations. The watchpoint will be reset when
1999 the target gains execution, through breakpoint_re_set. */
2000 if (!can_use_hw_watchpoints)
2001 {
2002 if (b->works_in_software_mode ())
2003 b->type = bp_watchpoint;
2004 else
2005 error (_("Can't set read/access watchpoint when "
2006 "hardware watchpoints are disabled."));
2007 }
2008 }
2009 else if (within_current_scope && b->exp)
2010 {
2011 std::vector<value_ref_ptr> val_chain;
2012 struct value *v, *result;
2013 struct program_space *frame_pspace;
2014
2015 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
2016 &val_chain, false);
2017
2018 /* Avoid setting b->val if it's already set. The meaning of
2019 b->val is 'the last value' user saw, and we should update
2020 it only if we reported that last value to user. As it
2021 happens, the code that reports it updates b->val directly.
2022 We don't keep track of the memory value for masked
2023 watchpoints. */
2024 if (!b->val_valid && !is_masked_watchpoint (b))
2025 {
2026 if (b->val_bitsize != 0)
2027 v = extract_bitfield_from_watchpoint_value (b, v);
2028 b->val = release_value (v);
2029 b->val_valid = true;
2030 }
2031
2032 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2033
2034 /* Look at each value on the value chain. */
2035 gdb_assert (!val_chain.empty ());
2036 for (const value_ref_ptr &iter : val_chain)
2037 {
2038 v = iter.get ();
2039
2040 /* If it's a memory location, and GDB actually needed
2041 its contents to evaluate the expression, then we
2042 must watch it. If the first value returned is
2043 still lazy, that means an error occurred reading it;
2044 watch it anyway in case it becomes readable. */
2045 if (VALUE_LVAL (v) == lval_memory
2046 && (v == val_chain[0] || ! value_lazy (v)))
2047 {
2048 struct type *vtype = check_typedef (value_type (v));
2049
2050 /* We only watch structs and arrays if user asked
2051 for it explicitly, never if they just happen to
2052 appear in the middle of some value chain. */
2053 if (v == result
2054 || (vtype->code () != TYPE_CODE_STRUCT
2055 && vtype->code () != TYPE_CODE_ARRAY))
2056 {
2057 CORE_ADDR addr;
2058 enum target_hw_bp_type type;
2059 struct bp_location *loc, **tmp;
2060 int bitpos = 0, bitsize = 0;
2061
2062 if (value_bitsize (v) != 0)
2063 {
2064 /* Extract the bit parameters out from the bitfield
2065 sub-expression. */
2066 bitpos = value_bitpos (v);
2067 bitsize = value_bitsize (v);
2068 }
2069 else if (v == result && b->val_bitsize != 0)
2070 {
2071 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2072 lvalue whose bit parameters are saved in the fields
2073 VAL_BITPOS and VAL_BITSIZE. */
2074 bitpos = b->val_bitpos;
2075 bitsize = b->val_bitsize;
2076 }
2077
2078 addr = value_address (v);
2079 if (bitsize != 0)
2080 {
2081 /* Skip the bytes that don't contain the bitfield. */
2082 addr += bitpos / 8;
2083 }
2084
2085 type = hw_write;
2086 if (b->type == bp_read_watchpoint)
2087 type = hw_read;
2088 else if (b->type == bp_access_watchpoint)
2089 type = hw_access;
2090
2091 loc = b->allocate_location ();
2092 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2093 ;
2094 *tmp = loc;
2095 loc->gdbarch = value_type (v)->arch ();
2096
2097 loc->pspace = frame_pspace;
2098 loc->address = address_significant (loc->gdbarch, addr);
2099
2100 if (bitsize != 0)
2101 {
2102 /* Just cover the bytes that make up the bitfield. */
2103 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2104 }
2105 else
2106 loc->length = TYPE_LENGTH (value_type (v));
2107
2108 loc->watchpoint_type = type;
2109 }
2110 }
2111 }
2112
2113 /* Change the type of breakpoint between hardware assisted or
2114 an ordinary watchpoint depending on the hardware support
2115 and free hardware slots. REPARSE is set when the inferior
2116 is started. */
2117 if (reparse)
2118 {
2119 int reg_cnt;
2120 enum bp_loc_type loc_type;
2121
2122 reg_cnt = can_use_hardware_watchpoint (val_chain);
2123
2124 if (reg_cnt)
2125 {
2126 int i, target_resources_ok, other_type_used;
2127 enum bptype type;
2128
2129 /* Use an exact watchpoint when there's only one memory region to be
2130 watched, and only one debug register is needed to watch it. */
2131 b->exact = target_exact_watchpoints && reg_cnt == 1;
2132
2133 /* We need to determine how many resources are already
2134 used for all other hardware watchpoints plus this one
2135 to see if we still have enough resources to also fit
2136 this watchpoint in as well. */
2137
2138 /* If this is a software watchpoint, we try to turn it
2139 to a hardware one -- count resources as if B was of
2140 hardware watchpoint type. */
2141 type = b->type;
2142 if (type == bp_watchpoint)
2143 type = bp_hardware_watchpoint;
2144
2145 /* This watchpoint may or may not have been placed on
2146 the list yet at this point (it won't be in the list
2147 if we're trying to create it for the first time,
2148 through watch_command), so always account for it
2149 manually. */
2150
2151 /* Count resources used by all watchpoints except B. */
2152 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2153
2154 /* Add in the resources needed for B. */
2155 i += hw_watchpoint_use_count (b);
2156
2157 target_resources_ok
2158 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2159 if (target_resources_ok <= 0)
2160 {
2161 bool sw_mode = b->works_in_software_mode ();
2162
2163 if (target_resources_ok == 0 && !sw_mode)
2164 error (_("Target does not support this type of "
2165 "hardware watchpoint."));
2166 else if (target_resources_ok < 0 && !sw_mode)
2167 error (_("There are not enough available hardware "
2168 "resources for this watchpoint."));
2169
2170 /* Downgrade to software watchpoint. */
2171 b->type = bp_watchpoint;
2172 }
2173 else
2174 {
2175 /* If this was a software watchpoint, we've just
2176 found we have enough resources to turn it to a
2177 hardware watchpoint. Otherwise, this is a
2178 nop. */
2179 b->type = type;
2180 }
2181 }
2182 else if (!b->works_in_software_mode ())
2183 {
2184 if (!can_use_hw_watchpoints)
2185 error (_("Can't set read/access watchpoint when "
2186 "hardware watchpoints are disabled."));
2187 else
2188 error (_("Expression cannot be implemented with "
2189 "read/access watchpoint."));
2190 }
2191 else
2192 b->type = bp_watchpoint;
2193
2194 loc_type = (b->type == bp_watchpoint? bp_loc_other
2195 : bp_loc_hardware_watchpoint);
2196 for (bp_location *bl : b->locations ())
2197 bl->loc_type = loc_type;
2198 }
2199
2200 /* If a software watchpoint is not watching any memory, then the
2201 above left it without any location set up. But,
2202 bpstat_stop_status requires a location to be able to report
2203 stops, so make sure there's at least a dummy one. */
2204 if (b->type == bp_watchpoint && b->loc == NULL)
2205 software_watchpoint_add_no_memory_location (b, frame_pspace);
2206 }
2207 else if (!within_current_scope)
2208 {
2209 gdb_printf (_("\
2210 Watchpoint %d deleted because the program has left the block\n\
2211 in which its expression is valid.\n"),
2212 b->number);
2213 watchpoint_del_at_next_stop (b);
2214 }
2215
2216 /* Restore the selected frame. */
2217 if (frame_saved)
2218 select_frame (frame_find_by_id (saved_frame_id));
2219 }
2220
2221
2222 /* Returns 1 iff breakpoint location should be
2223 inserted in the inferior. We don't differentiate the type of BL's owner
2224 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2225 breakpoint_ops is not defined, because in insert_bp_location,
2226 tracepoint's insert_location will not be called. */
2227 static int
2228 should_be_inserted (struct bp_location *bl)
2229 {
2230 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2231 return 0;
2232
2233 if (bl->owner->disposition == disp_del_at_next_stop)
2234 return 0;
2235
2236 if (!bl->enabled || bl->disabled_by_cond
2237 || bl->shlib_disabled || bl->duplicate)
2238 return 0;
2239
2240 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2241 return 0;
2242
2243 /* This is set for example, when we're attached to the parent of a
2244 vfork, and have detached from the child. The child is running
2245 free, and we expect it to do an exec or exit, at which point the
2246 OS makes the parent schedulable again (and the target reports
2247 that the vfork is done). Until the child is done with the shared
2248 memory region, do not insert breakpoints in the parent, otherwise
2249 the child could still trip on the parent's breakpoints. Since
2250 the parent is blocked anyway, it won't miss any breakpoint. */
2251 if (bl->pspace->breakpoints_not_allowed)
2252 return 0;
2253
2254 /* Don't insert a breakpoint if we're trying to step past its
2255 location, except if the breakpoint is a single-step breakpoint,
2256 and the breakpoint's thread is the thread which is stepping past
2257 a breakpoint. */
2258 if ((bl->loc_type == bp_loc_software_breakpoint
2259 || bl->loc_type == bp_loc_hardware_breakpoint)
2260 && stepping_past_instruction_at (bl->pspace->aspace,
2261 bl->address)
2262 /* The single-step breakpoint may be inserted at the location
2263 we're trying to step if the instruction branches to itself.
2264 However, the instruction won't be executed at all and it may
2265 break the semantics of the instruction, for example, the
2266 instruction is a conditional branch or updates some flags.
2267 We can't fix it unless GDB is able to emulate the instruction
2268 or switch to displaced stepping. */
2269 && !(bl->owner->type == bp_single_step
2270 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2271 {
2272 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2273 paddress (bl->gdbarch, bl->address));
2274 return 0;
2275 }
2276
2277 /* Don't insert watchpoints if we're trying to step past the
2278 instruction that triggered one. */
2279 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2280 && stepping_past_nonsteppable_watchpoint ())
2281 {
2282 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2283 "skipping watchpoint at %s:%d",
2284 paddress (bl->gdbarch, bl->address), bl->length);
2285 return 0;
2286 }
2287
2288 return 1;
2289 }
2290
2291 /* Same as should_be_inserted but does the check assuming
2292 that the location is not duplicated. */
2293
2294 static int
2295 unduplicated_should_be_inserted (struct bp_location *bl)
2296 {
2297 int result;
2298 const int save_duplicate = bl->duplicate;
2299
2300 bl->duplicate = 0;
2301 result = should_be_inserted (bl);
2302 bl->duplicate = save_duplicate;
2303 return result;
2304 }
2305
2306 /* Parses a conditional described by an expression COND into an
2307 agent expression bytecode suitable for evaluation
2308 by the bytecode interpreter. Return NULL if there was
2309 any error during parsing. */
2310
2311 static agent_expr_up
2312 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2313 {
2314 if (cond == NULL)
2315 return NULL;
2316
2317 agent_expr_up aexpr;
2318
2319 /* We don't want to stop processing, so catch any errors
2320 that may show up. */
2321 try
2322 {
2323 aexpr = gen_eval_for_expr (scope, cond);
2324 }
2325
2326 catch (const gdb_exception_error &ex)
2327 {
2328 /* If we got here, it means the condition could not be parsed to a valid
2329 bytecode expression and thus can't be evaluated on the target's side.
2330 It's no use iterating through the conditions. */
2331 }
2332
2333 /* We have a valid agent expression. */
2334 return aexpr;
2335 }
2336
2337 /* Based on location BL, create a list of breakpoint conditions to be
2338 passed on to the target. If we have duplicated locations with different
2339 conditions, we will add such conditions to the list. The idea is that the
2340 target will evaluate the list of conditions and will only notify GDB when
2341 one of them is true. */
2342
2343 static void
2344 build_target_condition_list (struct bp_location *bl)
2345 {
2346 int null_condition_or_parse_error = 0;
2347 int modified = bl->needs_update;
2348
2349 /* Release conditions left over from a previous insert. */
2350 bl->target_info.conditions.clear ();
2351
2352 /* This is only meaningful if the target is
2353 evaluating conditions and if the user has
2354 opted for condition evaluation on the target's
2355 side. */
2356 if (gdb_evaluates_breakpoint_condition_p ()
2357 || !target_supports_evaluation_of_breakpoint_conditions ())
2358 return;
2359
2360 auto loc_range = all_bp_locations_at_addr (bl->address);
2361
2362 /* Do a first pass to check for locations with no assigned
2363 conditions or conditions that fail to parse to a valid agent
2364 expression bytecode. If any of these happen, then it's no use to
2365 send conditions to the target since this location will always
2366 trigger and generate a response back to GDB. Note we consider
2367 all locations at the same address irrespective of type, i.e.,
2368 even if the locations aren't considered duplicates (e.g.,
2369 software breakpoint and hardware breakpoint at the same
2370 address). */
2371 for (bp_location *loc : loc_range)
2372 {
2373 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2374 {
2375 if (modified)
2376 {
2377 /* Re-parse the conditions since something changed. In that
2378 case we already freed the condition bytecodes (see
2379 force_breakpoint_reinsertion). We just
2380 need to parse the condition to bytecodes again. */
2381 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2382 loc->cond.get ());
2383 }
2384
2385 /* If we have a NULL bytecode expression, it means something
2386 went wrong or we have a null condition expression. */
2387 if (!loc->cond_bytecode)
2388 {
2389 null_condition_or_parse_error = 1;
2390 break;
2391 }
2392 }
2393 }
2394
2395 /* If any of these happened, it means we will have to evaluate the conditions
2396 for the location's address on gdb's side. It is no use keeping bytecodes
2397 for all the other duplicate locations, thus we free all of them here.
2398
2399 This is so we have a finer control over which locations' conditions are
2400 being evaluated by GDB or the remote stub. */
2401 if (null_condition_or_parse_error)
2402 {
2403 for (bp_location *loc : loc_range)
2404 {
2405 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2406 {
2407 /* Only go as far as the first NULL bytecode is
2408 located. */
2409 if (!loc->cond_bytecode)
2410 return;
2411
2412 loc->cond_bytecode.reset ();
2413 }
2414 }
2415 }
2416
2417 /* No NULL conditions or failed bytecode generation. Build a
2418 condition list for this location's address. If we have software
2419 and hardware locations at the same address, they aren't
2420 considered duplicates, but we still marge all the conditions
2421 anyway, as it's simpler, and doesn't really make a practical
2422 difference. */
2423 for (bp_location *loc : loc_range)
2424 if (loc->cond
2425 && is_breakpoint (loc->owner)
2426 && loc->pspace->num == bl->pspace->num
2427 && loc->owner->enable_state == bp_enabled
2428 && loc->enabled
2429 && !loc->disabled_by_cond)
2430 {
2431 /* Add the condition to the vector. This will be used later
2432 to send the conditions to the target. */
2433 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2434 }
2435
2436 return;
2437 }
2438
2439 /* Parses a command described by string CMD into an agent expression
2440 bytecode suitable for evaluation by the bytecode interpreter.
2441 Return NULL if there was any error during parsing. */
2442
2443 static agent_expr_up
2444 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2445 {
2446 const char *cmdrest;
2447 const char *format_start, *format_end;
2448 struct gdbarch *gdbarch = get_current_arch ();
2449
2450 if (cmd == NULL)
2451 return NULL;
2452
2453 cmdrest = cmd;
2454
2455 if (*cmdrest == ',')
2456 ++cmdrest;
2457 cmdrest = skip_spaces (cmdrest);
2458
2459 if (*cmdrest++ != '"')
2460 error (_("No format string following the location"));
2461
2462 format_start = cmdrest;
2463
2464 format_pieces fpieces (&cmdrest);
2465
2466 format_end = cmdrest;
2467
2468 if (*cmdrest++ != '"')
2469 error (_("Bad format string, non-terminated '\"'."));
2470
2471 cmdrest = skip_spaces (cmdrest);
2472
2473 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2474 error (_("Invalid argument syntax"));
2475
2476 if (*cmdrest == ',')
2477 cmdrest++;
2478 cmdrest = skip_spaces (cmdrest);
2479
2480 /* For each argument, make an expression. */
2481
2482 std::vector<struct expression *> argvec;
2483 while (*cmdrest != '\0')
2484 {
2485 const char *cmd1;
2486
2487 cmd1 = cmdrest;
2488 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2489 argvec.push_back (expr.release ());
2490 cmdrest = cmd1;
2491 if (*cmdrest == ',')
2492 ++cmdrest;
2493 }
2494
2495 agent_expr_up aexpr;
2496
2497 /* We don't want to stop processing, so catch any errors
2498 that may show up. */
2499 try
2500 {
2501 aexpr = gen_printf (scope, gdbarch, 0, 0,
2502 format_start, format_end - format_start,
2503 argvec.size (), argvec.data ());
2504 }
2505 catch (const gdb_exception_error &ex)
2506 {
2507 /* If we got here, it means the command could not be parsed to a valid
2508 bytecode expression and thus can't be evaluated on the target's side.
2509 It's no use iterating through the other commands. */
2510 }
2511
2512 /* We have a valid agent expression, return it. */
2513 return aexpr;
2514 }
2515
2516 /* Based on location BL, create a list of breakpoint commands to be
2517 passed on to the target. If we have duplicated locations with
2518 different commands, we will add any such to the list. */
2519
2520 static void
2521 build_target_command_list (struct bp_location *bl)
2522 {
2523 int null_command_or_parse_error = 0;
2524 int modified = bl->needs_update;
2525
2526 /* Clear commands left over from a previous insert. */
2527 bl->target_info.tcommands.clear ();
2528
2529 if (!target_can_run_breakpoint_commands ())
2530 return;
2531
2532 /* For now, limit to agent-style dprintf breakpoints. */
2533 if (dprintf_style != dprintf_style_agent)
2534 return;
2535
2536 auto loc_range = all_bp_locations_at_addr (bl->address);
2537
2538 /* For now, if we have any location at the same address that isn't a
2539 dprintf, don't install the target-side commands, as that would
2540 make the breakpoint not be reported to the core, and we'd lose
2541 control. */
2542 for (bp_location *loc : loc_range)
2543 if (is_breakpoint (loc->owner)
2544 && loc->pspace->num == bl->pspace->num
2545 && loc->owner->type != bp_dprintf)
2546 return;
2547
2548 /* Do a first pass to check for locations with no assigned
2549 conditions or conditions that fail to parse to a valid agent expression
2550 bytecode. If any of these happen, then it's no use to send conditions
2551 to the target since this location will always trigger and generate a
2552 response back to GDB. */
2553 for (bp_location *loc : loc_range)
2554 {
2555 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2556 {
2557 if (modified)
2558 {
2559 /* Re-parse the commands since something changed. In that
2560 case we already freed the command bytecodes (see
2561 force_breakpoint_reinsertion). We just
2562 need to parse the command to bytecodes again. */
2563 loc->cmd_bytecode
2564 = parse_cmd_to_aexpr (bl->address,
2565 loc->owner->extra_string.get ());
2566 }
2567
2568 /* If we have a NULL bytecode expression, it means something
2569 went wrong or we have a null command expression. */
2570 if (!loc->cmd_bytecode)
2571 {
2572 null_command_or_parse_error = 1;
2573 break;
2574 }
2575 }
2576 }
2577
2578 /* If anything failed, then we're not doing target-side commands,
2579 and so clean up. */
2580 if (null_command_or_parse_error)
2581 {
2582 for (bp_location *loc : loc_range)
2583 if (is_breakpoint (loc->owner)
2584 && loc->pspace->num == bl->pspace->num)
2585 {
2586 /* Only go as far as the first NULL bytecode is
2587 located. */
2588 if (loc->cmd_bytecode == NULL)
2589 return;
2590
2591 loc->cmd_bytecode.reset ();
2592 }
2593 }
2594
2595 /* No NULL commands or failed bytecode generation. Build a command
2596 list for all duplicate locations at this location's address.
2597 Note that here we must care for whether the breakpoint location
2598 types are considered duplicates, otherwise, say, if we have a
2599 software and hardware location at the same address, the target
2600 could end up running the commands twice. For the moment, we only
2601 support targets-side commands with dprintf, but it doesn't hurt
2602 to be pedantically correct in case that changes. */
2603 for (bp_location *loc : loc_range)
2604 if (breakpoint_locations_match (bl, loc)
2605 && loc->owner->extra_string
2606 && is_breakpoint (loc->owner)
2607 && loc->pspace->num == bl->pspace->num
2608 && loc->owner->enable_state == bp_enabled
2609 && loc->enabled
2610 && !loc->disabled_by_cond)
2611 {
2612 /* Add the command to the vector. This will be used later
2613 to send the commands to the target. */
2614 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2615 }
2616
2617 bl->target_info.persist = 0;
2618 /* Maybe flag this location as persistent. */
2619 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2620 bl->target_info.persist = 1;
2621 }
2622
2623 /* Return the kind of breakpoint on address *ADDR. Get the kind
2624 of breakpoint according to ADDR except single-step breakpoint.
2625 Get the kind of single-step breakpoint according to the current
2626 registers state. */
2627
2628 static int
2629 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2630 {
2631 if (bl->owner->type == bp_single_step)
2632 {
2633 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2634 struct regcache *regcache;
2635
2636 regcache = get_thread_regcache (thr);
2637
2638 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2639 regcache, addr);
2640 }
2641 else
2642 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2643 }
2644
2645 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2646 location. Any error messages are printed to TMP_ERROR_STREAM; and
2647 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2648 Returns 0 for success, 1 if the bp_location type is not supported or
2649 -1 for failure.
2650
2651 NOTE drow/2003-09-09: This routine could be broken down to an
2652 object-style method for each breakpoint or catchpoint type. */
2653 static int
2654 insert_bp_location (struct bp_location *bl,
2655 struct ui_file *tmp_error_stream,
2656 int *disabled_breaks,
2657 int *hw_breakpoint_error,
2658 int *hw_bp_error_explained_already)
2659 {
2660 gdb_exception bp_excpt;
2661
2662 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2663 return 0;
2664
2665 /* Note we don't initialize bl->target_info, as that wipes out
2666 the breakpoint location's shadow_contents if the breakpoint
2667 is still inserted at that location. This in turn breaks
2668 target_read_memory which depends on these buffers when
2669 a memory read is requested at the breakpoint location:
2670 Once the target_info has been wiped, we fail to see that
2671 we have a breakpoint inserted at that address and thus
2672 read the breakpoint instead of returning the data saved in
2673 the breakpoint location's shadow contents. */
2674 bl->target_info.reqstd_address = bl->address;
2675 bl->target_info.placed_address_space = bl->pspace->aspace;
2676 bl->target_info.length = bl->length;
2677
2678 /* When working with target-side conditions, we must pass all the conditions
2679 for the same breakpoint address down to the target since GDB will not
2680 insert those locations. With a list of breakpoint conditions, the target
2681 can decide when to stop and notify GDB. */
2682
2683 if (is_breakpoint (bl->owner))
2684 {
2685 build_target_condition_list (bl);
2686 build_target_command_list (bl);
2687 /* Reset the modification marker. */
2688 bl->needs_update = 0;
2689 }
2690
2691 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2692 set at a read-only address, then a breakpoint location will have
2693 been changed to hardware breakpoint before we get here. If it is
2694 "off" however, error out before actually trying to insert the
2695 breakpoint, with a nicer error message. */
2696 if (bl->loc_type == bp_loc_software_breakpoint
2697 && !automatic_hardware_breakpoints)
2698 {
2699 mem_region *mr = lookup_mem_region (bl->address);
2700
2701 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2702 {
2703 gdb_printf (tmp_error_stream,
2704 _("Cannot insert breakpoint %d.\n"
2705 "Cannot set software breakpoint "
2706 "at read-only address %s\n"),
2707 bl->owner->number,
2708 paddress (bl->gdbarch, bl->address));
2709 return 1;
2710 }
2711 }
2712
2713 if (bl->loc_type == bp_loc_software_breakpoint
2714 || bl->loc_type == bp_loc_hardware_breakpoint)
2715 {
2716 /* First check to see if we have to handle an overlay. */
2717 if (overlay_debugging == ovly_off
2718 || bl->section == NULL
2719 || !(section_is_overlay (bl->section)))
2720 {
2721 /* No overlay handling: just set the breakpoint. */
2722 try
2723 {
2724 int val;
2725
2726 val = bl->owner->insert_location (bl);
2727 if (val)
2728 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2729 }
2730 catch (gdb_exception &e)
2731 {
2732 bp_excpt = std::move (e);
2733 }
2734 }
2735 else
2736 {
2737 /* This breakpoint is in an overlay section.
2738 Shall we set a breakpoint at the LMA? */
2739 if (!overlay_events_enabled)
2740 {
2741 /* Yes -- overlay event support is not active,
2742 so we must try to set a breakpoint at the LMA.
2743 This will not work for a hardware breakpoint. */
2744 if (bl->loc_type == bp_loc_hardware_breakpoint)
2745 warning (_("hardware breakpoint %d not supported in overlay!"),
2746 bl->owner->number);
2747 else
2748 {
2749 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2750 bl->section);
2751 /* Set a software (trap) breakpoint at the LMA. */
2752 bl->overlay_target_info = bl->target_info;
2753 bl->overlay_target_info.reqstd_address = addr;
2754
2755 /* No overlay handling: just set the breakpoint. */
2756 try
2757 {
2758 int val;
2759
2760 bl->overlay_target_info.kind
2761 = breakpoint_kind (bl, &addr);
2762 bl->overlay_target_info.placed_address = addr;
2763 val = target_insert_breakpoint (bl->gdbarch,
2764 &bl->overlay_target_info);
2765 if (val)
2766 bp_excpt
2767 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2768 }
2769 catch (gdb_exception &e)
2770 {
2771 bp_excpt = std::move (e);
2772 }
2773
2774 if (bp_excpt.reason != 0)
2775 gdb_printf (tmp_error_stream,
2776 "Overlay breakpoint %d "
2777 "failed: in ROM?\n",
2778 bl->owner->number);
2779 }
2780 }
2781 /* Shall we set a breakpoint at the VMA? */
2782 if (section_is_mapped (bl->section))
2783 {
2784 /* Yes. This overlay section is mapped into memory. */
2785 try
2786 {
2787 int val;
2788
2789 val = bl->owner->insert_location (bl);
2790 if (val)
2791 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2792 }
2793 catch (gdb_exception &e)
2794 {
2795 bp_excpt = std::move (e);
2796 }
2797 }
2798 else
2799 {
2800 /* No. This breakpoint will not be inserted.
2801 No error, but do not mark the bp as 'inserted'. */
2802 return 0;
2803 }
2804 }
2805
2806 if (bp_excpt.reason != 0)
2807 {
2808 /* Can't set the breakpoint. */
2809
2810 /* If the target has closed then it will have deleted any
2811 breakpoints inserted within the target inferior, as a result
2812 any further attempts to interact with the breakpoint objects
2813 is not possible. Just rethrow the error. */
2814 if (bp_excpt.error == TARGET_CLOSE_ERROR)
2815 throw bp_excpt;
2816 gdb_assert (bl->owner != nullptr);
2817
2818 /* In some cases, we might not be able to insert a
2819 breakpoint in a shared library that has already been
2820 removed, but we have not yet processed the shlib unload
2821 event. Unfortunately, some targets that implement
2822 breakpoint insertion themselves can't tell why the
2823 breakpoint insertion failed (e.g., the remote target
2824 doesn't define error codes), so we must treat generic
2825 errors as memory errors. */
2826 if (bp_excpt.reason == RETURN_ERROR
2827 && (bp_excpt.error == GENERIC_ERROR
2828 || bp_excpt.error == MEMORY_ERROR)
2829 && bl->loc_type == bp_loc_software_breakpoint
2830 && (solib_name_from_address (bl->pspace, bl->address)
2831 || shared_objfile_contains_address_p (bl->pspace,
2832 bl->address)))
2833 {
2834 /* See also: disable_breakpoints_in_shlibs. */
2835 bl->shlib_disabled = 1;
2836 gdb::observers::breakpoint_modified.notify (bl->owner);
2837 if (!*disabled_breaks)
2838 {
2839 gdb_printf (tmp_error_stream,
2840 "Cannot insert breakpoint %d.\n",
2841 bl->owner->number);
2842 gdb_printf (tmp_error_stream,
2843 "Temporarily disabling shared "
2844 "library breakpoints:\n");
2845 }
2846 *disabled_breaks = 1;
2847 gdb_printf (tmp_error_stream,
2848 "breakpoint #%d\n", bl->owner->number);
2849 return 0;
2850 }
2851 else
2852 {
2853 if (bl->loc_type == bp_loc_hardware_breakpoint)
2854 {
2855 *hw_breakpoint_error = 1;
2856 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2857 gdb_printf (tmp_error_stream,
2858 "Cannot insert hardware breakpoint %d%s",
2859 bl->owner->number,
2860 bp_excpt.message ? ":" : ".\n");
2861 if (bp_excpt.message != NULL)
2862 gdb_printf (tmp_error_stream, "%s.\n",
2863 bp_excpt.what ());
2864 }
2865 else
2866 {
2867 if (bp_excpt.message == NULL)
2868 {
2869 std::string message
2870 = memory_error_message (TARGET_XFER_E_IO,
2871 bl->gdbarch, bl->address);
2872
2873 gdb_printf (tmp_error_stream,
2874 "Cannot insert breakpoint %d.\n"
2875 "%s\n",
2876 bl->owner->number, message.c_str ());
2877 }
2878 else
2879 {
2880 gdb_printf (tmp_error_stream,
2881 "Cannot insert breakpoint %d: %s\n",
2882 bl->owner->number,
2883 bp_excpt.what ());
2884 }
2885 }
2886 return 1;
2887
2888 }
2889 }
2890 else
2891 bl->inserted = 1;
2892
2893 return 0;
2894 }
2895
2896 else if (bl->loc_type == bp_loc_hardware_watchpoint
2897 /* NOTE drow/2003-09-08: This state only exists for removing
2898 watchpoints. It's not clear that it's necessary... */
2899 && bl->owner->disposition != disp_del_at_next_stop)
2900 {
2901 int val;
2902
2903 val = bl->owner->insert_location (bl);
2904
2905 /* If trying to set a read-watchpoint, and it turns out it's not
2906 supported, try emulating one with an access watchpoint. */
2907 if (val == 1 && bl->watchpoint_type == hw_read)
2908 {
2909 /* But don't try to insert it, if there's already another
2910 hw_access location that would be considered a duplicate
2911 of this one. */
2912 for (bp_location *loc : all_bp_locations ())
2913 if (loc != bl
2914 && loc->watchpoint_type == hw_access
2915 && watchpoint_locations_match (bl, loc))
2916 {
2917 bl->duplicate = 1;
2918 bl->inserted = 1;
2919 bl->target_info = loc->target_info;
2920 bl->watchpoint_type = hw_access;
2921 val = 0;
2922 break;
2923 }
2924
2925 if (val == 1)
2926 {
2927 bl->watchpoint_type = hw_access;
2928 val = bl->owner->insert_location (bl);
2929
2930 if (val)
2931 /* Back to the original value. */
2932 bl->watchpoint_type = hw_read;
2933 }
2934 }
2935
2936 bl->inserted = (val == 0);
2937 }
2938
2939 else if (bl->owner->type == bp_catchpoint)
2940 {
2941 int val;
2942
2943 val = bl->owner->insert_location (bl);
2944 if (val)
2945 {
2946 bl->owner->enable_state = bp_disabled;
2947
2948 if (val == 1)
2949 warning (_("\
2950 Error inserting catchpoint %d: Your system does not support this type\n\
2951 of catchpoint."), bl->owner->number);
2952 else
2953 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2954 }
2955
2956 bl->inserted = (val == 0);
2957
2958 /* We've already printed an error message if there was a problem
2959 inserting this catchpoint, and we've disabled the catchpoint,
2960 so just return success. */
2961 return 0;
2962 }
2963
2964 return 0;
2965 }
2966
2967 /* This function is called when program space PSPACE is about to be
2968 deleted. It takes care of updating breakpoints to not reference
2969 PSPACE anymore. */
2970
2971 void
2972 breakpoint_program_space_exit (struct program_space *pspace)
2973 {
2974 /* Remove any breakpoint that was set through this program space. */
2975 for (breakpoint *b : all_breakpoints_safe ())
2976 if (b->pspace == pspace)
2977 delete_breakpoint (b);
2978
2979 /* Breakpoints set through other program spaces could have locations
2980 bound to PSPACE as well. Remove those. */
2981 for (bp_location *loc : all_bp_locations ())
2982 {
2983 struct bp_location *tmp;
2984
2985 if (loc->pspace == pspace)
2986 {
2987 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2988 if (loc->owner->loc == loc)
2989 loc->owner->loc = loc->next;
2990 else
2991 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2992 if (tmp->next == loc)
2993 {
2994 tmp->next = loc->next;
2995 break;
2996 }
2997 }
2998 }
2999
3000 /* Now update the global location list to permanently delete the
3001 removed locations above. */
3002 update_global_location_list (UGLL_DONT_INSERT);
3003 }
3004
3005 /* Make sure all breakpoints are inserted in inferior.
3006 Throws exception on any error.
3007 A breakpoint that is already inserted won't be inserted
3008 again, so calling this function twice is safe. */
3009 void
3010 insert_breakpoints (void)
3011 {
3012 for (breakpoint *bpt : all_breakpoints ())
3013 if (is_hardware_watchpoint (bpt))
3014 {
3015 struct watchpoint *w = (struct watchpoint *) bpt;
3016
3017 update_watchpoint (w, 0 /* don't reparse. */);
3018 }
3019
3020 /* Updating watchpoints creates new locations, so update the global
3021 location list. Explicitly tell ugll to insert locations and
3022 ignore breakpoints_always_inserted_mode. Also,
3023 update_global_location_list tries to "upgrade" software
3024 breakpoints to hardware breakpoints to handle "set breakpoint
3025 auto-hw", so we need to call it even if we don't have new
3026 locations. */
3027 update_global_location_list (UGLL_INSERT);
3028 }
3029
3030 /* This is used when we need to synch breakpoint conditions between GDB and the
3031 target. It is the case with deleting and disabling of breakpoints when using
3032 always-inserted mode. */
3033
3034 static void
3035 update_inserted_breakpoint_locations (void)
3036 {
3037 int error_flag = 0;
3038 int val = 0;
3039 int disabled_breaks = 0;
3040 int hw_breakpoint_error = 0;
3041 int hw_bp_details_reported = 0;
3042
3043 string_file tmp_error_stream;
3044
3045 /* Explicitly mark the warning -- this will only be printed if
3046 there was an error. */
3047 tmp_error_stream.puts ("Warning:\n");
3048
3049 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3050
3051 for (bp_location *bl : all_bp_locations ())
3052 {
3053 /* We only want to update software breakpoints and hardware
3054 breakpoints. */
3055 if (!is_breakpoint (bl->owner))
3056 continue;
3057
3058 /* We only want to update locations that are already inserted
3059 and need updating. This is to avoid unwanted insertion during
3060 deletion of breakpoints. */
3061 if (!bl->inserted || !bl->needs_update)
3062 continue;
3063
3064 switch_to_program_space_and_thread (bl->pspace);
3065
3066 /* For targets that support global breakpoints, there's no need
3067 to select an inferior to insert breakpoint to. In fact, even
3068 if we aren't attached to any process yet, we should still
3069 insert breakpoints. */
3070 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3071 && (inferior_ptid == null_ptid || !target_has_execution ()))
3072 continue;
3073
3074 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3075 &hw_breakpoint_error, &hw_bp_details_reported);
3076 if (val)
3077 error_flag = val;
3078 }
3079
3080 if (error_flag)
3081 {
3082 target_terminal::ours_for_output ();
3083 error_stream (tmp_error_stream);
3084 }
3085 }
3086
3087 /* Used when starting or continuing the program. */
3088
3089 static void
3090 insert_breakpoint_locations (void)
3091 {
3092 int error_flag = 0;
3093 int val = 0;
3094 int disabled_breaks = 0;
3095 int hw_breakpoint_error = 0;
3096 int hw_bp_error_explained_already = 0;
3097
3098 string_file tmp_error_stream;
3099
3100 /* Explicitly mark the warning -- this will only be printed if
3101 there was an error. */
3102 tmp_error_stream.puts ("Warning:\n");
3103
3104 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3105
3106 for (bp_location *bl : all_bp_locations ())
3107 {
3108 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3109 continue;
3110
3111 /* There is no point inserting thread-specific breakpoints if
3112 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3113 has BL->OWNER always non-NULL. */
3114 if (bl->owner->thread != -1
3115 && !valid_global_thread_id (bl->owner->thread))
3116 continue;
3117
3118 switch_to_program_space_and_thread (bl->pspace);
3119
3120 /* For targets that support global breakpoints, there's no need
3121 to select an inferior to insert breakpoint to. In fact, even
3122 if we aren't attached to any process yet, we should still
3123 insert breakpoints. */
3124 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3125 && (inferior_ptid == null_ptid || !target_has_execution ()))
3126 continue;
3127
3128 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3129 &hw_breakpoint_error, &hw_bp_error_explained_already);
3130 if (val)
3131 error_flag = val;
3132 }
3133
3134 /* If we failed to insert all locations of a watchpoint, remove
3135 them, as half-inserted watchpoint is of limited use. */
3136 for (breakpoint *bpt : all_breakpoints ())
3137 {
3138 int some_failed = 0;
3139
3140 if (!is_hardware_watchpoint (bpt))
3141 continue;
3142
3143 if (!breakpoint_enabled (bpt))
3144 continue;
3145
3146 if (bpt->disposition == disp_del_at_next_stop)
3147 continue;
3148
3149 for (bp_location *loc : bpt->locations ())
3150 if (!loc->inserted && should_be_inserted (loc))
3151 {
3152 some_failed = 1;
3153 break;
3154 }
3155
3156 if (some_failed)
3157 {
3158 for (bp_location *loc : bpt->locations ())
3159 if (loc->inserted)
3160 remove_breakpoint (loc);
3161
3162 hw_breakpoint_error = 1;
3163 tmp_error_stream.printf ("Could not insert "
3164 "hardware watchpoint %d.\n",
3165 bpt->number);
3166 error_flag = -1;
3167 }
3168 }
3169
3170 if (error_flag)
3171 {
3172 /* If a hardware breakpoint or watchpoint was inserted, add a
3173 message about possibly exhausted resources. */
3174 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3175 {
3176 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3177 You may have requested too many hardware breakpoints/watchpoints.\n");
3178 }
3179 target_terminal::ours_for_output ();
3180 error_stream (tmp_error_stream);
3181 }
3182 }
3183
3184 /* Used when the program stops.
3185 Returns zero if successful, or non-zero if there was a problem
3186 removing a breakpoint location. */
3187
3188 int
3189 remove_breakpoints (void)
3190 {
3191 int val = 0;
3192
3193 for (bp_location *bl : all_bp_locations ())
3194 if (bl->inserted && !is_tracepoint (bl->owner))
3195 val |= remove_breakpoint (bl);
3196
3197 return val;
3198 }
3199
3200 /* When a thread exits, remove breakpoints that are related to
3201 that thread. */
3202
3203 static void
3204 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3205 {
3206 for (breakpoint *b : all_breakpoints_safe ())
3207 {
3208 if (b->thread == tp->global_num && user_breakpoint_p (b))
3209 {
3210 b->disposition = disp_del_at_next_stop;
3211
3212 gdb_printf (_("\
3213 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3214 b->number, print_thread_id (tp));
3215
3216 /* Hide it from the user. */
3217 b->number = 0;
3218 }
3219 }
3220 }
3221
3222 /* See breakpoint.h. */
3223
3224 void
3225 remove_breakpoints_inf (inferior *inf)
3226 {
3227 int val;
3228
3229 for (bp_location *bl : all_bp_locations ())
3230 {
3231 if (bl->pspace != inf->pspace)
3232 continue;
3233
3234 if (bl->inserted && !bl->target_info.persist)
3235 {
3236 val = remove_breakpoint (bl);
3237 if (val != 0)
3238 return;
3239 }
3240 }
3241 }
3242
3243 static int internal_breakpoint_number = -1;
3244
3245 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3246 If INTERNAL is non-zero, the breakpoint number will be populated
3247 from internal_breakpoint_number and that variable decremented.
3248 Otherwise the breakpoint number will be populated from
3249 breakpoint_count and that value incremented. Internal breakpoints
3250 do not set the internal var bpnum. */
3251 static void
3252 set_breakpoint_number (int internal, struct breakpoint *b)
3253 {
3254 if (internal)
3255 b->number = internal_breakpoint_number--;
3256 else
3257 {
3258 set_breakpoint_count (breakpoint_count + 1);
3259 b->number = breakpoint_count;
3260 }
3261 }
3262
3263 static struct breakpoint *
3264 create_internal_breakpoint (struct gdbarch *gdbarch,
3265 CORE_ADDR address, enum bptype type)
3266 {
3267 symtab_and_line sal;
3268 sal.pc = address;
3269 sal.section = find_pc_overlay (sal.pc);
3270 sal.pspace = current_program_space;
3271
3272 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type);
3273 b->number = internal_breakpoint_number--;
3274 b->disposition = disp_donttouch;
3275
3276 return b;
3277 }
3278
3279 static const char *const longjmp_names[] =
3280 {
3281 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3282 };
3283 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3284
3285 /* Per-objfile data private to breakpoint.c. */
3286 struct breakpoint_objfile_data
3287 {
3288 /* Minimal symbol for "_ovly_debug_event" (if any). */
3289 struct bound_minimal_symbol overlay_msym;
3290
3291 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3292 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3293
3294 /* True if we have looked for longjmp probes. */
3295 int longjmp_searched = 0;
3296
3297 /* SystemTap probe points for longjmp (if any). These are non-owning
3298 references. */
3299 std::vector<probe *> longjmp_probes;
3300
3301 /* Minimal symbol for "std::terminate()" (if any). */
3302 struct bound_minimal_symbol terminate_msym;
3303
3304 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3305 struct bound_minimal_symbol exception_msym;
3306
3307 /* True if we have looked for exception probes. */
3308 int exception_searched = 0;
3309
3310 /* SystemTap probe points for unwinding (if any). These are non-owning
3311 references. */
3312 std::vector<probe *> exception_probes;
3313 };
3314
3315 static const struct objfile_key<breakpoint_objfile_data>
3316 breakpoint_objfile_key;
3317
3318 /* Minimal symbol not found sentinel. */
3319 static struct minimal_symbol msym_not_found;
3320
3321 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3322
3323 static int
3324 msym_not_found_p (const struct minimal_symbol *msym)
3325 {
3326 return msym == &msym_not_found;
3327 }
3328
3329 /* Return per-objfile data needed by breakpoint.c.
3330 Allocate the data if necessary. */
3331
3332 static struct breakpoint_objfile_data *
3333 get_breakpoint_objfile_data (struct objfile *objfile)
3334 {
3335 struct breakpoint_objfile_data *bp_objfile_data;
3336
3337 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3338 if (bp_objfile_data == NULL)
3339 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3340 return bp_objfile_data;
3341 }
3342
3343 static void
3344 create_overlay_event_breakpoint (void)
3345 {
3346 const char *const func_name = "_ovly_debug_event";
3347
3348 for (objfile *objfile : current_program_space->objfiles ())
3349 {
3350 struct breakpoint *b;
3351 struct breakpoint_objfile_data *bp_objfile_data;
3352 CORE_ADDR addr;
3353 struct explicit_location explicit_loc;
3354
3355 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3356
3357 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3358 continue;
3359
3360 if (bp_objfile_data->overlay_msym.minsym == NULL)
3361 {
3362 struct bound_minimal_symbol m;
3363
3364 m = lookup_minimal_symbol_text (func_name, objfile);
3365 if (m.minsym == NULL)
3366 {
3367 /* Avoid future lookups in this objfile. */
3368 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3369 continue;
3370 }
3371 bp_objfile_data->overlay_msym = m;
3372 }
3373
3374 addr = bp_objfile_data->overlay_msym.value_address ();
3375 b = create_internal_breakpoint (objfile->arch (), addr,
3376 bp_overlay_event);
3377 initialize_explicit_location (&explicit_loc);
3378 explicit_loc.function_name = ASTRDUP (func_name);
3379 b->location = new_explicit_location (&explicit_loc);
3380
3381 if (overlay_debugging == ovly_auto)
3382 {
3383 b->enable_state = bp_enabled;
3384 overlay_events_enabled = 1;
3385 }
3386 else
3387 {
3388 b->enable_state = bp_disabled;
3389 overlay_events_enabled = 0;
3390 }
3391 }
3392 }
3393
3394 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3395 true if a breakpoint was installed. */
3396
3397 static bool
3398 create_longjmp_master_breakpoint_probe (objfile *objfile)
3399 {
3400 struct gdbarch *gdbarch = objfile->arch ();
3401 struct breakpoint_objfile_data *bp_objfile_data
3402 = get_breakpoint_objfile_data (objfile);
3403
3404 if (!bp_objfile_data->longjmp_searched)
3405 {
3406 std::vector<probe *> ret
3407 = find_probes_in_objfile (objfile, "libc", "longjmp");
3408
3409 if (!ret.empty ())
3410 {
3411 /* We are only interested in checking one element. */
3412 probe *p = ret[0];
3413
3414 if (!p->can_evaluate_arguments ())
3415 {
3416 /* We cannot use the probe interface here,
3417 because it does not know how to evaluate
3418 arguments. */
3419 ret.clear ();
3420 }
3421 }
3422 bp_objfile_data->longjmp_probes = ret;
3423 bp_objfile_data->longjmp_searched = 1;
3424 }
3425
3426 if (bp_objfile_data->longjmp_probes.empty ())
3427 return false;
3428
3429 for (probe *p : bp_objfile_data->longjmp_probes)
3430 {
3431 struct breakpoint *b;
3432
3433 b = create_internal_breakpoint (gdbarch,
3434 p->get_relocated_address (objfile),
3435 bp_longjmp_master);
3436 b->location = new_probe_location ("-probe-stap libc:longjmp");
3437 b->enable_state = bp_disabled;
3438 }
3439
3440 return true;
3441 }
3442
3443 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3444 Return true if at least one breakpoint was installed. */
3445
3446 static bool
3447 create_longjmp_master_breakpoint_names (objfile *objfile)
3448 {
3449 struct gdbarch *gdbarch = objfile->arch ();
3450 if (!gdbarch_get_longjmp_target_p (gdbarch))
3451 return false;
3452
3453 struct breakpoint_objfile_data *bp_objfile_data
3454 = get_breakpoint_objfile_data (objfile);
3455 unsigned int installed_bp = 0;
3456
3457 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3458 {
3459 struct breakpoint *b;
3460 const char *func_name;
3461 CORE_ADDR addr;
3462 struct explicit_location explicit_loc;
3463
3464 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3465 continue;
3466
3467 func_name = longjmp_names[i];
3468 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3469 {
3470 struct bound_minimal_symbol m;
3471
3472 m = lookup_minimal_symbol_text (func_name, objfile);
3473 if (m.minsym == NULL)
3474 {
3475 /* Prevent future lookups in this objfile. */
3476 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3477 continue;
3478 }
3479 bp_objfile_data->longjmp_msym[i] = m;
3480 }
3481
3482 addr = bp_objfile_data->longjmp_msym[i].value_address ();
3483 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
3484 initialize_explicit_location (&explicit_loc);
3485 explicit_loc.function_name = ASTRDUP (func_name);
3486 b->location = new_explicit_location (&explicit_loc);
3487 b->enable_state = bp_disabled;
3488 installed_bp++;
3489 }
3490
3491 return installed_bp > 0;
3492 }
3493
3494 /* Create a master longjmp breakpoint. */
3495
3496 static void
3497 create_longjmp_master_breakpoint (void)
3498 {
3499 scoped_restore_current_program_space restore_pspace;
3500
3501 for (struct program_space *pspace : program_spaces)
3502 {
3503 set_current_program_space (pspace);
3504
3505 for (objfile *obj : current_program_space->objfiles ())
3506 {
3507 /* Skip separate debug object, it's handled in the loop below. */
3508 if (obj->separate_debug_objfile_backlink != nullptr)
3509 continue;
3510
3511 /* Try a probe kind breakpoint on main objfile. */
3512 if (create_longjmp_master_breakpoint_probe (obj))
3513 continue;
3514
3515 /* Try longjmp_names kind breakpoints on main and separate_debug
3516 objfiles. */
3517 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3518 if (create_longjmp_master_breakpoint_names (debug_objfile))
3519 break;
3520 }
3521 }
3522 }
3523
3524 /* Create a master std::terminate breakpoint. */
3525 static void
3526 create_std_terminate_master_breakpoint (void)
3527 {
3528 const char *const func_name = "std::terminate()";
3529
3530 scoped_restore_current_program_space restore_pspace;
3531
3532 for (struct program_space *pspace : program_spaces)
3533 {
3534 CORE_ADDR addr;
3535
3536 set_current_program_space (pspace);
3537
3538 for (objfile *objfile : current_program_space->objfiles ())
3539 {
3540 struct breakpoint *b;
3541 struct breakpoint_objfile_data *bp_objfile_data;
3542 struct explicit_location explicit_loc;
3543
3544 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3545
3546 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3547 continue;
3548
3549 if (bp_objfile_data->terminate_msym.minsym == NULL)
3550 {
3551 struct bound_minimal_symbol m;
3552
3553 m = lookup_minimal_symbol (func_name, NULL, objfile);
3554 if (m.minsym == NULL || (m.minsym->type () != mst_text
3555 && m.minsym->type () != mst_file_text))
3556 {
3557 /* Prevent future lookups in this objfile. */
3558 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3559 continue;
3560 }
3561 bp_objfile_data->terminate_msym = m;
3562 }
3563
3564 addr = bp_objfile_data->terminate_msym.value_address ();
3565 b = create_internal_breakpoint (objfile->arch (), addr,
3566 bp_std_terminate_master);
3567 initialize_explicit_location (&explicit_loc);
3568 explicit_loc.function_name = ASTRDUP (func_name);
3569 b->location = new_explicit_location (&explicit_loc);
3570 b->enable_state = bp_disabled;
3571 }
3572 }
3573 }
3574
3575 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3576 probe. Return true if a breakpoint was installed. */
3577
3578 static bool
3579 create_exception_master_breakpoint_probe (objfile *objfile)
3580 {
3581 struct breakpoint *b;
3582 struct gdbarch *gdbarch;
3583 struct breakpoint_objfile_data *bp_objfile_data;
3584
3585 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3586
3587 /* We prefer the SystemTap probe point if it exists. */
3588 if (!bp_objfile_data->exception_searched)
3589 {
3590 std::vector<probe *> ret
3591 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3592
3593 if (!ret.empty ())
3594 {
3595 /* We are only interested in checking one element. */
3596 probe *p = ret[0];
3597
3598 if (!p->can_evaluate_arguments ())
3599 {
3600 /* We cannot use the probe interface here, because it does
3601 not know how to evaluate arguments. */
3602 ret.clear ();
3603 }
3604 }
3605 bp_objfile_data->exception_probes = ret;
3606 bp_objfile_data->exception_searched = 1;
3607 }
3608
3609 if (bp_objfile_data->exception_probes.empty ())
3610 return false;
3611
3612 gdbarch = objfile->arch ();
3613
3614 for (probe *p : bp_objfile_data->exception_probes)
3615 {
3616 b = create_internal_breakpoint (gdbarch,
3617 p->get_relocated_address (objfile),
3618 bp_exception_master);
3619 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3620 b->enable_state = bp_disabled;
3621 }
3622
3623 return true;
3624 }
3625
3626 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3627 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3628
3629 static bool
3630 create_exception_master_breakpoint_hook (objfile *objfile)
3631 {
3632 const char *const func_name = "_Unwind_DebugHook";
3633 struct breakpoint *b;
3634 struct gdbarch *gdbarch;
3635 struct breakpoint_objfile_data *bp_objfile_data;
3636 CORE_ADDR addr;
3637 struct explicit_location explicit_loc;
3638
3639 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3640
3641 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3642 return false;
3643
3644 gdbarch = objfile->arch ();
3645
3646 if (bp_objfile_data->exception_msym.minsym == NULL)
3647 {
3648 struct bound_minimal_symbol debug_hook;
3649
3650 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3651 if (debug_hook.minsym == NULL)
3652 {
3653 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3654 return false;
3655 }
3656
3657 bp_objfile_data->exception_msym = debug_hook;
3658 }
3659
3660 addr = bp_objfile_data->exception_msym.value_address ();
3661 addr = gdbarch_convert_from_func_ptr_addr
3662 (gdbarch, addr, current_inferior ()->top_target ());
3663 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
3664 initialize_explicit_location (&explicit_loc);
3665 explicit_loc.function_name = ASTRDUP (func_name);
3666 b->location = new_explicit_location (&explicit_loc);
3667 b->enable_state = bp_disabled;
3668
3669 return true;
3670 }
3671
3672 /* Install a master breakpoint on the unwinder's debug hook. */
3673
3674 static void
3675 create_exception_master_breakpoint (void)
3676 {
3677 for (objfile *obj : current_program_space->objfiles ())
3678 {
3679 /* Skip separate debug object. */
3680 if (obj->separate_debug_objfile_backlink)
3681 continue;
3682
3683 /* Try a probe kind breakpoint. */
3684 if (create_exception_master_breakpoint_probe (obj))
3685 continue;
3686
3687 /* Iterate over main and separate debug objects and try an
3688 _Unwind_DebugHook kind breakpoint. */
3689 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3690 if (create_exception_master_breakpoint_hook (debug_objfile))
3691 break;
3692 }
3693 }
3694
3695 /* Does B have a location spec? */
3696
3697 static int
3698 breakpoint_event_location_empty_p (const struct breakpoint *b)
3699 {
3700 return b->location != NULL && event_location_empty_p (b->location.get ());
3701 }
3702
3703 void
3704 update_breakpoints_after_exec (void)
3705 {
3706 /* We're about to delete breakpoints from GDB's lists. If the
3707 INSERTED flag is true, GDB will try to lift the breakpoints by
3708 writing the breakpoints' "shadow contents" back into memory. The
3709 "shadow contents" are NOT valid after an exec, so GDB should not
3710 do that. Instead, the target is responsible from marking
3711 breakpoints out as soon as it detects an exec. We don't do that
3712 here instead, because there may be other attempts to delete
3713 breakpoints after detecting an exec and before reaching here. */
3714 for (bp_location *bploc : all_bp_locations ())
3715 if (bploc->pspace == current_program_space)
3716 gdb_assert (!bploc->inserted);
3717
3718 for (breakpoint *b : all_breakpoints_safe ())
3719 {
3720 if (b->pspace != current_program_space)
3721 continue;
3722
3723 /* Solib breakpoints must be explicitly reset after an exec(). */
3724 if (b->type == bp_shlib_event)
3725 {
3726 delete_breakpoint (b);
3727 continue;
3728 }
3729
3730 /* JIT breakpoints must be explicitly reset after an exec(). */
3731 if (b->type == bp_jit_event)
3732 {
3733 delete_breakpoint (b);
3734 continue;
3735 }
3736
3737 /* Thread event breakpoints must be set anew after an exec(),
3738 as must overlay event and longjmp master breakpoints. */
3739 if (b->type == bp_thread_event || b->type == bp_overlay_event
3740 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3741 || b->type == bp_exception_master)
3742 {
3743 delete_breakpoint (b);
3744 continue;
3745 }
3746
3747 /* Step-resume breakpoints are meaningless after an exec(). */
3748 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3749 {
3750 delete_breakpoint (b);
3751 continue;
3752 }
3753
3754 /* Just like single-step breakpoints. */
3755 if (b->type == bp_single_step)
3756 {
3757 delete_breakpoint (b);
3758 continue;
3759 }
3760
3761 /* Longjmp and longjmp-resume breakpoints are also meaningless
3762 after an exec. */
3763 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3764 || b->type == bp_longjmp_call_dummy
3765 || b->type == bp_exception || b->type == bp_exception_resume)
3766 {
3767 delete_breakpoint (b);
3768 continue;
3769 }
3770
3771 if (b->type == bp_catchpoint)
3772 {
3773 /* For now, none of the bp_catchpoint breakpoints need to
3774 do anything at this point. In the future, if some of
3775 the catchpoints need to something, we will need to add
3776 a new method, and call this method from here. */
3777 continue;
3778 }
3779
3780 /* bp_finish is a special case. The only way we ought to be able
3781 to see one of these when an exec() has happened, is if the user
3782 caught a vfork, and then said "finish". Ordinarily a finish just
3783 carries them to the call-site of the current callee, by setting
3784 a temporary bp there and resuming. But in this case, the finish
3785 will carry them entirely through the vfork & exec.
3786
3787 We don't want to allow a bp_finish to remain inserted now. But
3788 we can't safely delete it, 'cause finish_command has a handle to
3789 the bp on a bpstat, and will later want to delete it. There's a
3790 chance (and I've seen it happen) that if we delete the bp_finish
3791 here, that its storage will get reused by the time finish_command
3792 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3793 We really must allow finish_command to delete a bp_finish.
3794
3795 In the absence of a general solution for the "how do we know
3796 it's safe to delete something others may have handles to?"
3797 problem, what we'll do here is just uninsert the bp_finish, and
3798 let finish_command delete it.
3799
3800 (We know the bp_finish is "doomed" in the sense that it's
3801 momentary, and will be deleted as soon as finish_command sees
3802 the inferior stopped. So it doesn't matter that the bp's
3803 address is probably bogus in the new a.out, unlike e.g., the
3804 solib breakpoints.) */
3805
3806 if (b->type == bp_finish)
3807 {
3808 continue;
3809 }
3810
3811 /* Without a symbolic address, we have little hope of the
3812 pre-exec() address meaning the same thing in the post-exec()
3813 a.out. */
3814 if (breakpoint_event_location_empty_p (b))
3815 {
3816 delete_breakpoint (b);
3817 continue;
3818 }
3819 }
3820 }
3821
3822 int
3823 detach_breakpoints (ptid_t ptid)
3824 {
3825 int val = 0;
3826 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3827 struct inferior *inf = current_inferior ();
3828
3829 if (ptid.pid () == inferior_ptid.pid ())
3830 error (_("Cannot detach breakpoints of inferior_ptid"));
3831
3832 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3833 inferior_ptid = ptid;
3834 for (bp_location *bl : all_bp_locations ())
3835 {
3836 if (bl->pspace != inf->pspace)
3837 continue;
3838
3839 /* This function must physically remove breakpoints locations
3840 from the specified ptid, without modifying the breakpoint
3841 package's state. Locations of type bp_loc_other are only
3842 maintained at GDB side. So, there is no need to remove
3843 these bp_loc_other locations. Moreover, removing these
3844 would modify the breakpoint package's state. */
3845 if (bl->loc_type == bp_loc_other)
3846 continue;
3847
3848 if (bl->inserted)
3849 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3850 }
3851
3852 return val;
3853 }
3854
3855 /* Remove the breakpoint location BL from the current address space.
3856 Note that this is used to detach breakpoints from a child fork.
3857 When we get here, the child isn't in the inferior list, and neither
3858 do we have objects to represent its address space --- we should
3859 *not* look at bl->pspace->aspace here. */
3860
3861 static int
3862 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3863 {
3864 int val;
3865
3866 /* BL is never in moribund_locations by our callers. */
3867 gdb_assert (bl->owner != NULL);
3868
3869 /* The type of none suggests that owner is actually deleted.
3870 This should not ever happen. */
3871 gdb_assert (bl->owner->type != bp_none);
3872
3873 if (bl->loc_type == bp_loc_software_breakpoint
3874 || bl->loc_type == bp_loc_hardware_breakpoint)
3875 {
3876 /* "Normal" instruction breakpoint: either the standard
3877 trap-instruction bp (bp_breakpoint), or a
3878 bp_hardware_breakpoint. */
3879
3880 /* First check to see if we have to handle an overlay. */
3881 if (overlay_debugging == ovly_off
3882 || bl->section == NULL
3883 || !(section_is_overlay (bl->section)))
3884 {
3885 /* No overlay handling: just remove the breakpoint. */
3886
3887 /* If we're trying to uninsert a memory breakpoint that we
3888 know is set in a dynamic object that is marked
3889 shlib_disabled, then either the dynamic object was
3890 removed with "remove-symbol-file" or with
3891 "nosharedlibrary". In the former case, we don't know
3892 whether another dynamic object might have loaded over the
3893 breakpoint's address -- the user might well let us know
3894 about it next with add-symbol-file (the whole point of
3895 add-symbol-file is letting the user manually maintain a
3896 list of dynamically loaded objects). If we have the
3897 breakpoint's shadow memory, that is, this is a software
3898 breakpoint managed by GDB, check whether the breakpoint
3899 is still inserted in memory, to avoid overwriting wrong
3900 code with stale saved shadow contents. Note that HW
3901 breakpoints don't have shadow memory, as they're
3902 implemented using a mechanism that is not dependent on
3903 being able to modify the target's memory, and as such
3904 they should always be removed. */
3905 if (bl->shlib_disabled
3906 && bl->target_info.shadow_len != 0
3907 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3908 val = 0;
3909 else
3910 val = bl->owner->remove_location (bl, reason);
3911 }
3912 else
3913 {
3914 /* This breakpoint is in an overlay section.
3915 Did we set a breakpoint at the LMA? */
3916 if (!overlay_events_enabled)
3917 {
3918 /* Yes -- overlay event support is not active, so we
3919 should have set a breakpoint at the LMA. Remove it.
3920 */
3921 /* Ignore any failures: if the LMA is in ROM, we will
3922 have already warned when we failed to insert it. */
3923 if (bl->loc_type == bp_loc_hardware_breakpoint)
3924 target_remove_hw_breakpoint (bl->gdbarch,
3925 &bl->overlay_target_info);
3926 else
3927 target_remove_breakpoint (bl->gdbarch,
3928 &bl->overlay_target_info,
3929 reason);
3930 }
3931 /* Did we set a breakpoint at the VMA?
3932 If so, we will have marked the breakpoint 'inserted'. */
3933 if (bl->inserted)
3934 {
3935 /* Yes -- remove it. Previously we did not bother to
3936 remove the breakpoint if the section had been
3937 unmapped, but let's not rely on that being safe. We
3938 don't know what the overlay manager might do. */
3939
3940 /* However, we should remove *software* breakpoints only
3941 if the section is still mapped, or else we overwrite
3942 wrong code with the saved shadow contents. */
3943 if (bl->loc_type == bp_loc_hardware_breakpoint
3944 || section_is_mapped (bl->section))
3945 val = bl->owner->remove_location (bl, reason);
3946 else
3947 val = 0;
3948 }
3949 else
3950 {
3951 /* No -- not inserted, so no need to remove. No error. */
3952 val = 0;
3953 }
3954 }
3955
3956 /* In some cases, we might not be able to remove a breakpoint in
3957 a shared library that has already been removed, but we have
3958 not yet processed the shlib unload event. Similarly for an
3959 unloaded add-symbol-file object - the user might not yet have
3960 had the chance to remove-symbol-file it. shlib_disabled will
3961 be set if the library/object has already been removed, but
3962 the breakpoint hasn't been uninserted yet, e.g., after
3963 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3964 always-inserted mode. */
3965 if (val
3966 && (bl->loc_type == bp_loc_software_breakpoint
3967 && (bl->shlib_disabled
3968 || solib_name_from_address (bl->pspace, bl->address)
3969 || shared_objfile_contains_address_p (bl->pspace,
3970 bl->address))))
3971 val = 0;
3972
3973 if (val)
3974 return val;
3975 bl->inserted = (reason == DETACH_BREAKPOINT);
3976 }
3977 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3978 {
3979 bl->inserted = (reason == DETACH_BREAKPOINT);
3980 bl->owner->remove_location (bl, reason);
3981
3982 /* Failure to remove any of the hardware watchpoints comes here. */
3983 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3984 warning (_("Could not remove hardware watchpoint %d."),
3985 bl->owner->number);
3986 }
3987 else if (bl->owner->type == bp_catchpoint
3988 && breakpoint_enabled (bl->owner)
3989 && !bl->duplicate)
3990 {
3991 val = bl->owner->remove_location (bl, reason);
3992 if (val)
3993 return val;
3994
3995 bl->inserted = (reason == DETACH_BREAKPOINT);
3996 }
3997
3998 return 0;
3999 }
4000
4001 static int
4002 remove_breakpoint (struct bp_location *bl)
4003 {
4004 /* BL is never in moribund_locations by our callers. */
4005 gdb_assert (bl->owner != NULL);
4006
4007 /* The type of none suggests that owner is actually deleted.
4008 This should not ever happen. */
4009 gdb_assert (bl->owner->type != bp_none);
4010
4011 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4012
4013 switch_to_program_space_and_thread (bl->pspace);
4014
4015 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4016 }
4017
4018 /* Clear the "inserted" flag in all breakpoints. */
4019
4020 void
4021 mark_breakpoints_out (void)
4022 {
4023 for (bp_location *bl : all_bp_locations ())
4024 if (bl->pspace == current_program_space)
4025 bl->inserted = 0;
4026 }
4027
4028 /* Clear the "inserted" flag in all breakpoints and delete any
4029 breakpoints which should go away between runs of the program.
4030
4031 Plus other such housekeeping that has to be done for breakpoints
4032 between runs.
4033
4034 Note: this function gets called at the end of a run (by
4035 generic_mourn_inferior) and when a run begins (by
4036 init_wait_for_inferior). */
4037
4038
4039
4040 void
4041 breakpoint_init_inferior (enum inf_context context)
4042 {
4043 struct program_space *pspace = current_program_space;
4044
4045 /* If breakpoint locations are shared across processes, then there's
4046 nothing to do. */
4047 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4048 return;
4049
4050 mark_breakpoints_out ();
4051
4052 for (breakpoint *b : all_breakpoints_safe ())
4053 {
4054 if (b->loc && b->loc->pspace != pspace)
4055 continue;
4056
4057 switch (b->type)
4058 {
4059 case bp_call_dummy:
4060 case bp_longjmp_call_dummy:
4061
4062 /* If the call dummy breakpoint is at the entry point it will
4063 cause problems when the inferior is rerun, so we better get
4064 rid of it. */
4065
4066 case bp_watchpoint_scope:
4067
4068 /* Also get rid of scope breakpoints. */
4069
4070 case bp_shlib_event:
4071
4072 /* Also remove solib event breakpoints. Their addresses may
4073 have changed since the last time we ran the program.
4074 Actually we may now be debugging against different target;
4075 and so the solib backend that installed this breakpoint may
4076 not be used in by the target. E.g.,
4077
4078 (gdb) file prog-linux
4079 (gdb) run # native linux target
4080 ...
4081 (gdb) kill
4082 (gdb) file prog-win.exe
4083 (gdb) tar rem :9999 # remote Windows gdbserver.
4084 */
4085
4086 case bp_step_resume:
4087
4088 /* Also remove step-resume breakpoints. */
4089
4090 case bp_single_step:
4091
4092 /* Also remove single-step breakpoints. */
4093
4094 delete_breakpoint (b);
4095 break;
4096
4097 case bp_watchpoint:
4098 case bp_hardware_watchpoint:
4099 case bp_read_watchpoint:
4100 case bp_access_watchpoint:
4101 {
4102 struct watchpoint *w = (struct watchpoint *) b;
4103
4104 /* Likewise for watchpoints on local expressions. */
4105 if (w->exp_valid_block != NULL)
4106 delete_breakpoint (b);
4107 else
4108 {
4109 /* Get rid of existing locations, which are no longer
4110 valid. New ones will be created in
4111 update_watchpoint, when the inferior is restarted.
4112 The next update_global_location_list call will
4113 garbage collect them. */
4114 b->loc = NULL;
4115
4116 if (context == inf_starting)
4117 {
4118 /* Reset val field to force reread of starting value in
4119 insert_breakpoints. */
4120 w->val.reset (nullptr);
4121 w->val_valid = false;
4122 }
4123 }
4124 }
4125 break;
4126 default:
4127 break;
4128 }
4129 }
4130
4131 /* Get rid of the moribund locations. */
4132 for (bp_location *bl : moribund_locations)
4133 decref_bp_location (&bl);
4134 moribund_locations.clear ();
4135 }
4136
4137 /* These functions concern about actual breakpoints inserted in the
4138 target --- to e.g. check if we need to do decr_pc adjustment or if
4139 we need to hop over the bkpt --- so we check for address space
4140 match, not program space. */
4141
4142 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4143 exists at PC. It returns ordinary_breakpoint_here if it's an
4144 ordinary breakpoint, or permanent_breakpoint_here if it's a
4145 permanent breakpoint.
4146 - When continuing from a location with an ordinary breakpoint, we
4147 actually single step once before calling insert_breakpoints.
4148 - When continuing from a location with a permanent breakpoint, we
4149 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4150 the target, to advance the PC past the breakpoint. */
4151
4152 enum breakpoint_here
4153 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4154 {
4155 int any_breakpoint_here = 0;
4156
4157 for (bp_location *bl : all_bp_locations ())
4158 {
4159 if (bl->loc_type != bp_loc_software_breakpoint
4160 && bl->loc_type != bp_loc_hardware_breakpoint)
4161 continue;
4162
4163 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4164 if ((breakpoint_enabled (bl->owner)
4165 || bl->permanent)
4166 && breakpoint_location_address_match (bl, aspace, pc))
4167 {
4168 if (overlay_debugging
4169 && section_is_overlay (bl->section)
4170 && !section_is_mapped (bl->section))
4171 continue; /* unmapped overlay -- can't be a match */
4172 else if (bl->permanent)
4173 return permanent_breakpoint_here;
4174 else
4175 any_breakpoint_here = 1;
4176 }
4177 }
4178
4179 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4180 }
4181
4182 /* See breakpoint.h. */
4183
4184 int
4185 breakpoint_in_range_p (const address_space *aspace,
4186 CORE_ADDR addr, ULONGEST len)
4187 {
4188 for (bp_location *bl : all_bp_locations ())
4189 {
4190 if (bl->loc_type != bp_loc_software_breakpoint
4191 && bl->loc_type != bp_loc_hardware_breakpoint)
4192 continue;
4193
4194 if ((breakpoint_enabled (bl->owner)
4195 || bl->permanent)
4196 && breakpoint_location_address_range_overlap (bl, aspace,
4197 addr, len))
4198 {
4199 if (overlay_debugging
4200 && section_is_overlay (bl->section)
4201 && !section_is_mapped (bl->section))
4202 {
4203 /* Unmapped overlay -- can't be a match. */
4204 continue;
4205 }
4206
4207 return 1;
4208 }
4209 }
4210
4211 return 0;
4212 }
4213
4214 /* Return true if there's a moribund breakpoint at PC. */
4215
4216 int
4217 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4218 {
4219 for (bp_location *loc : moribund_locations)
4220 if (breakpoint_location_address_match (loc, aspace, pc))
4221 return 1;
4222
4223 return 0;
4224 }
4225
4226 /* Returns non-zero iff BL is inserted at PC, in address space
4227 ASPACE. */
4228
4229 static int
4230 bp_location_inserted_here_p (struct bp_location *bl,
4231 const address_space *aspace, CORE_ADDR pc)
4232 {
4233 if (bl->inserted
4234 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4235 aspace, pc))
4236 {
4237 if (overlay_debugging
4238 && section_is_overlay (bl->section)
4239 && !section_is_mapped (bl->section))
4240 return 0; /* unmapped overlay -- can't be a match */
4241 else
4242 return 1;
4243 }
4244 return 0;
4245 }
4246
4247 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4248
4249 int
4250 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4251 {
4252 for (bp_location *bl : all_bp_locations_at_addr (pc))
4253 {
4254 if (bl->loc_type != bp_loc_software_breakpoint
4255 && bl->loc_type != bp_loc_hardware_breakpoint)
4256 continue;
4257
4258 if (bp_location_inserted_here_p (bl, aspace, pc))
4259 return 1;
4260 }
4261 return 0;
4262 }
4263
4264 /* This function returns non-zero iff there is a software breakpoint
4265 inserted at PC. */
4266
4267 int
4268 software_breakpoint_inserted_here_p (const address_space *aspace,
4269 CORE_ADDR pc)
4270 {
4271 for (bp_location *bl : all_bp_locations_at_addr (pc))
4272 {
4273 if (bl->loc_type != bp_loc_software_breakpoint)
4274 continue;
4275
4276 if (bp_location_inserted_here_p (bl, aspace, pc))
4277 return 1;
4278 }
4279
4280 return 0;
4281 }
4282
4283 /* See breakpoint.h. */
4284
4285 int
4286 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4287 CORE_ADDR pc)
4288 {
4289 for (bp_location *bl : all_bp_locations_at_addr (pc))
4290 {
4291 if (bl->loc_type != bp_loc_hardware_breakpoint)
4292 continue;
4293
4294 if (bp_location_inserted_here_p (bl, aspace, pc))
4295 return 1;
4296 }
4297
4298 return 0;
4299 }
4300
4301 int
4302 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4303 CORE_ADDR addr, ULONGEST len)
4304 {
4305 for (breakpoint *bpt : all_breakpoints ())
4306 {
4307 if (bpt->type != bp_hardware_watchpoint
4308 && bpt->type != bp_access_watchpoint)
4309 continue;
4310
4311 if (!breakpoint_enabled (bpt))
4312 continue;
4313
4314 for (bp_location *loc : bpt->locations ())
4315 if (loc->pspace->aspace == aspace && loc->inserted)
4316 {
4317 CORE_ADDR l, h;
4318
4319 /* Check for intersection. */
4320 l = std::max<CORE_ADDR> (loc->address, addr);
4321 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4322 if (l < h)
4323 return 1;
4324 }
4325 }
4326 return 0;
4327 }
4328
4329 /* See breakpoint.h. */
4330
4331 bool
4332 is_catchpoint (struct breakpoint *b)
4333 {
4334 return (b->type == bp_catchpoint);
4335 }
4336
4337 /* Clear a bpstat so that it says we are not at any breakpoint.
4338 Also free any storage that is part of a bpstat. */
4339
4340 void
4341 bpstat_clear (bpstat **bsp)
4342 {
4343 bpstat *p;
4344 bpstat *q;
4345
4346 if (bsp == 0)
4347 return;
4348 p = *bsp;
4349 while (p != NULL)
4350 {
4351 q = p->next;
4352 delete p;
4353 p = q;
4354 }
4355 *bsp = NULL;
4356 }
4357
4358 bpstat::bpstat (const bpstat &other)
4359 : next (NULL),
4360 bp_location_at (other.bp_location_at),
4361 breakpoint_at (other.breakpoint_at),
4362 commands (other.commands),
4363 print (other.print),
4364 stop (other.stop),
4365 print_it (other.print_it)
4366 {
4367 if (other.old_val != NULL)
4368 old_val = release_value (value_copy (other.old_val.get ()));
4369 }
4370
4371 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4372 is part of the bpstat is copied as well. */
4373
4374 bpstat *
4375 bpstat_copy (bpstat *bs)
4376 {
4377 bpstat *p = nullptr;
4378 bpstat *tmp;
4379 bpstat *retval = nullptr;
4380
4381 if (bs == NULL)
4382 return bs;
4383
4384 for (; bs != NULL; bs = bs->next)
4385 {
4386 tmp = new bpstat (*bs);
4387
4388 if (p == NULL)
4389 /* This is the first thing in the chain. */
4390 retval = tmp;
4391 else
4392 p->next = tmp;
4393 p = tmp;
4394 }
4395 p->next = NULL;
4396 return retval;
4397 }
4398
4399 /* Find the bpstat associated with this breakpoint. */
4400
4401 bpstat *
4402 bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
4403 {
4404 if (bsp == NULL)
4405 return NULL;
4406
4407 for (; bsp != NULL; bsp = bsp->next)
4408 {
4409 if (bsp->breakpoint_at == breakpoint)
4410 return bsp;
4411 }
4412 return NULL;
4413 }
4414
4415 /* See breakpoint.h. */
4416
4417 bool
4418 bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
4419 {
4420 for (; bsp != NULL; bsp = bsp->next)
4421 {
4422 if (bsp->breakpoint_at == NULL)
4423 {
4424 /* A moribund location can never explain a signal other than
4425 GDB_SIGNAL_TRAP. */
4426 if (sig == GDB_SIGNAL_TRAP)
4427 return true;
4428 }
4429 else
4430 {
4431 if (bsp->breakpoint_at->explains_signal (sig))
4432 return true;
4433 }
4434 }
4435
4436 return false;
4437 }
4438
4439 /* Put in *NUM the breakpoint number of the first breakpoint we are
4440 stopped at. *BSP upon return is a bpstat which points to the
4441 remaining breakpoints stopped at (but which is not guaranteed to be
4442 good for anything but further calls to bpstat_num).
4443
4444 Return 0 if passed a bpstat which does not indicate any breakpoints.
4445 Return -1 if stopped at a breakpoint that has been deleted since
4446 we set it.
4447 Return 1 otherwise. */
4448
4449 int
4450 bpstat_num (bpstat **bsp, int *num)
4451 {
4452 struct breakpoint *b;
4453
4454 if ((*bsp) == NULL)
4455 return 0; /* No more breakpoint values */
4456
4457 /* We assume we'll never have several bpstats that correspond to a
4458 single breakpoint -- otherwise, this function might return the
4459 same number more than once and this will look ugly. */
4460 b = (*bsp)->breakpoint_at;
4461 *bsp = (*bsp)->next;
4462 if (b == NULL)
4463 return -1; /* breakpoint that's been deleted since */
4464
4465 *num = b->number; /* We have its number */
4466 return 1;
4467 }
4468
4469 /* See breakpoint.h. */
4470
4471 void
4472 bpstat_clear_actions (void)
4473 {
4474 bpstat *bs;
4475
4476 if (inferior_ptid == null_ptid)
4477 return;
4478
4479 thread_info *tp = inferior_thread ();
4480 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4481 {
4482 bs->commands = NULL;
4483 bs->old_val.reset (nullptr);
4484 }
4485 }
4486
4487 /* Called when a command is about to proceed the inferior. */
4488
4489 static void
4490 breakpoint_about_to_proceed (void)
4491 {
4492 if (inferior_ptid != null_ptid)
4493 {
4494 struct thread_info *tp = inferior_thread ();
4495
4496 /* Allow inferior function calls in breakpoint commands to not
4497 interrupt the command list. When the call finishes
4498 successfully, the inferior will be standing at the same
4499 breakpoint as if nothing happened. */
4500 if (tp->control.in_infcall)
4501 return;
4502 }
4503
4504 breakpoint_proceeded = 1;
4505 }
4506
4507 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4508 or its equivalent. */
4509
4510 static int
4511 command_line_is_silent (struct command_line *cmd)
4512 {
4513 return cmd && (strcmp ("silent", cmd->line) == 0);
4514 }
4515
4516 /* Execute all the commands associated with all the breakpoints at
4517 this location. Any of these commands could cause the process to
4518 proceed beyond this point, etc. We look out for such changes by
4519 checking the global "breakpoint_proceeded" after each command.
4520
4521 Returns true if a breakpoint command resumed the inferior. In that
4522 case, it is the caller's responsibility to recall it again with the
4523 bpstat of the current thread. */
4524
4525 static int
4526 bpstat_do_actions_1 (bpstat **bsp)
4527 {
4528 bpstat *bs;
4529 int again = 0;
4530
4531 /* Avoid endless recursion if a `source' command is contained
4532 in bs->commands. */
4533 if (executing_breakpoint_commands)
4534 return 0;
4535
4536 scoped_restore save_executing
4537 = make_scoped_restore (&executing_breakpoint_commands, 1);
4538
4539 scoped_restore preventer = prevent_dont_repeat ();
4540
4541 /* This pointer will iterate over the list of bpstat's. */
4542 bs = *bsp;
4543
4544 breakpoint_proceeded = 0;
4545 for (; bs != NULL; bs = bs->next)
4546 {
4547 struct command_line *cmd = NULL;
4548
4549 /* Take ownership of the BSP's command tree, if it has one.
4550
4551 The command tree could legitimately contain commands like
4552 'step' and 'next', which call clear_proceed_status, which
4553 frees stop_bpstat's command tree. To make sure this doesn't
4554 free the tree we're executing out from under us, we need to
4555 take ownership of the tree ourselves. Since a given bpstat's
4556 commands are only executed once, we don't need to copy it; we
4557 can clear the pointer in the bpstat, and make sure we free
4558 the tree when we're done. */
4559 counted_command_line ccmd = bs->commands;
4560 bs->commands = NULL;
4561 if (ccmd != NULL)
4562 cmd = ccmd.get ();
4563 if (command_line_is_silent (cmd))
4564 {
4565 /* The action has been already done by bpstat_stop_status. */
4566 cmd = cmd->next;
4567 }
4568
4569 while (cmd != NULL)
4570 {
4571 execute_control_command (cmd);
4572
4573 if (breakpoint_proceeded)
4574 break;
4575 else
4576 cmd = cmd->next;
4577 }
4578
4579 if (breakpoint_proceeded)
4580 {
4581 if (current_ui->async)
4582 /* If we are in async mode, then the target might be still
4583 running, not stopped at any breakpoint, so nothing for
4584 us to do here -- just return to the event loop. */
4585 ;
4586 else
4587 /* In sync mode, when execute_control_command returns
4588 we're already standing on the next breakpoint.
4589 Breakpoint commands for that stop were not run, since
4590 execute_command does not run breakpoint commands --
4591 only command_line_handler does, but that one is not
4592 involved in execution of breakpoint commands. So, we
4593 can now execute breakpoint commands. It should be
4594 noted that making execute_command do bpstat actions is
4595 not an option -- in this case we'll have recursive
4596 invocation of bpstat for each breakpoint with a
4597 command, and can easily blow up GDB stack. Instead, we
4598 return true, which will trigger the caller to recall us
4599 with the new stop_bpstat. */
4600 again = 1;
4601 break;
4602 }
4603 }
4604 return again;
4605 }
4606
4607 /* Helper for bpstat_do_actions. Get the current thread, if there's
4608 one, is alive and has execution. Return NULL otherwise. */
4609
4610 static thread_info *
4611 get_bpstat_thread ()
4612 {
4613 if (inferior_ptid == null_ptid || !target_has_execution ())
4614 return NULL;
4615
4616 thread_info *tp = inferior_thread ();
4617 if (tp->state == THREAD_EXITED || tp->executing ())
4618 return NULL;
4619 return tp;
4620 }
4621
4622 void
4623 bpstat_do_actions (void)
4624 {
4625 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4626 thread_info *tp;
4627
4628 /* Do any commands attached to breakpoint we are stopped at. */
4629 while ((tp = get_bpstat_thread ()) != NULL)
4630 {
4631 /* Since in sync mode, bpstat_do_actions may resume the
4632 inferior, and only return when it is stopped at the next
4633 breakpoint, we keep doing breakpoint actions until it returns
4634 false to indicate the inferior was not resumed. */
4635 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4636 break;
4637 }
4638
4639 cleanup_if_error.release ();
4640 }
4641
4642 /* Print out the (old or new) value associated with a watchpoint. */
4643
4644 static void
4645 watchpoint_value_print (struct value *val, struct ui_file *stream)
4646 {
4647 if (val == NULL)
4648 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4649 else
4650 {
4651 struct value_print_options opts;
4652 get_user_print_options (&opts);
4653 value_print (val, stream, &opts);
4654 }
4655 }
4656
4657 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4658 debugging multiple threads. */
4659
4660 void
4661 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4662 {
4663 if (uiout->is_mi_like_p ())
4664 return;
4665
4666 uiout->text ("\n");
4667
4668 if (show_thread_that_caused_stop ())
4669 {
4670 struct thread_info *thr = inferior_thread ();
4671
4672 uiout->text ("Thread ");
4673 uiout->field_string ("thread-id", print_thread_id (thr));
4674
4675 const char *name = thread_name (thr);
4676 if (name != NULL)
4677 {
4678 uiout->text (" \"");
4679 uiout->field_string ("name", name);
4680 uiout->text ("\"");
4681 }
4682
4683 uiout->text (" hit ");
4684 }
4685 }
4686
4687 /* Generic routine for printing messages indicating why we
4688 stopped. The behavior of this function depends on the value
4689 'print_it' in the bpstat structure. Under some circumstances we
4690 may decide not to print anything here and delegate the task to
4691 normal_stop(). */
4692
4693 static enum print_stop_action
4694 print_bp_stop_message (bpstat *bs)
4695 {
4696 switch (bs->print_it)
4697 {
4698 case print_it_noop:
4699 /* Nothing should be printed for this bpstat entry. */
4700 return PRINT_UNKNOWN;
4701 break;
4702
4703 case print_it_done:
4704 /* We still want to print the frame, but we already printed the
4705 relevant messages. */
4706 return PRINT_SRC_AND_LOC;
4707 break;
4708
4709 case print_it_normal:
4710 {
4711 struct breakpoint *b = bs->breakpoint_at;
4712
4713 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4714 which has since been deleted. */
4715 if (b == NULL)
4716 return PRINT_UNKNOWN;
4717
4718 /* Normal case. Call the breakpoint's print_it method. */
4719 return b->print_it (bs);
4720 }
4721 break;
4722
4723 default:
4724 internal_error (__FILE__, __LINE__,
4725 _("print_bp_stop_message: unrecognized enum value"));
4726 break;
4727 }
4728 }
4729
4730 /* See breakpoint.h. */
4731
4732 void
4733 print_solib_event (bool is_catchpoint)
4734 {
4735 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4736 bool any_added = !current_program_space->added_solibs.empty ();
4737
4738 if (!is_catchpoint)
4739 {
4740 if (any_added || any_deleted)
4741 current_uiout->text (_("Stopped due to shared library event:\n"));
4742 else
4743 current_uiout->text (_("Stopped due to shared library event (no "
4744 "libraries added or removed)\n"));
4745 }
4746
4747 if (current_uiout->is_mi_like_p ())
4748 current_uiout->field_string ("reason",
4749 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4750
4751 if (any_deleted)
4752 {
4753 current_uiout->text (_(" Inferior unloaded "));
4754 ui_out_emit_list list_emitter (current_uiout, "removed");
4755 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4756 {
4757 const std::string &name = current_program_space->deleted_solibs[ix];
4758
4759 if (ix > 0)
4760 current_uiout->text (" ");
4761 current_uiout->field_string ("library", name);
4762 current_uiout->text ("\n");
4763 }
4764 }
4765
4766 if (any_added)
4767 {
4768 current_uiout->text (_(" Inferior loaded "));
4769 ui_out_emit_list list_emitter (current_uiout, "added");
4770 bool first = true;
4771 for (so_list *iter : current_program_space->added_solibs)
4772 {
4773 if (!first)
4774 current_uiout->text (" ");
4775 first = false;
4776 current_uiout->field_string ("library", iter->so_name);
4777 current_uiout->text ("\n");
4778 }
4779 }
4780 }
4781
4782 /* Print a message indicating what happened. This is called from
4783 normal_stop(). The input to this routine is the head of the bpstat
4784 list - a list of the eventpoints that caused this stop. KIND is
4785 the target_waitkind for the stopping event. This
4786 routine calls the generic print routine for printing a message
4787 about reasons for stopping. This will print (for example) the
4788 "Breakpoint n," part of the output. The return value of this
4789 routine is one of:
4790
4791 PRINT_UNKNOWN: Means we printed nothing.
4792 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4793 code to print the location. An example is
4794 "Breakpoint 1, " which should be followed by
4795 the location.
4796 PRINT_SRC_ONLY: Means we printed something, but there is no need
4797 to also print the location part of the message.
4798 An example is the catch/throw messages, which
4799 don't require a location appended to the end.
4800 PRINT_NOTHING: We have done some printing and we don't need any
4801 further info to be printed. */
4802
4803 enum print_stop_action
4804 bpstat_print (bpstat *bs, int kind)
4805 {
4806 enum print_stop_action val;
4807
4808 /* Maybe another breakpoint in the chain caused us to stop.
4809 (Currently all watchpoints go on the bpstat whether hit or not.
4810 That probably could (should) be changed, provided care is taken
4811 with respect to bpstat_explains_signal). */
4812 for (; bs; bs = bs->next)
4813 {
4814 val = print_bp_stop_message (bs);
4815 if (val == PRINT_SRC_ONLY
4816 || val == PRINT_SRC_AND_LOC
4817 || val == PRINT_NOTHING)
4818 return val;
4819 }
4820
4821 /* If we had hit a shared library event breakpoint,
4822 print_bp_stop_message would print out this message. If we hit an
4823 OS-level shared library event, do the same thing. */
4824 if (kind == TARGET_WAITKIND_LOADED)
4825 {
4826 print_solib_event (false);
4827 return PRINT_NOTHING;
4828 }
4829
4830 /* We reached the end of the chain, or we got a null BS to start
4831 with and nothing was printed. */
4832 return PRINT_UNKNOWN;
4833 }
4834
4835 /* Evaluate the boolean expression EXP and return the result. */
4836
4837 static bool
4838 breakpoint_cond_eval (expression *exp)
4839 {
4840 struct value *mark = value_mark ();
4841 bool res = value_true (evaluate_expression (exp));
4842
4843 value_free_to_mark (mark);
4844 return res;
4845 }
4846
4847 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4848
4849 bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
4850 : next (NULL),
4851 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4852 breakpoint_at (bl->owner),
4853 commands (NULL),
4854 print (0),
4855 stop (0),
4856 print_it (print_it_normal)
4857 {
4858 **bs_link_pointer = this;
4859 *bs_link_pointer = &next;
4860 }
4861
4862 bpstat::bpstat ()
4863 : next (NULL),
4864 breakpoint_at (NULL),
4865 commands (NULL),
4866 print (0),
4867 stop (0),
4868 print_it (print_it_normal)
4869 {
4870 }
4871 \f
4872 /* The target has stopped with waitstatus WS. Check if any hardware
4873 watchpoints have triggered, according to the target. */
4874
4875 int
4876 watchpoints_triggered (const target_waitstatus &ws)
4877 {
4878 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4879 CORE_ADDR addr;
4880
4881 if (!stopped_by_watchpoint)
4882 {
4883 /* We were not stopped by a watchpoint. Mark all watchpoints
4884 as not triggered. */
4885 for (breakpoint *b : all_breakpoints ())
4886 if (is_hardware_watchpoint (b))
4887 {
4888 struct watchpoint *w = (struct watchpoint *) b;
4889
4890 w->watchpoint_triggered = watch_triggered_no;
4891 }
4892
4893 return 0;
4894 }
4895
4896 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4897 {
4898 /* We were stopped by a watchpoint, but we don't know where.
4899 Mark all watchpoints as unknown. */
4900 for (breakpoint *b : all_breakpoints ())
4901 if (is_hardware_watchpoint (b))
4902 {
4903 struct watchpoint *w = (struct watchpoint *) b;
4904
4905 w->watchpoint_triggered = watch_triggered_unknown;
4906 }
4907
4908 return 1;
4909 }
4910
4911 /* The target could report the data address. Mark watchpoints
4912 affected by this data address as triggered, and all others as not
4913 triggered. */
4914
4915 for (breakpoint *b : all_breakpoints ())
4916 if (is_hardware_watchpoint (b))
4917 {
4918 struct watchpoint *w = (struct watchpoint *) b;
4919
4920 w->watchpoint_triggered = watch_triggered_no;
4921 for (bp_location *loc : b->locations ())
4922 {
4923 if (is_masked_watchpoint (b))
4924 {
4925 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4926 CORE_ADDR start = loc->address & w->hw_wp_mask;
4927
4928 if (newaddr == start)
4929 {
4930 w->watchpoint_triggered = watch_triggered_yes;
4931 break;
4932 }
4933 }
4934 /* Exact match not required. Within range is sufficient. */
4935 else if (target_watchpoint_addr_within_range
4936 (current_inferior ()->top_target (), addr, loc->address,
4937 loc->length))
4938 {
4939 w->watchpoint_triggered = watch_triggered_yes;
4940 break;
4941 }
4942 }
4943 }
4944
4945 return 1;
4946 }
4947
4948 /* Possible return values for watchpoint_check. */
4949 enum wp_check_result
4950 {
4951 /* The watchpoint has been deleted. */
4952 WP_DELETED = 1,
4953
4954 /* The value has changed. */
4955 WP_VALUE_CHANGED = 2,
4956
4957 /* The value has not changed. */
4958 WP_VALUE_NOT_CHANGED = 3,
4959
4960 /* Ignore this watchpoint, no matter if the value changed or not. */
4961 WP_IGNORE = 4,
4962 };
4963
4964 #define BP_TEMPFLAG 1
4965 #define BP_HARDWAREFLAG 2
4966
4967 /* Evaluate watchpoint condition expression and check if its value
4968 changed. */
4969
4970 static wp_check_result
4971 watchpoint_check (bpstat *bs)
4972 {
4973 struct watchpoint *b;
4974 struct frame_info *fr;
4975 int within_current_scope;
4976
4977 /* BS is built from an existing struct breakpoint. */
4978 gdb_assert (bs->breakpoint_at != NULL);
4979 b = (struct watchpoint *) bs->breakpoint_at;
4980
4981 /* If this is a local watchpoint, we only want to check if the
4982 watchpoint frame is in scope if the current thread is the thread
4983 that was used to create the watchpoint. */
4984 if (!watchpoint_in_thread_scope (b))
4985 return WP_IGNORE;
4986
4987 if (b->exp_valid_block == NULL)
4988 within_current_scope = 1;
4989 else
4990 {
4991 struct frame_info *frame = get_current_frame ();
4992 struct gdbarch *frame_arch = get_frame_arch (frame);
4993 CORE_ADDR frame_pc = get_frame_pc (frame);
4994
4995 /* stack_frame_destroyed_p() returns a non-zero value if we're
4996 still in the function but the stack frame has already been
4997 invalidated. Since we can't rely on the values of local
4998 variables after the stack has been destroyed, we are treating
4999 the watchpoint in that state as `not changed' without further
5000 checking. Don't mark watchpoints as changed if the current
5001 frame is in an epilogue - even if they are in some other
5002 frame, our view of the stack is likely to be wrong and
5003 frame_find_by_id could error out. */
5004 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5005 return WP_IGNORE;
5006
5007 fr = frame_find_by_id (b->watchpoint_frame);
5008 within_current_scope = (fr != NULL);
5009
5010 /* If we've gotten confused in the unwinder, we might have
5011 returned a frame that can't describe this variable. */
5012 if (within_current_scope)
5013 {
5014 struct symbol *function;
5015
5016 function = get_frame_function (fr);
5017 if (function == NULL
5018 || !contained_in (b->exp_valid_block, function->value_block ()))
5019 within_current_scope = 0;
5020 }
5021
5022 if (within_current_scope)
5023 /* If we end up stopping, the current frame will get selected
5024 in normal_stop. So this call to select_frame won't affect
5025 the user. */
5026 select_frame (fr);
5027 }
5028
5029 if (within_current_scope)
5030 {
5031 /* We use value_{,free_to_}mark because it could be a *long*
5032 time before we return to the command level and call
5033 free_all_values. We can't call free_all_values because we
5034 might be in the middle of evaluating a function call. */
5035
5036 struct value *mark;
5037 struct value *new_val;
5038
5039 if (is_masked_watchpoint (b))
5040 /* Since we don't know the exact trigger address (from
5041 stopped_data_address), just tell the user we've triggered
5042 a mask watchpoint. */
5043 return WP_VALUE_CHANGED;
5044
5045 mark = value_mark ();
5046 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5047 NULL, NULL, false);
5048
5049 if (b->val_bitsize != 0)
5050 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5051
5052 /* We use value_equal_contents instead of value_equal because
5053 the latter coerces an array to a pointer, thus comparing just
5054 the address of the array instead of its contents. This is
5055 not what we want. */
5056 if ((b->val != NULL) != (new_val != NULL)
5057 || (b->val != NULL && !value_equal_contents (b->val.get (),
5058 new_val)))
5059 {
5060 bs->old_val = b->val;
5061 b->val = release_value (new_val);
5062 b->val_valid = true;
5063 if (new_val != NULL)
5064 value_free_to_mark (mark);
5065 return WP_VALUE_CHANGED;
5066 }
5067 else
5068 {
5069 /* Nothing changed. */
5070 value_free_to_mark (mark);
5071 return WP_VALUE_NOT_CHANGED;
5072 }
5073 }
5074 else
5075 {
5076 /* This seems like the only logical thing to do because
5077 if we temporarily ignored the watchpoint, then when
5078 we reenter the block in which it is valid it contains
5079 garbage (in the case of a function, it may have two
5080 garbage values, one before and one after the prologue).
5081 So we can't even detect the first assignment to it and
5082 watch after that (since the garbage may or may not equal
5083 the first value assigned). */
5084 /* We print all the stop information in
5085 breakpointprint_it, but in this case, by the time we
5086 call breakpoint->print_it this bp will be deleted
5087 already. So we have no choice but print the information
5088 here. */
5089
5090 SWITCH_THRU_ALL_UIS ()
5091 {
5092 struct ui_out *uiout = current_uiout;
5093
5094 if (uiout->is_mi_like_p ())
5095 uiout->field_string
5096 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5097 uiout->message ("\nWatchpoint %pF deleted because the program has "
5098 "left the block in\n"
5099 "which its expression is valid.\n",
5100 signed_field ("wpnum", b->number));
5101 }
5102
5103 /* Make sure the watchpoint's commands aren't executed. */
5104 b->commands = NULL;
5105 watchpoint_del_at_next_stop (b);
5106
5107 return WP_DELETED;
5108 }
5109 }
5110
5111 /* Return true if it looks like target has stopped due to hitting
5112 breakpoint location BL. This function does not check if we should
5113 stop, only if BL explains the stop. */
5114
5115 static int
5116 bpstat_check_location (const struct bp_location *bl,
5117 const address_space *aspace, CORE_ADDR bp_addr,
5118 const target_waitstatus &ws)
5119 {
5120 struct breakpoint *b = bl->owner;
5121
5122 /* BL is from an existing breakpoint. */
5123 gdb_assert (b != NULL);
5124
5125 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
5126 }
5127
5128 /* Determine if the watched values have actually changed, and we
5129 should stop. If not, set BS->stop to 0. */
5130
5131 static void
5132 bpstat_check_watchpoint (bpstat *bs)
5133 {
5134 const struct bp_location *bl;
5135 struct watchpoint *b;
5136
5137 /* BS is built for existing struct breakpoint. */
5138 bl = bs->bp_location_at.get ();
5139 gdb_assert (bl != NULL);
5140 b = (struct watchpoint *) bs->breakpoint_at;
5141 gdb_assert (b != NULL);
5142
5143 {
5144 int must_check_value = 0;
5145
5146 if (b->type == bp_watchpoint)
5147 /* For a software watchpoint, we must always check the
5148 watched value. */
5149 must_check_value = 1;
5150 else if (b->watchpoint_triggered == watch_triggered_yes)
5151 /* We have a hardware watchpoint (read, write, or access)
5152 and the target earlier reported an address watched by
5153 this watchpoint. */
5154 must_check_value = 1;
5155 else if (b->watchpoint_triggered == watch_triggered_unknown
5156 && b->type == bp_hardware_watchpoint)
5157 /* We were stopped by a hardware watchpoint, but the target could
5158 not report the data address. We must check the watchpoint's
5159 value. Access and read watchpoints are out of luck; without
5160 a data address, we can't figure it out. */
5161 must_check_value = 1;
5162
5163 if (must_check_value)
5164 {
5165 wp_check_result e;
5166
5167 try
5168 {
5169 e = watchpoint_check (bs);
5170 }
5171 catch (const gdb_exception &ex)
5172 {
5173 exception_fprintf (gdb_stderr, ex,
5174 "Error evaluating expression "
5175 "for watchpoint %d\n",
5176 b->number);
5177
5178 SWITCH_THRU_ALL_UIS ()
5179 {
5180 gdb_printf (_("Watchpoint %d deleted.\n"),
5181 b->number);
5182 }
5183 watchpoint_del_at_next_stop (b);
5184 e = WP_DELETED;
5185 }
5186
5187 switch (e)
5188 {
5189 case WP_DELETED:
5190 /* We've already printed what needs to be printed. */
5191 bs->print_it = print_it_done;
5192 /* Stop. */
5193 break;
5194 case WP_IGNORE:
5195 bs->print_it = print_it_noop;
5196 bs->stop = 0;
5197 break;
5198 case WP_VALUE_CHANGED:
5199 if (b->type == bp_read_watchpoint)
5200 {
5201 /* There are two cases to consider here:
5202
5203 1. We're watching the triggered memory for reads.
5204 In that case, trust the target, and always report
5205 the watchpoint hit to the user. Even though
5206 reads don't cause value changes, the value may
5207 have changed since the last time it was read, and
5208 since we're not trapping writes, we will not see
5209 those, and as such we should ignore our notion of
5210 old value.
5211
5212 2. We're watching the triggered memory for both
5213 reads and writes. There are two ways this may
5214 happen:
5215
5216 2.1. This is a target that can't break on data
5217 reads only, but can break on accesses (reads or
5218 writes), such as e.g., x86. We detect this case
5219 at the time we try to insert read watchpoints.
5220
5221 2.2. Otherwise, the target supports read
5222 watchpoints, but, the user set an access or write
5223 watchpoint watching the same memory as this read
5224 watchpoint.
5225
5226 If we're watching memory writes as well as reads,
5227 ignore watchpoint hits when we find that the
5228 value hasn't changed, as reads don't cause
5229 changes. This still gives false positives when
5230 the program writes the same value to memory as
5231 what there was already in memory (we will confuse
5232 it for a read), but it's much better than
5233 nothing. */
5234
5235 int other_write_watchpoint = 0;
5236
5237 if (bl->watchpoint_type == hw_read)
5238 {
5239 for (breakpoint *other_b : all_breakpoints ())
5240 if (other_b->type == bp_hardware_watchpoint
5241 || other_b->type == bp_access_watchpoint)
5242 {
5243 struct watchpoint *other_w =
5244 (struct watchpoint *) other_b;
5245
5246 if (other_w->watchpoint_triggered
5247 == watch_triggered_yes)
5248 {
5249 other_write_watchpoint = 1;
5250 break;
5251 }
5252 }
5253 }
5254
5255 if (other_write_watchpoint
5256 || bl->watchpoint_type == hw_access)
5257 {
5258 /* We're watching the same memory for writes,
5259 and the value changed since the last time we
5260 updated it, so this trap must be for a write.
5261 Ignore it. */
5262 bs->print_it = print_it_noop;
5263 bs->stop = 0;
5264 }
5265 }
5266 break;
5267 case WP_VALUE_NOT_CHANGED:
5268 if (b->type == bp_hardware_watchpoint
5269 || b->type == bp_watchpoint)
5270 {
5271 /* Don't stop: write watchpoints shouldn't fire if
5272 the value hasn't changed. */
5273 bs->print_it = print_it_noop;
5274 bs->stop = 0;
5275 }
5276 /* Stop. */
5277 break;
5278 default:
5279 /* Can't happen. */
5280 break;
5281 }
5282 }
5283 else /* must_check_value == 0 */
5284 {
5285 /* This is a case where some watchpoint(s) triggered, but
5286 not at the address of this watchpoint, or else no
5287 watchpoint triggered after all. So don't print
5288 anything for this watchpoint. */
5289 bs->print_it = print_it_noop;
5290 bs->stop = 0;
5291 }
5292 }
5293 }
5294
5295 /* For breakpoints that are currently marked as telling gdb to stop,
5296 check conditions (condition proper, frame, thread and ignore count)
5297 of breakpoint referred to by BS. If we should not stop for this
5298 breakpoint, set BS->stop to 0. */
5299
5300 static void
5301 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5302 {
5303 const struct bp_location *bl;
5304 struct breakpoint *b;
5305 /* Assume stop. */
5306 bool condition_result = true;
5307 struct expression *cond;
5308
5309 gdb_assert (bs->stop);
5310
5311 /* BS is built for existing struct breakpoint. */
5312 bl = bs->bp_location_at.get ();
5313 gdb_assert (bl != NULL);
5314 b = bs->breakpoint_at;
5315 gdb_assert (b != NULL);
5316
5317 /* Even if the target evaluated the condition on its end and notified GDB, we
5318 need to do so again since GDB does not know if we stopped due to a
5319 breakpoint or a single step breakpoint. */
5320
5321 if (frame_id_p (b->frame_id)
5322 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5323 {
5324 bs->stop = 0;
5325 return;
5326 }
5327
5328 /* If this is a thread/task-specific breakpoint, don't waste cpu
5329 evaluating the condition if this isn't the specified
5330 thread/task. */
5331 if ((b->thread != -1 && b->thread != thread->global_num)
5332 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5333 {
5334 bs->stop = 0;
5335 return;
5336 }
5337
5338 /* Evaluate extension language breakpoints that have a "stop" method
5339 implemented. */
5340 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5341
5342 if (is_watchpoint (b))
5343 {
5344 struct watchpoint *w = (struct watchpoint *) b;
5345
5346 cond = w->cond_exp.get ();
5347 }
5348 else
5349 cond = bl->cond.get ();
5350
5351 if (cond && b->disposition != disp_del_at_next_stop)
5352 {
5353 int within_current_scope = 1;
5354 struct watchpoint * w;
5355
5356 /* We use value_mark and value_free_to_mark because it could
5357 be a long time before we return to the command level and
5358 call free_all_values. We can't call free_all_values
5359 because we might be in the middle of evaluating a
5360 function call. */
5361 struct value *mark = value_mark ();
5362
5363 if (is_watchpoint (b))
5364 w = (struct watchpoint *) b;
5365 else
5366 w = NULL;
5367
5368 /* Need to select the frame, with all that implies so that
5369 the conditions will have the right context. Because we
5370 use the frame, we will not see an inlined function's
5371 variables when we arrive at a breakpoint at the start
5372 of the inlined function; the current frame will be the
5373 call site. */
5374 if (w == NULL || w->cond_exp_valid_block == NULL)
5375 select_frame (get_current_frame ());
5376 else
5377 {
5378 struct frame_info *frame;
5379
5380 /* For local watchpoint expressions, which particular
5381 instance of a local is being watched matters, so we
5382 keep track of the frame to evaluate the expression
5383 in. To evaluate the condition however, it doesn't
5384 really matter which instantiation of the function
5385 where the condition makes sense triggers the
5386 watchpoint. This allows an expression like "watch
5387 global if q > 10" set in `func', catch writes to
5388 global on all threads that call `func', or catch
5389 writes on all recursive calls of `func' by a single
5390 thread. We simply always evaluate the condition in
5391 the innermost frame that's executing where it makes
5392 sense to evaluate the condition. It seems
5393 intuitive. */
5394 frame = block_innermost_frame (w->cond_exp_valid_block);
5395 if (frame != NULL)
5396 select_frame (frame);
5397 else
5398 within_current_scope = 0;
5399 }
5400 if (within_current_scope)
5401 {
5402 try
5403 {
5404 condition_result = breakpoint_cond_eval (cond);
5405 }
5406 catch (const gdb_exception &ex)
5407 {
5408 exception_fprintf (gdb_stderr, ex,
5409 "Error in testing breakpoint condition:\n");
5410 }
5411 }
5412 else
5413 {
5414 warning (_("Watchpoint condition cannot be tested "
5415 "in the current scope"));
5416 /* If we failed to set the right context for this
5417 watchpoint, unconditionally report it. */
5418 }
5419 /* FIXME-someday, should give breakpoint #. */
5420 value_free_to_mark (mark);
5421 }
5422
5423 if (cond && !condition_result)
5424 {
5425 bs->stop = 0;
5426 }
5427 else if (b->ignore_count > 0)
5428 {
5429 b->ignore_count--;
5430 bs->stop = 0;
5431 /* Increase the hit count even though we don't stop. */
5432 ++(b->hit_count);
5433 gdb::observers::breakpoint_modified.notify (b);
5434 }
5435 }
5436
5437 /* Returns true if we need to track moribund locations of LOC's type
5438 on the current target. */
5439
5440 static int
5441 need_moribund_for_location_type (struct bp_location *loc)
5442 {
5443 return ((loc->loc_type == bp_loc_software_breakpoint
5444 && !target_supports_stopped_by_sw_breakpoint ())
5445 || (loc->loc_type == bp_loc_hardware_breakpoint
5446 && !target_supports_stopped_by_hw_breakpoint ()));
5447 }
5448
5449 /* See breakpoint.h. */
5450
5451 bpstat *
5452 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5453 const target_waitstatus &ws)
5454 {
5455 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5456
5457 for (breakpoint *b : all_breakpoints ())
5458 {
5459 if (!breakpoint_enabled (b))
5460 continue;
5461
5462 for (bp_location *bl : b->locations ())
5463 {
5464 /* For hardware watchpoints, we look only at the first
5465 location. The watchpoint_check function will work on the
5466 entire expression, not the individual locations. For
5467 read watchpoints, the watchpoints_triggered function has
5468 checked all locations already. */
5469 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5470 break;
5471
5472 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5473 continue;
5474
5475 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5476 continue;
5477
5478 /* Come here if it's a watchpoint, or if the break address
5479 matches. */
5480
5481 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
5482 explain stop. */
5483
5484 /* Assume we stop. Should we find a watchpoint that is not
5485 actually triggered, or if the condition of the breakpoint
5486 evaluates as false, we'll reset 'stop' to 0. */
5487 bs->stop = 1;
5488 bs->print = 1;
5489
5490 /* If this is a scope breakpoint, mark the associated
5491 watchpoint as triggered so that we will handle the
5492 out-of-scope event. We'll get to the watchpoint next
5493 iteration. */
5494 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5495 {
5496 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5497
5498 w->watchpoint_triggered = watch_triggered_yes;
5499 }
5500 }
5501 }
5502
5503 /* Check if a moribund breakpoint explains the stop. */
5504 if (!target_supports_stopped_by_sw_breakpoint ()
5505 || !target_supports_stopped_by_hw_breakpoint ())
5506 {
5507 for (bp_location *loc : moribund_locations)
5508 {
5509 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5510 && need_moribund_for_location_type (loc))
5511 {
5512 bpstat *bs = new bpstat (loc, &bs_link);
5513 /* For hits of moribund locations, we should just proceed. */
5514 bs->stop = 0;
5515 bs->print = 0;
5516 bs->print_it = print_it_noop;
5517 }
5518 }
5519 }
5520
5521 return bs_head;
5522 }
5523
5524 /* See breakpoint.h. */
5525
5526 bpstat *
5527 bpstat_stop_status (const address_space *aspace,
5528 CORE_ADDR bp_addr, thread_info *thread,
5529 const target_waitstatus &ws,
5530 bpstat *stop_chain)
5531 {
5532 struct breakpoint *b = NULL;
5533 /* First item of allocated bpstat's. */
5534 bpstat *bs_head = stop_chain;
5535 bpstat *bs;
5536 int need_remove_insert;
5537 int removed_any;
5538
5539 /* First, build the bpstat chain with locations that explain a
5540 target stop, while being careful to not set the target running,
5541 as that may invalidate locations (in particular watchpoint
5542 locations are recreated). Resuming will happen here with
5543 breakpoint conditions or watchpoint expressions that include
5544 inferior function calls. */
5545 if (bs_head == NULL)
5546 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5547
5548 /* A bit of special processing for shlib breakpoints. We need to
5549 process solib loading here, so that the lists of loaded and
5550 unloaded libraries are correct before we handle "catch load" and
5551 "catch unload". */
5552 for (bs = bs_head; bs != NULL; bs = bs->next)
5553 {
5554 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5555 {
5556 handle_solib_event ();
5557 break;
5558 }
5559 }
5560
5561 /* Now go through the locations that caused the target to stop, and
5562 check whether we're interested in reporting this stop to higher
5563 layers, or whether we should resume the target transparently. */
5564
5565 removed_any = 0;
5566
5567 for (bs = bs_head; bs != NULL; bs = bs->next)
5568 {
5569 if (!bs->stop)
5570 continue;
5571
5572 b = bs->breakpoint_at;
5573 b->check_status (bs);
5574 if (bs->stop)
5575 {
5576 bpstat_check_breakpoint_conditions (bs, thread);
5577
5578 if (bs->stop)
5579 {
5580 ++(b->hit_count);
5581
5582 /* We will stop here. */
5583 if (b->disposition == disp_disable)
5584 {
5585 --(b->enable_count);
5586 if (b->enable_count <= 0)
5587 b->enable_state = bp_disabled;
5588 removed_any = 1;
5589 }
5590 gdb::observers::breakpoint_modified.notify (b);
5591 if (b->silent)
5592 bs->print = 0;
5593 bs->commands = b->commands;
5594 if (command_line_is_silent (bs->commands
5595 ? bs->commands.get () : NULL))
5596 bs->print = 0;
5597
5598 b->after_condition_true (bs);
5599 }
5600
5601 }
5602
5603 /* Print nothing for this entry if we don't stop or don't
5604 print. */
5605 if (!bs->stop || !bs->print)
5606 bs->print_it = print_it_noop;
5607 }
5608
5609 /* If we aren't stopping, the value of some hardware watchpoint may
5610 not have changed, but the intermediate memory locations we are
5611 watching may have. Don't bother if we're stopping; this will get
5612 done later. */
5613 need_remove_insert = 0;
5614 if (! bpstat_causes_stop (bs_head))
5615 for (bs = bs_head; bs != NULL; bs = bs->next)
5616 if (!bs->stop
5617 && bs->breakpoint_at
5618 && is_hardware_watchpoint (bs->breakpoint_at))
5619 {
5620 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5621
5622 update_watchpoint (w, 0 /* don't reparse. */);
5623 need_remove_insert = 1;
5624 }
5625
5626 if (need_remove_insert)
5627 update_global_location_list (UGLL_MAY_INSERT);
5628 else if (removed_any)
5629 update_global_location_list (UGLL_DONT_INSERT);
5630
5631 return bs_head;
5632 }
5633
5634 /* See breakpoint.h. */
5635
5636 bpstat *
5637 bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5638 thread_info *thread, const target_waitstatus &ws)
5639 {
5640 gdb_assert (!target_stopped_by_watchpoint ());
5641
5642 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5643 previous stop to avoid confusing bpstat_stop_status. */
5644 watchpoints_triggered (ws);
5645
5646 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5647 }
5648
5649 static void
5650 handle_jit_event (CORE_ADDR address)
5651 {
5652 struct gdbarch *gdbarch;
5653
5654 infrun_debug_printf ("handling bp_jit_event");
5655
5656 /* Switch terminal for any messages produced by
5657 breakpoint_re_set. */
5658 target_terminal::ours_for_output ();
5659
5660 gdbarch = get_frame_arch (get_current_frame ());
5661 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5662 thus it is expected that its objectfile can be found through
5663 minimal symbol lookup. If it doesn't work (and assert fails), it
5664 most likely means that `jit_breakpoint_re_set` was changes and this
5665 function needs to be updated too. */
5666 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5667 gdb_assert (jit_bp_sym.objfile != nullptr);
5668 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5669
5670 target_terminal::inferior ();
5671 }
5672
5673 /* Prepare WHAT final decision for infrun. */
5674
5675 /* Decide what infrun needs to do with this bpstat. */
5676
5677 struct bpstat_what
5678 bpstat_what (bpstat *bs_head)
5679 {
5680 struct bpstat_what retval;
5681 bpstat *bs;
5682
5683 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5684 retval.call_dummy = STOP_NONE;
5685 retval.is_longjmp = false;
5686
5687 for (bs = bs_head; bs != NULL; bs = bs->next)
5688 {
5689 /* Extract this BS's action. After processing each BS, we check
5690 if its action overrides all we've seem so far. */
5691 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5692 enum bptype bptype;
5693
5694 if (bs->breakpoint_at == NULL)
5695 {
5696 /* I suspect this can happen if it was a momentary
5697 breakpoint which has since been deleted. */
5698 bptype = bp_none;
5699 }
5700 else
5701 bptype = bs->breakpoint_at->type;
5702
5703 switch (bptype)
5704 {
5705 case bp_none:
5706 break;
5707 case bp_breakpoint:
5708 case bp_hardware_breakpoint:
5709 case bp_single_step:
5710 case bp_until:
5711 case bp_finish:
5712 case bp_shlib_event:
5713 if (bs->stop)
5714 {
5715 if (bs->print)
5716 this_action = BPSTAT_WHAT_STOP_NOISY;
5717 else
5718 this_action = BPSTAT_WHAT_STOP_SILENT;
5719 }
5720 else
5721 this_action = BPSTAT_WHAT_SINGLE;
5722 break;
5723 case bp_watchpoint:
5724 case bp_hardware_watchpoint:
5725 case bp_read_watchpoint:
5726 case bp_access_watchpoint:
5727 if (bs->stop)
5728 {
5729 if (bs->print)
5730 this_action = BPSTAT_WHAT_STOP_NOISY;
5731 else
5732 this_action = BPSTAT_WHAT_STOP_SILENT;
5733 }
5734 else
5735 {
5736 /* There was a watchpoint, but we're not stopping.
5737 This requires no further action. */
5738 }
5739 break;
5740 case bp_longjmp:
5741 case bp_longjmp_call_dummy:
5742 case bp_exception:
5743 if (bs->stop)
5744 {
5745 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5746 retval.is_longjmp = bptype != bp_exception;
5747 }
5748 else
5749 this_action = BPSTAT_WHAT_SINGLE;
5750 break;
5751 case bp_longjmp_resume:
5752 case bp_exception_resume:
5753 if (bs->stop)
5754 {
5755 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5756 retval.is_longjmp = bptype == bp_longjmp_resume;
5757 }
5758 else
5759 this_action = BPSTAT_WHAT_SINGLE;
5760 break;
5761 case bp_step_resume:
5762 if (bs->stop)
5763 this_action = BPSTAT_WHAT_STEP_RESUME;
5764 else
5765 {
5766 /* It is for the wrong frame. */
5767 this_action = BPSTAT_WHAT_SINGLE;
5768 }
5769 break;
5770 case bp_hp_step_resume:
5771 if (bs->stop)
5772 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5773 else
5774 {
5775 /* It is for the wrong frame. */
5776 this_action = BPSTAT_WHAT_SINGLE;
5777 }
5778 break;
5779 case bp_watchpoint_scope:
5780 case bp_thread_event:
5781 case bp_overlay_event:
5782 case bp_longjmp_master:
5783 case bp_std_terminate_master:
5784 case bp_exception_master:
5785 this_action = BPSTAT_WHAT_SINGLE;
5786 break;
5787 case bp_catchpoint:
5788 if (bs->stop)
5789 {
5790 if (bs->print)
5791 this_action = BPSTAT_WHAT_STOP_NOISY;
5792 else
5793 this_action = BPSTAT_WHAT_STOP_SILENT;
5794 }
5795 else
5796 {
5797 /* Some catchpoints are implemented with breakpoints.
5798 For those, we need to step over the breakpoint. */
5799 if (bs->bp_location_at->loc_type != bp_loc_other)
5800 this_action = BPSTAT_WHAT_SINGLE;
5801 }
5802 break;
5803 case bp_jit_event:
5804 this_action = BPSTAT_WHAT_SINGLE;
5805 break;
5806 case bp_call_dummy:
5807 /* Make sure the action is stop (silent or noisy),
5808 so infrun.c pops the dummy frame. */
5809 retval.call_dummy = STOP_STACK_DUMMY;
5810 this_action = BPSTAT_WHAT_STOP_SILENT;
5811 break;
5812 case bp_std_terminate:
5813 /* Make sure the action is stop (silent or noisy),
5814 so infrun.c pops the dummy frame. */
5815 retval.call_dummy = STOP_STD_TERMINATE;
5816 this_action = BPSTAT_WHAT_STOP_SILENT;
5817 break;
5818 case bp_tracepoint:
5819 case bp_fast_tracepoint:
5820 case bp_static_tracepoint:
5821 case bp_static_marker_tracepoint:
5822 /* Tracepoint hits should not be reported back to GDB, and
5823 if one got through somehow, it should have been filtered
5824 out already. */
5825 internal_error (__FILE__, __LINE__,
5826 _("bpstat_what: tracepoint encountered"));
5827 break;
5828 case bp_gnu_ifunc_resolver:
5829 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5830 this_action = BPSTAT_WHAT_SINGLE;
5831 break;
5832 case bp_gnu_ifunc_resolver_return:
5833 /* The breakpoint will be removed, execution will restart from the
5834 PC of the former breakpoint. */
5835 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5836 break;
5837
5838 case bp_dprintf:
5839 if (bs->stop)
5840 this_action = BPSTAT_WHAT_STOP_SILENT;
5841 else
5842 this_action = BPSTAT_WHAT_SINGLE;
5843 break;
5844
5845 default:
5846 internal_error (__FILE__, __LINE__,
5847 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5848 }
5849
5850 retval.main_action = std::max (retval.main_action, this_action);
5851 }
5852
5853 return retval;
5854 }
5855
5856 void
5857 bpstat_run_callbacks (bpstat *bs_head)
5858 {
5859 bpstat *bs;
5860
5861 for (bs = bs_head; bs != NULL; bs = bs->next)
5862 {
5863 struct breakpoint *b = bs->breakpoint_at;
5864
5865 if (b == NULL)
5866 continue;
5867 switch (b->type)
5868 {
5869 case bp_jit_event:
5870 handle_jit_event (bs->bp_location_at->address);
5871 break;
5872 case bp_gnu_ifunc_resolver:
5873 gnu_ifunc_resolver_stop (b);
5874 break;
5875 case bp_gnu_ifunc_resolver_return:
5876 gnu_ifunc_resolver_return_stop (b);
5877 break;
5878 }
5879 }
5880 }
5881
5882 /* See breakpoint.h. */
5883
5884 bool
5885 bpstat_should_step ()
5886 {
5887 for (breakpoint *b : all_breakpoints ())
5888 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5889 return true;
5890
5891 return false;
5892 }
5893
5894 /* See breakpoint.h. */
5895
5896 bool
5897 bpstat_causes_stop (bpstat *bs)
5898 {
5899 for (; bs != NULL; bs = bs->next)
5900 if (bs->stop)
5901 return true;
5902
5903 return false;
5904 }
5905
5906 \f
5907
5908 /* Compute a number of spaces suitable to indent the next line
5909 so it starts at the position corresponding to the table column
5910 named COL_NAME in the currently active table of UIOUT. */
5911
5912 static int
5913 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5914 {
5915 int i, total_width, width, align;
5916 const char *text;
5917
5918 total_width = 0;
5919 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5920 {
5921 if (strcmp (text, col_name) == 0)
5922 return total_width;
5923
5924 total_width += width + 1;
5925 }
5926
5927 return 0;
5928 }
5929
5930 /* Determine if the locations of this breakpoint will have their conditions
5931 evaluated by the target, host or a mix of both. Returns the following:
5932
5933 "host": Host evals condition.
5934 "host or target": Host or Target evals condition.
5935 "target": Target evals condition.
5936 */
5937
5938 static const char *
5939 bp_condition_evaluator (const breakpoint *b)
5940 {
5941 char host_evals = 0;
5942 char target_evals = 0;
5943
5944 if (!b)
5945 return NULL;
5946
5947 if (!is_breakpoint (b))
5948 return NULL;
5949
5950 if (gdb_evaluates_breakpoint_condition_p ()
5951 || !target_supports_evaluation_of_breakpoint_conditions ())
5952 return condition_evaluation_host;
5953
5954 for (bp_location *bl : b->locations ())
5955 {
5956 if (bl->cond_bytecode)
5957 target_evals++;
5958 else
5959 host_evals++;
5960 }
5961
5962 if (host_evals && target_evals)
5963 return condition_evaluation_both;
5964 else if (target_evals)
5965 return condition_evaluation_target;
5966 else
5967 return condition_evaluation_host;
5968 }
5969
5970 /* Determine the breakpoint location's condition evaluator. This is
5971 similar to bp_condition_evaluator, but for locations. */
5972
5973 static const char *
5974 bp_location_condition_evaluator (struct bp_location *bl)
5975 {
5976 if (bl && !is_breakpoint (bl->owner))
5977 return NULL;
5978
5979 if (gdb_evaluates_breakpoint_condition_p ()
5980 || !target_supports_evaluation_of_breakpoint_conditions ())
5981 return condition_evaluation_host;
5982
5983 if (bl && bl->cond_bytecode)
5984 return condition_evaluation_target;
5985 else
5986 return condition_evaluation_host;
5987 }
5988
5989 /* Print the LOC location out of the list of B->LOC locations. */
5990
5991 static void
5992 print_breakpoint_location (const breakpoint *b,
5993 struct bp_location *loc)
5994 {
5995 struct ui_out *uiout = current_uiout;
5996
5997 scoped_restore_current_program_space restore_pspace;
5998
5999 if (loc != NULL && loc->shlib_disabled)
6000 loc = NULL;
6001
6002 if (loc != NULL)
6003 set_current_program_space (loc->pspace);
6004
6005 if (b->display_canonical)
6006 uiout->field_string ("what", event_location_to_string (b->location.get ()));
6007 else if (loc && loc->symtab)
6008 {
6009 const struct symbol *sym = loc->symbol;
6010
6011 if (sym)
6012 {
6013 uiout->text ("in ");
6014 uiout->field_string ("func", sym->print_name (),
6015 function_name_style.style ());
6016 uiout->text (" ");
6017 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6018 uiout->text ("at ");
6019 }
6020 uiout->field_string ("file",
6021 symtab_to_filename_for_display (loc->symtab),
6022 file_name_style.style ());
6023 uiout->text (":");
6024
6025 if (uiout->is_mi_like_p ())
6026 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6027
6028 uiout->field_signed ("line", loc->line_number);
6029 }
6030 else if (loc)
6031 {
6032 string_file stb;
6033
6034 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6035 demangle, "");
6036 uiout->field_stream ("at", stb);
6037 }
6038 else
6039 {
6040 uiout->field_string ("pending",
6041 event_location_to_string (b->location.get ()));
6042 /* If extra_string is available, it could be holding a condition
6043 or dprintf arguments. In either case, make sure it is printed,
6044 too, but only for non-MI streams. */
6045 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6046 {
6047 if (b->type == bp_dprintf)
6048 uiout->text (",");
6049 else
6050 uiout->text (" ");
6051 uiout->text (b->extra_string.get ());
6052 }
6053 }
6054
6055 if (loc && is_breakpoint (b)
6056 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6057 && bp_condition_evaluator (b) == condition_evaluation_both)
6058 {
6059 uiout->text (" (");
6060 uiout->field_string ("evaluated-by",
6061 bp_location_condition_evaluator (loc));
6062 uiout->text (")");
6063 }
6064 }
6065
6066 static const char *
6067 bptype_string (enum bptype type)
6068 {
6069 struct ep_type_description
6070 {
6071 enum bptype type;
6072 const char *description;
6073 };
6074 static struct ep_type_description bptypes[] =
6075 {
6076 {bp_none, "?deleted?"},
6077 {bp_breakpoint, "breakpoint"},
6078 {bp_hardware_breakpoint, "hw breakpoint"},
6079 {bp_single_step, "sw single-step"},
6080 {bp_until, "until"},
6081 {bp_finish, "finish"},
6082 {bp_watchpoint, "watchpoint"},
6083 {bp_hardware_watchpoint, "hw watchpoint"},
6084 {bp_read_watchpoint, "read watchpoint"},
6085 {bp_access_watchpoint, "acc watchpoint"},
6086 {bp_longjmp, "longjmp"},
6087 {bp_longjmp_resume, "longjmp resume"},
6088 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6089 {bp_exception, "exception"},
6090 {bp_exception_resume, "exception resume"},
6091 {bp_step_resume, "step resume"},
6092 {bp_hp_step_resume, "high-priority step resume"},
6093 {bp_watchpoint_scope, "watchpoint scope"},
6094 {bp_call_dummy, "call dummy"},
6095 {bp_std_terminate, "std::terminate"},
6096 {bp_shlib_event, "shlib events"},
6097 {bp_thread_event, "thread events"},
6098 {bp_overlay_event, "overlay events"},
6099 {bp_longjmp_master, "longjmp master"},
6100 {bp_std_terminate_master, "std::terminate master"},
6101 {bp_exception_master, "exception master"},
6102 {bp_catchpoint, "catchpoint"},
6103 {bp_tracepoint, "tracepoint"},
6104 {bp_fast_tracepoint, "fast tracepoint"},
6105 {bp_static_tracepoint, "static tracepoint"},
6106 {bp_static_marker_tracepoint, "static marker tracepoint"},
6107 {bp_dprintf, "dprintf"},
6108 {bp_jit_event, "jit events"},
6109 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6110 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6111 };
6112
6113 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6114 || ((int) type != bptypes[(int) type].type))
6115 internal_error (__FILE__, __LINE__,
6116 _("bptypes table does not describe type #%d."),
6117 (int) type);
6118
6119 return bptypes[(int) type].description;
6120 }
6121
6122 /* For MI, output a field named 'thread-groups' with a list as the value.
6123 For CLI, prefix the list with the string 'inf'. */
6124
6125 static void
6126 output_thread_groups (struct ui_out *uiout,
6127 const char *field_name,
6128 const std::vector<int> &inf_nums,
6129 int mi_only)
6130 {
6131 int is_mi = uiout->is_mi_like_p ();
6132
6133 /* For backward compatibility, don't display inferiors in CLI unless
6134 there are several. Always display them for MI. */
6135 if (!is_mi && mi_only)
6136 return;
6137
6138 ui_out_emit_list list_emitter (uiout, field_name);
6139
6140 for (size_t i = 0; i < inf_nums.size (); i++)
6141 {
6142 if (is_mi)
6143 {
6144 char mi_group[10];
6145
6146 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6147 uiout->field_string (NULL, mi_group);
6148 }
6149 else
6150 {
6151 if (i == 0)
6152 uiout->text (" inf ");
6153 else
6154 uiout->text (", ");
6155
6156 uiout->text (plongest (inf_nums[i]));
6157 }
6158 }
6159 }
6160
6161 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6162 instead of going via breakpoint_ops::print_one. This makes "maint
6163 info breakpoints" show the software breakpoint locations of
6164 catchpoints, which are considered internal implementation
6165 detail. Returns true if RAW_LOC is false and if the breakpoint's
6166 print_one method did something; false otherwise. */
6167
6168 static bool
6169 print_one_breakpoint_location (struct breakpoint *b,
6170 struct bp_location *loc,
6171 int loc_number,
6172 struct bp_location **last_loc,
6173 int allflag, bool raw_loc)
6174 {
6175 struct command_line *l;
6176 static char bpenables[] = "nynny";
6177
6178 struct ui_out *uiout = current_uiout;
6179 int header_of_multiple = 0;
6180 int part_of_multiple = (loc != NULL);
6181 struct value_print_options opts;
6182
6183 get_user_print_options (&opts);
6184
6185 gdb_assert (!loc || loc_number != 0);
6186 /* See comment in print_one_breakpoint concerning treatment of
6187 breakpoints with single disabled location. */
6188 if (loc == NULL
6189 && (b->loc != NULL
6190 && (b->loc->next != NULL
6191 || !b->loc->enabled || b->loc->disabled_by_cond)))
6192 header_of_multiple = 1;
6193 if (loc == NULL)
6194 loc = b->loc;
6195
6196 annotate_record ();
6197
6198 /* 1 */
6199 annotate_field (0);
6200 if (part_of_multiple)
6201 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6202 else
6203 uiout->field_signed ("number", b->number);
6204
6205 /* 2 */
6206 annotate_field (1);
6207 if (part_of_multiple)
6208 uiout->field_skip ("type");
6209 else
6210 uiout->field_string ("type", bptype_string (b->type));
6211
6212 /* 3 */
6213 annotate_field (2);
6214 if (part_of_multiple)
6215 uiout->field_skip ("disp");
6216 else
6217 uiout->field_string ("disp", bpdisp_text (b->disposition));
6218
6219 /* 4 */
6220 annotate_field (3);
6221 /* For locations that are disabled because of an invalid condition,
6222 display "N*" on CLI, where "*" refers to a footnote below the
6223 table. For MI, simply display a "N" without a footnote. */
6224 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
6225 if (part_of_multiple)
6226 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
6227 : (loc->enabled ? "y" : "n")));
6228 else
6229 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6230
6231 /* 5 and 6 */
6232 bool result = false;
6233 if (!raw_loc && b->print_one (last_loc))
6234 result = true;
6235 else
6236 {
6237 if (is_watchpoint (b))
6238 {
6239 struct watchpoint *w = (struct watchpoint *) b;
6240
6241 /* Field 4, the address, is omitted (which makes the columns
6242 not line up too nicely with the headers, but the effect
6243 is relatively readable). */
6244 if (opts.addressprint)
6245 uiout->field_skip ("addr");
6246 annotate_field (5);
6247 uiout->field_string ("what", w->exp_string.get ());
6248 }
6249 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6250 || is_ada_exception_catchpoint (b))
6251 {
6252 if (opts.addressprint)
6253 {
6254 annotate_field (4);
6255 if (header_of_multiple)
6256 uiout->field_string ("addr", "<MULTIPLE>",
6257 metadata_style.style ());
6258 else if (b->loc == NULL || loc->shlib_disabled)
6259 uiout->field_string ("addr", "<PENDING>",
6260 metadata_style.style ());
6261 else
6262 uiout->field_core_addr ("addr",
6263 loc->gdbarch, loc->address);
6264 }
6265 annotate_field (5);
6266 if (!header_of_multiple)
6267 print_breakpoint_location (b, loc);
6268 if (b->loc)
6269 *last_loc = b->loc;
6270 }
6271 }
6272
6273 if (loc != NULL && !header_of_multiple)
6274 {
6275 std::vector<int> inf_nums;
6276 int mi_only = 1;
6277
6278 for (inferior *inf : all_inferiors ())
6279 {
6280 if (inf->pspace == loc->pspace)
6281 inf_nums.push_back (inf->num);
6282 }
6283
6284 /* For backward compatibility, don't display inferiors in CLI unless
6285 there are several. Always display for MI. */
6286 if (allflag
6287 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6288 && (program_spaces.size () > 1
6289 || number_of_inferiors () > 1)
6290 /* LOC is for existing B, it cannot be in
6291 moribund_locations and thus having NULL OWNER. */
6292 && loc->owner->type != bp_catchpoint))
6293 mi_only = 0;
6294 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6295 }
6296
6297 if (!part_of_multiple)
6298 {
6299 if (b->thread != -1)
6300 {
6301 /* FIXME: This seems to be redundant and lost here; see the
6302 "stop only in" line a little further down. */
6303 uiout->text (" thread ");
6304 uiout->field_signed ("thread", b->thread);
6305 }
6306 else if (b->task != 0)
6307 {
6308 uiout->text (" task ");
6309 uiout->field_signed ("task", b->task);
6310 }
6311 }
6312
6313 uiout->text ("\n");
6314
6315 if (!part_of_multiple)
6316 b->print_one_detail (uiout);
6317
6318 if (part_of_multiple && frame_id_p (b->frame_id))
6319 {
6320 annotate_field (6);
6321 uiout->text ("\tstop only in stack frame at ");
6322 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6323 the frame ID. */
6324 uiout->field_core_addr ("frame",
6325 b->gdbarch, b->frame_id.stack_addr);
6326 uiout->text ("\n");
6327 }
6328
6329 if (!part_of_multiple && b->cond_string)
6330 {
6331 annotate_field (7);
6332 if (is_tracepoint (b))
6333 uiout->text ("\ttrace only if ");
6334 else
6335 uiout->text ("\tstop only if ");
6336 uiout->field_string ("cond", b->cond_string.get ());
6337
6338 /* Print whether the target is doing the breakpoint's condition
6339 evaluation. If GDB is doing the evaluation, don't print anything. */
6340 if (is_breakpoint (b)
6341 && breakpoint_condition_evaluation_mode ()
6342 == condition_evaluation_target)
6343 {
6344 uiout->message (" (%pF evals)",
6345 string_field ("evaluated-by",
6346 bp_condition_evaluator (b)));
6347 }
6348 uiout->text ("\n");
6349 }
6350
6351 if (!part_of_multiple && b->thread != -1)
6352 {
6353 /* FIXME should make an annotation for this. */
6354 uiout->text ("\tstop only in thread ");
6355 if (uiout->is_mi_like_p ())
6356 uiout->field_signed ("thread", b->thread);
6357 else
6358 {
6359 struct thread_info *thr = find_thread_global_id (b->thread);
6360
6361 uiout->field_string ("thread", print_thread_id (thr));
6362 }
6363 uiout->text ("\n");
6364 }
6365
6366 if (!part_of_multiple)
6367 {
6368 if (b->hit_count)
6369 {
6370 /* FIXME should make an annotation for this. */
6371 if (is_catchpoint (b))
6372 uiout->text ("\tcatchpoint");
6373 else if (is_tracepoint (b))
6374 uiout->text ("\ttracepoint");
6375 else
6376 uiout->text ("\tbreakpoint");
6377 uiout->text (" already hit ");
6378 uiout->field_signed ("times", b->hit_count);
6379 if (b->hit_count == 1)
6380 uiout->text (" time\n");
6381 else
6382 uiout->text (" times\n");
6383 }
6384 else
6385 {
6386 /* Output the count also if it is zero, but only if this is mi. */
6387 if (uiout->is_mi_like_p ())
6388 uiout->field_signed ("times", b->hit_count);
6389 }
6390 }
6391
6392 if (!part_of_multiple && b->ignore_count)
6393 {
6394 annotate_field (8);
6395 uiout->message ("\tignore next %pF hits\n",
6396 signed_field ("ignore", b->ignore_count));
6397 }
6398
6399 /* Note that an enable count of 1 corresponds to "enable once"
6400 behavior, which is reported by the combination of enablement and
6401 disposition, so we don't need to mention it here. */
6402 if (!part_of_multiple && b->enable_count > 1)
6403 {
6404 annotate_field (8);
6405 uiout->text ("\tdisable after ");
6406 /* Tweak the wording to clarify that ignore and enable counts
6407 are distinct, and have additive effect. */
6408 if (b->ignore_count)
6409 uiout->text ("additional ");
6410 else
6411 uiout->text ("next ");
6412 uiout->field_signed ("enable", b->enable_count);
6413 uiout->text (" hits\n");
6414 }
6415
6416 if (!part_of_multiple && is_tracepoint (b))
6417 {
6418 struct tracepoint *tp = (struct tracepoint *) b;
6419
6420 if (tp->traceframe_usage)
6421 {
6422 uiout->text ("\ttrace buffer usage ");
6423 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6424 uiout->text (" bytes\n");
6425 }
6426 }
6427
6428 l = b->commands ? b->commands.get () : NULL;
6429 if (!part_of_multiple && l)
6430 {
6431 annotate_field (9);
6432 ui_out_emit_tuple tuple_emitter (uiout, "script");
6433 print_command_lines (uiout, l, 4);
6434 }
6435
6436 if (is_tracepoint (b))
6437 {
6438 struct tracepoint *t = (struct tracepoint *) b;
6439
6440 if (!part_of_multiple && t->pass_count)
6441 {
6442 annotate_field (10);
6443 uiout->text ("\tpass count ");
6444 uiout->field_signed ("pass", t->pass_count);
6445 uiout->text (" \n");
6446 }
6447
6448 /* Don't display it when tracepoint or tracepoint location is
6449 pending. */
6450 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6451 {
6452 annotate_field (11);
6453
6454 if (uiout->is_mi_like_p ())
6455 uiout->field_string ("installed",
6456 loc->inserted ? "y" : "n");
6457 else
6458 {
6459 if (loc->inserted)
6460 uiout->text ("\t");
6461 else
6462 uiout->text ("\tnot ");
6463 uiout->text ("installed on target\n");
6464 }
6465 }
6466 }
6467
6468 if (uiout->is_mi_like_p () && !part_of_multiple)
6469 {
6470 if (is_watchpoint (b))
6471 {
6472 struct watchpoint *w = (struct watchpoint *) b;
6473
6474 uiout->field_string ("original-location", w->exp_string.get ());
6475 }
6476 else if (b->location != NULL
6477 && event_location_to_string (b->location.get ()) != NULL)
6478 uiout->field_string ("original-location",
6479 event_location_to_string (b->location.get ()));
6480 }
6481
6482 return result;
6483 }
6484
6485 /* See breakpoint.h. */
6486
6487 bool fix_multi_location_breakpoint_output_globally = false;
6488
6489 static void
6490 print_one_breakpoint (struct breakpoint *b,
6491 struct bp_location **last_loc,
6492 int allflag)
6493 {
6494 struct ui_out *uiout = current_uiout;
6495 bool use_fixed_output
6496 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6497 || fix_multi_location_breakpoint_output_globally);
6498
6499 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6500 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6501 allflag, false);
6502
6503 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6504 are outside. */
6505 if (!use_fixed_output)
6506 bkpt_tuple_emitter.reset ();
6507
6508 /* If this breakpoint has custom print function,
6509 it's already printed. Otherwise, print individual
6510 locations, if any. */
6511 if (!printed || allflag)
6512 {
6513 /* If breakpoint has a single location that is disabled, we
6514 print it as if it had several locations, since otherwise it's
6515 hard to represent "breakpoint enabled, location disabled"
6516 situation.
6517
6518 Note that while hardware watchpoints have several locations
6519 internally, that's not a property exposed to users.
6520
6521 Likewise, while catchpoints may be implemented with
6522 breakpoints (e.g., catch throw), that's not a property
6523 exposed to users. We do however display the internal
6524 breakpoint locations with "maint info breakpoints". */
6525 if (!is_hardware_watchpoint (b)
6526 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6527 || is_ada_exception_catchpoint (b))
6528 && (allflag
6529 || (b->loc && (b->loc->next
6530 || !b->loc->enabled
6531 || b->loc->disabled_by_cond))))
6532 {
6533 gdb::optional<ui_out_emit_list> locations_list;
6534
6535 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6536 MI record. For later versions, place breakpoint locations in a
6537 list. */
6538 if (uiout->is_mi_like_p () && use_fixed_output)
6539 locations_list.emplace (uiout, "locations");
6540
6541 int n = 1;
6542 for (bp_location *loc : b->locations ())
6543 {
6544 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6545 print_one_breakpoint_location (b, loc, n, last_loc,
6546 allflag, allflag);
6547 n++;
6548 }
6549 }
6550 }
6551 }
6552
6553 static int
6554 breakpoint_address_bits (struct breakpoint *b)
6555 {
6556 int print_address_bits = 0;
6557
6558 /* Software watchpoints that aren't watching memory don't have an
6559 address to print. */
6560 if (is_no_memory_software_watchpoint (b))
6561 return 0;
6562
6563 for (bp_location *loc : b->locations ())
6564 {
6565 int addr_bit;
6566
6567 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6568 if (addr_bit > print_address_bits)
6569 print_address_bits = addr_bit;
6570 }
6571
6572 return print_address_bits;
6573 }
6574
6575 /* See breakpoint.h. */
6576
6577 void
6578 print_breakpoint (breakpoint *b)
6579 {
6580 struct bp_location *dummy_loc = NULL;
6581 print_one_breakpoint (b, &dummy_loc, 0);
6582 }
6583
6584 /* Return true if this breakpoint was set by the user, false if it is
6585 internal or momentary. */
6586
6587 int
6588 user_breakpoint_p (struct breakpoint *b)
6589 {
6590 return b->number > 0;
6591 }
6592
6593 /* See breakpoint.h. */
6594
6595 int
6596 pending_breakpoint_p (struct breakpoint *b)
6597 {
6598 return b->loc == NULL;
6599 }
6600
6601 /* Print information on breakpoints (including watchpoints and tracepoints).
6602
6603 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6604 understood by number_or_range_parser. Only breakpoints included in this
6605 list are then printed.
6606
6607 If SHOW_INTERNAL is true, print internal breakpoints.
6608
6609 If FILTER is non-NULL, call it on each breakpoint and only include the
6610 ones for which it returns true.
6611
6612 Return the total number of breakpoints listed. */
6613
6614 static int
6615 breakpoint_1 (const char *bp_num_list, bool show_internal,
6616 bool (*filter) (const struct breakpoint *))
6617 {
6618 struct bp_location *last_loc = NULL;
6619 int nr_printable_breakpoints;
6620 struct value_print_options opts;
6621 int print_address_bits = 0;
6622 int print_type_col_width = 14;
6623 struct ui_out *uiout = current_uiout;
6624 bool has_disabled_by_cond_location = false;
6625
6626 get_user_print_options (&opts);
6627
6628 /* Compute the number of rows in the table, as well as the size
6629 required for address fields. */
6630 nr_printable_breakpoints = 0;
6631 for (breakpoint *b : all_breakpoints ())
6632 {
6633 /* If we have a filter, only list the breakpoints it accepts. */
6634 if (filter && !filter (b))
6635 continue;
6636
6637 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6638 accept. Skip the others. */
6639 if (bp_num_list != NULL && *bp_num_list != '\0')
6640 {
6641 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6642 continue;
6643 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6644 continue;
6645 }
6646
6647 if (show_internal || user_breakpoint_p (b))
6648 {
6649 int addr_bit, type_len;
6650
6651 addr_bit = breakpoint_address_bits (b);
6652 if (addr_bit > print_address_bits)
6653 print_address_bits = addr_bit;
6654
6655 type_len = strlen (bptype_string (b->type));
6656 if (type_len > print_type_col_width)
6657 print_type_col_width = type_len;
6658
6659 nr_printable_breakpoints++;
6660 }
6661 }
6662
6663 {
6664 ui_out_emit_table table_emitter (uiout,
6665 opts.addressprint ? 6 : 5,
6666 nr_printable_breakpoints,
6667 "BreakpointTable");
6668
6669 if (nr_printable_breakpoints > 0)
6670 annotate_breakpoints_headers ();
6671 if (nr_printable_breakpoints > 0)
6672 annotate_field (0);
6673 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6674 if (nr_printable_breakpoints > 0)
6675 annotate_field (1);
6676 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6677 if (nr_printable_breakpoints > 0)
6678 annotate_field (2);
6679 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6680 if (nr_printable_breakpoints > 0)
6681 annotate_field (3);
6682 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6683 if (opts.addressprint)
6684 {
6685 if (nr_printable_breakpoints > 0)
6686 annotate_field (4);
6687 if (print_address_bits <= 32)
6688 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6689 else
6690 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6691 }
6692 if (nr_printable_breakpoints > 0)
6693 annotate_field (5);
6694 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6695 uiout->table_body ();
6696 if (nr_printable_breakpoints > 0)
6697 annotate_breakpoints_table ();
6698
6699 for (breakpoint *b : all_breakpoints ())
6700 {
6701 QUIT;
6702 /* If we have a filter, only list the breakpoints it accepts. */
6703 if (filter && !filter (b))
6704 continue;
6705
6706 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6707 accept. Skip the others. */
6708
6709 if (bp_num_list != NULL && *bp_num_list != '\0')
6710 {
6711 if (show_internal) /* maintenance info breakpoint */
6712 {
6713 if (parse_and_eval_long (bp_num_list) != b->number)
6714 continue;
6715 }
6716 else /* all others */
6717 {
6718 if (!number_is_in_list (bp_num_list, b->number))
6719 continue;
6720 }
6721 }
6722 /* We only print out user settable breakpoints unless the
6723 show_internal is set. */
6724 if (show_internal || user_breakpoint_p (b))
6725 {
6726 print_one_breakpoint (b, &last_loc, show_internal);
6727 for (bp_location *loc : b->locations ())
6728 if (loc->disabled_by_cond)
6729 has_disabled_by_cond_location = true;
6730 }
6731 }
6732 }
6733
6734 if (nr_printable_breakpoints == 0)
6735 {
6736 /* If there's a filter, let the caller decide how to report
6737 empty list. */
6738 if (!filter)
6739 {
6740 if (bp_num_list == NULL || *bp_num_list == '\0')
6741 uiout->message ("No breakpoints or watchpoints.\n");
6742 else
6743 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6744 bp_num_list);
6745 }
6746 }
6747 else
6748 {
6749 if (last_loc && !server_command)
6750 set_next_address (last_loc->gdbarch, last_loc->address);
6751
6752 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6753 uiout->message (_("(*): Breakpoint condition is invalid at this "
6754 "location.\n"));
6755 }
6756
6757 /* FIXME? Should this be moved up so that it is only called when
6758 there have been breakpoints? */
6759 annotate_breakpoints_table_end ();
6760
6761 return nr_printable_breakpoints;
6762 }
6763
6764 /* Display the value of default-collect in a way that is generally
6765 compatible with the breakpoint list. */
6766
6767 static void
6768 default_collect_info (void)
6769 {
6770 struct ui_out *uiout = current_uiout;
6771
6772 /* If it has no value (which is frequently the case), say nothing; a
6773 message like "No default-collect." gets in user's face when it's
6774 not wanted. */
6775 if (default_collect.empty ())
6776 return;
6777
6778 /* The following phrase lines up nicely with per-tracepoint collect
6779 actions. */
6780 uiout->text ("default collect ");
6781 uiout->field_string ("default-collect", default_collect);
6782 uiout->text (" \n");
6783 }
6784
6785 static void
6786 info_breakpoints_command (const char *args, int from_tty)
6787 {
6788 breakpoint_1 (args, false, NULL);
6789
6790 default_collect_info ();
6791 }
6792
6793 static void
6794 info_watchpoints_command (const char *args, int from_tty)
6795 {
6796 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6797 struct ui_out *uiout = current_uiout;
6798
6799 if (num_printed == 0)
6800 {
6801 if (args == NULL || *args == '\0')
6802 uiout->message ("No watchpoints.\n");
6803 else
6804 uiout->message ("No watchpoint matching '%s'.\n", args);
6805 }
6806 }
6807
6808 static void
6809 maintenance_info_breakpoints (const char *args, int from_tty)
6810 {
6811 breakpoint_1 (args, true, NULL);
6812
6813 default_collect_info ();
6814 }
6815
6816 static int
6817 breakpoint_has_pc (struct breakpoint *b,
6818 struct program_space *pspace,
6819 CORE_ADDR pc, struct obj_section *section)
6820 {
6821 for (bp_location *bl : b->locations ())
6822 {
6823 if (bl->pspace == pspace
6824 && bl->address == pc
6825 && (!overlay_debugging || bl->section == section))
6826 return 1;
6827 }
6828 return 0;
6829 }
6830
6831 /* Print a message describing any user-breakpoints set at PC. This
6832 concerns with logical breakpoints, so we match program spaces, not
6833 address spaces. */
6834
6835 static void
6836 describe_other_breakpoints (struct gdbarch *gdbarch,
6837 struct program_space *pspace, CORE_ADDR pc,
6838 struct obj_section *section, int thread)
6839 {
6840 int others = 0;
6841
6842 for (breakpoint *b : all_breakpoints ())
6843 others += (user_breakpoint_p (b)
6844 && breakpoint_has_pc (b, pspace, pc, section));
6845
6846 if (others > 0)
6847 {
6848 if (others == 1)
6849 gdb_printf (_("Note: breakpoint "));
6850 else /* if (others == ???) */
6851 gdb_printf (_("Note: breakpoints "));
6852 for (breakpoint *b : all_breakpoints ())
6853 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6854 {
6855 others--;
6856 gdb_printf ("%d", b->number);
6857 if (b->thread == -1 && thread != -1)
6858 gdb_printf (" (all threads)");
6859 else if (b->thread != -1)
6860 gdb_printf (" (thread %d)", b->thread);
6861 gdb_printf ("%s%s ",
6862 ((b->enable_state == bp_disabled
6863 || b->enable_state == bp_call_disabled)
6864 ? " (disabled)"
6865 : ""),
6866 (others > 1) ? ","
6867 : ((others == 1) ? " and" : ""));
6868 }
6869 current_uiout->message (_("also set at pc %ps.\n"),
6870 styled_string (address_style.style (),
6871 paddress (gdbarch, pc)));
6872 }
6873 }
6874 \f
6875
6876 /* Return true iff it is meaningful to use the address member of LOC.
6877 For some breakpoint types, the locations' address members are
6878 irrelevant and it makes no sense to attempt to compare them to
6879 other addresses (or use them for any other purpose either).
6880
6881 More specifically, software watchpoints and catchpoints that are
6882 not backed by breakpoints always have a zero valued location
6883 address and we don't want to mark breakpoints of any of these types
6884 to be a duplicate of an actual breakpoint location at address
6885 zero. */
6886
6887 static bool
6888 bl_address_is_meaningful (bp_location *loc)
6889 {
6890 return loc->loc_type != bp_loc_other;
6891 }
6892
6893 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6894 true if LOC1 and LOC2 represent the same watchpoint location. */
6895
6896 static int
6897 watchpoint_locations_match (struct bp_location *loc1,
6898 struct bp_location *loc2)
6899 {
6900 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6901 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6902
6903 /* Both of them must exist. */
6904 gdb_assert (w1 != NULL);
6905 gdb_assert (w2 != NULL);
6906
6907 /* If the target can evaluate the condition expression in hardware,
6908 then we we need to insert both watchpoints even if they are at
6909 the same place. Otherwise the watchpoint will only trigger when
6910 the condition of whichever watchpoint was inserted evaluates to
6911 true, not giving a chance for GDB to check the condition of the
6912 other watchpoint. */
6913 if ((w1->cond_exp
6914 && target_can_accel_watchpoint_condition (loc1->address,
6915 loc1->length,
6916 loc1->watchpoint_type,
6917 w1->cond_exp.get ()))
6918 || (w2->cond_exp
6919 && target_can_accel_watchpoint_condition (loc2->address,
6920 loc2->length,
6921 loc2->watchpoint_type,
6922 w2->cond_exp.get ())))
6923 return 0;
6924
6925 /* Note that this checks the owner's type, not the location's. In
6926 case the target does not support read watchpoints, but does
6927 support access watchpoints, we'll have bp_read_watchpoint
6928 watchpoints with hw_access locations. Those should be considered
6929 duplicates of hw_read locations. The hw_read locations will
6930 become hw_access locations later. */
6931 return (loc1->owner->type == loc2->owner->type
6932 && loc1->pspace->aspace == loc2->pspace->aspace
6933 && loc1->address == loc2->address
6934 && loc1->length == loc2->length);
6935 }
6936
6937 /* See breakpoint.h. */
6938
6939 int
6940 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6941 const address_space *aspace2, CORE_ADDR addr2)
6942 {
6943 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6944 || aspace1 == aspace2)
6945 && addr1 == addr2);
6946 }
6947
6948 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6949 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6950 matches ASPACE2. On targets that have global breakpoints, the address
6951 space doesn't really matter. */
6952
6953 static int
6954 breakpoint_address_match_range (const address_space *aspace1,
6955 CORE_ADDR addr1,
6956 int len1, const address_space *aspace2,
6957 CORE_ADDR addr2)
6958 {
6959 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6960 || aspace1 == aspace2)
6961 && addr2 >= addr1 && addr2 < addr1 + len1);
6962 }
6963
6964 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6965 a ranged breakpoint. In most targets, a match happens only if ASPACE
6966 matches the breakpoint's address space. On targets that have global
6967 breakpoints, the address space doesn't really matter. */
6968
6969 static int
6970 breakpoint_location_address_match (struct bp_location *bl,
6971 const address_space *aspace,
6972 CORE_ADDR addr)
6973 {
6974 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6975 aspace, addr)
6976 || (bl->length
6977 && breakpoint_address_match_range (bl->pspace->aspace,
6978 bl->address, bl->length,
6979 aspace, addr)));
6980 }
6981
6982 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6983 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6984 match happens only if ASPACE matches the breakpoint's address
6985 space. On targets that have global breakpoints, the address space
6986 doesn't really matter. */
6987
6988 static int
6989 breakpoint_location_address_range_overlap (struct bp_location *bl,
6990 const address_space *aspace,
6991 CORE_ADDR addr, int len)
6992 {
6993 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6994 || bl->pspace->aspace == aspace)
6995 {
6996 int bl_len = bl->length != 0 ? bl->length : 1;
6997
6998 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6999 return 1;
7000 }
7001 return 0;
7002 }
7003
7004 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7005 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7006 true, otherwise returns false. */
7007
7008 static int
7009 tracepoint_locations_match (struct bp_location *loc1,
7010 struct bp_location *loc2)
7011 {
7012 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7013 /* Since tracepoint locations are never duplicated with others', tracepoint
7014 locations at the same address of different tracepoints are regarded as
7015 different locations. */
7016 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7017 else
7018 return 0;
7019 }
7020
7021 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7022 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7023 the same location. If SW_HW_BPS_MATCH is true, then software
7024 breakpoint locations and hardware breakpoint locations match,
7025 otherwise they don't. */
7026
7027 static int
7028 breakpoint_locations_match (struct bp_location *loc1,
7029 struct bp_location *loc2,
7030 bool sw_hw_bps_match)
7031 {
7032 int hw_point1, hw_point2;
7033
7034 /* Both of them must not be in moribund_locations. */
7035 gdb_assert (loc1->owner != NULL);
7036 gdb_assert (loc2->owner != NULL);
7037
7038 hw_point1 = is_hardware_watchpoint (loc1->owner);
7039 hw_point2 = is_hardware_watchpoint (loc2->owner);
7040
7041 if (hw_point1 != hw_point2)
7042 return 0;
7043 else if (hw_point1)
7044 return watchpoint_locations_match (loc1, loc2);
7045 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7046 return tracepoint_locations_match (loc1, loc2);
7047 else
7048 /* We compare bp_location.length in order to cover ranged
7049 breakpoints. Keep this in sync with
7050 bp_location_is_less_than. */
7051 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7052 loc2->pspace->aspace, loc2->address)
7053 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7054 && loc1->length == loc2->length);
7055 }
7056
7057 static void
7058 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7059 int bnum, int have_bnum)
7060 {
7061 /* The longest string possibly returned by hex_string_custom
7062 is 50 chars. These must be at least that big for safety. */
7063 char astr1[64];
7064 char astr2[64];
7065
7066 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7067 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7068 if (have_bnum)
7069 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7070 bnum, astr1, astr2);
7071 else
7072 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7073 }
7074
7075 /* Adjust a breakpoint's address to account for architectural
7076 constraints on breakpoint placement. Return the adjusted address.
7077 Note: Very few targets require this kind of adjustment. For most
7078 targets, this function is simply the identity function. */
7079
7080 static CORE_ADDR
7081 adjust_breakpoint_address (struct gdbarch *gdbarch,
7082 CORE_ADDR bpaddr, enum bptype bptype)
7083 {
7084 if (bptype == bp_watchpoint
7085 || bptype == bp_hardware_watchpoint
7086 || bptype == bp_read_watchpoint
7087 || bptype == bp_access_watchpoint
7088 || bptype == bp_catchpoint)
7089 {
7090 /* Watchpoints and the various bp_catch_* eventpoints should not
7091 have their addresses modified. */
7092 return bpaddr;
7093 }
7094 else if (bptype == bp_single_step)
7095 {
7096 /* Single-step breakpoints should not have their addresses
7097 modified. If there's any architectural constrain that
7098 applies to this address, then it should have already been
7099 taken into account when the breakpoint was created in the
7100 first place. If we didn't do this, stepping through e.g.,
7101 Thumb-2 IT blocks would break. */
7102 return bpaddr;
7103 }
7104 else
7105 {
7106 CORE_ADDR adjusted_bpaddr = bpaddr;
7107
7108 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7109 {
7110 /* Some targets have architectural constraints on the placement
7111 of breakpoint instructions. Obtain the adjusted address. */
7112 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7113 }
7114
7115 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7116
7117 /* An adjusted breakpoint address can significantly alter
7118 a user's expectations. Print a warning if an adjustment
7119 is required. */
7120 if (adjusted_bpaddr != bpaddr)
7121 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7122
7123 return adjusted_bpaddr;
7124 }
7125 }
7126
7127 static bp_loc_type
7128 bp_location_from_bp_type (bptype type)
7129 {
7130 switch (type)
7131 {
7132 case bp_breakpoint:
7133 case bp_single_step:
7134 case bp_until:
7135 case bp_finish:
7136 case bp_longjmp:
7137 case bp_longjmp_resume:
7138 case bp_longjmp_call_dummy:
7139 case bp_exception:
7140 case bp_exception_resume:
7141 case bp_step_resume:
7142 case bp_hp_step_resume:
7143 case bp_watchpoint_scope:
7144 case bp_call_dummy:
7145 case bp_std_terminate:
7146 case bp_shlib_event:
7147 case bp_thread_event:
7148 case bp_overlay_event:
7149 case bp_jit_event:
7150 case bp_longjmp_master:
7151 case bp_std_terminate_master:
7152 case bp_exception_master:
7153 case bp_gnu_ifunc_resolver:
7154 case bp_gnu_ifunc_resolver_return:
7155 case bp_dprintf:
7156 return bp_loc_software_breakpoint;
7157 case bp_hardware_breakpoint:
7158 return bp_loc_hardware_breakpoint;
7159 case bp_hardware_watchpoint:
7160 case bp_read_watchpoint:
7161 case bp_access_watchpoint:
7162 return bp_loc_hardware_watchpoint;
7163 case bp_watchpoint:
7164 case bp_catchpoint:
7165 case bp_tracepoint:
7166 case bp_fast_tracepoint:
7167 case bp_static_tracepoint:
7168 case bp_static_marker_tracepoint:
7169 return bp_loc_other;
7170 default:
7171 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7172 }
7173 }
7174
7175 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7176 {
7177 this->owner = owner;
7178 this->cond_bytecode = NULL;
7179 this->shlib_disabled = 0;
7180 this->enabled = 1;
7181 this->disabled_by_cond = false;
7182
7183 this->loc_type = type;
7184
7185 if (this->loc_type == bp_loc_software_breakpoint
7186 || this->loc_type == bp_loc_hardware_breakpoint)
7187 mark_breakpoint_location_modified (this);
7188
7189 incref ();
7190 }
7191
7192 bp_location::bp_location (breakpoint *owner)
7193 : bp_location::bp_location (owner,
7194 bp_location_from_bp_type (owner->type))
7195 {
7196 }
7197
7198 /* Decrement reference count. If the reference count reaches 0,
7199 destroy the bp_location. Sets *BLP to NULL. */
7200
7201 static void
7202 decref_bp_location (struct bp_location **blp)
7203 {
7204 bp_location_ref_policy::decref (*blp);
7205 *blp = NULL;
7206 }
7207
7208 /* Add breakpoint B at the end of the global breakpoint chain. */
7209
7210 static breakpoint *
7211 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7212 {
7213 struct breakpoint *b1;
7214 struct breakpoint *result = b.get ();
7215
7216 /* Add this breakpoint to the end of the chain so that a list of
7217 breakpoints will come out in order of increasing numbers. */
7218
7219 b1 = breakpoint_chain;
7220 if (b1 == 0)
7221 breakpoint_chain = b.release ();
7222 else
7223 {
7224 while (b1->next)
7225 b1 = b1->next;
7226 b1->next = b.release ();
7227 }
7228
7229 return result;
7230 }
7231
7232 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7233 that has type BPTYPE and has no locations as yet. */
7234
7235 static struct breakpoint *
7236 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7237 enum bptype bptype)
7238 {
7239 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (gdbarch, bptype);
7240
7241 return add_to_breakpoint_chain (std::move (b));
7242 }
7243
7244 /* Initialize loc->function_name. */
7245
7246 static void
7247 set_breakpoint_location_function (struct bp_location *loc)
7248 {
7249 gdb_assert (loc->owner != NULL);
7250
7251 if (loc->owner->type == bp_breakpoint
7252 || loc->owner->type == bp_hardware_breakpoint
7253 || is_tracepoint (loc->owner))
7254 {
7255 const char *function_name;
7256
7257 if (loc->msymbol != NULL
7258 && (loc->msymbol->type () == mst_text_gnu_ifunc
7259 || loc->msymbol->type () == mst_data_gnu_ifunc))
7260 {
7261 struct breakpoint *b = loc->owner;
7262
7263 function_name = loc->msymbol->linkage_name ();
7264
7265 if (b->type == bp_breakpoint && b->loc == loc
7266 && loc->next == NULL && b->related_breakpoint == b)
7267 {
7268 /* Create only the whole new breakpoint of this type but do not
7269 mess more complicated breakpoints with multiple locations. */
7270 b->type = bp_gnu_ifunc_resolver;
7271 /* Remember the resolver's address for use by the return
7272 breakpoint. */
7273 loc->related_address = loc->address;
7274 }
7275 }
7276 else
7277 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7278
7279 if (function_name)
7280 loc->function_name = make_unique_xstrdup (function_name);
7281 }
7282 }
7283
7284 /* Attempt to determine architecture of location identified by SAL. */
7285 struct gdbarch *
7286 get_sal_arch (struct symtab_and_line sal)
7287 {
7288 if (sal.section)
7289 return sal.section->objfile->arch ();
7290 if (sal.symtab)
7291 return sal.symtab->compunit ()->objfile ()->arch ();
7292
7293 return NULL;
7294 }
7295
7296 /* Low level routine for partially initializing a breakpoint of type
7297 BPTYPE. The newly created breakpoint's address, section, source
7298 file name, and line number are provided by SAL.
7299
7300 It is expected that the caller will complete the initialization of
7301 the newly created breakpoint struct as well as output any status
7302 information regarding the creation of a new breakpoint. */
7303
7304 static void
7305 init_raw_breakpoint (struct breakpoint *b, struct symtab_and_line sal,
7306 enum bptype bptype)
7307 {
7308 b->add_location (sal);
7309
7310 if (bptype != bp_catchpoint)
7311 gdb_assert (sal.pspace != NULL);
7312
7313 /* Store the program space that was used to set the breakpoint,
7314 except for ordinary breakpoints, which are independent of the
7315 program space. */
7316 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7317 b->pspace = sal.pspace;
7318 }
7319
7320 /* set_raw_breakpoint is a low level routine for allocating and
7321 partially initializing a breakpoint of type BPTYPE. The newly
7322 created breakpoint's address, section, source file name, and line
7323 number are provided by SAL. The newly created and partially
7324 initialized breakpoint is added to the breakpoint chain and
7325 is also returned as the value of this function.
7326
7327 It is expected that the caller will complete the initialization of
7328 the newly created breakpoint struct as well as output any status
7329 information regarding the creation of a new breakpoint. In
7330 particular, set_raw_breakpoint does NOT set the breakpoint
7331 number! Care should be taken to not allow an error to occur
7332 prior to completing the initialization of the breakpoint. If this
7333 should happen, a bogus breakpoint will be left on the chain. */
7334
7335 static struct breakpoint *
7336 set_raw_breakpoint (struct gdbarch *gdbarch,
7337 struct symtab_and_line sal, enum bptype bptype)
7338 {
7339 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (gdbarch, bptype);
7340
7341 init_raw_breakpoint (b.get (), sal, bptype);
7342 return add_to_breakpoint_chain (std::move (b));
7343 }
7344
7345 /* Call this routine when stepping and nexting to enable a breakpoint
7346 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7347 initiated the operation. */
7348
7349 void
7350 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7351 {
7352 int thread = tp->global_num;
7353
7354 /* To avoid having to rescan all objfile symbols at every step,
7355 we maintain a list of continually-inserted but always disabled
7356 longjmp "master" breakpoints. Here, we simply create momentary
7357 clones of those and enable them for the requested thread. */
7358 for (breakpoint *b : all_breakpoints_safe ())
7359 if (b->pspace == current_program_space
7360 && (b->type == bp_longjmp_master
7361 || b->type == bp_exception_master))
7362 {
7363 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7364 struct breakpoint *clone;
7365
7366 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7367 after their removal. */
7368 clone = momentary_breakpoint_from_master (b, type, 1);
7369 clone->thread = thread;
7370 }
7371
7372 tp->initiating_frame = frame;
7373 }
7374
7375 /* Delete all longjmp breakpoints from THREAD. */
7376 void
7377 delete_longjmp_breakpoint (int thread)
7378 {
7379 for (breakpoint *b : all_breakpoints_safe ())
7380 if (b->type == bp_longjmp || b->type == bp_exception)
7381 {
7382 if (b->thread == thread)
7383 delete_breakpoint (b);
7384 }
7385 }
7386
7387 void
7388 delete_longjmp_breakpoint_at_next_stop (int thread)
7389 {
7390 for (breakpoint *b : all_breakpoints_safe ())
7391 if (b->type == bp_longjmp || b->type == bp_exception)
7392 {
7393 if (b->thread == thread)
7394 b->disposition = disp_del_at_next_stop;
7395 }
7396 }
7397
7398 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7399 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7400 pointer to any of them. Return NULL if this system cannot place longjmp
7401 breakpoints. */
7402
7403 struct breakpoint *
7404 set_longjmp_breakpoint_for_call_dummy (void)
7405 {
7406 breakpoint *retval = nullptr;
7407
7408 for (breakpoint *b : all_breakpoints ())
7409 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7410 {
7411 struct breakpoint *new_b;
7412
7413 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7414 1);
7415 new_b->thread = inferior_thread ()->global_num;
7416
7417 /* Link NEW_B into the chain of RETVAL breakpoints. */
7418
7419 gdb_assert (new_b->related_breakpoint == new_b);
7420 if (retval == NULL)
7421 retval = new_b;
7422 new_b->related_breakpoint = retval;
7423 while (retval->related_breakpoint != new_b->related_breakpoint)
7424 retval = retval->related_breakpoint;
7425 retval->related_breakpoint = new_b;
7426 }
7427
7428 return retval;
7429 }
7430
7431 /* Verify all existing dummy frames and their associated breakpoints for
7432 TP. Remove those which can no longer be found in the current frame
7433 stack.
7434
7435 If the unwind fails then there is not sufficient information to discard
7436 dummy frames. In this case, elide the clean up and the dummy frames will
7437 be cleaned up next time this function is called from a location where
7438 unwinding is possible. */
7439
7440 void
7441 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7442 {
7443 struct breakpoint *b, *b_tmp;
7444
7445 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7446 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7447 {
7448 struct breakpoint *dummy_b = b->related_breakpoint;
7449
7450 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7451 chained off b->related_breakpoint. */
7452 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7453 dummy_b = dummy_b->related_breakpoint;
7454
7455 /* If there was no bp_call_dummy breakpoint then there's nothing
7456 more to do. Or, if the dummy frame associated with the
7457 bp_call_dummy is still on the stack then we need to leave this
7458 bp_call_dummy in place. */
7459 if (dummy_b->type != bp_call_dummy
7460 || frame_find_by_id (dummy_b->frame_id) != NULL)
7461 continue;
7462
7463 /* We didn't find the dummy frame on the stack, this could be
7464 because we have longjmp'd to a stack frame that is previous to
7465 the dummy frame, or it could be because the stack unwind is
7466 broken at some point between the longjmp frame and the dummy
7467 frame.
7468
7469 Next we figure out why the stack unwind stopped. If it looks
7470 like the unwind is complete then we assume the dummy frame has
7471 been jumped over, however, if the unwind stopped for an
7472 unexpected reason then we assume the stack unwind is currently
7473 broken, and that we will (eventually) return to the dummy
7474 frame.
7475
7476 It might be tempting to consider using frame_id_inner here, but
7477 that is not safe. There is no guarantee that the stack frames
7478 we are looking at here are even on the same stack as the
7479 original dummy frame, hence frame_id_inner can't be used. See
7480 the comments on frame_id_inner for more details. */
7481 bool unwind_finished_unexpectedly = false;
7482 for (struct frame_info *fi = get_current_frame (); fi != nullptr; )
7483 {
7484 struct frame_info *prev = get_prev_frame (fi);
7485 if (prev == nullptr)
7486 {
7487 /* FI is the last stack frame. Why did this frame not
7488 unwind further? */
7489 auto stop_reason = get_frame_unwind_stop_reason (fi);
7490 if (stop_reason != UNWIND_NO_REASON
7491 && stop_reason != UNWIND_OUTERMOST)
7492 unwind_finished_unexpectedly = true;
7493 }
7494 fi = prev;
7495 }
7496 if (unwind_finished_unexpectedly)
7497 continue;
7498
7499 dummy_frame_discard (dummy_b->frame_id, tp);
7500
7501 while (b->related_breakpoint != b)
7502 {
7503 if (b_tmp == b->related_breakpoint)
7504 b_tmp = b->related_breakpoint->next;
7505 delete_breakpoint (b->related_breakpoint);
7506 }
7507 delete_breakpoint (b);
7508 }
7509 }
7510
7511 void
7512 enable_overlay_breakpoints (void)
7513 {
7514 for (breakpoint *b : all_breakpoints ())
7515 if (b->type == bp_overlay_event)
7516 {
7517 b->enable_state = bp_enabled;
7518 update_global_location_list (UGLL_MAY_INSERT);
7519 overlay_events_enabled = 1;
7520 }
7521 }
7522
7523 void
7524 disable_overlay_breakpoints (void)
7525 {
7526 for (breakpoint *b : all_breakpoints ())
7527 if (b->type == bp_overlay_event)
7528 {
7529 b->enable_state = bp_disabled;
7530 update_global_location_list (UGLL_DONT_INSERT);
7531 overlay_events_enabled = 0;
7532 }
7533 }
7534
7535 /* Set an active std::terminate breakpoint for each std::terminate
7536 master breakpoint. */
7537 void
7538 set_std_terminate_breakpoint (void)
7539 {
7540 for (breakpoint *b : all_breakpoints_safe ())
7541 if (b->pspace == current_program_space
7542 && b->type == bp_std_terminate_master)
7543 {
7544 momentary_breakpoint_from_master (b, bp_std_terminate, 1);
7545 }
7546 }
7547
7548 /* Delete all the std::terminate breakpoints. */
7549 void
7550 delete_std_terminate_breakpoint (void)
7551 {
7552 for (breakpoint *b : all_breakpoints_safe ())
7553 if (b->type == bp_std_terminate)
7554 delete_breakpoint (b);
7555 }
7556
7557 struct breakpoint *
7558 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7559 {
7560 struct breakpoint *b;
7561
7562 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
7563
7564 b->enable_state = bp_enabled;
7565 /* location has to be used or breakpoint_re_set will delete me. */
7566 b->location = new_address_location (b->loc->address, NULL, 0);
7567
7568 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7569
7570 return b;
7571 }
7572
7573 struct lang_and_radix
7574 {
7575 enum language lang;
7576 int radix;
7577 };
7578
7579 /* Create a breakpoint for JIT code registration and unregistration. */
7580
7581 struct breakpoint *
7582 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7583 {
7584 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
7585 }
7586
7587 /* Remove JIT code registration and unregistration breakpoint(s). */
7588
7589 void
7590 remove_jit_event_breakpoints (void)
7591 {
7592 for (breakpoint *b : all_breakpoints_safe ())
7593 if (b->type == bp_jit_event
7594 && b->loc->pspace == current_program_space)
7595 delete_breakpoint (b);
7596 }
7597
7598 void
7599 remove_solib_event_breakpoints (void)
7600 {
7601 for (breakpoint *b : all_breakpoints_safe ())
7602 if (b->type == bp_shlib_event
7603 && b->loc->pspace == current_program_space)
7604 delete_breakpoint (b);
7605 }
7606
7607 /* See breakpoint.h. */
7608
7609 void
7610 remove_solib_event_breakpoints_at_next_stop (void)
7611 {
7612 for (breakpoint *b : all_breakpoints_safe ())
7613 if (b->type == bp_shlib_event
7614 && b->loc->pspace == current_program_space)
7615 b->disposition = disp_del_at_next_stop;
7616 }
7617
7618 /* Helper for create_solib_event_breakpoint /
7619 create_and_insert_solib_event_breakpoint. Allows specifying which
7620 INSERT_MODE to pass through to update_global_location_list. */
7621
7622 static struct breakpoint *
7623 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7624 enum ugll_insert_mode insert_mode)
7625 {
7626 struct breakpoint *b;
7627
7628 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
7629 update_global_location_list_nothrow (insert_mode);
7630 return b;
7631 }
7632
7633 struct breakpoint *
7634 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7635 {
7636 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7637 }
7638
7639 /* See breakpoint.h. */
7640
7641 struct breakpoint *
7642 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7643 {
7644 struct breakpoint *b;
7645
7646 /* Explicitly tell update_global_location_list to insert
7647 locations. */
7648 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7649 if (!b->loc->inserted)
7650 {
7651 delete_breakpoint (b);
7652 return NULL;
7653 }
7654 return b;
7655 }
7656
7657 /* Disable any breakpoints that are on code in shared libraries. Only
7658 apply to enabled breakpoints, disabled ones can just stay disabled. */
7659
7660 void
7661 disable_breakpoints_in_shlibs (void)
7662 {
7663 for (bp_location *loc : all_bp_locations ())
7664 {
7665 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7666 struct breakpoint *b = loc->owner;
7667
7668 /* We apply the check to all breakpoints, including disabled for
7669 those with loc->duplicate set. This is so that when breakpoint
7670 becomes enabled, or the duplicate is removed, gdb will try to
7671 insert all breakpoints. If we don't set shlib_disabled here,
7672 we'll try to insert those breakpoints and fail. */
7673 if (((b->type == bp_breakpoint)
7674 || (b->type == bp_jit_event)
7675 || (b->type == bp_hardware_breakpoint)
7676 || (is_tracepoint (b)))
7677 && loc->pspace == current_program_space
7678 && !loc->shlib_disabled
7679 && solib_name_from_address (loc->pspace, loc->address)
7680 )
7681 {
7682 loc->shlib_disabled = 1;
7683 }
7684 }
7685 }
7686
7687 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7688 notification of unloaded_shlib. Only apply to enabled breakpoints,
7689 disabled ones can just stay disabled. */
7690
7691 static void
7692 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7693 {
7694 int disabled_shlib_breaks = 0;
7695
7696 for (bp_location *loc : all_bp_locations ())
7697 {
7698 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7699 struct breakpoint *b = loc->owner;
7700
7701 if (solib->pspace == loc->pspace
7702 && !loc->shlib_disabled
7703 && (((b->type == bp_breakpoint
7704 || b->type == bp_jit_event
7705 || b->type == bp_hardware_breakpoint)
7706 && (loc->loc_type == bp_loc_hardware_breakpoint
7707 || loc->loc_type == bp_loc_software_breakpoint))
7708 || is_tracepoint (b))
7709 && solib_contains_address_p (solib, loc->address))
7710 {
7711 loc->shlib_disabled = 1;
7712 /* At this point, we cannot rely on remove_breakpoint
7713 succeeding so we must mark the breakpoint as not inserted
7714 to prevent future errors occurring in remove_breakpoints. */
7715 loc->inserted = 0;
7716
7717 /* This may cause duplicate notifications for the same breakpoint. */
7718 gdb::observers::breakpoint_modified.notify (b);
7719
7720 if (!disabled_shlib_breaks)
7721 {
7722 target_terminal::ours_for_output ();
7723 warning (_("Temporarily disabling breakpoints "
7724 "for unloaded shared library \"%s\""),
7725 solib->so_name);
7726 }
7727 disabled_shlib_breaks = 1;
7728 }
7729 }
7730 }
7731
7732 /* Disable any breakpoints and tracepoints in OBJFILE upon
7733 notification of free_objfile. Only apply to enabled breakpoints,
7734 disabled ones can just stay disabled. */
7735
7736 static void
7737 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7738 {
7739 if (objfile == NULL)
7740 return;
7741
7742 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7743 managed by the user with add-symbol-file/remove-symbol-file.
7744 Similarly to how breakpoints in shared libraries are handled in
7745 response to "nosharedlibrary", mark breakpoints in such modules
7746 shlib_disabled so they end up uninserted on the next global
7747 location list update. Shared libraries not loaded by the user
7748 aren't handled here -- they're already handled in
7749 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7750 solib_unloaded observer. We skip objfiles that are not
7751 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7752 main objfile). */
7753 if ((objfile->flags & OBJF_SHARED) == 0
7754 || (objfile->flags & OBJF_USERLOADED) == 0)
7755 return;
7756
7757 for (breakpoint *b : all_breakpoints ())
7758 {
7759 int bp_modified = 0;
7760
7761 if (!is_breakpoint (b) && !is_tracepoint (b))
7762 continue;
7763
7764 for (bp_location *loc : b->locations ())
7765 {
7766 CORE_ADDR loc_addr = loc->address;
7767
7768 if (loc->loc_type != bp_loc_hardware_breakpoint
7769 && loc->loc_type != bp_loc_software_breakpoint)
7770 continue;
7771
7772 if (loc->shlib_disabled != 0)
7773 continue;
7774
7775 if (objfile->pspace != loc->pspace)
7776 continue;
7777
7778 if (loc->loc_type != bp_loc_hardware_breakpoint
7779 && loc->loc_type != bp_loc_software_breakpoint)
7780 continue;
7781
7782 if (is_addr_in_objfile (loc_addr, objfile))
7783 {
7784 loc->shlib_disabled = 1;
7785 /* At this point, we don't know whether the object was
7786 unmapped from the inferior or not, so leave the
7787 inserted flag alone. We'll handle failure to
7788 uninsert quietly, in case the object was indeed
7789 unmapped. */
7790
7791 mark_breakpoint_location_modified (loc);
7792
7793 bp_modified = 1;
7794 }
7795 }
7796
7797 if (bp_modified)
7798 gdb::observers::breakpoint_modified.notify (b);
7799 }
7800 }
7801
7802 /* See breakpoint.h. */
7803
7804 catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
7805 const char *cond_string_)
7806 : base_breakpoint (gdbarch, bp_catchpoint)
7807 {
7808 symtab_and_line sal;
7809 sal.pspace = current_program_space;
7810
7811 init_raw_breakpoint (this, sal, bp_catchpoint);
7812
7813 if (cond_string_ != nullptr)
7814 cond_string = make_unique_xstrdup (cond_string_);
7815 disposition = temp ? disp_del : disp_donttouch;
7816 }
7817
7818 void
7819 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
7820 {
7821 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
7822 set_breakpoint_number (internal, b);
7823 if (is_tracepoint (b))
7824 set_tracepoint_count (breakpoint_count);
7825 if (!internal)
7826 mention (b);
7827 gdb::observers::breakpoint_created.notify (b);
7828
7829 if (update_gll)
7830 update_global_location_list (UGLL_MAY_INSERT);
7831 }
7832
7833 static int
7834 hw_breakpoint_used_count (void)
7835 {
7836 int i = 0;
7837
7838 for (breakpoint *b : all_breakpoints ())
7839 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
7840 for (bp_location *bl : b->locations ())
7841 {
7842 /* Special types of hardware breakpoints may use more than
7843 one register. */
7844 i += b->resources_needed (bl);
7845 }
7846
7847 return i;
7848 }
7849
7850 /* Returns the resources B would use if it were a hardware
7851 watchpoint. */
7852
7853 static int
7854 hw_watchpoint_use_count (struct breakpoint *b)
7855 {
7856 int i = 0;
7857
7858 if (!breakpoint_enabled (b))
7859 return 0;
7860
7861 for (bp_location *bl : b->locations ())
7862 {
7863 /* Special types of hardware watchpoints may use more than
7864 one register. */
7865 i += b->resources_needed (bl);
7866 }
7867
7868 return i;
7869 }
7870
7871 /* Returns the sum the used resources of all hardware watchpoints of
7872 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
7873 the sum of the used resources of all hardware watchpoints of other
7874 types _not_ TYPE. */
7875
7876 static int
7877 hw_watchpoint_used_count_others (struct breakpoint *except,
7878 enum bptype type, int *other_type_used)
7879 {
7880 int i = 0;
7881
7882 *other_type_used = 0;
7883 for (breakpoint *b : all_breakpoints ())
7884 {
7885 if (b == except)
7886 continue;
7887 if (!breakpoint_enabled (b))
7888 continue;
7889
7890 if (b->type == type)
7891 i += hw_watchpoint_use_count (b);
7892 else if (is_hardware_watchpoint (b))
7893 *other_type_used = 1;
7894 }
7895
7896 return i;
7897 }
7898
7899 void
7900 disable_watchpoints_before_interactive_call_start (void)
7901 {
7902 for (breakpoint *b : all_breakpoints ())
7903 if (is_watchpoint (b) && breakpoint_enabled (b))
7904 {
7905 b->enable_state = bp_call_disabled;
7906 update_global_location_list (UGLL_DONT_INSERT);
7907 }
7908 }
7909
7910 void
7911 enable_watchpoints_after_interactive_call_stop (void)
7912 {
7913 for (breakpoint *b : all_breakpoints ())
7914 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
7915 {
7916 b->enable_state = bp_enabled;
7917 update_global_location_list (UGLL_MAY_INSERT);
7918 }
7919 }
7920
7921 void
7922 disable_breakpoints_before_startup (void)
7923 {
7924 current_program_space->executing_startup = 1;
7925 update_global_location_list (UGLL_DONT_INSERT);
7926 }
7927
7928 void
7929 enable_breakpoints_after_startup (void)
7930 {
7931 current_program_space->executing_startup = 0;
7932 breakpoint_re_set ();
7933 }
7934
7935 /* Create a new single-step breakpoint for thread THREAD, with no
7936 locations. */
7937
7938 static struct breakpoint *
7939 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
7940 {
7941 std::unique_ptr<breakpoint> b (new momentary_breakpoint (gdbarch,
7942 bp_single_step));
7943
7944 b->disposition = disp_donttouch;
7945 b->frame_id = null_frame_id;
7946
7947 b->thread = thread;
7948 gdb_assert (b->thread != 0);
7949
7950 return add_to_breakpoint_chain (std::move (b));
7951 }
7952
7953 /* Set a momentary breakpoint of type TYPE at address specified by
7954 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
7955 frame. */
7956
7957 breakpoint_up
7958 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7959 struct frame_id frame_id, enum bptype type)
7960 {
7961 struct breakpoint *b;
7962
7963 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
7964 tail-called one. */
7965 gdb_assert (!frame_id_artificial_p (frame_id));
7966
7967 b = set_raw_breakpoint (gdbarch, sal, type);
7968 b->enable_state = bp_enabled;
7969 b->disposition = disp_donttouch;
7970 b->frame_id = frame_id;
7971
7972 b->thread = inferior_thread ()->global_num;
7973
7974 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7975
7976 return breakpoint_up (b);
7977 }
7978
7979 /* Make a momentary breakpoint based on the master breakpoint ORIG.
7980 The new breakpoint will have type TYPE, use OPS as its
7981 breakpoint_ops, and will set enabled to LOC_ENABLED. */
7982
7983 static struct breakpoint *
7984 momentary_breakpoint_from_master (struct breakpoint *orig,
7985 enum bptype type,
7986 int loc_enabled)
7987 {
7988 struct breakpoint *copy;
7989
7990 copy = set_raw_breakpoint_without_location (orig->gdbarch, type);
7991 copy->loc = copy->allocate_location ();
7992 set_breakpoint_location_function (copy->loc);
7993
7994 copy->loc->gdbarch = orig->loc->gdbarch;
7995 copy->loc->requested_address = orig->loc->requested_address;
7996 copy->loc->address = orig->loc->address;
7997 copy->loc->section = orig->loc->section;
7998 copy->loc->pspace = orig->loc->pspace;
7999 copy->loc->probe = orig->loc->probe;
8000 copy->loc->line_number = orig->loc->line_number;
8001 copy->loc->symtab = orig->loc->symtab;
8002 copy->loc->enabled = loc_enabled;
8003 copy->frame_id = orig->frame_id;
8004 copy->thread = orig->thread;
8005 copy->pspace = orig->pspace;
8006
8007 copy->enable_state = bp_enabled;
8008 copy->disposition = disp_donttouch;
8009 copy->number = internal_breakpoint_number--;
8010
8011 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8012 return copy;
8013 }
8014
8015 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8016 ORIG is NULL. */
8017
8018 struct breakpoint *
8019 clone_momentary_breakpoint (struct breakpoint *orig)
8020 {
8021 /* If there's nothing to clone, then return nothing. */
8022 if (orig == NULL)
8023 return NULL;
8024
8025 return momentary_breakpoint_from_master (orig, orig->type, 0);
8026 }
8027
8028 breakpoint_up
8029 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8030 enum bptype type)
8031 {
8032 struct symtab_and_line sal;
8033
8034 sal = find_pc_line (pc, 0);
8035 sal.pc = pc;
8036 sal.section = find_pc_overlay (pc);
8037 sal.explicit_pc = 1;
8038
8039 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8040 }
8041 \f
8042
8043 /* Tell the user we have just set a breakpoint B. */
8044
8045 static void
8046 mention (const breakpoint *b)
8047 {
8048 b->print_mention ();
8049 current_uiout->text ("\n");
8050 }
8051 \f
8052
8053 static bool bp_loc_is_permanent (struct bp_location *loc);
8054
8055 /* Handle "set breakpoint auto-hw on".
8056
8057 If the explicitly specified breakpoint type is not hardware
8058 breakpoint, check the memory map to see whether the breakpoint
8059 address is in read-only memory.
8060
8061 - location type is not hardware breakpoint, memory is read-only.
8062 We change the type of the location to hardware breakpoint.
8063
8064 - location type is hardware breakpoint, memory is read-write. This
8065 means we've previously made the location hardware one, but then the
8066 memory map changed, so we undo.
8067 */
8068
8069 static void
8070 handle_automatic_hardware_breakpoints (bp_location *bl)
8071 {
8072 if (automatic_hardware_breakpoints
8073 && bl->owner->type != bp_hardware_breakpoint
8074 && (bl->loc_type == bp_loc_software_breakpoint
8075 || bl->loc_type == bp_loc_hardware_breakpoint))
8076 {
8077 /* When breakpoints are removed, remove_breakpoints will use
8078 location types we've just set here, the only possible problem
8079 is that memory map has changed during running program, but
8080 it's not going to work anyway with current gdb. */
8081 mem_region *mr = lookup_mem_region (bl->address);
8082
8083 if (mr != nullptr)
8084 {
8085 enum bp_loc_type new_type;
8086
8087 if (mr->attrib.mode != MEM_RW)
8088 new_type = bp_loc_hardware_breakpoint;
8089 else
8090 new_type = bp_loc_software_breakpoint;
8091
8092 if (new_type != bl->loc_type)
8093 {
8094 static bool said = false;
8095
8096 bl->loc_type = new_type;
8097 if (!said)
8098 {
8099 gdb_printf (_("Note: automatically using "
8100 "hardware breakpoints for "
8101 "read-only addresses.\n"));
8102 said = true;
8103 }
8104 }
8105 }
8106 }
8107 }
8108
8109 bp_location *
8110 breakpoint::add_location (const symtab_and_line &sal)
8111 {
8112 struct bp_location *new_loc, **tmp;
8113 CORE_ADDR adjusted_address;
8114 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8115
8116 if (loc_gdbarch == NULL)
8117 loc_gdbarch = gdbarch;
8118
8119 /* Adjust the breakpoint's address prior to allocating a location.
8120 Once we call allocate_location(), that mostly uninitialized
8121 location will be placed on the location chain. Adjustment of the
8122 breakpoint may cause target_read_memory() to be called and we do
8123 not want its scan of the location chain to find a breakpoint and
8124 location that's only been partially initialized. */
8125 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8126 sal.pc, type);
8127
8128 /* Sort the locations by their ADDRESS. */
8129 new_loc = allocate_location ();
8130 for (tmp = &(loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8131 tmp = &((*tmp)->next))
8132 ;
8133 new_loc->next = *tmp;
8134 *tmp = new_loc;
8135
8136 new_loc->requested_address = sal.pc;
8137 new_loc->address = adjusted_address;
8138 new_loc->pspace = sal.pspace;
8139 new_loc->probe.prob = sal.prob;
8140 new_loc->probe.objfile = sal.objfile;
8141 gdb_assert (new_loc->pspace != NULL);
8142 new_loc->section = sal.section;
8143 new_loc->gdbarch = loc_gdbarch;
8144 new_loc->line_number = sal.line;
8145 new_loc->symtab = sal.symtab;
8146 new_loc->symbol = sal.symbol;
8147 new_loc->msymbol = sal.msymbol;
8148 new_loc->objfile = sal.objfile;
8149
8150 set_breakpoint_location_function (new_loc);
8151
8152 /* While by definition, permanent breakpoints are already present in the
8153 code, we don't mark the location as inserted. Normally one would expect
8154 that GDB could rely on that breakpoint instruction to stop the program,
8155 thus removing the need to insert its own breakpoint, except that executing
8156 the breakpoint instruction can kill the target instead of reporting a
8157 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8158 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8159 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8160 breakpoint be inserted normally results in QEMU knowing about the GDB
8161 breakpoint, and thus trap before the breakpoint instruction is executed.
8162 (If GDB later needs to continue execution past the permanent breakpoint,
8163 it manually increments the PC, thus avoiding executing the breakpoint
8164 instruction.) */
8165 if (bp_loc_is_permanent (new_loc))
8166 new_loc->permanent = 1;
8167
8168 return new_loc;
8169 }
8170 \f
8171
8172 /* Return true if LOC is pointing to a permanent breakpoint,
8173 return false otherwise. */
8174
8175 static bool
8176 bp_loc_is_permanent (struct bp_location *loc)
8177 {
8178 gdb_assert (loc != NULL);
8179
8180 /* If we have a non-breakpoint-backed catchpoint or a software
8181 watchpoint, just return 0. We should not attempt to read from
8182 the addresses the locations of these breakpoint types point to.
8183 gdbarch_program_breakpoint_here_p, below, will attempt to read
8184 memory. */
8185 if (!bl_address_is_meaningful (loc))
8186 return false;
8187
8188 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8189 switch_to_program_space_and_thread (loc->pspace);
8190 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8191 }
8192
8193 /* Build a command list for the dprintf corresponding to the current
8194 settings of the dprintf style options. */
8195
8196 static void
8197 update_dprintf_command_list (struct breakpoint *b)
8198 {
8199 const char *dprintf_args = b->extra_string.get ();
8200 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8201
8202 if (!dprintf_args)
8203 return;
8204
8205 dprintf_args = skip_spaces (dprintf_args);
8206
8207 /* Allow a comma, as it may have terminated a location, but don't
8208 insist on it. */
8209 if (*dprintf_args == ',')
8210 ++dprintf_args;
8211 dprintf_args = skip_spaces (dprintf_args);
8212
8213 if (*dprintf_args != '"')
8214 error (_("Bad format string, missing '\"'."));
8215
8216 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8217 printf_line = xstrprintf ("printf %s", dprintf_args);
8218 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8219 {
8220 if (dprintf_function.empty ())
8221 error (_("No function supplied for dprintf call"));
8222
8223 if (!dprintf_channel.empty ())
8224 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8225 dprintf_function.c_str (),
8226 dprintf_channel.c_str (),
8227 dprintf_args);
8228 else
8229 printf_line = xstrprintf ("call (void) %s (%s)",
8230 dprintf_function.c_str (),
8231 dprintf_args);
8232 }
8233 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8234 {
8235 if (target_can_run_breakpoint_commands ())
8236 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8237 else
8238 {
8239 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8240 printf_line = xstrprintf ("printf %s", dprintf_args);
8241 }
8242 }
8243 else
8244 internal_error (__FILE__, __LINE__,
8245 _("Invalid dprintf style."));
8246
8247 gdb_assert (printf_line != NULL);
8248
8249 /* Manufacture a printf sequence. */
8250 struct command_line *printf_cmd_line
8251 = new struct command_line (simple_control, printf_line.release ());
8252 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8253 command_lines_deleter ()));
8254 }
8255
8256 /* Update all dprintf commands, making their command lists reflect
8257 current style settings. */
8258
8259 static void
8260 update_dprintf_commands (const char *args, int from_tty,
8261 struct cmd_list_element *c)
8262 {
8263 for (breakpoint *b : all_breakpoints ())
8264 if (b->type == bp_dprintf)
8265 update_dprintf_command_list (b);
8266 }
8267
8268 /* Create a breakpoint with SAL as location. Use LOCATION
8269 as a description of the location, and COND_STRING
8270 as condition expression. If LOCATION is NULL then create an
8271 "address location" from the address in the SAL. */
8272
8273 static void
8274 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8275 gdb::array_view<const symtab_and_line> sals,
8276 event_location_up &&location,
8277 gdb::unique_xmalloc_ptr<char> filter,
8278 gdb::unique_xmalloc_ptr<char> cond_string,
8279 gdb::unique_xmalloc_ptr<char> extra_string,
8280 enum bptype type, enum bpdisp disposition,
8281 int thread, int task, int ignore_count,
8282 const struct breakpoint_ops *ops, int from_tty,
8283 int enabled, int internal, unsigned flags,
8284 int display_canonical)
8285 {
8286 int i;
8287
8288 if (type == bp_hardware_breakpoint)
8289 {
8290 int target_resources_ok;
8291
8292 i = hw_breakpoint_used_count ();
8293 target_resources_ok =
8294 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8295 i + 1, 0);
8296 if (target_resources_ok == 0)
8297 error (_("No hardware breakpoint support in the target."));
8298 else if (target_resources_ok < 0)
8299 error (_("Hardware breakpoints used exceeds limit."));
8300 }
8301
8302 gdb_assert (!sals.empty ());
8303
8304 for (const auto &sal : sals)
8305 {
8306 struct bp_location *loc;
8307
8308 if (from_tty)
8309 {
8310 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8311 if (!loc_gdbarch)
8312 loc_gdbarch = gdbarch;
8313
8314 describe_other_breakpoints (loc_gdbarch,
8315 sal.pspace, sal.pc, sal.section, thread);
8316 }
8317
8318 if (&sal == &sals[0])
8319 {
8320 init_raw_breakpoint (b, sal, type);
8321 b->thread = thread;
8322 b->task = task;
8323
8324 b->cond_string = std::move (cond_string);
8325 b->extra_string = std::move (extra_string);
8326 b->ignore_count = ignore_count;
8327 b->enable_state = enabled ? bp_enabled : bp_disabled;
8328 b->disposition = disposition;
8329
8330 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8331 b->loc->inserted = 1;
8332
8333 if (type == bp_static_tracepoint
8334 || type == bp_static_marker_tracepoint)
8335 {
8336 struct tracepoint *t = (struct tracepoint *) b;
8337 struct static_tracepoint_marker marker;
8338
8339 if (strace_marker_p (b))
8340 {
8341 /* We already know the marker exists, otherwise, we
8342 wouldn't see a sal for it. */
8343 const char *p
8344 = &event_location_to_string (b->location.get ())[3];
8345 const char *endp;
8346
8347 p = skip_spaces (p);
8348
8349 endp = skip_to_space (p);
8350
8351 t->static_trace_marker_id.assign (p, endp - p);
8352
8353 gdb_printf (_("Probed static tracepoint "
8354 "marker \"%s\"\n"),
8355 t->static_trace_marker_id.c_str ());
8356 }
8357 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8358 {
8359 t->static_trace_marker_id = std::move (marker.str_id);
8360
8361 gdb_printf (_("Probed static tracepoint "
8362 "marker \"%s\"\n"),
8363 t->static_trace_marker_id.c_str ());
8364 }
8365 else
8366 warning (_("Couldn't determine the static "
8367 "tracepoint marker to probe"));
8368 }
8369
8370 loc = b->loc;
8371 }
8372 else
8373 {
8374 loc = b->add_location (sal);
8375 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8376 loc->inserted = 1;
8377 }
8378
8379 /* Do not set breakpoint locations conditions yet. As locations
8380 are inserted, they get sorted based on their addresses. Let
8381 the list stabilize to have reliable location numbers. */
8382
8383 /* Dynamic printf requires and uses additional arguments on the
8384 command line, otherwise it's an error. */
8385 if (type == bp_dprintf)
8386 {
8387 if (b->extra_string)
8388 update_dprintf_command_list (b);
8389 else
8390 error (_("Format string required"));
8391 }
8392 else if (b->extra_string)
8393 error (_("Garbage '%s' at end of command"), b->extra_string.get ());
8394 }
8395
8396
8397 /* The order of the locations is now stable. Set the location
8398 condition using the location's number. */
8399 int loc_num = 1;
8400 for (bp_location *loc : b->locations ())
8401 {
8402 if (b->cond_string != nullptr)
8403 set_breakpoint_location_condition (b->cond_string.get (), loc,
8404 b->number, loc_num);
8405
8406 ++loc_num;
8407 }
8408
8409 b->display_canonical = display_canonical;
8410 if (location != NULL)
8411 b->location = std::move (location);
8412 else
8413 b->location = new_address_location (b->loc->address, NULL, 0);
8414 b->filter = std::move (filter);
8415 }
8416
8417 static void
8418 create_breakpoint_sal (struct gdbarch *gdbarch,
8419 gdb::array_view<const symtab_and_line> sals,
8420 event_location_up &&location,
8421 gdb::unique_xmalloc_ptr<char> filter,
8422 gdb::unique_xmalloc_ptr<char> cond_string,
8423 gdb::unique_xmalloc_ptr<char> extra_string,
8424 enum bptype type, enum bpdisp disposition,
8425 int thread, int task, int ignore_count,
8426 const struct breakpoint_ops *ops, int from_tty,
8427 int enabled, int internal, unsigned flags,
8428 int display_canonical)
8429 {
8430 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (gdbarch, type);
8431
8432 init_breakpoint_sal (b.get (), gdbarch,
8433 sals, std::move (location),
8434 std::move (filter),
8435 std::move (cond_string),
8436 std::move (extra_string),
8437 type, disposition,
8438 thread, task, ignore_count,
8439 ops, from_tty,
8440 enabled, internal, flags,
8441 display_canonical);
8442
8443 install_breakpoint (internal, std::move (b), 0);
8444 }
8445
8446 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8447 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8448 value. COND_STRING, if not NULL, specified the condition to be
8449 used for all breakpoints. Essentially the only case where
8450 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8451 function. In that case, it's still not possible to specify
8452 separate conditions for different overloaded functions, so
8453 we take just a single condition string.
8454
8455 NOTE: If the function succeeds, the caller is expected to cleanup
8456 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8457 array contents). If the function fails (error() is called), the
8458 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8459 COND and SALS arrays and each of those arrays contents. */
8460
8461 static void
8462 create_breakpoints_sal (struct gdbarch *gdbarch,
8463 struct linespec_result *canonical,
8464 gdb::unique_xmalloc_ptr<char> cond_string,
8465 gdb::unique_xmalloc_ptr<char> extra_string,
8466 enum bptype type, enum bpdisp disposition,
8467 int thread, int task, int ignore_count,
8468 const struct breakpoint_ops *ops, int from_tty,
8469 int enabled, int internal, unsigned flags)
8470 {
8471 if (canonical->pre_expanded)
8472 gdb_assert (canonical->lsals.size () == 1);
8473
8474 for (const auto &lsal : canonical->lsals)
8475 {
8476 /* Note that 'location' can be NULL in the case of a plain
8477 'break', without arguments. */
8478 event_location_up location
8479 = (canonical->location != NULL
8480 ? copy_event_location (canonical->location.get ()) : NULL);
8481 gdb::unique_xmalloc_ptr<char> filter_string
8482 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8483
8484 create_breakpoint_sal (gdbarch, lsal.sals,
8485 std::move (location),
8486 std::move (filter_string),
8487 std::move (cond_string),
8488 std::move (extra_string),
8489 type, disposition,
8490 thread, task, ignore_count, ops,
8491 from_tty, enabled, internal, flags,
8492 canonical->special_display);
8493 }
8494 }
8495
8496 /* Parse LOCATION which is assumed to be a SAL specification possibly
8497 followed by conditionals. On return, SALS contains an array of SAL
8498 addresses found. LOCATION points to the end of the SAL (for
8499 linespec locations).
8500
8501 The array and the line spec strings are allocated on the heap, it is
8502 the caller's responsibility to free them. */
8503
8504 static void
8505 parse_breakpoint_sals (struct event_location *location,
8506 struct linespec_result *canonical)
8507 {
8508 struct symtab_and_line cursal;
8509
8510 if (event_location_type (location) == LINESPEC_LOCATION)
8511 {
8512 const char *spec = get_linespec_location (location)->spec_string;
8513
8514 if (spec == NULL)
8515 {
8516 /* The last displayed codepoint, if it's valid, is our default
8517 breakpoint address. */
8518 if (last_displayed_sal_is_valid ())
8519 {
8520 /* Set sal's pspace, pc, symtab, and line to the values
8521 corresponding to the last call to print_frame_info.
8522 Be sure to reinitialize LINE with NOTCURRENT == 0
8523 as the breakpoint line number is inappropriate otherwise.
8524 find_pc_line would adjust PC, re-set it back. */
8525 symtab_and_line sal = get_last_displayed_sal ();
8526 CORE_ADDR pc = sal.pc;
8527
8528 sal = find_pc_line (pc, 0);
8529
8530 /* "break" without arguments is equivalent to "break *PC"
8531 where PC is the last displayed codepoint's address. So
8532 make sure to set sal.explicit_pc to prevent GDB from
8533 trying to expand the list of sals to include all other
8534 instances with the same symtab and line. */
8535 sal.pc = pc;
8536 sal.explicit_pc = 1;
8537
8538 struct linespec_sals lsal;
8539 lsal.sals = {sal};
8540 lsal.canonical = NULL;
8541
8542 canonical->lsals.push_back (std::move (lsal));
8543 return;
8544 }
8545 else
8546 error (_("No default breakpoint address now."));
8547 }
8548 }
8549
8550 /* Force almost all breakpoints to be in terms of the
8551 current_source_symtab (which is decode_line_1's default).
8552 This should produce the results we want almost all of the
8553 time while leaving default_breakpoint_* alone.
8554
8555 ObjC: However, don't match an Objective-C method name which
8556 may have a '+' or '-' succeeded by a '['. */
8557 cursal = get_current_source_symtab_and_line ();
8558 if (last_displayed_sal_is_valid ())
8559 {
8560 const char *spec = NULL;
8561
8562 if (event_location_type (location) == LINESPEC_LOCATION)
8563 spec = get_linespec_location (location)->spec_string;
8564
8565 if (!cursal.symtab
8566 || (spec != NULL
8567 && strchr ("+-", spec[0]) != NULL
8568 && spec[1] != '['))
8569 {
8570 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
8571 get_last_displayed_symtab (),
8572 get_last_displayed_line (),
8573 canonical, NULL, NULL);
8574 return;
8575 }
8576 }
8577
8578 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
8579 cursal.symtab, cursal.line, canonical, NULL, NULL);
8580 }
8581
8582
8583 /* Convert each SAL into a real PC. Verify that the PC can be
8584 inserted as a breakpoint. If it can't throw an error. */
8585
8586 static void
8587 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
8588 {
8589 for (auto &sal : sals)
8590 resolve_sal_pc (&sal);
8591 }
8592
8593 /* Fast tracepoints may have restrictions on valid locations. For
8594 instance, a fast tracepoint using a jump instead of a trap will
8595 likely have to overwrite more bytes than a trap would, and so can
8596 only be placed where the instruction is longer than the jump, or a
8597 multi-instruction sequence does not have a jump into the middle of
8598 it, etc. */
8599
8600 static void
8601 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8602 gdb::array_view<const symtab_and_line> sals)
8603 {
8604 for (const auto &sal : sals)
8605 {
8606 struct gdbarch *sarch;
8607
8608 sarch = get_sal_arch (sal);
8609 /* We fall back to GDBARCH if there is no architecture
8610 associated with SAL. */
8611 if (sarch == NULL)
8612 sarch = gdbarch;
8613 std::string msg;
8614 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
8615 error (_("May not have a fast tracepoint at %s%s"),
8616 paddress (sarch, sal.pc), msg.c_str ());
8617 }
8618 }
8619
8620 /* Given TOK, a string specification of condition and thread, as
8621 accepted by the 'break' command, extract the condition
8622 string and thread number and set *COND_STRING and *THREAD.
8623 PC identifies the context at which the condition should be parsed.
8624 If no condition is found, *COND_STRING is set to NULL.
8625 If no thread is found, *THREAD is set to -1. */
8626
8627 static void
8628 find_condition_and_thread (const char *tok, CORE_ADDR pc,
8629 gdb::unique_xmalloc_ptr<char> *cond_string,
8630 int *thread, int *task,
8631 gdb::unique_xmalloc_ptr<char> *rest)
8632 {
8633 cond_string->reset ();
8634 *thread = -1;
8635 *task = 0;
8636 rest->reset ();
8637 bool force = false;
8638
8639 while (tok && *tok)
8640 {
8641 const char *end_tok;
8642 int toklen;
8643 const char *cond_start = NULL;
8644 const char *cond_end = NULL;
8645
8646 tok = skip_spaces (tok);
8647
8648 if ((*tok == '"' || *tok == ',') && rest)
8649 {
8650 rest->reset (savestring (tok, strlen (tok)));
8651 return;
8652 }
8653
8654 end_tok = skip_to_space (tok);
8655
8656 toklen = end_tok - tok;
8657
8658 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8659 {
8660 tok = cond_start = end_tok + 1;
8661 try
8662 {
8663 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8664 }
8665 catch (const gdb_exception_error &)
8666 {
8667 if (!force)
8668 throw;
8669 else
8670 tok = tok + strlen (tok);
8671 }
8672 cond_end = tok;
8673 cond_string->reset (savestring (cond_start, cond_end - cond_start));
8674 }
8675 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8676 {
8677 tok = tok + toklen;
8678 force = true;
8679 }
8680 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8681 {
8682 const char *tmptok;
8683 struct thread_info *thr;
8684
8685 tok = end_tok + 1;
8686 thr = parse_thread_id (tok, &tmptok);
8687 if (tok == tmptok)
8688 error (_("Junk after thread keyword."));
8689 *thread = thr->global_num;
8690 tok = tmptok;
8691 }
8692 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8693 {
8694 char *tmptok;
8695
8696 tok = end_tok + 1;
8697 *task = strtol (tok, &tmptok, 0);
8698 if (tok == tmptok)
8699 error (_("Junk after task keyword."));
8700 if (!valid_task_id (*task))
8701 error (_("Unknown task %d."), *task);
8702 tok = tmptok;
8703 }
8704 else if (rest)
8705 {
8706 rest->reset (savestring (tok, strlen (tok)));
8707 return;
8708 }
8709 else
8710 error (_("Junk at end of arguments."));
8711 }
8712 }
8713
8714 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
8715 succeeds. The parsed values are written to COND_STRING, THREAD,
8716 TASK, and REST. See the comment of 'find_condition_and_thread'
8717 for the description of these parameters and INPUT. */
8718
8719 static void
8720 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
8721 const char *input,
8722 gdb::unique_xmalloc_ptr<char> *cond_string,
8723 int *thread, int *task,
8724 gdb::unique_xmalloc_ptr<char> *rest)
8725 {
8726 int num_failures = 0;
8727 for (auto &sal : sals)
8728 {
8729 gdb::unique_xmalloc_ptr<char> cond;
8730 int thread_id = 0;
8731 int task_id = 0;
8732 gdb::unique_xmalloc_ptr<char> remaining;
8733
8734 /* Here we want to parse 'arg' to separate condition from thread
8735 number. But because parsing happens in a context and the
8736 contexts of sals might be different, try each until there is
8737 success. Finding one successful parse is sufficient for our
8738 goal. When setting the breakpoint we'll re-parse the
8739 condition in the context of each sal. */
8740 try
8741 {
8742 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8743 &task_id, &remaining);
8744 *cond_string = std::move (cond);
8745 *thread = thread_id;
8746 *task = task_id;
8747 *rest = std::move (remaining);
8748 break;
8749 }
8750 catch (const gdb_exception_error &e)
8751 {
8752 num_failures++;
8753 /* If no sal remains, do not continue. */
8754 if (num_failures == sals.size ())
8755 throw;
8756 }
8757 }
8758 }
8759
8760 /* Decode a static tracepoint marker spec. */
8761
8762 static std::vector<symtab_and_line>
8763 decode_static_tracepoint_spec (const char **arg_p)
8764 {
8765 const char *p = &(*arg_p)[3];
8766 const char *endp;
8767
8768 p = skip_spaces (p);
8769
8770 endp = skip_to_space (p);
8771
8772 std::string marker_str (p, endp - p);
8773
8774 std::vector<static_tracepoint_marker> markers
8775 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8776 if (markers.empty ())
8777 error (_("No known static tracepoint marker named %s"),
8778 marker_str.c_str ());
8779
8780 std::vector<symtab_and_line> sals;
8781 sals.reserve (markers.size ());
8782
8783 for (const static_tracepoint_marker &marker : markers)
8784 {
8785 symtab_and_line sal = find_pc_line (marker.address, 0);
8786 sal.pc = marker.address;
8787 sals.push_back (sal);
8788 }
8789
8790 *arg_p = endp;
8791 return sals;
8792 }
8793
8794 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8795 according to IS_TRACEPOINT. */
8796
8797 static const struct breakpoint_ops *
8798 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
8799 bool is_tracepoint)
8800 {
8801 if (is_tracepoint)
8802 {
8803 if (location_type == PROBE_LOCATION)
8804 return &tracepoint_probe_breakpoint_ops;
8805 else
8806 return &base_breakpoint_ops;
8807 }
8808 else
8809 {
8810 if (location_type == PROBE_LOCATION)
8811 return &bkpt_probe_breakpoint_ops;
8812 else
8813 return &base_breakpoint_ops;
8814 }
8815 }
8816
8817 /* See breakpoint.h. */
8818
8819 const struct breakpoint_ops *
8820 breakpoint_ops_for_event_location (const struct event_location *location,
8821 bool is_tracepoint)
8822 {
8823 if (location != nullptr)
8824 return breakpoint_ops_for_event_location_type
8825 (event_location_type (location), is_tracepoint);
8826 return &base_breakpoint_ops;
8827 }
8828
8829 /* See breakpoint.h. */
8830
8831 int
8832 create_breakpoint (struct gdbarch *gdbarch,
8833 struct event_location *location,
8834 const char *cond_string,
8835 int thread, const char *extra_string,
8836 bool force_condition, int parse_extra,
8837 int tempflag, enum bptype type_wanted,
8838 int ignore_count,
8839 enum auto_boolean pending_break_support,
8840 const struct breakpoint_ops *ops,
8841 int from_tty, int enabled, int internal,
8842 unsigned flags)
8843 {
8844 struct linespec_result canonical;
8845 int pending = 0;
8846 int task = 0;
8847 int prev_bkpt_count = breakpoint_count;
8848
8849 gdb_assert (ops != NULL);
8850
8851 /* If extra_string isn't useful, set it to NULL. */
8852 if (extra_string != NULL && *extra_string == '\0')
8853 extra_string = NULL;
8854
8855 try
8856 {
8857 ops->create_sals_from_location (location, &canonical);
8858 }
8859 catch (const gdb_exception_error &e)
8860 {
8861 /* If caller is interested in rc value from parse, set
8862 value. */
8863 if (e.error == NOT_FOUND_ERROR)
8864 {
8865 /* If pending breakpoint support is turned off, throw
8866 error. */
8867
8868 if (pending_break_support == AUTO_BOOLEAN_FALSE)
8869 throw;
8870
8871 exception_print (gdb_stderr, e);
8872
8873 /* If pending breakpoint support is auto query and the user
8874 selects no, then simply return the error code. */
8875 if (pending_break_support == AUTO_BOOLEAN_AUTO
8876 && !nquery (_("Make %s pending on future shared library load? "),
8877 bptype_string (type_wanted)))
8878 return 0;
8879
8880 /* At this point, either the user was queried about setting
8881 a pending breakpoint and selected yes, or pending
8882 breakpoint behavior is on and thus a pending breakpoint
8883 is defaulted on behalf of the user. */
8884 pending = 1;
8885 }
8886 else
8887 throw;
8888 }
8889
8890 if (!pending && canonical.lsals.empty ())
8891 return 0;
8892
8893 /* Resolve all line numbers to PC's and verify that the addresses
8894 are ok for the target. */
8895 if (!pending)
8896 {
8897 for (auto &lsal : canonical.lsals)
8898 breakpoint_sals_to_pc (lsal.sals);
8899 }
8900
8901 /* Fast tracepoints may have additional restrictions on location. */
8902 if (!pending && type_wanted == bp_fast_tracepoint)
8903 {
8904 for (const auto &lsal : canonical.lsals)
8905 check_fast_tracepoint_sals (gdbarch, lsal.sals);
8906 }
8907
8908 /* Verify that condition can be parsed, before setting any
8909 breakpoints. Allocate a separate condition expression for each
8910 breakpoint. */
8911 if (!pending)
8912 {
8913 gdb::unique_xmalloc_ptr<char> cond_string_copy;
8914 gdb::unique_xmalloc_ptr<char> extra_string_copy;
8915
8916 if (parse_extra)
8917 {
8918 gdb::unique_xmalloc_ptr<char> rest;
8919 gdb::unique_xmalloc_ptr<char> cond;
8920
8921 const linespec_sals &lsal = canonical.lsals[0];
8922
8923 find_condition_and_thread_for_sals (lsal.sals, extra_string,
8924 &cond, &thread, &task, &rest);
8925 cond_string_copy = std::move (cond);
8926 extra_string_copy = std::move (rest);
8927 }
8928 else
8929 {
8930 if (type_wanted != bp_dprintf
8931 && extra_string != NULL && *extra_string != '\0')
8932 error (_("Garbage '%s' at end of location"), extra_string);
8933
8934 /* Check the validity of the condition. We should error out
8935 if the condition is invalid at all of the locations and
8936 if it is not forced. In the PARSE_EXTRA case above, this
8937 check is done when parsing the EXTRA_STRING. */
8938 if (cond_string != nullptr && !force_condition)
8939 {
8940 int num_failures = 0;
8941 const linespec_sals &lsal = canonical.lsals[0];
8942 for (const auto &sal : lsal.sals)
8943 {
8944 const char *cond = cond_string;
8945 try
8946 {
8947 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
8948 /* One success is sufficient to keep going. */
8949 break;
8950 }
8951 catch (const gdb_exception_error &)
8952 {
8953 num_failures++;
8954 /* If this is the last sal, error out. */
8955 if (num_failures == lsal.sals.size ())
8956 throw;
8957 }
8958 }
8959 }
8960
8961 /* Create a private copy of condition string. */
8962 if (cond_string)
8963 cond_string_copy.reset (xstrdup (cond_string));
8964 /* Create a private copy of any extra string. */
8965 if (extra_string)
8966 extra_string_copy.reset (xstrdup (extra_string));
8967 }
8968
8969 ops->create_breakpoints_sal (gdbarch, &canonical,
8970 std::move (cond_string_copy),
8971 std::move (extra_string_copy),
8972 type_wanted,
8973 tempflag ? disp_del : disp_donttouch,
8974 thread, task, ignore_count, ops,
8975 from_tty, enabled, internal, flags);
8976 }
8977 else
8978 {
8979 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
8980 type_wanted);
8981 b->location = copy_event_location (location);
8982
8983 if (parse_extra)
8984 b->cond_string = NULL;
8985 else
8986 {
8987 /* Create a private copy of condition string. */
8988 b->cond_string.reset (cond_string != NULL
8989 ? xstrdup (cond_string)
8990 : NULL);
8991 b->thread = thread;
8992 }
8993
8994 /* Create a private copy of any extra string. */
8995 b->extra_string.reset (extra_string != NULL
8996 ? xstrdup (extra_string)
8997 : NULL);
8998 b->ignore_count = ignore_count;
8999 b->disposition = tempflag ? disp_del : disp_donttouch;
9000 b->condition_not_parsed = 1;
9001 b->enable_state = enabled ? bp_enabled : bp_disabled;
9002 if ((type_wanted != bp_breakpoint
9003 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9004 b->pspace = current_program_space;
9005
9006 install_breakpoint (internal, std::move (b), 0);
9007 }
9008
9009 if (canonical.lsals.size () > 1)
9010 {
9011 warning (_("Multiple breakpoints were set.\nUse the "
9012 "\"delete\" command to delete unwanted breakpoints."));
9013 prev_breakpoint_count = prev_bkpt_count;
9014 }
9015
9016 update_global_location_list (UGLL_MAY_INSERT);
9017
9018 return 1;
9019 }
9020
9021 /* Set a breakpoint.
9022 ARG is a string describing breakpoint address,
9023 condition, and thread.
9024 FLAG specifies if a breakpoint is hardware on,
9025 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9026 and BP_TEMPFLAG. */
9027
9028 static void
9029 break_command_1 (const char *arg, int flag, int from_tty)
9030 {
9031 int tempflag = flag & BP_TEMPFLAG;
9032 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9033 ? bp_hardware_breakpoint
9034 : bp_breakpoint);
9035
9036 event_location_up location = string_to_event_location (&arg, current_language);
9037 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9038 (location.get (), false /* is_tracepoint */);
9039
9040 create_breakpoint (get_current_arch (),
9041 location.get (),
9042 NULL, 0, arg, false, 1 /* parse arg */,
9043 tempflag, type_wanted,
9044 0 /* Ignore count */,
9045 pending_break_support,
9046 ops,
9047 from_tty,
9048 1 /* enabled */,
9049 0 /* internal */,
9050 0);
9051 }
9052
9053 /* Helper function for break_command_1 and disassemble_command. */
9054
9055 void
9056 resolve_sal_pc (struct symtab_and_line *sal)
9057 {
9058 CORE_ADDR pc;
9059
9060 if (sal->pc == 0 && sal->symtab != NULL)
9061 {
9062 if (!find_line_pc (sal->symtab, sal->line, &pc))
9063 error (_("No line %d in file \"%s\"."),
9064 sal->line, symtab_to_filename_for_display (sal->symtab));
9065 sal->pc = pc;
9066
9067 /* If this SAL corresponds to a breakpoint inserted using a line
9068 number, then skip the function prologue if necessary. */
9069 if (sal->explicit_line)
9070 skip_prologue_sal (sal);
9071 }
9072
9073 if (sal->section == 0 && sal->symtab != NULL)
9074 {
9075 const struct blockvector *bv;
9076 const struct block *b;
9077 struct symbol *sym;
9078
9079 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9080 sal->symtab->compunit ());
9081 if (bv != NULL)
9082 {
9083 sym = block_linkage_function (b);
9084 if (sym != NULL)
9085 {
9086 fixup_symbol_section (sym, sal->symtab->compunit ()->objfile ());
9087 sal->section
9088 = sym->obj_section (sal->symtab->compunit ()->objfile ());
9089 }
9090 else
9091 {
9092 /* It really is worthwhile to have the section, so we'll
9093 just have to look harder. This case can be executed
9094 if we have line numbers but no functions (as can
9095 happen in assembly source). */
9096
9097 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9098 switch_to_program_space_and_thread (sal->pspace);
9099
9100 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9101 if (msym.minsym)
9102 sal->section = msym.obj_section ();
9103 }
9104 }
9105 }
9106 }
9107
9108 void
9109 break_command (const char *arg, int from_tty)
9110 {
9111 break_command_1 (arg, 0, from_tty);
9112 }
9113
9114 void
9115 tbreak_command (const char *arg, int from_tty)
9116 {
9117 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9118 }
9119
9120 static void
9121 hbreak_command (const char *arg, int from_tty)
9122 {
9123 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9124 }
9125
9126 static void
9127 thbreak_command (const char *arg, int from_tty)
9128 {
9129 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9130 }
9131
9132 /* The dynamic printf command is mostly like a regular breakpoint, but
9133 with a prewired command list consisting of a single output command,
9134 built from extra arguments supplied on the dprintf command
9135 line. */
9136
9137 static void
9138 dprintf_command (const char *arg, int from_tty)
9139 {
9140 event_location_up location = string_to_event_location (&arg, current_language);
9141
9142 /* If non-NULL, ARG should have been advanced past the location;
9143 the next character must be ','. */
9144 if (arg != NULL)
9145 {
9146 if (arg[0] != ',' || arg[1] == '\0')
9147 error (_("Format string required"));
9148 else
9149 {
9150 /* Skip the comma. */
9151 ++arg;
9152 }
9153 }
9154
9155 create_breakpoint (get_current_arch (),
9156 location.get (),
9157 NULL, 0, arg, false, 1 /* parse arg */,
9158 0, bp_dprintf,
9159 0 /* Ignore count */,
9160 pending_break_support,
9161 &base_breakpoint_ops,
9162 from_tty,
9163 1 /* enabled */,
9164 0 /* internal */,
9165 0);
9166 }
9167
9168 static void
9169 agent_printf_command (const char *arg, int from_tty)
9170 {
9171 error (_("May only run agent-printf on the target"));
9172 }
9173
9174 /* Implement the "breakpoint_hit" method for ranged breakpoints. */
9175
9176 int
9177 ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9178 const address_space *aspace,
9179 CORE_ADDR bp_addr,
9180 const target_waitstatus &ws)
9181 {
9182 if (ws.kind () != TARGET_WAITKIND_STOPPED
9183 || ws.sig () != GDB_SIGNAL_TRAP)
9184 return 0;
9185
9186 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9187 bl->length, aspace, bp_addr);
9188 }
9189
9190 /* Implement the "resources_needed" method for ranged breakpoints. */
9191
9192 int
9193 ranged_breakpoint::resources_needed (const struct bp_location *bl)
9194 {
9195 return target_ranged_break_num_registers ();
9196 }
9197
9198 /* Implement the "print_it" method for ranged breakpoints. */
9199
9200 enum print_stop_action
9201 ranged_breakpoint::print_it (const bpstat *bs) const
9202 {
9203 struct bp_location *bl = loc;
9204 struct ui_out *uiout = current_uiout;
9205
9206 gdb_assert (type == bp_hardware_breakpoint);
9207
9208 /* Ranged breakpoints have only one location. */
9209 gdb_assert (bl && bl->next == NULL);
9210
9211 annotate_breakpoint (number);
9212
9213 maybe_print_thread_hit_breakpoint (uiout);
9214
9215 if (disposition == disp_del)
9216 uiout->text ("Temporary ranged breakpoint ");
9217 else
9218 uiout->text ("Ranged breakpoint ");
9219 if (uiout->is_mi_like_p ())
9220 {
9221 uiout->field_string ("reason",
9222 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9223 uiout->field_string ("disp", bpdisp_text (disposition));
9224 }
9225 uiout->field_signed ("bkptno", number);
9226 uiout->text (", ");
9227
9228 return PRINT_SRC_AND_LOC;
9229 }
9230
9231 /* Implement the "print_one" method for ranged breakpoints. */
9232
9233 bool
9234 ranged_breakpoint::print_one (bp_location **last_loc) const
9235 {
9236 struct bp_location *bl = loc;
9237 struct value_print_options opts;
9238 struct ui_out *uiout = current_uiout;
9239
9240 /* Ranged breakpoints have only one location. */
9241 gdb_assert (bl && bl->next == NULL);
9242
9243 get_user_print_options (&opts);
9244
9245 if (opts.addressprint)
9246 /* We don't print the address range here, it will be printed later
9247 by print_one_detail_ranged_breakpoint. */
9248 uiout->field_skip ("addr");
9249 annotate_field (5);
9250 print_breakpoint_location (this, bl);
9251 *last_loc = bl;
9252
9253 return true;
9254 }
9255
9256 /* Implement the "print_one_detail" method for ranged breakpoints. */
9257
9258 void
9259 ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
9260 {
9261 CORE_ADDR address_start, address_end;
9262 struct bp_location *bl = loc;
9263 string_file stb;
9264
9265 gdb_assert (bl);
9266
9267 address_start = bl->address;
9268 address_end = address_start + bl->length - 1;
9269
9270 uiout->text ("\taddress range: ");
9271 stb.printf ("[%s, %s]",
9272 print_core_address (bl->gdbarch, address_start),
9273 print_core_address (bl->gdbarch, address_end));
9274 uiout->field_stream ("addr", stb);
9275 uiout->text ("\n");
9276 }
9277
9278 /* Implement the "print_mention" method for ranged breakpoints. */
9279
9280 void
9281 ranged_breakpoint::print_mention () const
9282 {
9283 struct bp_location *bl = loc;
9284 struct ui_out *uiout = current_uiout;
9285
9286 gdb_assert (bl);
9287 gdb_assert (type == bp_hardware_breakpoint);
9288
9289 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9290 number, paddress (bl->gdbarch, bl->address),
9291 paddress (bl->gdbarch, bl->address + bl->length - 1));
9292 }
9293
9294 /* Implement the "print_recreate" method for ranged breakpoints. */
9295
9296 void
9297 ranged_breakpoint::print_recreate (struct ui_file *fp) const
9298 {
9299 gdb_printf (fp, "break-range %s, %s",
9300 event_location_to_string (location.get ()),
9301 event_location_to_string (location_range_end.get ()));
9302 print_recreate_thread (fp);
9303 }
9304
9305 /* Find the address where the end of the breakpoint range should be
9306 placed, given the SAL of the end of the range. This is so that if
9307 the user provides a line number, the end of the range is set to the
9308 last instruction of the given line. */
9309
9310 static CORE_ADDR
9311 find_breakpoint_range_end (struct symtab_and_line sal)
9312 {
9313 CORE_ADDR end;
9314
9315 /* If the user provided a PC value, use it. Otherwise,
9316 find the address of the end of the given location. */
9317 if (sal.explicit_pc)
9318 end = sal.pc;
9319 else
9320 {
9321 int ret;
9322 CORE_ADDR start;
9323
9324 ret = find_line_pc_range (sal, &start, &end);
9325 if (!ret)
9326 error (_("Could not find location of the end of the range."));
9327
9328 /* find_line_pc_range returns the start of the next line. */
9329 end--;
9330 }
9331
9332 return end;
9333 }
9334
9335 /* Implement the "break-range" CLI command. */
9336
9337 static void
9338 break_range_command (const char *arg, int from_tty)
9339 {
9340 const char *arg_start;
9341 struct linespec_result canonical_start, canonical_end;
9342 int bp_count, can_use_bp, length;
9343 CORE_ADDR end;
9344 struct breakpoint *b;
9345
9346 /* We don't support software ranged breakpoints. */
9347 if (target_ranged_break_num_registers () < 0)
9348 error (_("This target does not support hardware ranged breakpoints."));
9349
9350 bp_count = hw_breakpoint_used_count ();
9351 bp_count += target_ranged_break_num_registers ();
9352 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9353 bp_count, 0);
9354 if (can_use_bp < 0)
9355 error (_("Hardware breakpoints used exceeds limit."));
9356
9357 arg = skip_spaces (arg);
9358 if (arg == NULL || arg[0] == '\0')
9359 error(_("No address range specified."));
9360
9361 arg_start = arg;
9362 event_location_up start_location = string_to_event_location (&arg,
9363 current_language);
9364 parse_breakpoint_sals (start_location.get (), &canonical_start);
9365
9366 if (arg[0] != ',')
9367 error (_("Too few arguments."));
9368 else if (canonical_start.lsals.empty ())
9369 error (_("Could not find location of the beginning of the range."));
9370
9371 const linespec_sals &lsal_start = canonical_start.lsals[0];
9372
9373 if (canonical_start.lsals.size () > 1
9374 || lsal_start.sals.size () != 1)
9375 error (_("Cannot create a ranged breakpoint with multiple locations."));
9376
9377 const symtab_and_line &sal_start = lsal_start.sals[0];
9378 std::string addr_string_start (arg_start, arg - arg_start);
9379
9380 arg++; /* Skip the comma. */
9381 arg = skip_spaces (arg);
9382
9383 /* Parse the end location. */
9384
9385 arg_start = arg;
9386
9387 /* We call decode_line_full directly here instead of using
9388 parse_breakpoint_sals because we need to specify the start location's
9389 symtab and line as the default symtab and line for the end of the
9390 range. This makes it possible to have ranges like "foo.c:27, +14",
9391 where +14 means 14 lines from the start location. */
9392 event_location_up end_location = string_to_event_location (&arg,
9393 current_language);
9394 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9395 sal_start.symtab, sal_start.line,
9396 &canonical_end, NULL, NULL);
9397
9398 if (canonical_end.lsals.empty ())
9399 error (_("Could not find location of the end of the range."));
9400
9401 const linespec_sals &lsal_end = canonical_end.lsals[0];
9402 if (canonical_end.lsals.size () > 1
9403 || lsal_end.sals.size () != 1)
9404 error (_("Cannot create a ranged breakpoint with multiple locations."));
9405
9406 const symtab_and_line &sal_end = lsal_end.sals[0];
9407
9408 end = find_breakpoint_range_end (sal_end);
9409 if (sal_start.pc > end)
9410 error (_("Invalid address range, end precedes start."));
9411
9412 length = end - sal_start.pc + 1;
9413 if (length < 0)
9414 /* Length overflowed. */
9415 error (_("Address range too large."));
9416 else if (length == 1)
9417 {
9418 /* This range is simple enough to be handled by
9419 the `hbreak' command. */
9420 hbreak_command (&addr_string_start[0], 1);
9421
9422 return;
9423 }
9424
9425 /* Now set up the breakpoint. */
9426 std::unique_ptr<breakpoint> br (new ranged_breakpoint (get_current_arch ()));
9427 init_raw_breakpoint (br.get (), sal_start, bp_hardware_breakpoint);
9428 b = add_to_breakpoint_chain (std::move (br));
9429
9430 set_breakpoint_count (breakpoint_count + 1);
9431 b->number = breakpoint_count;
9432 b->disposition = disp_donttouch;
9433 b->location = std::move (start_location);
9434 b->location_range_end = std::move (end_location);
9435 b->loc->length = length;
9436
9437 mention (b);
9438 gdb::observers::breakpoint_created.notify (b);
9439 update_global_location_list (UGLL_MAY_INSERT);
9440 }
9441
9442 /* Return non-zero if EXP is verified as constant. Returned zero
9443 means EXP is variable. Also the constant detection may fail for
9444 some constant expressions and in such case still falsely return
9445 zero. */
9446
9447 static bool
9448 watchpoint_exp_is_const (const struct expression *exp)
9449 {
9450 return exp->op->constant_p ();
9451 }
9452
9453 /* Implement the "re_set" method for watchpoints. */
9454
9455 void
9456 watchpoint::re_set ()
9457 {
9458 /* Watchpoint can be either on expression using entirely global
9459 variables, or it can be on local variables.
9460
9461 Watchpoints of the first kind are never auto-deleted, and even
9462 persist across program restarts. Since they can use variables
9463 from shared libraries, we need to reparse expression as libraries
9464 are loaded and unloaded.
9465
9466 Watchpoints on local variables can also change meaning as result
9467 of solib event. For example, if a watchpoint uses both a local
9468 and a global variables in expression, it's a local watchpoint,
9469 but unloading of a shared library will make the expression
9470 invalid. This is not a very common use case, but we still
9471 re-evaluate expression, to avoid surprises to the user.
9472
9473 Note that for local watchpoints, we re-evaluate it only if
9474 watchpoints frame id is still valid. If it's not, it means the
9475 watchpoint is out of scope and will be deleted soon. In fact,
9476 I'm not sure we'll ever be called in this case.
9477
9478 If a local watchpoint's frame id is still valid, then
9479 exp_valid_block is likewise valid, and we can safely use it.
9480
9481 Don't do anything about disabled watchpoints, since they will be
9482 reevaluated again when enabled. */
9483 update_watchpoint (this, 1 /* reparse */);
9484 }
9485
9486 /* Implement the "insert" method for hardware watchpoints. */
9487
9488 int
9489 watchpoint::insert_location (struct bp_location *bl)
9490 {
9491 int length = exact ? 1 : bl->length;
9492
9493 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9494 cond_exp.get ());
9495 }
9496
9497 /* Implement the "remove" method for hardware watchpoints. */
9498
9499 int
9500 watchpoint::remove_location (struct bp_location *bl,
9501 enum remove_bp_reason reason)
9502 {
9503 int length = exact ? 1 : bl->length;
9504
9505 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9506 cond_exp.get ());
9507 }
9508
9509 int
9510 watchpoint::breakpoint_hit (const struct bp_location *bl,
9511 const address_space *aspace, CORE_ADDR bp_addr,
9512 const target_waitstatus &ws)
9513 {
9514 struct breakpoint *b = bl->owner;
9515
9516 /* Continuable hardware watchpoints are treated as non-existent if the
9517 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9518 some data address). Otherwise gdb won't stop on a break instruction
9519 in the code (not from a breakpoint) when a hardware watchpoint has
9520 been defined. Also skip watchpoints which we know did not trigger
9521 (did not match the data address). */
9522 if (is_hardware_watchpoint (b)
9523 && watchpoint_triggered == watch_triggered_no)
9524 return 0;
9525
9526 return 1;
9527 }
9528
9529 void
9530 watchpoint::check_status (bpstat *bs)
9531 {
9532 gdb_assert (is_watchpoint (bs->breakpoint_at));
9533
9534 bpstat_check_watchpoint (bs);
9535 }
9536
9537 /* Implement the "resources_needed" method for hardware
9538 watchpoints. */
9539
9540 int
9541 watchpoint::resources_needed (const struct bp_location *bl)
9542 {
9543 int length = exact? 1 : bl->length;
9544
9545 return target_region_ok_for_hw_watchpoint (bl->address, length);
9546 }
9547
9548 /* Implement the "works_in_software_mode" method for hardware
9549 watchpoints. */
9550
9551 bool
9552 watchpoint::works_in_software_mode () const
9553 {
9554 /* Read and access watchpoints only work with hardware support. */
9555 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9556 }
9557
9558 enum print_stop_action
9559 watchpoint::print_it (const bpstat *bs) const
9560 {
9561 struct breakpoint *b;
9562 enum print_stop_action result;
9563 struct ui_out *uiout = current_uiout;
9564
9565 gdb_assert (bs->bp_location_at != NULL);
9566
9567 b = bs->breakpoint_at;
9568
9569 annotate_watchpoint (b->number);
9570 maybe_print_thread_hit_breakpoint (uiout);
9571
9572 string_file stb;
9573
9574 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9575 switch (b->type)
9576 {
9577 case bp_watchpoint:
9578 case bp_hardware_watchpoint:
9579 if (uiout->is_mi_like_p ())
9580 uiout->field_string
9581 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9582 mention (b);
9583 tuple_emitter.emplace (uiout, "value");
9584 uiout->text ("\nOld value = ");
9585 watchpoint_value_print (bs->old_val.get (), &stb);
9586 uiout->field_stream ("old", stb);
9587 uiout->text ("\nNew value = ");
9588 watchpoint_value_print (val.get (), &stb);
9589 uiout->field_stream ("new", stb);
9590 uiout->text ("\n");
9591 /* More than one watchpoint may have been triggered. */
9592 result = PRINT_UNKNOWN;
9593 break;
9594
9595 case bp_read_watchpoint:
9596 if (uiout->is_mi_like_p ())
9597 uiout->field_string
9598 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9599 mention (b);
9600 tuple_emitter.emplace (uiout, "value");
9601 uiout->text ("\nValue = ");
9602 watchpoint_value_print (val.get (), &stb);
9603 uiout->field_stream ("value", stb);
9604 uiout->text ("\n");
9605 result = PRINT_UNKNOWN;
9606 break;
9607
9608 case bp_access_watchpoint:
9609 if (bs->old_val != NULL)
9610 {
9611 if (uiout->is_mi_like_p ())
9612 uiout->field_string
9613 ("reason",
9614 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9615 mention (b);
9616 tuple_emitter.emplace (uiout, "value");
9617 uiout->text ("\nOld value = ");
9618 watchpoint_value_print (bs->old_val.get (), &stb);
9619 uiout->field_stream ("old", stb);
9620 uiout->text ("\nNew value = ");
9621 }
9622 else
9623 {
9624 mention (b);
9625 if (uiout->is_mi_like_p ())
9626 uiout->field_string
9627 ("reason",
9628 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9629 tuple_emitter.emplace (uiout, "value");
9630 uiout->text ("\nValue = ");
9631 }
9632 watchpoint_value_print (val.get (), &stb);
9633 uiout->field_stream ("new", stb);
9634 uiout->text ("\n");
9635 result = PRINT_UNKNOWN;
9636 break;
9637 default:
9638 result = PRINT_UNKNOWN;
9639 }
9640
9641 return result;
9642 }
9643
9644 /* Implement the "print_mention" method for hardware watchpoints. */
9645
9646 void
9647 watchpoint::print_mention () const
9648 {
9649 struct ui_out *uiout = current_uiout;
9650 const char *tuple_name;
9651
9652 switch (type)
9653 {
9654 case bp_watchpoint:
9655 uiout->text ("Watchpoint ");
9656 tuple_name = "wpt";
9657 break;
9658 case bp_hardware_watchpoint:
9659 uiout->text ("Hardware watchpoint ");
9660 tuple_name = "wpt";
9661 break;
9662 case bp_read_watchpoint:
9663 uiout->text ("Hardware read watchpoint ");
9664 tuple_name = "hw-rwpt";
9665 break;
9666 case bp_access_watchpoint:
9667 uiout->text ("Hardware access (read/write) watchpoint ");
9668 tuple_name = "hw-awpt";
9669 break;
9670 default:
9671 internal_error (__FILE__, __LINE__,
9672 _("Invalid hardware watchpoint type."));
9673 }
9674
9675 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9676 uiout->field_signed ("number", number);
9677 uiout->text (": ");
9678 uiout->field_string ("exp", exp_string.get ());
9679 }
9680
9681 /* Implement the "print_recreate" method for watchpoints. */
9682
9683 void
9684 watchpoint::print_recreate (struct ui_file *fp) const
9685 {
9686 switch (type)
9687 {
9688 case bp_watchpoint:
9689 case bp_hardware_watchpoint:
9690 gdb_printf (fp, "watch");
9691 break;
9692 case bp_read_watchpoint:
9693 gdb_printf (fp, "rwatch");
9694 break;
9695 case bp_access_watchpoint:
9696 gdb_printf (fp, "awatch");
9697 break;
9698 default:
9699 internal_error (__FILE__, __LINE__,
9700 _("Invalid watchpoint type."));
9701 }
9702
9703 gdb_printf (fp, " %s", exp_string.get ());
9704 print_recreate_thread (fp);
9705 }
9706
9707 /* Implement the "explains_signal" method for watchpoints. */
9708
9709 bool
9710 watchpoint::explains_signal (enum gdb_signal sig)
9711 {
9712 /* A software watchpoint cannot cause a signal other than
9713 GDB_SIGNAL_TRAP. */
9714 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
9715 return false;
9716
9717 return true;
9718 }
9719
9720 struct masked_watchpoint : public watchpoint
9721 {
9722 using watchpoint::watchpoint;
9723
9724 int insert_location (struct bp_location *) override;
9725 int remove_location (struct bp_location *,
9726 enum remove_bp_reason reason) override;
9727 int resources_needed (const struct bp_location *) override;
9728 bool works_in_software_mode () const override;
9729 enum print_stop_action print_it (const bpstat *bs) const override;
9730 void print_one_detail (struct ui_out *) const override;
9731 void print_mention () const override;
9732 void print_recreate (struct ui_file *fp) const override;
9733 };
9734
9735 /* Implement the "insert" method for masked hardware watchpoints. */
9736
9737 int
9738 masked_watchpoint::insert_location (struct bp_location *bl)
9739 {
9740 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
9741 bl->watchpoint_type);
9742 }
9743
9744 /* Implement the "remove" method for masked hardware watchpoints. */
9745
9746 int
9747 masked_watchpoint::remove_location (struct bp_location *bl,
9748 enum remove_bp_reason reason)
9749 {
9750 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
9751 bl->watchpoint_type);
9752 }
9753
9754 /* Implement the "resources_needed" method for masked hardware
9755 watchpoints. */
9756
9757 int
9758 masked_watchpoint::resources_needed (const struct bp_location *bl)
9759 {
9760 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
9761 }
9762
9763 /* Implement the "works_in_software_mode" method for masked hardware
9764 watchpoints. */
9765
9766 bool
9767 masked_watchpoint::works_in_software_mode () const
9768 {
9769 return false;
9770 }
9771
9772 /* Implement the "print_it" method for masked hardware
9773 watchpoints. */
9774
9775 enum print_stop_action
9776 masked_watchpoint::print_it (const bpstat *bs) const
9777 {
9778 struct breakpoint *b = bs->breakpoint_at;
9779 struct ui_out *uiout = current_uiout;
9780
9781 /* Masked watchpoints have only one location. */
9782 gdb_assert (b->loc && b->loc->next == NULL);
9783
9784 annotate_watchpoint (b->number);
9785 maybe_print_thread_hit_breakpoint (uiout);
9786
9787 switch (b->type)
9788 {
9789 case bp_hardware_watchpoint:
9790 if (uiout->is_mi_like_p ())
9791 uiout->field_string
9792 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9793 break;
9794
9795 case bp_read_watchpoint:
9796 if (uiout->is_mi_like_p ())
9797 uiout->field_string
9798 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9799 break;
9800
9801 case bp_access_watchpoint:
9802 if (uiout->is_mi_like_p ())
9803 uiout->field_string
9804 ("reason",
9805 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9806 break;
9807 default:
9808 internal_error (__FILE__, __LINE__,
9809 _("Invalid hardware watchpoint type."));
9810 }
9811
9812 mention (b);
9813 uiout->text (_("\n\
9814 Check the underlying instruction at PC for the memory\n\
9815 address and value which triggered this watchpoint.\n"));
9816 uiout->text ("\n");
9817
9818 /* More than one watchpoint may have been triggered. */
9819 return PRINT_UNKNOWN;
9820 }
9821
9822 /* Implement the "print_one_detail" method for masked hardware
9823 watchpoints. */
9824
9825 void
9826 masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9827 {
9828 /* Masked watchpoints have only one location. */
9829 gdb_assert (loc && loc->next == NULL);
9830
9831 uiout->text ("\tmask ");
9832 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
9833 uiout->text ("\n");
9834 }
9835
9836 /* Implement the "print_mention" method for masked hardware
9837 watchpoints. */
9838
9839 void
9840 masked_watchpoint::print_mention () const
9841 {
9842 struct ui_out *uiout = current_uiout;
9843 const char *tuple_name;
9844
9845 switch (type)
9846 {
9847 case bp_hardware_watchpoint:
9848 uiout->text ("Masked hardware watchpoint ");
9849 tuple_name = "wpt";
9850 break;
9851 case bp_read_watchpoint:
9852 uiout->text ("Masked hardware read watchpoint ");
9853 tuple_name = "hw-rwpt";
9854 break;
9855 case bp_access_watchpoint:
9856 uiout->text ("Masked hardware access (read/write) watchpoint ");
9857 tuple_name = "hw-awpt";
9858 break;
9859 default:
9860 internal_error (__FILE__, __LINE__,
9861 _("Invalid hardware watchpoint type."));
9862 }
9863
9864 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9865 uiout->field_signed ("number", number);
9866 uiout->text (": ");
9867 uiout->field_string ("exp", exp_string.get ());
9868 }
9869
9870 /* Implement the "print_recreate" method for masked hardware
9871 watchpoints. */
9872
9873 void
9874 masked_watchpoint::print_recreate (struct ui_file *fp) const
9875 {
9876 switch (type)
9877 {
9878 case bp_hardware_watchpoint:
9879 gdb_printf (fp, "watch");
9880 break;
9881 case bp_read_watchpoint:
9882 gdb_printf (fp, "rwatch");
9883 break;
9884 case bp_access_watchpoint:
9885 gdb_printf (fp, "awatch");
9886 break;
9887 default:
9888 internal_error (__FILE__, __LINE__,
9889 _("Invalid hardware watchpoint type."));
9890 }
9891
9892 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
9893 phex (hw_wp_mask, sizeof (CORE_ADDR)));
9894 print_recreate_thread (fp);
9895 }
9896
9897 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
9898
9899 static bool
9900 is_masked_watchpoint (const struct breakpoint *b)
9901 {
9902 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9903 }
9904
9905 /* accessflag: hw_write: watch write,
9906 hw_read: watch read,
9907 hw_access: watch access (read or write) */
9908 static void
9909 watch_command_1 (const char *arg, int accessflag, int from_tty,
9910 bool just_location, bool internal)
9911 {
9912 struct breakpoint *scope_breakpoint = NULL;
9913 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
9914 struct value *result;
9915 int saved_bitpos = 0, saved_bitsize = 0;
9916 const char *exp_start = NULL;
9917 const char *exp_end = NULL;
9918 const char *tok, *end_tok;
9919 int toklen = -1;
9920 const char *cond_start = NULL;
9921 const char *cond_end = NULL;
9922 enum bptype bp_type;
9923 int thread = -1;
9924 /* Flag to indicate whether we are going to use masks for
9925 the hardware watchpoint. */
9926 bool use_mask = false;
9927 CORE_ADDR mask = 0;
9928 int task = 0;
9929
9930 /* Make sure that we actually have parameters to parse. */
9931 if (arg != NULL && arg[0] != '\0')
9932 {
9933 const char *value_start;
9934
9935 exp_end = arg + strlen (arg);
9936
9937 /* Look for "parameter value" pairs at the end
9938 of the arguments string. */
9939 for (tok = exp_end - 1; tok > arg; tok--)
9940 {
9941 /* Skip whitespace at the end of the argument list. */
9942 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9943 tok--;
9944
9945 /* Find the beginning of the last token.
9946 This is the value of the parameter. */
9947 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9948 tok--;
9949 value_start = tok + 1;
9950
9951 /* Skip whitespace. */
9952 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9953 tok--;
9954
9955 end_tok = tok;
9956
9957 /* Find the beginning of the second to last token.
9958 This is the parameter itself. */
9959 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9960 tok--;
9961 tok++;
9962 toklen = end_tok - tok + 1;
9963
9964 if (toklen == 6 && startswith (tok, "thread"))
9965 {
9966 struct thread_info *thr;
9967 /* At this point we've found a "thread" token, which means
9968 the user is trying to set a watchpoint that triggers
9969 only in a specific thread. */
9970 const char *endp;
9971
9972 if (thread != -1)
9973 error(_("You can specify only one thread."));
9974
9975 /* Extract the thread ID from the next token. */
9976 thr = parse_thread_id (value_start, &endp);
9977
9978 /* Check if the user provided a valid thread ID. */
9979 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9980 invalid_thread_id_error (value_start);
9981
9982 thread = thr->global_num;
9983 }
9984 else if (toklen == 4 && startswith (tok, "task"))
9985 {
9986 char *tmp;
9987
9988 task = strtol (value_start, &tmp, 0);
9989 if (tmp == value_start)
9990 error (_("Junk after task keyword."));
9991 if (!valid_task_id (task))
9992 error (_("Unknown task %d."), task);
9993 }
9994 else if (toklen == 4 && startswith (tok, "mask"))
9995 {
9996 /* We've found a "mask" token, which means the user wants to
9997 create a hardware watchpoint that is going to have the mask
9998 facility. */
9999 struct value *mask_value, *mark;
10000
10001 if (use_mask)
10002 error(_("You can specify only one mask."));
10003
10004 use_mask = just_location = true;
10005
10006 mark = value_mark ();
10007 mask_value = parse_to_comma_and_eval (&value_start);
10008 mask = value_as_address (mask_value);
10009 value_free_to_mark (mark);
10010 }
10011 else
10012 /* We didn't recognize what we found. We should stop here. */
10013 break;
10014
10015 /* Truncate the string and get rid of the "parameter value" pair before
10016 the arguments string is parsed by the parse_exp_1 function. */
10017 exp_end = tok;
10018 }
10019 }
10020 else
10021 exp_end = arg;
10022
10023 /* Parse the rest of the arguments. From here on out, everything
10024 is in terms of a newly allocated string instead of the original
10025 ARG. */
10026 std::string expression (arg, exp_end - arg);
10027 exp_start = arg = expression.c_str ();
10028 innermost_block_tracker tracker;
10029 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10030 exp_end = arg;
10031 /* Remove trailing whitespace from the expression before saving it.
10032 This makes the eventual display of the expression string a bit
10033 prettier. */
10034 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10035 --exp_end;
10036
10037 /* Checking if the expression is not constant. */
10038 if (watchpoint_exp_is_const (exp.get ()))
10039 {
10040 int len;
10041
10042 len = exp_end - exp_start;
10043 while (len > 0 && isspace (exp_start[len - 1]))
10044 len--;
10045 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10046 }
10047
10048 exp_valid_block = tracker.block ();
10049 struct value *mark = value_mark ();
10050 struct value *val_as_value = nullptr;
10051 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10052 just_location);
10053
10054 if (val_as_value != NULL && just_location)
10055 {
10056 saved_bitpos = value_bitpos (val_as_value);
10057 saved_bitsize = value_bitsize (val_as_value);
10058 }
10059
10060 value_ref_ptr val;
10061 if (just_location)
10062 {
10063 int ret;
10064
10065 exp_valid_block = NULL;
10066 val = release_value (value_addr (result));
10067 value_free_to_mark (mark);
10068
10069 if (use_mask)
10070 {
10071 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10072 mask);
10073 if (ret == -1)
10074 error (_("This target does not support masked watchpoints."));
10075 else if (ret == -2)
10076 error (_("Invalid mask or memory region."));
10077 }
10078 }
10079 else if (val_as_value != NULL)
10080 val = release_value (val_as_value);
10081
10082 tok = skip_spaces (arg);
10083 end_tok = skip_to_space (tok);
10084
10085 toklen = end_tok - tok;
10086 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10087 {
10088 tok = cond_start = end_tok + 1;
10089 innermost_block_tracker if_tracker;
10090 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10091
10092 /* The watchpoint expression may not be local, but the condition
10093 may still be. E.g.: `watch global if local > 0'. */
10094 cond_exp_valid_block = if_tracker.block ();
10095
10096 cond_end = tok;
10097 }
10098 if (*tok)
10099 error (_("Junk at end of command."));
10100
10101 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10102
10103 /* Save this because create_internal_breakpoint below invalidates
10104 'wp_frame'. */
10105 frame_id watchpoint_frame = get_frame_id (wp_frame);
10106
10107 /* If the expression is "local", then set up a "watchpoint scope"
10108 breakpoint at the point where we've left the scope of the watchpoint
10109 expression. Create the scope breakpoint before the watchpoint, so
10110 that we will encounter it first in bpstat_stop_status. */
10111 if (exp_valid_block != NULL && wp_frame != NULL)
10112 {
10113 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10114
10115 if (frame_id_p (caller_frame_id))
10116 {
10117 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10118 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10119
10120 scope_breakpoint
10121 = create_internal_breakpoint (caller_arch, caller_pc,
10122 bp_watchpoint_scope);
10123
10124 /* create_internal_breakpoint could invalidate WP_FRAME. */
10125 wp_frame = NULL;
10126
10127 scope_breakpoint->enable_state = bp_enabled;
10128
10129 /* Automatically delete the breakpoint when it hits. */
10130 scope_breakpoint->disposition = disp_del;
10131
10132 /* Only break in the proper frame (help with recursion). */
10133 scope_breakpoint->frame_id = caller_frame_id;
10134
10135 /* Set the address at which we will stop. */
10136 scope_breakpoint->loc->gdbarch = caller_arch;
10137 scope_breakpoint->loc->requested_address = caller_pc;
10138 scope_breakpoint->loc->address
10139 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10140 scope_breakpoint->loc->requested_address,
10141 scope_breakpoint->type);
10142 }
10143 }
10144
10145 /* Now set up the breakpoint. We create all watchpoints as hardware
10146 watchpoints here even if hardware watchpoints are turned off, a call
10147 to update_watchpoint later in this function will cause the type to
10148 drop back to bp_watchpoint (software watchpoint) if required. */
10149
10150 if (accessflag == hw_read)
10151 bp_type = bp_read_watchpoint;
10152 else if (accessflag == hw_access)
10153 bp_type = bp_access_watchpoint;
10154 else
10155 bp_type = bp_hardware_watchpoint;
10156
10157 std::unique_ptr<watchpoint> w;
10158 if (use_mask)
10159 w.reset (new masked_watchpoint (nullptr, bp_type));
10160 else
10161 w.reset (new watchpoint (nullptr, bp_type));
10162
10163 w->thread = thread;
10164 w->task = task;
10165 w->disposition = disp_donttouch;
10166 w->pspace = current_program_space;
10167 w->exp = std::move (exp);
10168 w->exp_valid_block = exp_valid_block;
10169 w->cond_exp_valid_block = cond_exp_valid_block;
10170 if (just_location)
10171 {
10172 struct type *t = value_type (val.get ());
10173 CORE_ADDR addr = value_as_address (val.get ());
10174
10175 w->exp_string_reparse
10176 = current_language->watch_location_expression (t, addr);
10177
10178 w->exp_string = xstrprintf ("-location %.*s",
10179 (int) (exp_end - exp_start), exp_start);
10180 }
10181 else
10182 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10183
10184 if (use_mask)
10185 {
10186 w->hw_wp_mask = mask;
10187 }
10188 else
10189 {
10190 w->val = val;
10191 w->val_bitpos = saved_bitpos;
10192 w->val_bitsize = saved_bitsize;
10193 w->val_valid = true;
10194 }
10195
10196 if (cond_start)
10197 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10198 else
10199 w->cond_string = 0;
10200
10201 if (frame_id_p (watchpoint_frame))
10202 {
10203 w->watchpoint_frame = watchpoint_frame;
10204 w->watchpoint_thread = inferior_ptid;
10205 }
10206 else
10207 {
10208 w->watchpoint_frame = null_frame_id;
10209 w->watchpoint_thread = null_ptid;
10210 }
10211
10212 if (scope_breakpoint != NULL)
10213 {
10214 /* The scope breakpoint is related to the watchpoint. We will
10215 need to act on them together. */
10216 w->related_breakpoint = scope_breakpoint;
10217 scope_breakpoint->related_breakpoint = w.get ();
10218 }
10219
10220 if (!just_location)
10221 value_free_to_mark (mark);
10222
10223 /* Finally update the new watchpoint. This creates the locations
10224 that should be inserted. */
10225 update_watchpoint (w.get (), 1);
10226
10227 install_breakpoint (internal, std::move (w), 1);
10228 }
10229
10230 /* Return count of debug registers needed to watch the given expression.
10231 If the watchpoint cannot be handled in hardware return zero. */
10232
10233 static int
10234 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10235 {
10236 int found_memory_cnt = 0;
10237
10238 /* Did the user specifically forbid us to use hardware watchpoints? */
10239 if (!can_use_hw_watchpoints)
10240 return 0;
10241
10242 gdb_assert (!vals.empty ());
10243 struct value *head = vals[0].get ();
10244
10245 /* Make sure that the value of the expression depends only upon
10246 memory contents, and values computed from them within GDB. If we
10247 find any register references or function calls, we can't use a
10248 hardware watchpoint.
10249
10250 The idea here is that evaluating an expression generates a series
10251 of values, one holding the value of every subexpression. (The
10252 expression a*b+c has five subexpressions: a, b, a*b, c, and
10253 a*b+c.) GDB's values hold almost enough information to establish
10254 the criteria given above --- they identify memory lvalues,
10255 register lvalues, computed values, etcetera. So we can evaluate
10256 the expression, and then scan the chain of values that leaves
10257 behind to decide whether we can detect any possible change to the
10258 expression's final value using only hardware watchpoints.
10259
10260 However, I don't think that the values returned by inferior
10261 function calls are special in any way. So this function may not
10262 notice that an expression involving an inferior function call
10263 can't be watched with hardware watchpoints. FIXME. */
10264 for (const value_ref_ptr &iter : vals)
10265 {
10266 struct value *v = iter.get ();
10267
10268 if (VALUE_LVAL (v) == lval_memory)
10269 {
10270 if (v != head && value_lazy (v))
10271 /* A lazy memory lvalue in the chain is one that GDB never
10272 needed to fetch; we either just used its address (e.g.,
10273 `a' in `a.b') or we never needed it at all (e.g., `a'
10274 in `a,b'). This doesn't apply to HEAD; if that is
10275 lazy then it was not readable, but watch it anyway. */
10276 ;
10277 else
10278 {
10279 /* Ahh, memory we actually used! Check if we can cover
10280 it with hardware watchpoints. */
10281 struct type *vtype = check_typedef (value_type (v));
10282
10283 /* We only watch structs and arrays if user asked for it
10284 explicitly, never if they just happen to appear in a
10285 middle of some value chain. */
10286 if (v == head
10287 || (vtype->code () != TYPE_CODE_STRUCT
10288 && vtype->code () != TYPE_CODE_ARRAY))
10289 {
10290 CORE_ADDR vaddr = value_address (v);
10291 int len;
10292 int num_regs;
10293
10294 len = (target_exact_watchpoints
10295 && is_scalar_type_recursive (vtype))?
10296 1 : TYPE_LENGTH (value_type (v));
10297
10298 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10299 if (!num_regs)
10300 return 0;
10301 else
10302 found_memory_cnt += num_regs;
10303 }
10304 }
10305 }
10306 else if (VALUE_LVAL (v) != not_lval
10307 && deprecated_value_modifiable (v) == 0)
10308 return 0; /* These are values from the history (e.g., $1). */
10309 else if (VALUE_LVAL (v) == lval_register)
10310 return 0; /* Cannot watch a register with a HW watchpoint. */
10311 }
10312
10313 /* The expression itself looks suitable for using a hardware
10314 watchpoint, but give the target machine a chance to reject it. */
10315 return found_memory_cnt;
10316 }
10317
10318 void
10319 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10320 {
10321 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10322 }
10323
10324 /* Options for the watch, awatch, and rwatch commands. */
10325
10326 struct watch_options
10327 {
10328 /* For -location. */
10329 bool location = false;
10330 };
10331
10332 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10333
10334 Historically GDB always accepted both '-location' and '-l' flags for
10335 these commands (both flags being synonyms). When converting to the
10336 newer option scheme only '-location' is added here. That's fine (for
10337 backward compatibility) as any non-ambiguous prefix of a flag will be
10338 accepted, so '-l', '-loc', are now all accepted.
10339
10340 What this means is that, if in the future, we add any new flag here
10341 that starts with '-l' then this will break backward compatibility, so
10342 please, don't do that! */
10343
10344 static const gdb::option::option_def watch_option_defs[] = {
10345 gdb::option::flag_option_def<watch_options> {
10346 "location",
10347 [] (watch_options *opt) { return &opt->location; },
10348 N_("\
10349 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10350 -l can be used as a short form of -location."),
10351 },
10352 };
10353
10354 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10355 commands. */
10356
10357 static gdb::option::option_def_group
10358 make_watch_options_def_group (watch_options *opts)
10359 {
10360 return {{watch_option_defs}, opts};
10361 }
10362
10363 /* A helper function that looks for the "-location" argument and then
10364 calls watch_command_1. */
10365
10366 static void
10367 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10368 {
10369 watch_options opts;
10370 auto grp = make_watch_options_def_group (&opts);
10371 gdb::option::process_options
10372 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10373 if (arg != nullptr && *arg == '\0')
10374 arg = nullptr;
10375
10376 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10377 }
10378
10379 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10380 static void
10381 watch_command_completer (struct cmd_list_element *ignore,
10382 completion_tracker &tracker,
10383 const char *text, const char * /*word*/)
10384 {
10385 const auto group = make_watch_options_def_group (nullptr);
10386 if (gdb::option::complete_options
10387 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10388 return;
10389
10390 const char *word = advance_to_expression_complete_word_point (tracker, text);
10391 expression_completer (ignore, tracker, text, word);
10392 }
10393
10394 static void
10395 watch_command (const char *arg, int from_tty)
10396 {
10397 watch_maybe_just_location (arg, hw_write, from_tty);
10398 }
10399
10400 void
10401 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10402 {
10403 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10404 }
10405
10406 static void
10407 rwatch_command (const char *arg, int from_tty)
10408 {
10409 watch_maybe_just_location (arg, hw_read, from_tty);
10410 }
10411
10412 void
10413 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
10414 {
10415 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10416 }
10417
10418 static void
10419 awatch_command (const char *arg, int from_tty)
10420 {
10421 watch_maybe_just_location (arg, hw_access, from_tty);
10422 }
10423 \f
10424
10425 /* Data for the FSM that manages the until(location)/advance commands
10426 in infcmd.c. Here because it uses the mechanisms of
10427 breakpoints. */
10428
10429 struct until_break_fsm : public thread_fsm
10430 {
10431 /* The thread that was current when the command was executed. */
10432 int thread;
10433
10434 /* The breakpoint set at the return address in the caller frame,
10435 plus breakpoints at all the destination locations. */
10436 std::vector<breakpoint_up> breakpoints;
10437
10438 until_break_fsm (struct interp *cmd_interp, int thread,
10439 std::vector<breakpoint_up> &&breakpoints)
10440 : thread_fsm (cmd_interp),
10441 thread (thread),
10442 breakpoints (std::move (breakpoints))
10443 {
10444 }
10445
10446 void clean_up (struct thread_info *thread) override;
10447 bool should_stop (struct thread_info *thread) override;
10448 enum async_reply_reason do_async_reply_reason () override;
10449 };
10450
10451 /* Implementation of the 'should_stop' FSM method for the
10452 until(location)/advance commands. */
10453
10454 bool
10455 until_break_fsm::should_stop (struct thread_info *tp)
10456 {
10457 for (const breakpoint_up &bp : breakpoints)
10458 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10459 bp.get ()) != NULL)
10460 {
10461 set_finished ();
10462 break;
10463 }
10464
10465 return true;
10466 }
10467
10468 /* Implementation of the 'clean_up' FSM method for the
10469 until(location)/advance commands. */
10470
10471 void
10472 until_break_fsm::clean_up (struct thread_info *)
10473 {
10474 /* Clean up our temporary breakpoints. */
10475 breakpoints.clear ();
10476 delete_longjmp_breakpoint (thread);
10477 }
10478
10479 /* Implementation of the 'async_reply_reason' FSM method for the
10480 until(location)/advance commands. */
10481
10482 enum async_reply_reason
10483 until_break_fsm::do_async_reply_reason ()
10484 {
10485 return EXEC_ASYNC_LOCATION_REACHED;
10486 }
10487
10488 void
10489 until_break_command (const char *arg, int from_tty, int anywhere)
10490 {
10491 struct frame_info *frame;
10492 struct gdbarch *frame_gdbarch;
10493 struct frame_id stack_frame_id;
10494 struct frame_id caller_frame_id;
10495 int thread;
10496 struct thread_info *tp;
10497
10498 clear_proceed_status (0);
10499
10500 /* Set a breakpoint where the user wants it and at return from
10501 this function. */
10502
10503 event_location_up location = string_to_event_location (&arg, current_language);
10504
10505 std::vector<symtab_and_line> sals
10506 = (last_displayed_sal_is_valid ()
10507 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10508 get_last_displayed_symtab (),
10509 get_last_displayed_line ())
10510 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
10511 NULL, NULL, 0));
10512
10513 if (sals.empty ())
10514 error (_("Couldn't get information on specified line."));
10515
10516 if (*arg)
10517 error (_("Junk at end of arguments."));
10518
10519 tp = inferior_thread ();
10520 thread = tp->global_num;
10521
10522 /* Note linespec handling above invalidates the frame chain.
10523 Installing a breakpoint also invalidates the frame chain (as it
10524 may need to switch threads), so do any frame handling before
10525 that. */
10526
10527 frame = get_selected_frame (NULL);
10528 frame_gdbarch = get_frame_arch (frame);
10529 stack_frame_id = get_stack_frame_id (frame);
10530 caller_frame_id = frame_unwind_caller_id (frame);
10531
10532 /* Keep within the current frame, or in frames called by the current
10533 one. */
10534
10535 std::vector<breakpoint_up> breakpoints;
10536
10537 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10538
10539 if (frame_id_p (caller_frame_id))
10540 {
10541 struct symtab_and_line sal2;
10542 struct gdbarch *caller_gdbarch;
10543
10544 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10545 sal2.pc = frame_unwind_caller_pc (frame);
10546 caller_gdbarch = frame_unwind_caller_arch (frame);
10547
10548 breakpoint_up caller_breakpoint
10549 = set_momentary_breakpoint (caller_gdbarch, sal2,
10550 caller_frame_id, bp_until);
10551 breakpoints.emplace_back (std::move (caller_breakpoint));
10552
10553 set_longjmp_breakpoint (tp, caller_frame_id);
10554 lj_deleter.emplace (thread);
10555 }
10556
10557 /* set_momentary_breakpoint could invalidate FRAME. */
10558 frame = NULL;
10559
10560 /* If the user told us to continue until a specified location, we
10561 don't specify a frame at which we need to stop. Otherwise,
10562 specify the selected frame, because we want to stop only at the
10563 very same frame. */
10564 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10565
10566 for (symtab_and_line &sal : sals)
10567 {
10568 resolve_sal_pc (&sal);
10569
10570 breakpoint_up location_breakpoint
10571 = set_momentary_breakpoint (frame_gdbarch, sal,
10572 stop_frame_id, bp_until);
10573 breakpoints.emplace_back (std::move (location_breakpoint));
10574 }
10575
10576 tp->set_thread_fsm
10577 (std::unique_ptr<thread_fsm>
10578 (new until_break_fsm (command_interp (), tp->global_num,
10579 std::move (breakpoints))));
10580
10581 if (lj_deleter)
10582 lj_deleter->release ();
10583
10584 proceed (-1, GDB_SIGNAL_DEFAULT);
10585 }
10586
10587 void
10588 init_ada_exception_breakpoint (struct breakpoint *b,
10589 struct gdbarch *gdbarch,
10590 struct symtab_and_line sal,
10591 const char *addr_string,
10592 int tempflag,
10593 int enabled,
10594 int from_tty)
10595 {
10596 if (from_tty)
10597 {
10598 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
10599 if (!loc_gdbarch)
10600 loc_gdbarch = gdbarch;
10601
10602 describe_other_breakpoints (loc_gdbarch,
10603 sal.pspace, sal.pc, sal.section, -1);
10604 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
10605 version for exception catchpoints, because two catchpoints
10606 used for different exception names will use the same address.
10607 In this case, a "breakpoint ... also set at..." warning is
10608 unproductive. Besides, the warning phrasing is also a bit
10609 inappropriate, we should use the word catchpoint, and tell
10610 the user what type of catchpoint it is. The above is good
10611 enough for now, though. */
10612 }
10613
10614 init_raw_breakpoint (b, sal, bp_catchpoint);
10615
10616 b->enable_state = enabled ? bp_enabled : bp_disabled;
10617 b->disposition = tempflag ? disp_del : disp_donttouch;
10618 b->location = string_to_event_location (&addr_string,
10619 language_def (language_ada));
10620 b->language = language_ada;
10621 }
10622
10623 \f
10624
10625 /* Compare two breakpoints and return a strcmp-like result. */
10626
10627 static int
10628 compare_breakpoints (const breakpoint *a, const breakpoint *b)
10629 {
10630 uintptr_t ua = (uintptr_t) a;
10631 uintptr_t ub = (uintptr_t) b;
10632
10633 if (a->number < b->number)
10634 return -1;
10635 else if (a->number > b->number)
10636 return 1;
10637
10638 /* Now sort by address, in case we see, e..g, two breakpoints with
10639 the number 0. */
10640 if (ua < ub)
10641 return -1;
10642 return ua > ub ? 1 : 0;
10643 }
10644
10645 /* Delete breakpoints by address or line. */
10646
10647 static void
10648 clear_command (const char *arg, int from_tty)
10649 {
10650 int default_match;
10651
10652 std::vector<symtab_and_line> decoded_sals;
10653 symtab_and_line last_sal;
10654 gdb::array_view<symtab_and_line> sals;
10655 if (arg)
10656 {
10657 decoded_sals
10658 = decode_line_with_current_source (arg,
10659 (DECODE_LINE_FUNFIRSTLINE
10660 | DECODE_LINE_LIST_MODE));
10661 default_match = 0;
10662 sals = decoded_sals;
10663 }
10664 else
10665 {
10666 /* Set sal's line, symtab, pc, and pspace to the values
10667 corresponding to the last call to print_frame_info. If the
10668 codepoint is not valid, this will set all the fields to 0. */
10669 last_sal = get_last_displayed_sal ();
10670 if (last_sal.symtab == 0)
10671 error (_("No source file specified."));
10672
10673 default_match = 1;
10674 sals = last_sal;
10675 }
10676
10677 /* We don't call resolve_sal_pc here. That's not as bad as it
10678 seems, because all existing breakpoints typically have both
10679 file/line and pc set. So, if clear is given file/line, we can
10680 match this to existing breakpoint without obtaining pc at all.
10681
10682 We only support clearing given the address explicitly
10683 present in breakpoint table. Say, we've set breakpoint
10684 at file:line. There were several PC values for that file:line,
10685 due to optimization, all in one block.
10686
10687 We've picked one PC value. If "clear" is issued with another
10688 PC corresponding to the same file:line, the breakpoint won't
10689 be cleared. We probably can still clear the breakpoint, but
10690 since the other PC value is never presented to user, user
10691 can only find it by guessing, and it does not seem important
10692 to support that. */
10693
10694 /* For each line spec given, delete bps which correspond to it. Do
10695 it in two passes, solely to preserve the current behavior that
10696 from_tty is forced true if we delete more than one
10697 breakpoint. */
10698
10699 std::vector<struct breakpoint *> found;
10700 for (const auto &sal : sals)
10701 {
10702 const char *sal_fullname;
10703
10704 /* If exact pc given, clear bpts at that pc.
10705 If line given (pc == 0), clear all bpts on specified line.
10706 If defaulting, clear all bpts on default line
10707 or at default pc.
10708
10709 defaulting sal.pc != 0 tests to do
10710
10711 0 1 pc
10712 1 1 pc _and_ line
10713 0 0 line
10714 1 0 <can't happen> */
10715
10716 sal_fullname = (sal.symtab == NULL
10717 ? NULL : symtab_to_fullname (sal.symtab));
10718
10719 /* Find all matching breakpoints and add them to 'found'. */
10720 for (breakpoint *b : all_breakpoints ())
10721 {
10722 int match = 0;
10723 /* Are we going to delete b? */
10724 if (b->type != bp_none && !is_watchpoint (b)
10725 && user_breakpoint_p (b))
10726 {
10727 for (bp_location *loc : b->locations ())
10728 {
10729 /* If the user specified file:line, don't allow a PC
10730 match. This matches historical gdb behavior. */
10731 int pc_match = (!sal.explicit_line
10732 && sal.pc
10733 && (loc->pspace == sal.pspace)
10734 && (loc->address == sal.pc)
10735 && (!section_is_overlay (loc->section)
10736 || loc->section == sal.section));
10737 int line_match = 0;
10738
10739 if ((default_match || sal.explicit_line)
10740 && loc->symtab != NULL
10741 && sal_fullname != NULL
10742 && sal.pspace == loc->pspace
10743 && loc->line_number == sal.line
10744 && filename_cmp (symtab_to_fullname (loc->symtab),
10745 sal_fullname) == 0)
10746 line_match = 1;
10747
10748 if (pc_match || line_match)
10749 {
10750 match = 1;
10751 break;
10752 }
10753 }
10754 }
10755
10756 if (match)
10757 found.push_back (b);
10758 }
10759 }
10760
10761 /* Now go thru the 'found' chain and delete them. */
10762 if (found.empty ())
10763 {
10764 if (arg)
10765 error (_("No breakpoint at %s."), arg);
10766 else
10767 error (_("No breakpoint at this line."));
10768 }
10769
10770 /* Remove duplicates from the vec. */
10771 std::sort (found.begin (), found.end (),
10772 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10773 {
10774 return compare_breakpoints (bp_a, bp_b) < 0;
10775 });
10776 found.erase (std::unique (found.begin (), found.end (),
10777 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10778 {
10779 return compare_breakpoints (bp_a, bp_b) == 0;
10780 }),
10781 found.end ());
10782
10783 if (found.size () > 1)
10784 from_tty = 1; /* Always report if deleted more than one. */
10785 if (from_tty)
10786 {
10787 if (found.size () == 1)
10788 gdb_printf (_("Deleted breakpoint "));
10789 else
10790 gdb_printf (_("Deleted breakpoints "));
10791 }
10792
10793 for (breakpoint *iter : found)
10794 {
10795 if (from_tty)
10796 gdb_printf ("%d ", iter->number);
10797 delete_breakpoint (iter);
10798 }
10799 if (from_tty)
10800 gdb_putc ('\n');
10801 }
10802 \f
10803 /* Delete breakpoint in BS if they are `delete' breakpoints and
10804 all breakpoints that are marked for deletion, whether hit or not.
10805 This is called after any breakpoint is hit, or after errors. */
10806
10807 void
10808 breakpoint_auto_delete (bpstat *bs)
10809 {
10810 for (; bs; bs = bs->next)
10811 if (bs->breakpoint_at
10812 && bs->breakpoint_at->disposition == disp_del
10813 && bs->stop)
10814 delete_breakpoint (bs->breakpoint_at);
10815
10816 for (breakpoint *b : all_breakpoints_safe ())
10817 if (b->disposition == disp_del_at_next_stop)
10818 delete_breakpoint (b);
10819 }
10820
10821 /* A comparison function for bp_location AP and BP being interfaced to
10822 std::sort. Sort elements primarily by their ADDRESS (no matter what
10823 bl_address_is_meaningful says), secondarily by ordering first
10824 permanent elements and terciarily just ensuring the array is sorted
10825 stable way despite std::sort being an unstable algorithm. */
10826
10827 static int
10828 bp_location_is_less_than (const bp_location *a, const bp_location *b)
10829 {
10830 if (a->address != b->address)
10831 return a->address < b->address;
10832
10833 /* Sort locations at the same address by their pspace number, keeping
10834 locations of the same inferior (in a multi-inferior environment)
10835 grouped. */
10836
10837 if (a->pspace->num != b->pspace->num)
10838 return a->pspace->num < b->pspace->num;
10839
10840 /* Sort permanent breakpoints first. */
10841 if (a->permanent != b->permanent)
10842 return a->permanent > b->permanent;
10843
10844 /* Sort by type in order to make duplicate determination easier.
10845 See update_global_location_list. This is kept in sync with
10846 breakpoint_locations_match. */
10847 if (a->loc_type < b->loc_type)
10848 return true;
10849
10850 /* Likewise, for range-breakpoints, sort by length. */
10851 if (a->loc_type == bp_loc_hardware_breakpoint
10852 && b->loc_type == bp_loc_hardware_breakpoint
10853 && a->length < b->length)
10854 return true;
10855
10856 /* Make the internal GDB representation stable across GDB runs
10857 where A and B memory inside GDB can differ. Breakpoint locations of
10858 the same type at the same address can be sorted in arbitrary order. */
10859
10860 if (a->owner->number != b->owner->number)
10861 return a->owner->number < b->owner->number;
10862
10863 return a < b;
10864 }
10865
10866 /* Set bp_locations_placed_address_before_address_max and
10867 bp_locations_shadow_len_after_address_max according to the current
10868 content of the bp_locations array. */
10869
10870 static void
10871 bp_locations_target_extensions_update (void)
10872 {
10873 bp_locations_placed_address_before_address_max = 0;
10874 bp_locations_shadow_len_after_address_max = 0;
10875
10876 for (bp_location *bl : all_bp_locations ())
10877 {
10878 CORE_ADDR start, end, addr;
10879
10880 if (!bp_location_has_shadow (bl))
10881 continue;
10882
10883 start = bl->target_info.placed_address;
10884 end = start + bl->target_info.shadow_len;
10885
10886 gdb_assert (bl->address >= start);
10887 addr = bl->address - start;
10888 if (addr > bp_locations_placed_address_before_address_max)
10889 bp_locations_placed_address_before_address_max = addr;
10890
10891 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10892
10893 gdb_assert (bl->address < end);
10894 addr = end - bl->address;
10895 if (addr > bp_locations_shadow_len_after_address_max)
10896 bp_locations_shadow_len_after_address_max = addr;
10897 }
10898 }
10899
10900 /* Download tracepoint locations if they haven't been. */
10901
10902 static void
10903 download_tracepoint_locations (void)
10904 {
10905 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
10906
10907 scoped_restore_current_pspace_and_thread restore_pspace_thread;
10908
10909 for (breakpoint *b : all_tracepoints ())
10910 {
10911 struct tracepoint *t;
10912 int bp_location_downloaded = 0;
10913
10914 if ((b->type == bp_fast_tracepoint
10915 ? !may_insert_fast_tracepoints
10916 : !may_insert_tracepoints))
10917 continue;
10918
10919 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
10920 {
10921 if (target_can_download_tracepoint ())
10922 can_download_tracepoint = TRIBOOL_TRUE;
10923 else
10924 can_download_tracepoint = TRIBOOL_FALSE;
10925 }
10926
10927 if (can_download_tracepoint == TRIBOOL_FALSE)
10928 break;
10929
10930 for (bp_location *bl : b->locations ())
10931 {
10932 /* In tracepoint, locations are _never_ duplicated, so
10933 should_be_inserted is equivalent to
10934 unduplicated_should_be_inserted. */
10935 if (!should_be_inserted (bl) || bl->inserted)
10936 continue;
10937
10938 switch_to_program_space_and_thread (bl->pspace);
10939
10940 target_download_tracepoint (bl);
10941
10942 bl->inserted = 1;
10943 bp_location_downloaded = 1;
10944 }
10945 t = (struct tracepoint *) b;
10946 t->number_on_target = b->number;
10947 if (bp_location_downloaded)
10948 gdb::observers::breakpoint_modified.notify (b);
10949 }
10950 }
10951
10952 /* Swap the insertion/duplication state between two locations. */
10953
10954 static void
10955 swap_insertion (struct bp_location *left, struct bp_location *right)
10956 {
10957 const int left_inserted = left->inserted;
10958 const int left_duplicate = left->duplicate;
10959 const int left_needs_update = left->needs_update;
10960 const struct bp_target_info left_target_info = left->target_info;
10961
10962 /* Locations of tracepoints can never be duplicated. */
10963 if (is_tracepoint (left->owner))
10964 gdb_assert (!left->duplicate);
10965 if (is_tracepoint (right->owner))
10966 gdb_assert (!right->duplicate);
10967
10968 left->inserted = right->inserted;
10969 left->duplicate = right->duplicate;
10970 left->needs_update = right->needs_update;
10971 left->target_info = right->target_info;
10972 right->inserted = left_inserted;
10973 right->duplicate = left_duplicate;
10974 right->needs_update = left_needs_update;
10975 right->target_info = left_target_info;
10976 }
10977
10978 /* Force the re-insertion of the locations at ADDRESS. This is called
10979 once a new/deleted/modified duplicate location is found and we are evaluating
10980 conditions on the target's side. Such conditions need to be updated on
10981 the target. */
10982
10983 static void
10984 force_breakpoint_reinsertion (struct bp_location *bl)
10985 {
10986 CORE_ADDR address = 0;
10987 int pspace_num;
10988
10989 address = bl->address;
10990 pspace_num = bl->pspace->num;
10991
10992 /* This is only meaningful if the target is
10993 evaluating conditions and if the user has
10994 opted for condition evaluation on the target's
10995 side. */
10996 if (gdb_evaluates_breakpoint_condition_p ()
10997 || !target_supports_evaluation_of_breakpoint_conditions ())
10998 return;
10999
11000 /* Flag all breakpoint locations with this address and
11001 the same program space as the location
11002 as "its condition has changed". We need to
11003 update the conditions on the target's side. */
11004 for (bp_location *loc : all_bp_locations_at_addr (address))
11005 {
11006 if (!is_breakpoint (loc->owner)
11007 || pspace_num != loc->pspace->num)
11008 continue;
11009
11010 /* Flag the location appropriately. We use a different state to
11011 let everyone know that we already updated the set of locations
11012 with addr bl->address and program space bl->pspace. This is so
11013 we don't have to keep calling these functions just to mark locations
11014 that have already been marked. */
11015 loc->condition_changed = condition_updated;
11016
11017 /* Free the agent expression bytecode as well. We will compute
11018 it later on. */
11019 loc->cond_bytecode.reset ();
11020 }
11021 }
11022
11023 /* Called whether new breakpoints are created, or existing breakpoints
11024 deleted, to update the global location list and recompute which
11025 locations are duplicate of which.
11026
11027 The INSERT_MODE flag determines whether locations may not, may, or
11028 shall be inserted now. See 'enum ugll_insert_mode' for more
11029 info. */
11030
11031 static void
11032 update_global_location_list (enum ugll_insert_mode insert_mode)
11033 {
11034 /* Last breakpoint location address that was marked for update. */
11035 CORE_ADDR last_addr = 0;
11036 /* Last breakpoint location program space that was marked for update. */
11037 int last_pspace_num = -1;
11038
11039 /* Used in the duplicates detection below. When iterating over all
11040 bp_locations, points to the first bp_location of a given address.
11041 Breakpoints and watchpoints of different types are never
11042 duplicates of each other. Keep one pointer for each type of
11043 breakpoint/watchpoint, so we only need to loop over all locations
11044 once. */
11045 struct bp_location *bp_loc_first; /* breakpoint */
11046 struct bp_location *wp_loc_first; /* hardware watchpoint */
11047 struct bp_location *awp_loc_first; /* access watchpoint */
11048 struct bp_location *rwp_loc_first; /* read watchpoint */
11049
11050 /* Saved former bp_locations array which we compare against the newly
11051 built bp_locations from the current state of ALL_BREAKPOINTS. */
11052 std::vector<bp_location *> old_locations = std::move (bp_locations);
11053 bp_locations.clear ();
11054
11055 for (breakpoint *b : all_breakpoints ())
11056 for (bp_location *loc : b->locations ())
11057 bp_locations.push_back (loc);
11058
11059 /* See if we need to "upgrade" a software breakpoint to a hardware
11060 breakpoint. Do this before deciding whether locations are
11061 duplicates. Also do this before sorting because sorting order
11062 depends on location type. */
11063 for (bp_location *loc : bp_locations)
11064 if (!loc->inserted && should_be_inserted (loc))
11065 handle_automatic_hardware_breakpoints (loc);
11066
11067 std::sort (bp_locations.begin (), bp_locations.end (),
11068 bp_location_is_less_than);
11069
11070 bp_locations_target_extensions_update ();
11071
11072 /* Identify bp_location instances that are no longer present in the
11073 new list, and therefore should be freed. Note that it's not
11074 necessary that those locations should be removed from inferior --
11075 if there's another location at the same address (previously
11076 marked as duplicate), we don't need to remove/insert the
11077 location.
11078
11079 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11080 and former bp_location array state respectively. */
11081
11082 size_t loc_i = 0;
11083 for (bp_location *old_loc : old_locations)
11084 {
11085 /* Tells if 'old_loc' is found among the new locations. If
11086 not, we have to free it. */
11087 int found_object = 0;
11088 /* Tells if the location should remain inserted in the target. */
11089 int keep_in_target = 0;
11090 int removed = 0;
11091
11092 /* Skip LOCP entries which will definitely never be needed.
11093 Stop either at or being the one matching OLD_LOC. */
11094 while (loc_i < bp_locations.size ()
11095 && bp_locations[loc_i]->address < old_loc->address)
11096 loc_i++;
11097
11098 for (size_t loc2_i = loc_i;
11099 (loc2_i < bp_locations.size ()
11100 && bp_locations[loc2_i]->address == old_loc->address);
11101 loc2_i++)
11102 {
11103 /* Check if this is a new/duplicated location or a duplicated
11104 location that had its condition modified. If so, we want to send
11105 its condition to the target if evaluation of conditions is taking
11106 place there. */
11107 if (bp_locations[loc2_i]->condition_changed == condition_modified
11108 && (last_addr != old_loc->address
11109 || last_pspace_num != old_loc->pspace->num))
11110 {
11111 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11112 last_pspace_num = old_loc->pspace->num;
11113 }
11114
11115 if (bp_locations[loc2_i] == old_loc)
11116 found_object = 1;
11117 }
11118
11119 /* We have already handled this address, update it so that we don't
11120 have to go through updates again. */
11121 last_addr = old_loc->address;
11122
11123 /* Target-side condition evaluation: Handle deleted locations. */
11124 if (!found_object)
11125 force_breakpoint_reinsertion (old_loc);
11126
11127 /* If this location is no longer present, and inserted, look if
11128 there's maybe a new location at the same address. If so,
11129 mark that one inserted, and don't remove this one. This is
11130 needed so that we don't have a time window where a breakpoint
11131 at certain location is not inserted. */
11132
11133 if (old_loc->inserted)
11134 {
11135 /* If the location is inserted now, we might have to remove
11136 it. */
11137
11138 if (found_object && should_be_inserted (old_loc))
11139 {
11140 /* The location is still present in the location list,
11141 and still should be inserted. Don't do anything. */
11142 keep_in_target = 1;
11143 }
11144 else
11145 {
11146 /* This location still exists, but it won't be kept in the
11147 target since it may have been disabled. We proceed to
11148 remove its target-side condition. */
11149
11150 /* The location is either no longer present, or got
11151 disabled. See if there's another location at the
11152 same address, in which case we don't need to remove
11153 this one from the target. */
11154
11155 /* OLD_LOC comes from existing struct breakpoint. */
11156 if (bl_address_is_meaningful (old_loc))
11157 {
11158 for (size_t loc2_i = loc_i;
11159 (loc2_i < bp_locations.size ()
11160 && bp_locations[loc2_i]->address == old_loc->address);
11161 loc2_i++)
11162 {
11163 bp_location *loc2 = bp_locations[loc2_i];
11164
11165 if (loc2 == old_loc)
11166 continue;
11167
11168 if (breakpoint_locations_match (loc2, old_loc))
11169 {
11170 /* Read watchpoint locations are switched to
11171 access watchpoints, if the former are not
11172 supported, but the latter are. */
11173 if (is_hardware_watchpoint (old_loc->owner))
11174 {
11175 gdb_assert (is_hardware_watchpoint (loc2->owner));
11176 loc2->watchpoint_type = old_loc->watchpoint_type;
11177 }
11178
11179 /* loc2 is a duplicated location. We need to check
11180 if it should be inserted in case it will be
11181 unduplicated. */
11182 if (unduplicated_should_be_inserted (loc2))
11183 {
11184 swap_insertion (old_loc, loc2);
11185 keep_in_target = 1;
11186 break;
11187 }
11188 }
11189 }
11190 }
11191 }
11192
11193 if (!keep_in_target)
11194 {
11195 if (remove_breakpoint (old_loc))
11196 {
11197 /* This is just about all we can do. We could keep
11198 this location on the global list, and try to
11199 remove it next time, but there's no particular
11200 reason why we will succeed next time.
11201
11202 Note that at this point, old_loc->owner is still
11203 valid, as delete_breakpoint frees the breakpoint
11204 only after calling us. */
11205 gdb_printf (_("warning: Error removing "
11206 "breakpoint %d\n"),
11207 old_loc->owner->number);
11208 }
11209 removed = 1;
11210 }
11211 }
11212
11213 if (!found_object)
11214 {
11215 if (removed && target_is_non_stop_p ()
11216 && need_moribund_for_location_type (old_loc))
11217 {
11218 /* This location was removed from the target. In
11219 non-stop mode, a race condition is possible where
11220 we've removed a breakpoint, but stop events for that
11221 breakpoint are already queued and will arrive later.
11222 We apply an heuristic to be able to distinguish such
11223 SIGTRAPs from other random SIGTRAPs: we keep this
11224 breakpoint location for a bit, and will retire it
11225 after we see some number of events. The theory here
11226 is that reporting of events should, "on the average",
11227 be fair, so after a while we'll see events from all
11228 threads that have anything of interest, and no longer
11229 need to keep this breakpoint location around. We
11230 don't hold locations forever so to reduce chances of
11231 mistaking a non-breakpoint SIGTRAP for a breakpoint
11232 SIGTRAP.
11233
11234 The heuristic failing can be disastrous on
11235 decr_pc_after_break targets.
11236
11237 On decr_pc_after_break targets, like e.g., x86-linux,
11238 if we fail to recognize a late breakpoint SIGTRAP,
11239 because events_till_retirement has reached 0 too
11240 soon, we'll fail to do the PC adjustment, and report
11241 a random SIGTRAP to the user. When the user resumes
11242 the inferior, it will most likely immediately crash
11243 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11244 corrupted, because of being resumed e.g., in the
11245 middle of a multi-byte instruction, or skipped a
11246 one-byte instruction. This was actually seen happen
11247 on native x86-linux, and should be less rare on
11248 targets that do not support new thread events, like
11249 remote, due to the heuristic depending on
11250 thread_count.
11251
11252 Mistaking a random SIGTRAP for a breakpoint trap
11253 causes similar symptoms (PC adjustment applied when
11254 it shouldn't), but then again, playing with SIGTRAPs
11255 behind the debugger's back is asking for trouble.
11256
11257 Since hardware watchpoint traps are always
11258 distinguishable from other traps, so we don't need to
11259 apply keep hardware watchpoint moribund locations
11260 around. We simply always ignore hardware watchpoint
11261 traps we can no longer explain. */
11262
11263 process_stratum_target *proc_target = nullptr;
11264 for (inferior *inf : all_inferiors ())
11265 if (inf->pspace == old_loc->pspace)
11266 {
11267 proc_target = inf->process_target ();
11268 break;
11269 }
11270 if (proc_target != nullptr)
11271 old_loc->events_till_retirement
11272 = 3 * (thread_count (proc_target) + 1);
11273 else
11274 old_loc->events_till_retirement = 1;
11275 old_loc->owner = NULL;
11276
11277 moribund_locations.push_back (old_loc);
11278 }
11279 else
11280 {
11281 old_loc->owner = NULL;
11282 decref_bp_location (&old_loc);
11283 }
11284 }
11285 }
11286
11287 /* Rescan breakpoints at the same address and section, marking the
11288 first one as "first" and any others as "duplicates". This is so
11289 that the bpt instruction is only inserted once. If we have a
11290 permanent breakpoint at the same place as BPT, make that one the
11291 official one, and the rest as duplicates. Permanent breakpoints
11292 are sorted first for the same address.
11293
11294 Do the same for hardware watchpoints, but also considering the
11295 watchpoint's type (regular/access/read) and length. */
11296
11297 bp_loc_first = NULL;
11298 wp_loc_first = NULL;
11299 awp_loc_first = NULL;
11300 rwp_loc_first = NULL;
11301
11302 for (bp_location *loc : all_bp_locations ())
11303 {
11304 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11305 non-NULL. */
11306 struct bp_location **loc_first_p;
11307 breakpoint *b = loc->owner;
11308
11309 if (!unduplicated_should_be_inserted (loc)
11310 || !bl_address_is_meaningful (loc)
11311 /* Don't detect duplicate for tracepoint locations because they are
11312 never duplicated. See the comments in field `duplicate' of
11313 `struct bp_location'. */
11314 || is_tracepoint (b))
11315 {
11316 /* Clear the condition modification flag. */
11317 loc->condition_changed = condition_unchanged;
11318 continue;
11319 }
11320
11321 if (b->type == bp_hardware_watchpoint)
11322 loc_first_p = &wp_loc_first;
11323 else if (b->type == bp_read_watchpoint)
11324 loc_first_p = &rwp_loc_first;
11325 else if (b->type == bp_access_watchpoint)
11326 loc_first_p = &awp_loc_first;
11327 else
11328 loc_first_p = &bp_loc_first;
11329
11330 if (*loc_first_p == NULL
11331 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11332 || !breakpoint_locations_match (loc, *loc_first_p))
11333 {
11334 *loc_first_p = loc;
11335 loc->duplicate = 0;
11336
11337 if (is_breakpoint (loc->owner) && loc->condition_changed)
11338 {
11339 loc->needs_update = 1;
11340 /* Clear the condition modification flag. */
11341 loc->condition_changed = condition_unchanged;
11342 }
11343 continue;
11344 }
11345
11346
11347 /* This and the above ensure the invariant that the first location
11348 is not duplicated, and is the inserted one.
11349 All following are marked as duplicated, and are not inserted. */
11350 if (loc->inserted)
11351 swap_insertion (loc, *loc_first_p);
11352 loc->duplicate = 1;
11353
11354 /* Clear the condition modification flag. */
11355 loc->condition_changed = condition_unchanged;
11356 }
11357
11358 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11359 {
11360 if (insert_mode != UGLL_DONT_INSERT)
11361 insert_breakpoint_locations ();
11362 else
11363 {
11364 /* Even though the caller told us to not insert new
11365 locations, we may still need to update conditions on the
11366 target's side of breakpoints that were already inserted
11367 if the target is evaluating breakpoint conditions. We
11368 only update conditions for locations that are marked
11369 "needs_update". */
11370 update_inserted_breakpoint_locations ();
11371 }
11372 }
11373
11374 if (insert_mode != UGLL_DONT_INSERT)
11375 download_tracepoint_locations ();
11376 }
11377
11378 void
11379 breakpoint_retire_moribund (void)
11380 {
11381 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11382 {
11383 struct bp_location *loc = moribund_locations[ix];
11384 if (--(loc->events_till_retirement) == 0)
11385 {
11386 decref_bp_location (&loc);
11387 unordered_remove (moribund_locations, ix);
11388 --ix;
11389 }
11390 }
11391 }
11392
11393 static void
11394 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11395 {
11396
11397 try
11398 {
11399 update_global_location_list (insert_mode);
11400 }
11401 catch (const gdb_exception_error &e)
11402 {
11403 }
11404 }
11405
11406 /* Clear BKP from a BPS. */
11407
11408 static void
11409 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
11410 {
11411 bpstat *bs;
11412
11413 for (bs = bps; bs; bs = bs->next)
11414 if (bs->breakpoint_at == bpt)
11415 {
11416 bs->breakpoint_at = NULL;
11417 bs->old_val = NULL;
11418 /* bs->commands will be freed later. */
11419 }
11420 }
11421
11422 /* Callback for iterate_over_threads. */
11423 static int
11424 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11425 {
11426 struct breakpoint *bpt = (struct breakpoint *) data;
11427
11428 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11429 return 0;
11430 }
11431
11432 /* Helper for breakpoint and tracepoint breakpoint->mention
11433 callbacks. */
11434
11435 static void
11436 say_where (const breakpoint *b)
11437 {
11438 struct value_print_options opts;
11439
11440 get_user_print_options (&opts);
11441
11442 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11443 single string. */
11444 if (b->loc == NULL)
11445 {
11446 /* For pending locations, the output differs slightly based
11447 on b->extra_string. If this is non-NULL, it contains either
11448 a condition or dprintf arguments. */
11449 if (b->extra_string == NULL)
11450 {
11451 gdb_printf (_(" (%s) pending."),
11452 event_location_to_string (b->location.get ()));
11453 }
11454 else if (b->type == bp_dprintf)
11455 {
11456 gdb_printf (_(" (%s,%s) pending."),
11457 event_location_to_string (b->location.get ()),
11458 b->extra_string.get ());
11459 }
11460 else
11461 {
11462 gdb_printf (_(" (%s %s) pending."),
11463 event_location_to_string (b->location.get ()),
11464 b->extra_string.get ());
11465 }
11466 }
11467 else
11468 {
11469 if (opts.addressprint || b->loc->symtab == NULL)
11470 gdb_printf (" at %ps",
11471 styled_string (address_style.style (),
11472 paddress (b->loc->gdbarch,
11473 b->loc->address)));
11474 if (b->loc->symtab != NULL)
11475 {
11476 /* If there is a single location, we can print the location
11477 more nicely. */
11478 if (b->loc->next == NULL)
11479 {
11480 const char *filename
11481 = symtab_to_filename_for_display (b->loc->symtab);
11482 gdb_printf (": file %ps, line %d.",
11483 styled_string (file_name_style.style (),
11484 filename),
11485 b->loc->line_number);
11486 }
11487 else
11488 /* This is not ideal, but each location may have a
11489 different file name, and this at least reflects the
11490 real situation somewhat. */
11491 gdb_printf (": %s.",
11492 event_location_to_string (b->location.get ()));
11493 }
11494
11495 if (b->loc->next)
11496 {
11497 struct bp_location *loc = b->loc;
11498 int n = 0;
11499 for (; loc; loc = loc->next)
11500 ++n;
11501 gdb_printf (" (%d locations)", n);
11502 }
11503 }
11504 }
11505
11506 /* See breakpoint.h. */
11507
11508 bp_location_range breakpoint::locations () const
11509 {
11510 return bp_location_range (this->loc);
11511 }
11512
11513 struct bp_location *
11514 breakpoint::allocate_location ()
11515 {
11516 return new bp_location (this);
11517 }
11518
11519 #define internal_error_pure_virtual_called() \
11520 gdb_assert_not_reached ("pure virtual function called")
11521
11522 int
11523 breakpoint::insert_location (struct bp_location *bl)
11524 {
11525 internal_error_pure_virtual_called ();
11526 }
11527
11528 int
11529 breakpoint::remove_location (struct bp_location *bl,
11530 enum remove_bp_reason reason)
11531 {
11532 internal_error_pure_virtual_called ();
11533 }
11534
11535 int
11536 breakpoint::breakpoint_hit (const struct bp_location *bl,
11537 const address_space *aspace,
11538 CORE_ADDR bp_addr,
11539 const target_waitstatus &ws)
11540 {
11541 internal_error_pure_virtual_called ();
11542 }
11543
11544 int
11545 breakpoint::resources_needed (const struct bp_location *bl)
11546 {
11547 internal_error_pure_virtual_called ();
11548 }
11549
11550 enum print_stop_action
11551 breakpoint::print_it (const bpstat *bs) const
11552 {
11553 internal_error_pure_virtual_called ();
11554 }
11555
11556 void
11557 breakpoint::print_mention () const
11558 {
11559 internal_error_pure_virtual_called ();
11560 }
11561
11562 void
11563 breakpoint::print_recreate (struct ui_file *fp) const
11564 {
11565 internal_error_pure_virtual_called ();
11566 }
11567
11568 std::vector<symtab_and_line>
11569 breakpoint::decode_location (struct event_location *location,
11570 struct program_space *search_pspace)
11571 {
11572 internal_error_pure_virtual_called ();
11573 }
11574
11575 /* Default breakpoint_ops methods. */
11576
11577 void
11578 base_breakpoint::re_set ()
11579 {
11580 /* FIXME: is this still reachable? */
11581 if (breakpoint_event_location_empty_p (this))
11582 {
11583 /* Anything without a location can't be re-set. */
11584 delete_breakpoint (this);
11585 return;
11586 }
11587
11588 breakpoint_re_set_default (this);
11589 }
11590
11591 int
11592 base_breakpoint::insert_location (struct bp_location *bl)
11593 {
11594 CORE_ADDR addr = bl->target_info.reqstd_address;
11595
11596 bl->target_info.kind = breakpoint_kind (bl, &addr);
11597 bl->target_info.placed_address = addr;
11598
11599 int result;
11600 if (bl->loc_type == bp_loc_hardware_breakpoint)
11601 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
11602 else
11603 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11604
11605 if (result == 0 && bl->probe.prob != nullptr)
11606 {
11607 /* The insertion was successful, now let's set the probe's semaphore
11608 if needed. */
11609 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11610 }
11611
11612 return result;
11613 }
11614
11615 int
11616 base_breakpoint::remove_location (struct bp_location *bl,
11617 enum remove_bp_reason reason)
11618 {
11619 if (bl->probe.prob != nullptr)
11620 {
11621 /* Let's clear the semaphore before removing the location. */
11622 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11623 }
11624
11625 if (bl->loc_type == bp_loc_hardware_breakpoint)
11626 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11627 else
11628 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
11629 }
11630
11631 int
11632 base_breakpoint::breakpoint_hit (const struct bp_location *bl,
11633 const address_space *aspace,
11634 CORE_ADDR bp_addr,
11635 const target_waitstatus &ws)
11636 {
11637 if (ws.kind () != TARGET_WAITKIND_STOPPED
11638 || ws.sig () != GDB_SIGNAL_TRAP)
11639 return 0;
11640
11641 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11642 aspace, bp_addr))
11643 return 0;
11644
11645 if (overlay_debugging /* unmapped overlay section */
11646 && section_is_overlay (bl->section)
11647 && !section_is_mapped (bl->section))
11648 return 0;
11649
11650 return 1;
11651 }
11652
11653 int
11654 dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
11655 const address_space *aspace,
11656 CORE_ADDR bp_addr,
11657 const target_waitstatus &ws)
11658 {
11659 if (dprintf_style == dprintf_style_agent
11660 && target_can_run_breakpoint_commands ())
11661 {
11662 /* An agent-style dprintf never causes a stop. If we see a trap
11663 for this address it must be for a breakpoint that happens to
11664 be set at the same address. */
11665 return 0;
11666 }
11667
11668 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
11669 }
11670
11671 int
11672 ordinary_breakpoint::resources_needed (const struct bp_location *bl)
11673 {
11674 gdb_assert (type == bp_hardware_breakpoint);
11675
11676 return 1;
11677 }
11678
11679 enum print_stop_action
11680 ordinary_breakpoint::print_it (const bpstat *bs) const
11681 {
11682 const struct bp_location *bl;
11683 int bp_temp;
11684 struct ui_out *uiout = current_uiout;
11685
11686 bl = bs->bp_location_at.get ();
11687
11688 bp_temp = disposition == disp_del;
11689 if (bl->address != bl->requested_address)
11690 breakpoint_adjustment_warning (bl->requested_address,
11691 bl->address,
11692 number, 1);
11693 annotate_breakpoint (number);
11694 maybe_print_thread_hit_breakpoint (uiout);
11695
11696 if (uiout->is_mi_like_p ())
11697 {
11698 uiout->field_string ("reason",
11699 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11700 uiout->field_string ("disp", bpdisp_text (disposition));
11701 }
11702 if (bp_temp)
11703 uiout->message ("Temporary breakpoint %pF, ",
11704 signed_field ("bkptno", number));
11705 else
11706 uiout->message ("Breakpoint %pF, ",
11707 signed_field ("bkptno", number));
11708
11709 return PRINT_SRC_AND_LOC;
11710 }
11711
11712 void
11713 ordinary_breakpoint::print_mention () const
11714 {
11715 if (current_uiout->is_mi_like_p ())
11716 return;
11717
11718 switch (type)
11719 {
11720 case bp_breakpoint:
11721 case bp_gnu_ifunc_resolver:
11722 if (disposition == disp_del)
11723 gdb_printf (_("Temporary breakpoint"));
11724 else
11725 gdb_printf (_("Breakpoint"));
11726 gdb_printf (_(" %d"), number);
11727 if (type == bp_gnu_ifunc_resolver)
11728 gdb_printf (_(" at gnu-indirect-function resolver"));
11729 break;
11730 case bp_hardware_breakpoint:
11731 gdb_printf (_("Hardware assisted breakpoint %d"), number);
11732 break;
11733 case bp_dprintf:
11734 gdb_printf (_("Dprintf %d"), number);
11735 break;
11736 }
11737
11738 say_where (this);
11739 }
11740
11741 void
11742 ordinary_breakpoint::print_recreate (struct ui_file *fp) const
11743 {
11744 if (type == bp_breakpoint && disposition == disp_del)
11745 gdb_printf (fp, "tbreak");
11746 else if (type == bp_breakpoint)
11747 gdb_printf (fp, "break");
11748 else if (type == bp_hardware_breakpoint
11749 && disposition == disp_del)
11750 gdb_printf (fp, "thbreak");
11751 else if (type == bp_hardware_breakpoint)
11752 gdb_printf (fp, "hbreak");
11753 else
11754 internal_error (__FILE__, __LINE__,
11755 _("unhandled breakpoint type %d"), (int) type);
11756
11757 gdb_printf (fp, " %s", event_location_to_string (location.get ()));
11758
11759 /* Print out extra_string if this breakpoint is pending. It might
11760 contain, for example, conditions that were set by the user. */
11761 if (loc == NULL && extra_string != NULL)
11762 gdb_printf (fp, " %s", extra_string.get ());
11763
11764 print_recreate_thread (fp);
11765 }
11766
11767 std::vector<symtab_and_line>
11768 base_breakpoint::decode_location (struct event_location *location,
11769 struct program_space *search_pspace)
11770 {
11771 if (event_location_type (location) == PROBE_LOCATION)
11772 return bkpt_probe_decode_location (this, location, search_pspace);
11773
11774 return decode_location_default (this, location, search_pspace);
11775 }
11776
11777 /* Virtual table for internal breakpoints. */
11778
11779 void
11780 internal_breakpoint::re_set ()
11781 {
11782 switch (type)
11783 {
11784 /* Delete overlay event and longjmp master breakpoints; they
11785 will be reset later by breakpoint_re_set. */
11786 case bp_overlay_event:
11787 case bp_longjmp_master:
11788 case bp_std_terminate_master:
11789 case bp_exception_master:
11790 delete_breakpoint (this);
11791 break;
11792
11793 /* This breakpoint is special, it's set up when the inferior
11794 starts and we really don't want to touch it. */
11795 case bp_shlib_event:
11796
11797 /* Like bp_shlib_event, this breakpoint type is special. Once
11798 it is set up, we do not want to touch it. */
11799 case bp_thread_event:
11800 break;
11801 }
11802 }
11803
11804 void
11805 internal_breakpoint::check_status (bpstat *bs)
11806 {
11807 if (type == bp_shlib_event)
11808 {
11809 /* If requested, stop when the dynamic linker notifies GDB of
11810 events. This allows the user to get control and place
11811 breakpoints in initializer routines for dynamically loaded
11812 objects (among other things). */
11813 bs->stop = stop_on_solib_events;
11814 bs->print = stop_on_solib_events;
11815 }
11816 else
11817 bs->stop = 0;
11818 }
11819
11820 enum print_stop_action
11821 internal_breakpoint::print_it (const bpstat *bs) const
11822 {
11823 switch (type)
11824 {
11825 case bp_shlib_event:
11826 /* Did we stop because the user set the stop_on_solib_events
11827 variable? (If so, we report this as a generic, "Stopped due
11828 to shlib event" message.) */
11829 print_solib_event (false);
11830 break;
11831
11832 case bp_thread_event:
11833 /* Not sure how we will get here.
11834 GDB should not stop for these breakpoints. */
11835 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
11836 break;
11837
11838 case bp_overlay_event:
11839 /* By analogy with the thread event, GDB should not stop for these. */
11840 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
11841 break;
11842
11843 case bp_longjmp_master:
11844 /* These should never be enabled. */
11845 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
11846 break;
11847
11848 case bp_std_terminate_master:
11849 /* These should never be enabled. */
11850 gdb_printf (_("std::terminate Master Breakpoint: "
11851 "gdb should not stop!\n"));
11852 break;
11853
11854 case bp_exception_master:
11855 /* These should never be enabled. */
11856 gdb_printf (_("Exception Master Breakpoint: "
11857 "gdb should not stop!\n"));
11858 break;
11859 }
11860
11861 return PRINT_NOTHING;
11862 }
11863
11864 void
11865 internal_breakpoint::print_mention () const
11866 {
11867 /* Nothing to mention. These breakpoints are internal. */
11868 }
11869
11870 /* Virtual table for momentary breakpoints */
11871
11872 void
11873 momentary_breakpoint::re_set ()
11874 {
11875 /* Keep temporary breakpoints, which can be encountered when we step
11876 over a dlopen call and solib_add is resetting the breakpoints.
11877 Otherwise these should have been blown away via the cleanup chain
11878 or by breakpoint_init_inferior when we rerun the executable. */
11879 }
11880
11881 void
11882 momentary_breakpoint::check_status (bpstat *bs)
11883 {
11884 /* Nothing. The point of these breakpoints is causing a stop. */
11885 }
11886
11887 enum print_stop_action
11888 momentary_breakpoint::print_it (const bpstat *bs) const
11889 {
11890 return PRINT_UNKNOWN;
11891 }
11892
11893 void
11894 momentary_breakpoint::print_mention () const
11895 {
11896 /* Nothing to mention. These breakpoints are internal. */
11897 }
11898
11899 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
11900
11901 It gets cleared already on the removal of the first one of such placed
11902 breakpoints. This is OK as they get all removed altogether. */
11903
11904 longjmp_breakpoint::~longjmp_breakpoint ()
11905 {
11906 thread_info *tp = find_thread_global_id (this->thread);
11907
11908 if (tp != NULL)
11909 tp->initiating_frame = null_frame_id;
11910 }
11911
11912 static void
11913 bkpt_probe_create_sals_from_location (struct event_location *location,
11914 struct linespec_result *canonical)
11915
11916 {
11917 struct linespec_sals lsal;
11918
11919 lsal.sals = parse_probes (location, NULL, canonical);
11920 lsal.canonical
11921 = xstrdup (event_location_to_string (canonical->location.get ()));
11922 canonical->lsals.push_back (std::move (lsal));
11923 }
11924
11925 static std::vector<symtab_and_line>
11926 bkpt_probe_decode_location (struct breakpoint *b,
11927 struct event_location *location,
11928 struct program_space *search_pspace)
11929 {
11930 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
11931 if (sals.empty ())
11932 error (_("probe not found"));
11933 return sals;
11934 }
11935
11936 /* The breakpoint_ops structure to be used in tracepoints. */
11937
11938 void
11939 tracepoint::re_set ()
11940 {
11941 breakpoint_re_set_default (this);
11942 }
11943
11944 int
11945 tracepoint::breakpoint_hit (const struct bp_location *bl,
11946 const address_space *aspace, CORE_ADDR bp_addr,
11947 const target_waitstatus &ws)
11948 {
11949 /* By definition, the inferior does not report stops at
11950 tracepoints. */
11951 return 0;
11952 }
11953
11954 void
11955 tracepoint::print_one_detail (struct ui_out *uiout) const
11956 {
11957 if (!static_trace_marker_id.empty ())
11958 {
11959 gdb_assert (type == bp_static_tracepoint
11960 || type == bp_static_marker_tracepoint);
11961
11962 uiout->message ("\tmarker id is %pF\n",
11963 string_field ("static-tracepoint-marker-string-id",
11964 static_trace_marker_id.c_str ()));
11965 }
11966 }
11967
11968 void
11969 tracepoint::print_mention () const
11970 {
11971 if (current_uiout->is_mi_like_p ())
11972 return;
11973
11974 switch (type)
11975 {
11976 case bp_tracepoint:
11977 gdb_printf (_("Tracepoint"));
11978 gdb_printf (_(" %d"), number);
11979 break;
11980 case bp_fast_tracepoint:
11981 gdb_printf (_("Fast tracepoint"));
11982 gdb_printf (_(" %d"), number);
11983 break;
11984 case bp_static_tracepoint:
11985 case bp_static_marker_tracepoint:
11986 gdb_printf (_("Static tracepoint"));
11987 gdb_printf (_(" %d"), number);
11988 break;
11989 default:
11990 internal_error (__FILE__, __LINE__,
11991 _("unhandled tracepoint type %d"), (int) type);
11992 }
11993
11994 say_where (this);
11995 }
11996
11997 void
11998 tracepoint::print_recreate (struct ui_file *fp) const
11999 {
12000 if (type == bp_fast_tracepoint)
12001 gdb_printf (fp, "ftrace");
12002 else if (type == bp_static_tracepoint
12003 || type == bp_static_marker_tracepoint)
12004 gdb_printf (fp, "strace");
12005 else if (type == bp_tracepoint)
12006 gdb_printf (fp, "trace");
12007 else
12008 internal_error (__FILE__, __LINE__,
12009 _("unhandled tracepoint type %d"), (int) type);
12010
12011 gdb_printf (fp, " %s", event_location_to_string (location.get ()));
12012 print_recreate_thread (fp);
12013
12014 if (pass_count)
12015 gdb_printf (fp, " passcount %d\n", pass_count);
12016 }
12017
12018 std::vector<symtab_and_line>
12019 tracepoint::decode_location (struct event_location *location,
12020 struct program_space *search_pspace)
12021 {
12022 if (event_location_type (location) == PROBE_LOCATION)
12023 return bkpt_probe_decode_location (this, location, search_pspace);
12024
12025 return decode_location_default (this, location, search_pspace);
12026 }
12027
12028 /* Virtual table for tracepoints on static probes. */
12029
12030 static void
12031 tracepoint_probe_create_sals_from_location
12032 (struct event_location *location,
12033 struct linespec_result *canonical)
12034 {
12035 /* We use the same method for breakpoint on probes. */
12036 bkpt_probe_create_sals_from_location (location, canonical);
12037 }
12038
12039 void
12040 dprintf_breakpoint::re_set ()
12041 {
12042 breakpoint_re_set_default (this);
12043
12044 /* extra_string should never be non-NULL for dprintf. */
12045 gdb_assert (extra_string != NULL);
12046
12047 /* 1 - connect to target 1, that can run breakpoint commands.
12048 2 - create a dprintf, which resolves fine.
12049 3 - disconnect from target 1
12050 4 - connect to target 2, that can NOT run breakpoint commands.
12051
12052 After steps #3/#4, you'll want the dprintf command list to
12053 be updated, because target 1 and 2 may well return different
12054 answers for target_can_run_breakpoint_commands().
12055 Given absence of finer grained resetting, we get to do
12056 it all the time. */
12057 if (extra_string != NULL)
12058 update_dprintf_command_list (this);
12059 }
12060
12061 /* Implement the "print_recreate" method for dprintf. */
12062
12063 void
12064 dprintf_breakpoint::print_recreate (struct ui_file *fp) const
12065 {
12066 gdb_printf (fp, "dprintf %s,%s",
12067 event_location_to_string (location.get ()),
12068 extra_string.get ());
12069 print_recreate_thread (fp);
12070 }
12071
12072 /* Implement the "after_condition_true" method for dprintf.
12073
12074 dprintf's are implemented with regular commands in their command
12075 list, but we run the commands here instead of before presenting the
12076 stop to the user, as dprintf's don't actually cause a stop. This
12077 also makes it so that the commands of multiple dprintfs at the same
12078 address are all handled. */
12079
12080 void
12081 dprintf_breakpoint::after_condition_true (struct bpstat *bs)
12082 {
12083 /* dprintf's never cause a stop. This wasn't set in the
12084 check_status hook instead because that would make the dprintf's
12085 condition not be evaluated. */
12086 bs->stop = 0;
12087
12088 /* Run the command list here. Take ownership of it instead of
12089 copying. We never want these commands to run later in
12090 bpstat_do_actions, if a breakpoint that causes a stop happens to
12091 be set at same address as this dprintf, or even if running the
12092 commands here throws. */
12093 counted_command_line cmds = std::move (bs->commands);
12094 gdb_assert (cmds != nullptr);
12095 execute_control_commands (cmds.get (), 0);
12096 }
12097
12098 /* The breakpoint_ops structure to be used on static tracepoints with
12099 markers (`-m'). */
12100
12101 static void
12102 strace_marker_create_sals_from_location (struct event_location *location,
12103 struct linespec_result *canonical)
12104 {
12105 struct linespec_sals lsal;
12106 const char *arg_start, *arg;
12107
12108 arg = arg_start = get_linespec_location (location)->spec_string;
12109 lsal.sals = decode_static_tracepoint_spec (&arg);
12110
12111 std::string str (arg_start, arg - arg_start);
12112 const char *ptr = str.c_str ();
12113 canonical->location
12114 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
12115
12116 lsal.canonical
12117 = xstrdup (event_location_to_string (canonical->location.get ()));
12118 canonical->lsals.push_back (std::move (lsal));
12119 }
12120
12121 static void
12122 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12123 struct linespec_result *canonical,
12124 gdb::unique_xmalloc_ptr<char> cond_string,
12125 gdb::unique_xmalloc_ptr<char> extra_string,
12126 enum bptype type_wanted,
12127 enum bpdisp disposition,
12128 int thread,
12129 int task, int ignore_count,
12130 const struct breakpoint_ops *ops,
12131 int from_tty, int enabled,
12132 int internal, unsigned flags)
12133 {
12134 const linespec_sals &lsal = canonical->lsals[0];
12135
12136 /* If the user is creating a static tracepoint by marker id
12137 (strace -m MARKER_ID), then store the sals index, so that
12138 breakpoint_re_set can try to match up which of the newly
12139 found markers corresponds to this one, and, don't try to
12140 expand multiple locations for each sal, given than SALS
12141 already should contain all sals for MARKER_ID. */
12142
12143 for (size_t i = 0; i < lsal.sals.size (); i++)
12144 {
12145 event_location_up location
12146 = copy_event_location (canonical->location.get ());
12147
12148 std::unique_ptr<tracepoint> tp (new tracepoint (gdbarch,
12149 type_wanted));
12150 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
12151 std::move (location), NULL,
12152 std::move (cond_string),
12153 std::move (extra_string),
12154 type_wanted, disposition,
12155 thread, task, ignore_count, ops,
12156 from_tty, enabled, internal, flags,
12157 canonical->special_display);
12158 /* Given that its possible to have multiple markers with
12159 the same string id, if the user is creating a static
12160 tracepoint by marker id ("strace -m MARKER_ID"), then
12161 store the sals index, so that breakpoint_re_set can
12162 try to match up which of the newly found markers
12163 corresponds to this one */
12164 tp->static_trace_marker_id_idx = i;
12165
12166 install_breakpoint (internal, std::move (tp), 0);
12167 }
12168 }
12169
12170 std::vector<symtab_and_line>
12171 static_marker_tracepoint::decode_location (struct event_location *location,
12172 struct program_space *search_pspace)
12173 {
12174 const char *s = get_linespec_location (location)->spec_string;
12175
12176 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12177 if (sals.size () > static_trace_marker_id_idx)
12178 {
12179 sals[0] = sals[static_trace_marker_id_idx];
12180 sals.resize (1);
12181 return sals;
12182 }
12183 else
12184 error (_("marker %s not found"), static_trace_marker_id.c_str ());
12185 }
12186
12187 /* Static tracepoints with marker (`-m'). */
12188 static struct breakpoint_ops strace_marker_breakpoint_ops =
12189 {
12190 strace_marker_create_sals_from_location,
12191 strace_marker_create_breakpoints_sal,
12192 };
12193
12194 static int
12195 strace_marker_p (struct breakpoint *b)
12196 {
12197 return b->type == bp_static_marker_tracepoint;
12198 }
12199
12200 /* Delete a breakpoint and clean up all traces of it in the data
12201 structures. */
12202
12203 void
12204 delete_breakpoint (struct breakpoint *bpt)
12205 {
12206 gdb_assert (bpt != NULL);
12207
12208 /* Has this bp already been deleted? This can happen because
12209 multiple lists can hold pointers to bp's. bpstat lists are
12210 especial culprits.
12211
12212 One example of this happening is a watchpoint's scope bp. When
12213 the scope bp triggers, we notice that the watchpoint is out of
12214 scope, and delete it. We also delete its scope bp. But the
12215 scope bp is marked "auto-deleting", and is already on a bpstat.
12216 That bpstat is then checked for auto-deleting bp's, which are
12217 deleted.
12218
12219 A real solution to this problem might involve reference counts in
12220 bp's, and/or giving them pointers back to their referencing
12221 bpstat's, and teaching delete_breakpoint to only free a bp's
12222 storage when no more references were extent. A cheaper bandaid
12223 was chosen. */
12224 if (bpt->type == bp_none)
12225 return;
12226
12227 /* At least avoid this stale reference until the reference counting
12228 of breakpoints gets resolved. */
12229 if (bpt->related_breakpoint != bpt)
12230 {
12231 struct breakpoint *related;
12232 struct watchpoint *w;
12233
12234 if (bpt->type == bp_watchpoint_scope)
12235 w = (struct watchpoint *) bpt->related_breakpoint;
12236 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12237 w = (struct watchpoint *) bpt;
12238 else
12239 w = NULL;
12240 if (w != NULL)
12241 watchpoint_del_at_next_stop (w);
12242
12243 /* Unlink bpt from the bpt->related_breakpoint ring. */
12244 for (related = bpt; related->related_breakpoint != bpt;
12245 related = related->related_breakpoint);
12246 related->related_breakpoint = bpt->related_breakpoint;
12247 bpt->related_breakpoint = bpt;
12248 }
12249
12250 /* watch_command_1 creates a watchpoint but only sets its number if
12251 update_watchpoint succeeds in creating its bp_locations. If there's
12252 a problem in that process, we'll be asked to delete the half-created
12253 watchpoint. In that case, don't announce the deletion. */
12254 if (bpt->number)
12255 gdb::observers::breakpoint_deleted.notify (bpt);
12256
12257 if (breakpoint_chain == bpt)
12258 breakpoint_chain = bpt->next;
12259
12260 for (breakpoint *b : all_breakpoints ())
12261 if (b->next == bpt)
12262 {
12263 b->next = bpt->next;
12264 break;
12265 }
12266
12267 /* Be sure no bpstat's are pointing at the breakpoint after it's
12268 been freed. */
12269 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12270 in all threads for now. Note that we cannot just remove bpstats
12271 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12272 commands are associated with the bpstat; if we remove it here,
12273 then the later call to bpstat_do_actions (&stop_bpstat); in
12274 event-top.c won't do anything, and temporary breakpoints with
12275 commands won't work. */
12276
12277 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12278
12279 /* Now that breakpoint is removed from breakpoint list, update the
12280 global location list. This will remove locations that used to
12281 belong to this breakpoint. Do this before freeing the breakpoint
12282 itself, since remove_breakpoint looks at location's owner. It
12283 might be better design to have location completely
12284 self-contained, but it's not the case now. */
12285 update_global_location_list (UGLL_DONT_INSERT);
12286
12287 /* On the chance that someone will soon try again to delete this
12288 same bp, we mark it as deleted before freeing its storage. */
12289 bpt->type = bp_none;
12290 delete bpt;
12291 }
12292
12293 /* Iterator function to call a user-provided callback function once
12294 for each of B and its related breakpoints. */
12295
12296 static void
12297 iterate_over_related_breakpoints (struct breakpoint *b,
12298 gdb::function_view<void (breakpoint *)> function)
12299 {
12300 struct breakpoint *related;
12301
12302 related = b;
12303 do
12304 {
12305 struct breakpoint *next;
12306
12307 /* FUNCTION may delete RELATED. */
12308 next = related->related_breakpoint;
12309
12310 if (next == related)
12311 {
12312 /* RELATED is the last ring entry. */
12313 function (related);
12314
12315 /* FUNCTION may have deleted it, so we'd never reach back to
12316 B. There's nothing left to do anyway, so just break
12317 out. */
12318 break;
12319 }
12320 else
12321 function (related);
12322
12323 related = next;
12324 }
12325 while (related != b);
12326 }
12327
12328 static void
12329 delete_command (const char *arg, int from_tty)
12330 {
12331 dont_repeat ();
12332
12333 if (arg == 0)
12334 {
12335 int breaks_to_delete = 0;
12336
12337 /* Delete all breakpoints if no argument. Do not delete
12338 internal breakpoints, these have to be deleted with an
12339 explicit breakpoint number argument. */
12340 for (breakpoint *b : all_breakpoints ())
12341 if (user_breakpoint_p (b))
12342 {
12343 breaks_to_delete = 1;
12344 break;
12345 }
12346
12347 /* Ask user only if there are some breakpoints to delete. */
12348 if (!from_tty
12349 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
12350 for (breakpoint *b : all_breakpoints_safe ())
12351 if (user_breakpoint_p (b))
12352 delete_breakpoint (b);
12353 }
12354 else
12355 map_breakpoint_numbers
12356 (arg, [&] (breakpoint *br)
12357 {
12358 iterate_over_related_breakpoints (br, delete_breakpoint);
12359 });
12360 }
12361
12362 /* Return true if all locations of B bound to PSPACE are pending. If
12363 PSPACE is NULL, all locations of all program spaces are
12364 considered. */
12365
12366 static int
12367 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
12368 {
12369 for (bp_location *loc : b->locations ())
12370 if ((pspace == NULL
12371 || loc->pspace == pspace)
12372 && !loc->shlib_disabled
12373 && !loc->pspace->executing_startup)
12374 return 0;
12375 return 1;
12376 }
12377
12378 /* Subroutine of update_breakpoint_locations to simplify it.
12379 Return non-zero if multiple fns in list LOC have the same name.
12380 Null names are ignored. */
12381
12382 static int
12383 ambiguous_names_p (struct bp_location *loc)
12384 {
12385 struct bp_location *l;
12386 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
12387 xcalloc, xfree));
12388
12389 for (l = loc; l != NULL; l = l->next)
12390 {
12391 const char **slot;
12392 const char *name = l->function_name.get ();
12393
12394 /* Allow for some names to be NULL, ignore them. */
12395 if (name == NULL)
12396 continue;
12397
12398 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
12399 INSERT);
12400 /* NOTE: We can assume slot != NULL here because xcalloc never
12401 returns NULL. */
12402 if (*slot != NULL)
12403 return 1;
12404 *slot = name;
12405 }
12406
12407 return 0;
12408 }
12409
12410 /* When symbols change, it probably means the sources changed as well,
12411 and it might mean the static tracepoint markers are no longer at
12412 the same address or line numbers they used to be at last we
12413 checked. Losing your static tracepoints whenever you rebuild is
12414 undesirable. This function tries to resync/rematch gdb static
12415 tracepoints with the markers on the target, for static tracepoints
12416 that have not been set by marker id. Static tracepoint that have
12417 been set by marker id are reset by marker id in breakpoint_re_set.
12418 The heuristic is:
12419
12420 1) For a tracepoint set at a specific address, look for a marker at
12421 the old PC. If one is found there, assume to be the same marker.
12422 If the name / string id of the marker found is different from the
12423 previous known name, assume that means the user renamed the marker
12424 in the sources, and output a warning.
12425
12426 2) For a tracepoint set at a given line number, look for a marker
12427 at the new address of the old line number. If one is found there,
12428 assume to be the same marker. If the name / string id of the
12429 marker found is different from the previous known name, assume that
12430 means the user renamed the marker in the sources, and output a
12431 warning.
12432
12433 3) If a marker is no longer found at the same address or line, it
12434 may mean the marker no longer exists. But it may also just mean
12435 the code changed a bit. Maybe the user added a few lines of code
12436 that made the marker move up or down (in line number terms). Ask
12437 the target for info about the marker with the string id as we knew
12438 it. If found, update line number and address in the matching
12439 static tracepoint. This will get confused if there's more than one
12440 marker with the same ID (possible in UST, although unadvised
12441 precisely because it confuses tools). */
12442
12443 static struct symtab_and_line
12444 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12445 {
12446 struct tracepoint *tp = (struct tracepoint *) b;
12447 struct static_tracepoint_marker marker;
12448 CORE_ADDR pc;
12449
12450 pc = sal.pc;
12451 if (sal.line)
12452 find_line_pc (sal.symtab, sal.line, &pc);
12453
12454 if (target_static_tracepoint_marker_at (pc, &marker))
12455 {
12456 if (tp->static_trace_marker_id != marker.str_id)
12457 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12458 b->number, tp->static_trace_marker_id.c_str (),
12459 marker.str_id.c_str ());
12460
12461 tp->static_trace_marker_id = std::move (marker.str_id);
12462
12463 return sal;
12464 }
12465
12466 /* Old marker wasn't found on target at lineno. Try looking it up
12467 by string ID. */
12468 if (!sal.explicit_pc
12469 && sal.line != 0
12470 && sal.symtab != NULL
12471 && !tp->static_trace_marker_id.empty ())
12472 {
12473 std::vector<static_tracepoint_marker> markers
12474 = target_static_tracepoint_markers_by_strid
12475 (tp->static_trace_marker_id.c_str ());
12476
12477 if (!markers.empty ())
12478 {
12479 struct symbol *sym;
12480 struct static_tracepoint_marker *tpmarker;
12481 struct ui_out *uiout = current_uiout;
12482 struct explicit_location explicit_loc;
12483
12484 tpmarker = &markers[0];
12485
12486 tp->static_trace_marker_id = std::move (tpmarker->str_id);
12487
12488 warning (_("marker for static tracepoint %d (%s) not "
12489 "found at previous line number"),
12490 b->number, tp->static_trace_marker_id.c_str ());
12491
12492 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
12493 sym = find_pc_sect_function (tpmarker->address, NULL);
12494 uiout->text ("Now in ");
12495 if (sym)
12496 {
12497 uiout->field_string ("func", sym->print_name (),
12498 function_name_style.style ());
12499 uiout->text (" at ");
12500 }
12501 uiout->field_string ("file",
12502 symtab_to_filename_for_display (sal2.symtab),
12503 file_name_style.style ());
12504 uiout->text (":");
12505
12506 if (uiout->is_mi_like_p ())
12507 {
12508 const char *fullname = symtab_to_fullname (sal2.symtab);
12509
12510 uiout->field_string ("fullname", fullname);
12511 }
12512
12513 uiout->field_signed ("line", sal2.line);
12514 uiout->text ("\n");
12515
12516 b->loc->line_number = sal2.line;
12517 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
12518
12519 b->location.reset (NULL);
12520 initialize_explicit_location (&explicit_loc);
12521 explicit_loc.source_filename
12522 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
12523 explicit_loc.line_offset.offset = b->loc->line_number;
12524 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
12525 b->location = new_explicit_location (&explicit_loc);
12526
12527 /* Might be nice to check if function changed, and warn if
12528 so. */
12529 }
12530 }
12531 return sal;
12532 }
12533
12534 /* Returns 1 iff locations A and B are sufficiently same that
12535 we don't need to report breakpoint as changed. */
12536
12537 static int
12538 locations_are_equal (struct bp_location *a, struct bp_location *b)
12539 {
12540 while (a && b)
12541 {
12542 if (a->address != b->address)
12543 return 0;
12544
12545 if (a->shlib_disabled != b->shlib_disabled)
12546 return 0;
12547
12548 if (a->enabled != b->enabled)
12549 return 0;
12550
12551 if (a->disabled_by_cond != b->disabled_by_cond)
12552 return 0;
12553
12554 a = a->next;
12555 b = b->next;
12556 }
12557
12558 if ((a == NULL) != (b == NULL))
12559 return 0;
12560
12561 return 1;
12562 }
12563
12564 /* Split all locations of B that are bound to PSPACE out of B's
12565 location list to a separate list and return that list's head. If
12566 PSPACE is NULL, hoist out all locations of B. */
12567
12568 static struct bp_location *
12569 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12570 {
12571 struct bp_location head;
12572 struct bp_location *i = b->loc;
12573 struct bp_location **i_link = &b->loc;
12574 struct bp_location *hoisted = &head;
12575
12576 if (pspace == NULL)
12577 {
12578 i = b->loc;
12579 b->loc = NULL;
12580 return i;
12581 }
12582
12583 head.next = NULL;
12584
12585 while (i != NULL)
12586 {
12587 if (i->pspace == pspace)
12588 {
12589 *i_link = i->next;
12590 i->next = NULL;
12591 hoisted->next = i;
12592 hoisted = i;
12593 }
12594 else
12595 i_link = &i->next;
12596 i = *i_link;
12597 }
12598
12599 return head.next;
12600 }
12601
12602 /* Create new breakpoint locations for B (a hardware or software
12603 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12604 zero, then B is a ranged breakpoint. Only recreates locations for
12605 FILTER_PSPACE. Locations of other program spaces are left
12606 untouched. */
12607
12608 void
12609 update_breakpoint_locations (struct breakpoint *b,
12610 struct program_space *filter_pspace,
12611 gdb::array_view<const symtab_and_line> sals,
12612 gdb::array_view<const symtab_and_line> sals_end)
12613 {
12614 struct bp_location *existing_locations;
12615
12616 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
12617 {
12618 /* Ranged breakpoints have only one start location and one end
12619 location. */
12620 b->enable_state = bp_disabled;
12621 gdb_printf (gdb_stderr,
12622 _("Could not reset ranged breakpoint %d: "
12623 "multiple locations found\n"),
12624 b->number);
12625 return;
12626 }
12627
12628 /* If there's no new locations, and all existing locations are
12629 pending, don't do anything. This optimizes the common case where
12630 all locations are in the same shared library, that was unloaded.
12631 We'd like to retain the location, so that when the library is
12632 loaded again, we don't loose the enabled/disabled status of the
12633 individual locations. */
12634 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
12635 return;
12636
12637 existing_locations = hoist_existing_locations (b, filter_pspace);
12638
12639 for (const auto &sal : sals)
12640 {
12641 struct bp_location *new_loc;
12642
12643 switch_to_program_space_and_thread (sal.pspace);
12644
12645 new_loc = b->add_location (sal);
12646
12647 /* Reparse conditions, they might contain references to the
12648 old symtab. */
12649 if (b->cond_string != NULL)
12650 {
12651 const char *s;
12652
12653 s = b->cond_string.get ();
12654 try
12655 {
12656 new_loc->cond = parse_exp_1 (&s, sal.pc,
12657 block_for_pc (sal.pc),
12658 0);
12659 }
12660 catch (const gdb_exception_error &e)
12661 {
12662 new_loc->disabled_by_cond = true;
12663 }
12664 }
12665
12666 if (!sals_end.empty ())
12667 {
12668 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
12669
12670 new_loc->length = end - sals[0].pc + 1;
12671 }
12672 }
12673
12674 /* If possible, carry over 'disable' status from existing
12675 breakpoints. */
12676 {
12677 struct bp_location *e = existing_locations;
12678 /* If there are multiple breakpoints with the same function name,
12679 e.g. for inline functions, comparing function names won't work.
12680 Instead compare pc addresses; this is just a heuristic as things
12681 may have moved, but in practice it gives the correct answer
12682 often enough until a better solution is found. */
12683 int have_ambiguous_names = ambiguous_names_p (b->loc);
12684
12685 for (; e; e = e->next)
12686 {
12687 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
12688 {
12689 if (have_ambiguous_names)
12690 {
12691 for (bp_location *l : b->locations ())
12692 {
12693 /* Ignore software vs hardware location type at
12694 this point, because with "set breakpoint
12695 auto-hw", after a re-set, locations that were
12696 hardware can end up as software, or vice versa.
12697 As mentioned above, this is an heuristic and in
12698 practice should give the correct answer often
12699 enough. */
12700 if (breakpoint_locations_match (e, l, true))
12701 {
12702 l->enabled = e->enabled;
12703 l->disabled_by_cond = e->disabled_by_cond;
12704 break;
12705 }
12706 }
12707 }
12708 else
12709 {
12710 for (bp_location *l : b->locations ())
12711 if (l->function_name
12712 && strcmp (e->function_name.get (),
12713 l->function_name.get ()) == 0)
12714 {
12715 l->enabled = e->enabled;
12716 l->disabled_by_cond = e->disabled_by_cond;
12717 break;
12718 }
12719 }
12720 }
12721 }
12722 }
12723
12724 if (!locations_are_equal (existing_locations, b->loc))
12725 gdb::observers::breakpoint_modified.notify (b);
12726 }
12727
12728 /* Find the SaL locations corresponding to the given LOCATION.
12729 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12730
12731 static std::vector<symtab_and_line>
12732 location_to_sals (struct breakpoint *b, struct event_location *location,
12733 struct program_space *search_pspace, int *found)
12734 {
12735 struct gdb_exception exception;
12736
12737 std::vector<symtab_and_line> sals;
12738
12739 try
12740 {
12741 sals = b->decode_location (location, search_pspace);
12742 }
12743 catch (gdb_exception_error &e)
12744 {
12745 int not_found_and_ok = 0;
12746
12747 /* For pending breakpoints, it's expected that parsing will
12748 fail until the right shared library is loaded. User has
12749 already told to create pending breakpoints and don't need
12750 extra messages. If breakpoint is in bp_shlib_disabled
12751 state, then user already saw the message about that
12752 breakpoint being disabled, and don't want to see more
12753 errors. */
12754 if (e.error == NOT_FOUND_ERROR
12755 && (b->condition_not_parsed
12756 || (b->loc != NULL
12757 && search_pspace != NULL
12758 && b->loc->pspace != search_pspace)
12759 || (b->loc && b->loc->shlib_disabled)
12760 || (b->loc && b->loc->pspace->executing_startup)
12761 || b->enable_state == bp_disabled))
12762 not_found_and_ok = 1;
12763
12764 if (!not_found_and_ok)
12765 {
12766 /* We surely don't want to warn about the same breakpoint
12767 10 times. One solution, implemented here, is disable
12768 the breakpoint on error. Another solution would be to
12769 have separate 'warning emitted' flag. Since this
12770 happens only when a binary has changed, I don't know
12771 which approach is better. */
12772 b->enable_state = bp_disabled;
12773 throw;
12774 }
12775
12776 exception = std::move (e);
12777 }
12778
12779 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
12780 {
12781 for (auto &sal : sals)
12782 resolve_sal_pc (&sal);
12783 if (b->condition_not_parsed && b->extra_string != NULL)
12784 {
12785 gdb::unique_xmalloc_ptr<char> cond_string, extra_string;
12786 int thread, task;
12787
12788 find_condition_and_thread_for_sals (sals, b->extra_string.get (),
12789 &cond_string, &thread,
12790 &task, &extra_string);
12791 gdb_assert (b->cond_string == NULL);
12792 if (cond_string)
12793 b->cond_string = std::move (cond_string);
12794 b->thread = thread;
12795 b->task = task;
12796 if (extra_string)
12797 b->extra_string = std::move (extra_string);
12798 b->condition_not_parsed = 0;
12799 }
12800
12801 if (b->type == bp_static_tracepoint)
12802 sals[0] = update_static_tracepoint (b, sals[0]);
12803
12804 *found = 1;
12805 }
12806 else
12807 *found = 0;
12808
12809 return sals;
12810 }
12811
12812 /* The default re_set method, for typical hardware or software
12813 breakpoints. Reevaluate the breakpoint and recreate its
12814 locations. */
12815
12816 static void
12817 breakpoint_re_set_default (struct breakpoint *b)
12818 {
12819 struct program_space *filter_pspace = current_program_space;
12820 std::vector<symtab_and_line> expanded, expanded_end;
12821
12822 int found;
12823 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
12824 filter_pspace, &found);
12825 if (found)
12826 expanded = std::move (sals);
12827
12828 if (b->location_range_end != NULL)
12829 {
12830 std::vector<symtab_and_line> sals_end
12831 = location_to_sals (b, b->location_range_end.get (),
12832 filter_pspace, &found);
12833 if (found)
12834 expanded_end = std::move (sals_end);
12835 }
12836
12837 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
12838 }
12839
12840 /* Default method for creating SALs from an address string. It basically
12841 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12842
12843 static void
12844 create_sals_from_location_default (struct event_location *location,
12845 struct linespec_result *canonical)
12846 {
12847 parse_breakpoint_sals (location, canonical);
12848 }
12849
12850 /* Decode the line represented by S by calling decode_line_full. This is the
12851 default function for the `decode_location' method of breakpoint_ops. */
12852
12853 static std::vector<symtab_and_line>
12854 decode_location_default (struct breakpoint *b,
12855 struct event_location *location,
12856 struct program_space *search_pspace)
12857 {
12858 struct linespec_result canonical;
12859
12860 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
12861 NULL, 0, &canonical, multiple_symbols_all,
12862 b->filter.get ());
12863
12864 /* We should get 0 or 1 resulting SALs. */
12865 gdb_assert (canonical.lsals.size () < 2);
12866
12867 if (!canonical.lsals.empty ())
12868 {
12869 const linespec_sals &lsal = canonical.lsals[0];
12870 return std::move (lsal.sals);
12871 }
12872 return {};
12873 }
12874
12875 /* Reset a breakpoint. */
12876
12877 static void
12878 breakpoint_re_set_one (breakpoint *b)
12879 {
12880 input_radix = b->input_radix;
12881 set_language (b->language);
12882
12883 b->re_set ();
12884 }
12885
12886 /* Re-set breakpoint locations for the current program space.
12887 Locations bound to other program spaces are left untouched. */
12888
12889 void
12890 breakpoint_re_set (void)
12891 {
12892 {
12893 scoped_restore_current_language save_language;
12894 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
12895 scoped_restore_current_pspace_and_thread restore_pspace_thread;
12896
12897 /* breakpoint_re_set_one sets the current_language to the language
12898 of the breakpoint it is resetting (see prepare_re_set_context)
12899 before re-evaluating the breakpoint's location. This change can
12900 unfortunately get undone by accident if the language_mode is set
12901 to auto, and we either switch frames, or more likely in this context,
12902 we select the current frame.
12903
12904 We prevent this by temporarily turning the language_mode to
12905 language_mode_manual. We restore it once all breakpoints
12906 have been reset. */
12907 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
12908 language_mode = language_mode_manual;
12909
12910 /* Note: we must not try to insert locations until after all
12911 breakpoints have been re-set. Otherwise, e.g., when re-setting
12912 breakpoint 1, we'd insert the locations of breakpoint 2, which
12913 hadn't been re-set yet, and thus may have stale locations. */
12914
12915 for (breakpoint *b : all_breakpoints_safe ())
12916 {
12917 try
12918 {
12919 breakpoint_re_set_one (b);
12920 }
12921 catch (const gdb_exception &ex)
12922 {
12923 exception_fprintf (gdb_stderr, ex,
12924 "Error in re-setting breakpoint %d: ",
12925 b->number);
12926 }
12927 }
12928
12929 jit_breakpoint_re_set ();
12930 }
12931
12932 create_overlay_event_breakpoint ();
12933 create_longjmp_master_breakpoint ();
12934 create_std_terminate_master_breakpoint ();
12935 create_exception_master_breakpoint ();
12936
12937 /* Now we can insert. */
12938 update_global_location_list (UGLL_MAY_INSERT);
12939 }
12940 \f
12941 /* Reset the thread number of this breakpoint:
12942
12943 - If the breakpoint is for all threads, leave it as-is.
12944 - Else, reset it to the current thread for inferior_ptid. */
12945 void
12946 breakpoint_re_set_thread (struct breakpoint *b)
12947 {
12948 if (b->thread != -1)
12949 {
12950 b->thread = inferior_thread ()->global_num;
12951
12952 /* We're being called after following a fork. The new fork is
12953 selected as current, and unless this was a vfork will have a
12954 different program space from the original thread. Reset that
12955 as well. */
12956 b->loc->pspace = current_program_space;
12957 }
12958 }
12959
12960 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
12961 If from_tty is nonzero, it prints a message to that effect,
12962 which ends with a period (no newline). */
12963
12964 void
12965 set_ignore_count (int bptnum, int count, int from_tty)
12966 {
12967 if (count < 0)
12968 count = 0;
12969
12970 for (breakpoint *b : all_breakpoints ())
12971 if (b->number == bptnum)
12972 {
12973 if (is_tracepoint (b))
12974 {
12975 if (from_tty && count != 0)
12976 gdb_printf (_("Ignore count ignored for tracepoint %d."),
12977 bptnum);
12978 return;
12979 }
12980
12981 b->ignore_count = count;
12982 if (from_tty)
12983 {
12984 if (count == 0)
12985 gdb_printf (_("Will stop next time "
12986 "breakpoint %d is reached."),
12987 bptnum);
12988 else if (count == 1)
12989 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
12990 bptnum);
12991 else
12992 gdb_printf (_("Will ignore next %d "
12993 "crossings of breakpoint %d."),
12994 count, bptnum);
12995 }
12996 gdb::observers::breakpoint_modified.notify (b);
12997 return;
12998 }
12999
13000 error (_("No breakpoint number %d."), bptnum);
13001 }
13002
13003 /* Command to set ignore-count of breakpoint N to COUNT. */
13004
13005 static void
13006 ignore_command (const char *args, int from_tty)
13007 {
13008 const char *p = args;
13009 int num;
13010
13011 if (p == 0)
13012 error_no_arg (_("a breakpoint number"));
13013
13014 num = get_number (&p);
13015 if (num == 0)
13016 error (_("bad breakpoint number: '%s'"), args);
13017 if (*p == 0)
13018 error (_("Second argument (specified ignore-count) is missing."));
13019
13020 set_ignore_count (num,
13021 longest_to_int (value_as_long (parse_and_eval (p))),
13022 from_tty);
13023 if (from_tty)
13024 gdb_printf ("\n");
13025 }
13026 \f
13027
13028 /* Call FUNCTION on each of the breakpoints with numbers in the range
13029 defined by BP_NUM_RANGE (an inclusive range). */
13030
13031 static void
13032 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13033 gdb::function_view<void (breakpoint *)> function)
13034 {
13035 if (bp_num_range.first == 0)
13036 {
13037 warning (_("bad breakpoint number at or near '%d'"),
13038 bp_num_range.first);
13039 }
13040 else
13041 {
13042 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13043 {
13044 bool match = false;
13045
13046 for (breakpoint *b : all_breakpoints_safe ())
13047 if (b->number == i)
13048 {
13049 match = true;
13050 function (b);
13051 break;
13052 }
13053 if (!match)
13054 gdb_printf (_("No breakpoint number %d.\n"), i);
13055 }
13056 }
13057 }
13058
13059 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13060 ARGS. */
13061
13062 static void
13063 map_breakpoint_numbers (const char *args,
13064 gdb::function_view<void (breakpoint *)> function)
13065 {
13066 if (args == NULL || *args == '\0')
13067 error_no_arg (_("one or more breakpoint numbers"));
13068
13069 number_or_range_parser parser (args);
13070
13071 while (!parser.finished ())
13072 {
13073 int num = parser.get_number ();
13074 map_breakpoint_number_range (std::make_pair (num, num), function);
13075 }
13076 }
13077
13078 /* Return the breakpoint location structure corresponding to the
13079 BP_NUM and LOC_NUM values. */
13080
13081 static struct bp_location *
13082 find_location_by_number (int bp_num, int loc_num)
13083 {
13084 breakpoint *b = get_breakpoint (bp_num);
13085
13086 if (!b || b->number != bp_num)
13087 error (_("Bad breakpoint number '%d'"), bp_num);
13088
13089 if (loc_num == 0)
13090 error (_("Bad breakpoint location number '%d'"), loc_num);
13091
13092 int n = 0;
13093 for (bp_location *loc : b->locations ())
13094 if (++n == loc_num)
13095 return loc;
13096
13097 error (_("Bad breakpoint location number '%d'"), loc_num);
13098 }
13099
13100 /* Modes of operation for extract_bp_num. */
13101 enum class extract_bp_kind
13102 {
13103 /* Extracting a breakpoint number. */
13104 bp,
13105
13106 /* Extracting a location number. */
13107 loc,
13108 };
13109
13110 /* Extract a breakpoint or location number (as determined by KIND)
13111 from the string starting at START. TRAILER is a character which
13112 can be found after the number. If you don't want a trailer, use
13113 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13114 string. This always returns a positive integer. */
13115
13116 static int
13117 extract_bp_num (extract_bp_kind kind, const char *start,
13118 int trailer, const char **end_out = NULL)
13119 {
13120 const char *end = start;
13121 int num = get_number_trailer (&end, trailer);
13122 if (num < 0)
13123 error (kind == extract_bp_kind::bp
13124 ? _("Negative breakpoint number '%.*s'")
13125 : _("Negative breakpoint location number '%.*s'"),
13126 int (end - start), start);
13127 if (num == 0)
13128 error (kind == extract_bp_kind::bp
13129 ? _("Bad breakpoint number '%.*s'")
13130 : _("Bad breakpoint location number '%.*s'"),
13131 int (end - start), start);
13132
13133 if (end_out != NULL)
13134 *end_out = end;
13135 return num;
13136 }
13137
13138 /* Extract a breakpoint or location range (as determined by KIND) in
13139 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13140 representing the (inclusive) range. The returned pair's elements
13141 are always positive integers. */
13142
13143 static std::pair<int, int>
13144 extract_bp_or_bp_range (extract_bp_kind kind,
13145 const std::string &arg,
13146 std::string::size_type arg_offset)
13147 {
13148 std::pair<int, int> range;
13149 const char *bp_loc = &arg[arg_offset];
13150 std::string::size_type dash = arg.find ('-', arg_offset);
13151 if (dash != std::string::npos)
13152 {
13153 /* bp_loc is a range (x-z). */
13154 if (arg.length () == dash + 1)
13155 error (kind == extract_bp_kind::bp
13156 ? _("Bad breakpoint number at or near: '%s'")
13157 : _("Bad breakpoint location number at or near: '%s'"),
13158 bp_loc);
13159
13160 const char *end;
13161 const char *start_first = bp_loc;
13162 const char *start_second = &arg[dash + 1];
13163 range.first = extract_bp_num (kind, start_first, '-');
13164 range.second = extract_bp_num (kind, start_second, '\0', &end);
13165
13166 if (range.first > range.second)
13167 error (kind == extract_bp_kind::bp
13168 ? _("Inverted breakpoint range at '%.*s'")
13169 : _("Inverted breakpoint location range at '%.*s'"),
13170 int (end - start_first), start_first);
13171 }
13172 else
13173 {
13174 /* bp_loc is a single value. */
13175 range.first = extract_bp_num (kind, bp_loc, '\0');
13176 range.second = range.first;
13177 }
13178 return range;
13179 }
13180
13181 /* Extract the breakpoint/location range specified by ARG. Returns
13182 the breakpoint range in BP_NUM_RANGE, and the location range in
13183 BP_LOC_RANGE.
13184
13185 ARG may be in any of the following forms:
13186
13187 x where 'x' is a breakpoint number.
13188 x-y where 'x' and 'y' specify a breakpoint numbers range.
13189 x.y where 'x' is a breakpoint number and 'y' a location number.
13190 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13191 location number range.
13192 */
13193
13194 static void
13195 extract_bp_number_and_location (const std::string &arg,
13196 std::pair<int, int> &bp_num_range,
13197 std::pair<int, int> &bp_loc_range)
13198 {
13199 std::string::size_type dot = arg.find ('.');
13200
13201 if (dot != std::string::npos)
13202 {
13203 /* Handle 'x.y' and 'x.y-z' cases. */
13204
13205 if (arg.length () == dot + 1 || dot == 0)
13206 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
13207
13208 bp_num_range.first
13209 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13210 bp_num_range.second = bp_num_range.first;
13211
13212 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13213 arg, dot + 1);
13214 }
13215 else
13216 {
13217 /* Handle x and x-y cases. */
13218
13219 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
13220 bp_loc_range.first = 0;
13221 bp_loc_range.second = 0;
13222 }
13223 }
13224
13225 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13226 specifies whether to enable or disable. */
13227
13228 static void
13229 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13230 {
13231 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13232 if (loc != NULL)
13233 {
13234 if (loc->disabled_by_cond && enable)
13235 error (_("Breakpoint %d's condition is invalid at location %d, "
13236 "cannot enable."), bp_num, loc_num);
13237
13238 if (loc->enabled != enable)
13239 {
13240 loc->enabled = enable;
13241 mark_breakpoint_location_modified (loc);
13242 }
13243 if (target_supports_enable_disable_tracepoint ()
13244 && current_trace_status ()->running && loc->owner
13245 && is_tracepoint (loc->owner))
13246 target_disable_tracepoint (loc);
13247 }
13248 update_global_location_list (UGLL_DONT_INSERT);
13249
13250 gdb::observers::breakpoint_modified.notify (loc->owner);
13251 }
13252
13253 /* Enable or disable a range of breakpoint locations. BP_NUM is the
13254 number of the breakpoint, and BP_LOC_RANGE specifies the
13255 (inclusive) range of location numbers of that breakpoint to
13256 enable/disable. ENABLE specifies whether to enable or disable the
13257 location. */
13258
13259 static void
13260 enable_disable_breakpoint_location_range (int bp_num,
13261 std::pair<int, int> &bp_loc_range,
13262 bool enable)
13263 {
13264 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13265 enable_disable_bp_num_loc (bp_num, i, enable);
13266 }
13267
13268 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13269 If from_tty is nonzero, it prints a message to that effect,
13270 which ends with a period (no newline). */
13271
13272 void
13273 disable_breakpoint (struct breakpoint *bpt)
13274 {
13275 /* Never disable a watchpoint scope breakpoint; we want to
13276 hit them when we leave scope so we can delete both the
13277 watchpoint and its scope breakpoint at that time. */
13278 if (bpt->type == bp_watchpoint_scope)
13279 return;
13280
13281 bpt->enable_state = bp_disabled;
13282
13283 /* Mark breakpoint locations modified. */
13284 mark_breakpoint_modified (bpt);
13285
13286 if (target_supports_enable_disable_tracepoint ()
13287 && current_trace_status ()->running && is_tracepoint (bpt))
13288 {
13289 for (bp_location *location : bpt->locations ())
13290 target_disable_tracepoint (location);
13291 }
13292
13293 update_global_location_list (UGLL_DONT_INSERT);
13294
13295 gdb::observers::breakpoint_modified.notify (bpt);
13296 }
13297
13298 /* Enable or disable the breakpoint(s) or breakpoint location(s)
13299 specified in ARGS. ARGS may be in any of the formats handled by
13300 extract_bp_number_and_location. ENABLE specifies whether to enable
13301 or disable the breakpoints/locations. */
13302
13303 static void
13304 enable_disable_command (const char *args, int from_tty, bool enable)
13305 {
13306 if (args == 0)
13307 {
13308 for (breakpoint *bpt : all_breakpoints ())
13309 if (user_breakpoint_p (bpt))
13310 {
13311 if (enable)
13312 enable_breakpoint (bpt);
13313 else
13314 disable_breakpoint (bpt);
13315 }
13316 }
13317 else
13318 {
13319 std::string num = extract_arg (&args);
13320
13321 while (!num.empty ())
13322 {
13323 std::pair<int, int> bp_num_range, bp_loc_range;
13324
13325 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13326
13327 if (bp_loc_range.first == bp_loc_range.second
13328 && bp_loc_range.first == 0)
13329 {
13330 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
13331 map_breakpoint_number_range (bp_num_range,
13332 enable
13333 ? enable_breakpoint
13334 : disable_breakpoint);
13335 }
13336 else
13337 {
13338 /* Handle breakpoint ids with formats 'x.y' or
13339 'x.y-z'. */
13340 enable_disable_breakpoint_location_range
13341 (bp_num_range.first, bp_loc_range, enable);
13342 }
13343 num = extract_arg (&args);
13344 }
13345 }
13346 }
13347
13348 /* The disable command disables the specified breakpoints/locations
13349 (or all defined breakpoints) so they're no longer effective in
13350 stopping the inferior. ARGS may be in any of the forms defined in
13351 extract_bp_number_and_location. */
13352
13353 static void
13354 disable_command (const char *args, int from_tty)
13355 {
13356 enable_disable_command (args, from_tty, false);
13357 }
13358
13359 static void
13360 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13361 int count)
13362 {
13363 int target_resources_ok;
13364
13365 if (bpt->type == bp_hardware_breakpoint)
13366 {
13367 int i;
13368 i = hw_breakpoint_used_count ();
13369 target_resources_ok =
13370 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13371 i + 1, 0);
13372 if (target_resources_ok == 0)
13373 error (_("No hardware breakpoint support in the target."));
13374 else if (target_resources_ok < 0)
13375 error (_("Hardware breakpoints used exceeds limit."));
13376 }
13377
13378 if (is_watchpoint (bpt))
13379 {
13380 /* Initialize it just to avoid a GCC false warning. */
13381 enum enable_state orig_enable_state = bp_disabled;
13382
13383 try
13384 {
13385 struct watchpoint *w = (struct watchpoint *) bpt;
13386
13387 orig_enable_state = bpt->enable_state;
13388 bpt->enable_state = bp_enabled;
13389 update_watchpoint (w, 1 /* reparse */);
13390 }
13391 catch (const gdb_exception &e)
13392 {
13393 bpt->enable_state = orig_enable_state;
13394 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13395 bpt->number);
13396 return;
13397 }
13398 }
13399
13400 bpt->enable_state = bp_enabled;
13401
13402 /* Mark breakpoint locations modified. */
13403 mark_breakpoint_modified (bpt);
13404
13405 if (target_supports_enable_disable_tracepoint ()
13406 && current_trace_status ()->running && is_tracepoint (bpt))
13407 {
13408 for (bp_location *location : bpt->locations ())
13409 target_enable_tracepoint (location);
13410 }
13411
13412 bpt->disposition = disposition;
13413 bpt->enable_count = count;
13414 update_global_location_list (UGLL_MAY_INSERT);
13415
13416 gdb::observers::breakpoint_modified.notify (bpt);
13417 }
13418
13419
13420 void
13421 enable_breakpoint (struct breakpoint *bpt)
13422 {
13423 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13424 }
13425
13426 /* The enable command enables the specified breakpoints/locations (or
13427 all defined breakpoints) so they once again become (or continue to
13428 be) effective in stopping the inferior. ARGS may be in any of the
13429 forms defined in extract_bp_number_and_location. */
13430
13431 static void
13432 enable_command (const char *args, int from_tty)
13433 {
13434 enable_disable_command (args, from_tty, true);
13435 }
13436
13437 static void
13438 enable_once_command (const char *args, int from_tty)
13439 {
13440 map_breakpoint_numbers
13441 (args, [&] (breakpoint *b)
13442 {
13443 iterate_over_related_breakpoints
13444 (b, [&] (breakpoint *bpt)
13445 {
13446 enable_breakpoint_disp (bpt, disp_disable, 1);
13447 });
13448 });
13449 }
13450
13451 static void
13452 enable_count_command (const char *args, int from_tty)
13453 {
13454 int count;
13455
13456 if (args == NULL)
13457 error_no_arg (_("hit count"));
13458
13459 count = get_number (&args);
13460
13461 map_breakpoint_numbers
13462 (args, [&] (breakpoint *b)
13463 {
13464 iterate_over_related_breakpoints
13465 (b, [&] (breakpoint *bpt)
13466 {
13467 enable_breakpoint_disp (bpt, disp_disable, count);
13468 });
13469 });
13470 }
13471
13472 static void
13473 enable_delete_command (const char *args, int from_tty)
13474 {
13475 map_breakpoint_numbers
13476 (args, [&] (breakpoint *b)
13477 {
13478 iterate_over_related_breakpoints
13479 (b, [&] (breakpoint *bpt)
13480 {
13481 enable_breakpoint_disp (bpt, disp_del, 1);
13482 });
13483 });
13484 }
13485 \f
13486 /* Invalidate last known value of any hardware watchpoint if
13487 the memory which that value represents has been written to by
13488 GDB itself. */
13489
13490 static void
13491 invalidate_bp_value_on_memory_change (struct inferior *inferior,
13492 CORE_ADDR addr, ssize_t len,
13493 const bfd_byte *data)
13494 {
13495 for (breakpoint *bp : all_breakpoints ())
13496 if (bp->enable_state == bp_enabled
13497 && bp->type == bp_hardware_watchpoint)
13498 {
13499 struct watchpoint *wp = (struct watchpoint *) bp;
13500
13501 if (wp->val_valid && wp->val != nullptr)
13502 {
13503 for (bp_location *loc : bp->locations ())
13504 if (loc->loc_type == bp_loc_hardware_watchpoint
13505 && loc->address + loc->length > addr
13506 && addr + len > loc->address)
13507 {
13508 wp->val = NULL;
13509 wp->val_valid = false;
13510 }
13511 }
13512 }
13513 }
13514
13515 /* Create and insert a breakpoint for software single step. */
13516
13517 void
13518 insert_single_step_breakpoint (struct gdbarch *gdbarch,
13519 const address_space *aspace,
13520 CORE_ADDR next_pc)
13521 {
13522 struct thread_info *tp = inferior_thread ();
13523 struct symtab_and_line sal;
13524 CORE_ADDR pc = next_pc;
13525
13526 if (tp->control.single_step_breakpoints == NULL)
13527 {
13528 tp->control.single_step_breakpoints
13529 = new_single_step_breakpoint (tp->global_num, gdbarch);
13530 }
13531
13532 sal = find_pc_line (pc, 0);
13533 sal.pc = pc;
13534 sal.section = find_pc_overlay (pc);
13535 sal.explicit_pc = 1;
13536 tp->control.single_step_breakpoints->add_location (sal);
13537
13538 update_global_location_list (UGLL_INSERT);
13539 }
13540
13541 /* Insert single step breakpoints according to the current state. */
13542
13543 int
13544 insert_single_step_breakpoints (struct gdbarch *gdbarch)
13545 {
13546 struct regcache *regcache = get_current_regcache ();
13547 std::vector<CORE_ADDR> next_pcs;
13548
13549 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
13550
13551 if (!next_pcs.empty ())
13552 {
13553 struct frame_info *frame = get_current_frame ();
13554 const address_space *aspace = get_frame_address_space (frame);
13555
13556 for (CORE_ADDR pc : next_pcs)
13557 insert_single_step_breakpoint (gdbarch, aspace, pc);
13558
13559 return 1;
13560 }
13561 else
13562 return 0;
13563 }
13564
13565 /* See breakpoint.h. */
13566
13567 int
13568 breakpoint_has_location_inserted_here (struct breakpoint *bp,
13569 const address_space *aspace,
13570 CORE_ADDR pc)
13571 {
13572 for (bp_location *loc : bp->locations ())
13573 if (loc->inserted
13574 && breakpoint_location_address_match (loc, aspace, pc))
13575 return 1;
13576
13577 return 0;
13578 }
13579
13580 /* Check whether a software single-step breakpoint is inserted at
13581 PC. */
13582
13583 int
13584 single_step_breakpoint_inserted_here_p (const address_space *aspace,
13585 CORE_ADDR pc)
13586 {
13587 for (breakpoint *bpt : all_breakpoints ())
13588 {
13589 if (bpt->type == bp_single_step
13590 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13591 return 1;
13592 }
13593 return 0;
13594 }
13595
13596 /* Tracepoint-specific operations. */
13597
13598 /* Set tracepoint count to NUM. */
13599 static void
13600 set_tracepoint_count (int num)
13601 {
13602 tracepoint_count = num;
13603 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
13604 }
13605
13606 static void
13607 trace_command (const char *arg, int from_tty)
13608 {
13609 event_location_up location = string_to_event_location (&arg,
13610 current_language);
13611 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
13612 (location.get (), true /* is_tracepoint */);
13613
13614 create_breakpoint (get_current_arch (),
13615 location.get (),
13616 NULL, 0, arg, false, 1 /* parse arg */,
13617 0 /* tempflag */,
13618 bp_tracepoint /* type_wanted */,
13619 0 /* Ignore count */,
13620 pending_break_support,
13621 ops,
13622 from_tty,
13623 1 /* enabled */,
13624 0 /* internal */, 0);
13625 }
13626
13627 static void
13628 ftrace_command (const char *arg, int from_tty)
13629 {
13630 event_location_up location = string_to_event_location (&arg,
13631 current_language);
13632 create_breakpoint (get_current_arch (),
13633 location.get (),
13634 NULL, 0, arg, false, 1 /* parse arg */,
13635 0 /* tempflag */,
13636 bp_fast_tracepoint /* type_wanted */,
13637 0 /* Ignore count */,
13638 pending_break_support,
13639 &base_breakpoint_ops,
13640 from_tty,
13641 1 /* enabled */,
13642 0 /* internal */, 0);
13643 }
13644
13645 /* strace command implementation. Creates a static tracepoint. */
13646
13647 static void
13648 strace_command (const char *arg, int from_tty)
13649 {
13650 const struct breakpoint_ops *ops;
13651 event_location_up location;
13652 enum bptype type;
13653
13654 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13655 or with a normal static tracepoint. */
13656 if (arg && startswith (arg, "-m") && isspace (arg[2]))
13657 {
13658 ops = &strace_marker_breakpoint_ops;
13659 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
13660 type = bp_static_marker_tracepoint;
13661 }
13662 else
13663 {
13664 ops = &base_breakpoint_ops;
13665 location = string_to_event_location (&arg, current_language);
13666 type = bp_static_tracepoint;
13667 }
13668
13669 create_breakpoint (get_current_arch (),
13670 location.get (),
13671 NULL, 0, arg, false, 1 /* parse arg */,
13672 0 /* tempflag */,
13673 type /* type_wanted */,
13674 0 /* Ignore count */,
13675 pending_break_support,
13676 ops,
13677 from_tty,
13678 1 /* enabled */,
13679 0 /* internal */, 0);
13680 }
13681
13682 /* Set up a fake reader function that gets command lines from a linked
13683 list that was acquired during tracepoint uploading. */
13684
13685 static struct uploaded_tp *this_utp;
13686 static int next_cmd;
13687
13688 static char *
13689 read_uploaded_action (void)
13690 {
13691 char *rslt = nullptr;
13692
13693 if (next_cmd < this_utp->cmd_strings.size ())
13694 {
13695 rslt = this_utp->cmd_strings[next_cmd].get ();
13696 next_cmd++;
13697 }
13698
13699 return rslt;
13700 }
13701
13702 /* Given information about a tracepoint as recorded on a target (which
13703 can be either a live system or a trace file), attempt to create an
13704 equivalent GDB tracepoint. This is not a reliable process, since
13705 the target does not necessarily have all the information used when
13706 the tracepoint was originally defined. */
13707
13708 struct tracepoint *
13709 create_tracepoint_from_upload (struct uploaded_tp *utp)
13710 {
13711 const char *addr_str;
13712 char small_buf[100];
13713 struct tracepoint *tp;
13714
13715 if (utp->at_string)
13716 addr_str = utp->at_string.get ();
13717 else
13718 {
13719 /* In the absence of a source location, fall back to raw
13720 address. Since there is no way to confirm that the address
13721 means the same thing as when the trace was started, warn the
13722 user. */
13723 warning (_("Uploaded tracepoint %d has no "
13724 "source location, using raw address"),
13725 utp->number);
13726 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
13727 addr_str = small_buf;
13728 }
13729
13730 /* There's not much we can do with a sequence of bytecodes. */
13731 if (utp->cond && !utp->cond_string)
13732 warning (_("Uploaded tracepoint %d condition "
13733 "has no source form, ignoring it"),
13734 utp->number);
13735
13736 event_location_up location = string_to_event_location (&addr_str,
13737 current_language);
13738 if (!create_breakpoint (get_current_arch (),
13739 location.get (),
13740 utp->cond_string.get (), -1, addr_str,
13741 false /* force_condition */,
13742 0 /* parse cond/thread */,
13743 0 /* tempflag */,
13744 utp->type /* type_wanted */,
13745 0 /* Ignore count */,
13746 pending_break_support,
13747 &base_breakpoint_ops,
13748 0 /* from_tty */,
13749 utp->enabled /* enabled */,
13750 0 /* internal */,
13751 CREATE_BREAKPOINT_FLAGS_INSERTED))
13752 return NULL;
13753
13754 /* Get the tracepoint we just created. */
13755 tp = get_tracepoint (tracepoint_count);
13756 gdb_assert (tp != NULL);
13757
13758 if (utp->pass > 0)
13759 {
13760 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
13761 tp->number);
13762
13763 trace_pass_command (small_buf, 0);
13764 }
13765
13766 /* If we have uploaded versions of the original commands, set up a
13767 special-purpose "reader" function and call the usual command line
13768 reader, then pass the result to the breakpoint command-setting
13769 function. */
13770 if (!utp->cmd_strings.empty ())
13771 {
13772 counted_command_line cmd_list;
13773
13774 this_utp = utp;
13775 next_cmd = 0;
13776
13777 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
13778
13779 breakpoint_set_commands (tp, std::move (cmd_list));
13780 }
13781 else if (!utp->actions.empty ()
13782 || !utp->step_actions.empty ())
13783 warning (_("Uploaded tracepoint %d actions "
13784 "have no source form, ignoring them"),
13785 utp->number);
13786
13787 /* Copy any status information that might be available. */
13788 tp->hit_count = utp->hit_count;
13789 tp->traceframe_usage = utp->traceframe_usage;
13790
13791 return tp;
13792 }
13793
13794 /* Print information on tracepoint number TPNUM_EXP, or all if
13795 omitted. */
13796
13797 static void
13798 info_tracepoints_command (const char *args, int from_tty)
13799 {
13800 struct ui_out *uiout = current_uiout;
13801 int num_printed;
13802
13803 num_printed = breakpoint_1 (args, false, is_tracepoint);
13804
13805 if (num_printed == 0)
13806 {
13807 if (args == NULL || *args == '\0')
13808 uiout->message ("No tracepoints.\n");
13809 else
13810 uiout->message ("No tracepoint matching '%s'.\n", args);
13811 }
13812
13813 default_collect_info ();
13814 }
13815
13816 /* The 'enable trace' command enables tracepoints.
13817 Not supported by all targets. */
13818 static void
13819 enable_trace_command (const char *args, int from_tty)
13820 {
13821 enable_command (args, from_tty);
13822 }
13823
13824 /* The 'disable trace' command disables tracepoints.
13825 Not supported by all targets. */
13826 static void
13827 disable_trace_command (const char *args, int from_tty)
13828 {
13829 disable_command (args, from_tty);
13830 }
13831
13832 /* Remove a tracepoint (or all if no argument). */
13833 static void
13834 delete_trace_command (const char *arg, int from_tty)
13835 {
13836 dont_repeat ();
13837
13838 if (arg == 0)
13839 {
13840 int breaks_to_delete = 0;
13841
13842 /* Delete all breakpoints if no argument.
13843 Do not delete internal or call-dummy breakpoints, these
13844 have to be deleted with an explicit breakpoint number
13845 argument. */
13846 for (breakpoint *tp : all_tracepoints ())
13847 if (is_tracepoint (tp) && user_breakpoint_p (tp))
13848 {
13849 breaks_to_delete = 1;
13850 break;
13851 }
13852
13853 /* Ask user only if there are some breakpoints to delete. */
13854 if (!from_tty
13855 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13856 {
13857 for (breakpoint *b : all_breakpoints_safe ())
13858 if (is_tracepoint (b) && user_breakpoint_p (b))
13859 delete_breakpoint (b);
13860 }
13861 }
13862 else
13863 map_breakpoint_numbers
13864 (arg, [&] (breakpoint *br)
13865 {
13866 iterate_over_related_breakpoints (br, delete_breakpoint);
13867 });
13868 }
13869
13870 /* Helper function for trace_pass_command. */
13871
13872 static void
13873 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
13874 {
13875 tp->pass_count = count;
13876 gdb::observers::breakpoint_modified.notify (tp);
13877 if (from_tty)
13878 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
13879 tp->number, count);
13880 }
13881
13882 /* Set passcount for tracepoint.
13883
13884 First command argument is passcount, second is tracepoint number.
13885 If tracepoint number omitted, apply to most recently defined.
13886 Also accepts special argument "all". */
13887
13888 static void
13889 trace_pass_command (const char *args, int from_tty)
13890 {
13891 struct tracepoint *t1;
13892 ULONGEST count;
13893
13894 if (args == 0 || *args == 0)
13895 error (_("passcount command requires an "
13896 "argument (count + optional TP num)"));
13897
13898 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
13899
13900 args = skip_spaces (args);
13901 if (*args && strncasecmp (args, "all", 3) == 0)
13902 {
13903 args += 3; /* Skip special argument "all". */
13904 if (*args)
13905 error (_("Junk at end of arguments."));
13906
13907 for (breakpoint *b : all_tracepoints ())
13908 {
13909 t1 = (struct tracepoint *) b;
13910 trace_pass_set_count (t1, count, from_tty);
13911 }
13912 }
13913 else if (*args == '\0')
13914 {
13915 t1 = get_tracepoint_by_number (&args, NULL);
13916 if (t1)
13917 trace_pass_set_count (t1, count, from_tty);
13918 }
13919 else
13920 {
13921 number_or_range_parser parser (args);
13922 while (!parser.finished ())
13923 {
13924 t1 = get_tracepoint_by_number (&args, &parser);
13925 if (t1)
13926 trace_pass_set_count (t1, count, from_tty);
13927 }
13928 }
13929 }
13930
13931 struct tracepoint *
13932 get_tracepoint (int num)
13933 {
13934 for (breakpoint *t : all_tracepoints ())
13935 if (t->number == num)
13936 return (struct tracepoint *) t;
13937
13938 return NULL;
13939 }
13940
13941 /* Find the tracepoint with the given target-side number (which may be
13942 different from the tracepoint number after disconnecting and
13943 reconnecting). */
13944
13945 struct tracepoint *
13946 get_tracepoint_by_number_on_target (int num)
13947 {
13948 for (breakpoint *b : all_tracepoints ())
13949 {
13950 struct tracepoint *t = (struct tracepoint *) b;
13951
13952 if (t->number_on_target == num)
13953 return t;
13954 }
13955
13956 return NULL;
13957 }
13958
13959 /* Utility: parse a tracepoint number and look it up in the list.
13960 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
13961 If the argument is missing, the most recent tracepoint
13962 (tracepoint_count) is returned. */
13963
13964 struct tracepoint *
13965 get_tracepoint_by_number (const char **arg,
13966 number_or_range_parser *parser)
13967 {
13968 int tpnum;
13969 const char *instring = arg == NULL ? NULL : *arg;
13970
13971 if (parser != NULL)
13972 {
13973 gdb_assert (!parser->finished ());
13974 tpnum = parser->get_number ();
13975 }
13976 else if (arg == NULL || *arg == NULL || ! **arg)
13977 tpnum = tracepoint_count;
13978 else
13979 tpnum = get_number (arg);
13980
13981 if (tpnum <= 0)
13982 {
13983 if (instring && *instring)
13984 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
13985 instring);
13986 else
13987 gdb_printf (_("No previous tracepoint\n"));
13988 return NULL;
13989 }
13990
13991 for (breakpoint *t : all_tracepoints ())
13992 if (t->number == tpnum)
13993 return (struct tracepoint *) t;
13994
13995 gdb_printf ("No tracepoint number %d.\n", tpnum);
13996 return NULL;
13997 }
13998
13999 void
14000 breakpoint::print_recreate_thread (struct ui_file *fp) const
14001 {
14002 if (thread != -1)
14003 gdb_printf (fp, " thread %d", thread);
14004
14005 if (task != 0)
14006 gdb_printf (fp, " task %d", task);
14007
14008 gdb_printf (fp, "\n");
14009 }
14010
14011 /* Save information on user settable breakpoints (watchpoints, etc) to
14012 a new script file named FILENAME. If FILTER is non-NULL, call it
14013 on each breakpoint and only include the ones for which it returns
14014 true. */
14015
14016 static void
14017 save_breakpoints (const char *filename, int from_tty,
14018 bool (*filter) (const struct breakpoint *))
14019 {
14020 int any = 0;
14021 int extra_trace_bits = 0;
14022
14023 if (filename == 0 || *filename == 0)
14024 error (_("Argument required (file name in which to save)"));
14025
14026 /* See if we have anything to save. */
14027 for (breakpoint *tp : all_breakpoints ())
14028 {
14029 /* Skip internal and momentary breakpoints. */
14030 if (!user_breakpoint_p (tp))
14031 continue;
14032
14033 /* If we have a filter, only save the breakpoints it accepts. */
14034 if (filter && !filter (tp))
14035 continue;
14036
14037 any = 1;
14038
14039 if (is_tracepoint (tp))
14040 {
14041 extra_trace_bits = 1;
14042
14043 /* We can stop searching. */
14044 break;
14045 }
14046 }
14047
14048 if (!any)
14049 {
14050 warning (_("Nothing to save."));
14051 return;
14052 }
14053
14054 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14055
14056 stdio_file fp;
14057
14058 if (!fp.open (expanded_filename.get (), "w"))
14059 error (_("Unable to open file '%s' for saving (%s)"),
14060 expanded_filename.get (), safe_strerror (errno));
14061
14062 if (extra_trace_bits)
14063 save_trace_state_variables (&fp);
14064
14065 for (breakpoint *tp : all_breakpoints ())
14066 {
14067 /* Skip internal and momentary breakpoints. */
14068 if (!user_breakpoint_p (tp))
14069 continue;
14070
14071 /* If we have a filter, only save the breakpoints it accepts. */
14072 if (filter && !filter (tp))
14073 continue;
14074
14075 tp->print_recreate (&fp);
14076
14077 /* Note, we can't rely on tp->number for anything, as we can't
14078 assume the recreated breakpoint numbers will match. Use $bpnum
14079 instead. */
14080
14081 if (tp->cond_string)
14082 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
14083
14084 if (tp->ignore_count)
14085 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
14086
14087 if (tp->type != bp_dprintf && tp->commands)
14088 {
14089 fp.puts (" commands\n");
14090
14091 current_uiout->redirect (&fp);
14092 try
14093 {
14094 print_command_lines (current_uiout, tp->commands.get (), 2);
14095 }
14096 catch (const gdb_exception &ex)
14097 {
14098 current_uiout->redirect (NULL);
14099 throw;
14100 }
14101
14102 current_uiout->redirect (NULL);
14103 fp.puts (" end\n");
14104 }
14105
14106 if (tp->enable_state == bp_disabled)
14107 fp.puts ("disable $bpnum\n");
14108
14109 /* If this is a multi-location breakpoint, check if the locations
14110 should be individually disabled. Watchpoint locations are
14111 special, and not user visible. */
14112 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14113 {
14114 int n = 1;
14115
14116 for (bp_location *loc : tp->locations ())
14117 {
14118 if (!loc->enabled)
14119 fp.printf ("disable $bpnum.%d\n", n);
14120
14121 n++;
14122 }
14123 }
14124 }
14125
14126 if (extra_trace_bits && !default_collect.empty ())
14127 fp.printf ("set default-collect %s\n", default_collect.c_str ());
14128
14129 if (from_tty)
14130 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
14131 }
14132
14133 /* The `save breakpoints' command. */
14134
14135 static void
14136 save_breakpoints_command (const char *args, int from_tty)
14137 {
14138 save_breakpoints (args, from_tty, NULL);
14139 }
14140
14141 /* The `save tracepoints' command. */
14142
14143 static void
14144 save_tracepoints_command (const char *args, int from_tty)
14145 {
14146 save_breakpoints (args, from_tty, is_tracepoint);
14147 }
14148
14149 \f
14150 /* This help string is used to consolidate all the help string for specifying
14151 locations used by several commands. */
14152
14153 #define LOCATION_HELP_STRING \
14154 "Linespecs are colon-separated lists of location parameters, such as\n\
14155 source filename, function name, label name, and line number.\n\
14156 Example: To specify the start of a label named \"the_top\" in the\n\
14157 function \"fact\" in the file \"factorial.c\", use\n\
14158 \"factorial.c:fact:the_top\".\n\
14159 \n\
14160 Address locations begin with \"*\" and specify an exact address in the\n\
14161 program. Example: To specify the fourth byte past the start function\n\
14162 \"main\", use \"*main + 4\".\n\
14163 \n\
14164 Explicit locations are similar to linespecs but use an option/argument\n\
14165 syntax to specify location parameters.\n\
14166 Example: To specify the start of the label named \"the_top\" in the\n\
14167 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
14168 -function fact -label the_top\".\n\
14169 \n\
14170 By default, a specified function is matched against the program's\n\
14171 functions in all scopes. For C++, this means in all namespaces and\n\
14172 classes. For Ada, this means in all packages. E.g., in C++,\n\
14173 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14174 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
14175 specified name as a complete fully-qualified name instead."
14176
14177 /* This help string is used for the break, hbreak, tbreak and thbreak
14178 commands. It is defined as a macro to prevent duplication.
14179 COMMAND should be a string constant containing the name of the
14180 command. */
14181
14182 #define BREAK_ARGS_HELP(command) \
14183 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14184 \t[-force-condition] [if CONDITION]\n\
14185 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14186 probe point. Accepted values are `-probe' (for a generic, automatically\n\
14187 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14188 `-probe-dtrace' (for a DTrace probe).\n\
14189 LOCATION may be a linespec, address, or explicit location as described\n\
14190 below.\n\
14191 \n\
14192 With no LOCATION, uses current execution address of the selected\n\
14193 stack frame. This is useful for breaking on return to a stack frame.\n\
14194 \n\
14195 THREADNUM is the number from \"info threads\".\n\
14196 CONDITION is a boolean expression.\n\
14197 \n\
14198 With the \"-force-condition\" flag, the condition is defined even when\n\
14199 it is invalid for all current locations.\n\
14200 \n" LOCATION_HELP_STRING "\n\n\
14201 Multiple breakpoints at one place are permitted, and useful if their\n\
14202 conditions are different.\n\
14203 \n\
14204 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14205
14206 /* List of subcommands for "catch". */
14207 static struct cmd_list_element *catch_cmdlist;
14208
14209 /* List of subcommands for "tcatch". */
14210 static struct cmd_list_element *tcatch_cmdlist;
14211
14212 void
14213 add_catch_command (const char *name, const char *docstring,
14214 cmd_func_ftype *func,
14215 completer_ftype *completer,
14216 void *user_data_catch,
14217 void *user_data_tcatch)
14218 {
14219 struct cmd_list_element *command;
14220
14221 command = add_cmd (name, class_breakpoint, docstring,
14222 &catch_cmdlist);
14223 command->func = func;
14224 command->set_context (user_data_catch);
14225 set_cmd_completer (command, completer);
14226
14227 command = add_cmd (name, class_breakpoint, docstring,
14228 &tcatch_cmdlist);
14229 command->func = func;
14230 command->set_context (user_data_tcatch);
14231 set_cmd_completer (command, completer);
14232 }
14233
14234 /* Zero if any of the breakpoint's locations could be a location where
14235 functions have been inlined, nonzero otherwise. */
14236
14237 static int
14238 is_non_inline_function (struct breakpoint *b)
14239 {
14240 /* The shared library event breakpoint is set on the address of a
14241 non-inline function. */
14242 if (b->type == bp_shlib_event)
14243 return 1;
14244
14245 return 0;
14246 }
14247
14248 /* Nonzero if the specified PC cannot be a location where functions
14249 have been inlined. */
14250
14251 int
14252 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
14253 const target_waitstatus &ws)
14254 {
14255 for (breakpoint *b : all_breakpoints ())
14256 {
14257 if (!is_non_inline_function (b))
14258 continue;
14259
14260 for (bp_location *bl : b->locations ())
14261 {
14262 if (!bl->shlib_disabled
14263 && bpstat_check_location (bl, aspace, pc, ws))
14264 return 1;
14265 }
14266 }
14267
14268 return 0;
14269 }
14270
14271 /* Remove any references to OBJFILE which is going to be freed. */
14272
14273 void
14274 breakpoint_free_objfile (struct objfile *objfile)
14275 {
14276 for (bp_location *loc : all_bp_locations ())
14277 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
14278 loc->symtab = NULL;
14279 }
14280
14281 /* Chain containing all defined "enable breakpoint" subcommands. */
14282
14283 static struct cmd_list_element *enablebreaklist = NULL;
14284
14285 /* See breakpoint.h. */
14286
14287 cmd_list_element *commands_cmd_element = nullptr;
14288
14289 void _initialize_breakpoint ();
14290 void
14291 _initialize_breakpoint ()
14292 {
14293 struct cmd_list_element *c;
14294
14295 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14296 "breakpoint");
14297 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14298 "breakpoint");
14299 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14300 "breakpoint");
14301
14302 breakpoint_chain = 0;
14303 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14304 before a breakpoint is set. */
14305 breakpoint_count = 0;
14306
14307 tracepoint_count = 0;
14308
14309 add_com ("ignore", class_breakpoint, ignore_command, _("\
14310 Set ignore-count of breakpoint number N to COUNT.\n\
14311 Usage is `ignore N COUNT'."));
14312
14313 commands_cmd_element = add_com ("commands", class_breakpoint,
14314 commands_command, _("\
14315 Set commands to be executed when the given breakpoints are hit.\n\
14316 Give a space-separated breakpoint list as argument after \"commands\".\n\
14317 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14318 (e.g. `5-7').\n\
14319 With no argument, the targeted breakpoint is the last one set.\n\
14320 The commands themselves follow starting on the next line.\n\
14321 Type a line containing \"end\" to indicate the end of them.\n\
14322 Give \"silent\" as the first line to make the breakpoint silent;\n\
14323 then no output is printed when it is hit, except what the commands print."));
14324
14325 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14326 static std::string condition_command_help
14327 = gdb::option::build_help (_("\
14328 Specify breakpoint number N to break only if COND is true.\n\
14329 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
14330 is an expression to be evaluated whenever breakpoint N is reached.\n\
14331 \n\
14332 Options:\n\
14333 %OPTIONS%"), cc_opts);
14334
14335 c = add_com ("condition", class_breakpoint, condition_command,
14336 condition_command_help.c_str ());
14337 set_cmd_completer_handle_brkchars (c, condition_completer);
14338
14339 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
14340 Set a temporary breakpoint.\n\
14341 Like \"break\" except the breakpoint is only temporary,\n\
14342 so it will be deleted when hit. Equivalent to \"break\" followed\n\
14343 by using \"enable delete\" on the breakpoint number.\n\
14344 \n"
14345 BREAK_ARGS_HELP ("tbreak")));
14346 set_cmd_completer (c, location_completer);
14347
14348 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
14349 Set a hardware assisted breakpoint.\n\
14350 Like \"break\" except the breakpoint requires hardware support,\n\
14351 some target hardware may not have this support.\n\
14352 \n"
14353 BREAK_ARGS_HELP ("hbreak")));
14354 set_cmd_completer (c, location_completer);
14355
14356 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
14357 Set a temporary hardware assisted breakpoint.\n\
14358 Like \"hbreak\" except the breakpoint is only temporary,\n\
14359 so it will be deleted when hit.\n\
14360 \n"
14361 BREAK_ARGS_HELP ("thbreak")));
14362 set_cmd_completer (c, location_completer);
14363
14364 cmd_list_element *enable_cmd
14365 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14366 Enable all or some breakpoints.\n\
14367 Usage: enable [BREAKPOINTNUM]...\n\
14368 Give breakpoint numbers (separated by spaces) as arguments.\n\
14369 With no subcommand, breakpoints are enabled until you command otherwise.\n\
14370 This is used to cancel the effect of the \"disable\" command.\n\
14371 With a subcommand you can enable temporarily."),
14372 &enablelist, 1, &cmdlist);
14373
14374 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
14375
14376 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
14377 Enable all or some breakpoints.\n\
14378 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
14379 Give breakpoint numbers (separated by spaces) as arguments.\n\
14380 This is used to cancel the effect of the \"disable\" command.\n\
14381 May be abbreviated to simply \"enable\"."),
14382 &enablebreaklist, 1, &enablelist);
14383
14384 add_cmd ("once", no_class, enable_once_command, _("\
14385 Enable some breakpoints for one hit.\n\
14386 Usage: enable breakpoints once BREAKPOINTNUM...\n\
14387 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14388 &enablebreaklist);
14389
14390 add_cmd ("delete", no_class, enable_delete_command, _("\
14391 Enable some breakpoints and delete when hit.\n\
14392 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
14393 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14394 &enablebreaklist);
14395
14396 add_cmd ("count", no_class, enable_count_command, _("\
14397 Enable some breakpoints for COUNT hits.\n\
14398 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
14399 If a breakpoint is hit while enabled in this fashion,\n\
14400 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14401 &enablebreaklist);
14402
14403 add_cmd ("delete", no_class, enable_delete_command, _("\
14404 Enable some breakpoints and delete when hit.\n\
14405 Usage: enable delete BREAKPOINTNUM...\n\
14406 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14407 &enablelist);
14408
14409 add_cmd ("once", no_class, enable_once_command, _("\
14410 Enable some breakpoints for one hit.\n\
14411 Usage: enable once BREAKPOINTNUM...\n\
14412 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14413 &enablelist);
14414
14415 add_cmd ("count", no_class, enable_count_command, _("\
14416 Enable some breakpoints for COUNT hits.\n\
14417 Usage: enable count COUNT BREAKPOINTNUM...\n\
14418 If a breakpoint is hit while enabled in this fashion,\n\
14419 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14420 &enablelist);
14421
14422 cmd_list_element *disable_cmd
14423 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14424 Disable all or some breakpoints.\n\
14425 Usage: disable [BREAKPOINTNUM]...\n\
14426 Arguments are breakpoint numbers with spaces in between.\n\
14427 To disable all breakpoints, give no argument.\n\
14428 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
14429 &disablelist, 1, &cmdlist);
14430 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14431 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
14432
14433 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
14434 Disable all or some breakpoints.\n\
14435 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
14436 Arguments are breakpoint numbers with spaces in between.\n\
14437 To disable all breakpoints, give no argument.\n\
14438 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
14439 This command may be abbreviated \"disable\"."),
14440 &disablelist);
14441
14442 cmd_list_element *delete_cmd
14443 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14444 Delete all or some breakpoints.\n\
14445 Usage: delete [BREAKPOINTNUM]...\n\
14446 Arguments are breakpoint numbers with spaces in between.\n\
14447 To delete all breakpoints, give no argument.\n\
14448 \n\
14449 Also a prefix command for deletion of other GDB objects."),
14450 &deletelist, 1, &cmdlist);
14451 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14452 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
14453
14454 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
14455 Delete all or some breakpoints or auto-display expressions.\n\
14456 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
14457 Arguments are breakpoint numbers with spaces in between.\n\
14458 To delete all breakpoints, give no argument.\n\
14459 This command may be abbreviated \"delete\"."),
14460 &deletelist);
14461
14462 cmd_list_element *clear_cmd
14463 = add_com ("clear", class_breakpoint, clear_command, _("\
14464 Clear breakpoint at specified location.\n\
14465 Argument may be a linespec, explicit, or address location as described below.\n\
14466 \n\
14467 With no argument, clears all breakpoints in the line that the selected frame\n\
14468 is executing in.\n"
14469 "\n" LOCATION_HELP_STRING "\n\n\
14470 See also the \"delete\" command which clears breakpoints by number."));
14471 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
14472
14473 cmd_list_element *break_cmd
14474 = add_com ("break", class_breakpoint, break_command, _("\
14475 Set breakpoint at specified location.\n"
14476 BREAK_ARGS_HELP ("break")));
14477 set_cmd_completer (break_cmd, location_completer);
14478
14479 add_com_alias ("b", break_cmd, class_run, 1);
14480 add_com_alias ("br", break_cmd, class_run, 1);
14481 add_com_alias ("bre", break_cmd, class_run, 1);
14482 add_com_alias ("brea", break_cmd, class_run, 1);
14483
14484 cmd_list_element *info_breakpoints_cmd
14485 = add_info ("breakpoints", info_breakpoints_command, _("\
14486 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
14487 The \"Type\" column indicates one of:\n\
14488 \tbreakpoint - normal breakpoint\n\
14489 \twatchpoint - watchpoint\n\
14490 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14491 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14492 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14493 address and file/line number respectively.\n\
14494 \n\
14495 Convenience variable \"$_\" and default examine address for \"x\"\n\
14496 are set to the address of the last breakpoint listed unless the command\n\
14497 is prefixed with \"server \".\n\n\
14498 Convenience variable \"$bpnum\" contains the number of the last\n\
14499 breakpoint set."));
14500
14501 add_info_alias ("b", info_breakpoints_cmd, 1);
14502
14503 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14504 Status of all breakpoints, or breakpoint number NUMBER.\n\
14505 The \"Type\" column indicates one of:\n\
14506 \tbreakpoint - normal breakpoint\n\
14507 \twatchpoint - watchpoint\n\
14508 \tlongjmp - internal breakpoint used to step through longjmp()\n\
14509 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14510 \tuntil - internal breakpoint used by the \"until\" command\n\
14511 \tfinish - internal breakpoint used by the \"finish\" command\n\
14512 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14513 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14514 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14515 address and file/line number respectively.\n\
14516 \n\
14517 Convenience variable \"$_\" and default examine address for \"x\"\n\
14518 are set to the address of the last breakpoint listed unless the command\n\
14519 is prefixed with \"server \".\n\n\
14520 Convenience variable \"$bpnum\" contains the number of the last\n\
14521 breakpoint set."),
14522 &maintenanceinfolist);
14523
14524 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
14525 Set catchpoints to catch events."),
14526 &catch_cmdlist,
14527 0/*allow-unknown*/, &cmdlist);
14528
14529 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
14530 Set temporary catchpoints to catch events."),
14531 &tcatch_cmdlist,
14532 0/*allow-unknown*/, &cmdlist);
14533
14534 const auto opts = make_watch_options_def_group (nullptr);
14535
14536 static const std::string watch_help = gdb::option::build_help (_("\
14537 Set a watchpoint for EXPRESSION.\n\
14538 Usage: watch [-location] EXPRESSION\n\
14539 \n\
14540 Options:\n\
14541 %OPTIONS%\n\
14542 \n\
14543 A watchpoint stops execution of your program whenever the value of\n\
14544 an expression changes."), opts);
14545 c = add_com ("watch", class_breakpoint, watch_command,
14546 watch_help.c_str ());
14547 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14548
14549 static const std::string rwatch_help = gdb::option::build_help (_("\
14550 Set a read watchpoint for EXPRESSION.\n\
14551 Usage: rwatch [-location] EXPRESSION\n\
14552 \n\
14553 Options:\n\
14554 %OPTIONS%\n\
14555 \n\
14556 A read watchpoint stops execution of your program whenever the value of\n\
14557 an expression is read."), opts);
14558 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14559 rwatch_help.c_str ());
14560 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14561
14562 static const std::string awatch_help = gdb::option::build_help (_("\
14563 Set an access watchpoint for EXPRESSION.\n\
14564 Usage: awatch [-location] EXPRESSION\n\
14565 \n\
14566 Options:\n\
14567 %OPTIONS%\n\
14568 \n\
14569 An access watchpoint stops execution of your program whenever the value\n\
14570 of an expression is either read or written."), opts);
14571 c = add_com ("awatch", class_breakpoint, awatch_command,
14572 awatch_help.c_str ());
14573 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14574
14575 add_info ("watchpoints", info_watchpoints_command, _("\
14576 Status of specified watchpoints (all watchpoints if no argument)."));
14577
14578 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14579 respond to changes - contrary to the description. */
14580 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14581 &can_use_hw_watchpoints, _("\
14582 Set debugger's willingness to use watchpoint hardware."), _("\
14583 Show debugger's willingness to use watchpoint hardware."), _("\
14584 If zero, gdb will not use hardware for new watchpoints, even if\n\
14585 such is available. (However, any hardware watchpoints that were\n\
14586 created before setting this to nonzero, will continue to use watchpoint\n\
14587 hardware.)"),
14588 NULL,
14589 show_can_use_hw_watchpoints,
14590 &setlist, &showlist);
14591
14592 can_use_hw_watchpoints = 1;
14593
14594 /* Tracepoint manipulation commands. */
14595
14596 cmd_list_element *trace_cmd
14597 = add_com ("trace", class_breakpoint, trace_command, _("\
14598 Set a tracepoint at specified location.\n\
14599 \n"
14600 BREAK_ARGS_HELP ("trace") "\n\
14601 Do \"help tracepoints\" for info on other tracepoint commands."));
14602 set_cmd_completer (trace_cmd, location_completer);
14603
14604 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14605 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14606 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14607 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
14608
14609 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
14610 Set a fast tracepoint at specified location.\n\
14611 \n"
14612 BREAK_ARGS_HELP ("ftrace") "\n\
14613 Do \"help tracepoints\" for info on other tracepoint commands."));
14614 set_cmd_completer (c, location_completer);
14615
14616 c = add_com ("strace", class_breakpoint, strace_command, _("\
14617 Set a static tracepoint at location or marker.\n\
14618 \n\
14619 strace [LOCATION] [if CONDITION]\n\
14620 LOCATION may be a linespec, explicit, or address location (described below) \n\
14621 or -m MARKER_ID.\n\n\
14622 If a marker id is specified, probe the marker with that name. With\n\
14623 no LOCATION, uses current execution address of the selected stack frame.\n\
14624 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14625 This collects arbitrary user data passed in the probe point call to the\n\
14626 tracing library. You can inspect it when analyzing the trace buffer,\n\
14627 by printing the $_sdata variable like any other convenience variable.\n\
14628 \n\
14629 CONDITION is a boolean expression.\n\
14630 \n" LOCATION_HELP_STRING "\n\n\
14631 Multiple tracepoints at one place are permitted, and useful if their\n\
14632 conditions are different.\n\
14633 \n\
14634 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14635 Do \"help tracepoints\" for info on other tracepoint commands."));
14636 set_cmd_completer (c, location_completer);
14637
14638 cmd_list_element *info_tracepoints_cmd
14639 = add_info ("tracepoints", info_tracepoints_command, _("\
14640 Status of specified tracepoints (all tracepoints if no argument).\n\
14641 Convenience variable \"$tpnum\" contains the number of the\n\
14642 last tracepoint set."));
14643
14644 add_info_alias ("tp", info_tracepoints_cmd, 1);
14645
14646 cmd_list_element *delete_tracepoints_cmd
14647 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
14648 Delete specified tracepoints.\n\
14649 Arguments are tracepoint numbers, separated by spaces.\n\
14650 No argument means delete all tracepoints."),
14651 &deletelist);
14652 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
14653
14654 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14655 Disable specified tracepoints.\n\
14656 Arguments are tracepoint numbers, separated by spaces.\n\
14657 No argument means disable all tracepoints."),
14658 &disablelist);
14659 deprecate_cmd (c, "disable");
14660
14661 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14662 Enable specified tracepoints.\n\
14663 Arguments are tracepoint numbers, separated by spaces.\n\
14664 No argument means enable all tracepoints."),
14665 &enablelist);
14666 deprecate_cmd (c, "enable");
14667
14668 add_com ("passcount", class_trace, trace_pass_command, _("\
14669 Set the passcount for a tracepoint.\n\
14670 The trace will end when the tracepoint has been passed 'count' times.\n\
14671 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14672 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14673
14674 add_basic_prefix_cmd ("save", class_breakpoint,
14675 _("Save breakpoint definitions as a script."),
14676 &save_cmdlist,
14677 0/*allow-unknown*/, &cmdlist);
14678
14679 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14680 Save current breakpoint definitions as a script.\n\
14681 This includes all types of breakpoints (breakpoints, watchpoints,\n\
14682 catchpoints, tracepoints). Use the 'source' command in another debug\n\
14683 session to restore them."),
14684 &save_cmdlist);
14685 set_cmd_completer (c, filename_completer);
14686
14687 cmd_list_element *save_tracepoints_cmd
14688 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
14689 Save current tracepoint definitions as a script.\n\
14690 Use the 'source' command in another debug session to restore them."),
14691 &save_cmdlist);
14692 set_cmd_completer (save_tracepoints_cmd, filename_completer);
14693
14694 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
14695 deprecate_cmd (c, "save tracepoints");
14696
14697 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14698 _("\
14699 Breakpoint specific settings.\n\
14700 Configure various breakpoint-specific variables such as\n\
14701 pending breakpoint behavior."),
14702 _("\
14703 Breakpoint specific settings.\n\
14704 Configure various breakpoint-specific variables such as\n\
14705 pending breakpoint behavior."),
14706 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14707 &setlist, &showlist);
14708
14709 add_setshow_auto_boolean_cmd ("pending", no_class,
14710 &pending_break_support, _("\
14711 Set debugger's behavior regarding pending breakpoints."), _("\
14712 Show debugger's behavior regarding pending breakpoints."), _("\
14713 If on, an unrecognized breakpoint location will cause gdb to create a\n\
14714 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14715 an error. If auto, an unrecognized breakpoint location results in a\n\
14716 user-query to see if a pending breakpoint should be created."),
14717 NULL,
14718 show_pending_break_support,
14719 &breakpoint_set_cmdlist,
14720 &breakpoint_show_cmdlist);
14721
14722 pending_break_support = AUTO_BOOLEAN_AUTO;
14723
14724 add_setshow_boolean_cmd ("auto-hw", no_class,
14725 &automatic_hardware_breakpoints, _("\
14726 Set automatic usage of hardware breakpoints."), _("\
14727 Show automatic usage of hardware breakpoints."), _("\
14728 If set, the debugger will automatically use hardware breakpoints for\n\
14729 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14730 a warning will be emitted for such breakpoints."),
14731 NULL,
14732 show_automatic_hardware_breakpoints,
14733 &breakpoint_set_cmdlist,
14734 &breakpoint_show_cmdlist);
14735
14736 add_setshow_boolean_cmd ("always-inserted", class_support,
14737 &always_inserted_mode, _("\
14738 Set mode for inserting breakpoints."), _("\
14739 Show mode for inserting breakpoints."), _("\
14740 When this mode is on, breakpoints are inserted immediately as soon as\n\
14741 they're created, kept inserted even when execution stops, and removed\n\
14742 only when the user deletes them. When this mode is off (the default),\n\
14743 breakpoints are inserted only when execution continues, and removed\n\
14744 when execution stops."),
14745 NULL,
14746 &show_always_inserted_mode,
14747 &breakpoint_set_cmdlist,
14748 &breakpoint_show_cmdlist);
14749
14750 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14751 condition_evaluation_enums,
14752 &condition_evaluation_mode_1, _("\
14753 Set mode of breakpoint condition evaluation."), _("\
14754 Show mode of breakpoint condition evaluation."), _("\
14755 When this is set to \"host\", breakpoint conditions will be\n\
14756 evaluated on the host's side by GDB. When it is set to \"target\",\n\
14757 breakpoint conditions will be downloaded to the target (if the target\n\
14758 supports such feature) and conditions will be evaluated on the target's side.\n\
14759 If this is set to \"auto\" (default), this will be automatically set to\n\
14760 \"target\" if it supports condition evaluation, otherwise it will\n\
14761 be set to \"host\"."),
14762 &set_condition_evaluation_mode,
14763 &show_condition_evaluation_mode,
14764 &breakpoint_set_cmdlist,
14765 &breakpoint_show_cmdlist);
14766
14767 add_com ("break-range", class_breakpoint, break_range_command, _("\
14768 Set a breakpoint for an address range.\n\
14769 break-range START-LOCATION, END-LOCATION\n\
14770 where START-LOCATION and END-LOCATION can be one of the following:\n\
14771 LINENUM, for that line in the current file,\n\
14772 FILE:LINENUM, for that line in that file,\n\
14773 +OFFSET, for that number of lines after the current line\n\
14774 or the start of the range\n\
14775 FUNCTION, for the first line in that function,\n\
14776 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14777 *ADDRESS, for the instruction at that address.\n\
14778 \n\
14779 The breakpoint will stop execution of the inferior whenever it executes\n\
14780 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14781 range (including START-LOCATION and END-LOCATION)."));
14782
14783 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
14784 Set a dynamic printf at specified location.\n\
14785 dprintf location,format string,arg1,arg2,...\n\
14786 location may be a linespec, explicit, or address location.\n"
14787 "\n" LOCATION_HELP_STRING));
14788 set_cmd_completer (c, location_completer);
14789
14790 add_setshow_enum_cmd ("dprintf-style", class_support,
14791 dprintf_style_enums, &dprintf_style, _("\
14792 Set the style of usage for dynamic printf."), _("\
14793 Show the style of usage for dynamic printf."), _("\
14794 This setting chooses how GDB will do a dynamic printf.\n\
14795 If the value is \"gdb\", then the printing is done by GDB to its own\n\
14796 console, as with the \"printf\" command.\n\
14797 If the value is \"call\", the print is done by calling a function in your\n\
14798 program; by default printf(), but you can choose a different function or\n\
14799 output stream by setting dprintf-function and dprintf-channel."),
14800 update_dprintf_commands, NULL,
14801 &setlist, &showlist);
14802
14803 add_setshow_string_cmd ("dprintf-function", class_support,
14804 &dprintf_function, _("\
14805 Set the function to use for dynamic printf."), _("\
14806 Show the function to use for dynamic printf."), NULL,
14807 update_dprintf_commands, NULL,
14808 &setlist, &showlist);
14809
14810 add_setshow_string_cmd ("dprintf-channel", class_support,
14811 &dprintf_channel, _("\
14812 Set the channel to use for dynamic printf."), _("\
14813 Show the channel to use for dynamic printf."), NULL,
14814 update_dprintf_commands, NULL,
14815 &setlist, &showlist);
14816
14817 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14818 &disconnected_dprintf, _("\
14819 Set whether dprintf continues after GDB disconnects."), _("\
14820 Show whether dprintf continues after GDB disconnects."), _("\
14821 Use this to let dprintf commands continue to hit and produce output\n\
14822 even if GDB disconnects or detaches from the target."),
14823 NULL,
14824 NULL,
14825 &setlist, &showlist);
14826
14827 add_com ("agent-printf", class_vars, agent_printf_command, _("\
14828 Target agent only formatted printing, like the C \"printf\" function.\n\
14829 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14830 This supports most C printf format specifications, like %s, %d, etc.\n\
14831 This is useful for formatted output in user-defined commands."));
14832
14833 automatic_hardware_breakpoints = true;
14834
14835 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14836 "breakpoint");
14837 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
14838 "breakpoint");
14839 }