Convert ordinary breakpoints to vtable ops
[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 enum bptype type_wanted);
96
97 static void create_breakpoints_sal_default (struct gdbarch *,
98 struct linespec_result *,
99 gdb::unique_xmalloc_ptr<char>,
100 gdb::unique_xmalloc_ptr<char>,
101 enum bptype,
102 enum bpdisp, int, int,
103 int,
104 const struct breakpoint_ops *,
105 int, int, int, unsigned);
106
107 static std::vector<symtab_and_line> decode_location_default
108 (struct breakpoint *b, struct event_location *location,
109 struct program_space *search_pspace);
110
111 static int can_use_hardware_watchpoint
112 (const std::vector<value_ref_ptr> &vals);
113
114 static void mention (struct breakpoint *);
115
116 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
117 enum bptype,
118 const struct breakpoint_ops *);
119 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
120 const struct symtab_and_line *);
121
122 /* This function is used in gdbtk sources and thus can not be made
123 static. */
124 static struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
125 struct symtab_and_line,
126 enum bptype,
127 const struct breakpoint_ops *);
128
129 static struct breakpoint *
130 momentary_breakpoint_from_master (struct breakpoint *orig,
131 enum bptype type,
132 const struct breakpoint_ops *ops,
133 int loc_enabled);
134
135 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
136
137 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
138 CORE_ADDR bpaddr,
139 enum bptype bptype);
140
141 static void describe_other_breakpoints (struct gdbarch *,
142 struct program_space *, CORE_ADDR,
143 struct obj_section *, int);
144
145 static int watchpoint_locations_match (struct bp_location *loc1,
146 struct bp_location *loc2);
147
148 static int breakpoint_locations_match (struct bp_location *loc1,
149 struct bp_location *loc2,
150 bool sw_hw_bps_match = false);
151
152 static int breakpoint_location_address_match (struct bp_location *bl,
153 const struct address_space *aspace,
154 CORE_ADDR addr);
155
156 static int breakpoint_location_address_range_overlap (struct bp_location *,
157 const address_space *,
158 CORE_ADDR, int);
159
160 static int remove_breakpoint (struct bp_location *);
161 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
162
163 static enum print_stop_action print_bp_stop_message (bpstat *bs);
164
165 static int hw_breakpoint_used_count (void);
166
167 static int hw_watchpoint_use_count (struct breakpoint *);
168
169 static int hw_watchpoint_used_count_others (struct breakpoint *except,
170 enum bptype type,
171 int *other_type_used);
172
173 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
174 int count);
175
176 static void decref_bp_location (struct bp_location **loc);
177
178 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
179
180 /* update_global_location_list's modes of operation wrt to whether to
181 insert locations now. */
182 enum ugll_insert_mode
183 {
184 /* Don't insert any breakpoint locations into the inferior, only
185 remove already-inserted locations that no longer should be
186 inserted. Functions that delete a breakpoint or breakpoints
187 should specify this mode, so that deleting a breakpoint doesn't
188 have the side effect of inserting the locations of other
189 breakpoints that are marked not-inserted, but should_be_inserted
190 returns true on them.
191
192 This behavior is useful is situations close to tear-down -- e.g.,
193 after an exec, while the target still has execution, but
194 breakpoint shadows of the previous executable image should *NOT*
195 be restored to the new image; or before detaching, where the
196 target still has execution and wants to delete breakpoints from
197 GDB's lists, and all breakpoints had already been removed from
198 the inferior. */
199 UGLL_DONT_INSERT,
200
201 /* May insert breakpoints iff breakpoints_should_be_inserted_now
202 claims breakpoints should be inserted now. */
203 UGLL_MAY_INSERT,
204
205 /* Insert locations now, irrespective of
206 breakpoints_should_be_inserted_now. E.g., say all threads are
207 stopped right now, and the user did "continue". We need to
208 insert breakpoints _before_ resuming the target, but
209 UGLL_MAY_INSERT wouldn't insert them, because
210 breakpoints_should_be_inserted_now returns false at that point,
211 as no thread is running yet. */
212 UGLL_INSERT
213 };
214
215 static void update_global_location_list (enum ugll_insert_mode);
216
217 static void update_global_location_list_nothrow (enum ugll_insert_mode);
218
219 static void insert_breakpoint_locations (void);
220
221 static void trace_pass_command (const char *, int);
222
223 static void set_tracepoint_count (int num);
224
225 static bool is_masked_watchpoint (const struct breakpoint *b);
226
227 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
228 otherwise. */
229
230 static int strace_marker_p (struct breakpoint *b);
231
232 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
233 that are implemented on top of software or hardware breakpoints
234 (user breakpoints, internal and momentary breakpoints, etc.). */
235 static struct breakpoint_ops bkpt_base_breakpoint_ops;
236
237 /* Breakpoints set on probes. */
238 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
239
240 /* Tracepoints set on probes. */
241 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
242
243 /* Dynamic printf class type. */
244 struct breakpoint_ops dprintf_breakpoint_ops;
245
246 /* The structure to be used in regular breakpoints. */
247 struct ordinary_breakpoint : public base_breakpoint
248 {
249 int resources_needed (const struct bp_location *) override;
250 enum print_stop_action print_it (struct bpstat *bs) override;
251 void print_mention () override;
252 void print_recreate (struct ui_file *fp) override;
253 };
254
255 /* Internal breakpoints. */
256 struct internal_breakpoint : public base_breakpoint
257 {
258 void re_set () override;
259 void check_status (struct bpstat *bs) override;
260 enum print_stop_action print_it (struct bpstat *bs) override;
261 void print_mention () override;
262 };
263
264 /* Momentary breakpoints. */
265 struct momentary_breakpoint : public base_breakpoint
266 {
267 void re_set () override;
268 void check_status (struct bpstat *bs) override;
269 enum print_stop_action print_it (struct bpstat *bs) override;
270 void print_mention () override;
271 };
272
273 /* DPrintf breakpoints. */
274 struct dprintf_breakpoint : public ordinary_breakpoint
275 {
276 };
277
278 /* The style in which to perform a dynamic printf. This is a user
279 option because different output options have different tradeoffs;
280 if GDB does the printing, there is better error handling if there
281 is a problem with any of the arguments, but using an inferior
282 function lets you have special-purpose printers and sending of
283 output to the same place as compiled-in print functions. */
284
285 static const char dprintf_style_gdb[] = "gdb";
286 static const char dprintf_style_call[] = "call";
287 static const char dprintf_style_agent[] = "agent";
288 static const char *const dprintf_style_enums[] = {
289 dprintf_style_gdb,
290 dprintf_style_call,
291 dprintf_style_agent,
292 NULL
293 };
294 static const char *dprintf_style = dprintf_style_gdb;
295
296 /* The function to use for dynamic printf if the preferred style is to
297 call into the inferior. The value is simply a string that is
298 copied into the command, so it can be anything that GDB can
299 evaluate to a callable address, not necessarily a function name. */
300
301 static std::string dprintf_function = "printf";
302
303 /* The channel to use for dynamic printf if the preferred style is to
304 call into the inferior; if a nonempty string, it will be passed to
305 the call as the first argument, with the format string as the
306 second. As with the dprintf function, this can be anything that
307 GDB knows how to evaluate, so in addition to common choices like
308 "stderr", this could be an app-specific expression like
309 "mystreams[curlogger]". */
310
311 static std::string dprintf_channel;
312
313 /* True if dprintf commands should continue to operate even if GDB
314 has disconnected. */
315 static bool disconnected_dprintf = true;
316
317 struct command_line *
318 breakpoint_commands (struct breakpoint *b)
319 {
320 return b->commands ? b->commands.get () : NULL;
321 }
322
323 /* Flag indicating that a command has proceeded the inferior past the
324 current breakpoint. */
325
326 static bool breakpoint_proceeded;
327
328 const char *
329 bpdisp_text (enum bpdisp disp)
330 {
331 /* NOTE: the following values are a part of MI protocol and
332 represent values of 'disp' field returned when inferior stops at
333 a breakpoint. */
334 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
335
336 return bpdisps[(int) disp];
337 }
338
339 /* Prototypes for exported functions. */
340 /* If FALSE, gdb will not use hardware support for watchpoints, even
341 if such is available. */
342 static int can_use_hw_watchpoints;
343
344 static void
345 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
346 struct cmd_list_element *c,
347 const char *value)
348 {
349 gdb_printf (file,
350 _("Debugger's willingness to use "
351 "watchpoint hardware is %s.\n"),
352 value);
353 }
354
355 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
356 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
357 for unrecognized breakpoint locations.
358 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
359 static enum auto_boolean pending_break_support;
360 static void
361 show_pending_break_support (struct ui_file *file, int from_tty,
362 struct cmd_list_element *c,
363 const char *value)
364 {
365 gdb_printf (file,
366 _("Debugger's behavior regarding "
367 "pending breakpoints is %s.\n"),
368 value);
369 }
370
371 /* If true, gdb will automatically use hardware breakpoints for breakpoints
372 set with "break" but falling in read-only memory.
373 If false, gdb will warn about such breakpoints, but won't automatically
374 use hardware breakpoints. */
375 static bool automatic_hardware_breakpoints;
376 static void
377 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
378 struct cmd_list_element *c,
379 const char *value)
380 {
381 gdb_printf (file,
382 _("Automatic usage of hardware breakpoints is %s.\n"),
383 value);
384 }
385
386 /* If on, GDB keeps breakpoints inserted even if the inferior is
387 stopped, and immediately inserts any new breakpoints as soon as
388 they're created. If off (default), GDB keeps breakpoints off of
389 the target as long as possible. That is, it delays inserting
390 breakpoints until the next resume, and removes them again when the
391 target fully stops. This is a bit safer in case GDB crashes while
392 processing user input. */
393 static bool always_inserted_mode = false;
394
395 static void
396 show_always_inserted_mode (struct ui_file *file, int from_tty,
397 struct cmd_list_element *c, const char *value)
398 {
399 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
400 value);
401 }
402
403 /* See breakpoint.h. */
404
405 int
406 breakpoints_should_be_inserted_now (void)
407 {
408 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
409 {
410 /* If breakpoints are global, they should be inserted even if no
411 thread under gdb's control is running, or even if there are
412 no threads under GDB's control yet. */
413 return 1;
414 }
415 else
416 {
417 if (always_inserted_mode)
418 {
419 /* The user wants breakpoints inserted even if all threads
420 are stopped. */
421 return 1;
422 }
423
424 for (inferior *inf : all_inferiors ())
425 if (inf->has_execution ()
426 && threads_are_executing (inf->process_target ()))
427 return 1;
428
429 /* Don't remove breakpoints yet if, even though all threads are
430 stopped, we still have events to process. */
431 for (thread_info *tp : all_non_exited_threads ())
432 if (tp->resumed () && tp->has_pending_waitstatus ())
433 return 1;
434 }
435 return 0;
436 }
437
438 static const char condition_evaluation_both[] = "host or target";
439
440 /* Modes for breakpoint condition evaluation. */
441 static const char condition_evaluation_auto[] = "auto";
442 static const char condition_evaluation_host[] = "host";
443 static const char condition_evaluation_target[] = "target";
444 static const char *const condition_evaluation_enums[] = {
445 condition_evaluation_auto,
446 condition_evaluation_host,
447 condition_evaluation_target,
448 NULL
449 };
450
451 /* Global that holds the current mode for breakpoint condition evaluation. */
452 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
453
454 /* Global that we use to display information to the user (gets its value from
455 condition_evaluation_mode_1. */
456 static const char *condition_evaluation_mode = condition_evaluation_auto;
457
458 /* Translate a condition evaluation mode MODE into either "host"
459 or "target". This is used mostly to translate from "auto" to the
460 real setting that is being used. It returns the translated
461 evaluation mode. */
462
463 static const char *
464 translate_condition_evaluation_mode (const char *mode)
465 {
466 if (mode == condition_evaluation_auto)
467 {
468 if (target_supports_evaluation_of_breakpoint_conditions ())
469 return condition_evaluation_target;
470 else
471 return condition_evaluation_host;
472 }
473 else
474 return mode;
475 }
476
477 /* Discovers what condition_evaluation_auto translates to. */
478
479 static const char *
480 breakpoint_condition_evaluation_mode (void)
481 {
482 return translate_condition_evaluation_mode (condition_evaluation_mode);
483 }
484
485 /* Return true if GDB should evaluate breakpoint conditions or false
486 otherwise. */
487
488 static int
489 gdb_evaluates_breakpoint_condition_p (void)
490 {
491 const char *mode = breakpoint_condition_evaluation_mode ();
492
493 return (mode == condition_evaluation_host);
494 }
495
496 /* Are we executing breakpoint commands? */
497 static int executing_breakpoint_commands;
498
499 /* Are overlay event breakpoints enabled? */
500 static int overlay_events_enabled;
501
502 /* See description in breakpoint.h. */
503 bool target_exact_watchpoints = false;
504
505 /* Walk the following statement or block through all breakpoints.
506 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
507 current breakpoint. */
508
509 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
510 for (B = breakpoint_chain; \
511 B ? (TMP=B->next, 1): 0; \
512 B = TMP)
513
514 /* Chains of all breakpoints defined. */
515
516 static struct breakpoint *breakpoint_chain;
517
518 /* See breakpoint.h. */
519
520 breakpoint_range
521 all_breakpoints ()
522 {
523 return breakpoint_range (breakpoint_chain);
524 }
525
526 /* See breakpoint.h. */
527
528 breakpoint_safe_range
529 all_breakpoints_safe ()
530 {
531 return breakpoint_safe_range (all_breakpoints ());
532 }
533
534 /* See breakpoint.h. */
535
536 tracepoint_range
537 all_tracepoints ()
538 {
539 return tracepoint_range (breakpoint_chain);
540 }
541
542 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
543
544 static std::vector<bp_location *> bp_locations;
545
546 /* See breakpoint.h. */
547
548 const std::vector<bp_location *> &
549 all_bp_locations ()
550 {
551 return bp_locations;
552 }
553
554 /* Range to iterate over breakpoint locations at a given address. */
555
556 struct bp_locations_at_addr_range
557 {
558 using iterator = std::vector<bp_location *>::iterator;
559
560 bp_locations_at_addr_range (CORE_ADDR addr)
561 {
562 struct compare
563 {
564 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
565 { return loc->address < addr_; }
566
567 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
568 { return addr_ < loc->address; }
569 };
570
571 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
572 addr, compare ());
573
574 m_begin = it_pair.first;
575 m_end = it_pair.second;
576 }
577
578 iterator begin () const
579 { return m_begin; }
580
581 iterator end () const
582 { return m_end; }
583
584 private:
585 iterator m_begin;
586 iterator m_end;
587 };
588
589 /* Return a range to iterate over all breakpoint locations exactly at address
590 ADDR.
591
592 If it's needed to iterate multiple times on the same range, it's possible
593 to save the range in a local variable and use it multiple times:
594
595 auto range = all_bp_locations_at_addr (addr);
596
597 for (bp_location *loc : range)
598 // use loc
599
600 for (bp_location *loc : range)
601 // use loc
602
603 This saves a bit of time, as it avoids re-doing the binary searches to find
604 the range's boundaries. Just remember not to change the bp_locations vector
605 in the mean time, as it could make the range's iterators stale. */
606
607 static bp_locations_at_addr_range
608 all_bp_locations_at_addr (CORE_ADDR addr)
609 {
610 return bp_locations_at_addr_range (addr);
611 }
612
613 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
614 ADDRESS for the current elements of BP_LOCATIONS which get a valid
615 result from bp_location_has_shadow. You can use it for roughly
616 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
617 an address you need to read. */
618
619 static CORE_ADDR bp_locations_placed_address_before_address_max;
620
621 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
622 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
623 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
624 You can use it for roughly limiting the subrange of BP_LOCATIONS to
625 scan for shadow bytes for an address you need to read. */
626
627 static CORE_ADDR bp_locations_shadow_len_after_address_max;
628
629 /* The locations that no longer correspond to any breakpoint, unlinked
630 from the bp_locations array, but for which a hit may still be
631 reported by a target. */
632 static std::vector<bp_location *> moribund_locations;
633
634 /* Number of last breakpoint made. */
635
636 static int breakpoint_count;
637
638 /* The value of `breakpoint_count' before the last command that
639 created breakpoints. If the last (break-like) command created more
640 than one breakpoint, then the difference between BREAKPOINT_COUNT
641 and PREV_BREAKPOINT_COUNT is more than one. */
642 static int prev_breakpoint_count;
643
644 /* Number of last tracepoint made. */
645
646 static int tracepoint_count;
647
648 static struct cmd_list_element *breakpoint_set_cmdlist;
649 static struct cmd_list_element *breakpoint_show_cmdlist;
650 struct cmd_list_element *save_cmdlist;
651
652 /* Return whether a breakpoint is an active enabled breakpoint. */
653 static int
654 breakpoint_enabled (struct breakpoint *b)
655 {
656 return (b->enable_state == bp_enabled);
657 }
658
659 /* Set breakpoint count to NUM. */
660
661 static void
662 set_breakpoint_count (int num)
663 {
664 prev_breakpoint_count = breakpoint_count;
665 breakpoint_count = num;
666 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
667 }
668
669 /* Used by `start_rbreak_breakpoints' below, to record the current
670 breakpoint count before "rbreak" creates any breakpoint. */
671 static int rbreak_start_breakpoint_count;
672
673 /* Called at the start an "rbreak" command to record the first
674 breakpoint made. */
675
676 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
677 {
678 rbreak_start_breakpoint_count = breakpoint_count;
679 }
680
681 /* Called at the end of an "rbreak" command to record the last
682 breakpoint made. */
683
684 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
685 {
686 prev_breakpoint_count = rbreak_start_breakpoint_count;
687 }
688
689 /* Used in run_command to zero the hit count when a new run starts. */
690
691 void
692 clear_breakpoint_hit_counts (void)
693 {
694 for (breakpoint *b : all_breakpoints ())
695 b->hit_count = 0;
696 }
697
698 \f
699 /* Return the breakpoint with the specified number, or NULL
700 if the number does not refer to an existing breakpoint. */
701
702 struct breakpoint *
703 get_breakpoint (int num)
704 {
705 for (breakpoint *b : all_breakpoints ())
706 if (b->number == num)
707 return b;
708
709 return nullptr;
710 }
711
712 \f
713
714 /* Mark locations as "conditions have changed" in case the target supports
715 evaluating conditions on its side. */
716
717 static void
718 mark_breakpoint_modified (struct breakpoint *b)
719 {
720 /* This is only meaningful if the target is
721 evaluating conditions and if the user has
722 opted for condition evaluation on the target's
723 side. */
724 if (gdb_evaluates_breakpoint_condition_p ()
725 || !target_supports_evaluation_of_breakpoint_conditions ())
726 return;
727
728 if (!is_breakpoint (b))
729 return;
730
731 for (bp_location *loc : b->locations ())
732 loc->condition_changed = condition_modified;
733 }
734
735 /* Mark location as "conditions have changed" in case the target supports
736 evaluating conditions on its side. */
737
738 static void
739 mark_breakpoint_location_modified (struct bp_location *loc)
740 {
741 /* This is only meaningful if the target is
742 evaluating conditions and if the user has
743 opted for condition evaluation on the target's
744 side. */
745 if (gdb_evaluates_breakpoint_condition_p ()
746 || !target_supports_evaluation_of_breakpoint_conditions ())
747
748 return;
749
750 if (!is_breakpoint (loc->owner))
751 return;
752
753 loc->condition_changed = condition_modified;
754 }
755
756 /* Sets the condition-evaluation mode using the static global
757 condition_evaluation_mode. */
758
759 static void
760 set_condition_evaluation_mode (const char *args, int from_tty,
761 struct cmd_list_element *c)
762 {
763 const char *old_mode, *new_mode;
764
765 if ((condition_evaluation_mode_1 == condition_evaluation_target)
766 && !target_supports_evaluation_of_breakpoint_conditions ())
767 {
768 condition_evaluation_mode_1 = condition_evaluation_mode;
769 warning (_("Target does not support breakpoint condition evaluation.\n"
770 "Using host evaluation mode instead."));
771 return;
772 }
773
774 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
775 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
776
777 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
778 settings was "auto". */
779 condition_evaluation_mode = condition_evaluation_mode_1;
780
781 /* Only update the mode if the user picked a different one. */
782 if (new_mode != old_mode)
783 {
784 /* If the user switched to a different evaluation mode, we
785 need to synch the changes with the target as follows:
786
787 "host" -> "target": Send all (valid) conditions to the target.
788 "target" -> "host": Remove all the conditions from the target.
789 */
790
791 if (new_mode == condition_evaluation_target)
792 {
793 /* Mark everything modified and synch conditions with the
794 target. */
795 for (bp_location *loc : all_bp_locations ())
796 mark_breakpoint_location_modified (loc);
797 }
798 else
799 {
800 /* Manually mark non-duplicate locations to synch conditions
801 with the target. We do this to remove all the conditions the
802 target knows about. */
803 for (bp_location *loc : all_bp_locations ())
804 if (is_breakpoint (loc->owner) && loc->inserted)
805 loc->needs_update = 1;
806 }
807
808 /* Do the update. */
809 update_global_location_list (UGLL_MAY_INSERT);
810 }
811
812 return;
813 }
814
815 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
816 what "auto" is translating to. */
817
818 static void
819 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
820 struct cmd_list_element *c, const char *value)
821 {
822 if (condition_evaluation_mode == condition_evaluation_auto)
823 gdb_printf (file,
824 _("Breakpoint condition evaluation "
825 "mode is %s (currently %s).\n"),
826 value,
827 breakpoint_condition_evaluation_mode ());
828 else
829 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
830 value);
831 }
832
833 /* Parse COND_STRING in the context of LOC and set as the condition
834 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
835 the number of LOC within its owner. In case of parsing error, mark
836 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
837
838 static void
839 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
840 int bp_num, int loc_num)
841 {
842 bool has_junk = false;
843 try
844 {
845 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
846 block_for_pc (loc->address), 0);
847 if (*cond_string != 0)
848 has_junk = true;
849 else
850 {
851 loc->cond = std::move (new_exp);
852 if (loc->disabled_by_cond && loc->enabled)
853 gdb_printf (_("Breakpoint %d's condition is now valid at "
854 "location %d, enabling.\n"),
855 bp_num, loc_num);
856
857 loc->disabled_by_cond = false;
858 }
859 }
860 catch (const gdb_exception_error &e)
861 {
862 if (loc->enabled)
863 {
864 /* Warn if a user-enabled location is now becoming disabled-by-cond.
865 BP_NUM is 0 if the breakpoint is being defined for the first
866 time using the "break ... if ..." command, and non-zero if
867 already defined. */
868 if (bp_num != 0)
869 warning (_("failed to validate condition at location %d.%d, "
870 "disabling:\n %s"), bp_num, loc_num, e.what ());
871 else
872 warning (_("failed to validate condition at location %d, "
873 "disabling:\n %s"), loc_num, e.what ());
874 }
875
876 loc->disabled_by_cond = true;
877 }
878
879 if (has_junk)
880 error (_("Garbage '%s' follows condition"), cond_string);
881 }
882
883 void
884 set_breakpoint_condition (struct breakpoint *b, const char *exp,
885 int from_tty, bool force)
886 {
887 if (*exp == 0)
888 {
889 b->cond_string.reset ();
890
891 if (is_watchpoint (b))
892 static_cast<watchpoint *> (b)->cond_exp.reset ();
893 else
894 {
895 int loc_num = 1;
896 for (bp_location *loc : b->locations ())
897 {
898 loc->cond.reset ();
899 if (loc->disabled_by_cond && loc->enabled)
900 gdb_printf (_("Breakpoint %d's condition is now valid at "
901 "location %d, enabling.\n"),
902 b->number, loc_num);
903 loc->disabled_by_cond = false;
904 loc_num++;
905
906 /* No need to free the condition agent expression
907 bytecode (if we have one). We will handle this
908 when we go through update_global_location_list. */
909 }
910 }
911
912 if (from_tty)
913 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
914 }
915 else
916 {
917 if (is_watchpoint (b))
918 {
919 innermost_block_tracker tracker;
920 const char *arg = exp;
921 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
922 if (*arg != 0)
923 error (_("Junk at end of expression"));
924 watchpoint *w = static_cast<watchpoint *> (b);
925 w->cond_exp = std::move (new_exp);
926 w->cond_exp_valid_block = tracker.block ();
927 }
928 else
929 {
930 /* Parse and set condition expressions. We make two passes.
931 In the first, we parse the condition string to see if it
932 is valid in at least one location. If so, the condition
933 would be accepted. So we go ahead and set the locations'
934 conditions. In case no valid case is found, we throw
935 the error and the condition string will be rejected.
936 This two-pass approach is taken to avoid setting the
937 state of locations in case of a reject. */
938 for (bp_location *loc : b->locations ())
939 {
940 try
941 {
942 const char *arg = exp;
943 parse_exp_1 (&arg, loc->address,
944 block_for_pc (loc->address), 0);
945 if (*arg != 0)
946 error (_("Junk at end of expression"));
947 break;
948 }
949 catch (const gdb_exception_error &e)
950 {
951 /* Condition string is invalid. If this happens to
952 be the last loc, abandon (if not forced) or continue
953 (if forced). */
954 if (loc->next == nullptr && !force)
955 throw;
956 }
957 }
958
959 /* If we reach here, the condition is valid at some locations. */
960 int loc_num = 1;
961 for (bp_location *loc : b->locations ())
962 {
963 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
964 loc_num++;
965 }
966 }
967
968 /* We know that the new condition parsed successfully. The
969 condition string of the breakpoint can be safely updated. */
970 b->cond_string = make_unique_xstrdup (exp);
971 b->condition_not_parsed = 0;
972 }
973 mark_breakpoint_modified (b);
974
975 gdb::observers::breakpoint_modified.notify (b);
976 }
977
978 /* See breakpoint.h. */
979
980 void
981 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
982 bool force)
983 {
984 for (breakpoint *b : all_breakpoints ())
985 if (b->number == bpnum)
986 {
987 /* Check if this breakpoint has a "stop" method implemented in an
988 extension language. This method and conditions entered into GDB
989 from the CLI are mutually exclusive. */
990 const struct extension_language_defn *extlang
991 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
992
993 if (extlang != NULL)
994 {
995 error (_("Only one stop condition allowed. There is currently"
996 " a %s stop condition defined for this breakpoint."),
997 ext_lang_capitalized_name (extlang));
998 }
999 set_breakpoint_condition (b, exp, from_tty, force);
1000
1001 if (is_breakpoint (b))
1002 update_global_location_list (UGLL_MAY_INSERT);
1003
1004 return;
1005 }
1006
1007 error (_("No breakpoint number %d."), bpnum);
1008 }
1009
1010 /* The options for the "condition" command. */
1011
1012 struct condition_command_opts
1013 {
1014 /* For "-force". */
1015 bool force_condition = false;
1016 };
1017
1018 static const gdb::option::option_def condition_command_option_defs[] = {
1019
1020 gdb::option::flag_option_def<condition_command_opts> {
1021 "force",
1022 [] (condition_command_opts *opts) { return &opts->force_condition; },
1023 N_("Set the condition even if it is invalid for all current locations."),
1024 },
1025
1026 };
1027
1028 /* Create an option_def_group for the "condition" options, with
1029 CC_OPTS as context. */
1030
1031 static inline gdb::option::option_def_group
1032 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1033 {
1034 return {{condition_command_option_defs}, cc_opts};
1035 }
1036
1037 /* Completion for the "condition" command. */
1038
1039 static void
1040 condition_completer (struct cmd_list_element *cmd,
1041 completion_tracker &tracker,
1042 const char *text, const char * /*word*/)
1043 {
1044 bool has_no_arguments = (*text == '\0');
1045 condition_command_opts cc_opts;
1046 const auto group = make_condition_command_options_def_group (&cc_opts);
1047 if (gdb::option::complete_options
1048 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1049 return;
1050
1051 text = skip_spaces (text);
1052 const char *space = skip_to_space (text);
1053 if (*space == '\0')
1054 {
1055 int len;
1056
1057 if (text[0] == '$')
1058 {
1059 tracker.advance_custom_word_point_by (1);
1060 /* We don't support completion of history indices. */
1061 if (!isdigit (text[1]))
1062 complete_internalvar (tracker, &text[1]);
1063 return;
1064 }
1065
1066 /* Suggest the "-force" flag if no arguments are given. If
1067 arguments were passed, they either already include the flag,
1068 or we are beyond the point of suggesting it because it's
1069 positionally the first argument. */
1070 if (has_no_arguments)
1071 gdb::option::complete_on_all_options (tracker, group);
1072
1073 /* We're completing the breakpoint number. */
1074 len = strlen (text);
1075
1076 for (breakpoint *b : all_breakpoints ())
1077 {
1078 char number[50];
1079
1080 xsnprintf (number, sizeof (number), "%d", b->number);
1081
1082 if (strncmp (number, text, len) == 0)
1083 tracker.add_completion (make_unique_xstrdup (number));
1084 }
1085
1086 return;
1087 }
1088
1089 /* We're completing the expression part. Skip the breakpoint num. */
1090 const char *exp_start = skip_spaces (space);
1091 tracker.advance_custom_word_point_by (exp_start - text);
1092 text = exp_start;
1093 const char *word = advance_to_expression_complete_word_point (tracker, text);
1094 expression_completer (cmd, tracker, text, word);
1095 }
1096
1097 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1098
1099 static void
1100 condition_command (const char *arg, int from_tty)
1101 {
1102 const char *p;
1103 int bnum;
1104
1105 if (arg == 0)
1106 error_no_arg (_("breakpoint number"));
1107
1108 p = arg;
1109
1110 /* Check if the "-force" flag was passed. */
1111 condition_command_opts cc_opts;
1112 const auto group = make_condition_command_options_def_group (&cc_opts);
1113 gdb::option::process_options
1114 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1115
1116 bnum = get_number (&p);
1117 if (bnum == 0)
1118 error (_("Bad breakpoint argument: '%s'"), arg);
1119
1120 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1121 }
1122
1123 /* Check that COMMAND do not contain commands that are suitable
1124 only for tracepoints and not suitable for ordinary breakpoints.
1125 Throw if any such commands is found. */
1126
1127 static void
1128 check_no_tracepoint_commands (struct command_line *commands)
1129 {
1130 struct command_line *c;
1131
1132 for (c = commands; c; c = c->next)
1133 {
1134 if (c->control_type == while_stepping_control)
1135 error (_("The 'while-stepping' command can "
1136 "only be used for tracepoints"));
1137
1138 check_no_tracepoint_commands (c->body_list_0.get ());
1139 check_no_tracepoint_commands (c->body_list_1.get ());
1140
1141 /* Not that command parsing removes leading whitespace and comment
1142 lines and also empty lines. So, we only need to check for
1143 command directly. */
1144 if (strstr (c->line, "collect ") == c->line)
1145 error (_("The 'collect' command can only be used for tracepoints"));
1146
1147 if (strstr (c->line, "teval ") == c->line)
1148 error (_("The 'teval' command can only be used for tracepoints"));
1149 }
1150 }
1151
1152 struct longjmp_breakpoint : public momentary_breakpoint
1153 {
1154 ~longjmp_breakpoint () override;
1155 };
1156
1157 /* Encapsulate tests for different types of tracepoints. */
1158
1159 static bool
1160 is_tracepoint_type (bptype type)
1161 {
1162 return (type == bp_tracepoint
1163 || type == bp_fast_tracepoint
1164 || type == bp_static_tracepoint);
1165 }
1166
1167 /* See breakpoint.h. */
1168
1169 bool
1170 is_tracepoint (const struct breakpoint *b)
1171 {
1172 return is_tracepoint_type (b->type);
1173 }
1174
1175 /* Factory function to create an appropriate instance of breakpoint given
1176 TYPE. */
1177
1178 static std::unique_ptr<breakpoint>
1179 new_breakpoint_from_type (bptype type)
1180 {
1181 breakpoint *b;
1182
1183 switch (type)
1184 {
1185 case bp_breakpoint:
1186 case bp_hardware_breakpoint:
1187 b = new ordinary_breakpoint ();
1188 break;
1189
1190 case bp_fast_tracepoint:
1191 case bp_static_tracepoint:
1192 case bp_tracepoint:
1193 b = new tracepoint ();
1194 break;
1195
1196 case bp_dprintf:
1197 b = new dprintf_breakpoint ();
1198 break;
1199
1200 case bp_overlay_event:
1201 case bp_longjmp_master:
1202 case bp_std_terminate_master:
1203 case bp_exception_master:
1204 case bp_thread_event:
1205 case bp_jit_event:
1206 case bp_shlib_event:
1207 b = new internal_breakpoint ();
1208 break;
1209
1210 case bp_longjmp:
1211 case bp_exception:
1212 b = new longjmp_breakpoint ();
1213 break;
1214
1215 case bp_watchpoint_scope:
1216 case bp_finish:
1217 case bp_gnu_ifunc_resolver_return:
1218 case bp_step_resume:
1219 case bp_hp_step_resume:
1220 case bp_longjmp_resume:
1221 case bp_longjmp_call_dummy:
1222 case bp_exception_resume:
1223 case bp_call_dummy:
1224 case bp_until:
1225 case bp_std_terminate:
1226 b = new momentary_breakpoint ();
1227 break;
1228
1229 default:
1230 gdb_assert_not_reached ("invalid type");
1231 }
1232
1233 return std::unique_ptr<breakpoint> (b);
1234 }
1235
1236 /* A helper function that validates that COMMANDS are valid for a
1237 breakpoint. This function will throw an exception if a problem is
1238 found. */
1239
1240 static void
1241 validate_commands_for_breakpoint (struct breakpoint *b,
1242 struct command_line *commands)
1243 {
1244 if (is_tracepoint (b))
1245 {
1246 struct tracepoint *t = (struct tracepoint *) b;
1247 struct command_line *c;
1248 struct command_line *while_stepping = 0;
1249
1250 /* Reset the while-stepping step count. The previous commands
1251 might have included a while-stepping action, while the new
1252 ones might not. */
1253 t->step_count = 0;
1254
1255 /* We need to verify that each top-level element of commands is
1256 valid for tracepoints, that there's at most one
1257 while-stepping element, and that the while-stepping's body
1258 has valid tracing commands excluding nested while-stepping.
1259 We also need to validate the tracepoint action line in the
1260 context of the tracepoint --- validate_actionline actually
1261 has side effects, like setting the tracepoint's
1262 while-stepping STEP_COUNT, in addition to checking if the
1263 collect/teval actions parse and make sense in the
1264 tracepoint's context. */
1265 for (c = commands; c; c = c->next)
1266 {
1267 if (c->control_type == while_stepping_control)
1268 {
1269 if (b->type == bp_fast_tracepoint)
1270 error (_("The 'while-stepping' command "
1271 "cannot be used for fast tracepoint"));
1272 else if (b->type == bp_static_tracepoint)
1273 error (_("The 'while-stepping' command "
1274 "cannot be used for static tracepoint"));
1275
1276 if (while_stepping)
1277 error (_("The 'while-stepping' command "
1278 "can be used only once"));
1279 else
1280 while_stepping = c;
1281 }
1282
1283 validate_actionline (c->line, b);
1284 }
1285 if (while_stepping)
1286 {
1287 struct command_line *c2;
1288
1289 gdb_assert (while_stepping->body_list_1 == nullptr);
1290 c2 = while_stepping->body_list_0.get ();
1291 for (; c2; c2 = c2->next)
1292 {
1293 if (c2->control_type == while_stepping_control)
1294 error (_("The 'while-stepping' command cannot be nested"));
1295 }
1296 }
1297 }
1298 else
1299 {
1300 check_no_tracepoint_commands (commands);
1301 }
1302 }
1303
1304 /* Return a vector of all the static tracepoints set at ADDR. The
1305 caller is responsible for releasing the vector. */
1306
1307 std::vector<breakpoint *>
1308 static_tracepoints_here (CORE_ADDR addr)
1309 {
1310 std::vector<breakpoint *> found;
1311
1312 for (breakpoint *b : all_breakpoints ())
1313 if (b->type == bp_static_tracepoint)
1314 {
1315 for (bp_location *loc : b->locations ())
1316 if (loc->address == addr)
1317 found.push_back (b);
1318 }
1319
1320 return found;
1321 }
1322
1323 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1324 validate that only allowed commands are included. */
1325
1326 void
1327 breakpoint_set_commands (struct breakpoint *b,
1328 counted_command_line &&commands)
1329 {
1330 validate_commands_for_breakpoint (b, commands.get ());
1331
1332 b->commands = std::move (commands);
1333 gdb::observers::breakpoint_modified.notify (b);
1334 }
1335
1336 /* Set the internal `silent' flag on the breakpoint. Note that this
1337 is not the same as the "silent" that may appear in the breakpoint's
1338 commands. */
1339
1340 void
1341 breakpoint_set_silent (struct breakpoint *b, int silent)
1342 {
1343 int old_silent = b->silent;
1344
1345 b->silent = silent;
1346 if (old_silent != silent)
1347 gdb::observers::breakpoint_modified.notify (b);
1348 }
1349
1350 /* Set the thread for this breakpoint. If THREAD is -1, make the
1351 breakpoint work for any thread. */
1352
1353 void
1354 breakpoint_set_thread (struct breakpoint *b, int thread)
1355 {
1356 int old_thread = b->thread;
1357
1358 b->thread = thread;
1359 if (old_thread != thread)
1360 gdb::observers::breakpoint_modified.notify (b);
1361 }
1362
1363 /* Set the task for this breakpoint. If TASK is 0, make the
1364 breakpoint work for any task. */
1365
1366 void
1367 breakpoint_set_task (struct breakpoint *b, int task)
1368 {
1369 int old_task = b->task;
1370
1371 b->task = task;
1372 if (old_task != task)
1373 gdb::observers::breakpoint_modified.notify (b);
1374 }
1375
1376 static void
1377 commands_command_1 (const char *arg, int from_tty,
1378 struct command_line *control)
1379 {
1380 counted_command_line cmd;
1381 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1382 NULL after the call to read_command_lines if the user provides an empty
1383 list of command by just typing "end". */
1384 bool cmd_read = false;
1385
1386 std::string new_arg;
1387
1388 if (arg == NULL || !*arg)
1389 {
1390 /* Argument not explicitly given. Synthesize it. */
1391 if (breakpoint_count - prev_breakpoint_count > 1)
1392 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1393 breakpoint_count);
1394 else if (breakpoint_count > 0)
1395 new_arg = string_printf ("%d", breakpoint_count);
1396 }
1397 else
1398 {
1399 /* Create a copy of ARG. This is needed because the "commands"
1400 command may be coming from a script. In that case, the read
1401 line buffer is going to be overwritten in the lambda of
1402 'map_breakpoint_numbers' below when reading the next line
1403 before we are are done parsing the breakpoint numbers. */
1404 new_arg = arg;
1405 }
1406 arg = new_arg.c_str ();
1407
1408 map_breakpoint_numbers
1409 (arg, [&] (breakpoint *b)
1410 {
1411 if (!cmd_read)
1412 {
1413 gdb_assert (cmd == NULL);
1414 if (control != NULL)
1415 cmd = control->body_list_0;
1416 else
1417 {
1418 std::string str
1419 = string_printf (_("Type commands for breakpoint(s) "
1420 "%s, one per line."),
1421 arg);
1422
1423 auto do_validate = [=] (const char *line)
1424 {
1425 validate_actionline (line, b);
1426 };
1427 gdb::function_view<void (const char *)> validator;
1428 if (is_tracepoint (b))
1429 validator = do_validate;
1430
1431 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1432 }
1433 cmd_read = true;
1434 }
1435
1436 /* If a breakpoint was on the list more than once, we don't need to
1437 do anything. */
1438 if (b->commands != cmd)
1439 {
1440 validate_commands_for_breakpoint (b, cmd.get ());
1441 b->commands = cmd;
1442 gdb::observers::breakpoint_modified.notify (b);
1443 }
1444 });
1445 }
1446
1447 static void
1448 commands_command (const char *arg, int from_tty)
1449 {
1450 commands_command_1 (arg, from_tty, NULL);
1451 }
1452
1453 /* Like commands_command, but instead of reading the commands from
1454 input stream, takes them from an already parsed command structure.
1455
1456 This is used by cli-script.c to DTRT with breakpoint commands
1457 that are part of if and while bodies. */
1458 enum command_control_type
1459 commands_from_control_command (const char *arg, struct command_line *cmd)
1460 {
1461 commands_command_1 (arg, 0, cmd);
1462 return simple_control;
1463 }
1464
1465 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1466
1467 static int
1468 bp_location_has_shadow (struct bp_location *bl)
1469 {
1470 if (bl->loc_type != bp_loc_software_breakpoint)
1471 return 0;
1472 if (!bl->inserted)
1473 return 0;
1474 if (bl->target_info.shadow_len == 0)
1475 /* BL isn't valid, or doesn't shadow memory. */
1476 return 0;
1477 return 1;
1478 }
1479
1480 /* Update BUF, which is LEN bytes read from the target address
1481 MEMADDR, by replacing a memory breakpoint with its shadowed
1482 contents.
1483
1484 If READBUF is not NULL, this buffer must not overlap with the of
1485 the breakpoint location's shadow_contents buffer. Otherwise, a
1486 failed assertion internal error will be raised. */
1487
1488 static void
1489 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1490 const gdb_byte *writebuf_org,
1491 ULONGEST memaddr, LONGEST len,
1492 struct bp_target_info *target_info,
1493 struct gdbarch *gdbarch)
1494 {
1495 /* Now do full processing of the found relevant range of elements. */
1496 CORE_ADDR bp_addr = 0;
1497 int bp_size = 0;
1498 int bptoffset = 0;
1499
1500 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1501 current_program_space->aspace, 0))
1502 {
1503 /* The breakpoint is inserted in a different address space. */
1504 return;
1505 }
1506
1507 /* Addresses and length of the part of the breakpoint that
1508 we need to copy. */
1509 bp_addr = target_info->placed_address;
1510 bp_size = target_info->shadow_len;
1511
1512 if (bp_addr + bp_size <= memaddr)
1513 {
1514 /* The breakpoint is entirely before the chunk of memory we are
1515 reading. */
1516 return;
1517 }
1518
1519 if (bp_addr >= memaddr + len)
1520 {
1521 /* The breakpoint is entirely after the chunk of memory we are
1522 reading. */
1523 return;
1524 }
1525
1526 /* Offset within shadow_contents. */
1527 if (bp_addr < memaddr)
1528 {
1529 /* Only copy the second part of the breakpoint. */
1530 bp_size -= memaddr - bp_addr;
1531 bptoffset = memaddr - bp_addr;
1532 bp_addr = memaddr;
1533 }
1534
1535 if (bp_addr + bp_size > memaddr + len)
1536 {
1537 /* Only copy the first part of the breakpoint. */
1538 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1539 }
1540
1541 if (readbuf != NULL)
1542 {
1543 /* Verify that the readbuf buffer does not overlap with the
1544 shadow_contents buffer. */
1545 gdb_assert (target_info->shadow_contents >= readbuf + len
1546 || readbuf >= (target_info->shadow_contents
1547 + target_info->shadow_len));
1548
1549 /* Update the read buffer with this inserted breakpoint's
1550 shadow. */
1551 memcpy (readbuf + bp_addr - memaddr,
1552 target_info->shadow_contents + bptoffset, bp_size);
1553 }
1554 else
1555 {
1556 const unsigned char *bp;
1557 CORE_ADDR addr = target_info->reqstd_address;
1558 int placed_size;
1559
1560 /* Update the shadow with what we want to write to memory. */
1561 memcpy (target_info->shadow_contents + bptoffset,
1562 writebuf_org + bp_addr - memaddr, bp_size);
1563
1564 /* Determine appropriate breakpoint contents and size for this
1565 address. */
1566 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1567
1568 /* Update the final write buffer with this inserted
1569 breakpoint's INSN. */
1570 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1571 }
1572 }
1573
1574 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1575 by replacing any memory breakpoints with their shadowed contents.
1576
1577 If READBUF is not NULL, this buffer must not overlap with any of
1578 the breakpoint location's shadow_contents buffers. Otherwise,
1579 a failed assertion internal error will be raised.
1580
1581 The range of shadowed area by each bp_location is:
1582 bl->address - bp_locations_placed_address_before_address_max
1583 up to bl->address + bp_locations_shadow_len_after_address_max
1584 The range we were requested to resolve shadows for is:
1585 memaddr ... memaddr + len
1586 Thus the safe cutoff boundaries for performance optimization are
1587 memaddr + len <= (bl->address
1588 - bp_locations_placed_address_before_address_max)
1589 and:
1590 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1591
1592 void
1593 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1594 const gdb_byte *writebuf_org,
1595 ULONGEST memaddr, LONGEST len)
1596 {
1597 /* Left boundary, right boundary and median element of our binary
1598 search. */
1599 unsigned bc_l, bc_r, bc;
1600
1601 /* Find BC_L which is a leftmost element which may affect BUF
1602 content. It is safe to report lower value but a failure to
1603 report higher one. */
1604
1605 bc_l = 0;
1606 bc_r = bp_locations.size ();
1607 while (bc_l + 1 < bc_r)
1608 {
1609 struct bp_location *bl;
1610
1611 bc = (bc_l + bc_r) / 2;
1612 bl = bp_locations[bc];
1613
1614 /* Check first BL->ADDRESS will not overflow due to the added
1615 constant. Then advance the left boundary only if we are sure
1616 the BC element can in no way affect the BUF content (MEMADDR
1617 to MEMADDR + LEN range).
1618
1619 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1620 offset so that we cannot miss a breakpoint with its shadow
1621 range tail still reaching MEMADDR. */
1622
1623 if ((bl->address + bp_locations_shadow_len_after_address_max
1624 >= bl->address)
1625 && (bl->address + bp_locations_shadow_len_after_address_max
1626 <= memaddr))
1627 bc_l = bc;
1628 else
1629 bc_r = bc;
1630 }
1631
1632 /* Due to the binary search above, we need to make sure we pick the
1633 first location that's at BC_L's address. E.g., if there are
1634 multiple locations at the same address, BC_L may end up pointing
1635 at a duplicate location, and miss the "master"/"inserted"
1636 location. Say, given locations L1, L2 and L3 at addresses A and
1637 B:
1638
1639 L1@A, L2@A, L3@B, ...
1640
1641 BC_L could end up pointing at location L2, while the "master"
1642 location could be L1. Since the `loc->inserted' flag is only set
1643 on "master" locations, we'd forget to restore the shadow of L1
1644 and L2. */
1645 while (bc_l > 0
1646 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1647 bc_l--;
1648
1649 /* Now do full processing of the found relevant range of elements. */
1650
1651 for (bc = bc_l; bc < bp_locations.size (); bc++)
1652 {
1653 struct bp_location *bl = bp_locations[bc];
1654
1655 /* bp_location array has BL->OWNER always non-NULL. */
1656 if (bl->owner->type == bp_none)
1657 warning (_("reading through apparently deleted breakpoint #%d?"),
1658 bl->owner->number);
1659
1660 /* Performance optimization: any further element can no longer affect BUF
1661 content. */
1662
1663 if (bl->address >= bp_locations_placed_address_before_address_max
1664 && (memaddr + len
1665 <= (bl->address
1666 - bp_locations_placed_address_before_address_max)))
1667 break;
1668
1669 if (!bp_location_has_shadow (bl))
1670 continue;
1671
1672 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1673 memaddr, len, &bl->target_info, bl->gdbarch);
1674 }
1675 }
1676
1677 /* See breakpoint.h. */
1678
1679 bool
1680 is_breakpoint (const struct breakpoint *bpt)
1681 {
1682 return (bpt->type == bp_breakpoint
1683 || bpt->type == bp_hardware_breakpoint
1684 || bpt->type == bp_dprintf);
1685 }
1686
1687 /* Return true if BPT is of any hardware watchpoint kind. */
1688
1689 static bool
1690 is_hardware_watchpoint (const struct breakpoint *bpt)
1691 {
1692 return (bpt->type == bp_hardware_watchpoint
1693 || bpt->type == bp_read_watchpoint
1694 || bpt->type == bp_access_watchpoint);
1695 }
1696
1697 /* See breakpoint.h. */
1698
1699 bool
1700 is_watchpoint (const struct breakpoint *bpt)
1701 {
1702 return (is_hardware_watchpoint (bpt)
1703 || bpt->type == bp_watchpoint);
1704 }
1705
1706 /* Returns true if the current thread and its running state are safe
1707 to evaluate or update watchpoint B. Watchpoints on local
1708 expressions need to be evaluated in the context of the thread that
1709 was current when the watchpoint was created, and, that thread needs
1710 to be stopped to be able to select the correct frame context.
1711 Watchpoints on global expressions can be evaluated on any thread,
1712 and in any state. It is presently left to the target allowing
1713 memory accesses when threads are running. */
1714
1715 static int
1716 watchpoint_in_thread_scope (struct watchpoint *b)
1717 {
1718 return (b->pspace == current_program_space
1719 && (b->watchpoint_thread == null_ptid
1720 || (inferior_ptid == b->watchpoint_thread
1721 && !inferior_thread ()->executing ())));
1722 }
1723
1724 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1725 associated bp_watchpoint_scope breakpoint. */
1726
1727 static void
1728 watchpoint_del_at_next_stop (struct watchpoint *w)
1729 {
1730 if (w->related_breakpoint != w)
1731 {
1732 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1733 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1734 w->related_breakpoint->disposition = disp_del_at_next_stop;
1735 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1736 w->related_breakpoint = w;
1737 }
1738 w->disposition = disp_del_at_next_stop;
1739 }
1740
1741 /* Extract a bitfield value from value VAL using the bit parameters contained in
1742 watchpoint W. */
1743
1744 static struct value *
1745 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1746 {
1747 struct value *bit_val;
1748
1749 if (val == NULL)
1750 return NULL;
1751
1752 bit_val = allocate_value (value_type (val));
1753
1754 unpack_value_bitfield (bit_val,
1755 w->val_bitpos,
1756 w->val_bitsize,
1757 value_contents_for_printing (val).data (),
1758 value_offset (val),
1759 val);
1760
1761 return bit_val;
1762 }
1763
1764 /* Allocate a dummy location and add it to B, which must be a software
1765 watchpoint. This is required because even if a software watchpoint
1766 is not watching any memory, bpstat_stop_status requires a location
1767 to be able to report stops. */
1768
1769 static void
1770 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1771 struct program_space *pspace)
1772 {
1773 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1774
1775 b->loc = allocate_bp_location (b);
1776 b->loc->pspace = pspace;
1777 b->loc->address = -1;
1778 b->loc->length = -1;
1779 }
1780
1781 /* Returns true if B is a software watchpoint that is not watching any
1782 memory (e.g., "watch $pc"). */
1783
1784 static bool
1785 is_no_memory_software_watchpoint (struct breakpoint *b)
1786 {
1787 return (b->type == bp_watchpoint
1788 && b->loc != NULL
1789 && b->loc->next == NULL
1790 && b->loc->address == -1
1791 && b->loc->length == -1);
1792 }
1793
1794 /* Assuming that B is a watchpoint:
1795 - Reparse watchpoint expression, if REPARSE is non-zero
1796 - Evaluate expression and store the result in B->val
1797 - Evaluate the condition if there is one, and store the result
1798 in b->loc->cond.
1799 - Update the list of values that must be watched in B->loc.
1800
1801 If the watchpoint disposition is disp_del_at_next_stop, then do
1802 nothing. If this is local watchpoint that is out of scope, delete
1803 it.
1804
1805 Even with `set breakpoint always-inserted on' the watchpoints are
1806 removed + inserted on each stop here. Normal breakpoints must
1807 never be removed because they might be missed by a running thread
1808 when debugging in non-stop mode. On the other hand, hardware
1809 watchpoints (is_hardware_watchpoint; processed here) are specific
1810 to each LWP since they are stored in each LWP's hardware debug
1811 registers. Therefore, such LWP must be stopped first in order to
1812 be able to modify its hardware watchpoints.
1813
1814 Hardware watchpoints must be reset exactly once after being
1815 presented to the user. It cannot be done sooner, because it would
1816 reset the data used to present the watchpoint hit to the user. And
1817 it must not be done later because it could display the same single
1818 watchpoint hit during multiple GDB stops. Note that the latter is
1819 relevant only to the hardware watchpoint types bp_read_watchpoint
1820 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1821 not user-visible - its hit is suppressed if the memory content has
1822 not changed.
1823
1824 The following constraints influence the location where we can reset
1825 hardware watchpoints:
1826
1827 * target_stopped_by_watchpoint and target_stopped_data_address are
1828 called several times when GDB stops.
1829
1830 [linux]
1831 * Multiple hardware watchpoints can be hit at the same time,
1832 causing GDB to stop. GDB only presents one hardware watchpoint
1833 hit at a time as the reason for stopping, and all the other hits
1834 are presented later, one after the other, each time the user
1835 requests the execution to be resumed. Execution is not resumed
1836 for the threads still having pending hit event stored in
1837 LWP_INFO->STATUS. While the watchpoint is already removed from
1838 the inferior on the first stop the thread hit event is kept being
1839 reported from its cached value by linux_nat_stopped_data_address
1840 until the real thread resume happens after the watchpoint gets
1841 presented and thus its LWP_INFO->STATUS gets reset.
1842
1843 Therefore the hardware watchpoint hit can get safely reset on the
1844 watchpoint removal from inferior. */
1845
1846 static void
1847 update_watchpoint (struct watchpoint *b, int reparse)
1848 {
1849 int within_current_scope;
1850 struct frame_id saved_frame_id;
1851 int frame_saved;
1852
1853 /* If this is a local watchpoint, we only want to check if the
1854 watchpoint frame is in scope if the current thread is the thread
1855 that was used to create the watchpoint. */
1856 if (!watchpoint_in_thread_scope (b))
1857 return;
1858
1859 if (b->disposition == disp_del_at_next_stop)
1860 return;
1861
1862 frame_saved = 0;
1863
1864 /* Determine if the watchpoint is within scope. */
1865 if (b->exp_valid_block == NULL)
1866 within_current_scope = 1;
1867 else
1868 {
1869 struct frame_info *fi = get_current_frame ();
1870 struct gdbarch *frame_arch = get_frame_arch (fi);
1871 CORE_ADDR frame_pc = get_frame_pc (fi);
1872
1873 /* If we're at a point where the stack has been destroyed
1874 (e.g. in a function epilogue), unwinding may not work
1875 properly. Do not attempt to recreate locations at this
1876 point. See similar comments in watchpoint_check. */
1877 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1878 return;
1879
1880 /* Save the current frame's ID so we can restore it after
1881 evaluating the watchpoint expression on its own frame. */
1882 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1883 took a frame parameter, so that we didn't have to change the
1884 selected frame. */
1885 frame_saved = 1;
1886 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1887
1888 fi = frame_find_by_id (b->watchpoint_frame);
1889 within_current_scope = (fi != NULL);
1890 if (within_current_scope)
1891 select_frame (fi);
1892 }
1893
1894 /* We don't free locations. They are stored in the bp_location array
1895 and update_global_location_list will eventually delete them and
1896 remove breakpoints if needed. */
1897 b->loc = NULL;
1898
1899 if (within_current_scope && reparse)
1900 {
1901 const char *s;
1902
1903 b->exp.reset ();
1904 s = (b->exp_string_reparse
1905 ? b->exp_string_reparse.get ()
1906 : b->exp_string.get ());
1907 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1908 /* If the meaning of expression itself changed, the old value is
1909 no longer relevant. We don't want to report a watchpoint hit
1910 to the user when the old value and the new value may actually
1911 be completely different objects. */
1912 b->val = NULL;
1913 b->val_valid = false;
1914
1915 /* Note that unlike with breakpoints, the watchpoint's condition
1916 expression is stored in the breakpoint object, not in the
1917 locations (re)created below. */
1918 if (b->cond_string != NULL)
1919 {
1920 b->cond_exp.reset ();
1921
1922 s = b->cond_string.get ();
1923 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1924 }
1925 }
1926
1927 /* If we failed to parse the expression, for example because
1928 it refers to a global variable in a not-yet-loaded shared library,
1929 don't try to insert watchpoint. We don't automatically delete
1930 such watchpoint, though, since failure to parse expression
1931 is different from out-of-scope watchpoint. */
1932 if (!target_has_execution ())
1933 {
1934 /* Without execution, memory can't change. No use to try and
1935 set watchpoint locations. The watchpoint will be reset when
1936 the target gains execution, through breakpoint_re_set. */
1937 if (!can_use_hw_watchpoints)
1938 {
1939 if (b->ops->works_in_software_mode (b))
1940 b->type = bp_watchpoint;
1941 else
1942 error (_("Can't set read/access watchpoint when "
1943 "hardware watchpoints are disabled."));
1944 }
1945 }
1946 else if (within_current_scope && b->exp)
1947 {
1948 std::vector<value_ref_ptr> val_chain;
1949 struct value *v, *result;
1950 struct program_space *frame_pspace;
1951
1952 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
1953 &val_chain, false);
1954
1955 /* Avoid setting b->val if it's already set. The meaning of
1956 b->val is 'the last value' user saw, and we should update
1957 it only if we reported that last value to user. As it
1958 happens, the code that reports it updates b->val directly.
1959 We don't keep track of the memory value for masked
1960 watchpoints. */
1961 if (!b->val_valid && !is_masked_watchpoint (b))
1962 {
1963 if (b->val_bitsize != 0)
1964 v = extract_bitfield_from_watchpoint_value (b, v);
1965 b->val = release_value (v);
1966 b->val_valid = true;
1967 }
1968
1969 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1970
1971 /* Look at each value on the value chain. */
1972 gdb_assert (!val_chain.empty ());
1973 for (const value_ref_ptr &iter : val_chain)
1974 {
1975 v = iter.get ();
1976
1977 /* If it's a memory location, and GDB actually needed
1978 its contents to evaluate the expression, then we
1979 must watch it. If the first value returned is
1980 still lazy, that means an error occurred reading it;
1981 watch it anyway in case it becomes readable. */
1982 if (VALUE_LVAL (v) == lval_memory
1983 && (v == val_chain[0] || ! value_lazy (v)))
1984 {
1985 struct type *vtype = check_typedef (value_type (v));
1986
1987 /* We only watch structs and arrays if user asked
1988 for it explicitly, never if they just happen to
1989 appear in the middle of some value chain. */
1990 if (v == result
1991 || (vtype->code () != TYPE_CODE_STRUCT
1992 && vtype->code () != TYPE_CODE_ARRAY))
1993 {
1994 CORE_ADDR addr;
1995 enum target_hw_bp_type type;
1996 struct bp_location *loc, **tmp;
1997 int bitpos = 0, bitsize = 0;
1998
1999 if (value_bitsize (v) != 0)
2000 {
2001 /* Extract the bit parameters out from the bitfield
2002 sub-expression. */
2003 bitpos = value_bitpos (v);
2004 bitsize = value_bitsize (v);
2005 }
2006 else if (v == result && b->val_bitsize != 0)
2007 {
2008 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2009 lvalue whose bit parameters are saved in the fields
2010 VAL_BITPOS and VAL_BITSIZE. */
2011 bitpos = b->val_bitpos;
2012 bitsize = b->val_bitsize;
2013 }
2014
2015 addr = value_address (v);
2016 if (bitsize != 0)
2017 {
2018 /* Skip the bytes that don't contain the bitfield. */
2019 addr += bitpos / 8;
2020 }
2021
2022 type = hw_write;
2023 if (b->type == bp_read_watchpoint)
2024 type = hw_read;
2025 else if (b->type == bp_access_watchpoint)
2026 type = hw_access;
2027
2028 loc = allocate_bp_location (b);
2029 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2030 ;
2031 *tmp = loc;
2032 loc->gdbarch = value_type (v)->arch ();
2033
2034 loc->pspace = frame_pspace;
2035 loc->address = address_significant (loc->gdbarch, addr);
2036
2037 if (bitsize != 0)
2038 {
2039 /* Just cover the bytes that make up the bitfield. */
2040 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2041 }
2042 else
2043 loc->length = TYPE_LENGTH (value_type (v));
2044
2045 loc->watchpoint_type = type;
2046 }
2047 }
2048 }
2049
2050 /* Change the type of breakpoint between hardware assisted or
2051 an ordinary watchpoint depending on the hardware support
2052 and free hardware slots. REPARSE is set when the inferior
2053 is started. */
2054 if (reparse)
2055 {
2056 int reg_cnt;
2057 enum bp_loc_type loc_type;
2058
2059 reg_cnt = can_use_hardware_watchpoint (val_chain);
2060
2061 if (reg_cnt)
2062 {
2063 int i, target_resources_ok, other_type_used;
2064 enum bptype type;
2065
2066 /* Use an exact watchpoint when there's only one memory region to be
2067 watched, and only one debug register is needed to watch it. */
2068 b->exact = target_exact_watchpoints && reg_cnt == 1;
2069
2070 /* We need to determine how many resources are already
2071 used for all other hardware watchpoints plus this one
2072 to see if we still have enough resources to also fit
2073 this watchpoint in as well. */
2074
2075 /* If this is a software watchpoint, we try to turn it
2076 to a hardware one -- count resources as if B was of
2077 hardware watchpoint type. */
2078 type = b->type;
2079 if (type == bp_watchpoint)
2080 type = bp_hardware_watchpoint;
2081
2082 /* This watchpoint may or may not have been placed on
2083 the list yet at this point (it won't be in the list
2084 if we're trying to create it for the first time,
2085 through watch_command), so always account for it
2086 manually. */
2087
2088 /* Count resources used by all watchpoints except B. */
2089 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2090
2091 /* Add in the resources needed for B. */
2092 i += hw_watchpoint_use_count (b);
2093
2094 target_resources_ok
2095 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2096 if (target_resources_ok <= 0)
2097 {
2098 int sw_mode = b->ops->works_in_software_mode (b);
2099
2100 if (target_resources_ok == 0 && !sw_mode)
2101 error (_("Target does not support this type of "
2102 "hardware watchpoint."));
2103 else if (target_resources_ok < 0 && !sw_mode)
2104 error (_("There are not enough available hardware "
2105 "resources for this watchpoint."));
2106
2107 /* Downgrade to software watchpoint. */
2108 b->type = bp_watchpoint;
2109 }
2110 else
2111 {
2112 /* If this was a software watchpoint, we've just
2113 found we have enough resources to turn it to a
2114 hardware watchpoint. Otherwise, this is a
2115 nop. */
2116 b->type = type;
2117 }
2118 }
2119 else if (!b->ops->works_in_software_mode (b))
2120 {
2121 if (!can_use_hw_watchpoints)
2122 error (_("Can't set read/access watchpoint when "
2123 "hardware watchpoints are disabled."));
2124 else
2125 error (_("Expression cannot be implemented with "
2126 "read/access watchpoint."));
2127 }
2128 else
2129 b->type = bp_watchpoint;
2130
2131 loc_type = (b->type == bp_watchpoint? bp_loc_other
2132 : bp_loc_hardware_watchpoint);
2133 for (bp_location *bl : b->locations ())
2134 bl->loc_type = loc_type;
2135 }
2136
2137 /* If a software watchpoint is not watching any memory, then the
2138 above left it without any location set up. But,
2139 bpstat_stop_status requires a location to be able to report
2140 stops, so make sure there's at least a dummy one. */
2141 if (b->type == bp_watchpoint && b->loc == NULL)
2142 software_watchpoint_add_no_memory_location (b, frame_pspace);
2143 }
2144 else if (!within_current_scope)
2145 {
2146 gdb_printf (_("\
2147 Watchpoint %d deleted because the program has left the block\n\
2148 in which its expression is valid.\n"),
2149 b->number);
2150 watchpoint_del_at_next_stop (b);
2151 }
2152
2153 /* Restore the selected frame. */
2154 if (frame_saved)
2155 select_frame (frame_find_by_id (saved_frame_id));
2156 }
2157
2158
2159 /* Returns 1 iff breakpoint location should be
2160 inserted in the inferior. We don't differentiate the type of BL's owner
2161 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2162 breakpoint_ops is not defined, because in insert_bp_location,
2163 tracepoint's insert_location will not be called. */
2164 static int
2165 should_be_inserted (struct bp_location *bl)
2166 {
2167 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2168 return 0;
2169
2170 if (bl->owner->disposition == disp_del_at_next_stop)
2171 return 0;
2172
2173 if (!bl->enabled || bl->disabled_by_cond
2174 || bl->shlib_disabled || bl->duplicate)
2175 return 0;
2176
2177 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2178 return 0;
2179
2180 /* This is set for example, when we're attached to the parent of a
2181 vfork, and have detached from the child. The child is running
2182 free, and we expect it to do an exec or exit, at which point the
2183 OS makes the parent schedulable again (and the target reports
2184 that the vfork is done). Until the child is done with the shared
2185 memory region, do not insert breakpoints in the parent, otherwise
2186 the child could still trip on the parent's breakpoints. Since
2187 the parent is blocked anyway, it won't miss any breakpoint. */
2188 if (bl->pspace->breakpoints_not_allowed)
2189 return 0;
2190
2191 /* Don't insert a breakpoint if we're trying to step past its
2192 location, except if the breakpoint is a single-step breakpoint,
2193 and the breakpoint's thread is the thread which is stepping past
2194 a breakpoint. */
2195 if ((bl->loc_type == bp_loc_software_breakpoint
2196 || bl->loc_type == bp_loc_hardware_breakpoint)
2197 && stepping_past_instruction_at (bl->pspace->aspace,
2198 bl->address)
2199 /* The single-step breakpoint may be inserted at the location
2200 we're trying to step if the instruction branches to itself.
2201 However, the instruction won't be executed at all and it may
2202 break the semantics of the instruction, for example, the
2203 instruction is a conditional branch or updates some flags.
2204 We can't fix it unless GDB is able to emulate the instruction
2205 or switch to displaced stepping. */
2206 && !(bl->owner->type == bp_single_step
2207 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2208 {
2209 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2210 paddress (bl->gdbarch, bl->address));
2211 return 0;
2212 }
2213
2214 /* Don't insert watchpoints if we're trying to step past the
2215 instruction that triggered one. */
2216 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2217 && stepping_past_nonsteppable_watchpoint ())
2218 {
2219 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2220 "skipping watchpoint at %s:%d",
2221 paddress (bl->gdbarch, bl->address), bl->length);
2222 return 0;
2223 }
2224
2225 return 1;
2226 }
2227
2228 /* Same as should_be_inserted but does the check assuming
2229 that the location is not duplicated. */
2230
2231 static int
2232 unduplicated_should_be_inserted (struct bp_location *bl)
2233 {
2234 int result;
2235 const int save_duplicate = bl->duplicate;
2236
2237 bl->duplicate = 0;
2238 result = should_be_inserted (bl);
2239 bl->duplicate = save_duplicate;
2240 return result;
2241 }
2242
2243 /* Parses a conditional described by an expression COND into an
2244 agent expression bytecode suitable for evaluation
2245 by the bytecode interpreter. Return NULL if there was
2246 any error during parsing. */
2247
2248 static agent_expr_up
2249 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2250 {
2251 if (cond == NULL)
2252 return NULL;
2253
2254 agent_expr_up aexpr;
2255
2256 /* We don't want to stop processing, so catch any errors
2257 that may show up. */
2258 try
2259 {
2260 aexpr = gen_eval_for_expr (scope, cond);
2261 }
2262
2263 catch (const gdb_exception_error &ex)
2264 {
2265 /* If we got here, it means the condition could not be parsed to a valid
2266 bytecode expression and thus can't be evaluated on the target's side.
2267 It's no use iterating through the conditions. */
2268 }
2269
2270 /* We have a valid agent expression. */
2271 return aexpr;
2272 }
2273
2274 /* Based on location BL, create a list of breakpoint conditions to be
2275 passed on to the target. If we have duplicated locations with different
2276 conditions, we will add such conditions to the list. The idea is that the
2277 target will evaluate the list of conditions and will only notify GDB when
2278 one of them is true. */
2279
2280 static void
2281 build_target_condition_list (struct bp_location *bl)
2282 {
2283 int null_condition_or_parse_error = 0;
2284 int modified = bl->needs_update;
2285
2286 /* Release conditions left over from a previous insert. */
2287 bl->target_info.conditions.clear ();
2288
2289 /* This is only meaningful if the target is
2290 evaluating conditions and if the user has
2291 opted for condition evaluation on the target's
2292 side. */
2293 if (gdb_evaluates_breakpoint_condition_p ()
2294 || !target_supports_evaluation_of_breakpoint_conditions ())
2295 return;
2296
2297 auto loc_range = all_bp_locations_at_addr (bl->address);
2298
2299 /* Do a first pass to check for locations with no assigned
2300 conditions or conditions that fail to parse to a valid agent
2301 expression bytecode. If any of these happen, then it's no use to
2302 send conditions to the target since this location will always
2303 trigger and generate a response back to GDB. Note we consider
2304 all locations at the same address irrespective of type, i.e.,
2305 even if the locations aren't considered duplicates (e.g.,
2306 software breakpoint and hardware breakpoint at the same
2307 address). */
2308 for (bp_location *loc : loc_range)
2309 {
2310 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2311 {
2312 if (modified)
2313 {
2314 /* Re-parse the conditions since something changed. In that
2315 case we already freed the condition bytecodes (see
2316 force_breakpoint_reinsertion). We just
2317 need to parse the condition to bytecodes again. */
2318 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2319 loc->cond.get ());
2320 }
2321
2322 /* If we have a NULL bytecode expression, it means something
2323 went wrong or we have a null condition expression. */
2324 if (!loc->cond_bytecode)
2325 {
2326 null_condition_or_parse_error = 1;
2327 break;
2328 }
2329 }
2330 }
2331
2332 /* If any of these happened, it means we will have to evaluate the conditions
2333 for the location's address on gdb's side. It is no use keeping bytecodes
2334 for all the other duplicate locations, thus we free all of them here.
2335
2336 This is so we have a finer control over which locations' conditions are
2337 being evaluated by GDB or the remote stub. */
2338 if (null_condition_or_parse_error)
2339 {
2340 for (bp_location *loc : loc_range)
2341 {
2342 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2343 {
2344 /* Only go as far as the first NULL bytecode is
2345 located. */
2346 if (!loc->cond_bytecode)
2347 return;
2348
2349 loc->cond_bytecode.reset ();
2350 }
2351 }
2352 }
2353
2354 /* No NULL conditions or failed bytecode generation. Build a
2355 condition list for this location's address. If we have software
2356 and hardware locations at the same address, they aren't
2357 considered duplicates, but we still marge all the conditions
2358 anyway, as it's simpler, and doesn't really make a practical
2359 difference. */
2360 for (bp_location *loc : loc_range)
2361 if (loc->cond
2362 && is_breakpoint (loc->owner)
2363 && loc->pspace->num == bl->pspace->num
2364 && loc->owner->enable_state == bp_enabled
2365 && loc->enabled
2366 && !loc->disabled_by_cond)
2367 {
2368 /* Add the condition to the vector. This will be used later
2369 to send the conditions to the target. */
2370 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2371 }
2372
2373 return;
2374 }
2375
2376 /* Parses a command described by string CMD into an agent expression
2377 bytecode suitable for evaluation by the bytecode interpreter.
2378 Return NULL if there was any error during parsing. */
2379
2380 static agent_expr_up
2381 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2382 {
2383 const char *cmdrest;
2384 const char *format_start, *format_end;
2385 struct gdbarch *gdbarch = get_current_arch ();
2386
2387 if (cmd == NULL)
2388 return NULL;
2389
2390 cmdrest = cmd;
2391
2392 if (*cmdrest == ',')
2393 ++cmdrest;
2394 cmdrest = skip_spaces (cmdrest);
2395
2396 if (*cmdrest++ != '"')
2397 error (_("No format string following the location"));
2398
2399 format_start = cmdrest;
2400
2401 format_pieces fpieces (&cmdrest);
2402
2403 format_end = cmdrest;
2404
2405 if (*cmdrest++ != '"')
2406 error (_("Bad format string, non-terminated '\"'."));
2407
2408 cmdrest = skip_spaces (cmdrest);
2409
2410 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2411 error (_("Invalid argument syntax"));
2412
2413 if (*cmdrest == ',')
2414 cmdrest++;
2415 cmdrest = skip_spaces (cmdrest);
2416
2417 /* For each argument, make an expression. */
2418
2419 std::vector<struct expression *> argvec;
2420 while (*cmdrest != '\0')
2421 {
2422 const char *cmd1;
2423
2424 cmd1 = cmdrest;
2425 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2426 argvec.push_back (expr.release ());
2427 cmdrest = cmd1;
2428 if (*cmdrest == ',')
2429 ++cmdrest;
2430 }
2431
2432 agent_expr_up aexpr;
2433
2434 /* We don't want to stop processing, so catch any errors
2435 that may show up. */
2436 try
2437 {
2438 aexpr = gen_printf (scope, gdbarch, 0, 0,
2439 format_start, format_end - format_start,
2440 argvec.size (), argvec.data ());
2441 }
2442 catch (const gdb_exception_error &ex)
2443 {
2444 /* If we got here, it means the command could not be parsed to a valid
2445 bytecode expression and thus can't be evaluated on the target's side.
2446 It's no use iterating through the other commands. */
2447 }
2448
2449 /* We have a valid agent expression, return it. */
2450 return aexpr;
2451 }
2452
2453 /* Based on location BL, create a list of breakpoint commands to be
2454 passed on to the target. If we have duplicated locations with
2455 different commands, we will add any such to the list. */
2456
2457 static void
2458 build_target_command_list (struct bp_location *bl)
2459 {
2460 int null_command_or_parse_error = 0;
2461 int modified = bl->needs_update;
2462
2463 /* Clear commands left over from a previous insert. */
2464 bl->target_info.tcommands.clear ();
2465
2466 if (!target_can_run_breakpoint_commands ())
2467 return;
2468
2469 /* For now, limit to agent-style dprintf breakpoints. */
2470 if (dprintf_style != dprintf_style_agent)
2471 return;
2472
2473 auto loc_range = all_bp_locations_at_addr (bl->address);
2474
2475 /* For now, if we have any location at the same address that isn't a
2476 dprintf, don't install the target-side commands, as that would
2477 make the breakpoint not be reported to the core, and we'd lose
2478 control. */
2479 for (bp_location *loc : loc_range)
2480 if (is_breakpoint (loc->owner)
2481 && loc->pspace->num == bl->pspace->num
2482 && loc->owner->type != bp_dprintf)
2483 return;
2484
2485 /* Do a first pass to check for locations with no assigned
2486 conditions or conditions that fail to parse to a valid agent expression
2487 bytecode. If any of these happen, then it's no use to send conditions
2488 to the target since this location will always trigger and generate a
2489 response back to GDB. */
2490 for (bp_location *loc : loc_range)
2491 {
2492 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2493 {
2494 if (modified)
2495 {
2496 /* Re-parse the commands since something changed. In that
2497 case we already freed the command bytecodes (see
2498 force_breakpoint_reinsertion). We just
2499 need to parse the command to bytecodes again. */
2500 loc->cmd_bytecode
2501 = parse_cmd_to_aexpr (bl->address,
2502 loc->owner->extra_string.get ());
2503 }
2504
2505 /* If we have a NULL bytecode expression, it means something
2506 went wrong or we have a null command expression. */
2507 if (!loc->cmd_bytecode)
2508 {
2509 null_command_or_parse_error = 1;
2510 break;
2511 }
2512 }
2513 }
2514
2515 /* If anything failed, then we're not doing target-side commands,
2516 and so clean up. */
2517 if (null_command_or_parse_error)
2518 {
2519 for (bp_location *loc : loc_range)
2520 if (is_breakpoint (loc->owner)
2521 && loc->pspace->num == bl->pspace->num)
2522 {
2523 /* Only go as far as the first NULL bytecode is
2524 located. */
2525 if (loc->cmd_bytecode == NULL)
2526 return;
2527
2528 loc->cmd_bytecode.reset ();
2529 }
2530 }
2531
2532 /* No NULL commands or failed bytecode generation. Build a command
2533 list for all duplicate locations at this location's address.
2534 Note that here we must care for whether the breakpoint location
2535 types are considered duplicates, otherwise, say, if we have a
2536 software and hardware location at the same address, the target
2537 could end up running the commands twice. For the moment, we only
2538 support targets-side commands with dprintf, but it doesn't hurt
2539 to be pedantically correct in case that changes. */
2540 for (bp_location *loc : loc_range)
2541 if (breakpoint_locations_match (bl, loc)
2542 && loc->owner->extra_string
2543 && is_breakpoint (loc->owner)
2544 && loc->pspace->num == bl->pspace->num
2545 && loc->owner->enable_state == bp_enabled
2546 && loc->enabled
2547 && !loc->disabled_by_cond)
2548 {
2549 /* Add the command to the vector. This will be used later
2550 to send the commands to the target. */
2551 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2552 }
2553
2554 bl->target_info.persist = 0;
2555 /* Maybe flag this location as persistent. */
2556 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2557 bl->target_info.persist = 1;
2558 }
2559
2560 /* Return the kind of breakpoint on address *ADDR. Get the kind
2561 of breakpoint according to ADDR except single-step breakpoint.
2562 Get the kind of single-step breakpoint according to the current
2563 registers state. */
2564
2565 static int
2566 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2567 {
2568 if (bl->owner->type == bp_single_step)
2569 {
2570 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2571 struct regcache *regcache;
2572
2573 regcache = get_thread_regcache (thr);
2574
2575 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2576 regcache, addr);
2577 }
2578 else
2579 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2580 }
2581
2582 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2583 location. Any error messages are printed to TMP_ERROR_STREAM; and
2584 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2585 Returns 0 for success, 1 if the bp_location type is not supported or
2586 -1 for failure.
2587
2588 NOTE drow/2003-09-09: This routine could be broken down to an
2589 object-style method for each breakpoint or catchpoint type. */
2590 static int
2591 insert_bp_location (struct bp_location *bl,
2592 struct ui_file *tmp_error_stream,
2593 int *disabled_breaks,
2594 int *hw_breakpoint_error,
2595 int *hw_bp_error_explained_already)
2596 {
2597 gdb_exception bp_excpt;
2598
2599 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2600 return 0;
2601
2602 /* Note we don't initialize bl->target_info, as that wipes out
2603 the breakpoint location's shadow_contents if the breakpoint
2604 is still inserted at that location. This in turn breaks
2605 target_read_memory which depends on these buffers when
2606 a memory read is requested at the breakpoint location:
2607 Once the target_info has been wiped, we fail to see that
2608 we have a breakpoint inserted at that address and thus
2609 read the breakpoint instead of returning the data saved in
2610 the breakpoint location's shadow contents. */
2611 bl->target_info.reqstd_address = bl->address;
2612 bl->target_info.placed_address_space = bl->pspace->aspace;
2613 bl->target_info.length = bl->length;
2614
2615 /* When working with target-side conditions, we must pass all the conditions
2616 for the same breakpoint address down to the target since GDB will not
2617 insert those locations. With a list of breakpoint conditions, the target
2618 can decide when to stop and notify GDB. */
2619
2620 if (is_breakpoint (bl->owner))
2621 {
2622 build_target_condition_list (bl);
2623 build_target_command_list (bl);
2624 /* Reset the modification marker. */
2625 bl->needs_update = 0;
2626 }
2627
2628 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2629 set at a read-only address, then a breakpoint location will have
2630 been changed to hardware breakpoint before we get here. If it is
2631 "off" however, error out before actually trying to insert the
2632 breakpoint, with a nicer error message. */
2633 if (bl->loc_type == bp_loc_software_breakpoint
2634 && !automatic_hardware_breakpoints)
2635 {
2636 mem_region *mr = lookup_mem_region (bl->address);
2637
2638 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2639 {
2640 gdb_printf (tmp_error_stream,
2641 _("Cannot insert breakpoint %d.\n"
2642 "Cannot set software breakpoint "
2643 "at read-only address %s\n"),
2644 bl->owner->number,
2645 paddress (bl->gdbarch, bl->address));
2646 return 1;
2647 }
2648 }
2649
2650 if (bl->loc_type == bp_loc_software_breakpoint
2651 || bl->loc_type == bp_loc_hardware_breakpoint)
2652 {
2653 /* First check to see if we have to handle an overlay. */
2654 if (overlay_debugging == ovly_off
2655 || bl->section == NULL
2656 || !(section_is_overlay (bl->section)))
2657 {
2658 /* No overlay handling: just set the breakpoint. */
2659 try
2660 {
2661 int val;
2662
2663 val = bl->owner->ops->insert_location (bl);
2664 if (val)
2665 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2666 }
2667 catch (gdb_exception &e)
2668 {
2669 bp_excpt = std::move (e);
2670 }
2671 }
2672 else
2673 {
2674 /* This breakpoint is in an overlay section.
2675 Shall we set a breakpoint at the LMA? */
2676 if (!overlay_events_enabled)
2677 {
2678 /* Yes -- overlay event support is not active,
2679 so we must try to set a breakpoint at the LMA.
2680 This will not work for a hardware breakpoint. */
2681 if (bl->loc_type == bp_loc_hardware_breakpoint)
2682 warning (_("hardware breakpoint %d not supported in overlay!"),
2683 bl->owner->number);
2684 else
2685 {
2686 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2687 bl->section);
2688 /* Set a software (trap) breakpoint at the LMA. */
2689 bl->overlay_target_info = bl->target_info;
2690 bl->overlay_target_info.reqstd_address = addr;
2691
2692 /* No overlay handling: just set the breakpoint. */
2693 try
2694 {
2695 int val;
2696
2697 bl->overlay_target_info.kind
2698 = breakpoint_kind (bl, &addr);
2699 bl->overlay_target_info.placed_address = addr;
2700 val = target_insert_breakpoint (bl->gdbarch,
2701 &bl->overlay_target_info);
2702 if (val)
2703 bp_excpt
2704 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2705 }
2706 catch (gdb_exception &e)
2707 {
2708 bp_excpt = std::move (e);
2709 }
2710
2711 if (bp_excpt.reason != 0)
2712 gdb_printf (tmp_error_stream,
2713 "Overlay breakpoint %d "
2714 "failed: in ROM?\n",
2715 bl->owner->number);
2716 }
2717 }
2718 /* Shall we set a breakpoint at the VMA? */
2719 if (section_is_mapped (bl->section))
2720 {
2721 /* Yes. This overlay section is mapped into memory. */
2722 try
2723 {
2724 int val;
2725
2726 val = bl->owner->ops->insert_location (bl);
2727 if (val)
2728 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2729 }
2730 catch (gdb_exception &e)
2731 {
2732 bp_excpt = std::move (e);
2733 }
2734 }
2735 else
2736 {
2737 /* No. This breakpoint will not be inserted.
2738 No error, but do not mark the bp as 'inserted'. */
2739 return 0;
2740 }
2741 }
2742
2743 if (bp_excpt.reason != 0)
2744 {
2745 /* Can't set the breakpoint. */
2746
2747 /* If the target has closed then it will have deleted any
2748 breakpoints inserted within the target inferior, as a result
2749 any further attempts to interact with the breakpoint objects
2750 is not possible. Just rethrow the error. */
2751 if (bp_excpt.error == TARGET_CLOSE_ERROR)
2752 throw bp_excpt;
2753 gdb_assert (bl->owner != nullptr);
2754
2755 /* In some cases, we might not be able to insert a
2756 breakpoint in a shared library that has already been
2757 removed, but we have not yet processed the shlib unload
2758 event. Unfortunately, some targets that implement
2759 breakpoint insertion themselves can't tell why the
2760 breakpoint insertion failed (e.g., the remote target
2761 doesn't define error codes), so we must treat generic
2762 errors as memory errors. */
2763 if (bp_excpt.reason == RETURN_ERROR
2764 && (bp_excpt.error == GENERIC_ERROR
2765 || bp_excpt.error == MEMORY_ERROR)
2766 && bl->loc_type == bp_loc_software_breakpoint
2767 && (solib_name_from_address (bl->pspace, bl->address)
2768 || shared_objfile_contains_address_p (bl->pspace,
2769 bl->address)))
2770 {
2771 /* See also: disable_breakpoints_in_shlibs. */
2772 bl->shlib_disabled = 1;
2773 gdb::observers::breakpoint_modified.notify (bl->owner);
2774 if (!*disabled_breaks)
2775 {
2776 gdb_printf (tmp_error_stream,
2777 "Cannot insert breakpoint %d.\n",
2778 bl->owner->number);
2779 gdb_printf (tmp_error_stream,
2780 "Temporarily disabling shared "
2781 "library breakpoints:\n");
2782 }
2783 *disabled_breaks = 1;
2784 gdb_printf (tmp_error_stream,
2785 "breakpoint #%d\n", bl->owner->number);
2786 return 0;
2787 }
2788 else
2789 {
2790 if (bl->loc_type == bp_loc_hardware_breakpoint)
2791 {
2792 *hw_breakpoint_error = 1;
2793 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2794 gdb_printf (tmp_error_stream,
2795 "Cannot insert hardware breakpoint %d%s",
2796 bl->owner->number,
2797 bp_excpt.message ? ":" : ".\n");
2798 if (bp_excpt.message != NULL)
2799 gdb_printf (tmp_error_stream, "%s.\n",
2800 bp_excpt.what ());
2801 }
2802 else
2803 {
2804 if (bp_excpt.message == NULL)
2805 {
2806 std::string message
2807 = memory_error_message (TARGET_XFER_E_IO,
2808 bl->gdbarch, bl->address);
2809
2810 gdb_printf (tmp_error_stream,
2811 "Cannot insert breakpoint %d.\n"
2812 "%s\n",
2813 bl->owner->number, message.c_str ());
2814 }
2815 else
2816 {
2817 gdb_printf (tmp_error_stream,
2818 "Cannot insert breakpoint %d: %s\n",
2819 bl->owner->number,
2820 bp_excpt.what ());
2821 }
2822 }
2823 return 1;
2824
2825 }
2826 }
2827 else
2828 bl->inserted = 1;
2829
2830 return 0;
2831 }
2832
2833 else if (bl->loc_type == bp_loc_hardware_watchpoint
2834 /* NOTE drow/2003-09-08: This state only exists for removing
2835 watchpoints. It's not clear that it's necessary... */
2836 && bl->owner->disposition != disp_del_at_next_stop)
2837 {
2838 int val;
2839
2840 gdb_assert (bl->owner->ops != NULL
2841 && bl->owner->ops->insert_location != NULL);
2842
2843 val = bl->owner->ops->insert_location (bl);
2844
2845 /* If trying to set a read-watchpoint, and it turns out it's not
2846 supported, try emulating one with an access watchpoint. */
2847 if (val == 1 && bl->watchpoint_type == hw_read)
2848 {
2849 /* But don't try to insert it, if there's already another
2850 hw_access location that would be considered a duplicate
2851 of this one. */
2852 for (bp_location *loc : all_bp_locations ())
2853 if (loc != bl
2854 && loc->watchpoint_type == hw_access
2855 && watchpoint_locations_match (bl, loc))
2856 {
2857 bl->duplicate = 1;
2858 bl->inserted = 1;
2859 bl->target_info = loc->target_info;
2860 bl->watchpoint_type = hw_access;
2861 val = 0;
2862 break;
2863 }
2864
2865 if (val == 1)
2866 {
2867 bl->watchpoint_type = hw_access;
2868 val = bl->owner->ops->insert_location (bl);
2869
2870 if (val)
2871 /* Back to the original value. */
2872 bl->watchpoint_type = hw_read;
2873 }
2874 }
2875
2876 bl->inserted = (val == 0);
2877 }
2878
2879 else if (bl->owner->type == bp_catchpoint)
2880 {
2881 int val;
2882
2883 gdb_assert (bl->owner->ops != NULL
2884 && bl->owner->ops->insert_location != NULL);
2885
2886 val = bl->owner->ops->insert_location (bl);
2887 if (val)
2888 {
2889 bl->owner->enable_state = bp_disabled;
2890
2891 if (val == 1)
2892 warning (_("\
2893 Error inserting catchpoint %d: Your system does not support this type\n\
2894 of catchpoint."), bl->owner->number);
2895 else
2896 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2897 }
2898
2899 bl->inserted = (val == 0);
2900
2901 /* We've already printed an error message if there was a problem
2902 inserting this catchpoint, and we've disabled the catchpoint,
2903 so just return success. */
2904 return 0;
2905 }
2906
2907 return 0;
2908 }
2909
2910 /* This function is called when program space PSPACE is about to be
2911 deleted. It takes care of updating breakpoints to not reference
2912 PSPACE anymore. */
2913
2914 void
2915 breakpoint_program_space_exit (struct program_space *pspace)
2916 {
2917 /* Remove any breakpoint that was set through this program space. */
2918 for (breakpoint *b : all_breakpoints_safe ())
2919 if (b->pspace == pspace)
2920 delete_breakpoint (b);
2921
2922 /* Breakpoints set through other program spaces could have locations
2923 bound to PSPACE as well. Remove those. */
2924 for (bp_location *loc : all_bp_locations ())
2925 {
2926 struct bp_location *tmp;
2927
2928 if (loc->pspace == pspace)
2929 {
2930 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2931 if (loc->owner->loc == loc)
2932 loc->owner->loc = loc->next;
2933 else
2934 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2935 if (tmp->next == loc)
2936 {
2937 tmp->next = loc->next;
2938 break;
2939 }
2940 }
2941 }
2942
2943 /* Now update the global location list to permanently delete the
2944 removed locations above. */
2945 update_global_location_list (UGLL_DONT_INSERT);
2946 }
2947
2948 /* Make sure all breakpoints are inserted in inferior.
2949 Throws exception on any error.
2950 A breakpoint that is already inserted won't be inserted
2951 again, so calling this function twice is safe. */
2952 void
2953 insert_breakpoints (void)
2954 {
2955 for (breakpoint *bpt : all_breakpoints ())
2956 if (is_hardware_watchpoint (bpt))
2957 {
2958 struct watchpoint *w = (struct watchpoint *) bpt;
2959
2960 update_watchpoint (w, 0 /* don't reparse. */);
2961 }
2962
2963 /* Updating watchpoints creates new locations, so update the global
2964 location list. Explicitly tell ugll to insert locations and
2965 ignore breakpoints_always_inserted_mode. Also,
2966 update_global_location_list tries to "upgrade" software
2967 breakpoints to hardware breakpoints to handle "set breakpoint
2968 auto-hw", so we need to call it even if we don't have new
2969 locations. */
2970 update_global_location_list (UGLL_INSERT);
2971 }
2972
2973 /* This is used when we need to synch breakpoint conditions between GDB and the
2974 target. It is the case with deleting and disabling of breakpoints when using
2975 always-inserted mode. */
2976
2977 static void
2978 update_inserted_breakpoint_locations (void)
2979 {
2980 int error_flag = 0;
2981 int val = 0;
2982 int disabled_breaks = 0;
2983 int hw_breakpoint_error = 0;
2984 int hw_bp_details_reported = 0;
2985
2986 string_file tmp_error_stream;
2987
2988 /* Explicitly mark the warning -- this will only be printed if
2989 there was an error. */
2990 tmp_error_stream.puts ("Warning:\n");
2991
2992 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2993
2994 for (bp_location *bl : all_bp_locations ())
2995 {
2996 /* We only want to update software breakpoints and hardware
2997 breakpoints. */
2998 if (!is_breakpoint (bl->owner))
2999 continue;
3000
3001 /* We only want to update locations that are already inserted
3002 and need updating. This is to avoid unwanted insertion during
3003 deletion of breakpoints. */
3004 if (!bl->inserted || !bl->needs_update)
3005 continue;
3006
3007 switch_to_program_space_and_thread (bl->pspace);
3008
3009 /* For targets that support global breakpoints, there's no need
3010 to select an inferior to insert breakpoint to. In fact, even
3011 if we aren't attached to any process yet, we should still
3012 insert breakpoints. */
3013 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3014 && (inferior_ptid == null_ptid || !target_has_execution ()))
3015 continue;
3016
3017 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3018 &hw_breakpoint_error, &hw_bp_details_reported);
3019 if (val)
3020 error_flag = val;
3021 }
3022
3023 if (error_flag)
3024 {
3025 target_terminal::ours_for_output ();
3026 error_stream (tmp_error_stream);
3027 }
3028 }
3029
3030 /* Used when starting or continuing the program. */
3031
3032 static void
3033 insert_breakpoint_locations (void)
3034 {
3035 int error_flag = 0;
3036 int val = 0;
3037 int disabled_breaks = 0;
3038 int hw_breakpoint_error = 0;
3039 int hw_bp_error_explained_already = 0;
3040
3041 string_file tmp_error_stream;
3042
3043 /* Explicitly mark the warning -- this will only be printed if
3044 there was an error. */
3045 tmp_error_stream.puts ("Warning:\n");
3046
3047 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3048
3049 for (bp_location *bl : all_bp_locations ())
3050 {
3051 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3052 continue;
3053
3054 /* There is no point inserting thread-specific breakpoints if
3055 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3056 has BL->OWNER always non-NULL. */
3057 if (bl->owner->thread != -1
3058 && !valid_global_thread_id (bl->owner->thread))
3059 continue;
3060
3061 switch_to_program_space_and_thread (bl->pspace);
3062
3063 /* For targets that support global breakpoints, there's no need
3064 to select an inferior to insert breakpoint to. In fact, even
3065 if we aren't attached to any process yet, we should still
3066 insert breakpoints. */
3067 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3068 && (inferior_ptid == null_ptid || !target_has_execution ()))
3069 continue;
3070
3071 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3072 &hw_breakpoint_error, &hw_bp_error_explained_already);
3073 if (val)
3074 error_flag = val;
3075 }
3076
3077 /* If we failed to insert all locations of a watchpoint, remove
3078 them, as half-inserted watchpoint is of limited use. */
3079 for (breakpoint *bpt : all_breakpoints ())
3080 {
3081 int some_failed = 0;
3082
3083 if (!is_hardware_watchpoint (bpt))
3084 continue;
3085
3086 if (!breakpoint_enabled (bpt))
3087 continue;
3088
3089 if (bpt->disposition == disp_del_at_next_stop)
3090 continue;
3091
3092 for (bp_location *loc : bpt->locations ())
3093 if (!loc->inserted && should_be_inserted (loc))
3094 {
3095 some_failed = 1;
3096 break;
3097 }
3098
3099 if (some_failed)
3100 {
3101 for (bp_location *loc : bpt->locations ())
3102 if (loc->inserted)
3103 remove_breakpoint (loc);
3104
3105 hw_breakpoint_error = 1;
3106 tmp_error_stream.printf ("Could not insert "
3107 "hardware watchpoint %d.\n",
3108 bpt->number);
3109 error_flag = -1;
3110 }
3111 }
3112
3113 if (error_flag)
3114 {
3115 /* If a hardware breakpoint or watchpoint was inserted, add a
3116 message about possibly exhausted resources. */
3117 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3118 {
3119 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3120 You may have requested too many hardware breakpoints/watchpoints.\n");
3121 }
3122 target_terminal::ours_for_output ();
3123 error_stream (tmp_error_stream);
3124 }
3125 }
3126
3127 /* Used when the program stops.
3128 Returns zero if successful, or non-zero if there was a problem
3129 removing a breakpoint location. */
3130
3131 int
3132 remove_breakpoints (void)
3133 {
3134 int val = 0;
3135
3136 for (bp_location *bl : all_bp_locations ())
3137 if (bl->inserted && !is_tracepoint (bl->owner))
3138 val |= remove_breakpoint (bl);
3139
3140 return val;
3141 }
3142
3143 /* When a thread exits, remove breakpoints that are related to
3144 that thread. */
3145
3146 static void
3147 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3148 {
3149 for (breakpoint *b : all_breakpoints_safe ())
3150 {
3151 if (b->thread == tp->global_num && user_breakpoint_p (b))
3152 {
3153 b->disposition = disp_del_at_next_stop;
3154
3155 gdb_printf (_("\
3156 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3157 b->number, print_thread_id (tp));
3158
3159 /* Hide it from the user. */
3160 b->number = 0;
3161 }
3162 }
3163 }
3164
3165 /* See breakpoint.h. */
3166
3167 void
3168 remove_breakpoints_inf (inferior *inf)
3169 {
3170 int val;
3171
3172 for (bp_location *bl : all_bp_locations ())
3173 {
3174 if (bl->pspace != inf->pspace)
3175 continue;
3176
3177 if (bl->inserted && !bl->target_info.persist)
3178 {
3179 val = remove_breakpoint (bl);
3180 if (val != 0)
3181 return;
3182 }
3183 }
3184 }
3185
3186 static int internal_breakpoint_number = -1;
3187
3188 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3189 If INTERNAL is non-zero, the breakpoint number will be populated
3190 from internal_breakpoint_number and that variable decremented.
3191 Otherwise the breakpoint number will be populated from
3192 breakpoint_count and that value incremented. Internal breakpoints
3193 do not set the internal var bpnum. */
3194 static void
3195 set_breakpoint_number (int internal, struct breakpoint *b)
3196 {
3197 if (internal)
3198 b->number = internal_breakpoint_number--;
3199 else
3200 {
3201 set_breakpoint_count (breakpoint_count + 1);
3202 b->number = breakpoint_count;
3203 }
3204 }
3205
3206 static struct breakpoint *
3207 create_internal_breakpoint (struct gdbarch *gdbarch,
3208 CORE_ADDR address, enum bptype type,
3209 const struct breakpoint_ops *ops)
3210 {
3211 symtab_and_line sal;
3212 sal.pc = address;
3213 sal.section = find_pc_overlay (sal.pc);
3214 sal.pspace = current_program_space;
3215
3216 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3217 b->number = internal_breakpoint_number--;
3218 b->disposition = disp_donttouch;
3219
3220 return b;
3221 }
3222
3223 static const char *const longjmp_names[] =
3224 {
3225 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3226 };
3227 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3228
3229 /* Per-objfile data private to breakpoint.c. */
3230 struct breakpoint_objfile_data
3231 {
3232 /* Minimal symbol for "_ovly_debug_event" (if any). */
3233 struct bound_minimal_symbol overlay_msym;
3234
3235 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3236 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3237
3238 /* True if we have looked for longjmp probes. */
3239 int longjmp_searched = 0;
3240
3241 /* SystemTap probe points for longjmp (if any). These are non-owning
3242 references. */
3243 std::vector<probe *> longjmp_probes;
3244
3245 /* Minimal symbol for "std::terminate()" (if any). */
3246 struct bound_minimal_symbol terminate_msym;
3247
3248 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3249 struct bound_minimal_symbol exception_msym;
3250
3251 /* True if we have looked for exception probes. */
3252 int exception_searched = 0;
3253
3254 /* SystemTap probe points for unwinding (if any). These are non-owning
3255 references. */
3256 std::vector<probe *> exception_probes;
3257 };
3258
3259 static const struct objfile_key<breakpoint_objfile_data>
3260 breakpoint_objfile_key;
3261
3262 /* Minimal symbol not found sentinel. */
3263 static struct minimal_symbol msym_not_found;
3264
3265 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3266
3267 static int
3268 msym_not_found_p (const struct minimal_symbol *msym)
3269 {
3270 return msym == &msym_not_found;
3271 }
3272
3273 /* Return per-objfile data needed by breakpoint.c.
3274 Allocate the data if necessary. */
3275
3276 static struct breakpoint_objfile_data *
3277 get_breakpoint_objfile_data (struct objfile *objfile)
3278 {
3279 struct breakpoint_objfile_data *bp_objfile_data;
3280
3281 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3282 if (bp_objfile_data == NULL)
3283 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3284 return bp_objfile_data;
3285 }
3286
3287 static void
3288 create_overlay_event_breakpoint (void)
3289 {
3290 const char *const func_name = "_ovly_debug_event";
3291
3292 for (objfile *objfile : current_program_space->objfiles ())
3293 {
3294 struct breakpoint *b;
3295 struct breakpoint_objfile_data *bp_objfile_data;
3296 CORE_ADDR addr;
3297 struct explicit_location explicit_loc;
3298
3299 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3300
3301 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3302 continue;
3303
3304 if (bp_objfile_data->overlay_msym.minsym == NULL)
3305 {
3306 struct bound_minimal_symbol m;
3307
3308 m = lookup_minimal_symbol_text (func_name, objfile);
3309 if (m.minsym == NULL)
3310 {
3311 /* Avoid future lookups in this objfile. */
3312 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3313 continue;
3314 }
3315 bp_objfile_data->overlay_msym = m;
3316 }
3317
3318 addr = bp_objfile_data->overlay_msym.value_address ();
3319 b = create_internal_breakpoint (objfile->arch (), addr,
3320 bp_overlay_event,
3321 &vtable_breakpoint_ops);
3322 initialize_explicit_location (&explicit_loc);
3323 explicit_loc.function_name = ASTRDUP (func_name);
3324 b->location = new_explicit_location (&explicit_loc);
3325
3326 if (overlay_debugging == ovly_auto)
3327 {
3328 b->enable_state = bp_enabled;
3329 overlay_events_enabled = 1;
3330 }
3331 else
3332 {
3333 b->enable_state = bp_disabled;
3334 overlay_events_enabled = 0;
3335 }
3336 }
3337 }
3338
3339 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3340 true if a breakpoint was installed. */
3341
3342 static bool
3343 create_longjmp_master_breakpoint_probe (objfile *objfile)
3344 {
3345 struct gdbarch *gdbarch = objfile->arch ();
3346 struct breakpoint_objfile_data *bp_objfile_data
3347 = get_breakpoint_objfile_data (objfile);
3348
3349 if (!bp_objfile_data->longjmp_searched)
3350 {
3351 std::vector<probe *> ret
3352 = find_probes_in_objfile (objfile, "libc", "longjmp");
3353
3354 if (!ret.empty ())
3355 {
3356 /* We are only interested in checking one element. */
3357 probe *p = ret[0];
3358
3359 if (!p->can_evaluate_arguments ())
3360 {
3361 /* We cannot use the probe interface here,
3362 because it does not know how to evaluate
3363 arguments. */
3364 ret.clear ();
3365 }
3366 }
3367 bp_objfile_data->longjmp_probes = ret;
3368 bp_objfile_data->longjmp_searched = 1;
3369 }
3370
3371 if (bp_objfile_data->longjmp_probes.empty ())
3372 return false;
3373
3374 for (probe *p : bp_objfile_data->longjmp_probes)
3375 {
3376 struct breakpoint *b;
3377
3378 b = create_internal_breakpoint (gdbarch,
3379 p->get_relocated_address (objfile),
3380 bp_longjmp_master,
3381 &vtable_breakpoint_ops);
3382 b->location = new_probe_location ("-probe-stap libc:longjmp");
3383 b->enable_state = bp_disabled;
3384 }
3385
3386 return true;
3387 }
3388
3389 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3390 Return true if at least one breakpoint was installed. */
3391
3392 static bool
3393 create_longjmp_master_breakpoint_names (objfile *objfile)
3394 {
3395 struct gdbarch *gdbarch = objfile->arch ();
3396 if (!gdbarch_get_longjmp_target_p (gdbarch))
3397 return false;
3398
3399 struct breakpoint_objfile_data *bp_objfile_data
3400 = get_breakpoint_objfile_data (objfile);
3401 unsigned int installed_bp = 0;
3402
3403 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3404 {
3405 struct breakpoint *b;
3406 const char *func_name;
3407 CORE_ADDR addr;
3408 struct explicit_location explicit_loc;
3409
3410 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3411 continue;
3412
3413 func_name = longjmp_names[i];
3414 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3415 {
3416 struct bound_minimal_symbol m;
3417
3418 m = lookup_minimal_symbol_text (func_name, objfile);
3419 if (m.minsym == NULL)
3420 {
3421 /* Prevent future lookups in this objfile. */
3422 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3423 continue;
3424 }
3425 bp_objfile_data->longjmp_msym[i] = m;
3426 }
3427
3428 addr = bp_objfile_data->longjmp_msym[i].value_address ();
3429 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3430 &vtable_breakpoint_ops);
3431 initialize_explicit_location (&explicit_loc);
3432 explicit_loc.function_name = ASTRDUP (func_name);
3433 b->location = new_explicit_location (&explicit_loc);
3434 b->enable_state = bp_disabled;
3435 installed_bp++;
3436 }
3437
3438 return installed_bp > 0;
3439 }
3440
3441 /* Create a master longjmp breakpoint. */
3442
3443 static void
3444 create_longjmp_master_breakpoint (void)
3445 {
3446 scoped_restore_current_program_space restore_pspace;
3447
3448 for (struct program_space *pspace : program_spaces)
3449 {
3450 set_current_program_space (pspace);
3451
3452 for (objfile *obj : current_program_space->objfiles ())
3453 {
3454 /* Skip separate debug object, it's handled in the loop below. */
3455 if (obj->separate_debug_objfile_backlink != nullptr)
3456 continue;
3457
3458 /* Try a probe kind breakpoint on main objfile. */
3459 if (create_longjmp_master_breakpoint_probe (obj))
3460 continue;
3461
3462 /* Try longjmp_names kind breakpoints on main and separate_debug
3463 objfiles. */
3464 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3465 if (create_longjmp_master_breakpoint_names (debug_objfile))
3466 break;
3467 }
3468 }
3469 }
3470
3471 /* Create a master std::terminate breakpoint. */
3472 static void
3473 create_std_terminate_master_breakpoint (void)
3474 {
3475 const char *const func_name = "std::terminate()";
3476
3477 scoped_restore_current_program_space restore_pspace;
3478
3479 for (struct program_space *pspace : program_spaces)
3480 {
3481 CORE_ADDR addr;
3482
3483 set_current_program_space (pspace);
3484
3485 for (objfile *objfile : current_program_space->objfiles ())
3486 {
3487 struct breakpoint *b;
3488 struct breakpoint_objfile_data *bp_objfile_data;
3489 struct explicit_location explicit_loc;
3490
3491 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3492
3493 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3494 continue;
3495
3496 if (bp_objfile_data->terminate_msym.minsym == NULL)
3497 {
3498 struct bound_minimal_symbol m;
3499
3500 m = lookup_minimal_symbol (func_name, NULL, objfile);
3501 if (m.minsym == NULL || (m.minsym->type () != mst_text
3502 && m.minsym->type () != mst_file_text))
3503 {
3504 /* Prevent future lookups in this objfile. */
3505 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3506 continue;
3507 }
3508 bp_objfile_data->terminate_msym = m;
3509 }
3510
3511 addr = bp_objfile_data->terminate_msym.value_address ();
3512 b = create_internal_breakpoint (objfile->arch (), addr,
3513 bp_std_terminate_master,
3514 &vtable_breakpoint_ops);
3515 initialize_explicit_location (&explicit_loc);
3516 explicit_loc.function_name = ASTRDUP (func_name);
3517 b->location = new_explicit_location (&explicit_loc);
3518 b->enable_state = bp_disabled;
3519 }
3520 }
3521 }
3522
3523 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3524 probe. Return true if a breakpoint was installed. */
3525
3526 static bool
3527 create_exception_master_breakpoint_probe (objfile *objfile)
3528 {
3529 struct breakpoint *b;
3530 struct gdbarch *gdbarch;
3531 struct breakpoint_objfile_data *bp_objfile_data;
3532
3533 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3534
3535 /* We prefer the SystemTap probe point if it exists. */
3536 if (!bp_objfile_data->exception_searched)
3537 {
3538 std::vector<probe *> ret
3539 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3540
3541 if (!ret.empty ())
3542 {
3543 /* We are only interested in checking one element. */
3544 probe *p = ret[0];
3545
3546 if (!p->can_evaluate_arguments ())
3547 {
3548 /* We cannot use the probe interface here, because it does
3549 not know how to evaluate arguments. */
3550 ret.clear ();
3551 }
3552 }
3553 bp_objfile_data->exception_probes = ret;
3554 bp_objfile_data->exception_searched = 1;
3555 }
3556
3557 if (bp_objfile_data->exception_probes.empty ())
3558 return false;
3559
3560 gdbarch = objfile->arch ();
3561
3562 for (probe *p : bp_objfile_data->exception_probes)
3563 {
3564 b = create_internal_breakpoint (gdbarch,
3565 p->get_relocated_address (objfile),
3566 bp_exception_master,
3567 &vtable_breakpoint_ops);
3568 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3569 b->enable_state = bp_disabled;
3570 }
3571
3572 return true;
3573 }
3574
3575 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3576 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3577
3578 static bool
3579 create_exception_master_breakpoint_hook (objfile *objfile)
3580 {
3581 const char *const func_name = "_Unwind_DebugHook";
3582 struct breakpoint *b;
3583 struct gdbarch *gdbarch;
3584 struct breakpoint_objfile_data *bp_objfile_data;
3585 CORE_ADDR addr;
3586 struct explicit_location explicit_loc;
3587
3588 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3589
3590 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3591 return false;
3592
3593 gdbarch = objfile->arch ();
3594
3595 if (bp_objfile_data->exception_msym.minsym == NULL)
3596 {
3597 struct bound_minimal_symbol debug_hook;
3598
3599 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3600 if (debug_hook.minsym == NULL)
3601 {
3602 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3603 return false;
3604 }
3605
3606 bp_objfile_data->exception_msym = debug_hook;
3607 }
3608
3609 addr = bp_objfile_data->exception_msym.value_address ();
3610 addr = gdbarch_convert_from_func_ptr_addr
3611 (gdbarch, addr, current_inferior ()->top_target ());
3612 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3613 &vtable_breakpoint_ops);
3614 initialize_explicit_location (&explicit_loc);
3615 explicit_loc.function_name = ASTRDUP (func_name);
3616 b->location = new_explicit_location (&explicit_loc);
3617 b->enable_state = bp_disabled;
3618
3619 return true;
3620 }
3621
3622 /* Install a master breakpoint on the unwinder's debug hook. */
3623
3624 static void
3625 create_exception_master_breakpoint (void)
3626 {
3627 for (objfile *obj : current_program_space->objfiles ())
3628 {
3629 /* Skip separate debug object. */
3630 if (obj->separate_debug_objfile_backlink)
3631 continue;
3632
3633 /* Try a probe kind breakpoint. */
3634 if (create_exception_master_breakpoint_probe (obj))
3635 continue;
3636
3637 /* Iterate over main and separate debug objects and try an
3638 _Unwind_DebugHook kind breakpoint. */
3639 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3640 if (create_exception_master_breakpoint_hook (debug_objfile))
3641 break;
3642 }
3643 }
3644
3645 /* Does B have a location spec? */
3646
3647 static int
3648 breakpoint_event_location_empty_p (const struct breakpoint *b)
3649 {
3650 return b->location != NULL && event_location_empty_p (b->location.get ());
3651 }
3652
3653 void
3654 update_breakpoints_after_exec (void)
3655 {
3656 /* We're about to delete breakpoints from GDB's lists. If the
3657 INSERTED flag is true, GDB will try to lift the breakpoints by
3658 writing the breakpoints' "shadow contents" back into memory. The
3659 "shadow contents" are NOT valid after an exec, so GDB should not
3660 do that. Instead, the target is responsible from marking
3661 breakpoints out as soon as it detects an exec. We don't do that
3662 here instead, because there may be other attempts to delete
3663 breakpoints after detecting an exec and before reaching here. */
3664 for (bp_location *bploc : all_bp_locations ())
3665 if (bploc->pspace == current_program_space)
3666 gdb_assert (!bploc->inserted);
3667
3668 for (breakpoint *b : all_breakpoints_safe ())
3669 {
3670 if (b->pspace != current_program_space)
3671 continue;
3672
3673 /* Solib breakpoints must be explicitly reset after an exec(). */
3674 if (b->type == bp_shlib_event)
3675 {
3676 delete_breakpoint (b);
3677 continue;
3678 }
3679
3680 /* JIT breakpoints must be explicitly reset after an exec(). */
3681 if (b->type == bp_jit_event)
3682 {
3683 delete_breakpoint (b);
3684 continue;
3685 }
3686
3687 /* Thread event breakpoints must be set anew after an exec(),
3688 as must overlay event and longjmp master breakpoints. */
3689 if (b->type == bp_thread_event || b->type == bp_overlay_event
3690 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3691 || b->type == bp_exception_master)
3692 {
3693 delete_breakpoint (b);
3694 continue;
3695 }
3696
3697 /* Step-resume breakpoints are meaningless after an exec(). */
3698 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3699 {
3700 delete_breakpoint (b);
3701 continue;
3702 }
3703
3704 /* Just like single-step breakpoints. */
3705 if (b->type == bp_single_step)
3706 {
3707 delete_breakpoint (b);
3708 continue;
3709 }
3710
3711 /* Longjmp and longjmp-resume breakpoints are also meaningless
3712 after an exec. */
3713 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3714 || b->type == bp_longjmp_call_dummy
3715 || b->type == bp_exception || b->type == bp_exception_resume)
3716 {
3717 delete_breakpoint (b);
3718 continue;
3719 }
3720
3721 if (b->type == bp_catchpoint)
3722 {
3723 /* For now, none of the bp_catchpoint breakpoints need to
3724 do anything at this point. In the future, if some of
3725 the catchpoints need to something, we will need to add
3726 a new method, and call this method from here. */
3727 continue;
3728 }
3729
3730 /* bp_finish is a special case. The only way we ought to be able
3731 to see one of these when an exec() has happened, is if the user
3732 caught a vfork, and then said "finish". Ordinarily a finish just
3733 carries them to the call-site of the current callee, by setting
3734 a temporary bp there and resuming. But in this case, the finish
3735 will carry them entirely through the vfork & exec.
3736
3737 We don't want to allow a bp_finish to remain inserted now. But
3738 we can't safely delete it, 'cause finish_command has a handle to
3739 the bp on a bpstat, and will later want to delete it. There's a
3740 chance (and I've seen it happen) that if we delete the bp_finish
3741 here, that its storage will get reused by the time finish_command
3742 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3743 We really must allow finish_command to delete a bp_finish.
3744
3745 In the absence of a general solution for the "how do we know
3746 it's safe to delete something others may have handles to?"
3747 problem, what we'll do here is just uninsert the bp_finish, and
3748 let finish_command delete it.
3749
3750 (We know the bp_finish is "doomed" in the sense that it's
3751 momentary, and will be deleted as soon as finish_command sees
3752 the inferior stopped. So it doesn't matter that the bp's
3753 address is probably bogus in the new a.out, unlike e.g., the
3754 solib breakpoints.) */
3755
3756 if (b->type == bp_finish)
3757 {
3758 continue;
3759 }
3760
3761 /* Without a symbolic address, we have little hope of the
3762 pre-exec() address meaning the same thing in the post-exec()
3763 a.out. */
3764 if (breakpoint_event_location_empty_p (b))
3765 {
3766 delete_breakpoint (b);
3767 continue;
3768 }
3769 }
3770 }
3771
3772 int
3773 detach_breakpoints (ptid_t ptid)
3774 {
3775 int val = 0;
3776 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3777 struct inferior *inf = current_inferior ();
3778
3779 if (ptid.pid () == inferior_ptid.pid ())
3780 error (_("Cannot detach breakpoints of inferior_ptid"));
3781
3782 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3783 inferior_ptid = ptid;
3784 for (bp_location *bl : all_bp_locations ())
3785 {
3786 if (bl->pspace != inf->pspace)
3787 continue;
3788
3789 /* This function must physically remove breakpoints locations
3790 from the specified ptid, without modifying the breakpoint
3791 package's state. Locations of type bp_loc_other are only
3792 maintained at GDB side. So, there is no need to remove
3793 these bp_loc_other locations. Moreover, removing these
3794 would modify the breakpoint package's state. */
3795 if (bl->loc_type == bp_loc_other)
3796 continue;
3797
3798 if (bl->inserted)
3799 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3800 }
3801
3802 return val;
3803 }
3804
3805 /* Remove the breakpoint location BL from the current address space.
3806 Note that this is used to detach breakpoints from a child fork.
3807 When we get here, the child isn't in the inferior list, and neither
3808 do we have objects to represent its address space --- we should
3809 *not* look at bl->pspace->aspace here. */
3810
3811 static int
3812 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3813 {
3814 int val;
3815
3816 /* BL is never in moribund_locations by our callers. */
3817 gdb_assert (bl->owner != NULL);
3818
3819 /* The type of none suggests that owner is actually deleted.
3820 This should not ever happen. */
3821 gdb_assert (bl->owner->type != bp_none);
3822
3823 if (bl->loc_type == bp_loc_software_breakpoint
3824 || bl->loc_type == bp_loc_hardware_breakpoint)
3825 {
3826 /* "Normal" instruction breakpoint: either the standard
3827 trap-instruction bp (bp_breakpoint), or a
3828 bp_hardware_breakpoint. */
3829
3830 /* First check to see if we have to handle an overlay. */
3831 if (overlay_debugging == ovly_off
3832 || bl->section == NULL
3833 || !(section_is_overlay (bl->section)))
3834 {
3835 /* No overlay handling: just remove the breakpoint. */
3836
3837 /* If we're trying to uninsert a memory breakpoint that we
3838 know is set in a dynamic object that is marked
3839 shlib_disabled, then either the dynamic object was
3840 removed with "remove-symbol-file" or with
3841 "nosharedlibrary". In the former case, we don't know
3842 whether another dynamic object might have loaded over the
3843 breakpoint's address -- the user might well let us know
3844 about it next with add-symbol-file (the whole point of
3845 add-symbol-file is letting the user manually maintain a
3846 list of dynamically loaded objects). If we have the
3847 breakpoint's shadow memory, that is, this is a software
3848 breakpoint managed by GDB, check whether the breakpoint
3849 is still inserted in memory, to avoid overwriting wrong
3850 code with stale saved shadow contents. Note that HW
3851 breakpoints don't have shadow memory, as they're
3852 implemented using a mechanism that is not dependent on
3853 being able to modify the target's memory, and as such
3854 they should always be removed. */
3855 if (bl->shlib_disabled
3856 && bl->target_info.shadow_len != 0
3857 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3858 val = 0;
3859 else
3860 val = bl->owner->ops->remove_location (bl, reason);
3861 }
3862 else
3863 {
3864 /* This breakpoint is in an overlay section.
3865 Did we set a breakpoint at the LMA? */
3866 if (!overlay_events_enabled)
3867 {
3868 /* Yes -- overlay event support is not active, so we
3869 should have set a breakpoint at the LMA. Remove it.
3870 */
3871 /* Ignore any failures: if the LMA is in ROM, we will
3872 have already warned when we failed to insert it. */
3873 if (bl->loc_type == bp_loc_hardware_breakpoint)
3874 target_remove_hw_breakpoint (bl->gdbarch,
3875 &bl->overlay_target_info);
3876 else
3877 target_remove_breakpoint (bl->gdbarch,
3878 &bl->overlay_target_info,
3879 reason);
3880 }
3881 /* Did we set a breakpoint at the VMA?
3882 If so, we will have marked the breakpoint 'inserted'. */
3883 if (bl->inserted)
3884 {
3885 /* Yes -- remove it. Previously we did not bother to
3886 remove the breakpoint if the section had been
3887 unmapped, but let's not rely on that being safe. We
3888 don't know what the overlay manager might do. */
3889
3890 /* However, we should remove *software* breakpoints only
3891 if the section is still mapped, or else we overwrite
3892 wrong code with the saved shadow contents. */
3893 if (bl->loc_type == bp_loc_hardware_breakpoint
3894 || section_is_mapped (bl->section))
3895 val = bl->owner->ops->remove_location (bl, reason);
3896 else
3897 val = 0;
3898 }
3899 else
3900 {
3901 /* No -- not inserted, so no need to remove. No error. */
3902 val = 0;
3903 }
3904 }
3905
3906 /* In some cases, we might not be able to remove a breakpoint in
3907 a shared library that has already been removed, but we have
3908 not yet processed the shlib unload event. Similarly for an
3909 unloaded add-symbol-file object - the user might not yet have
3910 had the chance to remove-symbol-file it. shlib_disabled will
3911 be set if the library/object has already been removed, but
3912 the breakpoint hasn't been uninserted yet, e.g., after
3913 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3914 always-inserted mode. */
3915 if (val
3916 && (bl->loc_type == bp_loc_software_breakpoint
3917 && (bl->shlib_disabled
3918 || solib_name_from_address (bl->pspace, bl->address)
3919 || shared_objfile_contains_address_p (bl->pspace,
3920 bl->address))))
3921 val = 0;
3922
3923 if (val)
3924 return val;
3925 bl->inserted = (reason == DETACH_BREAKPOINT);
3926 }
3927 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3928 {
3929 gdb_assert (bl->owner->ops != NULL
3930 && bl->owner->ops->remove_location != NULL);
3931
3932 bl->inserted = (reason == DETACH_BREAKPOINT);
3933 bl->owner->ops->remove_location (bl, reason);
3934
3935 /* Failure to remove any of the hardware watchpoints comes here. */
3936 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3937 warning (_("Could not remove hardware watchpoint %d."),
3938 bl->owner->number);
3939 }
3940 else if (bl->owner->type == bp_catchpoint
3941 && breakpoint_enabled (bl->owner)
3942 && !bl->duplicate)
3943 {
3944 gdb_assert (bl->owner->ops != NULL
3945 && bl->owner->ops->remove_location != NULL);
3946
3947 val = bl->owner->ops->remove_location (bl, reason);
3948 if (val)
3949 return val;
3950
3951 bl->inserted = (reason == DETACH_BREAKPOINT);
3952 }
3953
3954 return 0;
3955 }
3956
3957 static int
3958 remove_breakpoint (struct bp_location *bl)
3959 {
3960 /* BL is never in moribund_locations by our callers. */
3961 gdb_assert (bl->owner != NULL);
3962
3963 /* The type of none suggests that owner is actually deleted.
3964 This should not ever happen. */
3965 gdb_assert (bl->owner->type != bp_none);
3966
3967 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3968
3969 switch_to_program_space_and_thread (bl->pspace);
3970
3971 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3972 }
3973
3974 /* Clear the "inserted" flag in all breakpoints. */
3975
3976 void
3977 mark_breakpoints_out (void)
3978 {
3979 for (bp_location *bl : all_bp_locations ())
3980 if (bl->pspace == current_program_space)
3981 bl->inserted = 0;
3982 }
3983
3984 /* Clear the "inserted" flag in all breakpoints and delete any
3985 breakpoints which should go away between runs of the program.
3986
3987 Plus other such housekeeping that has to be done for breakpoints
3988 between runs.
3989
3990 Note: this function gets called at the end of a run (by
3991 generic_mourn_inferior) and when a run begins (by
3992 init_wait_for_inferior). */
3993
3994
3995
3996 void
3997 breakpoint_init_inferior (enum inf_context context)
3998 {
3999 struct program_space *pspace = current_program_space;
4000
4001 /* If breakpoint locations are shared across processes, then there's
4002 nothing to do. */
4003 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4004 return;
4005
4006 mark_breakpoints_out ();
4007
4008 for (breakpoint *b : all_breakpoints_safe ())
4009 {
4010 if (b->loc && b->loc->pspace != pspace)
4011 continue;
4012
4013 switch (b->type)
4014 {
4015 case bp_call_dummy:
4016 case bp_longjmp_call_dummy:
4017
4018 /* If the call dummy breakpoint is at the entry point it will
4019 cause problems when the inferior is rerun, so we better get
4020 rid of it. */
4021
4022 case bp_watchpoint_scope:
4023
4024 /* Also get rid of scope breakpoints. */
4025
4026 case bp_shlib_event:
4027
4028 /* Also remove solib event breakpoints. Their addresses may
4029 have changed since the last time we ran the program.
4030 Actually we may now be debugging against different target;
4031 and so the solib backend that installed this breakpoint may
4032 not be used in by the target. E.g.,
4033
4034 (gdb) file prog-linux
4035 (gdb) run # native linux target
4036 ...
4037 (gdb) kill
4038 (gdb) file prog-win.exe
4039 (gdb) tar rem :9999 # remote Windows gdbserver.
4040 */
4041
4042 case bp_step_resume:
4043
4044 /* Also remove step-resume breakpoints. */
4045
4046 case bp_single_step:
4047
4048 /* Also remove single-step breakpoints. */
4049
4050 delete_breakpoint (b);
4051 break;
4052
4053 case bp_watchpoint:
4054 case bp_hardware_watchpoint:
4055 case bp_read_watchpoint:
4056 case bp_access_watchpoint:
4057 {
4058 struct watchpoint *w = (struct watchpoint *) b;
4059
4060 /* Likewise for watchpoints on local expressions. */
4061 if (w->exp_valid_block != NULL)
4062 delete_breakpoint (b);
4063 else
4064 {
4065 /* Get rid of existing locations, which are no longer
4066 valid. New ones will be created in
4067 update_watchpoint, when the inferior is restarted.
4068 The next update_global_location_list call will
4069 garbage collect them. */
4070 b->loc = NULL;
4071
4072 if (context == inf_starting)
4073 {
4074 /* Reset val field to force reread of starting value in
4075 insert_breakpoints. */
4076 w->val.reset (nullptr);
4077 w->val_valid = false;
4078 }
4079 }
4080 }
4081 break;
4082 default:
4083 break;
4084 }
4085 }
4086
4087 /* Get rid of the moribund locations. */
4088 for (bp_location *bl : moribund_locations)
4089 decref_bp_location (&bl);
4090 moribund_locations.clear ();
4091 }
4092
4093 /* These functions concern about actual breakpoints inserted in the
4094 target --- to e.g. check if we need to do decr_pc adjustment or if
4095 we need to hop over the bkpt --- so we check for address space
4096 match, not program space. */
4097
4098 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4099 exists at PC. It returns ordinary_breakpoint_here if it's an
4100 ordinary breakpoint, or permanent_breakpoint_here if it's a
4101 permanent breakpoint.
4102 - When continuing from a location with an ordinary breakpoint, we
4103 actually single step once before calling insert_breakpoints.
4104 - When continuing from a location with a permanent breakpoint, we
4105 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4106 the target, to advance the PC past the breakpoint. */
4107
4108 enum breakpoint_here
4109 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4110 {
4111 int any_breakpoint_here = 0;
4112
4113 for (bp_location *bl : all_bp_locations ())
4114 {
4115 if (bl->loc_type != bp_loc_software_breakpoint
4116 && bl->loc_type != bp_loc_hardware_breakpoint)
4117 continue;
4118
4119 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4120 if ((breakpoint_enabled (bl->owner)
4121 || bl->permanent)
4122 && breakpoint_location_address_match (bl, aspace, pc))
4123 {
4124 if (overlay_debugging
4125 && section_is_overlay (bl->section)
4126 && !section_is_mapped (bl->section))
4127 continue; /* unmapped overlay -- can't be a match */
4128 else if (bl->permanent)
4129 return permanent_breakpoint_here;
4130 else
4131 any_breakpoint_here = 1;
4132 }
4133 }
4134
4135 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4136 }
4137
4138 /* See breakpoint.h. */
4139
4140 int
4141 breakpoint_in_range_p (const address_space *aspace,
4142 CORE_ADDR addr, ULONGEST len)
4143 {
4144 for (bp_location *bl : all_bp_locations ())
4145 {
4146 if (bl->loc_type != bp_loc_software_breakpoint
4147 && bl->loc_type != bp_loc_hardware_breakpoint)
4148 continue;
4149
4150 if ((breakpoint_enabled (bl->owner)
4151 || bl->permanent)
4152 && breakpoint_location_address_range_overlap (bl, aspace,
4153 addr, len))
4154 {
4155 if (overlay_debugging
4156 && section_is_overlay (bl->section)
4157 && !section_is_mapped (bl->section))
4158 {
4159 /* Unmapped overlay -- can't be a match. */
4160 continue;
4161 }
4162
4163 return 1;
4164 }
4165 }
4166
4167 return 0;
4168 }
4169
4170 /* Return true if there's a moribund breakpoint at PC. */
4171
4172 int
4173 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4174 {
4175 for (bp_location *loc : moribund_locations)
4176 if (breakpoint_location_address_match (loc, aspace, pc))
4177 return 1;
4178
4179 return 0;
4180 }
4181
4182 /* Returns non-zero iff BL is inserted at PC, in address space
4183 ASPACE. */
4184
4185 static int
4186 bp_location_inserted_here_p (struct bp_location *bl,
4187 const address_space *aspace, CORE_ADDR pc)
4188 {
4189 if (bl->inserted
4190 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4191 aspace, pc))
4192 {
4193 if (overlay_debugging
4194 && section_is_overlay (bl->section)
4195 && !section_is_mapped (bl->section))
4196 return 0; /* unmapped overlay -- can't be a match */
4197 else
4198 return 1;
4199 }
4200 return 0;
4201 }
4202
4203 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4204
4205 int
4206 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4207 {
4208 for (bp_location *bl : all_bp_locations_at_addr (pc))
4209 {
4210 if (bl->loc_type != bp_loc_software_breakpoint
4211 && bl->loc_type != bp_loc_hardware_breakpoint)
4212 continue;
4213
4214 if (bp_location_inserted_here_p (bl, aspace, pc))
4215 return 1;
4216 }
4217 return 0;
4218 }
4219
4220 /* This function returns non-zero iff there is a software breakpoint
4221 inserted at PC. */
4222
4223 int
4224 software_breakpoint_inserted_here_p (const address_space *aspace,
4225 CORE_ADDR pc)
4226 {
4227 for (bp_location *bl : all_bp_locations_at_addr (pc))
4228 {
4229 if (bl->loc_type != bp_loc_software_breakpoint)
4230 continue;
4231
4232 if (bp_location_inserted_here_p (bl, aspace, pc))
4233 return 1;
4234 }
4235
4236 return 0;
4237 }
4238
4239 /* See breakpoint.h. */
4240
4241 int
4242 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4243 CORE_ADDR pc)
4244 {
4245 for (bp_location *bl : all_bp_locations_at_addr (pc))
4246 {
4247 if (bl->loc_type != bp_loc_hardware_breakpoint)
4248 continue;
4249
4250 if (bp_location_inserted_here_p (bl, aspace, pc))
4251 return 1;
4252 }
4253
4254 return 0;
4255 }
4256
4257 int
4258 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4259 CORE_ADDR addr, ULONGEST len)
4260 {
4261 for (breakpoint *bpt : all_breakpoints ())
4262 {
4263 if (bpt->type != bp_hardware_watchpoint
4264 && bpt->type != bp_access_watchpoint)
4265 continue;
4266
4267 if (!breakpoint_enabled (bpt))
4268 continue;
4269
4270 for (bp_location *loc : bpt->locations ())
4271 if (loc->pspace->aspace == aspace && loc->inserted)
4272 {
4273 CORE_ADDR l, h;
4274
4275 /* Check for intersection. */
4276 l = std::max<CORE_ADDR> (loc->address, addr);
4277 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4278 if (l < h)
4279 return 1;
4280 }
4281 }
4282 return 0;
4283 }
4284
4285 /* See breakpoint.h. */
4286
4287 bool
4288 is_catchpoint (struct breakpoint *b)
4289 {
4290 return (b->type == bp_catchpoint);
4291 }
4292
4293 /* Clear a bpstat so that it says we are not at any breakpoint.
4294 Also free any storage that is part of a bpstat. */
4295
4296 void
4297 bpstat_clear (bpstat **bsp)
4298 {
4299 bpstat *p;
4300 bpstat *q;
4301
4302 if (bsp == 0)
4303 return;
4304 p = *bsp;
4305 while (p != NULL)
4306 {
4307 q = p->next;
4308 delete p;
4309 p = q;
4310 }
4311 *bsp = NULL;
4312 }
4313
4314 bpstat::bpstat (const bpstat &other)
4315 : next (NULL),
4316 bp_location_at (other.bp_location_at),
4317 breakpoint_at (other.breakpoint_at),
4318 commands (other.commands),
4319 print (other.print),
4320 stop (other.stop),
4321 print_it (other.print_it)
4322 {
4323 if (other.old_val != NULL)
4324 old_val = release_value (value_copy (other.old_val.get ()));
4325 }
4326
4327 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4328 is part of the bpstat is copied as well. */
4329
4330 bpstat *
4331 bpstat_copy (bpstat *bs)
4332 {
4333 bpstat *p = nullptr;
4334 bpstat *tmp;
4335 bpstat *retval = nullptr;
4336
4337 if (bs == NULL)
4338 return bs;
4339
4340 for (; bs != NULL; bs = bs->next)
4341 {
4342 tmp = new bpstat (*bs);
4343
4344 if (p == NULL)
4345 /* This is the first thing in the chain. */
4346 retval = tmp;
4347 else
4348 p->next = tmp;
4349 p = tmp;
4350 }
4351 p->next = NULL;
4352 return retval;
4353 }
4354
4355 /* Find the bpstat associated with this breakpoint. */
4356
4357 bpstat *
4358 bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
4359 {
4360 if (bsp == NULL)
4361 return NULL;
4362
4363 for (; bsp != NULL; bsp = bsp->next)
4364 {
4365 if (bsp->breakpoint_at == breakpoint)
4366 return bsp;
4367 }
4368 return NULL;
4369 }
4370
4371 /* See breakpoint.h. */
4372
4373 bool
4374 bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
4375 {
4376 for (; bsp != NULL; bsp = bsp->next)
4377 {
4378 if (bsp->breakpoint_at == NULL)
4379 {
4380 /* A moribund location can never explain a signal other than
4381 GDB_SIGNAL_TRAP. */
4382 if (sig == GDB_SIGNAL_TRAP)
4383 return true;
4384 }
4385 else
4386 {
4387 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4388 sig))
4389 return true;
4390 }
4391 }
4392
4393 return false;
4394 }
4395
4396 /* Put in *NUM the breakpoint number of the first breakpoint we are
4397 stopped at. *BSP upon return is a bpstat which points to the
4398 remaining breakpoints stopped at (but which is not guaranteed to be
4399 good for anything but further calls to bpstat_num).
4400
4401 Return 0 if passed a bpstat which does not indicate any breakpoints.
4402 Return -1 if stopped at a breakpoint that has been deleted since
4403 we set it.
4404 Return 1 otherwise. */
4405
4406 int
4407 bpstat_num (bpstat **bsp, int *num)
4408 {
4409 struct breakpoint *b;
4410
4411 if ((*bsp) == NULL)
4412 return 0; /* No more breakpoint values */
4413
4414 /* We assume we'll never have several bpstats that correspond to a
4415 single breakpoint -- otherwise, this function might return the
4416 same number more than once and this will look ugly. */
4417 b = (*bsp)->breakpoint_at;
4418 *bsp = (*bsp)->next;
4419 if (b == NULL)
4420 return -1; /* breakpoint that's been deleted since */
4421
4422 *num = b->number; /* We have its number */
4423 return 1;
4424 }
4425
4426 /* See breakpoint.h. */
4427
4428 void
4429 bpstat_clear_actions (void)
4430 {
4431 bpstat *bs;
4432
4433 if (inferior_ptid == null_ptid)
4434 return;
4435
4436 thread_info *tp = inferior_thread ();
4437 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4438 {
4439 bs->commands = NULL;
4440 bs->old_val.reset (nullptr);
4441 }
4442 }
4443
4444 /* Called when a command is about to proceed the inferior. */
4445
4446 static void
4447 breakpoint_about_to_proceed (void)
4448 {
4449 if (inferior_ptid != null_ptid)
4450 {
4451 struct thread_info *tp = inferior_thread ();
4452
4453 /* Allow inferior function calls in breakpoint commands to not
4454 interrupt the command list. When the call finishes
4455 successfully, the inferior will be standing at the same
4456 breakpoint as if nothing happened. */
4457 if (tp->control.in_infcall)
4458 return;
4459 }
4460
4461 breakpoint_proceeded = 1;
4462 }
4463
4464 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4465 or its equivalent. */
4466
4467 static int
4468 command_line_is_silent (struct command_line *cmd)
4469 {
4470 return cmd && (strcmp ("silent", cmd->line) == 0);
4471 }
4472
4473 /* Execute all the commands associated with all the breakpoints at
4474 this location. Any of these commands could cause the process to
4475 proceed beyond this point, etc. We look out for such changes by
4476 checking the global "breakpoint_proceeded" after each command.
4477
4478 Returns true if a breakpoint command resumed the inferior. In that
4479 case, it is the caller's responsibility to recall it again with the
4480 bpstat of the current thread. */
4481
4482 static int
4483 bpstat_do_actions_1 (bpstat **bsp)
4484 {
4485 bpstat *bs;
4486 int again = 0;
4487
4488 /* Avoid endless recursion if a `source' command is contained
4489 in bs->commands. */
4490 if (executing_breakpoint_commands)
4491 return 0;
4492
4493 scoped_restore save_executing
4494 = make_scoped_restore (&executing_breakpoint_commands, 1);
4495
4496 scoped_restore preventer = prevent_dont_repeat ();
4497
4498 /* This pointer will iterate over the list of bpstat's. */
4499 bs = *bsp;
4500
4501 breakpoint_proceeded = 0;
4502 for (; bs != NULL; bs = bs->next)
4503 {
4504 struct command_line *cmd = NULL;
4505
4506 /* Take ownership of the BSP's command tree, if it has one.
4507
4508 The command tree could legitimately contain commands like
4509 'step' and 'next', which call clear_proceed_status, which
4510 frees stop_bpstat's command tree. To make sure this doesn't
4511 free the tree we're executing out from under us, we need to
4512 take ownership of the tree ourselves. Since a given bpstat's
4513 commands are only executed once, we don't need to copy it; we
4514 can clear the pointer in the bpstat, and make sure we free
4515 the tree when we're done. */
4516 counted_command_line ccmd = bs->commands;
4517 bs->commands = NULL;
4518 if (ccmd != NULL)
4519 cmd = ccmd.get ();
4520 if (command_line_is_silent (cmd))
4521 {
4522 /* The action has been already done by bpstat_stop_status. */
4523 cmd = cmd->next;
4524 }
4525
4526 while (cmd != NULL)
4527 {
4528 execute_control_command (cmd);
4529
4530 if (breakpoint_proceeded)
4531 break;
4532 else
4533 cmd = cmd->next;
4534 }
4535
4536 if (breakpoint_proceeded)
4537 {
4538 if (current_ui->async)
4539 /* If we are in async mode, then the target might be still
4540 running, not stopped at any breakpoint, so nothing for
4541 us to do here -- just return to the event loop. */
4542 ;
4543 else
4544 /* In sync mode, when execute_control_command returns
4545 we're already standing on the next breakpoint.
4546 Breakpoint commands for that stop were not run, since
4547 execute_command does not run breakpoint commands --
4548 only command_line_handler does, but that one is not
4549 involved in execution of breakpoint commands. So, we
4550 can now execute breakpoint commands. It should be
4551 noted that making execute_command do bpstat actions is
4552 not an option -- in this case we'll have recursive
4553 invocation of bpstat for each breakpoint with a
4554 command, and can easily blow up GDB stack. Instead, we
4555 return true, which will trigger the caller to recall us
4556 with the new stop_bpstat. */
4557 again = 1;
4558 break;
4559 }
4560 }
4561 return again;
4562 }
4563
4564 /* Helper for bpstat_do_actions. Get the current thread, if there's
4565 one, is alive and has execution. Return NULL otherwise. */
4566
4567 static thread_info *
4568 get_bpstat_thread ()
4569 {
4570 if (inferior_ptid == null_ptid || !target_has_execution ())
4571 return NULL;
4572
4573 thread_info *tp = inferior_thread ();
4574 if (tp->state == THREAD_EXITED || tp->executing ())
4575 return NULL;
4576 return tp;
4577 }
4578
4579 void
4580 bpstat_do_actions (void)
4581 {
4582 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4583 thread_info *tp;
4584
4585 /* Do any commands attached to breakpoint we are stopped at. */
4586 while ((tp = get_bpstat_thread ()) != NULL)
4587 {
4588 /* Since in sync mode, bpstat_do_actions may resume the
4589 inferior, and only return when it is stopped at the next
4590 breakpoint, we keep doing breakpoint actions until it returns
4591 false to indicate the inferior was not resumed. */
4592 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4593 break;
4594 }
4595
4596 cleanup_if_error.release ();
4597 }
4598
4599 /* Print out the (old or new) value associated with a watchpoint. */
4600
4601 static void
4602 watchpoint_value_print (struct value *val, struct ui_file *stream)
4603 {
4604 if (val == NULL)
4605 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4606 else
4607 {
4608 struct value_print_options opts;
4609 get_user_print_options (&opts);
4610 value_print (val, stream, &opts);
4611 }
4612 }
4613
4614 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4615 debugging multiple threads. */
4616
4617 void
4618 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4619 {
4620 if (uiout->is_mi_like_p ())
4621 return;
4622
4623 uiout->text ("\n");
4624
4625 if (show_thread_that_caused_stop ())
4626 {
4627 struct thread_info *thr = inferior_thread ();
4628
4629 uiout->text ("Thread ");
4630 uiout->field_string ("thread-id", print_thread_id (thr));
4631
4632 const char *name = thread_name (thr);
4633 if (name != NULL)
4634 {
4635 uiout->text (" \"");
4636 uiout->field_string ("name", name);
4637 uiout->text ("\"");
4638 }
4639
4640 uiout->text (" hit ");
4641 }
4642 }
4643
4644 /* Generic routine for printing messages indicating why we
4645 stopped. The behavior of this function depends on the value
4646 'print_it' in the bpstat structure. Under some circumstances we
4647 may decide not to print anything here and delegate the task to
4648 normal_stop(). */
4649
4650 static enum print_stop_action
4651 print_bp_stop_message (bpstat *bs)
4652 {
4653 switch (bs->print_it)
4654 {
4655 case print_it_noop:
4656 /* Nothing should be printed for this bpstat entry. */
4657 return PRINT_UNKNOWN;
4658 break;
4659
4660 case print_it_done:
4661 /* We still want to print the frame, but we already printed the
4662 relevant messages. */
4663 return PRINT_SRC_AND_LOC;
4664 break;
4665
4666 case print_it_normal:
4667 {
4668 struct breakpoint *b = bs->breakpoint_at;
4669
4670 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4671 which has since been deleted. */
4672 if (b == NULL)
4673 return PRINT_UNKNOWN;
4674
4675 /* Normal case. Call the breakpoint's print_it method. */
4676 return b->ops->print_it (bs);
4677 }
4678 break;
4679
4680 default:
4681 internal_error (__FILE__, __LINE__,
4682 _("print_bp_stop_message: unrecognized enum value"));
4683 break;
4684 }
4685 }
4686
4687 /* See breakpoint.h. */
4688
4689 void
4690 print_solib_event (bool is_catchpoint)
4691 {
4692 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4693 bool any_added = !current_program_space->added_solibs.empty ();
4694
4695 if (!is_catchpoint)
4696 {
4697 if (any_added || any_deleted)
4698 current_uiout->text (_("Stopped due to shared library event:\n"));
4699 else
4700 current_uiout->text (_("Stopped due to shared library event (no "
4701 "libraries added or removed)\n"));
4702 }
4703
4704 if (current_uiout->is_mi_like_p ())
4705 current_uiout->field_string ("reason",
4706 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4707
4708 if (any_deleted)
4709 {
4710 current_uiout->text (_(" Inferior unloaded "));
4711 ui_out_emit_list list_emitter (current_uiout, "removed");
4712 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4713 {
4714 const std::string &name = current_program_space->deleted_solibs[ix];
4715
4716 if (ix > 0)
4717 current_uiout->text (" ");
4718 current_uiout->field_string ("library", name);
4719 current_uiout->text ("\n");
4720 }
4721 }
4722
4723 if (any_added)
4724 {
4725 current_uiout->text (_(" Inferior loaded "));
4726 ui_out_emit_list list_emitter (current_uiout, "added");
4727 bool first = true;
4728 for (so_list *iter : current_program_space->added_solibs)
4729 {
4730 if (!first)
4731 current_uiout->text (" ");
4732 first = false;
4733 current_uiout->field_string ("library", iter->so_name);
4734 current_uiout->text ("\n");
4735 }
4736 }
4737 }
4738
4739 /* Print a message indicating what happened. This is called from
4740 normal_stop(). The input to this routine is the head of the bpstat
4741 list - a list of the eventpoints that caused this stop. KIND is
4742 the target_waitkind for the stopping event. This
4743 routine calls the generic print routine for printing a message
4744 about reasons for stopping. This will print (for example) the
4745 "Breakpoint n," part of the output. The return value of this
4746 routine is one of:
4747
4748 PRINT_UNKNOWN: Means we printed nothing.
4749 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4750 code to print the location. An example is
4751 "Breakpoint 1, " which should be followed by
4752 the location.
4753 PRINT_SRC_ONLY: Means we printed something, but there is no need
4754 to also print the location part of the message.
4755 An example is the catch/throw messages, which
4756 don't require a location appended to the end.
4757 PRINT_NOTHING: We have done some printing and we don't need any
4758 further info to be printed. */
4759
4760 enum print_stop_action
4761 bpstat_print (bpstat *bs, int kind)
4762 {
4763 enum print_stop_action val;
4764
4765 /* Maybe another breakpoint in the chain caused us to stop.
4766 (Currently all watchpoints go on the bpstat whether hit or not.
4767 That probably could (should) be changed, provided care is taken
4768 with respect to bpstat_explains_signal). */
4769 for (; bs; bs = bs->next)
4770 {
4771 val = print_bp_stop_message (bs);
4772 if (val == PRINT_SRC_ONLY
4773 || val == PRINT_SRC_AND_LOC
4774 || val == PRINT_NOTHING)
4775 return val;
4776 }
4777
4778 /* If we had hit a shared library event breakpoint,
4779 print_bp_stop_message would print out this message. If we hit an
4780 OS-level shared library event, do the same thing. */
4781 if (kind == TARGET_WAITKIND_LOADED)
4782 {
4783 print_solib_event (false);
4784 return PRINT_NOTHING;
4785 }
4786
4787 /* We reached the end of the chain, or we got a null BS to start
4788 with and nothing was printed. */
4789 return PRINT_UNKNOWN;
4790 }
4791
4792 /* Evaluate the boolean expression EXP and return the result. */
4793
4794 static bool
4795 breakpoint_cond_eval (expression *exp)
4796 {
4797 struct value *mark = value_mark ();
4798 bool res = value_true (evaluate_expression (exp));
4799
4800 value_free_to_mark (mark);
4801 return res;
4802 }
4803
4804 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4805
4806 bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
4807 : next (NULL),
4808 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4809 breakpoint_at (bl->owner),
4810 commands (NULL),
4811 print (0),
4812 stop (0),
4813 print_it (print_it_normal)
4814 {
4815 **bs_link_pointer = this;
4816 *bs_link_pointer = &next;
4817 }
4818
4819 bpstat::bpstat ()
4820 : next (NULL),
4821 breakpoint_at (NULL),
4822 commands (NULL),
4823 print (0),
4824 stop (0),
4825 print_it (print_it_normal)
4826 {
4827 }
4828 \f
4829 /* The target has stopped with waitstatus WS. Check if any hardware
4830 watchpoints have triggered, according to the target. */
4831
4832 int
4833 watchpoints_triggered (const target_waitstatus &ws)
4834 {
4835 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4836 CORE_ADDR addr;
4837
4838 if (!stopped_by_watchpoint)
4839 {
4840 /* We were not stopped by a watchpoint. Mark all watchpoints
4841 as not triggered. */
4842 for (breakpoint *b : all_breakpoints ())
4843 if (is_hardware_watchpoint (b))
4844 {
4845 struct watchpoint *w = (struct watchpoint *) b;
4846
4847 w->watchpoint_triggered = watch_triggered_no;
4848 }
4849
4850 return 0;
4851 }
4852
4853 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4854 {
4855 /* We were stopped by a watchpoint, but we don't know where.
4856 Mark all watchpoints as unknown. */
4857 for (breakpoint *b : all_breakpoints ())
4858 if (is_hardware_watchpoint (b))
4859 {
4860 struct watchpoint *w = (struct watchpoint *) b;
4861
4862 w->watchpoint_triggered = watch_triggered_unknown;
4863 }
4864
4865 return 1;
4866 }
4867
4868 /* The target could report the data address. Mark watchpoints
4869 affected by this data address as triggered, and all others as not
4870 triggered. */
4871
4872 for (breakpoint *b : all_breakpoints ())
4873 if (is_hardware_watchpoint (b))
4874 {
4875 struct watchpoint *w = (struct watchpoint *) b;
4876
4877 w->watchpoint_triggered = watch_triggered_no;
4878 for (bp_location *loc : b->locations ())
4879 {
4880 if (is_masked_watchpoint (b))
4881 {
4882 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4883 CORE_ADDR start = loc->address & w->hw_wp_mask;
4884
4885 if (newaddr == start)
4886 {
4887 w->watchpoint_triggered = watch_triggered_yes;
4888 break;
4889 }
4890 }
4891 /* Exact match not required. Within range is sufficient. */
4892 else if (target_watchpoint_addr_within_range
4893 (current_inferior ()->top_target (), addr, loc->address,
4894 loc->length))
4895 {
4896 w->watchpoint_triggered = watch_triggered_yes;
4897 break;
4898 }
4899 }
4900 }
4901
4902 return 1;
4903 }
4904
4905 /* Possible return values for watchpoint_check. */
4906 enum wp_check_result
4907 {
4908 /* The watchpoint has been deleted. */
4909 WP_DELETED = 1,
4910
4911 /* The value has changed. */
4912 WP_VALUE_CHANGED = 2,
4913
4914 /* The value has not changed. */
4915 WP_VALUE_NOT_CHANGED = 3,
4916
4917 /* Ignore this watchpoint, no matter if the value changed or not. */
4918 WP_IGNORE = 4,
4919 };
4920
4921 #define BP_TEMPFLAG 1
4922 #define BP_HARDWAREFLAG 2
4923
4924 /* Evaluate watchpoint condition expression and check if its value
4925 changed. */
4926
4927 static wp_check_result
4928 watchpoint_check (bpstat *bs)
4929 {
4930 struct watchpoint *b;
4931 struct frame_info *fr;
4932 int within_current_scope;
4933
4934 /* BS is built from an existing struct breakpoint. */
4935 gdb_assert (bs->breakpoint_at != NULL);
4936 b = (struct watchpoint *) bs->breakpoint_at;
4937
4938 /* If this is a local watchpoint, we only want to check if the
4939 watchpoint frame is in scope if the current thread is the thread
4940 that was used to create the watchpoint. */
4941 if (!watchpoint_in_thread_scope (b))
4942 return WP_IGNORE;
4943
4944 if (b->exp_valid_block == NULL)
4945 within_current_scope = 1;
4946 else
4947 {
4948 struct frame_info *frame = get_current_frame ();
4949 struct gdbarch *frame_arch = get_frame_arch (frame);
4950 CORE_ADDR frame_pc = get_frame_pc (frame);
4951
4952 /* stack_frame_destroyed_p() returns a non-zero value if we're
4953 still in the function but the stack frame has already been
4954 invalidated. Since we can't rely on the values of local
4955 variables after the stack has been destroyed, we are treating
4956 the watchpoint in that state as `not changed' without further
4957 checking. Don't mark watchpoints as changed if the current
4958 frame is in an epilogue - even if they are in some other
4959 frame, our view of the stack is likely to be wrong and
4960 frame_find_by_id could error out. */
4961 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4962 return WP_IGNORE;
4963
4964 fr = frame_find_by_id (b->watchpoint_frame);
4965 within_current_scope = (fr != NULL);
4966
4967 /* If we've gotten confused in the unwinder, we might have
4968 returned a frame that can't describe this variable. */
4969 if (within_current_scope)
4970 {
4971 struct symbol *function;
4972
4973 function = get_frame_function (fr);
4974 if (function == NULL
4975 || !contained_in (b->exp_valid_block, function->value_block ()))
4976 within_current_scope = 0;
4977 }
4978
4979 if (within_current_scope)
4980 /* If we end up stopping, the current frame will get selected
4981 in normal_stop. So this call to select_frame won't affect
4982 the user. */
4983 select_frame (fr);
4984 }
4985
4986 if (within_current_scope)
4987 {
4988 /* We use value_{,free_to_}mark because it could be a *long*
4989 time before we return to the command level and call
4990 free_all_values. We can't call free_all_values because we
4991 might be in the middle of evaluating a function call. */
4992
4993 struct value *mark;
4994 struct value *new_val;
4995
4996 if (is_masked_watchpoint (b))
4997 /* Since we don't know the exact trigger address (from
4998 stopped_data_address), just tell the user we've triggered
4999 a mask watchpoint. */
5000 return WP_VALUE_CHANGED;
5001
5002 mark = value_mark ();
5003 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5004 NULL, NULL, false);
5005
5006 if (b->val_bitsize != 0)
5007 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5008
5009 /* We use value_equal_contents instead of value_equal because
5010 the latter coerces an array to a pointer, thus comparing just
5011 the address of the array instead of its contents. This is
5012 not what we want. */
5013 if ((b->val != NULL) != (new_val != NULL)
5014 || (b->val != NULL && !value_equal_contents (b->val.get (),
5015 new_val)))
5016 {
5017 bs->old_val = b->val;
5018 b->val = release_value (new_val);
5019 b->val_valid = true;
5020 if (new_val != NULL)
5021 value_free_to_mark (mark);
5022 return WP_VALUE_CHANGED;
5023 }
5024 else
5025 {
5026 /* Nothing changed. */
5027 value_free_to_mark (mark);
5028 return WP_VALUE_NOT_CHANGED;
5029 }
5030 }
5031 else
5032 {
5033 /* This seems like the only logical thing to do because
5034 if we temporarily ignored the watchpoint, then when
5035 we reenter the block in which it is valid it contains
5036 garbage (in the case of a function, it may have two
5037 garbage values, one before and one after the prologue).
5038 So we can't even detect the first assignment to it and
5039 watch after that (since the garbage may or may not equal
5040 the first value assigned). */
5041 /* We print all the stop information in
5042 breakpoint_ops->print_it, but in this case, by the time we
5043 call breakpoint_ops->print_it this bp will be deleted
5044 already. So we have no choice but print the information
5045 here. */
5046
5047 SWITCH_THRU_ALL_UIS ()
5048 {
5049 struct ui_out *uiout = current_uiout;
5050
5051 if (uiout->is_mi_like_p ())
5052 uiout->field_string
5053 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5054 uiout->message ("\nWatchpoint %pF deleted because the program has "
5055 "left the block in\n"
5056 "which its expression is valid.\n",
5057 signed_field ("wpnum", b->number));
5058 }
5059
5060 /* Make sure the watchpoint's commands aren't executed. */
5061 b->commands = NULL;
5062 watchpoint_del_at_next_stop (b);
5063
5064 return WP_DELETED;
5065 }
5066 }
5067
5068 /* Return true if it looks like target has stopped due to hitting
5069 breakpoint location BL. This function does not check if we should
5070 stop, only if BL explains the stop. */
5071
5072 static int
5073 bpstat_check_location (const struct bp_location *bl,
5074 const address_space *aspace, CORE_ADDR bp_addr,
5075 const target_waitstatus &ws)
5076 {
5077 struct breakpoint *b = bl->owner;
5078
5079 /* BL is from an existing breakpoint. */
5080 gdb_assert (b != NULL);
5081
5082 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5083 }
5084
5085 /* Determine if the watched values have actually changed, and we
5086 should stop. If not, set BS->stop to 0. */
5087
5088 static void
5089 bpstat_check_watchpoint (bpstat *bs)
5090 {
5091 const struct bp_location *bl;
5092 struct watchpoint *b;
5093
5094 /* BS is built for existing struct breakpoint. */
5095 bl = bs->bp_location_at.get ();
5096 gdb_assert (bl != NULL);
5097 b = (struct watchpoint *) bs->breakpoint_at;
5098 gdb_assert (b != NULL);
5099
5100 {
5101 int must_check_value = 0;
5102
5103 if (b->type == bp_watchpoint)
5104 /* For a software watchpoint, we must always check the
5105 watched value. */
5106 must_check_value = 1;
5107 else if (b->watchpoint_triggered == watch_triggered_yes)
5108 /* We have a hardware watchpoint (read, write, or access)
5109 and the target earlier reported an address watched by
5110 this watchpoint. */
5111 must_check_value = 1;
5112 else if (b->watchpoint_triggered == watch_triggered_unknown
5113 && b->type == bp_hardware_watchpoint)
5114 /* We were stopped by a hardware watchpoint, but the target could
5115 not report the data address. We must check the watchpoint's
5116 value. Access and read watchpoints are out of luck; without
5117 a data address, we can't figure it out. */
5118 must_check_value = 1;
5119
5120 if (must_check_value)
5121 {
5122 wp_check_result e;
5123
5124 try
5125 {
5126 e = watchpoint_check (bs);
5127 }
5128 catch (const gdb_exception &ex)
5129 {
5130 exception_fprintf (gdb_stderr, ex,
5131 "Error evaluating expression "
5132 "for watchpoint %d\n",
5133 b->number);
5134
5135 SWITCH_THRU_ALL_UIS ()
5136 {
5137 gdb_printf (_("Watchpoint %d deleted.\n"),
5138 b->number);
5139 }
5140 watchpoint_del_at_next_stop (b);
5141 e = WP_DELETED;
5142 }
5143
5144 switch (e)
5145 {
5146 case WP_DELETED:
5147 /* We've already printed what needs to be printed. */
5148 bs->print_it = print_it_done;
5149 /* Stop. */
5150 break;
5151 case WP_IGNORE:
5152 bs->print_it = print_it_noop;
5153 bs->stop = 0;
5154 break;
5155 case WP_VALUE_CHANGED:
5156 if (b->type == bp_read_watchpoint)
5157 {
5158 /* There are two cases to consider here:
5159
5160 1. We're watching the triggered memory for reads.
5161 In that case, trust the target, and always report
5162 the watchpoint hit to the user. Even though
5163 reads don't cause value changes, the value may
5164 have changed since the last time it was read, and
5165 since we're not trapping writes, we will not see
5166 those, and as such we should ignore our notion of
5167 old value.
5168
5169 2. We're watching the triggered memory for both
5170 reads and writes. There are two ways this may
5171 happen:
5172
5173 2.1. This is a target that can't break on data
5174 reads only, but can break on accesses (reads or
5175 writes), such as e.g., x86. We detect this case
5176 at the time we try to insert read watchpoints.
5177
5178 2.2. Otherwise, the target supports read
5179 watchpoints, but, the user set an access or write
5180 watchpoint watching the same memory as this read
5181 watchpoint.
5182
5183 If we're watching memory writes as well as reads,
5184 ignore watchpoint hits when we find that the
5185 value hasn't changed, as reads don't cause
5186 changes. This still gives false positives when
5187 the program writes the same value to memory as
5188 what there was already in memory (we will confuse
5189 it for a read), but it's much better than
5190 nothing. */
5191
5192 int other_write_watchpoint = 0;
5193
5194 if (bl->watchpoint_type == hw_read)
5195 {
5196 for (breakpoint *other_b : all_breakpoints ())
5197 if (other_b->type == bp_hardware_watchpoint
5198 || other_b->type == bp_access_watchpoint)
5199 {
5200 struct watchpoint *other_w =
5201 (struct watchpoint *) other_b;
5202
5203 if (other_w->watchpoint_triggered
5204 == watch_triggered_yes)
5205 {
5206 other_write_watchpoint = 1;
5207 break;
5208 }
5209 }
5210 }
5211
5212 if (other_write_watchpoint
5213 || bl->watchpoint_type == hw_access)
5214 {
5215 /* We're watching the same memory for writes,
5216 and the value changed since the last time we
5217 updated it, so this trap must be for a write.
5218 Ignore it. */
5219 bs->print_it = print_it_noop;
5220 bs->stop = 0;
5221 }
5222 }
5223 break;
5224 case WP_VALUE_NOT_CHANGED:
5225 if (b->type == bp_hardware_watchpoint
5226 || b->type == bp_watchpoint)
5227 {
5228 /* Don't stop: write watchpoints shouldn't fire if
5229 the value hasn't changed. */
5230 bs->print_it = print_it_noop;
5231 bs->stop = 0;
5232 }
5233 /* Stop. */
5234 break;
5235 default:
5236 /* Can't happen. */
5237 break;
5238 }
5239 }
5240 else /* must_check_value == 0 */
5241 {
5242 /* This is a case where some watchpoint(s) triggered, but
5243 not at the address of this watchpoint, or else no
5244 watchpoint triggered after all. So don't print
5245 anything for this watchpoint. */
5246 bs->print_it = print_it_noop;
5247 bs->stop = 0;
5248 }
5249 }
5250 }
5251
5252 /* For breakpoints that are currently marked as telling gdb to stop,
5253 check conditions (condition proper, frame, thread and ignore count)
5254 of breakpoint referred to by BS. If we should not stop for this
5255 breakpoint, set BS->stop to 0. */
5256
5257 static void
5258 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5259 {
5260 const struct bp_location *bl;
5261 struct breakpoint *b;
5262 /* Assume stop. */
5263 bool condition_result = true;
5264 struct expression *cond;
5265
5266 gdb_assert (bs->stop);
5267
5268 /* BS is built for existing struct breakpoint. */
5269 bl = bs->bp_location_at.get ();
5270 gdb_assert (bl != NULL);
5271 b = bs->breakpoint_at;
5272 gdb_assert (b != NULL);
5273
5274 /* Even if the target evaluated the condition on its end and notified GDB, we
5275 need to do so again since GDB does not know if we stopped due to a
5276 breakpoint or a single step breakpoint. */
5277
5278 if (frame_id_p (b->frame_id)
5279 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5280 {
5281 bs->stop = 0;
5282 return;
5283 }
5284
5285 /* If this is a thread/task-specific breakpoint, don't waste cpu
5286 evaluating the condition if this isn't the specified
5287 thread/task. */
5288 if ((b->thread != -1 && b->thread != thread->global_num)
5289 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5290 {
5291 bs->stop = 0;
5292 return;
5293 }
5294
5295 /* Evaluate extension language breakpoints that have a "stop" method
5296 implemented. */
5297 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5298
5299 if (is_watchpoint (b))
5300 {
5301 struct watchpoint *w = (struct watchpoint *) b;
5302
5303 cond = w->cond_exp.get ();
5304 }
5305 else
5306 cond = bl->cond.get ();
5307
5308 if (cond && b->disposition != disp_del_at_next_stop)
5309 {
5310 int within_current_scope = 1;
5311 struct watchpoint * w;
5312
5313 /* We use value_mark and value_free_to_mark because it could
5314 be a long time before we return to the command level and
5315 call free_all_values. We can't call free_all_values
5316 because we might be in the middle of evaluating a
5317 function call. */
5318 struct value *mark = value_mark ();
5319
5320 if (is_watchpoint (b))
5321 w = (struct watchpoint *) b;
5322 else
5323 w = NULL;
5324
5325 /* Need to select the frame, with all that implies so that
5326 the conditions will have the right context. Because we
5327 use the frame, we will not see an inlined function's
5328 variables when we arrive at a breakpoint at the start
5329 of the inlined function; the current frame will be the
5330 call site. */
5331 if (w == NULL || w->cond_exp_valid_block == NULL)
5332 select_frame (get_current_frame ());
5333 else
5334 {
5335 struct frame_info *frame;
5336
5337 /* For local watchpoint expressions, which particular
5338 instance of a local is being watched matters, so we
5339 keep track of the frame to evaluate the expression
5340 in. To evaluate the condition however, it doesn't
5341 really matter which instantiation of the function
5342 where the condition makes sense triggers the
5343 watchpoint. This allows an expression like "watch
5344 global if q > 10" set in `func', catch writes to
5345 global on all threads that call `func', or catch
5346 writes on all recursive calls of `func' by a single
5347 thread. We simply always evaluate the condition in
5348 the innermost frame that's executing where it makes
5349 sense to evaluate the condition. It seems
5350 intuitive. */
5351 frame = block_innermost_frame (w->cond_exp_valid_block);
5352 if (frame != NULL)
5353 select_frame (frame);
5354 else
5355 within_current_scope = 0;
5356 }
5357 if (within_current_scope)
5358 {
5359 try
5360 {
5361 condition_result = breakpoint_cond_eval (cond);
5362 }
5363 catch (const gdb_exception &ex)
5364 {
5365 exception_fprintf (gdb_stderr, ex,
5366 "Error in testing breakpoint condition:\n");
5367 }
5368 }
5369 else
5370 {
5371 warning (_("Watchpoint condition cannot be tested "
5372 "in the current scope"));
5373 /* If we failed to set the right context for this
5374 watchpoint, unconditionally report it. */
5375 }
5376 /* FIXME-someday, should give breakpoint #. */
5377 value_free_to_mark (mark);
5378 }
5379
5380 if (cond && !condition_result)
5381 {
5382 bs->stop = 0;
5383 }
5384 else if (b->ignore_count > 0)
5385 {
5386 b->ignore_count--;
5387 bs->stop = 0;
5388 /* Increase the hit count even though we don't stop. */
5389 ++(b->hit_count);
5390 gdb::observers::breakpoint_modified.notify (b);
5391 }
5392 }
5393
5394 /* Returns true if we need to track moribund locations of LOC's type
5395 on the current target. */
5396
5397 static int
5398 need_moribund_for_location_type (struct bp_location *loc)
5399 {
5400 return ((loc->loc_type == bp_loc_software_breakpoint
5401 && !target_supports_stopped_by_sw_breakpoint ())
5402 || (loc->loc_type == bp_loc_hardware_breakpoint
5403 && !target_supports_stopped_by_hw_breakpoint ()));
5404 }
5405
5406 /* See breakpoint.h. */
5407
5408 bpstat *
5409 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5410 const target_waitstatus &ws)
5411 {
5412 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5413
5414 for (breakpoint *b : all_breakpoints ())
5415 {
5416 if (!breakpoint_enabled (b))
5417 continue;
5418
5419 for (bp_location *bl : b->locations ())
5420 {
5421 /* For hardware watchpoints, we look only at the first
5422 location. The watchpoint_check function will work on the
5423 entire expression, not the individual locations. For
5424 read watchpoints, the watchpoints_triggered function has
5425 checked all locations already. */
5426 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5427 break;
5428
5429 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5430 continue;
5431
5432 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5433 continue;
5434
5435 /* Come here if it's a watchpoint, or if the break address
5436 matches. */
5437
5438 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
5439 explain stop. */
5440
5441 /* Assume we stop. Should we find a watchpoint that is not
5442 actually triggered, or if the condition of the breakpoint
5443 evaluates as false, we'll reset 'stop' to 0. */
5444 bs->stop = 1;
5445 bs->print = 1;
5446
5447 /* If this is a scope breakpoint, mark the associated
5448 watchpoint as triggered so that we will handle the
5449 out-of-scope event. We'll get to the watchpoint next
5450 iteration. */
5451 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5452 {
5453 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5454
5455 w->watchpoint_triggered = watch_triggered_yes;
5456 }
5457 }
5458 }
5459
5460 /* Check if a moribund breakpoint explains the stop. */
5461 if (!target_supports_stopped_by_sw_breakpoint ()
5462 || !target_supports_stopped_by_hw_breakpoint ())
5463 {
5464 for (bp_location *loc : moribund_locations)
5465 {
5466 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5467 && need_moribund_for_location_type (loc))
5468 {
5469 bpstat *bs = new bpstat (loc, &bs_link);
5470 /* For hits of moribund locations, we should just proceed. */
5471 bs->stop = 0;
5472 bs->print = 0;
5473 bs->print_it = print_it_noop;
5474 }
5475 }
5476 }
5477
5478 return bs_head;
5479 }
5480
5481 /* See breakpoint.h. */
5482
5483 bpstat *
5484 bpstat_stop_status (const address_space *aspace,
5485 CORE_ADDR bp_addr, thread_info *thread,
5486 const target_waitstatus &ws,
5487 bpstat *stop_chain)
5488 {
5489 struct breakpoint *b = NULL;
5490 /* First item of allocated bpstat's. */
5491 bpstat *bs_head = stop_chain;
5492 bpstat *bs;
5493 int need_remove_insert;
5494 int removed_any;
5495
5496 /* First, build the bpstat chain with locations that explain a
5497 target stop, while being careful to not set the target running,
5498 as that may invalidate locations (in particular watchpoint
5499 locations are recreated). Resuming will happen here with
5500 breakpoint conditions or watchpoint expressions that include
5501 inferior function calls. */
5502 if (bs_head == NULL)
5503 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5504
5505 /* A bit of special processing for shlib breakpoints. We need to
5506 process solib loading here, so that the lists of loaded and
5507 unloaded libraries are correct before we handle "catch load" and
5508 "catch unload". */
5509 for (bs = bs_head; bs != NULL; bs = bs->next)
5510 {
5511 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5512 {
5513 handle_solib_event ();
5514 break;
5515 }
5516 }
5517
5518 /* Now go through the locations that caused the target to stop, and
5519 check whether we're interested in reporting this stop to higher
5520 layers, or whether we should resume the target transparently. */
5521
5522 removed_any = 0;
5523
5524 for (bs = bs_head; bs != NULL; bs = bs->next)
5525 {
5526 if (!bs->stop)
5527 continue;
5528
5529 b = bs->breakpoint_at;
5530 b->ops->check_status (bs);
5531 if (bs->stop)
5532 {
5533 bpstat_check_breakpoint_conditions (bs, thread);
5534
5535 if (bs->stop)
5536 {
5537 ++(b->hit_count);
5538
5539 /* We will stop here. */
5540 if (b->disposition == disp_disable)
5541 {
5542 --(b->enable_count);
5543 if (b->enable_count <= 0)
5544 b->enable_state = bp_disabled;
5545 removed_any = 1;
5546 }
5547 gdb::observers::breakpoint_modified.notify (b);
5548 if (b->silent)
5549 bs->print = 0;
5550 bs->commands = b->commands;
5551 if (command_line_is_silent (bs->commands
5552 ? bs->commands.get () : NULL))
5553 bs->print = 0;
5554
5555 b->ops->after_condition_true (bs);
5556 }
5557
5558 }
5559
5560 /* Print nothing for this entry if we don't stop or don't
5561 print. */
5562 if (!bs->stop || !bs->print)
5563 bs->print_it = print_it_noop;
5564 }
5565
5566 /* If we aren't stopping, the value of some hardware watchpoint may
5567 not have changed, but the intermediate memory locations we are
5568 watching may have. Don't bother if we're stopping; this will get
5569 done later. */
5570 need_remove_insert = 0;
5571 if (! bpstat_causes_stop (bs_head))
5572 for (bs = bs_head; bs != NULL; bs = bs->next)
5573 if (!bs->stop
5574 && bs->breakpoint_at
5575 && is_hardware_watchpoint (bs->breakpoint_at))
5576 {
5577 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5578
5579 update_watchpoint (w, 0 /* don't reparse. */);
5580 need_remove_insert = 1;
5581 }
5582
5583 if (need_remove_insert)
5584 update_global_location_list (UGLL_MAY_INSERT);
5585 else if (removed_any)
5586 update_global_location_list (UGLL_DONT_INSERT);
5587
5588 return bs_head;
5589 }
5590
5591 /* See breakpoint.h. */
5592
5593 bpstat *
5594 bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5595 thread_info *thread, const target_waitstatus &ws)
5596 {
5597 gdb_assert (!target_stopped_by_watchpoint ());
5598
5599 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5600 previous stop to avoid confusing bpstat_stop_status. */
5601 watchpoints_triggered (ws);
5602
5603 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5604 }
5605
5606 static void
5607 handle_jit_event (CORE_ADDR address)
5608 {
5609 struct gdbarch *gdbarch;
5610
5611 infrun_debug_printf ("handling bp_jit_event");
5612
5613 /* Switch terminal for any messages produced by
5614 breakpoint_re_set. */
5615 target_terminal::ours_for_output ();
5616
5617 gdbarch = get_frame_arch (get_current_frame ());
5618 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5619 thus it is expected that its objectfile can be found through
5620 minimal symbol lookup. If it doesn't work (and assert fails), it
5621 most likely means that `jit_breakpoint_re_set` was changes and this
5622 function needs to be updated too. */
5623 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5624 gdb_assert (jit_bp_sym.objfile != nullptr);
5625 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5626
5627 target_terminal::inferior ();
5628 }
5629
5630 /* Prepare WHAT final decision for infrun. */
5631
5632 /* Decide what infrun needs to do with this bpstat. */
5633
5634 struct bpstat_what
5635 bpstat_what (bpstat *bs_head)
5636 {
5637 struct bpstat_what retval;
5638 bpstat *bs;
5639
5640 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5641 retval.call_dummy = STOP_NONE;
5642 retval.is_longjmp = false;
5643
5644 for (bs = bs_head; bs != NULL; bs = bs->next)
5645 {
5646 /* Extract this BS's action. After processing each BS, we check
5647 if its action overrides all we've seem so far. */
5648 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5649 enum bptype bptype;
5650
5651 if (bs->breakpoint_at == NULL)
5652 {
5653 /* I suspect this can happen if it was a momentary
5654 breakpoint which has since been deleted. */
5655 bptype = bp_none;
5656 }
5657 else
5658 bptype = bs->breakpoint_at->type;
5659
5660 switch (bptype)
5661 {
5662 case bp_none:
5663 break;
5664 case bp_breakpoint:
5665 case bp_hardware_breakpoint:
5666 case bp_single_step:
5667 case bp_until:
5668 case bp_finish:
5669 case bp_shlib_event:
5670 if (bs->stop)
5671 {
5672 if (bs->print)
5673 this_action = BPSTAT_WHAT_STOP_NOISY;
5674 else
5675 this_action = BPSTAT_WHAT_STOP_SILENT;
5676 }
5677 else
5678 this_action = BPSTAT_WHAT_SINGLE;
5679 break;
5680 case bp_watchpoint:
5681 case bp_hardware_watchpoint:
5682 case bp_read_watchpoint:
5683 case bp_access_watchpoint:
5684 if (bs->stop)
5685 {
5686 if (bs->print)
5687 this_action = BPSTAT_WHAT_STOP_NOISY;
5688 else
5689 this_action = BPSTAT_WHAT_STOP_SILENT;
5690 }
5691 else
5692 {
5693 /* There was a watchpoint, but we're not stopping.
5694 This requires no further action. */
5695 }
5696 break;
5697 case bp_longjmp:
5698 case bp_longjmp_call_dummy:
5699 case bp_exception:
5700 if (bs->stop)
5701 {
5702 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5703 retval.is_longjmp = bptype != bp_exception;
5704 }
5705 else
5706 this_action = BPSTAT_WHAT_SINGLE;
5707 break;
5708 case bp_longjmp_resume:
5709 case bp_exception_resume:
5710 if (bs->stop)
5711 {
5712 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5713 retval.is_longjmp = bptype == bp_longjmp_resume;
5714 }
5715 else
5716 this_action = BPSTAT_WHAT_SINGLE;
5717 break;
5718 case bp_step_resume:
5719 if (bs->stop)
5720 this_action = BPSTAT_WHAT_STEP_RESUME;
5721 else
5722 {
5723 /* It is for the wrong frame. */
5724 this_action = BPSTAT_WHAT_SINGLE;
5725 }
5726 break;
5727 case bp_hp_step_resume:
5728 if (bs->stop)
5729 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5730 else
5731 {
5732 /* It is for the wrong frame. */
5733 this_action = BPSTAT_WHAT_SINGLE;
5734 }
5735 break;
5736 case bp_watchpoint_scope:
5737 case bp_thread_event:
5738 case bp_overlay_event:
5739 case bp_longjmp_master:
5740 case bp_std_terminate_master:
5741 case bp_exception_master:
5742 this_action = BPSTAT_WHAT_SINGLE;
5743 break;
5744 case bp_catchpoint:
5745 if (bs->stop)
5746 {
5747 if (bs->print)
5748 this_action = BPSTAT_WHAT_STOP_NOISY;
5749 else
5750 this_action = BPSTAT_WHAT_STOP_SILENT;
5751 }
5752 else
5753 {
5754 /* Some catchpoints are implemented with breakpoints.
5755 For those, we need to step over the breakpoint. */
5756 if (bs->bp_location_at->loc_type != bp_loc_other)
5757 this_action = BPSTAT_WHAT_SINGLE;
5758 }
5759 break;
5760 case bp_jit_event:
5761 this_action = BPSTAT_WHAT_SINGLE;
5762 break;
5763 case bp_call_dummy:
5764 /* Make sure the action is stop (silent or noisy),
5765 so infrun.c pops the dummy frame. */
5766 retval.call_dummy = STOP_STACK_DUMMY;
5767 this_action = BPSTAT_WHAT_STOP_SILENT;
5768 break;
5769 case bp_std_terminate:
5770 /* Make sure the action is stop (silent or noisy),
5771 so infrun.c pops the dummy frame. */
5772 retval.call_dummy = STOP_STD_TERMINATE;
5773 this_action = BPSTAT_WHAT_STOP_SILENT;
5774 break;
5775 case bp_tracepoint:
5776 case bp_fast_tracepoint:
5777 case bp_static_tracepoint:
5778 /* Tracepoint hits should not be reported back to GDB, and
5779 if one got through somehow, it should have been filtered
5780 out already. */
5781 internal_error (__FILE__, __LINE__,
5782 _("bpstat_what: tracepoint encountered"));
5783 break;
5784 case bp_gnu_ifunc_resolver:
5785 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5786 this_action = BPSTAT_WHAT_SINGLE;
5787 break;
5788 case bp_gnu_ifunc_resolver_return:
5789 /* The breakpoint will be removed, execution will restart from the
5790 PC of the former breakpoint. */
5791 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5792 break;
5793
5794 case bp_dprintf:
5795 if (bs->stop)
5796 this_action = BPSTAT_WHAT_STOP_SILENT;
5797 else
5798 this_action = BPSTAT_WHAT_SINGLE;
5799 break;
5800
5801 default:
5802 internal_error (__FILE__, __LINE__,
5803 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5804 }
5805
5806 retval.main_action = std::max (retval.main_action, this_action);
5807 }
5808
5809 return retval;
5810 }
5811
5812 void
5813 bpstat_run_callbacks (bpstat *bs_head)
5814 {
5815 bpstat *bs;
5816
5817 for (bs = bs_head; bs != NULL; bs = bs->next)
5818 {
5819 struct breakpoint *b = bs->breakpoint_at;
5820
5821 if (b == NULL)
5822 continue;
5823 switch (b->type)
5824 {
5825 case bp_jit_event:
5826 handle_jit_event (bs->bp_location_at->address);
5827 break;
5828 case bp_gnu_ifunc_resolver:
5829 gnu_ifunc_resolver_stop (b);
5830 break;
5831 case bp_gnu_ifunc_resolver_return:
5832 gnu_ifunc_resolver_return_stop (b);
5833 break;
5834 }
5835 }
5836 }
5837
5838 /* See breakpoint.h. */
5839
5840 bool
5841 bpstat_should_step ()
5842 {
5843 for (breakpoint *b : all_breakpoints ())
5844 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5845 return true;
5846
5847 return false;
5848 }
5849
5850 /* See breakpoint.h. */
5851
5852 bool
5853 bpstat_causes_stop (bpstat *bs)
5854 {
5855 for (; bs != NULL; bs = bs->next)
5856 if (bs->stop)
5857 return true;
5858
5859 return false;
5860 }
5861
5862 \f
5863
5864 /* Compute a number of spaces suitable to indent the next line
5865 so it starts at the position corresponding to the table column
5866 named COL_NAME in the currently active table of UIOUT. */
5867
5868 static int
5869 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5870 {
5871 int i, total_width, width, align;
5872 const char *text;
5873
5874 total_width = 0;
5875 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5876 {
5877 if (strcmp (text, col_name) == 0)
5878 return total_width;
5879
5880 total_width += width + 1;
5881 }
5882
5883 return 0;
5884 }
5885
5886 /* Determine if the locations of this breakpoint will have their conditions
5887 evaluated by the target, host or a mix of both. Returns the following:
5888
5889 "host": Host evals condition.
5890 "host or target": Host or Target evals condition.
5891 "target": Target evals condition.
5892 */
5893
5894 static const char *
5895 bp_condition_evaluator (struct breakpoint *b)
5896 {
5897 char host_evals = 0;
5898 char target_evals = 0;
5899
5900 if (!b)
5901 return NULL;
5902
5903 if (!is_breakpoint (b))
5904 return NULL;
5905
5906 if (gdb_evaluates_breakpoint_condition_p ()
5907 || !target_supports_evaluation_of_breakpoint_conditions ())
5908 return condition_evaluation_host;
5909
5910 for (bp_location *bl : b->locations ())
5911 {
5912 if (bl->cond_bytecode)
5913 target_evals++;
5914 else
5915 host_evals++;
5916 }
5917
5918 if (host_evals && target_evals)
5919 return condition_evaluation_both;
5920 else if (target_evals)
5921 return condition_evaluation_target;
5922 else
5923 return condition_evaluation_host;
5924 }
5925
5926 /* Determine the breakpoint location's condition evaluator. This is
5927 similar to bp_condition_evaluator, but for locations. */
5928
5929 static const char *
5930 bp_location_condition_evaluator (struct bp_location *bl)
5931 {
5932 if (bl && !is_breakpoint (bl->owner))
5933 return NULL;
5934
5935 if (gdb_evaluates_breakpoint_condition_p ()
5936 || !target_supports_evaluation_of_breakpoint_conditions ())
5937 return condition_evaluation_host;
5938
5939 if (bl && bl->cond_bytecode)
5940 return condition_evaluation_target;
5941 else
5942 return condition_evaluation_host;
5943 }
5944
5945 /* Print the LOC location out of the list of B->LOC locations. */
5946
5947 static void
5948 print_breakpoint_location (struct breakpoint *b,
5949 struct bp_location *loc)
5950 {
5951 struct ui_out *uiout = current_uiout;
5952
5953 scoped_restore_current_program_space restore_pspace;
5954
5955 if (loc != NULL && loc->shlib_disabled)
5956 loc = NULL;
5957
5958 if (loc != NULL)
5959 set_current_program_space (loc->pspace);
5960
5961 if (b->display_canonical)
5962 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5963 else if (loc && loc->symtab)
5964 {
5965 const struct symbol *sym = loc->symbol;
5966
5967 if (sym)
5968 {
5969 uiout->text ("in ");
5970 uiout->field_string ("func", sym->print_name (),
5971 function_name_style.style ());
5972 uiout->text (" ");
5973 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5974 uiout->text ("at ");
5975 }
5976 uiout->field_string ("file",
5977 symtab_to_filename_for_display (loc->symtab),
5978 file_name_style.style ());
5979 uiout->text (":");
5980
5981 if (uiout->is_mi_like_p ())
5982 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5983
5984 uiout->field_signed ("line", loc->line_number);
5985 }
5986 else if (loc)
5987 {
5988 string_file stb;
5989
5990 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5991 demangle, "");
5992 uiout->field_stream ("at", stb);
5993 }
5994 else
5995 {
5996 uiout->field_string ("pending",
5997 event_location_to_string (b->location.get ()));
5998 /* If extra_string is available, it could be holding a condition
5999 or dprintf arguments. In either case, make sure it is printed,
6000 too, but only for non-MI streams. */
6001 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6002 {
6003 if (b->type == bp_dprintf)
6004 uiout->text (",");
6005 else
6006 uiout->text (" ");
6007 uiout->text (b->extra_string.get ());
6008 }
6009 }
6010
6011 if (loc && is_breakpoint (b)
6012 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6013 && bp_condition_evaluator (b) == condition_evaluation_both)
6014 {
6015 uiout->text (" (");
6016 uiout->field_string ("evaluated-by",
6017 bp_location_condition_evaluator (loc));
6018 uiout->text (")");
6019 }
6020 }
6021
6022 static const char *
6023 bptype_string (enum bptype type)
6024 {
6025 struct ep_type_description
6026 {
6027 enum bptype type;
6028 const char *description;
6029 };
6030 static struct ep_type_description bptypes[] =
6031 {
6032 {bp_none, "?deleted?"},
6033 {bp_breakpoint, "breakpoint"},
6034 {bp_hardware_breakpoint, "hw breakpoint"},
6035 {bp_single_step, "sw single-step"},
6036 {bp_until, "until"},
6037 {bp_finish, "finish"},
6038 {bp_watchpoint, "watchpoint"},
6039 {bp_hardware_watchpoint, "hw watchpoint"},
6040 {bp_read_watchpoint, "read watchpoint"},
6041 {bp_access_watchpoint, "acc watchpoint"},
6042 {bp_longjmp, "longjmp"},
6043 {bp_longjmp_resume, "longjmp resume"},
6044 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6045 {bp_exception, "exception"},
6046 {bp_exception_resume, "exception resume"},
6047 {bp_step_resume, "step resume"},
6048 {bp_hp_step_resume, "high-priority step resume"},
6049 {bp_watchpoint_scope, "watchpoint scope"},
6050 {bp_call_dummy, "call dummy"},
6051 {bp_std_terminate, "std::terminate"},
6052 {bp_shlib_event, "shlib events"},
6053 {bp_thread_event, "thread events"},
6054 {bp_overlay_event, "overlay events"},
6055 {bp_longjmp_master, "longjmp master"},
6056 {bp_std_terminate_master, "std::terminate master"},
6057 {bp_exception_master, "exception master"},
6058 {bp_catchpoint, "catchpoint"},
6059 {bp_tracepoint, "tracepoint"},
6060 {bp_fast_tracepoint, "fast tracepoint"},
6061 {bp_static_tracepoint, "static tracepoint"},
6062 {bp_dprintf, "dprintf"},
6063 {bp_jit_event, "jit events"},
6064 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6065 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6066 };
6067
6068 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6069 || ((int) type != bptypes[(int) type].type))
6070 internal_error (__FILE__, __LINE__,
6071 _("bptypes table does not describe type #%d."),
6072 (int) type);
6073
6074 return bptypes[(int) type].description;
6075 }
6076
6077 /* For MI, output a field named 'thread-groups' with a list as the value.
6078 For CLI, prefix the list with the string 'inf'. */
6079
6080 static void
6081 output_thread_groups (struct ui_out *uiout,
6082 const char *field_name,
6083 const std::vector<int> &inf_nums,
6084 int mi_only)
6085 {
6086 int is_mi = uiout->is_mi_like_p ();
6087
6088 /* For backward compatibility, don't display inferiors in CLI unless
6089 there are several. Always display them for MI. */
6090 if (!is_mi && mi_only)
6091 return;
6092
6093 ui_out_emit_list list_emitter (uiout, field_name);
6094
6095 for (size_t i = 0; i < inf_nums.size (); i++)
6096 {
6097 if (is_mi)
6098 {
6099 char mi_group[10];
6100
6101 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6102 uiout->field_string (NULL, mi_group);
6103 }
6104 else
6105 {
6106 if (i == 0)
6107 uiout->text (" inf ");
6108 else
6109 uiout->text (", ");
6110
6111 uiout->text (plongest (inf_nums[i]));
6112 }
6113 }
6114 }
6115
6116 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6117 instead of going via breakpoint_ops::print_one. This makes "maint
6118 info breakpoints" show the software breakpoint locations of
6119 catchpoints, which are considered internal implementation
6120 detail. Returns true if RAW_LOC is false and if the breakpoint's
6121 print_one method did something; false otherwise. */
6122
6123 static bool
6124 print_one_breakpoint_location (struct breakpoint *b,
6125 struct bp_location *loc,
6126 int loc_number,
6127 struct bp_location **last_loc,
6128 int allflag, bool raw_loc)
6129 {
6130 struct command_line *l;
6131 static char bpenables[] = "nynny";
6132
6133 struct ui_out *uiout = current_uiout;
6134 int header_of_multiple = 0;
6135 int part_of_multiple = (loc != NULL);
6136 struct value_print_options opts;
6137
6138 get_user_print_options (&opts);
6139
6140 gdb_assert (!loc || loc_number != 0);
6141 /* See comment in print_one_breakpoint concerning treatment of
6142 breakpoints with single disabled location. */
6143 if (loc == NULL
6144 && (b->loc != NULL
6145 && (b->loc->next != NULL
6146 || !b->loc->enabled || b->loc->disabled_by_cond)))
6147 header_of_multiple = 1;
6148 if (loc == NULL)
6149 loc = b->loc;
6150
6151 annotate_record ();
6152
6153 /* 1 */
6154 annotate_field (0);
6155 if (part_of_multiple)
6156 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6157 else
6158 uiout->field_signed ("number", b->number);
6159
6160 /* 2 */
6161 annotate_field (1);
6162 if (part_of_multiple)
6163 uiout->field_skip ("type");
6164 else
6165 uiout->field_string ("type", bptype_string (b->type));
6166
6167 /* 3 */
6168 annotate_field (2);
6169 if (part_of_multiple)
6170 uiout->field_skip ("disp");
6171 else
6172 uiout->field_string ("disp", bpdisp_text (b->disposition));
6173
6174 /* 4 */
6175 annotate_field (3);
6176 /* For locations that are disabled because of an invalid condition,
6177 display "N*" on CLI, where "*" refers to a footnote below the
6178 table. For MI, simply display a "N" without a footnote. */
6179 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
6180 if (part_of_multiple)
6181 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
6182 : (loc->enabled ? "y" : "n")));
6183 else
6184 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6185
6186 /* 5 and 6 */
6187 bool result = false;
6188 if (!raw_loc && b->ops != NULL && b->ops->print_one (b, last_loc))
6189 result = true;
6190 else
6191 {
6192 if (is_watchpoint (b))
6193 {
6194 struct watchpoint *w = (struct watchpoint *) b;
6195
6196 /* Field 4, the address, is omitted (which makes the columns
6197 not line up too nicely with the headers, but the effect
6198 is relatively readable). */
6199 if (opts.addressprint)
6200 uiout->field_skip ("addr");
6201 annotate_field (5);
6202 uiout->field_string ("what", w->exp_string.get ());
6203 }
6204 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6205 || is_ada_exception_catchpoint (b))
6206 {
6207 if (opts.addressprint)
6208 {
6209 annotate_field (4);
6210 if (header_of_multiple)
6211 uiout->field_string ("addr", "<MULTIPLE>",
6212 metadata_style.style ());
6213 else if (b->loc == NULL || loc->shlib_disabled)
6214 uiout->field_string ("addr", "<PENDING>",
6215 metadata_style.style ());
6216 else
6217 uiout->field_core_addr ("addr",
6218 loc->gdbarch, loc->address);
6219 }
6220 annotate_field (5);
6221 if (!header_of_multiple)
6222 print_breakpoint_location (b, loc);
6223 if (b->loc)
6224 *last_loc = b->loc;
6225 }
6226 }
6227
6228 if (loc != NULL && !header_of_multiple)
6229 {
6230 std::vector<int> inf_nums;
6231 int mi_only = 1;
6232
6233 for (inferior *inf : all_inferiors ())
6234 {
6235 if (inf->pspace == loc->pspace)
6236 inf_nums.push_back (inf->num);
6237 }
6238
6239 /* For backward compatibility, don't display inferiors in CLI unless
6240 there are several. Always display for MI. */
6241 if (allflag
6242 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6243 && (program_spaces.size () > 1
6244 || number_of_inferiors () > 1)
6245 /* LOC is for existing B, it cannot be in
6246 moribund_locations and thus having NULL OWNER. */
6247 && loc->owner->type != bp_catchpoint))
6248 mi_only = 0;
6249 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6250 }
6251
6252 if (!part_of_multiple)
6253 {
6254 if (b->thread != -1)
6255 {
6256 /* FIXME: This seems to be redundant and lost here; see the
6257 "stop only in" line a little further down. */
6258 uiout->text (" thread ");
6259 uiout->field_signed ("thread", b->thread);
6260 }
6261 else if (b->task != 0)
6262 {
6263 uiout->text (" task ");
6264 uiout->field_signed ("task", b->task);
6265 }
6266 }
6267
6268 uiout->text ("\n");
6269
6270 if (!part_of_multiple)
6271 b->ops->print_one_detail (b, uiout);
6272
6273 if (part_of_multiple && frame_id_p (b->frame_id))
6274 {
6275 annotate_field (6);
6276 uiout->text ("\tstop only in stack frame at ");
6277 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6278 the frame ID. */
6279 uiout->field_core_addr ("frame",
6280 b->gdbarch, b->frame_id.stack_addr);
6281 uiout->text ("\n");
6282 }
6283
6284 if (!part_of_multiple && b->cond_string)
6285 {
6286 annotate_field (7);
6287 if (is_tracepoint (b))
6288 uiout->text ("\ttrace only if ");
6289 else
6290 uiout->text ("\tstop only if ");
6291 uiout->field_string ("cond", b->cond_string.get ());
6292
6293 /* Print whether the target is doing the breakpoint's condition
6294 evaluation. If GDB is doing the evaluation, don't print anything. */
6295 if (is_breakpoint (b)
6296 && breakpoint_condition_evaluation_mode ()
6297 == condition_evaluation_target)
6298 {
6299 uiout->message (" (%pF evals)",
6300 string_field ("evaluated-by",
6301 bp_condition_evaluator (b)));
6302 }
6303 uiout->text ("\n");
6304 }
6305
6306 if (!part_of_multiple && b->thread != -1)
6307 {
6308 /* FIXME should make an annotation for this. */
6309 uiout->text ("\tstop only in thread ");
6310 if (uiout->is_mi_like_p ())
6311 uiout->field_signed ("thread", b->thread);
6312 else
6313 {
6314 struct thread_info *thr = find_thread_global_id (b->thread);
6315
6316 uiout->field_string ("thread", print_thread_id (thr));
6317 }
6318 uiout->text ("\n");
6319 }
6320
6321 if (!part_of_multiple)
6322 {
6323 if (b->hit_count)
6324 {
6325 /* FIXME should make an annotation for this. */
6326 if (is_catchpoint (b))
6327 uiout->text ("\tcatchpoint");
6328 else if (is_tracepoint (b))
6329 uiout->text ("\ttracepoint");
6330 else
6331 uiout->text ("\tbreakpoint");
6332 uiout->text (" already hit ");
6333 uiout->field_signed ("times", b->hit_count);
6334 if (b->hit_count == 1)
6335 uiout->text (" time\n");
6336 else
6337 uiout->text (" times\n");
6338 }
6339 else
6340 {
6341 /* Output the count also if it is zero, but only if this is mi. */
6342 if (uiout->is_mi_like_p ())
6343 uiout->field_signed ("times", b->hit_count);
6344 }
6345 }
6346
6347 if (!part_of_multiple && b->ignore_count)
6348 {
6349 annotate_field (8);
6350 uiout->message ("\tignore next %pF hits\n",
6351 signed_field ("ignore", b->ignore_count));
6352 }
6353
6354 /* Note that an enable count of 1 corresponds to "enable once"
6355 behavior, which is reported by the combination of enablement and
6356 disposition, so we don't need to mention it here. */
6357 if (!part_of_multiple && b->enable_count > 1)
6358 {
6359 annotate_field (8);
6360 uiout->text ("\tdisable after ");
6361 /* Tweak the wording to clarify that ignore and enable counts
6362 are distinct, and have additive effect. */
6363 if (b->ignore_count)
6364 uiout->text ("additional ");
6365 else
6366 uiout->text ("next ");
6367 uiout->field_signed ("enable", b->enable_count);
6368 uiout->text (" hits\n");
6369 }
6370
6371 if (!part_of_multiple && is_tracepoint (b))
6372 {
6373 struct tracepoint *tp = (struct tracepoint *) b;
6374
6375 if (tp->traceframe_usage)
6376 {
6377 uiout->text ("\ttrace buffer usage ");
6378 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6379 uiout->text (" bytes\n");
6380 }
6381 }
6382
6383 l = b->commands ? b->commands.get () : NULL;
6384 if (!part_of_multiple && l)
6385 {
6386 annotate_field (9);
6387 ui_out_emit_tuple tuple_emitter (uiout, "script");
6388 print_command_lines (uiout, l, 4);
6389 }
6390
6391 if (is_tracepoint (b))
6392 {
6393 struct tracepoint *t = (struct tracepoint *) b;
6394
6395 if (!part_of_multiple && t->pass_count)
6396 {
6397 annotate_field (10);
6398 uiout->text ("\tpass count ");
6399 uiout->field_signed ("pass", t->pass_count);
6400 uiout->text (" \n");
6401 }
6402
6403 /* Don't display it when tracepoint or tracepoint location is
6404 pending. */
6405 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6406 {
6407 annotate_field (11);
6408
6409 if (uiout->is_mi_like_p ())
6410 uiout->field_string ("installed",
6411 loc->inserted ? "y" : "n");
6412 else
6413 {
6414 if (loc->inserted)
6415 uiout->text ("\t");
6416 else
6417 uiout->text ("\tnot ");
6418 uiout->text ("installed on target\n");
6419 }
6420 }
6421 }
6422
6423 if (uiout->is_mi_like_p () && !part_of_multiple)
6424 {
6425 if (is_watchpoint (b))
6426 {
6427 struct watchpoint *w = (struct watchpoint *) b;
6428
6429 uiout->field_string ("original-location", w->exp_string.get ());
6430 }
6431 else if (b->location != NULL
6432 && event_location_to_string (b->location.get ()) != NULL)
6433 uiout->field_string ("original-location",
6434 event_location_to_string (b->location.get ()));
6435 }
6436
6437 return result;
6438 }
6439
6440 /* See breakpoint.h. */
6441
6442 bool fix_multi_location_breakpoint_output_globally = false;
6443
6444 static void
6445 print_one_breakpoint (struct breakpoint *b,
6446 struct bp_location **last_loc,
6447 int allflag)
6448 {
6449 struct ui_out *uiout = current_uiout;
6450 bool use_fixed_output
6451 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6452 || fix_multi_location_breakpoint_output_globally);
6453
6454 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6455 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6456 allflag, false);
6457
6458 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6459 are outside. */
6460 if (!use_fixed_output)
6461 bkpt_tuple_emitter.reset ();
6462
6463 /* If this breakpoint has custom print function,
6464 it's already printed. Otherwise, print individual
6465 locations, if any. */
6466 if (!printed || allflag)
6467 {
6468 /* If breakpoint has a single location that is disabled, we
6469 print it as if it had several locations, since otherwise it's
6470 hard to represent "breakpoint enabled, location disabled"
6471 situation.
6472
6473 Note that while hardware watchpoints have several locations
6474 internally, that's not a property exposed to users.
6475
6476 Likewise, while catchpoints may be implemented with
6477 breakpoints (e.g., catch throw), that's not a property
6478 exposed to users. We do however display the internal
6479 breakpoint locations with "maint info breakpoints". */
6480 if (!is_hardware_watchpoint (b)
6481 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6482 || is_ada_exception_catchpoint (b))
6483 && (allflag
6484 || (b->loc && (b->loc->next
6485 || !b->loc->enabled
6486 || b->loc->disabled_by_cond))))
6487 {
6488 gdb::optional<ui_out_emit_list> locations_list;
6489
6490 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6491 MI record. For later versions, place breakpoint locations in a
6492 list. */
6493 if (uiout->is_mi_like_p () && use_fixed_output)
6494 locations_list.emplace (uiout, "locations");
6495
6496 int n = 1;
6497 for (bp_location *loc : b->locations ())
6498 {
6499 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6500 print_one_breakpoint_location (b, loc, n, last_loc,
6501 allflag, allflag);
6502 n++;
6503 }
6504 }
6505 }
6506 }
6507
6508 static int
6509 breakpoint_address_bits (struct breakpoint *b)
6510 {
6511 int print_address_bits = 0;
6512
6513 /* Software watchpoints that aren't watching memory don't have an
6514 address to print. */
6515 if (is_no_memory_software_watchpoint (b))
6516 return 0;
6517
6518 for (bp_location *loc : b->locations ())
6519 {
6520 int addr_bit;
6521
6522 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6523 if (addr_bit > print_address_bits)
6524 print_address_bits = addr_bit;
6525 }
6526
6527 return print_address_bits;
6528 }
6529
6530 /* See breakpoint.h. */
6531
6532 void
6533 print_breakpoint (breakpoint *b)
6534 {
6535 struct bp_location *dummy_loc = NULL;
6536 print_one_breakpoint (b, &dummy_loc, 0);
6537 }
6538
6539 /* Return true if this breakpoint was set by the user, false if it is
6540 internal or momentary. */
6541
6542 int
6543 user_breakpoint_p (struct breakpoint *b)
6544 {
6545 return b->number > 0;
6546 }
6547
6548 /* See breakpoint.h. */
6549
6550 int
6551 pending_breakpoint_p (struct breakpoint *b)
6552 {
6553 return b->loc == NULL;
6554 }
6555
6556 /* Print information on breakpoints (including watchpoints and tracepoints).
6557
6558 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6559 understood by number_or_range_parser. Only breakpoints included in this
6560 list are then printed.
6561
6562 If SHOW_INTERNAL is true, print internal breakpoints.
6563
6564 If FILTER is non-NULL, call it on each breakpoint and only include the
6565 ones for which it returns true.
6566
6567 Return the total number of breakpoints listed. */
6568
6569 static int
6570 breakpoint_1 (const char *bp_num_list, bool show_internal,
6571 bool (*filter) (const struct breakpoint *))
6572 {
6573 struct bp_location *last_loc = NULL;
6574 int nr_printable_breakpoints;
6575 struct value_print_options opts;
6576 int print_address_bits = 0;
6577 int print_type_col_width = 14;
6578 struct ui_out *uiout = current_uiout;
6579 bool has_disabled_by_cond_location = false;
6580
6581 get_user_print_options (&opts);
6582
6583 /* Compute the number of rows in the table, as well as the size
6584 required for address fields. */
6585 nr_printable_breakpoints = 0;
6586 for (breakpoint *b : all_breakpoints ())
6587 {
6588 /* If we have a filter, only list the breakpoints it accepts. */
6589 if (filter && !filter (b))
6590 continue;
6591
6592 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6593 accept. Skip the others. */
6594 if (bp_num_list != NULL && *bp_num_list != '\0')
6595 {
6596 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6597 continue;
6598 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6599 continue;
6600 }
6601
6602 if (show_internal || user_breakpoint_p (b))
6603 {
6604 int addr_bit, type_len;
6605
6606 addr_bit = breakpoint_address_bits (b);
6607 if (addr_bit > print_address_bits)
6608 print_address_bits = addr_bit;
6609
6610 type_len = strlen (bptype_string (b->type));
6611 if (type_len > print_type_col_width)
6612 print_type_col_width = type_len;
6613
6614 nr_printable_breakpoints++;
6615 }
6616 }
6617
6618 {
6619 ui_out_emit_table table_emitter (uiout,
6620 opts.addressprint ? 6 : 5,
6621 nr_printable_breakpoints,
6622 "BreakpointTable");
6623
6624 if (nr_printable_breakpoints > 0)
6625 annotate_breakpoints_headers ();
6626 if (nr_printable_breakpoints > 0)
6627 annotate_field (0);
6628 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6629 if (nr_printable_breakpoints > 0)
6630 annotate_field (1);
6631 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6632 if (nr_printable_breakpoints > 0)
6633 annotate_field (2);
6634 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6635 if (nr_printable_breakpoints > 0)
6636 annotate_field (3);
6637 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6638 if (opts.addressprint)
6639 {
6640 if (nr_printable_breakpoints > 0)
6641 annotate_field (4);
6642 if (print_address_bits <= 32)
6643 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6644 else
6645 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6646 }
6647 if (nr_printable_breakpoints > 0)
6648 annotate_field (5);
6649 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6650 uiout->table_body ();
6651 if (nr_printable_breakpoints > 0)
6652 annotate_breakpoints_table ();
6653
6654 for (breakpoint *b : all_breakpoints ())
6655 {
6656 QUIT;
6657 /* If we have a filter, only list the breakpoints it accepts. */
6658 if (filter && !filter (b))
6659 continue;
6660
6661 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6662 accept. Skip the others. */
6663
6664 if (bp_num_list != NULL && *bp_num_list != '\0')
6665 {
6666 if (show_internal) /* maintenance info breakpoint */
6667 {
6668 if (parse_and_eval_long (bp_num_list) != b->number)
6669 continue;
6670 }
6671 else /* all others */
6672 {
6673 if (!number_is_in_list (bp_num_list, b->number))
6674 continue;
6675 }
6676 }
6677 /* We only print out user settable breakpoints unless the
6678 show_internal is set. */
6679 if (show_internal || user_breakpoint_p (b))
6680 {
6681 print_one_breakpoint (b, &last_loc, show_internal);
6682 for (bp_location *loc : b->locations ())
6683 if (loc->disabled_by_cond)
6684 has_disabled_by_cond_location = true;
6685 }
6686 }
6687 }
6688
6689 if (nr_printable_breakpoints == 0)
6690 {
6691 /* If there's a filter, let the caller decide how to report
6692 empty list. */
6693 if (!filter)
6694 {
6695 if (bp_num_list == NULL || *bp_num_list == '\0')
6696 uiout->message ("No breakpoints or watchpoints.\n");
6697 else
6698 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6699 bp_num_list);
6700 }
6701 }
6702 else
6703 {
6704 if (last_loc && !server_command)
6705 set_next_address (last_loc->gdbarch, last_loc->address);
6706
6707 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6708 uiout->message (_("(*): Breakpoint condition is invalid at this "
6709 "location.\n"));
6710 }
6711
6712 /* FIXME? Should this be moved up so that it is only called when
6713 there have been breakpoints? */
6714 annotate_breakpoints_table_end ();
6715
6716 return nr_printable_breakpoints;
6717 }
6718
6719 /* Display the value of default-collect in a way that is generally
6720 compatible with the breakpoint list. */
6721
6722 static void
6723 default_collect_info (void)
6724 {
6725 struct ui_out *uiout = current_uiout;
6726
6727 /* If it has no value (which is frequently the case), say nothing; a
6728 message like "No default-collect." gets in user's face when it's
6729 not wanted. */
6730 if (default_collect.empty ())
6731 return;
6732
6733 /* The following phrase lines up nicely with per-tracepoint collect
6734 actions. */
6735 uiout->text ("default collect ");
6736 uiout->field_string ("default-collect", default_collect);
6737 uiout->text (" \n");
6738 }
6739
6740 static void
6741 info_breakpoints_command (const char *args, int from_tty)
6742 {
6743 breakpoint_1 (args, false, NULL);
6744
6745 default_collect_info ();
6746 }
6747
6748 static void
6749 info_watchpoints_command (const char *args, int from_tty)
6750 {
6751 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6752 struct ui_out *uiout = current_uiout;
6753
6754 if (num_printed == 0)
6755 {
6756 if (args == NULL || *args == '\0')
6757 uiout->message ("No watchpoints.\n");
6758 else
6759 uiout->message ("No watchpoint matching '%s'.\n", args);
6760 }
6761 }
6762
6763 static void
6764 maintenance_info_breakpoints (const char *args, int from_tty)
6765 {
6766 breakpoint_1 (args, true, NULL);
6767
6768 default_collect_info ();
6769 }
6770
6771 static int
6772 breakpoint_has_pc (struct breakpoint *b,
6773 struct program_space *pspace,
6774 CORE_ADDR pc, struct obj_section *section)
6775 {
6776 for (bp_location *bl : b->locations ())
6777 {
6778 if (bl->pspace == pspace
6779 && bl->address == pc
6780 && (!overlay_debugging || bl->section == section))
6781 return 1;
6782 }
6783 return 0;
6784 }
6785
6786 /* Print a message describing any user-breakpoints set at PC. This
6787 concerns with logical breakpoints, so we match program spaces, not
6788 address spaces. */
6789
6790 static void
6791 describe_other_breakpoints (struct gdbarch *gdbarch,
6792 struct program_space *pspace, CORE_ADDR pc,
6793 struct obj_section *section, int thread)
6794 {
6795 int others = 0;
6796
6797 for (breakpoint *b : all_breakpoints ())
6798 others += (user_breakpoint_p (b)
6799 && breakpoint_has_pc (b, pspace, pc, section));
6800
6801 if (others > 0)
6802 {
6803 if (others == 1)
6804 gdb_printf (_("Note: breakpoint "));
6805 else /* if (others == ???) */
6806 gdb_printf (_("Note: breakpoints "));
6807 for (breakpoint *b : all_breakpoints ())
6808 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6809 {
6810 others--;
6811 gdb_printf ("%d", b->number);
6812 if (b->thread == -1 && thread != -1)
6813 gdb_printf (" (all threads)");
6814 else if (b->thread != -1)
6815 gdb_printf (" (thread %d)", b->thread);
6816 gdb_printf ("%s%s ",
6817 ((b->enable_state == bp_disabled
6818 || b->enable_state == bp_call_disabled)
6819 ? " (disabled)"
6820 : ""),
6821 (others > 1) ? ","
6822 : ((others == 1) ? " and" : ""));
6823 }
6824 current_uiout->message (_("also set at pc %ps.\n"),
6825 styled_string (address_style.style (),
6826 paddress (gdbarch, pc)));
6827 }
6828 }
6829 \f
6830
6831 /* Return true iff it is meaningful to use the address member of LOC.
6832 For some breakpoint types, the locations' address members are
6833 irrelevant and it makes no sense to attempt to compare them to
6834 other addresses (or use them for any other purpose either).
6835
6836 More specifically, software watchpoints and catchpoints that are
6837 not backed by breakpoints always have a zero valued location
6838 address and we don't want to mark breakpoints of any of these types
6839 to be a duplicate of an actual breakpoint location at address
6840 zero. */
6841
6842 static bool
6843 bl_address_is_meaningful (bp_location *loc)
6844 {
6845 return loc->loc_type != bp_loc_other;
6846 }
6847
6848 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6849 true if LOC1 and LOC2 represent the same watchpoint location. */
6850
6851 static int
6852 watchpoint_locations_match (struct bp_location *loc1,
6853 struct bp_location *loc2)
6854 {
6855 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6856 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6857
6858 /* Both of them must exist. */
6859 gdb_assert (w1 != NULL);
6860 gdb_assert (w2 != NULL);
6861
6862 /* If the target can evaluate the condition expression in hardware,
6863 then we we need to insert both watchpoints even if they are at
6864 the same place. Otherwise the watchpoint will only trigger when
6865 the condition of whichever watchpoint was inserted evaluates to
6866 true, not giving a chance for GDB to check the condition of the
6867 other watchpoint. */
6868 if ((w1->cond_exp
6869 && target_can_accel_watchpoint_condition (loc1->address,
6870 loc1->length,
6871 loc1->watchpoint_type,
6872 w1->cond_exp.get ()))
6873 || (w2->cond_exp
6874 && target_can_accel_watchpoint_condition (loc2->address,
6875 loc2->length,
6876 loc2->watchpoint_type,
6877 w2->cond_exp.get ())))
6878 return 0;
6879
6880 /* Note that this checks the owner's type, not the location's. In
6881 case the target does not support read watchpoints, but does
6882 support access watchpoints, we'll have bp_read_watchpoint
6883 watchpoints with hw_access locations. Those should be considered
6884 duplicates of hw_read locations. The hw_read locations will
6885 become hw_access locations later. */
6886 return (loc1->owner->type == loc2->owner->type
6887 && loc1->pspace->aspace == loc2->pspace->aspace
6888 && loc1->address == loc2->address
6889 && loc1->length == loc2->length);
6890 }
6891
6892 /* See breakpoint.h. */
6893
6894 int
6895 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6896 const address_space *aspace2, CORE_ADDR addr2)
6897 {
6898 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6899 || aspace1 == aspace2)
6900 && addr1 == addr2);
6901 }
6902
6903 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6904 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6905 matches ASPACE2. On targets that have global breakpoints, the address
6906 space doesn't really matter. */
6907
6908 static int
6909 breakpoint_address_match_range (const address_space *aspace1,
6910 CORE_ADDR addr1,
6911 int len1, const address_space *aspace2,
6912 CORE_ADDR addr2)
6913 {
6914 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6915 || aspace1 == aspace2)
6916 && addr2 >= addr1 && addr2 < addr1 + len1);
6917 }
6918
6919 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6920 a ranged breakpoint. In most targets, a match happens only if ASPACE
6921 matches the breakpoint's address space. On targets that have global
6922 breakpoints, the address space doesn't really matter. */
6923
6924 static int
6925 breakpoint_location_address_match (struct bp_location *bl,
6926 const address_space *aspace,
6927 CORE_ADDR addr)
6928 {
6929 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6930 aspace, addr)
6931 || (bl->length
6932 && breakpoint_address_match_range (bl->pspace->aspace,
6933 bl->address, bl->length,
6934 aspace, addr)));
6935 }
6936
6937 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6938 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6939 match happens only if ASPACE matches the breakpoint's address
6940 space. On targets that have global breakpoints, the address space
6941 doesn't really matter. */
6942
6943 static int
6944 breakpoint_location_address_range_overlap (struct bp_location *bl,
6945 const address_space *aspace,
6946 CORE_ADDR addr, int len)
6947 {
6948 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6949 || bl->pspace->aspace == aspace)
6950 {
6951 int bl_len = bl->length != 0 ? bl->length : 1;
6952
6953 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6954 return 1;
6955 }
6956 return 0;
6957 }
6958
6959 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6960 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6961 true, otherwise returns false. */
6962
6963 static int
6964 tracepoint_locations_match (struct bp_location *loc1,
6965 struct bp_location *loc2)
6966 {
6967 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6968 /* Since tracepoint locations are never duplicated with others', tracepoint
6969 locations at the same address of different tracepoints are regarded as
6970 different locations. */
6971 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6972 else
6973 return 0;
6974 }
6975
6976 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6977 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6978 the same location. If SW_HW_BPS_MATCH is true, then software
6979 breakpoint locations and hardware breakpoint locations match,
6980 otherwise they don't. */
6981
6982 static int
6983 breakpoint_locations_match (struct bp_location *loc1,
6984 struct bp_location *loc2,
6985 bool sw_hw_bps_match)
6986 {
6987 int hw_point1, hw_point2;
6988
6989 /* Both of them must not be in moribund_locations. */
6990 gdb_assert (loc1->owner != NULL);
6991 gdb_assert (loc2->owner != NULL);
6992
6993 hw_point1 = is_hardware_watchpoint (loc1->owner);
6994 hw_point2 = is_hardware_watchpoint (loc2->owner);
6995
6996 if (hw_point1 != hw_point2)
6997 return 0;
6998 else if (hw_point1)
6999 return watchpoint_locations_match (loc1, loc2);
7000 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7001 return tracepoint_locations_match (loc1, loc2);
7002 else
7003 /* We compare bp_location.length in order to cover ranged
7004 breakpoints. Keep this in sync with
7005 bp_location_is_less_than. */
7006 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7007 loc2->pspace->aspace, loc2->address)
7008 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7009 && loc1->length == loc2->length);
7010 }
7011
7012 static void
7013 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7014 int bnum, int have_bnum)
7015 {
7016 /* The longest string possibly returned by hex_string_custom
7017 is 50 chars. These must be at least that big for safety. */
7018 char astr1[64];
7019 char astr2[64];
7020
7021 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7022 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7023 if (have_bnum)
7024 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7025 bnum, astr1, astr2);
7026 else
7027 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7028 }
7029
7030 /* Adjust a breakpoint's address to account for architectural
7031 constraints on breakpoint placement. Return the adjusted address.
7032 Note: Very few targets require this kind of adjustment. For most
7033 targets, this function is simply the identity function. */
7034
7035 static CORE_ADDR
7036 adjust_breakpoint_address (struct gdbarch *gdbarch,
7037 CORE_ADDR bpaddr, enum bptype bptype)
7038 {
7039 if (bptype == bp_watchpoint
7040 || bptype == bp_hardware_watchpoint
7041 || bptype == bp_read_watchpoint
7042 || bptype == bp_access_watchpoint
7043 || bptype == bp_catchpoint)
7044 {
7045 /* Watchpoints and the various bp_catch_* eventpoints should not
7046 have their addresses modified. */
7047 return bpaddr;
7048 }
7049 else if (bptype == bp_single_step)
7050 {
7051 /* Single-step breakpoints should not have their addresses
7052 modified. If there's any architectural constrain that
7053 applies to this address, then it should have already been
7054 taken into account when the breakpoint was created in the
7055 first place. If we didn't do this, stepping through e.g.,
7056 Thumb-2 IT blocks would break. */
7057 return bpaddr;
7058 }
7059 else
7060 {
7061 CORE_ADDR adjusted_bpaddr = bpaddr;
7062
7063 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7064 {
7065 /* Some targets have architectural constraints on the placement
7066 of breakpoint instructions. Obtain the adjusted address. */
7067 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7068 }
7069
7070 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7071
7072 /* An adjusted breakpoint address can significantly alter
7073 a user's expectations. Print a warning if an adjustment
7074 is required. */
7075 if (adjusted_bpaddr != bpaddr)
7076 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7077
7078 return adjusted_bpaddr;
7079 }
7080 }
7081
7082 static bp_loc_type
7083 bp_location_from_bp_type (bptype type)
7084 {
7085 switch (type)
7086 {
7087 case bp_breakpoint:
7088 case bp_single_step:
7089 case bp_until:
7090 case bp_finish:
7091 case bp_longjmp:
7092 case bp_longjmp_resume:
7093 case bp_longjmp_call_dummy:
7094 case bp_exception:
7095 case bp_exception_resume:
7096 case bp_step_resume:
7097 case bp_hp_step_resume:
7098 case bp_watchpoint_scope:
7099 case bp_call_dummy:
7100 case bp_std_terminate:
7101 case bp_shlib_event:
7102 case bp_thread_event:
7103 case bp_overlay_event:
7104 case bp_jit_event:
7105 case bp_longjmp_master:
7106 case bp_std_terminate_master:
7107 case bp_exception_master:
7108 case bp_gnu_ifunc_resolver:
7109 case bp_gnu_ifunc_resolver_return:
7110 case bp_dprintf:
7111 return bp_loc_software_breakpoint;
7112 case bp_hardware_breakpoint:
7113 return bp_loc_hardware_breakpoint;
7114 case bp_hardware_watchpoint:
7115 case bp_read_watchpoint:
7116 case bp_access_watchpoint:
7117 return bp_loc_hardware_watchpoint;
7118 case bp_watchpoint:
7119 case bp_catchpoint:
7120 case bp_tracepoint:
7121 case bp_fast_tracepoint:
7122 case bp_static_tracepoint:
7123 return bp_loc_other;
7124 default:
7125 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7126 }
7127 }
7128
7129 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7130 {
7131 this->owner = owner;
7132 this->cond_bytecode = NULL;
7133 this->shlib_disabled = 0;
7134 this->enabled = 1;
7135 this->disabled_by_cond = false;
7136
7137 this->loc_type = type;
7138
7139 if (this->loc_type == bp_loc_software_breakpoint
7140 || this->loc_type == bp_loc_hardware_breakpoint)
7141 mark_breakpoint_location_modified (this);
7142
7143 incref ();
7144 }
7145
7146 bp_location::bp_location (breakpoint *owner)
7147 : bp_location::bp_location (owner,
7148 bp_location_from_bp_type (owner->type))
7149 {
7150 }
7151
7152 /* Allocate a struct bp_location. */
7153
7154 static struct bp_location *
7155 allocate_bp_location (struct breakpoint *bpt)
7156 {
7157 return bpt->ops->allocate_location (bpt);
7158 }
7159
7160 /* Decrement reference count. If the reference count reaches 0,
7161 destroy the bp_location. Sets *BLP to NULL. */
7162
7163 static void
7164 decref_bp_location (struct bp_location **blp)
7165 {
7166 bp_location_ref_policy::decref (*blp);
7167 *blp = NULL;
7168 }
7169
7170 /* Add breakpoint B at the end of the global breakpoint chain. */
7171
7172 static breakpoint *
7173 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7174 {
7175 struct breakpoint *b1;
7176 struct breakpoint *result = b.get ();
7177
7178 /* Add this breakpoint to the end of the chain so that a list of
7179 breakpoints will come out in order of increasing numbers. */
7180
7181 b1 = breakpoint_chain;
7182 if (b1 == 0)
7183 breakpoint_chain = b.release ();
7184 else
7185 {
7186 while (b1->next)
7187 b1 = b1->next;
7188 b1->next = b.release ();
7189 }
7190
7191 return result;
7192 }
7193
7194 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7195
7196 static void
7197 init_raw_breakpoint_without_location (struct breakpoint *b,
7198 struct gdbarch *gdbarch,
7199 enum bptype bptype,
7200 const struct breakpoint_ops *ops)
7201 {
7202 gdb_assert (ops != NULL);
7203
7204 b->ops = ops;
7205 b->type = bptype;
7206 b->gdbarch = gdbarch;
7207 b->language = current_language->la_language;
7208 b->input_radix = input_radix;
7209 b->related_breakpoint = b;
7210 }
7211
7212 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7213 that has type BPTYPE and has no locations as yet. */
7214
7215 static struct breakpoint *
7216 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7217 enum bptype bptype,
7218 const struct breakpoint_ops *ops)
7219 {
7220 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7221
7222 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7223 return add_to_breakpoint_chain (std::move (b));
7224 }
7225
7226 /* Initialize loc->function_name. */
7227
7228 static void
7229 set_breakpoint_location_function (struct bp_location *loc)
7230 {
7231 gdb_assert (loc->owner != NULL);
7232
7233 if (loc->owner->type == bp_breakpoint
7234 || loc->owner->type == bp_hardware_breakpoint
7235 || is_tracepoint (loc->owner))
7236 {
7237 const char *function_name;
7238
7239 if (loc->msymbol != NULL
7240 && (loc->msymbol->type () == mst_text_gnu_ifunc
7241 || loc->msymbol->type () == mst_data_gnu_ifunc))
7242 {
7243 struct breakpoint *b = loc->owner;
7244
7245 function_name = loc->msymbol->linkage_name ();
7246
7247 if (b->type == bp_breakpoint && b->loc == loc
7248 && loc->next == NULL && b->related_breakpoint == b)
7249 {
7250 /* Create only the whole new breakpoint of this type but do not
7251 mess more complicated breakpoints with multiple locations. */
7252 b->type = bp_gnu_ifunc_resolver;
7253 /* Remember the resolver's address for use by the return
7254 breakpoint. */
7255 loc->related_address = loc->address;
7256 }
7257 }
7258 else
7259 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7260
7261 if (function_name)
7262 loc->function_name = make_unique_xstrdup (function_name);
7263 }
7264 }
7265
7266 /* Attempt to determine architecture of location identified by SAL. */
7267 struct gdbarch *
7268 get_sal_arch (struct symtab_and_line sal)
7269 {
7270 if (sal.section)
7271 return sal.section->objfile->arch ();
7272 if (sal.symtab)
7273 return sal.symtab->compunit ()->objfile ()->arch ();
7274
7275 return NULL;
7276 }
7277
7278 /* Low level routine for partially initializing a breakpoint of type
7279 BPTYPE. The newly created breakpoint's address, section, source
7280 file name, and line number are provided by SAL.
7281
7282 It is expected that the caller will complete the initialization of
7283 the newly created breakpoint struct as well as output any status
7284 information regarding the creation of a new breakpoint. */
7285
7286 static void
7287 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7288 struct symtab_and_line sal, enum bptype bptype,
7289 const struct breakpoint_ops *ops)
7290 {
7291 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7292
7293 add_location_to_breakpoint (b, &sal);
7294
7295 if (bptype != bp_catchpoint)
7296 gdb_assert (sal.pspace != NULL);
7297
7298 /* Store the program space that was used to set the breakpoint,
7299 except for ordinary breakpoints, which are independent of the
7300 program space. */
7301 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7302 b->pspace = sal.pspace;
7303 }
7304
7305 /* set_raw_breakpoint is a low level routine for allocating and
7306 partially initializing a breakpoint of type BPTYPE. The newly
7307 created breakpoint's address, section, source file name, and line
7308 number are provided by SAL. The newly created and partially
7309 initialized breakpoint is added to the breakpoint chain and
7310 is also returned as the value of this function.
7311
7312 It is expected that the caller will complete the initialization of
7313 the newly created breakpoint struct as well as output any status
7314 information regarding the creation of a new breakpoint. In
7315 particular, set_raw_breakpoint does NOT set the breakpoint
7316 number! Care should be taken to not allow an error to occur
7317 prior to completing the initialization of the breakpoint. If this
7318 should happen, a bogus breakpoint will be left on the chain. */
7319
7320 static struct breakpoint *
7321 set_raw_breakpoint (struct gdbarch *gdbarch,
7322 struct symtab_and_line sal, enum bptype bptype,
7323 const struct breakpoint_ops *ops)
7324 {
7325 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7326
7327 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7328 return add_to_breakpoint_chain (std::move (b));
7329 }
7330
7331 /* Call this routine when stepping and nexting to enable a breakpoint
7332 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7333 initiated the operation. */
7334
7335 void
7336 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7337 {
7338 int thread = tp->global_num;
7339
7340 /* To avoid having to rescan all objfile symbols at every step,
7341 we maintain a list of continually-inserted but always disabled
7342 longjmp "master" breakpoints. Here, we simply create momentary
7343 clones of those and enable them for the requested thread. */
7344 for (breakpoint *b : all_breakpoints_safe ())
7345 if (b->pspace == current_program_space
7346 && (b->type == bp_longjmp_master
7347 || b->type == bp_exception_master))
7348 {
7349 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7350 struct breakpoint *clone;
7351
7352 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7353 after their removal. */
7354 clone = momentary_breakpoint_from_master (b, type,
7355 &vtable_breakpoint_ops, 1);
7356 clone->thread = thread;
7357 }
7358
7359 tp->initiating_frame = frame;
7360 }
7361
7362 /* Delete all longjmp breakpoints from THREAD. */
7363 void
7364 delete_longjmp_breakpoint (int thread)
7365 {
7366 for (breakpoint *b : all_breakpoints_safe ())
7367 if (b->type == bp_longjmp || b->type == bp_exception)
7368 {
7369 if (b->thread == thread)
7370 delete_breakpoint (b);
7371 }
7372 }
7373
7374 void
7375 delete_longjmp_breakpoint_at_next_stop (int thread)
7376 {
7377 for (breakpoint *b : all_breakpoints_safe ())
7378 if (b->type == bp_longjmp || b->type == bp_exception)
7379 {
7380 if (b->thread == thread)
7381 b->disposition = disp_del_at_next_stop;
7382 }
7383 }
7384
7385 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7386 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7387 pointer to any of them. Return NULL if this system cannot place longjmp
7388 breakpoints. */
7389
7390 struct breakpoint *
7391 set_longjmp_breakpoint_for_call_dummy (void)
7392 {
7393 breakpoint *retval = nullptr;
7394
7395 for (breakpoint *b : all_breakpoints ())
7396 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7397 {
7398 struct breakpoint *new_b;
7399
7400 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7401 &vtable_breakpoint_ops,
7402 1);
7403 new_b->thread = inferior_thread ()->global_num;
7404
7405 /* Link NEW_B into the chain of RETVAL breakpoints. */
7406
7407 gdb_assert (new_b->related_breakpoint == new_b);
7408 if (retval == NULL)
7409 retval = new_b;
7410 new_b->related_breakpoint = retval;
7411 while (retval->related_breakpoint != new_b->related_breakpoint)
7412 retval = retval->related_breakpoint;
7413 retval->related_breakpoint = new_b;
7414 }
7415
7416 return retval;
7417 }
7418
7419 /* Verify all existing dummy frames and their associated breakpoints for
7420 TP. Remove those which can no longer be found in the current frame
7421 stack.
7422
7423 If the unwind fails then there is not sufficient information to discard
7424 dummy frames. In this case, elide the clean up and the dummy frames will
7425 be cleaned up next time this function is called from a location where
7426 unwinding is possible. */
7427
7428 void
7429 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7430 {
7431 struct breakpoint *b, *b_tmp;
7432
7433 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7434 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7435 {
7436 struct breakpoint *dummy_b = b->related_breakpoint;
7437
7438 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7439 chained off b->related_breakpoint. */
7440 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7441 dummy_b = dummy_b->related_breakpoint;
7442
7443 /* If there was no bp_call_dummy breakpoint then there's nothing
7444 more to do. Or, if the dummy frame associated with the
7445 bp_call_dummy is still on the stack then we need to leave this
7446 bp_call_dummy in place. */
7447 if (dummy_b->type != bp_call_dummy
7448 || frame_find_by_id (dummy_b->frame_id) != NULL)
7449 continue;
7450
7451 /* We didn't find the dummy frame on the stack, this could be
7452 because we have longjmp'd to a stack frame that is previous to
7453 the dummy frame, or it could be because the stack unwind is
7454 broken at some point between the longjmp frame and the dummy
7455 frame.
7456
7457 Next we figure out why the stack unwind stopped. If it looks
7458 like the unwind is complete then we assume the dummy frame has
7459 been jumped over, however, if the unwind stopped for an
7460 unexpected reason then we assume the stack unwind is currently
7461 broken, and that we will (eventually) return to the dummy
7462 frame.
7463
7464 It might be tempting to consider using frame_id_inner here, but
7465 that is not safe. There is no guarantee that the stack frames
7466 we are looking at here are even on the same stack as the
7467 original dummy frame, hence frame_id_inner can't be used. See
7468 the comments on frame_id_inner for more details. */
7469 bool unwind_finished_unexpectedly = false;
7470 for (struct frame_info *fi = get_current_frame (); fi != nullptr; )
7471 {
7472 struct frame_info *prev = get_prev_frame (fi);
7473 if (prev == nullptr)
7474 {
7475 /* FI is the last stack frame. Why did this frame not
7476 unwind further? */
7477 auto stop_reason = get_frame_unwind_stop_reason (fi);
7478 if (stop_reason != UNWIND_NO_REASON
7479 && stop_reason != UNWIND_OUTERMOST)
7480 unwind_finished_unexpectedly = true;
7481 }
7482 fi = prev;
7483 }
7484 if (unwind_finished_unexpectedly)
7485 continue;
7486
7487 dummy_frame_discard (dummy_b->frame_id, tp);
7488
7489 while (b->related_breakpoint != b)
7490 {
7491 if (b_tmp == b->related_breakpoint)
7492 b_tmp = b->related_breakpoint->next;
7493 delete_breakpoint (b->related_breakpoint);
7494 }
7495 delete_breakpoint (b);
7496 }
7497 }
7498
7499 void
7500 enable_overlay_breakpoints (void)
7501 {
7502 for (breakpoint *b : all_breakpoints ())
7503 if (b->type == bp_overlay_event)
7504 {
7505 b->enable_state = bp_enabled;
7506 update_global_location_list (UGLL_MAY_INSERT);
7507 overlay_events_enabled = 1;
7508 }
7509 }
7510
7511 void
7512 disable_overlay_breakpoints (void)
7513 {
7514 for (breakpoint *b : all_breakpoints ())
7515 if (b->type == bp_overlay_event)
7516 {
7517 b->enable_state = bp_disabled;
7518 update_global_location_list (UGLL_DONT_INSERT);
7519 overlay_events_enabled = 0;
7520 }
7521 }
7522
7523 /* Set an active std::terminate breakpoint for each std::terminate
7524 master breakpoint. */
7525 void
7526 set_std_terminate_breakpoint (void)
7527 {
7528 for (breakpoint *b : all_breakpoints_safe ())
7529 if (b->pspace == current_program_space
7530 && b->type == bp_std_terminate_master)
7531 {
7532 momentary_breakpoint_from_master (b, bp_std_terminate,
7533 &vtable_breakpoint_ops, 1);
7534 }
7535 }
7536
7537 /* Delete all the std::terminate breakpoints. */
7538 void
7539 delete_std_terminate_breakpoint (void)
7540 {
7541 for (breakpoint *b : all_breakpoints_safe ())
7542 if (b->type == bp_std_terminate)
7543 delete_breakpoint (b);
7544 }
7545
7546 struct breakpoint *
7547 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7548 {
7549 struct breakpoint *b;
7550
7551 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7552 &vtable_breakpoint_ops);
7553
7554 b->enable_state = bp_enabled;
7555 /* location has to be used or breakpoint_re_set will delete me. */
7556 b->location = new_address_location (b->loc->address, NULL, 0);
7557
7558 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7559
7560 return b;
7561 }
7562
7563 struct lang_and_radix
7564 {
7565 enum language lang;
7566 int radix;
7567 };
7568
7569 /* Create a breakpoint for JIT code registration and unregistration. */
7570
7571 struct breakpoint *
7572 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7573 {
7574 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7575 &vtable_breakpoint_ops);
7576 }
7577
7578 /* Remove JIT code registration and unregistration breakpoint(s). */
7579
7580 void
7581 remove_jit_event_breakpoints (void)
7582 {
7583 for (breakpoint *b : all_breakpoints_safe ())
7584 if (b->type == bp_jit_event
7585 && b->loc->pspace == current_program_space)
7586 delete_breakpoint (b);
7587 }
7588
7589 void
7590 remove_solib_event_breakpoints (void)
7591 {
7592 for (breakpoint *b : all_breakpoints_safe ())
7593 if (b->type == bp_shlib_event
7594 && b->loc->pspace == current_program_space)
7595 delete_breakpoint (b);
7596 }
7597
7598 /* See breakpoint.h. */
7599
7600 void
7601 remove_solib_event_breakpoints_at_next_stop (void)
7602 {
7603 for (breakpoint *b : all_breakpoints_safe ())
7604 if (b->type == bp_shlib_event
7605 && b->loc->pspace == current_program_space)
7606 b->disposition = disp_del_at_next_stop;
7607 }
7608
7609 /* Helper for create_solib_event_breakpoint /
7610 create_and_insert_solib_event_breakpoint. Allows specifying which
7611 INSERT_MODE to pass through to update_global_location_list. */
7612
7613 static struct breakpoint *
7614 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7615 enum ugll_insert_mode insert_mode)
7616 {
7617 struct breakpoint *b;
7618
7619 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7620 &vtable_breakpoint_ops);
7621 update_global_location_list_nothrow (insert_mode);
7622 return b;
7623 }
7624
7625 struct breakpoint *
7626 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7627 {
7628 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7629 }
7630
7631 /* See breakpoint.h. */
7632
7633 struct breakpoint *
7634 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7635 {
7636 struct breakpoint *b;
7637
7638 /* Explicitly tell update_global_location_list to insert
7639 locations. */
7640 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7641 if (!b->loc->inserted)
7642 {
7643 delete_breakpoint (b);
7644 return NULL;
7645 }
7646 return b;
7647 }
7648
7649 /* Disable any breakpoints that are on code in shared libraries. Only
7650 apply to enabled breakpoints, disabled ones can just stay disabled. */
7651
7652 void
7653 disable_breakpoints_in_shlibs (void)
7654 {
7655 for (bp_location *loc : all_bp_locations ())
7656 {
7657 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7658 struct breakpoint *b = loc->owner;
7659
7660 /* We apply the check to all breakpoints, including disabled for
7661 those with loc->duplicate set. This is so that when breakpoint
7662 becomes enabled, or the duplicate is removed, gdb will try to
7663 insert all breakpoints. If we don't set shlib_disabled here,
7664 we'll try to insert those breakpoints and fail. */
7665 if (((b->type == bp_breakpoint)
7666 || (b->type == bp_jit_event)
7667 || (b->type == bp_hardware_breakpoint)
7668 || (is_tracepoint (b)))
7669 && loc->pspace == current_program_space
7670 && !loc->shlib_disabled
7671 && solib_name_from_address (loc->pspace, loc->address)
7672 )
7673 {
7674 loc->shlib_disabled = 1;
7675 }
7676 }
7677 }
7678
7679 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7680 notification of unloaded_shlib. Only apply to enabled breakpoints,
7681 disabled ones can just stay disabled. */
7682
7683 static void
7684 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7685 {
7686 int disabled_shlib_breaks = 0;
7687
7688 for (bp_location *loc : all_bp_locations ())
7689 {
7690 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7691 struct breakpoint *b = loc->owner;
7692
7693 if (solib->pspace == loc->pspace
7694 && !loc->shlib_disabled
7695 && (((b->type == bp_breakpoint
7696 || b->type == bp_jit_event
7697 || b->type == bp_hardware_breakpoint)
7698 && (loc->loc_type == bp_loc_hardware_breakpoint
7699 || loc->loc_type == bp_loc_software_breakpoint))
7700 || is_tracepoint (b))
7701 && solib_contains_address_p (solib, loc->address))
7702 {
7703 loc->shlib_disabled = 1;
7704 /* At this point, we cannot rely on remove_breakpoint
7705 succeeding so we must mark the breakpoint as not inserted
7706 to prevent future errors occurring in remove_breakpoints. */
7707 loc->inserted = 0;
7708
7709 /* This may cause duplicate notifications for the same breakpoint. */
7710 gdb::observers::breakpoint_modified.notify (b);
7711
7712 if (!disabled_shlib_breaks)
7713 {
7714 target_terminal::ours_for_output ();
7715 warning (_("Temporarily disabling breakpoints "
7716 "for unloaded shared library \"%s\""),
7717 solib->so_name);
7718 }
7719 disabled_shlib_breaks = 1;
7720 }
7721 }
7722 }
7723
7724 /* Disable any breakpoints and tracepoints in OBJFILE upon
7725 notification of free_objfile. Only apply to enabled breakpoints,
7726 disabled ones can just stay disabled. */
7727
7728 static void
7729 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7730 {
7731 if (objfile == NULL)
7732 return;
7733
7734 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7735 managed by the user with add-symbol-file/remove-symbol-file.
7736 Similarly to how breakpoints in shared libraries are handled in
7737 response to "nosharedlibrary", mark breakpoints in such modules
7738 shlib_disabled so they end up uninserted on the next global
7739 location list update. Shared libraries not loaded by the user
7740 aren't handled here -- they're already handled in
7741 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7742 solib_unloaded observer. We skip objfiles that are not
7743 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7744 main objfile). */
7745 if ((objfile->flags & OBJF_SHARED) == 0
7746 || (objfile->flags & OBJF_USERLOADED) == 0)
7747 return;
7748
7749 for (breakpoint *b : all_breakpoints ())
7750 {
7751 int bp_modified = 0;
7752
7753 if (!is_breakpoint (b) && !is_tracepoint (b))
7754 continue;
7755
7756 for (bp_location *loc : b->locations ())
7757 {
7758 CORE_ADDR loc_addr = loc->address;
7759
7760 if (loc->loc_type != bp_loc_hardware_breakpoint
7761 && loc->loc_type != bp_loc_software_breakpoint)
7762 continue;
7763
7764 if (loc->shlib_disabled != 0)
7765 continue;
7766
7767 if (objfile->pspace != loc->pspace)
7768 continue;
7769
7770 if (loc->loc_type != bp_loc_hardware_breakpoint
7771 && loc->loc_type != bp_loc_software_breakpoint)
7772 continue;
7773
7774 if (is_addr_in_objfile (loc_addr, objfile))
7775 {
7776 loc->shlib_disabled = 1;
7777 /* At this point, we don't know whether the object was
7778 unmapped from the inferior or not, so leave the
7779 inserted flag alone. We'll handle failure to
7780 uninsert quietly, in case the object was indeed
7781 unmapped. */
7782
7783 mark_breakpoint_location_modified (loc);
7784
7785 bp_modified = 1;
7786 }
7787 }
7788
7789 if (bp_modified)
7790 gdb::observers::breakpoint_modified.notify (b);
7791 }
7792 }
7793
7794 /* See breakpoint.h. */
7795
7796 void
7797 init_catchpoint (struct breakpoint *b,
7798 struct gdbarch *gdbarch, bool temp,
7799 const char *cond_string,
7800 const struct breakpoint_ops *ops)
7801 {
7802 symtab_and_line sal;
7803 sal.pspace = current_program_space;
7804
7805 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
7806
7807 if (cond_string == nullptr)
7808 b->cond_string.reset ();
7809 else
7810 b->cond_string = make_unique_xstrdup (cond_string);
7811 b->disposition = temp ? disp_del : disp_donttouch;
7812 }
7813
7814 void
7815 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
7816 {
7817 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
7818 set_breakpoint_number (internal, b);
7819 if (is_tracepoint (b))
7820 set_tracepoint_count (breakpoint_count);
7821 if (!internal)
7822 mention (b);
7823 gdb::observers::breakpoint_created.notify (b);
7824
7825 if (update_gll)
7826 update_global_location_list (UGLL_MAY_INSERT);
7827 }
7828
7829 static int
7830 hw_breakpoint_used_count (void)
7831 {
7832 int i = 0;
7833
7834 for (breakpoint *b : all_breakpoints ())
7835 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
7836 for (bp_location *bl : b->locations ())
7837 {
7838 /* Special types of hardware breakpoints may use more than
7839 one register. */
7840 i += b->ops->resources_needed (bl);
7841 }
7842
7843 return i;
7844 }
7845
7846 /* Returns the resources B would use if it were a hardware
7847 watchpoint. */
7848
7849 static int
7850 hw_watchpoint_use_count (struct breakpoint *b)
7851 {
7852 int i = 0;
7853
7854 if (!breakpoint_enabled (b))
7855 return 0;
7856
7857 for (bp_location *bl : b->locations ())
7858 {
7859 /* Special types of hardware watchpoints may use more than
7860 one register. */
7861 i += b->ops->resources_needed (bl);
7862 }
7863
7864 return i;
7865 }
7866
7867 /* Returns the sum the used resources of all hardware watchpoints of
7868 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
7869 the sum of the used resources of all hardware watchpoints of other
7870 types _not_ TYPE. */
7871
7872 static int
7873 hw_watchpoint_used_count_others (struct breakpoint *except,
7874 enum bptype type, int *other_type_used)
7875 {
7876 int i = 0;
7877
7878 *other_type_used = 0;
7879 for (breakpoint *b : all_breakpoints ())
7880 {
7881 if (b == except)
7882 continue;
7883 if (!breakpoint_enabled (b))
7884 continue;
7885
7886 if (b->type == type)
7887 i += hw_watchpoint_use_count (b);
7888 else if (is_hardware_watchpoint (b))
7889 *other_type_used = 1;
7890 }
7891
7892 return i;
7893 }
7894
7895 void
7896 disable_watchpoints_before_interactive_call_start (void)
7897 {
7898 for (breakpoint *b : all_breakpoints ())
7899 if (is_watchpoint (b) && breakpoint_enabled (b))
7900 {
7901 b->enable_state = bp_call_disabled;
7902 update_global_location_list (UGLL_DONT_INSERT);
7903 }
7904 }
7905
7906 void
7907 enable_watchpoints_after_interactive_call_stop (void)
7908 {
7909 for (breakpoint *b : all_breakpoints ())
7910 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
7911 {
7912 b->enable_state = bp_enabled;
7913 update_global_location_list (UGLL_MAY_INSERT);
7914 }
7915 }
7916
7917 void
7918 disable_breakpoints_before_startup (void)
7919 {
7920 current_program_space->executing_startup = 1;
7921 update_global_location_list (UGLL_DONT_INSERT);
7922 }
7923
7924 void
7925 enable_breakpoints_after_startup (void)
7926 {
7927 current_program_space->executing_startup = 0;
7928 breakpoint_re_set ();
7929 }
7930
7931 /* Create a new single-step breakpoint for thread THREAD, with no
7932 locations. */
7933
7934 static struct breakpoint *
7935 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
7936 {
7937 std::unique_ptr<breakpoint> b (new momentary_breakpoint ());
7938
7939 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7940 &vtable_breakpoint_ops);
7941
7942 b->disposition = disp_donttouch;
7943 b->frame_id = null_frame_id;
7944
7945 b->thread = thread;
7946 gdb_assert (b->thread != 0);
7947
7948 return add_to_breakpoint_chain (std::move (b));
7949 }
7950
7951 /* Set a momentary breakpoint of type TYPE at address specified by
7952 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
7953 frame. */
7954
7955 breakpoint_up
7956 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7957 struct frame_id frame_id, enum bptype type)
7958 {
7959 struct breakpoint *b;
7960
7961 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
7962 tail-called one. */
7963 gdb_assert (!frame_id_artificial_p (frame_id));
7964
7965 b = set_raw_breakpoint (gdbarch, sal, type, &vtable_breakpoint_ops);
7966 b->enable_state = bp_enabled;
7967 b->disposition = disp_donttouch;
7968 b->frame_id = frame_id;
7969
7970 b->thread = inferior_thread ()->global_num;
7971
7972 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7973
7974 return breakpoint_up (b);
7975 }
7976
7977 /* Make a momentary breakpoint based on the master breakpoint ORIG.
7978 The new breakpoint will have type TYPE, use OPS as its
7979 breakpoint_ops, and will set enabled to LOC_ENABLED. */
7980
7981 static struct breakpoint *
7982 momentary_breakpoint_from_master (struct breakpoint *orig,
7983 enum bptype type,
7984 const struct breakpoint_ops *ops,
7985 int loc_enabled)
7986 {
7987 struct breakpoint *copy;
7988
7989 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
7990 copy->loc = allocate_bp_location (copy);
7991 set_breakpoint_location_function (copy->loc);
7992
7993 copy->loc->gdbarch = orig->loc->gdbarch;
7994 copy->loc->requested_address = orig->loc->requested_address;
7995 copy->loc->address = orig->loc->address;
7996 copy->loc->section = orig->loc->section;
7997 copy->loc->pspace = orig->loc->pspace;
7998 copy->loc->probe = orig->loc->probe;
7999 copy->loc->line_number = orig->loc->line_number;
8000 copy->loc->symtab = orig->loc->symtab;
8001 copy->loc->enabled = loc_enabled;
8002 copy->frame_id = orig->frame_id;
8003 copy->thread = orig->thread;
8004 copy->pspace = orig->pspace;
8005
8006 copy->enable_state = bp_enabled;
8007 copy->disposition = disp_donttouch;
8008 copy->number = internal_breakpoint_number--;
8009
8010 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8011 return copy;
8012 }
8013
8014 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8015 ORIG is NULL. */
8016
8017 struct breakpoint *
8018 clone_momentary_breakpoint (struct breakpoint *orig)
8019 {
8020 /* If there's nothing to clone, then return nothing. */
8021 if (orig == NULL)
8022 return NULL;
8023
8024 gdb_assert (orig->ops == &vtable_breakpoint_ops);
8025 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8026 }
8027
8028 breakpoint_up
8029 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8030 enum bptype type)
8031 {
8032 struct symtab_and_line sal;
8033
8034 sal = find_pc_line (pc, 0);
8035 sal.pc = pc;
8036 sal.section = find_pc_overlay (pc);
8037 sal.explicit_pc = 1;
8038
8039 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8040 }
8041 \f
8042
8043 /* Tell the user we have just set a breakpoint B. */
8044
8045 static void
8046 mention (struct breakpoint *b)
8047 {
8048 b->ops->print_mention (b);
8049 current_uiout->text ("\n");
8050 }
8051 \f
8052
8053 static bool bp_loc_is_permanent (struct bp_location *loc);
8054
8055 /* Handle "set breakpoint auto-hw on".
8056
8057 If the explicitly specified breakpoint type is not hardware
8058 breakpoint, check the memory map to see whether the breakpoint
8059 address is in read-only memory.
8060
8061 - location type is not hardware breakpoint, memory is read-only.
8062 We change the type of the location to hardware breakpoint.
8063
8064 - location type is hardware breakpoint, memory is read-write. This
8065 means we've previously made the location hardware one, but then the
8066 memory map changed, so we undo.
8067 */
8068
8069 static void
8070 handle_automatic_hardware_breakpoints (bp_location *bl)
8071 {
8072 if (automatic_hardware_breakpoints
8073 && bl->owner->type != bp_hardware_breakpoint
8074 && (bl->loc_type == bp_loc_software_breakpoint
8075 || bl->loc_type == bp_loc_hardware_breakpoint))
8076 {
8077 /* When breakpoints are removed, remove_breakpoints will use
8078 location types we've just set here, the only possible problem
8079 is that memory map has changed during running program, but
8080 it's not going to work anyway with current gdb. */
8081 mem_region *mr = lookup_mem_region (bl->address);
8082
8083 if (mr != nullptr)
8084 {
8085 enum bp_loc_type new_type;
8086
8087 if (mr->attrib.mode != MEM_RW)
8088 new_type = bp_loc_hardware_breakpoint;
8089 else
8090 new_type = bp_loc_software_breakpoint;
8091
8092 if (new_type != bl->loc_type)
8093 {
8094 static bool said = false;
8095
8096 bl->loc_type = new_type;
8097 if (!said)
8098 {
8099 gdb_printf (_("Note: automatically using "
8100 "hardware breakpoints for "
8101 "read-only addresses.\n"));
8102 said = true;
8103 }
8104 }
8105 }
8106 }
8107 }
8108
8109 static struct bp_location *
8110 add_location_to_breakpoint (struct breakpoint *b,
8111 const struct symtab_and_line *sal)
8112 {
8113 struct bp_location *loc, **tmp;
8114 CORE_ADDR adjusted_address;
8115 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8116
8117 if (loc_gdbarch == NULL)
8118 loc_gdbarch = b->gdbarch;
8119
8120 /* Adjust the breakpoint's address prior to allocating a location.
8121 Once we call allocate_bp_location(), that mostly uninitialized
8122 location will be placed on the location chain. Adjustment of the
8123 breakpoint may cause target_read_memory() to be called and we do
8124 not want its scan of the location chain to find a breakpoint and
8125 location that's only been partially initialized. */
8126 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8127 sal->pc, b->type);
8128
8129 /* Sort the locations by their ADDRESS. */
8130 loc = allocate_bp_location (b);
8131 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8132 tmp = &((*tmp)->next))
8133 ;
8134 loc->next = *tmp;
8135 *tmp = loc;
8136
8137 loc->requested_address = sal->pc;
8138 loc->address = adjusted_address;
8139 loc->pspace = sal->pspace;
8140 loc->probe.prob = sal->prob;
8141 loc->probe.objfile = sal->objfile;
8142 gdb_assert (loc->pspace != NULL);
8143 loc->section = sal->section;
8144 loc->gdbarch = loc_gdbarch;
8145 loc->line_number = sal->line;
8146 loc->symtab = sal->symtab;
8147 loc->symbol = sal->symbol;
8148 loc->msymbol = sal->msymbol;
8149 loc->objfile = sal->objfile;
8150
8151 set_breakpoint_location_function (loc);
8152
8153 /* While by definition, permanent breakpoints are already present in the
8154 code, we don't mark the location as inserted. Normally one would expect
8155 that GDB could rely on that breakpoint instruction to stop the program,
8156 thus removing the need to insert its own breakpoint, except that executing
8157 the breakpoint instruction can kill the target instead of reporting a
8158 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8159 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8160 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8161 breakpoint be inserted normally results in QEMU knowing about the GDB
8162 breakpoint, and thus trap before the breakpoint instruction is executed.
8163 (If GDB later needs to continue execution past the permanent breakpoint,
8164 it manually increments the PC, thus avoiding executing the breakpoint
8165 instruction.) */
8166 if (bp_loc_is_permanent (loc))
8167 loc->permanent = 1;
8168
8169 return loc;
8170 }
8171 \f
8172
8173 /* Return true if LOC is pointing to a permanent breakpoint,
8174 return false otherwise. */
8175
8176 static bool
8177 bp_loc_is_permanent (struct bp_location *loc)
8178 {
8179 gdb_assert (loc != NULL);
8180
8181 /* If we have a non-breakpoint-backed catchpoint or a software
8182 watchpoint, just return 0. We should not attempt to read from
8183 the addresses the locations of these breakpoint types point to.
8184 gdbarch_program_breakpoint_here_p, below, will attempt to read
8185 memory. */
8186 if (!bl_address_is_meaningful (loc))
8187 return false;
8188
8189 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8190 switch_to_program_space_and_thread (loc->pspace);
8191 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8192 }
8193
8194 /* Build a command list for the dprintf corresponding to the current
8195 settings of the dprintf style options. */
8196
8197 static void
8198 update_dprintf_command_list (struct breakpoint *b)
8199 {
8200 const char *dprintf_args = b->extra_string.get ();
8201 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8202
8203 if (!dprintf_args)
8204 return;
8205
8206 dprintf_args = skip_spaces (dprintf_args);
8207
8208 /* Allow a comma, as it may have terminated a location, but don't
8209 insist on it. */
8210 if (*dprintf_args == ',')
8211 ++dprintf_args;
8212 dprintf_args = skip_spaces (dprintf_args);
8213
8214 if (*dprintf_args != '"')
8215 error (_("Bad format string, missing '\"'."));
8216
8217 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8218 printf_line = xstrprintf ("printf %s", dprintf_args);
8219 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8220 {
8221 if (dprintf_function.empty ())
8222 error (_("No function supplied for dprintf call"));
8223
8224 if (!dprintf_channel.empty ())
8225 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8226 dprintf_function.c_str (),
8227 dprintf_channel.c_str (),
8228 dprintf_args);
8229 else
8230 printf_line = xstrprintf ("call (void) %s (%s)",
8231 dprintf_function.c_str (),
8232 dprintf_args);
8233 }
8234 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8235 {
8236 if (target_can_run_breakpoint_commands ())
8237 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8238 else
8239 {
8240 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8241 printf_line = xstrprintf ("printf %s", dprintf_args);
8242 }
8243 }
8244 else
8245 internal_error (__FILE__, __LINE__,
8246 _("Invalid dprintf style."));
8247
8248 gdb_assert (printf_line != NULL);
8249
8250 /* Manufacture a printf sequence. */
8251 struct command_line *printf_cmd_line
8252 = new struct command_line (simple_control, printf_line.release ());
8253 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8254 command_lines_deleter ()));
8255 }
8256
8257 /* Update all dprintf commands, making their command lists reflect
8258 current style settings. */
8259
8260 static void
8261 update_dprintf_commands (const char *args, int from_tty,
8262 struct cmd_list_element *c)
8263 {
8264 for (breakpoint *b : all_breakpoints ())
8265 if (b->type == bp_dprintf)
8266 update_dprintf_command_list (b);
8267 }
8268
8269 /* Create a breakpoint with SAL as location. Use LOCATION
8270 as a description of the location, and COND_STRING
8271 as condition expression. If LOCATION is NULL then create an
8272 "address location" from the address in the SAL. */
8273
8274 static void
8275 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8276 gdb::array_view<const symtab_and_line> sals,
8277 event_location_up &&location,
8278 gdb::unique_xmalloc_ptr<char> filter,
8279 gdb::unique_xmalloc_ptr<char> cond_string,
8280 gdb::unique_xmalloc_ptr<char> extra_string,
8281 enum bptype type, enum bpdisp disposition,
8282 int thread, int task, int ignore_count,
8283 const struct breakpoint_ops *ops, int from_tty,
8284 int enabled, int internal, unsigned flags,
8285 int display_canonical)
8286 {
8287 int i;
8288
8289 if (type == bp_hardware_breakpoint)
8290 {
8291 int target_resources_ok;
8292
8293 i = hw_breakpoint_used_count ();
8294 target_resources_ok =
8295 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8296 i + 1, 0);
8297 if (target_resources_ok == 0)
8298 error (_("No hardware breakpoint support in the target."));
8299 else if (target_resources_ok < 0)
8300 error (_("Hardware breakpoints used exceeds limit."));
8301 }
8302
8303 gdb_assert (!sals.empty ());
8304
8305 for (const auto &sal : sals)
8306 {
8307 struct bp_location *loc;
8308
8309 if (from_tty)
8310 {
8311 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8312 if (!loc_gdbarch)
8313 loc_gdbarch = gdbarch;
8314
8315 describe_other_breakpoints (loc_gdbarch,
8316 sal.pspace, sal.pc, sal.section, thread);
8317 }
8318
8319 if (&sal == &sals[0])
8320 {
8321 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8322 b->thread = thread;
8323 b->task = task;
8324
8325 b->cond_string = std::move (cond_string);
8326 b->extra_string = std::move (extra_string);
8327 b->ignore_count = ignore_count;
8328 b->enable_state = enabled ? bp_enabled : bp_disabled;
8329 b->disposition = disposition;
8330
8331 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8332 b->loc->inserted = 1;
8333
8334 if (type == bp_static_tracepoint)
8335 {
8336 struct tracepoint *t = (struct tracepoint *) b;
8337 struct static_tracepoint_marker marker;
8338
8339 if (strace_marker_p (b))
8340 {
8341 /* We already know the marker exists, otherwise, we
8342 wouldn't see a sal for it. */
8343 const char *p
8344 = &event_location_to_string (b->location.get ())[3];
8345 const char *endp;
8346
8347 p = skip_spaces (p);
8348
8349 endp = skip_to_space (p);
8350
8351 t->static_trace_marker_id.assign (p, endp - p);
8352
8353 gdb_printf (_("Probed static tracepoint "
8354 "marker \"%s\"\n"),
8355 t->static_trace_marker_id.c_str ());
8356 }
8357 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8358 {
8359 t->static_trace_marker_id = std::move (marker.str_id);
8360
8361 gdb_printf (_("Probed static tracepoint "
8362 "marker \"%s\"\n"),
8363 t->static_trace_marker_id.c_str ());
8364 }
8365 else
8366 warning (_("Couldn't determine the static "
8367 "tracepoint marker to probe"));
8368 }
8369
8370 loc = b->loc;
8371 }
8372 else
8373 {
8374 loc = add_location_to_breakpoint (b, &sal);
8375 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8376 loc->inserted = 1;
8377 }
8378
8379 /* Do not set breakpoint locations conditions yet. As locations
8380 are inserted, they get sorted based on their addresses. Let
8381 the list stabilize to have reliable location numbers. */
8382
8383 /* Dynamic printf requires and uses additional arguments on the
8384 command line, otherwise it's an error. */
8385 if (type == bp_dprintf)
8386 {
8387 if (b->extra_string)
8388 update_dprintf_command_list (b);
8389 else
8390 error (_("Format string required"));
8391 }
8392 else if (b->extra_string)
8393 error (_("Garbage '%s' at end of command"), b->extra_string.get ());
8394 }
8395
8396
8397 /* The order of the locations is now stable. Set the location
8398 condition using the location's number. */
8399 int loc_num = 1;
8400 for (bp_location *loc : b->locations ())
8401 {
8402 if (b->cond_string != nullptr)
8403 set_breakpoint_location_condition (b->cond_string.get (), loc,
8404 b->number, loc_num);
8405
8406 ++loc_num;
8407 }
8408
8409 b->display_canonical = display_canonical;
8410 if (location != NULL)
8411 b->location = std::move (location);
8412 else
8413 b->location = new_address_location (b->loc->address, NULL, 0);
8414 b->filter = std::move (filter);
8415 }
8416
8417 static void
8418 create_breakpoint_sal (struct gdbarch *gdbarch,
8419 gdb::array_view<const symtab_and_line> sals,
8420 event_location_up &&location,
8421 gdb::unique_xmalloc_ptr<char> filter,
8422 gdb::unique_xmalloc_ptr<char> cond_string,
8423 gdb::unique_xmalloc_ptr<char> extra_string,
8424 enum bptype type, enum bpdisp disposition,
8425 int thread, int task, int ignore_count,
8426 const struct breakpoint_ops *ops, int from_tty,
8427 int enabled, int internal, unsigned flags,
8428 int display_canonical)
8429 {
8430 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8431
8432 init_breakpoint_sal (b.get (), gdbarch,
8433 sals, std::move (location),
8434 std::move (filter),
8435 std::move (cond_string),
8436 std::move (extra_string),
8437 type, disposition,
8438 thread, task, ignore_count,
8439 ops, from_tty,
8440 enabled, internal, flags,
8441 display_canonical);
8442
8443 install_breakpoint (internal, std::move (b), 0);
8444 }
8445
8446 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8447 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8448 value. COND_STRING, if not NULL, specified the condition to be
8449 used for all breakpoints. Essentially the only case where
8450 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8451 function. In that case, it's still not possible to specify
8452 separate conditions for different overloaded functions, so
8453 we take just a single condition string.
8454
8455 NOTE: If the function succeeds, the caller is expected to cleanup
8456 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8457 array contents). If the function fails (error() is called), the
8458 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8459 COND and SALS arrays and each of those arrays contents. */
8460
8461 static void
8462 create_breakpoints_sal (struct gdbarch *gdbarch,
8463 struct linespec_result *canonical,
8464 gdb::unique_xmalloc_ptr<char> cond_string,
8465 gdb::unique_xmalloc_ptr<char> extra_string,
8466 enum bptype type, enum bpdisp disposition,
8467 int thread, int task, int ignore_count,
8468 const struct breakpoint_ops *ops, int from_tty,
8469 int enabled, int internal, unsigned flags)
8470 {
8471 if (canonical->pre_expanded)
8472 gdb_assert (canonical->lsals.size () == 1);
8473
8474 for (const auto &lsal : canonical->lsals)
8475 {
8476 /* Note that 'location' can be NULL in the case of a plain
8477 'break', without arguments. */
8478 event_location_up location
8479 = (canonical->location != NULL
8480 ? copy_event_location (canonical->location.get ()) : NULL);
8481 gdb::unique_xmalloc_ptr<char> filter_string
8482 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8483
8484 create_breakpoint_sal (gdbarch, lsal.sals,
8485 std::move (location),
8486 std::move (filter_string),
8487 std::move (cond_string),
8488 std::move (extra_string),
8489 type, disposition,
8490 thread, task, ignore_count, ops,
8491 from_tty, enabled, internal, flags,
8492 canonical->special_display);
8493 }
8494 }
8495
8496 /* Parse LOCATION which is assumed to be a SAL specification possibly
8497 followed by conditionals. On return, SALS contains an array of SAL
8498 addresses found. LOCATION points to the end of the SAL (for
8499 linespec locations).
8500
8501 The array and the line spec strings are allocated on the heap, it is
8502 the caller's responsibility to free them. */
8503
8504 static void
8505 parse_breakpoint_sals (struct event_location *location,
8506 struct linespec_result *canonical)
8507 {
8508 struct symtab_and_line cursal;
8509
8510 if (event_location_type (location) == LINESPEC_LOCATION)
8511 {
8512 const char *spec = get_linespec_location (location)->spec_string;
8513
8514 if (spec == NULL)
8515 {
8516 /* The last displayed codepoint, if it's valid, is our default
8517 breakpoint address. */
8518 if (last_displayed_sal_is_valid ())
8519 {
8520 /* Set sal's pspace, pc, symtab, and line to the values
8521 corresponding to the last call to print_frame_info.
8522 Be sure to reinitialize LINE with NOTCURRENT == 0
8523 as the breakpoint line number is inappropriate otherwise.
8524 find_pc_line would adjust PC, re-set it back. */
8525 symtab_and_line sal = get_last_displayed_sal ();
8526 CORE_ADDR pc = sal.pc;
8527
8528 sal = find_pc_line (pc, 0);
8529
8530 /* "break" without arguments is equivalent to "break *PC"
8531 where PC is the last displayed codepoint's address. So
8532 make sure to set sal.explicit_pc to prevent GDB from
8533 trying to expand the list of sals to include all other
8534 instances with the same symtab and line. */
8535 sal.pc = pc;
8536 sal.explicit_pc = 1;
8537
8538 struct linespec_sals lsal;
8539 lsal.sals = {sal};
8540 lsal.canonical = NULL;
8541
8542 canonical->lsals.push_back (std::move (lsal));
8543 return;
8544 }
8545 else
8546 error (_("No default breakpoint address now."));
8547 }
8548 }
8549
8550 /* Force almost all breakpoints to be in terms of the
8551 current_source_symtab (which is decode_line_1's default).
8552 This should produce the results we want almost all of the
8553 time while leaving default_breakpoint_* alone.
8554
8555 ObjC: However, don't match an Objective-C method name which
8556 may have a '+' or '-' succeeded by a '['. */
8557 cursal = get_current_source_symtab_and_line ();
8558 if (last_displayed_sal_is_valid ())
8559 {
8560 const char *spec = NULL;
8561
8562 if (event_location_type (location) == LINESPEC_LOCATION)
8563 spec = get_linespec_location (location)->spec_string;
8564
8565 if (!cursal.symtab
8566 || (spec != NULL
8567 && strchr ("+-", spec[0]) != NULL
8568 && spec[1] != '['))
8569 {
8570 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
8571 get_last_displayed_symtab (),
8572 get_last_displayed_line (),
8573 canonical, NULL, NULL);
8574 return;
8575 }
8576 }
8577
8578 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
8579 cursal.symtab, cursal.line, canonical, NULL, NULL);
8580 }
8581
8582
8583 /* Convert each SAL into a real PC. Verify that the PC can be
8584 inserted as a breakpoint. If it can't throw an error. */
8585
8586 static void
8587 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
8588 {
8589 for (auto &sal : sals)
8590 resolve_sal_pc (&sal);
8591 }
8592
8593 /* Fast tracepoints may have restrictions on valid locations. For
8594 instance, a fast tracepoint using a jump instead of a trap will
8595 likely have to overwrite more bytes than a trap would, and so can
8596 only be placed where the instruction is longer than the jump, or a
8597 multi-instruction sequence does not have a jump into the middle of
8598 it, etc. */
8599
8600 static void
8601 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8602 gdb::array_view<const symtab_and_line> sals)
8603 {
8604 for (const auto &sal : sals)
8605 {
8606 struct gdbarch *sarch;
8607
8608 sarch = get_sal_arch (sal);
8609 /* We fall back to GDBARCH if there is no architecture
8610 associated with SAL. */
8611 if (sarch == NULL)
8612 sarch = gdbarch;
8613 std::string msg;
8614 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
8615 error (_("May not have a fast tracepoint at %s%s"),
8616 paddress (sarch, sal.pc), msg.c_str ());
8617 }
8618 }
8619
8620 /* Given TOK, a string specification of condition and thread, as
8621 accepted by the 'break' command, extract the condition
8622 string and thread number and set *COND_STRING and *THREAD.
8623 PC identifies the context at which the condition should be parsed.
8624 If no condition is found, *COND_STRING is set to NULL.
8625 If no thread is found, *THREAD is set to -1. */
8626
8627 static void
8628 find_condition_and_thread (const char *tok, CORE_ADDR pc,
8629 gdb::unique_xmalloc_ptr<char> *cond_string,
8630 int *thread, int *task,
8631 gdb::unique_xmalloc_ptr<char> *rest)
8632 {
8633 cond_string->reset ();
8634 *thread = -1;
8635 *task = 0;
8636 rest->reset ();
8637 bool force = false;
8638
8639 while (tok && *tok)
8640 {
8641 const char *end_tok;
8642 int toklen;
8643 const char *cond_start = NULL;
8644 const char *cond_end = NULL;
8645
8646 tok = skip_spaces (tok);
8647
8648 if ((*tok == '"' || *tok == ',') && rest)
8649 {
8650 rest->reset (savestring (tok, strlen (tok)));
8651 return;
8652 }
8653
8654 end_tok = skip_to_space (tok);
8655
8656 toklen = end_tok - tok;
8657
8658 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8659 {
8660 tok = cond_start = end_tok + 1;
8661 try
8662 {
8663 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8664 }
8665 catch (const gdb_exception_error &)
8666 {
8667 if (!force)
8668 throw;
8669 else
8670 tok = tok + strlen (tok);
8671 }
8672 cond_end = tok;
8673 cond_string->reset (savestring (cond_start, cond_end - cond_start));
8674 }
8675 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8676 {
8677 tok = tok + toklen;
8678 force = true;
8679 }
8680 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8681 {
8682 const char *tmptok;
8683 struct thread_info *thr;
8684
8685 tok = end_tok + 1;
8686 thr = parse_thread_id (tok, &tmptok);
8687 if (tok == tmptok)
8688 error (_("Junk after thread keyword."));
8689 *thread = thr->global_num;
8690 tok = tmptok;
8691 }
8692 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8693 {
8694 char *tmptok;
8695
8696 tok = end_tok + 1;
8697 *task = strtol (tok, &tmptok, 0);
8698 if (tok == tmptok)
8699 error (_("Junk after task keyword."));
8700 if (!valid_task_id (*task))
8701 error (_("Unknown task %d."), *task);
8702 tok = tmptok;
8703 }
8704 else if (rest)
8705 {
8706 rest->reset (savestring (tok, strlen (tok)));
8707 return;
8708 }
8709 else
8710 error (_("Junk at end of arguments."));
8711 }
8712 }
8713
8714 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
8715 succeeds. The parsed values are written to COND_STRING, THREAD,
8716 TASK, and REST. See the comment of 'find_condition_and_thread'
8717 for the description of these parameters and INPUT. */
8718
8719 static void
8720 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
8721 const char *input,
8722 gdb::unique_xmalloc_ptr<char> *cond_string,
8723 int *thread, int *task,
8724 gdb::unique_xmalloc_ptr<char> *rest)
8725 {
8726 int num_failures = 0;
8727 for (auto &sal : sals)
8728 {
8729 gdb::unique_xmalloc_ptr<char> cond;
8730 int thread_id = 0;
8731 int task_id = 0;
8732 gdb::unique_xmalloc_ptr<char> remaining;
8733
8734 /* Here we want to parse 'arg' to separate condition from thread
8735 number. But because parsing happens in a context and the
8736 contexts of sals might be different, try each until there is
8737 success. Finding one successful parse is sufficient for our
8738 goal. When setting the breakpoint we'll re-parse the
8739 condition in the context of each sal. */
8740 try
8741 {
8742 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8743 &task_id, &remaining);
8744 *cond_string = std::move (cond);
8745 *thread = thread_id;
8746 *task = task_id;
8747 *rest = std::move (remaining);
8748 break;
8749 }
8750 catch (const gdb_exception_error &e)
8751 {
8752 num_failures++;
8753 /* If no sal remains, do not continue. */
8754 if (num_failures == sals.size ())
8755 throw;
8756 }
8757 }
8758 }
8759
8760 /* Decode a static tracepoint marker spec. */
8761
8762 static std::vector<symtab_and_line>
8763 decode_static_tracepoint_spec (const char **arg_p)
8764 {
8765 const char *p = &(*arg_p)[3];
8766 const char *endp;
8767
8768 p = skip_spaces (p);
8769
8770 endp = skip_to_space (p);
8771
8772 std::string marker_str (p, endp - p);
8773
8774 std::vector<static_tracepoint_marker> markers
8775 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8776 if (markers.empty ())
8777 error (_("No known static tracepoint marker named %s"),
8778 marker_str.c_str ());
8779
8780 std::vector<symtab_and_line> sals;
8781 sals.reserve (markers.size ());
8782
8783 for (const static_tracepoint_marker &marker : markers)
8784 {
8785 symtab_and_line sal = find_pc_line (marker.address, 0);
8786 sal.pc = marker.address;
8787 sals.push_back (sal);
8788 }
8789
8790 *arg_p = endp;
8791 return sals;
8792 }
8793
8794 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8795 according to IS_TRACEPOINT. */
8796
8797 static const struct breakpoint_ops *
8798 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
8799 bool is_tracepoint)
8800 {
8801 if (is_tracepoint)
8802 {
8803 if (location_type == PROBE_LOCATION)
8804 return &tracepoint_probe_breakpoint_ops;
8805 else
8806 return &vtable_breakpoint_ops;
8807 }
8808 else
8809 {
8810 if (location_type == PROBE_LOCATION)
8811 return &bkpt_probe_breakpoint_ops;
8812 else
8813 return &vtable_breakpoint_ops;
8814 }
8815 }
8816
8817 /* See breakpoint.h. */
8818
8819 const struct breakpoint_ops *
8820 breakpoint_ops_for_event_location (const struct event_location *location,
8821 bool is_tracepoint)
8822 {
8823 if (location != nullptr)
8824 return breakpoint_ops_for_event_location_type
8825 (event_location_type (location), is_tracepoint);
8826 return &vtable_breakpoint_ops;
8827 }
8828
8829 /* See breakpoint.h. */
8830
8831 int
8832 create_breakpoint (struct gdbarch *gdbarch,
8833 struct event_location *location,
8834 const char *cond_string,
8835 int thread, const char *extra_string,
8836 bool force_condition, int parse_extra,
8837 int tempflag, enum bptype type_wanted,
8838 int ignore_count,
8839 enum auto_boolean pending_break_support,
8840 const struct breakpoint_ops *ops,
8841 int from_tty, int enabled, int internal,
8842 unsigned flags)
8843 {
8844 struct linespec_result canonical;
8845 int pending = 0;
8846 int task = 0;
8847 int prev_bkpt_count = breakpoint_count;
8848
8849 gdb_assert (ops != NULL);
8850
8851 /* If extra_string isn't useful, set it to NULL. */
8852 if (extra_string != NULL && *extra_string == '\0')
8853 extra_string = NULL;
8854
8855 try
8856 {
8857 ops->create_sals_from_location (location, &canonical, type_wanted);
8858 }
8859 catch (const gdb_exception_error &e)
8860 {
8861 /* If caller is interested in rc value from parse, set
8862 value. */
8863 if (e.error == NOT_FOUND_ERROR)
8864 {
8865 /* If pending breakpoint support is turned off, throw
8866 error. */
8867
8868 if (pending_break_support == AUTO_BOOLEAN_FALSE)
8869 throw;
8870
8871 exception_print (gdb_stderr, e);
8872
8873 /* If pending breakpoint support is auto query and the user
8874 selects no, then simply return the error code. */
8875 if (pending_break_support == AUTO_BOOLEAN_AUTO
8876 && !nquery (_("Make %s pending on future shared library load? "),
8877 bptype_string (type_wanted)))
8878 return 0;
8879
8880 /* At this point, either the user was queried about setting
8881 a pending breakpoint and selected yes, or pending
8882 breakpoint behavior is on and thus a pending breakpoint
8883 is defaulted on behalf of the user. */
8884 pending = 1;
8885 }
8886 else
8887 throw;
8888 }
8889
8890 if (!pending && canonical.lsals.empty ())
8891 return 0;
8892
8893 /* Resolve all line numbers to PC's and verify that the addresses
8894 are ok for the target. */
8895 if (!pending)
8896 {
8897 for (auto &lsal : canonical.lsals)
8898 breakpoint_sals_to_pc (lsal.sals);
8899 }
8900
8901 /* Fast tracepoints may have additional restrictions on location. */
8902 if (!pending && type_wanted == bp_fast_tracepoint)
8903 {
8904 for (const auto &lsal : canonical.lsals)
8905 check_fast_tracepoint_sals (gdbarch, lsal.sals);
8906 }
8907
8908 /* Verify that condition can be parsed, before setting any
8909 breakpoints. Allocate a separate condition expression for each
8910 breakpoint. */
8911 if (!pending)
8912 {
8913 gdb::unique_xmalloc_ptr<char> cond_string_copy;
8914 gdb::unique_xmalloc_ptr<char> extra_string_copy;
8915
8916 if (parse_extra)
8917 {
8918 gdb::unique_xmalloc_ptr<char> rest;
8919 gdb::unique_xmalloc_ptr<char> cond;
8920
8921 const linespec_sals &lsal = canonical.lsals[0];
8922
8923 find_condition_and_thread_for_sals (lsal.sals, extra_string,
8924 &cond, &thread, &task, &rest);
8925 cond_string_copy = std::move (cond);
8926 extra_string_copy = std::move (rest);
8927 }
8928 else
8929 {
8930 if (type_wanted != bp_dprintf
8931 && extra_string != NULL && *extra_string != '\0')
8932 error (_("Garbage '%s' at end of location"), extra_string);
8933
8934 /* Check the validity of the condition. We should error out
8935 if the condition is invalid at all of the locations and
8936 if it is not forced. In the PARSE_EXTRA case above, this
8937 check is done when parsing the EXTRA_STRING. */
8938 if (cond_string != nullptr && !force_condition)
8939 {
8940 int num_failures = 0;
8941 const linespec_sals &lsal = canonical.lsals[0];
8942 for (const auto &sal : lsal.sals)
8943 {
8944 const char *cond = cond_string;
8945 try
8946 {
8947 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
8948 /* One success is sufficient to keep going. */
8949 break;
8950 }
8951 catch (const gdb_exception_error &)
8952 {
8953 num_failures++;
8954 /* If this is the last sal, error out. */
8955 if (num_failures == lsal.sals.size ())
8956 throw;
8957 }
8958 }
8959 }
8960
8961 /* Create a private copy of condition string. */
8962 if (cond_string)
8963 cond_string_copy.reset (xstrdup (cond_string));
8964 /* Create a private copy of any extra string. */
8965 if (extra_string)
8966 extra_string_copy.reset (xstrdup (extra_string));
8967 }
8968
8969 ops->create_breakpoints_sal (gdbarch, &canonical,
8970 std::move (cond_string_copy),
8971 std::move (extra_string_copy),
8972 type_wanted,
8973 tempflag ? disp_del : disp_donttouch,
8974 thread, task, ignore_count, ops,
8975 from_tty, enabled, internal, flags);
8976 }
8977 else
8978 {
8979 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
8980
8981 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
8982 b->location = copy_event_location (location);
8983
8984 if (parse_extra)
8985 b->cond_string = NULL;
8986 else
8987 {
8988 /* Create a private copy of condition string. */
8989 b->cond_string.reset (cond_string != NULL
8990 ? xstrdup (cond_string)
8991 : NULL);
8992 b->thread = thread;
8993 }
8994
8995 /* Create a private copy of any extra string. */
8996 b->extra_string.reset (extra_string != NULL
8997 ? xstrdup (extra_string)
8998 : NULL);
8999 b->ignore_count = ignore_count;
9000 b->disposition = tempflag ? disp_del : disp_donttouch;
9001 b->condition_not_parsed = 1;
9002 b->enable_state = enabled ? bp_enabled : bp_disabled;
9003 if ((type_wanted != bp_breakpoint
9004 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9005 b->pspace = current_program_space;
9006
9007 install_breakpoint (internal, std::move (b), 0);
9008 }
9009
9010 if (canonical.lsals.size () > 1)
9011 {
9012 warning (_("Multiple breakpoints were set.\nUse the "
9013 "\"delete\" command to delete unwanted breakpoints."));
9014 prev_breakpoint_count = prev_bkpt_count;
9015 }
9016
9017 update_global_location_list (UGLL_MAY_INSERT);
9018
9019 return 1;
9020 }
9021
9022 /* Set a breakpoint.
9023 ARG is a string describing breakpoint address,
9024 condition, and thread.
9025 FLAG specifies if a breakpoint is hardware on,
9026 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9027 and BP_TEMPFLAG. */
9028
9029 static void
9030 break_command_1 (const char *arg, int flag, int from_tty)
9031 {
9032 int tempflag = flag & BP_TEMPFLAG;
9033 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9034 ? bp_hardware_breakpoint
9035 : bp_breakpoint);
9036
9037 event_location_up location = string_to_event_location (&arg, current_language);
9038 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9039 (location.get (), false /* is_tracepoint */);
9040
9041 create_breakpoint (get_current_arch (),
9042 location.get (),
9043 NULL, 0, arg, false, 1 /* parse arg */,
9044 tempflag, type_wanted,
9045 0 /* Ignore count */,
9046 pending_break_support,
9047 ops,
9048 from_tty,
9049 1 /* enabled */,
9050 0 /* internal */,
9051 0);
9052 }
9053
9054 /* Helper function for break_command_1 and disassemble_command. */
9055
9056 void
9057 resolve_sal_pc (struct symtab_and_line *sal)
9058 {
9059 CORE_ADDR pc;
9060
9061 if (sal->pc == 0 && sal->symtab != NULL)
9062 {
9063 if (!find_line_pc (sal->symtab, sal->line, &pc))
9064 error (_("No line %d in file \"%s\"."),
9065 sal->line, symtab_to_filename_for_display (sal->symtab));
9066 sal->pc = pc;
9067
9068 /* If this SAL corresponds to a breakpoint inserted using a line
9069 number, then skip the function prologue if necessary. */
9070 if (sal->explicit_line)
9071 skip_prologue_sal (sal);
9072 }
9073
9074 if (sal->section == 0 && sal->symtab != NULL)
9075 {
9076 const struct blockvector *bv;
9077 const struct block *b;
9078 struct symbol *sym;
9079
9080 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9081 sal->symtab->compunit ());
9082 if (bv != NULL)
9083 {
9084 sym = block_linkage_function (b);
9085 if (sym != NULL)
9086 {
9087 fixup_symbol_section (sym, sal->symtab->compunit ()->objfile ());
9088 sal->section
9089 = sym->obj_section (sal->symtab->compunit ()->objfile ());
9090 }
9091 else
9092 {
9093 /* It really is worthwhile to have the section, so we'll
9094 just have to look harder. This case can be executed
9095 if we have line numbers but no functions (as can
9096 happen in assembly source). */
9097
9098 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9099 switch_to_program_space_and_thread (sal->pspace);
9100
9101 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9102 if (msym.minsym)
9103 sal->section = msym.obj_section ();
9104 }
9105 }
9106 }
9107 }
9108
9109 void
9110 break_command (const char *arg, int from_tty)
9111 {
9112 break_command_1 (arg, 0, from_tty);
9113 }
9114
9115 void
9116 tbreak_command (const char *arg, int from_tty)
9117 {
9118 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9119 }
9120
9121 static void
9122 hbreak_command (const char *arg, int from_tty)
9123 {
9124 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9125 }
9126
9127 static void
9128 thbreak_command (const char *arg, int from_tty)
9129 {
9130 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9131 }
9132
9133 /* The dynamic printf command is mostly like a regular breakpoint, but
9134 with a prewired command list consisting of a single output command,
9135 built from extra arguments supplied on the dprintf command
9136 line. */
9137
9138 static void
9139 dprintf_command (const char *arg, int from_tty)
9140 {
9141 event_location_up location = string_to_event_location (&arg, current_language);
9142
9143 /* If non-NULL, ARG should have been advanced past the location;
9144 the next character must be ','. */
9145 if (arg != NULL)
9146 {
9147 if (arg[0] != ',' || arg[1] == '\0')
9148 error (_("Format string required"));
9149 else
9150 {
9151 /* Skip the comma. */
9152 ++arg;
9153 }
9154 }
9155
9156 create_breakpoint (get_current_arch (),
9157 location.get (),
9158 NULL, 0, arg, false, 1 /* parse arg */,
9159 0, bp_dprintf,
9160 0 /* Ignore count */,
9161 pending_break_support,
9162 &dprintf_breakpoint_ops,
9163 from_tty,
9164 1 /* enabled */,
9165 0 /* internal */,
9166 0);
9167 }
9168
9169 static void
9170 agent_printf_command (const char *arg, int from_tty)
9171 {
9172 error (_("May only run agent-printf on the target"));
9173 }
9174
9175 /* Implement the "breakpoint_hit" breakpoint_ops method for
9176 ranged breakpoints. */
9177
9178 static int
9179 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9180 const address_space *aspace,
9181 CORE_ADDR bp_addr,
9182 const target_waitstatus &ws)
9183 {
9184 if (ws.kind () != TARGET_WAITKIND_STOPPED
9185 || ws.sig () != GDB_SIGNAL_TRAP)
9186 return 0;
9187
9188 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9189 bl->length, aspace, bp_addr);
9190 }
9191
9192 /* Implement the "resources_needed" breakpoint_ops method for
9193 ranged breakpoints. */
9194
9195 static int
9196 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9197 {
9198 return target_ranged_break_num_registers ();
9199 }
9200
9201 /* Implement the "print_it" breakpoint_ops method for
9202 ranged breakpoints. */
9203
9204 static enum print_stop_action
9205 print_it_ranged_breakpoint (bpstat *bs)
9206 {
9207 struct breakpoint *b = bs->breakpoint_at;
9208 struct bp_location *bl = b->loc;
9209 struct ui_out *uiout = current_uiout;
9210
9211 gdb_assert (b->type == bp_hardware_breakpoint);
9212
9213 /* Ranged breakpoints have only one location. */
9214 gdb_assert (bl && bl->next == NULL);
9215
9216 annotate_breakpoint (b->number);
9217
9218 maybe_print_thread_hit_breakpoint (uiout);
9219
9220 if (b->disposition == disp_del)
9221 uiout->text ("Temporary ranged breakpoint ");
9222 else
9223 uiout->text ("Ranged breakpoint ");
9224 if (uiout->is_mi_like_p ())
9225 {
9226 uiout->field_string ("reason",
9227 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9228 uiout->field_string ("disp", bpdisp_text (b->disposition));
9229 }
9230 uiout->field_signed ("bkptno", b->number);
9231 uiout->text (", ");
9232
9233 return PRINT_SRC_AND_LOC;
9234 }
9235
9236 /* Implement the "print_one" breakpoint_ops method for
9237 ranged breakpoints. */
9238
9239 static bool
9240 print_one_ranged_breakpoint (struct breakpoint *b,
9241 struct bp_location **last_loc)
9242 {
9243 struct bp_location *bl = b->loc;
9244 struct value_print_options opts;
9245 struct ui_out *uiout = current_uiout;
9246
9247 /* Ranged breakpoints have only one location. */
9248 gdb_assert (bl && bl->next == NULL);
9249
9250 get_user_print_options (&opts);
9251
9252 if (opts.addressprint)
9253 /* We don't print the address range here, it will be printed later
9254 by print_one_detail_ranged_breakpoint. */
9255 uiout->field_skip ("addr");
9256 annotate_field (5);
9257 print_breakpoint_location (b, bl);
9258 *last_loc = bl;
9259
9260 return true;
9261 }
9262
9263 /* Implement the "print_one_detail" breakpoint_ops method for
9264 ranged breakpoints. */
9265
9266 static void
9267 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9268 struct ui_out *uiout)
9269 {
9270 CORE_ADDR address_start, address_end;
9271 struct bp_location *bl = b->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" breakpoint_ops method for
9288 ranged breakpoints. */
9289
9290 static void
9291 print_mention_ranged_breakpoint (struct breakpoint *b)
9292 {
9293 struct bp_location *bl = b->loc;
9294 struct ui_out *uiout = current_uiout;
9295
9296 gdb_assert (bl);
9297 gdb_assert (b->type == bp_hardware_breakpoint);
9298
9299 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9300 b->number, paddress (bl->gdbarch, bl->address),
9301 paddress (bl->gdbarch, bl->address + bl->length - 1));
9302 }
9303
9304 /* Implement the "print_recreate" breakpoint_ops method for
9305 ranged breakpoints. */
9306
9307 static void
9308 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9309 {
9310 gdb_printf (fp, "break-range %s, %s",
9311 event_location_to_string (b->location.get ()),
9312 event_location_to_string (b->location_range_end.get ()));
9313 print_recreate_thread (b, fp);
9314 }
9315
9316 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9317
9318 static struct breakpoint_ops ranged_breakpoint_ops;
9319
9320 /* Find the address where the end of the breakpoint range should be
9321 placed, given the SAL of the end of the range. This is so that if
9322 the user provides a line number, the end of the range is set to the
9323 last instruction of the given line. */
9324
9325 static CORE_ADDR
9326 find_breakpoint_range_end (struct symtab_and_line sal)
9327 {
9328 CORE_ADDR end;
9329
9330 /* If the user provided a PC value, use it. Otherwise,
9331 find the address of the end of the given location. */
9332 if (sal.explicit_pc)
9333 end = sal.pc;
9334 else
9335 {
9336 int ret;
9337 CORE_ADDR start;
9338
9339 ret = find_line_pc_range (sal, &start, &end);
9340 if (!ret)
9341 error (_("Could not find location of the end of the range."));
9342
9343 /* find_line_pc_range returns the start of the next line. */
9344 end--;
9345 }
9346
9347 return end;
9348 }
9349
9350 /* Implement the "break-range" CLI command. */
9351
9352 static void
9353 break_range_command (const char *arg, int from_tty)
9354 {
9355 const char *arg_start;
9356 struct linespec_result canonical_start, canonical_end;
9357 int bp_count, can_use_bp, length;
9358 CORE_ADDR end;
9359 struct breakpoint *b;
9360
9361 /* We don't support software ranged breakpoints. */
9362 if (target_ranged_break_num_registers () < 0)
9363 error (_("This target does not support hardware ranged breakpoints."));
9364
9365 bp_count = hw_breakpoint_used_count ();
9366 bp_count += target_ranged_break_num_registers ();
9367 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9368 bp_count, 0);
9369 if (can_use_bp < 0)
9370 error (_("Hardware breakpoints used exceeds limit."));
9371
9372 arg = skip_spaces (arg);
9373 if (arg == NULL || arg[0] == '\0')
9374 error(_("No address range specified."));
9375
9376 arg_start = arg;
9377 event_location_up start_location = string_to_event_location (&arg,
9378 current_language);
9379 parse_breakpoint_sals (start_location.get (), &canonical_start);
9380
9381 if (arg[0] != ',')
9382 error (_("Too few arguments."));
9383 else if (canonical_start.lsals.empty ())
9384 error (_("Could not find location of the beginning of the range."));
9385
9386 const linespec_sals &lsal_start = canonical_start.lsals[0];
9387
9388 if (canonical_start.lsals.size () > 1
9389 || lsal_start.sals.size () != 1)
9390 error (_("Cannot create a ranged breakpoint with multiple locations."));
9391
9392 const symtab_and_line &sal_start = lsal_start.sals[0];
9393 std::string addr_string_start (arg_start, arg - arg_start);
9394
9395 arg++; /* Skip the comma. */
9396 arg = skip_spaces (arg);
9397
9398 /* Parse the end location. */
9399
9400 arg_start = arg;
9401
9402 /* We call decode_line_full directly here instead of using
9403 parse_breakpoint_sals because we need to specify the start location's
9404 symtab and line as the default symtab and line for the end of the
9405 range. This makes it possible to have ranges like "foo.c:27, +14",
9406 where +14 means 14 lines from the start location. */
9407 event_location_up end_location = string_to_event_location (&arg,
9408 current_language);
9409 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9410 sal_start.symtab, sal_start.line,
9411 &canonical_end, NULL, NULL);
9412
9413 if (canonical_end.lsals.empty ())
9414 error (_("Could not find location of the end of the range."));
9415
9416 const linespec_sals &lsal_end = canonical_end.lsals[0];
9417 if (canonical_end.lsals.size () > 1
9418 || lsal_end.sals.size () != 1)
9419 error (_("Cannot create a ranged breakpoint with multiple locations."));
9420
9421 const symtab_and_line &sal_end = lsal_end.sals[0];
9422
9423 end = find_breakpoint_range_end (sal_end);
9424 if (sal_start.pc > end)
9425 error (_("Invalid address range, end precedes start."));
9426
9427 length = end - sal_start.pc + 1;
9428 if (length < 0)
9429 /* Length overflowed. */
9430 error (_("Address range too large."));
9431 else if (length == 1)
9432 {
9433 /* This range is simple enough to be handled by
9434 the `hbreak' command. */
9435 hbreak_command (&addr_string_start[0], 1);
9436
9437 return;
9438 }
9439
9440 /* Now set up the breakpoint. */
9441 b = set_raw_breakpoint (get_current_arch (), sal_start,
9442 bp_hardware_breakpoint, &ranged_breakpoint_ops);
9443 set_breakpoint_count (breakpoint_count + 1);
9444 b->number = breakpoint_count;
9445 b->disposition = disp_donttouch;
9446 b->location = std::move (start_location);
9447 b->location_range_end = std::move (end_location);
9448 b->loc->length = length;
9449
9450 mention (b);
9451 gdb::observers::breakpoint_created.notify (b);
9452 update_global_location_list (UGLL_MAY_INSERT);
9453 }
9454
9455 /* Return non-zero if EXP is verified as constant. Returned zero
9456 means EXP is variable. Also the constant detection may fail for
9457 some constant expressions and in such case still falsely return
9458 zero. */
9459
9460 static bool
9461 watchpoint_exp_is_const (const struct expression *exp)
9462 {
9463 return exp->op->constant_p ();
9464 }
9465
9466 /* Implement the "re_set" method for watchpoints. */
9467
9468 void
9469 watchpoint::re_set ()
9470 {
9471 /* Watchpoint can be either on expression using entirely global
9472 variables, or it can be on local variables.
9473
9474 Watchpoints of the first kind are never auto-deleted, and even
9475 persist across program restarts. Since they can use variables
9476 from shared libraries, we need to reparse expression as libraries
9477 are loaded and unloaded.
9478
9479 Watchpoints on local variables can also change meaning as result
9480 of solib event. For example, if a watchpoint uses both a local
9481 and a global variables in expression, it's a local watchpoint,
9482 but unloading of a shared library will make the expression
9483 invalid. This is not a very common use case, but we still
9484 re-evaluate expression, to avoid surprises to the user.
9485
9486 Note that for local watchpoints, we re-evaluate it only if
9487 watchpoints frame id is still valid. If it's not, it means the
9488 watchpoint is out of scope and will be deleted soon. In fact,
9489 I'm not sure we'll ever be called in this case.
9490
9491 If a local watchpoint's frame id is still valid, then
9492 exp_valid_block is likewise valid, and we can safely use it.
9493
9494 Don't do anything about disabled watchpoints, since they will be
9495 reevaluated again when enabled. */
9496 update_watchpoint (this, 1 /* reparse */);
9497 }
9498
9499 /* Implement the "insert" method for hardware watchpoints. */
9500
9501 int
9502 watchpoint::insert_location (struct bp_location *bl)
9503 {
9504 int length = exact ? 1 : bl->length;
9505
9506 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9507 cond_exp.get ());
9508 }
9509
9510 /* Implement the "remove" method for hardware watchpoints. */
9511
9512 int
9513 watchpoint::remove_location (struct bp_location *bl,
9514 enum remove_bp_reason reason)
9515 {
9516 int length = exact ? 1 : bl->length;
9517
9518 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9519 cond_exp.get ());
9520 }
9521
9522 int
9523 watchpoint::breakpoint_hit (const struct bp_location *bl,
9524 const address_space *aspace, CORE_ADDR bp_addr,
9525 const target_waitstatus &ws)
9526 {
9527 struct breakpoint *b = bl->owner;
9528
9529 /* Continuable hardware watchpoints are treated as non-existent if the
9530 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9531 some data address). Otherwise gdb won't stop on a break instruction
9532 in the code (not from a breakpoint) when a hardware watchpoint has
9533 been defined. Also skip watchpoints which we know did not trigger
9534 (did not match the data address). */
9535 if (is_hardware_watchpoint (b)
9536 && watchpoint_triggered == watch_triggered_no)
9537 return 0;
9538
9539 return 1;
9540 }
9541
9542 void
9543 watchpoint::check_status (bpstat *bs)
9544 {
9545 gdb_assert (is_watchpoint (bs->breakpoint_at));
9546
9547 bpstat_check_watchpoint (bs);
9548 }
9549
9550 /* Implement the "resources_needed" method for hardware
9551 watchpoints. */
9552
9553 int
9554 watchpoint::resources_needed (const struct bp_location *bl)
9555 {
9556 int length = exact? 1 : bl->length;
9557
9558 return target_region_ok_for_hw_watchpoint (bl->address, length);
9559 }
9560
9561 /* Implement the "works_in_software_mode" method for hardware
9562 watchpoints. */
9563
9564 int
9565 watchpoint::works_in_software_mode () const
9566 {
9567 /* Read and access watchpoints only work with hardware support. */
9568 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9569 }
9570
9571 enum print_stop_action
9572 watchpoint::print_it (bpstat *bs)
9573 {
9574 struct breakpoint *b;
9575 enum print_stop_action result;
9576 struct ui_out *uiout = current_uiout;
9577
9578 gdb_assert (bs->bp_location_at != NULL);
9579
9580 b = bs->breakpoint_at;
9581
9582 annotate_watchpoint (b->number);
9583 maybe_print_thread_hit_breakpoint (uiout);
9584
9585 string_file stb;
9586
9587 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9588 switch (b->type)
9589 {
9590 case bp_watchpoint:
9591 case bp_hardware_watchpoint:
9592 if (uiout->is_mi_like_p ())
9593 uiout->field_string
9594 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9595 mention (b);
9596 tuple_emitter.emplace (uiout, "value");
9597 uiout->text ("\nOld value = ");
9598 watchpoint_value_print (bs->old_val.get (), &stb);
9599 uiout->field_stream ("old", stb);
9600 uiout->text ("\nNew value = ");
9601 watchpoint_value_print (val.get (), &stb);
9602 uiout->field_stream ("new", stb);
9603 uiout->text ("\n");
9604 /* More than one watchpoint may have been triggered. */
9605 result = PRINT_UNKNOWN;
9606 break;
9607
9608 case bp_read_watchpoint:
9609 if (uiout->is_mi_like_p ())
9610 uiout->field_string
9611 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9612 mention (b);
9613 tuple_emitter.emplace (uiout, "value");
9614 uiout->text ("\nValue = ");
9615 watchpoint_value_print (val.get (), &stb);
9616 uiout->field_stream ("value", stb);
9617 uiout->text ("\n");
9618 result = PRINT_UNKNOWN;
9619 break;
9620
9621 case bp_access_watchpoint:
9622 if (bs->old_val != NULL)
9623 {
9624 if (uiout->is_mi_like_p ())
9625 uiout->field_string
9626 ("reason",
9627 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9628 mention (b);
9629 tuple_emitter.emplace (uiout, "value");
9630 uiout->text ("\nOld value = ");
9631 watchpoint_value_print (bs->old_val.get (), &stb);
9632 uiout->field_stream ("old", stb);
9633 uiout->text ("\nNew value = ");
9634 }
9635 else
9636 {
9637 mention (b);
9638 if (uiout->is_mi_like_p ())
9639 uiout->field_string
9640 ("reason",
9641 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9642 tuple_emitter.emplace (uiout, "value");
9643 uiout->text ("\nValue = ");
9644 }
9645 watchpoint_value_print (val.get (), &stb);
9646 uiout->field_stream ("new", stb);
9647 uiout->text ("\n");
9648 result = PRINT_UNKNOWN;
9649 break;
9650 default:
9651 result = PRINT_UNKNOWN;
9652 }
9653
9654 return result;
9655 }
9656
9657 /* Implement the "print_mention" method for hardware watchpoints. */
9658
9659 void
9660 watchpoint::print_mention ()
9661 {
9662 struct ui_out *uiout = current_uiout;
9663 const char *tuple_name;
9664
9665 switch (type)
9666 {
9667 case bp_watchpoint:
9668 uiout->text ("Watchpoint ");
9669 tuple_name = "wpt";
9670 break;
9671 case bp_hardware_watchpoint:
9672 uiout->text ("Hardware watchpoint ");
9673 tuple_name = "wpt";
9674 break;
9675 case bp_read_watchpoint:
9676 uiout->text ("Hardware read watchpoint ");
9677 tuple_name = "hw-rwpt";
9678 break;
9679 case bp_access_watchpoint:
9680 uiout->text ("Hardware access (read/write) watchpoint ");
9681 tuple_name = "hw-awpt";
9682 break;
9683 default:
9684 internal_error (__FILE__, __LINE__,
9685 _("Invalid hardware watchpoint type."));
9686 }
9687
9688 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9689 uiout->field_signed ("number", number);
9690 uiout->text (": ");
9691 uiout->field_string ("exp", exp_string.get ());
9692 }
9693
9694 /* Implement the "print_recreate" method for watchpoints. */
9695
9696 void
9697 watchpoint::print_recreate (struct ui_file *fp)
9698 {
9699 switch (type)
9700 {
9701 case bp_watchpoint:
9702 case bp_hardware_watchpoint:
9703 gdb_printf (fp, "watch");
9704 break;
9705 case bp_read_watchpoint:
9706 gdb_printf (fp, "rwatch");
9707 break;
9708 case bp_access_watchpoint:
9709 gdb_printf (fp, "awatch");
9710 break;
9711 default:
9712 internal_error (__FILE__, __LINE__,
9713 _("Invalid watchpoint type."));
9714 }
9715
9716 gdb_printf (fp, " %s", exp_string.get ());
9717 print_recreate_thread (this, fp);
9718 }
9719
9720 /* Implement the "explains_signal" method for watchpoints. */
9721
9722 int
9723 watchpoint::explains_signal (enum gdb_signal sig)
9724 {
9725 /* A software watchpoint cannot cause a signal other than
9726 GDB_SIGNAL_TRAP. */
9727 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
9728 return 0;
9729
9730 return 1;
9731 }
9732
9733 struct masked_watchpoint : public watchpoint
9734 {
9735 int insert_location (struct bp_location *) override;
9736 int remove_location (struct bp_location *,
9737 enum remove_bp_reason reason) override;
9738 int resources_needed (const struct bp_location *) override;
9739 int works_in_software_mode () const override;
9740 enum print_stop_action print_it (struct bpstat *bs) override;
9741 void print_one_detail (struct ui_out *) const override;
9742 void print_mention () override;
9743 void print_recreate (struct ui_file *fp) override;
9744 };
9745
9746 /* Implement the "insert" method for masked hardware watchpoints. */
9747
9748 int
9749 masked_watchpoint::insert_location (struct bp_location *bl)
9750 {
9751 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
9752 bl->watchpoint_type);
9753 }
9754
9755 /* Implement the "remove" method for masked hardware watchpoints. */
9756
9757 int
9758 masked_watchpoint::remove_location (struct bp_location *bl,
9759 enum remove_bp_reason reason)
9760 {
9761 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
9762 bl->watchpoint_type);
9763 }
9764
9765 /* Implement the "resources_needed" method for masked hardware
9766 watchpoints. */
9767
9768 int
9769 masked_watchpoint::resources_needed (const struct bp_location *bl)
9770 {
9771 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
9772 }
9773
9774 /* Implement the "works_in_software_mode" method for masked hardware
9775 watchpoints. */
9776
9777 int
9778 masked_watchpoint::works_in_software_mode () const
9779 {
9780 return 0;
9781 }
9782
9783 /* Implement the "print_it" method for masked hardware
9784 watchpoints. */
9785
9786 enum print_stop_action
9787 masked_watchpoint::print_it (bpstat *bs)
9788 {
9789 struct breakpoint *b = bs->breakpoint_at;
9790 struct ui_out *uiout = current_uiout;
9791
9792 /* Masked watchpoints have only one location. */
9793 gdb_assert (b->loc && b->loc->next == NULL);
9794
9795 annotate_watchpoint (b->number);
9796 maybe_print_thread_hit_breakpoint (uiout);
9797
9798 switch (b->type)
9799 {
9800 case bp_hardware_watchpoint:
9801 if (uiout->is_mi_like_p ())
9802 uiout->field_string
9803 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9804 break;
9805
9806 case bp_read_watchpoint:
9807 if (uiout->is_mi_like_p ())
9808 uiout->field_string
9809 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9810 break;
9811
9812 case bp_access_watchpoint:
9813 if (uiout->is_mi_like_p ())
9814 uiout->field_string
9815 ("reason",
9816 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9817 break;
9818 default:
9819 internal_error (__FILE__, __LINE__,
9820 _("Invalid hardware watchpoint type."));
9821 }
9822
9823 mention (b);
9824 uiout->text (_("\n\
9825 Check the underlying instruction at PC for the memory\n\
9826 address and value which triggered this watchpoint.\n"));
9827 uiout->text ("\n");
9828
9829 /* More than one watchpoint may have been triggered. */
9830 return PRINT_UNKNOWN;
9831 }
9832
9833 /* Implement the "print_one_detail" method for masked hardware
9834 watchpoints. */
9835
9836 void
9837 masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9838 {
9839 /* Masked watchpoints have only one location. */
9840 gdb_assert (loc && loc->next == NULL);
9841
9842 uiout->text ("\tmask ");
9843 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
9844 uiout->text ("\n");
9845 }
9846
9847 /* Implement the "print_mention" method for masked hardware
9848 watchpoints. */
9849
9850 void
9851 masked_watchpoint::print_mention ()
9852 {
9853 struct ui_out *uiout = current_uiout;
9854 const char *tuple_name;
9855
9856 switch (type)
9857 {
9858 case bp_hardware_watchpoint:
9859 uiout->text ("Masked hardware watchpoint ");
9860 tuple_name = "wpt";
9861 break;
9862 case bp_read_watchpoint:
9863 uiout->text ("Masked hardware read watchpoint ");
9864 tuple_name = "hw-rwpt";
9865 break;
9866 case bp_access_watchpoint:
9867 uiout->text ("Masked hardware access (read/write) watchpoint ");
9868 tuple_name = "hw-awpt";
9869 break;
9870 default:
9871 internal_error (__FILE__, __LINE__,
9872 _("Invalid hardware watchpoint type."));
9873 }
9874
9875 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9876 uiout->field_signed ("number", number);
9877 uiout->text (": ");
9878 uiout->field_string ("exp", exp_string.get ());
9879 }
9880
9881 /* Implement the "print_recreate" method for masked hardware
9882 watchpoints. */
9883
9884 void
9885 masked_watchpoint::print_recreate (struct ui_file *fp)
9886 {
9887 switch (type)
9888 {
9889 case bp_hardware_watchpoint:
9890 gdb_printf (fp, "watch");
9891 break;
9892 case bp_read_watchpoint:
9893 gdb_printf (fp, "rwatch");
9894 break;
9895 case bp_access_watchpoint:
9896 gdb_printf (fp, "awatch");
9897 break;
9898 default:
9899 internal_error (__FILE__, __LINE__,
9900 _("Invalid hardware watchpoint type."));
9901 }
9902
9903 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
9904 phex (hw_wp_mask, sizeof (CORE_ADDR)));
9905 print_recreate_thread (this, fp);
9906 }
9907
9908 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
9909
9910 static bool
9911 is_masked_watchpoint (const struct breakpoint *b)
9912 {
9913 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9914 }
9915
9916 /* accessflag: hw_write: watch write,
9917 hw_read: watch read,
9918 hw_access: watch access (read or write) */
9919 static void
9920 watch_command_1 (const char *arg, int accessflag, int from_tty,
9921 bool just_location, bool internal)
9922 {
9923 struct breakpoint *scope_breakpoint = NULL;
9924 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
9925 struct value *result;
9926 int saved_bitpos = 0, saved_bitsize = 0;
9927 const char *exp_start = NULL;
9928 const char *exp_end = NULL;
9929 const char *tok, *end_tok;
9930 int toklen = -1;
9931 const char *cond_start = NULL;
9932 const char *cond_end = NULL;
9933 enum bptype bp_type;
9934 int thread = -1;
9935 /* Flag to indicate whether we are going to use masks for
9936 the hardware watchpoint. */
9937 bool use_mask = false;
9938 CORE_ADDR mask = 0;
9939 int task = 0;
9940
9941 /* Make sure that we actually have parameters to parse. */
9942 if (arg != NULL && arg[0] != '\0')
9943 {
9944 const char *value_start;
9945
9946 exp_end = arg + strlen (arg);
9947
9948 /* Look for "parameter value" pairs at the end
9949 of the arguments string. */
9950 for (tok = exp_end - 1; tok > arg; tok--)
9951 {
9952 /* Skip whitespace at the end of the argument list. */
9953 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9954 tok--;
9955
9956 /* Find the beginning of the last token.
9957 This is the value of the parameter. */
9958 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9959 tok--;
9960 value_start = tok + 1;
9961
9962 /* Skip whitespace. */
9963 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9964 tok--;
9965
9966 end_tok = tok;
9967
9968 /* Find the beginning of the second to last token.
9969 This is the parameter itself. */
9970 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9971 tok--;
9972 tok++;
9973 toklen = end_tok - tok + 1;
9974
9975 if (toklen == 6 && startswith (tok, "thread"))
9976 {
9977 struct thread_info *thr;
9978 /* At this point we've found a "thread" token, which means
9979 the user is trying to set a watchpoint that triggers
9980 only in a specific thread. */
9981 const char *endp;
9982
9983 if (thread != -1)
9984 error(_("You can specify only one thread."));
9985
9986 /* Extract the thread ID from the next token. */
9987 thr = parse_thread_id (value_start, &endp);
9988
9989 /* Check if the user provided a valid thread ID. */
9990 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9991 invalid_thread_id_error (value_start);
9992
9993 thread = thr->global_num;
9994 }
9995 else if (toklen == 4 && startswith (tok, "task"))
9996 {
9997 char *tmp;
9998
9999 task = strtol (value_start, &tmp, 0);
10000 if (tmp == value_start)
10001 error (_("Junk after task keyword."));
10002 if (!valid_task_id (task))
10003 error (_("Unknown task %d."), task);
10004 }
10005 else if (toklen == 4 && startswith (tok, "mask"))
10006 {
10007 /* We've found a "mask" token, which means the user wants to
10008 create a hardware watchpoint that is going to have the mask
10009 facility. */
10010 struct value *mask_value, *mark;
10011
10012 if (use_mask)
10013 error(_("You can specify only one mask."));
10014
10015 use_mask = just_location = true;
10016
10017 mark = value_mark ();
10018 mask_value = parse_to_comma_and_eval (&value_start);
10019 mask = value_as_address (mask_value);
10020 value_free_to_mark (mark);
10021 }
10022 else
10023 /* We didn't recognize what we found. We should stop here. */
10024 break;
10025
10026 /* Truncate the string and get rid of the "parameter value" pair before
10027 the arguments string is parsed by the parse_exp_1 function. */
10028 exp_end = tok;
10029 }
10030 }
10031 else
10032 exp_end = arg;
10033
10034 /* Parse the rest of the arguments. From here on out, everything
10035 is in terms of a newly allocated string instead of the original
10036 ARG. */
10037 std::string expression (arg, exp_end - arg);
10038 exp_start = arg = expression.c_str ();
10039 innermost_block_tracker tracker;
10040 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10041 exp_end = arg;
10042 /* Remove trailing whitespace from the expression before saving it.
10043 This makes the eventual display of the expression string a bit
10044 prettier. */
10045 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10046 --exp_end;
10047
10048 /* Checking if the expression is not constant. */
10049 if (watchpoint_exp_is_const (exp.get ()))
10050 {
10051 int len;
10052
10053 len = exp_end - exp_start;
10054 while (len > 0 && isspace (exp_start[len - 1]))
10055 len--;
10056 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10057 }
10058
10059 exp_valid_block = tracker.block ();
10060 struct value *mark = value_mark ();
10061 struct value *val_as_value = nullptr;
10062 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10063 just_location);
10064
10065 if (val_as_value != NULL && just_location)
10066 {
10067 saved_bitpos = value_bitpos (val_as_value);
10068 saved_bitsize = value_bitsize (val_as_value);
10069 }
10070
10071 value_ref_ptr val;
10072 if (just_location)
10073 {
10074 int ret;
10075
10076 exp_valid_block = NULL;
10077 val = release_value (value_addr (result));
10078 value_free_to_mark (mark);
10079
10080 if (use_mask)
10081 {
10082 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10083 mask);
10084 if (ret == -1)
10085 error (_("This target does not support masked watchpoints."));
10086 else if (ret == -2)
10087 error (_("Invalid mask or memory region."));
10088 }
10089 }
10090 else if (val_as_value != NULL)
10091 val = release_value (val_as_value);
10092
10093 tok = skip_spaces (arg);
10094 end_tok = skip_to_space (tok);
10095
10096 toklen = end_tok - tok;
10097 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10098 {
10099 tok = cond_start = end_tok + 1;
10100 innermost_block_tracker if_tracker;
10101 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10102
10103 /* The watchpoint expression may not be local, but the condition
10104 may still be. E.g.: `watch global if local > 0'. */
10105 cond_exp_valid_block = if_tracker.block ();
10106
10107 cond_end = tok;
10108 }
10109 if (*tok)
10110 error (_("Junk at end of command."));
10111
10112 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10113
10114 /* Save this because create_internal_breakpoint below invalidates
10115 'wp_frame'. */
10116 frame_id watchpoint_frame = get_frame_id (wp_frame);
10117
10118 /* If the expression is "local", then set up a "watchpoint scope"
10119 breakpoint at the point where we've left the scope of the watchpoint
10120 expression. Create the scope breakpoint before the watchpoint, so
10121 that we will encounter it first in bpstat_stop_status. */
10122 if (exp_valid_block != NULL && wp_frame != NULL)
10123 {
10124 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10125
10126 if (frame_id_p (caller_frame_id))
10127 {
10128 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10129 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10130
10131 scope_breakpoint
10132 = create_internal_breakpoint (caller_arch, caller_pc,
10133 bp_watchpoint_scope,
10134 &vtable_breakpoint_ops);
10135
10136 /* create_internal_breakpoint could invalidate WP_FRAME. */
10137 wp_frame = NULL;
10138
10139 scope_breakpoint->enable_state = bp_enabled;
10140
10141 /* Automatically delete the breakpoint when it hits. */
10142 scope_breakpoint->disposition = disp_del;
10143
10144 /* Only break in the proper frame (help with recursion). */
10145 scope_breakpoint->frame_id = caller_frame_id;
10146
10147 /* Set the address at which we will stop. */
10148 scope_breakpoint->loc->gdbarch = caller_arch;
10149 scope_breakpoint->loc->requested_address = caller_pc;
10150 scope_breakpoint->loc->address
10151 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10152 scope_breakpoint->loc->requested_address,
10153 scope_breakpoint->type);
10154 }
10155 }
10156
10157 /* Now set up the breakpoint. We create all watchpoints as hardware
10158 watchpoints here even if hardware watchpoints are turned off, a call
10159 to update_watchpoint later in this function will cause the type to
10160 drop back to bp_watchpoint (software watchpoint) if required. */
10161
10162 if (accessflag == hw_read)
10163 bp_type = bp_read_watchpoint;
10164 else if (accessflag == hw_access)
10165 bp_type = bp_access_watchpoint;
10166 else
10167 bp_type = bp_hardware_watchpoint;
10168
10169 std::unique_ptr<watchpoint> w;
10170 if (use_mask)
10171 w.reset (new masked_watchpoint ());
10172 else
10173 w.reset (new watchpoint ());
10174 init_raw_breakpoint_without_location (w.get (), nullptr, bp_type,
10175 &vtable_breakpoint_ops);
10176
10177 w->thread = thread;
10178 w->task = task;
10179 w->disposition = disp_donttouch;
10180 w->pspace = current_program_space;
10181 w->exp = std::move (exp);
10182 w->exp_valid_block = exp_valid_block;
10183 w->cond_exp_valid_block = cond_exp_valid_block;
10184 if (just_location)
10185 {
10186 struct type *t = value_type (val.get ());
10187 CORE_ADDR addr = value_as_address (val.get ());
10188
10189 w->exp_string_reparse
10190 = current_language->watch_location_expression (t, addr);
10191
10192 w->exp_string = xstrprintf ("-location %.*s",
10193 (int) (exp_end - exp_start), exp_start);
10194 }
10195 else
10196 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10197
10198 if (use_mask)
10199 {
10200 w->hw_wp_mask = mask;
10201 }
10202 else
10203 {
10204 w->val = val;
10205 w->val_bitpos = saved_bitpos;
10206 w->val_bitsize = saved_bitsize;
10207 w->val_valid = true;
10208 }
10209
10210 if (cond_start)
10211 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10212 else
10213 w->cond_string = 0;
10214
10215 if (frame_id_p (watchpoint_frame))
10216 {
10217 w->watchpoint_frame = watchpoint_frame;
10218 w->watchpoint_thread = inferior_ptid;
10219 }
10220 else
10221 {
10222 w->watchpoint_frame = null_frame_id;
10223 w->watchpoint_thread = null_ptid;
10224 }
10225
10226 if (scope_breakpoint != NULL)
10227 {
10228 /* The scope breakpoint is related to the watchpoint. We will
10229 need to act on them together. */
10230 w->related_breakpoint = scope_breakpoint;
10231 scope_breakpoint->related_breakpoint = w.get ();
10232 }
10233
10234 if (!just_location)
10235 value_free_to_mark (mark);
10236
10237 /* Finally update the new watchpoint. This creates the locations
10238 that should be inserted. */
10239 update_watchpoint (w.get (), 1);
10240
10241 install_breakpoint (internal, std::move (w), 1);
10242 }
10243
10244 /* Return count of debug registers needed to watch the given expression.
10245 If the watchpoint cannot be handled in hardware return zero. */
10246
10247 static int
10248 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10249 {
10250 int found_memory_cnt = 0;
10251
10252 /* Did the user specifically forbid us to use hardware watchpoints? */
10253 if (!can_use_hw_watchpoints)
10254 return 0;
10255
10256 gdb_assert (!vals.empty ());
10257 struct value *head = vals[0].get ();
10258
10259 /* Make sure that the value of the expression depends only upon
10260 memory contents, and values computed from them within GDB. If we
10261 find any register references or function calls, we can't use a
10262 hardware watchpoint.
10263
10264 The idea here is that evaluating an expression generates a series
10265 of values, one holding the value of every subexpression. (The
10266 expression a*b+c has five subexpressions: a, b, a*b, c, and
10267 a*b+c.) GDB's values hold almost enough information to establish
10268 the criteria given above --- they identify memory lvalues,
10269 register lvalues, computed values, etcetera. So we can evaluate
10270 the expression, and then scan the chain of values that leaves
10271 behind to decide whether we can detect any possible change to the
10272 expression's final value using only hardware watchpoints.
10273
10274 However, I don't think that the values returned by inferior
10275 function calls are special in any way. So this function may not
10276 notice that an expression involving an inferior function call
10277 can't be watched with hardware watchpoints. FIXME. */
10278 for (const value_ref_ptr &iter : vals)
10279 {
10280 struct value *v = iter.get ();
10281
10282 if (VALUE_LVAL (v) == lval_memory)
10283 {
10284 if (v != head && value_lazy (v))
10285 /* A lazy memory lvalue in the chain is one that GDB never
10286 needed to fetch; we either just used its address (e.g.,
10287 `a' in `a.b') or we never needed it at all (e.g., `a'
10288 in `a,b'). This doesn't apply to HEAD; if that is
10289 lazy then it was not readable, but watch it anyway. */
10290 ;
10291 else
10292 {
10293 /* Ahh, memory we actually used! Check if we can cover
10294 it with hardware watchpoints. */
10295 struct type *vtype = check_typedef (value_type (v));
10296
10297 /* We only watch structs and arrays if user asked for it
10298 explicitly, never if they just happen to appear in a
10299 middle of some value chain. */
10300 if (v == head
10301 || (vtype->code () != TYPE_CODE_STRUCT
10302 && vtype->code () != TYPE_CODE_ARRAY))
10303 {
10304 CORE_ADDR vaddr = value_address (v);
10305 int len;
10306 int num_regs;
10307
10308 len = (target_exact_watchpoints
10309 && is_scalar_type_recursive (vtype))?
10310 1 : TYPE_LENGTH (value_type (v));
10311
10312 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10313 if (!num_regs)
10314 return 0;
10315 else
10316 found_memory_cnt += num_regs;
10317 }
10318 }
10319 }
10320 else if (VALUE_LVAL (v) != not_lval
10321 && deprecated_value_modifiable (v) == 0)
10322 return 0; /* These are values from the history (e.g., $1). */
10323 else if (VALUE_LVAL (v) == lval_register)
10324 return 0; /* Cannot watch a register with a HW watchpoint. */
10325 }
10326
10327 /* The expression itself looks suitable for using a hardware
10328 watchpoint, but give the target machine a chance to reject it. */
10329 return found_memory_cnt;
10330 }
10331
10332 void
10333 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10334 {
10335 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10336 }
10337
10338 /* Options for the watch, awatch, and rwatch commands. */
10339
10340 struct watch_options
10341 {
10342 /* For -location. */
10343 bool location = false;
10344 };
10345
10346 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10347
10348 Historically GDB always accepted both '-location' and '-l' flags for
10349 these commands (both flags being synonyms). When converting to the
10350 newer option scheme only '-location' is added here. That's fine (for
10351 backward compatibility) as any non-ambiguous prefix of a flag will be
10352 accepted, so '-l', '-loc', are now all accepted.
10353
10354 What this means is that, if in the future, we add any new flag here
10355 that starts with '-l' then this will break backward compatibility, so
10356 please, don't do that! */
10357
10358 static const gdb::option::option_def watch_option_defs[] = {
10359 gdb::option::flag_option_def<watch_options> {
10360 "location",
10361 [] (watch_options *opt) { return &opt->location; },
10362 N_("\
10363 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10364 -l can be used as a short form of -location."),
10365 },
10366 };
10367
10368 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10369 commands. */
10370
10371 static gdb::option::option_def_group
10372 make_watch_options_def_group (watch_options *opts)
10373 {
10374 return {{watch_option_defs}, opts};
10375 }
10376
10377 /* A helper function that looks for the "-location" argument and then
10378 calls watch_command_1. */
10379
10380 static void
10381 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10382 {
10383 watch_options opts;
10384 auto grp = make_watch_options_def_group (&opts);
10385 gdb::option::process_options
10386 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10387 if (arg != nullptr && *arg == '\0')
10388 arg = nullptr;
10389
10390 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10391 }
10392
10393 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10394 static void
10395 watch_command_completer (struct cmd_list_element *ignore,
10396 completion_tracker &tracker,
10397 const char *text, const char * /*word*/)
10398 {
10399 const auto group = make_watch_options_def_group (nullptr);
10400 if (gdb::option::complete_options
10401 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10402 return;
10403
10404 const char *word = advance_to_expression_complete_word_point (tracker, text);
10405 expression_completer (ignore, tracker, text, word);
10406 }
10407
10408 static void
10409 watch_command (const char *arg, int from_tty)
10410 {
10411 watch_maybe_just_location (arg, hw_write, from_tty);
10412 }
10413
10414 void
10415 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10416 {
10417 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10418 }
10419
10420 static void
10421 rwatch_command (const char *arg, int from_tty)
10422 {
10423 watch_maybe_just_location (arg, hw_read, from_tty);
10424 }
10425
10426 void
10427 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
10428 {
10429 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10430 }
10431
10432 static void
10433 awatch_command (const char *arg, int from_tty)
10434 {
10435 watch_maybe_just_location (arg, hw_access, from_tty);
10436 }
10437 \f
10438
10439 /* Data for the FSM that manages the until(location)/advance commands
10440 in infcmd.c. Here because it uses the mechanisms of
10441 breakpoints. */
10442
10443 struct until_break_fsm : public thread_fsm
10444 {
10445 /* The thread that was current when the command was executed. */
10446 int thread;
10447
10448 /* The breakpoint set at the return address in the caller frame,
10449 plus breakpoints at all the destination locations. */
10450 std::vector<breakpoint_up> breakpoints;
10451
10452 until_break_fsm (struct interp *cmd_interp, int thread,
10453 std::vector<breakpoint_up> &&breakpoints)
10454 : thread_fsm (cmd_interp),
10455 thread (thread),
10456 breakpoints (std::move (breakpoints))
10457 {
10458 }
10459
10460 void clean_up (struct thread_info *thread) override;
10461 bool should_stop (struct thread_info *thread) override;
10462 enum async_reply_reason do_async_reply_reason () override;
10463 };
10464
10465 /* Implementation of the 'should_stop' FSM method for the
10466 until(location)/advance commands. */
10467
10468 bool
10469 until_break_fsm::should_stop (struct thread_info *tp)
10470 {
10471 for (const breakpoint_up &bp : breakpoints)
10472 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10473 bp.get ()) != NULL)
10474 {
10475 set_finished ();
10476 break;
10477 }
10478
10479 return true;
10480 }
10481
10482 /* Implementation of the 'clean_up' FSM method for the
10483 until(location)/advance commands. */
10484
10485 void
10486 until_break_fsm::clean_up (struct thread_info *)
10487 {
10488 /* Clean up our temporary breakpoints. */
10489 breakpoints.clear ();
10490 delete_longjmp_breakpoint (thread);
10491 }
10492
10493 /* Implementation of the 'async_reply_reason' FSM method for the
10494 until(location)/advance commands. */
10495
10496 enum async_reply_reason
10497 until_break_fsm::do_async_reply_reason ()
10498 {
10499 return EXEC_ASYNC_LOCATION_REACHED;
10500 }
10501
10502 void
10503 until_break_command (const char *arg, int from_tty, int anywhere)
10504 {
10505 struct frame_info *frame;
10506 struct gdbarch *frame_gdbarch;
10507 struct frame_id stack_frame_id;
10508 struct frame_id caller_frame_id;
10509 int thread;
10510 struct thread_info *tp;
10511
10512 clear_proceed_status (0);
10513
10514 /* Set a breakpoint where the user wants it and at return from
10515 this function. */
10516
10517 event_location_up location = string_to_event_location (&arg, current_language);
10518
10519 std::vector<symtab_and_line> sals
10520 = (last_displayed_sal_is_valid ()
10521 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10522 get_last_displayed_symtab (),
10523 get_last_displayed_line ())
10524 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
10525 NULL, NULL, 0));
10526
10527 if (sals.empty ())
10528 error (_("Couldn't get information on specified line."));
10529
10530 if (*arg)
10531 error (_("Junk at end of arguments."));
10532
10533 tp = inferior_thread ();
10534 thread = tp->global_num;
10535
10536 /* Note linespec handling above invalidates the frame chain.
10537 Installing a breakpoint also invalidates the frame chain (as it
10538 may need to switch threads), so do any frame handling before
10539 that. */
10540
10541 frame = get_selected_frame (NULL);
10542 frame_gdbarch = get_frame_arch (frame);
10543 stack_frame_id = get_stack_frame_id (frame);
10544 caller_frame_id = frame_unwind_caller_id (frame);
10545
10546 /* Keep within the current frame, or in frames called by the current
10547 one. */
10548
10549 std::vector<breakpoint_up> breakpoints;
10550
10551 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10552
10553 if (frame_id_p (caller_frame_id))
10554 {
10555 struct symtab_and_line sal2;
10556 struct gdbarch *caller_gdbarch;
10557
10558 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10559 sal2.pc = frame_unwind_caller_pc (frame);
10560 caller_gdbarch = frame_unwind_caller_arch (frame);
10561
10562 breakpoint_up caller_breakpoint
10563 = set_momentary_breakpoint (caller_gdbarch, sal2,
10564 caller_frame_id, bp_until);
10565 breakpoints.emplace_back (std::move (caller_breakpoint));
10566
10567 set_longjmp_breakpoint (tp, caller_frame_id);
10568 lj_deleter.emplace (thread);
10569 }
10570
10571 /* set_momentary_breakpoint could invalidate FRAME. */
10572 frame = NULL;
10573
10574 /* If the user told us to continue until a specified location, we
10575 don't specify a frame at which we need to stop. Otherwise,
10576 specify the selected frame, because we want to stop only at the
10577 very same frame. */
10578 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10579
10580 for (symtab_and_line &sal : sals)
10581 {
10582 resolve_sal_pc (&sal);
10583
10584 breakpoint_up location_breakpoint
10585 = set_momentary_breakpoint (frame_gdbarch, sal,
10586 stop_frame_id, bp_until);
10587 breakpoints.emplace_back (std::move (location_breakpoint));
10588 }
10589
10590 tp->set_thread_fsm
10591 (std::unique_ptr<thread_fsm>
10592 (new until_break_fsm (command_interp (), tp->global_num,
10593 std::move (breakpoints))));
10594
10595 if (lj_deleter)
10596 lj_deleter->release ();
10597
10598 proceed (-1, GDB_SIGNAL_DEFAULT);
10599 }
10600
10601 void
10602 init_ada_exception_breakpoint (struct breakpoint *b,
10603 struct gdbarch *gdbarch,
10604 struct symtab_and_line sal,
10605 const char *addr_string,
10606 const struct breakpoint_ops *ops,
10607 int tempflag,
10608 int enabled,
10609 int from_tty)
10610 {
10611 if (from_tty)
10612 {
10613 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
10614 if (!loc_gdbarch)
10615 loc_gdbarch = gdbarch;
10616
10617 describe_other_breakpoints (loc_gdbarch,
10618 sal.pspace, sal.pc, sal.section, -1);
10619 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
10620 version for exception catchpoints, because two catchpoints
10621 used for different exception names will use the same address.
10622 In this case, a "breakpoint ... also set at..." warning is
10623 unproductive. Besides, the warning phrasing is also a bit
10624 inappropriate, we should use the word catchpoint, and tell
10625 the user what type of catchpoint it is. The above is good
10626 enough for now, though. */
10627 }
10628
10629 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
10630
10631 b->enable_state = enabled ? bp_enabled : bp_disabled;
10632 b->disposition = tempflag ? disp_del : disp_donttouch;
10633 b->location = string_to_event_location (&addr_string,
10634 language_def (language_ada));
10635 b->language = language_ada;
10636 }
10637
10638 \f
10639
10640 /* Compare two breakpoints and return a strcmp-like result. */
10641
10642 static int
10643 compare_breakpoints (const breakpoint *a, const breakpoint *b)
10644 {
10645 uintptr_t ua = (uintptr_t) a;
10646 uintptr_t ub = (uintptr_t) b;
10647
10648 if (a->number < b->number)
10649 return -1;
10650 else if (a->number > b->number)
10651 return 1;
10652
10653 /* Now sort by address, in case we see, e..g, two breakpoints with
10654 the number 0. */
10655 if (ua < ub)
10656 return -1;
10657 return ua > ub ? 1 : 0;
10658 }
10659
10660 /* Delete breakpoints by address or line. */
10661
10662 static void
10663 clear_command (const char *arg, int from_tty)
10664 {
10665 int default_match;
10666
10667 std::vector<symtab_and_line> decoded_sals;
10668 symtab_and_line last_sal;
10669 gdb::array_view<symtab_and_line> sals;
10670 if (arg)
10671 {
10672 decoded_sals
10673 = decode_line_with_current_source (arg,
10674 (DECODE_LINE_FUNFIRSTLINE
10675 | DECODE_LINE_LIST_MODE));
10676 default_match = 0;
10677 sals = decoded_sals;
10678 }
10679 else
10680 {
10681 /* Set sal's line, symtab, pc, and pspace to the values
10682 corresponding to the last call to print_frame_info. If the
10683 codepoint is not valid, this will set all the fields to 0. */
10684 last_sal = get_last_displayed_sal ();
10685 if (last_sal.symtab == 0)
10686 error (_("No source file specified."));
10687
10688 default_match = 1;
10689 sals = last_sal;
10690 }
10691
10692 /* We don't call resolve_sal_pc here. That's not as bad as it
10693 seems, because all existing breakpoints typically have both
10694 file/line and pc set. So, if clear is given file/line, we can
10695 match this to existing breakpoint without obtaining pc at all.
10696
10697 We only support clearing given the address explicitly
10698 present in breakpoint table. Say, we've set breakpoint
10699 at file:line. There were several PC values for that file:line,
10700 due to optimization, all in one block.
10701
10702 We've picked one PC value. If "clear" is issued with another
10703 PC corresponding to the same file:line, the breakpoint won't
10704 be cleared. We probably can still clear the breakpoint, but
10705 since the other PC value is never presented to user, user
10706 can only find it by guessing, and it does not seem important
10707 to support that. */
10708
10709 /* For each line spec given, delete bps which correspond to it. Do
10710 it in two passes, solely to preserve the current behavior that
10711 from_tty is forced true if we delete more than one
10712 breakpoint. */
10713
10714 std::vector<struct breakpoint *> found;
10715 for (const auto &sal : sals)
10716 {
10717 const char *sal_fullname;
10718
10719 /* If exact pc given, clear bpts at that pc.
10720 If line given (pc == 0), clear all bpts on specified line.
10721 If defaulting, clear all bpts on default line
10722 or at default pc.
10723
10724 defaulting sal.pc != 0 tests to do
10725
10726 0 1 pc
10727 1 1 pc _and_ line
10728 0 0 line
10729 1 0 <can't happen> */
10730
10731 sal_fullname = (sal.symtab == NULL
10732 ? NULL : symtab_to_fullname (sal.symtab));
10733
10734 /* Find all matching breakpoints and add them to 'found'. */
10735 for (breakpoint *b : all_breakpoints ())
10736 {
10737 int match = 0;
10738 /* Are we going to delete b? */
10739 if (b->type != bp_none && !is_watchpoint (b)
10740 && user_breakpoint_p (b))
10741 {
10742 for (bp_location *loc : b->locations ())
10743 {
10744 /* If the user specified file:line, don't allow a PC
10745 match. This matches historical gdb behavior. */
10746 int pc_match = (!sal.explicit_line
10747 && sal.pc
10748 && (loc->pspace == sal.pspace)
10749 && (loc->address == sal.pc)
10750 && (!section_is_overlay (loc->section)
10751 || loc->section == sal.section));
10752 int line_match = 0;
10753
10754 if ((default_match || sal.explicit_line)
10755 && loc->symtab != NULL
10756 && sal_fullname != NULL
10757 && sal.pspace == loc->pspace
10758 && loc->line_number == sal.line
10759 && filename_cmp (symtab_to_fullname (loc->symtab),
10760 sal_fullname) == 0)
10761 line_match = 1;
10762
10763 if (pc_match || line_match)
10764 {
10765 match = 1;
10766 break;
10767 }
10768 }
10769 }
10770
10771 if (match)
10772 found.push_back (b);
10773 }
10774 }
10775
10776 /* Now go thru the 'found' chain and delete them. */
10777 if (found.empty ())
10778 {
10779 if (arg)
10780 error (_("No breakpoint at %s."), arg);
10781 else
10782 error (_("No breakpoint at this line."));
10783 }
10784
10785 /* Remove duplicates from the vec. */
10786 std::sort (found.begin (), found.end (),
10787 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10788 {
10789 return compare_breakpoints (bp_a, bp_b) < 0;
10790 });
10791 found.erase (std::unique (found.begin (), found.end (),
10792 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10793 {
10794 return compare_breakpoints (bp_a, bp_b) == 0;
10795 }),
10796 found.end ());
10797
10798 if (found.size () > 1)
10799 from_tty = 1; /* Always report if deleted more than one. */
10800 if (from_tty)
10801 {
10802 if (found.size () == 1)
10803 gdb_printf (_("Deleted breakpoint "));
10804 else
10805 gdb_printf (_("Deleted breakpoints "));
10806 }
10807
10808 for (breakpoint *iter : found)
10809 {
10810 if (from_tty)
10811 gdb_printf ("%d ", iter->number);
10812 delete_breakpoint (iter);
10813 }
10814 if (from_tty)
10815 gdb_putc ('\n');
10816 }
10817 \f
10818 /* Delete breakpoint in BS if they are `delete' breakpoints and
10819 all breakpoints that are marked for deletion, whether hit or not.
10820 This is called after any breakpoint is hit, or after errors. */
10821
10822 void
10823 breakpoint_auto_delete (bpstat *bs)
10824 {
10825 for (; bs; bs = bs->next)
10826 if (bs->breakpoint_at
10827 && bs->breakpoint_at->disposition == disp_del
10828 && bs->stop)
10829 delete_breakpoint (bs->breakpoint_at);
10830
10831 for (breakpoint *b : all_breakpoints_safe ())
10832 if (b->disposition == disp_del_at_next_stop)
10833 delete_breakpoint (b);
10834 }
10835
10836 /* A comparison function for bp_location AP and BP being interfaced to
10837 std::sort. Sort elements primarily by their ADDRESS (no matter what
10838 bl_address_is_meaningful says), secondarily by ordering first
10839 permanent elements and terciarily just ensuring the array is sorted
10840 stable way despite std::sort being an unstable algorithm. */
10841
10842 static int
10843 bp_location_is_less_than (const bp_location *a, const bp_location *b)
10844 {
10845 if (a->address != b->address)
10846 return a->address < b->address;
10847
10848 /* Sort locations at the same address by their pspace number, keeping
10849 locations of the same inferior (in a multi-inferior environment)
10850 grouped. */
10851
10852 if (a->pspace->num != b->pspace->num)
10853 return a->pspace->num < b->pspace->num;
10854
10855 /* Sort permanent breakpoints first. */
10856 if (a->permanent != b->permanent)
10857 return a->permanent > b->permanent;
10858
10859 /* Sort by type in order to make duplicate determination easier.
10860 See update_global_location_list. This is kept in sync with
10861 breakpoint_locations_match. */
10862 if (a->loc_type < b->loc_type)
10863 return true;
10864
10865 /* Likewise, for range-breakpoints, sort by length. */
10866 if (a->loc_type == bp_loc_hardware_breakpoint
10867 && b->loc_type == bp_loc_hardware_breakpoint
10868 && a->length < b->length)
10869 return true;
10870
10871 /* Make the internal GDB representation stable across GDB runs
10872 where A and B memory inside GDB can differ. Breakpoint locations of
10873 the same type at the same address can be sorted in arbitrary order. */
10874
10875 if (a->owner->number != b->owner->number)
10876 return a->owner->number < b->owner->number;
10877
10878 return a < b;
10879 }
10880
10881 /* Set bp_locations_placed_address_before_address_max and
10882 bp_locations_shadow_len_after_address_max according to the current
10883 content of the bp_locations array. */
10884
10885 static void
10886 bp_locations_target_extensions_update (void)
10887 {
10888 bp_locations_placed_address_before_address_max = 0;
10889 bp_locations_shadow_len_after_address_max = 0;
10890
10891 for (bp_location *bl : all_bp_locations ())
10892 {
10893 CORE_ADDR start, end, addr;
10894
10895 if (!bp_location_has_shadow (bl))
10896 continue;
10897
10898 start = bl->target_info.placed_address;
10899 end = start + bl->target_info.shadow_len;
10900
10901 gdb_assert (bl->address >= start);
10902 addr = bl->address - start;
10903 if (addr > bp_locations_placed_address_before_address_max)
10904 bp_locations_placed_address_before_address_max = addr;
10905
10906 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10907
10908 gdb_assert (bl->address < end);
10909 addr = end - bl->address;
10910 if (addr > bp_locations_shadow_len_after_address_max)
10911 bp_locations_shadow_len_after_address_max = addr;
10912 }
10913 }
10914
10915 /* Download tracepoint locations if they haven't been. */
10916
10917 static void
10918 download_tracepoint_locations (void)
10919 {
10920 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
10921
10922 scoped_restore_current_pspace_and_thread restore_pspace_thread;
10923
10924 for (breakpoint *b : all_tracepoints ())
10925 {
10926 struct tracepoint *t;
10927 int bp_location_downloaded = 0;
10928
10929 if ((b->type == bp_fast_tracepoint
10930 ? !may_insert_fast_tracepoints
10931 : !may_insert_tracepoints))
10932 continue;
10933
10934 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
10935 {
10936 if (target_can_download_tracepoint ())
10937 can_download_tracepoint = TRIBOOL_TRUE;
10938 else
10939 can_download_tracepoint = TRIBOOL_FALSE;
10940 }
10941
10942 if (can_download_tracepoint == TRIBOOL_FALSE)
10943 break;
10944
10945 for (bp_location *bl : b->locations ())
10946 {
10947 /* In tracepoint, locations are _never_ duplicated, so
10948 should_be_inserted is equivalent to
10949 unduplicated_should_be_inserted. */
10950 if (!should_be_inserted (bl) || bl->inserted)
10951 continue;
10952
10953 switch_to_program_space_and_thread (bl->pspace);
10954
10955 target_download_tracepoint (bl);
10956
10957 bl->inserted = 1;
10958 bp_location_downloaded = 1;
10959 }
10960 t = (struct tracepoint *) b;
10961 t->number_on_target = b->number;
10962 if (bp_location_downloaded)
10963 gdb::observers::breakpoint_modified.notify (b);
10964 }
10965 }
10966
10967 /* Swap the insertion/duplication state between two locations. */
10968
10969 static void
10970 swap_insertion (struct bp_location *left, struct bp_location *right)
10971 {
10972 const int left_inserted = left->inserted;
10973 const int left_duplicate = left->duplicate;
10974 const int left_needs_update = left->needs_update;
10975 const struct bp_target_info left_target_info = left->target_info;
10976
10977 /* Locations of tracepoints can never be duplicated. */
10978 if (is_tracepoint (left->owner))
10979 gdb_assert (!left->duplicate);
10980 if (is_tracepoint (right->owner))
10981 gdb_assert (!right->duplicate);
10982
10983 left->inserted = right->inserted;
10984 left->duplicate = right->duplicate;
10985 left->needs_update = right->needs_update;
10986 left->target_info = right->target_info;
10987 right->inserted = left_inserted;
10988 right->duplicate = left_duplicate;
10989 right->needs_update = left_needs_update;
10990 right->target_info = left_target_info;
10991 }
10992
10993 /* Force the re-insertion of the locations at ADDRESS. This is called
10994 once a new/deleted/modified duplicate location is found and we are evaluating
10995 conditions on the target's side. Such conditions need to be updated on
10996 the target. */
10997
10998 static void
10999 force_breakpoint_reinsertion (struct bp_location *bl)
11000 {
11001 CORE_ADDR address = 0;
11002 int pspace_num;
11003
11004 address = bl->address;
11005 pspace_num = bl->pspace->num;
11006
11007 /* This is only meaningful if the target is
11008 evaluating conditions and if the user has
11009 opted for condition evaluation on the target's
11010 side. */
11011 if (gdb_evaluates_breakpoint_condition_p ()
11012 || !target_supports_evaluation_of_breakpoint_conditions ())
11013 return;
11014
11015 /* Flag all breakpoint locations with this address and
11016 the same program space as the location
11017 as "its condition has changed". We need to
11018 update the conditions on the target's side. */
11019 for (bp_location *loc : all_bp_locations_at_addr (address))
11020 {
11021 if (!is_breakpoint (loc->owner)
11022 || pspace_num != loc->pspace->num)
11023 continue;
11024
11025 /* Flag the location appropriately. We use a different state to
11026 let everyone know that we already updated the set of locations
11027 with addr bl->address and program space bl->pspace. This is so
11028 we don't have to keep calling these functions just to mark locations
11029 that have already been marked. */
11030 loc->condition_changed = condition_updated;
11031
11032 /* Free the agent expression bytecode as well. We will compute
11033 it later on. */
11034 loc->cond_bytecode.reset ();
11035 }
11036 }
11037
11038 /* Called whether new breakpoints are created, or existing breakpoints
11039 deleted, to update the global location list and recompute which
11040 locations are duplicate of which.
11041
11042 The INSERT_MODE flag determines whether locations may not, may, or
11043 shall be inserted now. See 'enum ugll_insert_mode' for more
11044 info. */
11045
11046 static void
11047 update_global_location_list (enum ugll_insert_mode insert_mode)
11048 {
11049 /* Last breakpoint location address that was marked for update. */
11050 CORE_ADDR last_addr = 0;
11051 /* Last breakpoint location program space that was marked for update. */
11052 int last_pspace_num = -1;
11053
11054 /* Used in the duplicates detection below. When iterating over all
11055 bp_locations, points to the first bp_location of a given address.
11056 Breakpoints and watchpoints of different types are never
11057 duplicates of each other. Keep one pointer for each type of
11058 breakpoint/watchpoint, so we only need to loop over all locations
11059 once. */
11060 struct bp_location *bp_loc_first; /* breakpoint */
11061 struct bp_location *wp_loc_first; /* hardware watchpoint */
11062 struct bp_location *awp_loc_first; /* access watchpoint */
11063 struct bp_location *rwp_loc_first; /* read watchpoint */
11064
11065 /* Saved former bp_locations array which we compare against the newly
11066 built bp_locations from the current state of ALL_BREAKPOINTS. */
11067 std::vector<bp_location *> old_locations = std::move (bp_locations);
11068 bp_locations.clear ();
11069
11070 for (breakpoint *b : all_breakpoints ())
11071 for (bp_location *loc : b->locations ())
11072 bp_locations.push_back (loc);
11073
11074 /* See if we need to "upgrade" a software breakpoint to a hardware
11075 breakpoint. Do this before deciding whether locations are
11076 duplicates. Also do this before sorting because sorting order
11077 depends on location type. */
11078 for (bp_location *loc : bp_locations)
11079 if (!loc->inserted && should_be_inserted (loc))
11080 handle_automatic_hardware_breakpoints (loc);
11081
11082 std::sort (bp_locations.begin (), bp_locations.end (),
11083 bp_location_is_less_than);
11084
11085 bp_locations_target_extensions_update ();
11086
11087 /* Identify bp_location instances that are no longer present in the
11088 new list, and therefore should be freed. Note that it's not
11089 necessary that those locations should be removed from inferior --
11090 if there's another location at the same address (previously
11091 marked as duplicate), we don't need to remove/insert the
11092 location.
11093
11094 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11095 and former bp_location array state respectively. */
11096
11097 size_t loc_i = 0;
11098 for (bp_location *old_loc : old_locations)
11099 {
11100 /* Tells if 'old_loc' is found among the new locations. If
11101 not, we have to free it. */
11102 int found_object = 0;
11103 /* Tells if the location should remain inserted in the target. */
11104 int keep_in_target = 0;
11105 int removed = 0;
11106
11107 /* Skip LOCP entries which will definitely never be needed.
11108 Stop either at or being the one matching OLD_LOC. */
11109 while (loc_i < bp_locations.size ()
11110 && bp_locations[loc_i]->address < old_loc->address)
11111 loc_i++;
11112
11113 for (size_t loc2_i = loc_i;
11114 (loc2_i < bp_locations.size ()
11115 && bp_locations[loc2_i]->address == old_loc->address);
11116 loc2_i++)
11117 {
11118 /* Check if this is a new/duplicated location or a duplicated
11119 location that had its condition modified. If so, we want to send
11120 its condition to the target if evaluation of conditions is taking
11121 place there. */
11122 if (bp_locations[loc2_i]->condition_changed == condition_modified
11123 && (last_addr != old_loc->address
11124 || last_pspace_num != old_loc->pspace->num))
11125 {
11126 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11127 last_pspace_num = old_loc->pspace->num;
11128 }
11129
11130 if (bp_locations[loc2_i] == old_loc)
11131 found_object = 1;
11132 }
11133
11134 /* We have already handled this address, update it so that we don't
11135 have to go through updates again. */
11136 last_addr = old_loc->address;
11137
11138 /* Target-side condition evaluation: Handle deleted locations. */
11139 if (!found_object)
11140 force_breakpoint_reinsertion (old_loc);
11141
11142 /* If this location is no longer present, and inserted, look if
11143 there's maybe a new location at the same address. If so,
11144 mark that one inserted, and don't remove this one. This is
11145 needed so that we don't have a time window where a breakpoint
11146 at certain location is not inserted. */
11147
11148 if (old_loc->inserted)
11149 {
11150 /* If the location is inserted now, we might have to remove
11151 it. */
11152
11153 if (found_object && should_be_inserted (old_loc))
11154 {
11155 /* The location is still present in the location list,
11156 and still should be inserted. Don't do anything. */
11157 keep_in_target = 1;
11158 }
11159 else
11160 {
11161 /* This location still exists, but it won't be kept in the
11162 target since it may have been disabled. We proceed to
11163 remove its target-side condition. */
11164
11165 /* The location is either no longer present, or got
11166 disabled. See if there's another location at the
11167 same address, in which case we don't need to remove
11168 this one from the target. */
11169
11170 /* OLD_LOC comes from existing struct breakpoint. */
11171 if (bl_address_is_meaningful (old_loc))
11172 {
11173 for (size_t loc2_i = loc_i;
11174 (loc2_i < bp_locations.size ()
11175 && bp_locations[loc2_i]->address == old_loc->address);
11176 loc2_i++)
11177 {
11178 bp_location *loc2 = bp_locations[loc2_i];
11179
11180 if (loc2 == old_loc)
11181 continue;
11182
11183 if (breakpoint_locations_match (loc2, old_loc))
11184 {
11185 /* Read watchpoint locations are switched to
11186 access watchpoints, if the former are not
11187 supported, but the latter are. */
11188 if (is_hardware_watchpoint (old_loc->owner))
11189 {
11190 gdb_assert (is_hardware_watchpoint (loc2->owner));
11191 loc2->watchpoint_type = old_loc->watchpoint_type;
11192 }
11193
11194 /* loc2 is a duplicated location. We need to check
11195 if it should be inserted in case it will be
11196 unduplicated. */
11197 if (unduplicated_should_be_inserted (loc2))
11198 {
11199 swap_insertion (old_loc, loc2);
11200 keep_in_target = 1;
11201 break;
11202 }
11203 }
11204 }
11205 }
11206 }
11207
11208 if (!keep_in_target)
11209 {
11210 if (remove_breakpoint (old_loc))
11211 {
11212 /* This is just about all we can do. We could keep
11213 this location on the global list, and try to
11214 remove it next time, but there's no particular
11215 reason why we will succeed next time.
11216
11217 Note that at this point, old_loc->owner is still
11218 valid, as delete_breakpoint frees the breakpoint
11219 only after calling us. */
11220 gdb_printf (_("warning: Error removing "
11221 "breakpoint %d\n"),
11222 old_loc->owner->number);
11223 }
11224 removed = 1;
11225 }
11226 }
11227
11228 if (!found_object)
11229 {
11230 if (removed && target_is_non_stop_p ()
11231 && need_moribund_for_location_type (old_loc))
11232 {
11233 /* This location was removed from the target. In
11234 non-stop mode, a race condition is possible where
11235 we've removed a breakpoint, but stop events for that
11236 breakpoint are already queued and will arrive later.
11237 We apply an heuristic to be able to distinguish such
11238 SIGTRAPs from other random SIGTRAPs: we keep this
11239 breakpoint location for a bit, and will retire it
11240 after we see some number of events. The theory here
11241 is that reporting of events should, "on the average",
11242 be fair, so after a while we'll see events from all
11243 threads that have anything of interest, and no longer
11244 need to keep this breakpoint location around. We
11245 don't hold locations forever so to reduce chances of
11246 mistaking a non-breakpoint SIGTRAP for a breakpoint
11247 SIGTRAP.
11248
11249 The heuristic failing can be disastrous on
11250 decr_pc_after_break targets.
11251
11252 On decr_pc_after_break targets, like e.g., x86-linux,
11253 if we fail to recognize a late breakpoint SIGTRAP,
11254 because events_till_retirement has reached 0 too
11255 soon, we'll fail to do the PC adjustment, and report
11256 a random SIGTRAP to the user. When the user resumes
11257 the inferior, it will most likely immediately crash
11258 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11259 corrupted, because of being resumed e.g., in the
11260 middle of a multi-byte instruction, or skipped a
11261 one-byte instruction. This was actually seen happen
11262 on native x86-linux, and should be less rare on
11263 targets that do not support new thread events, like
11264 remote, due to the heuristic depending on
11265 thread_count.
11266
11267 Mistaking a random SIGTRAP for a breakpoint trap
11268 causes similar symptoms (PC adjustment applied when
11269 it shouldn't), but then again, playing with SIGTRAPs
11270 behind the debugger's back is asking for trouble.
11271
11272 Since hardware watchpoint traps are always
11273 distinguishable from other traps, so we don't need to
11274 apply keep hardware watchpoint moribund locations
11275 around. We simply always ignore hardware watchpoint
11276 traps we can no longer explain. */
11277
11278 process_stratum_target *proc_target = nullptr;
11279 for (inferior *inf : all_inferiors ())
11280 if (inf->pspace == old_loc->pspace)
11281 {
11282 proc_target = inf->process_target ();
11283 break;
11284 }
11285 if (proc_target != nullptr)
11286 old_loc->events_till_retirement
11287 = 3 * (thread_count (proc_target) + 1);
11288 else
11289 old_loc->events_till_retirement = 1;
11290 old_loc->owner = NULL;
11291
11292 moribund_locations.push_back (old_loc);
11293 }
11294 else
11295 {
11296 old_loc->owner = NULL;
11297 decref_bp_location (&old_loc);
11298 }
11299 }
11300 }
11301
11302 /* Rescan breakpoints at the same address and section, marking the
11303 first one as "first" and any others as "duplicates". This is so
11304 that the bpt instruction is only inserted once. If we have a
11305 permanent breakpoint at the same place as BPT, make that one the
11306 official one, and the rest as duplicates. Permanent breakpoints
11307 are sorted first for the same address.
11308
11309 Do the same for hardware watchpoints, but also considering the
11310 watchpoint's type (regular/access/read) and length. */
11311
11312 bp_loc_first = NULL;
11313 wp_loc_first = NULL;
11314 awp_loc_first = NULL;
11315 rwp_loc_first = NULL;
11316
11317 for (bp_location *loc : all_bp_locations ())
11318 {
11319 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11320 non-NULL. */
11321 struct bp_location **loc_first_p;
11322 breakpoint *b = loc->owner;
11323
11324 if (!unduplicated_should_be_inserted (loc)
11325 || !bl_address_is_meaningful (loc)
11326 /* Don't detect duplicate for tracepoint locations because they are
11327 never duplicated. See the comments in field `duplicate' of
11328 `struct bp_location'. */
11329 || is_tracepoint (b))
11330 {
11331 /* Clear the condition modification flag. */
11332 loc->condition_changed = condition_unchanged;
11333 continue;
11334 }
11335
11336 if (b->type == bp_hardware_watchpoint)
11337 loc_first_p = &wp_loc_first;
11338 else if (b->type == bp_read_watchpoint)
11339 loc_first_p = &rwp_loc_first;
11340 else if (b->type == bp_access_watchpoint)
11341 loc_first_p = &awp_loc_first;
11342 else
11343 loc_first_p = &bp_loc_first;
11344
11345 if (*loc_first_p == NULL
11346 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11347 || !breakpoint_locations_match (loc, *loc_first_p))
11348 {
11349 *loc_first_p = loc;
11350 loc->duplicate = 0;
11351
11352 if (is_breakpoint (loc->owner) && loc->condition_changed)
11353 {
11354 loc->needs_update = 1;
11355 /* Clear the condition modification flag. */
11356 loc->condition_changed = condition_unchanged;
11357 }
11358 continue;
11359 }
11360
11361
11362 /* This and the above ensure the invariant that the first location
11363 is not duplicated, and is the inserted one.
11364 All following are marked as duplicated, and are not inserted. */
11365 if (loc->inserted)
11366 swap_insertion (loc, *loc_first_p);
11367 loc->duplicate = 1;
11368
11369 /* Clear the condition modification flag. */
11370 loc->condition_changed = condition_unchanged;
11371 }
11372
11373 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11374 {
11375 if (insert_mode != UGLL_DONT_INSERT)
11376 insert_breakpoint_locations ();
11377 else
11378 {
11379 /* Even though the caller told us to not insert new
11380 locations, we may still need to update conditions on the
11381 target's side of breakpoints that were already inserted
11382 if the target is evaluating breakpoint conditions. We
11383 only update conditions for locations that are marked
11384 "needs_update". */
11385 update_inserted_breakpoint_locations ();
11386 }
11387 }
11388
11389 if (insert_mode != UGLL_DONT_INSERT)
11390 download_tracepoint_locations ();
11391 }
11392
11393 void
11394 breakpoint_retire_moribund (void)
11395 {
11396 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11397 {
11398 struct bp_location *loc = moribund_locations[ix];
11399 if (--(loc->events_till_retirement) == 0)
11400 {
11401 decref_bp_location (&loc);
11402 unordered_remove (moribund_locations, ix);
11403 --ix;
11404 }
11405 }
11406 }
11407
11408 static void
11409 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11410 {
11411
11412 try
11413 {
11414 update_global_location_list (insert_mode);
11415 }
11416 catch (const gdb_exception_error &e)
11417 {
11418 }
11419 }
11420
11421 /* Clear BKP from a BPS. */
11422
11423 static void
11424 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
11425 {
11426 bpstat *bs;
11427
11428 for (bs = bps; bs; bs = bs->next)
11429 if (bs->breakpoint_at == bpt)
11430 {
11431 bs->breakpoint_at = NULL;
11432 bs->old_val = NULL;
11433 /* bs->commands will be freed later. */
11434 }
11435 }
11436
11437 /* Callback for iterate_over_threads. */
11438 static int
11439 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11440 {
11441 struct breakpoint *bpt = (struct breakpoint *) data;
11442
11443 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11444 return 0;
11445 }
11446
11447 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
11448 callbacks. */
11449
11450 static void
11451 say_where (struct breakpoint *b)
11452 {
11453 struct value_print_options opts;
11454
11455 get_user_print_options (&opts);
11456
11457 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11458 single string. */
11459 if (b->loc == NULL)
11460 {
11461 /* For pending locations, the output differs slightly based
11462 on b->extra_string. If this is non-NULL, it contains either
11463 a condition or dprintf arguments. */
11464 if (b->extra_string == NULL)
11465 {
11466 gdb_printf (_(" (%s) pending."),
11467 event_location_to_string (b->location.get ()));
11468 }
11469 else if (b->type == bp_dprintf)
11470 {
11471 gdb_printf (_(" (%s,%s) pending."),
11472 event_location_to_string (b->location.get ()),
11473 b->extra_string.get ());
11474 }
11475 else
11476 {
11477 gdb_printf (_(" (%s %s) pending."),
11478 event_location_to_string (b->location.get ()),
11479 b->extra_string.get ());
11480 }
11481 }
11482 else
11483 {
11484 if (opts.addressprint || b->loc->symtab == NULL)
11485 gdb_printf (" at %ps",
11486 styled_string (address_style.style (),
11487 paddress (b->loc->gdbarch,
11488 b->loc->address)));
11489 if (b->loc->symtab != NULL)
11490 {
11491 /* If there is a single location, we can print the location
11492 more nicely. */
11493 if (b->loc->next == NULL)
11494 {
11495 const char *filename
11496 = symtab_to_filename_for_display (b->loc->symtab);
11497 gdb_printf (": file %ps, line %d.",
11498 styled_string (file_name_style.style (),
11499 filename),
11500 b->loc->line_number);
11501 }
11502 else
11503 /* This is not ideal, but each location may have a
11504 different file name, and this at least reflects the
11505 real situation somewhat. */
11506 gdb_printf (": %s.",
11507 event_location_to_string (b->location.get ()));
11508 }
11509
11510 if (b->loc->next)
11511 {
11512 struct bp_location *loc = b->loc;
11513 int n = 0;
11514 for (; loc; loc = loc->next)
11515 ++n;
11516 gdb_printf (" (%d locations)", n);
11517 }
11518 }
11519 }
11520
11521 /* See breakpoint.h. */
11522
11523 bp_location_range breakpoint::locations ()
11524 {
11525 return bp_location_range (this->loc);
11526 }
11527
11528 static struct bp_location *
11529 base_breakpoint_allocate_location (struct breakpoint *self)
11530 {
11531 return new bp_location (self);
11532 }
11533
11534 struct bp_location *
11535 breakpoint::allocate_location ()
11536 {
11537 return new bp_location (this);
11538 }
11539
11540 static void
11541 base_breakpoint_re_set (struct breakpoint *b)
11542 {
11543 /* Nothing to re-set. */
11544 }
11545
11546 #define internal_error_pure_virtual_called() \
11547 gdb_assert_not_reached ("pure virtual function called")
11548
11549 static int
11550 base_breakpoint_insert_location (struct bp_location *bl)
11551 {
11552 internal_error_pure_virtual_called ();
11553 }
11554
11555 int
11556 breakpoint::insert_location (struct bp_location *bl)
11557 {
11558 internal_error_pure_virtual_called ();
11559 }
11560
11561 static int
11562 base_breakpoint_remove_location (struct bp_location *bl,
11563 enum remove_bp_reason reason)
11564 {
11565 internal_error_pure_virtual_called ();
11566 }
11567
11568 int
11569 breakpoint::remove_location (struct bp_location *bl,
11570 enum remove_bp_reason reason)
11571 {
11572 internal_error_pure_virtual_called ();
11573 }
11574
11575 static int
11576 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
11577 const address_space *aspace,
11578 CORE_ADDR bp_addr,
11579 const target_waitstatus &ws)
11580 {
11581 internal_error_pure_virtual_called ();
11582 }
11583
11584 int
11585 breakpoint::breakpoint_hit (const struct bp_location *bl,
11586 const address_space *aspace,
11587 CORE_ADDR bp_addr,
11588 const target_waitstatus &ws)
11589 {
11590 internal_error_pure_virtual_called ();
11591 }
11592
11593 static void
11594 base_breakpoint_check_status (bpstat *bs)
11595 {
11596 /* Always stop. */
11597 }
11598
11599 /* A "works_in_software_mode" breakpoint_ops method that just internal
11600 errors. */
11601
11602 static int
11603 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
11604 {
11605 internal_error_pure_virtual_called ();
11606 }
11607
11608 int
11609 breakpoint::works_in_software_mode () const
11610 {
11611 internal_error_pure_virtual_called ();
11612 }
11613
11614 /* A "resources_needed" breakpoint_ops method that just internal
11615 errors. */
11616
11617 static int
11618 base_breakpoint_resources_needed (const struct bp_location *bl)
11619 {
11620 internal_error_pure_virtual_called ();
11621 }
11622
11623 int
11624 breakpoint::resources_needed (const struct bp_location *bl)
11625 {
11626 internal_error_pure_virtual_called ();
11627 }
11628
11629 static enum print_stop_action
11630 base_breakpoint_print_it (bpstat *bs)
11631 {
11632 internal_error_pure_virtual_called ();
11633 }
11634
11635 enum print_stop_action
11636 breakpoint::print_it (bpstat *bs)
11637 {
11638 internal_error_pure_virtual_called ();
11639 }
11640
11641 static bool
11642 base_breakpoint_print_one (struct breakpoint *, struct bp_location **)
11643 {
11644 return false;
11645 }
11646
11647 static void
11648 base_breakpoint_print_one_detail (const struct breakpoint *self,
11649 struct ui_out *uiout)
11650 {
11651 /* nothing */
11652 }
11653
11654 static void
11655 base_breakpoint_print_mention (struct breakpoint *b)
11656 {
11657 internal_error_pure_virtual_called ();
11658 }
11659
11660 void
11661 breakpoint::print_mention ()
11662 {
11663 internal_error_pure_virtual_called ();
11664 }
11665
11666 static void
11667 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
11668 {
11669 internal_error_pure_virtual_called ();
11670 }
11671
11672 void
11673 breakpoint::print_recreate (struct ui_file *fp)
11674 {
11675 internal_error_pure_virtual_called ();
11676 }
11677
11678 static void
11679 base_breakpoint_create_sals_from_location
11680 (struct event_location *location,
11681 struct linespec_result *canonical,
11682 enum bptype type_wanted)
11683 {
11684 internal_error_pure_virtual_called ();
11685 }
11686
11687 static void
11688 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
11689 struct linespec_result *c,
11690 gdb::unique_xmalloc_ptr<char> cond_string,
11691 gdb::unique_xmalloc_ptr<char> extra_string,
11692 enum bptype type_wanted,
11693 enum bpdisp disposition,
11694 int thread,
11695 int task, int ignore_count,
11696 const struct breakpoint_ops *o,
11697 int from_tty, int enabled,
11698 int internal, unsigned flags)
11699 {
11700 internal_error_pure_virtual_called ();
11701 }
11702
11703 static std::vector<symtab_and_line>
11704 base_breakpoint_decode_location (struct breakpoint *b,
11705 struct event_location *location,
11706 struct program_space *search_pspace)
11707 {
11708 internal_error_pure_virtual_called ();
11709 }
11710
11711 std::vector<symtab_and_line>
11712 breakpoint::decode_location (struct event_location *location,
11713 struct program_space *search_pspace)
11714 {
11715 internal_error_pure_virtual_called ();
11716 }
11717
11718 /* The default 'explains_signal' method. */
11719
11720 static int
11721 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
11722 {
11723 return 1;
11724 }
11725
11726 /* The default "after_condition_true" method. */
11727
11728 static void
11729 base_breakpoint_after_condition_true (struct bpstat *bs)
11730 {
11731 /* Nothing to do. */
11732 }
11733
11734 struct breakpoint_ops base_breakpoint_ops =
11735 {
11736 base_breakpoint_allocate_location,
11737 base_breakpoint_re_set,
11738 base_breakpoint_insert_location,
11739 base_breakpoint_remove_location,
11740 base_breakpoint_breakpoint_hit,
11741 base_breakpoint_check_status,
11742 base_breakpoint_resources_needed,
11743 base_breakpoint_works_in_software_mode,
11744 base_breakpoint_print_it,
11745 base_breakpoint_print_one,
11746 base_breakpoint_print_one_detail,
11747 base_breakpoint_print_mention,
11748 base_breakpoint_print_recreate,
11749 base_breakpoint_create_sals_from_location,
11750 base_breakpoint_create_breakpoints_sal,
11751 base_breakpoint_decode_location,
11752 base_breakpoint_explains_signal,
11753 base_breakpoint_after_condition_true,
11754 };
11755
11756 struct breakpoint_ops vtable_breakpoint_ops =
11757 {
11758 [] (struct breakpoint *b) { return b->allocate_location (); },
11759 [] (struct breakpoint *b) { b->re_set (); },
11760 [] (struct bp_location *l)
11761 {
11762 return l->owner->insert_location (l);
11763 },
11764 [] (struct bp_location *l, enum remove_bp_reason reason)
11765 {
11766 return l->owner->remove_location (l, reason);
11767 },
11768 [] (const struct bp_location *bl,
11769 const address_space *aspace,
11770 CORE_ADDR bp_addr,
11771 const target_waitstatus &ws)
11772 {
11773 return bl->owner->breakpoint_hit (bl, aspace, bp_addr, ws);
11774 },
11775 [] (struct bpstat *bs) { bs->breakpoint_at->check_status (bs); },
11776 [] (const struct bp_location *bl)
11777 { return bl->owner->resources_needed (bl); },
11778 [] (const struct breakpoint *b)
11779 { return b->works_in_software_mode (); },
11780 [] (struct bpstat *bs)
11781 { return bs->breakpoint_at->print_it (bs); },
11782 [] (struct breakpoint *b, struct bp_location **bl)
11783 { return b->print_one (bl); },
11784 [] (const struct breakpoint *b, struct ui_out *out)
11785 { b->print_one_detail (out); },
11786 [] (struct breakpoint *b) { b->print_mention (); },
11787 [] (struct breakpoint *b, struct ui_file *fp)
11788 { b->print_recreate (fp); },
11789 create_sals_from_location_default,
11790 create_breakpoints_sal_default,
11791 [] (struct breakpoint *b,
11792 struct event_location *location,
11793 struct program_space *search_pspace)
11794 { return b->decode_location (location, search_pspace); },
11795 [] (struct breakpoint *b, enum gdb_signal s)
11796 { return b->explains_signal (s); },
11797 [] (struct bpstat *bs)
11798 { bs->breakpoint_at->after_condition_true (bs); }
11799 };
11800
11801 /* Default breakpoint_ops methods. */
11802
11803 void
11804 base_breakpoint::re_set ()
11805 {
11806 /* FIXME: is this still reachable? */
11807 if (breakpoint_event_location_empty_p (this))
11808 {
11809 /* Anything without a location can't be re-set. */
11810 delete_breakpoint (this);
11811 return;
11812 }
11813
11814 breakpoint_re_set_default (this);
11815 }
11816
11817 int
11818 base_breakpoint::insert_location (struct bp_location *bl)
11819 {
11820 CORE_ADDR addr = bl->target_info.reqstd_address;
11821
11822 bl->target_info.kind = breakpoint_kind (bl, &addr);
11823 bl->target_info.placed_address = addr;
11824
11825 if (bl->loc_type == bp_loc_hardware_breakpoint)
11826 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
11827 else
11828 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11829 }
11830
11831 int
11832 base_breakpoint::remove_location (struct bp_location *bl,
11833 enum remove_bp_reason reason)
11834 {
11835 if (bl->loc_type == bp_loc_hardware_breakpoint)
11836 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11837 else
11838 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
11839 }
11840
11841 int
11842 base_breakpoint::breakpoint_hit (const struct bp_location *bl,
11843 const address_space *aspace,
11844 CORE_ADDR bp_addr,
11845 const target_waitstatus &ws)
11846 {
11847 if (ws.kind () != TARGET_WAITKIND_STOPPED
11848 || ws.sig () != GDB_SIGNAL_TRAP)
11849 return 0;
11850
11851 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11852 aspace, bp_addr))
11853 return 0;
11854
11855 if (overlay_debugging /* unmapped overlay section */
11856 && section_is_overlay (bl->section)
11857 && !section_is_mapped (bl->section))
11858 return 0;
11859
11860 return 1;
11861 }
11862
11863 static int
11864 dprintf_breakpoint_hit (const struct bp_location *bl,
11865 const address_space *aspace, CORE_ADDR bp_addr,
11866 const target_waitstatus &ws)
11867 {
11868 if (dprintf_style == dprintf_style_agent
11869 && target_can_run_breakpoint_commands ())
11870 {
11871 /* An agent-style dprintf never causes a stop. If we see a trap
11872 for this address it must be for a breakpoint that happens to
11873 be set at the same address. */
11874 return 0;
11875 }
11876
11877 return bl->owner->breakpoint_hit (bl, aspace, bp_addr, ws);
11878 }
11879
11880 int
11881 ordinary_breakpoint::resources_needed (const struct bp_location *bl)
11882 {
11883 gdb_assert (type == bp_hardware_breakpoint);
11884
11885 return 1;
11886 }
11887
11888 enum print_stop_action
11889 ordinary_breakpoint::print_it (bpstat *bs)
11890 {
11891 const struct bp_location *bl;
11892 int bp_temp;
11893 struct ui_out *uiout = current_uiout;
11894
11895 bl = bs->bp_location_at.get ();
11896
11897 bp_temp = disposition == disp_del;
11898 if (bl->address != bl->requested_address)
11899 breakpoint_adjustment_warning (bl->requested_address,
11900 bl->address,
11901 number, 1);
11902 annotate_breakpoint (number);
11903 maybe_print_thread_hit_breakpoint (uiout);
11904
11905 if (uiout->is_mi_like_p ())
11906 {
11907 uiout->field_string ("reason",
11908 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11909 uiout->field_string ("disp", bpdisp_text (disposition));
11910 }
11911 if (bp_temp)
11912 uiout->message ("Temporary breakpoint %pF, ",
11913 signed_field ("bkptno", number));
11914 else
11915 uiout->message ("Breakpoint %pF, ",
11916 signed_field ("bkptno", number));
11917
11918 return PRINT_SRC_AND_LOC;
11919 }
11920
11921 void
11922 ordinary_breakpoint::print_mention ()
11923 {
11924 if (current_uiout->is_mi_like_p ())
11925 return;
11926
11927 switch (type)
11928 {
11929 case bp_breakpoint:
11930 case bp_gnu_ifunc_resolver:
11931 if (disposition == disp_del)
11932 gdb_printf (_("Temporary breakpoint"));
11933 else
11934 gdb_printf (_("Breakpoint"));
11935 gdb_printf (_(" %d"), number);
11936 if (type == bp_gnu_ifunc_resolver)
11937 gdb_printf (_(" at gnu-indirect-function resolver"));
11938 break;
11939 case bp_hardware_breakpoint:
11940 gdb_printf (_("Hardware assisted breakpoint %d"), number);
11941 break;
11942 case bp_dprintf:
11943 gdb_printf (_("Dprintf %d"), number);
11944 break;
11945 }
11946
11947 say_where (this);
11948 }
11949
11950 void
11951 ordinary_breakpoint::print_recreate (struct ui_file *fp)
11952 {
11953 if (type == bp_breakpoint && disposition == disp_del)
11954 gdb_printf (fp, "tbreak");
11955 else if (type == bp_breakpoint)
11956 gdb_printf (fp, "break");
11957 else if (type == bp_hardware_breakpoint
11958 && disposition == disp_del)
11959 gdb_printf (fp, "thbreak");
11960 else if (type == bp_hardware_breakpoint)
11961 gdb_printf (fp, "hbreak");
11962 else
11963 internal_error (__FILE__, __LINE__,
11964 _("unhandled breakpoint type %d"), (int) type);
11965
11966 gdb_printf (fp, " %s", event_location_to_string (location.get ()));
11967
11968 /* Print out extra_string if this breakpoint is pending. It might
11969 contain, for example, conditions that were set by the user. */
11970 if (loc == NULL && extra_string != NULL)
11971 gdb_printf (fp, " %s", extra_string.get ());
11972
11973 print_recreate_thread (this, fp);
11974 }
11975
11976 std::vector<symtab_and_line>
11977 base_breakpoint::decode_location (struct event_location *location,
11978 struct program_space *search_pspace)
11979 {
11980 return decode_location_default (this, location, search_pspace);
11981 }
11982
11983 /* Virtual table for internal breakpoints. */
11984
11985 void
11986 internal_breakpoint::re_set ()
11987 {
11988 switch (type)
11989 {
11990 /* Delete overlay event and longjmp master breakpoints; they
11991 will be reset later by breakpoint_re_set. */
11992 case bp_overlay_event:
11993 case bp_longjmp_master:
11994 case bp_std_terminate_master:
11995 case bp_exception_master:
11996 delete_breakpoint (this);
11997 break;
11998
11999 /* This breakpoint is special, it's set up when the inferior
12000 starts and we really don't want to touch it. */
12001 case bp_shlib_event:
12002
12003 /* Like bp_shlib_event, this breakpoint type is special. Once
12004 it is set up, we do not want to touch it. */
12005 case bp_thread_event:
12006 break;
12007 }
12008 }
12009
12010 void
12011 internal_breakpoint::check_status (bpstat *bs)
12012 {
12013 if (type == bp_shlib_event)
12014 {
12015 /* If requested, stop when the dynamic linker notifies GDB of
12016 events. This allows the user to get control and place
12017 breakpoints in initializer routines for dynamically loaded
12018 objects (among other things). */
12019 bs->stop = stop_on_solib_events;
12020 bs->print = stop_on_solib_events;
12021 }
12022 else
12023 bs->stop = 0;
12024 }
12025
12026 enum print_stop_action
12027 internal_breakpoint::print_it (bpstat *bs)
12028 {
12029 switch (type)
12030 {
12031 case bp_shlib_event:
12032 /* Did we stop because the user set the stop_on_solib_events
12033 variable? (If so, we report this as a generic, "Stopped due
12034 to shlib event" message.) */
12035 print_solib_event (false);
12036 break;
12037
12038 case bp_thread_event:
12039 /* Not sure how we will get here.
12040 GDB should not stop for these breakpoints. */
12041 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
12042 break;
12043
12044 case bp_overlay_event:
12045 /* By analogy with the thread event, GDB should not stop for these. */
12046 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12047 break;
12048
12049 case bp_longjmp_master:
12050 /* These should never be enabled. */
12051 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12052 break;
12053
12054 case bp_std_terminate_master:
12055 /* These should never be enabled. */
12056 gdb_printf (_("std::terminate Master Breakpoint: "
12057 "gdb should not stop!\n"));
12058 break;
12059
12060 case bp_exception_master:
12061 /* These should never be enabled. */
12062 gdb_printf (_("Exception Master Breakpoint: "
12063 "gdb should not stop!\n"));
12064 break;
12065 }
12066
12067 return PRINT_NOTHING;
12068 }
12069
12070 void
12071 internal_breakpoint::print_mention ()
12072 {
12073 /* Nothing to mention. These breakpoints are internal. */
12074 }
12075
12076 /* Virtual table for momentary breakpoints */
12077
12078 void
12079 momentary_breakpoint::re_set ()
12080 {
12081 /* Keep temporary breakpoints, which can be encountered when we step
12082 over a dlopen call and solib_add is resetting the breakpoints.
12083 Otherwise these should have been blown away via the cleanup chain
12084 or by breakpoint_init_inferior when we rerun the executable. */
12085 }
12086
12087 void
12088 momentary_breakpoint::check_status (bpstat *bs)
12089 {
12090 /* Nothing. The point of these breakpoints is causing a stop. */
12091 }
12092
12093 enum print_stop_action
12094 momentary_breakpoint::print_it (bpstat *bs)
12095 {
12096 return PRINT_UNKNOWN;
12097 }
12098
12099 void
12100 momentary_breakpoint::print_mention ()
12101 {
12102 /* Nothing to mention. These breakpoints are internal. */
12103 }
12104
12105 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12106
12107 It gets cleared already on the removal of the first one of such placed
12108 breakpoints. This is OK as they get all removed altogether. */
12109
12110 longjmp_breakpoint::~longjmp_breakpoint ()
12111 {
12112 thread_info *tp = find_thread_global_id (this->thread);
12113
12114 if (tp != NULL)
12115 tp->initiating_frame = null_frame_id;
12116 }
12117
12118 /* Specific methods for probe breakpoints. */
12119
12120 static int
12121 bkpt_probe_insert_location (struct bp_location *bl)
12122 {
12123 int v = bl->owner->insert_location (bl);
12124
12125 if (v == 0)
12126 {
12127 /* The insertion was successful, now let's set the probe's semaphore
12128 if needed. */
12129 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12130 }
12131
12132 return v;
12133 }
12134
12135 static int
12136 bkpt_probe_remove_location (struct bp_location *bl,
12137 enum remove_bp_reason reason)
12138 {
12139 /* Let's clear the semaphore before removing the location. */
12140 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12141
12142 return bl->owner->remove_location (bl, reason);
12143 }
12144
12145 static void
12146 bkpt_probe_create_sals_from_location (struct event_location *location,
12147 struct linespec_result *canonical,
12148 enum bptype type_wanted)
12149 {
12150 struct linespec_sals lsal;
12151
12152 lsal.sals = parse_probes (location, NULL, canonical);
12153 lsal.canonical
12154 = xstrdup (event_location_to_string (canonical->location.get ()));
12155 canonical->lsals.push_back (std::move (lsal));
12156 }
12157
12158 static std::vector<symtab_and_line>
12159 bkpt_probe_decode_location (struct breakpoint *b,
12160 struct event_location *location,
12161 struct program_space *search_pspace)
12162 {
12163 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12164 if (sals.empty ())
12165 error (_("probe not found"));
12166 return sals;
12167 }
12168
12169 /* The breakpoint_ops structure to be used in tracepoints. */
12170
12171 void
12172 tracepoint::re_set ()
12173 {
12174 breakpoint_re_set_default (this);
12175 }
12176
12177 int
12178 tracepoint::breakpoint_hit (const struct bp_location *bl,
12179 const address_space *aspace, CORE_ADDR bp_addr,
12180 const target_waitstatus &ws)
12181 {
12182 /* By definition, the inferior does not report stops at
12183 tracepoints. */
12184 return 0;
12185 }
12186
12187 void
12188 tracepoint::print_one_detail (struct ui_out *uiout) const
12189 {
12190 if (!static_trace_marker_id.empty ())
12191 {
12192 gdb_assert (type == bp_static_tracepoint);
12193
12194 uiout->message ("\tmarker id is %pF\n",
12195 string_field ("static-tracepoint-marker-string-id",
12196 static_trace_marker_id.c_str ()));
12197 }
12198 }
12199
12200 void
12201 tracepoint::print_mention ()
12202 {
12203 if (current_uiout->is_mi_like_p ())
12204 return;
12205
12206 switch (type)
12207 {
12208 case bp_tracepoint:
12209 gdb_printf (_("Tracepoint"));
12210 gdb_printf (_(" %d"), number);
12211 break;
12212 case bp_fast_tracepoint:
12213 gdb_printf (_("Fast tracepoint"));
12214 gdb_printf (_(" %d"), number);
12215 break;
12216 case bp_static_tracepoint:
12217 gdb_printf (_("Static tracepoint"));
12218 gdb_printf (_(" %d"), number);
12219 break;
12220 default:
12221 internal_error (__FILE__, __LINE__,
12222 _("unhandled tracepoint type %d"), (int) type);
12223 }
12224
12225 say_where (this);
12226 }
12227
12228 void
12229 tracepoint::print_recreate (struct ui_file *fp)
12230 {
12231 if (type == bp_fast_tracepoint)
12232 gdb_printf (fp, "ftrace");
12233 else if (type == bp_static_tracepoint)
12234 gdb_printf (fp, "strace");
12235 else if (type == bp_tracepoint)
12236 gdb_printf (fp, "trace");
12237 else
12238 internal_error (__FILE__, __LINE__,
12239 _("unhandled tracepoint type %d"), (int) type);
12240
12241 gdb_printf (fp, " %s", event_location_to_string (location.get ()));
12242 print_recreate_thread (this, fp);
12243
12244 if (pass_count)
12245 gdb_printf (fp, " passcount %d\n", pass_count);
12246 }
12247
12248 std::vector<symtab_and_line>
12249 tracepoint::decode_location (struct event_location *location,
12250 struct program_space *search_pspace)
12251 {
12252 return decode_location_default (this, location, search_pspace);
12253 }
12254
12255 /* Virtual table for tracepoints on static probes. */
12256
12257 static void
12258 tracepoint_probe_create_sals_from_location
12259 (struct event_location *location,
12260 struct linespec_result *canonical,
12261 enum bptype type_wanted)
12262 {
12263 /* We use the same method for breakpoint on probes. */
12264 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12265 }
12266
12267 static std::vector<symtab_and_line>
12268 tracepoint_probe_decode_location (struct breakpoint *b,
12269 struct event_location *location,
12270 struct program_space *search_pspace)
12271 {
12272 /* We use the same method for breakpoint on probes. */
12273 return bkpt_probe_decode_location (b, location, search_pspace);
12274 }
12275
12276 /* Dprintf breakpoint_ops methods. */
12277
12278 static void
12279 dprintf_re_set (struct breakpoint *b)
12280 {
12281 breakpoint_re_set_default (b);
12282
12283 /* extra_string should never be non-NULL for dprintf. */
12284 gdb_assert (b->extra_string != NULL);
12285
12286 /* 1 - connect to target 1, that can run breakpoint commands.
12287 2 - create a dprintf, which resolves fine.
12288 3 - disconnect from target 1
12289 4 - connect to target 2, that can NOT run breakpoint commands.
12290
12291 After steps #3/#4, you'll want the dprintf command list to
12292 be updated, because target 1 and 2 may well return different
12293 answers for target_can_run_breakpoint_commands().
12294 Given absence of finer grained resetting, we get to do
12295 it all the time. */
12296 if (b->extra_string != NULL)
12297 update_dprintf_command_list (b);
12298 }
12299
12300 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12301
12302 static void
12303 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12304 {
12305 gdb_printf (fp, "dprintf %s,%s",
12306 event_location_to_string (tp->location.get ()),
12307 tp->extra_string.get ());
12308 print_recreate_thread (tp, fp);
12309 }
12310
12311 /* Implement the "after_condition_true" breakpoint_ops method for
12312 dprintf.
12313
12314 dprintf's are implemented with regular commands in their command
12315 list, but we run the commands here instead of before presenting the
12316 stop to the user, as dprintf's don't actually cause a stop. This
12317 also makes it so that the commands of multiple dprintfs at the same
12318 address are all handled. */
12319
12320 static void
12321 dprintf_after_condition_true (struct bpstat *bs)
12322 {
12323 /* dprintf's never cause a stop. This wasn't set in the
12324 check_status hook instead because that would make the dprintf's
12325 condition not be evaluated. */
12326 bs->stop = 0;
12327
12328 /* Run the command list here. Take ownership of it instead of
12329 copying. We never want these commands to run later in
12330 bpstat_do_actions, if a breakpoint that causes a stop happens to
12331 be set at same address as this dprintf, or even if running the
12332 commands here throws. */
12333 counted_command_line cmds = std::move (bs->commands);
12334 gdb_assert (cmds != nullptr);
12335 execute_control_commands (cmds.get (), 0);
12336 }
12337
12338 /* The breakpoint_ops structure to be used on static tracepoints with
12339 markers (`-m'). */
12340
12341 static void
12342 strace_marker_create_sals_from_location (struct event_location *location,
12343 struct linespec_result *canonical,
12344 enum bptype type_wanted)
12345 {
12346 struct linespec_sals lsal;
12347 const char *arg_start, *arg;
12348
12349 arg = arg_start = get_linespec_location (location)->spec_string;
12350 lsal.sals = decode_static_tracepoint_spec (&arg);
12351
12352 std::string str (arg_start, arg - arg_start);
12353 const char *ptr = str.c_str ();
12354 canonical->location
12355 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
12356
12357 lsal.canonical
12358 = xstrdup (event_location_to_string (canonical->location.get ()));
12359 canonical->lsals.push_back (std::move (lsal));
12360 }
12361
12362 static void
12363 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12364 struct linespec_result *canonical,
12365 gdb::unique_xmalloc_ptr<char> cond_string,
12366 gdb::unique_xmalloc_ptr<char> extra_string,
12367 enum bptype type_wanted,
12368 enum bpdisp disposition,
12369 int thread,
12370 int task, int ignore_count,
12371 const struct breakpoint_ops *ops,
12372 int from_tty, int enabled,
12373 int internal, unsigned flags)
12374 {
12375 const linespec_sals &lsal = canonical->lsals[0];
12376
12377 /* If the user is creating a static tracepoint by marker id
12378 (strace -m MARKER_ID), then store the sals index, so that
12379 breakpoint_re_set can try to match up which of the newly
12380 found markers corresponds to this one, and, don't try to
12381 expand multiple locations for each sal, given than SALS
12382 already should contain all sals for MARKER_ID. */
12383
12384 for (size_t i = 0; i < lsal.sals.size (); i++)
12385 {
12386 event_location_up location
12387 = copy_event_location (canonical->location.get ());
12388
12389 std::unique_ptr<tracepoint> tp (new tracepoint ());
12390 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
12391 std::move (location), NULL,
12392 std::move (cond_string),
12393 std::move (extra_string),
12394 type_wanted, disposition,
12395 thread, task, ignore_count, ops,
12396 from_tty, enabled, internal, flags,
12397 canonical->special_display);
12398 /* Given that its possible to have multiple markers with
12399 the same string id, if the user is creating a static
12400 tracepoint by marker id ("strace -m MARKER_ID"), then
12401 store the sals index, so that breakpoint_re_set can
12402 try to match up which of the newly found markers
12403 corresponds to this one */
12404 tp->static_trace_marker_id_idx = i;
12405
12406 install_breakpoint (internal, std::move (tp), 0);
12407 }
12408 }
12409
12410 static std::vector<symtab_and_line>
12411 strace_marker_decode_location (struct breakpoint *b,
12412 struct event_location *location,
12413 struct program_space *search_pspace)
12414 {
12415 struct tracepoint *tp = (struct tracepoint *) b;
12416 const char *s = get_linespec_location (location)->spec_string;
12417
12418 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12419 if (sals.size () > tp->static_trace_marker_id_idx)
12420 {
12421 sals[0] = sals[tp->static_trace_marker_id_idx];
12422 sals.resize (1);
12423 return sals;
12424 }
12425 else
12426 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
12427 }
12428
12429 static struct breakpoint_ops strace_marker_breakpoint_ops;
12430
12431 static int
12432 strace_marker_p (struct breakpoint *b)
12433 {
12434 return b->ops == &strace_marker_breakpoint_ops;
12435 }
12436
12437 /* Delete a breakpoint and clean up all traces of it in the data
12438 structures. */
12439
12440 void
12441 delete_breakpoint (struct breakpoint *bpt)
12442 {
12443 gdb_assert (bpt != NULL);
12444
12445 /* Has this bp already been deleted? This can happen because
12446 multiple lists can hold pointers to bp's. bpstat lists are
12447 especial culprits.
12448
12449 One example of this happening is a watchpoint's scope bp. When
12450 the scope bp triggers, we notice that the watchpoint is out of
12451 scope, and delete it. We also delete its scope bp. But the
12452 scope bp is marked "auto-deleting", and is already on a bpstat.
12453 That bpstat is then checked for auto-deleting bp's, which are
12454 deleted.
12455
12456 A real solution to this problem might involve reference counts in
12457 bp's, and/or giving them pointers back to their referencing
12458 bpstat's, and teaching delete_breakpoint to only free a bp's
12459 storage when no more references were extent. A cheaper bandaid
12460 was chosen. */
12461 if (bpt->type == bp_none)
12462 return;
12463
12464 /* At least avoid this stale reference until the reference counting
12465 of breakpoints gets resolved. */
12466 if (bpt->related_breakpoint != bpt)
12467 {
12468 struct breakpoint *related;
12469 struct watchpoint *w;
12470
12471 if (bpt->type == bp_watchpoint_scope)
12472 w = (struct watchpoint *) bpt->related_breakpoint;
12473 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12474 w = (struct watchpoint *) bpt;
12475 else
12476 w = NULL;
12477 if (w != NULL)
12478 watchpoint_del_at_next_stop (w);
12479
12480 /* Unlink bpt from the bpt->related_breakpoint ring. */
12481 for (related = bpt; related->related_breakpoint != bpt;
12482 related = related->related_breakpoint);
12483 related->related_breakpoint = bpt->related_breakpoint;
12484 bpt->related_breakpoint = bpt;
12485 }
12486
12487 /* watch_command_1 creates a watchpoint but only sets its number if
12488 update_watchpoint succeeds in creating its bp_locations. If there's
12489 a problem in that process, we'll be asked to delete the half-created
12490 watchpoint. In that case, don't announce the deletion. */
12491 if (bpt->number)
12492 gdb::observers::breakpoint_deleted.notify (bpt);
12493
12494 if (breakpoint_chain == bpt)
12495 breakpoint_chain = bpt->next;
12496
12497 for (breakpoint *b : all_breakpoints ())
12498 if (b->next == bpt)
12499 {
12500 b->next = bpt->next;
12501 break;
12502 }
12503
12504 /* Be sure no bpstat's are pointing at the breakpoint after it's
12505 been freed. */
12506 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12507 in all threads for now. Note that we cannot just remove bpstats
12508 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12509 commands are associated with the bpstat; if we remove it here,
12510 then the later call to bpstat_do_actions (&stop_bpstat); in
12511 event-top.c won't do anything, and temporary breakpoints with
12512 commands won't work. */
12513
12514 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12515
12516 /* Now that breakpoint is removed from breakpoint list, update the
12517 global location list. This will remove locations that used to
12518 belong to this breakpoint. Do this before freeing the breakpoint
12519 itself, since remove_breakpoint looks at location's owner. It
12520 might be better design to have location completely
12521 self-contained, but it's not the case now. */
12522 update_global_location_list (UGLL_DONT_INSERT);
12523
12524 /* On the chance that someone will soon try again to delete this
12525 same bp, we mark it as deleted before freeing its storage. */
12526 bpt->type = bp_none;
12527 delete bpt;
12528 }
12529
12530 /* Iterator function to call a user-provided callback function once
12531 for each of B and its related breakpoints. */
12532
12533 static void
12534 iterate_over_related_breakpoints (struct breakpoint *b,
12535 gdb::function_view<void (breakpoint *)> function)
12536 {
12537 struct breakpoint *related;
12538
12539 related = b;
12540 do
12541 {
12542 struct breakpoint *next;
12543
12544 /* FUNCTION may delete RELATED. */
12545 next = related->related_breakpoint;
12546
12547 if (next == related)
12548 {
12549 /* RELATED is the last ring entry. */
12550 function (related);
12551
12552 /* FUNCTION may have deleted it, so we'd never reach back to
12553 B. There's nothing left to do anyway, so just break
12554 out. */
12555 break;
12556 }
12557 else
12558 function (related);
12559
12560 related = next;
12561 }
12562 while (related != b);
12563 }
12564
12565 static void
12566 delete_command (const char *arg, int from_tty)
12567 {
12568 dont_repeat ();
12569
12570 if (arg == 0)
12571 {
12572 int breaks_to_delete = 0;
12573
12574 /* Delete all breakpoints if no argument. Do not delete
12575 internal breakpoints, these have to be deleted with an
12576 explicit breakpoint number argument. */
12577 for (breakpoint *b : all_breakpoints ())
12578 if (user_breakpoint_p (b))
12579 {
12580 breaks_to_delete = 1;
12581 break;
12582 }
12583
12584 /* Ask user only if there are some breakpoints to delete. */
12585 if (!from_tty
12586 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
12587 for (breakpoint *b : all_breakpoints_safe ())
12588 if (user_breakpoint_p (b))
12589 delete_breakpoint (b);
12590 }
12591 else
12592 map_breakpoint_numbers
12593 (arg, [&] (breakpoint *br)
12594 {
12595 iterate_over_related_breakpoints (br, delete_breakpoint);
12596 });
12597 }
12598
12599 /* Return true if all locations of B bound to PSPACE are pending. If
12600 PSPACE is NULL, all locations of all program spaces are
12601 considered. */
12602
12603 static int
12604 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
12605 {
12606 for (bp_location *loc : b->locations ())
12607 if ((pspace == NULL
12608 || loc->pspace == pspace)
12609 && !loc->shlib_disabled
12610 && !loc->pspace->executing_startup)
12611 return 0;
12612 return 1;
12613 }
12614
12615 /* Subroutine of update_breakpoint_locations to simplify it.
12616 Return non-zero if multiple fns in list LOC have the same name.
12617 Null names are ignored. */
12618
12619 static int
12620 ambiguous_names_p (struct bp_location *loc)
12621 {
12622 struct bp_location *l;
12623 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
12624 xcalloc, xfree));
12625
12626 for (l = loc; l != NULL; l = l->next)
12627 {
12628 const char **slot;
12629 const char *name = l->function_name.get ();
12630
12631 /* Allow for some names to be NULL, ignore them. */
12632 if (name == NULL)
12633 continue;
12634
12635 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
12636 INSERT);
12637 /* NOTE: We can assume slot != NULL here because xcalloc never
12638 returns NULL. */
12639 if (*slot != NULL)
12640 return 1;
12641 *slot = name;
12642 }
12643
12644 return 0;
12645 }
12646
12647 /* When symbols change, it probably means the sources changed as well,
12648 and it might mean the static tracepoint markers are no longer at
12649 the same address or line numbers they used to be at last we
12650 checked. Losing your static tracepoints whenever you rebuild is
12651 undesirable. This function tries to resync/rematch gdb static
12652 tracepoints with the markers on the target, for static tracepoints
12653 that have not been set by marker id. Static tracepoint that have
12654 been set by marker id are reset by marker id in breakpoint_re_set.
12655 The heuristic is:
12656
12657 1) For a tracepoint set at a specific address, look for a marker at
12658 the old PC. If one is found there, assume to be the same marker.
12659 If the name / string id of the marker found is different from the
12660 previous known name, assume that means the user renamed the marker
12661 in the sources, and output a warning.
12662
12663 2) For a tracepoint set at a given line number, look for a marker
12664 at the new address of the old line number. If one is found there,
12665 assume to be the same marker. If the name / string id of the
12666 marker found is different from the previous known name, assume that
12667 means the user renamed the marker in the sources, and output a
12668 warning.
12669
12670 3) If a marker is no longer found at the same address or line, it
12671 may mean the marker no longer exists. But it may also just mean
12672 the code changed a bit. Maybe the user added a few lines of code
12673 that made the marker move up or down (in line number terms). Ask
12674 the target for info about the marker with the string id as we knew
12675 it. If found, update line number and address in the matching
12676 static tracepoint. This will get confused if there's more than one
12677 marker with the same ID (possible in UST, although unadvised
12678 precisely because it confuses tools). */
12679
12680 static struct symtab_and_line
12681 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12682 {
12683 struct tracepoint *tp = (struct tracepoint *) b;
12684 struct static_tracepoint_marker marker;
12685 CORE_ADDR pc;
12686
12687 pc = sal.pc;
12688 if (sal.line)
12689 find_line_pc (sal.symtab, sal.line, &pc);
12690
12691 if (target_static_tracepoint_marker_at (pc, &marker))
12692 {
12693 if (tp->static_trace_marker_id != marker.str_id)
12694 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12695 b->number, tp->static_trace_marker_id.c_str (),
12696 marker.str_id.c_str ());
12697
12698 tp->static_trace_marker_id = std::move (marker.str_id);
12699
12700 return sal;
12701 }
12702
12703 /* Old marker wasn't found on target at lineno. Try looking it up
12704 by string ID. */
12705 if (!sal.explicit_pc
12706 && sal.line != 0
12707 && sal.symtab != NULL
12708 && !tp->static_trace_marker_id.empty ())
12709 {
12710 std::vector<static_tracepoint_marker> markers
12711 = target_static_tracepoint_markers_by_strid
12712 (tp->static_trace_marker_id.c_str ());
12713
12714 if (!markers.empty ())
12715 {
12716 struct symbol *sym;
12717 struct static_tracepoint_marker *tpmarker;
12718 struct ui_out *uiout = current_uiout;
12719 struct explicit_location explicit_loc;
12720
12721 tpmarker = &markers[0];
12722
12723 tp->static_trace_marker_id = std::move (tpmarker->str_id);
12724
12725 warning (_("marker for static tracepoint %d (%s) not "
12726 "found at previous line number"),
12727 b->number, tp->static_trace_marker_id.c_str ());
12728
12729 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
12730 sym = find_pc_sect_function (tpmarker->address, NULL);
12731 uiout->text ("Now in ");
12732 if (sym)
12733 {
12734 uiout->field_string ("func", sym->print_name (),
12735 function_name_style.style ());
12736 uiout->text (" at ");
12737 }
12738 uiout->field_string ("file",
12739 symtab_to_filename_for_display (sal2.symtab),
12740 file_name_style.style ());
12741 uiout->text (":");
12742
12743 if (uiout->is_mi_like_p ())
12744 {
12745 const char *fullname = symtab_to_fullname (sal2.symtab);
12746
12747 uiout->field_string ("fullname", fullname);
12748 }
12749
12750 uiout->field_signed ("line", sal2.line);
12751 uiout->text ("\n");
12752
12753 b->loc->line_number = sal2.line;
12754 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
12755
12756 b->location.reset (NULL);
12757 initialize_explicit_location (&explicit_loc);
12758 explicit_loc.source_filename
12759 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
12760 explicit_loc.line_offset.offset = b->loc->line_number;
12761 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
12762 b->location = new_explicit_location (&explicit_loc);
12763
12764 /* Might be nice to check if function changed, and warn if
12765 so. */
12766 }
12767 }
12768 return sal;
12769 }
12770
12771 /* Returns 1 iff locations A and B are sufficiently same that
12772 we don't need to report breakpoint as changed. */
12773
12774 static int
12775 locations_are_equal (struct bp_location *a, struct bp_location *b)
12776 {
12777 while (a && b)
12778 {
12779 if (a->address != b->address)
12780 return 0;
12781
12782 if (a->shlib_disabled != b->shlib_disabled)
12783 return 0;
12784
12785 if (a->enabled != b->enabled)
12786 return 0;
12787
12788 if (a->disabled_by_cond != b->disabled_by_cond)
12789 return 0;
12790
12791 a = a->next;
12792 b = b->next;
12793 }
12794
12795 if ((a == NULL) != (b == NULL))
12796 return 0;
12797
12798 return 1;
12799 }
12800
12801 /* Split all locations of B that are bound to PSPACE out of B's
12802 location list to a separate list and return that list's head. If
12803 PSPACE is NULL, hoist out all locations of B. */
12804
12805 static struct bp_location *
12806 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12807 {
12808 struct bp_location head;
12809 struct bp_location *i = b->loc;
12810 struct bp_location **i_link = &b->loc;
12811 struct bp_location *hoisted = &head;
12812
12813 if (pspace == NULL)
12814 {
12815 i = b->loc;
12816 b->loc = NULL;
12817 return i;
12818 }
12819
12820 head.next = NULL;
12821
12822 while (i != NULL)
12823 {
12824 if (i->pspace == pspace)
12825 {
12826 *i_link = i->next;
12827 i->next = NULL;
12828 hoisted->next = i;
12829 hoisted = i;
12830 }
12831 else
12832 i_link = &i->next;
12833 i = *i_link;
12834 }
12835
12836 return head.next;
12837 }
12838
12839 /* Create new breakpoint locations for B (a hardware or software
12840 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12841 zero, then B is a ranged breakpoint. Only recreates locations for
12842 FILTER_PSPACE. Locations of other program spaces are left
12843 untouched. */
12844
12845 void
12846 update_breakpoint_locations (struct breakpoint *b,
12847 struct program_space *filter_pspace,
12848 gdb::array_view<const symtab_and_line> sals,
12849 gdb::array_view<const symtab_and_line> sals_end)
12850 {
12851 struct bp_location *existing_locations;
12852
12853 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
12854 {
12855 /* Ranged breakpoints have only one start location and one end
12856 location. */
12857 b->enable_state = bp_disabled;
12858 gdb_printf (gdb_stderr,
12859 _("Could not reset ranged breakpoint %d: "
12860 "multiple locations found\n"),
12861 b->number);
12862 return;
12863 }
12864
12865 /* If there's no new locations, and all existing locations are
12866 pending, don't do anything. This optimizes the common case where
12867 all locations are in the same shared library, that was unloaded.
12868 We'd like to retain the location, so that when the library is
12869 loaded again, we don't loose the enabled/disabled status of the
12870 individual locations. */
12871 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
12872 return;
12873
12874 existing_locations = hoist_existing_locations (b, filter_pspace);
12875
12876 for (const auto &sal : sals)
12877 {
12878 struct bp_location *new_loc;
12879
12880 switch_to_program_space_and_thread (sal.pspace);
12881
12882 new_loc = add_location_to_breakpoint (b, &sal);
12883
12884 /* Reparse conditions, they might contain references to the
12885 old symtab. */
12886 if (b->cond_string != NULL)
12887 {
12888 const char *s;
12889
12890 s = b->cond_string.get ();
12891 try
12892 {
12893 new_loc->cond = parse_exp_1 (&s, sal.pc,
12894 block_for_pc (sal.pc),
12895 0);
12896 }
12897 catch (const gdb_exception_error &e)
12898 {
12899 new_loc->disabled_by_cond = true;
12900 }
12901 }
12902
12903 if (!sals_end.empty ())
12904 {
12905 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
12906
12907 new_loc->length = end - sals[0].pc + 1;
12908 }
12909 }
12910
12911 /* If possible, carry over 'disable' status from existing
12912 breakpoints. */
12913 {
12914 struct bp_location *e = existing_locations;
12915 /* If there are multiple breakpoints with the same function name,
12916 e.g. for inline functions, comparing function names won't work.
12917 Instead compare pc addresses; this is just a heuristic as things
12918 may have moved, but in practice it gives the correct answer
12919 often enough until a better solution is found. */
12920 int have_ambiguous_names = ambiguous_names_p (b->loc);
12921
12922 for (; e; e = e->next)
12923 {
12924 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
12925 {
12926 if (have_ambiguous_names)
12927 {
12928 for (bp_location *l : b->locations ())
12929 {
12930 /* Ignore software vs hardware location type at
12931 this point, because with "set breakpoint
12932 auto-hw", after a re-set, locations that were
12933 hardware can end up as software, or vice versa.
12934 As mentioned above, this is an heuristic and in
12935 practice should give the correct answer often
12936 enough. */
12937 if (breakpoint_locations_match (e, l, true))
12938 {
12939 l->enabled = e->enabled;
12940 l->disabled_by_cond = e->disabled_by_cond;
12941 break;
12942 }
12943 }
12944 }
12945 else
12946 {
12947 for (bp_location *l : b->locations ())
12948 if (l->function_name
12949 && strcmp (e->function_name.get (),
12950 l->function_name.get ()) == 0)
12951 {
12952 l->enabled = e->enabled;
12953 l->disabled_by_cond = e->disabled_by_cond;
12954 break;
12955 }
12956 }
12957 }
12958 }
12959 }
12960
12961 if (!locations_are_equal (existing_locations, b->loc))
12962 gdb::observers::breakpoint_modified.notify (b);
12963 }
12964
12965 /* Find the SaL locations corresponding to the given LOCATION.
12966 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12967
12968 static std::vector<symtab_and_line>
12969 location_to_sals (struct breakpoint *b, struct event_location *location,
12970 struct program_space *search_pspace, int *found)
12971 {
12972 struct gdb_exception exception;
12973
12974 gdb_assert (b->ops != NULL);
12975
12976 std::vector<symtab_and_line> sals;
12977
12978 try
12979 {
12980 sals = b->ops->decode_location (b, location, search_pspace);
12981 }
12982 catch (gdb_exception_error &e)
12983 {
12984 int not_found_and_ok = 0;
12985
12986 /* For pending breakpoints, it's expected that parsing will
12987 fail until the right shared library is loaded. User has
12988 already told to create pending breakpoints and don't need
12989 extra messages. If breakpoint is in bp_shlib_disabled
12990 state, then user already saw the message about that
12991 breakpoint being disabled, and don't want to see more
12992 errors. */
12993 if (e.error == NOT_FOUND_ERROR
12994 && (b->condition_not_parsed
12995 || (b->loc != NULL
12996 && search_pspace != NULL
12997 && b->loc->pspace != search_pspace)
12998 || (b->loc && b->loc->shlib_disabled)
12999 || (b->loc && b->loc->pspace->executing_startup)
13000 || b->enable_state == bp_disabled))
13001 not_found_and_ok = 1;
13002
13003 if (!not_found_and_ok)
13004 {
13005 /* We surely don't want to warn about the same breakpoint
13006 10 times. One solution, implemented here, is disable
13007 the breakpoint on error. Another solution would be to
13008 have separate 'warning emitted' flag. Since this
13009 happens only when a binary has changed, I don't know
13010 which approach is better. */
13011 b->enable_state = bp_disabled;
13012 throw;
13013 }
13014
13015 exception = std::move (e);
13016 }
13017
13018 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13019 {
13020 for (auto &sal : sals)
13021 resolve_sal_pc (&sal);
13022 if (b->condition_not_parsed && b->extra_string != NULL)
13023 {
13024 gdb::unique_xmalloc_ptr<char> cond_string, extra_string;
13025 int thread, task;
13026
13027 find_condition_and_thread_for_sals (sals, b->extra_string.get (),
13028 &cond_string, &thread,
13029 &task, &extra_string);
13030 gdb_assert (b->cond_string == NULL);
13031 if (cond_string)
13032 b->cond_string = std::move (cond_string);
13033 b->thread = thread;
13034 b->task = task;
13035 if (extra_string)
13036 b->extra_string = std::move (extra_string);
13037 b->condition_not_parsed = 0;
13038 }
13039
13040 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13041 sals[0] = update_static_tracepoint (b, sals[0]);
13042
13043 *found = 1;
13044 }
13045 else
13046 *found = 0;
13047
13048 return sals;
13049 }
13050
13051 /* The default re_set method, for typical hardware or software
13052 breakpoints. Reevaluate the breakpoint and recreate its
13053 locations. */
13054
13055 static void
13056 breakpoint_re_set_default (struct breakpoint *b)
13057 {
13058 struct program_space *filter_pspace = current_program_space;
13059 std::vector<symtab_and_line> expanded, expanded_end;
13060
13061 int found;
13062 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13063 filter_pspace, &found);
13064 if (found)
13065 expanded = std::move (sals);
13066
13067 if (b->location_range_end != NULL)
13068 {
13069 std::vector<symtab_and_line> sals_end
13070 = location_to_sals (b, b->location_range_end.get (),
13071 filter_pspace, &found);
13072 if (found)
13073 expanded_end = std::move (sals_end);
13074 }
13075
13076 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13077 }
13078
13079 /* Default method for creating SALs from an address string. It basically
13080 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13081
13082 static void
13083 create_sals_from_location_default (struct event_location *location,
13084 struct linespec_result *canonical,
13085 enum bptype type_wanted)
13086 {
13087 parse_breakpoint_sals (location, canonical);
13088 }
13089
13090 /* Call create_breakpoints_sal for the given arguments. This is the default
13091 function for the `create_breakpoints_sal' method of
13092 breakpoint_ops. */
13093
13094 static void
13095 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13096 struct linespec_result *canonical,
13097 gdb::unique_xmalloc_ptr<char> cond_string,
13098 gdb::unique_xmalloc_ptr<char> extra_string,
13099 enum bptype type_wanted,
13100 enum bpdisp disposition,
13101 int thread,
13102 int task, int ignore_count,
13103 const struct breakpoint_ops *ops,
13104 int from_tty, int enabled,
13105 int internal, unsigned flags)
13106 {
13107 create_breakpoints_sal (gdbarch, canonical,
13108 std::move (cond_string),
13109 std::move (extra_string),
13110 type_wanted, disposition,
13111 thread, task, ignore_count, ops, from_tty,
13112 enabled, internal, flags);
13113 }
13114
13115 /* Decode the line represented by S by calling decode_line_full. This is the
13116 default function for the `decode_location' method of breakpoint_ops. */
13117
13118 static std::vector<symtab_and_line>
13119 decode_location_default (struct breakpoint *b,
13120 struct event_location *location,
13121 struct program_space *search_pspace)
13122 {
13123 struct linespec_result canonical;
13124
13125 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13126 NULL, 0, &canonical, multiple_symbols_all,
13127 b->filter.get ());
13128
13129 /* We should get 0 or 1 resulting SALs. */
13130 gdb_assert (canonical.lsals.size () < 2);
13131
13132 if (!canonical.lsals.empty ())
13133 {
13134 const linespec_sals &lsal = canonical.lsals[0];
13135 return std::move (lsal.sals);
13136 }
13137 return {};
13138 }
13139
13140 /* Reset a breakpoint. */
13141
13142 static void
13143 breakpoint_re_set_one (breakpoint *b)
13144 {
13145 input_radix = b->input_radix;
13146 set_language (b->language);
13147
13148 b->ops->re_set (b);
13149 }
13150
13151 /* Re-set breakpoint locations for the current program space.
13152 Locations bound to other program spaces are left untouched. */
13153
13154 void
13155 breakpoint_re_set (void)
13156 {
13157 {
13158 scoped_restore_current_language save_language;
13159 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13160 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13161
13162 /* breakpoint_re_set_one sets the current_language to the language
13163 of the breakpoint it is resetting (see prepare_re_set_context)
13164 before re-evaluating the breakpoint's location. This change can
13165 unfortunately get undone by accident if the language_mode is set
13166 to auto, and we either switch frames, or more likely in this context,
13167 we select the current frame.
13168
13169 We prevent this by temporarily turning the language_mode to
13170 language_mode_manual. We restore it once all breakpoints
13171 have been reset. */
13172 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13173 language_mode = language_mode_manual;
13174
13175 /* Note: we must not try to insert locations until after all
13176 breakpoints have been re-set. Otherwise, e.g., when re-setting
13177 breakpoint 1, we'd insert the locations of breakpoint 2, which
13178 hadn't been re-set yet, and thus may have stale locations. */
13179
13180 for (breakpoint *b : all_breakpoints_safe ())
13181 {
13182 try
13183 {
13184 breakpoint_re_set_one (b);
13185 }
13186 catch (const gdb_exception &ex)
13187 {
13188 exception_fprintf (gdb_stderr, ex,
13189 "Error in re-setting breakpoint %d: ",
13190 b->number);
13191 }
13192 }
13193
13194 jit_breakpoint_re_set ();
13195 }
13196
13197 create_overlay_event_breakpoint ();
13198 create_longjmp_master_breakpoint ();
13199 create_std_terminate_master_breakpoint ();
13200 create_exception_master_breakpoint ();
13201
13202 /* Now we can insert. */
13203 update_global_location_list (UGLL_MAY_INSERT);
13204 }
13205 \f
13206 /* Reset the thread number of this breakpoint:
13207
13208 - If the breakpoint is for all threads, leave it as-is.
13209 - Else, reset it to the current thread for inferior_ptid. */
13210 void
13211 breakpoint_re_set_thread (struct breakpoint *b)
13212 {
13213 if (b->thread != -1)
13214 {
13215 b->thread = inferior_thread ()->global_num;
13216
13217 /* We're being called after following a fork. The new fork is
13218 selected as current, and unless this was a vfork will have a
13219 different program space from the original thread. Reset that
13220 as well. */
13221 b->loc->pspace = current_program_space;
13222 }
13223 }
13224
13225 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13226 If from_tty is nonzero, it prints a message to that effect,
13227 which ends with a period (no newline). */
13228
13229 void
13230 set_ignore_count (int bptnum, int count, int from_tty)
13231 {
13232 if (count < 0)
13233 count = 0;
13234
13235 for (breakpoint *b : all_breakpoints ())
13236 if (b->number == bptnum)
13237 {
13238 if (is_tracepoint (b))
13239 {
13240 if (from_tty && count != 0)
13241 gdb_printf (_("Ignore count ignored for tracepoint %d."),
13242 bptnum);
13243 return;
13244 }
13245
13246 b->ignore_count = count;
13247 if (from_tty)
13248 {
13249 if (count == 0)
13250 gdb_printf (_("Will stop next time "
13251 "breakpoint %d is reached."),
13252 bptnum);
13253 else if (count == 1)
13254 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
13255 bptnum);
13256 else
13257 gdb_printf (_("Will ignore next %d "
13258 "crossings of breakpoint %d."),
13259 count, bptnum);
13260 }
13261 gdb::observers::breakpoint_modified.notify (b);
13262 return;
13263 }
13264
13265 error (_("No breakpoint number %d."), bptnum);
13266 }
13267
13268 /* Command to set ignore-count of breakpoint N to COUNT. */
13269
13270 static void
13271 ignore_command (const char *args, int from_tty)
13272 {
13273 const char *p = args;
13274 int num;
13275
13276 if (p == 0)
13277 error_no_arg (_("a breakpoint number"));
13278
13279 num = get_number (&p);
13280 if (num == 0)
13281 error (_("bad breakpoint number: '%s'"), args);
13282 if (*p == 0)
13283 error (_("Second argument (specified ignore-count) is missing."));
13284
13285 set_ignore_count (num,
13286 longest_to_int (value_as_long (parse_and_eval (p))),
13287 from_tty);
13288 if (from_tty)
13289 gdb_printf ("\n");
13290 }
13291 \f
13292
13293 /* Call FUNCTION on each of the breakpoints with numbers in the range
13294 defined by BP_NUM_RANGE (an inclusive range). */
13295
13296 static void
13297 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13298 gdb::function_view<void (breakpoint *)> function)
13299 {
13300 if (bp_num_range.first == 0)
13301 {
13302 warning (_("bad breakpoint number at or near '%d'"),
13303 bp_num_range.first);
13304 }
13305 else
13306 {
13307 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13308 {
13309 bool match = false;
13310
13311 for (breakpoint *b : all_breakpoints_safe ())
13312 if (b->number == i)
13313 {
13314 match = true;
13315 function (b);
13316 break;
13317 }
13318 if (!match)
13319 gdb_printf (_("No breakpoint number %d.\n"), i);
13320 }
13321 }
13322 }
13323
13324 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13325 ARGS. */
13326
13327 static void
13328 map_breakpoint_numbers (const char *args,
13329 gdb::function_view<void (breakpoint *)> function)
13330 {
13331 if (args == NULL || *args == '\0')
13332 error_no_arg (_("one or more breakpoint numbers"));
13333
13334 number_or_range_parser parser (args);
13335
13336 while (!parser.finished ())
13337 {
13338 int num = parser.get_number ();
13339 map_breakpoint_number_range (std::make_pair (num, num), function);
13340 }
13341 }
13342
13343 /* Return the breakpoint location structure corresponding to the
13344 BP_NUM and LOC_NUM values. */
13345
13346 static struct bp_location *
13347 find_location_by_number (int bp_num, int loc_num)
13348 {
13349 breakpoint *b = get_breakpoint (bp_num);
13350
13351 if (!b || b->number != bp_num)
13352 error (_("Bad breakpoint number '%d'"), bp_num);
13353
13354 if (loc_num == 0)
13355 error (_("Bad breakpoint location number '%d'"), loc_num);
13356
13357 int n = 0;
13358 for (bp_location *loc : b->locations ())
13359 if (++n == loc_num)
13360 return loc;
13361
13362 error (_("Bad breakpoint location number '%d'"), loc_num);
13363 }
13364
13365 /* Modes of operation for extract_bp_num. */
13366 enum class extract_bp_kind
13367 {
13368 /* Extracting a breakpoint number. */
13369 bp,
13370
13371 /* Extracting a location number. */
13372 loc,
13373 };
13374
13375 /* Extract a breakpoint or location number (as determined by KIND)
13376 from the string starting at START. TRAILER is a character which
13377 can be found after the number. If you don't want a trailer, use
13378 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13379 string. This always returns a positive integer. */
13380
13381 static int
13382 extract_bp_num (extract_bp_kind kind, const char *start,
13383 int trailer, const char **end_out = NULL)
13384 {
13385 const char *end = start;
13386 int num = get_number_trailer (&end, trailer);
13387 if (num < 0)
13388 error (kind == extract_bp_kind::bp
13389 ? _("Negative breakpoint number '%.*s'")
13390 : _("Negative breakpoint location number '%.*s'"),
13391 int (end - start), start);
13392 if (num == 0)
13393 error (kind == extract_bp_kind::bp
13394 ? _("Bad breakpoint number '%.*s'")
13395 : _("Bad breakpoint location number '%.*s'"),
13396 int (end - start), start);
13397
13398 if (end_out != NULL)
13399 *end_out = end;
13400 return num;
13401 }
13402
13403 /* Extract a breakpoint or location range (as determined by KIND) in
13404 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13405 representing the (inclusive) range. The returned pair's elements
13406 are always positive integers. */
13407
13408 static std::pair<int, int>
13409 extract_bp_or_bp_range (extract_bp_kind kind,
13410 const std::string &arg,
13411 std::string::size_type arg_offset)
13412 {
13413 std::pair<int, int> range;
13414 const char *bp_loc = &arg[arg_offset];
13415 std::string::size_type dash = arg.find ('-', arg_offset);
13416 if (dash != std::string::npos)
13417 {
13418 /* bp_loc is a range (x-z). */
13419 if (arg.length () == dash + 1)
13420 error (kind == extract_bp_kind::bp
13421 ? _("Bad breakpoint number at or near: '%s'")
13422 : _("Bad breakpoint location number at or near: '%s'"),
13423 bp_loc);
13424
13425 const char *end;
13426 const char *start_first = bp_loc;
13427 const char *start_second = &arg[dash + 1];
13428 range.first = extract_bp_num (kind, start_first, '-');
13429 range.second = extract_bp_num (kind, start_second, '\0', &end);
13430
13431 if (range.first > range.second)
13432 error (kind == extract_bp_kind::bp
13433 ? _("Inverted breakpoint range at '%.*s'")
13434 : _("Inverted breakpoint location range at '%.*s'"),
13435 int (end - start_first), start_first);
13436 }
13437 else
13438 {
13439 /* bp_loc is a single value. */
13440 range.first = extract_bp_num (kind, bp_loc, '\0');
13441 range.second = range.first;
13442 }
13443 return range;
13444 }
13445
13446 /* Extract the breakpoint/location range specified by ARG. Returns
13447 the breakpoint range in BP_NUM_RANGE, and the location range in
13448 BP_LOC_RANGE.
13449
13450 ARG may be in any of the following forms:
13451
13452 x where 'x' is a breakpoint number.
13453 x-y where 'x' and 'y' specify a breakpoint numbers range.
13454 x.y where 'x' is a breakpoint number and 'y' a location number.
13455 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13456 location number range.
13457 */
13458
13459 static void
13460 extract_bp_number_and_location (const std::string &arg,
13461 std::pair<int, int> &bp_num_range,
13462 std::pair<int, int> &bp_loc_range)
13463 {
13464 std::string::size_type dot = arg.find ('.');
13465
13466 if (dot != std::string::npos)
13467 {
13468 /* Handle 'x.y' and 'x.y-z' cases. */
13469
13470 if (arg.length () == dot + 1 || dot == 0)
13471 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
13472
13473 bp_num_range.first
13474 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13475 bp_num_range.second = bp_num_range.first;
13476
13477 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13478 arg, dot + 1);
13479 }
13480 else
13481 {
13482 /* Handle x and x-y cases. */
13483
13484 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
13485 bp_loc_range.first = 0;
13486 bp_loc_range.second = 0;
13487 }
13488 }
13489
13490 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13491 specifies whether to enable or disable. */
13492
13493 static void
13494 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13495 {
13496 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13497 if (loc != NULL)
13498 {
13499 if (loc->disabled_by_cond && enable)
13500 error (_("Breakpoint %d's condition is invalid at location %d, "
13501 "cannot enable."), bp_num, loc_num);
13502
13503 if (loc->enabled != enable)
13504 {
13505 loc->enabled = enable;
13506 mark_breakpoint_location_modified (loc);
13507 }
13508 if (target_supports_enable_disable_tracepoint ()
13509 && current_trace_status ()->running && loc->owner
13510 && is_tracepoint (loc->owner))
13511 target_disable_tracepoint (loc);
13512 }
13513 update_global_location_list (UGLL_DONT_INSERT);
13514
13515 gdb::observers::breakpoint_modified.notify (loc->owner);
13516 }
13517
13518 /* Enable or disable a range of breakpoint locations. BP_NUM is the
13519 number of the breakpoint, and BP_LOC_RANGE specifies the
13520 (inclusive) range of location numbers of that breakpoint to
13521 enable/disable. ENABLE specifies whether to enable or disable the
13522 location. */
13523
13524 static void
13525 enable_disable_breakpoint_location_range (int bp_num,
13526 std::pair<int, int> &bp_loc_range,
13527 bool enable)
13528 {
13529 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13530 enable_disable_bp_num_loc (bp_num, i, enable);
13531 }
13532
13533 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13534 If from_tty is nonzero, it prints a message to that effect,
13535 which ends with a period (no newline). */
13536
13537 void
13538 disable_breakpoint (struct breakpoint *bpt)
13539 {
13540 /* Never disable a watchpoint scope breakpoint; we want to
13541 hit them when we leave scope so we can delete both the
13542 watchpoint and its scope breakpoint at that time. */
13543 if (bpt->type == bp_watchpoint_scope)
13544 return;
13545
13546 bpt->enable_state = bp_disabled;
13547
13548 /* Mark breakpoint locations modified. */
13549 mark_breakpoint_modified (bpt);
13550
13551 if (target_supports_enable_disable_tracepoint ()
13552 && current_trace_status ()->running && is_tracepoint (bpt))
13553 {
13554 for (bp_location *location : bpt->locations ())
13555 target_disable_tracepoint (location);
13556 }
13557
13558 update_global_location_list (UGLL_DONT_INSERT);
13559
13560 gdb::observers::breakpoint_modified.notify (bpt);
13561 }
13562
13563 /* Enable or disable the breakpoint(s) or breakpoint location(s)
13564 specified in ARGS. ARGS may be in any of the formats handled by
13565 extract_bp_number_and_location. ENABLE specifies whether to enable
13566 or disable the breakpoints/locations. */
13567
13568 static void
13569 enable_disable_command (const char *args, int from_tty, bool enable)
13570 {
13571 if (args == 0)
13572 {
13573 for (breakpoint *bpt : all_breakpoints ())
13574 if (user_breakpoint_p (bpt))
13575 {
13576 if (enable)
13577 enable_breakpoint (bpt);
13578 else
13579 disable_breakpoint (bpt);
13580 }
13581 }
13582 else
13583 {
13584 std::string num = extract_arg (&args);
13585
13586 while (!num.empty ())
13587 {
13588 std::pair<int, int> bp_num_range, bp_loc_range;
13589
13590 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13591
13592 if (bp_loc_range.first == bp_loc_range.second
13593 && bp_loc_range.first == 0)
13594 {
13595 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
13596 map_breakpoint_number_range (bp_num_range,
13597 enable
13598 ? enable_breakpoint
13599 : disable_breakpoint);
13600 }
13601 else
13602 {
13603 /* Handle breakpoint ids with formats 'x.y' or
13604 'x.y-z'. */
13605 enable_disable_breakpoint_location_range
13606 (bp_num_range.first, bp_loc_range, enable);
13607 }
13608 num = extract_arg (&args);
13609 }
13610 }
13611 }
13612
13613 /* The disable command disables the specified breakpoints/locations
13614 (or all defined breakpoints) so they're no longer effective in
13615 stopping the inferior. ARGS may be in any of the forms defined in
13616 extract_bp_number_and_location. */
13617
13618 static void
13619 disable_command (const char *args, int from_tty)
13620 {
13621 enable_disable_command (args, from_tty, false);
13622 }
13623
13624 static void
13625 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13626 int count)
13627 {
13628 int target_resources_ok;
13629
13630 if (bpt->type == bp_hardware_breakpoint)
13631 {
13632 int i;
13633 i = hw_breakpoint_used_count ();
13634 target_resources_ok =
13635 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13636 i + 1, 0);
13637 if (target_resources_ok == 0)
13638 error (_("No hardware breakpoint support in the target."));
13639 else if (target_resources_ok < 0)
13640 error (_("Hardware breakpoints used exceeds limit."));
13641 }
13642
13643 if (is_watchpoint (bpt))
13644 {
13645 /* Initialize it just to avoid a GCC false warning. */
13646 enum enable_state orig_enable_state = bp_disabled;
13647
13648 try
13649 {
13650 struct watchpoint *w = (struct watchpoint *) bpt;
13651
13652 orig_enable_state = bpt->enable_state;
13653 bpt->enable_state = bp_enabled;
13654 update_watchpoint (w, 1 /* reparse */);
13655 }
13656 catch (const gdb_exception &e)
13657 {
13658 bpt->enable_state = orig_enable_state;
13659 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13660 bpt->number);
13661 return;
13662 }
13663 }
13664
13665 bpt->enable_state = bp_enabled;
13666
13667 /* Mark breakpoint locations modified. */
13668 mark_breakpoint_modified (bpt);
13669
13670 if (target_supports_enable_disable_tracepoint ()
13671 && current_trace_status ()->running && is_tracepoint (bpt))
13672 {
13673 for (bp_location *location : bpt->locations ())
13674 target_enable_tracepoint (location);
13675 }
13676
13677 bpt->disposition = disposition;
13678 bpt->enable_count = count;
13679 update_global_location_list (UGLL_MAY_INSERT);
13680
13681 gdb::observers::breakpoint_modified.notify (bpt);
13682 }
13683
13684
13685 void
13686 enable_breakpoint (struct breakpoint *bpt)
13687 {
13688 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13689 }
13690
13691 /* The enable command enables the specified breakpoints/locations (or
13692 all defined breakpoints) so they once again become (or continue to
13693 be) effective in stopping the inferior. ARGS may be in any of the
13694 forms defined in extract_bp_number_and_location. */
13695
13696 static void
13697 enable_command (const char *args, int from_tty)
13698 {
13699 enable_disable_command (args, from_tty, true);
13700 }
13701
13702 static void
13703 enable_once_command (const char *args, int from_tty)
13704 {
13705 map_breakpoint_numbers
13706 (args, [&] (breakpoint *b)
13707 {
13708 iterate_over_related_breakpoints
13709 (b, [&] (breakpoint *bpt)
13710 {
13711 enable_breakpoint_disp (bpt, disp_disable, 1);
13712 });
13713 });
13714 }
13715
13716 static void
13717 enable_count_command (const char *args, int from_tty)
13718 {
13719 int count;
13720
13721 if (args == NULL)
13722 error_no_arg (_("hit count"));
13723
13724 count = get_number (&args);
13725
13726 map_breakpoint_numbers
13727 (args, [&] (breakpoint *b)
13728 {
13729 iterate_over_related_breakpoints
13730 (b, [&] (breakpoint *bpt)
13731 {
13732 enable_breakpoint_disp (bpt, disp_disable, count);
13733 });
13734 });
13735 }
13736
13737 static void
13738 enable_delete_command (const char *args, int from_tty)
13739 {
13740 map_breakpoint_numbers
13741 (args, [&] (breakpoint *b)
13742 {
13743 iterate_over_related_breakpoints
13744 (b, [&] (breakpoint *bpt)
13745 {
13746 enable_breakpoint_disp (bpt, disp_del, 1);
13747 });
13748 });
13749 }
13750 \f
13751 /* Invalidate last known value of any hardware watchpoint if
13752 the memory which that value represents has been written to by
13753 GDB itself. */
13754
13755 static void
13756 invalidate_bp_value_on_memory_change (struct inferior *inferior,
13757 CORE_ADDR addr, ssize_t len,
13758 const bfd_byte *data)
13759 {
13760 for (breakpoint *bp : all_breakpoints ())
13761 if (bp->enable_state == bp_enabled
13762 && bp->type == bp_hardware_watchpoint)
13763 {
13764 struct watchpoint *wp = (struct watchpoint *) bp;
13765
13766 if (wp->val_valid && wp->val != nullptr)
13767 {
13768 for (bp_location *loc : bp->locations ())
13769 if (loc->loc_type == bp_loc_hardware_watchpoint
13770 && loc->address + loc->length > addr
13771 && addr + len > loc->address)
13772 {
13773 wp->val = NULL;
13774 wp->val_valid = false;
13775 }
13776 }
13777 }
13778 }
13779
13780 /* Create and insert a breakpoint for software single step. */
13781
13782 void
13783 insert_single_step_breakpoint (struct gdbarch *gdbarch,
13784 const address_space *aspace,
13785 CORE_ADDR next_pc)
13786 {
13787 struct thread_info *tp = inferior_thread ();
13788 struct symtab_and_line sal;
13789 CORE_ADDR pc = next_pc;
13790
13791 if (tp->control.single_step_breakpoints == NULL)
13792 {
13793 tp->control.single_step_breakpoints
13794 = new_single_step_breakpoint (tp->global_num, gdbarch);
13795 }
13796
13797 sal = find_pc_line (pc, 0);
13798 sal.pc = pc;
13799 sal.section = find_pc_overlay (pc);
13800 sal.explicit_pc = 1;
13801 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
13802
13803 update_global_location_list (UGLL_INSERT);
13804 }
13805
13806 /* Insert single step breakpoints according to the current state. */
13807
13808 int
13809 insert_single_step_breakpoints (struct gdbarch *gdbarch)
13810 {
13811 struct regcache *regcache = get_current_regcache ();
13812 std::vector<CORE_ADDR> next_pcs;
13813
13814 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
13815
13816 if (!next_pcs.empty ())
13817 {
13818 struct frame_info *frame = get_current_frame ();
13819 const address_space *aspace = get_frame_address_space (frame);
13820
13821 for (CORE_ADDR pc : next_pcs)
13822 insert_single_step_breakpoint (gdbarch, aspace, pc);
13823
13824 return 1;
13825 }
13826 else
13827 return 0;
13828 }
13829
13830 /* See breakpoint.h. */
13831
13832 int
13833 breakpoint_has_location_inserted_here (struct breakpoint *bp,
13834 const address_space *aspace,
13835 CORE_ADDR pc)
13836 {
13837 for (bp_location *loc : bp->locations ())
13838 if (loc->inserted
13839 && breakpoint_location_address_match (loc, aspace, pc))
13840 return 1;
13841
13842 return 0;
13843 }
13844
13845 /* Check whether a software single-step breakpoint is inserted at
13846 PC. */
13847
13848 int
13849 single_step_breakpoint_inserted_here_p (const address_space *aspace,
13850 CORE_ADDR pc)
13851 {
13852 for (breakpoint *bpt : all_breakpoints ())
13853 {
13854 if (bpt->type == bp_single_step
13855 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13856 return 1;
13857 }
13858 return 0;
13859 }
13860
13861 /* Tracepoint-specific operations. */
13862
13863 /* Set tracepoint count to NUM. */
13864 static void
13865 set_tracepoint_count (int num)
13866 {
13867 tracepoint_count = num;
13868 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
13869 }
13870
13871 static void
13872 trace_command (const char *arg, int from_tty)
13873 {
13874 event_location_up location = string_to_event_location (&arg,
13875 current_language);
13876 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
13877 (location.get (), true /* is_tracepoint */);
13878
13879 create_breakpoint (get_current_arch (),
13880 location.get (),
13881 NULL, 0, arg, false, 1 /* parse arg */,
13882 0 /* tempflag */,
13883 bp_tracepoint /* type_wanted */,
13884 0 /* Ignore count */,
13885 pending_break_support,
13886 ops,
13887 from_tty,
13888 1 /* enabled */,
13889 0 /* internal */, 0);
13890 }
13891
13892 static void
13893 ftrace_command (const char *arg, int from_tty)
13894 {
13895 event_location_up location = string_to_event_location (&arg,
13896 current_language);
13897 create_breakpoint (get_current_arch (),
13898 location.get (),
13899 NULL, 0, arg, false, 1 /* parse arg */,
13900 0 /* tempflag */,
13901 bp_fast_tracepoint /* type_wanted */,
13902 0 /* Ignore count */,
13903 pending_break_support,
13904 &vtable_breakpoint_ops,
13905 from_tty,
13906 1 /* enabled */,
13907 0 /* internal */, 0);
13908 }
13909
13910 /* strace command implementation. Creates a static tracepoint. */
13911
13912 static void
13913 strace_command (const char *arg, int from_tty)
13914 {
13915 struct breakpoint_ops *ops;
13916 event_location_up location;
13917
13918 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13919 or with a normal static tracepoint. */
13920 if (arg && startswith (arg, "-m") && isspace (arg[2]))
13921 {
13922 ops = &strace_marker_breakpoint_ops;
13923 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
13924 }
13925 else
13926 {
13927 ops = &vtable_breakpoint_ops;
13928 location = string_to_event_location (&arg, current_language);
13929 }
13930
13931 create_breakpoint (get_current_arch (),
13932 location.get (),
13933 NULL, 0, arg, false, 1 /* parse arg */,
13934 0 /* tempflag */,
13935 bp_static_tracepoint /* type_wanted */,
13936 0 /* Ignore count */,
13937 pending_break_support,
13938 ops,
13939 from_tty,
13940 1 /* enabled */,
13941 0 /* internal */, 0);
13942 }
13943
13944 /* Set up a fake reader function that gets command lines from a linked
13945 list that was acquired during tracepoint uploading. */
13946
13947 static struct uploaded_tp *this_utp;
13948 static int next_cmd;
13949
13950 static char *
13951 read_uploaded_action (void)
13952 {
13953 char *rslt = nullptr;
13954
13955 if (next_cmd < this_utp->cmd_strings.size ())
13956 {
13957 rslt = this_utp->cmd_strings[next_cmd].get ();
13958 next_cmd++;
13959 }
13960
13961 return rslt;
13962 }
13963
13964 /* Given information about a tracepoint as recorded on a target (which
13965 can be either a live system or a trace file), attempt to create an
13966 equivalent GDB tracepoint. This is not a reliable process, since
13967 the target does not necessarily have all the information used when
13968 the tracepoint was originally defined. */
13969
13970 struct tracepoint *
13971 create_tracepoint_from_upload (struct uploaded_tp *utp)
13972 {
13973 const char *addr_str;
13974 char small_buf[100];
13975 struct tracepoint *tp;
13976
13977 if (utp->at_string)
13978 addr_str = utp->at_string.get ();
13979 else
13980 {
13981 /* In the absence of a source location, fall back to raw
13982 address. Since there is no way to confirm that the address
13983 means the same thing as when the trace was started, warn the
13984 user. */
13985 warning (_("Uploaded tracepoint %d has no "
13986 "source location, using raw address"),
13987 utp->number);
13988 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
13989 addr_str = small_buf;
13990 }
13991
13992 /* There's not much we can do with a sequence of bytecodes. */
13993 if (utp->cond && !utp->cond_string)
13994 warning (_("Uploaded tracepoint %d condition "
13995 "has no source form, ignoring it"),
13996 utp->number);
13997
13998 event_location_up location = string_to_event_location (&addr_str,
13999 current_language);
14000 if (!create_breakpoint (get_current_arch (),
14001 location.get (),
14002 utp->cond_string.get (), -1, addr_str,
14003 false /* force_condition */,
14004 0 /* parse cond/thread */,
14005 0 /* tempflag */,
14006 utp->type /* type_wanted */,
14007 0 /* Ignore count */,
14008 pending_break_support,
14009 &vtable_breakpoint_ops,
14010 0 /* from_tty */,
14011 utp->enabled /* enabled */,
14012 0 /* internal */,
14013 CREATE_BREAKPOINT_FLAGS_INSERTED))
14014 return NULL;
14015
14016 /* Get the tracepoint we just created. */
14017 tp = get_tracepoint (tracepoint_count);
14018 gdb_assert (tp != NULL);
14019
14020 if (utp->pass > 0)
14021 {
14022 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14023 tp->number);
14024
14025 trace_pass_command (small_buf, 0);
14026 }
14027
14028 /* If we have uploaded versions of the original commands, set up a
14029 special-purpose "reader" function and call the usual command line
14030 reader, then pass the result to the breakpoint command-setting
14031 function. */
14032 if (!utp->cmd_strings.empty ())
14033 {
14034 counted_command_line cmd_list;
14035
14036 this_utp = utp;
14037 next_cmd = 0;
14038
14039 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14040
14041 breakpoint_set_commands (tp, std::move (cmd_list));
14042 }
14043 else if (!utp->actions.empty ()
14044 || !utp->step_actions.empty ())
14045 warning (_("Uploaded tracepoint %d actions "
14046 "have no source form, ignoring them"),
14047 utp->number);
14048
14049 /* Copy any status information that might be available. */
14050 tp->hit_count = utp->hit_count;
14051 tp->traceframe_usage = utp->traceframe_usage;
14052
14053 return tp;
14054 }
14055
14056 /* Print information on tracepoint number TPNUM_EXP, or all if
14057 omitted. */
14058
14059 static void
14060 info_tracepoints_command (const char *args, int from_tty)
14061 {
14062 struct ui_out *uiout = current_uiout;
14063 int num_printed;
14064
14065 num_printed = breakpoint_1 (args, false, is_tracepoint);
14066
14067 if (num_printed == 0)
14068 {
14069 if (args == NULL || *args == '\0')
14070 uiout->message ("No tracepoints.\n");
14071 else
14072 uiout->message ("No tracepoint matching '%s'.\n", args);
14073 }
14074
14075 default_collect_info ();
14076 }
14077
14078 /* The 'enable trace' command enables tracepoints.
14079 Not supported by all targets. */
14080 static void
14081 enable_trace_command (const char *args, int from_tty)
14082 {
14083 enable_command (args, from_tty);
14084 }
14085
14086 /* The 'disable trace' command disables tracepoints.
14087 Not supported by all targets. */
14088 static void
14089 disable_trace_command (const char *args, int from_tty)
14090 {
14091 disable_command (args, from_tty);
14092 }
14093
14094 /* Remove a tracepoint (or all if no argument). */
14095 static void
14096 delete_trace_command (const char *arg, int from_tty)
14097 {
14098 dont_repeat ();
14099
14100 if (arg == 0)
14101 {
14102 int breaks_to_delete = 0;
14103
14104 /* Delete all breakpoints if no argument.
14105 Do not delete internal or call-dummy breakpoints, these
14106 have to be deleted with an explicit breakpoint number
14107 argument. */
14108 for (breakpoint *tp : all_tracepoints ())
14109 if (is_tracepoint (tp) && user_breakpoint_p (tp))
14110 {
14111 breaks_to_delete = 1;
14112 break;
14113 }
14114
14115 /* Ask user only if there are some breakpoints to delete. */
14116 if (!from_tty
14117 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14118 {
14119 for (breakpoint *b : all_breakpoints_safe ())
14120 if (is_tracepoint (b) && user_breakpoint_p (b))
14121 delete_breakpoint (b);
14122 }
14123 }
14124 else
14125 map_breakpoint_numbers
14126 (arg, [&] (breakpoint *br)
14127 {
14128 iterate_over_related_breakpoints (br, delete_breakpoint);
14129 });
14130 }
14131
14132 /* Helper function for trace_pass_command. */
14133
14134 static void
14135 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14136 {
14137 tp->pass_count = count;
14138 gdb::observers::breakpoint_modified.notify (tp);
14139 if (from_tty)
14140 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
14141 tp->number, count);
14142 }
14143
14144 /* Set passcount for tracepoint.
14145
14146 First command argument is passcount, second is tracepoint number.
14147 If tracepoint number omitted, apply to most recently defined.
14148 Also accepts special argument "all". */
14149
14150 static void
14151 trace_pass_command (const char *args, int from_tty)
14152 {
14153 struct tracepoint *t1;
14154 ULONGEST count;
14155
14156 if (args == 0 || *args == 0)
14157 error (_("passcount command requires an "
14158 "argument (count + optional TP num)"));
14159
14160 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14161
14162 args = skip_spaces (args);
14163 if (*args && strncasecmp (args, "all", 3) == 0)
14164 {
14165 args += 3; /* Skip special argument "all". */
14166 if (*args)
14167 error (_("Junk at end of arguments."));
14168
14169 for (breakpoint *b : all_tracepoints ())
14170 {
14171 t1 = (struct tracepoint *) b;
14172 trace_pass_set_count (t1, count, from_tty);
14173 }
14174 }
14175 else if (*args == '\0')
14176 {
14177 t1 = get_tracepoint_by_number (&args, NULL);
14178 if (t1)
14179 trace_pass_set_count (t1, count, from_tty);
14180 }
14181 else
14182 {
14183 number_or_range_parser parser (args);
14184 while (!parser.finished ())
14185 {
14186 t1 = get_tracepoint_by_number (&args, &parser);
14187 if (t1)
14188 trace_pass_set_count (t1, count, from_tty);
14189 }
14190 }
14191 }
14192
14193 struct tracepoint *
14194 get_tracepoint (int num)
14195 {
14196 for (breakpoint *t : all_tracepoints ())
14197 if (t->number == num)
14198 return (struct tracepoint *) t;
14199
14200 return NULL;
14201 }
14202
14203 /* Find the tracepoint with the given target-side number (which may be
14204 different from the tracepoint number after disconnecting and
14205 reconnecting). */
14206
14207 struct tracepoint *
14208 get_tracepoint_by_number_on_target (int num)
14209 {
14210 for (breakpoint *b : all_tracepoints ())
14211 {
14212 struct tracepoint *t = (struct tracepoint *) b;
14213
14214 if (t->number_on_target == num)
14215 return t;
14216 }
14217
14218 return NULL;
14219 }
14220
14221 /* Utility: parse a tracepoint number and look it up in the list.
14222 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14223 If the argument is missing, the most recent tracepoint
14224 (tracepoint_count) is returned. */
14225
14226 struct tracepoint *
14227 get_tracepoint_by_number (const char **arg,
14228 number_or_range_parser *parser)
14229 {
14230 int tpnum;
14231 const char *instring = arg == NULL ? NULL : *arg;
14232
14233 if (parser != NULL)
14234 {
14235 gdb_assert (!parser->finished ());
14236 tpnum = parser->get_number ();
14237 }
14238 else if (arg == NULL || *arg == NULL || ! **arg)
14239 tpnum = tracepoint_count;
14240 else
14241 tpnum = get_number (arg);
14242
14243 if (tpnum <= 0)
14244 {
14245 if (instring && *instring)
14246 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14247 instring);
14248 else
14249 gdb_printf (_("No previous tracepoint\n"));
14250 return NULL;
14251 }
14252
14253 for (breakpoint *t : all_tracepoints ())
14254 if (t->number == tpnum)
14255 return (struct tracepoint *) t;
14256
14257 gdb_printf ("No tracepoint number %d.\n", tpnum);
14258 return NULL;
14259 }
14260
14261 void
14262 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14263 {
14264 if (b->thread != -1)
14265 gdb_printf (fp, " thread %d", b->thread);
14266
14267 if (b->task != 0)
14268 gdb_printf (fp, " task %d", b->task);
14269
14270 gdb_printf (fp, "\n");
14271 }
14272
14273 /* Save information on user settable breakpoints (watchpoints, etc) to
14274 a new script file named FILENAME. If FILTER is non-NULL, call it
14275 on each breakpoint and only include the ones for which it returns
14276 true. */
14277
14278 static void
14279 save_breakpoints (const char *filename, int from_tty,
14280 bool (*filter) (const struct breakpoint *))
14281 {
14282 int any = 0;
14283 int extra_trace_bits = 0;
14284
14285 if (filename == 0 || *filename == 0)
14286 error (_("Argument required (file name in which to save)"));
14287
14288 /* See if we have anything to save. */
14289 for (breakpoint *tp : all_breakpoints ())
14290 {
14291 /* Skip internal and momentary breakpoints. */
14292 if (!user_breakpoint_p (tp))
14293 continue;
14294
14295 /* If we have a filter, only save the breakpoints it accepts. */
14296 if (filter && !filter (tp))
14297 continue;
14298
14299 any = 1;
14300
14301 if (is_tracepoint (tp))
14302 {
14303 extra_trace_bits = 1;
14304
14305 /* We can stop searching. */
14306 break;
14307 }
14308 }
14309
14310 if (!any)
14311 {
14312 warning (_("Nothing to save."));
14313 return;
14314 }
14315
14316 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14317
14318 stdio_file fp;
14319
14320 if (!fp.open (expanded_filename.get (), "w"))
14321 error (_("Unable to open file '%s' for saving (%s)"),
14322 expanded_filename.get (), safe_strerror (errno));
14323
14324 if (extra_trace_bits)
14325 save_trace_state_variables (&fp);
14326
14327 for (breakpoint *tp : all_breakpoints ())
14328 {
14329 /* Skip internal and momentary breakpoints. */
14330 if (!user_breakpoint_p (tp))
14331 continue;
14332
14333 /* If we have a filter, only save the breakpoints it accepts. */
14334 if (filter && !filter (tp))
14335 continue;
14336
14337 tp->ops->print_recreate (tp, &fp);
14338
14339 /* Note, we can't rely on tp->number for anything, as we can't
14340 assume the recreated breakpoint numbers will match. Use $bpnum
14341 instead. */
14342
14343 if (tp->cond_string)
14344 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
14345
14346 if (tp->ignore_count)
14347 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
14348
14349 if (tp->type != bp_dprintf && tp->commands)
14350 {
14351 fp.puts (" commands\n");
14352
14353 current_uiout->redirect (&fp);
14354 try
14355 {
14356 print_command_lines (current_uiout, tp->commands.get (), 2);
14357 }
14358 catch (const gdb_exception &ex)
14359 {
14360 current_uiout->redirect (NULL);
14361 throw;
14362 }
14363
14364 current_uiout->redirect (NULL);
14365 fp.puts (" end\n");
14366 }
14367
14368 if (tp->enable_state == bp_disabled)
14369 fp.puts ("disable $bpnum\n");
14370
14371 /* If this is a multi-location breakpoint, check if the locations
14372 should be individually disabled. Watchpoint locations are
14373 special, and not user visible. */
14374 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14375 {
14376 int n = 1;
14377
14378 for (bp_location *loc : tp->locations ())
14379 {
14380 if (!loc->enabled)
14381 fp.printf ("disable $bpnum.%d\n", n);
14382
14383 n++;
14384 }
14385 }
14386 }
14387
14388 if (extra_trace_bits && !default_collect.empty ())
14389 fp.printf ("set default-collect %s\n", default_collect.c_str ());
14390
14391 if (from_tty)
14392 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
14393 }
14394
14395 /* The `save breakpoints' command. */
14396
14397 static void
14398 save_breakpoints_command (const char *args, int from_tty)
14399 {
14400 save_breakpoints (args, from_tty, NULL);
14401 }
14402
14403 /* The `save tracepoints' command. */
14404
14405 static void
14406 save_tracepoints_command (const char *args, int from_tty)
14407 {
14408 save_breakpoints (args, from_tty, is_tracepoint);
14409 }
14410
14411 \f
14412 /* This help string is used to consolidate all the help string for specifying
14413 locations used by several commands. */
14414
14415 #define LOCATION_HELP_STRING \
14416 "Linespecs are colon-separated lists of location parameters, such as\n\
14417 source filename, function name, label name, and line number.\n\
14418 Example: To specify the start of a label named \"the_top\" in the\n\
14419 function \"fact\" in the file \"factorial.c\", use\n\
14420 \"factorial.c:fact:the_top\".\n\
14421 \n\
14422 Address locations begin with \"*\" and specify an exact address in the\n\
14423 program. Example: To specify the fourth byte past the start function\n\
14424 \"main\", use \"*main + 4\".\n\
14425 \n\
14426 Explicit locations are similar to linespecs but use an option/argument\n\
14427 syntax to specify location parameters.\n\
14428 Example: To specify the start of the label named \"the_top\" in the\n\
14429 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
14430 -function fact -label the_top\".\n\
14431 \n\
14432 By default, a specified function is matched against the program's\n\
14433 functions in all scopes. For C++, this means in all namespaces and\n\
14434 classes. For Ada, this means in all packages. E.g., in C++,\n\
14435 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14436 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
14437 specified name as a complete fully-qualified name instead."
14438
14439 /* This help string is used for the break, hbreak, tbreak and thbreak
14440 commands. It is defined as a macro to prevent duplication.
14441 COMMAND should be a string constant containing the name of the
14442 command. */
14443
14444 #define BREAK_ARGS_HELP(command) \
14445 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14446 \t[-force-condition] [if CONDITION]\n\
14447 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14448 probe point. Accepted values are `-probe' (for a generic, automatically\n\
14449 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14450 `-probe-dtrace' (for a DTrace probe).\n\
14451 LOCATION may be a linespec, address, or explicit location as described\n\
14452 below.\n\
14453 \n\
14454 With no LOCATION, uses current execution address of the selected\n\
14455 stack frame. This is useful for breaking on return to a stack frame.\n\
14456 \n\
14457 THREADNUM is the number from \"info threads\".\n\
14458 CONDITION is a boolean expression.\n\
14459 \n\
14460 With the \"-force-condition\" flag, the condition is defined even when\n\
14461 it is invalid for all current locations.\n\
14462 \n" LOCATION_HELP_STRING "\n\n\
14463 Multiple breakpoints at one place are permitted, and useful if their\n\
14464 conditions are different.\n\
14465 \n\
14466 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14467
14468 /* List of subcommands for "catch". */
14469 static struct cmd_list_element *catch_cmdlist;
14470
14471 /* List of subcommands for "tcatch". */
14472 static struct cmd_list_element *tcatch_cmdlist;
14473
14474 void
14475 add_catch_command (const char *name, const char *docstring,
14476 cmd_func_ftype *func,
14477 completer_ftype *completer,
14478 void *user_data_catch,
14479 void *user_data_tcatch)
14480 {
14481 struct cmd_list_element *command;
14482
14483 command = add_cmd (name, class_breakpoint, docstring,
14484 &catch_cmdlist);
14485 command->func = func;
14486 command->set_context (user_data_catch);
14487 set_cmd_completer (command, completer);
14488
14489 command = add_cmd (name, class_breakpoint, docstring,
14490 &tcatch_cmdlist);
14491 command->func = func;
14492 command->set_context (user_data_tcatch);
14493 set_cmd_completer (command, completer);
14494 }
14495
14496 /* Zero if any of the breakpoint's locations could be a location where
14497 functions have been inlined, nonzero otherwise. */
14498
14499 static int
14500 is_non_inline_function (struct breakpoint *b)
14501 {
14502 /* The shared library event breakpoint is set on the address of a
14503 non-inline function. */
14504 if (b->type == bp_shlib_event)
14505 return 1;
14506
14507 return 0;
14508 }
14509
14510 /* Nonzero if the specified PC cannot be a location where functions
14511 have been inlined. */
14512
14513 int
14514 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
14515 const target_waitstatus &ws)
14516 {
14517 for (breakpoint *b : all_breakpoints ())
14518 {
14519 if (!is_non_inline_function (b))
14520 continue;
14521
14522 for (bp_location *bl : b->locations ())
14523 {
14524 if (!bl->shlib_disabled
14525 && bpstat_check_location (bl, aspace, pc, ws))
14526 return 1;
14527 }
14528 }
14529
14530 return 0;
14531 }
14532
14533 /* Remove any references to OBJFILE which is going to be freed. */
14534
14535 void
14536 breakpoint_free_objfile (struct objfile *objfile)
14537 {
14538 for (bp_location *loc : all_bp_locations ())
14539 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
14540 loc->symtab = NULL;
14541 }
14542
14543 void
14544 initialize_breakpoint_ops (void)
14545 {
14546 static int initialized = 0;
14547
14548 struct breakpoint_ops *ops;
14549
14550 if (initialized)
14551 return;
14552 initialized = 1;
14553
14554 /* The breakpoint_ops structure to be inherit by all kinds of
14555 breakpoints (real breakpoints, i.e., user "break" breakpoints,
14556 internal and momentary breakpoints, etc.). */
14557 ops = &bkpt_base_breakpoint_ops;
14558 *ops = vtable_breakpoint_ops;
14559
14560 /* Ranged breakpoints. */
14561 ops = &ranged_breakpoint_ops;
14562 *ops = vtable_breakpoint_ops;
14563 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
14564 ops->resources_needed = resources_needed_ranged_breakpoint;
14565 ops->print_it = print_it_ranged_breakpoint;
14566 ops->print_one = print_one_ranged_breakpoint;
14567 ops->print_one_detail = print_one_detail_ranged_breakpoint;
14568 ops->print_mention = print_mention_ranged_breakpoint;
14569 ops->print_recreate = print_recreate_ranged_breakpoint;
14570
14571 /* Probe breakpoints. */
14572 ops = &bkpt_probe_breakpoint_ops;
14573 *ops = vtable_breakpoint_ops;
14574 ops->insert_location = bkpt_probe_insert_location;
14575 ops->remove_location = bkpt_probe_remove_location;
14576 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
14577 ops->decode_location = bkpt_probe_decode_location;
14578
14579 /* Probe tracepoints. */
14580 ops = &tracepoint_probe_breakpoint_ops;
14581 *ops = vtable_breakpoint_ops;
14582 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
14583 ops->decode_location = tracepoint_probe_decode_location;
14584
14585 /* Static tracepoints with marker (`-m'). */
14586 ops = &strace_marker_breakpoint_ops;
14587 *ops = vtable_breakpoint_ops;
14588 ops->create_sals_from_location = strace_marker_create_sals_from_location;
14589 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
14590 ops->decode_location = strace_marker_decode_location;
14591
14592 ops = &dprintf_breakpoint_ops;
14593 *ops = vtable_breakpoint_ops;
14594 ops->re_set = dprintf_re_set;
14595 ops->print_recreate = dprintf_print_recreate;
14596 ops->after_condition_true = dprintf_after_condition_true;
14597 ops->breakpoint_hit = dprintf_breakpoint_hit;
14598 }
14599
14600 /* Chain containing all defined "enable breakpoint" subcommands. */
14601
14602 static struct cmd_list_element *enablebreaklist = NULL;
14603
14604 /* See breakpoint.h. */
14605
14606 cmd_list_element *commands_cmd_element = nullptr;
14607
14608 void _initialize_breakpoint ();
14609 void
14610 _initialize_breakpoint ()
14611 {
14612 struct cmd_list_element *c;
14613
14614 initialize_breakpoint_ops ();
14615
14616 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14617 "breakpoint");
14618 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14619 "breakpoint");
14620 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14621 "breakpoint");
14622
14623 breakpoint_chain = 0;
14624 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14625 before a breakpoint is set. */
14626 breakpoint_count = 0;
14627
14628 tracepoint_count = 0;
14629
14630 add_com ("ignore", class_breakpoint, ignore_command, _("\
14631 Set ignore-count of breakpoint number N to COUNT.\n\
14632 Usage is `ignore N COUNT'."));
14633
14634 commands_cmd_element = add_com ("commands", class_breakpoint,
14635 commands_command, _("\
14636 Set commands to be executed when the given breakpoints are hit.\n\
14637 Give a space-separated breakpoint list as argument after \"commands\".\n\
14638 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14639 (e.g. `5-7').\n\
14640 With no argument, the targeted breakpoint is the last one set.\n\
14641 The commands themselves follow starting on the next line.\n\
14642 Type a line containing \"end\" to indicate the end of them.\n\
14643 Give \"silent\" as the first line to make the breakpoint silent;\n\
14644 then no output is printed when it is hit, except what the commands print."));
14645
14646 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14647 static std::string condition_command_help
14648 = gdb::option::build_help (_("\
14649 Specify breakpoint number N to break only if COND is true.\n\
14650 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
14651 is an expression to be evaluated whenever breakpoint N is reached.\n\
14652 \n\
14653 Options:\n\
14654 %OPTIONS%"), cc_opts);
14655
14656 c = add_com ("condition", class_breakpoint, condition_command,
14657 condition_command_help.c_str ());
14658 set_cmd_completer_handle_brkchars (c, condition_completer);
14659
14660 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
14661 Set a temporary breakpoint.\n\
14662 Like \"break\" except the breakpoint is only temporary,\n\
14663 so it will be deleted when hit. Equivalent to \"break\" followed\n\
14664 by using \"enable delete\" on the breakpoint number.\n\
14665 \n"
14666 BREAK_ARGS_HELP ("tbreak")));
14667 set_cmd_completer (c, location_completer);
14668
14669 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
14670 Set a hardware assisted breakpoint.\n\
14671 Like \"break\" except the breakpoint requires hardware support,\n\
14672 some target hardware may not have this support.\n\
14673 \n"
14674 BREAK_ARGS_HELP ("hbreak")));
14675 set_cmd_completer (c, location_completer);
14676
14677 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
14678 Set a temporary hardware assisted breakpoint.\n\
14679 Like \"hbreak\" except the breakpoint is only temporary,\n\
14680 so it will be deleted when hit.\n\
14681 \n"
14682 BREAK_ARGS_HELP ("thbreak")));
14683 set_cmd_completer (c, location_completer);
14684
14685 cmd_list_element *enable_cmd
14686 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14687 Enable all or some breakpoints.\n\
14688 Usage: enable [BREAKPOINTNUM]...\n\
14689 Give breakpoint numbers (separated by spaces) as arguments.\n\
14690 With no subcommand, breakpoints are enabled until you command otherwise.\n\
14691 This is used to cancel the effect of the \"disable\" command.\n\
14692 With a subcommand you can enable temporarily."),
14693 &enablelist, 1, &cmdlist);
14694
14695 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
14696
14697 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
14698 Enable all or some breakpoints.\n\
14699 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
14700 Give breakpoint numbers (separated by spaces) as arguments.\n\
14701 This is used to cancel the effect of the \"disable\" command.\n\
14702 May be abbreviated to simply \"enable\"."),
14703 &enablebreaklist, 1, &enablelist);
14704
14705 add_cmd ("once", no_class, enable_once_command, _("\
14706 Enable some breakpoints for one hit.\n\
14707 Usage: enable breakpoints once BREAKPOINTNUM...\n\
14708 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14709 &enablebreaklist);
14710
14711 add_cmd ("delete", no_class, enable_delete_command, _("\
14712 Enable some breakpoints and delete when hit.\n\
14713 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
14714 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14715 &enablebreaklist);
14716
14717 add_cmd ("count", no_class, enable_count_command, _("\
14718 Enable some breakpoints for COUNT hits.\n\
14719 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
14720 If a breakpoint is hit while enabled in this fashion,\n\
14721 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14722 &enablebreaklist);
14723
14724 add_cmd ("delete", no_class, enable_delete_command, _("\
14725 Enable some breakpoints and delete when hit.\n\
14726 Usage: enable delete BREAKPOINTNUM...\n\
14727 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14728 &enablelist);
14729
14730 add_cmd ("once", no_class, enable_once_command, _("\
14731 Enable some breakpoints for one hit.\n\
14732 Usage: enable once BREAKPOINTNUM...\n\
14733 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14734 &enablelist);
14735
14736 add_cmd ("count", no_class, enable_count_command, _("\
14737 Enable some breakpoints for COUNT hits.\n\
14738 Usage: enable count COUNT BREAKPOINTNUM...\n\
14739 If a breakpoint is hit while enabled in this fashion,\n\
14740 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14741 &enablelist);
14742
14743 cmd_list_element *disable_cmd
14744 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14745 Disable all or some breakpoints.\n\
14746 Usage: disable [BREAKPOINTNUM]...\n\
14747 Arguments are breakpoint numbers with spaces in between.\n\
14748 To disable all breakpoints, give no argument.\n\
14749 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
14750 &disablelist, 1, &cmdlist);
14751 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14752 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
14753
14754 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
14755 Disable all or some breakpoints.\n\
14756 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
14757 Arguments are breakpoint numbers with spaces in between.\n\
14758 To disable all breakpoints, give no argument.\n\
14759 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
14760 This command may be abbreviated \"disable\"."),
14761 &disablelist);
14762
14763 cmd_list_element *delete_cmd
14764 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14765 Delete all or some breakpoints.\n\
14766 Usage: delete [BREAKPOINTNUM]...\n\
14767 Arguments are breakpoint numbers with spaces in between.\n\
14768 To delete all breakpoints, give no argument.\n\
14769 \n\
14770 Also a prefix command for deletion of other GDB objects."),
14771 &deletelist, 1, &cmdlist);
14772 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14773 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
14774
14775 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
14776 Delete all or some breakpoints or auto-display expressions.\n\
14777 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
14778 Arguments are breakpoint numbers with spaces in between.\n\
14779 To delete all breakpoints, give no argument.\n\
14780 This command may be abbreviated \"delete\"."),
14781 &deletelist);
14782
14783 cmd_list_element *clear_cmd
14784 = add_com ("clear", class_breakpoint, clear_command, _("\
14785 Clear breakpoint at specified location.\n\
14786 Argument may be a linespec, explicit, or address location as described below.\n\
14787 \n\
14788 With no argument, clears all breakpoints in the line that the selected frame\n\
14789 is executing in.\n"
14790 "\n" LOCATION_HELP_STRING "\n\n\
14791 See also the \"delete\" command which clears breakpoints by number."));
14792 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
14793
14794 cmd_list_element *break_cmd
14795 = add_com ("break", class_breakpoint, break_command, _("\
14796 Set breakpoint at specified location.\n"
14797 BREAK_ARGS_HELP ("break")));
14798 set_cmd_completer (break_cmd, location_completer);
14799
14800 add_com_alias ("b", break_cmd, class_run, 1);
14801 add_com_alias ("br", break_cmd, class_run, 1);
14802 add_com_alias ("bre", break_cmd, class_run, 1);
14803 add_com_alias ("brea", break_cmd, class_run, 1);
14804
14805 cmd_list_element *info_breakpoints_cmd
14806 = add_info ("breakpoints", info_breakpoints_command, _("\
14807 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
14808 The \"Type\" column indicates one of:\n\
14809 \tbreakpoint - normal breakpoint\n\
14810 \twatchpoint - watchpoint\n\
14811 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14812 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14813 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14814 address and file/line number respectively.\n\
14815 \n\
14816 Convenience variable \"$_\" and default examine address for \"x\"\n\
14817 are set to the address of the last breakpoint listed unless the command\n\
14818 is prefixed with \"server \".\n\n\
14819 Convenience variable \"$bpnum\" contains the number of the last\n\
14820 breakpoint set."));
14821
14822 add_info_alias ("b", info_breakpoints_cmd, 1);
14823
14824 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14825 Status of all breakpoints, or breakpoint number NUMBER.\n\
14826 The \"Type\" column indicates one of:\n\
14827 \tbreakpoint - normal breakpoint\n\
14828 \twatchpoint - watchpoint\n\
14829 \tlongjmp - internal breakpoint used to step through longjmp()\n\
14830 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14831 \tuntil - internal breakpoint used by the \"until\" command\n\
14832 \tfinish - internal breakpoint used by the \"finish\" command\n\
14833 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14834 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14835 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14836 address and file/line number respectively.\n\
14837 \n\
14838 Convenience variable \"$_\" and default examine address for \"x\"\n\
14839 are set to the address of the last breakpoint listed unless the command\n\
14840 is prefixed with \"server \".\n\n\
14841 Convenience variable \"$bpnum\" contains the number of the last\n\
14842 breakpoint set."),
14843 &maintenanceinfolist);
14844
14845 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
14846 Set catchpoints to catch events."),
14847 &catch_cmdlist,
14848 0/*allow-unknown*/, &cmdlist);
14849
14850 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
14851 Set temporary catchpoints to catch events."),
14852 &tcatch_cmdlist,
14853 0/*allow-unknown*/, &cmdlist);
14854
14855 const auto opts = make_watch_options_def_group (nullptr);
14856
14857 static const std::string watch_help = gdb::option::build_help (_("\
14858 Set a watchpoint for EXPRESSION.\n\
14859 Usage: watch [-location] EXPRESSION\n\
14860 \n\
14861 Options:\n\
14862 %OPTIONS%\n\
14863 \n\
14864 A watchpoint stops execution of your program whenever the value of\n\
14865 an expression changes."), opts);
14866 c = add_com ("watch", class_breakpoint, watch_command,
14867 watch_help.c_str ());
14868 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14869
14870 static const std::string rwatch_help = gdb::option::build_help (_("\
14871 Set a read watchpoint for EXPRESSION.\n\
14872 Usage: rwatch [-location] EXPRESSION\n\
14873 \n\
14874 Options:\n\
14875 %OPTIONS%\n\
14876 \n\
14877 A read watchpoint stops execution of your program whenever the value of\n\
14878 an expression is read."), opts);
14879 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14880 rwatch_help.c_str ());
14881 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14882
14883 static const std::string awatch_help = gdb::option::build_help (_("\
14884 Set an access watchpoint for EXPRESSION.\n\
14885 Usage: awatch [-location] EXPRESSION\n\
14886 \n\
14887 Options:\n\
14888 %OPTIONS%\n\
14889 \n\
14890 An access watchpoint stops execution of your program whenever the value\n\
14891 of an expression is either read or written."), opts);
14892 c = add_com ("awatch", class_breakpoint, awatch_command,
14893 awatch_help.c_str ());
14894 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14895
14896 add_info ("watchpoints", info_watchpoints_command, _("\
14897 Status of specified watchpoints (all watchpoints if no argument)."));
14898
14899 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14900 respond to changes - contrary to the description. */
14901 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14902 &can_use_hw_watchpoints, _("\
14903 Set debugger's willingness to use watchpoint hardware."), _("\
14904 Show debugger's willingness to use watchpoint hardware."), _("\
14905 If zero, gdb will not use hardware for new watchpoints, even if\n\
14906 such is available. (However, any hardware watchpoints that were\n\
14907 created before setting this to nonzero, will continue to use watchpoint\n\
14908 hardware.)"),
14909 NULL,
14910 show_can_use_hw_watchpoints,
14911 &setlist, &showlist);
14912
14913 can_use_hw_watchpoints = 1;
14914
14915 /* Tracepoint manipulation commands. */
14916
14917 cmd_list_element *trace_cmd
14918 = add_com ("trace", class_breakpoint, trace_command, _("\
14919 Set a tracepoint at specified location.\n\
14920 \n"
14921 BREAK_ARGS_HELP ("trace") "\n\
14922 Do \"help tracepoints\" for info on other tracepoint commands."));
14923 set_cmd_completer (trace_cmd, location_completer);
14924
14925 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14926 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14927 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14928 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
14929
14930 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
14931 Set a fast tracepoint at specified location.\n\
14932 \n"
14933 BREAK_ARGS_HELP ("ftrace") "\n\
14934 Do \"help tracepoints\" for info on other tracepoint commands."));
14935 set_cmd_completer (c, location_completer);
14936
14937 c = add_com ("strace", class_breakpoint, strace_command, _("\
14938 Set a static tracepoint at location or marker.\n\
14939 \n\
14940 strace [LOCATION] [if CONDITION]\n\
14941 LOCATION may be a linespec, explicit, or address location (described below) \n\
14942 or -m MARKER_ID.\n\n\
14943 If a marker id is specified, probe the marker with that name. With\n\
14944 no LOCATION, uses current execution address of the selected stack frame.\n\
14945 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14946 This collects arbitrary user data passed in the probe point call to the\n\
14947 tracing library. You can inspect it when analyzing the trace buffer,\n\
14948 by printing the $_sdata variable like any other convenience variable.\n\
14949 \n\
14950 CONDITION is a boolean expression.\n\
14951 \n" LOCATION_HELP_STRING "\n\n\
14952 Multiple tracepoints at one place are permitted, and useful if their\n\
14953 conditions are different.\n\
14954 \n\
14955 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14956 Do \"help tracepoints\" for info on other tracepoint commands."));
14957 set_cmd_completer (c, location_completer);
14958
14959 cmd_list_element *info_tracepoints_cmd
14960 = add_info ("tracepoints", info_tracepoints_command, _("\
14961 Status of specified tracepoints (all tracepoints if no argument).\n\
14962 Convenience variable \"$tpnum\" contains the number of the\n\
14963 last tracepoint set."));
14964
14965 add_info_alias ("tp", info_tracepoints_cmd, 1);
14966
14967 cmd_list_element *delete_tracepoints_cmd
14968 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
14969 Delete specified tracepoints.\n\
14970 Arguments are tracepoint numbers, separated by spaces.\n\
14971 No argument means delete all tracepoints."),
14972 &deletelist);
14973 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
14974
14975 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14976 Disable specified tracepoints.\n\
14977 Arguments are tracepoint numbers, separated by spaces.\n\
14978 No argument means disable all tracepoints."),
14979 &disablelist);
14980 deprecate_cmd (c, "disable");
14981
14982 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14983 Enable specified tracepoints.\n\
14984 Arguments are tracepoint numbers, separated by spaces.\n\
14985 No argument means enable all tracepoints."),
14986 &enablelist);
14987 deprecate_cmd (c, "enable");
14988
14989 add_com ("passcount", class_trace, trace_pass_command, _("\
14990 Set the passcount for a tracepoint.\n\
14991 The trace will end when the tracepoint has been passed 'count' times.\n\
14992 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14993 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14994
14995 add_basic_prefix_cmd ("save", class_breakpoint,
14996 _("Save breakpoint definitions as a script."),
14997 &save_cmdlist,
14998 0/*allow-unknown*/, &cmdlist);
14999
15000 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15001 Save current breakpoint definitions as a script.\n\
15002 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15003 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15004 session to restore them."),
15005 &save_cmdlist);
15006 set_cmd_completer (c, filename_completer);
15007
15008 cmd_list_element *save_tracepoints_cmd
15009 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15010 Save current tracepoint definitions as a script.\n\
15011 Use the 'source' command in another debug session to restore them."),
15012 &save_cmdlist);
15013 set_cmd_completer (save_tracepoints_cmd, filename_completer);
15014
15015 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
15016 deprecate_cmd (c, "save tracepoints");
15017
15018 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
15019 _("\
15020 Breakpoint specific settings.\n\
15021 Configure various breakpoint-specific variables such as\n\
15022 pending breakpoint behavior."),
15023 _("\
15024 Breakpoint specific settings.\n\
15025 Configure various breakpoint-specific variables such as\n\
15026 pending breakpoint behavior."),
15027 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
15028 &setlist, &showlist);
15029
15030 add_setshow_auto_boolean_cmd ("pending", no_class,
15031 &pending_break_support, _("\
15032 Set debugger's behavior regarding pending breakpoints."), _("\
15033 Show debugger's behavior regarding pending breakpoints."), _("\
15034 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15035 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15036 an error. If auto, an unrecognized breakpoint location results in a\n\
15037 user-query to see if a pending breakpoint should be created."),
15038 NULL,
15039 show_pending_break_support,
15040 &breakpoint_set_cmdlist,
15041 &breakpoint_show_cmdlist);
15042
15043 pending_break_support = AUTO_BOOLEAN_AUTO;
15044
15045 add_setshow_boolean_cmd ("auto-hw", no_class,
15046 &automatic_hardware_breakpoints, _("\
15047 Set automatic usage of hardware breakpoints."), _("\
15048 Show automatic usage of hardware breakpoints."), _("\
15049 If set, the debugger will automatically use hardware breakpoints for\n\
15050 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15051 a warning will be emitted for such breakpoints."),
15052 NULL,
15053 show_automatic_hardware_breakpoints,
15054 &breakpoint_set_cmdlist,
15055 &breakpoint_show_cmdlist);
15056
15057 add_setshow_boolean_cmd ("always-inserted", class_support,
15058 &always_inserted_mode, _("\
15059 Set mode for inserting breakpoints."), _("\
15060 Show mode for inserting breakpoints."), _("\
15061 When this mode is on, breakpoints are inserted immediately as soon as\n\
15062 they're created, kept inserted even when execution stops, and removed\n\
15063 only when the user deletes them. When this mode is off (the default),\n\
15064 breakpoints are inserted only when execution continues, and removed\n\
15065 when execution stops."),
15066 NULL,
15067 &show_always_inserted_mode,
15068 &breakpoint_set_cmdlist,
15069 &breakpoint_show_cmdlist);
15070
15071 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15072 condition_evaluation_enums,
15073 &condition_evaluation_mode_1, _("\
15074 Set mode of breakpoint condition evaluation."), _("\
15075 Show mode of breakpoint condition evaluation."), _("\
15076 When this is set to \"host\", breakpoint conditions will be\n\
15077 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15078 breakpoint conditions will be downloaded to the target (if the target\n\
15079 supports such feature) and conditions will be evaluated on the target's side.\n\
15080 If this is set to \"auto\" (default), this will be automatically set to\n\
15081 \"target\" if it supports condition evaluation, otherwise it will\n\
15082 be set to \"host\"."),
15083 &set_condition_evaluation_mode,
15084 &show_condition_evaluation_mode,
15085 &breakpoint_set_cmdlist,
15086 &breakpoint_show_cmdlist);
15087
15088 add_com ("break-range", class_breakpoint, break_range_command, _("\
15089 Set a breakpoint for an address range.\n\
15090 break-range START-LOCATION, END-LOCATION\n\
15091 where START-LOCATION and END-LOCATION can be one of the following:\n\
15092 LINENUM, for that line in the current file,\n\
15093 FILE:LINENUM, for that line in that file,\n\
15094 +OFFSET, for that number of lines after the current line\n\
15095 or the start of the range\n\
15096 FUNCTION, for the first line in that function,\n\
15097 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15098 *ADDRESS, for the instruction at that address.\n\
15099 \n\
15100 The breakpoint will stop execution of the inferior whenever it executes\n\
15101 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15102 range (including START-LOCATION and END-LOCATION)."));
15103
15104 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15105 Set a dynamic printf at specified location.\n\
15106 dprintf location,format string,arg1,arg2,...\n\
15107 location may be a linespec, explicit, or address location.\n"
15108 "\n" LOCATION_HELP_STRING));
15109 set_cmd_completer (c, location_completer);
15110
15111 add_setshow_enum_cmd ("dprintf-style", class_support,
15112 dprintf_style_enums, &dprintf_style, _("\
15113 Set the style of usage for dynamic printf."), _("\
15114 Show the style of usage for dynamic printf."), _("\
15115 This setting chooses how GDB will do a dynamic printf.\n\
15116 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15117 console, as with the \"printf\" command.\n\
15118 If the value is \"call\", the print is done by calling a function in your\n\
15119 program; by default printf(), but you can choose a different function or\n\
15120 output stream by setting dprintf-function and dprintf-channel."),
15121 update_dprintf_commands, NULL,
15122 &setlist, &showlist);
15123
15124 add_setshow_string_cmd ("dprintf-function", class_support,
15125 &dprintf_function, _("\
15126 Set the function to use for dynamic printf."), _("\
15127 Show the function to use for dynamic printf."), NULL,
15128 update_dprintf_commands, NULL,
15129 &setlist, &showlist);
15130
15131 add_setshow_string_cmd ("dprintf-channel", class_support,
15132 &dprintf_channel, _("\
15133 Set the channel to use for dynamic printf."), _("\
15134 Show the channel to use for dynamic printf."), NULL,
15135 update_dprintf_commands, NULL,
15136 &setlist, &showlist);
15137
15138 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15139 &disconnected_dprintf, _("\
15140 Set whether dprintf continues after GDB disconnects."), _("\
15141 Show whether dprintf continues after GDB disconnects."), _("\
15142 Use this to let dprintf commands continue to hit and produce output\n\
15143 even if GDB disconnects or detaches from the target."),
15144 NULL,
15145 NULL,
15146 &setlist, &showlist);
15147
15148 add_com ("agent-printf", class_vars, agent_printf_command, _("\
15149 Target agent only formatted printing, like the C \"printf\" function.\n\
15150 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15151 This supports most C printf format specifications, like %s, %d, etc.\n\
15152 This is useful for formatted output in user-defined commands."));
15153
15154 automatic_hardware_breakpoints = true;
15155
15156 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
15157 "breakpoint");
15158 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
15159 "breakpoint");
15160 }