Constify breakpoint::print_one
[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 (struct breakpoint *);
114
115 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
116 enum bptype);
117 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
118 const struct symtab_and_line *);
119
120 /* This function is used in gdbtk sources and thus can not be made
121 static. */
122 static struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
123 struct symtab_and_line,
124 enum bptype);
125
126 static struct breakpoint *
127 momentary_breakpoint_from_master (struct breakpoint *orig,
128 enum bptype type,
129 int loc_enabled);
130
131 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
132
133 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
134 CORE_ADDR bpaddr,
135 enum bptype bptype);
136
137 static void describe_other_breakpoints (struct gdbarch *,
138 struct program_space *, CORE_ADDR,
139 struct obj_section *, int);
140
141 static int watchpoint_locations_match (struct bp_location *loc1,
142 struct bp_location *loc2);
143
144 static int breakpoint_locations_match (struct bp_location *loc1,
145 struct bp_location *loc2,
146 bool sw_hw_bps_match = false);
147
148 static int breakpoint_location_address_match (struct bp_location *bl,
149 const struct address_space *aspace,
150 CORE_ADDR addr);
151
152 static int breakpoint_location_address_range_overlap (struct bp_location *,
153 const address_space *,
154 CORE_ADDR, int);
155
156 static int remove_breakpoint (struct bp_location *);
157 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
158
159 static enum print_stop_action print_bp_stop_message (bpstat *bs);
160
161 static int hw_breakpoint_used_count (void);
162
163 static int hw_watchpoint_use_count (struct breakpoint *);
164
165 static int hw_watchpoint_used_count_others (struct breakpoint *except,
166 enum bptype type,
167 int *other_type_used);
168
169 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
170 int count);
171
172 static void decref_bp_location (struct bp_location **loc);
173
174 static std::vector<symtab_and_line> bkpt_probe_decode_location
175 (struct breakpoint *b,
176 struct event_location *location,
177 struct program_space *search_pspace);
178
179 /* update_global_location_list's modes of operation wrt to whether to
180 insert locations now. */
181 enum ugll_insert_mode
182 {
183 /* Don't insert any breakpoint locations into the inferior, only
184 remove already-inserted locations that no longer should be
185 inserted. Functions that delete a breakpoint or breakpoints
186 should specify this mode, so that deleting a breakpoint doesn't
187 have the side effect of inserting the locations of other
188 breakpoints that are marked not-inserted, but should_be_inserted
189 returns true on them.
190
191 This behavior is useful is situations close to tear-down -- e.g.,
192 after an exec, while the target still has execution, but
193 breakpoint shadows of the previous executable image should *NOT*
194 be restored to the new image; or before detaching, where the
195 target still has execution and wants to delete breakpoints from
196 GDB's lists, and all breakpoints had already been removed from
197 the inferior. */
198 UGLL_DONT_INSERT,
199
200 /* May insert breakpoints iff breakpoints_should_be_inserted_now
201 claims breakpoints should be inserted now. */
202 UGLL_MAY_INSERT,
203
204 /* Insert locations now, irrespective of
205 breakpoints_should_be_inserted_now. E.g., say all threads are
206 stopped right now, and the user did "continue". We need to
207 insert breakpoints _before_ resuming the target, but
208 UGLL_MAY_INSERT wouldn't insert them, because
209 breakpoints_should_be_inserted_now returns false at that point,
210 as no thread is running yet. */
211 UGLL_INSERT
212 };
213
214 static void update_global_location_list (enum ugll_insert_mode);
215
216 static void update_global_location_list_nothrow (enum ugll_insert_mode);
217
218 static void insert_breakpoint_locations (void);
219
220 static void trace_pass_command (const char *, int);
221
222 static void set_tracepoint_count (int num);
223
224 static bool is_masked_watchpoint (const struct breakpoint *b);
225
226 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
227 otherwise. */
228
229 static int strace_marker_p (struct breakpoint *b);
230
231 static void bkpt_probe_create_sals_from_location
232 (struct event_location *location,
233 struct linespec_result *canonical);
234 static void tracepoint_probe_create_sals_from_location
235 (struct event_location *location,
236 struct linespec_result *canonical);
237
238 const struct breakpoint_ops base_breakpoint_ops =
239 {
240 create_sals_from_location_default,
241 create_breakpoints_sal,
242 };
243
244 /* Breakpoints set on probes. */
245 static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
246 {
247 bkpt_probe_create_sals_from_location,
248 create_breakpoints_sal,
249 };
250
251 /* Tracepoints set on probes. */
252 static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
253 {
254 tracepoint_probe_create_sals_from_location,
255 create_breakpoints_sal,
256 };
257
258 /* The structure to be used in regular breakpoints. */
259 struct ordinary_breakpoint : public base_breakpoint
260 {
261 int resources_needed (const struct bp_location *) override;
262 enum print_stop_action print_it (const bpstat *bs) const override;
263 void print_mention () override;
264 void print_recreate (struct ui_file *fp) override;
265 };
266
267 /* Internal breakpoints. */
268 struct internal_breakpoint : public base_breakpoint
269 {
270 void re_set () override;
271 void check_status (struct bpstat *bs) override;
272 enum print_stop_action print_it (const bpstat *bs) const override;
273 void print_mention () override;
274 };
275
276 /* Momentary breakpoints. */
277 struct momentary_breakpoint : public base_breakpoint
278 {
279 void re_set () override;
280 void check_status (struct bpstat *bs) override;
281 enum print_stop_action print_it (const bpstat *bs) const override;
282 void print_mention () override;
283 };
284
285 /* DPrintf breakpoints. */
286 struct dprintf_breakpoint : public ordinary_breakpoint
287 {
288 void re_set () override;
289 int breakpoint_hit (const struct bp_location *bl,
290 const address_space *aspace,
291 CORE_ADDR bp_addr,
292 const target_waitstatus &ws) override;
293 void print_recreate (struct ui_file *fp) override;
294 void after_condition_true (struct bpstat *bs) override;
295 };
296
297 /* Ranged breakpoints. */
298 struct ranged_breakpoint : public ordinary_breakpoint
299 {
300 int breakpoint_hit (const struct bp_location *bl,
301 const address_space *aspace,
302 CORE_ADDR bp_addr,
303 const target_waitstatus &ws) override;
304 int resources_needed (const struct bp_location *) override;
305 enum print_stop_action print_it (const bpstat *bs) const override;
306 bool print_one (bp_location **) const override;
307 void print_one_detail (struct ui_out *) const override;
308 void print_mention () override;
309 void print_recreate (struct ui_file *fp) override;
310 };
311
312 /* Static tracepoints with marker (`-m'). */
313 struct static_marker_tracepoint : public tracepoint
314 {
315 std::vector<symtab_and_line> decode_location
316 (struct event_location *location,
317 struct program_space *search_pspace) override;
318 };
319
320 /* The style in which to perform a dynamic printf. This is a user
321 option because different output options have different tradeoffs;
322 if GDB does the printing, there is better error handling if there
323 is a problem with any of the arguments, but using an inferior
324 function lets you have special-purpose printers and sending of
325 output to the same place as compiled-in print functions. */
326
327 static const char dprintf_style_gdb[] = "gdb";
328 static const char dprintf_style_call[] = "call";
329 static const char dprintf_style_agent[] = "agent";
330 static const char *const dprintf_style_enums[] = {
331 dprintf_style_gdb,
332 dprintf_style_call,
333 dprintf_style_agent,
334 NULL
335 };
336 static const char *dprintf_style = dprintf_style_gdb;
337
338 /* The function to use for dynamic printf if the preferred style is to
339 call into the inferior. The value is simply a string that is
340 copied into the command, so it can be anything that GDB can
341 evaluate to a callable address, not necessarily a function name. */
342
343 static std::string dprintf_function = "printf";
344
345 /* The channel to use for dynamic printf if the preferred style is to
346 call into the inferior; if a nonempty string, it will be passed to
347 the call as the first argument, with the format string as the
348 second. As with the dprintf function, this can be anything that
349 GDB knows how to evaluate, so in addition to common choices like
350 "stderr", this could be an app-specific expression like
351 "mystreams[curlogger]". */
352
353 static std::string dprintf_channel;
354
355 /* True if dprintf commands should continue to operate even if GDB
356 has disconnected. */
357 static bool disconnected_dprintf = true;
358
359 struct command_line *
360 breakpoint_commands (struct breakpoint *b)
361 {
362 return b->commands ? b->commands.get () : NULL;
363 }
364
365 /* Flag indicating that a command has proceeded the inferior past the
366 current breakpoint. */
367
368 static bool breakpoint_proceeded;
369
370 const char *
371 bpdisp_text (enum bpdisp disp)
372 {
373 /* NOTE: the following values are a part of MI protocol and
374 represent values of 'disp' field returned when inferior stops at
375 a breakpoint. */
376 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
377
378 return bpdisps[(int) disp];
379 }
380
381 /* Prototypes for exported functions. */
382 /* If FALSE, gdb will not use hardware support for watchpoints, even
383 if such is available. */
384 static int can_use_hw_watchpoints;
385
386 static void
387 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
388 struct cmd_list_element *c,
389 const char *value)
390 {
391 gdb_printf (file,
392 _("Debugger's willingness to use "
393 "watchpoint hardware is %s.\n"),
394 value);
395 }
396
397 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
398 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
399 for unrecognized breakpoint locations.
400 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
401 static enum auto_boolean pending_break_support;
402 static void
403 show_pending_break_support (struct ui_file *file, int from_tty,
404 struct cmd_list_element *c,
405 const char *value)
406 {
407 gdb_printf (file,
408 _("Debugger's behavior regarding "
409 "pending breakpoints is %s.\n"),
410 value);
411 }
412
413 /* If true, gdb will automatically use hardware breakpoints for breakpoints
414 set with "break" but falling in read-only memory.
415 If false, gdb will warn about such breakpoints, but won't automatically
416 use hardware breakpoints. */
417 static bool automatic_hardware_breakpoints;
418 static void
419 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
420 struct cmd_list_element *c,
421 const char *value)
422 {
423 gdb_printf (file,
424 _("Automatic usage of hardware breakpoints is %s.\n"),
425 value);
426 }
427
428 /* If on, GDB keeps breakpoints inserted even if the inferior is
429 stopped, and immediately inserts any new breakpoints as soon as
430 they're created. If off (default), GDB keeps breakpoints off of
431 the target as long as possible. That is, it delays inserting
432 breakpoints until the next resume, and removes them again when the
433 target fully stops. This is a bit safer in case GDB crashes while
434 processing user input. */
435 static bool always_inserted_mode = false;
436
437 static void
438 show_always_inserted_mode (struct ui_file *file, int from_tty,
439 struct cmd_list_element *c, const char *value)
440 {
441 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
442 value);
443 }
444
445 /* See breakpoint.h. */
446
447 int
448 breakpoints_should_be_inserted_now (void)
449 {
450 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
451 {
452 /* If breakpoints are global, they should be inserted even if no
453 thread under gdb's control is running, or even if there are
454 no threads under GDB's control yet. */
455 return 1;
456 }
457 else
458 {
459 if (always_inserted_mode)
460 {
461 /* The user wants breakpoints inserted even if all threads
462 are stopped. */
463 return 1;
464 }
465
466 for (inferior *inf : all_inferiors ())
467 if (inf->has_execution ()
468 && threads_are_executing (inf->process_target ()))
469 return 1;
470
471 /* Don't remove breakpoints yet if, even though all threads are
472 stopped, we still have events to process. */
473 for (thread_info *tp : all_non_exited_threads ())
474 if (tp->resumed () && tp->has_pending_waitstatus ())
475 return 1;
476 }
477 return 0;
478 }
479
480 static const char condition_evaluation_both[] = "host or target";
481
482 /* Modes for breakpoint condition evaluation. */
483 static const char condition_evaluation_auto[] = "auto";
484 static const char condition_evaluation_host[] = "host";
485 static const char condition_evaluation_target[] = "target";
486 static const char *const condition_evaluation_enums[] = {
487 condition_evaluation_auto,
488 condition_evaluation_host,
489 condition_evaluation_target,
490 NULL
491 };
492
493 /* Global that holds the current mode for breakpoint condition evaluation. */
494 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
495
496 /* Global that we use to display information to the user (gets its value from
497 condition_evaluation_mode_1. */
498 static const char *condition_evaluation_mode = condition_evaluation_auto;
499
500 /* Translate a condition evaluation mode MODE into either "host"
501 or "target". This is used mostly to translate from "auto" to the
502 real setting that is being used. It returns the translated
503 evaluation mode. */
504
505 static const char *
506 translate_condition_evaluation_mode (const char *mode)
507 {
508 if (mode == condition_evaluation_auto)
509 {
510 if (target_supports_evaluation_of_breakpoint_conditions ())
511 return condition_evaluation_target;
512 else
513 return condition_evaluation_host;
514 }
515 else
516 return mode;
517 }
518
519 /* Discovers what condition_evaluation_auto translates to. */
520
521 static const char *
522 breakpoint_condition_evaluation_mode (void)
523 {
524 return translate_condition_evaluation_mode (condition_evaluation_mode);
525 }
526
527 /* Return true if GDB should evaluate breakpoint conditions or false
528 otherwise. */
529
530 static int
531 gdb_evaluates_breakpoint_condition_p (void)
532 {
533 const char *mode = breakpoint_condition_evaluation_mode ();
534
535 return (mode == condition_evaluation_host);
536 }
537
538 /* Are we executing breakpoint commands? */
539 static int executing_breakpoint_commands;
540
541 /* Are overlay event breakpoints enabled? */
542 static int overlay_events_enabled;
543
544 /* See description in breakpoint.h. */
545 bool target_exact_watchpoints = false;
546
547 /* Walk the following statement or block through all breakpoints.
548 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
549 current breakpoint. */
550
551 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
552 for (B = breakpoint_chain; \
553 B ? (TMP=B->next, 1): 0; \
554 B = TMP)
555
556 /* Chains of all breakpoints defined. */
557
558 static struct breakpoint *breakpoint_chain;
559
560 /* See breakpoint.h. */
561
562 breakpoint_range
563 all_breakpoints ()
564 {
565 return breakpoint_range (breakpoint_chain);
566 }
567
568 /* See breakpoint.h. */
569
570 breakpoint_safe_range
571 all_breakpoints_safe ()
572 {
573 return breakpoint_safe_range (all_breakpoints ());
574 }
575
576 /* See breakpoint.h. */
577
578 tracepoint_range
579 all_tracepoints ()
580 {
581 return tracepoint_range (breakpoint_chain);
582 }
583
584 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
585
586 static std::vector<bp_location *> bp_locations;
587
588 /* See breakpoint.h. */
589
590 const std::vector<bp_location *> &
591 all_bp_locations ()
592 {
593 return bp_locations;
594 }
595
596 /* Range to iterate over breakpoint locations at a given address. */
597
598 struct bp_locations_at_addr_range
599 {
600 using iterator = std::vector<bp_location *>::iterator;
601
602 bp_locations_at_addr_range (CORE_ADDR addr)
603 {
604 struct compare
605 {
606 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
607 { return loc->address < addr_; }
608
609 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
610 { return addr_ < loc->address; }
611 };
612
613 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
614 addr, compare ());
615
616 m_begin = it_pair.first;
617 m_end = it_pair.second;
618 }
619
620 iterator begin () const
621 { return m_begin; }
622
623 iterator end () const
624 { return m_end; }
625
626 private:
627 iterator m_begin;
628 iterator m_end;
629 };
630
631 /* Return a range to iterate over all breakpoint locations exactly at address
632 ADDR.
633
634 If it's needed to iterate multiple times on the same range, it's possible
635 to save the range in a local variable and use it multiple times:
636
637 auto range = all_bp_locations_at_addr (addr);
638
639 for (bp_location *loc : range)
640 // use loc
641
642 for (bp_location *loc : range)
643 // use loc
644
645 This saves a bit of time, as it avoids re-doing the binary searches to find
646 the range's boundaries. Just remember not to change the bp_locations vector
647 in the mean time, as it could make the range's iterators stale. */
648
649 static bp_locations_at_addr_range
650 all_bp_locations_at_addr (CORE_ADDR addr)
651 {
652 return bp_locations_at_addr_range (addr);
653 }
654
655 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
656 ADDRESS for the current elements of BP_LOCATIONS which get a valid
657 result from bp_location_has_shadow. You can use it for roughly
658 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
659 an address you need to read. */
660
661 static CORE_ADDR bp_locations_placed_address_before_address_max;
662
663 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
664 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
665 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
666 You can use it for roughly limiting the subrange of BP_LOCATIONS to
667 scan for shadow bytes for an address you need to read. */
668
669 static CORE_ADDR bp_locations_shadow_len_after_address_max;
670
671 /* The locations that no longer correspond to any breakpoint, unlinked
672 from the bp_locations array, but for which a hit may still be
673 reported by a target. */
674 static std::vector<bp_location *> moribund_locations;
675
676 /* Number of last breakpoint made. */
677
678 static int breakpoint_count;
679
680 /* The value of `breakpoint_count' before the last command that
681 created breakpoints. If the last (break-like) command created more
682 than one breakpoint, then the difference between BREAKPOINT_COUNT
683 and PREV_BREAKPOINT_COUNT is more than one. */
684 static int prev_breakpoint_count;
685
686 /* Number of last tracepoint made. */
687
688 static int tracepoint_count;
689
690 static struct cmd_list_element *breakpoint_set_cmdlist;
691 static struct cmd_list_element *breakpoint_show_cmdlist;
692 struct cmd_list_element *save_cmdlist;
693
694 /* Return whether a breakpoint is an active enabled breakpoint. */
695 static int
696 breakpoint_enabled (struct breakpoint *b)
697 {
698 return (b->enable_state == bp_enabled);
699 }
700
701 /* Set breakpoint count to NUM. */
702
703 static void
704 set_breakpoint_count (int num)
705 {
706 prev_breakpoint_count = breakpoint_count;
707 breakpoint_count = num;
708 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
709 }
710
711 /* Used by `start_rbreak_breakpoints' below, to record the current
712 breakpoint count before "rbreak" creates any breakpoint. */
713 static int rbreak_start_breakpoint_count;
714
715 /* Called at the start an "rbreak" command to record the first
716 breakpoint made. */
717
718 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
719 {
720 rbreak_start_breakpoint_count = breakpoint_count;
721 }
722
723 /* Called at the end of an "rbreak" command to record the last
724 breakpoint made. */
725
726 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
727 {
728 prev_breakpoint_count = rbreak_start_breakpoint_count;
729 }
730
731 /* Used in run_command to zero the hit count when a new run starts. */
732
733 void
734 clear_breakpoint_hit_counts (void)
735 {
736 for (breakpoint *b : all_breakpoints ())
737 b->hit_count = 0;
738 }
739
740 \f
741 /* Return the breakpoint with the specified number, or NULL
742 if the number does not refer to an existing breakpoint. */
743
744 struct breakpoint *
745 get_breakpoint (int num)
746 {
747 for (breakpoint *b : all_breakpoints ())
748 if (b->number == num)
749 return b;
750
751 return nullptr;
752 }
753
754 \f
755
756 /* Mark locations as "conditions have changed" in case the target supports
757 evaluating conditions on its side. */
758
759 static void
760 mark_breakpoint_modified (struct breakpoint *b)
761 {
762 /* This is only meaningful if the target is
763 evaluating conditions and if the user has
764 opted for condition evaluation on the target's
765 side. */
766 if (gdb_evaluates_breakpoint_condition_p ()
767 || !target_supports_evaluation_of_breakpoint_conditions ())
768 return;
769
770 if (!is_breakpoint (b))
771 return;
772
773 for (bp_location *loc : b->locations ())
774 loc->condition_changed = condition_modified;
775 }
776
777 /* Mark location as "conditions have changed" in case the target supports
778 evaluating conditions on its side. */
779
780 static void
781 mark_breakpoint_location_modified (struct bp_location *loc)
782 {
783 /* This is only meaningful if the target is
784 evaluating conditions and if the user has
785 opted for condition evaluation on the target's
786 side. */
787 if (gdb_evaluates_breakpoint_condition_p ()
788 || !target_supports_evaluation_of_breakpoint_conditions ())
789
790 return;
791
792 if (!is_breakpoint (loc->owner))
793 return;
794
795 loc->condition_changed = condition_modified;
796 }
797
798 /* Sets the condition-evaluation mode using the static global
799 condition_evaluation_mode. */
800
801 static void
802 set_condition_evaluation_mode (const char *args, int from_tty,
803 struct cmd_list_element *c)
804 {
805 const char *old_mode, *new_mode;
806
807 if ((condition_evaluation_mode_1 == condition_evaluation_target)
808 && !target_supports_evaluation_of_breakpoint_conditions ())
809 {
810 condition_evaluation_mode_1 = condition_evaluation_mode;
811 warning (_("Target does not support breakpoint condition evaluation.\n"
812 "Using host evaluation mode instead."));
813 return;
814 }
815
816 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
817 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
818
819 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
820 settings was "auto". */
821 condition_evaluation_mode = condition_evaluation_mode_1;
822
823 /* Only update the mode if the user picked a different one. */
824 if (new_mode != old_mode)
825 {
826 /* If the user switched to a different evaluation mode, we
827 need to synch the changes with the target as follows:
828
829 "host" -> "target": Send all (valid) conditions to the target.
830 "target" -> "host": Remove all the conditions from the target.
831 */
832
833 if (new_mode == condition_evaluation_target)
834 {
835 /* Mark everything modified and synch conditions with the
836 target. */
837 for (bp_location *loc : all_bp_locations ())
838 mark_breakpoint_location_modified (loc);
839 }
840 else
841 {
842 /* Manually mark non-duplicate locations to synch conditions
843 with the target. We do this to remove all the conditions the
844 target knows about. */
845 for (bp_location *loc : all_bp_locations ())
846 if (is_breakpoint (loc->owner) && loc->inserted)
847 loc->needs_update = 1;
848 }
849
850 /* Do the update. */
851 update_global_location_list (UGLL_MAY_INSERT);
852 }
853
854 return;
855 }
856
857 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
858 what "auto" is translating to. */
859
860 static void
861 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
862 struct cmd_list_element *c, const char *value)
863 {
864 if (condition_evaluation_mode == condition_evaluation_auto)
865 gdb_printf (file,
866 _("Breakpoint condition evaluation "
867 "mode is %s (currently %s).\n"),
868 value,
869 breakpoint_condition_evaluation_mode ());
870 else
871 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
872 value);
873 }
874
875 /* Parse COND_STRING in the context of LOC and set as the condition
876 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
877 the number of LOC within its owner. In case of parsing error, mark
878 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
879
880 static void
881 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
882 int bp_num, int loc_num)
883 {
884 bool has_junk = false;
885 try
886 {
887 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
888 block_for_pc (loc->address), 0);
889 if (*cond_string != 0)
890 has_junk = true;
891 else
892 {
893 loc->cond = std::move (new_exp);
894 if (loc->disabled_by_cond && loc->enabled)
895 gdb_printf (_("Breakpoint %d's condition is now valid at "
896 "location %d, enabling.\n"),
897 bp_num, loc_num);
898
899 loc->disabled_by_cond = false;
900 }
901 }
902 catch (const gdb_exception_error &e)
903 {
904 if (loc->enabled)
905 {
906 /* Warn if a user-enabled location is now becoming disabled-by-cond.
907 BP_NUM is 0 if the breakpoint is being defined for the first
908 time using the "break ... if ..." command, and non-zero if
909 already defined. */
910 if (bp_num != 0)
911 warning (_("failed to validate condition at location %d.%d, "
912 "disabling:\n %s"), bp_num, loc_num, e.what ());
913 else
914 warning (_("failed to validate condition at location %d, "
915 "disabling:\n %s"), loc_num, e.what ());
916 }
917
918 loc->disabled_by_cond = true;
919 }
920
921 if (has_junk)
922 error (_("Garbage '%s' follows condition"), cond_string);
923 }
924
925 void
926 set_breakpoint_condition (struct breakpoint *b, const char *exp,
927 int from_tty, bool force)
928 {
929 if (*exp == 0)
930 {
931 b->cond_string.reset ();
932
933 if (is_watchpoint (b))
934 static_cast<watchpoint *> (b)->cond_exp.reset ();
935 else
936 {
937 int loc_num = 1;
938 for (bp_location *loc : b->locations ())
939 {
940 loc->cond.reset ();
941 if (loc->disabled_by_cond && loc->enabled)
942 gdb_printf (_("Breakpoint %d's condition is now valid at "
943 "location %d, enabling.\n"),
944 b->number, loc_num);
945 loc->disabled_by_cond = false;
946 loc_num++;
947
948 /* No need to free the condition agent expression
949 bytecode (if we have one). We will handle this
950 when we go through update_global_location_list. */
951 }
952 }
953
954 if (from_tty)
955 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
956 }
957 else
958 {
959 if (is_watchpoint (b))
960 {
961 innermost_block_tracker tracker;
962 const char *arg = exp;
963 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
964 if (*arg != 0)
965 error (_("Junk at end of expression"));
966 watchpoint *w = static_cast<watchpoint *> (b);
967 w->cond_exp = std::move (new_exp);
968 w->cond_exp_valid_block = tracker.block ();
969 }
970 else
971 {
972 /* Parse and set condition expressions. We make two passes.
973 In the first, we parse the condition string to see if it
974 is valid in at least one location. If so, the condition
975 would be accepted. So we go ahead and set the locations'
976 conditions. In case no valid case is found, we throw
977 the error and the condition string will be rejected.
978 This two-pass approach is taken to avoid setting the
979 state of locations in case of a reject. */
980 for (bp_location *loc : b->locations ())
981 {
982 try
983 {
984 const char *arg = exp;
985 parse_exp_1 (&arg, loc->address,
986 block_for_pc (loc->address), 0);
987 if (*arg != 0)
988 error (_("Junk at end of expression"));
989 break;
990 }
991 catch (const gdb_exception_error &e)
992 {
993 /* Condition string is invalid. If this happens to
994 be the last loc, abandon (if not forced) or continue
995 (if forced). */
996 if (loc->next == nullptr && !force)
997 throw;
998 }
999 }
1000
1001 /* If we reach here, the condition is valid at some locations. */
1002 int loc_num = 1;
1003 for (bp_location *loc : b->locations ())
1004 {
1005 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
1006 loc_num++;
1007 }
1008 }
1009
1010 /* We know that the new condition parsed successfully. The
1011 condition string of the breakpoint can be safely updated. */
1012 b->cond_string = make_unique_xstrdup (exp);
1013 b->condition_not_parsed = 0;
1014 }
1015 mark_breakpoint_modified (b);
1016
1017 gdb::observers::breakpoint_modified.notify (b);
1018 }
1019
1020 /* See breakpoint.h. */
1021
1022 void
1023 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1024 bool force)
1025 {
1026 for (breakpoint *b : all_breakpoints ())
1027 if (b->number == bpnum)
1028 {
1029 /* Check if this breakpoint has a "stop" method implemented in an
1030 extension language. This method and conditions entered into GDB
1031 from the CLI are mutually exclusive. */
1032 const struct extension_language_defn *extlang
1033 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1034
1035 if (extlang != NULL)
1036 {
1037 error (_("Only one stop condition allowed. There is currently"
1038 " a %s stop condition defined for this breakpoint."),
1039 ext_lang_capitalized_name (extlang));
1040 }
1041 set_breakpoint_condition (b, exp, from_tty, force);
1042
1043 if (is_breakpoint (b))
1044 update_global_location_list (UGLL_MAY_INSERT);
1045
1046 return;
1047 }
1048
1049 error (_("No breakpoint number %d."), bpnum);
1050 }
1051
1052 /* The options for the "condition" command. */
1053
1054 struct condition_command_opts
1055 {
1056 /* For "-force". */
1057 bool force_condition = false;
1058 };
1059
1060 static const gdb::option::option_def condition_command_option_defs[] = {
1061
1062 gdb::option::flag_option_def<condition_command_opts> {
1063 "force",
1064 [] (condition_command_opts *opts) { return &opts->force_condition; },
1065 N_("Set the condition even if it is invalid for all current locations."),
1066 },
1067
1068 };
1069
1070 /* Create an option_def_group for the "condition" options, with
1071 CC_OPTS as context. */
1072
1073 static inline gdb::option::option_def_group
1074 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1075 {
1076 return {{condition_command_option_defs}, cc_opts};
1077 }
1078
1079 /* Completion for the "condition" command. */
1080
1081 static void
1082 condition_completer (struct cmd_list_element *cmd,
1083 completion_tracker &tracker,
1084 const char *text, const char * /*word*/)
1085 {
1086 bool has_no_arguments = (*text == '\0');
1087 condition_command_opts cc_opts;
1088 const auto group = make_condition_command_options_def_group (&cc_opts);
1089 if (gdb::option::complete_options
1090 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1091 return;
1092
1093 text = skip_spaces (text);
1094 const char *space = skip_to_space (text);
1095 if (*space == '\0')
1096 {
1097 int len;
1098
1099 if (text[0] == '$')
1100 {
1101 tracker.advance_custom_word_point_by (1);
1102 /* We don't support completion of history indices. */
1103 if (!isdigit (text[1]))
1104 complete_internalvar (tracker, &text[1]);
1105 return;
1106 }
1107
1108 /* Suggest the "-force" flag if no arguments are given. If
1109 arguments were passed, they either already include the flag,
1110 or we are beyond the point of suggesting it because it's
1111 positionally the first argument. */
1112 if (has_no_arguments)
1113 gdb::option::complete_on_all_options (tracker, group);
1114
1115 /* We're completing the breakpoint number. */
1116 len = strlen (text);
1117
1118 for (breakpoint *b : all_breakpoints ())
1119 {
1120 char number[50];
1121
1122 xsnprintf (number, sizeof (number), "%d", b->number);
1123
1124 if (strncmp (number, text, len) == 0)
1125 tracker.add_completion (make_unique_xstrdup (number));
1126 }
1127
1128 return;
1129 }
1130
1131 /* We're completing the expression part. Skip the breakpoint num. */
1132 const char *exp_start = skip_spaces (space);
1133 tracker.advance_custom_word_point_by (exp_start - text);
1134 text = exp_start;
1135 const char *word = advance_to_expression_complete_word_point (tracker, text);
1136 expression_completer (cmd, tracker, text, word);
1137 }
1138
1139 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1140
1141 static void
1142 condition_command (const char *arg, int from_tty)
1143 {
1144 const char *p;
1145 int bnum;
1146
1147 if (arg == 0)
1148 error_no_arg (_("breakpoint number"));
1149
1150 p = arg;
1151
1152 /* Check if the "-force" flag was passed. */
1153 condition_command_opts cc_opts;
1154 const auto group = make_condition_command_options_def_group (&cc_opts);
1155 gdb::option::process_options
1156 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1157
1158 bnum = get_number (&p);
1159 if (bnum == 0)
1160 error (_("Bad breakpoint argument: '%s'"), arg);
1161
1162 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1163 }
1164
1165 /* Check that COMMAND do not contain commands that are suitable
1166 only for tracepoints and not suitable for ordinary breakpoints.
1167 Throw if any such commands is found. */
1168
1169 static void
1170 check_no_tracepoint_commands (struct command_line *commands)
1171 {
1172 struct command_line *c;
1173
1174 for (c = commands; c; c = c->next)
1175 {
1176 if (c->control_type == while_stepping_control)
1177 error (_("The 'while-stepping' command can "
1178 "only be used for tracepoints"));
1179
1180 check_no_tracepoint_commands (c->body_list_0.get ());
1181 check_no_tracepoint_commands (c->body_list_1.get ());
1182
1183 /* Not that command parsing removes leading whitespace and comment
1184 lines and also empty lines. So, we only need to check for
1185 command directly. */
1186 if (strstr (c->line, "collect ") == c->line)
1187 error (_("The 'collect' command can only be used for tracepoints"));
1188
1189 if (strstr (c->line, "teval ") == c->line)
1190 error (_("The 'teval' command can only be used for tracepoints"));
1191 }
1192 }
1193
1194 struct longjmp_breakpoint : public momentary_breakpoint
1195 {
1196 ~longjmp_breakpoint () override;
1197 };
1198
1199 /* Encapsulate tests for different types of tracepoints. */
1200
1201 static bool
1202 is_tracepoint_type (bptype type)
1203 {
1204 return (type == bp_tracepoint
1205 || type == bp_fast_tracepoint
1206 || type == bp_static_tracepoint
1207 || type == bp_static_marker_tracepoint);
1208 }
1209
1210 /* See breakpoint.h. */
1211
1212 bool
1213 is_tracepoint (const struct breakpoint *b)
1214 {
1215 return is_tracepoint_type (b->type);
1216 }
1217
1218 /* Factory function to create an appropriate instance of breakpoint given
1219 TYPE. */
1220
1221 static std::unique_ptr<breakpoint>
1222 new_breakpoint_from_type (bptype type)
1223 {
1224 breakpoint *b;
1225
1226 switch (type)
1227 {
1228 case bp_breakpoint:
1229 case bp_hardware_breakpoint:
1230 b = new ordinary_breakpoint ();
1231 break;
1232
1233 case bp_fast_tracepoint:
1234 case bp_static_tracepoint:
1235 case bp_tracepoint:
1236 b = new tracepoint ();
1237 break;
1238
1239 case bp_static_marker_tracepoint:
1240 b = new static_marker_tracepoint ();
1241 break;
1242
1243 case bp_dprintf:
1244 b = new dprintf_breakpoint ();
1245 break;
1246
1247 case bp_overlay_event:
1248 case bp_longjmp_master:
1249 case bp_std_terminate_master:
1250 case bp_exception_master:
1251 case bp_thread_event:
1252 case bp_jit_event:
1253 case bp_shlib_event:
1254 b = new internal_breakpoint ();
1255 break;
1256
1257 case bp_longjmp:
1258 case bp_exception:
1259 b = new longjmp_breakpoint ();
1260 break;
1261
1262 case bp_watchpoint_scope:
1263 case bp_finish:
1264 case bp_gnu_ifunc_resolver_return:
1265 case bp_step_resume:
1266 case bp_hp_step_resume:
1267 case bp_longjmp_resume:
1268 case bp_longjmp_call_dummy:
1269 case bp_exception_resume:
1270 case bp_call_dummy:
1271 case bp_until:
1272 case bp_std_terminate:
1273 b = new momentary_breakpoint ();
1274 break;
1275
1276 default:
1277 gdb_assert_not_reached ("invalid type");
1278 }
1279
1280 return std::unique_ptr<breakpoint> (b);
1281 }
1282
1283 /* A helper function that validates that COMMANDS are valid for a
1284 breakpoint. This function will throw an exception if a problem is
1285 found. */
1286
1287 static void
1288 validate_commands_for_breakpoint (struct breakpoint *b,
1289 struct command_line *commands)
1290 {
1291 if (is_tracepoint (b))
1292 {
1293 struct tracepoint *t = (struct tracepoint *) b;
1294 struct command_line *c;
1295 struct command_line *while_stepping = 0;
1296
1297 /* Reset the while-stepping step count. The previous commands
1298 might have included a while-stepping action, while the new
1299 ones might not. */
1300 t->step_count = 0;
1301
1302 /* We need to verify that each top-level element of commands is
1303 valid for tracepoints, that there's at most one
1304 while-stepping element, and that the while-stepping's body
1305 has valid tracing commands excluding nested while-stepping.
1306 We also need to validate the tracepoint action line in the
1307 context of the tracepoint --- validate_actionline actually
1308 has side effects, like setting the tracepoint's
1309 while-stepping STEP_COUNT, in addition to checking if the
1310 collect/teval actions parse and make sense in the
1311 tracepoint's context. */
1312 for (c = commands; c; c = c->next)
1313 {
1314 if (c->control_type == while_stepping_control)
1315 {
1316 if (b->type == bp_fast_tracepoint)
1317 error (_("The 'while-stepping' command "
1318 "cannot be used for fast tracepoint"));
1319 else if (b->type == bp_static_tracepoint
1320 || b->type == bp_static_marker_tracepoint)
1321 error (_("The 'while-stepping' command "
1322 "cannot be used for static tracepoint"));
1323
1324 if (while_stepping)
1325 error (_("The 'while-stepping' command "
1326 "can be used only once"));
1327 else
1328 while_stepping = c;
1329 }
1330
1331 validate_actionline (c->line, b);
1332 }
1333 if (while_stepping)
1334 {
1335 struct command_line *c2;
1336
1337 gdb_assert (while_stepping->body_list_1 == nullptr);
1338 c2 = while_stepping->body_list_0.get ();
1339 for (; c2; c2 = c2->next)
1340 {
1341 if (c2->control_type == while_stepping_control)
1342 error (_("The 'while-stepping' command cannot be nested"));
1343 }
1344 }
1345 }
1346 else
1347 {
1348 check_no_tracepoint_commands (commands);
1349 }
1350 }
1351
1352 /* Return a vector of all the static tracepoints set at ADDR. The
1353 caller is responsible for releasing the vector. */
1354
1355 std::vector<breakpoint *>
1356 static_tracepoints_here (CORE_ADDR addr)
1357 {
1358 std::vector<breakpoint *> found;
1359
1360 for (breakpoint *b : all_breakpoints ())
1361 if (b->type == bp_static_tracepoint
1362 || b->type == bp_static_marker_tracepoint)
1363 {
1364 for (bp_location *loc : b->locations ())
1365 if (loc->address == addr)
1366 found.push_back (b);
1367 }
1368
1369 return found;
1370 }
1371
1372 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1373 validate that only allowed commands are included. */
1374
1375 void
1376 breakpoint_set_commands (struct breakpoint *b,
1377 counted_command_line &&commands)
1378 {
1379 validate_commands_for_breakpoint (b, commands.get ());
1380
1381 b->commands = std::move (commands);
1382 gdb::observers::breakpoint_modified.notify (b);
1383 }
1384
1385 /* Set the internal `silent' flag on the breakpoint. Note that this
1386 is not the same as the "silent" that may appear in the breakpoint's
1387 commands. */
1388
1389 void
1390 breakpoint_set_silent (struct breakpoint *b, int silent)
1391 {
1392 int old_silent = b->silent;
1393
1394 b->silent = silent;
1395 if (old_silent != silent)
1396 gdb::observers::breakpoint_modified.notify (b);
1397 }
1398
1399 /* Set the thread for this breakpoint. If THREAD is -1, make the
1400 breakpoint work for any thread. */
1401
1402 void
1403 breakpoint_set_thread (struct breakpoint *b, int thread)
1404 {
1405 int old_thread = b->thread;
1406
1407 b->thread = thread;
1408 if (old_thread != thread)
1409 gdb::observers::breakpoint_modified.notify (b);
1410 }
1411
1412 /* Set the task for this breakpoint. If TASK is 0, make the
1413 breakpoint work for any task. */
1414
1415 void
1416 breakpoint_set_task (struct breakpoint *b, int task)
1417 {
1418 int old_task = b->task;
1419
1420 b->task = task;
1421 if (old_task != task)
1422 gdb::observers::breakpoint_modified.notify (b);
1423 }
1424
1425 static void
1426 commands_command_1 (const char *arg, int from_tty,
1427 struct command_line *control)
1428 {
1429 counted_command_line cmd;
1430 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1431 NULL after the call to read_command_lines if the user provides an empty
1432 list of command by just typing "end". */
1433 bool cmd_read = false;
1434
1435 std::string new_arg;
1436
1437 if (arg == NULL || !*arg)
1438 {
1439 /* Argument not explicitly given. Synthesize it. */
1440 if (breakpoint_count - prev_breakpoint_count > 1)
1441 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1442 breakpoint_count);
1443 else if (breakpoint_count > 0)
1444 new_arg = string_printf ("%d", breakpoint_count);
1445 }
1446 else
1447 {
1448 /* Create a copy of ARG. This is needed because the "commands"
1449 command may be coming from a script. In that case, the read
1450 line buffer is going to be overwritten in the lambda of
1451 'map_breakpoint_numbers' below when reading the next line
1452 before we are are done parsing the breakpoint numbers. */
1453 new_arg = arg;
1454 }
1455 arg = new_arg.c_str ();
1456
1457 map_breakpoint_numbers
1458 (arg, [&] (breakpoint *b)
1459 {
1460 if (!cmd_read)
1461 {
1462 gdb_assert (cmd == NULL);
1463 if (control != NULL)
1464 cmd = control->body_list_0;
1465 else
1466 {
1467 std::string str
1468 = string_printf (_("Type commands for breakpoint(s) "
1469 "%s, one per line."),
1470 arg);
1471
1472 auto do_validate = [=] (const char *line)
1473 {
1474 validate_actionline (line, b);
1475 };
1476 gdb::function_view<void (const char *)> validator;
1477 if (is_tracepoint (b))
1478 validator = do_validate;
1479
1480 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1481 }
1482 cmd_read = true;
1483 }
1484
1485 /* If a breakpoint was on the list more than once, we don't need to
1486 do anything. */
1487 if (b->commands != cmd)
1488 {
1489 validate_commands_for_breakpoint (b, cmd.get ());
1490 b->commands = cmd;
1491 gdb::observers::breakpoint_modified.notify (b);
1492 }
1493 });
1494 }
1495
1496 static void
1497 commands_command (const char *arg, int from_tty)
1498 {
1499 commands_command_1 (arg, from_tty, NULL);
1500 }
1501
1502 /* Like commands_command, but instead of reading the commands from
1503 input stream, takes them from an already parsed command structure.
1504
1505 This is used by cli-script.c to DTRT with breakpoint commands
1506 that are part of if and while bodies. */
1507 enum command_control_type
1508 commands_from_control_command (const char *arg, struct command_line *cmd)
1509 {
1510 commands_command_1 (arg, 0, cmd);
1511 return simple_control;
1512 }
1513
1514 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1515
1516 static int
1517 bp_location_has_shadow (struct bp_location *bl)
1518 {
1519 if (bl->loc_type != bp_loc_software_breakpoint)
1520 return 0;
1521 if (!bl->inserted)
1522 return 0;
1523 if (bl->target_info.shadow_len == 0)
1524 /* BL isn't valid, or doesn't shadow memory. */
1525 return 0;
1526 return 1;
1527 }
1528
1529 /* Update BUF, which is LEN bytes read from the target address
1530 MEMADDR, by replacing a memory breakpoint with its shadowed
1531 contents.
1532
1533 If READBUF is not NULL, this buffer must not overlap with the of
1534 the breakpoint location's shadow_contents buffer. Otherwise, a
1535 failed assertion internal error will be raised. */
1536
1537 static void
1538 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1539 const gdb_byte *writebuf_org,
1540 ULONGEST memaddr, LONGEST len,
1541 struct bp_target_info *target_info,
1542 struct gdbarch *gdbarch)
1543 {
1544 /* Now do full processing of the found relevant range of elements. */
1545 CORE_ADDR bp_addr = 0;
1546 int bp_size = 0;
1547 int bptoffset = 0;
1548
1549 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1550 current_program_space->aspace, 0))
1551 {
1552 /* The breakpoint is inserted in a different address space. */
1553 return;
1554 }
1555
1556 /* Addresses and length of the part of the breakpoint that
1557 we need to copy. */
1558 bp_addr = target_info->placed_address;
1559 bp_size = target_info->shadow_len;
1560
1561 if (bp_addr + bp_size <= memaddr)
1562 {
1563 /* The breakpoint is entirely before the chunk of memory we are
1564 reading. */
1565 return;
1566 }
1567
1568 if (bp_addr >= memaddr + len)
1569 {
1570 /* The breakpoint is entirely after the chunk of memory we are
1571 reading. */
1572 return;
1573 }
1574
1575 /* Offset within shadow_contents. */
1576 if (bp_addr < memaddr)
1577 {
1578 /* Only copy the second part of the breakpoint. */
1579 bp_size -= memaddr - bp_addr;
1580 bptoffset = memaddr - bp_addr;
1581 bp_addr = memaddr;
1582 }
1583
1584 if (bp_addr + bp_size > memaddr + len)
1585 {
1586 /* Only copy the first part of the breakpoint. */
1587 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1588 }
1589
1590 if (readbuf != NULL)
1591 {
1592 /* Verify that the readbuf buffer does not overlap with the
1593 shadow_contents buffer. */
1594 gdb_assert (target_info->shadow_contents >= readbuf + len
1595 || readbuf >= (target_info->shadow_contents
1596 + target_info->shadow_len));
1597
1598 /* Update the read buffer with this inserted breakpoint's
1599 shadow. */
1600 memcpy (readbuf + bp_addr - memaddr,
1601 target_info->shadow_contents + bptoffset, bp_size);
1602 }
1603 else
1604 {
1605 const unsigned char *bp;
1606 CORE_ADDR addr = target_info->reqstd_address;
1607 int placed_size;
1608
1609 /* Update the shadow with what we want to write to memory. */
1610 memcpy (target_info->shadow_contents + bptoffset,
1611 writebuf_org + bp_addr - memaddr, bp_size);
1612
1613 /* Determine appropriate breakpoint contents and size for this
1614 address. */
1615 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1616
1617 /* Update the final write buffer with this inserted
1618 breakpoint's INSN. */
1619 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1620 }
1621 }
1622
1623 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1624 by replacing any memory breakpoints with their shadowed contents.
1625
1626 If READBUF is not NULL, this buffer must not overlap with any of
1627 the breakpoint location's shadow_contents buffers. Otherwise,
1628 a failed assertion internal error will be raised.
1629
1630 The range of shadowed area by each bp_location is:
1631 bl->address - bp_locations_placed_address_before_address_max
1632 up to bl->address + bp_locations_shadow_len_after_address_max
1633 The range we were requested to resolve shadows for is:
1634 memaddr ... memaddr + len
1635 Thus the safe cutoff boundaries for performance optimization are
1636 memaddr + len <= (bl->address
1637 - bp_locations_placed_address_before_address_max)
1638 and:
1639 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1640
1641 void
1642 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1643 const gdb_byte *writebuf_org,
1644 ULONGEST memaddr, LONGEST len)
1645 {
1646 /* Left boundary, right boundary and median element of our binary
1647 search. */
1648 unsigned bc_l, bc_r, bc;
1649
1650 /* Find BC_L which is a leftmost element which may affect BUF
1651 content. It is safe to report lower value but a failure to
1652 report higher one. */
1653
1654 bc_l = 0;
1655 bc_r = bp_locations.size ();
1656 while (bc_l + 1 < bc_r)
1657 {
1658 struct bp_location *bl;
1659
1660 bc = (bc_l + bc_r) / 2;
1661 bl = bp_locations[bc];
1662
1663 /* Check first BL->ADDRESS will not overflow due to the added
1664 constant. Then advance the left boundary only if we are sure
1665 the BC element can in no way affect the BUF content (MEMADDR
1666 to MEMADDR + LEN range).
1667
1668 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1669 offset so that we cannot miss a breakpoint with its shadow
1670 range tail still reaching MEMADDR. */
1671
1672 if ((bl->address + bp_locations_shadow_len_after_address_max
1673 >= bl->address)
1674 && (bl->address + bp_locations_shadow_len_after_address_max
1675 <= memaddr))
1676 bc_l = bc;
1677 else
1678 bc_r = bc;
1679 }
1680
1681 /* Due to the binary search above, we need to make sure we pick the
1682 first location that's at BC_L's address. E.g., if there are
1683 multiple locations at the same address, BC_L may end up pointing
1684 at a duplicate location, and miss the "master"/"inserted"
1685 location. Say, given locations L1, L2 and L3 at addresses A and
1686 B:
1687
1688 L1@A, L2@A, L3@B, ...
1689
1690 BC_L could end up pointing at location L2, while the "master"
1691 location could be L1. Since the `loc->inserted' flag is only set
1692 on "master" locations, we'd forget to restore the shadow of L1
1693 and L2. */
1694 while (bc_l > 0
1695 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1696 bc_l--;
1697
1698 /* Now do full processing of the found relevant range of elements. */
1699
1700 for (bc = bc_l; bc < bp_locations.size (); bc++)
1701 {
1702 struct bp_location *bl = bp_locations[bc];
1703
1704 /* bp_location array has BL->OWNER always non-NULL. */
1705 if (bl->owner->type == bp_none)
1706 warning (_("reading through apparently deleted breakpoint #%d?"),
1707 bl->owner->number);
1708
1709 /* Performance optimization: any further element can no longer affect BUF
1710 content. */
1711
1712 if (bl->address >= bp_locations_placed_address_before_address_max
1713 && (memaddr + len
1714 <= (bl->address
1715 - bp_locations_placed_address_before_address_max)))
1716 break;
1717
1718 if (!bp_location_has_shadow (bl))
1719 continue;
1720
1721 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1722 memaddr, len, &bl->target_info, bl->gdbarch);
1723 }
1724 }
1725
1726 /* See breakpoint.h. */
1727
1728 bool
1729 is_breakpoint (const struct breakpoint *bpt)
1730 {
1731 return (bpt->type == bp_breakpoint
1732 || bpt->type == bp_hardware_breakpoint
1733 || bpt->type == bp_dprintf);
1734 }
1735
1736 /* Return true if BPT is of any hardware watchpoint kind. */
1737
1738 static bool
1739 is_hardware_watchpoint (const struct breakpoint *bpt)
1740 {
1741 return (bpt->type == bp_hardware_watchpoint
1742 || bpt->type == bp_read_watchpoint
1743 || bpt->type == bp_access_watchpoint);
1744 }
1745
1746 /* See breakpoint.h. */
1747
1748 bool
1749 is_watchpoint (const struct breakpoint *bpt)
1750 {
1751 return (is_hardware_watchpoint (bpt)
1752 || bpt->type == bp_watchpoint);
1753 }
1754
1755 /* Returns true if the current thread and its running state are safe
1756 to evaluate or update watchpoint B. Watchpoints on local
1757 expressions need to be evaluated in the context of the thread that
1758 was current when the watchpoint was created, and, that thread needs
1759 to be stopped to be able to select the correct frame context.
1760 Watchpoints on global expressions can be evaluated on any thread,
1761 and in any state. It is presently left to the target allowing
1762 memory accesses when threads are running. */
1763
1764 static int
1765 watchpoint_in_thread_scope (struct watchpoint *b)
1766 {
1767 return (b->pspace == current_program_space
1768 && (b->watchpoint_thread == null_ptid
1769 || (inferior_ptid == b->watchpoint_thread
1770 && !inferior_thread ()->executing ())));
1771 }
1772
1773 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1774 associated bp_watchpoint_scope breakpoint. */
1775
1776 static void
1777 watchpoint_del_at_next_stop (struct watchpoint *w)
1778 {
1779 if (w->related_breakpoint != w)
1780 {
1781 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1782 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1783 w->related_breakpoint->disposition = disp_del_at_next_stop;
1784 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1785 w->related_breakpoint = w;
1786 }
1787 w->disposition = disp_del_at_next_stop;
1788 }
1789
1790 /* Extract a bitfield value from value VAL using the bit parameters contained in
1791 watchpoint W. */
1792
1793 static struct value *
1794 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1795 {
1796 struct value *bit_val;
1797
1798 if (val == NULL)
1799 return NULL;
1800
1801 bit_val = allocate_value (value_type (val));
1802
1803 unpack_value_bitfield (bit_val,
1804 w->val_bitpos,
1805 w->val_bitsize,
1806 value_contents_for_printing (val).data (),
1807 value_offset (val),
1808 val);
1809
1810 return bit_val;
1811 }
1812
1813 /* Allocate a dummy location and add it to B, which must be a software
1814 watchpoint. This is required because even if a software watchpoint
1815 is not watching any memory, bpstat_stop_status requires a location
1816 to be able to report stops. */
1817
1818 static void
1819 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1820 struct program_space *pspace)
1821 {
1822 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1823
1824 b->loc = b->allocate_location ();
1825 b->loc->pspace = pspace;
1826 b->loc->address = -1;
1827 b->loc->length = -1;
1828 }
1829
1830 /* Returns true if B is a software watchpoint that is not watching any
1831 memory (e.g., "watch $pc"). */
1832
1833 static bool
1834 is_no_memory_software_watchpoint (struct breakpoint *b)
1835 {
1836 return (b->type == bp_watchpoint
1837 && b->loc != NULL
1838 && b->loc->next == NULL
1839 && b->loc->address == -1
1840 && b->loc->length == -1);
1841 }
1842
1843 /* Assuming that B is a watchpoint:
1844 - Reparse watchpoint expression, if REPARSE is non-zero
1845 - Evaluate expression and store the result in B->val
1846 - Evaluate the condition if there is one, and store the result
1847 in b->loc->cond.
1848 - Update the list of values that must be watched in B->loc.
1849
1850 If the watchpoint disposition is disp_del_at_next_stop, then do
1851 nothing. If this is local watchpoint that is out of scope, delete
1852 it.
1853
1854 Even with `set breakpoint always-inserted on' the watchpoints are
1855 removed + inserted on each stop here. Normal breakpoints must
1856 never be removed because they might be missed by a running thread
1857 when debugging in non-stop mode. On the other hand, hardware
1858 watchpoints (is_hardware_watchpoint; processed here) are specific
1859 to each LWP since they are stored in each LWP's hardware debug
1860 registers. Therefore, such LWP must be stopped first in order to
1861 be able to modify its hardware watchpoints.
1862
1863 Hardware watchpoints must be reset exactly once after being
1864 presented to the user. It cannot be done sooner, because it would
1865 reset the data used to present the watchpoint hit to the user. And
1866 it must not be done later because it could display the same single
1867 watchpoint hit during multiple GDB stops. Note that the latter is
1868 relevant only to the hardware watchpoint types bp_read_watchpoint
1869 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1870 not user-visible - its hit is suppressed if the memory content has
1871 not changed.
1872
1873 The following constraints influence the location where we can reset
1874 hardware watchpoints:
1875
1876 * target_stopped_by_watchpoint and target_stopped_data_address are
1877 called several times when GDB stops.
1878
1879 [linux]
1880 * Multiple hardware watchpoints can be hit at the same time,
1881 causing GDB to stop. GDB only presents one hardware watchpoint
1882 hit at a time as the reason for stopping, and all the other hits
1883 are presented later, one after the other, each time the user
1884 requests the execution to be resumed. Execution is not resumed
1885 for the threads still having pending hit event stored in
1886 LWP_INFO->STATUS. While the watchpoint is already removed from
1887 the inferior on the first stop the thread hit event is kept being
1888 reported from its cached value by linux_nat_stopped_data_address
1889 until the real thread resume happens after the watchpoint gets
1890 presented and thus its LWP_INFO->STATUS gets reset.
1891
1892 Therefore the hardware watchpoint hit can get safely reset on the
1893 watchpoint removal from inferior. */
1894
1895 static void
1896 update_watchpoint (struct watchpoint *b, int reparse)
1897 {
1898 int within_current_scope;
1899 struct frame_id saved_frame_id;
1900 int frame_saved;
1901
1902 /* If this is a local watchpoint, we only want to check if the
1903 watchpoint frame is in scope if the current thread is the thread
1904 that was used to create the watchpoint. */
1905 if (!watchpoint_in_thread_scope (b))
1906 return;
1907
1908 if (b->disposition == disp_del_at_next_stop)
1909 return;
1910
1911 frame_saved = 0;
1912
1913 /* Determine if the watchpoint is within scope. */
1914 if (b->exp_valid_block == NULL)
1915 within_current_scope = 1;
1916 else
1917 {
1918 struct frame_info *fi = get_current_frame ();
1919 struct gdbarch *frame_arch = get_frame_arch (fi);
1920 CORE_ADDR frame_pc = get_frame_pc (fi);
1921
1922 /* If we're at a point where the stack has been destroyed
1923 (e.g. in a function epilogue), unwinding may not work
1924 properly. Do not attempt to recreate locations at this
1925 point. See similar comments in watchpoint_check. */
1926 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1927 return;
1928
1929 /* Save the current frame's ID so we can restore it after
1930 evaluating the watchpoint expression on its own frame. */
1931 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1932 took a frame parameter, so that we didn't have to change the
1933 selected frame. */
1934 frame_saved = 1;
1935 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1936
1937 fi = frame_find_by_id (b->watchpoint_frame);
1938 within_current_scope = (fi != NULL);
1939 if (within_current_scope)
1940 select_frame (fi);
1941 }
1942
1943 /* We don't free locations. They are stored in the bp_location array
1944 and update_global_location_list will eventually delete them and
1945 remove breakpoints if needed. */
1946 b->loc = NULL;
1947
1948 if (within_current_scope && reparse)
1949 {
1950 const char *s;
1951
1952 b->exp.reset ();
1953 s = (b->exp_string_reparse
1954 ? b->exp_string_reparse.get ()
1955 : b->exp_string.get ());
1956 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1957 /* If the meaning of expression itself changed, the old value is
1958 no longer relevant. We don't want to report a watchpoint hit
1959 to the user when the old value and the new value may actually
1960 be completely different objects. */
1961 b->val = NULL;
1962 b->val_valid = false;
1963
1964 /* Note that unlike with breakpoints, the watchpoint's condition
1965 expression is stored in the breakpoint object, not in the
1966 locations (re)created below. */
1967 if (b->cond_string != NULL)
1968 {
1969 b->cond_exp.reset ();
1970
1971 s = b->cond_string.get ();
1972 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1973 }
1974 }
1975
1976 /* If we failed to parse the expression, for example because
1977 it refers to a global variable in a not-yet-loaded shared library,
1978 don't try to insert watchpoint. We don't automatically delete
1979 such watchpoint, though, since failure to parse expression
1980 is different from out-of-scope watchpoint. */
1981 if (!target_has_execution ())
1982 {
1983 /* Without execution, memory can't change. No use to try and
1984 set watchpoint locations. The watchpoint will be reset when
1985 the target gains execution, through breakpoint_re_set. */
1986 if (!can_use_hw_watchpoints)
1987 {
1988 if (b->works_in_software_mode ())
1989 b->type = bp_watchpoint;
1990 else
1991 error (_("Can't set read/access watchpoint when "
1992 "hardware watchpoints are disabled."));
1993 }
1994 }
1995 else if (within_current_scope && b->exp)
1996 {
1997 std::vector<value_ref_ptr> val_chain;
1998 struct value *v, *result;
1999 struct program_space *frame_pspace;
2000
2001 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
2002 &val_chain, false);
2003
2004 /* Avoid setting b->val if it's already set. The meaning of
2005 b->val is 'the last value' user saw, and we should update
2006 it only if we reported that last value to user. As it
2007 happens, the code that reports it updates b->val directly.
2008 We don't keep track of the memory value for masked
2009 watchpoints. */
2010 if (!b->val_valid && !is_masked_watchpoint (b))
2011 {
2012 if (b->val_bitsize != 0)
2013 v = extract_bitfield_from_watchpoint_value (b, v);
2014 b->val = release_value (v);
2015 b->val_valid = true;
2016 }
2017
2018 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2019
2020 /* Look at each value on the value chain. */
2021 gdb_assert (!val_chain.empty ());
2022 for (const value_ref_ptr &iter : val_chain)
2023 {
2024 v = iter.get ();
2025
2026 /* If it's a memory location, and GDB actually needed
2027 its contents to evaluate the expression, then we
2028 must watch it. If the first value returned is
2029 still lazy, that means an error occurred reading it;
2030 watch it anyway in case it becomes readable. */
2031 if (VALUE_LVAL (v) == lval_memory
2032 && (v == val_chain[0] || ! value_lazy (v)))
2033 {
2034 struct type *vtype = check_typedef (value_type (v));
2035
2036 /* We only watch structs and arrays if user asked
2037 for it explicitly, never if they just happen to
2038 appear in the middle of some value chain. */
2039 if (v == result
2040 || (vtype->code () != TYPE_CODE_STRUCT
2041 && vtype->code () != TYPE_CODE_ARRAY))
2042 {
2043 CORE_ADDR addr;
2044 enum target_hw_bp_type type;
2045 struct bp_location *loc, **tmp;
2046 int bitpos = 0, bitsize = 0;
2047
2048 if (value_bitsize (v) != 0)
2049 {
2050 /* Extract the bit parameters out from the bitfield
2051 sub-expression. */
2052 bitpos = value_bitpos (v);
2053 bitsize = value_bitsize (v);
2054 }
2055 else if (v == result && b->val_bitsize != 0)
2056 {
2057 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2058 lvalue whose bit parameters are saved in the fields
2059 VAL_BITPOS and VAL_BITSIZE. */
2060 bitpos = b->val_bitpos;
2061 bitsize = b->val_bitsize;
2062 }
2063
2064 addr = value_address (v);
2065 if (bitsize != 0)
2066 {
2067 /* Skip the bytes that don't contain the bitfield. */
2068 addr += bitpos / 8;
2069 }
2070
2071 type = hw_write;
2072 if (b->type == bp_read_watchpoint)
2073 type = hw_read;
2074 else if (b->type == bp_access_watchpoint)
2075 type = hw_access;
2076
2077 loc = b->allocate_location ();
2078 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2079 ;
2080 *tmp = loc;
2081 loc->gdbarch = value_type (v)->arch ();
2082
2083 loc->pspace = frame_pspace;
2084 loc->address = address_significant (loc->gdbarch, addr);
2085
2086 if (bitsize != 0)
2087 {
2088 /* Just cover the bytes that make up the bitfield. */
2089 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2090 }
2091 else
2092 loc->length = TYPE_LENGTH (value_type (v));
2093
2094 loc->watchpoint_type = type;
2095 }
2096 }
2097 }
2098
2099 /* Change the type of breakpoint between hardware assisted or
2100 an ordinary watchpoint depending on the hardware support
2101 and free hardware slots. REPARSE is set when the inferior
2102 is started. */
2103 if (reparse)
2104 {
2105 int reg_cnt;
2106 enum bp_loc_type loc_type;
2107
2108 reg_cnt = can_use_hardware_watchpoint (val_chain);
2109
2110 if (reg_cnt)
2111 {
2112 int i, target_resources_ok, other_type_used;
2113 enum bptype type;
2114
2115 /* Use an exact watchpoint when there's only one memory region to be
2116 watched, and only one debug register is needed to watch it. */
2117 b->exact = target_exact_watchpoints && reg_cnt == 1;
2118
2119 /* We need to determine how many resources are already
2120 used for all other hardware watchpoints plus this one
2121 to see if we still have enough resources to also fit
2122 this watchpoint in as well. */
2123
2124 /* If this is a software watchpoint, we try to turn it
2125 to a hardware one -- count resources as if B was of
2126 hardware watchpoint type. */
2127 type = b->type;
2128 if (type == bp_watchpoint)
2129 type = bp_hardware_watchpoint;
2130
2131 /* This watchpoint may or may not have been placed on
2132 the list yet at this point (it won't be in the list
2133 if we're trying to create it for the first time,
2134 through watch_command), so always account for it
2135 manually. */
2136
2137 /* Count resources used by all watchpoints except B. */
2138 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2139
2140 /* Add in the resources needed for B. */
2141 i += hw_watchpoint_use_count (b);
2142
2143 target_resources_ok
2144 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2145 if (target_resources_ok <= 0)
2146 {
2147 bool sw_mode = b->works_in_software_mode ();
2148
2149 if (target_resources_ok == 0 && !sw_mode)
2150 error (_("Target does not support this type of "
2151 "hardware watchpoint."));
2152 else if (target_resources_ok < 0 && !sw_mode)
2153 error (_("There are not enough available hardware "
2154 "resources for this watchpoint."));
2155
2156 /* Downgrade to software watchpoint. */
2157 b->type = bp_watchpoint;
2158 }
2159 else
2160 {
2161 /* If this was a software watchpoint, we've just
2162 found we have enough resources to turn it to a
2163 hardware watchpoint. Otherwise, this is a
2164 nop. */
2165 b->type = type;
2166 }
2167 }
2168 else if (!b->works_in_software_mode ())
2169 {
2170 if (!can_use_hw_watchpoints)
2171 error (_("Can't set read/access watchpoint when "
2172 "hardware watchpoints are disabled."));
2173 else
2174 error (_("Expression cannot be implemented with "
2175 "read/access watchpoint."));
2176 }
2177 else
2178 b->type = bp_watchpoint;
2179
2180 loc_type = (b->type == bp_watchpoint? bp_loc_other
2181 : bp_loc_hardware_watchpoint);
2182 for (bp_location *bl : b->locations ())
2183 bl->loc_type = loc_type;
2184 }
2185
2186 /* If a software watchpoint is not watching any memory, then the
2187 above left it without any location set up. But,
2188 bpstat_stop_status requires a location to be able to report
2189 stops, so make sure there's at least a dummy one. */
2190 if (b->type == bp_watchpoint && b->loc == NULL)
2191 software_watchpoint_add_no_memory_location (b, frame_pspace);
2192 }
2193 else if (!within_current_scope)
2194 {
2195 gdb_printf (_("\
2196 Watchpoint %d deleted because the program has left the block\n\
2197 in which its expression is valid.\n"),
2198 b->number);
2199 watchpoint_del_at_next_stop (b);
2200 }
2201
2202 /* Restore the selected frame. */
2203 if (frame_saved)
2204 select_frame (frame_find_by_id (saved_frame_id));
2205 }
2206
2207
2208 /* Returns 1 iff breakpoint location should be
2209 inserted in the inferior. We don't differentiate the type of BL's owner
2210 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2211 breakpoint_ops is not defined, because in insert_bp_location,
2212 tracepoint's insert_location will not be called. */
2213 static int
2214 should_be_inserted (struct bp_location *bl)
2215 {
2216 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2217 return 0;
2218
2219 if (bl->owner->disposition == disp_del_at_next_stop)
2220 return 0;
2221
2222 if (!bl->enabled || bl->disabled_by_cond
2223 || bl->shlib_disabled || bl->duplicate)
2224 return 0;
2225
2226 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2227 return 0;
2228
2229 /* This is set for example, when we're attached to the parent of a
2230 vfork, and have detached from the child. The child is running
2231 free, and we expect it to do an exec or exit, at which point the
2232 OS makes the parent schedulable again (and the target reports
2233 that the vfork is done). Until the child is done with the shared
2234 memory region, do not insert breakpoints in the parent, otherwise
2235 the child could still trip on the parent's breakpoints. Since
2236 the parent is blocked anyway, it won't miss any breakpoint. */
2237 if (bl->pspace->breakpoints_not_allowed)
2238 return 0;
2239
2240 /* Don't insert a breakpoint if we're trying to step past its
2241 location, except if the breakpoint is a single-step breakpoint,
2242 and the breakpoint's thread is the thread which is stepping past
2243 a breakpoint. */
2244 if ((bl->loc_type == bp_loc_software_breakpoint
2245 || bl->loc_type == bp_loc_hardware_breakpoint)
2246 && stepping_past_instruction_at (bl->pspace->aspace,
2247 bl->address)
2248 /* The single-step breakpoint may be inserted at the location
2249 we're trying to step if the instruction branches to itself.
2250 However, the instruction won't be executed at all and it may
2251 break the semantics of the instruction, for example, the
2252 instruction is a conditional branch or updates some flags.
2253 We can't fix it unless GDB is able to emulate the instruction
2254 or switch to displaced stepping. */
2255 && !(bl->owner->type == bp_single_step
2256 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2257 {
2258 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2259 paddress (bl->gdbarch, bl->address));
2260 return 0;
2261 }
2262
2263 /* Don't insert watchpoints if we're trying to step past the
2264 instruction that triggered one. */
2265 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2266 && stepping_past_nonsteppable_watchpoint ())
2267 {
2268 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2269 "skipping watchpoint at %s:%d",
2270 paddress (bl->gdbarch, bl->address), bl->length);
2271 return 0;
2272 }
2273
2274 return 1;
2275 }
2276
2277 /* Same as should_be_inserted but does the check assuming
2278 that the location is not duplicated. */
2279
2280 static int
2281 unduplicated_should_be_inserted (struct bp_location *bl)
2282 {
2283 int result;
2284 const int save_duplicate = bl->duplicate;
2285
2286 bl->duplicate = 0;
2287 result = should_be_inserted (bl);
2288 bl->duplicate = save_duplicate;
2289 return result;
2290 }
2291
2292 /* Parses a conditional described by an expression COND into an
2293 agent expression bytecode suitable for evaluation
2294 by the bytecode interpreter. Return NULL if there was
2295 any error during parsing. */
2296
2297 static agent_expr_up
2298 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2299 {
2300 if (cond == NULL)
2301 return NULL;
2302
2303 agent_expr_up aexpr;
2304
2305 /* We don't want to stop processing, so catch any errors
2306 that may show up. */
2307 try
2308 {
2309 aexpr = gen_eval_for_expr (scope, cond);
2310 }
2311
2312 catch (const gdb_exception_error &ex)
2313 {
2314 /* If we got here, it means the condition could not be parsed to a valid
2315 bytecode expression and thus can't be evaluated on the target's side.
2316 It's no use iterating through the conditions. */
2317 }
2318
2319 /* We have a valid agent expression. */
2320 return aexpr;
2321 }
2322
2323 /* Based on location BL, create a list of breakpoint conditions to be
2324 passed on to the target. If we have duplicated locations with different
2325 conditions, we will add such conditions to the list. The idea is that the
2326 target will evaluate the list of conditions and will only notify GDB when
2327 one of them is true. */
2328
2329 static void
2330 build_target_condition_list (struct bp_location *bl)
2331 {
2332 int null_condition_or_parse_error = 0;
2333 int modified = bl->needs_update;
2334
2335 /* Release conditions left over from a previous insert. */
2336 bl->target_info.conditions.clear ();
2337
2338 /* This is only meaningful if the target is
2339 evaluating conditions and if the user has
2340 opted for condition evaluation on the target's
2341 side. */
2342 if (gdb_evaluates_breakpoint_condition_p ()
2343 || !target_supports_evaluation_of_breakpoint_conditions ())
2344 return;
2345
2346 auto loc_range = all_bp_locations_at_addr (bl->address);
2347
2348 /* Do a first pass to check for locations with no assigned
2349 conditions or conditions that fail to parse to a valid agent
2350 expression bytecode. If any of these happen, then it's no use to
2351 send conditions to the target since this location will always
2352 trigger and generate a response back to GDB. Note we consider
2353 all locations at the same address irrespective of type, i.e.,
2354 even if the locations aren't considered duplicates (e.g.,
2355 software breakpoint and hardware breakpoint at the same
2356 address). */
2357 for (bp_location *loc : loc_range)
2358 {
2359 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2360 {
2361 if (modified)
2362 {
2363 /* Re-parse the conditions since something changed. In that
2364 case we already freed the condition bytecodes (see
2365 force_breakpoint_reinsertion). We just
2366 need to parse the condition to bytecodes again. */
2367 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2368 loc->cond.get ());
2369 }
2370
2371 /* If we have a NULL bytecode expression, it means something
2372 went wrong or we have a null condition expression. */
2373 if (!loc->cond_bytecode)
2374 {
2375 null_condition_or_parse_error = 1;
2376 break;
2377 }
2378 }
2379 }
2380
2381 /* If any of these happened, it means we will have to evaluate the conditions
2382 for the location's address on gdb's side. It is no use keeping bytecodes
2383 for all the other duplicate locations, thus we free all of them here.
2384
2385 This is so we have a finer control over which locations' conditions are
2386 being evaluated by GDB or the remote stub. */
2387 if (null_condition_or_parse_error)
2388 {
2389 for (bp_location *loc : loc_range)
2390 {
2391 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2392 {
2393 /* Only go as far as the first NULL bytecode is
2394 located. */
2395 if (!loc->cond_bytecode)
2396 return;
2397
2398 loc->cond_bytecode.reset ();
2399 }
2400 }
2401 }
2402
2403 /* No NULL conditions or failed bytecode generation. Build a
2404 condition list for this location's address. If we have software
2405 and hardware locations at the same address, they aren't
2406 considered duplicates, but we still marge all the conditions
2407 anyway, as it's simpler, and doesn't really make a practical
2408 difference. */
2409 for (bp_location *loc : loc_range)
2410 if (loc->cond
2411 && is_breakpoint (loc->owner)
2412 && loc->pspace->num == bl->pspace->num
2413 && loc->owner->enable_state == bp_enabled
2414 && loc->enabled
2415 && !loc->disabled_by_cond)
2416 {
2417 /* Add the condition to the vector. This will be used later
2418 to send the conditions to the target. */
2419 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2420 }
2421
2422 return;
2423 }
2424
2425 /* Parses a command described by string CMD into an agent expression
2426 bytecode suitable for evaluation by the bytecode interpreter.
2427 Return NULL if there was any error during parsing. */
2428
2429 static agent_expr_up
2430 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2431 {
2432 const char *cmdrest;
2433 const char *format_start, *format_end;
2434 struct gdbarch *gdbarch = get_current_arch ();
2435
2436 if (cmd == NULL)
2437 return NULL;
2438
2439 cmdrest = cmd;
2440
2441 if (*cmdrest == ',')
2442 ++cmdrest;
2443 cmdrest = skip_spaces (cmdrest);
2444
2445 if (*cmdrest++ != '"')
2446 error (_("No format string following the location"));
2447
2448 format_start = cmdrest;
2449
2450 format_pieces fpieces (&cmdrest);
2451
2452 format_end = cmdrest;
2453
2454 if (*cmdrest++ != '"')
2455 error (_("Bad format string, non-terminated '\"'."));
2456
2457 cmdrest = skip_spaces (cmdrest);
2458
2459 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2460 error (_("Invalid argument syntax"));
2461
2462 if (*cmdrest == ',')
2463 cmdrest++;
2464 cmdrest = skip_spaces (cmdrest);
2465
2466 /* For each argument, make an expression. */
2467
2468 std::vector<struct expression *> argvec;
2469 while (*cmdrest != '\0')
2470 {
2471 const char *cmd1;
2472
2473 cmd1 = cmdrest;
2474 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2475 argvec.push_back (expr.release ());
2476 cmdrest = cmd1;
2477 if (*cmdrest == ',')
2478 ++cmdrest;
2479 }
2480
2481 agent_expr_up aexpr;
2482
2483 /* We don't want to stop processing, so catch any errors
2484 that may show up. */
2485 try
2486 {
2487 aexpr = gen_printf (scope, gdbarch, 0, 0,
2488 format_start, format_end - format_start,
2489 argvec.size (), argvec.data ());
2490 }
2491 catch (const gdb_exception_error &ex)
2492 {
2493 /* If we got here, it means the command could not be parsed to a valid
2494 bytecode expression and thus can't be evaluated on the target's side.
2495 It's no use iterating through the other commands. */
2496 }
2497
2498 /* We have a valid agent expression, return it. */
2499 return aexpr;
2500 }
2501
2502 /* Based on location BL, create a list of breakpoint commands to be
2503 passed on to the target. If we have duplicated locations with
2504 different commands, we will add any such to the list. */
2505
2506 static void
2507 build_target_command_list (struct bp_location *bl)
2508 {
2509 int null_command_or_parse_error = 0;
2510 int modified = bl->needs_update;
2511
2512 /* Clear commands left over from a previous insert. */
2513 bl->target_info.tcommands.clear ();
2514
2515 if (!target_can_run_breakpoint_commands ())
2516 return;
2517
2518 /* For now, limit to agent-style dprintf breakpoints. */
2519 if (dprintf_style != dprintf_style_agent)
2520 return;
2521
2522 auto loc_range = all_bp_locations_at_addr (bl->address);
2523
2524 /* For now, if we have any location at the same address that isn't a
2525 dprintf, don't install the target-side commands, as that would
2526 make the breakpoint not be reported to the core, and we'd lose
2527 control. */
2528 for (bp_location *loc : loc_range)
2529 if (is_breakpoint (loc->owner)
2530 && loc->pspace->num == bl->pspace->num
2531 && loc->owner->type != bp_dprintf)
2532 return;
2533
2534 /* Do a first pass to check for locations with no assigned
2535 conditions or conditions that fail to parse to a valid agent expression
2536 bytecode. If any of these happen, then it's no use to send conditions
2537 to the target since this location will always trigger and generate a
2538 response back to GDB. */
2539 for (bp_location *loc : loc_range)
2540 {
2541 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2542 {
2543 if (modified)
2544 {
2545 /* Re-parse the commands since something changed. In that
2546 case we already freed the command bytecodes (see
2547 force_breakpoint_reinsertion). We just
2548 need to parse the command to bytecodes again. */
2549 loc->cmd_bytecode
2550 = parse_cmd_to_aexpr (bl->address,
2551 loc->owner->extra_string.get ());
2552 }
2553
2554 /* If we have a NULL bytecode expression, it means something
2555 went wrong or we have a null command expression. */
2556 if (!loc->cmd_bytecode)
2557 {
2558 null_command_or_parse_error = 1;
2559 break;
2560 }
2561 }
2562 }
2563
2564 /* If anything failed, then we're not doing target-side commands,
2565 and so clean up. */
2566 if (null_command_or_parse_error)
2567 {
2568 for (bp_location *loc : loc_range)
2569 if (is_breakpoint (loc->owner)
2570 && loc->pspace->num == bl->pspace->num)
2571 {
2572 /* Only go as far as the first NULL bytecode is
2573 located. */
2574 if (loc->cmd_bytecode == NULL)
2575 return;
2576
2577 loc->cmd_bytecode.reset ();
2578 }
2579 }
2580
2581 /* No NULL commands or failed bytecode generation. Build a command
2582 list for all duplicate locations at this location's address.
2583 Note that here we must care for whether the breakpoint location
2584 types are considered duplicates, otherwise, say, if we have a
2585 software and hardware location at the same address, the target
2586 could end up running the commands twice. For the moment, we only
2587 support targets-side commands with dprintf, but it doesn't hurt
2588 to be pedantically correct in case that changes. */
2589 for (bp_location *loc : loc_range)
2590 if (breakpoint_locations_match (bl, loc)
2591 && loc->owner->extra_string
2592 && is_breakpoint (loc->owner)
2593 && loc->pspace->num == bl->pspace->num
2594 && loc->owner->enable_state == bp_enabled
2595 && loc->enabled
2596 && !loc->disabled_by_cond)
2597 {
2598 /* Add the command to the vector. This will be used later
2599 to send the commands to the target. */
2600 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2601 }
2602
2603 bl->target_info.persist = 0;
2604 /* Maybe flag this location as persistent. */
2605 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2606 bl->target_info.persist = 1;
2607 }
2608
2609 /* Return the kind of breakpoint on address *ADDR. Get the kind
2610 of breakpoint according to ADDR except single-step breakpoint.
2611 Get the kind of single-step breakpoint according to the current
2612 registers state. */
2613
2614 static int
2615 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2616 {
2617 if (bl->owner->type == bp_single_step)
2618 {
2619 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2620 struct regcache *regcache;
2621
2622 regcache = get_thread_regcache (thr);
2623
2624 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2625 regcache, addr);
2626 }
2627 else
2628 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2629 }
2630
2631 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2632 location. Any error messages are printed to TMP_ERROR_STREAM; and
2633 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2634 Returns 0 for success, 1 if the bp_location type is not supported or
2635 -1 for failure.
2636
2637 NOTE drow/2003-09-09: This routine could be broken down to an
2638 object-style method for each breakpoint or catchpoint type. */
2639 static int
2640 insert_bp_location (struct bp_location *bl,
2641 struct ui_file *tmp_error_stream,
2642 int *disabled_breaks,
2643 int *hw_breakpoint_error,
2644 int *hw_bp_error_explained_already)
2645 {
2646 gdb_exception bp_excpt;
2647
2648 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2649 return 0;
2650
2651 /* Note we don't initialize bl->target_info, as that wipes out
2652 the breakpoint location's shadow_contents if the breakpoint
2653 is still inserted at that location. This in turn breaks
2654 target_read_memory which depends on these buffers when
2655 a memory read is requested at the breakpoint location:
2656 Once the target_info has been wiped, we fail to see that
2657 we have a breakpoint inserted at that address and thus
2658 read the breakpoint instead of returning the data saved in
2659 the breakpoint location's shadow contents. */
2660 bl->target_info.reqstd_address = bl->address;
2661 bl->target_info.placed_address_space = bl->pspace->aspace;
2662 bl->target_info.length = bl->length;
2663
2664 /* When working with target-side conditions, we must pass all the conditions
2665 for the same breakpoint address down to the target since GDB will not
2666 insert those locations. With a list of breakpoint conditions, the target
2667 can decide when to stop and notify GDB. */
2668
2669 if (is_breakpoint (bl->owner))
2670 {
2671 build_target_condition_list (bl);
2672 build_target_command_list (bl);
2673 /* Reset the modification marker. */
2674 bl->needs_update = 0;
2675 }
2676
2677 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2678 set at a read-only address, then a breakpoint location will have
2679 been changed to hardware breakpoint before we get here. If it is
2680 "off" however, error out before actually trying to insert the
2681 breakpoint, with a nicer error message. */
2682 if (bl->loc_type == bp_loc_software_breakpoint
2683 && !automatic_hardware_breakpoints)
2684 {
2685 mem_region *mr = lookup_mem_region (bl->address);
2686
2687 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2688 {
2689 gdb_printf (tmp_error_stream,
2690 _("Cannot insert breakpoint %d.\n"
2691 "Cannot set software breakpoint "
2692 "at read-only address %s\n"),
2693 bl->owner->number,
2694 paddress (bl->gdbarch, bl->address));
2695 return 1;
2696 }
2697 }
2698
2699 if (bl->loc_type == bp_loc_software_breakpoint
2700 || bl->loc_type == bp_loc_hardware_breakpoint)
2701 {
2702 /* First check to see if we have to handle an overlay. */
2703 if (overlay_debugging == ovly_off
2704 || bl->section == NULL
2705 || !(section_is_overlay (bl->section)))
2706 {
2707 /* No overlay handling: just set the breakpoint. */
2708 try
2709 {
2710 int val;
2711
2712 val = bl->owner->insert_location (bl);
2713 if (val)
2714 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2715 }
2716 catch (gdb_exception &e)
2717 {
2718 bp_excpt = std::move (e);
2719 }
2720 }
2721 else
2722 {
2723 /* This breakpoint is in an overlay section.
2724 Shall we set a breakpoint at the LMA? */
2725 if (!overlay_events_enabled)
2726 {
2727 /* Yes -- overlay event support is not active,
2728 so we must try to set a breakpoint at the LMA.
2729 This will not work for a hardware breakpoint. */
2730 if (bl->loc_type == bp_loc_hardware_breakpoint)
2731 warning (_("hardware breakpoint %d not supported in overlay!"),
2732 bl->owner->number);
2733 else
2734 {
2735 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2736 bl->section);
2737 /* Set a software (trap) breakpoint at the LMA. */
2738 bl->overlay_target_info = bl->target_info;
2739 bl->overlay_target_info.reqstd_address = addr;
2740
2741 /* No overlay handling: just set the breakpoint. */
2742 try
2743 {
2744 int val;
2745
2746 bl->overlay_target_info.kind
2747 = breakpoint_kind (bl, &addr);
2748 bl->overlay_target_info.placed_address = addr;
2749 val = target_insert_breakpoint (bl->gdbarch,
2750 &bl->overlay_target_info);
2751 if (val)
2752 bp_excpt
2753 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2754 }
2755 catch (gdb_exception &e)
2756 {
2757 bp_excpt = std::move (e);
2758 }
2759
2760 if (bp_excpt.reason != 0)
2761 gdb_printf (tmp_error_stream,
2762 "Overlay breakpoint %d "
2763 "failed: in ROM?\n",
2764 bl->owner->number);
2765 }
2766 }
2767 /* Shall we set a breakpoint at the VMA? */
2768 if (section_is_mapped (bl->section))
2769 {
2770 /* Yes. This overlay section is mapped into memory. */
2771 try
2772 {
2773 int val;
2774
2775 val = bl->owner->insert_location (bl);
2776 if (val)
2777 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2778 }
2779 catch (gdb_exception &e)
2780 {
2781 bp_excpt = std::move (e);
2782 }
2783 }
2784 else
2785 {
2786 /* No. This breakpoint will not be inserted.
2787 No error, but do not mark the bp as 'inserted'. */
2788 return 0;
2789 }
2790 }
2791
2792 if (bp_excpt.reason != 0)
2793 {
2794 /* Can't set the breakpoint. */
2795
2796 /* If the target has closed then it will have deleted any
2797 breakpoints inserted within the target inferior, as a result
2798 any further attempts to interact with the breakpoint objects
2799 is not possible. Just rethrow the error. */
2800 if (bp_excpt.error == TARGET_CLOSE_ERROR)
2801 throw bp_excpt;
2802 gdb_assert (bl->owner != nullptr);
2803
2804 /* In some cases, we might not be able to insert a
2805 breakpoint in a shared library that has already been
2806 removed, but we have not yet processed the shlib unload
2807 event. Unfortunately, some targets that implement
2808 breakpoint insertion themselves can't tell why the
2809 breakpoint insertion failed (e.g., the remote target
2810 doesn't define error codes), so we must treat generic
2811 errors as memory errors. */
2812 if (bp_excpt.reason == RETURN_ERROR
2813 && (bp_excpt.error == GENERIC_ERROR
2814 || bp_excpt.error == MEMORY_ERROR)
2815 && bl->loc_type == bp_loc_software_breakpoint
2816 && (solib_name_from_address (bl->pspace, bl->address)
2817 || shared_objfile_contains_address_p (bl->pspace,
2818 bl->address)))
2819 {
2820 /* See also: disable_breakpoints_in_shlibs. */
2821 bl->shlib_disabled = 1;
2822 gdb::observers::breakpoint_modified.notify (bl->owner);
2823 if (!*disabled_breaks)
2824 {
2825 gdb_printf (tmp_error_stream,
2826 "Cannot insert breakpoint %d.\n",
2827 bl->owner->number);
2828 gdb_printf (tmp_error_stream,
2829 "Temporarily disabling shared "
2830 "library breakpoints:\n");
2831 }
2832 *disabled_breaks = 1;
2833 gdb_printf (tmp_error_stream,
2834 "breakpoint #%d\n", bl->owner->number);
2835 return 0;
2836 }
2837 else
2838 {
2839 if (bl->loc_type == bp_loc_hardware_breakpoint)
2840 {
2841 *hw_breakpoint_error = 1;
2842 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2843 gdb_printf (tmp_error_stream,
2844 "Cannot insert hardware breakpoint %d%s",
2845 bl->owner->number,
2846 bp_excpt.message ? ":" : ".\n");
2847 if (bp_excpt.message != NULL)
2848 gdb_printf (tmp_error_stream, "%s.\n",
2849 bp_excpt.what ());
2850 }
2851 else
2852 {
2853 if (bp_excpt.message == NULL)
2854 {
2855 std::string message
2856 = memory_error_message (TARGET_XFER_E_IO,
2857 bl->gdbarch, bl->address);
2858
2859 gdb_printf (tmp_error_stream,
2860 "Cannot insert breakpoint %d.\n"
2861 "%s\n",
2862 bl->owner->number, message.c_str ());
2863 }
2864 else
2865 {
2866 gdb_printf (tmp_error_stream,
2867 "Cannot insert breakpoint %d: %s\n",
2868 bl->owner->number,
2869 bp_excpt.what ());
2870 }
2871 }
2872 return 1;
2873
2874 }
2875 }
2876 else
2877 bl->inserted = 1;
2878
2879 return 0;
2880 }
2881
2882 else if (bl->loc_type == bp_loc_hardware_watchpoint
2883 /* NOTE drow/2003-09-08: This state only exists for removing
2884 watchpoints. It's not clear that it's necessary... */
2885 && bl->owner->disposition != disp_del_at_next_stop)
2886 {
2887 int val;
2888
2889 val = bl->owner->insert_location (bl);
2890
2891 /* If trying to set a read-watchpoint, and it turns out it's not
2892 supported, try emulating one with an access watchpoint. */
2893 if (val == 1 && bl->watchpoint_type == hw_read)
2894 {
2895 /* But don't try to insert it, if there's already another
2896 hw_access location that would be considered a duplicate
2897 of this one. */
2898 for (bp_location *loc : all_bp_locations ())
2899 if (loc != bl
2900 && loc->watchpoint_type == hw_access
2901 && watchpoint_locations_match (bl, loc))
2902 {
2903 bl->duplicate = 1;
2904 bl->inserted = 1;
2905 bl->target_info = loc->target_info;
2906 bl->watchpoint_type = hw_access;
2907 val = 0;
2908 break;
2909 }
2910
2911 if (val == 1)
2912 {
2913 bl->watchpoint_type = hw_access;
2914 val = bl->owner->insert_location (bl);
2915
2916 if (val)
2917 /* Back to the original value. */
2918 bl->watchpoint_type = hw_read;
2919 }
2920 }
2921
2922 bl->inserted = (val == 0);
2923 }
2924
2925 else if (bl->owner->type == bp_catchpoint)
2926 {
2927 int val;
2928
2929 val = bl->owner->insert_location (bl);
2930 if (val)
2931 {
2932 bl->owner->enable_state = bp_disabled;
2933
2934 if (val == 1)
2935 warning (_("\
2936 Error inserting catchpoint %d: Your system does not support this type\n\
2937 of catchpoint."), bl->owner->number);
2938 else
2939 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2940 }
2941
2942 bl->inserted = (val == 0);
2943
2944 /* We've already printed an error message if there was a problem
2945 inserting this catchpoint, and we've disabled the catchpoint,
2946 so just return success. */
2947 return 0;
2948 }
2949
2950 return 0;
2951 }
2952
2953 /* This function is called when program space PSPACE is about to be
2954 deleted. It takes care of updating breakpoints to not reference
2955 PSPACE anymore. */
2956
2957 void
2958 breakpoint_program_space_exit (struct program_space *pspace)
2959 {
2960 /* Remove any breakpoint that was set through this program space. */
2961 for (breakpoint *b : all_breakpoints_safe ())
2962 if (b->pspace == pspace)
2963 delete_breakpoint (b);
2964
2965 /* Breakpoints set through other program spaces could have locations
2966 bound to PSPACE as well. Remove those. */
2967 for (bp_location *loc : all_bp_locations ())
2968 {
2969 struct bp_location *tmp;
2970
2971 if (loc->pspace == pspace)
2972 {
2973 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2974 if (loc->owner->loc == loc)
2975 loc->owner->loc = loc->next;
2976 else
2977 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2978 if (tmp->next == loc)
2979 {
2980 tmp->next = loc->next;
2981 break;
2982 }
2983 }
2984 }
2985
2986 /* Now update the global location list to permanently delete the
2987 removed locations above. */
2988 update_global_location_list (UGLL_DONT_INSERT);
2989 }
2990
2991 /* Make sure all breakpoints are inserted in inferior.
2992 Throws exception on any error.
2993 A breakpoint that is already inserted won't be inserted
2994 again, so calling this function twice is safe. */
2995 void
2996 insert_breakpoints (void)
2997 {
2998 for (breakpoint *bpt : all_breakpoints ())
2999 if (is_hardware_watchpoint (bpt))
3000 {
3001 struct watchpoint *w = (struct watchpoint *) bpt;
3002
3003 update_watchpoint (w, 0 /* don't reparse. */);
3004 }
3005
3006 /* Updating watchpoints creates new locations, so update the global
3007 location list. Explicitly tell ugll to insert locations and
3008 ignore breakpoints_always_inserted_mode. Also,
3009 update_global_location_list tries to "upgrade" software
3010 breakpoints to hardware breakpoints to handle "set breakpoint
3011 auto-hw", so we need to call it even if we don't have new
3012 locations. */
3013 update_global_location_list (UGLL_INSERT);
3014 }
3015
3016 /* This is used when we need to synch breakpoint conditions between GDB and the
3017 target. It is the case with deleting and disabling of breakpoints when using
3018 always-inserted mode. */
3019
3020 static void
3021 update_inserted_breakpoint_locations (void)
3022 {
3023 int error_flag = 0;
3024 int val = 0;
3025 int disabled_breaks = 0;
3026 int hw_breakpoint_error = 0;
3027 int hw_bp_details_reported = 0;
3028
3029 string_file tmp_error_stream;
3030
3031 /* Explicitly mark the warning -- this will only be printed if
3032 there was an error. */
3033 tmp_error_stream.puts ("Warning:\n");
3034
3035 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3036
3037 for (bp_location *bl : all_bp_locations ())
3038 {
3039 /* We only want to update software breakpoints and hardware
3040 breakpoints. */
3041 if (!is_breakpoint (bl->owner))
3042 continue;
3043
3044 /* We only want to update locations that are already inserted
3045 and need updating. This is to avoid unwanted insertion during
3046 deletion of breakpoints. */
3047 if (!bl->inserted || !bl->needs_update)
3048 continue;
3049
3050 switch_to_program_space_and_thread (bl->pspace);
3051
3052 /* For targets that support global breakpoints, there's no need
3053 to select an inferior to insert breakpoint to. In fact, even
3054 if we aren't attached to any process yet, we should still
3055 insert breakpoints. */
3056 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3057 && (inferior_ptid == null_ptid || !target_has_execution ()))
3058 continue;
3059
3060 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3061 &hw_breakpoint_error, &hw_bp_details_reported);
3062 if (val)
3063 error_flag = val;
3064 }
3065
3066 if (error_flag)
3067 {
3068 target_terminal::ours_for_output ();
3069 error_stream (tmp_error_stream);
3070 }
3071 }
3072
3073 /* Used when starting or continuing the program. */
3074
3075 static void
3076 insert_breakpoint_locations (void)
3077 {
3078 int error_flag = 0;
3079 int val = 0;
3080 int disabled_breaks = 0;
3081 int hw_breakpoint_error = 0;
3082 int hw_bp_error_explained_already = 0;
3083
3084 string_file tmp_error_stream;
3085
3086 /* Explicitly mark the warning -- this will only be printed if
3087 there was an error. */
3088 tmp_error_stream.puts ("Warning:\n");
3089
3090 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3091
3092 for (bp_location *bl : all_bp_locations ())
3093 {
3094 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3095 continue;
3096
3097 /* There is no point inserting thread-specific breakpoints if
3098 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3099 has BL->OWNER always non-NULL. */
3100 if (bl->owner->thread != -1
3101 && !valid_global_thread_id (bl->owner->thread))
3102 continue;
3103
3104 switch_to_program_space_and_thread (bl->pspace);
3105
3106 /* For targets that support global breakpoints, there's no need
3107 to select an inferior to insert breakpoint to. In fact, even
3108 if we aren't attached to any process yet, we should still
3109 insert breakpoints. */
3110 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3111 && (inferior_ptid == null_ptid || !target_has_execution ()))
3112 continue;
3113
3114 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3115 &hw_breakpoint_error, &hw_bp_error_explained_already);
3116 if (val)
3117 error_flag = val;
3118 }
3119
3120 /* If we failed to insert all locations of a watchpoint, remove
3121 them, as half-inserted watchpoint is of limited use. */
3122 for (breakpoint *bpt : all_breakpoints ())
3123 {
3124 int some_failed = 0;
3125
3126 if (!is_hardware_watchpoint (bpt))
3127 continue;
3128
3129 if (!breakpoint_enabled (bpt))
3130 continue;
3131
3132 if (bpt->disposition == disp_del_at_next_stop)
3133 continue;
3134
3135 for (bp_location *loc : bpt->locations ())
3136 if (!loc->inserted && should_be_inserted (loc))
3137 {
3138 some_failed = 1;
3139 break;
3140 }
3141
3142 if (some_failed)
3143 {
3144 for (bp_location *loc : bpt->locations ())
3145 if (loc->inserted)
3146 remove_breakpoint (loc);
3147
3148 hw_breakpoint_error = 1;
3149 tmp_error_stream.printf ("Could not insert "
3150 "hardware watchpoint %d.\n",
3151 bpt->number);
3152 error_flag = -1;
3153 }
3154 }
3155
3156 if (error_flag)
3157 {
3158 /* If a hardware breakpoint or watchpoint was inserted, add a
3159 message about possibly exhausted resources. */
3160 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3161 {
3162 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3163 You may have requested too many hardware breakpoints/watchpoints.\n");
3164 }
3165 target_terminal::ours_for_output ();
3166 error_stream (tmp_error_stream);
3167 }
3168 }
3169
3170 /* Used when the program stops.
3171 Returns zero if successful, or non-zero if there was a problem
3172 removing a breakpoint location. */
3173
3174 int
3175 remove_breakpoints (void)
3176 {
3177 int val = 0;
3178
3179 for (bp_location *bl : all_bp_locations ())
3180 if (bl->inserted && !is_tracepoint (bl->owner))
3181 val |= remove_breakpoint (bl);
3182
3183 return val;
3184 }
3185
3186 /* When a thread exits, remove breakpoints that are related to
3187 that thread. */
3188
3189 static void
3190 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3191 {
3192 for (breakpoint *b : all_breakpoints_safe ())
3193 {
3194 if (b->thread == tp->global_num && user_breakpoint_p (b))
3195 {
3196 b->disposition = disp_del_at_next_stop;
3197
3198 gdb_printf (_("\
3199 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3200 b->number, print_thread_id (tp));
3201
3202 /* Hide it from the user. */
3203 b->number = 0;
3204 }
3205 }
3206 }
3207
3208 /* See breakpoint.h. */
3209
3210 void
3211 remove_breakpoints_inf (inferior *inf)
3212 {
3213 int val;
3214
3215 for (bp_location *bl : all_bp_locations ())
3216 {
3217 if (bl->pspace != inf->pspace)
3218 continue;
3219
3220 if (bl->inserted && !bl->target_info.persist)
3221 {
3222 val = remove_breakpoint (bl);
3223 if (val != 0)
3224 return;
3225 }
3226 }
3227 }
3228
3229 static int internal_breakpoint_number = -1;
3230
3231 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3232 If INTERNAL is non-zero, the breakpoint number will be populated
3233 from internal_breakpoint_number and that variable decremented.
3234 Otherwise the breakpoint number will be populated from
3235 breakpoint_count and that value incremented. Internal breakpoints
3236 do not set the internal var bpnum. */
3237 static void
3238 set_breakpoint_number (int internal, struct breakpoint *b)
3239 {
3240 if (internal)
3241 b->number = internal_breakpoint_number--;
3242 else
3243 {
3244 set_breakpoint_count (breakpoint_count + 1);
3245 b->number = breakpoint_count;
3246 }
3247 }
3248
3249 static struct breakpoint *
3250 create_internal_breakpoint (struct gdbarch *gdbarch,
3251 CORE_ADDR address, enum bptype type)
3252 {
3253 symtab_and_line sal;
3254 sal.pc = address;
3255 sal.section = find_pc_overlay (sal.pc);
3256 sal.pspace = current_program_space;
3257
3258 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type);
3259 b->number = internal_breakpoint_number--;
3260 b->disposition = disp_donttouch;
3261
3262 return b;
3263 }
3264
3265 static const char *const longjmp_names[] =
3266 {
3267 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3268 };
3269 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3270
3271 /* Per-objfile data private to breakpoint.c. */
3272 struct breakpoint_objfile_data
3273 {
3274 /* Minimal symbol for "_ovly_debug_event" (if any). */
3275 struct bound_minimal_symbol overlay_msym;
3276
3277 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3278 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3279
3280 /* True if we have looked for longjmp probes. */
3281 int longjmp_searched = 0;
3282
3283 /* SystemTap probe points for longjmp (if any). These are non-owning
3284 references. */
3285 std::vector<probe *> longjmp_probes;
3286
3287 /* Minimal symbol for "std::terminate()" (if any). */
3288 struct bound_minimal_symbol terminate_msym;
3289
3290 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3291 struct bound_minimal_symbol exception_msym;
3292
3293 /* True if we have looked for exception probes. */
3294 int exception_searched = 0;
3295
3296 /* SystemTap probe points for unwinding (if any). These are non-owning
3297 references. */
3298 std::vector<probe *> exception_probes;
3299 };
3300
3301 static const struct objfile_key<breakpoint_objfile_data>
3302 breakpoint_objfile_key;
3303
3304 /* Minimal symbol not found sentinel. */
3305 static struct minimal_symbol msym_not_found;
3306
3307 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3308
3309 static int
3310 msym_not_found_p (const struct minimal_symbol *msym)
3311 {
3312 return msym == &msym_not_found;
3313 }
3314
3315 /* Return per-objfile data needed by breakpoint.c.
3316 Allocate the data if necessary. */
3317
3318 static struct breakpoint_objfile_data *
3319 get_breakpoint_objfile_data (struct objfile *objfile)
3320 {
3321 struct breakpoint_objfile_data *bp_objfile_data;
3322
3323 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3324 if (bp_objfile_data == NULL)
3325 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3326 return bp_objfile_data;
3327 }
3328
3329 static void
3330 create_overlay_event_breakpoint (void)
3331 {
3332 const char *const func_name = "_ovly_debug_event";
3333
3334 for (objfile *objfile : current_program_space->objfiles ())
3335 {
3336 struct breakpoint *b;
3337 struct breakpoint_objfile_data *bp_objfile_data;
3338 CORE_ADDR addr;
3339 struct explicit_location explicit_loc;
3340
3341 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3342
3343 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3344 continue;
3345
3346 if (bp_objfile_data->overlay_msym.minsym == NULL)
3347 {
3348 struct bound_minimal_symbol m;
3349
3350 m = lookup_minimal_symbol_text (func_name, objfile);
3351 if (m.minsym == NULL)
3352 {
3353 /* Avoid future lookups in this objfile. */
3354 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3355 continue;
3356 }
3357 bp_objfile_data->overlay_msym = m;
3358 }
3359
3360 addr = bp_objfile_data->overlay_msym.value_address ();
3361 b = create_internal_breakpoint (objfile->arch (), addr,
3362 bp_overlay_event);
3363 initialize_explicit_location (&explicit_loc);
3364 explicit_loc.function_name = ASTRDUP (func_name);
3365 b->location = new_explicit_location (&explicit_loc);
3366
3367 if (overlay_debugging == ovly_auto)
3368 {
3369 b->enable_state = bp_enabled;
3370 overlay_events_enabled = 1;
3371 }
3372 else
3373 {
3374 b->enable_state = bp_disabled;
3375 overlay_events_enabled = 0;
3376 }
3377 }
3378 }
3379
3380 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3381 true if a breakpoint was installed. */
3382
3383 static bool
3384 create_longjmp_master_breakpoint_probe (objfile *objfile)
3385 {
3386 struct gdbarch *gdbarch = objfile->arch ();
3387 struct breakpoint_objfile_data *bp_objfile_data
3388 = get_breakpoint_objfile_data (objfile);
3389
3390 if (!bp_objfile_data->longjmp_searched)
3391 {
3392 std::vector<probe *> ret
3393 = find_probes_in_objfile (objfile, "libc", "longjmp");
3394
3395 if (!ret.empty ())
3396 {
3397 /* We are only interested in checking one element. */
3398 probe *p = ret[0];
3399
3400 if (!p->can_evaluate_arguments ())
3401 {
3402 /* We cannot use the probe interface here,
3403 because it does not know how to evaluate
3404 arguments. */
3405 ret.clear ();
3406 }
3407 }
3408 bp_objfile_data->longjmp_probes = ret;
3409 bp_objfile_data->longjmp_searched = 1;
3410 }
3411
3412 if (bp_objfile_data->longjmp_probes.empty ())
3413 return false;
3414
3415 for (probe *p : bp_objfile_data->longjmp_probes)
3416 {
3417 struct breakpoint *b;
3418
3419 b = create_internal_breakpoint (gdbarch,
3420 p->get_relocated_address (objfile),
3421 bp_longjmp_master);
3422 b->location = new_probe_location ("-probe-stap libc:longjmp");
3423 b->enable_state = bp_disabled;
3424 }
3425
3426 return true;
3427 }
3428
3429 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3430 Return true if at least one breakpoint was installed. */
3431
3432 static bool
3433 create_longjmp_master_breakpoint_names (objfile *objfile)
3434 {
3435 struct gdbarch *gdbarch = objfile->arch ();
3436 if (!gdbarch_get_longjmp_target_p (gdbarch))
3437 return false;
3438
3439 struct breakpoint_objfile_data *bp_objfile_data
3440 = get_breakpoint_objfile_data (objfile);
3441 unsigned int installed_bp = 0;
3442
3443 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3444 {
3445 struct breakpoint *b;
3446 const char *func_name;
3447 CORE_ADDR addr;
3448 struct explicit_location explicit_loc;
3449
3450 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3451 continue;
3452
3453 func_name = longjmp_names[i];
3454 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3455 {
3456 struct bound_minimal_symbol m;
3457
3458 m = lookup_minimal_symbol_text (func_name, objfile);
3459 if (m.minsym == NULL)
3460 {
3461 /* Prevent future lookups in this objfile. */
3462 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3463 continue;
3464 }
3465 bp_objfile_data->longjmp_msym[i] = m;
3466 }
3467
3468 addr = bp_objfile_data->longjmp_msym[i].value_address ();
3469 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
3470 initialize_explicit_location (&explicit_loc);
3471 explicit_loc.function_name = ASTRDUP (func_name);
3472 b->location = new_explicit_location (&explicit_loc);
3473 b->enable_state = bp_disabled;
3474 installed_bp++;
3475 }
3476
3477 return installed_bp > 0;
3478 }
3479
3480 /* Create a master longjmp breakpoint. */
3481
3482 static void
3483 create_longjmp_master_breakpoint (void)
3484 {
3485 scoped_restore_current_program_space restore_pspace;
3486
3487 for (struct program_space *pspace : program_spaces)
3488 {
3489 set_current_program_space (pspace);
3490
3491 for (objfile *obj : current_program_space->objfiles ())
3492 {
3493 /* Skip separate debug object, it's handled in the loop below. */
3494 if (obj->separate_debug_objfile_backlink != nullptr)
3495 continue;
3496
3497 /* Try a probe kind breakpoint on main objfile. */
3498 if (create_longjmp_master_breakpoint_probe (obj))
3499 continue;
3500
3501 /* Try longjmp_names kind breakpoints on main and separate_debug
3502 objfiles. */
3503 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3504 if (create_longjmp_master_breakpoint_names (debug_objfile))
3505 break;
3506 }
3507 }
3508 }
3509
3510 /* Create a master std::terminate breakpoint. */
3511 static void
3512 create_std_terminate_master_breakpoint (void)
3513 {
3514 const char *const func_name = "std::terminate()";
3515
3516 scoped_restore_current_program_space restore_pspace;
3517
3518 for (struct program_space *pspace : program_spaces)
3519 {
3520 CORE_ADDR addr;
3521
3522 set_current_program_space (pspace);
3523
3524 for (objfile *objfile : current_program_space->objfiles ())
3525 {
3526 struct breakpoint *b;
3527 struct breakpoint_objfile_data *bp_objfile_data;
3528 struct explicit_location explicit_loc;
3529
3530 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3531
3532 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3533 continue;
3534
3535 if (bp_objfile_data->terminate_msym.minsym == NULL)
3536 {
3537 struct bound_minimal_symbol m;
3538
3539 m = lookup_minimal_symbol (func_name, NULL, objfile);
3540 if (m.minsym == NULL || (m.minsym->type () != mst_text
3541 && m.minsym->type () != mst_file_text))
3542 {
3543 /* Prevent future lookups in this objfile. */
3544 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3545 continue;
3546 }
3547 bp_objfile_data->terminate_msym = m;
3548 }
3549
3550 addr = bp_objfile_data->terminate_msym.value_address ();
3551 b = create_internal_breakpoint (objfile->arch (), addr,
3552 bp_std_terminate_master);
3553 initialize_explicit_location (&explicit_loc);
3554 explicit_loc.function_name = ASTRDUP (func_name);
3555 b->location = new_explicit_location (&explicit_loc);
3556 b->enable_state = bp_disabled;
3557 }
3558 }
3559 }
3560
3561 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3562 probe. Return true if a breakpoint was installed. */
3563
3564 static bool
3565 create_exception_master_breakpoint_probe (objfile *objfile)
3566 {
3567 struct breakpoint *b;
3568 struct gdbarch *gdbarch;
3569 struct breakpoint_objfile_data *bp_objfile_data;
3570
3571 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3572
3573 /* We prefer the SystemTap probe point if it exists. */
3574 if (!bp_objfile_data->exception_searched)
3575 {
3576 std::vector<probe *> ret
3577 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3578
3579 if (!ret.empty ())
3580 {
3581 /* We are only interested in checking one element. */
3582 probe *p = ret[0];
3583
3584 if (!p->can_evaluate_arguments ())
3585 {
3586 /* We cannot use the probe interface here, because it does
3587 not know how to evaluate arguments. */
3588 ret.clear ();
3589 }
3590 }
3591 bp_objfile_data->exception_probes = ret;
3592 bp_objfile_data->exception_searched = 1;
3593 }
3594
3595 if (bp_objfile_data->exception_probes.empty ())
3596 return false;
3597
3598 gdbarch = objfile->arch ();
3599
3600 for (probe *p : bp_objfile_data->exception_probes)
3601 {
3602 b = create_internal_breakpoint (gdbarch,
3603 p->get_relocated_address (objfile),
3604 bp_exception_master);
3605 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3606 b->enable_state = bp_disabled;
3607 }
3608
3609 return true;
3610 }
3611
3612 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3613 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3614
3615 static bool
3616 create_exception_master_breakpoint_hook (objfile *objfile)
3617 {
3618 const char *const func_name = "_Unwind_DebugHook";
3619 struct breakpoint *b;
3620 struct gdbarch *gdbarch;
3621 struct breakpoint_objfile_data *bp_objfile_data;
3622 CORE_ADDR addr;
3623 struct explicit_location explicit_loc;
3624
3625 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3626
3627 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3628 return false;
3629
3630 gdbarch = objfile->arch ();
3631
3632 if (bp_objfile_data->exception_msym.minsym == NULL)
3633 {
3634 struct bound_minimal_symbol debug_hook;
3635
3636 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3637 if (debug_hook.minsym == NULL)
3638 {
3639 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3640 return false;
3641 }
3642
3643 bp_objfile_data->exception_msym = debug_hook;
3644 }
3645
3646 addr = bp_objfile_data->exception_msym.value_address ();
3647 addr = gdbarch_convert_from_func_ptr_addr
3648 (gdbarch, addr, current_inferior ()->top_target ());
3649 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
3650 initialize_explicit_location (&explicit_loc);
3651 explicit_loc.function_name = ASTRDUP (func_name);
3652 b->location = new_explicit_location (&explicit_loc);
3653 b->enable_state = bp_disabled;
3654
3655 return true;
3656 }
3657
3658 /* Install a master breakpoint on the unwinder's debug hook. */
3659
3660 static void
3661 create_exception_master_breakpoint (void)
3662 {
3663 for (objfile *obj : current_program_space->objfiles ())
3664 {
3665 /* Skip separate debug object. */
3666 if (obj->separate_debug_objfile_backlink)
3667 continue;
3668
3669 /* Try a probe kind breakpoint. */
3670 if (create_exception_master_breakpoint_probe (obj))
3671 continue;
3672
3673 /* Iterate over main and separate debug objects and try an
3674 _Unwind_DebugHook kind breakpoint. */
3675 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3676 if (create_exception_master_breakpoint_hook (debug_objfile))
3677 break;
3678 }
3679 }
3680
3681 /* Does B have a location spec? */
3682
3683 static int
3684 breakpoint_event_location_empty_p (const struct breakpoint *b)
3685 {
3686 return b->location != NULL && event_location_empty_p (b->location.get ());
3687 }
3688
3689 void
3690 update_breakpoints_after_exec (void)
3691 {
3692 /* We're about to delete breakpoints from GDB's lists. If the
3693 INSERTED flag is true, GDB will try to lift the breakpoints by
3694 writing the breakpoints' "shadow contents" back into memory. The
3695 "shadow contents" are NOT valid after an exec, so GDB should not
3696 do that. Instead, the target is responsible from marking
3697 breakpoints out as soon as it detects an exec. We don't do that
3698 here instead, because there may be other attempts to delete
3699 breakpoints after detecting an exec and before reaching here. */
3700 for (bp_location *bploc : all_bp_locations ())
3701 if (bploc->pspace == current_program_space)
3702 gdb_assert (!bploc->inserted);
3703
3704 for (breakpoint *b : all_breakpoints_safe ())
3705 {
3706 if (b->pspace != current_program_space)
3707 continue;
3708
3709 /* Solib breakpoints must be explicitly reset after an exec(). */
3710 if (b->type == bp_shlib_event)
3711 {
3712 delete_breakpoint (b);
3713 continue;
3714 }
3715
3716 /* JIT breakpoints must be explicitly reset after an exec(). */
3717 if (b->type == bp_jit_event)
3718 {
3719 delete_breakpoint (b);
3720 continue;
3721 }
3722
3723 /* Thread event breakpoints must be set anew after an exec(),
3724 as must overlay event and longjmp master breakpoints. */
3725 if (b->type == bp_thread_event || b->type == bp_overlay_event
3726 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3727 || b->type == bp_exception_master)
3728 {
3729 delete_breakpoint (b);
3730 continue;
3731 }
3732
3733 /* Step-resume breakpoints are meaningless after an exec(). */
3734 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3735 {
3736 delete_breakpoint (b);
3737 continue;
3738 }
3739
3740 /* Just like single-step breakpoints. */
3741 if (b->type == bp_single_step)
3742 {
3743 delete_breakpoint (b);
3744 continue;
3745 }
3746
3747 /* Longjmp and longjmp-resume breakpoints are also meaningless
3748 after an exec. */
3749 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3750 || b->type == bp_longjmp_call_dummy
3751 || b->type == bp_exception || b->type == bp_exception_resume)
3752 {
3753 delete_breakpoint (b);
3754 continue;
3755 }
3756
3757 if (b->type == bp_catchpoint)
3758 {
3759 /* For now, none of the bp_catchpoint breakpoints need to
3760 do anything at this point. In the future, if some of
3761 the catchpoints need to something, we will need to add
3762 a new method, and call this method from here. */
3763 continue;
3764 }
3765
3766 /* bp_finish is a special case. The only way we ought to be able
3767 to see one of these when an exec() has happened, is if the user
3768 caught a vfork, and then said "finish". Ordinarily a finish just
3769 carries them to the call-site of the current callee, by setting
3770 a temporary bp there and resuming. But in this case, the finish
3771 will carry them entirely through the vfork & exec.
3772
3773 We don't want to allow a bp_finish to remain inserted now. But
3774 we can't safely delete it, 'cause finish_command has a handle to
3775 the bp on a bpstat, and will later want to delete it. There's a
3776 chance (and I've seen it happen) that if we delete the bp_finish
3777 here, that its storage will get reused by the time finish_command
3778 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3779 We really must allow finish_command to delete a bp_finish.
3780
3781 In the absence of a general solution for the "how do we know
3782 it's safe to delete something others may have handles to?"
3783 problem, what we'll do here is just uninsert the bp_finish, and
3784 let finish_command delete it.
3785
3786 (We know the bp_finish is "doomed" in the sense that it's
3787 momentary, and will be deleted as soon as finish_command sees
3788 the inferior stopped. So it doesn't matter that the bp's
3789 address is probably bogus in the new a.out, unlike e.g., the
3790 solib breakpoints.) */
3791
3792 if (b->type == bp_finish)
3793 {
3794 continue;
3795 }
3796
3797 /* Without a symbolic address, we have little hope of the
3798 pre-exec() address meaning the same thing in the post-exec()
3799 a.out. */
3800 if (breakpoint_event_location_empty_p (b))
3801 {
3802 delete_breakpoint (b);
3803 continue;
3804 }
3805 }
3806 }
3807
3808 int
3809 detach_breakpoints (ptid_t ptid)
3810 {
3811 int val = 0;
3812 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3813 struct inferior *inf = current_inferior ();
3814
3815 if (ptid.pid () == inferior_ptid.pid ())
3816 error (_("Cannot detach breakpoints of inferior_ptid"));
3817
3818 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3819 inferior_ptid = ptid;
3820 for (bp_location *bl : all_bp_locations ())
3821 {
3822 if (bl->pspace != inf->pspace)
3823 continue;
3824
3825 /* This function must physically remove breakpoints locations
3826 from the specified ptid, without modifying the breakpoint
3827 package's state. Locations of type bp_loc_other are only
3828 maintained at GDB side. So, there is no need to remove
3829 these bp_loc_other locations. Moreover, removing these
3830 would modify the breakpoint package's state. */
3831 if (bl->loc_type == bp_loc_other)
3832 continue;
3833
3834 if (bl->inserted)
3835 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3836 }
3837
3838 return val;
3839 }
3840
3841 /* Remove the breakpoint location BL from the current address space.
3842 Note that this is used to detach breakpoints from a child fork.
3843 When we get here, the child isn't in the inferior list, and neither
3844 do we have objects to represent its address space --- we should
3845 *not* look at bl->pspace->aspace here. */
3846
3847 static int
3848 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3849 {
3850 int val;
3851
3852 /* BL is never in moribund_locations by our callers. */
3853 gdb_assert (bl->owner != NULL);
3854
3855 /* The type of none suggests that owner is actually deleted.
3856 This should not ever happen. */
3857 gdb_assert (bl->owner->type != bp_none);
3858
3859 if (bl->loc_type == bp_loc_software_breakpoint
3860 || bl->loc_type == bp_loc_hardware_breakpoint)
3861 {
3862 /* "Normal" instruction breakpoint: either the standard
3863 trap-instruction bp (bp_breakpoint), or a
3864 bp_hardware_breakpoint. */
3865
3866 /* First check to see if we have to handle an overlay. */
3867 if (overlay_debugging == ovly_off
3868 || bl->section == NULL
3869 || !(section_is_overlay (bl->section)))
3870 {
3871 /* No overlay handling: just remove the breakpoint. */
3872
3873 /* If we're trying to uninsert a memory breakpoint that we
3874 know is set in a dynamic object that is marked
3875 shlib_disabled, then either the dynamic object was
3876 removed with "remove-symbol-file" or with
3877 "nosharedlibrary". In the former case, we don't know
3878 whether another dynamic object might have loaded over the
3879 breakpoint's address -- the user might well let us know
3880 about it next with add-symbol-file (the whole point of
3881 add-symbol-file is letting the user manually maintain a
3882 list of dynamically loaded objects). If we have the
3883 breakpoint's shadow memory, that is, this is a software
3884 breakpoint managed by GDB, check whether the breakpoint
3885 is still inserted in memory, to avoid overwriting wrong
3886 code with stale saved shadow contents. Note that HW
3887 breakpoints don't have shadow memory, as they're
3888 implemented using a mechanism that is not dependent on
3889 being able to modify the target's memory, and as such
3890 they should always be removed. */
3891 if (bl->shlib_disabled
3892 && bl->target_info.shadow_len != 0
3893 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3894 val = 0;
3895 else
3896 val = bl->owner->remove_location (bl, reason);
3897 }
3898 else
3899 {
3900 /* This breakpoint is in an overlay section.
3901 Did we set a breakpoint at the LMA? */
3902 if (!overlay_events_enabled)
3903 {
3904 /* Yes -- overlay event support is not active, so we
3905 should have set a breakpoint at the LMA. Remove it.
3906 */
3907 /* Ignore any failures: if the LMA is in ROM, we will
3908 have already warned when we failed to insert it. */
3909 if (bl->loc_type == bp_loc_hardware_breakpoint)
3910 target_remove_hw_breakpoint (bl->gdbarch,
3911 &bl->overlay_target_info);
3912 else
3913 target_remove_breakpoint (bl->gdbarch,
3914 &bl->overlay_target_info,
3915 reason);
3916 }
3917 /* Did we set a breakpoint at the VMA?
3918 If so, we will have marked the breakpoint 'inserted'. */
3919 if (bl->inserted)
3920 {
3921 /* Yes -- remove it. Previously we did not bother to
3922 remove the breakpoint if the section had been
3923 unmapped, but let's not rely on that being safe. We
3924 don't know what the overlay manager might do. */
3925
3926 /* However, we should remove *software* breakpoints only
3927 if the section is still mapped, or else we overwrite
3928 wrong code with the saved shadow contents. */
3929 if (bl->loc_type == bp_loc_hardware_breakpoint
3930 || section_is_mapped (bl->section))
3931 val = bl->owner->remove_location (bl, reason);
3932 else
3933 val = 0;
3934 }
3935 else
3936 {
3937 /* No -- not inserted, so no need to remove. No error. */
3938 val = 0;
3939 }
3940 }
3941
3942 /* In some cases, we might not be able to remove a breakpoint in
3943 a shared library that has already been removed, but we have
3944 not yet processed the shlib unload event. Similarly for an
3945 unloaded add-symbol-file object - the user might not yet have
3946 had the chance to remove-symbol-file it. shlib_disabled will
3947 be set if the library/object has already been removed, but
3948 the breakpoint hasn't been uninserted yet, e.g., after
3949 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3950 always-inserted mode. */
3951 if (val
3952 && (bl->loc_type == bp_loc_software_breakpoint
3953 && (bl->shlib_disabled
3954 || solib_name_from_address (bl->pspace, bl->address)
3955 || shared_objfile_contains_address_p (bl->pspace,
3956 bl->address))))
3957 val = 0;
3958
3959 if (val)
3960 return val;
3961 bl->inserted = (reason == DETACH_BREAKPOINT);
3962 }
3963 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3964 {
3965 bl->inserted = (reason == DETACH_BREAKPOINT);
3966 bl->owner->remove_location (bl, reason);
3967
3968 /* Failure to remove any of the hardware watchpoints comes here. */
3969 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3970 warning (_("Could not remove hardware watchpoint %d."),
3971 bl->owner->number);
3972 }
3973 else if (bl->owner->type == bp_catchpoint
3974 && breakpoint_enabled (bl->owner)
3975 && !bl->duplicate)
3976 {
3977 val = bl->owner->remove_location (bl, reason);
3978 if (val)
3979 return val;
3980
3981 bl->inserted = (reason == DETACH_BREAKPOINT);
3982 }
3983
3984 return 0;
3985 }
3986
3987 static int
3988 remove_breakpoint (struct bp_location *bl)
3989 {
3990 /* BL is never in moribund_locations by our callers. */
3991 gdb_assert (bl->owner != NULL);
3992
3993 /* The type of none suggests that owner is actually deleted.
3994 This should not ever happen. */
3995 gdb_assert (bl->owner->type != bp_none);
3996
3997 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3998
3999 switch_to_program_space_and_thread (bl->pspace);
4000
4001 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4002 }
4003
4004 /* Clear the "inserted" flag in all breakpoints. */
4005
4006 void
4007 mark_breakpoints_out (void)
4008 {
4009 for (bp_location *bl : all_bp_locations ())
4010 if (bl->pspace == current_program_space)
4011 bl->inserted = 0;
4012 }
4013
4014 /* Clear the "inserted" flag in all breakpoints and delete any
4015 breakpoints which should go away between runs of the program.
4016
4017 Plus other such housekeeping that has to be done for breakpoints
4018 between runs.
4019
4020 Note: this function gets called at the end of a run (by
4021 generic_mourn_inferior) and when a run begins (by
4022 init_wait_for_inferior). */
4023
4024
4025
4026 void
4027 breakpoint_init_inferior (enum inf_context context)
4028 {
4029 struct program_space *pspace = current_program_space;
4030
4031 /* If breakpoint locations are shared across processes, then there's
4032 nothing to do. */
4033 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4034 return;
4035
4036 mark_breakpoints_out ();
4037
4038 for (breakpoint *b : all_breakpoints_safe ())
4039 {
4040 if (b->loc && b->loc->pspace != pspace)
4041 continue;
4042
4043 switch (b->type)
4044 {
4045 case bp_call_dummy:
4046 case bp_longjmp_call_dummy:
4047
4048 /* If the call dummy breakpoint is at the entry point it will
4049 cause problems when the inferior is rerun, so we better get
4050 rid of it. */
4051
4052 case bp_watchpoint_scope:
4053
4054 /* Also get rid of scope breakpoints. */
4055
4056 case bp_shlib_event:
4057
4058 /* Also remove solib event breakpoints. Their addresses may
4059 have changed since the last time we ran the program.
4060 Actually we may now be debugging against different target;
4061 and so the solib backend that installed this breakpoint may
4062 not be used in by the target. E.g.,
4063
4064 (gdb) file prog-linux
4065 (gdb) run # native linux target
4066 ...
4067 (gdb) kill
4068 (gdb) file prog-win.exe
4069 (gdb) tar rem :9999 # remote Windows gdbserver.
4070 */
4071
4072 case bp_step_resume:
4073
4074 /* Also remove step-resume breakpoints. */
4075
4076 case bp_single_step:
4077
4078 /* Also remove single-step breakpoints. */
4079
4080 delete_breakpoint (b);
4081 break;
4082
4083 case bp_watchpoint:
4084 case bp_hardware_watchpoint:
4085 case bp_read_watchpoint:
4086 case bp_access_watchpoint:
4087 {
4088 struct watchpoint *w = (struct watchpoint *) b;
4089
4090 /* Likewise for watchpoints on local expressions. */
4091 if (w->exp_valid_block != NULL)
4092 delete_breakpoint (b);
4093 else
4094 {
4095 /* Get rid of existing locations, which are no longer
4096 valid. New ones will be created in
4097 update_watchpoint, when the inferior is restarted.
4098 The next update_global_location_list call will
4099 garbage collect them. */
4100 b->loc = NULL;
4101
4102 if (context == inf_starting)
4103 {
4104 /* Reset val field to force reread of starting value in
4105 insert_breakpoints. */
4106 w->val.reset (nullptr);
4107 w->val_valid = false;
4108 }
4109 }
4110 }
4111 break;
4112 default:
4113 break;
4114 }
4115 }
4116
4117 /* Get rid of the moribund locations. */
4118 for (bp_location *bl : moribund_locations)
4119 decref_bp_location (&bl);
4120 moribund_locations.clear ();
4121 }
4122
4123 /* These functions concern about actual breakpoints inserted in the
4124 target --- to e.g. check if we need to do decr_pc adjustment or if
4125 we need to hop over the bkpt --- so we check for address space
4126 match, not program space. */
4127
4128 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4129 exists at PC. It returns ordinary_breakpoint_here if it's an
4130 ordinary breakpoint, or permanent_breakpoint_here if it's a
4131 permanent breakpoint.
4132 - When continuing from a location with an ordinary breakpoint, we
4133 actually single step once before calling insert_breakpoints.
4134 - When continuing from a location with a permanent breakpoint, we
4135 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4136 the target, to advance the PC past the breakpoint. */
4137
4138 enum breakpoint_here
4139 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4140 {
4141 int any_breakpoint_here = 0;
4142
4143 for (bp_location *bl : all_bp_locations ())
4144 {
4145 if (bl->loc_type != bp_loc_software_breakpoint
4146 && bl->loc_type != bp_loc_hardware_breakpoint)
4147 continue;
4148
4149 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4150 if ((breakpoint_enabled (bl->owner)
4151 || bl->permanent)
4152 && breakpoint_location_address_match (bl, aspace, pc))
4153 {
4154 if (overlay_debugging
4155 && section_is_overlay (bl->section)
4156 && !section_is_mapped (bl->section))
4157 continue; /* unmapped overlay -- can't be a match */
4158 else if (bl->permanent)
4159 return permanent_breakpoint_here;
4160 else
4161 any_breakpoint_here = 1;
4162 }
4163 }
4164
4165 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4166 }
4167
4168 /* See breakpoint.h. */
4169
4170 int
4171 breakpoint_in_range_p (const address_space *aspace,
4172 CORE_ADDR addr, ULONGEST len)
4173 {
4174 for (bp_location *bl : all_bp_locations ())
4175 {
4176 if (bl->loc_type != bp_loc_software_breakpoint
4177 && bl->loc_type != bp_loc_hardware_breakpoint)
4178 continue;
4179
4180 if ((breakpoint_enabled (bl->owner)
4181 || bl->permanent)
4182 && breakpoint_location_address_range_overlap (bl, aspace,
4183 addr, len))
4184 {
4185 if (overlay_debugging
4186 && section_is_overlay (bl->section)
4187 && !section_is_mapped (bl->section))
4188 {
4189 /* Unmapped overlay -- can't be a match. */
4190 continue;
4191 }
4192
4193 return 1;
4194 }
4195 }
4196
4197 return 0;
4198 }
4199
4200 /* Return true if there's a moribund breakpoint at PC. */
4201
4202 int
4203 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4204 {
4205 for (bp_location *loc : moribund_locations)
4206 if (breakpoint_location_address_match (loc, aspace, pc))
4207 return 1;
4208
4209 return 0;
4210 }
4211
4212 /* Returns non-zero iff BL is inserted at PC, in address space
4213 ASPACE. */
4214
4215 static int
4216 bp_location_inserted_here_p (struct bp_location *bl,
4217 const address_space *aspace, CORE_ADDR pc)
4218 {
4219 if (bl->inserted
4220 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4221 aspace, pc))
4222 {
4223 if (overlay_debugging
4224 && section_is_overlay (bl->section)
4225 && !section_is_mapped (bl->section))
4226 return 0; /* unmapped overlay -- can't be a match */
4227 else
4228 return 1;
4229 }
4230 return 0;
4231 }
4232
4233 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4234
4235 int
4236 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4237 {
4238 for (bp_location *bl : all_bp_locations_at_addr (pc))
4239 {
4240 if (bl->loc_type != bp_loc_software_breakpoint
4241 && bl->loc_type != bp_loc_hardware_breakpoint)
4242 continue;
4243
4244 if (bp_location_inserted_here_p (bl, aspace, pc))
4245 return 1;
4246 }
4247 return 0;
4248 }
4249
4250 /* This function returns non-zero iff there is a software breakpoint
4251 inserted at PC. */
4252
4253 int
4254 software_breakpoint_inserted_here_p (const address_space *aspace,
4255 CORE_ADDR pc)
4256 {
4257 for (bp_location *bl : all_bp_locations_at_addr (pc))
4258 {
4259 if (bl->loc_type != bp_loc_software_breakpoint)
4260 continue;
4261
4262 if (bp_location_inserted_here_p (bl, aspace, pc))
4263 return 1;
4264 }
4265
4266 return 0;
4267 }
4268
4269 /* See breakpoint.h. */
4270
4271 int
4272 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4273 CORE_ADDR pc)
4274 {
4275 for (bp_location *bl : all_bp_locations_at_addr (pc))
4276 {
4277 if (bl->loc_type != bp_loc_hardware_breakpoint)
4278 continue;
4279
4280 if (bp_location_inserted_here_p (bl, aspace, pc))
4281 return 1;
4282 }
4283
4284 return 0;
4285 }
4286
4287 int
4288 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4289 CORE_ADDR addr, ULONGEST len)
4290 {
4291 for (breakpoint *bpt : all_breakpoints ())
4292 {
4293 if (bpt->type != bp_hardware_watchpoint
4294 && bpt->type != bp_access_watchpoint)
4295 continue;
4296
4297 if (!breakpoint_enabled (bpt))
4298 continue;
4299
4300 for (bp_location *loc : bpt->locations ())
4301 if (loc->pspace->aspace == aspace && loc->inserted)
4302 {
4303 CORE_ADDR l, h;
4304
4305 /* Check for intersection. */
4306 l = std::max<CORE_ADDR> (loc->address, addr);
4307 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4308 if (l < h)
4309 return 1;
4310 }
4311 }
4312 return 0;
4313 }
4314
4315 /* See breakpoint.h. */
4316
4317 bool
4318 is_catchpoint (struct breakpoint *b)
4319 {
4320 return (b->type == bp_catchpoint);
4321 }
4322
4323 /* Clear a bpstat so that it says we are not at any breakpoint.
4324 Also free any storage that is part of a bpstat. */
4325
4326 void
4327 bpstat_clear (bpstat **bsp)
4328 {
4329 bpstat *p;
4330 bpstat *q;
4331
4332 if (bsp == 0)
4333 return;
4334 p = *bsp;
4335 while (p != NULL)
4336 {
4337 q = p->next;
4338 delete p;
4339 p = q;
4340 }
4341 *bsp = NULL;
4342 }
4343
4344 bpstat::bpstat (const bpstat &other)
4345 : next (NULL),
4346 bp_location_at (other.bp_location_at),
4347 breakpoint_at (other.breakpoint_at),
4348 commands (other.commands),
4349 print (other.print),
4350 stop (other.stop),
4351 print_it (other.print_it)
4352 {
4353 if (other.old_val != NULL)
4354 old_val = release_value (value_copy (other.old_val.get ()));
4355 }
4356
4357 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4358 is part of the bpstat is copied as well. */
4359
4360 bpstat *
4361 bpstat_copy (bpstat *bs)
4362 {
4363 bpstat *p = nullptr;
4364 bpstat *tmp;
4365 bpstat *retval = nullptr;
4366
4367 if (bs == NULL)
4368 return bs;
4369
4370 for (; bs != NULL; bs = bs->next)
4371 {
4372 tmp = new bpstat (*bs);
4373
4374 if (p == NULL)
4375 /* This is the first thing in the chain. */
4376 retval = tmp;
4377 else
4378 p->next = tmp;
4379 p = tmp;
4380 }
4381 p->next = NULL;
4382 return retval;
4383 }
4384
4385 /* Find the bpstat associated with this breakpoint. */
4386
4387 bpstat *
4388 bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
4389 {
4390 if (bsp == NULL)
4391 return NULL;
4392
4393 for (; bsp != NULL; bsp = bsp->next)
4394 {
4395 if (bsp->breakpoint_at == breakpoint)
4396 return bsp;
4397 }
4398 return NULL;
4399 }
4400
4401 /* See breakpoint.h. */
4402
4403 bool
4404 bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
4405 {
4406 for (; bsp != NULL; bsp = bsp->next)
4407 {
4408 if (bsp->breakpoint_at == NULL)
4409 {
4410 /* A moribund location can never explain a signal other than
4411 GDB_SIGNAL_TRAP. */
4412 if (sig == GDB_SIGNAL_TRAP)
4413 return true;
4414 }
4415 else
4416 {
4417 if (bsp->breakpoint_at->explains_signal (sig))
4418 return true;
4419 }
4420 }
4421
4422 return false;
4423 }
4424
4425 /* Put in *NUM the breakpoint number of the first breakpoint we are
4426 stopped at. *BSP upon return is a bpstat which points to the
4427 remaining breakpoints stopped at (but which is not guaranteed to be
4428 good for anything but further calls to bpstat_num).
4429
4430 Return 0 if passed a bpstat which does not indicate any breakpoints.
4431 Return -1 if stopped at a breakpoint that has been deleted since
4432 we set it.
4433 Return 1 otherwise. */
4434
4435 int
4436 bpstat_num (bpstat **bsp, int *num)
4437 {
4438 struct breakpoint *b;
4439
4440 if ((*bsp) == NULL)
4441 return 0; /* No more breakpoint values */
4442
4443 /* We assume we'll never have several bpstats that correspond to a
4444 single breakpoint -- otherwise, this function might return the
4445 same number more than once and this will look ugly. */
4446 b = (*bsp)->breakpoint_at;
4447 *bsp = (*bsp)->next;
4448 if (b == NULL)
4449 return -1; /* breakpoint that's been deleted since */
4450
4451 *num = b->number; /* We have its number */
4452 return 1;
4453 }
4454
4455 /* See breakpoint.h. */
4456
4457 void
4458 bpstat_clear_actions (void)
4459 {
4460 bpstat *bs;
4461
4462 if (inferior_ptid == null_ptid)
4463 return;
4464
4465 thread_info *tp = inferior_thread ();
4466 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4467 {
4468 bs->commands = NULL;
4469 bs->old_val.reset (nullptr);
4470 }
4471 }
4472
4473 /* Called when a command is about to proceed the inferior. */
4474
4475 static void
4476 breakpoint_about_to_proceed (void)
4477 {
4478 if (inferior_ptid != null_ptid)
4479 {
4480 struct thread_info *tp = inferior_thread ();
4481
4482 /* Allow inferior function calls in breakpoint commands to not
4483 interrupt the command list. When the call finishes
4484 successfully, the inferior will be standing at the same
4485 breakpoint as if nothing happened. */
4486 if (tp->control.in_infcall)
4487 return;
4488 }
4489
4490 breakpoint_proceeded = 1;
4491 }
4492
4493 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4494 or its equivalent. */
4495
4496 static int
4497 command_line_is_silent (struct command_line *cmd)
4498 {
4499 return cmd && (strcmp ("silent", cmd->line) == 0);
4500 }
4501
4502 /* Execute all the commands associated with all the breakpoints at
4503 this location. Any of these commands could cause the process to
4504 proceed beyond this point, etc. We look out for such changes by
4505 checking the global "breakpoint_proceeded" after each command.
4506
4507 Returns true if a breakpoint command resumed the inferior. In that
4508 case, it is the caller's responsibility to recall it again with the
4509 bpstat of the current thread. */
4510
4511 static int
4512 bpstat_do_actions_1 (bpstat **bsp)
4513 {
4514 bpstat *bs;
4515 int again = 0;
4516
4517 /* Avoid endless recursion if a `source' command is contained
4518 in bs->commands. */
4519 if (executing_breakpoint_commands)
4520 return 0;
4521
4522 scoped_restore save_executing
4523 = make_scoped_restore (&executing_breakpoint_commands, 1);
4524
4525 scoped_restore preventer = prevent_dont_repeat ();
4526
4527 /* This pointer will iterate over the list of bpstat's. */
4528 bs = *bsp;
4529
4530 breakpoint_proceeded = 0;
4531 for (; bs != NULL; bs = bs->next)
4532 {
4533 struct command_line *cmd = NULL;
4534
4535 /* Take ownership of the BSP's command tree, if it has one.
4536
4537 The command tree could legitimately contain commands like
4538 'step' and 'next', which call clear_proceed_status, which
4539 frees stop_bpstat's command tree. To make sure this doesn't
4540 free the tree we're executing out from under us, we need to
4541 take ownership of the tree ourselves. Since a given bpstat's
4542 commands are only executed once, we don't need to copy it; we
4543 can clear the pointer in the bpstat, and make sure we free
4544 the tree when we're done. */
4545 counted_command_line ccmd = bs->commands;
4546 bs->commands = NULL;
4547 if (ccmd != NULL)
4548 cmd = ccmd.get ();
4549 if (command_line_is_silent (cmd))
4550 {
4551 /* The action has been already done by bpstat_stop_status. */
4552 cmd = cmd->next;
4553 }
4554
4555 while (cmd != NULL)
4556 {
4557 execute_control_command (cmd);
4558
4559 if (breakpoint_proceeded)
4560 break;
4561 else
4562 cmd = cmd->next;
4563 }
4564
4565 if (breakpoint_proceeded)
4566 {
4567 if (current_ui->async)
4568 /* If we are in async mode, then the target might be still
4569 running, not stopped at any breakpoint, so nothing for
4570 us to do here -- just return to the event loop. */
4571 ;
4572 else
4573 /* In sync mode, when execute_control_command returns
4574 we're already standing on the next breakpoint.
4575 Breakpoint commands for that stop were not run, since
4576 execute_command does not run breakpoint commands --
4577 only command_line_handler does, but that one is not
4578 involved in execution of breakpoint commands. So, we
4579 can now execute breakpoint commands. It should be
4580 noted that making execute_command do bpstat actions is
4581 not an option -- in this case we'll have recursive
4582 invocation of bpstat for each breakpoint with a
4583 command, and can easily blow up GDB stack. Instead, we
4584 return true, which will trigger the caller to recall us
4585 with the new stop_bpstat. */
4586 again = 1;
4587 break;
4588 }
4589 }
4590 return again;
4591 }
4592
4593 /* Helper for bpstat_do_actions. Get the current thread, if there's
4594 one, is alive and has execution. Return NULL otherwise. */
4595
4596 static thread_info *
4597 get_bpstat_thread ()
4598 {
4599 if (inferior_ptid == null_ptid || !target_has_execution ())
4600 return NULL;
4601
4602 thread_info *tp = inferior_thread ();
4603 if (tp->state == THREAD_EXITED || tp->executing ())
4604 return NULL;
4605 return tp;
4606 }
4607
4608 void
4609 bpstat_do_actions (void)
4610 {
4611 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4612 thread_info *tp;
4613
4614 /* Do any commands attached to breakpoint we are stopped at. */
4615 while ((tp = get_bpstat_thread ()) != NULL)
4616 {
4617 /* Since in sync mode, bpstat_do_actions may resume the
4618 inferior, and only return when it is stopped at the next
4619 breakpoint, we keep doing breakpoint actions until it returns
4620 false to indicate the inferior was not resumed. */
4621 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4622 break;
4623 }
4624
4625 cleanup_if_error.release ();
4626 }
4627
4628 /* Print out the (old or new) value associated with a watchpoint. */
4629
4630 static void
4631 watchpoint_value_print (struct value *val, struct ui_file *stream)
4632 {
4633 if (val == NULL)
4634 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4635 else
4636 {
4637 struct value_print_options opts;
4638 get_user_print_options (&opts);
4639 value_print (val, stream, &opts);
4640 }
4641 }
4642
4643 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4644 debugging multiple threads. */
4645
4646 void
4647 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4648 {
4649 if (uiout->is_mi_like_p ())
4650 return;
4651
4652 uiout->text ("\n");
4653
4654 if (show_thread_that_caused_stop ())
4655 {
4656 struct thread_info *thr = inferior_thread ();
4657
4658 uiout->text ("Thread ");
4659 uiout->field_string ("thread-id", print_thread_id (thr));
4660
4661 const char *name = thread_name (thr);
4662 if (name != NULL)
4663 {
4664 uiout->text (" \"");
4665 uiout->field_string ("name", name);
4666 uiout->text ("\"");
4667 }
4668
4669 uiout->text (" hit ");
4670 }
4671 }
4672
4673 /* Generic routine for printing messages indicating why we
4674 stopped. The behavior of this function depends on the value
4675 'print_it' in the bpstat structure. Under some circumstances we
4676 may decide not to print anything here and delegate the task to
4677 normal_stop(). */
4678
4679 static enum print_stop_action
4680 print_bp_stop_message (bpstat *bs)
4681 {
4682 switch (bs->print_it)
4683 {
4684 case print_it_noop:
4685 /* Nothing should be printed for this bpstat entry. */
4686 return PRINT_UNKNOWN;
4687 break;
4688
4689 case print_it_done:
4690 /* We still want to print the frame, but we already printed the
4691 relevant messages. */
4692 return PRINT_SRC_AND_LOC;
4693 break;
4694
4695 case print_it_normal:
4696 {
4697 struct breakpoint *b = bs->breakpoint_at;
4698
4699 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4700 which has since been deleted. */
4701 if (b == NULL)
4702 return PRINT_UNKNOWN;
4703
4704 /* Normal case. Call the breakpoint's print_it method. */
4705 return b->print_it (bs);
4706 }
4707 break;
4708
4709 default:
4710 internal_error (__FILE__, __LINE__,
4711 _("print_bp_stop_message: unrecognized enum value"));
4712 break;
4713 }
4714 }
4715
4716 /* See breakpoint.h. */
4717
4718 void
4719 print_solib_event (bool is_catchpoint)
4720 {
4721 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4722 bool any_added = !current_program_space->added_solibs.empty ();
4723
4724 if (!is_catchpoint)
4725 {
4726 if (any_added || any_deleted)
4727 current_uiout->text (_("Stopped due to shared library event:\n"));
4728 else
4729 current_uiout->text (_("Stopped due to shared library event (no "
4730 "libraries added or removed)\n"));
4731 }
4732
4733 if (current_uiout->is_mi_like_p ())
4734 current_uiout->field_string ("reason",
4735 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4736
4737 if (any_deleted)
4738 {
4739 current_uiout->text (_(" Inferior unloaded "));
4740 ui_out_emit_list list_emitter (current_uiout, "removed");
4741 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4742 {
4743 const std::string &name = current_program_space->deleted_solibs[ix];
4744
4745 if (ix > 0)
4746 current_uiout->text (" ");
4747 current_uiout->field_string ("library", name);
4748 current_uiout->text ("\n");
4749 }
4750 }
4751
4752 if (any_added)
4753 {
4754 current_uiout->text (_(" Inferior loaded "));
4755 ui_out_emit_list list_emitter (current_uiout, "added");
4756 bool first = true;
4757 for (so_list *iter : current_program_space->added_solibs)
4758 {
4759 if (!first)
4760 current_uiout->text (" ");
4761 first = false;
4762 current_uiout->field_string ("library", iter->so_name);
4763 current_uiout->text ("\n");
4764 }
4765 }
4766 }
4767
4768 /* Print a message indicating what happened. This is called from
4769 normal_stop(). The input to this routine is the head of the bpstat
4770 list - a list of the eventpoints that caused this stop. KIND is
4771 the target_waitkind for the stopping event. This
4772 routine calls the generic print routine for printing a message
4773 about reasons for stopping. This will print (for example) the
4774 "Breakpoint n," part of the output. The return value of this
4775 routine is one of:
4776
4777 PRINT_UNKNOWN: Means we printed nothing.
4778 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4779 code to print the location. An example is
4780 "Breakpoint 1, " which should be followed by
4781 the location.
4782 PRINT_SRC_ONLY: Means we printed something, but there is no need
4783 to also print the location part of the message.
4784 An example is the catch/throw messages, which
4785 don't require a location appended to the end.
4786 PRINT_NOTHING: We have done some printing and we don't need any
4787 further info to be printed. */
4788
4789 enum print_stop_action
4790 bpstat_print (bpstat *bs, int kind)
4791 {
4792 enum print_stop_action val;
4793
4794 /* Maybe another breakpoint in the chain caused us to stop.
4795 (Currently all watchpoints go on the bpstat whether hit or not.
4796 That probably could (should) be changed, provided care is taken
4797 with respect to bpstat_explains_signal). */
4798 for (; bs; bs = bs->next)
4799 {
4800 val = print_bp_stop_message (bs);
4801 if (val == PRINT_SRC_ONLY
4802 || val == PRINT_SRC_AND_LOC
4803 || val == PRINT_NOTHING)
4804 return val;
4805 }
4806
4807 /* If we had hit a shared library event breakpoint,
4808 print_bp_stop_message would print out this message. If we hit an
4809 OS-level shared library event, do the same thing. */
4810 if (kind == TARGET_WAITKIND_LOADED)
4811 {
4812 print_solib_event (false);
4813 return PRINT_NOTHING;
4814 }
4815
4816 /* We reached the end of the chain, or we got a null BS to start
4817 with and nothing was printed. */
4818 return PRINT_UNKNOWN;
4819 }
4820
4821 /* Evaluate the boolean expression EXP and return the result. */
4822
4823 static bool
4824 breakpoint_cond_eval (expression *exp)
4825 {
4826 struct value *mark = value_mark ();
4827 bool res = value_true (evaluate_expression (exp));
4828
4829 value_free_to_mark (mark);
4830 return res;
4831 }
4832
4833 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4834
4835 bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
4836 : next (NULL),
4837 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4838 breakpoint_at (bl->owner),
4839 commands (NULL),
4840 print (0),
4841 stop (0),
4842 print_it (print_it_normal)
4843 {
4844 **bs_link_pointer = this;
4845 *bs_link_pointer = &next;
4846 }
4847
4848 bpstat::bpstat ()
4849 : next (NULL),
4850 breakpoint_at (NULL),
4851 commands (NULL),
4852 print (0),
4853 stop (0),
4854 print_it (print_it_normal)
4855 {
4856 }
4857 \f
4858 /* The target has stopped with waitstatus WS. Check if any hardware
4859 watchpoints have triggered, according to the target. */
4860
4861 int
4862 watchpoints_triggered (const target_waitstatus &ws)
4863 {
4864 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4865 CORE_ADDR addr;
4866
4867 if (!stopped_by_watchpoint)
4868 {
4869 /* We were not stopped by a watchpoint. Mark all watchpoints
4870 as not triggered. */
4871 for (breakpoint *b : all_breakpoints ())
4872 if (is_hardware_watchpoint (b))
4873 {
4874 struct watchpoint *w = (struct watchpoint *) b;
4875
4876 w->watchpoint_triggered = watch_triggered_no;
4877 }
4878
4879 return 0;
4880 }
4881
4882 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4883 {
4884 /* We were stopped by a watchpoint, but we don't know where.
4885 Mark all watchpoints as unknown. */
4886 for (breakpoint *b : all_breakpoints ())
4887 if (is_hardware_watchpoint (b))
4888 {
4889 struct watchpoint *w = (struct watchpoint *) b;
4890
4891 w->watchpoint_triggered = watch_triggered_unknown;
4892 }
4893
4894 return 1;
4895 }
4896
4897 /* The target could report the data address. Mark watchpoints
4898 affected by this data address as triggered, and all others as not
4899 triggered. */
4900
4901 for (breakpoint *b : all_breakpoints ())
4902 if (is_hardware_watchpoint (b))
4903 {
4904 struct watchpoint *w = (struct watchpoint *) b;
4905
4906 w->watchpoint_triggered = watch_triggered_no;
4907 for (bp_location *loc : b->locations ())
4908 {
4909 if (is_masked_watchpoint (b))
4910 {
4911 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4912 CORE_ADDR start = loc->address & w->hw_wp_mask;
4913
4914 if (newaddr == start)
4915 {
4916 w->watchpoint_triggered = watch_triggered_yes;
4917 break;
4918 }
4919 }
4920 /* Exact match not required. Within range is sufficient. */
4921 else if (target_watchpoint_addr_within_range
4922 (current_inferior ()->top_target (), addr, loc->address,
4923 loc->length))
4924 {
4925 w->watchpoint_triggered = watch_triggered_yes;
4926 break;
4927 }
4928 }
4929 }
4930
4931 return 1;
4932 }
4933
4934 /* Possible return values for watchpoint_check. */
4935 enum wp_check_result
4936 {
4937 /* The watchpoint has been deleted. */
4938 WP_DELETED = 1,
4939
4940 /* The value has changed. */
4941 WP_VALUE_CHANGED = 2,
4942
4943 /* The value has not changed. */
4944 WP_VALUE_NOT_CHANGED = 3,
4945
4946 /* Ignore this watchpoint, no matter if the value changed or not. */
4947 WP_IGNORE = 4,
4948 };
4949
4950 #define BP_TEMPFLAG 1
4951 #define BP_HARDWAREFLAG 2
4952
4953 /* Evaluate watchpoint condition expression and check if its value
4954 changed. */
4955
4956 static wp_check_result
4957 watchpoint_check (bpstat *bs)
4958 {
4959 struct watchpoint *b;
4960 struct frame_info *fr;
4961 int within_current_scope;
4962
4963 /* BS is built from an existing struct breakpoint. */
4964 gdb_assert (bs->breakpoint_at != NULL);
4965 b = (struct watchpoint *) bs->breakpoint_at;
4966
4967 /* If this is a local watchpoint, we only want to check if the
4968 watchpoint frame is in scope if the current thread is the thread
4969 that was used to create the watchpoint. */
4970 if (!watchpoint_in_thread_scope (b))
4971 return WP_IGNORE;
4972
4973 if (b->exp_valid_block == NULL)
4974 within_current_scope = 1;
4975 else
4976 {
4977 struct frame_info *frame = get_current_frame ();
4978 struct gdbarch *frame_arch = get_frame_arch (frame);
4979 CORE_ADDR frame_pc = get_frame_pc (frame);
4980
4981 /* stack_frame_destroyed_p() returns a non-zero value if we're
4982 still in the function but the stack frame has already been
4983 invalidated. Since we can't rely on the values of local
4984 variables after the stack has been destroyed, we are treating
4985 the watchpoint in that state as `not changed' without further
4986 checking. Don't mark watchpoints as changed if the current
4987 frame is in an epilogue - even if they are in some other
4988 frame, our view of the stack is likely to be wrong and
4989 frame_find_by_id could error out. */
4990 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4991 return WP_IGNORE;
4992
4993 fr = frame_find_by_id (b->watchpoint_frame);
4994 within_current_scope = (fr != NULL);
4995
4996 /* If we've gotten confused in the unwinder, we might have
4997 returned a frame that can't describe this variable. */
4998 if (within_current_scope)
4999 {
5000 struct symbol *function;
5001
5002 function = get_frame_function (fr);
5003 if (function == NULL
5004 || !contained_in (b->exp_valid_block, function->value_block ()))
5005 within_current_scope = 0;
5006 }
5007
5008 if (within_current_scope)
5009 /* If we end up stopping, the current frame will get selected
5010 in normal_stop. So this call to select_frame won't affect
5011 the user. */
5012 select_frame (fr);
5013 }
5014
5015 if (within_current_scope)
5016 {
5017 /* We use value_{,free_to_}mark because it could be a *long*
5018 time before we return to the command level and call
5019 free_all_values. We can't call free_all_values because we
5020 might be in the middle of evaluating a function call. */
5021
5022 struct value *mark;
5023 struct value *new_val;
5024
5025 if (is_masked_watchpoint (b))
5026 /* Since we don't know the exact trigger address (from
5027 stopped_data_address), just tell the user we've triggered
5028 a mask watchpoint. */
5029 return WP_VALUE_CHANGED;
5030
5031 mark = value_mark ();
5032 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5033 NULL, NULL, false);
5034
5035 if (b->val_bitsize != 0)
5036 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5037
5038 /* We use value_equal_contents instead of value_equal because
5039 the latter coerces an array to a pointer, thus comparing just
5040 the address of the array instead of its contents. This is
5041 not what we want. */
5042 if ((b->val != NULL) != (new_val != NULL)
5043 || (b->val != NULL && !value_equal_contents (b->val.get (),
5044 new_val)))
5045 {
5046 bs->old_val = b->val;
5047 b->val = release_value (new_val);
5048 b->val_valid = true;
5049 if (new_val != NULL)
5050 value_free_to_mark (mark);
5051 return WP_VALUE_CHANGED;
5052 }
5053 else
5054 {
5055 /* Nothing changed. */
5056 value_free_to_mark (mark);
5057 return WP_VALUE_NOT_CHANGED;
5058 }
5059 }
5060 else
5061 {
5062 /* This seems like the only logical thing to do because
5063 if we temporarily ignored the watchpoint, then when
5064 we reenter the block in which it is valid it contains
5065 garbage (in the case of a function, it may have two
5066 garbage values, one before and one after the prologue).
5067 So we can't even detect the first assignment to it and
5068 watch after that (since the garbage may or may not equal
5069 the first value assigned). */
5070 /* We print all the stop information in
5071 breakpointprint_it, but in this case, by the time we
5072 call breakpoint->print_it this bp will be deleted
5073 already. So we have no choice but print the information
5074 here. */
5075
5076 SWITCH_THRU_ALL_UIS ()
5077 {
5078 struct ui_out *uiout = current_uiout;
5079
5080 if (uiout->is_mi_like_p ())
5081 uiout->field_string
5082 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5083 uiout->message ("\nWatchpoint %pF deleted because the program has "
5084 "left the block in\n"
5085 "which its expression is valid.\n",
5086 signed_field ("wpnum", b->number));
5087 }
5088
5089 /* Make sure the watchpoint's commands aren't executed. */
5090 b->commands = NULL;
5091 watchpoint_del_at_next_stop (b);
5092
5093 return WP_DELETED;
5094 }
5095 }
5096
5097 /* Return true if it looks like target has stopped due to hitting
5098 breakpoint location BL. This function does not check if we should
5099 stop, only if BL explains the stop. */
5100
5101 static int
5102 bpstat_check_location (const struct bp_location *bl,
5103 const address_space *aspace, CORE_ADDR bp_addr,
5104 const target_waitstatus &ws)
5105 {
5106 struct breakpoint *b = bl->owner;
5107
5108 /* BL is from an existing breakpoint. */
5109 gdb_assert (b != NULL);
5110
5111 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
5112 }
5113
5114 /* Determine if the watched values have actually changed, and we
5115 should stop. If not, set BS->stop to 0. */
5116
5117 static void
5118 bpstat_check_watchpoint (bpstat *bs)
5119 {
5120 const struct bp_location *bl;
5121 struct watchpoint *b;
5122
5123 /* BS is built for existing struct breakpoint. */
5124 bl = bs->bp_location_at.get ();
5125 gdb_assert (bl != NULL);
5126 b = (struct watchpoint *) bs->breakpoint_at;
5127 gdb_assert (b != NULL);
5128
5129 {
5130 int must_check_value = 0;
5131
5132 if (b->type == bp_watchpoint)
5133 /* For a software watchpoint, we must always check the
5134 watched value. */
5135 must_check_value = 1;
5136 else if (b->watchpoint_triggered == watch_triggered_yes)
5137 /* We have a hardware watchpoint (read, write, or access)
5138 and the target earlier reported an address watched by
5139 this watchpoint. */
5140 must_check_value = 1;
5141 else if (b->watchpoint_triggered == watch_triggered_unknown
5142 && b->type == bp_hardware_watchpoint)
5143 /* We were stopped by a hardware watchpoint, but the target could
5144 not report the data address. We must check the watchpoint's
5145 value. Access and read watchpoints are out of luck; without
5146 a data address, we can't figure it out. */
5147 must_check_value = 1;
5148
5149 if (must_check_value)
5150 {
5151 wp_check_result e;
5152
5153 try
5154 {
5155 e = watchpoint_check (bs);
5156 }
5157 catch (const gdb_exception &ex)
5158 {
5159 exception_fprintf (gdb_stderr, ex,
5160 "Error evaluating expression "
5161 "for watchpoint %d\n",
5162 b->number);
5163
5164 SWITCH_THRU_ALL_UIS ()
5165 {
5166 gdb_printf (_("Watchpoint %d deleted.\n"),
5167 b->number);
5168 }
5169 watchpoint_del_at_next_stop (b);
5170 e = WP_DELETED;
5171 }
5172
5173 switch (e)
5174 {
5175 case WP_DELETED:
5176 /* We've already printed what needs to be printed. */
5177 bs->print_it = print_it_done;
5178 /* Stop. */
5179 break;
5180 case WP_IGNORE:
5181 bs->print_it = print_it_noop;
5182 bs->stop = 0;
5183 break;
5184 case WP_VALUE_CHANGED:
5185 if (b->type == bp_read_watchpoint)
5186 {
5187 /* There are two cases to consider here:
5188
5189 1. We're watching the triggered memory for reads.
5190 In that case, trust the target, and always report
5191 the watchpoint hit to the user. Even though
5192 reads don't cause value changes, the value may
5193 have changed since the last time it was read, and
5194 since we're not trapping writes, we will not see
5195 those, and as such we should ignore our notion of
5196 old value.
5197
5198 2. We're watching the triggered memory for both
5199 reads and writes. There are two ways this may
5200 happen:
5201
5202 2.1. This is a target that can't break on data
5203 reads only, but can break on accesses (reads or
5204 writes), such as e.g., x86. We detect this case
5205 at the time we try to insert read watchpoints.
5206
5207 2.2. Otherwise, the target supports read
5208 watchpoints, but, the user set an access or write
5209 watchpoint watching the same memory as this read
5210 watchpoint.
5211
5212 If we're watching memory writes as well as reads,
5213 ignore watchpoint hits when we find that the
5214 value hasn't changed, as reads don't cause
5215 changes. This still gives false positives when
5216 the program writes the same value to memory as
5217 what there was already in memory (we will confuse
5218 it for a read), but it's much better than
5219 nothing. */
5220
5221 int other_write_watchpoint = 0;
5222
5223 if (bl->watchpoint_type == hw_read)
5224 {
5225 for (breakpoint *other_b : all_breakpoints ())
5226 if (other_b->type == bp_hardware_watchpoint
5227 || other_b->type == bp_access_watchpoint)
5228 {
5229 struct watchpoint *other_w =
5230 (struct watchpoint *) other_b;
5231
5232 if (other_w->watchpoint_triggered
5233 == watch_triggered_yes)
5234 {
5235 other_write_watchpoint = 1;
5236 break;
5237 }
5238 }
5239 }
5240
5241 if (other_write_watchpoint
5242 || bl->watchpoint_type == hw_access)
5243 {
5244 /* We're watching the same memory for writes,
5245 and the value changed since the last time we
5246 updated it, so this trap must be for a write.
5247 Ignore it. */
5248 bs->print_it = print_it_noop;
5249 bs->stop = 0;
5250 }
5251 }
5252 break;
5253 case WP_VALUE_NOT_CHANGED:
5254 if (b->type == bp_hardware_watchpoint
5255 || b->type == bp_watchpoint)
5256 {
5257 /* Don't stop: write watchpoints shouldn't fire if
5258 the value hasn't changed. */
5259 bs->print_it = print_it_noop;
5260 bs->stop = 0;
5261 }
5262 /* Stop. */
5263 break;
5264 default:
5265 /* Can't happen. */
5266 break;
5267 }
5268 }
5269 else /* must_check_value == 0 */
5270 {
5271 /* This is a case where some watchpoint(s) triggered, but
5272 not at the address of this watchpoint, or else no
5273 watchpoint triggered after all. So don't print
5274 anything for this watchpoint. */
5275 bs->print_it = print_it_noop;
5276 bs->stop = 0;
5277 }
5278 }
5279 }
5280
5281 /* For breakpoints that are currently marked as telling gdb to stop,
5282 check conditions (condition proper, frame, thread and ignore count)
5283 of breakpoint referred to by BS. If we should not stop for this
5284 breakpoint, set BS->stop to 0. */
5285
5286 static void
5287 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5288 {
5289 const struct bp_location *bl;
5290 struct breakpoint *b;
5291 /* Assume stop. */
5292 bool condition_result = true;
5293 struct expression *cond;
5294
5295 gdb_assert (bs->stop);
5296
5297 /* BS is built for existing struct breakpoint. */
5298 bl = bs->bp_location_at.get ();
5299 gdb_assert (bl != NULL);
5300 b = bs->breakpoint_at;
5301 gdb_assert (b != NULL);
5302
5303 /* Even if the target evaluated the condition on its end and notified GDB, we
5304 need to do so again since GDB does not know if we stopped due to a
5305 breakpoint or a single step breakpoint. */
5306
5307 if (frame_id_p (b->frame_id)
5308 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5309 {
5310 bs->stop = 0;
5311 return;
5312 }
5313
5314 /* If this is a thread/task-specific breakpoint, don't waste cpu
5315 evaluating the condition if this isn't the specified
5316 thread/task. */
5317 if ((b->thread != -1 && b->thread != thread->global_num)
5318 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5319 {
5320 bs->stop = 0;
5321 return;
5322 }
5323
5324 /* Evaluate extension language breakpoints that have a "stop" method
5325 implemented. */
5326 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5327
5328 if (is_watchpoint (b))
5329 {
5330 struct watchpoint *w = (struct watchpoint *) b;
5331
5332 cond = w->cond_exp.get ();
5333 }
5334 else
5335 cond = bl->cond.get ();
5336
5337 if (cond && b->disposition != disp_del_at_next_stop)
5338 {
5339 int within_current_scope = 1;
5340 struct watchpoint * w;
5341
5342 /* We use value_mark and value_free_to_mark because it could
5343 be a long time before we return to the command level and
5344 call free_all_values. We can't call free_all_values
5345 because we might be in the middle of evaluating a
5346 function call. */
5347 struct value *mark = value_mark ();
5348
5349 if (is_watchpoint (b))
5350 w = (struct watchpoint *) b;
5351 else
5352 w = NULL;
5353
5354 /* Need to select the frame, with all that implies so that
5355 the conditions will have the right context. Because we
5356 use the frame, we will not see an inlined function's
5357 variables when we arrive at a breakpoint at the start
5358 of the inlined function; the current frame will be the
5359 call site. */
5360 if (w == NULL || w->cond_exp_valid_block == NULL)
5361 select_frame (get_current_frame ());
5362 else
5363 {
5364 struct frame_info *frame;
5365
5366 /* For local watchpoint expressions, which particular
5367 instance of a local is being watched matters, so we
5368 keep track of the frame to evaluate the expression
5369 in. To evaluate the condition however, it doesn't
5370 really matter which instantiation of the function
5371 where the condition makes sense triggers the
5372 watchpoint. This allows an expression like "watch
5373 global if q > 10" set in `func', catch writes to
5374 global on all threads that call `func', or catch
5375 writes on all recursive calls of `func' by a single
5376 thread. We simply always evaluate the condition in
5377 the innermost frame that's executing where it makes
5378 sense to evaluate the condition. It seems
5379 intuitive. */
5380 frame = block_innermost_frame (w->cond_exp_valid_block);
5381 if (frame != NULL)
5382 select_frame (frame);
5383 else
5384 within_current_scope = 0;
5385 }
5386 if (within_current_scope)
5387 {
5388 try
5389 {
5390 condition_result = breakpoint_cond_eval (cond);
5391 }
5392 catch (const gdb_exception &ex)
5393 {
5394 exception_fprintf (gdb_stderr, ex,
5395 "Error in testing breakpoint condition:\n");
5396 }
5397 }
5398 else
5399 {
5400 warning (_("Watchpoint condition cannot be tested "
5401 "in the current scope"));
5402 /* If we failed to set the right context for this
5403 watchpoint, unconditionally report it. */
5404 }
5405 /* FIXME-someday, should give breakpoint #. */
5406 value_free_to_mark (mark);
5407 }
5408
5409 if (cond && !condition_result)
5410 {
5411 bs->stop = 0;
5412 }
5413 else if (b->ignore_count > 0)
5414 {
5415 b->ignore_count--;
5416 bs->stop = 0;
5417 /* Increase the hit count even though we don't stop. */
5418 ++(b->hit_count);
5419 gdb::observers::breakpoint_modified.notify (b);
5420 }
5421 }
5422
5423 /* Returns true if we need to track moribund locations of LOC's type
5424 on the current target. */
5425
5426 static int
5427 need_moribund_for_location_type (struct bp_location *loc)
5428 {
5429 return ((loc->loc_type == bp_loc_software_breakpoint
5430 && !target_supports_stopped_by_sw_breakpoint ())
5431 || (loc->loc_type == bp_loc_hardware_breakpoint
5432 && !target_supports_stopped_by_hw_breakpoint ()));
5433 }
5434
5435 /* See breakpoint.h. */
5436
5437 bpstat *
5438 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5439 const target_waitstatus &ws)
5440 {
5441 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5442
5443 for (breakpoint *b : all_breakpoints ())
5444 {
5445 if (!breakpoint_enabled (b))
5446 continue;
5447
5448 for (bp_location *bl : b->locations ())
5449 {
5450 /* For hardware watchpoints, we look only at the first
5451 location. The watchpoint_check function will work on the
5452 entire expression, not the individual locations. For
5453 read watchpoints, the watchpoints_triggered function has
5454 checked all locations already. */
5455 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5456 break;
5457
5458 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5459 continue;
5460
5461 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5462 continue;
5463
5464 /* Come here if it's a watchpoint, or if the break address
5465 matches. */
5466
5467 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
5468 explain stop. */
5469
5470 /* Assume we stop. Should we find a watchpoint that is not
5471 actually triggered, or if the condition of the breakpoint
5472 evaluates as false, we'll reset 'stop' to 0. */
5473 bs->stop = 1;
5474 bs->print = 1;
5475
5476 /* If this is a scope breakpoint, mark the associated
5477 watchpoint as triggered so that we will handle the
5478 out-of-scope event. We'll get to the watchpoint next
5479 iteration. */
5480 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5481 {
5482 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5483
5484 w->watchpoint_triggered = watch_triggered_yes;
5485 }
5486 }
5487 }
5488
5489 /* Check if a moribund breakpoint explains the stop. */
5490 if (!target_supports_stopped_by_sw_breakpoint ()
5491 || !target_supports_stopped_by_hw_breakpoint ())
5492 {
5493 for (bp_location *loc : moribund_locations)
5494 {
5495 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5496 && need_moribund_for_location_type (loc))
5497 {
5498 bpstat *bs = new bpstat (loc, &bs_link);
5499 /* For hits of moribund locations, we should just proceed. */
5500 bs->stop = 0;
5501 bs->print = 0;
5502 bs->print_it = print_it_noop;
5503 }
5504 }
5505 }
5506
5507 return bs_head;
5508 }
5509
5510 /* See breakpoint.h. */
5511
5512 bpstat *
5513 bpstat_stop_status (const address_space *aspace,
5514 CORE_ADDR bp_addr, thread_info *thread,
5515 const target_waitstatus &ws,
5516 bpstat *stop_chain)
5517 {
5518 struct breakpoint *b = NULL;
5519 /* First item of allocated bpstat's. */
5520 bpstat *bs_head = stop_chain;
5521 bpstat *bs;
5522 int need_remove_insert;
5523 int removed_any;
5524
5525 /* First, build the bpstat chain with locations that explain a
5526 target stop, while being careful to not set the target running,
5527 as that may invalidate locations (in particular watchpoint
5528 locations are recreated). Resuming will happen here with
5529 breakpoint conditions or watchpoint expressions that include
5530 inferior function calls. */
5531 if (bs_head == NULL)
5532 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5533
5534 /* A bit of special processing for shlib breakpoints. We need to
5535 process solib loading here, so that the lists of loaded and
5536 unloaded libraries are correct before we handle "catch load" and
5537 "catch unload". */
5538 for (bs = bs_head; bs != NULL; bs = bs->next)
5539 {
5540 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5541 {
5542 handle_solib_event ();
5543 break;
5544 }
5545 }
5546
5547 /* Now go through the locations that caused the target to stop, and
5548 check whether we're interested in reporting this stop to higher
5549 layers, or whether we should resume the target transparently. */
5550
5551 removed_any = 0;
5552
5553 for (bs = bs_head; bs != NULL; bs = bs->next)
5554 {
5555 if (!bs->stop)
5556 continue;
5557
5558 b = bs->breakpoint_at;
5559 b->check_status (bs);
5560 if (bs->stop)
5561 {
5562 bpstat_check_breakpoint_conditions (bs, thread);
5563
5564 if (bs->stop)
5565 {
5566 ++(b->hit_count);
5567
5568 /* We will stop here. */
5569 if (b->disposition == disp_disable)
5570 {
5571 --(b->enable_count);
5572 if (b->enable_count <= 0)
5573 b->enable_state = bp_disabled;
5574 removed_any = 1;
5575 }
5576 gdb::observers::breakpoint_modified.notify (b);
5577 if (b->silent)
5578 bs->print = 0;
5579 bs->commands = b->commands;
5580 if (command_line_is_silent (bs->commands
5581 ? bs->commands.get () : NULL))
5582 bs->print = 0;
5583
5584 b->after_condition_true (bs);
5585 }
5586
5587 }
5588
5589 /* Print nothing for this entry if we don't stop or don't
5590 print. */
5591 if (!bs->stop || !bs->print)
5592 bs->print_it = print_it_noop;
5593 }
5594
5595 /* If we aren't stopping, the value of some hardware watchpoint may
5596 not have changed, but the intermediate memory locations we are
5597 watching may have. Don't bother if we're stopping; this will get
5598 done later. */
5599 need_remove_insert = 0;
5600 if (! bpstat_causes_stop (bs_head))
5601 for (bs = bs_head; bs != NULL; bs = bs->next)
5602 if (!bs->stop
5603 && bs->breakpoint_at
5604 && is_hardware_watchpoint (bs->breakpoint_at))
5605 {
5606 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5607
5608 update_watchpoint (w, 0 /* don't reparse. */);
5609 need_remove_insert = 1;
5610 }
5611
5612 if (need_remove_insert)
5613 update_global_location_list (UGLL_MAY_INSERT);
5614 else if (removed_any)
5615 update_global_location_list (UGLL_DONT_INSERT);
5616
5617 return bs_head;
5618 }
5619
5620 /* See breakpoint.h. */
5621
5622 bpstat *
5623 bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5624 thread_info *thread, const target_waitstatus &ws)
5625 {
5626 gdb_assert (!target_stopped_by_watchpoint ());
5627
5628 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5629 previous stop to avoid confusing bpstat_stop_status. */
5630 watchpoints_triggered (ws);
5631
5632 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5633 }
5634
5635 static void
5636 handle_jit_event (CORE_ADDR address)
5637 {
5638 struct gdbarch *gdbarch;
5639
5640 infrun_debug_printf ("handling bp_jit_event");
5641
5642 /* Switch terminal for any messages produced by
5643 breakpoint_re_set. */
5644 target_terminal::ours_for_output ();
5645
5646 gdbarch = get_frame_arch (get_current_frame ());
5647 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5648 thus it is expected that its objectfile can be found through
5649 minimal symbol lookup. If it doesn't work (and assert fails), it
5650 most likely means that `jit_breakpoint_re_set` was changes and this
5651 function needs to be updated too. */
5652 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5653 gdb_assert (jit_bp_sym.objfile != nullptr);
5654 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5655
5656 target_terminal::inferior ();
5657 }
5658
5659 /* Prepare WHAT final decision for infrun. */
5660
5661 /* Decide what infrun needs to do with this bpstat. */
5662
5663 struct bpstat_what
5664 bpstat_what (bpstat *bs_head)
5665 {
5666 struct bpstat_what retval;
5667 bpstat *bs;
5668
5669 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5670 retval.call_dummy = STOP_NONE;
5671 retval.is_longjmp = false;
5672
5673 for (bs = bs_head; bs != NULL; bs = bs->next)
5674 {
5675 /* Extract this BS's action. After processing each BS, we check
5676 if its action overrides all we've seem so far. */
5677 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5678 enum bptype bptype;
5679
5680 if (bs->breakpoint_at == NULL)
5681 {
5682 /* I suspect this can happen if it was a momentary
5683 breakpoint which has since been deleted. */
5684 bptype = bp_none;
5685 }
5686 else
5687 bptype = bs->breakpoint_at->type;
5688
5689 switch (bptype)
5690 {
5691 case bp_none:
5692 break;
5693 case bp_breakpoint:
5694 case bp_hardware_breakpoint:
5695 case bp_single_step:
5696 case bp_until:
5697 case bp_finish:
5698 case bp_shlib_event:
5699 if (bs->stop)
5700 {
5701 if (bs->print)
5702 this_action = BPSTAT_WHAT_STOP_NOISY;
5703 else
5704 this_action = BPSTAT_WHAT_STOP_SILENT;
5705 }
5706 else
5707 this_action = BPSTAT_WHAT_SINGLE;
5708 break;
5709 case bp_watchpoint:
5710 case bp_hardware_watchpoint:
5711 case bp_read_watchpoint:
5712 case bp_access_watchpoint:
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 {
5722 /* There was a watchpoint, but we're not stopping.
5723 This requires no further action. */
5724 }
5725 break;
5726 case bp_longjmp:
5727 case bp_longjmp_call_dummy:
5728 case bp_exception:
5729 if (bs->stop)
5730 {
5731 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5732 retval.is_longjmp = bptype != bp_exception;
5733 }
5734 else
5735 this_action = BPSTAT_WHAT_SINGLE;
5736 break;
5737 case bp_longjmp_resume:
5738 case bp_exception_resume:
5739 if (bs->stop)
5740 {
5741 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5742 retval.is_longjmp = bptype == bp_longjmp_resume;
5743 }
5744 else
5745 this_action = BPSTAT_WHAT_SINGLE;
5746 break;
5747 case bp_step_resume:
5748 if (bs->stop)
5749 this_action = BPSTAT_WHAT_STEP_RESUME;
5750 else
5751 {
5752 /* It is for the wrong frame. */
5753 this_action = BPSTAT_WHAT_SINGLE;
5754 }
5755 break;
5756 case bp_hp_step_resume:
5757 if (bs->stop)
5758 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5759 else
5760 {
5761 /* It is for the wrong frame. */
5762 this_action = BPSTAT_WHAT_SINGLE;
5763 }
5764 break;
5765 case bp_watchpoint_scope:
5766 case bp_thread_event:
5767 case bp_overlay_event:
5768 case bp_longjmp_master:
5769 case bp_std_terminate_master:
5770 case bp_exception_master:
5771 this_action = BPSTAT_WHAT_SINGLE;
5772 break;
5773 case bp_catchpoint:
5774 if (bs->stop)
5775 {
5776 if (bs->print)
5777 this_action = BPSTAT_WHAT_STOP_NOISY;
5778 else
5779 this_action = BPSTAT_WHAT_STOP_SILENT;
5780 }
5781 else
5782 {
5783 /* Some catchpoints are implemented with breakpoints.
5784 For those, we need to step over the breakpoint. */
5785 if (bs->bp_location_at->loc_type != bp_loc_other)
5786 this_action = BPSTAT_WHAT_SINGLE;
5787 }
5788 break;
5789 case bp_jit_event:
5790 this_action = BPSTAT_WHAT_SINGLE;
5791 break;
5792 case bp_call_dummy:
5793 /* Make sure the action is stop (silent or noisy),
5794 so infrun.c pops the dummy frame. */
5795 retval.call_dummy = STOP_STACK_DUMMY;
5796 this_action = BPSTAT_WHAT_STOP_SILENT;
5797 break;
5798 case bp_std_terminate:
5799 /* Make sure the action is stop (silent or noisy),
5800 so infrun.c pops the dummy frame. */
5801 retval.call_dummy = STOP_STD_TERMINATE;
5802 this_action = BPSTAT_WHAT_STOP_SILENT;
5803 break;
5804 case bp_tracepoint:
5805 case bp_fast_tracepoint:
5806 case bp_static_tracepoint:
5807 case bp_static_marker_tracepoint:
5808 /* Tracepoint hits should not be reported back to GDB, and
5809 if one got through somehow, it should have been filtered
5810 out already. */
5811 internal_error (__FILE__, __LINE__,
5812 _("bpstat_what: tracepoint encountered"));
5813 break;
5814 case bp_gnu_ifunc_resolver:
5815 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5816 this_action = BPSTAT_WHAT_SINGLE;
5817 break;
5818 case bp_gnu_ifunc_resolver_return:
5819 /* The breakpoint will be removed, execution will restart from the
5820 PC of the former breakpoint. */
5821 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5822 break;
5823
5824 case bp_dprintf:
5825 if (bs->stop)
5826 this_action = BPSTAT_WHAT_STOP_SILENT;
5827 else
5828 this_action = BPSTAT_WHAT_SINGLE;
5829 break;
5830
5831 default:
5832 internal_error (__FILE__, __LINE__,
5833 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5834 }
5835
5836 retval.main_action = std::max (retval.main_action, this_action);
5837 }
5838
5839 return retval;
5840 }
5841
5842 void
5843 bpstat_run_callbacks (bpstat *bs_head)
5844 {
5845 bpstat *bs;
5846
5847 for (bs = bs_head; bs != NULL; bs = bs->next)
5848 {
5849 struct breakpoint *b = bs->breakpoint_at;
5850
5851 if (b == NULL)
5852 continue;
5853 switch (b->type)
5854 {
5855 case bp_jit_event:
5856 handle_jit_event (bs->bp_location_at->address);
5857 break;
5858 case bp_gnu_ifunc_resolver:
5859 gnu_ifunc_resolver_stop (b);
5860 break;
5861 case bp_gnu_ifunc_resolver_return:
5862 gnu_ifunc_resolver_return_stop (b);
5863 break;
5864 }
5865 }
5866 }
5867
5868 /* See breakpoint.h. */
5869
5870 bool
5871 bpstat_should_step ()
5872 {
5873 for (breakpoint *b : all_breakpoints ())
5874 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5875 return true;
5876
5877 return false;
5878 }
5879
5880 /* See breakpoint.h. */
5881
5882 bool
5883 bpstat_causes_stop (bpstat *bs)
5884 {
5885 for (; bs != NULL; bs = bs->next)
5886 if (bs->stop)
5887 return true;
5888
5889 return false;
5890 }
5891
5892 \f
5893
5894 /* Compute a number of spaces suitable to indent the next line
5895 so it starts at the position corresponding to the table column
5896 named COL_NAME in the currently active table of UIOUT. */
5897
5898 static int
5899 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5900 {
5901 int i, total_width, width, align;
5902 const char *text;
5903
5904 total_width = 0;
5905 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5906 {
5907 if (strcmp (text, col_name) == 0)
5908 return total_width;
5909
5910 total_width += width + 1;
5911 }
5912
5913 return 0;
5914 }
5915
5916 /* Determine if the locations of this breakpoint will have their conditions
5917 evaluated by the target, host or a mix of both. Returns the following:
5918
5919 "host": Host evals condition.
5920 "host or target": Host or Target evals condition.
5921 "target": Target evals condition.
5922 */
5923
5924 static const char *
5925 bp_condition_evaluator (const breakpoint *b)
5926 {
5927 char host_evals = 0;
5928 char target_evals = 0;
5929
5930 if (!b)
5931 return NULL;
5932
5933 if (!is_breakpoint (b))
5934 return NULL;
5935
5936 if (gdb_evaluates_breakpoint_condition_p ()
5937 || !target_supports_evaluation_of_breakpoint_conditions ())
5938 return condition_evaluation_host;
5939
5940 for (bp_location *bl : b->locations ())
5941 {
5942 if (bl->cond_bytecode)
5943 target_evals++;
5944 else
5945 host_evals++;
5946 }
5947
5948 if (host_evals && target_evals)
5949 return condition_evaluation_both;
5950 else if (target_evals)
5951 return condition_evaluation_target;
5952 else
5953 return condition_evaluation_host;
5954 }
5955
5956 /* Determine the breakpoint location's condition evaluator. This is
5957 similar to bp_condition_evaluator, but for locations. */
5958
5959 static const char *
5960 bp_location_condition_evaluator (struct bp_location *bl)
5961 {
5962 if (bl && !is_breakpoint (bl->owner))
5963 return NULL;
5964
5965 if (gdb_evaluates_breakpoint_condition_p ()
5966 || !target_supports_evaluation_of_breakpoint_conditions ())
5967 return condition_evaluation_host;
5968
5969 if (bl && bl->cond_bytecode)
5970 return condition_evaluation_target;
5971 else
5972 return condition_evaluation_host;
5973 }
5974
5975 /* Print the LOC location out of the list of B->LOC locations. */
5976
5977 static void
5978 print_breakpoint_location (const breakpoint *b,
5979 struct bp_location *loc)
5980 {
5981 struct ui_out *uiout = current_uiout;
5982
5983 scoped_restore_current_program_space restore_pspace;
5984
5985 if (loc != NULL && loc->shlib_disabled)
5986 loc = NULL;
5987
5988 if (loc != NULL)
5989 set_current_program_space (loc->pspace);
5990
5991 if (b->display_canonical)
5992 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5993 else if (loc && loc->symtab)
5994 {
5995 const struct symbol *sym = loc->symbol;
5996
5997 if (sym)
5998 {
5999 uiout->text ("in ");
6000 uiout->field_string ("func", sym->print_name (),
6001 function_name_style.style ());
6002 uiout->text (" ");
6003 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6004 uiout->text ("at ");
6005 }
6006 uiout->field_string ("file",
6007 symtab_to_filename_for_display (loc->symtab),
6008 file_name_style.style ());
6009 uiout->text (":");
6010
6011 if (uiout->is_mi_like_p ())
6012 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6013
6014 uiout->field_signed ("line", loc->line_number);
6015 }
6016 else if (loc)
6017 {
6018 string_file stb;
6019
6020 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6021 demangle, "");
6022 uiout->field_stream ("at", stb);
6023 }
6024 else
6025 {
6026 uiout->field_string ("pending",
6027 event_location_to_string (b->location.get ()));
6028 /* If extra_string is available, it could be holding a condition
6029 or dprintf arguments. In either case, make sure it is printed,
6030 too, but only for non-MI streams. */
6031 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6032 {
6033 if (b->type == bp_dprintf)
6034 uiout->text (",");
6035 else
6036 uiout->text (" ");
6037 uiout->text (b->extra_string.get ());
6038 }
6039 }
6040
6041 if (loc && is_breakpoint (b)
6042 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6043 && bp_condition_evaluator (b) == condition_evaluation_both)
6044 {
6045 uiout->text (" (");
6046 uiout->field_string ("evaluated-by",
6047 bp_location_condition_evaluator (loc));
6048 uiout->text (")");
6049 }
6050 }
6051
6052 static const char *
6053 bptype_string (enum bptype type)
6054 {
6055 struct ep_type_description
6056 {
6057 enum bptype type;
6058 const char *description;
6059 };
6060 static struct ep_type_description bptypes[] =
6061 {
6062 {bp_none, "?deleted?"},
6063 {bp_breakpoint, "breakpoint"},
6064 {bp_hardware_breakpoint, "hw breakpoint"},
6065 {bp_single_step, "sw single-step"},
6066 {bp_until, "until"},
6067 {bp_finish, "finish"},
6068 {bp_watchpoint, "watchpoint"},
6069 {bp_hardware_watchpoint, "hw watchpoint"},
6070 {bp_read_watchpoint, "read watchpoint"},
6071 {bp_access_watchpoint, "acc watchpoint"},
6072 {bp_longjmp, "longjmp"},
6073 {bp_longjmp_resume, "longjmp resume"},
6074 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6075 {bp_exception, "exception"},
6076 {bp_exception_resume, "exception resume"},
6077 {bp_step_resume, "step resume"},
6078 {bp_hp_step_resume, "high-priority step resume"},
6079 {bp_watchpoint_scope, "watchpoint scope"},
6080 {bp_call_dummy, "call dummy"},
6081 {bp_std_terminate, "std::terminate"},
6082 {bp_shlib_event, "shlib events"},
6083 {bp_thread_event, "thread events"},
6084 {bp_overlay_event, "overlay events"},
6085 {bp_longjmp_master, "longjmp master"},
6086 {bp_std_terminate_master, "std::terminate master"},
6087 {bp_exception_master, "exception master"},
6088 {bp_catchpoint, "catchpoint"},
6089 {bp_tracepoint, "tracepoint"},
6090 {bp_fast_tracepoint, "fast tracepoint"},
6091 {bp_static_tracepoint, "static tracepoint"},
6092 {bp_static_marker_tracepoint, "static marker tracepoint"},
6093 {bp_dprintf, "dprintf"},
6094 {bp_jit_event, "jit events"},
6095 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6096 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6097 };
6098
6099 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6100 || ((int) type != bptypes[(int) type].type))
6101 internal_error (__FILE__, __LINE__,
6102 _("bptypes table does not describe type #%d."),
6103 (int) type);
6104
6105 return bptypes[(int) type].description;
6106 }
6107
6108 /* For MI, output a field named 'thread-groups' with a list as the value.
6109 For CLI, prefix the list with the string 'inf'. */
6110
6111 static void
6112 output_thread_groups (struct ui_out *uiout,
6113 const char *field_name,
6114 const std::vector<int> &inf_nums,
6115 int mi_only)
6116 {
6117 int is_mi = uiout->is_mi_like_p ();
6118
6119 /* For backward compatibility, don't display inferiors in CLI unless
6120 there are several. Always display them for MI. */
6121 if (!is_mi && mi_only)
6122 return;
6123
6124 ui_out_emit_list list_emitter (uiout, field_name);
6125
6126 for (size_t i = 0; i < inf_nums.size (); i++)
6127 {
6128 if (is_mi)
6129 {
6130 char mi_group[10];
6131
6132 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6133 uiout->field_string (NULL, mi_group);
6134 }
6135 else
6136 {
6137 if (i == 0)
6138 uiout->text (" inf ");
6139 else
6140 uiout->text (", ");
6141
6142 uiout->text (plongest (inf_nums[i]));
6143 }
6144 }
6145 }
6146
6147 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6148 instead of going via breakpoint_ops::print_one. This makes "maint
6149 info breakpoints" show the software breakpoint locations of
6150 catchpoints, which are considered internal implementation
6151 detail. Returns true if RAW_LOC is false and if the breakpoint's
6152 print_one method did something; false otherwise. */
6153
6154 static bool
6155 print_one_breakpoint_location (struct breakpoint *b,
6156 struct bp_location *loc,
6157 int loc_number,
6158 struct bp_location **last_loc,
6159 int allflag, bool raw_loc)
6160 {
6161 struct command_line *l;
6162 static char bpenables[] = "nynny";
6163
6164 struct ui_out *uiout = current_uiout;
6165 int header_of_multiple = 0;
6166 int part_of_multiple = (loc != NULL);
6167 struct value_print_options opts;
6168
6169 get_user_print_options (&opts);
6170
6171 gdb_assert (!loc || loc_number != 0);
6172 /* See comment in print_one_breakpoint concerning treatment of
6173 breakpoints with single disabled location. */
6174 if (loc == NULL
6175 && (b->loc != NULL
6176 && (b->loc->next != NULL
6177 || !b->loc->enabled || b->loc->disabled_by_cond)))
6178 header_of_multiple = 1;
6179 if (loc == NULL)
6180 loc = b->loc;
6181
6182 annotate_record ();
6183
6184 /* 1 */
6185 annotate_field (0);
6186 if (part_of_multiple)
6187 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6188 else
6189 uiout->field_signed ("number", b->number);
6190
6191 /* 2 */
6192 annotate_field (1);
6193 if (part_of_multiple)
6194 uiout->field_skip ("type");
6195 else
6196 uiout->field_string ("type", bptype_string (b->type));
6197
6198 /* 3 */
6199 annotate_field (2);
6200 if (part_of_multiple)
6201 uiout->field_skip ("disp");
6202 else
6203 uiout->field_string ("disp", bpdisp_text (b->disposition));
6204
6205 /* 4 */
6206 annotate_field (3);
6207 /* For locations that are disabled because of an invalid condition,
6208 display "N*" on CLI, where "*" refers to a footnote below the
6209 table. For MI, simply display a "N" without a footnote. */
6210 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
6211 if (part_of_multiple)
6212 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
6213 : (loc->enabled ? "y" : "n")));
6214 else
6215 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6216
6217 /* 5 and 6 */
6218 bool result = false;
6219 if (!raw_loc && b->print_one (last_loc))
6220 result = true;
6221 else
6222 {
6223 if (is_watchpoint (b))
6224 {
6225 struct watchpoint *w = (struct watchpoint *) b;
6226
6227 /* Field 4, the address, is omitted (which makes the columns
6228 not line up too nicely with the headers, but the effect
6229 is relatively readable). */
6230 if (opts.addressprint)
6231 uiout->field_skip ("addr");
6232 annotate_field (5);
6233 uiout->field_string ("what", w->exp_string.get ());
6234 }
6235 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6236 || is_ada_exception_catchpoint (b))
6237 {
6238 if (opts.addressprint)
6239 {
6240 annotate_field (4);
6241 if (header_of_multiple)
6242 uiout->field_string ("addr", "<MULTIPLE>",
6243 metadata_style.style ());
6244 else if (b->loc == NULL || loc->shlib_disabled)
6245 uiout->field_string ("addr", "<PENDING>",
6246 metadata_style.style ());
6247 else
6248 uiout->field_core_addr ("addr",
6249 loc->gdbarch, loc->address);
6250 }
6251 annotate_field (5);
6252 if (!header_of_multiple)
6253 print_breakpoint_location (b, loc);
6254 if (b->loc)
6255 *last_loc = b->loc;
6256 }
6257 }
6258
6259 if (loc != NULL && !header_of_multiple)
6260 {
6261 std::vector<int> inf_nums;
6262 int mi_only = 1;
6263
6264 for (inferior *inf : all_inferiors ())
6265 {
6266 if (inf->pspace == loc->pspace)
6267 inf_nums.push_back (inf->num);
6268 }
6269
6270 /* For backward compatibility, don't display inferiors in CLI unless
6271 there are several. Always display for MI. */
6272 if (allflag
6273 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6274 && (program_spaces.size () > 1
6275 || number_of_inferiors () > 1)
6276 /* LOC is for existing B, it cannot be in
6277 moribund_locations and thus having NULL OWNER. */
6278 && loc->owner->type != bp_catchpoint))
6279 mi_only = 0;
6280 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6281 }
6282
6283 if (!part_of_multiple)
6284 {
6285 if (b->thread != -1)
6286 {
6287 /* FIXME: This seems to be redundant and lost here; see the
6288 "stop only in" line a little further down. */
6289 uiout->text (" thread ");
6290 uiout->field_signed ("thread", b->thread);
6291 }
6292 else if (b->task != 0)
6293 {
6294 uiout->text (" task ");
6295 uiout->field_signed ("task", b->task);
6296 }
6297 }
6298
6299 uiout->text ("\n");
6300
6301 if (!part_of_multiple)
6302 b->print_one_detail (uiout);
6303
6304 if (part_of_multiple && frame_id_p (b->frame_id))
6305 {
6306 annotate_field (6);
6307 uiout->text ("\tstop only in stack frame at ");
6308 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6309 the frame ID. */
6310 uiout->field_core_addr ("frame",
6311 b->gdbarch, b->frame_id.stack_addr);
6312 uiout->text ("\n");
6313 }
6314
6315 if (!part_of_multiple && b->cond_string)
6316 {
6317 annotate_field (7);
6318 if (is_tracepoint (b))
6319 uiout->text ("\ttrace only if ");
6320 else
6321 uiout->text ("\tstop only if ");
6322 uiout->field_string ("cond", b->cond_string.get ());
6323
6324 /* Print whether the target is doing the breakpoint's condition
6325 evaluation. If GDB is doing the evaluation, don't print anything. */
6326 if (is_breakpoint (b)
6327 && breakpoint_condition_evaluation_mode ()
6328 == condition_evaluation_target)
6329 {
6330 uiout->message (" (%pF evals)",
6331 string_field ("evaluated-by",
6332 bp_condition_evaluator (b)));
6333 }
6334 uiout->text ("\n");
6335 }
6336
6337 if (!part_of_multiple && b->thread != -1)
6338 {
6339 /* FIXME should make an annotation for this. */
6340 uiout->text ("\tstop only in thread ");
6341 if (uiout->is_mi_like_p ())
6342 uiout->field_signed ("thread", b->thread);
6343 else
6344 {
6345 struct thread_info *thr = find_thread_global_id (b->thread);
6346
6347 uiout->field_string ("thread", print_thread_id (thr));
6348 }
6349 uiout->text ("\n");
6350 }
6351
6352 if (!part_of_multiple)
6353 {
6354 if (b->hit_count)
6355 {
6356 /* FIXME should make an annotation for this. */
6357 if (is_catchpoint (b))
6358 uiout->text ("\tcatchpoint");
6359 else if (is_tracepoint (b))
6360 uiout->text ("\ttracepoint");
6361 else
6362 uiout->text ("\tbreakpoint");
6363 uiout->text (" already hit ");
6364 uiout->field_signed ("times", b->hit_count);
6365 if (b->hit_count == 1)
6366 uiout->text (" time\n");
6367 else
6368 uiout->text (" times\n");
6369 }
6370 else
6371 {
6372 /* Output the count also if it is zero, but only if this is mi. */
6373 if (uiout->is_mi_like_p ())
6374 uiout->field_signed ("times", b->hit_count);
6375 }
6376 }
6377
6378 if (!part_of_multiple && b->ignore_count)
6379 {
6380 annotate_field (8);
6381 uiout->message ("\tignore next %pF hits\n",
6382 signed_field ("ignore", b->ignore_count));
6383 }
6384
6385 /* Note that an enable count of 1 corresponds to "enable once"
6386 behavior, which is reported by the combination of enablement and
6387 disposition, so we don't need to mention it here. */
6388 if (!part_of_multiple && b->enable_count > 1)
6389 {
6390 annotate_field (8);
6391 uiout->text ("\tdisable after ");
6392 /* Tweak the wording to clarify that ignore and enable counts
6393 are distinct, and have additive effect. */
6394 if (b->ignore_count)
6395 uiout->text ("additional ");
6396 else
6397 uiout->text ("next ");
6398 uiout->field_signed ("enable", b->enable_count);
6399 uiout->text (" hits\n");
6400 }
6401
6402 if (!part_of_multiple && is_tracepoint (b))
6403 {
6404 struct tracepoint *tp = (struct tracepoint *) b;
6405
6406 if (tp->traceframe_usage)
6407 {
6408 uiout->text ("\ttrace buffer usage ");
6409 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6410 uiout->text (" bytes\n");
6411 }
6412 }
6413
6414 l = b->commands ? b->commands.get () : NULL;
6415 if (!part_of_multiple && l)
6416 {
6417 annotate_field (9);
6418 ui_out_emit_tuple tuple_emitter (uiout, "script");
6419 print_command_lines (uiout, l, 4);
6420 }
6421
6422 if (is_tracepoint (b))
6423 {
6424 struct tracepoint *t = (struct tracepoint *) b;
6425
6426 if (!part_of_multiple && t->pass_count)
6427 {
6428 annotate_field (10);
6429 uiout->text ("\tpass count ");
6430 uiout->field_signed ("pass", t->pass_count);
6431 uiout->text (" \n");
6432 }
6433
6434 /* Don't display it when tracepoint or tracepoint location is
6435 pending. */
6436 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6437 {
6438 annotate_field (11);
6439
6440 if (uiout->is_mi_like_p ())
6441 uiout->field_string ("installed",
6442 loc->inserted ? "y" : "n");
6443 else
6444 {
6445 if (loc->inserted)
6446 uiout->text ("\t");
6447 else
6448 uiout->text ("\tnot ");
6449 uiout->text ("installed on target\n");
6450 }
6451 }
6452 }
6453
6454 if (uiout->is_mi_like_p () && !part_of_multiple)
6455 {
6456 if (is_watchpoint (b))
6457 {
6458 struct watchpoint *w = (struct watchpoint *) b;
6459
6460 uiout->field_string ("original-location", w->exp_string.get ());
6461 }
6462 else if (b->location != NULL
6463 && event_location_to_string (b->location.get ()) != NULL)
6464 uiout->field_string ("original-location",
6465 event_location_to_string (b->location.get ()));
6466 }
6467
6468 return result;
6469 }
6470
6471 /* See breakpoint.h. */
6472
6473 bool fix_multi_location_breakpoint_output_globally = false;
6474
6475 static void
6476 print_one_breakpoint (struct breakpoint *b,
6477 struct bp_location **last_loc,
6478 int allflag)
6479 {
6480 struct ui_out *uiout = current_uiout;
6481 bool use_fixed_output
6482 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6483 || fix_multi_location_breakpoint_output_globally);
6484
6485 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6486 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6487 allflag, false);
6488
6489 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6490 are outside. */
6491 if (!use_fixed_output)
6492 bkpt_tuple_emitter.reset ();
6493
6494 /* If this breakpoint has custom print function,
6495 it's already printed. Otherwise, print individual
6496 locations, if any. */
6497 if (!printed || allflag)
6498 {
6499 /* If breakpoint has a single location that is disabled, we
6500 print it as if it had several locations, since otherwise it's
6501 hard to represent "breakpoint enabled, location disabled"
6502 situation.
6503
6504 Note that while hardware watchpoints have several locations
6505 internally, that's not a property exposed to users.
6506
6507 Likewise, while catchpoints may be implemented with
6508 breakpoints (e.g., catch throw), that's not a property
6509 exposed to users. We do however display the internal
6510 breakpoint locations with "maint info breakpoints". */
6511 if (!is_hardware_watchpoint (b)
6512 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6513 || is_ada_exception_catchpoint (b))
6514 && (allflag
6515 || (b->loc && (b->loc->next
6516 || !b->loc->enabled
6517 || b->loc->disabled_by_cond))))
6518 {
6519 gdb::optional<ui_out_emit_list> locations_list;
6520
6521 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6522 MI record. For later versions, place breakpoint locations in a
6523 list. */
6524 if (uiout->is_mi_like_p () && use_fixed_output)
6525 locations_list.emplace (uiout, "locations");
6526
6527 int n = 1;
6528 for (bp_location *loc : b->locations ())
6529 {
6530 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6531 print_one_breakpoint_location (b, loc, n, last_loc,
6532 allflag, allflag);
6533 n++;
6534 }
6535 }
6536 }
6537 }
6538
6539 static int
6540 breakpoint_address_bits (struct breakpoint *b)
6541 {
6542 int print_address_bits = 0;
6543
6544 /* Software watchpoints that aren't watching memory don't have an
6545 address to print. */
6546 if (is_no_memory_software_watchpoint (b))
6547 return 0;
6548
6549 for (bp_location *loc : b->locations ())
6550 {
6551 int addr_bit;
6552
6553 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6554 if (addr_bit > print_address_bits)
6555 print_address_bits = addr_bit;
6556 }
6557
6558 return print_address_bits;
6559 }
6560
6561 /* See breakpoint.h. */
6562
6563 void
6564 print_breakpoint (breakpoint *b)
6565 {
6566 struct bp_location *dummy_loc = NULL;
6567 print_one_breakpoint (b, &dummy_loc, 0);
6568 }
6569
6570 /* Return true if this breakpoint was set by the user, false if it is
6571 internal or momentary. */
6572
6573 int
6574 user_breakpoint_p (struct breakpoint *b)
6575 {
6576 return b->number > 0;
6577 }
6578
6579 /* See breakpoint.h. */
6580
6581 int
6582 pending_breakpoint_p (struct breakpoint *b)
6583 {
6584 return b->loc == NULL;
6585 }
6586
6587 /* Print information on breakpoints (including watchpoints and tracepoints).
6588
6589 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6590 understood by number_or_range_parser. Only breakpoints included in this
6591 list are then printed.
6592
6593 If SHOW_INTERNAL is true, print internal breakpoints.
6594
6595 If FILTER is non-NULL, call it on each breakpoint and only include the
6596 ones for which it returns true.
6597
6598 Return the total number of breakpoints listed. */
6599
6600 static int
6601 breakpoint_1 (const char *bp_num_list, bool show_internal,
6602 bool (*filter) (const struct breakpoint *))
6603 {
6604 struct bp_location *last_loc = NULL;
6605 int nr_printable_breakpoints;
6606 struct value_print_options opts;
6607 int print_address_bits = 0;
6608 int print_type_col_width = 14;
6609 struct ui_out *uiout = current_uiout;
6610 bool has_disabled_by_cond_location = false;
6611
6612 get_user_print_options (&opts);
6613
6614 /* Compute the number of rows in the table, as well as the size
6615 required for address fields. */
6616 nr_printable_breakpoints = 0;
6617 for (breakpoint *b : all_breakpoints ())
6618 {
6619 /* If we have a filter, only list the breakpoints it accepts. */
6620 if (filter && !filter (b))
6621 continue;
6622
6623 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6624 accept. Skip the others. */
6625 if (bp_num_list != NULL && *bp_num_list != '\0')
6626 {
6627 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6628 continue;
6629 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6630 continue;
6631 }
6632
6633 if (show_internal || user_breakpoint_p (b))
6634 {
6635 int addr_bit, type_len;
6636
6637 addr_bit = breakpoint_address_bits (b);
6638 if (addr_bit > print_address_bits)
6639 print_address_bits = addr_bit;
6640
6641 type_len = strlen (bptype_string (b->type));
6642 if (type_len > print_type_col_width)
6643 print_type_col_width = type_len;
6644
6645 nr_printable_breakpoints++;
6646 }
6647 }
6648
6649 {
6650 ui_out_emit_table table_emitter (uiout,
6651 opts.addressprint ? 6 : 5,
6652 nr_printable_breakpoints,
6653 "BreakpointTable");
6654
6655 if (nr_printable_breakpoints > 0)
6656 annotate_breakpoints_headers ();
6657 if (nr_printable_breakpoints > 0)
6658 annotate_field (0);
6659 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6660 if (nr_printable_breakpoints > 0)
6661 annotate_field (1);
6662 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6663 if (nr_printable_breakpoints > 0)
6664 annotate_field (2);
6665 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6666 if (nr_printable_breakpoints > 0)
6667 annotate_field (3);
6668 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6669 if (opts.addressprint)
6670 {
6671 if (nr_printable_breakpoints > 0)
6672 annotate_field (4);
6673 if (print_address_bits <= 32)
6674 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6675 else
6676 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6677 }
6678 if (nr_printable_breakpoints > 0)
6679 annotate_field (5);
6680 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6681 uiout->table_body ();
6682 if (nr_printable_breakpoints > 0)
6683 annotate_breakpoints_table ();
6684
6685 for (breakpoint *b : all_breakpoints ())
6686 {
6687 QUIT;
6688 /* If we have a filter, only list the breakpoints it accepts. */
6689 if (filter && !filter (b))
6690 continue;
6691
6692 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6693 accept. Skip the others. */
6694
6695 if (bp_num_list != NULL && *bp_num_list != '\0')
6696 {
6697 if (show_internal) /* maintenance info breakpoint */
6698 {
6699 if (parse_and_eval_long (bp_num_list) != b->number)
6700 continue;
6701 }
6702 else /* all others */
6703 {
6704 if (!number_is_in_list (bp_num_list, b->number))
6705 continue;
6706 }
6707 }
6708 /* We only print out user settable breakpoints unless the
6709 show_internal is set. */
6710 if (show_internal || user_breakpoint_p (b))
6711 {
6712 print_one_breakpoint (b, &last_loc, show_internal);
6713 for (bp_location *loc : b->locations ())
6714 if (loc->disabled_by_cond)
6715 has_disabled_by_cond_location = true;
6716 }
6717 }
6718 }
6719
6720 if (nr_printable_breakpoints == 0)
6721 {
6722 /* If there's a filter, let the caller decide how to report
6723 empty list. */
6724 if (!filter)
6725 {
6726 if (bp_num_list == NULL || *bp_num_list == '\0')
6727 uiout->message ("No breakpoints or watchpoints.\n");
6728 else
6729 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6730 bp_num_list);
6731 }
6732 }
6733 else
6734 {
6735 if (last_loc && !server_command)
6736 set_next_address (last_loc->gdbarch, last_loc->address);
6737
6738 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6739 uiout->message (_("(*): Breakpoint condition is invalid at this "
6740 "location.\n"));
6741 }
6742
6743 /* FIXME? Should this be moved up so that it is only called when
6744 there have been breakpoints? */
6745 annotate_breakpoints_table_end ();
6746
6747 return nr_printable_breakpoints;
6748 }
6749
6750 /* Display the value of default-collect in a way that is generally
6751 compatible with the breakpoint list. */
6752
6753 static void
6754 default_collect_info (void)
6755 {
6756 struct ui_out *uiout = current_uiout;
6757
6758 /* If it has no value (which is frequently the case), say nothing; a
6759 message like "No default-collect." gets in user's face when it's
6760 not wanted. */
6761 if (default_collect.empty ())
6762 return;
6763
6764 /* The following phrase lines up nicely with per-tracepoint collect
6765 actions. */
6766 uiout->text ("default collect ");
6767 uiout->field_string ("default-collect", default_collect);
6768 uiout->text (" \n");
6769 }
6770
6771 static void
6772 info_breakpoints_command (const char *args, int from_tty)
6773 {
6774 breakpoint_1 (args, false, NULL);
6775
6776 default_collect_info ();
6777 }
6778
6779 static void
6780 info_watchpoints_command (const char *args, int from_tty)
6781 {
6782 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6783 struct ui_out *uiout = current_uiout;
6784
6785 if (num_printed == 0)
6786 {
6787 if (args == NULL || *args == '\0')
6788 uiout->message ("No watchpoints.\n");
6789 else
6790 uiout->message ("No watchpoint matching '%s'.\n", args);
6791 }
6792 }
6793
6794 static void
6795 maintenance_info_breakpoints (const char *args, int from_tty)
6796 {
6797 breakpoint_1 (args, true, NULL);
6798
6799 default_collect_info ();
6800 }
6801
6802 static int
6803 breakpoint_has_pc (struct breakpoint *b,
6804 struct program_space *pspace,
6805 CORE_ADDR pc, struct obj_section *section)
6806 {
6807 for (bp_location *bl : b->locations ())
6808 {
6809 if (bl->pspace == pspace
6810 && bl->address == pc
6811 && (!overlay_debugging || bl->section == section))
6812 return 1;
6813 }
6814 return 0;
6815 }
6816
6817 /* Print a message describing any user-breakpoints set at PC. This
6818 concerns with logical breakpoints, so we match program spaces, not
6819 address spaces. */
6820
6821 static void
6822 describe_other_breakpoints (struct gdbarch *gdbarch,
6823 struct program_space *pspace, CORE_ADDR pc,
6824 struct obj_section *section, int thread)
6825 {
6826 int others = 0;
6827
6828 for (breakpoint *b : all_breakpoints ())
6829 others += (user_breakpoint_p (b)
6830 && breakpoint_has_pc (b, pspace, pc, section));
6831
6832 if (others > 0)
6833 {
6834 if (others == 1)
6835 gdb_printf (_("Note: breakpoint "));
6836 else /* if (others == ???) */
6837 gdb_printf (_("Note: breakpoints "));
6838 for (breakpoint *b : all_breakpoints ())
6839 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6840 {
6841 others--;
6842 gdb_printf ("%d", b->number);
6843 if (b->thread == -1 && thread != -1)
6844 gdb_printf (" (all threads)");
6845 else if (b->thread != -1)
6846 gdb_printf (" (thread %d)", b->thread);
6847 gdb_printf ("%s%s ",
6848 ((b->enable_state == bp_disabled
6849 || b->enable_state == bp_call_disabled)
6850 ? " (disabled)"
6851 : ""),
6852 (others > 1) ? ","
6853 : ((others == 1) ? " and" : ""));
6854 }
6855 current_uiout->message (_("also set at pc %ps.\n"),
6856 styled_string (address_style.style (),
6857 paddress (gdbarch, pc)));
6858 }
6859 }
6860 \f
6861
6862 /* Return true iff it is meaningful to use the address member of LOC.
6863 For some breakpoint types, the locations' address members are
6864 irrelevant and it makes no sense to attempt to compare them to
6865 other addresses (or use them for any other purpose either).
6866
6867 More specifically, software watchpoints and catchpoints that are
6868 not backed by breakpoints always have a zero valued location
6869 address and we don't want to mark breakpoints of any of these types
6870 to be a duplicate of an actual breakpoint location at address
6871 zero. */
6872
6873 static bool
6874 bl_address_is_meaningful (bp_location *loc)
6875 {
6876 return loc->loc_type != bp_loc_other;
6877 }
6878
6879 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6880 true if LOC1 and LOC2 represent the same watchpoint location. */
6881
6882 static int
6883 watchpoint_locations_match (struct bp_location *loc1,
6884 struct bp_location *loc2)
6885 {
6886 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6887 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6888
6889 /* Both of them must exist. */
6890 gdb_assert (w1 != NULL);
6891 gdb_assert (w2 != NULL);
6892
6893 /* If the target can evaluate the condition expression in hardware,
6894 then we we need to insert both watchpoints even if they are at
6895 the same place. Otherwise the watchpoint will only trigger when
6896 the condition of whichever watchpoint was inserted evaluates to
6897 true, not giving a chance for GDB to check the condition of the
6898 other watchpoint. */
6899 if ((w1->cond_exp
6900 && target_can_accel_watchpoint_condition (loc1->address,
6901 loc1->length,
6902 loc1->watchpoint_type,
6903 w1->cond_exp.get ()))
6904 || (w2->cond_exp
6905 && target_can_accel_watchpoint_condition (loc2->address,
6906 loc2->length,
6907 loc2->watchpoint_type,
6908 w2->cond_exp.get ())))
6909 return 0;
6910
6911 /* Note that this checks the owner's type, not the location's. In
6912 case the target does not support read watchpoints, but does
6913 support access watchpoints, we'll have bp_read_watchpoint
6914 watchpoints with hw_access locations. Those should be considered
6915 duplicates of hw_read locations. The hw_read locations will
6916 become hw_access locations later. */
6917 return (loc1->owner->type == loc2->owner->type
6918 && loc1->pspace->aspace == loc2->pspace->aspace
6919 && loc1->address == loc2->address
6920 && loc1->length == loc2->length);
6921 }
6922
6923 /* See breakpoint.h. */
6924
6925 int
6926 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6927 const address_space *aspace2, CORE_ADDR addr2)
6928 {
6929 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6930 || aspace1 == aspace2)
6931 && addr1 == addr2);
6932 }
6933
6934 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6935 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6936 matches ASPACE2. On targets that have global breakpoints, the address
6937 space doesn't really matter. */
6938
6939 static int
6940 breakpoint_address_match_range (const address_space *aspace1,
6941 CORE_ADDR addr1,
6942 int len1, const address_space *aspace2,
6943 CORE_ADDR addr2)
6944 {
6945 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6946 || aspace1 == aspace2)
6947 && addr2 >= addr1 && addr2 < addr1 + len1);
6948 }
6949
6950 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6951 a ranged breakpoint. In most targets, a match happens only if ASPACE
6952 matches the breakpoint's address space. On targets that have global
6953 breakpoints, the address space doesn't really matter. */
6954
6955 static int
6956 breakpoint_location_address_match (struct bp_location *bl,
6957 const address_space *aspace,
6958 CORE_ADDR addr)
6959 {
6960 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6961 aspace, addr)
6962 || (bl->length
6963 && breakpoint_address_match_range (bl->pspace->aspace,
6964 bl->address, bl->length,
6965 aspace, addr)));
6966 }
6967
6968 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6969 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6970 match happens only if ASPACE matches the breakpoint's address
6971 space. On targets that have global breakpoints, the address space
6972 doesn't really matter. */
6973
6974 static int
6975 breakpoint_location_address_range_overlap (struct bp_location *bl,
6976 const address_space *aspace,
6977 CORE_ADDR addr, int len)
6978 {
6979 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6980 || bl->pspace->aspace == aspace)
6981 {
6982 int bl_len = bl->length != 0 ? bl->length : 1;
6983
6984 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6985 return 1;
6986 }
6987 return 0;
6988 }
6989
6990 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6991 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6992 true, otherwise returns false. */
6993
6994 static int
6995 tracepoint_locations_match (struct bp_location *loc1,
6996 struct bp_location *loc2)
6997 {
6998 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6999 /* Since tracepoint locations are never duplicated with others', tracepoint
7000 locations at the same address of different tracepoints are regarded as
7001 different locations. */
7002 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7003 else
7004 return 0;
7005 }
7006
7007 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7008 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7009 the same location. If SW_HW_BPS_MATCH is true, then software
7010 breakpoint locations and hardware breakpoint locations match,
7011 otherwise they don't. */
7012
7013 static int
7014 breakpoint_locations_match (struct bp_location *loc1,
7015 struct bp_location *loc2,
7016 bool sw_hw_bps_match)
7017 {
7018 int hw_point1, hw_point2;
7019
7020 /* Both of them must not be in moribund_locations. */
7021 gdb_assert (loc1->owner != NULL);
7022 gdb_assert (loc2->owner != NULL);
7023
7024 hw_point1 = is_hardware_watchpoint (loc1->owner);
7025 hw_point2 = is_hardware_watchpoint (loc2->owner);
7026
7027 if (hw_point1 != hw_point2)
7028 return 0;
7029 else if (hw_point1)
7030 return watchpoint_locations_match (loc1, loc2);
7031 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7032 return tracepoint_locations_match (loc1, loc2);
7033 else
7034 /* We compare bp_location.length in order to cover ranged
7035 breakpoints. Keep this in sync with
7036 bp_location_is_less_than. */
7037 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7038 loc2->pspace->aspace, loc2->address)
7039 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7040 && loc1->length == loc2->length);
7041 }
7042
7043 static void
7044 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7045 int bnum, int have_bnum)
7046 {
7047 /* The longest string possibly returned by hex_string_custom
7048 is 50 chars. These must be at least that big for safety. */
7049 char astr1[64];
7050 char astr2[64];
7051
7052 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7053 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7054 if (have_bnum)
7055 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7056 bnum, astr1, astr2);
7057 else
7058 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7059 }
7060
7061 /* Adjust a breakpoint's address to account for architectural
7062 constraints on breakpoint placement. Return the adjusted address.
7063 Note: Very few targets require this kind of adjustment. For most
7064 targets, this function is simply the identity function. */
7065
7066 static CORE_ADDR
7067 adjust_breakpoint_address (struct gdbarch *gdbarch,
7068 CORE_ADDR bpaddr, enum bptype bptype)
7069 {
7070 if (bptype == bp_watchpoint
7071 || bptype == bp_hardware_watchpoint
7072 || bptype == bp_read_watchpoint
7073 || bptype == bp_access_watchpoint
7074 || bptype == bp_catchpoint)
7075 {
7076 /* Watchpoints and the various bp_catch_* eventpoints should not
7077 have their addresses modified. */
7078 return bpaddr;
7079 }
7080 else if (bptype == bp_single_step)
7081 {
7082 /* Single-step breakpoints should not have their addresses
7083 modified. If there's any architectural constrain that
7084 applies to this address, then it should have already been
7085 taken into account when the breakpoint was created in the
7086 first place. If we didn't do this, stepping through e.g.,
7087 Thumb-2 IT blocks would break. */
7088 return bpaddr;
7089 }
7090 else
7091 {
7092 CORE_ADDR adjusted_bpaddr = bpaddr;
7093
7094 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7095 {
7096 /* Some targets have architectural constraints on the placement
7097 of breakpoint instructions. Obtain the adjusted address. */
7098 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7099 }
7100
7101 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7102
7103 /* An adjusted breakpoint address can significantly alter
7104 a user's expectations. Print a warning if an adjustment
7105 is required. */
7106 if (adjusted_bpaddr != bpaddr)
7107 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7108
7109 return adjusted_bpaddr;
7110 }
7111 }
7112
7113 static bp_loc_type
7114 bp_location_from_bp_type (bptype type)
7115 {
7116 switch (type)
7117 {
7118 case bp_breakpoint:
7119 case bp_single_step:
7120 case bp_until:
7121 case bp_finish:
7122 case bp_longjmp:
7123 case bp_longjmp_resume:
7124 case bp_longjmp_call_dummy:
7125 case bp_exception:
7126 case bp_exception_resume:
7127 case bp_step_resume:
7128 case bp_hp_step_resume:
7129 case bp_watchpoint_scope:
7130 case bp_call_dummy:
7131 case bp_std_terminate:
7132 case bp_shlib_event:
7133 case bp_thread_event:
7134 case bp_overlay_event:
7135 case bp_jit_event:
7136 case bp_longjmp_master:
7137 case bp_std_terminate_master:
7138 case bp_exception_master:
7139 case bp_gnu_ifunc_resolver:
7140 case bp_gnu_ifunc_resolver_return:
7141 case bp_dprintf:
7142 return bp_loc_software_breakpoint;
7143 case bp_hardware_breakpoint:
7144 return bp_loc_hardware_breakpoint;
7145 case bp_hardware_watchpoint:
7146 case bp_read_watchpoint:
7147 case bp_access_watchpoint:
7148 return bp_loc_hardware_watchpoint;
7149 case bp_watchpoint:
7150 case bp_catchpoint:
7151 case bp_tracepoint:
7152 case bp_fast_tracepoint:
7153 case bp_static_tracepoint:
7154 case bp_static_marker_tracepoint:
7155 return bp_loc_other;
7156 default:
7157 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7158 }
7159 }
7160
7161 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7162 {
7163 this->owner = owner;
7164 this->cond_bytecode = NULL;
7165 this->shlib_disabled = 0;
7166 this->enabled = 1;
7167 this->disabled_by_cond = false;
7168
7169 this->loc_type = type;
7170
7171 if (this->loc_type == bp_loc_software_breakpoint
7172 || this->loc_type == bp_loc_hardware_breakpoint)
7173 mark_breakpoint_location_modified (this);
7174
7175 incref ();
7176 }
7177
7178 bp_location::bp_location (breakpoint *owner)
7179 : bp_location::bp_location (owner,
7180 bp_location_from_bp_type (owner->type))
7181 {
7182 }
7183
7184 /* Decrement reference count. If the reference count reaches 0,
7185 destroy the bp_location. Sets *BLP to NULL. */
7186
7187 static void
7188 decref_bp_location (struct bp_location **blp)
7189 {
7190 bp_location_ref_policy::decref (*blp);
7191 *blp = NULL;
7192 }
7193
7194 /* Add breakpoint B at the end of the global breakpoint chain. */
7195
7196 static breakpoint *
7197 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7198 {
7199 struct breakpoint *b1;
7200 struct breakpoint *result = b.get ();
7201
7202 /* Add this breakpoint to the end of the chain so that a list of
7203 breakpoints will come out in order of increasing numbers. */
7204
7205 b1 = breakpoint_chain;
7206 if (b1 == 0)
7207 breakpoint_chain = b.release ();
7208 else
7209 {
7210 while (b1->next)
7211 b1 = b1->next;
7212 b1->next = b.release ();
7213 }
7214
7215 return result;
7216 }
7217
7218 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7219
7220 static void
7221 init_raw_breakpoint_without_location (struct breakpoint *b,
7222 struct gdbarch *gdbarch,
7223 enum bptype bptype)
7224 {
7225 b->type = bptype;
7226 b->gdbarch = gdbarch;
7227 b->language = current_language->la_language;
7228 b->input_radix = input_radix;
7229 b->related_breakpoint = b;
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 (bptype);
7240
7241 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype);
7242 return add_to_breakpoint_chain (std::move (b));
7243 }
7244
7245 /* Initialize loc->function_name. */
7246
7247 static void
7248 set_breakpoint_location_function (struct bp_location *loc)
7249 {
7250 gdb_assert (loc->owner != NULL);
7251
7252 if (loc->owner->type == bp_breakpoint
7253 || loc->owner->type == bp_hardware_breakpoint
7254 || is_tracepoint (loc->owner))
7255 {
7256 const char *function_name;
7257
7258 if (loc->msymbol != NULL
7259 && (loc->msymbol->type () == mst_text_gnu_ifunc
7260 || loc->msymbol->type () == mst_data_gnu_ifunc))
7261 {
7262 struct breakpoint *b = loc->owner;
7263
7264 function_name = loc->msymbol->linkage_name ();
7265
7266 if (b->type == bp_breakpoint && b->loc == loc
7267 && loc->next == NULL && b->related_breakpoint == b)
7268 {
7269 /* Create only the whole new breakpoint of this type but do not
7270 mess more complicated breakpoints with multiple locations. */
7271 b->type = bp_gnu_ifunc_resolver;
7272 /* Remember the resolver's address for use by the return
7273 breakpoint. */
7274 loc->related_address = loc->address;
7275 }
7276 }
7277 else
7278 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7279
7280 if (function_name)
7281 loc->function_name = make_unique_xstrdup (function_name);
7282 }
7283 }
7284
7285 /* Attempt to determine architecture of location identified by SAL. */
7286 struct gdbarch *
7287 get_sal_arch (struct symtab_and_line sal)
7288 {
7289 if (sal.section)
7290 return sal.section->objfile->arch ();
7291 if (sal.symtab)
7292 return sal.symtab->compunit ()->objfile ()->arch ();
7293
7294 return NULL;
7295 }
7296
7297 /* Low level routine for partially initializing a breakpoint of type
7298 BPTYPE. The newly created breakpoint's address, section, source
7299 file name, and line number are provided by SAL.
7300
7301 It is expected that the caller will complete the initialization of
7302 the newly created breakpoint struct as well as output any status
7303 information regarding the creation of a new breakpoint. */
7304
7305 static void
7306 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7307 struct symtab_and_line sal, enum bptype bptype)
7308 {
7309 init_raw_breakpoint_without_location (b, gdbarch, bptype);
7310
7311 add_location_to_breakpoint (b, &sal);
7312
7313 if (bptype != bp_catchpoint)
7314 gdb_assert (sal.pspace != NULL);
7315
7316 /* Store the program space that was used to set the breakpoint,
7317 except for ordinary breakpoints, which are independent of the
7318 program space. */
7319 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7320 b->pspace = sal.pspace;
7321 }
7322
7323 /* set_raw_breakpoint is a low level routine for allocating and
7324 partially initializing a breakpoint of type BPTYPE. The newly
7325 created breakpoint's address, section, source file name, and line
7326 number are provided by SAL. The newly created and partially
7327 initialized breakpoint is added to the breakpoint chain and
7328 is also returned as the value of this function.
7329
7330 It is expected that the caller will complete the initialization of
7331 the newly created breakpoint struct as well as output any status
7332 information regarding the creation of a new breakpoint. In
7333 particular, set_raw_breakpoint does NOT set the breakpoint
7334 number! Care should be taken to not allow an error to occur
7335 prior to completing the initialization of the breakpoint. If this
7336 should happen, a bogus breakpoint will be left on the chain. */
7337
7338 static struct breakpoint *
7339 set_raw_breakpoint (struct gdbarch *gdbarch,
7340 struct symtab_and_line sal, enum bptype bptype)
7341 {
7342 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7343
7344 init_raw_breakpoint (b.get (), gdbarch, sal, bptype);
7345 return add_to_breakpoint_chain (std::move (b));
7346 }
7347
7348 /* Call this routine when stepping and nexting to enable a breakpoint
7349 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7350 initiated the operation. */
7351
7352 void
7353 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7354 {
7355 int thread = tp->global_num;
7356
7357 /* To avoid having to rescan all objfile symbols at every step,
7358 we maintain a list of continually-inserted but always disabled
7359 longjmp "master" breakpoints. Here, we simply create momentary
7360 clones of those and enable them for the requested thread. */
7361 for (breakpoint *b : all_breakpoints_safe ())
7362 if (b->pspace == current_program_space
7363 && (b->type == bp_longjmp_master
7364 || b->type == bp_exception_master))
7365 {
7366 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7367 struct breakpoint *clone;
7368
7369 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7370 after their removal. */
7371 clone = momentary_breakpoint_from_master (b, type, 1);
7372 clone->thread = thread;
7373 }
7374
7375 tp->initiating_frame = frame;
7376 }
7377
7378 /* Delete all longjmp breakpoints from THREAD. */
7379 void
7380 delete_longjmp_breakpoint (int thread)
7381 {
7382 for (breakpoint *b : all_breakpoints_safe ())
7383 if (b->type == bp_longjmp || b->type == bp_exception)
7384 {
7385 if (b->thread == thread)
7386 delete_breakpoint (b);
7387 }
7388 }
7389
7390 void
7391 delete_longjmp_breakpoint_at_next_stop (int thread)
7392 {
7393 for (breakpoint *b : all_breakpoints_safe ())
7394 if (b->type == bp_longjmp || b->type == bp_exception)
7395 {
7396 if (b->thread == thread)
7397 b->disposition = disp_del_at_next_stop;
7398 }
7399 }
7400
7401 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7402 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7403 pointer to any of them. Return NULL if this system cannot place longjmp
7404 breakpoints. */
7405
7406 struct breakpoint *
7407 set_longjmp_breakpoint_for_call_dummy (void)
7408 {
7409 breakpoint *retval = nullptr;
7410
7411 for (breakpoint *b : all_breakpoints ())
7412 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7413 {
7414 struct breakpoint *new_b;
7415
7416 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7417 1);
7418 new_b->thread = inferior_thread ()->global_num;
7419
7420 /* Link NEW_B into the chain of RETVAL breakpoints. */
7421
7422 gdb_assert (new_b->related_breakpoint == new_b);
7423 if (retval == NULL)
7424 retval = new_b;
7425 new_b->related_breakpoint = retval;
7426 while (retval->related_breakpoint != new_b->related_breakpoint)
7427 retval = retval->related_breakpoint;
7428 retval->related_breakpoint = new_b;
7429 }
7430
7431 return retval;
7432 }
7433
7434 /* Verify all existing dummy frames and their associated breakpoints for
7435 TP. Remove those which can no longer be found in the current frame
7436 stack.
7437
7438 If the unwind fails then there is not sufficient information to discard
7439 dummy frames. In this case, elide the clean up and the dummy frames will
7440 be cleaned up next time this function is called from a location where
7441 unwinding is possible. */
7442
7443 void
7444 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7445 {
7446 struct breakpoint *b, *b_tmp;
7447
7448 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7449 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7450 {
7451 struct breakpoint *dummy_b = b->related_breakpoint;
7452
7453 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7454 chained off b->related_breakpoint. */
7455 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7456 dummy_b = dummy_b->related_breakpoint;
7457
7458 /* If there was no bp_call_dummy breakpoint then there's nothing
7459 more to do. Or, if the dummy frame associated with the
7460 bp_call_dummy is still on the stack then we need to leave this
7461 bp_call_dummy in place. */
7462 if (dummy_b->type != bp_call_dummy
7463 || frame_find_by_id (dummy_b->frame_id) != NULL)
7464 continue;
7465
7466 /* We didn't find the dummy frame on the stack, this could be
7467 because we have longjmp'd to a stack frame that is previous to
7468 the dummy frame, or it could be because the stack unwind is
7469 broken at some point between the longjmp frame and the dummy
7470 frame.
7471
7472 Next we figure out why the stack unwind stopped. If it looks
7473 like the unwind is complete then we assume the dummy frame has
7474 been jumped over, however, if the unwind stopped for an
7475 unexpected reason then we assume the stack unwind is currently
7476 broken, and that we will (eventually) return to the dummy
7477 frame.
7478
7479 It might be tempting to consider using frame_id_inner here, but
7480 that is not safe. There is no guarantee that the stack frames
7481 we are looking at here are even on the same stack as the
7482 original dummy frame, hence frame_id_inner can't be used. See
7483 the comments on frame_id_inner for more details. */
7484 bool unwind_finished_unexpectedly = false;
7485 for (struct frame_info *fi = get_current_frame (); fi != nullptr; )
7486 {
7487 struct frame_info *prev = get_prev_frame (fi);
7488 if (prev == nullptr)
7489 {
7490 /* FI is the last stack frame. Why did this frame not
7491 unwind further? */
7492 auto stop_reason = get_frame_unwind_stop_reason (fi);
7493 if (stop_reason != UNWIND_NO_REASON
7494 && stop_reason != UNWIND_OUTERMOST)
7495 unwind_finished_unexpectedly = true;
7496 }
7497 fi = prev;
7498 }
7499 if (unwind_finished_unexpectedly)
7500 continue;
7501
7502 dummy_frame_discard (dummy_b->frame_id, tp);
7503
7504 while (b->related_breakpoint != b)
7505 {
7506 if (b_tmp == b->related_breakpoint)
7507 b_tmp = b->related_breakpoint->next;
7508 delete_breakpoint (b->related_breakpoint);
7509 }
7510 delete_breakpoint (b);
7511 }
7512 }
7513
7514 void
7515 enable_overlay_breakpoints (void)
7516 {
7517 for (breakpoint *b : all_breakpoints ())
7518 if (b->type == bp_overlay_event)
7519 {
7520 b->enable_state = bp_enabled;
7521 update_global_location_list (UGLL_MAY_INSERT);
7522 overlay_events_enabled = 1;
7523 }
7524 }
7525
7526 void
7527 disable_overlay_breakpoints (void)
7528 {
7529 for (breakpoint *b : all_breakpoints ())
7530 if (b->type == bp_overlay_event)
7531 {
7532 b->enable_state = bp_disabled;
7533 update_global_location_list (UGLL_DONT_INSERT);
7534 overlay_events_enabled = 0;
7535 }
7536 }
7537
7538 /* Set an active std::terminate breakpoint for each std::terminate
7539 master breakpoint. */
7540 void
7541 set_std_terminate_breakpoint (void)
7542 {
7543 for (breakpoint *b : all_breakpoints_safe ())
7544 if (b->pspace == current_program_space
7545 && b->type == bp_std_terminate_master)
7546 {
7547 momentary_breakpoint_from_master (b, bp_std_terminate, 1);
7548 }
7549 }
7550
7551 /* Delete all the std::terminate breakpoints. */
7552 void
7553 delete_std_terminate_breakpoint (void)
7554 {
7555 for (breakpoint *b : all_breakpoints_safe ())
7556 if (b->type == bp_std_terminate)
7557 delete_breakpoint (b);
7558 }
7559
7560 struct breakpoint *
7561 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7562 {
7563 struct breakpoint *b;
7564
7565 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
7566
7567 b->enable_state = bp_enabled;
7568 /* location has to be used or breakpoint_re_set will delete me. */
7569 b->location = new_address_location (b->loc->address, NULL, 0);
7570
7571 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7572
7573 return b;
7574 }
7575
7576 struct lang_and_radix
7577 {
7578 enum language lang;
7579 int radix;
7580 };
7581
7582 /* Create a breakpoint for JIT code registration and unregistration. */
7583
7584 struct breakpoint *
7585 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7586 {
7587 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
7588 }
7589
7590 /* Remove JIT code registration and unregistration breakpoint(s). */
7591
7592 void
7593 remove_jit_event_breakpoints (void)
7594 {
7595 for (breakpoint *b : all_breakpoints_safe ())
7596 if (b->type == bp_jit_event
7597 && b->loc->pspace == current_program_space)
7598 delete_breakpoint (b);
7599 }
7600
7601 void
7602 remove_solib_event_breakpoints (void)
7603 {
7604 for (breakpoint *b : all_breakpoints_safe ())
7605 if (b->type == bp_shlib_event
7606 && b->loc->pspace == current_program_space)
7607 delete_breakpoint (b);
7608 }
7609
7610 /* See breakpoint.h. */
7611
7612 void
7613 remove_solib_event_breakpoints_at_next_stop (void)
7614 {
7615 for (breakpoint *b : all_breakpoints_safe ())
7616 if (b->type == bp_shlib_event
7617 && b->loc->pspace == current_program_space)
7618 b->disposition = disp_del_at_next_stop;
7619 }
7620
7621 /* Helper for create_solib_event_breakpoint /
7622 create_and_insert_solib_event_breakpoint. Allows specifying which
7623 INSERT_MODE to pass through to update_global_location_list. */
7624
7625 static struct breakpoint *
7626 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7627 enum ugll_insert_mode insert_mode)
7628 {
7629 struct breakpoint *b;
7630
7631 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
7632 update_global_location_list_nothrow (insert_mode);
7633 return b;
7634 }
7635
7636 struct breakpoint *
7637 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7638 {
7639 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7640 }
7641
7642 /* See breakpoint.h. */
7643
7644 struct breakpoint *
7645 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7646 {
7647 struct breakpoint *b;
7648
7649 /* Explicitly tell update_global_location_list to insert
7650 locations. */
7651 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7652 if (!b->loc->inserted)
7653 {
7654 delete_breakpoint (b);
7655 return NULL;
7656 }
7657 return b;
7658 }
7659
7660 /* Disable any breakpoints that are on code in shared libraries. Only
7661 apply to enabled breakpoints, disabled ones can just stay disabled. */
7662
7663 void
7664 disable_breakpoints_in_shlibs (void)
7665 {
7666 for (bp_location *loc : all_bp_locations ())
7667 {
7668 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7669 struct breakpoint *b = loc->owner;
7670
7671 /* We apply the check to all breakpoints, including disabled for
7672 those with loc->duplicate set. This is so that when breakpoint
7673 becomes enabled, or the duplicate is removed, gdb will try to
7674 insert all breakpoints. If we don't set shlib_disabled here,
7675 we'll try to insert those breakpoints and fail. */
7676 if (((b->type == bp_breakpoint)
7677 || (b->type == bp_jit_event)
7678 || (b->type == bp_hardware_breakpoint)
7679 || (is_tracepoint (b)))
7680 && loc->pspace == current_program_space
7681 && !loc->shlib_disabled
7682 && solib_name_from_address (loc->pspace, loc->address)
7683 )
7684 {
7685 loc->shlib_disabled = 1;
7686 }
7687 }
7688 }
7689
7690 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7691 notification of unloaded_shlib. Only apply to enabled breakpoints,
7692 disabled ones can just stay disabled. */
7693
7694 static void
7695 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7696 {
7697 int disabled_shlib_breaks = 0;
7698
7699 for (bp_location *loc : all_bp_locations ())
7700 {
7701 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7702 struct breakpoint *b = loc->owner;
7703
7704 if (solib->pspace == loc->pspace
7705 && !loc->shlib_disabled
7706 && (((b->type == bp_breakpoint
7707 || b->type == bp_jit_event
7708 || b->type == bp_hardware_breakpoint)
7709 && (loc->loc_type == bp_loc_hardware_breakpoint
7710 || loc->loc_type == bp_loc_software_breakpoint))
7711 || is_tracepoint (b))
7712 && solib_contains_address_p (solib, loc->address))
7713 {
7714 loc->shlib_disabled = 1;
7715 /* At this point, we cannot rely on remove_breakpoint
7716 succeeding so we must mark the breakpoint as not inserted
7717 to prevent future errors occurring in remove_breakpoints. */
7718 loc->inserted = 0;
7719
7720 /* This may cause duplicate notifications for the same breakpoint. */
7721 gdb::observers::breakpoint_modified.notify (b);
7722
7723 if (!disabled_shlib_breaks)
7724 {
7725 target_terminal::ours_for_output ();
7726 warning (_("Temporarily disabling breakpoints "
7727 "for unloaded shared library \"%s\""),
7728 solib->so_name);
7729 }
7730 disabled_shlib_breaks = 1;
7731 }
7732 }
7733 }
7734
7735 /* Disable any breakpoints and tracepoints in OBJFILE upon
7736 notification of free_objfile. Only apply to enabled breakpoints,
7737 disabled ones can just stay disabled. */
7738
7739 static void
7740 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7741 {
7742 if (objfile == NULL)
7743 return;
7744
7745 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7746 managed by the user with add-symbol-file/remove-symbol-file.
7747 Similarly to how breakpoints in shared libraries are handled in
7748 response to "nosharedlibrary", mark breakpoints in such modules
7749 shlib_disabled so they end up uninserted on the next global
7750 location list update. Shared libraries not loaded by the user
7751 aren't handled here -- they're already handled in
7752 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7753 solib_unloaded observer. We skip objfiles that are not
7754 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7755 main objfile). */
7756 if ((objfile->flags & OBJF_SHARED) == 0
7757 || (objfile->flags & OBJF_USERLOADED) == 0)
7758 return;
7759
7760 for (breakpoint *b : all_breakpoints ())
7761 {
7762 int bp_modified = 0;
7763
7764 if (!is_breakpoint (b) && !is_tracepoint (b))
7765 continue;
7766
7767 for (bp_location *loc : b->locations ())
7768 {
7769 CORE_ADDR loc_addr = loc->address;
7770
7771 if (loc->loc_type != bp_loc_hardware_breakpoint
7772 && loc->loc_type != bp_loc_software_breakpoint)
7773 continue;
7774
7775 if (loc->shlib_disabled != 0)
7776 continue;
7777
7778 if (objfile->pspace != loc->pspace)
7779 continue;
7780
7781 if (loc->loc_type != bp_loc_hardware_breakpoint
7782 && loc->loc_type != bp_loc_software_breakpoint)
7783 continue;
7784
7785 if (is_addr_in_objfile (loc_addr, objfile))
7786 {
7787 loc->shlib_disabled = 1;
7788 /* At this point, we don't know whether the object was
7789 unmapped from the inferior or not, so leave the
7790 inserted flag alone. We'll handle failure to
7791 uninsert quietly, in case the object was indeed
7792 unmapped. */
7793
7794 mark_breakpoint_location_modified (loc);
7795
7796 bp_modified = 1;
7797 }
7798 }
7799
7800 if (bp_modified)
7801 gdb::observers::breakpoint_modified.notify (b);
7802 }
7803 }
7804
7805 /* See breakpoint.h. */
7806
7807 void
7808 init_catchpoint (struct breakpoint *b,
7809 struct gdbarch *gdbarch, bool temp,
7810 const char *cond_string)
7811 {
7812 symtab_and_line sal;
7813 sal.pspace = current_program_space;
7814
7815 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint);
7816
7817 if (cond_string == nullptr)
7818 b->cond_string.reset ();
7819 else
7820 b->cond_string = make_unique_xstrdup (cond_string);
7821 b->disposition = temp ? disp_del : disp_donttouch;
7822 }
7823
7824 void
7825 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
7826 {
7827 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
7828 set_breakpoint_number (internal, b);
7829 if (is_tracepoint (b))
7830 set_tracepoint_count (breakpoint_count);
7831 if (!internal)
7832 mention (b);
7833 gdb::observers::breakpoint_created.notify (b);
7834
7835 if (update_gll)
7836 update_global_location_list (UGLL_MAY_INSERT);
7837 }
7838
7839 static int
7840 hw_breakpoint_used_count (void)
7841 {
7842 int i = 0;
7843
7844 for (breakpoint *b : all_breakpoints ())
7845 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
7846 for (bp_location *bl : b->locations ())
7847 {
7848 /* Special types of hardware breakpoints may use more than
7849 one register. */
7850 i += b->resources_needed (bl);
7851 }
7852
7853 return i;
7854 }
7855
7856 /* Returns the resources B would use if it were a hardware
7857 watchpoint. */
7858
7859 static int
7860 hw_watchpoint_use_count (struct breakpoint *b)
7861 {
7862 int i = 0;
7863
7864 if (!breakpoint_enabled (b))
7865 return 0;
7866
7867 for (bp_location *bl : b->locations ())
7868 {
7869 /* Special types of hardware watchpoints may use more than
7870 one register. */
7871 i += b->resources_needed (bl);
7872 }
7873
7874 return i;
7875 }
7876
7877 /* Returns the sum the used resources of all hardware watchpoints of
7878 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
7879 the sum of the used resources of all hardware watchpoints of other
7880 types _not_ TYPE. */
7881
7882 static int
7883 hw_watchpoint_used_count_others (struct breakpoint *except,
7884 enum bptype type, int *other_type_used)
7885 {
7886 int i = 0;
7887
7888 *other_type_used = 0;
7889 for (breakpoint *b : all_breakpoints ())
7890 {
7891 if (b == except)
7892 continue;
7893 if (!breakpoint_enabled (b))
7894 continue;
7895
7896 if (b->type == type)
7897 i += hw_watchpoint_use_count (b);
7898 else if (is_hardware_watchpoint (b))
7899 *other_type_used = 1;
7900 }
7901
7902 return i;
7903 }
7904
7905 void
7906 disable_watchpoints_before_interactive_call_start (void)
7907 {
7908 for (breakpoint *b : all_breakpoints ())
7909 if (is_watchpoint (b) && breakpoint_enabled (b))
7910 {
7911 b->enable_state = bp_call_disabled;
7912 update_global_location_list (UGLL_DONT_INSERT);
7913 }
7914 }
7915
7916 void
7917 enable_watchpoints_after_interactive_call_stop (void)
7918 {
7919 for (breakpoint *b : all_breakpoints ())
7920 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
7921 {
7922 b->enable_state = bp_enabled;
7923 update_global_location_list (UGLL_MAY_INSERT);
7924 }
7925 }
7926
7927 void
7928 disable_breakpoints_before_startup (void)
7929 {
7930 current_program_space->executing_startup = 1;
7931 update_global_location_list (UGLL_DONT_INSERT);
7932 }
7933
7934 void
7935 enable_breakpoints_after_startup (void)
7936 {
7937 current_program_space->executing_startup = 0;
7938 breakpoint_re_set ();
7939 }
7940
7941 /* Create a new single-step breakpoint for thread THREAD, with no
7942 locations. */
7943
7944 static struct breakpoint *
7945 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
7946 {
7947 std::unique_ptr<breakpoint> b (new momentary_breakpoint ());
7948
7949 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step);
7950
7951 b->disposition = disp_donttouch;
7952 b->frame_id = null_frame_id;
7953
7954 b->thread = thread;
7955 gdb_assert (b->thread != 0);
7956
7957 return add_to_breakpoint_chain (std::move (b));
7958 }
7959
7960 /* Set a momentary breakpoint of type TYPE at address specified by
7961 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
7962 frame. */
7963
7964 breakpoint_up
7965 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7966 struct frame_id frame_id, enum bptype type)
7967 {
7968 struct breakpoint *b;
7969
7970 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
7971 tail-called one. */
7972 gdb_assert (!frame_id_artificial_p (frame_id));
7973
7974 b = set_raw_breakpoint (gdbarch, sal, type);
7975 b->enable_state = bp_enabled;
7976 b->disposition = disp_donttouch;
7977 b->frame_id = frame_id;
7978
7979 b->thread = inferior_thread ()->global_num;
7980
7981 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7982
7983 return breakpoint_up (b);
7984 }
7985
7986 /* Make a momentary breakpoint based on the master breakpoint ORIG.
7987 The new breakpoint will have type TYPE, use OPS as its
7988 breakpoint_ops, and will set enabled to LOC_ENABLED. */
7989
7990 static struct breakpoint *
7991 momentary_breakpoint_from_master (struct breakpoint *orig,
7992 enum bptype type,
7993 int loc_enabled)
7994 {
7995 struct breakpoint *copy;
7996
7997 copy = set_raw_breakpoint_without_location (orig->gdbarch, type);
7998 copy->loc = copy->allocate_location ();
7999 set_breakpoint_location_function (copy->loc);
8000
8001 copy->loc->gdbarch = orig->loc->gdbarch;
8002 copy->loc->requested_address = orig->loc->requested_address;
8003 copy->loc->address = orig->loc->address;
8004 copy->loc->section = orig->loc->section;
8005 copy->loc->pspace = orig->loc->pspace;
8006 copy->loc->probe = orig->loc->probe;
8007 copy->loc->line_number = orig->loc->line_number;
8008 copy->loc->symtab = orig->loc->symtab;
8009 copy->loc->enabled = loc_enabled;
8010 copy->frame_id = orig->frame_id;
8011 copy->thread = orig->thread;
8012 copy->pspace = orig->pspace;
8013
8014 copy->enable_state = bp_enabled;
8015 copy->disposition = disp_donttouch;
8016 copy->number = internal_breakpoint_number--;
8017
8018 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8019 return copy;
8020 }
8021
8022 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8023 ORIG is NULL. */
8024
8025 struct breakpoint *
8026 clone_momentary_breakpoint (struct breakpoint *orig)
8027 {
8028 /* If there's nothing to clone, then return nothing. */
8029 if (orig == NULL)
8030 return NULL;
8031
8032 return momentary_breakpoint_from_master (orig, orig->type, 0);
8033 }
8034
8035 breakpoint_up
8036 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8037 enum bptype type)
8038 {
8039 struct symtab_and_line sal;
8040
8041 sal = find_pc_line (pc, 0);
8042 sal.pc = pc;
8043 sal.section = find_pc_overlay (pc);
8044 sal.explicit_pc = 1;
8045
8046 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8047 }
8048 \f
8049
8050 /* Tell the user we have just set a breakpoint B. */
8051
8052 static void
8053 mention (struct breakpoint *b)
8054 {
8055 b->print_mention ();
8056 current_uiout->text ("\n");
8057 }
8058 \f
8059
8060 static bool bp_loc_is_permanent (struct bp_location *loc);
8061
8062 /* Handle "set breakpoint auto-hw on".
8063
8064 If the explicitly specified breakpoint type is not hardware
8065 breakpoint, check the memory map to see whether the breakpoint
8066 address is in read-only memory.
8067
8068 - location type is not hardware breakpoint, memory is read-only.
8069 We change the type of the location to hardware breakpoint.
8070
8071 - location type is hardware breakpoint, memory is read-write. This
8072 means we've previously made the location hardware one, but then the
8073 memory map changed, so we undo.
8074 */
8075
8076 static void
8077 handle_automatic_hardware_breakpoints (bp_location *bl)
8078 {
8079 if (automatic_hardware_breakpoints
8080 && bl->owner->type != bp_hardware_breakpoint
8081 && (bl->loc_type == bp_loc_software_breakpoint
8082 || bl->loc_type == bp_loc_hardware_breakpoint))
8083 {
8084 /* When breakpoints are removed, remove_breakpoints will use
8085 location types we've just set here, the only possible problem
8086 is that memory map has changed during running program, but
8087 it's not going to work anyway with current gdb. */
8088 mem_region *mr = lookup_mem_region (bl->address);
8089
8090 if (mr != nullptr)
8091 {
8092 enum bp_loc_type new_type;
8093
8094 if (mr->attrib.mode != MEM_RW)
8095 new_type = bp_loc_hardware_breakpoint;
8096 else
8097 new_type = bp_loc_software_breakpoint;
8098
8099 if (new_type != bl->loc_type)
8100 {
8101 static bool said = false;
8102
8103 bl->loc_type = new_type;
8104 if (!said)
8105 {
8106 gdb_printf (_("Note: automatically using "
8107 "hardware breakpoints for "
8108 "read-only addresses.\n"));
8109 said = true;
8110 }
8111 }
8112 }
8113 }
8114 }
8115
8116 static struct bp_location *
8117 add_location_to_breakpoint (struct breakpoint *b,
8118 const struct symtab_and_line *sal)
8119 {
8120 struct bp_location *loc, **tmp;
8121 CORE_ADDR adjusted_address;
8122 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8123
8124 if (loc_gdbarch == NULL)
8125 loc_gdbarch = b->gdbarch;
8126
8127 /* Adjust the breakpoint's address prior to allocating a location.
8128 Once we call allocate_location(), that mostly uninitialized
8129 location will be placed on the location chain. Adjustment of the
8130 breakpoint may cause target_read_memory() to be called and we do
8131 not want its scan of the location chain to find a breakpoint and
8132 location that's only been partially initialized. */
8133 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8134 sal->pc, b->type);
8135
8136 /* Sort the locations by their ADDRESS. */
8137 loc = b->allocate_location ();
8138 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8139 tmp = &((*tmp)->next))
8140 ;
8141 loc->next = *tmp;
8142 *tmp = loc;
8143
8144 loc->requested_address = sal->pc;
8145 loc->address = adjusted_address;
8146 loc->pspace = sal->pspace;
8147 loc->probe.prob = sal->prob;
8148 loc->probe.objfile = sal->objfile;
8149 gdb_assert (loc->pspace != NULL);
8150 loc->section = sal->section;
8151 loc->gdbarch = loc_gdbarch;
8152 loc->line_number = sal->line;
8153 loc->symtab = sal->symtab;
8154 loc->symbol = sal->symbol;
8155 loc->msymbol = sal->msymbol;
8156 loc->objfile = sal->objfile;
8157
8158 set_breakpoint_location_function (loc);
8159
8160 /* While by definition, permanent breakpoints are already present in the
8161 code, we don't mark the location as inserted. Normally one would expect
8162 that GDB could rely on that breakpoint instruction to stop the program,
8163 thus removing the need to insert its own breakpoint, except that executing
8164 the breakpoint instruction can kill the target instead of reporting a
8165 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8166 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8167 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8168 breakpoint be inserted normally results in QEMU knowing about the GDB
8169 breakpoint, and thus trap before the breakpoint instruction is executed.
8170 (If GDB later needs to continue execution past the permanent breakpoint,
8171 it manually increments the PC, thus avoiding executing the breakpoint
8172 instruction.) */
8173 if (bp_loc_is_permanent (loc))
8174 loc->permanent = 1;
8175
8176 return loc;
8177 }
8178 \f
8179
8180 /* Return true if LOC is pointing to a permanent breakpoint,
8181 return false otherwise. */
8182
8183 static bool
8184 bp_loc_is_permanent (struct bp_location *loc)
8185 {
8186 gdb_assert (loc != NULL);
8187
8188 /* If we have a non-breakpoint-backed catchpoint or a software
8189 watchpoint, just return 0. We should not attempt to read from
8190 the addresses the locations of these breakpoint types point to.
8191 gdbarch_program_breakpoint_here_p, below, will attempt to read
8192 memory. */
8193 if (!bl_address_is_meaningful (loc))
8194 return false;
8195
8196 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8197 switch_to_program_space_and_thread (loc->pspace);
8198 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8199 }
8200
8201 /* Build a command list for the dprintf corresponding to the current
8202 settings of the dprintf style options. */
8203
8204 static void
8205 update_dprintf_command_list (struct breakpoint *b)
8206 {
8207 const char *dprintf_args = b->extra_string.get ();
8208 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8209
8210 if (!dprintf_args)
8211 return;
8212
8213 dprintf_args = skip_spaces (dprintf_args);
8214
8215 /* Allow a comma, as it may have terminated a location, but don't
8216 insist on it. */
8217 if (*dprintf_args == ',')
8218 ++dprintf_args;
8219 dprintf_args = skip_spaces (dprintf_args);
8220
8221 if (*dprintf_args != '"')
8222 error (_("Bad format string, missing '\"'."));
8223
8224 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8225 printf_line = xstrprintf ("printf %s", dprintf_args);
8226 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8227 {
8228 if (dprintf_function.empty ())
8229 error (_("No function supplied for dprintf call"));
8230
8231 if (!dprintf_channel.empty ())
8232 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8233 dprintf_function.c_str (),
8234 dprintf_channel.c_str (),
8235 dprintf_args);
8236 else
8237 printf_line = xstrprintf ("call (void) %s (%s)",
8238 dprintf_function.c_str (),
8239 dprintf_args);
8240 }
8241 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8242 {
8243 if (target_can_run_breakpoint_commands ())
8244 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8245 else
8246 {
8247 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8248 printf_line = xstrprintf ("printf %s", dprintf_args);
8249 }
8250 }
8251 else
8252 internal_error (__FILE__, __LINE__,
8253 _("Invalid dprintf style."));
8254
8255 gdb_assert (printf_line != NULL);
8256
8257 /* Manufacture a printf sequence. */
8258 struct command_line *printf_cmd_line
8259 = new struct command_line (simple_control, printf_line.release ());
8260 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8261 command_lines_deleter ()));
8262 }
8263
8264 /* Update all dprintf commands, making their command lists reflect
8265 current style settings. */
8266
8267 static void
8268 update_dprintf_commands (const char *args, int from_tty,
8269 struct cmd_list_element *c)
8270 {
8271 for (breakpoint *b : all_breakpoints ())
8272 if (b->type == bp_dprintf)
8273 update_dprintf_command_list (b);
8274 }
8275
8276 /* Create a breakpoint with SAL as location. Use LOCATION
8277 as a description of the location, and COND_STRING
8278 as condition expression. If LOCATION is NULL then create an
8279 "address location" from the address in the SAL. */
8280
8281 static void
8282 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8283 gdb::array_view<const symtab_and_line> sals,
8284 event_location_up &&location,
8285 gdb::unique_xmalloc_ptr<char> filter,
8286 gdb::unique_xmalloc_ptr<char> cond_string,
8287 gdb::unique_xmalloc_ptr<char> extra_string,
8288 enum bptype type, enum bpdisp disposition,
8289 int thread, int task, int ignore_count,
8290 const struct breakpoint_ops *ops, int from_tty,
8291 int enabled, int internal, unsigned flags,
8292 int display_canonical)
8293 {
8294 int i;
8295
8296 if (type == bp_hardware_breakpoint)
8297 {
8298 int target_resources_ok;
8299
8300 i = hw_breakpoint_used_count ();
8301 target_resources_ok =
8302 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8303 i + 1, 0);
8304 if (target_resources_ok == 0)
8305 error (_("No hardware breakpoint support in the target."));
8306 else if (target_resources_ok < 0)
8307 error (_("Hardware breakpoints used exceeds limit."));
8308 }
8309
8310 gdb_assert (!sals.empty ());
8311
8312 for (const auto &sal : sals)
8313 {
8314 struct bp_location *loc;
8315
8316 if (from_tty)
8317 {
8318 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8319 if (!loc_gdbarch)
8320 loc_gdbarch = gdbarch;
8321
8322 describe_other_breakpoints (loc_gdbarch,
8323 sal.pspace, sal.pc, sal.section, thread);
8324 }
8325
8326 if (&sal == &sals[0])
8327 {
8328 init_raw_breakpoint (b, gdbarch, sal, type);
8329 b->thread = thread;
8330 b->task = task;
8331
8332 b->cond_string = std::move (cond_string);
8333 b->extra_string = std::move (extra_string);
8334 b->ignore_count = ignore_count;
8335 b->enable_state = enabled ? bp_enabled : bp_disabled;
8336 b->disposition = disposition;
8337
8338 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8339 b->loc->inserted = 1;
8340
8341 if (type == bp_static_tracepoint
8342 || type == bp_static_marker_tracepoint)
8343 {
8344 struct tracepoint *t = (struct tracepoint *) b;
8345 struct static_tracepoint_marker marker;
8346
8347 if (strace_marker_p (b))
8348 {
8349 /* We already know the marker exists, otherwise, we
8350 wouldn't see a sal for it. */
8351 const char *p
8352 = &event_location_to_string (b->location.get ())[3];
8353 const char *endp;
8354
8355 p = skip_spaces (p);
8356
8357 endp = skip_to_space (p);
8358
8359 t->static_trace_marker_id.assign (p, endp - p);
8360
8361 gdb_printf (_("Probed static tracepoint "
8362 "marker \"%s\"\n"),
8363 t->static_trace_marker_id.c_str ());
8364 }
8365 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8366 {
8367 t->static_trace_marker_id = std::move (marker.str_id);
8368
8369 gdb_printf (_("Probed static tracepoint "
8370 "marker \"%s\"\n"),
8371 t->static_trace_marker_id.c_str ());
8372 }
8373 else
8374 warning (_("Couldn't determine the static "
8375 "tracepoint marker to probe"));
8376 }
8377
8378 loc = b->loc;
8379 }
8380 else
8381 {
8382 loc = add_location_to_breakpoint (b, &sal);
8383 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8384 loc->inserted = 1;
8385 }
8386
8387 /* Do not set breakpoint locations conditions yet. As locations
8388 are inserted, they get sorted based on their addresses. Let
8389 the list stabilize to have reliable location numbers. */
8390
8391 /* Dynamic printf requires and uses additional arguments on the
8392 command line, otherwise it's an error. */
8393 if (type == bp_dprintf)
8394 {
8395 if (b->extra_string)
8396 update_dprintf_command_list (b);
8397 else
8398 error (_("Format string required"));
8399 }
8400 else if (b->extra_string)
8401 error (_("Garbage '%s' at end of command"), b->extra_string.get ());
8402 }
8403
8404
8405 /* The order of the locations is now stable. Set the location
8406 condition using the location's number. */
8407 int loc_num = 1;
8408 for (bp_location *loc : b->locations ())
8409 {
8410 if (b->cond_string != nullptr)
8411 set_breakpoint_location_condition (b->cond_string.get (), loc,
8412 b->number, loc_num);
8413
8414 ++loc_num;
8415 }
8416
8417 b->display_canonical = display_canonical;
8418 if (location != NULL)
8419 b->location = std::move (location);
8420 else
8421 b->location = new_address_location (b->loc->address, NULL, 0);
8422 b->filter = std::move (filter);
8423 }
8424
8425 static void
8426 create_breakpoint_sal (struct gdbarch *gdbarch,
8427 gdb::array_view<const symtab_and_line> sals,
8428 event_location_up &&location,
8429 gdb::unique_xmalloc_ptr<char> filter,
8430 gdb::unique_xmalloc_ptr<char> cond_string,
8431 gdb::unique_xmalloc_ptr<char> extra_string,
8432 enum bptype type, enum bpdisp disposition,
8433 int thread, int task, int ignore_count,
8434 const struct breakpoint_ops *ops, int from_tty,
8435 int enabled, int internal, unsigned flags,
8436 int display_canonical)
8437 {
8438 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8439
8440 init_breakpoint_sal (b.get (), gdbarch,
8441 sals, std::move (location),
8442 std::move (filter),
8443 std::move (cond_string),
8444 std::move (extra_string),
8445 type, disposition,
8446 thread, task, ignore_count,
8447 ops, from_tty,
8448 enabled, internal, flags,
8449 display_canonical);
8450
8451 install_breakpoint (internal, std::move (b), 0);
8452 }
8453
8454 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8455 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8456 value. COND_STRING, if not NULL, specified the condition to be
8457 used for all breakpoints. Essentially the only case where
8458 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8459 function. In that case, it's still not possible to specify
8460 separate conditions for different overloaded functions, so
8461 we take just a single condition string.
8462
8463 NOTE: If the function succeeds, the caller is expected to cleanup
8464 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8465 array contents). If the function fails (error() is called), the
8466 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8467 COND and SALS arrays and each of those arrays contents. */
8468
8469 static void
8470 create_breakpoints_sal (struct gdbarch *gdbarch,
8471 struct linespec_result *canonical,
8472 gdb::unique_xmalloc_ptr<char> cond_string,
8473 gdb::unique_xmalloc_ptr<char> extra_string,
8474 enum bptype type, enum bpdisp disposition,
8475 int thread, int task, int ignore_count,
8476 const struct breakpoint_ops *ops, int from_tty,
8477 int enabled, int internal, unsigned flags)
8478 {
8479 if (canonical->pre_expanded)
8480 gdb_assert (canonical->lsals.size () == 1);
8481
8482 for (const auto &lsal : canonical->lsals)
8483 {
8484 /* Note that 'location' can be NULL in the case of a plain
8485 'break', without arguments. */
8486 event_location_up location
8487 = (canonical->location != NULL
8488 ? copy_event_location (canonical->location.get ()) : NULL);
8489 gdb::unique_xmalloc_ptr<char> filter_string
8490 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8491
8492 create_breakpoint_sal (gdbarch, lsal.sals,
8493 std::move (location),
8494 std::move (filter_string),
8495 std::move (cond_string),
8496 std::move (extra_string),
8497 type, disposition,
8498 thread, task, ignore_count, ops,
8499 from_tty, enabled, internal, flags,
8500 canonical->special_display);
8501 }
8502 }
8503
8504 /* Parse LOCATION which is assumed to be a SAL specification possibly
8505 followed by conditionals. On return, SALS contains an array of SAL
8506 addresses found. LOCATION points to the end of the SAL (for
8507 linespec locations).
8508
8509 The array and the line spec strings are allocated on the heap, it is
8510 the caller's responsibility to free them. */
8511
8512 static void
8513 parse_breakpoint_sals (struct event_location *location,
8514 struct linespec_result *canonical)
8515 {
8516 struct symtab_and_line cursal;
8517
8518 if (event_location_type (location) == LINESPEC_LOCATION)
8519 {
8520 const char *spec = get_linespec_location (location)->spec_string;
8521
8522 if (spec == NULL)
8523 {
8524 /* The last displayed codepoint, if it's valid, is our default
8525 breakpoint address. */
8526 if (last_displayed_sal_is_valid ())
8527 {
8528 /* Set sal's pspace, pc, symtab, and line to the values
8529 corresponding to the last call to print_frame_info.
8530 Be sure to reinitialize LINE with NOTCURRENT == 0
8531 as the breakpoint line number is inappropriate otherwise.
8532 find_pc_line would adjust PC, re-set it back. */
8533 symtab_and_line sal = get_last_displayed_sal ();
8534 CORE_ADDR pc = sal.pc;
8535
8536 sal = find_pc_line (pc, 0);
8537
8538 /* "break" without arguments is equivalent to "break *PC"
8539 where PC is the last displayed codepoint's address. So
8540 make sure to set sal.explicit_pc to prevent GDB from
8541 trying to expand the list of sals to include all other
8542 instances with the same symtab and line. */
8543 sal.pc = pc;
8544 sal.explicit_pc = 1;
8545
8546 struct linespec_sals lsal;
8547 lsal.sals = {sal};
8548 lsal.canonical = NULL;
8549
8550 canonical->lsals.push_back (std::move (lsal));
8551 return;
8552 }
8553 else
8554 error (_("No default breakpoint address now."));
8555 }
8556 }
8557
8558 /* Force almost all breakpoints to be in terms of the
8559 current_source_symtab (which is decode_line_1's default).
8560 This should produce the results we want almost all of the
8561 time while leaving default_breakpoint_* alone.
8562
8563 ObjC: However, don't match an Objective-C method name which
8564 may have a '+' or '-' succeeded by a '['. */
8565 cursal = get_current_source_symtab_and_line ();
8566 if (last_displayed_sal_is_valid ())
8567 {
8568 const char *spec = NULL;
8569
8570 if (event_location_type (location) == LINESPEC_LOCATION)
8571 spec = get_linespec_location (location)->spec_string;
8572
8573 if (!cursal.symtab
8574 || (spec != NULL
8575 && strchr ("+-", spec[0]) != NULL
8576 && spec[1] != '['))
8577 {
8578 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
8579 get_last_displayed_symtab (),
8580 get_last_displayed_line (),
8581 canonical, NULL, NULL);
8582 return;
8583 }
8584 }
8585
8586 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
8587 cursal.symtab, cursal.line, canonical, NULL, NULL);
8588 }
8589
8590
8591 /* Convert each SAL into a real PC. Verify that the PC can be
8592 inserted as a breakpoint. If it can't throw an error. */
8593
8594 static void
8595 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
8596 {
8597 for (auto &sal : sals)
8598 resolve_sal_pc (&sal);
8599 }
8600
8601 /* Fast tracepoints may have restrictions on valid locations. For
8602 instance, a fast tracepoint using a jump instead of a trap will
8603 likely have to overwrite more bytes than a trap would, and so can
8604 only be placed where the instruction is longer than the jump, or a
8605 multi-instruction sequence does not have a jump into the middle of
8606 it, etc. */
8607
8608 static void
8609 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8610 gdb::array_view<const symtab_and_line> sals)
8611 {
8612 for (const auto &sal : sals)
8613 {
8614 struct gdbarch *sarch;
8615
8616 sarch = get_sal_arch (sal);
8617 /* We fall back to GDBARCH if there is no architecture
8618 associated with SAL. */
8619 if (sarch == NULL)
8620 sarch = gdbarch;
8621 std::string msg;
8622 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
8623 error (_("May not have a fast tracepoint at %s%s"),
8624 paddress (sarch, sal.pc), msg.c_str ());
8625 }
8626 }
8627
8628 /* Given TOK, a string specification of condition and thread, as
8629 accepted by the 'break' command, extract the condition
8630 string and thread number and set *COND_STRING and *THREAD.
8631 PC identifies the context at which the condition should be parsed.
8632 If no condition is found, *COND_STRING is set to NULL.
8633 If no thread is found, *THREAD is set to -1. */
8634
8635 static void
8636 find_condition_and_thread (const char *tok, CORE_ADDR pc,
8637 gdb::unique_xmalloc_ptr<char> *cond_string,
8638 int *thread, int *task,
8639 gdb::unique_xmalloc_ptr<char> *rest)
8640 {
8641 cond_string->reset ();
8642 *thread = -1;
8643 *task = 0;
8644 rest->reset ();
8645 bool force = false;
8646
8647 while (tok && *tok)
8648 {
8649 const char *end_tok;
8650 int toklen;
8651 const char *cond_start = NULL;
8652 const char *cond_end = NULL;
8653
8654 tok = skip_spaces (tok);
8655
8656 if ((*tok == '"' || *tok == ',') && rest)
8657 {
8658 rest->reset (savestring (tok, strlen (tok)));
8659 return;
8660 }
8661
8662 end_tok = skip_to_space (tok);
8663
8664 toklen = end_tok - tok;
8665
8666 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8667 {
8668 tok = cond_start = end_tok + 1;
8669 try
8670 {
8671 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8672 }
8673 catch (const gdb_exception_error &)
8674 {
8675 if (!force)
8676 throw;
8677 else
8678 tok = tok + strlen (tok);
8679 }
8680 cond_end = tok;
8681 cond_string->reset (savestring (cond_start, cond_end - cond_start));
8682 }
8683 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8684 {
8685 tok = tok + toklen;
8686 force = true;
8687 }
8688 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8689 {
8690 const char *tmptok;
8691 struct thread_info *thr;
8692
8693 tok = end_tok + 1;
8694 thr = parse_thread_id (tok, &tmptok);
8695 if (tok == tmptok)
8696 error (_("Junk after thread keyword."));
8697 *thread = thr->global_num;
8698 tok = tmptok;
8699 }
8700 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8701 {
8702 char *tmptok;
8703
8704 tok = end_tok + 1;
8705 *task = strtol (tok, &tmptok, 0);
8706 if (tok == tmptok)
8707 error (_("Junk after task keyword."));
8708 if (!valid_task_id (*task))
8709 error (_("Unknown task %d."), *task);
8710 tok = tmptok;
8711 }
8712 else if (rest)
8713 {
8714 rest->reset (savestring (tok, strlen (tok)));
8715 return;
8716 }
8717 else
8718 error (_("Junk at end of arguments."));
8719 }
8720 }
8721
8722 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
8723 succeeds. The parsed values are written to COND_STRING, THREAD,
8724 TASK, and REST. See the comment of 'find_condition_and_thread'
8725 for the description of these parameters and INPUT. */
8726
8727 static void
8728 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
8729 const char *input,
8730 gdb::unique_xmalloc_ptr<char> *cond_string,
8731 int *thread, int *task,
8732 gdb::unique_xmalloc_ptr<char> *rest)
8733 {
8734 int num_failures = 0;
8735 for (auto &sal : sals)
8736 {
8737 gdb::unique_xmalloc_ptr<char> cond;
8738 int thread_id = 0;
8739 int task_id = 0;
8740 gdb::unique_xmalloc_ptr<char> remaining;
8741
8742 /* Here we want to parse 'arg' to separate condition from thread
8743 number. But because parsing happens in a context and the
8744 contexts of sals might be different, try each until there is
8745 success. Finding one successful parse is sufficient for our
8746 goal. When setting the breakpoint we'll re-parse the
8747 condition in the context of each sal. */
8748 try
8749 {
8750 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8751 &task_id, &remaining);
8752 *cond_string = std::move (cond);
8753 *thread = thread_id;
8754 *task = task_id;
8755 *rest = std::move (remaining);
8756 break;
8757 }
8758 catch (const gdb_exception_error &e)
8759 {
8760 num_failures++;
8761 /* If no sal remains, do not continue. */
8762 if (num_failures == sals.size ())
8763 throw;
8764 }
8765 }
8766 }
8767
8768 /* Decode a static tracepoint marker spec. */
8769
8770 static std::vector<symtab_and_line>
8771 decode_static_tracepoint_spec (const char **arg_p)
8772 {
8773 const char *p = &(*arg_p)[3];
8774 const char *endp;
8775
8776 p = skip_spaces (p);
8777
8778 endp = skip_to_space (p);
8779
8780 std::string marker_str (p, endp - p);
8781
8782 std::vector<static_tracepoint_marker> markers
8783 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8784 if (markers.empty ())
8785 error (_("No known static tracepoint marker named %s"),
8786 marker_str.c_str ());
8787
8788 std::vector<symtab_and_line> sals;
8789 sals.reserve (markers.size ());
8790
8791 for (const static_tracepoint_marker &marker : markers)
8792 {
8793 symtab_and_line sal = find_pc_line (marker.address, 0);
8794 sal.pc = marker.address;
8795 sals.push_back (sal);
8796 }
8797
8798 *arg_p = endp;
8799 return sals;
8800 }
8801
8802 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8803 according to IS_TRACEPOINT. */
8804
8805 static const struct breakpoint_ops *
8806 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
8807 bool is_tracepoint)
8808 {
8809 if (is_tracepoint)
8810 {
8811 if (location_type == PROBE_LOCATION)
8812 return &tracepoint_probe_breakpoint_ops;
8813 else
8814 return &base_breakpoint_ops;
8815 }
8816 else
8817 {
8818 if (location_type == PROBE_LOCATION)
8819 return &bkpt_probe_breakpoint_ops;
8820 else
8821 return &base_breakpoint_ops;
8822 }
8823 }
8824
8825 /* See breakpoint.h. */
8826
8827 const struct breakpoint_ops *
8828 breakpoint_ops_for_event_location (const struct event_location *location,
8829 bool is_tracepoint)
8830 {
8831 if (location != nullptr)
8832 return breakpoint_ops_for_event_location_type
8833 (event_location_type (location), is_tracepoint);
8834 return &base_breakpoint_ops;
8835 }
8836
8837 /* See breakpoint.h. */
8838
8839 int
8840 create_breakpoint (struct gdbarch *gdbarch,
8841 struct event_location *location,
8842 const char *cond_string,
8843 int thread, const char *extra_string,
8844 bool force_condition, int parse_extra,
8845 int tempflag, enum bptype type_wanted,
8846 int ignore_count,
8847 enum auto_boolean pending_break_support,
8848 const struct breakpoint_ops *ops,
8849 int from_tty, int enabled, int internal,
8850 unsigned flags)
8851 {
8852 struct linespec_result canonical;
8853 int pending = 0;
8854 int task = 0;
8855 int prev_bkpt_count = breakpoint_count;
8856
8857 gdb_assert (ops != NULL);
8858
8859 /* If extra_string isn't useful, set it to NULL. */
8860 if (extra_string != NULL && *extra_string == '\0')
8861 extra_string = NULL;
8862
8863 try
8864 {
8865 ops->create_sals_from_location (location, &canonical);
8866 }
8867 catch (const gdb_exception_error &e)
8868 {
8869 /* If caller is interested in rc value from parse, set
8870 value. */
8871 if (e.error == NOT_FOUND_ERROR)
8872 {
8873 /* If pending breakpoint support is turned off, throw
8874 error. */
8875
8876 if (pending_break_support == AUTO_BOOLEAN_FALSE)
8877 throw;
8878
8879 exception_print (gdb_stderr, e);
8880
8881 /* If pending breakpoint support is auto query and the user
8882 selects no, then simply return the error code. */
8883 if (pending_break_support == AUTO_BOOLEAN_AUTO
8884 && !nquery (_("Make %s pending on future shared library load? "),
8885 bptype_string (type_wanted)))
8886 return 0;
8887
8888 /* At this point, either the user was queried about setting
8889 a pending breakpoint and selected yes, or pending
8890 breakpoint behavior is on and thus a pending breakpoint
8891 is defaulted on behalf of the user. */
8892 pending = 1;
8893 }
8894 else
8895 throw;
8896 }
8897
8898 if (!pending && canonical.lsals.empty ())
8899 return 0;
8900
8901 /* Resolve all line numbers to PC's and verify that the addresses
8902 are ok for the target. */
8903 if (!pending)
8904 {
8905 for (auto &lsal : canonical.lsals)
8906 breakpoint_sals_to_pc (lsal.sals);
8907 }
8908
8909 /* Fast tracepoints may have additional restrictions on location. */
8910 if (!pending && type_wanted == bp_fast_tracepoint)
8911 {
8912 for (const auto &lsal : canonical.lsals)
8913 check_fast_tracepoint_sals (gdbarch, lsal.sals);
8914 }
8915
8916 /* Verify that condition can be parsed, before setting any
8917 breakpoints. Allocate a separate condition expression for each
8918 breakpoint. */
8919 if (!pending)
8920 {
8921 gdb::unique_xmalloc_ptr<char> cond_string_copy;
8922 gdb::unique_xmalloc_ptr<char> extra_string_copy;
8923
8924 if (parse_extra)
8925 {
8926 gdb::unique_xmalloc_ptr<char> rest;
8927 gdb::unique_xmalloc_ptr<char> cond;
8928
8929 const linespec_sals &lsal = canonical.lsals[0];
8930
8931 find_condition_and_thread_for_sals (lsal.sals, extra_string,
8932 &cond, &thread, &task, &rest);
8933 cond_string_copy = std::move (cond);
8934 extra_string_copy = std::move (rest);
8935 }
8936 else
8937 {
8938 if (type_wanted != bp_dprintf
8939 && extra_string != NULL && *extra_string != '\0')
8940 error (_("Garbage '%s' at end of location"), extra_string);
8941
8942 /* Check the validity of the condition. We should error out
8943 if the condition is invalid at all of the locations and
8944 if it is not forced. In the PARSE_EXTRA case above, this
8945 check is done when parsing the EXTRA_STRING. */
8946 if (cond_string != nullptr && !force_condition)
8947 {
8948 int num_failures = 0;
8949 const linespec_sals &lsal = canonical.lsals[0];
8950 for (const auto &sal : lsal.sals)
8951 {
8952 const char *cond = cond_string;
8953 try
8954 {
8955 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
8956 /* One success is sufficient to keep going. */
8957 break;
8958 }
8959 catch (const gdb_exception_error &)
8960 {
8961 num_failures++;
8962 /* If this is the last sal, error out. */
8963 if (num_failures == lsal.sals.size ())
8964 throw;
8965 }
8966 }
8967 }
8968
8969 /* Create a private copy of condition string. */
8970 if (cond_string)
8971 cond_string_copy.reset (xstrdup (cond_string));
8972 /* Create a private copy of any extra string. */
8973 if (extra_string)
8974 extra_string_copy.reset (xstrdup (extra_string));
8975 }
8976
8977 ops->create_breakpoints_sal (gdbarch, &canonical,
8978 std::move (cond_string_copy),
8979 std::move (extra_string_copy),
8980 type_wanted,
8981 tempflag ? disp_del : disp_donttouch,
8982 thread, task, ignore_count, ops,
8983 from_tty, enabled, internal, flags);
8984 }
8985 else
8986 {
8987 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
8988
8989 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted);
8990 b->location = copy_event_location (location);
8991
8992 if (parse_extra)
8993 b->cond_string = NULL;
8994 else
8995 {
8996 /* Create a private copy of condition string. */
8997 b->cond_string.reset (cond_string != NULL
8998 ? xstrdup (cond_string)
8999 : NULL);
9000 b->thread = thread;
9001 }
9002
9003 /* Create a private copy of any extra string. */
9004 b->extra_string.reset (extra_string != NULL
9005 ? xstrdup (extra_string)
9006 : NULL);
9007 b->ignore_count = ignore_count;
9008 b->disposition = tempflag ? disp_del : disp_donttouch;
9009 b->condition_not_parsed = 1;
9010 b->enable_state = enabled ? bp_enabled : bp_disabled;
9011 if ((type_wanted != bp_breakpoint
9012 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9013 b->pspace = current_program_space;
9014
9015 install_breakpoint (internal, std::move (b), 0);
9016 }
9017
9018 if (canonical.lsals.size () > 1)
9019 {
9020 warning (_("Multiple breakpoints were set.\nUse the "
9021 "\"delete\" command to delete unwanted breakpoints."));
9022 prev_breakpoint_count = prev_bkpt_count;
9023 }
9024
9025 update_global_location_list (UGLL_MAY_INSERT);
9026
9027 return 1;
9028 }
9029
9030 /* Set a breakpoint.
9031 ARG is a string describing breakpoint address,
9032 condition, and thread.
9033 FLAG specifies if a breakpoint is hardware on,
9034 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9035 and BP_TEMPFLAG. */
9036
9037 static void
9038 break_command_1 (const char *arg, int flag, int from_tty)
9039 {
9040 int tempflag = flag & BP_TEMPFLAG;
9041 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9042 ? bp_hardware_breakpoint
9043 : bp_breakpoint);
9044
9045 event_location_up location = string_to_event_location (&arg, current_language);
9046 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9047 (location.get (), false /* is_tracepoint */);
9048
9049 create_breakpoint (get_current_arch (),
9050 location.get (),
9051 NULL, 0, arg, false, 1 /* parse arg */,
9052 tempflag, type_wanted,
9053 0 /* Ignore count */,
9054 pending_break_support,
9055 ops,
9056 from_tty,
9057 1 /* enabled */,
9058 0 /* internal */,
9059 0);
9060 }
9061
9062 /* Helper function for break_command_1 and disassemble_command. */
9063
9064 void
9065 resolve_sal_pc (struct symtab_and_line *sal)
9066 {
9067 CORE_ADDR pc;
9068
9069 if (sal->pc == 0 && sal->symtab != NULL)
9070 {
9071 if (!find_line_pc (sal->symtab, sal->line, &pc))
9072 error (_("No line %d in file \"%s\"."),
9073 sal->line, symtab_to_filename_for_display (sal->symtab));
9074 sal->pc = pc;
9075
9076 /* If this SAL corresponds to a breakpoint inserted using a line
9077 number, then skip the function prologue if necessary. */
9078 if (sal->explicit_line)
9079 skip_prologue_sal (sal);
9080 }
9081
9082 if (sal->section == 0 && sal->symtab != NULL)
9083 {
9084 const struct blockvector *bv;
9085 const struct block *b;
9086 struct symbol *sym;
9087
9088 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9089 sal->symtab->compunit ());
9090 if (bv != NULL)
9091 {
9092 sym = block_linkage_function (b);
9093 if (sym != NULL)
9094 {
9095 fixup_symbol_section (sym, sal->symtab->compunit ()->objfile ());
9096 sal->section
9097 = sym->obj_section (sal->symtab->compunit ()->objfile ());
9098 }
9099 else
9100 {
9101 /* It really is worthwhile to have the section, so we'll
9102 just have to look harder. This case can be executed
9103 if we have line numbers but no functions (as can
9104 happen in assembly source). */
9105
9106 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9107 switch_to_program_space_and_thread (sal->pspace);
9108
9109 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9110 if (msym.minsym)
9111 sal->section = msym.obj_section ();
9112 }
9113 }
9114 }
9115 }
9116
9117 void
9118 break_command (const char *arg, int from_tty)
9119 {
9120 break_command_1 (arg, 0, from_tty);
9121 }
9122
9123 void
9124 tbreak_command (const char *arg, int from_tty)
9125 {
9126 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9127 }
9128
9129 static void
9130 hbreak_command (const char *arg, int from_tty)
9131 {
9132 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9133 }
9134
9135 static void
9136 thbreak_command (const char *arg, int from_tty)
9137 {
9138 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9139 }
9140
9141 /* The dynamic printf command is mostly like a regular breakpoint, but
9142 with a prewired command list consisting of a single output command,
9143 built from extra arguments supplied on the dprintf command
9144 line. */
9145
9146 static void
9147 dprintf_command (const char *arg, int from_tty)
9148 {
9149 event_location_up location = string_to_event_location (&arg, current_language);
9150
9151 /* If non-NULL, ARG should have been advanced past the location;
9152 the next character must be ','. */
9153 if (arg != NULL)
9154 {
9155 if (arg[0] != ',' || arg[1] == '\0')
9156 error (_("Format string required"));
9157 else
9158 {
9159 /* Skip the comma. */
9160 ++arg;
9161 }
9162 }
9163
9164 create_breakpoint (get_current_arch (),
9165 location.get (),
9166 NULL, 0, arg, false, 1 /* parse arg */,
9167 0, bp_dprintf,
9168 0 /* Ignore count */,
9169 pending_break_support,
9170 &base_breakpoint_ops,
9171 from_tty,
9172 1 /* enabled */,
9173 0 /* internal */,
9174 0);
9175 }
9176
9177 static void
9178 agent_printf_command (const char *arg, int from_tty)
9179 {
9180 error (_("May only run agent-printf on the target"));
9181 }
9182
9183 /* Implement the "breakpoint_hit" method for ranged breakpoints. */
9184
9185 int
9186 ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9187 const address_space *aspace,
9188 CORE_ADDR bp_addr,
9189 const target_waitstatus &ws)
9190 {
9191 if (ws.kind () != TARGET_WAITKIND_STOPPED
9192 || ws.sig () != GDB_SIGNAL_TRAP)
9193 return 0;
9194
9195 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9196 bl->length, aspace, bp_addr);
9197 }
9198
9199 /* Implement the "resources_needed" method for ranged breakpoints. */
9200
9201 int
9202 ranged_breakpoint::resources_needed (const struct bp_location *bl)
9203 {
9204 return target_ranged_break_num_registers ();
9205 }
9206
9207 /* Implement the "print_it" method for ranged breakpoints. */
9208
9209 enum print_stop_action
9210 ranged_breakpoint::print_it (const bpstat *bs) const
9211 {
9212 struct bp_location *bl = loc;
9213 struct ui_out *uiout = current_uiout;
9214
9215 gdb_assert (type == bp_hardware_breakpoint);
9216
9217 /* Ranged breakpoints have only one location. */
9218 gdb_assert (bl && bl->next == NULL);
9219
9220 annotate_breakpoint (number);
9221
9222 maybe_print_thread_hit_breakpoint (uiout);
9223
9224 if (disposition == disp_del)
9225 uiout->text ("Temporary ranged breakpoint ");
9226 else
9227 uiout->text ("Ranged breakpoint ");
9228 if (uiout->is_mi_like_p ())
9229 {
9230 uiout->field_string ("reason",
9231 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9232 uiout->field_string ("disp", bpdisp_text (disposition));
9233 }
9234 uiout->field_signed ("bkptno", number);
9235 uiout->text (", ");
9236
9237 return PRINT_SRC_AND_LOC;
9238 }
9239
9240 /* Implement the "print_one" method for ranged breakpoints. */
9241
9242 bool
9243 ranged_breakpoint::print_one (bp_location **last_loc) const
9244 {
9245 struct bp_location *bl = loc;
9246 struct value_print_options opts;
9247 struct ui_out *uiout = current_uiout;
9248
9249 /* Ranged breakpoints have only one location. */
9250 gdb_assert (bl && bl->next == NULL);
9251
9252 get_user_print_options (&opts);
9253
9254 if (opts.addressprint)
9255 /* We don't print the address range here, it will be printed later
9256 by print_one_detail_ranged_breakpoint. */
9257 uiout->field_skip ("addr");
9258 annotate_field (5);
9259 print_breakpoint_location (this, bl);
9260 *last_loc = bl;
9261
9262 return true;
9263 }
9264
9265 /* Implement the "print_one_detail" method for ranged breakpoints. */
9266
9267 void
9268 ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
9269 {
9270 CORE_ADDR address_start, address_end;
9271 struct bp_location *bl = loc;
9272 string_file stb;
9273
9274 gdb_assert (bl);
9275
9276 address_start = bl->address;
9277 address_end = address_start + bl->length - 1;
9278
9279 uiout->text ("\taddress range: ");
9280 stb.printf ("[%s, %s]",
9281 print_core_address (bl->gdbarch, address_start),
9282 print_core_address (bl->gdbarch, address_end));
9283 uiout->field_stream ("addr", stb);
9284 uiout->text ("\n");
9285 }
9286
9287 /* Implement the "print_mention" method for ranged breakpoints. */
9288
9289 void
9290 ranged_breakpoint::print_mention ()
9291 {
9292 struct bp_location *bl = loc;
9293 struct ui_out *uiout = current_uiout;
9294
9295 gdb_assert (bl);
9296 gdb_assert (type == bp_hardware_breakpoint);
9297
9298 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9299 number, paddress (bl->gdbarch, bl->address),
9300 paddress (bl->gdbarch, bl->address + bl->length - 1));
9301 }
9302
9303 /* Implement the "print_recreate" method for ranged breakpoints. */
9304
9305 void
9306 ranged_breakpoint::print_recreate (struct ui_file *fp)
9307 {
9308 gdb_printf (fp, "break-range %s, %s",
9309 event_location_to_string (location.get ()),
9310 event_location_to_string (location_range_end.get ()));
9311 print_recreate_thread (fp);
9312 }
9313
9314 /* Find the address where the end of the breakpoint range should be
9315 placed, given the SAL of the end of the range. This is so that if
9316 the user provides a line number, the end of the range is set to the
9317 last instruction of the given line. */
9318
9319 static CORE_ADDR
9320 find_breakpoint_range_end (struct symtab_and_line sal)
9321 {
9322 CORE_ADDR end;
9323
9324 /* If the user provided a PC value, use it. Otherwise,
9325 find the address of the end of the given location. */
9326 if (sal.explicit_pc)
9327 end = sal.pc;
9328 else
9329 {
9330 int ret;
9331 CORE_ADDR start;
9332
9333 ret = find_line_pc_range (sal, &start, &end);
9334 if (!ret)
9335 error (_("Could not find location of the end of the range."));
9336
9337 /* find_line_pc_range returns the start of the next line. */
9338 end--;
9339 }
9340
9341 return end;
9342 }
9343
9344 /* Implement the "break-range" CLI command. */
9345
9346 static void
9347 break_range_command (const char *arg, int from_tty)
9348 {
9349 const char *arg_start;
9350 struct linespec_result canonical_start, canonical_end;
9351 int bp_count, can_use_bp, length;
9352 CORE_ADDR end;
9353 struct breakpoint *b;
9354
9355 /* We don't support software ranged breakpoints. */
9356 if (target_ranged_break_num_registers () < 0)
9357 error (_("This target does not support hardware ranged breakpoints."));
9358
9359 bp_count = hw_breakpoint_used_count ();
9360 bp_count += target_ranged_break_num_registers ();
9361 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9362 bp_count, 0);
9363 if (can_use_bp < 0)
9364 error (_("Hardware breakpoints used exceeds limit."));
9365
9366 arg = skip_spaces (arg);
9367 if (arg == NULL || arg[0] == '\0')
9368 error(_("No address range specified."));
9369
9370 arg_start = arg;
9371 event_location_up start_location = string_to_event_location (&arg,
9372 current_language);
9373 parse_breakpoint_sals (start_location.get (), &canonical_start);
9374
9375 if (arg[0] != ',')
9376 error (_("Too few arguments."));
9377 else if (canonical_start.lsals.empty ())
9378 error (_("Could not find location of the beginning of the range."));
9379
9380 const linespec_sals &lsal_start = canonical_start.lsals[0];
9381
9382 if (canonical_start.lsals.size () > 1
9383 || lsal_start.sals.size () != 1)
9384 error (_("Cannot create a ranged breakpoint with multiple locations."));
9385
9386 const symtab_and_line &sal_start = lsal_start.sals[0];
9387 std::string addr_string_start (arg_start, arg - arg_start);
9388
9389 arg++; /* Skip the comma. */
9390 arg = skip_spaces (arg);
9391
9392 /* Parse the end location. */
9393
9394 arg_start = arg;
9395
9396 /* We call decode_line_full directly here instead of using
9397 parse_breakpoint_sals because we need to specify the start location's
9398 symtab and line as the default symtab and line for the end of the
9399 range. This makes it possible to have ranges like "foo.c:27, +14",
9400 where +14 means 14 lines from the start location. */
9401 event_location_up end_location = string_to_event_location (&arg,
9402 current_language);
9403 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9404 sal_start.symtab, sal_start.line,
9405 &canonical_end, NULL, NULL);
9406
9407 if (canonical_end.lsals.empty ())
9408 error (_("Could not find location of the end of the range."));
9409
9410 const linespec_sals &lsal_end = canonical_end.lsals[0];
9411 if (canonical_end.lsals.size () > 1
9412 || lsal_end.sals.size () != 1)
9413 error (_("Cannot create a ranged breakpoint with multiple locations."));
9414
9415 const symtab_and_line &sal_end = lsal_end.sals[0];
9416
9417 end = find_breakpoint_range_end (sal_end);
9418 if (sal_start.pc > end)
9419 error (_("Invalid address range, end precedes start."));
9420
9421 length = end - sal_start.pc + 1;
9422 if (length < 0)
9423 /* Length overflowed. */
9424 error (_("Address range too large."));
9425 else if (length == 1)
9426 {
9427 /* This range is simple enough to be handled by
9428 the `hbreak' command. */
9429 hbreak_command (&addr_string_start[0], 1);
9430
9431 return;
9432 }
9433
9434 /* Now set up the breakpoint. */
9435 std::unique_ptr<breakpoint> br (new ranged_breakpoint ());
9436 init_raw_breakpoint (br.get (), get_current_arch (),
9437 sal_start, bp_hardware_breakpoint);
9438 b = add_to_breakpoint_chain (std::move (br));
9439
9440 set_breakpoint_count (breakpoint_count + 1);
9441 b->number = breakpoint_count;
9442 b->disposition = disp_donttouch;
9443 b->location = std::move (start_location);
9444 b->location_range_end = std::move (end_location);
9445 b->loc->length = length;
9446
9447 mention (b);
9448 gdb::observers::breakpoint_created.notify (b);
9449 update_global_location_list (UGLL_MAY_INSERT);
9450 }
9451
9452 /* Return non-zero if EXP is verified as constant. Returned zero
9453 means EXP is variable. Also the constant detection may fail for
9454 some constant expressions and in such case still falsely return
9455 zero. */
9456
9457 static bool
9458 watchpoint_exp_is_const (const struct expression *exp)
9459 {
9460 return exp->op->constant_p ();
9461 }
9462
9463 /* Implement the "re_set" method for watchpoints. */
9464
9465 void
9466 watchpoint::re_set ()
9467 {
9468 /* Watchpoint can be either on expression using entirely global
9469 variables, or it can be on local variables.
9470
9471 Watchpoints of the first kind are never auto-deleted, and even
9472 persist across program restarts. Since they can use variables
9473 from shared libraries, we need to reparse expression as libraries
9474 are loaded and unloaded.
9475
9476 Watchpoints on local variables can also change meaning as result
9477 of solib event. For example, if a watchpoint uses both a local
9478 and a global variables in expression, it's a local watchpoint,
9479 but unloading of a shared library will make the expression
9480 invalid. This is not a very common use case, but we still
9481 re-evaluate expression, to avoid surprises to the user.
9482
9483 Note that for local watchpoints, we re-evaluate it only if
9484 watchpoints frame id is still valid. If it's not, it means the
9485 watchpoint is out of scope and will be deleted soon. In fact,
9486 I'm not sure we'll ever be called in this case.
9487
9488 If a local watchpoint's frame id is still valid, then
9489 exp_valid_block is likewise valid, and we can safely use it.
9490
9491 Don't do anything about disabled watchpoints, since they will be
9492 reevaluated again when enabled. */
9493 update_watchpoint (this, 1 /* reparse */);
9494 }
9495
9496 /* Implement the "insert" method for hardware watchpoints. */
9497
9498 int
9499 watchpoint::insert_location (struct bp_location *bl)
9500 {
9501 int length = exact ? 1 : bl->length;
9502
9503 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9504 cond_exp.get ());
9505 }
9506
9507 /* Implement the "remove" method for hardware watchpoints. */
9508
9509 int
9510 watchpoint::remove_location (struct bp_location *bl,
9511 enum remove_bp_reason reason)
9512 {
9513 int length = exact ? 1 : bl->length;
9514
9515 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9516 cond_exp.get ());
9517 }
9518
9519 int
9520 watchpoint::breakpoint_hit (const struct bp_location *bl,
9521 const address_space *aspace, CORE_ADDR bp_addr,
9522 const target_waitstatus &ws)
9523 {
9524 struct breakpoint *b = bl->owner;
9525
9526 /* Continuable hardware watchpoints are treated as non-existent if the
9527 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9528 some data address). Otherwise gdb won't stop on a break instruction
9529 in the code (not from a breakpoint) when a hardware watchpoint has
9530 been defined. Also skip watchpoints which we know did not trigger
9531 (did not match the data address). */
9532 if (is_hardware_watchpoint (b)
9533 && watchpoint_triggered == watch_triggered_no)
9534 return 0;
9535
9536 return 1;
9537 }
9538
9539 void
9540 watchpoint::check_status (bpstat *bs)
9541 {
9542 gdb_assert (is_watchpoint (bs->breakpoint_at));
9543
9544 bpstat_check_watchpoint (bs);
9545 }
9546
9547 /* Implement the "resources_needed" method for hardware
9548 watchpoints. */
9549
9550 int
9551 watchpoint::resources_needed (const struct bp_location *bl)
9552 {
9553 int length = exact? 1 : bl->length;
9554
9555 return target_region_ok_for_hw_watchpoint (bl->address, length);
9556 }
9557
9558 /* Implement the "works_in_software_mode" method for hardware
9559 watchpoints. */
9560
9561 bool
9562 watchpoint::works_in_software_mode () const
9563 {
9564 /* Read and access watchpoints only work with hardware support. */
9565 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9566 }
9567
9568 enum print_stop_action
9569 watchpoint::print_it (const bpstat *bs) const
9570 {
9571 struct breakpoint *b;
9572 enum print_stop_action result;
9573 struct ui_out *uiout = current_uiout;
9574
9575 gdb_assert (bs->bp_location_at != NULL);
9576
9577 b = bs->breakpoint_at;
9578
9579 annotate_watchpoint (b->number);
9580 maybe_print_thread_hit_breakpoint (uiout);
9581
9582 string_file stb;
9583
9584 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9585 switch (b->type)
9586 {
9587 case bp_watchpoint:
9588 case bp_hardware_watchpoint:
9589 if (uiout->is_mi_like_p ())
9590 uiout->field_string
9591 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9592 mention (b);
9593 tuple_emitter.emplace (uiout, "value");
9594 uiout->text ("\nOld value = ");
9595 watchpoint_value_print (bs->old_val.get (), &stb);
9596 uiout->field_stream ("old", stb);
9597 uiout->text ("\nNew value = ");
9598 watchpoint_value_print (val.get (), &stb);
9599 uiout->field_stream ("new", stb);
9600 uiout->text ("\n");
9601 /* More than one watchpoint may have been triggered. */
9602 result = PRINT_UNKNOWN;
9603 break;
9604
9605 case bp_read_watchpoint:
9606 if (uiout->is_mi_like_p ())
9607 uiout->field_string
9608 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9609 mention (b);
9610 tuple_emitter.emplace (uiout, "value");
9611 uiout->text ("\nValue = ");
9612 watchpoint_value_print (val.get (), &stb);
9613 uiout->field_stream ("value", stb);
9614 uiout->text ("\n");
9615 result = PRINT_UNKNOWN;
9616 break;
9617
9618 case bp_access_watchpoint:
9619 if (bs->old_val != NULL)
9620 {
9621 if (uiout->is_mi_like_p ())
9622 uiout->field_string
9623 ("reason",
9624 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9625 mention (b);
9626 tuple_emitter.emplace (uiout, "value");
9627 uiout->text ("\nOld value = ");
9628 watchpoint_value_print (bs->old_val.get (), &stb);
9629 uiout->field_stream ("old", stb);
9630 uiout->text ("\nNew value = ");
9631 }
9632 else
9633 {
9634 mention (b);
9635 if (uiout->is_mi_like_p ())
9636 uiout->field_string
9637 ("reason",
9638 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9639 tuple_emitter.emplace (uiout, "value");
9640 uiout->text ("\nValue = ");
9641 }
9642 watchpoint_value_print (val.get (), &stb);
9643 uiout->field_stream ("new", stb);
9644 uiout->text ("\n");
9645 result = PRINT_UNKNOWN;
9646 break;
9647 default:
9648 result = PRINT_UNKNOWN;
9649 }
9650
9651 return result;
9652 }
9653
9654 /* Implement the "print_mention" method for hardware watchpoints. */
9655
9656 void
9657 watchpoint::print_mention ()
9658 {
9659 struct ui_out *uiout = current_uiout;
9660 const char *tuple_name;
9661
9662 switch (type)
9663 {
9664 case bp_watchpoint:
9665 uiout->text ("Watchpoint ");
9666 tuple_name = "wpt";
9667 break;
9668 case bp_hardware_watchpoint:
9669 uiout->text ("Hardware watchpoint ");
9670 tuple_name = "wpt";
9671 break;
9672 case bp_read_watchpoint:
9673 uiout->text ("Hardware read watchpoint ");
9674 tuple_name = "hw-rwpt";
9675 break;
9676 case bp_access_watchpoint:
9677 uiout->text ("Hardware access (read/write) watchpoint ");
9678 tuple_name = "hw-awpt";
9679 break;
9680 default:
9681 internal_error (__FILE__, __LINE__,
9682 _("Invalid hardware watchpoint type."));
9683 }
9684
9685 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9686 uiout->field_signed ("number", number);
9687 uiout->text (": ");
9688 uiout->field_string ("exp", exp_string.get ());
9689 }
9690
9691 /* Implement the "print_recreate" method for watchpoints. */
9692
9693 void
9694 watchpoint::print_recreate (struct ui_file *fp)
9695 {
9696 switch (type)
9697 {
9698 case bp_watchpoint:
9699 case bp_hardware_watchpoint:
9700 gdb_printf (fp, "watch");
9701 break;
9702 case bp_read_watchpoint:
9703 gdb_printf (fp, "rwatch");
9704 break;
9705 case bp_access_watchpoint:
9706 gdb_printf (fp, "awatch");
9707 break;
9708 default:
9709 internal_error (__FILE__, __LINE__,
9710 _("Invalid watchpoint type."));
9711 }
9712
9713 gdb_printf (fp, " %s", exp_string.get ());
9714 print_recreate_thread (fp);
9715 }
9716
9717 /* Implement the "explains_signal" method for watchpoints. */
9718
9719 bool
9720 watchpoint::explains_signal (enum gdb_signal sig)
9721 {
9722 /* A software watchpoint cannot cause a signal other than
9723 GDB_SIGNAL_TRAP. */
9724 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
9725 return false;
9726
9727 return true;
9728 }
9729
9730 struct masked_watchpoint : public watchpoint
9731 {
9732 int insert_location (struct bp_location *) override;
9733 int remove_location (struct bp_location *,
9734 enum remove_bp_reason reason) override;
9735 int resources_needed (const struct bp_location *) override;
9736 bool works_in_software_mode () const override;
9737 enum print_stop_action print_it (const bpstat *bs) const override;
9738 void print_one_detail (struct ui_out *) const override;
9739 void print_mention () override;
9740 void print_recreate (struct ui_file *fp) override;
9741 };
9742
9743 /* Implement the "insert" method for masked hardware watchpoints. */
9744
9745 int
9746 masked_watchpoint::insert_location (struct bp_location *bl)
9747 {
9748 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
9749 bl->watchpoint_type);
9750 }
9751
9752 /* Implement the "remove" method for masked hardware watchpoints. */
9753
9754 int
9755 masked_watchpoint::remove_location (struct bp_location *bl,
9756 enum remove_bp_reason reason)
9757 {
9758 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
9759 bl->watchpoint_type);
9760 }
9761
9762 /* Implement the "resources_needed" method for masked hardware
9763 watchpoints. */
9764
9765 int
9766 masked_watchpoint::resources_needed (const struct bp_location *bl)
9767 {
9768 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
9769 }
9770
9771 /* Implement the "works_in_software_mode" method for masked hardware
9772 watchpoints. */
9773
9774 bool
9775 masked_watchpoint::works_in_software_mode () const
9776 {
9777 return false;
9778 }
9779
9780 /* Implement the "print_it" method for masked hardware
9781 watchpoints. */
9782
9783 enum print_stop_action
9784 masked_watchpoint::print_it (const bpstat *bs) const
9785 {
9786 struct breakpoint *b = bs->breakpoint_at;
9787 struct ui_out *uiout = current_uiout;
9788
9789 /* Masked watchpoints have only one location. */
9790 gdb_assert (b->loc && b->loc->next == NULL);
9791
9792 annotate_watchpoint (b->number);
9793 maybe_print_thread_hit_breakpoint (uiout);
9794
9795 switch (b->type)
9796 {
9797 case bp_hardware_watchpoint:
9798 if (uiout->is_mi_like_p ())
9799 uiout->field_string
9800 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9801 break;
9802
9803 case bp_read_watchpoint:
9804 if (uiout->is_mi_like_p ())
9805 uiout->field_string
9806 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9807 break;
9808
9809 case bp_access_watchpoint:
9810 if (uiout->is_mi_like_p ())
9811 uiout->field_string
9812 ("reason",
9813 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9814 break;
9815 default:
9816 internal_error (__FILE__, __LINE__,
9817 _("Invalid hardware watchpoint type."));
9818 }
9819
9820 mention (b);
9821 uiout->text (_("\n\
9822 Check the underlying instruction at PC for the memory\n\
9823 address and value which triggered this watchpoint.\n"));
9824 uiout->text ("\n");
9825
9826 /* More than one watchpoint may have been triggered. */
9827 return PRINT_UNKNOWN;
9828 }
9829
9830 /* Implement the "print_one_detail" method for masked hardware
9831 watchpoints. */
9832
9833 void
9834 masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9835 {
9836 /* Masked watchpoints have only one location. */
9837 gdb_assert (loc && loc->next == NULL);
9838
9839 uiout->text ("\tmask ");
9840 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
9841 uiout->text ("\n");
9842 }
9843
9844 /* Implement the "print_mention" method for masked hardware
9845 watchpoints. */
9846
9847 void
9848 masked_watchpoint::print_mention ()
9849 {
9850 struct ui_out *uiout = current_uiout;
9851 const char *tuple_name;
9852
9853 switch (type)
9854 {
9855 case bp_hardware_watchpoint:
9856 uiout->text ("Masked hardware watchpoint ");
9857 tuple_name = "wpt";
9858 break;
9859 case bp_read_watchpoint:
9860 uiout->text ("Masked hardware read watchpoint ");
9861 tuple_name = "hw-rwpt";
9862 break;
9863 case bp_access_watchpoint:
9864 uiout->text ("Masked hardware access (read/write) watchpoint ");
9865 tuple_name = "hw-awpt";
9866 break;
9867 default:
9868 internal_error (__FILE__, __LINE__,
9869 _("Invalid hardware watchpoint type."));
9870 }
9871
9872 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9873 uiout->field_signed ("number", number);
9874 uiout->text (": ");
9875 uiout->field_string ("exp", exp_string.get ());
9876 }
9877
9878 /* Implement the "print_recreate" method for masked hardware
9879 watchpoints. */
9880
9881 void
9882 masked_watchpoint::print_recreate (struct ui_file *fp)
9883 {
9884 switch (type)
9885 {
9886 case bp_hardware_watchpoint:
9887 gdb_printf (fp, "watch");
9888 break;
9889 case bp_read_watchpoint:
9890 gdb_printf (fp, "rwatch");
9891 break;
9892 case bp_access_watchpoint:
9893 gdb_printf (fp, "awatch");
9894 break;
9895 default:
9896 internal_error (__FILE__, __LINE__,
9897 _("Invalid hardware watchpoint type."));
9898 }
9899
9900 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
9901 phex (hw_wp_mask, sizeof (CORE_ADDR)));
9902 print_recreate_thread (fp);
9903 }
9904
9905 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
9906
9907 static bool
9908 is_masked_watchpoint (const struct breakpoint *b)
9909 {
9910 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9911 }
9912
9913 /* accessflag: hw_write: watch write,
9914 hw_read: watch read,
9915 hw_access: watch access (read or write) */
9916 static void
9917 watch_command_1 (const char *arg, int accessflag, int from_tty,
9918 bool just_location, bool internal)
9919 {
9920 struct breakpoint *scope_breakpoint = NULL;
9921 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
9922 struct value *result;
9923 int saved_bitpos = 0, saved_bitsize = 0;
9924 const char *exp_start = NULL;
9925 const char *exp_end = NULL;
9926 const char *tok, *end_tok;
9927 int toklen = -1;
9928 const char *cond_start = NULL;
9929 const char *cond_end = NULL;
9930 enum bptype bp_type;
9931 int thread = -1;
9932 /* Flag to indicate whether we are going to use masks for
9933 the hardware watchpoint. */
9934 bool use_mask = false;
9935 CORE_ADDR mask = 0;
9936 int task = 0;
9937
9938 /* Make sure that we actually have parameters to parse. */
9939 if (arg != NULL && arg[0] != '\0')
9940 {
9941 const char *value_start;
9942
9943 exp_end = arg + strlen (arg);
9944
9945 /* Look for "parameter value" pairs at the end
9946 of the arguments string. */
9947 for (tok = exp_end - 1; tok > arg; tok--)
9948 {
9949 /* Skip whitespace at the end of the argument list. */
9950 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9951 tok--;
9952
9953 /* Find the beginning of the last token.
9954 This is the value of the parameter. */
9955 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9956 tok--;
9957 value_start = tok + 1;
9958
9959 /* Skip whitespace. */
9960 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9961 tok--;
9962
9963 end_tok = tok;
9964
9965 /* Find the beginning of the second to last token.
9966 This is the parameter itself. */
9967 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9968 tok--;
9969 tok++;
9970 toklen = end_tok - tok + 1;
9971
9972 if (toklen == 6 && startswith (tok, "thread"))
9973 {
9974 struct thread_info *thr;
9975 /* At this point we've found a "thread" token, which means
9976 the user is trying to set a watchpoint that triggers
9977 only in a specific thread. */
9978 const char *endp;
9979
9980 if (thread != -1)
9981 error(_("You can specify only one thread."));
9982
9983 /* Extract the thread ID from the next token. */
9984 thr = parse_thread_id (value_start, &endp);
9985
9986 /* Check if the user provided a valid thread ID. */
9987 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9988 invalid_thread_id_error (value_start);
9989
9990 thread = thr->global_num;
9991 }
9992 else if (toklen == 4 && startswith (tok, "task"))
9993 {
9994 char *tmp;
9995
9996 task = strtol (value_start, &tmp, 0);
9997 if (tmp == value_start)
9998 error (_("Junk after task keyword."));
9999 if (!valid_task_id (task))
10000 error (_("Unknown task %d."), task);
10001 }
10002 else if (toklen == 4 && startswith (tok, "mask"))
10003 {
10004 /* We've found a "mask" token, which means the user wants to
10005 create a hardware watchpoint that is going to have the mask
10006 facility. */
10007 struct value *mask_value, *mark;
10008
10009 if (use_mask)
10010 error(_("You can specify only one mask."));
10011
10012 use_mask = just_location = true;
10013
10014 mark = value_mark ();
10015 mask_value = parse_to_comma_and_eval (&value_start);
10016 mask = value_as_address (mask_value);
10017 value_free_to_mark (mark);
10018 }
10019 else
10020 /* We didn't recognize what we found. We should stop here. */
10021 break;
10022
10023 /* Truncate the string and get rid of the "parameter value" pair before
10024 the arguments string is parsed by the parse_exp_1 function. */
10025 exp_end = tok;
10026 }
10027 }
10028 else
10029 exp_end = arg;
10030
10031 /* Parse the rest of the arguments. From here on out, everything
10032 is in terms of a newly allocated string instead of the original
10033 ARG. */
10034 std::string expression (arg, exp_end - arg);
10035 exp_start = arg = expression.c_str ();
10036 innermost_block_tracker tracker;
10037 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10038 exp_end = arg;
10039 /* Remove trailing whitespace from the expression before saving it.
10040 This makes the eventual display of the expression string a bit
10041 prettier. */
10042 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10043 --exp_end;
10044
10045 /* Checking if the expression is not constant. */
10046 if (watchpoint_exp_is_const (exp.get ()))
10047 {
10048 int len;
10049
10050 len = exp_end - exp_start;
10051 while (len > 0 && isspace (exp_start[len - 1]))
10052 len--;
10053 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10054 }
10055
10056 exp_valid_block = tracker.block ();
10057 struct value *mark = value_mark ();
10058 struct value *val_as_value = nullptr;
10059 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10060 just_location);
10061
10062 if (val_as_value != NULL && just_location)
10063 {
10064 saved_bitpos = value_bitpos (val_as_value);
10065 saved_bitsize = value_bitsize (val_as_value);
10066 }
10067
10068 value_ref_ptr val;
10069 if (just_location)
10070 {
10071 int ret;
10072
10073 exp_valid_block = NULL;
10074 val = release_value (value_addr (result));
10075 value_free_to_mark (mark);
10076
10077 if (use_mask)
10078 {
10079 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10080 mask);
10081 if (ret == -1)
10082 error (_("This target does not support masked watchpoints."));
10083 else if (ret == -2)
10084 error (_("Invalid mask or memory region."));
10085 }
10086 }
10087 else if (val_as_value != NULL)
10088 val = release_value (val_as_value);
10089
10090 tok = skip_spaces (arg);
10091 end_tok = skip_to_space (tok);
10092
10093 toklen = end_tok - tok;
10094 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10095 {
10096 tok = cond_start = end_tok + 1;
10097 innermost_block_tracker if_tracker;
10098 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10099
10100 /* The watchpoint expression may not be local, but the condition
10101 may still be. E.g.: `watch global if local > 0'. */
10102 cond_exp_valid_block = if_tracker.block ();
10103
10104 cond_end = tok;
10105 }
10106 if (*tok)
10107 error (_("Junk at end of command."));
10108
10109 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10110
10111 /* Save this because create_internal_breakpoint below invalidates
10112 'wp_frame'. */
10113 frame_id watchpoint_frame = get_frame_id (wp_frame);
10114
10115 /* If the expression is "local", then set up a "watchpoint scope"
10116 breakpoint at the point where we've left the scope of the watchpoint
10117 expression. Create the scope breakpoint before the watchpoint, so
10118 that we will encounter it first in bpstat_stop_status. */
10119 if (exp_valid_block != NULL && wp_frame != NULL)
10120 {
10121 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10122
10123 if (frame_id_p (caller_frame_id))
10124 {
10125 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10126 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10127
10128 scope_breakpoint
10129 = create_internal_breakpoint (caller_arch, caller_pc,
10130 bp_watchpoint_scope);
10131
10132 /* create_internal_breakpoint could invalidate WP_FRAME. */
10133 wp_frame = NULL;
10134
10135 scope_breakpoint->enable_state = bp_enabled;
10136
10137 /* Automatically delete the breakpoint when it hits. */
10138 scope_breakpoint->disposition = disp_del;
10139
10140 /* Only break in the proper frame (help with recursion). */
10141 scope_breakpoint->frame_id = caller_frame_id;
10142
10143 /* Set the address at which we will stop. */
10144 scope_breakpoint->loc->gdbarch = caller_arch;
10145 scope_breakpoint->loc->requested_address = caller_pc;
10146 scope_breakpoint->loc->address
10147 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10148 scope_breakpoint->loc->requested_address,
10149 scope_breakpoint->type);
10150 }
10151 }
10152
10153 /* Now set up the breakpoint. We create all watchpoints as hardware
10154 watchpoints here even if hardware watchpoints are turned off, a call
10155 to update_watchpoint later in this function will cause the type to
10156 drop back to bp_watchpoint (software watchpoint) if required. */
10157
10158 if (accessflag == hw_read)
10159 bp_type = bp_read_watchpoint;
10160 else if (accessflag == hw_access)
10161 bp_type = bp_access_watchpoint;
10162 else
10163 bp_type = bp_hardware_watchpoint;
10164
10165 std::unique_ptr<watchpoint> w;
10166 if (use_mask)
10167 w.reset (new masked_watchpoint ());
10168 else
10169 w.reset (new watchpoint ());
10170 init_raw_breakpoint_without_location (w.get (), nullptr, bp_type);
10171
10172 w->thread = thread;
10173 w->task = task;
10174 w->disposition = disp_donttouch;
10175 w->pspace = current_program_space;
10176 w->exp = std::move (exp);
10177 w->exp_valid_block = exp_valid_block;
10178 w->cond_exp_valid_block = cond_exp_valid_block;
10179 if (just_location)
10180 {
10181 struct type *t = value_type (val.get ());
10182 CORE_ADDR addr = value_as_address (val.get ());
10183
10184 w->exp_string_reparse
10185 = current_language->watch_location_expression (t, addr);
10186
10187 w->exp_string = xstrprintf ("-location %.*s",
10188 (int) (exp_end - exp_start), exp_start);
10189 }
10190 else
10191 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10192
10193 if (use_mask)
10194 {
10195 w->hw_wp_mask = mask;
10196 }
10197 else
10198 {
10199 w->val = val;
10200 w->val_bitpos = saved_bitpos;
10201 w->val_bitsize = saved_bitsize;
10202 w->val_valid = true;
10203 }
10204
10205 if (cond_start)
10206 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10207 else
10208 w->cond_string = 0;
10209
10210 if (frame_id_p (watchpoint_frame))
10211 {
10212 w->watchpoint_frame = watchpoint_frame;
10213 w->watchpoint_thread = inferior_ptid;
10214 }
10215 else
10216 {
10217 w->watchpoint_frame = null_frame_id;
10218 w->watchpoint_thread = null_ptid;
10219 }
10220
10221 if (scope_breakpoint != NULL)
10222 {
10223 /* The scope breakpoint is related to the watchpoint. We will
10224 need to act on them together. */
10225 w->related_breakpoint = scope_breakpoint;
10226 scope_breakpoint->related_breakpoint = w.get ();
10227 }
10228
10229 if (!just_location)
10230 value_free_to_mark (mark);
10231
10232 /* Finally update the new watchpoint. This creates the locations
10233 that should be inserted. */
10234 update_watchpoint (w.get (), 1);
10235
10236 install_breakpoint (internal, std::move (w), 1);
10237 }
10238
10239 /* Return count of debug registers needed to watch the given expression.
10240 If the watchpoint cannot be handled in hardware return zero. */
10241
10242 static int
10243 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10244 {
10245 int found_memory_cnt = 0;
10246
10247 /* Did the user specifically forbid us to use hardware watchpoints? */
10248 if (!can_use_hw_watchpoints)
10249 return 0;
10250
10251 gdb_assert (!vals.empty ());
10252 struct value *head = vals[0].get ();
10253
10254 /* Make sure that the value of the expression depends only upon
10255 memory contents, and values computed from them within GDB. If we
10256 find any register references or function calls, we can't use a
10257 hardware watchpoint.
10258
10259 The idea here is that evaluating an expression generates a series
10260 of values, one holding the value of every subexpression. (The
10261 expression a*b+c has five subexpressions: a, b, a*b, c, and
10262 a*b+c.) GDB's values hold almost enough information to establish
10263 the criteria given above --- they identify memory lvalues,
10264 register lvalues, computed values, etcetera. So we can evaluate
10265 the expression, and then scan the chain of values that leaves
10266 behind to decide whether we can detect any possible change to the
10267 expression's final value using only hardware watchpoints.
10268
10269 However, I don't think that the values returned by inferior
10270 function calls are special in any way. So this function may not
10271 notice that an expression involving an inferior function call
10272 can't be watched with hardware watchpoints. FIXME. */
10273 for (const value_ref_ptr &iter : vals)
10274 {
10275 struct value *v = iter.get ();
10276
10277 if (VALUE_LVAL (v) == lval_memory)
10278 {
10279 if (v != head && value_lazy (v))
10280 /* A lazy memory lvalue in the chain is one that GDB never
10281 needed to fetch; we either just used its address (e.g.,
10282 `a' in `a.b') or we never needed it at all (e.g., `a'
10283 in `a,b'). This doesn't apply to HEAD; if that is
10284 lazy then it was not readable, but watch it anyway. */
10285 ;
10286 else
10287 {
10288 /* Ahh, memory we actually used! Check if we can cover
10289 it with hardware watchpoints. */
10290 struct type *vtype = check_typedef (value_type (v));
10291
10292 /* We only watch structs and arrays if user asked for it
10293 explicitly, never if they just happen to appear in a
10294 middle of some value chain. */
10295 if (v == head
10296 || (vtype->code () != TYPE_CODE_STRUCT
10297 && vtype->code () != TYPE_CODE_ARRAY))
10298 {
10299 CORE_ADDR vaddr = value_address (v);
10300 int len;
10301 int num_regs;
10302
10303 len = (target_exact_watchpoints
10304 && is_scalar_type_recursive (vtype))?
10305 1 : TYPE_LENGTH (value_type (v));
10306
10307 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10308 if (!num_regs)
10309 return 0;
10310 else
10311 found_memory_cnt += num_regs;
10312 }
10313 }
10314 }
10315 else if (VALUE_LVAL (v) != not_lval
10316 && deprecated_value_modifiable (v) == 0)
10317 return 0; /* These are values from the history (e.g., $1). */
10318 else if (VALUE_LVAL (v) == lval_register)
10319 return 0; /* Cannot watch a register with a HW watchpoint. */
10320 }
10321
10322 /* The expression itself looks suitable for using a hardware
10323 watchpoint, but give the target machine a chance to reject it. */
10324 return found_memory_cnt;
10325 }
10326
10327 void
10328 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10329 {
10330 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10331 }
10332
10333 /* Options for the watch, awatch, and rwatch commands. */
10334
10335 struct watch_options
10336 {
10337 /* For -location. */
10338 bool location = false;
10339 };
10340
10341 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10342
10343 Historically GDB always accepted both '-location' and '-l' flags for
10344 these commands (both flags being synonyms). When converting to the
10345 newer option scheme only '-location' is added here. That's fine (for
10346 backward compatibility) as any non-ambiguous prefix of a flag will be
10347 accepted, so '-l', '-loc', are now all accepted.
10348
10349 What this means is that, if in the future, we add any new flag here
10350 that starts with '-l' then this will break backward compatibility, so
10351 please, don't do that! */
10352
10353 static const gdb::option::option_def watch_option_defs[] = {
10354 gdb::option::flag_option_def<watch_options> {
10355 "location",
10356 [] (watch_options *opt) { return &opt->location; },
10357 N_("\
10358 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10359 -l can be used as a short form of -location."),
10360 },
10361 };
10362
10363 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10364 commands. */
10365
10366 static gdb::option::option_def_group
10367 make_watch_options_def_group (watch_options *opts)
10368 {
10369 return {{watch_option_defs}, opts};
10370 }
10371
10372 /* A helper function that looks for the "-location" argument and then
10373 calls watch_command_1. */
10374
10375 static void
10376 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10377 {
10378 watch_options opts;
10379 auto grp = make_watch_options_def_group (&opts);
10380 gdb::option::process_options
10381 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10382 if (arg != nullptr && *arg == '\0')
10383 arg = nullptr;
10384
10385 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10386 }
10387
10388 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10389 static void
10390 watch_command_completer (struct cmd_list_element *ignore,
10391 completion_tracker &tracker,
10392 const char *text, const char * /*word*/)
10393 {
10394 const auto group = make_watch_options_def_group (nullptr);
10395 if (gdb::option::complete_options
10396 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10397 return;
10398
10399 const char *word = advance_to_expression_complete_word_point (tracker, text);
10400 expression_completer (ignore, tracker, text, word);
10401 }
10402
10403 static void
10404 watch_command (const char *arg, int from_tty)
10405 {
10406 watch_maybe_just_location (arg, hw_write, from_tty);
10407 }
10408
10409 void
10410 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10411 {
10412 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10413 }
10414
10415 static void
10416 rwatch_command (const char *arg, int from_tty)
10417 {
10418 watch_maybe_just_location (arg, hw_read, from_tty);
10419 }
10420
10421 void
10422 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
10423 {
10424 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10425 }
10426
10427 static void
10428 awatch_command (const char *arg, int from_tty)
10429 {
10430 watch_maybe_just_location (arg, hw_access, from_tty);
10431 }
10432 \f
10433
10434 /* Data for the FSM that manages the until(location)/advance commands
10435 in infcmd.c. Here because it uses the mechanisms of
10436 breakpoints. */
10437
10438 struct until_break_fsm : public thread_fsm
10439 {
10440 /* The thread that was current when the command was executed. */
10441 int thread;
10442
10443 /* The breakpoint set at the return address in the caller frame,
10444 plus breakpoints at all the destination locations. */
10445 std::vector<breakpoint_up> breakpoints;
10446
10447 until_break_fsm (struct interp *cmd_interp, int thread,
10448 std::vector<breakpoint_up> &&breakpoints)
10449 : thread_fsm (cmd_interp),
10450 thread (thread),
10451 breakpoints (std::move (breakpoints))
10452 {
10453 }
10454
10455 void clean_up (struct thread_info *thread) override;
10456 bool should_stop (struct thread_info *thread) override;
10457 enum async_reply_reason do_async_reply_reason () override;
10458 };
10459
10460 /* Implementation of the 'should_stop' FSM method for the
10461 until(location)/advance commands. */
10462
10463 bool
10464 until_break_fsm::should_stop (struct thread_info *tp)
10465 {
10466 for (const breakpoint_up &bp : breakpoints)
10467 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10468 bp.get ()) != NULL)
10469 {
10470 set_finished ();
10471 break;
10472 }
10473
10474 return true;
10475 }
10476
10477 /* Implementation of the 'clean_up' FSM method for the
10478 until(location)/advance commands. */
10479
10480 void
10481 until_break_fsm::clean_up (struct thread_info *)
10482 {
10483 /* Clean up our temporary breakpoints. */
10484 breakpoints.clear ();
10485 delete_longjmp_breakpoint (thread);
10486 }
10487
10488 /* Implementation of the 'async_reply_reason' FSM method for the
10489 until(location)/advance commands. */
10490
10491 enum async_reply_reason
10492 until_break_fsm::do_async_reply_reason ()
10493 {
10494 return EXEC_ASYNC_LOCATION_REACHED;
10495 }
10496
10497 void
10498 until_break_command (const char *arg, int from_tty, int anywhere)
10499 {
10500 struct frame_info *frame;
10501 struct gdbarch *frame_gdbarch;
10502 struct frame_id stack_frame_id;
10503 struct frame_id caller_frame_id;
10504 int thread;
10505 struct thread_info *tp;
10506
10507 clear_proceed_status (0);
10508
10509 /* Set a breakpoint where the user wants it and at return from
10510 this function. */
10511
10512 event_location_up location = string_to_event_location (&arg, current_language);
10513
10514 std::vector<symtab_and_line> sals
10515 = (last_displayed_sal_is_valid ()
10516 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10517 get_last_displayed_symtab (),
10518 get_last_displayed_line ())
10519 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
10520 NULL, NULL, 0));
10521
10522 if (sals.empty ())
10523 error (_("Couldn't get information on specified line."));
10524
10525 if (*arg)
10526 error (_("Junk at end of arguments."));
10527
10528 tp = inferior_thread ();
10529 thread = tp->global_num;
10530
10531 /* Note linespec handling above invalidates the frame chain.
10532 Installing a breakpoint also invalidates the frame chain (as it
10533 may need to switch threads), so do any frame handling before
10534 that. */
10535
10536 frame = get_selected_frame (NULL);
10537 frame_gdbarch = get_frame_arch (frame);
10538 stack_frame_id = get_stack_frame_id (frame);
10539 caller_frame_id = frame_unwind_caller_id (frame);
10540
10541 /* Keep within the current frame, or in frames called by the current
10542 one. */
10543
10544 std::vector<breakpoint_up> breakpoints;
10545
10546 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10547
10548 if (frame_id_p (caller_frame_id))
10549 {
10550 struct symtab_and_line sal2;
10551 struct gdbarch *caller_gdbarch;
10552
10553 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10554 sal2.pc = frame_unwind_caller_pc (frame);
10555 caller_gdbarch = frame_unwind_caller_arch (frame);
10556
10557 breakpoint_up caller_breakpoint
10558 = set_momentary_breakpoint (caller_gdbarch, sal2,
10559 caller_frame_id, bp_until);
10560 breakpoints.emplace_back (std::move (caller_breakpoint));
10561
10562 set_longjmp_breakpoint (tp, caller_frame_id);
10563 lj_deleter.emplace (thread);
10564 }
10565
10566 /* set_momentary_breakpoint could invalidate FRAME. */
10567 frame = NULL;
10568
10569 /* If the user told us to continue until a specified location, we
10570 don't specify a frame at which we need to stop. Otherwise,
10571 specify the selected frame, because we want to stop only at the
10572 very same frame. */
10573 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10574
10575 for (symtab_and_line &sal : sals)
10576 {
10577 resolve_sal_pc (&sal);
10578
10579 breakpoint_up location_breakpoint
10580 = set_momentary_breakpoint (frame_gdbarch, sal,
10581 stop_frame_id, bp_until);
10582 breakpoints.emplace_back (std::move (location_breakpoint));
10583 }
10584
10585 tp->set_thread_fsm
10586 (std::unique_ptr<thread_fsm>
10587 (new until_break_fsm (command_interp (), tp->global_num,
10588 std::move (breakpoints))));
10589
10590 if (lj_deleter)
10591 lj_deleter->release ();
10592
10593 proceed (-1, GDB_SIGNAL_DEFAULT);
10594 }
10595
10596 void
10597 init_ada_exception_breakpoint (struct breakpoint *b,
10598 struct gdbarch *gdbarch,
10599 struct symtab_and_line sal,
10600 const char *addr_string,
10601 int tempflag,
10602 int enabled,
10603 int from_tty)
10604 {
10605 if (from_tty)
10606 {
10607 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
10608 if (!loc_gdbarch)
10609 loc_gdbarch = gdbarch;
10610
10611 describe_other_breakpoints (loc_gdbarch,
10612 sal.pspace, sal.pc, sal.section, -1);
10613 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
10614 version for exception catchpoints, because two catchpoints
10615 used for different exception names will use the same address.
10616 In this case, a "breakpoint ... also set at..." warning is
10617 unproductive. Besides, the warning phrasing is also a bit
10618 inappropriate, we should use the word catchpoint, and tell
10619 the user what type of catchpoint it is. The above is good
10620 enough for now, though. */
10621 }
10622
10623 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint);
10624
10625 b->enable_state = enabled ? bp_enabled : bp_disabled;
10626 b->disposition = tempflag ? disp_del : disp_donttouch;
10627 b->location = string_to_event_location (&addr_string,
10628 language_def (language_ada));
10629 b->language = language_ada;
10630 }
10631
10632 \f
10633
10634 /* Compare two breakpoints and return a strcmp-like result. */
10635
10636 static int
10637 compare_breakpoints (const breakpoint *a, const breakpoint *b)
10638 {
10639 uintptr_t ua = (uintptr_t) a;
10640 uintptr_t ub = (uintptr_t) b;
10641
10642 if (a->number < b->number)
10643 return -1;
10644 else if (a->number > b->number)
10645 return 1;
10646
10647 /* Now sort by address, in case we see, e..g, two breakpoints with
10648 the number 0. */
10649 if (ua < ub)
10650 return -1;
10651 return ua > ub ? 1 : 0;
10652 }
10653
10654 /* Delete breakpoints by address or line. */
10655
10656 static void
10657 clear_command (const char *arg, int from_tty)
10658 {
10659 int default_match;
10660
10661 std::vector<symtab_and_line> decoded_sals;
10662 symtab_and_line last_sal;
10663 gdb::array_view<symtab_and_line> sals;
10664 if (arg)
10665 {
10666 decoded_sals
10667 = decode_line_with_current_source (arg,
10668 (DECODE_LINE_FUNFIRSTLINE
10669 | DECODE_LINE_LIST_MODE));
10670 default_match = 0;
10671 sals = decoded_sals;
10672 }
10673 else
10674 {
10675 /* Set sal's line, symtab, pc, and pspace to the values
10676 corresponding to the last call to print_frame_info. If the
10677 codepoint is not valid, this will set all the fields to 0. */
10678 last_sal = get_last_displayed_sal ();
10679 if (last_sal.symtab == 0)
10680 error (_("No source file specified."));
10681
10682 default_match = 1;
10683 sals = last_sal;
10684 }
10685
10686 /* We don't call resolve_sal_pc here. That's not as bad as it
10687 seems, because all existing breakpoints typically have both
10688 file/line and pc set. So, if clear is given file/line, we can
10689 match this to existing breakpoint without obtaining pc at all.
10690
10691 We only support clearing given the address explicitly
10692 present in breakpoint table. Say, we've set breakpoint
10693 at file:line. There were several PC values for that file:line,
10694 due to optimization, all in one block.
10695
10696 We've picked one PC value. If "clear" is issued with another
10697 PC corresponding to the same file:line, the breakpoint won't
10698 be cleared. We probably can still clear the breakpoint, but
10699 since the other PC value is never presented to user, user
10700 can only find it by guessing, and it does not seem important
10701 to support that. */
10702
10703 /* For each line spec given, delete bps which correspond to it. Do
10704 it in two passes, solely to preserve the current behavior that
10705 from_tty is forced true if we delete more than one
10706 breakpoint. */
10707
10708 std::vector<struct breakpoint *> found;
10709 for (const auto &sal : sals)
10710 {
10711 const char *sal_fullname;
10712
10713 /* If exact pc given, clear bpts at that pc.
10714 If line given (pc == 0), clear all bpts on specified line.
10715 If defaulting, clear all bpts on default line
10716 or at default pc.
10717
10718 defaulting sal.pc != 0 tests to do
10719
10720 0 1 pc
10721 1 1 pc _and_ line
10722 0 0 line
10723 1 0 <can't happen> */
10724
10725 sal_fullname = (sal.symtab == NULL
10726 ? NULL : symtab_to_fullname (sal.symtab));
10727
10728 /* Find all matching breakpoints and add them to 'found'. */
10729 for (breakpoint *b : all_breakpoints ())
10730 {
10731 int match = 0;
10732 /* Are we going to delete b? */
10733 if (b->type != bp_none && !is_watchpoint (b)
10734 && user_breakpoint_p (b))
10735 {
10736 for (bp_location *loc : b->locations ())
10737 {
10738 /* If the user specified file:line, don't allow a PC
10739 match. This matches historical gdb behavior. */
10740 int pc_match = (!sal.explicit_line
10741 && sal.pc
10742 && (loc->pspace == sal.pspace)
10743 && (loc->address == sal.pc)
10744 && (!section_is_overlay (loc->section)
10745 || loc->section == sal.section));
10746 int line_match = 0;
10747
10748 if ((default_match || sal.explicit_line)
10749 && loc->symtab != NULL
10750 && sal_fullname != NULL
10751 && sal.pspace == loc->pspace
10752 && loc->line_number == sal.line
10753 && filename_cmp (symtab_to_fullname (loc->symtab),
10754 sal_fullname) == 0)
10755 line_match = 1;
10756
10757 if (pc_match || line_match)
10758 {
10759 match = 1;
10760 break;
10761 }
10762 }
10763 }
10764
10765 if (match)
10766 found.push_back (b);
10767 }
10768 }
10769
10770 /* Now go thru the 'found' chain and delete them. */
10771 if (found.empty ())
10772 {
10773 if (arg)
10774 error (_("No breakpoint at %s."), arg);
10775 else
10776 error (_("No breakpoint at this line."));
10777 }
10778
10779 /* Remove duplicates from the vec. */
10780 std::sort (found.begin (), found.end (),
10781 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10782 {
10783 return compare_breakpoints (bp_a, bp_b) < 0;
10784 });
10785 found.erase (std::unique (found.begin (), found.end (),
10786 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10787 {
10788 return compare_breakpoints (bp_a, bp_b) == 0;
10789 }),
10790 found.end ());
10791
10792 if (found.size () > 1)
10793 from_tty = 1; /* Always report if deleted more than one. */
10794 if (from_tty)
10795 {
10796 if (found.size () == 1)
10797 gdb_printf (_("Deleted breakpoint "));
10798 else
10799 gdb_printf (_("Deleted breakpoints "));
10800 }
10801
10802 for (breakpoint *iter : found)
10803 {
10804 if (from_tty)
10805 gdb_printf ("%d ", iter->number);
10806 delete_breakpoint (iter);
10807 }
10808 if (from_tty)
10809 gdb_putc ('\n');
10810 }
10811 \f
10812 /* Delete breakpoint in BS if they are `delete' breakpoints and
10813 all breakpoints that are marked for deletion, whether hit or not.
10814 This is called after any breakpoint is hit, or after errors. */
10815
10816 void
10817 breakpoint_auto_delete (bpstat *bs)
10818 {
10819 for (; bs; bs = bs->next)
10820 if (bs->breakpoint_at
10821 && bs->breakpoint_at->disposition == disp_del
10822 && bs->stop)
10823 delete_breakpoint (bs->breakpoint_at);
10824
10825 for (breakpoint *b : all_breakpoints_safe ())
10826 if (b->disposition == disp_del_at_next_stop)
10827 delete_breakpoint (b);
10828 }
10829
10830 /* A comparison function for bp_location AP and BP being interfaced to
10831 std::sort. Sort elements primarily by their ADDRESS (no matter what
10832 bl_address_is_meaningful says), secondarily by ordering first
10833 permanent elements and terciarily just ensuring the array is sorted
10834 stable way despite std::sort being an unstable algorithm. */
10835
10836 static int
10837 bp_location_is_less_than (const bp_location *a, const bp_location *b)
10838 {
10839 if (a->address != b->address)
10840 return a->address < b->address;
10841
10842 /* Sort locations at the same address by their pspace number, keeping
10843 locations of the same inferior (in a multi-inferior environment)
10844 grouped. */
10845
10846 if (a->pspace->num != b->pspace->num)
10847 return a->pspace->num < b->pspace->num;
10848
10849 /* Sort permanent breakpoints first. */
10850 if (a->permanent != b->permanent)
10851 return a->permanent > b->permanent;
10852
10853 /* Sort by type in order to make duplicate determination easier.
10854 See update_global_location_list. This is kept in sync with
10855 breakpoint_locations_match. */
10856 if (a->loc_type < b->loc_type)
10857 return true;
10858
10859 /* Likewise, for range-breakpoints, sort by length. */
10860 if (a->loc_type == bp_loc_hardware_breakpoint
10861 && b->loc_type == bp_loc_hardware_breakpoint
10862 && a->length < b->length)
10863 return true;
10864
10865 /* Make the internal GDB representation stable across GDB runs
10866 where A and B memory inside GDB can differ. Breakpoint locations of
10867 the same type at the same address can be sorted in arbitrary order. */
10868
10869 if (a->owner->number != b->owner->number)
10870 return a->owner->number < b->owner->number;
10871
10872 return a < b;
10873 }
10874
10875 /* Set bp_locations_placed_address_before_address_max and
10876 bp_locations_shadow_len_after_address_max according to the current
10877 content of the bp_locations array. */
10878
10879 static void
10880 bp_locations_target_extensions_update (void)
10881 {
10882 bp_locations_placed_address_before_address_max = 0;
10883 bp_locations_shadow_len_after_address_max = 0;
10884
10885 for (bp_location *bl : all_bp_locations ())
10886 {
10887 CORE_ADDR start, end, addr;
10888
10889 if (!bp_location_has_shadow (bl))
10890 continue;
10891
10892 start = bl->target_info.placed_address;
10893 end = start + bl->target_info.shadow_len;
10894
10895 gdb_assert (bl->address >= start);
10896 addr = bl->address - start;
10897 if (addr > bp_locations_placed_address_before_address_max)
10898 bp_locations_placed_address_before_address_max = addr;
10899
10900 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10901
10902 gdb_assert (bl->address < end);
10903 addr = end - bl->address;
10904 if (addr > bp_locations_shadow_len_after_address_max)
10905 bp_locations_shadow_len_after_address_max = addr;
10906 }
10907 }
10908
10909 /* Download tracepoint locations if they haven't been. */
10910
10911 static void
10912 download_tracepoint_locations (void)
10913 {
10914 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
10915
10916 scoped_restore_current_pspace_and_thread restore_pspace_thread;
10917
10918 for (breakpoint *b : all_tracepoints ())
10919 {
10920 struct tracepoint *t;
10921 int bp_location_downloaded = 0;
10922
10923 if ((b->type == bp_fast_tracepoint
10924 ? !may_insert_fast_tracepoints
10925 : !may_insert_tracepoints))
10926 continue;
10927
10928 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
10929 {
10930 if (target_can_download_tracepoint ())
10931 can_download_tracepoint = TRIBOOL_TRUE;
10932 else
10933 can_download_tracepoint = TRIBOOL_FALSE;
10934 }
10935
10936 if (can_download_tracepoint == TRIBOOL_FALSE)
10937 break;
10938
10939 for (bp_location *bl : b->locations ())
10940 {
10941 /* In tracepoint, locations are _never_ duplicated, so
10942 should_be_inserted is equivalent to
10943 unduplicated_should_be_inserted. */
10944 if (!should_be_inserted (bl) || bl->inserted)
10945 continue;
10946
10947 switch_to_program_space_and_thread (bl->pspace);
10948
10949 target_download_tracepoint (bl);
10950
10951 bl->inserted = 1;
10952 bp_location_downloaded = 1;
10953 }
10954 t = (struct tracepoint *) b;
10955 t->number_on_target = b->number;
10956 if (bp_location_downloaded)
10957 gdb::observers::breakpoint_modified.notify (b);
10958 }
10959 }
10960
10961 /* Swap the insertion/duplication state between two locations. */
10962
10963 static void
10964 swap_insertion (struct bp_location *left, struct bp_location *right)
10965 {
10966 const int left_inserted = left->inserted;
10967 const int left_duplicate = left->duplicate;
10968 const int left_needs_update = left->needs_update;
10969 const struct bp_target_info left_target_info = left->target_info;
10970
10971 /* Locations of tracepoints can never be duplicated. */
10972 if (is_tracepoint (left->owner))
10973 gdb_assert (!left->duplicate);
10974 if (is_tracepoint (right->owner))
10975 gdb_assert (!right->duplicate);
10976
10977 left->inserted = right->inserted;
10978 left->duplicate = right->duplicate;
10979 left->needs_update = right->needs_update;
10980 left->target_info = right->target_info;
10981 right->inserted = left_inserted;
10982 right->duplicate = left_duplicate;
10983 right->needs_update = left_needs_update;
10984 right->target_info = left_target_info;
10985 }
10986
10987 /* Force the re-insertion of the locations at ADDRESS. This is called
10988 once a new/deleted/modified duplicate location is found and we are evaluating
10989 conditions on the target's side. Such conditions need to be updated on
10990 the target. */
10991
10992 static void
10993 force_breakpoint_reinsertion (struct bp_location *bl)
10994 {
10995 CORE_ADDR address = 0;
10996 int pspace_num;
10997
10998 address = bl->address;
10999 pspace_num = bl->pspace->num;
11000
11001 /* This is only meaningful if the target is
11002 evaluating conditions and if the user has
11003 opted for condition evaluation on the target's
11004 side. */
11005 if (gdb_evaluates_breakpoint_condition_p ()
11006 || !target_supports_evaluation_of_breakpoint_conditions ())
11007 return;
11008
11009 /* Flag all breakpoint locations with this address and
11010 the same program space as the location
11011 as "its condition has changed". We need to
11012 update the conditions on the target's side. */
11013 for (bp_location *loc : all_bp_locations_at_addr (address))
11014 {
11015 if (!is_breakpoint (loc->owner)
11016 || pspace_num != loc->pspace->num)
11017 continue;
11018
11019 /* Flag the location appropriately. We use a different state to
11020 let everyone know that we already updated the set of locations
11021 with addr bl->address and program space bl->pspace. This is so
11022 we don't have to keep calling these functions just to mark locations
11023 that have already been marked. */
11024 loc->condition_changed = condition_updated;
11025
11026 /* Free the agent expression bytecode as well. We will compute
11027 it later on. */
11028 loc->cond_bytecode.reset ();
11029 }
11030 }
11031
11032 /* Called whether new breakpoints are created, or existing breakpoints
11033 deleted, to update the global location list and recompute which
11034 locations are duplicate of which.
11035
11036 The INSERT_MODE flag determines whether locations may not, may, or
11037 shall be inserted now. See 'enum ugll_insert_mode' for more
11038 info. */
11039
11040 static void
11041 update_global_location_list (enum ugll_insert_mode insert_mode)
11042 {
11043 /* Last breakpoint location address that was marked for update. */
11044 CORE_ADDR last_addr = 0;
11045 /* Last breakpoint location program space that was marked for update. */
11046 int last_pspace_num = -1;
11047
11048 /* Used in the duplicates detection below. When iterating over all
11049 bp_locations, points to the first bp_location of a given address.
11050 Breakpoints and watchpoints of different types are never
11051 duplicates of each other. Keep one pointer for each type of
11052 breakpoint/watchpoint, so we only need to loop over all locations
11053 once. */
11054 struct bp_location *bp_loc_first; /* breakpoint */
11055 struct bp_location *wp_loc_first; /* hardware watchpoint */
11056 struct bp_location *awp_loc_first; /* access watchpoint */
11057 struct bp_location *rwp_loc_first; /* read watchpoint */
11058
11059 /* Saved former bp_locations array which we compare against the newly
11060 built bp_locations from the current state of ALL_BREAKPOINTS. */
11061 std::vector<bp_location *> old_locations = std::move (bp_locations);
11062 bp_locations.clear ();
11063
11064 for (breakpoint *b : all_breakpoints ())
11065 for (bp_location *loc : b->locations ())
11066 bp_locations.push_back (loc);
11067
11068 /* See if we need to "upgrade" a software breakpoint to a hardware
11069 breakpoint. Do this before deciding whether locations are
11070 duplicates. Also do this before sorting because sorting order
11071 depends on location type. */
11072 for (bp_location *loc : bp_locations)
11073 if (!loc->inserted && should_be_inserted (loc))
11074 handle_automatic_hardware_breakpoints (loc);
11075
11076 std::sort (bp_locations.begin (), bp_locations.end (),
11077 bp_location_is_less_than);
11078
11079 bp_locations_target_extensions_update ();
11080
11081 /* Identify bp_location instances that are no longer present in the
11082 new list, and therefore should be freed. Note that it's not
11083 necessary that those locations should be removed from inferior --
11084 if there's another location at the same address (previously
11085 marked as duplicate), we don't need to remove/insert the
11086 location.
11087
11088 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11089 and former bp_location array state respectively. */
11090
11091 size_t loc_i = 0;
11092 for (bp_location *old_loc : old_locations)
11093 {
11094 /* Tells if 'old_loc' is found among the new locations. If
11095 not, we have to free it. */
11096 int found_object = 0;
11097 /* Tells if the location should remain inserted in the target. */
11098 int keep_in_target = 0;
11099 int removed = 0;
11100
11101 /* Skip LOCP entries which will definitely never be needed.
11102 Stop either at or being the one matching OLD_LOC. */
11103 while (loc_i < bp_locations.size ()
11104 && bp_locations[loc_i]->address < old_loc->address)
11105 loc_i++;
11106
11107 for (size_t loc2_i = loc_i;
11108 (loc2_i < bp_locations.size ()
11109 && bp_locations[loc2_i]->address == old_loc->address);
11110 loc2_i++)
11111 {
11112 /* Check if this is a new/duplicated location or a duplicated
11113 location that had its condition modified. If so, we want to send
11114 its condition to the target if evaluation of conditions is taking
11115 place there. */
11116 if (bp_locations[loc2_i]->condition_changed == condition_modified
11117 && (last_addr != old_loc->address
11118 || last_pspace_num != old_loc->pspace->num))
11119 {
11120 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11121 last_pspace_num = old_loc->pspace->num;
11122 }
11123
11124 if (bp_locations[loc2_i] == old_loc)
11125 found_object = 1;
11126 }
11127
11128 /* We have already handled this address, update it so that we don't
11129 have to go through updates again. */
11130 last_addr = old_loc->address;
11131
11132 /* Target-side condition evaluation: Handle deleted locations. */
11133 if (!found_object)
11134 force_breakpoint_reinsertion (old_loc);
11135
11136 /* If this location is no longer present, and inserted, look if
11137 there's maybe a new location at the same address. If so,
11138 mark that one inserted, and don't remove this one. This is
11139 needed so that we don't have a time window where a breakpoint
11140 at certain location is not inserted. */
11141
11142 if (old_loc->inserted)
11143 {
11144 /* If the location is inserted now, we might have to remove
11145 it. */
11146
11147 if (found_object && should_be_inserted (old_loc))
11148 {
11149 /* The location is still present in the location list,
11150 and still should be inserted. Don't do anything. */
11151 keep_in_target = 1;
11152 }
11153 else
11154 {
11155 /* This location still exists, but it won't be kept in the
11156 target since it may have been disabled. We proceed to
11157 remove its target-side condition. */
11158
11159 /* The location is either no longer present, or got
11160 disabled. See if there's another location at the
11161 same address, in which case we don't need to remove
11162 this one from the target. */
11163
11164 /* OLD_LOC comes from existing struct breakpoint. */
11165 if (bl_address_is_meaningful (old_loc))
11166 {
11167 for (size_t loc2_i = loc_i;
11168 (loc2_i < bp_locations.size ()
11169 && bp_locations[loc2_i]->address == old_loc->address);
11170 loc2_i++)
11171 {
11172 bp_location *loc2 = bp_locations[loc2_i];
11173
11174 if (loc2 == old_loc)
11175 continue;
11176
11177 if (breakpoint_locations_match (loc2, old_loc))
11178 {
11179 /* Read watchpoint locations are switched to
11180 access watchpoints, if the former are not
11181 supported, but the latter are. */
11182 if (is_hardware_watchpoint (old_loc->owner))
11183 {
11184 gdb_assert (is_hardware_watchpoint (loc2->owner));
11185 loc2->watchpoint_type = old_loc->watchpoint_type;
11186 }
11187
11188 /* loc2 is a duplicated location. We need to check
11189 if it should be inserted in case it will be
11190 unduplicated. */
11191 if (unduplicated_should_be_inserted (loc2))
11192 {
11193 swap_insertion (old_loc, loc2);
11194 keep_in_target = 1;
11195 break;
11196 }
11197 }
11198 }
11199 }
11200 }
11201
11202 if (!keep_in_target)
11203 {
11204 if (remove_breakpoint (old_loc))
11205 {
11206 /* This is just about all we can do. We could keep
11207 this location on the global list, and try to
11208 remove it next time, but there's no particular
11209 reason why we will succeed next time.
11210
11211 Note that at this point, old_loc->owner is still
11212 valid, as delete_breakpoint frees the breakpoint
11213 only after calling us. */
11214 gdb_printf (_("warning: Error removing "
11215 "breakpoint %d\n"),
11216 old_loc->owner->number);
11217 }
11218 removed = 1;
11219 }
11220 }
11221
11222 if (!found_object)
11223 {
11224 if (removed && target_is_non_stop_p ()
11225 && need_moribund_for_location_type (old_loc))
11226 {
11227 /* This location was removed from the target. In
11228 non-stop mode, a race condition is possible where
11229 we've removed a breakpoint, but stop events for that
11230 breakpoint are already queued and will arrive later.
11231 We apply an heuristic to be able to distinguish such
11232 SIGTRAPs from other random SIGTRAPs: we keep this
11233 breakpoint location for a bit, and will retire it
11234 after we see some number of events. The theory here
11235 is that reporting of events should, "on the average",
11236 be fair, so after a while we'll see events from all
11237 threads that have anything of interest, and no longer
11238 need to keep this breakpoint location around. We
11239 don't hold locations forever so to reduce chances of
11240 mistaking a non-breakpoint SIGTRAP for a breakpoint
11241 SIGTRAP.
11242
11243 The heuristic failing can be disastrous on
11244 decr_pc_after_break targets.
11245
11246 On decr_pc_after_break targets, like e.g., x86-linux,
11247 if we fail to recognize a late breakpoint SIGTRAP,
11248 because events_till_retirement has reached 0 too
11249 soon, we'll fail to do the PC adjustment, and report
11250 a random SIGTRAP to the user. When the user resumes
11251 the inferior, it will most likely immediately crash
11252 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11253 corrupted, because of being resumed e.g., in the
11254 middle of a multi-byte instruction, or skipped a
11255 one-byte instruction. This was actually seen happen
11256 on native x86-linux, and should be less rare on
11257 targets that do not support new thread events, like
11258 remote, due to the heuristic depending on
11259 thread_count.
11260
11261 Mistaking a random SIGTRAP for a breakpoint trap
11262 causes similar symptoms (PC adjustment applied when
11263 it shouldn't), but then again, playing with SIGTRAPs
11264 behind the debugger's back is asking for trouble.
11265
11266 Since hardware watchpoint traps are always
11267 distinguishable from other traps, so we don't need to
11268 apply keep hardware watchpoint moribund locations
11269 around. We simply always ignore hardware watchpoint
11270 traps we can no longer explain. */
11271
11272 process_stratum_target *proc_target = nullptr;
11273 for (inferior *inf : all_inferiors ())
11274 if (inf->pspace == old_loc->pspace)
11275 {
11276 proc_target = inf->process_target ();
11277 break;
11278 }
11279 if (proc_target != nullptr)
11280 old_loc->events_till_retirement
11281 = 3 * (thread_count (proc_target) + 1);
11282 else
11283 old_loc->events_till_retirement = 1;
11284 old_loc->owner = NULL;
11285
11286 moribund_locations.push_back (old_loc);
11287 }
11288 else
11289 {
11290 old_loc->owner = NULL;
11291 decref_bp_location (&old_loc);
11292 }
11293 }
11294 }
11295
11296 /* Rescan breakpoints at the same address and section, marking the
11297 first one as "first" and any others as "duplicates". This is so
11298 that the bpt instruction is only inserted once. If we have a
11299 permanent breakpoint at the same place as BPT, make that one the
11300 official one, and the rest as duplicates. Permanent breakpoints
11301 are sorted first for the same address.
11302
11303 Do the same for hardware watchpoints, but also considering the
11304 watchpoint's type (regular/access/read) and length. */
11305
11306 bp_loc_first = NULL;
11307 wp_loc_first = NULL;
11308 awp_loc_first = NULL;
11309 rwp_loc_first = NULL;
11310
11311 for (bp_location *loc : all_bp_locations ())
11312 {
11313 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11314 non-NULL. */
11315 struct bp_location **loc_first_p;
11316 breakpoint *b = loc->owner;
11317
11318 if (!unduplicated_should_be_inserted (loc)
11319 || !bl_address_is_meaningful (loc)
11320 /* Don't detect duplicate for tracepoint locations because they are
11321 never duplicated. See the comments in field `duplicate' of
11322 `struct bp_location'. */
11323 || is_tracepoint (b))
11324 {
11325 /* Clear the condition modification flag. */
11326 loc->condition_changed = condition_unchanged;
11327 continue;
11328 }
11329
11330 if (b->type == bp_hardware_watchpoint)
11331 loc_first_p = &wp_loc_first;
11332 else if (b->type == bp_read_watchpoint)
11333 loc_first_p = &rwp_loc_first;
11334 else if (b->type == bp_access_watchpoint)
11335 loc_first_p = &awp_loc_first;
11336 else
11337 loc_first_p = &bp_loc_first;
11338
11339 if (*loc_first_p == NULL
11340 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11341 || !breakpoint_locations_match (loc, *loc_first_p))
11342 {
11343 *loc_first_p = loc;
11344 loc->duplicate = 0;
11345
11346 if (is_breakpoint (loc->owner) && loc->condition_changed)
11347 {
11348 loc->needs_update = 1;
11349 /* Clear the condition modification flag. */
11350 loc->condition_changed = condition_unchanged;
11351 }
11352 continue;
11353 }
11354
11355
11356 /* This and the above ensure the invariant that the first location
11357 is not duplicated, and is the inserted one.
11358 All following are marked as duplicated, and are not inserted. */
11359 if (loc->inserted)
11360 swap_insertion (loc, *loc_first_p);
11361 loc->duplicate = 1;
11362
11363 /* Clear the condition modification flag. */
11364 loc->condition_changed = condition_unchanged;
11365 }
11366
11367 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11368 {
11369 if (insert_mode != UGLL_DONT_INSERT)
11370 insert_breakpoint_locations ();
11371 else
11372 {
11373 /* Even though the caller told us to not insert new
11374 locations, we may still need to update conditions on the
11375 target's side of breakpoints that were already inserted
11376 if the target is evaluating breakpoint conditions. We
11377 only update conditions for locations that are marked
11378 "needs_update". */
11379 update_inserted_breakpoint_locations ();
11380 }
11381 }
11382
11383 if (insert_mode != UGLL_DONT_INSERT)
11384 download_tracepoint_locations ();
11385 }
11386
11387 void
11388 breakpoint_retire_moribund (void)
11389 {
11390 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11391 {
11392 struct bp_location *loc = moribund_locations[ix];
11393 if (--(loc->events_till_retirement) == 0)
11394 {
11395 decref_bp_location (&loc);
11396 unordered_remove (moribund_locations, ix);
11397 --ix;
11398 }
11399 }
11400 }
11401
11402 static void
11403 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11404 {
11405
11406 try
11407 {
11408 update_global_location_list (insert_mode);
11409 }
11410 catch (const gdb_exception_error &e)
11411 {
11412 }
11413 }
11414
11415 /* Clear BKP from a BPS. */
11416
11417 static void
11418 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
11419 {
11420 bpstat *bs;
11421
11422 for (bs = bps; bs; bs = bs->next)
11423 if (bs->breakpoint_at == bpt)
11424 {
11425 bs->breakpoint_at = NULL;
11426 bs->old_val = NULL;
11427 /* bs->commands will be freed later. */
11428 }
11429 }
11430
11431 /* Callback for iterate_over_threads. */
11432 static int
11433 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11434 {
11435 struct breakpoint *bpt = (struct breakpoint *) data;
11436
11437 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11438 return 0;
11439 }
11440
11441 /* Helper for breakpoint and tracepoint breakpoint->mention
11442 callbacks. */
11443
11444 static void
11445 say_where (struct breakpoint *b)
11446 {
11447 struct value_print_options opts;
11448
11449 get_user_print_options (&opts);
11450
11451 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11452 single string. */
11453 if (b->loc == NULL)
11454 {
11455 /* For pending locations, the output differs slightly based
11456 on b->extra_string. If this is non-NULL, it contains either
11457 a condition or dprintf arguments. */
11458 if (b->extra_string == NULL)
11459 {
11460 gdb_printf (_(" (%s) pending."),
11461 event_location_to_string (b->location.get ()));
11462 }
11463 else if (b->type == bp_dprintf)
11464 {
11465 gdb_printf (_(" (%s,%s) pending."),
11466 event_location_to_string (b->location.get ()),
11467 b->extra_string.get ());
11468 }
11469 else
11470 {
11471 gdb_printf (_(" (%s %s) pending."),
11472 event_location_to_string (b->location.get ()),
11473 b->extra_string.get ());
11474 }
11475 }
11476 else
11477 {
11478 if (opts.addressprint || b->loc->symtab == NULL)
11479 gdb_printf (" at %ps",
11480 styled_string (address_style.style (),
11481 paddress (b->loc->gdbarch,
11482 b->loc->address)));
11483 if (b->loc->symtab != NULL)
11484 {
11485 /* If there is a single location, we can print the location
11486 more nicely. */
11487 if (b->loc->next == NULL)
11488 {
11489 const char *filename
11490 = symtab_to_filename_for_display (b->loc->symtab);
11491 gdb_printf (": file %ps, line %d.",
11492 styled_string (file_name_style.style (),
11493 filename),
11494 b->loc->line_number);
11495 }
11496 else
11497 /* This is not ideal, but each location may have a
11498 different file name, and this at least reflects the
11499 real situation somewhat. */
11500 gdb_printf (": %s.",
11501 event_location_to_string (b->location.get ()));
11502 }
11503
11504 if (b->loc->next)
11505 {
11506 struct bp_location *loc = b->loc;
11507 int n = 0;
11508 for (; loc; loc = loc->next)
11509 ++n;
11510 gdb_printf (" (%d locations)", n);
11511 }
11512 }
11513 }
11514
11515 /* See breakpoint.h. */
11516
11517 bp_location_range breakpoint::locations () const
11518 {
11519 return bp_location_range (this->loc);
11520 }
11521
11522 struct bp_location *
11523 breakpoint::allocate_location ()
11524 {
11525 return new bp_location (this);
11526 }
11527
11528 #define internal_error_pure_virtual_called() \
11529 gdb_assert_not_reached ("pure virtual function called")
11530
11531 int
11532 breakpoint::insert_location (struct bp_location *bl)
11533 {
11534 internal_error_pure_virtual_called ();
11535 }
11536
11537 int
11538 breakpoint::remove_location (struct bp_location *bl,
11539 enum remove_bp_reason reason)
11540 {
11541 internal_error_pure_virtual_called ();
11542 }
11543
11544 int
11545 breakpoint::breakpoint_hit (const struct bp_location *bl,
11546 const address_space *aspace,
11547 CORE_ADDR bp_addr,
11548 const target_waitstatus &ws)
11549 {
11550 internal_error_pure_virtual_called ();
11551 }
11552
11553 int
11554 breakpoint::resources_needed (const struct bp_location *bl)
11555 {
11556 internal_error_pure_virtual_called ();
11557 }
11558
11559 enum print_stop_action
11560 breakpoint::print_it (const bpstat *bs) const
11561 {
11562 internal_error_pure_virtual_called ();
11563 }
11564
11565 void
11566 breakpoint::print_mention ()
11567 {
11568 internal_error_pure_virtual_called ();
11569 }
11570
11571 void
11572 breakpoint::print_recreate (struct ui_file *fp)
11573 {
11574 internal_error_pure_virtual_called ();
11575 }
11576
11577 std::vector<symtab_and_line>
11578 breakpoint::decode_location (struct event_location *location,
11579 struct program_space *search_pspace)
11580 {
11581 internal_error_pure_virtual_called ();
11582 }
11583
11584 /* Default breakpoint_ops methods. */
11585
11586 void
11587 base_breakpoint::re_set ()
11588 {
11589 /* FIXME: is this still reachable? */
11590 if (breakpoint_event_location_empty_p (this))
11591 {
11592 /* Anything without a location can't be re-set. */
11593 delete_breakpoint (this);
11594 return;
11595 }
11596
11597 breakpoint_re_set_default (this);
11598 }
11599
11600 int
11601 base_breakpoint::insert_location (struct bp_location *bl)
11602 {
11603 CORE_ADDR addr = bl->target_info.reqstd_address;
11604
11605 bl->target_info.kind = breakpoint_kind (bl, &addr);
11606 bl->target_info.placed_address = addr;
11607
11608 int result;
11609 if (bl->loc_type == bp_loc_hardware_breakpoint)
11610 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
11611 else
11612 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11613
11614 if (result == 0 && bl->probe.prob != nullptr)
11615 {
11616 /* The insertion was successful, now let's set the probe's semaphore
11617 if needed. */
11618 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11619 }
11620
11621 return result;
11622 }
11623
11624 int
11625 base_breakpoint::remove_location (struct bp_location *bl,
11626 enum remove_bp_reason reason)
11627 {
11628 if (bl->probe.prob != nullptr)
11629 {
11630 /* Let's clear the semaphore before removing the location. */
11631 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11632 }
11633
11634 if (bl->loc_type == bp_loc_hardware_breakpoint)
11635 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11636 else
11637 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
11638 }
11639
11640 int
11641 base_breakpoint::breakpoint_hit (const struct bp_location *bl,
11642 const address_space *aspace,
11643 CORE_ADDR bp_addr,
11644 const target_waitstatus &ws)
11645 {
11646 if (ws.kind () != TARGET_WAITKIND_STOPPED
11647 || ws.sig () != GDB_SIGNAL_TRAP)
11648 return 0;
11649
11650 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11651 aspace, bp_addr))
11652 return 0;
11653
11654 if (overlay_debugging /* unmapped overlay section */
11655 && section_is_overlay (bl->section)
11656 && !section_is_mapped (bl->section))
11657 return 0;
11658
11659 return 1;
11660 }
11661
11662 int
11663 dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
11664 const address_space *aspace,
11665 CORE_ADDR bp_addr,
11666 const target_waitstatus &ws)
11667 {
11668 if (dprintf_style == dprintf_style_agent
11669 && target_can_run_breakpoint_commands ())
11670 {
11671 /* An agent-style dprintf never causes a stop. If we see a trap
11672 for this address it must be for a breakpoint that happens to
11673 be set at the same address. */
11674 return 0;
11675 }
11676
11677 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
11678 }
11679
11680 int
11681 ordinary_breakpoint::resources_needed (const struct bp_location *bl)
11682 {
11683 gdb_assert (type == bp_hardware_breakpoint);
11684
11685 return 1;
11686 }
11687
11688 enum print_stop_action
11689 ordinary_breakpoint::print_it (const bpstat *bs) const
11690 {
11691 const struct bp_location *bl;
11692 int bp_temp;
11693 struct ui_out *uiout = current_uiout;
11694
11695 bl = bs->bp_location_at.get ();
11696
11697 bp_temp = disposition == disp_del;
11698 if (bl->address != bl->requested_address)
11699 breakpoint_adjustment_warning (bl->requested_address,
11700 bl->address,
11701 number, 1);
11702 annotate_breakpoint (number);
11703 maybe_print_thread_hit_breakpoint (uiout);
11704
11705 if (uiout->is_mi_like_p ())
11706 {
11707 uiout->field_string ("reason",
11708 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11709 uiout->field_string ("disp", bpdisp_text (disposition));
11710 }
11711 if (bp_temp)
11712 uiout->message ("Temporary breakpoint %pF, ",
11713 signed_field ("bkptno", number));
11714 else
11715 uiout->message ("Breakpoint %pF, ",
11716 signed_field ("bkptno", number));
11717
11718 return PRINT_SRC_AND_LOC;
11719 }
11720
11721 void
11722 ordinary_breakpoint::print_mention ()
11723 {
11724 if (current_uiout->is_mi_like_p ())
11725 return;
11726
11727 switch (type)
11728 {
11729 case bp_breakpoint:
11730 case bp_gnu_ifunc_resolver:
11731 if (disposition == disp_del)
11732 gdb_printf (_("Temporary breakpoint"));
11733 else
11734 gdb_printf (_("Breakpoint"));
11735 gdb_printf (_(" %d"), number);
11736 if (type == bp_gnu_ifunc_resolver)
11737 gdb_printf (_(" at gnu-indirect-function resolver"));
11738 break;
11739 case bp_hardware_breakpoint:
11740 gdb_printf (_("Hardware assisted breakpoint %d"), number);
11741 break;
11742 case bp_dprintf:
11743 gdb_printf (_("Dprintf %d"), number);
11744 break;
11745 }
11746
11747 say_where (this);
11748 }
11749
11750 void
11751 ordinary_breakpoint::print_recreate (struct ui_file *fp)
11752 {
11753 if (type == bp_breakpoint && disposition == disp_del)
11754 gdb_printf (fp, "tbreak");
11755 else if (type == bp_breakpoint)
11756 gdb_printf (fp, "break");
11757 else if (type == bp_hardware_breakpoint
11758 && disposition == disp_del)
11759 gdb_printf (fp, "thbreak");
11760 else if (type == bp_hardware_breakpoint)
11761 gdb_printf (fp, "hbreak");
11762 else
11763 internal_error (__FILE__, __LINE__,
11764 _("unhandled breakpoint type %d"), (int) type);
11765
11766 gdb_printf (fp, " %s", event_location_to_string (location.get ()));
11767
11768 /* Print out extra_string if this breakpoint is pending. It might
11769 contain, for example, conditions that were set by the user. */
11770 if (loc == NULL && extra_string != NULL)
11771 gdb_printf (fp, " %s", extra_string.get ());
11772
11773 print_recreate_thread (fp);
11774 }
11775
11776 std::vector<symtab_and_line>
11777 base_breakpoint::decode_location (struct event_location *location,
11778 struct program_space *search_pspace)
11779 {
11780 if (event_location_type (location) == PROBE_LOCATION)
11781 return bkpt_probe_decode_location (this, location, search_pspace);
11782
11783 return decode_location_default (this, location, search_pspace);
11784 }
11785
11786 /* Virtual table for internal breakpoints. */
11787
11788 void
11789 internal_breakpoint::re_set ()
11790 {
11791 switch (type)
11792 {
11793 /* Delete overlay event and longjmp master breakpoints; they
11794 will be reset later by breakpoint_re_set. */
11795 case bp_overlay_event:
11796 case bp_longjmp_master:
11797 case bp_std_terminate_master:
11798 case bp_exception_master:
11799 delete_breakpoint (this);
11800 break;
11801
11802 /* This breakpoint is special, it's set up when the inferior
11803 starts and we really don't want to touch it. */
11804 case bp_shlib_event:
11805
11806 /* Like bp_shlib_event, this breakpoint type is special. Once
11807 it is set up, we do not want to touch it. */
11808 case bp_thread_event:
11809 break;
11810 }
11811 }
11812
11813 void
11814 internal_breakpoint::check_status (bpstat *bs)
11815 {
11816 if (type == bp_shlib_event)
11817 {
11818 /* If requested, stop when the dynamic linker notifies GDB of
11819 events. This allows the user to get control and place
11820 breakpoints in initializer routines for dynamically loaded
11821 objects (among other things). */
11822 bs->stop = stop_on_solib_events;
11823 bs->print = stop_on_solib_events;
11824 }
11825 else
11826 bs->stop = 0;
11827 }
11828
11829 enum print_stop_action
11830 internal_breakpoint::print_it (const bpstat *bs) const
11831 {
11832 switch (type)
11833 {
11834 case bp_shlib_event:
11835 /* Did we stop because the user set the stop_on_solib_events
11836 variable? (If so, we report this as a generic, "Stopped due
11837 to shlib event" message.) */
11838 print_solib_event (false);
11839 break;
11840
11841 case bp_thread_event:
11842 /* Not sure how we will get here.
11843 GDB should not stop for these breakpoints. */
11844 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
11845 break;
11846
11847 case bp_overlay_event:
11848 /* By analogy with the thread event, GDB should not stop for these. */
11849 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
11850 break;
11851
11852 case bp_longjmp_master:
11853 /* These should never be enabled. */
11854 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
11855 break;
11856
11857 case bp_std_terminate_master:
11858 /* These should never be enabled. */
11859 gdb_printf (_("std::terminate Master Breakpoint: "
11860 "gdb should not stop!\n"));
11861 break;
11862
11863 case bp_exception_master:
11864 /* These should never be enabled. */
11865 gdb_printf (_("Exception Master Breakpoint: "
11866 "gdb should not stop!\n"));
11867 break;
11868 }
11869
11870 return PRINT_NOTHING;
11871 }
11872
11873 void
11874 internal_breakpoint::print_mention ()
11875 {
11876 /* Nothing to mention. These breakpoints are internal. */
11877 }
11878
11879 /* Virtual table for momentary breakpoints */
11880
11881 void
11882 momentary_breakpoint::re_set ()
11883 {
11884 /* Keep temporary breakpoints, which can be encountered when we step
11885 over a dlopen call and solib_add is resetting the breakpoints.
11886 Otherwise these should have been blown away via the cleanup chain
11887 or by breakpoint_init_inferior when we rerun the executable. */
11888 }
11889
11890 void
11891 momentary_breakpoint::check_status (bpstat *bs)
11892 {
11893 /* Nothing. The point of these breakpoints is causing a stop. */
11894 }
11895
11896 enum print_stop_action
11897 momentary_breakpoint::print_it (const bpstat *bs) const
11898 {
11899 return PRINT_UNKNOWN;
11900 }
11901
11902 void
11903 momentary_breakpoint::print_mention ()
11904 {
11905 /* Nothing to mention. These breakpoints are internal. */
11906 }
11907
11908 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
11909
11910 It gets cleared already on the removal of the first one of such placed
11911 breakpoints. This is OK as they get all removed altogether. */
11912
11913 longjmp_breakpoint::~longjmp_breakpoint ()
11914 {
11915 thread_info *tp = find_thread_global_id (this->thread);
11916
11917 if (tp != NULL)
11918 tp->initiating_frame = null_frame_id;
11919 }
11920
11921 static void
11922 bkpt_probe_create_sals_from_location (struct event_location *location,
11923 struct linespec_result *canonical)
11924
11925 {
11926 struct linespec_sals lsal;
11927
11928 lsal.sals = parse_probes (location, NULL, canonical);
11929 lsal.canonical
11930 = xstrdup (event_location_to_string (canonical->location.get ()));
11931 canonical->lsals.push_back (std::move (lsal));
11932 }
11933
11934 static std::vector<symtab_and_line>
11935 bkpt_probe_decode_location (struct breakpoint *b,
11936 struct event_location *location,
11937 struct program_space *search_pspace)
11938 {
11939 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
11940 if (sals.empty ())
11941 error (_("probe not found"));
11942 return sals;
11943 }
11944
11945 /* The breakpoint_ops structure to be used in tracepoints. */
11946
11947 void
11948 tracepoint::re_set ()
11949 {
11950 breakpoint_re_set_default (this);
11951 }
11952
11953 int
11954 tracepoint::breakpoint_hit (const struct bp_location *bl,
11955 const address_space *aspace, CORE_ADDR bp_addr,
11956 const target_waitstatus &ws)
11957 {
11958 /* By definition, the inferior does not report stops at
11959 tracepoints. */
11960 return 0;
11961 }
11962
11963 void
11964 tracepoint::print_one_detail (struct ui_out *uiout) const
11965 {
11966 if (!static_trace_marker_id.empty ())
11967 {
11968 gdb_assert (type == bp_static_tracepoint
11969 || type == bp_static_marker_tracepoint);
11970
11971 uiout->message ("\tmarker id is %pF\n",
11972 string_field ("static-tracepoint-marker-string-id",
11973 static_trace_marker_id.c_str ()));
11974 }
11975 }
11976
11977 void
11978 tracepoint::print_mention ()
11979 {
11980 if (current_uiout->is_mi_like_p ())
11981 return;
11982
11983 switch (type)
11984 {
11985 case bp_tracepoint:
11986 gdb_printf (_("Tracepoint"));
11987 gdb_printf (_(" %d"), number);
11988 break;
11989 case bp_fast_tracepoint:
11990 gdb_printf (_("Fast tracepoint"));
11991 gdb_printf (_(" %d"), number);
11992 break;
11993 case bp_static_tracepoint:
11994 case bp_static_marker_tracepoint:
11995 gdb_printf (_("Static tracepoint"));
11996 gdb_printf (_(" %d"), number);
11997 break;
11998 default:
11999 internal_error (__FILE__, __LINE__,
12000 _("unhandled tracepoint type %d"), (int) type);
12001 }
12002
12003 say_where (this);
12004 }
12005
12006 void
12007 tracepoint::print_recreate (struct ui_file *fp)
12008 {
12009 if (type == bp_fast_tracepoint)
12010 gdb_printf (fp, "ftrace");
12011 else if (type == bp_static_tracepoint
12012 || type == bp_static_marker_tracepoint)
12013 gdb_printf (fp, "strace");
12014 else if (type == bp_tracepoint)
12015 gdb_printf (fp, "trace");
12016 else
12017 internal_error (__FILE__, __LINE__,
12018 _("unhandled tracepoint type %d"), (int) type);
12019
12020 gdb_printf (fp, " %s", event_location_to_string (location.get ()));
12021 print_recreate_thread (fp);
12022
12023 if (pass_count)
12024 gdb_printf (fp, " passcount %d\n", pass_count);
12025 }
12026
12027 std::vector<symtab_and_line>
12028 tracepoint::decode_location (struct event_location *location,
12029 struct program_space *search_pspace)
12030 {
12031 if (event_location_type (location) == PROBE_LOCATION)
12032 return bkpt_probe_decode_location (this, location, search_pspace);
12033
12034 return decode_location_default (this, location, search_pspace);
12035 }
12036
12037 /* Virtual table for tracepoints on static probes. */
12038
12039 static void
12040 tracepoint_probe_create_sals_from_location
12041 (struct event_location *location,
12042 struct linespec_result *canonical)
12043 {
12044 /* We use the same method for breakpoint on probes. */
12045 bkpt_probe_create_sals_from_location (location, canonical);
12046 }
12047
12048 void
12049 dprintf_breakpoint::re_set ()
12050 {
12051 breakpoint_re_set_default (this);
12052
12053 /* extra_string should never be non-NULL for dprintf. */
12054 gdb_assert (extra_string != NULL);
12055
12056 /* 1 - connect to target 1, that can run breakpoint commands.
12057 2 - create a dprintf, which resolves fine.
12058 3 - disconnect from target 1
12059 4 - connect to target 2, that can NOT run breakpoint commands.
12060
12061 After steps #3/#4, you'll want the dprintf command list to
12062 be updated, because target 1 and 2 may well return different
12063 answers for target_can_run_breakpoint_commands().
12064 Given absence of finer grained resetting, we get to do
12065 it all the time. */
12066 if (extra_string != NULL)
12067 update_dprintf_command_list (this);
12068 }
12069
12070 /* Implement the "print_recreate" method for dprintf. */
12071
12072 void
12073 dprintf_breakpoint::print_recreate (struct ui_file *fp)
12074 {
12075 gdb_printf (fp, "dprintf %s,%s",
12076 event_location_to_string (location.get ()),
12077 extra_string.get ());
12078 print_recreate_thread (fp);
12079 }
12080
12081 /* Implement the "after_condition_true" method for dprintf.
12082
12083 dprintf's are implemented with regular commands in their command
12084 list, but we run the commands here instead of before presenting the
12085 stop to the user, as dprintf's don't actually cause a stop. This
12086 also makes it so that the commands of multiple dprintfs at the same
12087 address are all handled. */
12088
12089 void
12090 dprintf_breakpoint::after_condition_true (struct bpstat *bs)
12091 {
12092 /* dprintf's never cause a stop. This wasn't set in the
12093 check_status hook instead because that would make the dprintf's
12094 condition not be evaluated. */
12095 bs->stop = 0;
12096
12097 /* Run the command list here. Take ownership of it instead of
12098 copying. We never want these commands to run later in
12099 bpstat_do_actions, if a breakpoint that causes a stop happens to
12100 be set at same address as this dprintf, or even if running the
12101 commands here throws. */
12102 counted_command_line cmds = std::move (bs->commands);
12103 gdb_assert (cmds != nullptr);
12104 execute_control_commands (cmds.get (), 0);
12105 }
12106
12107 /* The breakpoint_ops structure to be used on static tracepoints with
12108 markers (`-m'). */
12109
12110 static void
12111 strace_marker_create_sals_from_location (struct event_location *location,
12112 struct linespec_result *canonical)
12113 {
12114 struct linespec_sals lsal;
12115 const char *arg_start, *arg;
12116
12117 arg = arg_start = get_linespec_location (location)->spec_string;
12118 lsal.sals = decode_static_tracepoint_spec (&arg);
12119
12120 std::string str (arg_start, arg - arg_start);
12121 const char *ptr = str.c_str ();
12122 canonical->location
12123 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
12124
12125 lsal.canonical
12126 = xstrdup (event_location_to_string (canonical->location.get ()));
12127 canonical->lsals.push_back (std::move (lsal));
12128 }
12129
12130 static void
12131 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12132 struct linespec_result *canonical,
12133 gdb::unique_xmalloc_ptr<char> cond_string,
12134 gdb::unique_xmalloc_ptr<char> extra_string,
12135 enum bptype type_wanted,
12136 enum bpdisp disposition,
12137 int thread,
12138 int task, int ignore_count,
12139 const struct breakpoint_ops *ops,
12140 int from_tty, int enabled,
12141 int internal, unsigned flags)
12142 {
12143 const linespec_sals &lsal = canonical->lsals[0];
12144
12145 /* If the user is creating a static tracepoint by marker id
12146 (strace -m MARKER_ID), then store the sals index, so that
12147 breakpoint_re_set can try to match up which of the newly
12148 found markers corresponds to this one, and, don't try to
12149 expand multiple locations for each sal, given than SALS
12150 already should contain all sals for MARKER_ID. */
12151
12152 for (size_t i = 0; i < lsal.sals.size (); i++)
12153 {
12154 event_location_up location
12155 = copy_event_location (canonical->location.get ());
12156
12157 std::unique_ptr<tracepoint> tp (new tracepoint ());
12158 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
12159 std::move (location), NULL,
12160 std::move (cond_string),
12161 std::move (extra_string),
12162 type_wanted, disposition,
12163 thread, task, ignore_count, ops,
12164 from_tty, enabled, internal, flags,
12165 canonical->special_display);
12166 /* Given that its possible to have multiple markers with
12167 the same string id, if the user is creating a static
12168 tracepoint by marker id ("strace -m MARKER_ID"), then
12169 store the sals index, so that breakpoint_re_set can
12170 try to match up which of the newly found markers
12171 corresponds to this one */
12172 tp->static_trace_marker_id_idx = i;
12173
12174 install_breakpoint (internal, std::move (tp), 0);
12175 }
12176 }
12177
12178 std::vector<symtab_and_line>
12179 static_marker_tracepoint::decode_location (struct event_location *location,
12180 struct program_space *search_pspace)
12181 {
12182 const char *s = get_linespec_location (location)->spec_string;
12183
12184 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12185 if (sals.size () > static_trace_marker_id_idx)
12186 {
12187 sals[0] = sals[static_trace_marker_id_idx];
12188 sals.resize (1);
12189 return sals;
12190 }
12191 else
12192 error (_("marker %s not found"), static_trace_marker_id.c_str ());
12193 }
12194
12195 /* Static tracepoints with marker (`-m'). */
12196 static struct breakpoint_ops strace_marker_breakpoint_ops =
12197 {
12198 strace_marker_create_sals_from_location,
12199 strace_marker_create_breakpoints_sal,
12200 };
12201
12202 static int
12203 strace_marker_p (struct breakpoint *b)
12204 {
12205 return b->type == bp_static_marker_tracepoint;
12206 }
12207
12208 /* Delete a breakpoint and clean up all traces of it in the data
12209 structures. */
12210
12211 void
12212 delete_breakpoint (struct breakpoint *bpt)
12213 {
12214 gdb_assert (bpt != NULL);
12215
12216 /* Has this bp already been deleted? This can happen because
12217 multiple lists can hold pointers to bp's. bpstat lists are
12218 especial culprits.
12219
12220 One example of this happening is a watchpoint's scope bp. When
12221 the scope bp triggers, we notice that the watchpoint is out of
12222 scope, and delete it. We also delete its scope bp. But the
12223 scope bp is marked "auto-deleting", and is already on a bpstat.
12224 That bpstat is then checked for auto-deleting bp's, which are
12225 deleted.
12226
12227 A real solution to this problem might involve reference counts in
12228 bp's, and/or giving them pointers back to their referencing
12229 bpstat's, and teaching delete_breakpoint to only free a bp's
12230 storage when no more references were extent. A cheaper bandaid
12231 was chosen. */
12232 if (bpt->type == bp_none)
12233 return;
12234
12235 /* At least avoid this stale reference until the reference counting
12236 of breakpoints gets resolved. */
12237 if (bpt->related_breakpoint != bpt)
12238 {
12239 struct breakpoint *related;
12240 struct watchpoint *w;
12241
12242 if (bpt->type == bp_watchpoint_scope)
12243 w = (struct watchpoint *) bpt->related_breakpoint;
12244 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12245 w = (struct watchpoint *) bpt;
12246 else
12247 w = NULL;
12248 if (w != NULL)
12249 watchpoint_del_at_next_stop (w);
12250
12251 /* Unlink bpt from the bpt->related_breakpoint ring. */
12252 for (related = bpt; related->related_breakpoint != bpt;
12253 related = related->related_breakpoint);
12254 related->related_breakpoint = bpt->related_breakpoint;
12255 bpt->related_breakpoint = bpt;
12256 }
12257
12258 /* watch_command_1 creates a watchpoint but only sets its number if
12259 update_watchpoint succeeds in creating its bp_locations. If there's
12260 a problem in that process, we'll be asked to delete the half-created
12261 watchpoint. In that case, don't announce the deletion. */
12262 if (bpt->number)
12263 gdb::observers::breakpoint_deleted.notify (bpt);
12264
12265 if (breakpoint_chain == bpt)
12266 breakpoint_chain = bpt->next;
12267
12268 for (breakpoint *b : all_breakpoints ())
12269 if (b->next == bpt)
12270 {
12271 b->next = bpt->next;
12272 break;
12273 }
12274
12275 /* Be sure no bpstat's are pointing at the breakpoint after it's
12276 been freed. */
12277 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12278 in all threads for now. Note that we cannot just remove bpstats
12279 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12280 commands are associated with the bpstat; if we remove it here,
12281 then the later call to bpstat_do_actions (&stop_bpstat); in
12282 event-top.c won't do anything, and temporary breakpoints with
12283 commands won't work. */
12284
12285 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12286
12287 /* Now that breakpoint is removed from breakpoint list, update the
12288 global location list. This will remove locations that used to
12289 belong to this breakpoint. Do this before freeing the breakpoint
12290 itself, since remove_breakpoint looks at location's owner. It
12291 might be better design to have location completely
12292 self-contained, but it's not the case now. */
12293 update_global_location_list (UGLL_DONT_INSERT);
12294
12295 /* On the chance that someone will soon try again to delete this
12296 same bp, we mark it as deleted before freeing its storage. */
12297 bpt->type = bp_none;
12298 delete bpt;
12299 }
12300
12301 /* Iterator function to call a user-provided callback function once
12302 for each of B and its related breakpoints. */
12303
12304 static void
12305 iterate_over_related_breakpoints (struct breakpoint *b,
12306 gdb::function_view<void (breakpoint *)> function)
12307 {
12308 struct breakpoint *related;
12309
12310 related = b;
12311 do
12312 {
12313 struct breakpoint *next;
12314
12315 /* FUNCTION may delete RELATED. */
12316 next = related->related_breakpoint;
12317
12318 if (next == related)
12319 {
12320 /* RELATED is the last ring entry. */
12321 function (related);
12322
12323 /* FUNCTION may have deleted it, so we'd never reach back to
12324 B. There's nothing left to do anyway, so just break
12325 out. */
12326 break;
12327 }
12328 else
12329 function (related);
12330
12331 related = next;
12332 }
12333 while (related != b);
12334 }
12335
12336 static void
12337 delete_command (const char *arg, int from_tty)
12338 {
12339 dont_repeat ();
12340
12341 if (arg == 0)
12342 {
12343 int breaks_to_delete = 0;
12344
12345 /* Delete all breakpoints if no argument. Do not delete
12346 internal breakpoints, these have to be deleted with an
12347 explicit breakpoint number argument. */
12348 for (breakpoint *b : all_breakpoints ())
12349 if (user_breakpoint_p (b))
12350 {
12351 breaks_to_delete = 1;
12352 break;
12353 }
12354
12355 /* Ask user only if there are some breakpoints to delete. */
12356 if (!from_tty
12357 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
12358 for (breakpoint *b : all_breakpoints_safe ())
12359 if (user_breakpoint_p (b))
12360 delete_breakpoint (b);
12361 }
12362 else
12363 map_breakpoint_numbers
12364 (arg, [&] (breakpoint *br)
12365 {
12366 iterate_over_related_breakpoints (br, delete_breakpoint);
12367 });
12368 }
12369
12370 /* Return true if all locations of B bound to PSPACE are pending. If
12371 PSPACE is NULL, all locations of all program spaces are
12372 considered. */
12373
12374 static int
12375 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
12376 {
12377 for (bp_location *loc : b->locations ())
12378 if ((pspace == NULL
12379 || loc->pspace == pspace)
12380 && !loc->shlib_disabled
12381 && !loc->pspace->executing_startup)
12382 return 0;
12383 return 1;
12384 }
12385
12386 /* Subroutine of update_breakpoint_locations to simplify it.
12387 Return non-zero if multiple fns in list LOC have the same name.
12388 Null names are ignored. */
12389
12390 static int
12391 ambiguous_names_p (struct bp_location *loc)
12392 {
12393 struct bp_location *l;
12394 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
12395 xcalloc, xfree));
12396
12397 for (l = loc; l != NULL; l = l->next)
12398 {
12399 const char **slot;
12400 const char *name = l->function_name.get ();
12401
12402 /* Allow for some names to be NULL, ignore them. */
12403 if (name == NULL)
12404 continue;
12405
12406 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
12407 INSERT);
12408 /* NOTE: We can assume slot != NULL here because xcalloc never
12409 returns NULL. */
12410 if (*slot != NULL)
12411 return 1;
12412 *slot = name;
12413 }
12414
12415 return 0;
12416 }
12417
12418 /* When symbols change, it probably means the sources changed as well,
12419 and it might mean the static tracepoint markers are no longer at
12420 the same address or line numbers they used to be at last we
12421 checked. Losing your static tracepoints whenever you rebuild is
12422 undesirable. This function tries to resync/rematch gdb static
12423 tracepoints with the markers on the target, for static tracepoints
12424 that have not been set by marker id. Static tracepoint that have
12425 been set by marker id are reset by marker id in breakpoint_re_set.
12426 The heuristic is:
12427
12428 1) For a tracepoint set at a specific address, look for a marker at
12429 the old PC. If one is found there, assume to be the same marker.
12430 If the name / string id of the marker found is different from the
12431 previous known name, assume that means the user renamed the marker
12432 in the sources, and output a warning.
12433
12434 2) For a tracepoint set at a given line number, look for a marker
12435 at the new address of the old line number. If one is found there,
12436 assume to be the same marker. If the name / string id of the
12437 marker found is different from the previous known name, assume that
12438 means the user renamed the marker in the sources, and output a
12439 warning.
12440
12441 3) If a marker is no longer found at the same address or line, it
12442 may mean the marker no longer exists. But it may also just mean
12443 the code changed a bit. Maybe the user added a few lines of code
12444 that made the marker move up or down (in line number terms). Ask
12445 the target for info about the marker with the string id as we knew
12446 it. If found, update line number and address in the matching
12447 static tracepoint. This will get confused if there's more than one
12448 marker with the same ID (possible in UST, although unadvised
12449 precisely because it confuses tools). */
12450
12451 static struct symtab_and_line
12452 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12453 {
12454 struct tracepoint *tp = (struct tracepoint *) b;
12455 struct static_tracepoint_marker marker;
12456 CORE_ADDR pc;
12457
12458 pc = sal.pc;
12459 if (sal.line)
12460 find_line_pc (sal.symtab, sal.line, &pc);
12461
12462 if (target_static_tracepoint_marker_at (pc, &marker))
12463 {
12464 if (tp->static_trace_marker_id != marker.str_id)
12465 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12466 b->number, tp->static_trace_marker_id.c_str (),
12467 marker.str_id.c_str ());
12468
12469 tp->static_trace_marker_id = std::move (marker.str_id);
12470
12471 return sal;
12472 }
12473
12474 /* Old marker wasn't found on target at lineno. Try looking it up
12475 by string ID. */
12476 if (!sal.explicit_pc
12477 && sal.line != 0
12478 && sal.symtab != NULL
12479 && !tp->static_trace_marker_id.empty ())
12480 {
12481 std::vector<static_tracepoint_marker> markers
12482 = target_static_tracepoint_markers_by_strid
12483 (tp->static_trace_marker_id.c_str ());
12484
12485 if (!markers.empty ())
12486 {
12487 struct symbol *sym;
12488 struct static_tracepoint_marker *tpmarker;
12489 struct ui_out *uiout = current_uiout;
12490 struct explicit_location explicit_loc;
12491
12492 tpmarker = &markers[0];
12493
12494 tp->static_trace_marker_id = std::move (tpmarker->str_id);
12495
12496 warning (_("marker for static tracepoint %d (%s) not "
12497 "found at previous line number"),
12498 b->number, tp->static_trace_marker_id.c_str ());
12499
12500 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
12501 sym = find_pc_sect_function (tpmarker->address, NULL);
12502 uiout->text ("Now in ");
12503 if (sym)
12504 {
12505 uiout->field_string ("func", sym->print_name (),
12506 function_name_style.style ());
12507 uiout->text (" at ");
12508 }
12509 uiout->field_string ("file",
12510 symtab_to_filename_for_display (sal2.symtab),
12511 file_name_style.style ());
12512 uiout->text (":");
12513
12514 if (uiout->is_mi_like_p ())
12515 {
12516 const char *fullname = symtab_to_fullname (sal2.symtab);
12517
12518 uiout->field_string ("fullname", fullname);
12519 }
12520
12521 uiout->field_signed ("line", sal2.line);
12522 uiout->text ("\n");
12523
12524 b->loc->line_number = sal2.line;
12525 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
12526
12527 b->location.reset (NULL);
12528 initialize_explicit_location (&explicit_loc);
12529 explicit_loc.source_filename
12530 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
12531 explicit_loc.line_offset.offset = b->loc->line_number;
12532 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
12533 b->location = new_explicit_location (&explicit_loc);
12534
12535 /* Might be nice to check if function changed, and warn if
12536 so. */
12537 }
12538 }
12539 return sal;
12540 }
12541
12542 /* Returns 1 iff locations A and B are sufficiently same that
12543 we don't need to report breakpoint as changed. */
12544
12545 static int
12546 locations_are_equal (struct bp_location *a, struct bp_location *b)
12547 {
12548 while (a && b)
12549 {
12550 if (a->address != b->address)
12551 return 0;
12552
12553 if (a->shlib_disabled != b->shlib_disabled)
12554 return 0;
12555
12556 if (a->enabled != b->enabled)
12557 return 0;
12558
12559 if (a->disabled_by_cond != b->disabled_by_cond)
12560 return 0;
12561
12562 a = a->next;
12563 b = b->next;
12564 }
12565
12566 if ((a == NULL) != (b == NULL))
12567 return 0;
12568
12569 return 1;
12570 }
12571
12572 /* Split all locations of B that are bound to PSPACE out of B's
12573 location list to a separate list and return that list's head. If
12574 PSPACE is NULL, hoist out all locations of B. */
12575
12576 static struct bp_location *
12577 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12578 {
12579 struct bp_location head;
12580 struct bp_location *i = b->loc;
12581 struct bp_location **i_link = &b->loc;
12582 struct bp_location *hoisted = &head;
12583
12584 if (pspace == NULL)
12585 {
12586 i = b->loc;
12587 b->loc = NULL;
12588 return i;
12589 }
12590
12591 head.next = NULL;
12592
12593 while (i != NULL)
12594 {
12595 if (i->pspace == pspace)
12596 {
12597 *i_link = i->next;
12598 i->next = NULL;
12599 hoisted->next = i;
12600 hoisted = i;
12601 }
12602 else
12603 i_link = &i->next;
12604 i = *i_link;
12605 }
12606
12607 return head.next;
12608 }
12609
12610 /* Create new breakpoint locations for B (a hardware or software
12611 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12612 zero, then B is a ranged breakpoint. Only recreates locations for
12613 FILTER_PSPACE. Locations of other program spaces are left
12614 untouched. */
12615
12616 void
12617 update_breakpoint_locations (struct breakpoint *b,
12618 struct program_space *filter_pspace,
12619 gdb::array_view<const symtab_and_line> sals,
12620 gdb::array_view<const symtab_and_line> sals_end)
12621 {
12622 struct bp_location *existing_locations;
12623
12624 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
12625 {
12626 /* Ranged breakpoints have only one start location and one end
12627 location. */
12628 b->enable_state = bp_disabled;
12629 gdb_printf (gdb_stderr,
12630 _("Could not reset ranged breakpoint %d: "
12631 "multiple locations found\n"),
12632 b->number);
12633 return;
12634 }
12635
12636 /* If there's no new locations, and all existing locations are
12637 pending, don't do anything. This optimizes the common case where
12638 all locations are in the same shared library, that was unloaded.
12639 We'd like to retain the location, so that when the library is
12640 loaded again, we don't loose the enabled/disabled status of the
12641 individual locations. */
12642 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
12643 return;
12644
12645 existing_locations = hoist_existing_locations (b, filter_pspace);
12646
12647 for (const auto &sal : sals)
12648 {
12649 struct bp_location *new_loc;
12650
12651 switch_to_program_space_and_thread (sal.pspace);
12652
12653 new_loc = add_location_to_breakpoint (b, &sal);
12654
12655 /* Reparse conditions, they might contain references to the
12656 old symtab. */
12657 if (b->cond_string != NULL)
12658 {
12659 const char *s;
12660
12661 s = b->cond_string.get ();
12662 try
12663 {
12664 new_loc->cond = parse_exp_1 (&s, sal.pc,
12665 block_for_pc (sal.pc),
12666 0);
12667 }
12668 catch (const gdb_exception_error &e)
12669 {
12670 new_loc->disabled_by_cond = true;
12671 }
12672 }
12673
12674 if (!sals_end.empty ())
12675 {
12676 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
12677
12678 new_loc->length = end - sals[0].pc + 1;
12679 }
12680 }
12681
12682 /* If possible, carry over 'disable' status from existing
12683 breakpoints. */
12684 {
12685 struct bp_location *e = existing_locations;
12686 /* If there are multiple breakpoints with the same function name,
12687 e.g. for inline functions, comparing function names won't work.
12688 Instead compare pc addresses; this is just a heuristic as things
12689 may have moved, but in practice it gives the correct answer
12690 often enough until a better solution is found. */
12691 int have_ambiguous_names = ambiguous_names_p (b->loc);
12692
12693 for (; e; e = e->next)
12694 {
12695 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
12696 {
12697 if (have_ambiguous_names)
12698 {
12699 for (bp_location *l : b->locations ())
12700 {
12701 /* Ignore software vs hardware location type at
12702 this point, because with "set breakpoint
12703 auto-hw", after a re-set, locations that were
12704 hardware can end up as software, or vice versa.
12705 As mentioned above, this is an heuristic and in
12706 practice should give the correct answer often
12707 enough. */
12708 if (breakpoint_locations_match (e, l, true))
12709 {
12710 l->enabled = e->enabled;
12711 l->disabled_by_cond = e->disabled_by_cond;
12712 break;
12713 }
12714 }
12715 }
12716 else
12717 {
12718 for (bp_location *l : b->locations ())
12719 if (l->function_name
12720 && strcmp (e->function_name.get (),
12721 l->function_name.get ()) == 0)
12722 {
12723 l->enabled = e->enabled;
12724 l->disabled_by_cond = e->disabled_by_cond;
12725 break;
12726 }
12727 }
12728 }
12729 }
12730 }
12731
12732 if (!locations_are_equal (existing_locations, b->loc))
12733 gdb::observers::breakpoint_modified.notify (b);
12734 }
12735
12736 /* Find the SaL locations corresponding to the given LOCATION.
12737 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12738
12739 static std::vector<symtab_and_line>
12740 location_to_sals (struct breakpoint *b, struct event_location *location,
12741 struct program_space *search_pspace, int *found)
12742 {
12743 struct gdb_exception exception;
12744
12745 std::vector<symtab_and_line> sals;
12746
12747 try
12748 {
12749 sals = b->decode_location (location, search_pspace);
12750 }
12751 catch (gdb_exception_error &e)
12752 {
12753 int not_found_and_ok = 0;
12754
12755 /* For pending breakpoints, it's expected that parsing will
12756 fail until the right shared library is loaded. User has
12757 already told to create pending breakpoints and don't need
12758 extra messages. If breakpoint is in bp_shlib_disabled
12759 state, then user already saw the message about that
12760 breakpoint being disabled, and don't want to see more
12761 errors. */
12762 if (e.error == NOT_FOUND_ERROR
12763 && (b->condition_not_parsed
12764 || (b->loc != NULL
12765 && search_pspace != NULL
12766 && b->loc->pspace != search_pspace)
12767 || (b->loc && b->loc->shlib_disabled)
12768 || (b->loc && b->loc->pspace->executing_startup)
12769 || b->enable_state == bp_disabled))
12770 not_found_and_ok = 1;
12771
12772 if (!not_found_and_ok)
12773 {
12774 /* We surely don't want to warn about the same breakpoint
12775 10 times. One solution, implemented here, is disable
12776 the breakpoint on error. Another solution would be to
12777 have separate 'warning emitted' flag. Since this
12778 happens only when a binary has changed, I don't know
12779 which approach is better. */
12780 b->enable_state = bp_disabled;
12781 throw;
12782 }
12783
12784 exception = std::move (e);
12785 }
12786
12787 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
12788 {
12789 for (auto &sal : sals)
12790 resolve_sal_pc (&sal);
12791 if (b->condition_not_parsed && b->extra_string != NULL)
12792 {
12793 gdb::unique_xmalloc_ptr<char> cond_string, extra_string;
12794 int thread, task;
12795
12796 find_condition_and_thread_for_sals (sals, b->extra_string.get (),
12797 &cond_string, &thread,
12798 &task, &extra_string);
12799 gdb_assert (b->cond_string == NULL);
12800 if (cond_string)
12801 b->cond_string = std::move (cond_string);
12802 b->thread = thread;
12803 b->task = task;
12804 if (extra_string)
12805 b->extra_string = std::move (extra_string);
12806 b->condition_not_parsed = 0;
12807 }
12808
12809 if (b->type == bp_static_tracepoint)
12810 sals[0] = update_static_tracepoint (b, sals[0]);
12811
12812 *found = 1;
12813 }
12814 else
12815 *found = 0;
12816
12817 return sals;
12818 }
12819
12820 /* The default re_set method, for typical hardware or software
12821 breakpoints. Reevaluate the breakpoint and recreate its
12822 locations. */
12823
12824 static void
12825 breakpoint_re_set_default (struct breakpoint *b)
12826 {
12827 struct program_space *filter_pspace = current_program_space;
12828 std::vector<symtab_and_line> expanded, expanded_end;
12829
12830 int found;
12831 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
12832 filter_pspace, &found);
12833 if (found)
12834 expanded = std::move (sals);
12835
12836 if (b->location_range_end != NULL)
12837 {
12838 std::vector<symtab_and_line> sals_end
12839 = location_to_sals (b, b->location_range_end.get (),
12840 filter_pspace, &found);
12841 if (found)
12842 expanded_end = std::move (sals_end);
12843 }
12844
12845 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
12846 }
12847
12848 /* Default method for creating SALs from an address string. It basically
12849 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12850
12851 static void
12852 create_sals_from_location_default (struct event_location *location,
12853 struct linespec_result *canonical)
12854 {
12855 parse_breakpoint_sals (location, canonical);
12856 }
12857
12858 /* Decode the line represented by S by calling decode_line_full. This is the
12859 default function for the `decode_location' method of breakpoint_ops. */
12860
12861 static std::vector<symtab_and_line>
12862 decode_location_default (struct breakpoint *b,
12863 struct event_location *location,
12864 struct program_space *search_pspace)
12865 {
12866 struct linespec_result canonical;
12867
12868 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
12869 NULL, 0, &canonical, multiple_symbols_all,
12870 b->filter.get ());
12871
12872 /* We should get 0 or 1 resulting SALs. */
12873 gdb_assert (canonical.lsals.size () < 2);
12874
12875 if (!canonical.lsals.empty ())
12876 {
12877 const linespec_sals &lsal = canonical.lsals[0];
12878 return std::move (lsal.sals);
12879 }
12880 return {};
12881 }
12882
12883 /* Reset a breakpoint. */
12884
12885 static void
12886 breakpoint_re_set_one (breakpoint *b)
12887 {
12888 input_radix = b->input_radix;
12889 set_language (b->language);
12890
12891 b->re_set ();
12892 }
12893
12894 /* Re-set breakpoint locations for the current program space.
12895 Locations bound to other program spaces are left untouched. */
12896
12897 void
12898 breakpoint_re_set (void)
12899 {
12900 {
12901 scoped_restore_current_language save_language;
12902 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
12903 scoped_restore_current_pspace_and_thread restore_pspace_thread;
12904
12905 /* breakpoint_re_set_one sets the current_language to the language
12906 of the breakpoint it is resetting (see prepare_re_set_context)
12907 before re-evaluating the breakpoint's location. This change can
12908 unfortunately get undone by accident if the language_mode is set
12909 to auto, and we either switch frames, or more likely in this context,
12910 we select the current frame.
12911
12912 We prevent this by temporarily turning the language_mode to
12913 language_mode_manual. We restore it once all breakpoints
12914 have been reset. */
12915 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
12916 language_mode = language_mode_manual;
12917
12918 /* Note: we must not try to insert locations until after all
12919 breakpoints have been re-set. Otherwise, e.g., when re-setting
12920 breakpoint 1, we'd insert the locations of breakpoint 2, which
12921 hadn't been re-set yet, and thus may have stale locations. */
12922
12923 for (breakpoint *b : all_breakpoints_safe ())
12924 {
12925 try
12926 {
12927 breakpoint_re_set_one (b);
12928 }
12929 catch (const gdb_exception &ex)
12930 {
12931 exception_fprintf (gdb_stderr, ex,
12932 "Error in re-setting breakpoint %d: ",
12933 b->number);
12934 }
12935 }
12936
12937 jit_breakpoint_re_set ();
12938 }
12939
12940 create_overlay_event_breakpoint ();
12941 create_longjmp_master_breakpoint ();
12942 create_std_terminate_master_breakpoint ();
12943 create_exception_master_breakpoint ();
12944
12945 /* Now we can insert. */
12946 update_global_location_list (UGLL_MAY_INSERT);
12947 }
12948 \f
12949 /* Reset the thread number of this breakpoint:
12950
12951 - If the breakpoint is for all threads, leave it as-is.
12952 - Else, reset it to the current thread for inferior_ptid. */
12953 void
12954 breakpoint_re_set_thread (struct breakpoint *b)
12955 {
12956 if (b->thread != -1)
12957 {
12958 b->thread = inferior_thread ()->global_num;
12959
12960 /* We're being called after following a fork. The new fork is
12961 selected as current, and unless this was a vfork will have a
12962 different program space from the original thread. Reset that
12963 as well. */
12964 b->loc->pspace = current_program_space;
12965 }
12966 }
12967
12968 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
12969 If from_tty is nonzero, it prints a message to that effect,
12970 which ends with a period (no newline). */
12971
12972 void
12973 set_ignore_count (int bptnum, int count, int from_tty)
12974 {
12975 if (count < 0)
12976 count = 0;
12977
12978 for (breakpoint *b : all_breakpoints ())
12979 if (b->number == bptnum)
12980 {
12981 if (is_tracepoint (b))
12982 {
12983 if (from_tty && count != 0)
12984 gdb_printf (_("Ignore count ignored for tracepoint %d."),
12985 bptnum);
12986 return;
12987 }
12988
12989 b->ignore_count = count;
12990 if (from_tty)
12991 {
12992 if (count == 0)
12993 gdb_printf (_("Will stop next time "
12994 "breakpoint %d is reached."),
12995 bptnum);
12996 else if (count == 1)
12997 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
12998 bptnum);
12999 else
13000 gdb_printf (_("Will ignore next %d "
13001 "crossings of breakpoint %d."),
13002 count, bptnum);
13003 }
13004 gdb::observers::breakpoint_modified.notify (b);
13005 return;
13006 }
13007
13008 error (_("No breakpoint number %d."), bptnum);
13009 }
13010
13011 /* Command to set ignore-count of breakpoint N to COUNT. */
13012
13013 static void
13014 ignore_command (const char *args, int from_tty)
13015 {
13016 const char *p = args;
13017 int num;
13018
13019 if (p == 0)
13020 error_no_arg (_("a breakpoint number"));
13021
13022 num = get_number (&p);
13023 if (num == 0)
13024 error (_("bad breakpoint number: '%s'"), args);
13025 if (*p == 0)
13026 error (_("Second argument (specified ignore-count) is missing."));
13027
13028 set_ignore_count (num,
13029 longest_to_int (value_as_long (parse_and_eval (p))),
13030 from_tty);
13031 if (from_tty)
13032 gdb_printf ("\n");
13033 }
13034 \f
13035
13036 /* Call FUNCTION on each of the breakpoints with numbers in the range
13037 defined by BP_NUM_RANGE (an inclusive range). */
13038
13039 static void
13040 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13041 gdb::function_view<void (breakpoint *)> function)
13042 {
13043 if (bp_num_range.first == 0)
13044 {
13045 warning (_("bad breakpoint number at or near '%d'"),
13046 bp_num_range.first);
13047 }
13048 else
13049 {
13050 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13051 {
13052 bool match = false;
13053
13054 for (breakpoint *b : all_breakpoints_safe ())
13055 if (b->number == i)
13056 {
13057 match = true;
13058 function (b);
13059 break;
13060 }
13061 if (!match)
13062 gdb_printf (_("No breakpoint number %d.\n"), i);
13063 }
13064 }
13065 }
13066
13067 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13068 ARGS. */
13069
13070 static void
13071 map_breakpoint_numbers (const char *args,
13072 gdb::function_view<void (breakpoint *)> function)
13073 {
13074 if (args == NULL || *args == '\0')
13075 error_no_arg (_("one or more breakpoint numbers"));
13076
13077 number_or_range_parser parser (args);
13078
13079 while (!parser.finished ())
13080 {
13081 int num = parser.get_number ();
13082 map_breakpoint_number_range (std::make_pair (num, num), function);
13083 }
13084 }
13085
13086 /* Return the breakpoint location structure corresponding to the
13087 BP_NUM and LOC_NUM values. */
13088
13089 static struct bp_location *
13090 find_location_by_number (int bp_num, int loc_num)
13091 {
13092 breakpoint *b = get_breakpoint (bp_num);
13093
13094 if (!b || b->number != bp_num)
13095 error (_("Bad breakpoint number '%d'"), bp_num);
13096
13097 if (loc_num == 0)
13098 error (_("Bad breakpoint location number '%d'"), loc_num);
13099
13100 int n = 0;
13101 for (bp_location *loc : b->locations ())
13102 if (++n == loc_num)
13103 return loc;
13104
13105 error (_("Bad breakpoint location number '%d'"), loc_num);
13106 }
13107
13108 /* Modes of operation for extract_bp_num. */
13109 enum class extract_bp_kind
13110 {
13111 /* Extracting a breakpoint number. */
13112 bp,
13113
13114 /* Extracting a location number. */
13115 loc,
13116 };
13117
13118 /* Extract a breakpoint or location number (as determined by KIND)
13119 from the string starting at START. TRAILER is a character which
13120 can be found after the number. If you don't want a trailer, use
13121 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13122 string. This always returns a positive integer. */
13123
13124 static int
13125 extract_bp_num (extract_bp_kind kind, const char *start,
13126 int trailer, const char **end_out = NULL)
13127 {
13128 const char *end = start;
13129 int num = get_number_trailer (&end, trailer);
13130 if (num < 0)
13131 error (kind == extract_bp_kind::bp
13132 ? _("Negative breakpoint number '%.*s'")
13133 : _("Negative breakpoint location number '%.*s'"),
13134 int (end - start), start);
13135 if (num == 0)
13136 error (kind == extract_bp_kind::bp
13137 ? _("Bad breakpoint number '%.*s'")
13138 : _("Bad breakpoint location number '%.*s'"),
13139 int (end - start), start);
13140
13141 if (end_out != NULL)
13142 *end_out = end;
13143 return num;
13144 }
13145
13146 /* Extract a breakpoint or location range (as determined by KIND) in
13147 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13148 representing the (inclusive) range. The returned pair's elements
13149 are always positive integers. */
13150
13151 static std::pair<int, int>
13152 extract_bp_or_bp_range (extract_bp_kind kind,
13153 const std::string &arg,
13154 std::string::size_type arg_offset)
13155 {
13156 std::pair<int, int> range;
13157 const char *bp_loc = &arg[arg_offset];
13158 std::string::size_type dash = arg.find ('-', arg_offset);
13159 if (dash != std::string::npos)
13160 {
13161 /* bp_loc is a range (x-z). */
13162 if (arg.length () == dash + 1)
13163 error (kind == extract_bp_kind::bp
13164 ? _("Bad breakpoint number at or near: '%s'")
13165 : _("Bad breakpoint location number at or near: '%s'"),
13166 bp_loc);
13167
13168 const char *end;
13169 const char *start_first = bp_loc;
13170 const char *start_second = &arg[dash + 1];
13171 range.first = extract_bp_num (kind, start_first, '-');
13172 range.second = extract_bp_num (kind, start_second, '\0', &end);
13173
13174 if (range.first > range.second)
13175 error (kind == extract_bp_kind::bp
13176 ? _("Inverted breakpoint range at '%.*s'")
13177 : _("Inverted breakpoint location range at '%.*s'"),
13178 int (end - start_first), start_first);
13179 }
13180 else
13181 {
13182 /* bp_loc is a single value. */
13183 range.first = extract_bp_num (kind, bp_loc, '\0');
13184 range.second = range.first;
13185 }
13186 return range;
13187 }
13188
13189 /* Extract the breakpoint/location range specified by ARG. Returns
13190 the breakpoint range in BP_NUM_RANGE, and the location range in
13191 BP_LOC_RANGE.
13192
13193 ARG may be in any of the following forms:
13194
13195 x where 'x' is a breakpoint number.
13196 x-y where 'x' and 'y' specify a breakpoint numbers range.
13197 x.y where 'x' is a breakpoint number and 'y' a location number.
13198 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13199 location number range.
13200 */
13201
13202 static void
13203 extract_bp_number_and_location (const std::string &arg,
13204 std::pair<int, int> &bp_num_range,
13205 std::pair<int, int> &bp_loc_range)
13206 {
13207 std::string::size_type dot = arg.find ('.');
13208
13209 if (dot != std::string::npos)
13210 {
13211 /* Handle 'x.y' and 'x.y-z' cases. */
13212
13213 if (arg.length () == dot + 1 || dot == 0)
13214 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
13215
13216 bp_num_range.first
13217 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13218 bp_num_range.second = bp_num_range.first;
13219
13220 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13221 arg, dot + 1);
13222 }
13223 else
13224 {
13225 /* Handle x and x-y cases. */
13226
13227 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
13228 bp_loc_range.first = 0;
13229 bp_loc_range.second = 0;
13230 }
13231 }
13232
13233 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13234 specifies whether to enable or disable. */
13235
13236 static void
13237 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13238 {
13239 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13240 if (loc != NULL)
13241 {
13242 if (loc->disabled_by_cond && enable)
13243 error (_("Breakpoint %d's condition is invalid at location %d, "
13244 "cannot enable."), bp_num, loc_num);
13245
13246 if (loc->enabled != enable)
13247 {
13248 loc->enabled = enable;
13249 mark_breakpoint_location_modified (loc);
13250 }
13251 if (target_supports_enable_disable_tracepoint ()
13252 && current_trace_status ()->running && loc->owner
13253 && is_tracepoint (loc->owner))
13254 target_disable_tracepoint (loc);
13255 }
13256 update_global_location_list (UGLL_DONT_INSERT);
13257
13258 gdb::observers::breakpoint_modified.notify (loc->owner);
13259 }
13260
13261 /* Enable or disable a range of breakpoint locations. BP_NUM is the
13262 number of the breakpoint, and BP_LOC_RANGE specifies the
13263 (inclusive) range of location numbers of that breakpoint to
13264 enable/disable. ENABLE specifies whether to enable or disable the
13265 location. */
13266
13267 static void
13268 enable_disable_breakpoint_location_range (int bp_num,
13269 std::pair<int, int> &bp_loc_range,
13270 bool enable)
13271 {
13272 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13273 enable_disable_bp_num_loc (bp_num, i, enable);
13274 }
13275
13276 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13277 If from_tty is nonzero, it prints a message to that effect,
13278 which ends with a period (no newline). */
13279
13280 void
13281 disable_breakpoint (struct breakpoint *bpt)
13282 {
13283 /* Never disable a watchpoint scope breakpoint; we want to
13284 hit them when we leave scope so we can delete both the
13285 watchpoint and its scope breakpoint at that time. */
13286 if (bpt->type == bp_watchpoint_scope)
13287 return;
13288
13289 bpt->enable_state = bp_disabled;
13290
13291 /* Mark breakpoint locations modified. */
13292 mark_breakpoint_modified (bpt);
13293
13294 if (target_supports_enable_disable_tracepoint ()
13295 && current_trace_status ()->running && is_tracepoint (bpt))
13296 {
13297 for (bp_location *location : bpt->locations ())
13298 target_disable_tracepoint (location);
13299 }
13300
13301 update_global_location_list (UGLL_DONT_INSERT);
13302
13303 gdb::observers::breakpoint_modified.notify (bpt);
13304 }
13305
13306 /* Enable or disable the breakpoint(s) or breakpoint location(s)
13307 specified in ARGS. ARGS may be in any of the formats handled by
13308 extract_bp_number_and_location. ENABLE specifies whether to enable
13309 or disable the breakpoints/locations. */
13310
13311 static void
13312 enable_disable_command (const char *args, int from_tty, bool enable)
13313 {
13314 if (args == 0)
13315 {
13316 for (breakpoint *bpt : all_breakpoints ())
13317 if (user_breakpoint_p (bpt))
13318 {
13319 if (enable)
13320 enable_breakpoint (bpt);
13321 else
13322 disable_breakpoint (bpt);
13323 }
13324 }
13325 else
13326 {
13327 std::string num = extract_arg (&args);
13328
13329 while (!num.empty ())
13330 {
13331 std::pair<int, int> bp_num_range, bp_loc_range;
13332
13333 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13334
13335 if (bp_loc_range.first == bp_loc_range.second
13336 && bp_loc_range.first == 0)
13337 {
13338 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
13339 map_breakpoint_number_range (bp_num_range,
13340 enable
13341 ? enable_breakpoint
13342 : disable_breakpoint);
13343 }
13344 else
13345 {
13346 /* Handle breakpoint ids with formats 'x.y' or
13347 'x.y-z'. */
13348 enable_disable_breakpoint_location_range
13349 (bp_num_range.first, bp_loc_range, enable);
13350 }
13351 num = extract_arg (&args);
13352 }
13353 }
13354 }
13355
13356 /* The disable command disables the specified breakpoints/locations
13357 (or all defined breakpoints) so they're no longer effective in
13358 stopping the inferior. ARGS may be in any of the forms defined in
13359 extract_bp_number_and_location. */
13360
13361 static void
13362 disable_command (const char *args, int from_tty)
13363 {
13364 enable_disable_command (args, from_tty, false);
13365 }
13366
13367 static void
13368 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13369 int count)
13370 {
13371 int target_resources_ok;
13372
13373 if (bpt->type == bp_hardware_breakpoint)
13374 {
13375 int i;
13376 i = hw_breakpoint_used_count ();
13377 target_resources_ok =
13378 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13379 i + 1, 0);
13380 if (target_resources_ok == 0)
13381 error (_("No hardware breakpoint support in the target."));
13382 else if (target_resources_ok < 0)
13383 error (_("Hardware breakpoints used exceeds limit."));
13384 }
13385
13386 if (is_watchpoint (bpt))
13387 {
13388 /* Initialize it just to avoid a GCC false warning. */
13389 enum enable_state orig_enable_state = bp_disabled;
13390
13391 try
13392 {
13393 struct watchpoint *w = (struct watchpoint *) bpt;
13394
13395 orig_enable_state = bpt->enable_state;
13396 bpt->enable_state = bp_enabled;
13397 update_watchpoint (w, 1 /* reparse */);
13398 }
13399 catch (const gdb_exception &e)
13400 {
13401 bpt->enable_state = orig_enable_state;
13402 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13403 bpt->number);
13404 return;
13405 }
13406 }
13407
13408 bpt->enable_state = bp_enabled;
13409
13410 /* Mark breakpoint locations modified. */
13411 mark_breakpoint_modified (bpt);
13412
13413 if (target_supports_enable_disable_tracepoint ()
13414 && current_trace_status ()->running && is_tracepoint (bpt))
13415 {
13416 for (bp_location *location : bpt->locations ())
13417 target_enable_tracepoint (location);
13418 }
13419
13420 bpt->disposition = disposition;
13421 bpt->enable_count = count;
13422 update_global_location_list (UGLL_MAY_INSERT);
13423
13424 gdb::observers::breakpoint_modified.notify (bpt);
13425 }
13426
13427
13428 void
13429 enable_breakpoint (struct breakpoint *bpt)
13430 {
13431 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13432 }
13433
13434 /* The enable command enables the specified breakpoints/locations (or
13435 all defined breakpoints) so they once again become (or continue to
13436 be) effective in stopping the inferior. ARGS may be in any of the
13437 forms defined in extract_bp_number_and_location. */
13438
13439 static void
13440 enable_command (const char *args, int from_tty)
13441 {
13442 enable_disable_command (args, from_tty, true);
13443 }
13444
13445 static void
13446 enable_once_command (const char *args, int from_tty)
13447 {
13448 map_breakpoint_numbers
13449 (args, [&] (breakpoint *b)
13450 {
13451 iterate_over_related_breakpoints
13452 (b, [&] (breakpoint *bpt)
13453 {
13454 enable_breakpoint_disp (bpt, disp_disable, 1);
13455 });
13456 });
13457 }
13458
13459 static void
13460 enable_count_command (const char *args, int from_tty)
13461 {
13462 int count;
13463
13464 if (args == NULL)
13465 error_no_arg (_("hit count"));
13466
13467 count = get_number (&args);
13468
13469 map_breakpoint_numbers
13470 (args, [&] (breakpoint *b)
13471 {
13472 iterate_over_related_breakpoints
13473 (b, [&] (breakpoint *bpt)
13474 {
13475 enable_breakpoint_disp (bpt, disp_disable, count);
13476 });
13477 });
13478 }
13479
13480 static void
13481 enable_delete_command (const char *args, int from_tty)
13482 {
13483 map_breakpoint_numbers
13484 (args, [&] (breakpoint *b)
13485 {
13486 iterate_over_related_breakpoints
13487 (b, [&] (breakpoint *bpt)
13488 {
13489 enable_breakpoint_disp (bpt, disp_del, 1);
13490 });
13491 });
13492 }
13493 \f
13494 /* Invalidate last known value of any hardware watchpoint if
13495 the memory which that value represents has been written to by
13496 GDB itself. */
13497
13498 static void
13499 invalidate_bp_value_on_memory_change (struct inferior *inferior,
13500 CORE_ADDR addr, ssize_t len,
13501 const bfd_byte *data)
13502 {
13503 for (breakpoint *bp : all_breakpoints ())
13504 if (bp->enable_state == bp_enabled
13505 && bp->type == bp_hardware_watchpoint)
13506 {
13507 struct watchpoint *wp = (struct watchpoint *) bp;
13508
13509 if (wp->val_valid && wp->val != nullptr)
13510 {
13511 for (bp_location *loc : bp->locations ())
13512 if (loc->loc_type == bp_loc_hardware_watchpoint
13513 && loc->address + loc->length > addr
13514 && addr + len > loc->address)
13515 {
13516 wp->val = NULL;
13517 wp->val_valid = false;
13518 }
13519 }
13520 }
13521 }
13522
13523 /* Create and insert a breakpoint for software single step. */
13524
13525 void
13526 insert_single_step_breakpoint (struct gdbarch *gdbarch,
13527 const address_space *aspace,
13528 CORE_ADDR next_pc)
13529 {
13530 struct thread_info *tp = inferior_thread ();
13531 struct symtab_and_line sal;
13532 CORE_ADDR pc = next_pc;
13533
13534 if (tp->control.single_step_breakpoints == NULL)
13535 {
13536 tp->control.single_step_breakpoints
13537 = new_single_step_breakpoint (tp->global_num, gdbarch);
13538 }
13539
13540 sal = find_pc_line (pc, 0);
13541 sal.pc = pc;
13542 sal.section = find_pc_overlay (pc);
13543 sal.explicit_pc = 1;
13544 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
13545
13546 update_global_location_list (UGLL_INSERT);
13547 }
13548
13549 /* Insert single step breakpoints according to the current state. */
13550
13551 int
13552 insert_single_step_breakpoints (struct gdbarch *gdbarch)
13553 {
13554 struct regcache *regcache = get_current_regcache ();
13555 std::vector<CORE_ADDR> next_pcs;
13556
13557 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
13558
13559 if (!next_pcs.empty ())
13560 {
13561 struct frame_info *frame = get_current_frame ();
13562 const address_space *aspace = get_frame_address_space (frame);
13563
13564 for (CORE_ADDR pc : next_pcs)
13565 insert_single_step_breakpoint (gdbarch, aspace, pc);
13566
13567 return 1;
13568 }
13569 else
13570 return 0;
13571 }
13572
13573 /* See breakpoint.h. */
13574
13575 int
13576 breakpoint_has_location_inserted_here (struct breakpoint *bp,
13577 const address_space *aspace,
13578 CORE_ADDR pc)
13579 {
13580 for (bp_location *loc : bp->locations ())
13581 if (loc->inserted
13582 && breakpoint_location_address_match (loc, aspace, pc))
13583 return 1;
13584
13585 return 0;
13586 }
13587
13588 /* Check whether a software single-step breakpoint is inserted at
13589 PC. */
13590
13591 int
13592 single_step_breakpoint_inserted_here_p (const address_space *aspace,
13593 CORE_ADDR pc)
13594 {
13595 for (breakpoint *bpt : all_breakpoints ())
13596 {
13597 if (bpt->type == bp_single_step
13598 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13599 return 1;
13600 }
13601 return 0;
13602 }
13603
13604 /* Tracepoint-specific operations. */
13605
13606 /* Set tracepoint count to NUM. */
13607 static void
13608 set_tracepoint_count (int num)
13609 {
13610 tracepoint_count = num;
13611 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
13612 }
13613
13614 static void
13615 trace_command (const char *arg, int from_tty)
13616 {
13617 event_location_up location = string_to_event_location (&arg,
13618 current_language);
13619 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
13620 (location.get (), true /* is_tracepoint */);
13621
13622 create_breakpoint (get_current_arch (),
13623 location.get (),
13624 NULL, 0, arg, false, 1 /* parse arg */,
13625 0 /* tempflag */,
13626 bp_tracepoint /* type_wanted */,
13627 0 /* Ignore count */,
13628 pending_break_support,
13629 ops,
13630 from_tty,
13631 1 /* enabled */,
13632 0 /* internal */, 0);
13633 }
13634
13635 static void
13636 ftrace_command (const char *arg, int from_tty)
13637 {
13638 event_location_up location = string_to_event_location (&arg,
13639 current_language);
13640 create_breakpoint (get_current_arch (),
13641 location.get (),
13642 NULL, 0, arg, false, 1 /* parse arg */,
13643 0 /* tempflag */,
13644 bp_fast_tracepoint /* type_wanted */,
13645 0 /* Ignore count */,
13646 pending_break_support,
13647 &base_breakpoint_ops,
13648 from_tty,
13649 1 /* enabled */,
13650 0 /* internal */, 0);
13651 }
13652
13653 /* strace command implementation. Creates a static tracepoint. */
13654
13655 static void
13656 strace_command (const char *arg, int from_tty)
13657 {
13658 const struct breakpoint_ops *ops;
13659 event_location_up location;
13660 enum bptype type;
13661
13662 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13663 or with a normal static tracepoint. */
13664 if (arg && startswith (arg, "-m") && isspace (arg[2]))
13665 {
13666 ops = &strace_marker_breakpoint_ops;
13667 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
13668 type = bp_static_marker_tracepoint;
13669 }
13670 else
13671 {
13672 ops = &base_breakpoint_ops;
13673 location = string_to_event_location (&arg, current_language);
13674 type = bp_static_tracepoint;
13675 }
13676
13677 create_breakpoint (get_current_arch (),
13678 location.get (),
13679 NULL, 0, arg, false, 1 /* parse arg */,
13680 0 /* tempflag */,
13681 type /* type_wanted */,
13682 0 /* Ignore count */,
13683 pending_break_support,
13684 ops,
13685 from_tty,
13686 1 /* enabled */,
13687 0 /* internal */, 0);
13688 }
13689
13690 /* Set up a fake reader function that gets command lines from a linked
13691 list that was acquired during tracepoint uploading. */
13692
13693 static struct uploaded_tp *this_utp;
13694 static int next_cmd;
13695
13696 static char *
13697 read_uploaded_action (void)
13698 {
13699 char *rslt = nullptr;
13700
13701 if (next_cmd < this_utp->cmd_strings.size ())
13702 {
13703 rslt = this_utp->cmd_strings[next_cmd].get ();
13704 next_cmd++;
13705 }
13706
13707 return rslt;
13708 }
13709
13710 /* Given information about a tracepoint as recorded on a target (which
13711 can be either a live system or a trace file), attempt to create an
13712 equivalent GDB tracepoint. This is not a reliable process, since
13713 the target does not necessarily have all the information used when
13714 the tracepoint was originally defined. */
13715
13716 struct tracepoint *
13717 create_tracepoint_from_upload (struct uploaded_tp *utp)
13718 {
13719 const char *addr_str;
13720 char small_buf[100];
13721 struct tracepoint *tp;
13722
13723 if (utp->at_string)
13724 addr_str = utp->at_string.get ();
13725 else
13726 {
13727 /* In the absence of a source location, fall back to raw
13728 address. Since there is no way to confirm that the address
13729 means the same thing as when the trace was started, warn the
13730 user. */
13731 warning (_("Uploaded tracepoint %d has no "
13732 "source location, using raw address"),
13733 utp->number);
13734 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
13735 addr_str = small_buf;
13736 }
13737
13738 /* There's not much we can do with a sequence of bytecodes. */
13739 if (utp->cond && !utp->cond_string)
13740 warning (_("Uploaded tracepoint %d condition "
13741 "has no source form, ignoring it"),
13742 utp->number);
13743
13744 event_location_up location = string_to_event_location (&addr_str,
13745 current_language);
13746 if (!create_breakpoint (get_current_arch (),
13747 location.get (),
13748 utp->cond_string.get (), -1, addr_str,
13749 false /* force_condition */,
13750 0 /* parse cond/thread */,
13751 0 /* tempflag */,
13752 utp->type /* type_wanted */,
13753 0 /* Ignore count */,
13754 pending_break_support,
13755 &base_breakpoint_ops,
13756 0 /* from_tty */,
13757 utp->enabled /* enabled */,
13758 0 /* internal */,
13759 CREATE_BREAKPOINT_FLAGS_INSERTED))
13760 return NULL;
13761
13762 /* Get the tracepoint we just created. */
13763 tp = get_tracepoint (tracepoint_count);
13764 gdb_assert (tp != NULL);
13765
13766 if (utp->pass > 0)
13767 {
13768 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
13769 tp->number);
13770
13771 trace_pass_command (small_buf, 0);
13772 }
13773
13774 /* If we have uploaded versions of the original commands, set up a
13775 special-purpose "reader" function and call the usual command line
13776 reader, then pass the result to the breakpoint command-setting
13777 function. */
13778 if (!utp->cmd_strings.empty ())
13779 {
13780 counted_command_line cmd_list;
13781
13782 this_utp = utp;
13783 next_cmd = 0;
13784
13785 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
13786
13787 breakpoint_set_commands (tp, std::move (cmd_list));
13788 }
13789 else if (!utp->actions.empty ()
13790 || !utp->step_actions.empty ())
13791 warning (_("Uploaded tracepoint %d actions "
13792 "have no source form, ignoring them"),
13793 utp->number);
13794
13795 /* Copy any status information that might be available. */
13796 tp->hit_count = utp->hit_count;
13797 tp->traceframe_usage = utp->traceframe_usage;
13798
13799 return tp;
13800 }
13801
13802 /* Print information on tracepoint number TPNUM_EXP, or all if
13803 omitted. */
13804
13805 static void
13806 info_tracepoints_command (const char *args, int from_tty)
13807 {
13808 struct ui_out *uiout = current_uiout;
13809 int num_printed;
13810
13811 num_printed = breakpoint_1 (args, false, is_tracepoint);
13812
13813 if (num_printed == 0)
13814 {
13815 if (args == NULL || *args == '\0')
13816 uiout->message ("No tracepoints.\n");
13817 else
13818 uiout->message ("No tracepoint matching '%s'.\n", args);
13819 }
13820
13821 default_collect_info ();
13822 }
13823
13824 /* The 'enable trace' command enables tracepoints.
13825 Not supported by all targets. */
13826 static void
13827 enable_trace_command (const char *args, int from_tty)
13828 {
13829 enable_command (args, from_tty);
13830 }
13831
13832 /* The 'disable trace' command disables tracepoints.
13833 Not supported by all targets. */
13834 static void
13835 disable_trace_command (const char *args, int from_tty)
13836 {
13837 disable_command (args, from_tty);
13838 }
13839
13840 /* Remove a tracepoint (or all if no argument). */
13841 static void
13842 delete_trace_command (const char *arg, int from_tty)
13843 {
13844 dont_repeat ();
13845
13846 if (arg == 0)
13847 {
13848 int breaks_to_delete = 0;
13849
13850 /* Delete all breakpoints if no argument.
13851 Do not delete internal or call-dummy breakpoints, these
13852 have to be deleted with an explicit breakpoint number
13853 argument. */
13854 for (breakpoint *tp : all_tracepoints ())
13855 if (is_tracepoint (tp) && user_breakpoint_p (tp))
13856 {
13857 breaks_to_delete = 1;
13858 break;
13859 }
13860
13861 /* Ask user only if there are some breakpoints to delete. */
13862 if (!from_tty
13863 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13864 {
13865 for (breakpoint *b : all_breakpoints_safe ())
13866 if (is_tracepoint (b) && user_breakpoint_p (b))
13867 delete_breakpoint (b);
13868 }
13869 }
13870 else
13871 map_breakpoint_numbers
13872 (arg, [&] (breakpoint *br)
13873 {
13874 iterate_over_related_breakpoints (br, delete_breakpoint);
13875 });
13876 }
13877
13878 /* Helper function for trace_pass_command. */
13879
13880 static void
13881 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
13882 {
13883 tp->pass_count = count;
13884 gdb::observers::breakpoint_modified.notify (tp);
13885 if (from_tty)
13886 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
13887 tp->number, count);
13888 }
13889
13890 /* Set passcount for tracepoint.
13891
13892 First command argument is passcount, second is tracepoint number.
13893 If tracepoint number omitted, apply to most recently defined.
13894 Also accepts special argument "all". */
13895
13896 static void
13897 trace_pass_command (const char *args, int from_tty)
13898 {
13899 struct tracepoint *t1;
13900 ULONGEST count;
13901
13902 if (args == 0 || *args == 0)
13903 error (_("passcount command requires an "
13904 "argument (count + optional TP num)"));
13905
13906 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
13907
13908 args = skip_spaces (args);
13909 if (*args && strncasecmp (args, "all", 3) == 0)
13910 {
13911 args += 3; /* Skip special argument "all". */
13912 if (*args)
13913 error (_("Junk at end of arguments."));
13914
13915 for (breakpoint *b : all_tracepoints ())
13916 {
13917 t1 = (struct tracepoint *) b;
13918 trace_pass_set_count (t1, count, from_tty);
13919 }
13920 }
13921 else if (*args == '\0')
13922 {
13923 t1 = get_tracepoint_by_number (&args, NULL);
13924 if (t1)
13925 trace_pass_set_count (t1, count, from_tty);
13926 }
13927 else
13928 {
13929 number_or_range_parser parser (args);
13930 while (!parser.finished ())
13931 {
13932 t1 = get_tracepoint_by_number (&args, &parser);
13933 if (t1)
13934 trace_pass_set_count (t1, count, from_tty);
13935 }
13936 }
13937 }
13938
13939 struct tracepoint *
13940 get_tracepoint (int num)
13941 {
13942 for (breakpoint *t : all_tracepoints ())
13943 if (t->number == num)
13944 return (struct tracepoint *) t;
13945
13946 return NULL;
13947 }
13948
13949 /* Find the tracepoint with the given target-side number (which may be
13950 different from the tracepoint number after disconnecting and
13951 reconnecting). */
13952
13953 struct tracepoint *
13954 get_tracepoint_by_number_on_target (int num)
13955 {
13956 for (breakpoint *b : all_tracepoints ())
13957 {
13958 struct tracepoint *t = (struct tracepoint *) b;
13959
13960 if (t->number_on_target == num)
13961 return t;
13962 }
13963
13964 return NULL;
13965 }
13966
13967 /* Utility: parse a tracepoint number and look it up in the list.
13968 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
13969 If the argument is missing, the most recent tracepoint
13970 (tracepoint_count) is returned. */
13971
13972 struct tracepoint *
13973 get_tracepoint_by_number (const char **arg,
13974 number_or_range_parser *parser)
13975 {
13976 int tpnum;
13977 const char *instring = arg == NULL ? NULL : *arg;
13978
13979 if (parser != NULL)
13980 {
13981 gdb_assert (!parser->finished ());
13982 tpnum = parser->get_number ();
13983 }
13984 else if (arg == NULL || *arg == NULL || ! **arg)
13985 tpnum = tracepoint_count;
13986 else
13987 tpnum = get_number (arg);
13988
13989 if (tpnum <= 0)
13990 {
13991 if (instring && *instring)
13992 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
13993 instring);
13994 else
13995 gdb_printf (_("No previous tracepoint\n"));
13996 return NULL;
13997 }
13998
13999 for (breakpoint *t : all_tracepoints ())
14000 if (t->number == tpnum)
14001 return (struct tracepoint *) t;
14002
14003 gdb_printf ("No tracepoint number %d.\n", tpnum);
14004 return NULL;
14005 }
14006
14007 void
14008 breakpoint::print_recreate_thread (struct ui_file *fp) const
14009 {
14010 if (thread != -1)
14011 gdb_printf (fp, " thread %d", thread);
14012
14013 if (task != 0)
14014 gdb_printf (fp, " task %d", task);
14015
14016 gdb_printf (fp, "\n");
14017 }
14018
14019 /* Save information on user settable breakpoints (watchpoints, etc) to
14020 a new script file named FILENAME. If FILTER is non-NULL, call it
14021 on each breakpoint and only include the ones for which it returns
14022 true. */
14023
14024 static void
14025 save_breakpoints (const char *filename, int from_tty,
14026 bool (*filter) (const struct breakpoint *))
14027 {
14028 int any = 0;
14029 int extra_trace_bits = 0;
14030
14031 if (filename == 0 || *filename == 0)
14032 error (_("Argument required (file name in which to save)"));
14033
14034 /* See if we have anything to save. */
14035 for (breakpoint *tp : all_breakpoints ())
14036 {
14037 /* Skip internal and momentary breakpoints. */
14038 if (!user_breakpoint_p (tp))
14039 continue;
14040
14041 /* If we have a filter, only save the breakpoints it accepts. */
14042 if (filter && !filter (tp))
14043 continue;
14044
14045 any = 1;
14046
14047 if (is_tracepoint (tp))
14048 {
14049 extra_trace_bits = 1;
14050
14051 /* We can stop searching. */
14052 break;
14053 }
14054 }
14055
14056 if (!any)
14057 {
14058 warning (_("Nothing to save."));
14059 return;
14060 }
14061
14062 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14063
14064 stdio_file fp;
14065
14066 if (!fp.open (expanded_filename.get (), "w"))
14067 error (_("Unable to open file '%s' for saving (%s)"),
14068 expanded_filename.get (), safe_strerror (errno));
14069
14070 if (extra_trace_bits)
14071 save_trace_state_variables (&fp);
14072
14073 for (breakpoint *tp : all_breakpoints ())
14074 {
14075 /* Skip internal and momentary breakpoints. */
14076 if (!user_breakpoint_p (tp))
14077 continue;
14078
14079 /* If we have a filter, only save the breakpoints it accepts. */
14080 if (filter && !filter (tp))
14081 continue;
14082
14083 tp->print_recreate (&fp);
14084
14085 /* Note, we can't rely on tp->number for anything, as we can't
14086 assume the recreated breakpoint numbers will match. Use $bpnum
14087 instead. */
14088
14089 if (tp->cond_string)
14090 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
14091
14092 if (tp->ignore_count)
14093 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
14094
14095 if (tp->type != bp_dprintf && tp->commands)
14096 {
14097 fp.puts (" commands\n");
14098
14099 current_uiout->redirect (&fp);
14100 try
14101 {
14102 print_command_lines (current_uiout, tp->commands.get (), 2);
14103 }
14104 catch (const gdb_exception &ex)
14105 {
14106 current_uiout->redirect (NULL);
14107 throw;
14108 }
14109
14110 current_uiout->redirect (NULL);
14111 fp.puts (" end\n");
14112 }
14113
14114 if (tp->enable_state == bp_disabled)
14115 fp.puts ("disable $bpnum\n");
14116
14117 /* If this is a multi-location breakpoint, check if the locations
14118 should be individually disabled. Watchpoint locations are
14119 special, and not user visible. */
14120 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14121 {
14122 int n = 1;
14123
14124 for (bp_location *loc : tp->locations ())
14125 {
14126 if (!loc->enabled)
14127 fp.printf ("disable $bpnum.%d\n", n);
14128
14129 n++;
14130 }
14131 }
14132 }
14133
14134 if (extra_trace_bits && !default_collect.empty ())
14135 fp.printf ("set default-collect %s\n", default_collect.c_str ());
14136
14137 if (from_tty)
14138 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
14139 }
14140
14141 /* The `save breakpoints' command. */
14142
14143 static void
14144 save_breakpoints_command (const char *args, int from_tty)
14145 {
14146 save_breakpoints (args, from_tty, NULL);
14147 }
14148
14149 /* The `save tracepoints' command. */
14150
14151 static void
14152 save_tracepoints_command (const char *args, int from_tty)
14153 {
14154 save_breakpoints (args, from_tty, is_tracepoint);
14155 }
14156
14157 \f
14158 /* This help string is used to consolidate all the help string for specifying
14159 locations used by several commands. */
14160
14161 #define LOCATION_HELP_STRING \
14162 "Linespecs are colon-separated lists of location parameters, such as\n\
14163 source filename, function name, label name, and line number.\n\
14164 Example: To specify the start of a label named \"the_top\" in the\n\
14165 function \"fact\" in the file \"factorial.c\", use\n\
14166 \"factorial.c:fact:the_top\".\n\
14167 \n\
14168 Address locations begin with \"*\" and specify an exact address in the\n\
14169 program. Example: To specify the fourth byte past the start function\n\
14170 \"main\", use \"*main + 4\".\n\
14171 \n\
14172 Explicit locations are similar to linespecs but use an option/argument\n\
14173 syntax to specify location parameters.\n\
14174 Example: To specify the start of the label named \"the_top\" in the\n\
14175 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
14176 -function fact -label the_top\".\n\
14177 \n\
14178 By default, a specified function is matched against the program's\n\
14179 functions in all scopes. For C++, this means in all namespaces and\n\
14180 classes. For Ada, this means in all packages. E.g., in C++,\n\
14181 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14182 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
14183 specified name as a complete fully-qualified name instead."
14184
14185 /* This help string is used for the break, hbreak, tbreak and thbreak
14186 commands. It is defined as a macro to prevent duplication.
14187 COMMAND should be a string constant containing the name of the
14188 command. */
14189
14190 #define BREAK_ARGS_HELP(command) \
14191 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14192 \t[-force-condition] [if CONDITION]\n\
14193 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14194 probe point. Accepted values are `-probe' (for a generic, automatically\n\
14195 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14196 `-probe-dtrace' (for a DTrace probe).\n\
14197 LOCATION may be a linespec, address, or explicit location as described\n\
14198 below.\n\
14199 \n\
14200 With no LOCATION, uses current execution address of the selected\n\
14201 stack frame. This is useful for breaking on return to a stack frame.\n\
14202 \n\
14203 THREADNUM is the number from \"info threads\".\n\
14204 CONDITION is a boolean expression.\n\
14205 \n\
14206 With the \"-force-condition\" flag, the condition is defined even when\n\
14207 it is invalid for all current locations.\n\
14208 \n" LOCATION_HELP_STRING "\n\n\
14209 Multiple breakpoints at one place are permitted, and useful if their\n\
14210 conditions are different.\n\
14211 \n\
14212 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14213
14214 /* List of subcommands for "catch". */
14215 static struct cmd_list_element *catch_cmdlist;
14216
14217 /* List of subcommands for "tcatch". */
14218 static struct cmd_list_element *tcatch_cmdlist;
14219
14220 void
14221 add_catch_command (const char *name, const char *docstring,
14222 cmd_func_ftype *func,
14223 completer_ftype *completer,
14224 void *user_data_catch,
14225 void *user_data_tcatch)
14226 {
14227 struct cmd_list_element *command;
14228
14229 command = add_cmd (name, class_breakpoint, docstring,
14230 &catch_cmdlist);
14231 command->func = func;
14232 command->set_context (user_data_catch);
14233 set_cmd_completer (command, completer);
14234
14235 command = add_cmd (name, class_breakpoint, docstring,
14236 &tcatch_cmdlist);
14237 command->func = func;
14238 command->set_context (user_data_tcatch);
14239 set_cmd_completer (command, completer);
14240 }
14241
14242 /* Zero if any of the breakpoint's locations could be a location where
14243 functions have been inlined, nonzero otherwise. */
14244
14245 static int
14246 is_non_inline_function (struct breakpoint *b)
14247 {
14248 /* The shared library event breakpoint is set on the address of a
14249 non-inline function. */
14250 if (b->type == bp_shlib_event)
14251 return 1;
14252
14253 return 0;
14254 }
14255
14256 /* Nonzero if the specified PC cannot be a location where functions
14257 have been inlined. */
14258
14259 int
14260 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
14261 const target_waitstatus &ws)
14262 {
14263 for (breakpoint *b : all_breakpoints ())
14264 {
14265 if (!is_non_inline_function (b))
14266 continue;
14267
14268 for (bp_location *bl : b->locations ())
14269 {
14270 if (!bl->shlib_disabled
14271 && bpstat_check_location (bl, aspace, pc, ws))
14272 return 1;
14273 }
14274 }
14275
14276 return 0;
14277 }
14278
14279 /* Remove any references to OBJFILE which is going to be freed. */
14280
14281 void
14282 breakpoint_free_objfile (struct objfile *objfile)
14283 {
14284 for (bp_location *loc : all_bp_locations ())
14285 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
14286 loc->symtab = NULL;
14287 }
14288
14289 /* Chain containing all defined "enable breakpoint" subcommands. */
14290
14291 static struct cmd_list_element *enablebreaklist = NULL;
14292
14293 /* See breakpoint.h. */
14294
14295 cmd_list_element *commands_cmd_element = nullptr;
14296
14297 void _initialize_breakpoint ();
14298 void
14299 _initialize_breakpoint ()
14300 {
14301 struct cmd_list_element *c;
14302
14303 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14304 "breakpoint");
14305 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14306 "breakpoint");
14307 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14308 "breakpoint");
14309
14310 breakpoint_chain = 0;
14311 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14312 before a breakpoint is set. */
14313 breakpoint_count = 0;
14314
14315 tracepoint_count = 0;
14316
14317 add_com ("ignore", class_breakpoint, ignore_command, _("\
14318 Set ignore-count of breakpoint number N to COUNT.\n\
14319 Usage is `ignore N COUNT'."));
14320
14321 commands_cmd_element = add_com ("commands", class_breakpoint,
14322 commands_command, _("\
14323 Set commands to be executed when the given breakpoints are hit.\n\
14324 Give a space-separated breakpoint list as argument after \"commands\".\n\
14325 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14326 (e.g. `5-7').\n\
14327 With no argument, the targeted breakpoint is the last one set.\n\
14328 The commands themselves follow starting on the next line.\n\
14329 Type a line containing \"end\" to indicate the end of them.\n\
14330 Give \"silent\" as the first line to make the breakpoint silent;\n\
14331 then no output is printed when it is hit, except what the commands print."));
14332
14333 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14334 static std::string condition_command_help
14335 = gdb::option::build_help (_("\
14336 Specify breakpoint number N to break only if COND is true.\n\
14337 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
14338 is an expression to be evaluated whenever breakpoint N is reached.\n\
14339 \n\
14340 Options:\n\
14341 %OPTIONS%"), cc_opts);
14342
14343 c = add_com ("condition", class_breakpoint, condition_command,
14344 condition_command_help.c_str ());
14345 set_cmd_completer_handle_brkchars (c, condition_completer);
14346
14347 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
14348 Set a temporary breakpoint.\n\
14349 Like \"break\" except the breakpoint is only temporary,\n\
14350 so it will be deleted when hit. Equivalent to \"break\" followed\n\
14351 by using \"enable delete\" on the breakpoint number.\n\
14352 \n"
14353 BREAK_ARGS_HELP ("tbreak")));
14354 set_cmd_completer (c, location_completer);
14355
14356 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
14357 Set a hardware assisted breakpoint.\n\
14358 Like \"break\" except the breakpoint requires hardware support,\n\
14359 some target hardware may not have this support.\n\
14360 \n"
14361 BREAK_ARGS_HELP ("hbreak")));
14362 set_cmd_completer (c, location_completer);
14363
14364 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
14365 Set a temporary hardware assisted breakpoint.\n\
14366 Like \"hbreak\" except the breakpoint is only temporary,\n\
14367 so it will be deleted when hit.\n\
14368 \n"
14369 BREAK_ARGS_HELP ("thbreak")));
14370 set_cmd_completer (c, location_completer);
14371
14372 cmd_list_element *enable_cmd
14373 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14374 Enable all or some breakpoints.\n\
14375 Usage: enable [BREAKPOINTNUM]...\n\
14376 Give breakpoint numbers (separated by spaces) as arguments.\n\
14377 With no subcommand, breakpoints are enabled until you command otherwise.\n\
14378 This is used to cancel the effect of the \"disable\" command.\n\
14379 With a subcommand you can enable temporarily."),
14380 &enablelist, 1, &cmdlist);
14381
14382 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
14383
14384 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
14385 Enable all or some breakpoints.\n\
14386 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
14387 Give breakpoint numbers (separated by spaces) as arguments.\n\
14388 This is used to cancel the effect of the \"disable\" command.\n\
14389 May be abbreviated to simply \"enable\"."),
14390 &enablebreaklist, 1, &enablelist);
14391
14392 add_cmd ("once", no_class, enable_once_command, _("\
14393 Enable some breakpoints for one hit.\n\
14394 Usage: enable breakpoints once BREAKPOINTNUM...\n\
14395 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14396 &enablebreaklist);
14397
14398 add_cmd ("delete", no_class, enable_delete_command, _("\
14399 Enable some breakpoints and delete when hit.\n\
14400 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
14401 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14402 &enablebreaklist);
14403
14404 add_cmd ("count", no_class, enable_count_command, _("\
14405 Enable some breakpoints for COUNT hits.\n\
14406 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
14407 If a breakpoint is hit while enabled in this fashion,\n\
14408 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14409 &enablebreaklist);
14410
14411 add_cmd ("delete", no_class, enable_delete_command, _("\
14412 Enable some breakpoints and delete when hit.\n\
14413 Usage: enable delete BREAKPOINTNUM...\n\
14414 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14415 &enablelist);
14416
14417 add_cmd ("once", no_class, enable_once_command, _("\
14418 Enable some breakpoints for one hit.\n\
14419 Usage: enable once BREAKPOINTNUM...\n\
14420 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14421 &enablelist);
14422
14423 add_cmd ("count", no_class, enable_count_command, _("\
14424 Enable some breakpoints for COUNT hits.\n\
14425 Usage: enable count COUNT BREAKPOINTNUM...\n\
14426 If a breakpoint is hit while enabled in this fashion,\n\
14427 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14428 &enablelist);
14429
14430 cmd_list_element *disable_cmd
14431 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14432 Disable all or some breakpoints.\n\
14433 Usage: disable [BREAKPOINTNUM]...\n\
14434 Arguments are breakpoint numbers with spaces in between.\n\
14435 To disable all breakpoints, give no argument.\n\
14436 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
14437 &disablelist, 1, &cmdlist);
14438 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14439 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
14440
14441 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
14442 Disable all or some breakpoints.\n\
14443 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
14444 Arguments are breakpoint numbers with spaces in between.\n\
14445 To disable all breakpoints, give no argument.\n\
14446 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
14447 This command may be abbreviated \"disable\"."),
14448 &disablelist);
14449
14450 cmd_list_element *delete_cmd
14451 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14452 Delete all or some breakpoints.\n\
14453 Usage: delete [BREAKPOINTNUM]...\n\
14454 Arguments are breakpoint numbers with spaces in between.\n\
14455 To delete all breakpoints, give no argument.\n\
14456 \n\
14457 Also a prefix command for deletion of other GDB objects."),
14458 &deletelist, 1, &cmdlist);
14459 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14460 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
14461
14462 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
14463 Delete all or some breakpoints or auto-display expressions.\n\
14464 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
14465 Arguments are breakpoint numbers with spaces in between.\n\
14466 To delete all breakpoints, give no argument.\n\
14467 This command may be abbreviated \"delete\"."),
14468 &deletelist);
14469
14470 cmd_list_element *clear_cmd
14471 = add_com ("clear", class_breakpoint, clear_command, _("\
14472 Clear breakpoint at specified location.\n\
14473 Argument may be a linespec, explicit, or address location as described below.\n\
14474 \n\
14475 With no argument, clears all breakpoints in the line that the selected frame\n\
14476 is executing in.\n"
14477 "\n" LOCATION_HELP_STRING "\n\n\
14478 See also the \"delete\" command which clears breakpoints by number."));
14479 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
14480
14481 cmd_list_element *break_cmd
14482 = add_com ("break", class_breakpoint, break_command, _("\
14483 Set breakpoint at specified location.\n"
14484 BREAK_ARGS_HELP ("break")));
14485 set_cmd_completer (break_cmd, location_completer);
14486
14487 add_com_alias ("b", break_cmd, class_run, 1);
14488 add_com_alias ("br", break_cmd, class_run, 1);
14489 add_com_alias ("bre", break_cmd, class_run, 1);
14490 add_com_alias ("brea", break_cmd, class_run, 1);
14491
14492 cmd_list_element *info_breakpoints_cmd
14493 = add_info ("breakpoints", info_breakpoints_command, _("\
14494 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
14495 The \"Type\" column indicates one of:\n\
14496 \tbreakpoint - normal breakpoint\n\
14497 \twatchpoint - watchpoint\n\
14498 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14499 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14500 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14501 address and file/line number respectively.\n\
14502 \n\
14503 Convenience variable \"$_\" and default examine address for \"x\"\n\
14504 are set to the address of the last breakpoint listed unless the command\n\
14505 is prefixed with \"server \".\n\n\
14506 Convenience variable \"$bpnum\" contains the number of the last\n\
14507 breakpoint set."));
14508
14509 add_info_alias ("b", info_breakpoints_cmd, 1);
14510
14511 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14512 Status of all breakpoints, or breakpoint number NUMBER.\n\
14513 The \"Type\" column indicates one of:\n\
14514 \tbreakpoint - normal breakpoint\n\
14515 \twatchpoint - watchpoint\n\
14516 \tlongjmp - internal breakpoint used to step through longjmp()\n\
14517 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14518 \tuntil - internal breakpoint used by the \"until\" command\n\
14519 \tfinish - internal breakpoint used by the \"finish\" command\n\
14520 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14521 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14522 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14523 address and file/line number respectively.\n\
14524 \n\
14525 Convenience variable \"$_\" and default examine address for \"x\"\n\
14526 are set to the address of the last breakpoint listed unless the command\n\
14527 is prefixed with \"server \".\n\n\
14528 Convenience variable \"$bpnum\" contains the number of the last\n\
14529 breakpoint set."),
14530 &maintenanceinfolist);
14531
14532 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
14533 Set catchpoints to catch events."),
14534 &catch_cmdlist,
14535 0/*allow-unknown*/, &cmdlist);
14536
14537 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
14538 Set temporary catchpoints to catch events."),
14539 &tcatch_cmdlist,
14540 0/*allow-unknown*/, &cmdlist);
14541
14542 const auto opts = make_watch_options_def_group (nullptr);
14543
14544 static const std::string watch_help = gdb::option::build_help (_("\
14545 Set a watchpoint for EXPRESSION.\n\
14546 Usage: watch [-location] EXPRESSION\n\
14547 \n\
14548 Options:\n\
14549 %OPTIONS%\n\
14550 \n\
14551 A watchpoint stops execution of your program whenever the value of\n\
14552 an expression changes."), opts);
14553 c = add_com ("watch", class_breakpoint, watch_command,
14554 watch_help.c_str ());
14555 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14556
14557 static const std::string rwatch_help = gdb::option::build_help (_("\
14558 Set a read watchpoint for EXPRESSION.\n\
14559 Usage: rwatch [-location] EXPRESSION\n\
14560 \n\
14561 Options:\n\
14562 %OPTIONS%\n\
14563 \n\
14564 A read watchpoint stops execution of your program whenever the value of\n\
14565 an expression is read."), opts);
14566 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14567 rwatch_help.c_str ());
14568 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14569
14570 static const std::string awatch_help = gdb::option::build_help (_("\
14571 Set an access watchpoint for EXPRESSION.\n\
14572 Usage: awatch [-location] EXPRESSION\n\
14573 \n\
14574 Options:\n\
14575 %OPTIONS%\n\
14576 \n\
14577 An access watchpoint stops execution of your program whenever the value\n\
14578 of an expression is either read or written."), opts);
14579 c = add_com ("awatch", class_breakpoint, awatch_command,
14580 awatch_help.c_str ());
14581 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14582
14583 add_info ("watchpoints", info_watchpoints_command, _("\
14584 Status of specified watchpoints (all watchpoints if no argument)."));
14585
14586 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14587 respond to changes - contrary to the description. */
14588 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14589 &can_use_hw_watchpoints, _("\
14590 Set debugger's willingness to use watchpoint hardware."), _("\
14591 Show debugger's willingness to use watchpoint hardware."), _("\
14592 If zero, gdb will not use hardware for new watchpoints, even if\n\
14593 such is available. (However, any hardware watchpoints that were\n\
14594 created before setting this to nonzero, will continue to use watchpoint\n\
14595 hardware.)"),
14596 NULL,
14597 show_can_use_hw_watchpoints,
14598 &setlist, &showlist);
14599
14600 can_use_hw_watchpoints = 1;
14601
14602 /* Tracepoint manipulation commands. */
14603
14604 cmd_list_element *trace_cmd
14605 = add_com ("trace", class_breakpoint, trace_command, _("\
14606 Set a tracepoint at specified location.\n\
14607 \n"
14608 BREAK_ARGS_HELP ("trace") "\n\
14609 Do \"help tracepoints\" for info on other tracepoint commands."));
14610 set_cmd_completer (trace_cmd, location_completer);
14611
14612 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14613 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14614 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14615 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
14616
14617 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
14618 Set a fast tracepoint at specified location.\n\
14619 \n"
14620 BREAK_ARGS_HELP ("ftrace") "\n\
14621 Do \"help tracepoints\" for info on other tracepoint commands."));
14622 set_cmd_completer (c, location_completer);
14623
14624 c = add_com ("strace", class_breakpoint, strace_command, _("\
14625 Set a static tracepoint at location or marker.\n\
14626 \n\
14627 strace [LOCATION] [if CONDITION]\n\
14628 LOCATION may be a linespec, explicit, or address location (described below) \n\
14629 or -m MARKER_ID.\n\n\
14630 If a marker id is specified, probe the marker with that name. With\n\
14631 no LOCATION, uses current execution address of the selected stack frame.\n\
14632 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14633 This collects arbitrary user data passed in the probe point call to the\n\
14634 tracing library. You can inspect it when analyzing the trace buffer,\n\
14635 by printing the $_sdata variable like any other convenience variable.\n\
14636 \n\
14637 CONDITION is a boolean expression.\n\
14638 \n" LOCATION_HELP_STRING "\n\n\
14639 Multiple tracepoints at one place are permitted, and useful if their\n\
14640 conditions are different.\n\
14641 \n\
14642 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14643 Do \"help tracepoints\" for info on other tracepoint commands."));
14644 set_cmd_completer (c, location_completer);
14645
14646 cmd_list_element *info_tracepoints_cmd
14647 = add_info ("tracepoints", info_tracepoints_command, _("\
14648 Status of specified tracepoints (all tracepoints if no argument).\n\
14649 Convenience variable \"$tpnum\" contains the number of the\n\
14650 last tracepoint set."));
14651
14652 add_info_alias ("tp", info_tracepoints_cmd, 1);
14653
14654 cmd_list_element *delete_tracepoints_cmd
14655 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
14656 Delete specified tracepoints.\n\
14657 Arguments are tracepoint numbers, separated by spaces.\n\
14658 No argument means delete all tracepoints."),
14659 &deletelist);
14660 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
14661
14662 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14663 Disable specified tracepoints.\n\
14664 Arguments are tracepoint numbers, separated by spaces.\n\
14665 No argument means disable all tracepoints."),
14666 &disablelist);
14667 deprecate_cmd (c, "disable");
14668
14669 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14670 Enable specified tracepoints.\n\
14671 Arguments are tracepoint numbers, separated by spaces.\n\
14672 No argument means enable all tracepoints."),
14673 &enablelist);
14674 deprecate_cmd (c, "enable");
14675
14676 add_com ("passcount", class_trace, trace_pass_command, _("\
14677 Set the passcount for a tracepoint.\n\
14678 The trace will end when the tracepoint has been passed 'count' times.\n\
14679 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14680 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14681
14682 add_basic_prefix_cmd ("save", class_breakpoint,
14683 _("Save breakpoint definitions as a script."),
14684 &save_cmdlist,
14685 0/*allow-unknown*/, &cmdlist);
14686
14687 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14688 Save current breakpoint definitions as a script.\n\
14689 This includes all types of breakpoints (breakpoints, watchpoints,\n\
14690 catchpoints, tracepoints). Use the 'source' command in another debug\n\
14691 session to restore them."),
14692 &save_cmdlist);
14693 set_cmd_completer (c, filename_completer);
14694
14695 cmd_list_element *save_tracepoints_cmd
14696 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
14697 Save current tracepoint definitions as a script.\n\
14698 Use the 'source' command in another debug session to restore them."),
14699 &save_cmdlist);
14700 set_cmd_completer (save_tracepoints_cmd, filename_completer);
14701
14702 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
14703 deprecate_cmd (c, "save tracepoints");
14704
14705 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14706 _("\
14707 Breakpoint specific settings.\n\
14708 Configure various breakpoint-specific variables such as\n\
14709 pending breakpoint behavior."),
14710 _("\
14711 Breakpoint specific settings.\n\
14712 Configure various breakpoint-specific variables such as\n\
14713 pending breakpoint behavior."),
14714 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14715 &setlist, &showlist);
14716
14717 add_setshow_auto_boolean_cmd ("pending", no_class,
14718 &pending_break_support, _("\
14719 Set debugger's behavior regarding pending breakpoints."), _("\
14720 Show debugger's behavior regarding pending breakpoints."), _("\
14721 If on, an unrecognized breakpoint location will cause gdb to create a\n\
14722 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14723 an error. If auto, an unrecognized breakpoint location results in a\n\
14724 user-query to see if a pending breakpoint should be created."),
14725 NULL,
14726 show_pending_break_support,
14727 &breakpoint_set_cmdlist,
14728 &breakpoint_show_cmdlist);
14729
14730 pending_break_support = AUTO_BOOLEAN_AUTO;
14731
14732 add_setshow_boolean_cmd ("auto-hw", no_class,
14733 &automatic_hardware_breakpoints, _("\
14734 Set automatic usage of hardware breakpoints."), _("\
14735 Show automatic usage of hardware breakpoints."), _("\
14736 If set, the debugger will automatically use hardware breakpoints for\n\
14737 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14738 a warning will be emitted for such breakpoints."),
14739 NULL,
14740 show_automatic_hardware_breakpoints,
14741 &breakpoint_set_cmdlist,
14742 &breakpoint_show_cmdlist);
14743
14744 add_setshow_boolean_cmd ("always-inserted", class_support,
14745 &always_inserted_mode, _("\
14746 Set mode for inserting breakpoints."), _("\
14747 Show mode for inserting breakpoints."), _("\
14748 When this mode is on, breakpoints are inserted immediately as soon as\n\
14749 they're created, kept inserted even when execution stops, and removed\n\
14750 only when the user deletes them. When this mode is off (the default),\n\
14751 breakpoints are inserted only when execution continues, and removed\n\
14752 when execution stops."),
14753 NULL,
14754 &show_always_inserted_mode,
14755 &breakpoint_set_cmdlist,
14756 &breakpoint_show_cmdlist);
14757
14758 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14759 condition_evaluation_enums,
14760 &condition_evaluation_mode_1, _("\
14761 Set mode of breakpoint condition evaluation."), _("\
14762 Show mode of breakpoint condition evaluation."), _("\
14763 When this is set to \"host\", breakpoint conditions will be\n\
14764 evaluated on the host's side by GDB. When it is set to \"target\",\n\
14765 breakpoint conditions will be downloaded to the target (if the target\n\
14766 supports such feature) and conditions will be evaluated on the target's side.\n\
14767 If this is set to \"auto\" (default), this will be automatically set to\n\
14768 \"target\" if it supports condition evaluation, otherwise it will\n\
14769 be set to \"host\"."),
14770 &set_condition_evaluation_mode,
14771 &show_condition_evaluation_mode,
14772 &breakpoint_set_cmdlist,
14773 &breakpoint_show_cmdlist);
14774
14775 add_com ("break-range", class_breakpoint, break_range_command, _("\
14776 Set a breakpoint for an address range.\n\
14777 break-range START-LOCATION, END-LOCATION\n\
14778 where START-LOCATION and END-LOCATION can be one of the following:\n\
14779 LINENUM, for that line in the current file,\n\
14780 FILE:LINENUM, for that line in that file,\n\
14781 +OFFSET, for that number of lines after the current line\n\
14782 or the start of the range\n\
14783 FUNCTION, for the first line in that function,\n\
14784 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14785 *ADDRESS, for the instruction at that address.\n\
14786 \n\
14787 The breakpoint will stop execution of the inferior whenever it executes\n\
14788 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14789 range (including START-LOCATION and END-LOCATION)."));
14790
14791 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
14792 Set a dynamic printf at specified location.\n\
14793 dprintf location,format string,arg1,arg2,...\n\
14794 location may be a linespec, explicit, or address location.\n"
14795 "\n" LOCATION_HELP_STRING));
14796 set_cmd_completer (c, location_completer);
14797
14798 add_setshow_enum_cmd ("dprintf-style", class_support,
14799 dprintf_style_enums, &dprintf_style, _("\
14800 Set the style of usage for dynamic printf."), _("\
14801 Show the style of usage for dynamic printf."), _("\
14802 This setting chooses how GDB will do a dynamic printf.\n\
14803 If the value is \"gdb\", then the printing is done by GDB to its own\n\
14804 console, as with the \"printf\" command.\n\
14805 If the value is \"call\", the print is done by calling a function in your\n\
14806 program; by default printf(), but you can choose a different function or\n\
14807 output stream by setting dprintf-function and dprintf-channel."),
14808 update_dprintf_commands, NULL,
14809 &setlist, &showlist);
14810
14811 add_setshow_string_cmd ("dprintf-function", class_support,
14812 &dprintf_function, _("\
14813 Set the function to use for dynamic printf."), _("\
14814 Show the function to use for dynamic printf."), NULL,
14815 update_dprintf_commands, NULL,
14816 &setlist, &showlist);
14817
14818 add_setshow_string_cmd ("dprintf-channel", class_support,
14819 &dprintf_channel, _("\
14820 Set the channel to use for dynamic printf."), _("\
14821 Show the channel to use for dynamic printf."), NULL,
14822 update_dprintf_commands, NULL,
14823 &setlist, &showlist);
14824
14825 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14826 &disconnected_dprintf, _("\
14827 Set whether dprintf continues after GDB disconnects."), _("\
14828 Show whether dprintf continues after GDB disconnects."), _("\
14829 Use this to let dprintf commands continue to hit and produce output\n\
14830 even if GDB disconnects or detaches from the target."),
14831 NULL,
14832 NULL,
14833 &setlist, &showlist);
14834
14835 add_com ("agent-printf", class_vars, agent_printf_command, _("\
14836 Target agent only formatted printing, like the C \"printf\" function.\n\
14837 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14838 This supports most C printf format specifications, like %s, %d, etc.\n\
14839 This is useful for formatted output in user-defined commands."));
14840
14841 automatic_hardware_breakpoints = true;
14842
14843 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14844 "breakpoint");
14845 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
14846 "breakpoint");
14847 }