Introduce and use parser flags
[binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2023 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 "ui.h"
57 #include "valprint.h"
58 #include "jit.h"
59 #include "parser-defs.h"
60 #include "gdbsupport/gdb_regex.h"
61 #include "probe.h"
62 #include "cli/cli-utils.h"
63 #include "stack.h"
64 #include "ax-gdb.h"
65 #include "dummy-frame.h"
66 #include "interps.h"
67 #include "gdbsupport/format.h"
68 #include "thread-fsm.h"
69 #include "tid-parse.h"
70 #include "cli/cli-style.h"
71 #include "cli/cli-decode.h"
72 #include <unordered_set>
73
74 /* readline include files */
75 #include "readline/tilde.h"
76
77 /* readline defines this. */
78 #undef savestring
79
80 #include "mi/mi-common.h"
81 #include "extension.h"
82 #include <algorithm>
83 #include "progspace-and-thread.h"
84 #include "gdbsupport/array-view.h"
85 #include "gdbsupport/gdb_optional.h"
86
87 /* Prototypes for local functions. */
88
89 static void map_breakpoint_numbers (const char *,
90 gdb::function_view<void (breakpoint *)>);
91
92 static void
93 create_sals_from_location_spec_default (location_spec *locspec,
94 linespec_result *canonical);
95
96 static void create_breakpoints_sal (struct gdbarch *,
97 struct linespec_result *,
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
101 enum bpdisp, int, int,
102 int,
103 int, int, int, unsigned);
104
105 static int can_use_hardware_watchpoint
106 (const std::vector<value_ref_ptr> &vals);
107
108 static void mention (const breakpoint *);
109
110 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
111
112 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
113
114 static struct breakpoint *
115 momentary_breakpoint_from_master (struct breakpoint *orig,
116 enum bptype type,
117 int loc_enabled, int thread);
118
119 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, bool);
120
121 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
122 CORE_ADDR bpaddr,
123 enum bptype bptype,
124 struct program_space *pspace);
125
126 static bool watchpoint_locations_match (const struct bp_location *loc1,
127 const struct bp_location *loc2);
128
129 static bool breakpoint_locations_match (const struct bp_location *loc1,
130 const struct bp_location *loc2,
131 bool sw_hw_bps_match = false);
132
133 static bool breakpoint_location_address_match (struct bp_location *bl,
134 const struct address_space *aspace,
135 CORE_ADDR addr);
136
137 static bool breakpoint_location_address_range_overlap (struct bp_location *,
138 const address_space *,
139 CORE_ADDR, int);
140
141 static int remove_breakpoint (struct bp_location *);
142 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
143
144 static enum print_stop_action print_bp_stop_message (bpstat *bs);
145
146 static int hw_breakpoint_used_count (void);
147
148 static int hw_watchpoint_use_count (struct breakpoint *);
149
150 static int hw_watchpoint_used_count_others (struct breakpoint *except,
151 enum bptype type,
152 int *other_type_used);
153
154 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
155 int count);
156
157 static void decref_bp_location (struct bp_location **loc);
158
159 static std::vector<symtab_and_line> bkpt_probe_decode_location_spec
160 (struct breakpoint *b,
161 location_spec *locspec,
162 struct program_space *search_pspace);
163
164 static bool bl_address_is_meaningful (bp_location *loc);
165
166 static int find_loc_num_by_location (const bp_location *loc);
167
168 /* update_global_location_list's modes of operation wrt to whether to
169 insert locations now. */
170 enum ugll_insert_mode
171 {
172 /* Don't insert any breakpoint locations into the inferior, only
173 remove already-inserted locations that no longer should be
174 inserted. Functions that delete a breakpoint or breakpoints
175 should specify this mode, so that deleting a breakpoint doesn't
176 have the side effect of inserting the locations of other
177 breakpoints that are marked not-inserted, but should_be_inserted
178 returns true on them.
179
180 This behavior is useful is situations close to tear-down -- e.g.,
181 after an exec, while the target still has execution, but
182 breakpoint shadows of the previous executable image should *NOT*
183 be restored to the new image; or before detaching, where the
184 target still has execution and wants to delete breakpoints from
185 GDB's lists, and all breakpoints had already been removed from
186 the inferior. */
187 UGLL_DONT_INSERT,
188
189 /* May insert breakpoints iff breakpoints_should_be_inserted_now
190 claims breakpoints should be inserted now. */
191 UGLL_MAY_INSERT,
192
193 /* Insert locations now, irrespective of
194 breakpoints_should_be_inserted_now. E.g., say all threads are
195 stopped right now, and the user did "continue". We need to
196 insert breakpoints _before_ resuming the target, but
197 UGLL_MAY_INSERT wouldn't insert them, because
198 breakpoints_should_be_inserted_now returns false at that point,
199 as no thread is running yet. */
200 UGLL_INSERT
201 };
202
203 static void update_global_location_list (enum ugll_insert_mode);
204
205 static void update_global_location_list_nothrow (enum ugll_insert_mode);
206
207 static void insert_breakpoint_locations (void);
208
209 static void trace_pass_command (const char *, int);
210
211 static void set_tracepoint_count (int num);
212
213 static bool is_masked_watchpoint (const struct breakpoint *b);
214
215 /* Return true if B refers to a static tracepoint set by marker ("-m"),
216 zero otherwise. */
217
218 static bool strace_marker_p (struct breakpoint *b);
219
220 static void bkpt_probe_create_sals_from_location_spec
221 (location_spec *locspec,
222 struct linespec_result *canonical);
223 static void tracepoint_probe_create_sals_from_location_spec
224 (location_spec *locspec,
225 struct linespec_result *canonical);
226
227 const struct breakpoint_ops code_breakpoint_ops =
228 {
229 create_sals_from_location_spec_default,
230 create_breakpoints_sal,
231 };
232
233 /* Breakpoints set on probes. */
234 static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
235 {
236 bkpt_probe_create_sals_from_location_spec,
237 create_breakpoints_sal,
238 };
239
240 /* Tracepoints set on probes. */
241 static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
242 {
243 tracepoint_probe_create_sals_from_location_spec,
244 create_breakpoints_sal,
245 };
246
247 /* Implementation of abstract dtors. These must exist to satisfy the
248 linker. */
249
250 breakpoint::~breakpoint ()
251 {
252 }
253
254 code_breakpoint::~code_breakpoint ()
255 {
256 }
257
258 catchpoint::~catchpoint ()
259 {
260 }
261
262 /* The structure to be used in regular breakpoints. */
263 struct ordinary_breakpoint : public code_breakpoint
264 {
265 using code_breakpoint::code_breakpoint;
266
267 int resources_needed (const struct bp_location *) override;
268 enum print_stop_action print_it (const bpstat *bs) const override;
269 void print_mention () const override;
270 void print_recreate (struct ui_file *fp) const override;
271 };
272
273 /* Internal breakpoints. These typically have a lifetime the same as
274 the program, and they end up installed on the breakpoint chain with
275 a negative breakpoint number. They're visible in "maint info
276 breakpoints", but not "info breakpoints". */
277 struct internal_breakpoint : public code_breakpoint
278 {
279 internal_breakpoint (struct gdbarch *gdbarch,
280 enum bptype type, CORE_ADDR address)
281 : code_breakpoint (gdbarch, type)
282 {
283 symtab_and_line sal;
284 sal.pc = address;
285 sal.section = find_pc_overlay (sal.pc);
286 sal.pspace = current_program_space;
287 add_location (sal);
288
289 pspace = current_program_space;
290 disposition = disp_donttouch;
291 }
292
293 void re_set () override;
294 void check_status (struct bpstat *bs) override;
295 enum print_stop_action print_it (const bpstat *bs) const override;
296 void print_mention () const override;
297 };
298
299 /* Momentary breakpoints. These typically have a lifetime of some run
300 control command only, are always thread-specific, and have 0 for
301 breakpoint number. I.e., there can be many momentary breakpoints
302 on the breakpoint chain and they all same the same number (zero).
303 They're visible in "maint info breakpoints", but not "info
304 breakpoints". */
305 struct momentary_breakpoint : public code_breakpoint
306 {
307 momentary_breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
308 program_space *pspace_,
309 const struct frame_id &frame_id_,
310 int thread_)
311 : code_breakpoint (gdbarch_, bptype)
312 {
313 /* If FRAME_ID is valid, it should be a real frame, not an inlined
314 or tail-called one. */
315 gdb_assert (!frame_id_artificial_p (frame_id));
316
317 /* Momentary breakpoints are always thread-specific. */
318 gdb_assert (thread_ > 0);
319
320 pspace = pspace_;
321 enable_state = bp_enabled;
322 disposition = disp_donttouch;
323 frame_id = frame_id_;
324 thread = thread_;
325 }
326
327 void re_set () override;
328 void check_status (struct bpstat *bs) override;
329 enum print_stop_action print_it (const bpstat *bs) const override;
330 void print_mention () const override;
331 };
332
333 /* DPrintf breakpoints. */
334 struct dprintf_breakpoint : public ordinary_breakpoint
335 {
336 using ordinary_breakpoint::ordinary_breakpoint;
337
338 void re_set () override;
339 int breakpoint_hit (const struct bp_location *bl,
340 const address_space *aspace,
341 CORE_ADDR bp_addr,
342 const target_waitstatus &ws) override;
343 void print_recreate (struct ui_file *fp) const override;
344 void after_condition_true (struct bpstat *bs) override;
345 };
346
347 /* Ranged breakpoints. */
348 struct ranged_breakpoint : public ordinary_breakpoint
349 {
350 explicit ranged_breakpoint (struct gdbarch *gdbarch,
351 const symtab_and_line &sal_start,
352 int length,
353 location_spec_up start_locspec,
354 location_spec_up end_locspec)
355 : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
356 {
357 bp_location *bl = add_location (sal_start);
358 bl->length = length;
359
360 disposition = disp_donttouch;
361
362 locspec = std::move (start_locspec);
363 locspec_range_end = std::move (end_locspec);
364 }
365
366 int breakpoint_hit (const struct bp_location *bl,
367 const address_space *aspace,
368 CORE_ADDR bp_addr,
369 const target_waitstatus &ws) override;
370 int resources_needed (const struct bp_location *) override;
371 enum print_stop_action print_it (const bpstat *bs) const override;
372 bool print_one (bp_location **) const override;
373 void print_one_detail (struct ui_out *) const override;
374 void print_mention () const override;
375 void print_recreate (struct ui_file *fp) const override;
376 };
377
378 /* Static tracepoints with marker (`-m'). */
379 struct static_marker_tracepoint : public tracepoint
380 {
381 using tracepoint::tracepoint;
382
383 std::vector<symtab_and_line> decode_location_spec
384 (struct location_spec *locspec,
385 struct program_space *search_pspace) override;
386 };
387
388 /* The style in which to perform a dynamic printf. This is a user
389 option because different output options have different tradeoffs;
390 if GDB does the printing, there is better error handling if there
391 is a problem with any of the arguments, but using an inferior
392 function lets you have special-purpose printers and sending of
393 output to the same place as compiled-in print functions. */
394
395 static const char dprintf_style_gdb[] = "gdb";
396 static const char dprintf_style_call[] = "call";
397 static const char dprintf_style_agent[] = "agent";
398 static const char *const dprintf_style_enums[] = {
399 dprintf_style_gdb,
400 dprintf_style_call,
401 dprintf_style_agent,
402 NULL
403 };
404 static const char *dprintf_style = dprintf_style_gdb;
405
406 /* The function to use for dynamic printf if the preferred style is to
407 call into the inferior. The value is simply a string that is
408 copied into the command, so it can be anything that GDB can
409 evaluate to a callable address, not necessarily a function name. */
410
411 static std::string dprintf_function = "printf";
412
413 /* The channel to use for dynamic printf if the preferred style is to
414 call into the inferior; if a nonempty string, it will be passed to
415 the call as the first argument, with the format string as the
416 second. As with the dprintf function, this can be anything that
417 GDB knows how to evaluate, so in addition to common choices like
418 "stderr", this could be an app-specific expression like
419 "mystreams[curlogger]". */
420
421 static std::string dprintf_channel;
422
423 /* True if dprintf commands should continue to operate even if GDB
424 has disconnected. */
425 static bool disconnected_dprintf = true;
426
427 struct command_line *
428 breakpoint_commands (struct breakpoint *b)
429 {
430 return b->commands ? b->commands.get () : NULL;
431 }
432
433 /* Flag indicating that a command has proceeded the inferior past the
434 current breakpoint. */
435
436 static bool breakpoint_proceeded;
437
438 const char *
439 bpdisp_text (enum bpdisp disp)
440 {
441 /* NOTE: the following values are a part of MI protocol and
442 represent values of 'disp' field returned when inferior stops at
443 a breakpoint. */
444 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
445
446 return bpdisps[(int) disp];
447 }
448
449 /* Prototypes for exported functions. */
450 /* If FALSE, gdb will not use hardware support for watchpoints, even
451 if such is available. */
452 static int can_use_hw_watchpoints;
453
454 static void
455 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
456 struct cmd_list_element *c,
457 const char *value)
458 {
459 gdb_printf (file,
460 _("Debugger's willingness to use "
461 "watchpoint hardware is %s.\n"),
462 value);
463 }
464
465 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
466 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
467 for unrecognized breakpoint locations.
468 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
469 static enum auto_boolean pending_break_support;
470 static void
471 show_pending_break_support (struct ui_file *file, int from_tty,
472 struct cmd_list_element *c,
473 const char *value)
474 {
475 gdb_printf (file,
476 _("Debugger's behavior regarding "
477 "pending breakpoints is %s.\n"),
478 value);
479 }
480
481 /* If true, gdb will automatically use hardware breakpoints for breakpoints
482 set with "break" but falling in read-only memory.
483 If false, gdb will warn about such breakpoints, but won't automatically
484 use hardware breakpoints. */
485 static bool automatic_hardware_breakpoints;
486 static void
487 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
488 struct cmd_list_element *c,
489 const char *value)
490 {
491 gdb_printf (file,
492 _("Automatic usage of hardware breakpoints is %s.\n"),
493 value);
494 }
495
496 /* If on, GDB keeps breakpoints inserted even if the inferior is
497 stopped, and immediately inserts any new breakpoints as soon as
498 they're created. If off (default), GDB keeps breakpoints off of
499 the target as long as possible. That is, it delays inserting
500 breakpoints until the next resume, and removes them again when the
501 target fully stops. This is a bit safer in case GDB crashes while
502 processing user input. */
503 static bool always_inserted_mode = false;
504
505 static void
506 show_always_inserted_mode (struct ui_file *file, int from_tty,
507 struct cmd_list_element *c, const char *value)
508 {
509 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
510 value);
511 }
512
513 /* See breakpoint.h. */
514
515 int
516 breakpoints_should_be_inserted_now (void)
517 {
518 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
519 {
520 /* If breakpoints are global, they should be inserted even if no
521 thread under gdb's control is running, or even if there are
522 no threads under GDB's control yet. */
523 return 1;
524 }
525 else
526 {
527 if (always_inserted_mode)
528 {
529 /* The user wants breakpoints inserted even if all threads
530 are stopped. */
531 return 1;
532 }
533
534 for (inferior *inf : all_inferiors ())
535 if (inf->has_execution ()
536 && threads_are_executing (inf->process_target ()))
537 return 1;
538
539 /* Don't remove breakpoints yet if, even though all threads are
540 stopped, we still have events to process. */
541 for (thread_info *tp : all_non_exited_threads ())
542 if (tp->resumed () && tp->has_pending_waitstatus ())
543 return 1;
544 }
545 return 0;
546 }
547
548 static const char condition_evaluation_both[] = "host or target";
549
550 /* Modes for breakpoint condition evaluation. */
551 static const char condition_evaluation_auto[] = "auto";
552 static const char condition_evaluation_host[] = "host";
553 static const char condition_evaluation_target[] = "target";
554 static const char *const condition_evaluation_enums[] = {
555 condition_evaluation_auto,
556 condition_evaluation_host,
557 condition_evaluation_target,
558 NULL
559 };
560
561 /* Global that holds the current mode for breakpoint condition evaluation. */
562 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
563
564 /* Global that we use to display information to the user (gets its value from
565 condition_evaluation_mode_1. */
566 static const char *condition_evaluation_mode = condition_evaluation_auto;
567
568 /* Translate a condition evaluation mode MODE into either "host"
569 or "target". This is used mostly to translate from "auto" to the
570 real setting that is being used. It returns the translated
571 evaluation mode. */
572
573 static const char *
574 translate_condition_evaluation_mode (const char *mode)
575 {
576 if (mode == condition_evaluation_auto)
577 {
578 if (target_supports_evaluation_of_breakpoint_conditions ())
579 return condition_evaluation_target;
580 else
581 return condition_evaluation_host;
582 }
583 else
584 return mode;
585 }
586
587 /* Discovers what condition_evaluation_auto translates to. */
588
589 static const char *
590 breakpoint_condition_evaluation_mode (void)
591 {
592 return translate_condition_evaluation_mode (condition_evaluation_mode);
593 }
594
595 /* Return true if GDB should evaluate breakpoint conditions or false
596 otherwise. */
597
598 static bool
599 gdb_evaluates_breakpoint_condition_p (void)
600 {
601 const char *mode = breakpoint_condition_evaluation_mode ();
602
603 return (mode == condition_evaluation_host);
604 }
605
606 /* Are we executing breakpoint commands? */
607 static int executing_breakpoint_commands;
608
609 /* Are overlay event breakpoints enabled? */
610 static int overlay_events_enabled;
611
612 /* See description in breakpoint.h. */
613 bool target_exact_watchpoints = false;
614
615 /* Chains of all breakpoints defined. */
616
617 static struct breakpoint *breakpoint_chain;
618
619 /* See breakpoint.h. */
620
621 breakpoint_range
622 all_breakpoints ()
623 {
624 return breakpoint_range (breakpoint_chain);
625 }
626
627 /* See breakpoint.h. */
628
629 breakpoint_safe_range
630 all_breakpoints_safe ()
631 {
632 return breakpoint_safe_range (all_breakpoints ());
633 }
634
635 /* See breakpoint.h. */
636
637 tracepoint_range
638 all_tracepoints ()
639 {
640 return tracepoint_range (breakpoint_chain);
641 }
642
643 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
644
645 static std::vector<bp_location *> bp_locations;
646
647 /* See breakpoint.h. */
648
649 const std::vector<bp_location *> &
650 all_bp_locations ()
651 {
652 return bp_locations;
653 }
654
655 /* Range to iterate over breakpoint locations at a given address. */
656
657 struct bp_locations_at_addr_range
658 {
659 using iterator = std::vector<bp_location *>::iterator;
660
661 bp_locations_at_addr_range (CORE_ADDR addr)
662 {
663 struct compare
664 {
665 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
666 { return loc->address < addr_; }
667
668 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
669 { return addr_ < loc->address; }
670 };
671
672 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
673 addr, compare ());
674
675 m_begin = it_pair.first;
676 m_end = it_pair.second;
677 }
678
679 iterator begin () const
680 { return m_begin; }
681
682 iterator end () const
683 { return m_end; }
684
685 private:
686 iterator m_begin;
687 iterator m_end;
688 };
689
690 /* Return a range to iterate over all breakpoint locations exactly at address
691 ADDR.
692
693 If it's needed to iterate multiple times on the same range, it's possible
694 to save the range in a local variable and use it multiple times:
695
696 auto range = all_bp_locations_at_addr (addr);
697
698 for (bp_location *loc : range)
699 // use loc
700
701 for (bp_location *loc : range)
702 // use loc
703
704 This saves a bit of time, as it avoids re-doing the binary searches to find
705 the range's boundaries. Just remember not to change the bp_locations vector
706 in the mean time, as it could make the range's iterators stale. */
707
708 static bp_locations_at_addr_range
709 all_bp_locations_at_addr (CORE_ADDR addr)
710 {
711 return bp_locations_at_addr_range (addr);
712 }
713
714 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
715 ADDRESS for the current elements of BP_LOCATIONS which get a valid
716 result from bp_location_has_shadow. You can use it for roughly
717 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
718 an address you need to read. */
719
720 static CORE_ADDR bp_locations_placed_address_before_address_max;
721
722 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
723 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
724 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
725 You can use it for roughly limiting the subrange of BP_LOCATIONS to
726 scan for shadow bytes for an address you need to read. */
727
728 static CORE_ADDR bp_locations_shadow_len_after_address_max;
729
730 /* The locations that no longer correspond to any breakpoint, unlinked
731 from the bp_locations array, but for which a hit may still be
732 reported by a target. */
733 static std::vector<bp_location *> moribund_locations;
734
735 /* Number of last breakpoint made. */
736
737 static int breakpoint_count;
738
739 /* The value of `breakpoint_count' before the last command that
740 created breakpoints. If the last (break-like) command created more
741 than one breakpoint, then the difference between BREAKPOINT_COUNT
742 and PREV_BREAKPOINT_COUNT is more than one. */
743 static int prev_breakpoint_count;
744
745 /* Number of last tracepoint made. */
746
747 static int tracepoint_count;
748
749 static struct cmd_list_element *breakpoint_set_cmdlist;
750 static struct cmd_list_element *breakpoint_show_cmdlist;
751 struct cmd_list_element *save_cmdlist;
752
753 /* Return whether a breakpoint is an active enabled breakpoint. */
754 static bool
755 breakpoint_enabled (struct breakpoint *b)
756 {
757 return (b->enable_state == bp_enabled);
758 }
759
760 /* Set breakpoint count to NUM. */
761
762 static void
763 set_breakpoint_count (int num)
764 {
765 prev_breakpoint_count = breakpoint_count;
766 breakpoint_count = num;
767 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
768 }
769
770 /* Used by `start_rbreak_breakpoints' below, to record the current
771 breakpoint count before "rbreak" creates any breakpoint. */
772 static int rbreak_start_breakpoint_count;
773
774 /* Called at the start an "rbreak" command to record the first
775 breakpoint made. */
776
777 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
778 {
779 rbreak_start_breakpoint_count = breakpoint_count;
780 }
781
782 /* Called at the end of an "rbreak" command to record the last
783 breakpoint made. */
784
785 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
786 {
787 prev_breakpoint_count = rbreak_start_breakpoint_count;
788 }
789
790 /* Used in run_command to zero the hit count when a new run starts. */
791
792 void
793 clear_breakpoint_hit_counts (void)
794 {
795 for (breakpoint *b : all_breakpoints ())
796 b->hit_count = 0;
797 }
798
799 \f
800 /* Return the breakpoint with the specified number, or NULL
801 if the number does not refer to an existing breakpoint. */
802
803 struct breakpoint *
804 get_breakpoint (int num)
805 {
806 for (breakpoint *b : all_breakpoints ())
807 if (b->number == num)
808 return b;
809
810 return nullptr;
811 }
812
813 /* Return TRUE if NUM refer to an existing breakpoint that has
814 multiple code locations. */
815
816 static bool
817 has_multiple_locations (int num)
818 {
819 for (breakpoint *b : all_breakpoints ())
820 if (b->number == num)
821 return b->loc != nullptr && b->loc->next != nullptr;
822
823 return false;
824 }
825
826 \f
827
828 /* Mark locations as "conditions have changed" in case the target supports
829 evaluating conditions on its side. */
830
831 static void
832 mark_breakpoint_modified (struct breakpoint *b)
833 {
834 /* This is only meaningful if the target is
835 evaluating conditions and if the user has
836 opted for condition evaluation on the target's
837 side. */
838 if (gdb_evaluates_breakpoint_condition_p ()
839 || !target_supports_evaluation_of_breakpoint_conditions ())
840 return;
841
842 if (!is_breakpoint (b))
843 return;
844
845 for (bp_location *loc : b->locations ())
846 loc->condition_changed = condition_modified;
847 }
848
849 /* Mark location as "conditions have changed" in case the target supports
850 evaluating conditions on its side. */
851
852 static void
853 mark_breakpoint_location_modified (struct bp_location *loc)
854 {
855 /* This is only meaningful if the target is
856 evaluating conditions and if the user has
857 opted for condition evaluation on the target's
858 side. */
859 if (gdb_evaluates_breakpoint_condition_p ()
860 || !target_supports_evaluation_of_breakpoint_conditions ())
861
862 return;
863
864 if (!is_breakpoint (loc->owner))
865 return;
866
867 loc->condition_changed = condition_modified;
868 }
869
870 /* Sets the condition-evaluation mode using the static global
871 condition_evaluation_mode. */
872
873 static void
874 set_condition_evaluation_mode (const char *args, int from_tty,
875 struct cmd_list_element *c)
876 {
877 const char *old_mode, *new_mode;
878
879 if ((condition_evaluation_mode_1 == condition_evaluation_target)
880 && !target_supports_evaluation_of_breakpoint_conditions ())
881 {
882 condition_evaluation_mode_1 = condition_evaluation_mode;
883 warning (_("Target does not support breakpoint condition evaluation.\n"
884 "Using host evaluation mode instead."));
885 return;
886 }
887
888 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
889 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
890
891 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
892 settings was "auto". */
893 condition_evaluation_mode = condition_evaluation_mode_1;
894
895 /* Only update the mode if the user picked a different one. */
896 if (new_mode != old_mode)
897 {
898 /* If the user switched to a different evaluation mode, we
899 need to synch the changes with the target as follows:
900
901 "host" -> "target": Send all (valid) conditions to the target.
902 "target" -> "host": Remove all the conditions from the target.
903 */
904
905 if (new_mode == condition_evaluation_target)
906 {
907 /* Mark everything modified and synch conditions with the
908 target. */
909 for (bp_location *loc : all_bp_locations ())
910 mark_breakpoint_location_modified (loc);
911 }
912 else
913 {
914 /* Manually mark non-duplicate locations to synch conditions
915 with the target. We do this to remove all the conditions the
916 target knows about. */
917 for (bp_location *loc : all_bp_locations ())
918 if (is_breakpoint (loc->owner) && loc->inserted)
919 loc->needs_update = 1;
920 }
921
922 /* Do the update. */
923 update_global_location_list (UGLL_MAY_INSERT);
924 }
925
926 return;
927 }
928
929 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
930 what "auto" is translating to. */
931
932 static void
933 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
934 struct cmd_list_element *c, const char *value)
935 {
936 if (condition_evaluation_mode == condition_evaluation_auto)
937 gdb_printf (file,
938 _("Breakpoint condition evaluation "
939 "mode is %s (currently %s).\n"),
940 value,
941 breakpoint_condition_evaluation_mode ());
942 else
943 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
944 value);
945 }
946
947 /* Parse COND_STRING in the context of LOC and set as the condition
948 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
949 the number of LOC within its owner. In case of parsing error, mark
950 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
951
952 static void
953 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
954 int bp_num, int loc_num)
955 {
956 bool has_junk = false;
957 try
958 {
959 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
960 block_for_pc (loc->address), 0);
961 if (*cond_string != 0)
962 has_junk = true;
963 else
964 {
965 loc->cond = std::move (new_exp);
966 if (loc->disabled_by_cond && loc->enabled)
967 gdb_printf (_("Breakpoint %d's condition is now valid at "
968 "location %d, enabling.\n"),
969 bp_num, loc_num);
970
971 loc->disabled_by_cond = false;
972 }
973 }
974 catch (const gdb_exception_error &e)
975 {
976 if (loc->enabled)
977 {
978 /* Warn if a user-enabled location is now becoming disabled-by-cond.
979 BP_NUM is 0 if the breakpoint is being defined for the first
980 time using the "break ... if ..." command, and non-zero if
981 already defined. */
982 if (bp_num != 0)
983 warning (_("failed to validate condition at location %d.%d, "
984 "disabling:\n %s"), bp_num, loc_num, e.what ());
985 else
986 warning (_("failed to validate condition at location %d, "
987 "disabling:\n %s"), loc_num, e.what ());
988 }
989
990 loc->disabled_by_cond = true;
991 }
992
993 if (has_junk)
994 error (_("Garbage '%s' follows condition"), cond_string);
995 }
996
997 void
998 set_breakpoint_condition (struct breakpoint *b, const char *exp,
999 int from_tty, bool force)
1000 {
1001 if (*exp == 0)
1002 {
1003 b->cond_string.reset ();
1004
1005 if (is_watchpoint (b))
1006 gdb::checked_static_cast<watchpoint *> (b)->cond_exp.reset ();
1007 else
1008 {
1009 int loc_num = 1;
1010 for (bp_location *loc : b->locations ())
1011 {
1012 loc->cond.reset ();
1013 if (loc->disabled_by_cond && loc->enabled)
1014 gdb_printf (_("Breakpoint %d's condition is now valid at "
1015 "location %d, enabling.\n"),
1016 b->number, loc_num);
1017 loc->disabled_by_cond = false;
1018 loc_num++;
1019
1020 /* No need to free the condition agent expression
1021 bytecode (if we have one). We will handle this
1022 when we go through update_global_location_list. */
1023 }
1024 }
1025
1026 if (from_tty)
1027 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
1028 }
1029 else
1030 {
1031 if (is_watchpoint (b))
1032 {
1033 innermost_block_tracker tracker;
1034 const char *arg = exp;
1035 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
1036 if (*arg != 0)
1037 error (_("Junk at end of expression"));
1038 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
1039 w->cond_exp = std::move (new_exp);
1040 w->cond_exp_valid_block = tracker.block ();
1041 }
1042 else
1043 {
1044 /* Parse and set condition expressions. We make two passes.
1045 In the first, we parse the condition string to see if it
1046 is valid in at least one location. If so, the condition
1047 would be accepted. So we go ahead and set the locations'
1048 conditions. In case no valid case is found, we throw
1049 the error and the condition string will be rejected.
1050 This two-pass approach is taken to avoid setting the
1051 state of locations in case of a reject. */
1052 for (bp_location *loc : b->locations ())
1053 {
1054 try
1055 {
1056 const char *arg = exp;
1057 parse_exp_1 (&arg, loc->address,
1058 block_for_pc (loc->address), 0);
1059 if (*arg != 0)
1060 error (_("Junk at end of expression"));
1061 break;
1062 }
1063 catch (const gdb_exception_error &e)
1064 {
1065 /* Condition string is invalid. If this happens to
1066 be the last loc, abandon (if not forced) or continue
1067 (if forced). */
1068 if (loc->next == nullptr && !force)
1069 throw;
1070 }
1071 }
1072
1073 /* If we reach here, the condition is valid at some locations. */
1074 int loc_num = 1;
1075 for (bp_location *loc : b->locations ())
1076 {
1077 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
1078 loc_num++;
1079 }
1080 }
1081
1082 /* We know that the new condition parsed successfully. The
1083 condition string of the breakpoint can be safely updated. */
1084 b->cond_string = make_unique_xstrdup (exp);
1085 b->condition_not_parsed = 0;
1086 }
1087 mark_breakpoint_modified (b);
1088
1089 gdb::observers::breakpoint_modified.notify (b);
1090 }
1091
1092 /* See breakpoint.h. */
1093
1094 void
1095 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1096 bool force)
1097 {
1098 for (breakpoint *b : all_breakpoints ())
1099 if (b->number == bpnum)
1100 {
1101 /* Check if this breakpoint has a "stop" method implemented in an
1102 extension language. This method and conditions entered into GDB
1103 from the CLI are mutually exclusive. */
1104 const struct extension_language_defn *extlang
1105 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1106
1107 if (extlang != NULL)
1108 {
1109 error (_("Only one stop condition allowed. There is currently"
1110 " a %s stop condition defined for this breakpoint."),
1111 ext_lang_capitalized_name (extlang));
1112 }
1113 set_breakpoint_condition (b, exp, from_tty, force);
1114
1115 if (is_breakpoint (b))
1116 update_global_location_list (UGLL_MAY_INSERT);
1117
1118 return;
1119 }
1120
1121 error (_("No breakpoint number %d."), bpnum);
1122 }
1123
1124 /* The options for the "condition" command. */
1125
1126 struct condition_command_opts
1127 {
1128 /* For "-force". */
1129 bool force_condition = false;
1130 };
1131
1132 static const gdb::option::option_def condition_command_option_defs[] = {
1133
1134 gdb::option::flag_option_def<condition_command_opts> {
1135 "force",
1136 [] (condition_command_opts *opts) { return &opts->force_condition; },
1137 N_("Set the condition even if it is invalid for all current locations."),
1138 },
1139
1140 };
1141
1142 /* Create an option_def_group for the "condition" options, with
1143 CC_OPTS as context. */
1144
1145 static inline gdb::option::option_def_group
1146 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1147 {
1148 return {{condition_command_option_defs}, cc_opts};
1149 }
1150
1151 /* Completion for the "condition" command. */
1152
1153 static void
1154 condition_completer (struct cmd_list_element *cmd,
1155 completion_tracker &tracker,
1156 const char *text, const char * /*word*/)
1157 {
1158 bool has_no_arguments = (*text == '\0');
1159 condition_command_opts cc_opts;
1160 const auto group = make_condition_command_options_def_group (&cc_opts);
1161 if (gdb::option::complete_options
1162 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1163 return;
1164
1165 text = skip_spaces (text);
1166 const char *space = skip_to_space (text);
1167 if (*space == '\0')
1168 {
1169 int len;
1170
1171 if (text[0] == '$')
1172 {
1173 tracker.advance_custom_word_point_by (1);
1174 /* We don't support completion of history indices. */
1175 if (!isdigit (text[1]))
1176 complete_internalvar (tracker, &text[1]);
1177 return;
1178 }
1179
1180 /* Suggest the "-force" flag if no arguments are given. If
1181 arguments were passed, they either already include the flag,
1182 or we are beyond the point of suggesting it because it's
1183 positionally the first argument. */
1184 if (has_no_arguments)
1185 gdb::option::complete_on_all_options (tracker, group);
1186
1187 /* We're completing the breakpoint number. */
1188 len = strlen (text);
1189
1190 for (breakpoint *b : all_breakpoints ())
1191 {
1192 char number[50];
1193
1194 xsnprintf (number, sizeof (number), "%d", b->number);
1195
1196 if (strncmp (number, text, len) == 0)
1197 tracker.add_completion (make_unique_xstrdup (number));
1198 }
1199
1200 return;
1201 }
1202
1203 /* We're completing the expression part. Skip the breakpoint num. */
1204 const char *exp_start = skip_spaces (space);
1205 tracker.advance_custom_word_point_by (exp_start - text);
1206 text = exp_start;
1207 const char *word = advance_to_expression_complete_word_point (tracker, text);
1208 expression_completer (cmd, tracker, text, word);
1209 }
1210
1211 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1212
1213 static void
1214 condition_command (const char *arg, int from_tty)
1215 {
1216 const char *p;
1217 int bnum;
1218
1219 if (arg == 0)
1220 error_no_arg (_("breakpoint number"));
1221
1222 p = arg;
1223
1224 /* Check if the "-force" flag was passed. */
1225 condition_command_opts cc_opts;
1226 const auto group = make_condition_command_options_def_group (&cc_opts);
1227 gdb::option::process_options
1228 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1229
1230 bnum = get_number (&p);
1231 if (bnum == 0)
1232 error (_("Bad breakpoint argument: '%s'"), arg);
1233
1234 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1235 }
1236
1237 /* Check that COMMAND do not contain commands that are suitable
1238 only for tracepoints and not suitable for ordinary breakpoints.
1239 Throw if any such commands is found. */
1240
1241 static void
1242 check_no_tracepoint_commands (struct command_line *commands)
1243 {
1244 struct command_line *c;
1245
1246 for (c = commands; c; c = c->next)
1247 {
1248 if (c->control_type == while_stepping_control)
1249 error (_("The 'while-stepping' command can "
1250 "only be used for tracepoints"));
1251
1252 check_no_tracepoint_commands (c->body_list_0.get ());
1253 check_no_tracepoint_commands (c->body_list_1.get ());
1254
1255 /* Not that command parsing removes leading whitespace and comment
1256 lines and also empty lines. So, we only need to check for
1257 command directly. */
1258 if (strstr (c->line, "collect ") == c->line)
1259 error (_("The 'collect' command can only be used for tracepoints"));
1260
1261 if (strstr (c->line, "teval ") == c->line)
1262 error (_("The 'teval' command can only be used for tracepoints"));
1263 }
1264 }
1265
1266 struct longjmp_breakpoint : public momentary_breakpoint
1267 {
1268 using momentary_breakpoint::momentary_breakpoint;
1269
1270 ~longjmp_breakpoint () override;
1271 };
1272
1273 /* Encapsulate tests for different types of tracepoints. */
1274
1275 static bool
1276 is_tracepoint_type (bptype type)
1277 {
1278 return (type == bp_tracepoint
1279 || type == bp_fast_tracepoint
1280 || type == bp_static_tracepoint
1281 || type == bp_static_marker_tracepoint);
1282 }
1283
1284 /* See breakpoint.h. */
1285
1286 bool
1287 is_tracepoint (const struct breakpoint *b)
1288 {
1289 return is_tracepoint_type (b->type);
1290 }
1291
1292 /* Factory function to create an appropriate instance of breakpoint given
1293 TYPE. */
1294
1295 template<typename... Arg>
1296 static std::unique_ptr<code_breakpoint>
1297 new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type,
1298 Arg&&... args)
1299 {
1300 code_breakpoint *b;
1301
1302 switch (type)
1303 {
1304 case bp_breakpoint:
1305 case bp_hardware_breakpoint:
1306 b = new ordinary_breakpoint (gdbarch, type,
1307 std::forward<Arg> (args)...);
1308 break;
1309
1310 case bp_fast_tracepoint:
1311 case bp_static_tracepoint:
1312 case bp_tracepoint:
1313 b = new tracepoint (gdbarch, type,
1314 std::forward<Arg> (args)...);
1315 break;
1316
1317 case bp_static_marker_tracepoint:
1318 b = new static_marker_tracepoint (gdbarch, type,
1319 std::forward<Arg> (args)...);
1320 break;
1321
1322 case bp_dprintf:
1323 b = new dprintf_breakpoint (gdbarch, type,
1324 std::forward<Arg> (args)...);
1325 break;
1326
1327 default:
1328 gdb_assert_not_reached ("invalid type");
1329 }
1330
1331 return std::unique_ptr<code_breakpoint> (b);
1332 }
1333
1334 /* A helper function that validates that COMMANDS are valid for a
1335 breakpoint. This function will throw an exception if a problem is
1336 found. */
1337
1338 static void
1339 validate_commands_for_breakpoint (struct breakpoint *b,
1340 struct command_line *commands)
1341 {
1342 if (is_tracepoint (b))
1343 {
1344 struct tracepoint *t = (struct tracepoint *) b;
1345 struct command_line *c;
1346 struct command_line *while_stepping = 0;
1347
1348 /* Reset the while-stepping step count. The previous commands
1349 might have included a while-stepping action, while the new
1350 ones might not. */
1351 t->step_count = 0;
1352
1353 /* We need to verify that each top-level element of commands is
1354 valid for tracepoints, that there's at most one
1355 while-stepping element, and that the while-stepping's body
1356 has valid tracing commands excluding nested while-stepping.
1357 We also need to validate the tracepoint action line in the
1358 context of the tracepoint --- validate_actionline actually
1359 has side effects, like setting the tracepoint's
1360 while-stepping STEP_COUNT, in addition to checking if the
1361 collect/teval actions parse and make sense in the
1362 tracepoint's context. */
1363 for (c = commands; c; c = c->next)
1364 {
1365 if (c->control_type == while_stepping_control)
1366 {
1367 if (b->type == bp_fast_tracepoint)
1368 error (_("The 'while-stepping' command "
1369 "cannot be used for fast tracepoint"));
1370 else if (b->type == bp_static_tracepoint
1371 || b->type == bp_static_marker_tracepoint)
1372 error (_("The 'while-stepping' command "
1373 "cannot be used for static tracepoint"));
1374
1375 if (while_stepping)
1376 error (_("The 'while-stepping' command "
1377 "can be used only once"));
1378 else
1379 while_stepping = c;
1380 }
1381
1382 validate_actionline (c->line, b);
1383 }
1384 if (while_stepping)
1385 {
1386 struct command_line *c2;
1387
1388 gdb_assert (while_stepping->body_list_1 == nullptr);
1389 c2 = while_stepping->body_list_0.get ();
1390 for (; c2; c2 = c2->next)
1391 {
1392 if (c2->control_type == while_stepping_control)
1393 error (_("The 'while-stepping' command cannot be nested"));
1394 }
1395 }
1396 }
1397 else
1398 {
1399 check_no_tracepoint_commands (commands);
1400 }
1401 }
1402
1403 /* Return a vector of all the static tracepoints set at ADDR. The
1404 caller is responsible for releasing the vector. */
1405
1406 std::vector<breakpoint *>
1407 static_tracepoints_here (CORE_ADDR addr)
1408 {
1409 std::vector<breakpoint *> found;
1410
1411 for (breakpoint *b : all_breakpoints ())
1412 if (b->type == bp_static_tracepoint
1413 || b->type == bp_static_marker_tracepoint)
1414 {
1415 for (bp_location *loc : b->locations ())
1416 if (loc->address == addr)
1417 found.push_back (b);
1418 }
1419
1420 return found;
1421 }
1422
1423 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1424 validate that only allowed commands are included. */
1425
1426 void
1427 breakpoint_set_commands (struct breakpoint *b,
1428 counted_command_line &&commands)
1429 {
1430 validate_commands_for_breakpoint (b, commands.get ());
1431
1432 b->commands = std::move (commands);
1433 gdb::observers::breakpoint_modified.notify (b);
1434 }
1435
1436 /* Set the internal `silent' flag on the breakpoint. Note that this
1437 is not the same as the "silent" that may appear in the breakpoint's
1438 commands. */
1439
1440 void
1441 breakpoint_set_silent (struct breakpoint *b, int silent)
1442 {
1443 int old_silent = b->silent;
1444
1445 b->silent = silent;
1446 if (old_silent != silent)
1447 gdb::observers::breakpoint_modified.notify (b);
1448 }
1449
1450 /* See breakpoint.h. */
1451
1452 void
1453 breakpoint_set_thread (struct breakpoint *b, int thread)
1454 {
1455 /* It is invalid to set the thread field to anything other than -1 (which
1456 means no thread restriction) if a task restriction is already in
1457 place. */
1458 gdb_assert (thread == -1 || b->task == -1);
1459
1460 int old_thread = b->thread;
1461
1462 b->thread = thread;
1463 if (old_thread != thread)
1464 gdb::observers::breakpoint_modified.notify (b);
1465 }
1466
1467 /* See breakpoint.h. */
1468
1469 void
1470 breakpoint_set_task (struct breakpoint *b, int task)
1471 {
1472 /* It is invalid to set the task field to anything other than -1 (which
1473 means no task restriction) if a thread restriction is already in
1474 place. */
1475 gdb_assert (task == -1 || b->thread == -1);
1476
1477 int old_task = b->task;
1478
1479 b->task = task;
1480 if (old_task != task)
1481 gdb::observers::breakpoint_modified.notify (b);
1482 }
1483
1484 static void
1485 commands_command_1 (const char *arg, int from_tty,
1486 struct command_line *control)
1487 {
1488 counted_command_line cmd;
1489 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1490 NULL after the call to read_command_lines if the user provides an empty
1491 list of command by just typing "end". */
1492 bool cmd_read = false;
1493
1494 std::string new_arg;
1495
1496 if (arg == NULL || !*arg)
1497 {
1498 /* Argument not explicitly given. Synthesize it. */
1499 if (breakpoint_count - prev_breakpoint_count > 1)
1500 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1501 breakpoint_count);
1502 else if (breakpoint_count > 0)
1503 new_arg = string_printf ("%d", breakpoint_count);
1504 }
1505 else
1506 {
1507 /* Create a copy of ARG. This is needed because the "commands"
1508 command may be coming from a script. In that case, the read
1509 line buffer is going to be overwritten in the lambda of
1510 'map_breakpoint_numbers' below when reading the next line
1511 before we are are done parsing the breakpoint numbers. */
1512 new_arg = arg;
1513 }
1514 arg = new_arg.c_str ();
1515
1516 map_breakpoint_numbers
1517 (arg, [&] (breakpoint *b)
1518 {
1519 if (!cmd_read)
1520 {
1521 gdb_assert (cmd == NULL);
1522 if (control != NULL)
1523 cmd = control->body_list_0;
1524 else
1525 {
1526 std::string str
1527 = string_printf (_("Type commands for breakpoint(s) "
1528 "%s, one per line."),
1529 arg);
1530
1531 auto do_validate = [=] (const char *line)
1532 {
1533 validate_actionline (line, b);
1534 };
1535 gdb::function_view<void (const char *)> validator;
1536 if (is_tracepoint (b))
1537 validator = do_validate;
1538
1539 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1540 }
1541 cmd_read = true;
1542 }
1543
1544 /* If a breakpoint was on the list more than once, we don't need to
1545 do anything. */
1546 if (b->commands != cmd)
1547 {
1548 validate_commands_for_breakpoint (b, cmd.get ());
1549 b->commands = cmd;
1550 gdb::observers::breakpoint_modified.notify (b);
1551 }
1552 });
1553 }
1554
1555 static void
1556 commands_command (const char *arg, int from_tty)
1557 {
1558 commands_command_1 (arg, from_tty, NULL);
1559 }
1560
1561 /* Like commands_command, but instead of reading the commands from
1562 input stream, takes them from an already parsed command structure.
1563
1564 This is used by cli-script.c to DTRT with breakpoint commands
1565 that are part of if and while bodies. */
1566 enum command_control_type
1567 commands_from_control_command (const char *arg, struct command_line *cmd)
1568 {
1569 commands_command_1 (arg, 0, cmd);
1570 return simple_control;
1571 }
1572
1573 /* Return true if BL->TARGET_INFO contains valid information. */
1574
1575 static bool
1576 bp_location_has_shadow (struct bp_location *bl)
1577 {
1578 if (bl->loc_type != bp_loc_software_breakpoint)
1579 return false;
1580 if (!bl->inserted)
1581 return false;
1582 if (bl->target_info.shadow_len == 0)
1583 /* BL isn't valid, or doesn't shadow memory. */
1584 return false;
1585 return true;
1586 }
1587
1588 /* Update BUF, which is LEN bytes read from the target address
1589 MEMADDR, by replacing a memory breakpoint with its shadowed
1590 contents.
1591
1592 If READBUF is not NULL, this buffer must not overlap with the of
1593 the breakpoint location's shadow_contents buffer. Otherwise, a
1594 failed assertion internal error will be raised. */
1595
1596 static void
1597 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1598 const gdb_byte *writebuf_org,
1599 ULONGEST memaddr, LONGEST len,
1600 struct bp_target_info *target_info,
1601 struct gdbarch *gdbarch)
1602 {
1603 /* Now do full processing of the found relevant range of elements. */
1604 CORE_ADDR bp_addr = 0;
1605 int bp_size = 0;
1606 int bptoffset = 0;
1607
1608 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1609 current_program_space->aspace, 0))
1610 {
1611 /* The breakpoint is inserted in a different address space. */
1612 return;
1613 }
1614
1615 /* Addresses and length of the part of the breakpoint that
1616 we need to copy. */
1617 bp_addr = target_info->placed_address;
1618 bp_size = target_info->shadow_len;
1619
1620 if (bp_addr + bp_size <= memaddr)
1621 {
1622 /* The breakpoint is entirely before the chunk of memory we are
1623 reading. */
1624 return;
1625 }
1626
1627 if (bp_addr >= memaddr + len)
1628 {
1629 /* The breakpoint is entirely after the chunk of memory we are
1630 reading. */
1631 return;
1632 }
1633
1634 /* Offset within shadow_contents. */
1635 if (bp_addr < memaddr)
1636 {
1637 /* Only copy the second part of the breakpoint. */
1638 bp_size -= memaddr - bp_addr;
1639 bptoffset = memaddr - bp_addr;
1640 bp_addr = memaddr;
1641 }
1642
1643 if (bp_addr + bp_size > memaddr + len)
1644 {
1645 /* Only copy the first part of the breakpoint. */
1646 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1647 }
1648
1649 if (readbuf != NULL)
1650 {
1651 /* Verify that the readbuf buffer does not overlap with the
1652 shadow_contents buffer. */
1653 gdb_assert (target_info->shadow_contents >= readbuf + len
1654 || readbuf >= (target_info->shadow_contents
1655 + target_info->shadow_len));
1656
1657 /* Update the read buffer with this inserted breakpoint's
1658 shadow. */
1659 memcpy (readbuf + bp_addr - memaddr,
1660 target_info->shadow_contents + bptoffset, bp_size);
1661 }
1662 else
1663 {
1664 const unsigned char *bp;
1665 CORE_ADDR addr = target_info->reqstd_address;
1666 int placed_size;
1667
1668 /* Update the shadow with what we want to write to memory. */
1669 memcpy (target_info->shadow_contents + bptoffset,
1670 writebuf_org + bp_addr - memaddr, bp_size);
1671
1672 /* Determine appropriate breakpoint contents and size for this
1673 address. */
1674 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1675
1676 /* Update the final write buffer with this inserted
1677 breakpoint's INSN. */
1678 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1679 }
1680 }
1681
1682 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1683 by replacing any memory breakpoints with their shadowed contents.
1684
1685 If READBUF is not NULL, this buffer must not overlap with any of
1686 the breakpoint location's shadow_contents buffers. Otherwise,
1687 a failed assertion internal error will be raised.
1688
1689 The range of shadowed area by each bp_location is:
1690 bl->address - bp_locations_placed_address_before_address_max
1691 up to bl->address + bp_locations_shadow_len_after_address_max
1692 The range we were requested to resolve shadows for is:
1693 memaddr ... memaddr + len
1694 Thus the safe cutoff boundaries for performance optimization are
1695 memaddr + len <= (bl->address
1696 - bp_locations_placed_address_before_address_max)
1697 and:
1698 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1699
1700 void
1701 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1702 const gdb_byte *writebuf_org,
1703 ULONGEST memaddr, LONGEST len)
1704 {
1705 /* Left boundary, right boundary and median element of our binary
1706 search. */
1707 unsigned bc_l, bc_r, bc;
1708
1709 /* Find BC_L which is a leftmost element which may affect BUF
1710 content. It is safe to report lower value but a failure to
1711 report higher one. */
1712
1713 bc_l = 0;
1714 bc_r = bp_locations.size ();
1715 while (bc_l + 1 < bc_r)
1716 {
1717 struct bp_location *bl;
1718
1719 bc = (bc_l + bc_r) / 2;
1720 bl = bp_locations[bc];
1721
1722 /* Check first BL->ADDRESS will not overflow due to the added
1723 constant. Then advance the left boundary only if we are sure
1724 the BC element can in no way affect the BUF content (MEMADDR
1725 to MEMADDR + LEN range).
1726
1727 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1728 offset so that we cannot miss a breakpoint with its shadow
1729 range tail still reaching MEMADDR. */
1730
1731 if ((bl->address + bp_locations_shadow_len_after_address_max
1732 >= bl->address)
1733 && (bl->address + bp_locations_shadow_len_after_address_max
1734 <= memaddr))
1735 bc_l = bc;
1736 else
1737 bc_r = bc;
1738 }
1739
1740 /* Due to the binary search above, we need to make sure we pick the
1741 first location that's at BC_L's address. E.g., if there are
1742 multiple locations at the same address, BC_L may end up pointing
1743 at a duplicate location, and miss the "master"/"inserted"
1744 location. Say, given locations L1, L2 and L3 at addresses A and
1745 B:
1746
1747 L1@A, L2@A, L3@B, ...
1748
1749 BC_L could end up pointing at location L2, while the "master"
1750 location could be L1. Since the `loc->inserted' flag is only set
1751 on "master" locations, we'd forget to restore the shadow of L1
1752 and L2. */
1753 while (bc_l > 0
1754 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1755 bc_l--;
1756
1757 /* Now do full processing of the found relevant range of elements. */
1758
1759 for (bc = bc_l; bc < bp_locations.size (); bc++)
1760 {
1761 struct bp_location *bl = bp_locations[bc];
1762
1763 /* bp_location array has BL->OWNER always non-NULL. */
1764 if (bl->owner->type == bp_none)
1765 warning (_("reading through apparently deleted breakpoint #%d?"),
1766 bl->owner->number);
1767
1768 /* Performance optimization: any further element can no longer affect BUF
1769 content. */
1770
1771 if (bl->address >= bp_locations_placed_address_before_address_max
1772 && (memaddr + len
1773 <= (bl->address
1774 - bp_locations_placed_address_before_address_max)))
1775 break;
1776
1777 if (!bp_location_has_shadow (bl))
1778 continue;
1779
1780 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1781 memaddr, len, &bl->target_info, bl->gdbarch);
1782 }
1783 }
1784
1785 /* See breakpoint.h. */
1786
1787 bool
1788 is_breakpoint (const struct breakpoint *bpt)
1789 {
1790 return (bpt->type == bp_breakpoint
1791 || bpt->type == bp_hardware_breakpoint
1792 || bpt->type == bp_dprintf);
1793 }
1794
1795 /* Return true if BPT is of any hardware watchpoint kind. */
1796
1797 static bool
1798 is_hardware_watchpoint (const struct breakpoint *bpt)
1799 {
1800 return (bpt->type == bp_hardware_watchpoint
1801 || bpt->type == bp_read_watchpoint
1802 || bpt->type == bp_access_watchpoint);
1803 }
1804
1805 /* See breakpoint.h. */
1806
1807 bool
1808 is_watchpoint (const struct breakpoint *bpt)
1809 {
1810 return (is_hardware_watchpoint (bpt)
1811 || bpt->type == bp_watchpoint);
1812 }
1813
1814 /* Returns true if the current thread and its running state are safe
1815 to evaluate or update watchpoint B. Watchpoints on local
1816 expressions need to be evaluated in the context of the thread that
1817 was current when the watchpoint was created, and, that thread needs
1818 to be stopped to be able to select the correct frame context.
1819 Watchpoints on global expressions can be evaluated on any thread,
1820 and in any state. It is presently left to the target allowing
1821 memory accesses when threads are running. */
1822
1823 static bool
1824 watchpoint_in_thread_scope (struct watchpoint *b)
1825 {
1826 return (b->pspace == current_program_space
1827 && (b->watchpoint_thread == null_ptid
1828 || (inferior_ptid == b->watchpoint_thread
1829 && !inferior_thread ()->executing ())));
1830 }
1831
1832 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1833 associated bp_watchpoint_scope breakpoint. */
1834
1835 static void
1836 watchpoint_del_at_next_stop (struct watchpoint *w)
1837 {
1838 if (w->related_breakpoint != w)
1839 {
1840 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1841 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1842 w->related_breakpoint->disposition = disp_del_at_next_stop;
1843 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1844 w->related_breakpoint = w;
1845 }
1846 w->disposition = disp_del_at_next_stop;
1847 disable_breakpoint (w);
1848 }
1849
1850 /* Extract a bitfield value from value VAL using the bit parameters contained in
1851 watchpoint W. */
1852
1853 static struct value *
1854 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1855 {
1856 struct value *bit_val;
1857
1858 if (val == NULL)
1859 return NULL;
1860
1861 bit_val = value::allocate (val->type ());
1862
1863 val->unpack_bitfield (bit_val,
1864 w->val_bitpos,
1865 w->val_bitsize,
1866 val->contents_for_printing ().data (),
1867 val->offset ());
1868
1869 return bit_val;
1870 }
1871
1872 /* Allocate a dummy location and add it to B. This is required
1873 because bpstat_stop_status requires a location to be able to report
1874 stops. */
1875
1876 static void
1877 add_dummy_location (struct breakpoint *b,
1878 struct program_space *pspace)
1879 {
1880 gdb_assert (b->loc == NULL);
1881
1882 b->loc = new bp_location (b, bp_loc_other);
1883 b->loc->pspace = pspace;
1884 }
1885
1886 /* Assuming that B is a watchpoint:
1887 - Reparse watchpoint expression, if REPARSE is true
1888 - Evaluate expression and store the result in B->val
1889 - Evaluate the condition if there is one, and store the result
1890 in b->loc->cond.
1891 - Update the list of values that must be watched in B->loc.
1892
1893 If the watchpoint disposition is disp_del_at_next_stop, then do
1894 nothing. If this is local watchpoint that is out of scope, delete
1895 it.
1896
1897 Even with `set breakpoint always-inserted on' the watchpoints are
1898 removed + inserted on each stop here. Normal breakpoints must
1899 never be removed because they might be missed by a running thread
1900 when debugging in non-stop mode. On the other hand, hardware
1901 watchpoints (is_hardware_watchpoint; processed here) are specific
1902 to each LWP since they are stored in each LWP's hardware debug
1903 registers. Therefore, such LWP must be stopped first in order to
1904 be able to modify its hardware watchpoints.
1905
1906 Hardware watchpoints must be reset exactly once after being
1907 presented to the user. It cannot be done sooner, because it would
1908 reset the data used to present the watchpoint hit to the user. And
1909 it must not be done later because it could display the same single
1910 watchpoint hit during multiple GDB stops. Note that the latter is
1911 relevant only to the hardware watchpoint types bp_read_watchpoint
1912 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1913 not user-visible - its hit is suppressed if the memory content has
1914 not changed.
1915
1916 The following constraints influence the location where we can reset
1917 hardware watchpoints:
1918
1919 * target_stopped_by_watchpoint and target_stopped_data_address are
1920 called several times when GDB stops.
1921
1922 [linux]
1923 * Multiple hardware watchpoints can be hit at the same time,
1924 causing GDB to stop. GDB only presents one hardware watchpoint
1925 hit at a time as the reason for stopping, and all the other hits
1926 are presented later, one after the other, each time the user
1927 requests the execution to be resumed. Execution is not resumed
1928 for the threads still having pending hit event stored in
1929 LWP_INFO->STATUS. While the watchpoint is already removed from
1930 the inferior on the first stop the thread hit event is kept being
1931 reported from its cached value by linux_nat_stopped_data_address
1932 until the real thread resume happens after the watchpoint gets
1933 presented and thus its LWP_INFO->STATUS gets reset.
1934
1935 Therefore the hardware watchpoint hit can get safely reset on the
1936 watchpoint removal from inferior. */
1937
1938 static void
1939 update_watchpoint (struct watchpoint *b, bool reparse)
1940 {
1941 bool within_current_scope;
1942
1943 /* If this is a local watchpoint, we only want to check if the
1944 watchpoint frame is in scope if the current thread is the thread
1945 that was used to create the watchpoint. */
1946 if (!watchpoint_in_thread_scope (b))
1947 return;
1948
1949 if (b->disposition == disp_del_at_next_stop)
1950 return;
1951
1952 gdb::optional<scoped_restore_selected_frame> restore_frame;
1953
1954 /* Determine if the watchpoint is within scope. */
1955 if (b->exp_valid_block == NULL)
1956 within_current_scope = true;
1957 else
1958 {
1959 frame_info_ptr fi = get_current_frame ();
1960 struct gdbarch *frame_arch = get_frame_arch (fi);
1961 CORE_ADDR frame_pc = get_frame_pc (fi);
1962
1963 /* If we're at a point where the stack has been destroyed
1964 (e.g. in a function epilogue), unwinding may not work
1965 properly. Do not attempt to recreate locations at this
1966 point. See similar comments in watchpoint_check. */
1967 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1968 return;
1969
1970 /* Save the current frame's ID so we can restore it after
1971 evaluating the watchpoint expression on its own frame. */
1972 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1973 took a frame parameter, so that we didn't have to change the
1974 selected frame. */
1975 restore_frame.emplace ();
1976
1977 fi = frame_find_by_id (b->watchpoint_frame);
1978 within_current_scope = (fi != NULL);
1979 if (within_current_scope)
1980 select_frame (fi);
1981 }
1982
1983 /* We don't free locations. They are stored in the bp_location array
1984 and update_global_location_list will eventually delete them and
1985 remove breakpoints if needed. */
1986 b->loc = NULL;
1987
1988 if (within_current_scope && reparse)
1989 {
1990 const char *s;
1991
1992 b->exp.reset ();
1993 s = (b->exp_string_reparse
1994 ? b->exp_string_reparse.get ()
1995 : b->exp_string.get ());
1996 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1997 /* If the meaning of expression itself changed, the old value is
1998 no longer relevant. We don't want to report a watchpoint hit
1999 to the user when the old value and the new value may actually
2000 be completely different objects. */
2001 b->val = NULL;
2002 b->val_valid = false;
2003
2004 /* Note that unlike with breakpoints, the watchpoint's condition
2005 expression is stored in the breakpoint object, not in the
2006 locations (re)created below. */
2007 if (b->cond_string != NULL)
2008 {
2009 b->cond_exp.reset ();
2010
2011 s = b->cond_string.get ();
2012 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
2013 }
2014 }
2015
2016 /* If we failed to parse the expression, for example because
2017 it refers to a global variable in a not-yet-loaded shared library,
2018 don't try to insert watchpoint. We don't automatically delete
2019 such watchpoint, though, since failure to parse expression
2020 is different from out-of-scope watchpoint. */
2021 if (!target_has_execution ())
2022 {
2023 /* Without execution, memory can't change. No use to try and
2024 set watchpoint locations. The watchpoint will be reset when
2025 the target gains execution, through breakpoint_re_set. */
2026 if (!can_use_hw_watchpoints)
2027 {
2028 if (b->works_in_software_mode ())
2029 b->type = bp_watchpoint;
2030 else
2031 error (_("Can't set read/access watchpoint when "
2032 "hardware watchpoints are disabled."));
2033 }
2034 }
2035 else if (within_current_scope && b->exp)
2036 {
2037 std::vector<value_ref_ptr> val_chain;
2038 struct value *v, *result;
2039 struct program_space *frame_pspace;
2040
2041 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
2042 &val_chain, false);
2043
2044 /* Avoid setting b->val if it's already set. The meaning of
2045 b->val is 'the last value' user saw, and we should update
2046 it only if we reported that last value to user. As it
2047 happens, the code that reports it updates b->val directly.
2048 We don't keep track of the memory value for masked
2049 watchpoints. */
2050 if (!b->val_valid && !is_masked_watchpoint (b))
2051 {
2052 if (b->val_bitsize != 0)
2053 v = extract_bitfield_from_watchpoint_value (b, v);
2054 b->val = release_value (v);
2055 b->val_valid = true;
2056 }
2057
2058 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2059
2060 /* Look at each value on the value chain. */
2061 gdb_assert (!val_chain.empty ());
2062 for (const value_ref_ptr &iter : val_chain)
2063 {
2064 v = iter.get ();
2065
2066 /* If it's a memory location, and GDB actually needed
2067 its contents to evaluate the expression, then we
2068 must watch it. If the first value returned is
2069 still lazy, that means an error occurred reading it;
2070 watch it anyway in case it becomes readable. */
2071 if (v->lval () == lval_memory
2072 && (v == val_chain[0] || ! v->lazy ()))
2073 {
2074 struct type *vtype = check_typedef (v->type ());
2075
2076 /* We only watch structs and arrays if user asked
2077 for it explicitly, never if they just happen to
2078 appear in the middle of some value chain. */
2079 if (v == result
2080 || (vtype->code () != TYPE_CODE_STRUCT
2081 && vtype->code () != TYPE_CODE_ARRAY))
2082 {
2083 CORE_ADDR addr;
2084 enum target_hw_bp_type type;
2085 struct bp_location *loc, **tmp;
2086 int bitpos = 0, bitsize = 0;
2087
2088 if (v->bitsize () != 0)
2089 {
2090 /* Extract the bit parameters out from the bitfield
2091 sub-expression. */
2092 bitpos = v->bitpos ();
2093 bitsize = v->bitsize ();
2094 }
2095 else if (v == result && b->val_bitsize != 0)
2096 {
2097 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2098 lvalue whose bit parameters are saved in the fields
2099 VAL_BITPOS and VAL_BITSIZE. */
2100 bitpos = b->val_bitpos;
2101 bitsize = b->val_bitsize;
2102 }
2103
2104 addr = v->address ();
2105 if (bitsize != 0)
2106 {
2107 /* Skip the bytes that don't contain the bitfield. */
2108 addr += bitpos / 8;
2109 }
2110
2111 type = hw_write;
2112 if (b->type == bp_read_watchpoint)
2113 type = hw_read;
2114 else if (b->type == bp_access_watchpoint)
2115 type = hw_access;
2116
2117 loc = b->allocate_location ();
2118 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2119 ;
2120 *tmp = loc;
2121 loc->gdbarch = v->type ()->arch ();
2122
2123 loc->pspace = frame_pspace;
2124 loc->address
2125 = gdbarch_remove_non_address_bits (loc->gdbarch, addr);
2126
2127 if (bitsize != 0)
2128 {
2129 /* Just cover the bytes that make up the bitfield. */
2130 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2131 }
2132 else
2133 loc->length = v->type ()->length ();
2134
2135 loc->watchpoint_type = type;
2136 }
2137 }
2138 }
2139
2140 /* Helper function to bundle possibly emitting a warning along with
2141 changing the type of B to bp_watchpoint. */
2142 auto change_type_to_bp_watchpoint = [] (breakpoint *bp)
2143 {
2144 /* Only warn for breakpoints that have been assigned a +ve number,
2145 anything else is either an internal watchpoint (which we don't
2146 currently create) or has not yet been finalized, in which case
2147 this change of type will be occurring before the user is told
2148 the type of this watchpoint. */
2149 if (bp->type == bp_hardware_watchpoint && bp->number > 0)
2150 warning (_("watchpoint %d downgraded to software watchpoint"),
2151 bp->number);
2152 bp->type = bp_watchpoint;
2153 };
2154
2155 /* Change the type of breakpoint between hardware assisted or
2156 an ordinary watchpoint depending on the hardware support and
2157 free hardware slots. Recheck the number of free hardware slots
2158 as the value chain may have changed. */
2159 {
2160 int reg_cnt;
2161 enum bp_loc_type loc_type;
2162
2163 reg_cnt = can_use_hardware_watchpoint (val_chain);
2164
2165 if (reg_cnt)
2166 {
2167 int i, target_resources_ok, other_type_used;
2168 enum bptype type;
2169
2170 /* Use an exact watchpoint when there's only one memory region to be
2171 watched, and only one debug register is needed to watch it. */
2172 b->exact = target_exact_watchpoints && reg_cnt == 1;
2173
2174 /* We need to determine how many resources are already
2175 used for all other hardware watchpoints plus this one
2176 to see if we still have enough resources to also fit
2177 this watchpoint in as well. */
2178
2179 /* If this is a software watchpoint, we try to turn it
2180 to a hardware one -- count resources as if B was of
2181 hardware watchpoint type. */
2182 type = b->type;
2183 if (type == bp_watchpoint)
2184 type = bp_hardware_watchpoint;
2185
2186 /* This watchpoint may or may not have been placed on
2187 the list yet at this point (it won't be in the list
2188 if we're trying to create it for the first time,
2189 through watch_command), so always account for it
2190 manually. */
2191
2192 /* Count resources used by all watchpoints except B. */
2193 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2194
2195 /* Add in the resources needed for B. */
2196 i += hw_watchpoint_use_count (b);
2197
2198 target_resources_ok
2199 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2200 if (target_resources_ok <= 0)
2201 {
2202 bool sw_mode = b->works_in_software_mode ();
2203
2204 if (target_resources_ok == 0 && !sw_mode)
2205 error (_("Target does not support this type of "
2206 "hardware watchpoint."));
2207 else if (target_resources_ok < 0 && !sw_mode)
2208 error (_("There are not enough available hardware "
2209 "resources for this watchpoint."));
2210
2211 /* Downgrade to software watchpoint. */
2212 change_type_to_bp_watchpoint (b);
2213 }
2214 else
2215 {
2216 /* If this was a software watchpoint, we've just
2217 found we have enough resources to turn it to a
2218 hardware watchpoint. Otherwise, this is a
2219 nop. */
2220 b->type = type;
2221 }
2222 }
2223 else if (!b->works_in_software_mode ())
2224 {
2225 if (!can_use_hw_watchpoints)
2226 error (_("Can't set read/access watchpoint when "
2227 "hardware watchpoints are disabled."));
2228 else
2229 error (_("Expression cannot be implemented with "
2230 "read/access watchpoint."));
2231 }
2232 else
2233 change_type_to_bp_watchpoint (b);
2234
2235 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
2236 : bp_loc_hardware_watchpoint);
2237 for (bp_location *bl : b->locations ())
2238 bl->loc_type = loc_type;
2239 }
2240
2241 /* If a software watchpoint is not watching any memory, then the
2242 above left it without any location set up. But,
2243 bpstat_stop_status requires a location to be able to report
2244 stops, so make sure there's at least a dummy one. */
2245 if (b->type == bp_watchpoint && b->loc == NULL)
2246 add_dummy_location (b, frame_pspace);
2247 }
2248 else if (!within_current_scope)
2249 {
2250 gdb_printf (_("\
2251 Watchpoint %d deleted because the program has left the block\n\
2252 in which its expression is valid.\n"),
2253 b->number);
2254 watchpoint_del_at_next_stop (b);
2255 }
2256 }
2257
2258 /* Returns true iff breakpoint location should be
2259 inserted in the inferior. We don't differentiate the type of BL's owner
2260 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2261 breakpoint_ops is not defined, because in insert_bp_location,
2262 tracepoint's insert_location will not be called. */
2263
2264 static bool
2265 should_be_inserted (struct bp_location *bl)
2266 {
2267 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2268 return false;
2269
2270 if (bl->owner->disposition == disp_del_at_next_stop)
2271 return false;
2272
2273 if (!bl->enabled || bl->disabled_by_cond
2274 || bl->shlib_disabled || bl->duplicate)
2275 return false;
2276
2277 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2278 return false;
2279
2280 /* This is set for example, when we're attached to the parent of a
2281 vfork, and have detached from the child. The child is running
2282 free, and we expect it to do an exec or exit, at which point the
2283 OS makes the parent schedulable again (and the target reports
2284 that the vfork is done). Until the child is done with the shared
2285 memory region, do not insert breakpoints in the parent, otherwise
2286 the child could still trip on the parent's breakpoints. Since
2287 the parent is blocked anyway, it won't miss any breakpoint. */
2288 if (bl->pspace->breakpoints_not_allowed)
2289 return false;
2290
2291 /* Don't insert a breakpoint if we're trying to step past its
2292 location, except if the breakpoint is a single-step breakpoint,
2293 and the breakpoint's thread is the thread which is stepping past
2294 a breakpoint. */
2295 if ((bl->loc_type == bp_loc_software_breakpoint
2296 || bl->loc_type == bp_loc_hardware_breakpoint)
2297 && stepping_past_instruction_at (bl->pspace->aspace,
2298 bl->address)
2299 /* The single-step breakpoint may be inserted at the location
2300 we're trying to step if the instruction branches to itself.
2301 However, the instruction won't be executed at all and it may
2302 break the semantics of the instruction, for example, the
2303 instruction is a conditional branch or updates some flags.
2304 We can't fix it unless GDB is able to emulate the instruction
2305 or switch to displaced stepping. */
2306 && !(bl->owner->type == bp_single_step
2307 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2308 {
2309 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2310 paddress (bl->gdbarch, bl->address));
2311 return false;
2312 }
2313
2314 /* Don't insert watchpoints if we're trying to step past the
2315 instruction that triggered one. */
2316 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2317 && stepping_past_nonsteppable_watchpoint ())
2318 {
2319 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2320 "skipping watchpoint at %s:%d",
2321 paddress (bl->gdbarch, bl->address), bl->length);
2322 return false;
2323 }
2324
2325 return true;
2326 }
2327
2328 /* Same as should_be_inserted but does the check assuming
2329 that the location is not duplicated. */
2330
2331 static bool
2332 unduplicated_should_be_inserted (struct bp_location *bl)
2333 {
2334 scoped_restore restore_bl_duplicate
2335 = make_scoped_restore (&bl->duplicate, 0);
2336
2337 return should_be_inserted (bl);
2338 }
2339
2340 /* Parses a conditional described by an expression COND into an
2341 agent expression bytecode suitable for evaluation
2342 by the bytecode interpreter. Return NULL if there was
2343 any error during parsing. */
2344
2345 static agent_expr_up
2346 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2347 {
2348 if (cond == NULL)
2349 return NULL;
2350
2351 agent_expr_up aexpr;
2352
2353 /* We don't want to stop processing, so catch any errors
2354 that may show up. */
2355 try
2356 {
2357 aexpr = gen_eval_for_expr (scope, cond);
2358 }
2359
2360 catch (const gdb_exception_error &ex)
2361 {
2362 /* If we got here, it means the condition could not be parsed to a valid
2363 bytecode expression and thus can't be evaluated on the target's side.
2364 It's no use iterating through the conditions. */
2365 }
2366
2367 /* We have a valid agent expression. */
2368 return aexpr;
2369 }
2370
2371 /* Based on location BL, create a list of breakpoint conditions to be
2372 passed on to the target. If we have duplicated locations with different
2373 conditions, we will add such conditions to the list. The idea is that the
2374 target will evaluate the list of conditions and will only notify GDB when
2375 one of them is true. */
2376
2377 static void
2378 build_target_condition_list (struct bp_location *bl)
2379 {
2380 bool null_condition_or_parse_error = false;
2381 int modified = bl->needs_update;
2382
2383 /* Release conditions left over from a previous insert. */
2384 bl->target_info.conditions.clear ();
2385
2386 /* This is only meaningful if the target is
2387 evaluating conditions and if the user has
2388 opted for condition evaluation on the target's
2389 side. */
2390 if (gdb_evaluates_breakpoint_condition_p ()
2391 || !target_supports_evaluation_of_breakpoint_conditions ())
2392 return;
2393
2394 auto loc_range = all_bp_locations_at_addr (bl->address);
2395
2396 /* Do a first pass to check for locations with no assigned
2397 conditions or conditions that fail to parse to a valid agent
2398 expression bytecode. If any of these happen, then it's no use to
2399 send conditions to the target since this location will always
2400 trigger and generate a response back to GDB. Note we consider
2401 all locations at the same address irrespective of type, i.e.,
2402 even if the locations aren't considered duplicates (e.g.,
2403 software breakpoint and hardware breakpoint at the same
2404 address). */
2405 for (bp_location *loc : loc_range)
2406 {
2407 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2408 {
2409 if (modified)
2410 {
2411 /* Re-parse the conditions since something changed. In that
2412 case we already freed the condition bytecodes (see
2413 force_breakpoint_reinsertion). We just
2414 need to parse the condition to bytecodes again. */
2415 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2416 loc->cond.get ());
2417 }
2418
2419 /* If we have a NULL bytecode expression, it means something
2420 went wrong or we have a null condition expression. */
2421 if (!loc->cond_bytecode)
2422 {
2423 null_condition_or_parse_error = true;
2424 break;
2425 }
2426 }
2427 }
2428
2429 /* If any of these happened, it means we will have to evaluate the conditions
2430 for the location's address on gdb's side. It is no use keeping bytecodes
2431 for all the other duplicate locations, thus we free all of them here.
2432
2433 This is so we have a finer control over which locations' conditions are
2434 being evaluated by GDB or the remote stub. */
2435 if (null_condition_or_parse_error)
2436 {
2437 for (bp_location *loc : loc_range)
2438 {
2439 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2440 {
2441 /* Only go as far as the first NULL bytecode is
2442 located. */
2443 if (!loc->cond_bytecode)
2444 return;
2445
2446 loc->cond_bytecode.reset ();
2447 }
2448 }
2449 }
2450
2451 /* No NULL conditions or failed bytecode generation. Build a
2452 condition list for this location's address. If we have software
2453 and hardware locations at the same address, they aren't
2454 considered duplicates, but we still marge all the conditions
2455 anyway, as it's simpler, and doesn't really make a practical
2456 difference. */
2457 for (bp_location *loc : loc_range)
2458 if (loc->cond
2459 && is_breakpoint (loc->owner)
2460 && loc->pspace->num == bl->pspace->num
2461 && loc->owner->enable_state == bp_enabled
2462 && loc->enabled
2463 && !loc->disabled_by_cond)
2464 {
2465 /* Add the condition to the vector. This will be used later
2466 to send the conditions to the target. */
2467 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2468 }
2469
2470 return;
2471 }
2472
2473 /* Parses a command described by string CMD into an agent expression
2474 bytecode suitable for evaluation by the bytecode interpreter.
2475 Return NULL if there was any error during parsing. */
2476
2477 static agent_expr_up
2478 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2479 {
2480 const char *cmdrest;
2481 const char *format_start, *format_end;
2482 struct gdbarch *gdbarch = get_current_arch ();
2483
2484 if (cmd == NULL)
2485 return NULL;
2486
2487 cmdrest = cmd;
2488
2489 if (*cmdrest == ',')
2490 ++cmdrest;
2491 cmdrest = skip_spaces (cmdrest);
2492
2493 if (*cmdrest++ != '"')
2494 error (_("No format string following the location"));
2495
2496 format_start = cmdrest;
2497
2498 format_pieces fpieces (&cmdrest);
2499
2500 format_end = cmdrest;
2501
2502 if (*cmdrest++ != '"')
2503 error (_("Bad format string, non-terminated '\"'."));
2504
2505 cmdrest = skip_spaces (cmdrest);
2506
2507 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2508 error (_("Invalid argument syntax"));
2509
2510 if (*cmdrest == ',')
2511 cmdrest++;
2512 cmdrest = skip_spaces (cmdrest);
2513
2514 /* For each argument, make an expression. */
2515
2516 std::vector<struct expression *> argvec;
2517 while (*cmdrest != '\0')
2518 {
2519 const char *cmd1;
2520
2521 cmd1 = cmdrest;
2522 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope),
2523 PARSER_COMMA_TERMINATES);
2524 argvec.push_back (expr.release ());
2525 cmdrest = cmd1;
2526 if (*cmdrest == ',')
2527 ++cmdrest;
2528 }
2529
2530 agent_expr_up aexpr;
2531
2532 /* We don't want to stop processing, so catch any errors
2533 that may show up. */
2534 try
2535 {
2536 aexpr = gen_printf (scope, gdbarch, 0, 0,
2537 format_start, format_end - format_start,
2538 argvec.size (), argvec.data ());
2539 }
2540 catch (const gdb_exception_error &ex)
2541 {
2542 /* If we got here, it means the command could not be parsed to a valid
2543 bytecode expression and thus can't be evaluated on the target's side.
2544 It's no use iterating through the other commands. */
2545 }
2546
2547 /* We have a valid agent expression, return it. */
2548 return aexpr;
2549 }
2550
2551 /* Based on location BL, create a list of breakpoint commands to be
2552 passed on to the target. If we have duplicated locations with
2553 different commands, we will add any such to the list. */
2554
2555 static void
2556 build_target_command_list (struct bp_location *bl)
2557 {
2558 bool null_command_or_parse_error = false;
2559 int modified = bl->needs_update;
2560
2561 /* Clear commands left over from a previous insert. */
2562 bl->target_info.tcommands.clear ();
2563
2564 if (!target_can_run_breakpoint_commands ())
2565 return;
2566
2567 /* For now, limit to agent-style dprintf breakpoints. */
2568 if (dprintf_style != dprintf_style_agent)
2569 return;
2570
2571 auto loc_range = all_bp_locations_at_addr (bl->address);
2572
2573 /* For now, if we have any location at the same address that isn't a
2574 dprintf, don't install the target-side commands, as that would
2575 make the breakpoint not be reported to the core, and we'd lose
2576 control. */
2577 for (bp_location *loc : loc_range)
2578 if (is_breakpoint (loc->owner)
2579 && loc->pspace->num == bl->pspace->num
2580 && loc->owner->type != bp_dprintf)
2581 return;
2582
2583 /* Do a first pass to check for locations with no assigned
2584 conditions or conditions that fail to parse to a valid agent expression
2585 bytecode. If any of these happen, then it's no use to send conditions
2586 to the target since this location will always trigger and generate a
2587 response back to GDB. */
2588 for (bp_location *loc : loc_range)
2589 {
2590 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2591 {
2592 if (modified)
2593 {
2594 /* Re-parse the commands since something changed. In that
2595 case we already freed the command bytecodes (see
2596 force_breakpoint_reinsertion). We just
2597 need to parse the command to bytecodes again. */
2598 loc->cmd_bytecode
2599 = parse_cmd_to_aexpr (bl->address,
2600 loc->owner->extra_string.get ());
2601 }
2602
2603 /* If we have a NULL bytecode expression, it means something
2604 went wrong or we have a null command expression. */
2605 if (!loc->cmd_bytecode)
2606 {
2607 null_command_or_parse_error = true;
2608 break;
2609 }
2610 }
2611 }
2612
2613 /* If anything failed, then we're not doing target-side commands,
2614 and so clean up. */
2615 if (null_command_or_parse_error)
2616 {
2617 for (bp_location *loc : loc_range)
2618 if (is_breakpoint (loc->owner)
2619 && loc->pspace->num == bl->pspace->num)
2620 {
2621 /* Only go as far as the first NULL bytecode is
2622 located. */
2623 if (loc->cmd_bytecode == NULL)
2624 return;
2625
2626 loc->cmd_bytecode.reset ();
2627 }
2628 }
2629
2630 /* No NULL commands or failed bytecode generation. Build a command
2631 list for all duplicate locations at this location's address.
2632 Note that here we must care for whether the breakpoint location
2633 types are considered duplicates, otherwise, say, if we have a
2634 software and hardware location at the same address, the target
2635 could end up running the commands twice. For the moment, we only
2636 support targets-side commands with dprintf, but it doesn't hurt
2637 to be pedantically correct in case that changes. */
2638 for (bp_location *loc : loc_range)
2639 if (breakpoint_locations_match (bl, loc)
2640 && loc->owner->extra_string
2641 && is_breakpoint (loc->owner)
2642 && loc->pspace->num == bl->pspace->num
2643 && loc->owner->enable_state == bp_enabled
2644 && loc->enabled
2645 && !loc->disabled_by_cond)
2646 {
2647 /* Add the command to the vector. This will be used later
2648 to send the commands to the target. */
2649 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2650 }
2651
2652 bl->target_info.persist = 0;
2653 /* Maybe flag this location as persistent. */
2654 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2655 bl->target_info.persist = 1;
2656 }
2657
2658 /* Return the kind of breakpoint on address *ADDR. Get the kind
2659 of breakpoint according to ADDR except single-step breakpoint.
2660 Get the kind of single-step breakpoint according to the current
2661 registers state. */
2662
2663 static int
2664 breakpoint_kind (const struct bp_location *bl, CORE_ADDR *addr)
2665 {
2666 if (bl->owner->type == bp_single_step)
2667 {
2668 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2669 struct regcache *regcache;
2670
2671 regcache = get_thread_regcache (thr);
2672
2673 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2674 regcache, addr);
2675 }
2676 else
2677 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2678 }
2679
2680 /* Rethrow the currently handled exception, if it's a TARGET_CLOSE_ERROR.
2681 E is either the currently handled exception, or a copy, or a sliced copy,
2682 so we can't rethrow that one, but we can use it to inspect the properties
2683 of the currently handled exception. */
2684
2685 static void
2686 rethrow_on_target_close_error (const gdb_exception &e)
2687 {
2688 if (e.reason == 0)
2689 return;
2690 /* Can't set the breakpoint. */
2691
2692 if (e.error != TARGET_CLOSE_ERROR)
2693 return;
2694
2695 /* If the target has closed then it will have deleted any breakpoints
2696 inserted within the target inferior, as a result any further attempts
2697 to interact with the breakpoint objects is not possible. Just rethrow
2698 the error. Don't use e to rethrow, to prevent object slicing of the
2699 exception. */
2700 throw;
2701 }
2702
2703 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2704 location. Any error messages are printed to TMP_ERROR_STREAM; and
2705 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2706 Returns 0 for success, 1 if the bp_location type is not supported or
2707 -1 for failure.
2708
2709 NOTE drow/2003-09-09: This routine could be broken down to an
2710 object-style method for each breakpoint or catchpoint type. */
2711 static int
2712 insert_bp_location (struct bp_location *bl,
2713 struct ui_file *tmp_error_stream,
2714 int *disabled_breaks,
2715 int *hw_breakpoint_error,
2716 int *hw_bp_error_explained_already)
2717 {
2718 gdb_exception bp_excpt;
2719
2720 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2721 return 0;
2722
2723 /* Note we don't initialize bl->target_info, as that wipes out
2724 the breakpoint location's shadow_contents if the breakpoint
2725 is still inserted at that location. This in turn breaks
2726 target_read_memory which depends on these buffers when
2727 a memory read is requested at the breakpoint location:
2728 Once the target_info has been wiped, we fail to see that
2729 we have a breakpoint inserted at that address and thus
2730 read the breakpoint instead of returning the data saved in
2731 the breakpoint location's shadow contents. */
2732 bl->target_info.reqstd_address = bl->address;
2733 bl->target_info.placed_address_space = bl->pspace->aspace;
2734 bl->target_info.length = bl->length;
2735
2736 /* When working with target-side conditions, we must pass all the conditions
2737 for the same breakpoint address down to the target since GDB will not
2738 insert those locations. With a list of breakpoint conditions, the target
2739 can decide when to stop and notify GDB. */
2740
2741 if (is_breakpoint (bl->owner))
2742 {
2743 build_target_condition_list (bl);
2744 build_target_command_list (bl);
2745 /* Reset the modification marker. */
2746 bl->needs_update = 0;
2747 }
2748
2749 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2750 set at a read-only address, then a breakpoint location will have
2751 been changed to hardware breakpoint before we get here. If it is
2752 "off" however, error out before actually trying to insert the
2753 breakpoint, with a nicer error message. */
2754 if (bl->loc_type == bp_loc_software_breakpoint
2755 && !automatic_hardware_breakpoints)
2756 {
2757 mem_region *mr = lookup_mem_region (bl->address);
2758
2759 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2760 {
2761 gdb_printf (tmp_error_stream,
2762 _("Cannot insert breakpoint %d.\n"
2763 "Cannot set software breakpoint "
2764 "at read-only address %s\n"),
2765 bl->owner->number,
2766 paddress (bl->gdbarch, bl->address));
2767 return 1;
2768 }
2769 }
2770
2771 if (bl->loc_type == bp_loc_software_breakpoint
2772 || bl->loc_type == bp_loc_hardware_breakpoint)
2773 {
2774 /* First check to see if we have to handle an overlay. */
2775 if (overlay_debugging == ovly_off
2776 || bl->section == NULL
2777 || !(section_is_overlay (bl->section)))
2778 {
2779 /* No overlay handling: just set the breakpoint. */
2780 try
2781 {
2782 int val;
2783
2784 val = bl->owner->insert_location (bl);
2785 if (val)
2786 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2787 }
2788 catch (gdb_exception &e)
2789 {
2790 rethrow_on_target_close_error (e);
2791 bp_excpt = std::move (e);
2792 }
2793 }
2794 else
2795 {
2796 /* This breakpoint is in an overlay section.
2797 Shall we set a breakpoint at the LMA? */
2798 if (!overlay_events_enabled)
2799 {
2800 /* Yes -- overlay event support is not active,
2801 so we must try to set a breakpoint at the LMA.
2802 This will not work for a hardware breakpoint. */
2803 if (bl->loc_type == bp_loc_hardware_breakpoint)
2804 warning (_("hardware breakpoint %d not supported in overlay!"),
2805 bl->owner->number);
2806 else
2807 {
2808 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2809 bl->section);
2810 /* Set a software (trap) breakpoint at the LMA. */
2811 bl->overlay_target_info = bl->target_info;
2812 bl->overlay_target_info.reqstd_address = addr;
2813
2814 /* No overlay handling: just set the breakpoint. */
2815 try
2816 {
2817 int val;
2818
2819 bl->overlay_target_info.kind
2820 = breakpoint_kind (bl, &addr);
2821 bl->overlay_target_info.placed_address = addr;
2822 val = target_insert_breakpoint (bl->gdbarch,
2823 &bl->overlay_target_info);
2824 if (val)
2825 bp_excpt
2826 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2827 }
2828 catch (gdb_exception &e)
2829 {
2830 rethrow_on_target_close_error (e);
2831 bp_excpt = std::move (e);
2832 }
2833
2834 if (bp_excpt.reason != 0)
2835 gdb_printf (tmp_error_stream,
2836 "Overlay breakpoint %d "
2837 "failed: in ROM?\n",
2838 bl->owner->number);
2839 }
2840 }
2841 /* Shall we set a breakpoint at the VMA? */
2842 if (section_is_mapped (bl->section))
2843 {
2844 /* Yes. This overlay section is mapped into memory. */
2845 try
2846 {
2847 int val;
2848
2849 val = bl->owner->insert_location (bl);
2850 if (val)
2851 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2852 }
2853 catch (gdb_exception_error &e)
2854 {
2855 rethrow_on_target_close_error (e);
2856 bp_excpt = std::move (e);
2857 }
2858 }
2859 else
2860 {
2861 /* No. This breakpoint will not be inserted.
2862 No error, but do not mark the bp as 'inserted'. */
2863 return 0;
2864 }
2865 }
2866
2867 if (bp_excpt.reason != 0)
2868 {
2869 /* Can't set the breakpoint. */
2870 gdb_assert (bl->owner != nullptr);
2871
2872 /* In some cases, we might not be able to insert a
2873 breakpoint in a shared library that has already been
2874 removed, but we have not yet processed the shlib unload
2875 event. Unfortunately, some targets that implement
2876 breakpoint insertion themselves can't tell why the
2877 breakpoint insertion failed (e.g., the remote target
2878 doesn't define error codes), so we must treat generic
2879 errors as memory errors. */
2880 if (bp_excpt.reason == RETURN_ERROR
2881 && (bp_excpt.error == GENERIC_ERROR
2882 || bp_excpt.error == MEMORY_ERROR)
2883 && bl->loc_type == bp_loc_software_breakpoint
2884 && (solib_name_from_address (bl->pspace, bl->address)
2885 || shared_objfile_contains_address_p (bl->pspace,
2886 bl->address)))
2887 {
2888 /* See also: disable_breakpoints_in_shlibs. */
2889 bl->shlib_disabled = 1;
2890 gdb::observers::breakpoint_modified.notify (bl->owner);
2891 if (!*disabled_breaks)
2892 {
2893 gdb_printf (tmp_error_stream,
2894 "Cannot insert breakpoint %d.\n",
2895 bl->owner->number);
2896 gdb_printf (tmp_error_stream,
2897 "Temporarily disabling shared "
2898 "library breakpoints:\n");
2899 }
2900 *disabled_breaks = 1;
2901 gdb_printf (tmp_error_stream,
2902 "breakpoint #%d\n", bl->owner->number);
2903 return 0;
2904 }
2905 else
2906 {
2907 if (bl->loc_type == bp_loc_hardware_breakpoint)
2908 {
2909 *hw_breakpoint_error = 1;
2910 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2911 gdb_printf (tmp_error_stream,
2912 "Cannot insert hardware breakpoint %d%s",
2913 bl->owner->number,
2914 bp_excpt.message ? ":" : ".\n");
2915 if (bp_excpt.message != NULL)
2916 gdb_printf (tmp_error_stream, "%s.\n",
2917 bp_excpt.what ());
2918 }
2919 else
2920 {
2921 if (bp_excpt.message == NULL)
2922 {
2923 std::string message
2924 = memory_error_message (TARGET_XFER_E_IO,
2925 bl->gdbarch, bl->address);
2926
2927 gdb_printf (tmp_error_stream,
2928 "Cannot insert breakpoint %d.\n"
2929 "%s\n",
2930 bl->owner->number, message.c_str ());
2931 }
2932 else
2933 {
2934 gdb_printf (tmp_error_stream,
2935 "Cannot insert breakpoint %d: %s\n",
2936 bl->owner->number,
2937 bp_excpt.what ());
2938 }
2939 }
2940 return 1;
2941
2942 }
2943 }
2944 else
2945 bl->inserted = 1;
2946
2947 return 0;
2948 }
2949
2950 else if (bl->loc_type == bp_loc_hardware_watchpoint
2951 && bl->owner->disposition != disp_del_at_next_stop)
2952 {
2953 int val;
2954
2955 val = bl->owner->insert_location (bl);
2956
2957 /* If trying to set a read-watchpoint, and it turns out it's not
2958 supported, try emulating one with an access watchpoint. */
2959 if (val == 1 && bl->watchpoint_type == hw_read)
2960 {
2961 /* But don't try to insert it, if there's already another
2962 hw_access location that would be considered a duplicate
2963 of this one. */
2964 for (bp_location *loc : all_bp_locations ())
2965 if (loc != bl
2966 && loc->watchpoint_type == hw_access
2967 && watchpoint_locations_match (bl, loc))
2968 {
2969 bl->duplicate = 1;
2970 bl->inserted = 1;
2971 bl->target_info = loc->target_info;
2972 bl->watchpoint_type = hw_access;
2973 val = 0;
2974 break;
2975 }
2976
2977 if (val == 1)
2978 {
2979 bl->watchpoint_type = hw_access;
2980 val = bl->owner->insert_location (bl);
2981
2982 if (val)
2983 /* Back to the original value. */
2984 bl->watchpoint_type = hw_read;
2985 }
2986 }
2987
2988 bl->inserted = (val == 0);
2989 }
2990
2991 else if (bl->owner->type == bp_catchpoint)
2992 {
2993 int val;
2994
2995 val = bl->owner->insert_location (bl);
2996 if (val)
2997 {
2998 bl->owner->enable_state = bp_disabled;
2999
3000 if (val == 1)
3001 warning (_("\
3002 Error inserting catchpoint %d: Your system does not support this type\n\
3003 of catchpoint."), bl->owner->number);
3004 else
3005 warning (_("Error inserting catchpoint %d."), bl->owner->number);
3006 }
3007
3008 bl->inserted = (val == 0);
3009
3010 /* We've already printed an error message if there was a problem
3011 inserting this catchpoint, and we've disabled the catchpoint,
3012 so just return success. */
3013 return 0;
3014 }
3015
3016 return 0;
3017 }
3018
3019 /* This function is called when program space PSPACE is about to be
3020 deleted. It takes care of updating breakpoints to not reference
3021 PSPACE anymore. */
3022
3023 void
3024 breakpoint_program_space_exit (struct program_space *pspace)
3025 {
3026 /* Remove any breakpoint that was set through this program space. */
3027 for (breakpoint *b : all_breakpoints_safe ())
3028 if (b->pspace == pspace)
3029 delete_breakpoint (b);
3030
3031 /* Breakpoints set through other program spaces could have locations
3032 bound to PSPACE as well. Remove those. */
3033 for (bp_location *loc : all_bp_locations ())
3034 {
3035 struct bp_location *tmp;
3036
3037 if (loc->pspace == pspace)
3038 {
3039 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3040 if (loc->owner->loc == loc)
3041 loc->owner->loc = loc->next;
3042 else
3043 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3044 if (tmp->next == loc)
3045 {
3046 tmp->next = loc->next;
3047 break;
3048 }
3049 }
3050 }
3051
3052 /* Now update the global location list to permanently delete the
3053 removed locations above. */
3054 update_global_location_list (UGLL_DONT_INSERT);
3055 }
3056
3057 /* Make sure all breakpoints are inserted in inferior.
3058 Throws exception on any error.
3059 A breakpoint that is already inserted won't be inserted
3060 again, so calling this function twice is safe. */
3061 void
3062 insert_breakpoints (void)
3063 {
3064 for (breakpoint *bpt : all_breakpoints ())
3065 if (is_hardware_watchpoint (bpt))
3066 {
3067 struct watchpoint *w = (struct watchpoint *) bpt;
3068
3069 update_watchpoint (w, false /* don't reparse. */);
3070 }
3071
3072 /* Updating watchpoints creates new locations, so update the global
3073 location list. Explicitly tell ugll to insert locations and
3074 ignore breakpoints_always_inserted_mode. Also,
3075 update_global_location_list tries to "upgrade" software
3076 breakpoints to hardware breakpoints to handle "set breakpoint
3077 auto-hw", so we need to call it even if we don't have new
3078 locations. */
3079 update_global_location_list (UGLL_INSERT);
3080 }
3081
3082 /* This is used when we need to synch breakpoint conditions between GDB and the
3083 target. It is the case with deleting and disabling of breakpoints when using
3084 always-inserted mode. */
3085
3086 static void
3087 update_inserted_breakpoint_locations (void)
3088 {
3089 int error_flag = 0;
3090 int val = 0;
3091 int disabled_breaks = 0;
3092 int hw_breakpoint_error = 0;
3093 int hw_bp_details_reported = 0;
3094
3095 string_file tmp_error_stream;
3096
3097 /* Explicitly mark the warning -- this will only be printed if
3098 there was an error. */
3099 tmp_error_stream.puts ("Warning:\n");
3100
3101 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3102
3103 for (bp_location *bl : all_bp_locations ())
3104 {
3105 /* We only want to update software breakpoints and hardware
3106 breakpoints. */
3107 if (!is_breakpoint (bl->owner))
3108 continue;
3109
3110 /* We only want to update locations that are already inserted
3111 and need updating. This is to avoid unwanted insertion during
3112 deletion of breakpoints. */
3113 if (!bl->inserted || !bl->needs_update)
3114 continue;
3115
3116 switch_to_program_space_and_thread (bl->pspace);
3117
3118 /* For targets that support global breakpoints, there's no need
3119 to select an inferior to insert breakpoint to. In fact, even
3120 if we aren't attached to any process yet, we should still
3121 insert breakpoints. */
3122 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3123 && (inferior_ptid == null_ptid || !target_has_execution ()))
3124 continue;
3125
3126 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3127 &hw_breakpoint_error, &hw_bp_details_reported);
3128 if (val)
3129 error_flag = val;
3130 }
3131
3132 if (error_flag)
3133 {
3134 target_terminal::ours_for_output ();
3135 error (("%s"), tmp_error_stream.c_str ());
3136 }
3137 }
3138
3139 /* Used when starting or continuing the program. */
3140
3141 static void
3142 insert_breakpoint_locations (void)
3143 {
3144 int error_flag = 0;
3145 int val = 0;
3146 int disabled_breaks = 0;
3147 int hw_breakpoint_error = 0;
3148 int hw_bp_error_explained_already = 0;
3149
3150 string_file tmp_error_stream;
3151
3152 /* Explicitly mark the warning -- this will only be printed if
3153 there was an error. */
3154 tmp_error_stream.puts ("Warning:\n");
3155
3156 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3157
3158 for (bp_location *bl : all_bp_locations ())
3159 {
3160 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3161 continue;
3162
3163 /* There is no point inserting thread-specific breakpoints if
3164 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3165 has BL->OWNER always non-NULL. */
3166 if (bl->owner->thread != -1
3167 && !valid_global_thread_id (bl->owner->thread))
3168 continue;
3169
3170 switch_to_program_space_and_thread (bl->pspace);
3171
3172 /* For targets that support global breakpoints, there's no need
3173 to select an inferior to insert breakpoint to. In fact, even
3174 if we aren't attached to any process yet, we should still
3175 insert breakpoints. */
3176 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3177 && (inferior_ptid == null_ptid || !target_has_execution ()))
3178 continue;
3179
3180 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3181 &hw_breakpoint_error, &hw_bp_error_explained_already);
3182 if (val)
3183 error_flag = val;
3184 }
3185
3186 /* If we failed to insert all locations of a watchpoint, remove
3187 them, as half-inserted watchpoint is of limited use. */
3188 for (breakpoint *bpt : all_breakpoints ())
3189 {
3190 bool some_failed = false;
3191
3192 if (!is_hardware_watchpoint (bpt))
3193 continue;
3194
3195 if (!breakpoint_enabled (bpt))
3196 continue;
3197
3198 if (bpt->disposition == disp_del_at_next_stop)
3199 continue;
3200
3201 for (bp_location *loc : bpt->locations ())
3202 if (!loc->inserted && should_be_inserted (loc))
3203 {
3204 some_failed = true;
3205 break;
3206 }
3207
3208 if (some_failed)
3209 {
3210 for (bp_location *loc : bpt->locations ())
3211 if (loc->inserted)
3212 remove_breakpoint (loc);
3213
3214 hw_breakpoint_error = 1;
3215 tmp_error_stream.printf ("Could not insert "
3216 "hardware watchpoint %d.\n",
3217 bpt->number);
3218 error_flag = -1;
3219 }
3220 }
3221
3222 if (error_flag)
3223 {
3224 /* If a hardware breakpoint or watchpoint was inserted, add a
3225 message about possibly exhausted resources. */
3226 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3227 {
3228 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3229 You may have requested too many hardware breakpoints/watchpoints.\n");
3230 }
3231 target_terminal::ours_for_output ();
3232 error (("%s"), tmp_error_stream.c_str ());
3233 }
3234 }
3235
3236 /* Used when the program stops.
3237 Returns zero if successful, or non-zero if there was a problem
3238 removing a breakpoint location. */
3239
3240 int
3241 remove_breakpoints (void)
3242 {
3243 int val = 0;
3244
3245 for (bp_location *bl : all_bp_locations ())
3246 if (bl->inserted && !is_tracepoint (bl->owner))
3247 val |= remove_breakpoint (bl);
3248
3249 return val;
3250 }
3251
3252 /* When a thread exits, remove breakpoints that are related to
3253 that thread. */
3254
3255 static void
3256 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3257 {
3258 for (breakpoint *b : all_breakpoints_safe ())
3259 {
3260 if (b->thread == tp->global_num && user_breakpoint_p (b))
3261 {
3262 gdb_printf (_("\
3263 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3264 b->number, print_thread_id (tp));
3265 delete_breakpoint (b);
3266 }
3267 }
3268 }
3269
3270 /* See breakpoint.h. */
3271
3272 void
3273 remove_breakpoints_inf (inferior *inf)
3274 {
3275 int val;
3276
3277 for (bp_location *bl : all_bp_locations ())
3278 {
3279 if (bl->pspace != inf->pspace)
3280 continue;
3281
3282 if (bl->inserted && !bl->target_info.persist)
3283 {
3284 val = remove_breakpoint (bl);
3285 if (val != 0)
3286 return;
3287 }
3288 }
3289 }
3290
3291 static int internal_breakpoint_number = -1;
3292
3293 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3294 If INTERNAL is non-zero, the breakpoint number will be populated
3295 from internal_breakpoint_number and that variable decremented.
3296 Otherwise the breakpoint number will be populated from
3297 breakpoint_count and that value incremented. Internal breakpoints
3298 do not set the internal var bpnum. */
3299 static void
3300 set_breakpoint_number (int internal, struct breakpoint *b)
3301 {
3302 if (internal)
3303 b->number = internal_breakpoint_number--;
3304 else
3305 {
3306 set_breakpoint_count (breakpoint_count + 1);
3307 b->number = breakpoint_count;
3308 }
3309 }
3310
3311 /* Create a TYPE breakpoint on ADDRESS from an object file with GDBARCH. */
3312
3313 static struct breakpoint *
3314 create_internal_breakpoint (struct gdbarch *gdbarch,
3315 CORE_ADDR address, enum bptype type)
3316 {
3317 std::unique_ptr<internal_breakpoint> b
3318 (new internal_breakpoint (gdbarch, type, address));
3319
3320 b->number = internal_breakpoint_number--;
3321
3322 return add_to_breakpoint_chain (std::move (b));
3323 }
3324
3325 /* Create a TYPE breakpoint on minimal symbol MSYM from an object file with
3326 GDBARCH. */
3327
3328 static struct breakpoint *
3329 create_internal_breakpoint (struct gdbarch *gdbarch,
3330 struct bound_minimal_symbol &msym, enum bptype type)
3331 {
3332 CORE_ADDR address;
3333
3334 address = msym.value_address ();
3335
3336 address = gdbarch_convert_from_func_ptr_addr
3337 (gdbarch, address, current_inferior ()->top_target ());
3338
3339 /* Note that we're not using gdbarch_addr_bits_remove here, because that's
3340 related to addresses in $pc. We're getting the address from the
3341 minimal symbol table. */
3342
3343 /* Is gdbarch_deprecated_function_start_offset needed here? Or is that dealt
3344 with elsewhere? Needs testing on vax. */
3345
3346 if (gdbarch_skip_entrypoint_p (gdbarch))
3347 address = gdbarch_skip_entrypoint (gdbarch, address);
3348
3349 return create_internal_breakpoint (gdbarch, address, type);
3350 }
3351
3352 static const char *const longjmp_names[] =
3353 {
3354 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3355 };
3356 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3357
3358 /* Per-objfile data private to breakpoint.c. */
3359 struct breakpoint_objfile_data
3360 {
3361 /* Minimal symbol for "_ovly_debug_event" (if any). */
3362 struct bound_minimal_symbol overlay_msym;
3363
3364 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3365 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3366
3367 /* True if we have looked for longjmp probes. */
3368 int longjmp_searched = 0;
3369
3370 /* SystemTap probe points for longjmp (if any). These are non-owning
3371 references. */
3372 std::vector<probe *> longjmp_probes;
3373
3374 /* Minimal symbol for "std::terminate()" (if any). */
3375 struct bound_minimal_symbol terminate_msym;
3376
3377 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3378 struct bound_minimal_symbol exception_msym;
3379
3380 /* True if we have looked for exception probes. */
3381 int exception_searched = 0;
3382
3383 /* SystemTap probe points for unwinding (if any). These are non-owning
3384 references. */
3385 std::vector<probe *> exception_probes;
3386 };
3387
3388 static const registry<objfile>::key<breakpoint_objfile_data>
3389 breakpoint_objfile_key;
3390
3391 /* Minimal symbol not found sentinel. */
3392 static struct minimal_symbol msym_not_found;
3393
3394 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3395
3396 static bool
3397 msym_not_found_p (const struct minimal_symbol *msym)
3398 {
3399 return msym == &msym_not_found;
3400 }
3401
3402 /* Return per-objfile data needed by breakpoint.c.
3403 Allocate the data if necessary. */
3404
3405 static struct breakpoint_objfile_data *
3406 get_breakpoint_objfile_data (struct objfile *objfile)
3407 {
3408 struct breakpoint_objfile_data *bp_objfile_data;
3409
3410 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3411 if (bp_objfile_data == NULL)
3412 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3413 return bp_objfile_data;
3414 }
3415
3416 static void
3417 create_overlay_event_breakpoint (void)
3418 {
3419 const char *const func_name = "_ovly_debug_event";
3420
3421 for (objfile *objfile : current_program_space->objfiles ())
3422 {
3423 struct breakpoint *b;
3424 struct breakpoint_objfile_data *bp_objfile_data;
3425 CORE_ADDR addr;
3426
3427 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3428
3429 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3430 continue;
3431
3432 if (bp_objfile_data->overlay_msym.minsym == NULL)
3433 {
3434 struct bound_minimal_symbol m;
3435
3436 m = lookup_minimal_symbol_text (func_name, objfile);
3437 if (m.minsym == NULL)
3438 {
3439 /* Avoid future lookups in this objfile. */
3440 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3441 continue;
3442 }
3443 bp_objfile_data->overlay_msym = m;
3444 }
3445
3446 addr = bp_objfile_data->overlay_msym.value_address ();
3447 b = create_internal_breakpoint (objfile->arch (), addr,
3448 bp_overlay_event);
3449 b->locspec = new_explicit_location_spec_function (func_name);
3450
3451 if (overlay_debugging == ovly_auto)
3452 {
3453 b->enable_state = bp_enabled;
3454 overlay_events_enabled = 1;
3455 }
3456 else
3457 {
3458 b->enable_state = bp_disabled;
3459 overlay_events_enabled = 0;
3460 }
3461 }
3462 }
3463
3464 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3465 true if a breakpoint was installed. */
3466
3467 static bool
3468 create_longjmp_master_breakpoint_probe (objfile *objfile)
3469 {
3470 struct gdbarch *gdbarch = objfile->arch ();
3471 struct breakpoint_objfile_data *bp_objfile_data
3472 = get_breakpoint_objfile_data (objfile);
3473
3474 if (!bp_objfile_data->longjmp_searched)
3475 {
3476 std::vector<probe *> ret
3477 = find_probes_in_objfile (objfile, "libc", "longjmp");
3478
3479 if (!ret.empty ())
3480 {
3481 /* We are only interested in checking one element. */
3482 probe *p = ret[0];
3483
3484 if (!p->can_evaluate_arguments ())
3485 {
3486 /* We cannot use the probe interface here,
3487 because it does not know how to evaluate
3488 arguments. */
3489 ret.clear ();
3490 }
3491 }
3492 bp_objfile_data->longjmp_probes = ret;
3493 bp_objfile_data->longjmp_searched = 1;
3494 }
3495
3496 if (bp_objfile_data->longjmp_probes.empty ())
3497 return false;
3498
3499 for (probe *p : bp_objfile_data->longjmp_probes)
3500 {
3501 struct breakpoint *b;
3502
3503 b = create_internal_breakpoint (gdbarch,
3504 p->get_relocated_address (objfile),
3505 bp_longjmp_master);
3506 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
3507 b->enable_state = bp_disabled;
3508 }
3509
3510 return true;
3511 }
3512
3513 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3514 Return true if at least one breakpoint was installed. */
3515
3516 static bool
3517 create_longjmp_master_breakpoint_names (objfile *objfile)
3518 {
3519 struct gdbarch *gdbarch = objfile->arch ();
3520 if (!gdbarch_get_longjmp_target_p (gdbarch))
3521 return false;
3522
3523 struct breakpoint_objfile_data *bp_objfile_data
3524 = get_breakpoint_objfile_data (objfile);
3525 unsigned int installed_bp = 0;
3526
3527 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3528 {
3529 struct breakpoint *b;
3530 const char *func_name;
3531 CORE_ADDR addr;
3532
3533 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3534 continue;
3535
3536 func_name = longjmp_names[i];
3537 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3538 {
3539 struct bound_minimal_symbol m;
3540
3541 m = lookup_minimal_symbol_text (func_name, objfile);
3542 if (m.minsym == NULL)
3543 {
3544 /* Prevent future lookups in this objfile. */
3545 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3546 continue;
3547 }
3548 bp_objfile_data->longjmp_msym[i] = m;
3549 }
3550
3551 addr = bp_objfile_data->longjmp_msym[i].value_address ();
3552 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
3553 b->locspec = new_explicit_location_spec_function (func_name);
3554 b->enable_state = bp_disabled;
3555 installed_bp++;
3556 }
3557
3558 return installed_bp > 0;
3559 }
3560
3561 /* Create a master longjmp breakpoint. */
3562
3563 static void
3564 create_longjmp_master_breakpoint (void)
3565 {
3566 scoped_restore_current_program_space restore_pspace;
3567
3568 for (struct program_space *pspace : program_spaces)
3569 {
3570 set_current_program_space (pspace);
3571
3572 for (objfile *obj : current_program_space->objfiles ())
3573 {
3574 /* Skip separate debug object, it's handled in the loop below. */
3575 if (obj->separate_debug_objfile_backlink != nullptr)
3576 continue;
3577
3578 /* Try a probe kind breakpoint on main objfile. */
3579 if (create_longjmp_master_breakpoint_probe (obj))
3580 continue;
3581
3582 /* Try longjmp_names kind breakpoints on main and separate_debug
3583 objfiles. */
3584 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3585 if (create_longjmp_master_breakpoint_names (debug_objfile))
3586 break;
3587 }
3588 }
3589 }
3590
3591 /* Create a master std::terminate breakpoint. */
3592 static void
3593 create_std_terminate_master_breakpoint (void)
3594 {
3595 const char *const func_name = "std::terminate()";
3596
3597 scoped_restore_current_program_space restore_pspace;
3598
3599 for (struct program_space *pspace : program_spaces)
3600 {
3601 set_current_program_space (pspace);
3602
3603 for (objfile *objfile : current_program_space->objfiles ())
3604 {
3605 struct breakpoint *b;
3606 struct breakpoint_objfile_data *bp_objfile_data;
3607
3608 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3609
3610 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3611 continue;
3612
3613 if (bp_objfile_data->terminate_msym.minsym == NULL)
3614 {
3615 struct bound_minimal_symbol m;
3616
3617 m = lookup_minimal_symbol (func_name, NULL, objfile);
3618 if (m.minsym == NULL || (m.minsym->type () != mst_text
3619 && m.minsym->type () != mst_file_text))
3620 {
3621 /* Prevent future lookups in this objfile. */
3622 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3623 continue;
3624 }
3625 bp_objfile_data->terminate_msym = m;
3626 }
3627
3628 b = create_internal_breakpoint (objfile->arch (),
3629 bp_objfile_data->terminate_msym,
3630 bp_std_terminate_master);
3631 b->locspec = new_explicit_location_spec_function (func_name);
3632 b->enable_state = bp_disabled;
3633 }
3634 }
3635 }
3636
3637 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3638 probe. Return true if a breakpoint was installed. */
3639
3640 static bool
3641 create_exception_master_breakpoint_probe (objfile *objfile)
3642 {
3643 struct breakpoint *b;
3644 struct gdbarch *gdbarch;
3645 struct breakpoint_objfile_data *bp_objfile_data;
3646
3647 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3648
3649 /* We prefer the SystemTap probe point if it exists. */
3650 if (!bp_objfile_data->exception_searched)
3651 {
3652 std::vector<probe *> ret
3653 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3654
3655 if (!ret.empty ())
3656 {
3657 /* We are only interested in checking one element. */
3658 probe *p = ret[0];
3659
3660 if (!p->can_evaluate_arguments ())
3661 {
3662 /* We cannot use the probe interface here, because it does
3663 not know how to evaluate arguments. */
3664 ret.clear ();
3665 }
3666 }
3667 bp_objfile_data->exception_probes = ret;
3668 bp_objfile_data->exception_searched = 1;
3669 }
3670
3671 if (bp_objfile_data->exception_probes.empty ())
3672 return false;
3673
3674 gdbarch = objfile->arch ();
3675
3676 for (probe *p : bp_objfile_data->exception_probes)
3677 {
3678 b = create_internal_breakpoint (gdbarch,
3679 p->get_relocated_address (objfile),
3680 bp_exception_master);
3681 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
3682 b->enable_state = bp_disabled;
3683 }
3684
3685 return true;
3686 }
3687
3688 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3689 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3690
3691 static bool
3692 create_exception_master_breakpoint_hook (objfile *objfile)
3693 {
3694 const char *const func_name = "_Unwind_DebugHook";
3695 struct breakpoint *b;
3696 struct gdbarch *gdbarch;
3697 struct breakpoint_objfile_data *bp_objfile_data;
3698
3699 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3700
3701 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3702 return false;
3703
3704 gdbarch = objfile->arch ();
3705
3706 if (bp_objfile_data->exception_msym.minsym == NULL)
3707 {
3708 struct bound_minimal_symbol debug_hook;
3709
3710 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3711 if (debug_hook.minsym == NULL)
3712 {
3713 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3714 return false;
3715 }
3716
3717 bp_objfile_data->exception_msym = debug_hook;
3718 }
3719
3720 b = create_internal_breakpoint (gdbarch, bp_objfile_data->exception_msym,
3721 bp_exception_master);
3722 b->locspec = new_explicit_location_spec_function (func_name);
3723 b->enable_state = bp_disabled;
3724
3725 return true;
3726 }
3727
3728 /* Install a master breakpoint on the unwinder's debug hook. */
3729
3730 static void
3731 create_exception_master_breakpoint (void)
3732 {
3733 for (objfile *obj : current_program_space->objfiles ())
3734 {
3735 /* Skip separate debug object. */
3736 if (obj->separate_debug_objfile_backlink)
3737 continue;
3738
3739 /* Try a probe kind breakpoint. */
3740 if (create_exception_master_breakpoint_probe (obj))
3741 continue;
3742
3743 /* Iterate over main and separate debug objects and try an
3744 _Unwind_DebugHook kind breakpoint. */
3745 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3746 if (create_exception_master_breakpoint_hook (debug_objfile))
3747 break;
3748 }
3749 }
3750
3751 /* Does B have a location spec? */
3752
3753 static bool
3754 breakpoint_location_spec_empty_p (const struct breakpoint *b)
3755 {
3756 return (b->locspec != nullptr && b->locspec->empty_p ());
3757 }
3758
3759 void
3760 update_breakpoints_after_exec (void)
3761 {
3762 /* We're about to delete breakpoints from GDB's lists. If the
3763 INSERTED flag is true, GDB will try to lift the breakpoints by
3764 writing the breakpoints' "shadow contents" back into memory. The
3765 "shadow contents" are NOT valid after an exec, so GDB should not
3766 do that. Instead, the target is responsible from marking
3767 breakpoints out as soon as it detects an exec. We don't do that
3768 here instead, because there may be other attempts to delete
3769 breakpoints after detecting an exec and before reaching here. */
3770 for (bp_location *bploc : all_bp_locations ())
3771 if (bploc->pspace == current_program_space)
3772 gdb_assert (!bploc->inserted);
3773
3774 for (breakpoint *b : all_breakpoints_safe ())
3775 {
3776 if (b->pspace != current_program_space)
3777 continue;
3778
3779 /* Solib breakpoints must be explicitly reset after an exec(). */
3780 if (b->type == bp_shlib_event)
3781 {
3782 delete_breakpoint (b);
3783 continue;
3784 }
3785
3786 /* JIT breakpoints must be explicitly reset after an exec(). */
3787 if (b->type == bp_jit_event)
3788 {
3789 delete_breakpoint (b);
3790 continue;
3791 }
3792
3793 /* Thread event breakpoints must be set anew after an exec(),
3794 as must overlay event and longjmp master breakpoints. */
3795 if (b->type == bp_thread_event || b->type == bp_overlay_event
3796 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3797 || b->type == bp_exception_master)
3798 {
3799 delete_breakpoint (b);
3800 continue;
3801 }
3802
3803 /* Step-resume breakpoints are meaningless after an exec(). */
3804 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3805 {
3806 delete_breakpoint (b);
3807 continue;
3808 }
3809
3810 /* Just like single-step breakpoints. */
3811 if (b->type == bp_single_step)
3812 {
3813 delete_breakpoint (b);
3814 continue;
3815 }
3816
3817 /* Longjmp and longjmp-resume breakpoints are also meaningless
3818 after an exec. */
3819 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3820 || b->type == bp_longjmp_call_dummy
3821 || b->type == bp_exception || b->type == bp_exception_resume)
3822 {
3823 delete_breakpoint (b);
3824 continue;
3825 }
3826
3827 if (b->type == bp_catchpoint)
3828 {
3829 /* For now, none of the bp_catchpoint breakpoints need to
3830 do anything at this point. In the future, if some of
3831 the catchpoints need to something, we will need to add
3832 a new method, and call this method from here. */
3833 continue;
3834 }
3835
3836 /* bp_finish is a special case. The only way we ought to be able
3837 to see one of these when an exec() has happened, is if the user
3838 caught a vfork, and then said "finish". Ordinarily a finish just
3839 carries them to the call-site of the current callee, by setting
3840 a temporary bp there and resuming. But in this case, the finish
3841 will carry them entirely through the vfork & exec.
3842
3843 We don't want to allow a bp_finish to remain inserted now. But
3844 we can't safely delete it, 'cause finish_command has a handle to
3845 the bp on a bpstat, and will later want to delete it. There's a
3846 chance (and I've seen it happen) that if we delete the bp_finish
3847 here, that its storage will get reused by the time finish_command
3848 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3849 We really must allow finish_command to delete a bp_finish.
3850
3851 In the absence of a general solution for the "how do we know
3852 it's safe to delete something others may have handles to?"
3853 problem, what we'll do here is just uninsert the bp_finish, and
3854 let finish_command delete it.
3855
3856 (We know the bp_finish is "doomed" in the sense that it's
3857 momentary, and will be deleted as soon as finish_command sees
3858 the inferior stopped. So it doesn't matter that the bp's
3859 address is probably bogus in the new a.out, unlike e.g., the
3860 solib breakpoints.) */
3861
3862 if (b->type == bp_finish)
3863 {
3864 continue;
3865 }
3866
3867 /* Without a symbolic address, we have little hope of the
3868 pre-exec() address meaning the same thing in the post-exec()
3869 a.out. */
3870 if (breakpoint_location_spec_empty_p (b))
3871 {
3872 delete_breakpoint (b);
3873 continue;
3874 }
3875 }
3876 }
3877
3878 int
3879 detach_breakpoints (ptid_t ptid)
3880 {
3881 int val = 0;
3882 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3883 struct inferior *inf = current_inferior ();
3884
3885 if (ptid.pid () == inferior_ptid.pid ())
3886 error (_("Cannot detach breakpoints of inferior_ptid"));
3887
3888 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3889 inferior_ptid = ptid;
3890 for (bp_location *bl : all_bp_locations ())
3891 {
3892 if (bl->pspace != inf->pspace)
3893 continue;
3894
3895 /* This function must physically remove breakpoints locations
3896 from the specified ptid, without modifying the breakpoint
3897 package's state. Locations of type bp_loc_other and
3898 bp_loc_software_watchpoint are only maintained at GDB side,
3899 so there is no need to remove them. Moreover, removing these
3900 would modify the breakpoint package's state. */
3901 if (bl->loc_type == bp_loc_other
3902 || bl->loc_type == bp_loc_software_watchpoint)
3903 continue;
3904
3905 if (bl->inserted)
3906 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3907 }
3908
3909 return val;
3910 }
3911
3912 /* Remove the breakpoint location BL from the current address space.
3913 Note that this is used to detach breakpoints from a child fork.
3914 When we get here, the child isn't in the inferior list, and neither
3915 do we have objects to represent its address space --- we should
3916 *not* look at bl->pspace->aspace here. */
3917
3918 static int
3919 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3920 {
3921 int val;
3922
3923 /* BL is never in moribund_locations by our callers. */
3924 gdb_assert (bl->owner != NULL);
3925
3926 /* The type of none suggests that owner is actually deleted.
3927 This should not ever happen. */
3928 gdb_assert (bl->owner->type != bp_none);
3929
3930 if (bl->loc_type == bp_loc_software_breakpoint
3931 || bl->loc_type == bp_loc_hardware_breakpoint)
3932 {
3933 /* "Normal" instruction breakpoint: either the standard
3934 trap-instruction bp (bp_breakpoint), or a
3935 bp_hardware_breakpoint. */
3936
3937 /* First check to see if we have to handle an overlay. */
3938 if (overlay_debugging == ovly_off
3939 || bl->section == NULL
3940 || !(section_is_overlay (bl->section)))
3941 {
3942 /* No overlay handling: just remove the breakpoint. */
3943
3944 /* If we're trying to uninsert a memory breakpoint that we
3945 know is set in a dynamic object that is marked
3946 shlib_disabled, then either the dynamic object was
3947 removed with "remove-symbol-file" or with
3948 "nosharedlibrary". In the former case, we don't know
3949 whether another dynamic object might have loaded over the
3950 breakpoint's address -- the user might well let us know
3951 about it next with add-symbol-file (the whole point of
3952 add-symbol-file is letting the user manually maintain a
3953 list of dynamically loaded objects). If we have the
3954 breakpoint's shadow memory, that is, this is a software
3955 breakpoint managed by GDB, check whether the breakpoint
3956 is still inserted in memory, to avoid overwriting wrong
3957 code with stale saved shadow contents. Note that HW
3958 breakpoints don't have shadow memory, as they're
3959 implemented using a mechanism that is not dependent on
3960 being able to modify the target's memory, and as such
3961 they should always be removed. */
3962 if (bl->shlib_disabled
3963 && bl->target_info.shadow_len != 0
3964 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3965 val = 0;
3966 else
3967 val = bl->owner->remove_location (bl, reason);
3968 }
3969 else
3970 {
3971 /* This breakpoint is in an overlay section.
3972 Did we set a breakpoint at the LMA? */
3973 if (!overlay_events_enabled)
3974 {
3975 /* Yes -- overlay event support is not active, so we
3976 should have set a breakpoint at the LMA. Remove it.
3977 */
3978 /* Ignore any failures: if the LMA is in ROM, we will
3979 have already warned when we failed to insert it. */
3980 if (bl->loc_type == bp_loc_hardware_breakpoint)
3981 target_remove_hw_breakpoint (bl->gdbarch,
3982 &bl->overlay_target_info);
3983 else
3984 target_remove_breakpoint (bl->gdbarch,
3985 &bl->overlay_target_info,
3986 reason);
3987 }
3988 /* Did we set a breakpoint at the VMA?
3989 If so, we will have marked the breakpoint 'inserted'. */
3990 if (bl->inserted)
3991 {
3992 /* Yes -- remove it. Previously we did not bother to
3993 remove the breakpoint if the section had been
3994 unmapped, but let's not rely on that being safe. We
3995 don't know what the overlay manager might do. */
3996
3997 /* However, we should remove *software* breakpoints only
3998 if the section is still mapped, or else we overwrite
3999 wrong code with the saved shadow contents. */
4000 if (bl->loc_type == bp_loc_hardware_breakpoint
4001 || section_is_mapped (bl->section))
4002 val = bl->owner->remove_location (bl, reason);
4003 else
4004 val = 0;
4005 }
4006 else
4007 {
4008 /* No -- not inserted, so no need to remove. No error. */
4009 val = 0;
4010 }
4011 }
4012
4013 /* In some cases, we might not be able to remove a breakpoint in
4014 a shared library that has already been removed, but we have
4015 not yet processed the shlib unload event. Similarly for an
4016 unloaded add-symbol-file object - the user might not yet have
4017 had the chance to remove-symbol-file it. shlib_disabled will
4018 be set if the library/object has already been removed, but
4019 the breakpoint hasn't been uninserted yet, e.g., after
4020 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4021 always-inserted mode. */
4022 if (val
4023 && (bl->loc_type == bp_loc_software_breakpoint
4024 && (bl->shlib_disabled
4025 || solib_name_from_address (bl->pspace, bl->address)
4026 || shared_objfile_contains_address_p (bl->pspace,
4027 bl->address))))
4028 val = 0;
4029
4030 if (val)
4031 return val;
4032 bl->inserted = (reason == DETACH_BREAKPOINT);
4033 }
4034 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4035 {
4036 bl->inserted = (reason == DETACH_BREAKPOINT);
4037 bl->owner->remove_location (bl, reason);
4038
4039 /* Failure to remove any of the hardware watchpoints comes here. */
4040 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4041 warning (_("Could not remove hardware watchpoint %d."),
4042 bl->owner->number);
4043 }
4044 else if (bl->owner->type == bp_catchpoint
4045 && breakpoint_enabled (bl->owner)
4046 && !bl->duplicate)
4047 {
4048 val = bl->owner->remove_location (bl, reason);
4049 if (val)
4050 return val;
4051
4052 bl->inserted = (reason == DETACH_BREAKPOINT);
4053 }
4054
4055 return 0;
4056 }
4057
4058 static int
4059 remove_breakpoint (struct bp_location *bl)
4060 {
4061 /* BL is never in moribund_locations by our callers. */
4062 gdb_assert (bl->owner != NULL);
4063
4064 /* The type of none suggests that owner is actually deleted.
4065 This should not ever happen. */
4066 gdb_assert (bl->owner->type != bp_none);
4067
4068 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4069
4070 switch_to_program_space_and_thread (bl->pspace);
4071
4072 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4073 }
4074
4075 /* Clear the "inserted" flag in all breakpoints. */
4076
4077 void
4078 mark_breakpoints_out (void)
4079 {
4080 for (bp_location *bl : all_bp_locations ())
4081 if (bl->pspace == current_program_space)
4082 bl->inserted = 0;
4083 }
4084
4085 /* Clear the "inserted" flag in all breakpoints and delete any
4086 breakpoints which should go away between runs of the program.
4087
4088 Plus other such housekeeping that has to be done for breakpoints
4089 between runs.
4090
4091 Note: this function gets called at the end of a run (by
4092 generic_mourn_inferior) and when a run begins (by
4093 init_wait_for_inferior). */
4094
4095
4096
4097 void
4098 breakpoint_init_inferior (enum inf_context context)
4099 {
4100 struct program_space *pspace = current_program_space;
4101
4102 /* If breakpoint locations are shared across processes, then there's
4103 nothing to do. */
4104 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4105 return;
4106
4107 mark_breakpoints_out ();
4108
4109 for (breakpoint *b : all_breakpoints_safe ())
4110 {
4111 if (b->loc && b->loc->pspace != pspace)
4112 continue;
4113
4114 switch (b->type)
4115 {
4116 case bp_call_dummy:
4117 case bp_longjmp_call_dummy:
4118
4119 /* If the call dummy breakpoint is at the entry point it will
4120 cause problems when the inferior is rerun, so we better get
4121 rid of it. */
4122
4123 case bp_watchpoint_scope:
4124
4125 /* Also get rid of scope breakpoints. */
4126
4127 case bp_shlib_event:
4128
4129 /* Also remove solib event breakpoints. Their addresses may
4130 have changed since the last time we ran the program.
4131 Actually we may now be debugging against different target;
4132 and so the solib backend that installed this breakpoint may
4133 not be used in by the target. E.g.,
4134
4135 (gdb) file prog-linux
4136 (gdb) run # native linux target
4137 ...
4138 (gdb) kill
4139 (gdb) file prog-win.exe
4140 (gdb) tar rem :9999 # remote Windows gdbserver.
4141 */
4142
4143 case bp_step_resume:
4144
4145 /* Also remove step-resume breakpoints. */
4146
4147 case bp_single_step:
4148
4149 /* Also remove single-step breakpoints. */
4150
4151 delete_breakpoint (b);
4152 break;
4153
4154 case bp_watchpoint:
4155 case bp_hardware_watchpoint:
4156 case bp_read_watchpoint:
4157 case bp_access_watchpoint:
4158 {
4159 struct watchpoint *w = (struct watchpoint *) b;
4160
4161 /* Likewise for watchpoints on local expressions. */
4162 if (w->exp_valid_block != NULL)
4163 delete_breakpoint (b);
4164 else
4165 {
4166 /* Get rid of existing locations, which are no longer
4167 valid. New ones will be created in
4168 update_watchpoint, when the inferior is restarted.
4169 The next update_global_location_list call will
4170 garbage collect them. */
4171 b->loc = NULL;
4172
4173 if (context == inf_starting)
4174 {
4175 /* Reset val field to force reread of starting value in
4176 insert_breakpoints. */
4177 w->val.reset (nullptr);
4178 w->val_valid = false;
4179 }
4180 }
4181 }
4182 break;
4183 default:
4184 break;
4185 }
4186 }
4187
4188 /* Get rid of the moribund locations. */
4189 for (bp_location *bl : moribund_locations)
4190 decref_bp_location (&bl);
4191 moribund_locations.clear ();
4192 }
4193
4194 /* These functions concern about actual breakpoints inserted in the
4195 target --- to e.g. check if we need to do decr_pc adjustment or if
4196 we need to hop over the bkpt --- so we check for address space
4197 match, not program space. */
4198
4199 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4200 exists at PC. It returns ordinary_breakpoint_here if it's an
4201 ordinary breakpoint, or permanent_breakpoint_here if it's a
4202 permanent breakpoint.
4203 - When continuing from a location with an ordinary breakpoint, we
4204 actually single step once before calling insert_breakpoints.
4205 - When continuing from a location with a permanent breakpoint, we
4206 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4207 the target, to advance the PC past the breakpoint. */
4208
4209 enum breakpoint_here
4210 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4211 {
4212 bool any_breakpoint_here = false;
4213
4214 for (bp_location *bl : all_bp_locations ())
4215 {
4216 if (bl->loc_type != bp_loc_software_breakpoint
4217 && bl->loc_type != bp_loc_hardware_breakpoint)
4218 continue;
4219
4220 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4221 if ((breakpoint_enabled (bl->owner)
4222 || bl->permanent)
4223 && breakpoint_location_address_match (bl, aspace, pc))
4224 {
4225 if (overlay_debugging
4226 && section_is_overlay (bl->section)
4227 && !section_is_mapped (bl->section))
4228 continue; /* unmapped overlay -- can't be a match */
4229 else if (bl->permanent)
4230 return permanent_breakpoint_here;
4231 else
4232 any_breakpoint_here = true;
4233 }
4234 }
4235
4236 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4237 }
4238
4239 /* See breakpoint.h. */
4240
4241 int
4242 breakpoint_in_range_p (const address_space *aspace,
4243 CORE_ADDR addr, ULONGEST len)
4244 {
4245 for (bp_location *bl : all_bp_locations ())
4246 {
4247 if (bl->loc_type != bp_loc_software_breakpoint
4248 && bl->loc_type != bp_loc_hardware_breakpoint)
4249 continue;
4250
4251 if ((breakpoint_enabled (bl->owner)
4252 || bl->permanent)
4253 && breakpoint_location_address_range_overlap (bl, aspace,
4254 addr, len))
4255 {
4256 if (overlay_debugging
4257 && section_is_overlay (bl->section)
4258 && !section_is_mapped (bl->section))
4259 {
4260 /* Unmapped overlay -- can't be a match. */
4261 continue;
4262 }
4263
4264 return 1;
4265 }
4266 }
4267
4268 return 0;
4269 }
4270
4271 /* Return true if there's a moribund breakpoint at PC. */
4272
4273 int
4274 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4275 {
4276 for (bp_location *loc : moribund_locations)
4277 if (breakpoint_location_address_match (loc, aspace, pc))
4278 return 1;
4279
4280 return 0;
4281 }
4282
4283 /* Returns true iff BL is inserted at PC, in address space ASPACE. */
4284
4285 static bool
4286 bp_location_inserted_here_p (const struct bp_location *bl,
4287 const address_space *aspace, CORE_ADDR pc)
4288 {
4289 if (bl->inserted
4290 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4291 aspace, pc))
4292 {
4293 /* An unmapped overlay can't be a match. */
4294 return !(overlay_debugging
4295 && section_is_overlay (bl->section)
4296 && !section_is_mapped (bl->section));
4297 }
4298 return false;
4299 }
4300
4301 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4302
4303 int
4304 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4305 {
4306 for (bp_location *bl : all_bp_locations_at_addr (pc))
4307 {
4308 if (bl->loc_type != bp_loc_software_breakpoint
4309 && bl->loc_type != bp_loc_hardware_breakpoint)
4310 continue;
4311
4312 if (bp_location_inserted_here_p (bl, aspace, pc))
4313 return 1;
4314 }
4315 return 0;
4316 }
4317
4318 /* This function returns non-zero iff there is a software breakpoint
4319 inserted at PC. */
4320
4321 int
4322 software_breakpoint_inserted_here_p (const address_space *aspace,
4323 CORE_ADDR pc)
4324 {
4325 for (bp_location *bl : all_bp_locations_at_addr (pc))
4326 {
4327 if (bl->loc_type != bp_loc_software_breakpoint)
4328 continue;
4329
4330 if (bp_location_inserted_here_p (bl, aspace, pc))
4331 return 1;
4332 }
4333
4334 return 0;
4335 }
4336
4337 /* See breakpoint.h. */
4338
4339 int
4340 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4341 CORE_ADDR pc)
4342 {
4343 for (bp_location *bl : all_bp_locations_at_addr (pc))
4344 {
4345 if (bl->loc_type != bp_loc_hardware_breakpoint)
4346 continue;
4347
4348 if (bp_location_inserted_here_p (bl, aspace, pc))
4349 return 1;
4350 }
4351
4352 return 0;
4353 }
4354
4355 int
4356 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4357 CORE_ADDR addr, ULONGEST len)
4358 {
4359 for (breakpoint *bpt : all_breakpoints ())
4360 {
4361 if (bpt->type != bp_hardware_watchpoint
4362 && bpt->type != bp_access_watchpoint)
4363 continue;
4364
4365 if (!breakpoint_enabled (bpt))
4366 continue;
4367
4368 for (bp_location *loc : bpt->locations ())
4369 if (loc->pspace->aspace == aspace && loc->inserted)
4370 {
4371 CORE_ADDR l, h;
4372
4373 /* Check for intersection. */
4374 l = std::max<CORE_ADDR> (loc->address, addr);
4375 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4376 if (l < h)
4377 return 1;
4378 }
4379 }
4380 return 0;
4381 }
4382
4383 /* See breakpoint.h. */
4384
4385 bool
4386 is_catchpoint (struct breakpoint *b)
4387 {
4388 return (b->type == bp_catchpoint);
4389 }
4390
4391 /* Clear a bpstat so that it says we are not at any breakpoint.
4392 Also free any storage that is part of a bpstat. */
4393
4394 void
4395 bpstat_clear (bpstat **bsp)
4396 {
4397 bpstat *p;
4398 bpstat *q;
4399
4400 if (bsp == 0)
4401 return;
4402 p = *bsp;
4403 while (p != NULL)
4404 {
4405 q = p->next;
4406 delete p;
4407 p = q;
4408 }
4409 *bsp = NULL;
4410 }
4411
4412 bpstat::bpstat (const bpstat &other)
4413 : next (NULL),
4414 bp_location_at (other.bp_location_at),
4415 breakpoint_at (other.breakpoint_at),
4416 commands (other.commands),
4417 print (other.print),
4418 stop (other.stop),
4419 print_it (other.print_it)
4420 {
4421 if (other.old_val != NULL)
4422 old_val = release_value (other.old_val->copy ());
4423 }
4424
4425 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4426 is part of the bpstat is copied as well. */
4427
4428 bpstat *
4429 bpstat_copy (bpstat *bs)
4430 {
4431 bpstat *p = nullptr;
4432 bpstat *tmp;
4433 bpstat *retval = nullptr;
4434
4435 if (bs == NULL)
4436 return bs;
4437
4438 for (; bs != NULL; bs = bs->next)
4439 {
4440 tmp = new bpstat (*bs);
4441
4442 if (p == NULL)
4443 /* This is the first thing in the chain. */
4444 retval = tmp;
4445 else
4446 p->next = tmp;
4447 p = tmp;
4448 }
4449 p->next = NULL;
4450 return retval;
4451 }
4452
4453 /* Find the bpstat associated with this breakpoint. */
4454
4455 bpstat *
4456 bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
4457 {
4458 if (bsp == NULL)
4459 return NULL;
4460
4461 for (; bsp != NULL; bsp = bsp->next)
4462 {
4463 if (bsp->breakpoint_at == breakpoint)
4464 return bsp;
4465 }
4466 return NULL;
4467 }
4468
4469 /* See breakpoint.h. */
4470
4471 bool
4472 bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
4473 {
4474 for (; bsp != NULL; bsp = bsp->next)
4475 {
4476 if (bsp->breakpoint_at == NULL)
4477 {
4478 /* A moribund location can never explain a signal other than
4479 GDB_SIGNAL_TRAP. */
4480 if (sig == GDB_SIGNAL_TRAP)
4481 return true;
4482 }
4483 else
4484 {
4485 if (bsp->breakpoint_at->explains_signal (sig))
4486 return true;
4487 }
4488 }
4489
4490 return false;
4491 }
4492
4493 /* See breakpoint.h. */
4494
4495 int
4496 bpstat_num (bpstat **bsp, int *num)
4497 {
4498 struct breakpoint *b;
4499
4500 if ((*bsp) == NULL)
4501 return 0; /* No more breakpoint values */
4502
4503 /* We assume we'll never have several bpstats that correspond to a
4504 single breakpoint -- otherwise, this function might return the
4505 same number more than once and this will look ugly. */
4506 b = (*bsp)->breakpoint_at;
4507 *bsp = (*bsp)->next;
4508 if (b == NULL)
4509 return -1; /* breakpoint that's been deleted since */
4510
4511 *num = b->number; /* We have its number */
4512 return 1;
4513 }
4514
4515 /* See breakpoint.h */
4516
4517 int
4518 bpstat_locno (const bpstat *bs)
4519 {
4520 const struct breakpoint *b = bs->breakpoint_at;
4521 const struct bp_location *bl = bs->bp_location_at.get ();
4522
4523 int locno = 0;
4524
4525 if (b != nullptr && b->loc != nullptr && b->loc->next != nullptr)
4526 {
4527 const bp_location *bl_i;
4528
4529 for (bl_i = b->loc;
4530 bl_i != bl && bl_i->next != nullptr;
4531 bl_i = bl_i->next)
4532 locno++;
4533
4534 if (bl_i == bl)
4535 locno++;
4536 else
4537 {
4538 warning (_("location number not found for breakpoint %d address %s."),
4539 b->number, paddress (bl->gdbarch, bl->address));
4540 locno = 0;
4541 }
4542 }
4543
4544 return locno;
4545 }
4546
4547 /* See breakpoint.h. */
4548
4549 void
4550 print_num_locno (const bpstat *bs, struct ui_out *uiout)
4551 {
4552 struct breakpoint *b = bs->breakpoint_at;
4553
4554 if (b == nullptr)
4555 uiout->text (_("deleted breakpoint"));
4556 else
4557 {
4558 uiout->field_signed ("bkptno", b->number);
4559
4560 int locno = bpstat_locno (bs);
4561 if (locno != 0)
4562 uiout->message (".%pF", signed_field ("locno", locno));
4563 }
4564 }
4565
4566 /* See breakpoint.h. */
4567
4568 void
4569 bpstat_clear_actions (void)
4570 {
4571 bpstat *bs;
4572
4573 if (inferior_ptid == null_ptid)
4574 return;
4575
4576 thread_info *tp = inferior_thread ();
4577 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4578 {
4579 bs->commands = NULL;
4580 bs->old_val.reset (nullptr);
4581 }
4582 }
4583
4584 /* Called when a command is about to proceed the inferior. */
4585
4586 static void
4587 breakpoint_about_to_proceed (void)
4588 {
4589 if (inferior_ptid != null_ptid)
4590 {
4591 struct thread_info *tp = inferior_thread ();
4592
4593 /* Allow inferior function calls in breakpoint commands to not
4594 interrupt the command list. When the call finishes
4595 successfully, the inferior will be standing at the same
4596 breakpoint as if nothing happened. */
4597 if (tp->control.in_infcall)
4598 return;
4599 }
4600
4601 breakpoint_proceeded = 1;
4602 }
4603
4604 /* Return true iff CMD as the first line of a command sequence is `silent'
4605 or its equivalent. */
4606
4607 static bool
4608 command_line_is_silent (struct command_line *cmd)
4609 {
4610 return cmd && (strcmp ("silent", cmd->line) == 0);
4611 }
4612
4613 /* Sets the $_hit_bpnum and $_hit_locno to bpnum and locno.
4614 A locno 0 is changed to 1 to e.g. let the user do
4615 (gdb) disable $_hit_bpnum.$_hit_locno
4616 for a single location breakpoint. */
4617
4618 static void
4619 set_hit_convenience_vars (int bpnum, int locno)
4620 {
4621 set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), bpnum);
4622 set_internalvar_integer (lookup_internalvar ("_hit_locno"),
4623 (locno > 0 ? locno : 1));
4624 }
4625
4626 /* Execute all the commands associated with all the breakpoints at
4627 this location. Any of these commands could cause the process to
4628 proceed beyond this point, etc. We look out for such changes by
4629 checking the global "breakpoint_proceeded" after each command.
4630
4631 Returns true if a breakpoint command resumed the inferior. In that
4632 case, it is the caller's responsibility to recall it again with the
4633 bpstat of the current thread. */
4634
4635 static bool
4636 bpstat_do_actions_1 (bpstat **bsp)
4637 {
4638 bpstat *bs;
4639 bool again = false;
4640
4641 /* Avoid endless recursion if a `source' command is contained
4642 in bs->commands. */
4643 if (executing_breakpoint_commands)
4644 return false;
4645
4646 scoped_restore save_executing
4647 = make_scoped_restore (&executing_breakpoint_commands, 1);
4648
4649 scoped_restore preventer = prevent_dont_repeat ();
4650
4651 /* This pointer will iterate over the list of bpstat's. */
4652 bs = *bsp;
4653
4654 /* The $_hit_* convenience variables are set before running the
4655 commands of BS. In case we have several bs, after the loop,
4656 we set again the variables to the first printed bpnum and locno.
4657 For multiple breakpoints, this ensures the variables are set to the
4658 breakpoint printed for the user. */
4659 int printed_hit_bpnum = -1;
4660 int printed_hit_locno = -1;
4661
4662 breakpoint_proceeded = 0;
4663 for (; bs != NULL; bs = bs->next)
4664 {
4665 struct command_line *cmd = NULL;
4666
4667 /* Set the _hit_* convenience variables before running BS's commands. */
4668 {
4669 const struct breakpoint *b = bs->breakpoint_at;
4670 if (b != nullptr)
4671 {
4672 int locno = bpstat_locno (bs);
4673
4674 set_hit_convenience_vars (b->number, locno);
4675 if (printed_hit_locno == -1 && bs->print)
4676 {
4677 printed_hit_bpnum = b->number;
4678 printed_hit_locno = locno;
4679 }
4680 }
4681 }
4682
4683 /* Take ownership of the BSP's command tree, if it has one.
4684
4685 The command tree could legitimately contain commands like
4686 'step' and 'next', which call clear_proceed_status, which
4687 frees the bpstat BS and its command tree. To make sure this doesn't
4688 free the tree we're executing out from under us, we need to
4689 take ownership of the tree ourselves. Since a given bpstat's
4690 commands are only executed once, we don't need to copy it; we
4691 can clear the pointer in the bpstat, and make sure we free
4692 the tree when we're done. */
4693 counted_command_line ccmd = bs->commands;
4694 bs->commands = NULL;
4695 if (ccmd != NULL)
4696 cmd = ccmd.get ();
4697 if (command_line_is_silent (cmd))
4698 {
4699 /* The action has been already done by bpstat_stop_status. */
4700 cmd = cmd->next;
4701 }
4702
4703 while (cmd != NULL)
4704 {
4705 execute_control_command (cmd);
4706 /* After execute_control_command, if breakpoint_proceeded is true,
4707 BS has been freed and cannot be accessed anymore. */
4708
4709 if (breakpoint_proceeded)
4710 break;
4711 else
4712 cmd = cmd->next;
4713 }
4714
4715 if (breakpoint_proceeded)
4716 {
4717 if (current_ui->async)
4718 /* If we are in async mode, then the target might be still
4719 running, not stopped at any breakpoint, so nothing for
4720 us to do here -- just return to the event loop. */
4721 ;
4722 else
4723 /* In sync mode, when execute_control_command returns
4724 we're already standing on the next breakpoint.
4725 Breakpoint commands for that stop were not run, since
4726 execute_command does not run breakpoint commands --
4727 only command_line_handler does, but that one is not
4728 involved in execution of breakpoint commands. So, we
4729 can now execute breakpoint commands. It should be
4730 noted that making execute_command do bpstat actions is
4731 not an option -- in this case we'll have recursive
4732 invocation of bpstat for each breakpoint with a
4733 command, and can easily blow up GDB stack. Instead, we
4734 return true, which will trigger the caller to recall us
4735 with the new stop_bpstat. */
4736 again = true;
4737 break;
4738 }
4739 }
4740
4741 /* Now that we have executed the commands of all bs, set the _hit_*
4742 convenience variables to the printed values. */
4743 if (printed_hit_locno != -1)
4744 set_hit_convenience_vars (printed_hit_bpnum, printed_hit_locno);
4745
4746 return again;
4747 }
4748
4749 /* Helper for bpstat_do_actions. Get the current thread, if there's
4750 one, is alive and has execution. Return NULL otherwise. */
4751
4752 static thread_info *
4753 get_bpstat_thread ()
4754 {
4755 if (inferior_ptid == null_ptid || !target_has_execution ())
4756 return NULL;
4757
4758 thread_info *tp = inferior_thread ();
4759 if (tp->state == THREAD_EXITED || tp->executing ())
4760 return NULL;
4761 return tp;
4762 }
4763
4764 void
4765 bpstat_do_actions (void)
4766 {
4767 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4768 thread_info *tp;
4769
4770 /* Do any commands attached to breakpoint we are stopped at. */
4771 while ((tp = get_bpstat_thread ()) != NULL)
4772 {
4773 /* Since in sync mode, bpstat_do_actions may resume the
4774 inferior, and only return when it is stopped at the next
4775 breakpoint, we keep doing breakpoint actions until it returns
4776 false to indicate the inferior was not resumed. */
4777 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4778 break;
4779 }
4780
4781 cleanup_if_error.release ();
4782 }
4783
4784 /* Print out the (old or new) value associated with a watchpoint. */
4785
4786 static void
4787 watchpoint_value_print (struct value *val, struct ui_file *stream)
4788 {
4789 if (val == NULL)
4790 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4791 else
4792 {
4793 struct value_print_options opts;
4794 get_user_print_options (&opts);
4795 value_print (val, stream, &opts);
4796 }
4797 }
4798
4799 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4800 debugging multiple threads. */
4801
4802 void
4803 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4804 {
4805 if (uiout->is_mi_like_p ())
4806 return;
4807
4808 uiout->text ("\n");
4809
4810 if (show_thread_that_caused_stop ())
4811 {
4812 struct thread_info *thr = inferior_thread ();
4813
4814 uiout->text ("Thread ");
4815 uiout->field_string ("thread-id", print_thread_id (thr));
4816
4817 const char *name = thread_name (thr);
4818 if (name != NULL)
4819 {
4820 uiout->text (" \"");
4821 uiout->field_string ("name", name);
4822 uiout->text ("\"");
4823 }
4824
4825 uiout->text (" hit ");
4826 }
4827 }
4828
4829 /* Generic routine for printing messages indicating why we
4830 stopped. The behavior of this function depends on the value
4831 'print_it' in the bpstat structure. Under some circumstances we
4832 may decide not to print anything here and delegate the task to
4833 normal_stop(). */
4834
4835 static enum print_stop_action
4836 print_bp_stop_message (bpstat *bs)
4837 {
4838 switch (bs->print_it)
4839 {
4840 case print_it_noop:
4841 /* Nothing should be printed for this bpstat entry. */
4842 return PRINT_UNKNOWN;
4843 break;
4844
4845 case print_it_done:
4846 /* We still want to print the frame, but we already printed the
4847 relevant messages. */
4848 return PRINT_SRC_AND_LOC;
4849 break;
4850
4851 case print_it_normal:
4852 {
4853 struct breakpoint *b = bs->breakpoint_at;
4854
4855 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4856 which has since been deleted. */
4857 if (b == NULL)
4858 return PRINT_UNKNOWN;
4859
4860 /* Normal case. Call the breakpoint's print_it method. */
4861 return b->print_it (bs);
4862 }
4863 break;
4864
4865 default:
4866 internal_error (_("print_bp_stop_message: unrecognized enum value"));
4867 break;
4868 }
4869 }
4870
4871 /* See breakpoint.h. */
4872
4873 void
4874 print_solib_event (bool is_catchpoint)
4875 {
4876 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4877 bool any_added = !current_program_space->added_solibs.empty ();
4878
4879 if (!is_catchpoint)
4880 {
4881 if (any_added || any_deleted)
4882 current_uiout->text (_("Stopped due to shared library event:\n"));
4883 else
4884 current_uiout->text (_("Stopped due to shared library event (no "
4885 "libraries added or removed)\n"));
4886 }
4887
4888 if (current_uiout->is_mi_like_p ())
4889 current_uiout->field_string ("reason",
4890 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4891
4892 if (any_deleted)
4893 {
4894 current_uiout->text (_(" Inferior unloaded "));
4895 ui_out_emit_list list_emitter (current_uiout, "removed");
4896 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4897 {
4898 const std::string &name = current_program_space->deleted_solibs[ix];
4899
4900 if (ix > 0)
4901 current_uiout->text (" ");
4902 current_uiout->field_string ("library", name);
4903 current_uiout->text ("\n");
4904 }
4905 }
4906
4907 if (any_added)
4908 {
4909 current_uiout->text (_(" Inferior loaded "));
4910 ui_out_emit_list list_emitter (current_uiout, "added");
4911 bool first = true;
4912 for (so_list *iter : current_program_space->added_solibs)
4913 {
4914 if (!first)
4915 current_uiout->text (" ");
4916 first = false;
4917 current_uiout->field_string ("library", iter->so_name);
4918 current_uiout->text ("\n");
4919 }
4920 }
4921 }
4922
4923 /* Print a message indicating what happened. This is called from
4924 normal_stop(). The input to this routine is the head of the bpstat
4925 list - a list of the eventpoints that caused this stop. KIND is
4926 the target_waitkind for the stopping event. This
4927 routine calls the generic print routine for printing a message
4928 about reasons for stopping. This will print (for example) the
4929 "Breakpoint n," part of the output. The return value of this
4930 routine is one of:
4931
4932 PRINT_UNKNOWN: Means we printed nothing.
4933 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4934 code to print the location. An example is
4935 "Breakpoint 1, " which should be followed by
4936 the location.
4937 PRINT_SRC_ONLY: Means we printed something, but there is no need
4938 to also print the location part of the message.
4939 An example is the catch/throw messages, which
4940 don't require a location appended to the end.
4941 PRINT_NOTHING: We have done some printing and we don't need any
4942 further info to be printed. */
4943
4944 enum print_stop_action
4945 bpstat_print (bpstat *bs, target_waitkind kind)
4946 {
4947 enum print_stop_action val;
4948
4949 /* Maybe another breakpoint in the chain caused us to stop.
4950 (Currently all watchpoints go on the bpstat whether hit or not.
4951 That probably could (should) be changed, provided care is taken
4952 with respect to bpstat_explains_signal). */
4953 for (; bs; bs = bs->next)
4954 {
4955 val = print_bp_stop_message (bs);
4956 if (val == PRINT_SRC_ONLY
4957 || val == PRINT_SRC_AND_LOC
4958 || val == PRINT_NOTHING)
4959 return val;
4960 }
4961
4962 /* If we had hit a shared library event breakpoint,
4963 print_bp_stop_message would print out this message. If we hit an
4964 OS-level shared library event, do the same thing. */
4965 if (kind == TARGET_WAITKIND_LOADED)
4966 {
4967 print_solib_event (false);
4968 return PRINT_NOTHING;
4969 }
4970
4971 /* We reached the end of the chain, or we got a null BS to start
4972 with and nothing was printed. */
4973 return PRINT_UNKNOWN;
4974 }
4975
4976 /* Evaluate the boolean expression EXP and return the result. */
4977
4978 static bool
4979 breakpoint_cond_eval (expression *exp)
4980 {
4981 scoped_value_mark mark;
4982 return value_true (exp->evaluate ());
4983 }
4984
4985 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4986
4987 bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
4988 : next (NULL),
4989 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4990 breakpoint_at (bl->owner),
4991 commands (NULL),
4992 print (0),
4993 stop (0),
4994 print_it (print_it_normal)
4995 {
4996 **bs_link_pointer = this;
4997 *bs_link_pointer = &next;
4998 }
4999
5000 bpstat::bpstat ()
5001 : next (NULL),
5002 breakpoint_at (NULL),
5003 commands (NULL),
5004 print (0),
5005 stop (0),
5006 print_it (print_it_normal)
5007 {
5008 }
5009 \f
5010 /* The target has stopped with waitstatus WS. Check if any hardware
5011 watchpoints have triggered, according to the target. */
5012
5013 int
5014 watchpoints_triggered (const target_waitstatus &ws)
5015 {
5016 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
5017 CORE_ADDR addr;
5018
5019 if (!stopped_by_watchpoint)
5020 {
5021 /* We were not stopped by a watchpoint. Mark all watchpoints
5022 as not triggered. */
5023 for (breakpoint *b : all_breakpoints ())
5024 if (is_hardware_watchpoint (b))
5025 {
5026 struct watchpoint *w = (struct watchpoint *) b;
5027
5028 w->watchpoint_triggered = watch_triggered_no;
5029 }
5030
5031 return 0;
5032 }
5033
5034 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
5035 {
5036 /* We were stopped by a watchpoint, but we don't know where.
5037 Mark all watchpoints as unknown. */
5038 for (breakpoint *b : all_breakpoints ())
5039 if (is_hardware_watchpoint (b))
5040 {
5041 struct watchpoint *w = (struct watchpoint *) b;
5042
5043 w->watchpoint_triggered = watch_triggered_unknown;
5044 }
5045
5046 return 1;
5047 }
5048
5049 /* The target could report the data address. Mark watchpoints
5050 affected by this data address as triggered, and all others as not
5051 triggered. */
5052
5053 for (breakpoint *b : all_breakpoints ())
5054 if (is_hardware_watchpoint (b))
5055 {
5056 struct watchpoint *w = (struct watchpoint *) b;
5057
5058 w->watchpoint_triggered = watch_triggered_no;
5059 for (bp_location *loc : b->locations ())
5060 {
5061 if (is_masked_watchpoint (b))
5062 {
5063 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5064 CORE_ADDR start = loc->address & w->hw_wp_mask;
5065
5066 if (newaddr == start)
5067 {
5068 w->watchpoint_triggered = watch_triggered_yes;
5069 break;
5070 }
5071 }
5072 /* Exact match not required. Within range is sufficient. */
5073 else if (target_watchpoint_addr_within_range
5074 (current_inferior ()->top_target (), addr, loc->address,
5075 loc->length))
5076 {
5077 w->watchpoint_triggered = watch_triggered_yes;
5078 break;
5079 }
5080 }
5081 }
5082
5083 return 1;
5084 }
5085
5086 /* Possible return values for watchpoint_check. */
5087 enum wp_check_result
5088 {
5089 /* The watchpoint has been deleted. */
5090 WP_DELETED = 1,
5091
5092 /* The value has changed. */
5093 WP_VALUE_CHANGED = 2,
5094
5095 /* The value has not changed. */
5096 WP_VALUE_NOT_CHANGED = 3,
5097
5098 /* Ignore this watchpoint, no matter if the value changed or not. */
5099 WP_IGNORE = 4,
5100 };
5101
5102 #define BP_TEMPFLAG 1
5103 #define BP_HARDWAREFLAG 2
5104
5105 /* Evaluate watchpoint condition expression and check if its value
5106 changed. */
5107
5108 static wp_check_result
5109 watchpoint_check (bpstat *bs)
5110 {
5111 struct watchpoint *b;
5112 frame_info_ptr fr;
5113 bool within_current_scope;
5114
5115 /* BS is built from an existing struct breakpoint. */
5116 gdb_assert (bs->breakpoint_at != NULL);
5117 b = (struct watchpoint *) bs->breakpoint_at;
5118
5119 /* If this is a local watchpoint, we only want to check if the
5120 watchpoint frame is in scope if the current thread is the thread
5121 that was used to create the watchpoint. */
5122 if (!watchpoint_in_thread_scope (b))
5123 return WP_IGNORE;
5124
5125 if (b->exp_valid_block == NULL)
5126 within_current_scope = true;
5127 else
5128 {
5129 frame_info_ptr frame = get_current_frame ();
5130 struct gdbarch *frame_arch = get_frame_arch (frame);
5131 CORE_ADDR frame_pc = get_frame_pc (frame);
5132
5133 /* stack_frame_destroyed_p() returns a non-zero value if we're
5134 still in the function but the stack frame has already been
5135 invalidated. Since we can't rely on the values of local
5136 variables after the stack has been destroyed, we are treating
5137 the watchpoint in that state as `not changed' without further
5138 checking. Don't mark watchpoints as changed if the current
5139 frame is in an epilogue - even if they are in some other
5140 frame, our view of the stack is likely to be wrong and
5141 frame_find_by_id could error out. */
5142 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5143 return WP_IGNORE;
5144
5145 fr = frame_find_by_id (b->watchpoint_frame);
5146 within_current_scope = (fr != NULL);
5147
5148 /* If we've gotten confused in the unwinder, we might have
5149 returned a frame that can't describe this variable. */
5150 if (within_current_scope)
5151 {
5152 struct symbol *function;
5153
5154 function = get_frame_function (fr);
5155 if (function == NULL
5156 || !function->value_block ()->contains (b->exp_valid_block))
5157 within_current_scope = false;
5158 }
5159
5160 if (within_current_scope)
5161 /* If we end up stopping, the current frame will get selected
5162 in normal_stop. So this call to select_frame won't affect
5163 the user. */
5164 select_frame (fr);
5165 }
5166
5167 if (within_current_scope)
5168 {
5169 /* We use value_{,free_to_}mark because it could be a *long*
5170 time before we return to the command level and call
5171 free_all_values. We can't call free_all_values because we
5172 might be in the middle of evaluating a function call. */
5173
5174 struct value *mark;
5175 struct value *new_val;
5176
5177 if (is_masked_watchpoint (b))
5178 /* Since we don't know the exact trigger address (from
5179 stopped_data_address), just tell the user we've triggered
5180 a mask watchpoint. */
5181 return WP_VALUE_CHANGED;
5182
5183 mark = value_mark ();
5184 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5185 NULL, NULL, false);
5186
5187 if (b->val_bitsize != 0)
5188 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5189
5190 /* We use value_equal_contents instead of value_equal because
5191 the latter coerces an array to a pointer, thus comparing just
5192 the address of the array instead of its contents. This is
5193 not what we want. */
5194 if ((b->val != NULL) != (new_val != NULL)
5195 || (b->val != NULL && !value_equal_contents (b->val.get (),
5196 new_val)))
5197 {
5198 bs->old_val = b->val;
5199 b->val = release_value (new_val);
5200 b->val_valid = true;
5201 if (new_val != NULL)
5202 value_free_to_mark (mark);
5203 return WP_VALUE_CHANGED;
5204 }
5205 else
5206 {
5207 /* Nothing changed. */
5208 value_free_to_mark (mark);
5209 return WP_VALUE_NOT_CHANGED;
5210 }
5211 }
5212 else
5213 {
5214 /* This seems like the only logical thing to do because
5215 if we temporarily ignored the watchpoint, then when
5216 we reenter the block in which it is valid it contains
5217 garbage (in the case of a function, it may have two
5218 garbage values, one before and one after the prologue).
5219 So we can't even detect the first assignment to it and
5220 watch after that (since the garbage may or may not equal
5221 the first value assigned). */
5222 /* We print all the stop information in
5223 breakpointprint_it, but in this case, by the time we
5224 call breakpoint->print_it this bp will be deleted
5225 already. So we have no choice but print the information
5226 here. */
5227
5228 SWITCH_THRU_ALL_UIS ()
5229 {
5230 struct ui_out *uiout = current_uiout;
5231
5232 if (uiout->is_mi_like_p ())
5233 uiout->field_string
5234 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5235 uiout->message ("\nWatchpoint %pF deleted because the program has "
5236 "left the block in\n"
5237 "which its expression is valid.\n",
5238 signed_field ("wpnum", b->number));
5239 }
5240
5241 /* Make sure the watchpoint's commands aren't executed. */
5242 b->commands = NULL;
5243 watchpoint_del_at_next_stop (b);
5244
5245 return WP_DELETED;
5246 }
5247 }
5248
5249 /* Return true if it looks like target has stopped due to hitting
5250 breakpoint location BL. This function does not check if we should
5251 stop, only if BL explains the stop. */
5252
5253 static bool
5254 bpstat_check_location (const struct bp_location *bl,
5255 const address_space *aspace, CORE_ADDR bp_addr,
5256 const target_waitstatus &ws)
5257 {
5258 struct breakpoint *b = bl->owner;
5259
5260 /* BL is from an existing breakpoint. */
5261 gdb_assert (b != NULL);
5262
5263 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
5264 }
5265
5266 /* Determine if the watched values have actually changed, and we
5267 should stop. If not, set BS->stop to false. */
5268
5269 static void
5270 bpstat_check_watchpoint (bpstat *bs)
5271 {
5272 const struct bp_location *bl;
5273 struct watchpoint *b;
5274
5275 /* BS is built for existing struct breakpoint. */
5276 bl = bs->bp_location_at.get ();
5277 gdb_assert (bl != NULL);
5278 b = (struct watchpoint *) bs->breakpoint_at;
5279 gdb_assert (b != NULL);
5280
5281 {
5282 bool must_check_value = false;
5283
5284 if (b->type == bp_watchpoint)
5285 /* For a software watchpoint, we must always check the
5286 watched value. */
5287 must_check_value = true;
5288 else if (b->watchpoint_triggered == watch_triggered_yes)
5289 /* We have a hardware watchpoint (read, write, or access)
5290 and the target earlier reported an address watched by
5291 this watchpoint. */
5292 must_check_value = true;
5293 else if (b->watchpoint_triggered == watch_triggered_unknown
5294 && b->type == bp_hardware_watchpoint)
5295 /* We were stopped by a hardware watchpoint, but the target could
5296 not report the data address. We must check the watchpoint's
5297 value. Access and read watchpoints are out of luck; without
5298 a data address, we can't figure it out. */
5299 must_check_value = true;
5300
5301 if (must_check_value)
5302 {
5303 wp_check_result e;
5304
5305 try
5306 {
5307 e = watchpoint_check (bs);
5308 }
5309 catch (const gdb_exception_error &ex)
5310 {
5311 exception_fprintf (gdb_stderr, ex,
5312 "Error evaluating expression "
5313 "for watchpoint %d\n",
5314 b->number);
5315
5316 SWITCH_THRU_ALL_UIS ()
5317 {
5318 gdb_printf (_("Watchpoint %d deleted.\n"),
5319 b->number);
5320 }
5321 watchpoint_del_at_next_stop (b);
5322 e = WP_DELETED;
5323 }
5324
5325 switch (e)
5326 {
5327 case WP_DELETED:
5328 /* We've already printed what needs to be printed. */
5329 bs->print_it = print_it_done;
5330 /* Stop. */
5331 break;
5332 case WP_IGNORE:
5333 bs->print_it = print_it_noop;
5334 bs->stop = false;
5335 break;
5336 case WP_VALUE_CHANGED:
5337 if (b->type == bp_read_watchpoint)
5338 {
5339 /* There are two cases to consider here:
5340
5341 1. We're watching the triggered memory for reads.
5342 In that case, trust the target, and always report
5343 the watchpoint hit to the user. Even though
5344 reads don't cause value changes, the value may
5345 have changed since the last time it was read, and
5346 since we're not trapping writes, we will not see
5347 those, and as such we should ignore our notion of
5348 old value.
5349
5350 2. We're watching the triggered memory for both
5351 reads and writes. There are two ways this may
5352 happen:
5353
5354 2.1. This is a target that can't break on data
5355 reads only, but can break on accesses (reads or
5356 writes), such as e.g., x86. We detect this case
5357 at the time we try to insert read watchpoints.
5358
5359 2.2. Otherwise, the target supports read
5360 watchpoints, but, the user set an access or write
5361 watchpoint watching the same memory as this read
5362 watchpoint.
5363
5364 If we're watching memory writes as well as reads,
5365 ignore watchpoint hits when we find that the
5366 value hasn't changed, as reads don't cause
5367 changes. This still gives false positives when
5368 the program writes the same value to memory as
5369 what there was already in memory (we will confuse
5370 it for a read), but it's much better than
5371 nothing. */
5372
5373 int other_write_watchpoint = 0;
5374
5375 if (bl->watchpoint_type == hw_read)
5376 {
5377 for (breakpoint *other_b : all_breakpoints ())
5378 if (other_b->type == bp_hardware_watchpoint
5379 || other_b->type == bp_access_watchpoint)
5380 {
5381 struct watchpoint *other_w =
5382 (struct watchpoint *) other_b;
5383
5384 if (other_w->watchpoint_triggered
5385 == watch_triggered_yes)
5386 {
5387 other_write_watchpoint = 1;
5388 break;
5389 }
5390 }
5391 }
5392
5393 if (other_write_watchpoint
5394 || bl->watchpoint_type == hw_access)
5395 {
5396 /* We're watching the same memory for writes,
5397 and the value changed since the last time we
5398 updated it, so this trap must be for a write.
5399 Ignore it. */
5400 bs->print_it = print_it_noop;
5401 bs->stop = false;
5402 }
5403 }
5404 break;
5405 case WP_VALUE_NOT_CHANGED:
5406 if (b->type == bp_hardware_watchpoint
5407 || b->type == bp_watchpoint)
5408 {
5409 /* Don't stop: write watchpoints shouldn't fire if
5410 the value hasn't changed. */
5411 bs->print_it = print_it_noop;
5412 bs->stop = false;
5413 }
5414 /* Stop. */
5415 break;
5416 default:
5417 /* Can't happen. */
5418 break;
5419 }
5420 }
5421 else /* !must_check_value */
5422 {
5423 /* This is a case where some watchpoint(s) triggered, but
5424 not at the address of this watchpoint, or else no
5425 watchpoint triggered after all. So don't print
5426 anything for this watchpoint. */
5427 bs->print_it = print_it_noop;
5428 bs->stop = false;
5429 }
5430 }
5431 }
5432
5433 /* For breakpoints that are currently marked as telling gdb to stop,
5434 check conditions (condition proper, frame, thread and ignore count)
5435 of breakpoint referred to by BS. If we should not stop for this
5436 breakpoint, set BS->stop to 0. */
5437
5438 static void
5439 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5440 {
5441 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
5442
5443 const struct bp_location *bl;
5444 struct breakpoint *b;
5445 /* Assume stop. */
5446 bool condition_result = true;
5447 struct expression *cond;
5448
5449 gdb_assert (bs->stop);
5450
5451 /* BS is built for existing struct breakpoint. */
5452 bl = bs->bp_location_at.get ();
5453 gdb_assert (bl != NULL);
5454 b = bs->breakpoint_at;
5455 gdb_assert (b != NULL);
5456
5457 infrun_debug_printf ("thread = %s, breakpoint %d.%d",
5458 thread->ptid.to_string ().c_str (),
5459 b->number, find_loc_num_by_location (bl));
5460
5461 /* Even if the target evaluated the condition on its end and notified GDB, we
5462 need to do so again since GDB does not know if we stopped due to a
5463 breakpoint or a single step breakpoint. */
5464
5465 if (frame_id_p (b->frame_id)
5466 && b->frame_id != get_stack_frame_id (get_current_frame ()))
5467 {
5468 infrun_debug_printf ("incorrect frame %s not %s, not stopping",
5469 get_stack_frame_id (get_current_frame ()).to_string ().c_str (),
5470 b->frame_id.to_string ().c_str ());
5471 bs->stop = false;
5472 return;
5473 }
5474
5475 /* If this is a thread/task-specific breakpoint, don't waste cpu
5476 evaluating the condition if this isn't the specified
5477 thread/task. */
5478 if ((b->thread != -1 && b->thread != thread->global_num)
5479 || (b->task != -1 && b->task != ada_get_task_number (thread)))
5480 {
5481 infrun_debug_printf ("incorrect thread or task, not stopping");
5482 bs->stop = false;
5483 return;
5484 }
5485
5486 /* Evaluate extension language breakpoints that have a "stop" method
5487 implemented. */
5488 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5489
5490 if (is_watchpoint (b))
5491 {
5492 struct watchpoint *w = (struct watchpoint *) b;
5493
5494 cond = w->cond_exp.get ();
5495 }
5496 else
5497 cond = bl->cond.get ();
5498
5499 if (cond != nullptr && b->disposition != disp_del_at_next_stop)
5500 {
5501 bool within_current_scope = true;
5502 struct watchpoint * w;
5503
5504 /* We use scoped_value_mark because it could be a long time
5505 before we return to the command level and call
5506 free_all_values. We can't call free_all_values because we
5507 might be in the middle of evaluating a function call. */
5508 scoped_value_mark mark;
5509
5510 if (is_watchpoint (b))
5511 w = (struct watchpoint *) b;
5512 else
5513 w = NULL;
5514
5515 /* Need to select the frame, with all that implies so that
5516 the conditions will have the right context. Because we
5517 use the frame, we will not see an inlined function's
5518 variables when we arrive at a breakpoint at the start
5519 of the inlined function; the current frame will be the
5520 call site. */
5521 if (w == NULL || w->cond_exp_valid_block == NULL)
5522 select_frame (get_current_frame ());
5523 else
5524 {
5525 frame_info_ptr frame;
5526
5527 /* For local watchpoint expressions, which particular
5528 instance of a local is being watched matters, so we
5529 keep track of the frame to evaluate the expression
5530 in. To evaluate the condition however, it doesn't
5531 really matter which instantiation of the function
5532 where the condition makes sense triggers the
5533 watchpoint. This allows an expression like "watch
5534 global if q > 10" set in `func', catch writes to
5535 global on all threads that call `func', or catch
5536 writes on all recursive calls of `func' by a single
5537 thread. We simply always evaluate the condition in
5538 the innermost frame that's executing where it makes
5539 sense to evaluate the condition. It seems
5540 intuitive. */
5541 frame = block_innermost_frame (w->cond_exp_valid_block);
5542 if (frame != NULL)
5543 select_frame (frame);
5544 else
5545 within_current_scope = false;
5546 }
5547 CORE_ADDR pc_before_check = get_frame_pc (get_selected_frame (nullptr));
5548 if (within_current_scope)
5549 {
5550 try
5551 {
5552 condition_result = breakpoint_cond_eval (cond);
5553 }
5554 catch (const gdb_exception_error &ex)
5555 {
5556 exception_fprintf (gdb_stderr, ex,
5557 "Error in testing condition for breakpoint %d:\n",
5558 b->number);
5559
5560 /* If the pc value changed as a result of evaluating the
5561 condition then we probably stopped within an inferior
5562 function call due to some unexpected stop, e.g. the thread
5563 hit another breakpoint, or the thread received an
5564 unexpected signal. In this case we don't want to also
5565 print the information about this breakpoint. */
5566 CORE_ADDR pc_after_check
5567 = get_frame_pc (get_selected_frame (nullptr));
5568 if (pc_before_check != pc_after_check)
5569 bs->print = 0;
5570 }
5571 }
5572 else
5573 {
5574 warning (_("Watchpoint condition cannot be tested "
5575 "in the current scope"));
5576 /* If we failed to set the right context for this
5577 watchpoint, unconditionally report it. */
5578 }
5579 /* FIXME-someday, should give breakpoint #. */
5580 }
5581
5582 if (cond != nullptr && !condition_result)
5583 {
5584 infrun_debug_printf ("condition_result = false, not stopping");
5585 bs->stop = false;
5586 return;
5587 }
5588 else if (b->ignore_count > 0)
5589 {
5590 infrun_debug_printf ("ignore count %d, not stopping",
5591 b->ignore_count);
5592 b->ignore_count--;
5593 bs->stop = false;
5594 /* Increase the hit count even though we don't stop. */
5595 ++(b->hit_count);
5596 gdb::observers::breakpoint_modified.notify (b);
5597 return;
5598 }
5599
5600 if (bs->stop)
5601 infrun_debug_printf ("stopping at this breakpoint");
5602 else
5603 infrun_debug_printf ("not stopping at this breakpoint");
5604 }
5605
5606 /* Returns true if we need to track moribund locations of LOC's type
5607 on the current target. */
5608
5609 static bool
5610 need_moribund_for_location_type (const struct bp_location *loc)
5611 {
5612 return ((loc->loc_type == bp_loc_software_breakpoint
5613 && !target_supports_stopped_by_sw_breakpoint ())
5614 || (loc->loc_type == bp_loc_hardware_breakpoint
5615 && !target_supports_stopped_by_hw_breakpoint ()));
5616 }
5617
5618 /* See breakpoint.h. */
5619
5620 bpstat *
5621 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5622 const target_waitstatus &ws)
5623 {
5624 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5625
5626 for (breakpoint *b : all_breakpoints ())
5627 {
5628 if (!breakpoint_enabled (b))
5629 continue;
5630
5631 for (bp_location *bl : b->locations ())
5632 {
5633 /* For hardware watchpoints, we look only at the first
5634 location. The watchpoint_check function will work on the
5635 entire expression, not the individual locations. For
5636 read watchpoints, the watchpoints_triggered function has
5637 checked all locations already. */
5638 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5639 break;
5640
5641 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5642 continue;
5643
5644 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5645 continue;
5646
5647 /* Come here if it's a watchpoint, or if the break address
5648 matches. */
5649
5650 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
5651 explain stop. */
5652
5653 /* Assume we stop. Should we find a watchpoint that is not
5654 actually triggered, or if the condition of the breakpoint
5655 evaluates as false, we'll reset 'stop' to 0. */
5656 bs->stop = true;
5657 bs->print = true;
5658
5659 /* If this is a scope breakpoint, mark the associated
5660 watchpoint as triggered so that we will handle the
5661 out-of-scope event. We'll get to the watchpoint next
5662 iteration. */
5663 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5664 {
5665 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5666
5667 w->watchpoint_triggered = watch_triggered_yes;
5668 }
5669 }
5670 }
5671
5672 /* Check if a moribund breakpoint explains the stop. */
5673 if (!target_supports_stopped_by_sw_breakpoint ()
5674 || !target_supports_stopped_by_hw_breakpoint ())
5675 {
5676 for (bp_location *loc : moribund_locations)
5677 {
5678 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5679 && need_moribund_for_location_type (loc))
5680 {
5681 bpstat *bs = new bpstat (loc, &bs_link);
5682 /* For hits of moribund locations, we should just proceed. */
5683 bs->stop = false;
5684 bs->print = false;
5685 bs->print_it = print_it_noop;
5686 }
5687 }
5688 }
5689
5690 return bs_head;
5691 }
5692
5693 /* See breakpoint.h. */
5694
5695 bpstat *
5696 bpstat_stop_status (const address_space *aspace,
5697 CORE_ADDR bp_addr, thread_info *thread,
5698 const target_waitstatus &ws,
5699 bpstat *stop_chain)
5700 {
5701 struct breakpoint *b = NULL;
5702 /* First item of allocated bpstat's. */
5703 bpstat *bs_head = stop_chain;
5704 bpstat *bs;
5705 int need_remove_insert;
5706 int removed_any;
5707
5708 /* First, build the bpstat chain with locations that explain a
5709 target stop, while being careful to not set the target running,
5710 as that may invalidate locations (in particular watchpoint
5711 locations are recreated). Resuming will happen here with
5712 breakpoint conditions or watchpoint expressions that include
5713 inferior function calls. */
5714 if (bs_head == NULL)
5715 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5716
5717 /* A bit of special processing for shlib breakpoints. We need to
5718 process solib loading here, so that the lists of loaded and
5719 unloaded libraries are correct before we handle "catch load" and
5720 "catch unload". */
5721 for (bs = bs_head; bs != NULL; bs = bs->next)
5722 {
5723 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5724 {
5725 handle_solib_event ();
5726 break;
5727 }
5728 }
5729
5730 /* Now go through the locations that caused the target to stop, and
5731 check whether we're interested in reporting this stop to higher
5732 layers, or whether we should resume the target transparently. */
5733
5734 removed_any = 0;
5735
5736 for (bs = bs_head; bs != NULL; bs = bs->next)
5737 {
5738 if (!bs->stop)
5739 continue;
5740
5741 b = bs->breakpoint_at;
5742 b->check_status (bs);
5743 if (bs->stop)
5744 {
5745 bpstat_check_breakpoint_conditions (bs, thread);
5746
5747 if (bs->stop)
5748 {
5749 ++(b->hit_count);
5750
5751 /* We will stop here. */
5752 if (b->disposition == disp_disable)
5753 {
5754 --(b->enable_count);
5755 if (b->enable_count <= 0)
5756 b->enable_state = bp_disabled;
5757 removed_any = 1;
5758 }
5759 gdb::observers::breakpoint_modified.notify (b);
5760 if (b->silent)
5761 bs->print = false;
5762 bs->commands = b->commands;
5763 if (command_line_is_silent (bs->commands
5764 ? bs->commands.get () : NULL))
5765 bs->print = false;
5766
5767 b->after_condition_true (bs);
5768 }
5769
5770 }
5771
5772 /* Print nothing for this entry if we don't stop or don't
5773 print. */
5774 if (!bs->stop || !bs->print)
5775 bs->print_it = print_it_noop;
5776 }
5777
5778 /* If we aren't stopping, the value of some hardware watchpoint may
5779 not have changed, but the intermediate memory locations we are
5780 watching may have. Don't bother if we're stopping; this will get
5781 done later. */
5782 need_remove_insert = 0;
5783 if (! bpstat_causes_stop (bs_head))
5784 for (bs = bs_head; bs != NULL; bs = bs->next)
5785 if (!bs->stop
5786 && bs->breakpoint_at
5787 && is_hardware_watchpoint (bs->breakpoint_at))
5788 {
5789 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5790
5791 update_watchpoint (w, false /* don't reparse. */);
5792 need_remove_insert = 1;
5793 }
5794
5795 if (need_remove_insert)
5796 update_global_location_list (UGLL_MAY_INSERT);
5797 else if (removed_any)
5798 update_global_location_list (UGLL_DONT_INSERT);
5799
5800 return bs_head;
5801 }
5802
5803 /* See breakpoint.h. */
5804
5805 bpstat *
5806 bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5807 thread_info *thread, const target_waitstatus &ws)
5808 {
5809 gdb_assert (!target_stopped_by_watchpoint ());
5810
5811 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5812 previous stop to avoid confusing bpstat_stop_status. */
5813 watchpoints_triggered (ws);
5814
5815 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5816 }
5817
5818 static void
5819 handle_jit_event (CORE_ADDR address)
5820 {
5821 struct gdbarch *gdbarch;
5822
5823 infrun_debug_printf ("handling bp_jit_event");
5824
5825 /* Switch terminal for any messages produced by
5826 breakpoint_re_set. */
5827 target_terminal::ours_for_output ();
5828
5829 gdbarch = get_frame_arch (get_current_frame ());
5830 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5831 thus it is expected that its objectfile can be found through
5832 minimal symbol lookup. If it doesn't work (and assert fails), it
5833 most likely means that `jit_breakpoint_re_set` was changes and this
5834 function needs to be updated too. */
5835 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5836 gdb_assert (jit_bp_sym.objfile != nullptr);
5837 objfile *objfile = jit_bp_sym.objfile;
5838 if (objfile->separate_debug_objfile_backlink)
5839 objfile = objfile->separate_debug_objfile_backlink;
5840 jit_event_handler (gdbarch, objfile);
5841
5842 target_terminal::inferior ();
5843 }
5844
5845 /* Prepare WHAT final decision for infrun. */
5846
5847 /* Decide what infrun needs to do with this bpstat. */
5848
5849 struct bpstat_what
5850 bpstat_what (bpstat *bs_head)
5851 {
5852 struct bpstat_what retval;
5853 bpstat *bs;
5854
5855 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5856 retval.call_dummy = STOP_NONE;
5857 retval.is_longjmp = false;
5858
5859 for (bs = bs_head; bs != NULL; bs = bs->next)
5860 {
5861 /* Extract this BS's action. After processing each BS, we check
5862 if its action overrides all we've seem so far. */
5863 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5864 enum bptype bptype;
5865
5866 if (bs->breakpoint_at == NULL)
5867 {
5868 /* I suspect this can happen if it was a momentary
5869 breakpoint which has since been deleted. */
5870 bptype = bp_none;
5871 }
5872 else
5873 bptype = bs->breakpoint_at->type;
5874
5875 switch (bptype)
5876 {
5877 case bp_none:
5878 break;
5879 case bp_breakpoint:
5880 case bp_hardware_breakpoint:
5881 case bp_single_step:
5882 case bp_until:
5883 case bp_finish:
5884 case bp_shlib_event:
5885 if (bs->stop)
5886 {
5887 if (bs->print)
5888 this_action = BPSTAT_WHAT_STOP_NOISY;
5889 else
5890 this_action = BPSTAT_WHAT_STOP_SILENT;
5891 }
5892 else
5893 this_action = BPSTAT_WHAT_SINGLE;
5894 break;
5895 case bp_watchpoint:
5896 case bp_hardware_watchpoint:
5897 case bp_read_watchpoint:
5898 case bp_access_watchpoint:
5899 if (bs->stop)
5900 {
5901 if (bs->print)
5902 this_action = BPSTAT_WHAT_STOP_NOISY;
5903 else
5904 this_action = BPSTAT_WHAT_STOP_SILENT;
5905 }
5906 else
5907 {
5908 /* There was a watchpoint, but we're not stopping.
5909 This requires no further action. */
5910 }
5911 break;
5912 case bp_longjmp:
5913 case bp_longjmp_call_dummy:
5914 case bp_exception:
5915 if (bs->stop)
5916 {
5917 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5918 retval.is_longjmp = bptype != bp_exception;
5919 }
5920 else
5921 this_action = BPSTAT_WHAT_SINGLE;
5922 break;
5923 case bp_longjmp_resume:
5924 case bp_exception_resume:
5925 if (bs->stop)
5926 {
5927 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5928 retval.is_longjmp = bptype == bp_longjmp_resume;
5929 }
5930 else
5931 this_action = BPSTAT_WHAT_SINGLE;
5932 break;
5933 case bp_step_resume:
5934 if (bs->stop)
5935 this_action = BPSTAT_WHAT_STEP_RESUME;
5936 else
5937 {
5938 /* It is for the wrong frame. */
5939 this_action = BPSTAT_WHAT_SINGLE;
5940 }
5941 break;
5942 case bp_hp_step_resume:
5943 if (bs->stop)
5944 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5945 else
5946 {
5947 /* It is for the wrong frame. */
5948 this_action = BPSTAT_WHAT_SINGLE;
5949 }
5950 break;
5951 case bp_watchpoint_scope:
5952 case bp_thread_event:
5953 case bp_overlay_event:
5954 case bp_longjmp_master:
5955 case bp_std_terminate_master:
5956 case bp_exception_master:
5957 this_action = BPSTAT_WHAT_SINGLE;
5958 break;
5959 case bp_catchpoint:
5960 if (bs->stop)
5961 {
5962 if (bs->print)
5963 this_action = BPSTAT_WHAT_STOP_NOISY;
5964 else
5965 this_action = BPSTAT_WHAT_STOP_SILENT;
5966 }
5967 else
5968 {
5969 /* Some catchpoints are implemented with breakpoints.
5970 For those, we need to step over the breakpoint. */
5971 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
5972 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
5973 this_action = BPSTAT_WHAT_SINGLE;
5974 }
5975 break;
5976 case bp_jit_event:
5977 this_action = BPSTAT_WHAT_SINGLE;
5978 break;
5979 case bp_call_dummy:
5980 /* Make sure the action is stop (silent or noisy),
5981 so infrun.c pops the dummy frame. */
5982 retval.call_dummy = STOP_STACK_DUMMY;
5983 this_action = BPSTAT_WHAT_STOP_SILENT;
5984 break;
5985 case bp_std_terminate:
5986 /* Make sure the action is stop (silent or noisy),
5987 so infrun.c pops the dummy frame. */
5988 retval.call_dummy = STOP_STD_TERMINATE;
5989 this_action = BPSTAT_WHAT_STOP_SILENT;
5990 break;
5991 case bp_tracepoint:
5992 case bp_fast_tracepoint:
5993 case bp_static_tracepoint:
5994 case bp_static_marker_tracepoint:
5995 /* Tracepoint hits should not be reported back to GDB, and
5996 if one got through somehow, it should have been filtered
5997 out already. */
5998 internal_error (_("bpstat_what: tracepoint encountered"));
5999 break;
6000 case bp_gnu_ifunc_resolver:
6001 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
6002 this_action = BPSTAT_WHAT_SINGLE;
6003 break;
6004 case bp_gnu_ifunc_resolver_return:
6005 /* The breakpoint will be removed, execution will restart from the
6006 PC of the former breakpoint. */
6007 this_action = BPSTAT_WHAT_KEEP_CHECKING;
6008 break;
6009
6010 case bp_dprintf:
6011 if (bs->stop)
6012 this_action = BPSTAT_WHAT_STOP_SILENT;
6013 else
6014 this_action = BPSTAT_WHAT_SINGLE;
6015 break;
6016
6017 default:
6018 internal_error (_("bpstat_what: unhandled bptype %d"), (int) bptype);
6019 }
6020
6021 retval.main_action = std::max (retval.main_action, this_action);
6022 }
6023
6024 return retval;
6025 }
6026
6027 void
6028 bpstat_run_callbacks (bpstat *bs_head)
6029 {
6030 bpstat *bs;
6031
6032 for (bs = bs_head; bs != NULL; bs = bs->next)
6033 {
6034 struct breakpoint *b = bs->breakpoint_at;
6035
6036 if (b == NULL)
6037 continue;
6038 switch (b->type)
6039 {
6040 case bp_jit_event:
6041 handle_jit_event (bs->bp_location_at->address);
6042 break;
6043 case bp_gnu_ifunc_resolver:
6044 gnu_ifunc_resolver_stop ((code_breakpoint *) b);
6045 break;
6046 case bp_gnu_ifunc_resolver_return:
6047 gnu_ifunc_resolver_return_stop ((code_breakpoint *) b);
6048 break;
6049 }
6050 }
6051 }
6052
6053 /* See breakpoint.h. */
6054
6055 bool
6056 bpstat_should_step ()
6057 {
6058 for (breakpoint *b : all_breakpoints ())
6059 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6060 return true;
6061
6062 return false;
6063 }
6064
6065 /* See breakpoint.h. */
6066
6067 bool
6068 bpstat_causes_stop (bpstat *bs)
6069 {
6070 for (; bs != NULL; bs = bs->next)
6071 if (bs->stop)
6072 return true;
6073
6074 return false;
6075 }
6076
6077 \f
6078
6079 /* Compute a number of spaces suitable to indent the next line
6080 so it starts at the position corresponding to the table column
6081 named COL_NAME in the currently active table of UIOUT. */
6082
6083 static int
6084 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6085 {
6086 int i, total_width, width, align;
6087 const char *text;
6088
6089 total_width = 0;
6090 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
6091 {
6092 if (strcmp (text, col_name) == 0)
6093 return total_width;
6094
6095 total_width += width + 1;
6096 }
6097
6098 return 0;
6099 }
6100
6101 /* Determine if the locations of this breakpoint will have their conditions
6102 evaluated by the target, host or a mix of both. Returns the following:
6103
6104 "host": Host evals condition.
6105 "host or target": Host or Target evals condition.
6106 "target": Target evals condition.
6107 */
6108
6109 static const char *
6110 bp_condition_evaluator (const breakpoint *b)
6111 {
6112 char host_evals = 0;
6113 char target_evals = 0;
6114
6115 if (!b)
6116 return NULL;
6117
6118 if (!is_breakpoint (b))
6119 return NULL;
6120
6121 if (gdb_evaluates_breakpoint_condition_p ()
6122 || !target_supports_evaluation_of_breakpoint_conditions ())
6123 return condition_evaluation_host;
6124
6125 for (bp_location *bl : b->locations ())
6126 {
6127 if (bl->cond_bytecode)
6128 target_evals++;
6129 else
6130 host_evals++;
6131 }
6132
6133 if (host_evals && target_evals)
6134 return condition_evaluation_both;
6135 else if (target_evals)
6136 return condition_evaluation_target;
6137 else
6138 return condition_evaluation_host;
6139 }
6140
6141 /* Determine the breakpoint location's condition evaluator. This is
6142 similar to bp_condition_evaluator, but for locations. */
6143
6144 static const char *
6145 bp_location_condition_evaluator (const struct bp_location *bl)
6146 {
6147 if (bl && !is_breakpoint (bl->owner))
6148 return NULL;
6149
6150 if (gdb_evaluates_breakpoint_condition_p ()
6151 || !target_supports_evaluation_of_breakpoint_conditions ())
6152 return condition_evaluation_host;
6153
6154 if (bl && bl->cond_bytecode)
6155 return condition_evaluation_target;
6156 else
6157 return condition_evaluation_host;
6158 }
6159
6160 /* Print the LOC location out of the list of B->LOC locations. */
6161
6162 static void
6163 print_breakpoint_location (const breakpoint *b,
6164 struct bp_location *loc)
6165 {
6166 struct ui_out *uiout = current_uiout;
6167
6168 scoped_restore_current_program_space restore_pspace;
6169
6170 if (loc != NULL && loc->shlib_disabled)
6171 loc = NULL;
6172
6173 if (loc != NULL)
6174 set_current_program_space (loc->pspace);
6175
6176 if (b->display_canonical)
6177 uiout->field_string ("what", b->locspec->to_string ());
6178 else if (loc && loc->symtab)
6179 {
6180 const struct symbol *sym = loc->symbol;
6181
6182 if (sym)
6183 {
6184 uiout->text ("in ");
6185 uiout->field_string ("func", sym->print_name (),
6186 function_name_style.style ());
6187 uiout->text (" ");
6188 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6189 uiout->text ("at ");
6190 }
6191 uiout->field_string ("file",
6192 symtab_to_filename_for_display (loc->symtab),
6193 file_name_style.style ());
6194 uiout->text (":");
6195
6196 if (uiout->is_mi_like_p ())
6197 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6198
6199 uiout->field_signed ("line", loc->line_number);
6200 }
6201 else if (loc)
6202 {
6203 string_file stb;
6204
6205 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6206 demangle, "");
6207 uiout->field_stream ("at", stb);
6208 }
6209 else
6210 {
6211 uiout->field_string ("pending", b->locspec->to_string ());
6212 /* If extra_string is available, it could be holding a condition
6213 or dprintf arguments. In either case, make sure it is printed,
6214 too, but only for non-MI streams. */
6215 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6216 {
6217 if (b->type == bp_dprintf)
6218 uiout->text (",");
6219 else
6220 uiout->text (" ");
6221 uiout->text (b->extra_string.get ());
6222 }
6223 }
6224
6225 if (loc && is_breakpoint (b)
6226 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6227 && bp_condition_evaluator (b) == condition_evaluation_both)
6228 {
6229 uiout->text (" (");
6230 uiout->field_string ("evaluated-by",
6231 bp_location_condition_evaluator (loc));
6232 uiout->text (")");
6233 }
6234 }
6235
6236 static const char *
6237 bptype_string (enum bptype type)
6238 {
6239 struct ep_type_description
6240 {
6241 enum bptype type;
6242 const char *description;
6243 };
6244 static struct ep_type_description bptypes[] =
6245 {
6246 {bp_none, "?deleted?"},
6247 {bp_breakpoint, "breakpoint"},
6248 {bp_hardware_breakpoint, "hw breakpoint"},
6249 {bp_single_step, "sw single-step"},
6250 {bp_until, "until"},
6251 {bp_finish, "finish"},
6252 {bp_watchpoint, "watchpoint"},
6253 {bp_hardware_watchpoint, "hw watchpoint"},
6254 {bp_read_watchpoint, "read watchpoint"},
6255 {bp_access_watchpoint, "acc watchpoint"},
6256 {bp_longjmp, "longjmp"},
6257 {bp_longjmp_resume, "longjmp resume"},
6258 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6259 {bp_exception, "exception"},
6260 {bp_exception_resume, "exception resume"},
6261 {bp_step_resume, "step resume"},
6262 {bp_hp_step_resume, "high-priority step resume"},
6263 {bp_watchpoint_scope, "watchpoint scope"},
6264 {bp_call_dummy, "call dummy"},
6265 {bp_std_terminate, "std::terminate"},
6266 {bp_shlib_event, "shlib events"},
6267 {bp_thread_event, "thread events"},
6268 {bp_overlay_event, "overlay events"},
6269 {bp_longjmp_master, "longjmp master"},
6270 {bp_std_terminate_master, "std::terminate master"},
6271 {bp_exception_master, "exception master"},
6272 {bp_catchpoint, "catchpoint"},
6273 {bp_tracepoint, "tracepoint"},
6274 {bp_fast_tracepoint, "fast tracepoint"},
6275 {bp_static_tracepoint, "static tracepoint"},
6276 {bp_static_marker_tracepoint, "static marker tracepoint"},
6277 {bp_dprintf, "dprintf"},
6278 {bp_jit_event, "jit events"},
6279 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6280 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6281 };
6282
6283 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6284 || ((int) type != bptypes[(int) type].type))
6285 internal_error (_("bptypes table does not describe type #%d."),
6286 (int) type);
6287
6288 return bptypes[(int) type].description;
6289 }
6290
6291 /* For MI, output a field named 'thread-groups' with a list as the value.
6292 For CLI, prefix the list with the string 'inf'. */
6293
6294 static void
6295 output_thread_groups (struct ui_out *uiout,
6296 const char *field_name,
6297 const std::vector<int> &inf_nums,
6298 int mi_only)
6299 {
6300 int is_mi = uiout->is_mi_like_p ();
6301
6302 /* For backward compatibility, don't display inferiors in CLI unless
6303 there are several. Always display them for MI. */
6304 if (!is_mi && mi_only)
6305 return;
6306
6307 ui_out_emit_list list_emitter (uiout, field_name);
6308
6309 for (size_t i = 0; i < inf_nums.size (); i++)
6310 {
6311 if (is_mi)
6312 {
6313 char mi_group[10];
6314
6315 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6316 uiout->field_string (NULL, mi_group);
6317 }
6318 else
6319 {
6320 if (i == 0)
6321 uiout->text (" inf ");
6322 else
6323 uiout->text (", ");
6324
6325 uiout->text (plongest (inf_nums[i]));
6326 }
6327 }
6328 }
6329
6330 /* See breakpoint.h. */
6331
6332 bool fix_breakpoint_script_output_globally = false;
6333
6334 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6335 instead of going via breakpoint_ops::print_one. This makes "maint
6336 info breakpoints" show the software breakpoint locations of
6337 catchpoints, which are considered internal implementation
6338 detail. Returns true if RAW_LOC is false and if the breakpoint's
6339 print_one method did something; false otherwise. */
6340
6341 static bool
6342 print_one_breakpoint_location (struct breakpoint *b,
6343 struct bp_location *loc,
6344 int loc_number,
6345 struct bp_location **last_loc,
6346 int allflag, bool raw_loc)
6347 {
6348 struct command_line *l;
6349 static char bpenables[] = "nynny";
6350
6351 struct ui_out *uiout = current_uiout;
6352 bool header_of_multiple = false;
6353 bool part_of_multiple = (loc != NULL);
6354 struct value_print_options opts;
6355
6356 get_user_print_options (&opts);
6357
6358 gdb_assert (!loc || loc_number != 0);
6359 /* See comment in print_one_breakpoint concerning treatment of
6360 breakpoints with single disabled location. */
6361 if (loc == NULL
6362 && (b->loc != NULL
6363 && (b->loc->next != NULL
6364 || !b->loc->enabled || b->loc->disabled_by_cond)))
6365 header_of_multiple = true;
6366 if (loc == NULL)
6367 loc = b->loc;
6368
6369 annotate_record ();
6370
6371 /* 1 */
6372 annotate_field (0);
6373 if (part_of_multiple)
6374 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6375 else
6376 uiout->field_signed ("number", b->number);
6377
6378 /* 2 */
6379 annotate_field (1);
6380 if (part_of_multiple)
6381 uiout->field_skip ("type");
6382 else
6383 uiout->field_string ("type", bptype_string (b->type));
6384
6385 /* 3 */
6386 annotate_field (2);
6387 if (part_of_multiple)
6388 uiout->field_skip ("disp");
6389 else
6390 uiout->field_string ("disp", bpdisp_text (b->disposition));
6391
6392 /* 4 */
6393 annotate_field (3);
6394 if (part_of_multiple)
6395 {
6396 /* For locations that are disabled because of an invalid
6397 condition, display "N*" on the CLI, where "*" refers to a
6398 footnote below the table. For MI, simply display a "N"
6399 without a footnote. On the CLI, for enabled locations whose
6400 breakpoint is disabled, display "y-". */
6401 auto get_enable_state = [uiout, loc] () -> const char *
6402 {
6403 if (uiout->is_mi_like_p ())
6404 {
6405 if (loc->disabled_by_cond)
6406 return "N";
6407 else if (!loc->enabled)
6408 return "n";
6409 else
6410 return "y";
6411 }
6412 else
6413 {
6414 if (loc->disabled_by_cond)
6415 return "N*";
6416 else if (!loc->enabled)
6417 return "n";
6418 else if (!breakpoint_enabled (loc->owner))
6419 return "y-";
6420 else
6421 return "y";
6422 }
6423 };
6424 uiout->field_string ("enabled", get_enable_state ());
6425 }
6426 else
6427 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6428
6429 /* 5 and 6 */
6430 bool result = false;
6431 if (!raw_loc && b->print_one (last_loc))
6432 result = true;
6433 else
6434 {
6435 if (is_watchpoint (b))
6436 {
6437 struct watchpoint *w = (struct watchpoint *) b;
6438
6439 /* Field 4, the address, is omitted (which makes the columns
6440 not line up too nicely with the headers, but the effect
6441 is relatively readable). */
6442 if (opts.addressprint)
6443 uiout->field_skip ("addr");
6444 annotate_field (5);
6445 uiout->field_string ("what", w->exp_string.get ());
6446 }
6447 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6448 || is_ada_exception_catchpoint (b))
6449 {
6450 if (opts.addressprint)
6451 {
6452 annotate_field (4);
6453 if (header_of_multiple)
6454 uiout->field_string ("addr", "<MULTIPLE>",
6455 metadata_style.style ());
6456 else if (b->loc == NULL || loc->shlib_disabled)
6457 uiout->field_string ("addr", "<PENDING>",
6458 metadata_style.style ());
6459 else
6460 uiout->field_core_addr ("addr",
6461 loc->gdbarch, loc->address);
6462 }
6463 annotate_field (5);
6464 if (!header_of_multiple)
6465 print_breakpoint_location (b, loc);
6466 if (b->loc)
6467 *last_loc = b->loc;
6468 }
6469 }
6470
6471 if (loc != NULL && !header_of_multiple)
6472 {
6473 std::vector<int> inf_nums;
6474 int mi_only = 1;
6475
6476 for (inferior *inf : all_inferiors ())
6477 {
6478 if (inf->pspace == loc->pspace)
6479 inf_nums.push_back (inf->num);
6480 }
6481
6482 /* For backward compatibility, don't display inferiors in CLI unless
6483 there are several. Always display for MI. */
6484 if (allflag
6485 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6486 && (program_spaces.size () > 1
6487 || number_of_inferiors () > 1)
6488 /* LOC is for existing B, it cannot be in
6489 moribund_locations and thus having NULL OWNER. */
6490 && loc->owner->type != bp_catchpoint))
6491 mi_only = 0;
6492 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6493 }
6494
6495 /* In the MI output, each location of a thread or task specific
6496 breakpoint includes the relevant thread or task ID. This is done for
6497 backwards compatibility reasons.
6498
6499 For the CLI output, the thread/task information is printed on a
6500 separate line, see the 'stop only in thread' and 'stop only in task'
6501 output below. */
6502 if (part_of_multiple && uiout->is_mi_like_p ())
6503 {
6504 if (b->thread != -1)
6505 uiout->field_signed ("thread", b->thread);
6506 else if (b->task != -1)
6507 uiout->field_signed ("task", b->task);
6508 }
6509
6510 uiout->text ("\n");
6511
6512 if (!part_of_multiple)
6513 b->print_one_detail (uiout);
6514
6515 if (part_of_multiple && frame_id_p (b->frame_id))
6516 {
6517 annotate_field (6);
6518 uiout->text ("\tstop only in stack frame at ");
6519 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6520 the frame ID. */
6521 uiout->field_core_addr ("frame",
6522 b->gdbarch, b->frame_id.stack_addr);
6523 uiout->text ("\n");
6524 }
6525
6526 if (!part_of_multiple && b->cond_string)
6527 {
6528 annotate_field (7);
6529 if (is_tracepoint (b))
6530 uiout->text ("\ttrace only if ");
6531 else
6532 uiout->text ("\tstop only if ");
6533 uiout->field_string ("cond", b->cond_string.get ());
6534
6535 /* Print whether the target is doing the breakpoint's condition
6536 evaluation. If GDB is doing the evaluation, don't print anything. */
6537 if (is_breakpoint (b)
6538 && breakpoint_condition_evaluation_mode ()
6539 == condition_evaluation_target)
6540 {
6541 uiout->message (" (%pF evals)",
6542 string_field ("evaluated-by",
6543 bp_condition_evaluator (b)));
6544 }
6545 uiout->text ("\n");
6546 }
6547
6548 if (!part_of_multiple && b->thread != -1)
6549 {
6550 /* FIXME should make an annotation for this. */
6551 uiout->text ("\tstop only in thread ");
6552 if (uiout->is_mi_like_p ())
6553 uiout->field_signed ("thread", b->thread);
6554 else
6555 {
6556 struct thread_info *thr = find_thread_global_id (b->thread);
6557
6558 uiout->field_string ("thread", print_thread_id (thr));
6559 }
6560 uiout->text ("\n");
6561 }
6562
6563 if (!part_of_multiple && b->task != -1)
6564 {
6565 uiout->text ("\tstop only in task ");
6566 uiout->field_signed ("task", b->task);
6567 uiout->text ("\n");
6568 }
6569
6570 if (!part_of_multiple)
6571 {
6572 if (b->hit_count)
6573 {
6574 /* FIXME should make an annotation for this. */
6575 if (is_catchpoint (b))
6576 uiout->text ("\tcatchpoint");
6577 else if (is_tracepoint (b))
6578 uiout->text ("\ttracepoint");
6579 else
6580 uiout->text ("\tbreakpoint");
6581 uiout->text (" already hit ");
6582 uiout->field_signed ("times", b->hit_count);
6583 if (b->hit_count == 1)
6584 uiout->text (" time\n");
6585 else
6586 uiout->text (" times\n");
6587 }
6588 else
6589 {
6590 /* Output the count also if it is zero, but only if this is mi. */
6591 if (uiout->is_mi_like_p ())
6592 uiout->field_signed ("times", b->hit_count);
6593 }
6594 }
6595
6596 if (!part_of_multiple && b->ignore_count)
6597 {
6598 annotate_field (8);
6599 uiout->message ("\tignore next %pF hits\n",
6600 signed_field ("ignore", b->ignore_count));
6601 }
6602
6603 /* Note that an enable count of 1 corresponds to "enable once"
6604 behavior, which is reported by the combination of enablement and
6605 disposition, so we don't need to mention it here. */
6606 if (!part_of_multiple && b->enable_count > 1)
6607 {
6608 annotate_field (8);
6609 uiout->text ("\tdisable after ");
6610 /* Tweak the wording to clarify that ignore and enable counts
6611 are distinct, and have additive effect. */
6612 if (b->ignore_count)
6613 uiout->text ("additional ");
6614 else
6615 uiout->text ("next ");
6616 uiout->field_signed ("enable", b->enable_count);
6617 uiout->text (" hits\n");
6618 }
6619
6620 if (!part_of_multiple && is_tracepoint (b))
6621 {
6622 struct tracepoint *tp = (struct tracepoint *) b;
6623
6624 if (tp->traceframe_usage)
6625 {
6626 uiout->text ("\ttrace buffer usage ");
6627 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6628 uiout->text (" bytes\n");
6629 }
6630 }
6631
6632 l = b->commands ? b->commands.get () : NULL;
6633 if (!part_of_multiple && l)
6634 {
6635 annotate_field (9);
6636
6637 bool use_fixed_output =
6638 (uiout->test_flags (fix_breakpoint_script_output)
6639 || fix_breakpoint_script_output_globally);
6640
6641 gdb::optional<ui_out_emit_tuple> tuple_emitter;
6642 gdb::optional<ui_out_emit_list> list_emitter;
6643
6644 if (use_fixed_output)
6645 list_emitter.emplace (uiout, "script");
6646 else
6647 tuple_emitter.emplace (uiout, "script");
6648
6649 print_command_lines (uiout, l, 4);
6650 }
6651
6652 if (is_tracepoint (b))
6653 {
6654 struct tracepoint *t = (struct tracepoint *) b;
6655
6656 if (!part_of_multiple && t->pass_count)
6657 {
6658 annotate_field (10);
6659 uiout->text ("\tpass count ");
6660 uiout->field_signed ("pass", t->pass_count);
6661 uiout->text (" \n");
6662 }
6663
6664 /* Don't display it when tracepoint or tracepoint location is
6665 pending. */
6666 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6667 {
6668 annotate_field (11);
6669
6670 if (uiout->is_mi_like_p ())
6671 uiout->field_string ("installed",
6672 loc->inserted ? "y" : "n");
6673 else
6674 {
6675 if (loc->inserted)
6676 uiout->text ("\t");
6677 else
6678 uiout->text ("\tnot ");
6679 uiout->text ("installed on target\n");
6680 }
6681 }
6682 }
6683
6684 if (uiout->is_mi_like_p () && !part_of_multiple)
6685 {
6686 if (is_watchpoint (b))
6687 {
6688 struct watchpoint *w = (struct watchpoint *) b;
6689
6690 uiout->field_string ("original-location", w->exp_string.get ());
6691 }
6692 else if (b->locspec != nullptr)
6693 {
6694 const char *str = b->locspec->to_string ();
6695 if (str != nullptr)
6696 uiout->field_string ("original-location", str);
6697 }
6698 }
6699
6700 return result;
6701 }
6702
6703 /* See breakpoint.h. */
6704
6705 bool fix_multi_location_breakpoint_output_globally = false;
6706
6707 static void
6708 print_one_breakpoint (struct breakpoint *b,
6709 struct bp_location **last_loc,
6710 int allflag)
6711 {
6712 struct ui_out *uiout = current_uiout;
6713 bool use_fixed_output
6714 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6715 || fix_multi_location_breakpoint_output_globally);
6716
6717 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6718 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6719 allflag, false);
6720
6721 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6722 are outside. */
6723 if (!use_fixed_output)
6724 bkpt_tuple_emitter.reset ();
6725
6726 /* If this breakpoint has custom print function,
6727 it's already printed. Otherwise, print individual
6728 locations, if any. */
6729 if (!printed || allflag)
6730 {
6731 /* If breakpoint has a single location that is disabled, we
6732 print it as if it had several locations, since otherwise it's
6733 hard to represent "breakpoint enabled, location disabled"
6734 situation.
6735
6736 Note that while hardware watchpoints have several locations
6737 internally, that's not a property exposed to users.
6738
6739 Likewise, while catchpoints may be implemented with
6740 breakpoints (e.g., catch throw), that's not a property
6741 exposed to users. We do however display the internal
6742 breakpoint locations with "maint info breakpoints". */
6743 if (!is_hardware_watchpoint (b)
6744 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6745 || is_ada_exception_catchpoint (b))
6746 && (allflag
6747 || (b->loc && (b->loc->next
6748 || !b->loc->enabled
6749 || b->loc->disabled_by_cond))))
6750 {
6751 gdb::optional<ui_out_emit_list> locations_list;
6752
6753 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6754 MI record. For later versions, place breakpoint locations in a
6755 list. */
6756 if (uiout->is_mi_like_p () && use_fixed_output)
6757 locations_list.emplace (uiout, "locations");
6758
6759 int n = 1;
6760 for (bp_location *loc : b->locations ())
6761 {
6762 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6763 print_one_breakpoint_location (b, loc, n, last_loc,
6764 allflag, allflag);
6765 n++;
6766 }
6767 }
6768 }
6769 }
6770
6771 static int
6772 breakpoint_address_bits (struct breakpoint *b)
6773 {
6774 int print_address_bits = 0;
6775
6776 for (bp_location *loc : b->locations ())
6777 {
6778 if (!bl_address_is_meaningful (loc))
6779 continue;
6780
6781 int addr_bit = gdbarch_addr_bit (loc->gdbarch);
6782 if (addr_bit > print_address_bits)
6783 print_address_bits = addr_bit;
6784 }
6785
6786 return print_address_bits;
6787 }
6788
6789 /* See breakpoint.h. */
6790
6791 void
6792 print_breakpoint (breakpoint *b)
6793 {
6794 struct bp_location *dummy_loc = NULL;
6795 print_one_breakpoint (b, &dummy_loc, 0);
6796 }
6797
6798 /* Return true if this breakpoint was set by the user, false if it is
6799 internal or momentary. */
6800
6801 int
6802 user_breakpoint_p (struct breakpoint *b)
6803 {
6804 return b->number > 0;
6805 }
6806
6807 /* See breakpoint.h. */
6808
6809 int
6810 pending_breakpoint_p (struct breakpoint *b)
6811 {
6812 return b->loc == NULL;
6813 }
6814
6815 /* Print information on breakpoints (including watchpoints and tracepoints).
6816
6817 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6818 understood by number_or_range_parser. Only breakpoints included in this
6819 list are then printed.
6820
6821 If SHOW_INTERNAL is true, print internal breakpoints.
6822
6823 If FILTER is non-NULL, call it on each breakpoint and only include the
6824 ones for which it returns true.
6825
6826 Return the total number of breakpoints listed. */
6827
6828 static int
6829 breakpoint_1 (const char *bp_num_list, bool show_internal,
6830 bool (*filter) (const struct breakpoint *))
6831 {
6832 struct bp_location *last_loc = NULL;
6833 int nr_printable_breakpoints;
6834 struct value_print_options opts;
6835 int print_address_bits = 0;
6836 int print_type_col_width = 14;
6837 struct ui_out *uiout = current_uiout;
6838 bool has_disabled_by_cond_location = false;
6839
6840 get_user_print_options (&opts);
6841
6842 /* Compute the number of rows in the table, as well as the size
6843 required for address fields. */
6844 nr_printable_breakpoints = 0;
6845 for (breakpoint *b : all_breakpoints ())
6846 {
6847 /* If we have a filter, only list the breakpoints it accepts. */
6848 if (filter && !filter (b))
6849 continue;
6850
6851 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6852 accept. Skip the others. */
6853 if (bp_num_list != NULL && *bp_num_list != '\0')
6854 {
6855 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6856 continue;
6857 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6858 continue;
6859 }
6860
6861 if (show_internal || user_breakpoint_p (b))
6862 {
6863 int addr_bit, type_len;
6864
6865 addr_bit = breakpoint_address_bits (b);
6866 if (addr_bit > print_address_bits)
6867 print_address_bits = addr_bit;
6868
6869 type_len = strlen (bptype_string (b->type));
6870 if (type_len > print_type_col_width)
6871 print_type_col_width = type_len;
6872
6873 nr_printable_breakpoints++;
6874 }
6875 }
6876
6877 {
6878 ui_out_emit_table table_emitter (uiout,
6879 opts.addressprint ? 6 : 5,
6880 nr_printable_breakpoints,
6881 "BreakpointTable");
6882
6883 if (nr_printable_breakpoints > 0)
6884 annotate_breakpoints_headers ();
6885 if (nr_printable_breakpoints > 0)
6886 annotate_field (0);
6887 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6888 if (nr_printable_breakpoints > 0)
6889 annotate_field (1);
6890 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6891 if (nr_printable_breakpoints > 0)
6892 annotate_field (2);
6893 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6894 if (nr_printable_breakpoints > 0)
6895 annotate_field (3);
6896 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6897 if (opts.addressprint)
6898 {
6899 if (nr_printable_breakpoints > 0)
6900 annotate_field (4);
6901 if (print_address_bits <= 32)
6902 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6903 else
6904 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6905 }
6906 if (nr_printable_breakpoints > 0)
6907 annotate_field (5);
6908 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6909 uiout->table_body ();
6910 if (nr_printable_breakpoints > 0)
6911 annotate_breakpoints_table ();
6912
6913 for (breakpoint *b : all_breakpoints ())
6914 {
6915 QUIT;
6916 /* If we have a filter, only list the breakpoints it accepts. */
6917 if (filter && !filter (b))
6918 continue;
6919
6920 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6921 accept. Skip the others. */
6922
6923 if (bp_num_list != NULL && *bp_num_list != '\0')
6924 {
6925 if (show_internal) /* maintenance info breakpoint */
6926 {
6927 if (parse_and_eval_long (bp_num_list) != b->number)
6928 continue;
6929 }
6930 else /* all others */
6931 {
6932 if (!number_is_in_list (bp_num_list, b->number))
6933 continue;
6934 }
6935 }
6936 /* We only print out user settable breakpoints unless the
6937 show_internal is set. */
6938 if (show_internal || user_breakpoint_p (b))
6939 {
6940 print_one_breakpoint (b, &last_loc, show_internal);
6941 for (bp_location *loc : b->locations ())
6942 if (loc->disabled_by_cond)
6943 has_disabled_by_cond_location = true;
6944 }
6945 }
6946 }
6947
6948 if (nr_printable_breakpoints == 0)
6949 {
6950 /* If there's a filter, let the caller decide how to report
6951 empty list. */
6952 if (!filter)
6953 {
6954 if (bp_num_list == NULL || *bp_num_list == '\0')
6955 uiout->message ("No breakpoints or watchpoints.\n");
6956 else
6957 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6958 bp_num_list);
6959 }
6960 }
6961 else
6962 {
6963 if (last_loc && !server_command)
6964 set_next_address (last_loc->gdbarch, last_loc->address);
6965
6966 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6967 uiout->message (_("(*): Breakpoint condition is invalid at this "
6968 "location.\n"));
6969 }
6970
6971 /* FIXME? Should this be moved up so that it is only called when
6972 there have been breakpoints? */
6973 annotate_breakpoints_table_end ();
6974
6975 return nr_printable_breakpoints;
6976 }
6977
6978 /* Display the value of default-collect in a way that is generally
6979 compatible with the breakpoint list. */
6980
6981 static void
6982 default_collect_info (void)
6983 {
6984 struct ui_out *uiout = current_uiout;
6985
6986 /* If it has no value (which is frequently the case), say nothing; a
6987 message like "No default-collect." gets in user's face when it's
6988 not wanted. */
6989 if (default_collect.empty ())
6990 return;
6991
6992 /* The following phrase lines up nicely with per-tracepoint collect
6993 actions. */
6994 uiout->text ("default collect ");
6995 uiout->field_string ("default-collect", default_collect);
6996 uiout->text (" \n");
6997 }
6998
6999 static void
7000 info_breakpoints_command (const char *args, int from_tty)
7001 {
7002 breakpoint_1 (args, false, NULL);
7003
7004 default_collect_info ();
7005 }
7006
7007 static void
7008 info_watchpoints_command (const char *args, int from_tty)
7009 {
7010 int num_printed = breakpoint_1 (args, false, is_watchpoint);
7011 struct ui_out *uiout = current_uiout;
7012
7013 if (num_printed == 0)
7014 {
7015 if (args == NULL || *args == '\0')
7016 uiout->message ("No watchpoints.\n");
7017 else
7018 uiout->message ("No watchpoint matching '%s'.\n", args);
7019 }
7020 }
7021
7022 static void
7023 maintenance_info_breakpoints (const char *args, int from_tty)
7024 {
7025 breakpoint_1 (args, true, NULL);
7026
7027 default_collect_info ();
7028 }
7029
7030 static bool
7031 breakpoint_has_pc (struct breakpoint *b,
7032 struct program_space *pspace,
7033 CORE_ADDR pc, struct obj_section *section)
7034 {
7035 for (bp_location *bl : b->locations ())
7036 {
7037 if (bl->pspace == pspace
7038 && bl->address == pc
7039 && (!overlay_debugging || bl->section == section))
7040 return true;
7041 }
7042 return false;
7043 }
7044
7045 /* See breakpoint.h. */
7046
7047 void
7048 describe_other_breakpoints (struct gdbarch *gdbarch,
7049 struct program_space *pspace, CORE_ADDR pc,
7050 struct obj_section *section, int thread)
7051 {
7052 int others = 0;
7053
7054 for (breakpoint *b : all_breakpoints ())
7055 others += (user_breakpoint_p (b)
7056 && breakpoint_has_pc (b, pspace, pc, section));
7057
7058 if (others > 0)
7059 {
7060 if (others == 1)
7061 gdb_printf (_("Note: breakpoint "));
7062 else /* if (others == ???) */
7063 gdb_printf (_("Note: breakpoints "));
7064 for (breakpoint *b : all_breakpoints ())
7065 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7066 {
7067 others--;
7068 gdb_printf ("%d", b->number);
7069 if (b->thread == -1 && thread != -1)
7070 gdb_printf (" (all threads)");
7071 else if (b->thread != -1)
7072 {
7073 struct thread_info *thr = find_thread_global_id (b->thread);
7074 gdb_printf (" (thread %s)", print_thread_id (thr));
7075 }
7076 else if (b->task != -1)
7077 gdb_printf (" (task %d)", b->task);
7078 gdb_printf ("%s%s ",
7079 ((b->enable_state == bp_disabled
7080 || b->enable_state == bp_call_disabled)
7081 ? " (disabled)"
7082 : ""),
7083 (others > 1) ? ","
7084 : ((others == 1) ? " and" : ""));
7085 }
7086 current_uiout->message (_("also set at pc %ps.\n"),
7087 styled_string (address_style.style (),
7088 paddress (gdbarch, pc)));
7089 }
7090 }
7091 \f
7092
7093 /* Return true iff it is meaningful to use the address member of LOC.
7094 For some breakpoint types, the locations' address members are
7095 irrelevant and it makes no sense to attempt to compare them to
7096 other addresses (or use them for any other purpose either).
7097
7098 More specifically, software watchpoints and catchpoints that are
7099 not backed by breakpoints always have a zero valued location
7100 address and we don't want to mark breakpoints of any of these types
7101 to be a duplicate of an actual breakpoint location at address
7102 zero. */
7103
7104 static bool
7105 bl_address_is_meaningful (bp_location *loc)
7106 {
7107 return loc->loc_type != bp_loc_other;
7108 }
7109
7110 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7111 true if LOC1 and LOC2 represent the same watchpoint location. */
7112
7113 static bool
7114 watchpoint_locations_match (const struct bp_location *loc1,
7115 const struct bp_location *loc2)
7116 {
7117 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7118 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7119
7120 /* Both of them must exist. */
7121 gdb_assert (w1 != NULL);
7122 gdb_assert (w2 != NULL);
7123
7124 /* If the target can evaluate the condition expression in hardware,
7125 then we we need to insert both watchpoints even if they are at
7126 the same place. Otherwise the watchpoint will only trigger when
7127 the condition of whichever watchpoint was inserted evaluates to
7128 true, not giving a chance for GDB to check the condition of the
7129 other watchpoint. */
7130 if ((w1->cond_exp
7131 && target_can_accel_watchpoint_condition (loc1->address,
7132 loc1->length,
7133 loc1->watchpoint_type,
7134 w1->cond_exp.get ()))
7135 || (w2->cond_exp
7136 && target_can_accel_watchpoint_condition (loc2->address,
7137 loc2->length,
7138 loc2->watchpoint_type,
7139 w2->cond_exp.get ())))
7140 return false;
7141
7142 /* Note that this checks the owner's type, not the location's. In
7143 case the target does not support read watchpoints, but does
7144 support access watchpoints, we'll have bp_read_watchpoint
7145 watchpoints with hw_access locations. Those should be considered
7146 duplicates of hw_read locations. The hw_read locations will
7147 become hw_access locations later. */
7148 return (loc1->owner->type == loc2->owner->type
7149 && loc1->pspace->aspace == loc2->pspace->aspace
7150 && loc1->address == loc2->address
7151 && loc1->length == loc2->length);
7152 }
7153
7154 /* See breakpoint.h. */
7155
7156 int
7157 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
7158 const address_space *aspace2, CORE_ADDR addr2)
7159 {
7160 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7161 || aspace1 == aspace2)
7162 && addr1 == addr2);
7163 }
7164
7165 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7166 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7167 matches ASPACE2. On targets that have global breakpoints, the address
7168 space doesn't really matter. */
7169
7170 static bool
7171 breakpoint_address_match_range (const address_space *aspace1,
7172 CORE_ADDR addr1,
7173 int len1, const address_space *aspace2,
7174 CORE_ADDR addr2)
7175 {
7176 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7177 || aspace1 == aspace2)
7178 && addr2 >= addr1 && addr2 < addr1 + len1);
7179 }
7180
7181 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7182 a ranged breakpoint. In most targets, a match happens only if ASPACE
7183 matches the breakpoint's address space. On targets that have global
7184 breakpoints, the address space doesn't really matter. */
7185
7186 static bool
7187 breakpoint_location_address_match (struct bp_location *bl,
7188 const address_space *aspace,
7189 CORE_ADDR addr)
7190 {
7191 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7192 aspace, addr)
7193 || (bl->length
7194 && breakpoint_address_match_range (bl->pspace->aspace,
7195 bl->address, bl->length,
7196 aspace, addr)));
7197 }
7198
7199 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7200 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7201 match happens only if ASPACE matches the breakpoint's address
7202 space. On targets that have global breakpoints, the address space
7203 doesn't really matter. */
7204
7205 static bool
7206 breakpoint_location_address_range_overlap (struct bp_location *bl,
7207 const address_space *aspace,
7208 CORE_ADDR addr, int len)
7209 {
7210 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7211 || bl->pspace->aspace == aspace)
7212 {
7213 int bl_len = bl->length != 0 ? bl->length : 1;
7214
7215 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7216 return 1;
7217 }
7218 return 0;
7219 }
7220
7221 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7222 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7223 true, otherwise returns false. */
7224
7225 static bool
7226 tracepoint_locations_match (const struct bp_location *loc1,
7227 const struct bp_location *loc2)
7228 {
7229 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7230 /* Since tracepoint locations are never duplicated with others', tracepoint
7231 locations at the same address of different tracepoints are regarded as
7232 different locations. */
7233 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7234 else
7235 return false;
7236 }
7237
7238 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7239 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7240 the same location. If SW_HW_BPS_MATCH is true, then software
7241 breakpoint locations and hardware breakpoint locations match,
7242 otherwise they don't. */
7243
7244 static bool
7245 breakpoint_locations_match (const struct bp_location *loc1,
7246 const struct bp_location *loc2,
7247 bool sw_hw_bps_match)
7248 {
7249 int hw_point1, hw_point2;
7250
7251 /* Both of them must not be in moribund_locations. */
7252 gdb_assert (loc1->owner != NULL);
7253 gdb_assert (loc2->owner != NULL);
7254
7255 hw_point1 = is_hardware_watchpoint (loc1->owner);
7256 hw_point2 = is_hardware_watchpoint (loc2->owner);
7257
7258 if (hw_point1 != hw_point2)
7259 return false;
7260 else if (hw_point1)
7261 return watchpoint_locations_match (loc1, loc2);
7262 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7263 return tracepoint_locations_match (loc1, loc2);
7264 else
7265 /* We compare bp_location.length in order to cover ranged
7266 breakpoints. Keep this in sync with
7267 bp_location_is_less_than. */
7268 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7269 loc2->pspace->aspace, loc2->address)
7270 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7271 && loc1->length == loc2->length);
7272 }
7273
7274 static void
7275 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7276 int bnum, bool have_bnum)
7277 {
7278 /* The longest string possibly returned by hex_string_custom
7279 is 50 chars. These must be at least that big for safety. */
7280 char astr1[64];
7281 char astr2[64];
7282
7283 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7284 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7285 if (have_bnum)
7286 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7287 bnum, astr1, astr2);
7288 else
7289 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7290 }
7291
7292 /* Adjust a breakpoint's address to account for architectural
7293 constraints on breakpoint placement. Return the adjusted address.
7294 Note: Very few targets require this kind of adjustment. For most
7295 targets, this function is simply the identity function. */
7296
7297 static CORE_ADDR
7298 adjust_breakpoint_address (struct gdbarch *gdbarch,
7299 CORE_ADDR bpaddr, enum bptype bptype,
7300 struct program_space *pspace)
7301 {
7302 gdb_assert (pspace != nullptr);
7303
7304 if (bptype == bp_watchpoint
7305 || bptype == bp_hardware_watchpoint
7306 || bptype == bp_read_watchpoint
7307 || bptype == bp_access_watchpoint
7308 || bptype == bp_catchpoint)
7309 {
7310 /* Watchpoints and the various bp_catch_* eventpoints should not
7311 have their addresses modified. */
7312 return bpaddr;
7313 }
7314 else if (bptype == bp_single_step)
7315 {
7316 /* Single-step breakpoints should not have their addresses
7317 modified. If there's any architectural constrain that
7318 applies to this address, then it should have already been
7319 taken into account when the breakpoint was created in the
7320 first place. If we didn't do this, stepping through e.g.,
7321 Thumb-2 IT blocks would break. */
7322 return bpaddr;
7323 }
7324 else
7325 {
7326 CORE_ADDR adjusted_bpaddr = bpaddr;
7327
7328 /* Some targets have architectural constraints on the placement
7329 of breakpoint instructions. Obtain the adjusted address. */
7330 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7331 {
7332 /* Targets that implement this adjustment function will likely
7333 inspect either the symbol table, target memory at BPADDR, or
7334 even state registers, so ensure a suitable thread (and its
7335 associated program space) are currently selected. */
7336 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7337 switch_to_program_space_and_thread (pspace);
7338 adjusted_bpaddr
7339 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7340 }
7341
7342 adjusted_bpaddr
7343 = gdbarch_remove_non_address_bits (gdbarch, adjusted_bpaddr);
7344
7345 /* An adjusted breakpoint address can significantly alter
7346 a user's expectations. Print a warning if an adjustment
7347 is required. */
7348 if (adjusted_bpaddr != bpaddr)
7349 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, false);
7350
7351 return adjusted_bpaddr;
7352 }
7353 }
7354
7355 static bp_loc_type
7356 bp_location_from_bp_type (bptype type)
7357 {
7358 switch (type)
7359 {
7360 case bp_breakpoint:
7361 case bp_single_step:
7362 case bp_until:
7363 case bp_finish:
7364 case bp_longjmp:
7365 case bp_longjmp_resume:
7366 case bp_longjmp_call_dummy:
7367 case bp_exception:
7368 case bp_exception_resume:
7369 case bp_step_resume:
7370 case bp_hp_step_resume:
7371 case bp_watchpoint_scope:
7372 case bp_call_dummy:
7373 case bp_std_terminate:
7374 case bp_shlib_event:
7375 case bp_thread_event:
7376 case bp_overlay_event:
7377 case bp_jit_event:
7378 case bp_longjmp_master:
7379 case bp_std_terminate_master:
7380 case bp_exception_master:
7381 case bp_gnu_ifunc_resolver:
7382 case bp_gnu_ifunc_resolver_return:
7383 case bp_dprintf:
7384 return bp_loc_software_breakpoint;
7385
7386 case bp_hardware_breakpoint:
7387 return bp_loc_hardware_breakpoint;
7388
7389 case bp_hardware_watchpoint:
7390 case bp_read_watchpoint:
7391 case bp_access_watchpoint:
7392 return bp_loc_hardware_watchpoint;
7393
7394 case bp_watchpoint:
7395 return bp_loc_software_watchpoint;
7396
7397 case bp_tracepoint:
7398 case bp_fast_tracepoint:
7399 case bp_static_tracepoint:
7400 case bp_static_marker_tracepoint:
7401 return bp_loc_tracepoint;
7402
7403 case bp_catchpoint:
7404 return bp_loc_other;
7405
7406 default:
7407 internal_error (_("unknown breakpoint type"));
7408 }
7409 }
7410
7411 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7412 {
7413 this->owner = owner;
7414 this->cond_bytecode = NULL;
7415 this->shlib_disabled = 0;
7416 this->enabled = 1;
7417 this->disabled_by_cond = false;
7418
7419 this->loc_type = type;
7420
7421 if (this->loc_type == bp_loc_software_breakpoint
7422 || this->loc_type == bp_loc_hardware_breakpoint)
7423 mark_breakpoint_location_modified (this);
7424
7425 incref ();
7426 }
7427
7428 bp_location::bp_location (breakpoint *owner)
7429 : bp_location::bp_location (owner,
7430 bp_location_from_bp_type (owner->type))
7431 {
7432 }
7433
7434 /* Decrement reference count. If the reference count reaches 0,
7435 destroy the bp_location. Sets *BLP to NULL. */
7436
7437 static void
7438 decref_bp_location (struct bp_location **blp)
7439 {
7440 bp_location_ref_policy::decref (*blp);
7441 *blp = NULL;
7442 }
7443
7444 /* Add breakpoint B at the end of the global breakpoint chain. */
7445
7446 static breakpoint *
7447 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7448 {
7449 struct breakpoint *b1;
7450 struct breakpoint *result = b.get ();
7451
7452 /* Add this breakpoint to the end of the chain so that a list of
7453 breakpoints will come out in order of increasing numbers. */
7454
7455 b1 = breakpoint_chain;
7456 if (b1 == 0)
7457 breakpoint_chain = b.release ();
7458 else
7459 {
7460 while (b1->next)
7461 b1 = b1->next;
7462 b1->next = b.release ();
7463 }
7464
7465 return result;
7466 }
7467
7468 /* Initialize loc->function_name. */
7469
7470 static void
7471 set_breakpoint_location_function (struct bp_location *loc)
7472 {
7473 gdb_assert (loc->owner != NULL);
7474
7475 if (loc->owner->type == bp_breakpoint
7476 || loc->owner->type == bp_hardware_breakpoint
7477 || is_tracepoint (loc->owner))
7478 {
7479 const char *function_name;
7480
7481 if (loc->msymbol != NULL
7482 && (loc->msymbol->type () == mst_text_gnu_ifunc
7483 || loc->msymbol->type () == mst_data_gnu_ifunc))
7484 {
7485 struct breakpoint *b = loc->owner;
7486
7487 function_name = loc->msymbol->linkage_name ();
7488
7489 if (b->type == bp_breakpoint && b->loc == loc
7490 && loc->next == NULL && b->related_breakpoint == b)
7491 {
7492 /* Create only the whole new breakpoint of this type but do not
7493 mess more complicated breakpoints with multiple locations. */
7494 b->type = bp_gnu_ifunc_resolver;
7495 /* Remember the resolver's address for use by the return
7496 breakpoint. */
7497 loc->related_address = loc->address;
7498 }
7499 }
7500 else
7501 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7502
7503 if (function_name)
7504 loc->function_name = make_unique_xstrdup (function_name);
7505 }
7506 }
7507
7508 /* Attempt to determine architecture of location identified by SAL. */
7509 struct gdbarch *
7510 get_sal_arch (struct symtab_and_line sal)
7511 {
7512 if (sal.section)
7513 return sal.section->objfile->arch ();
7514 if (sal.symtab)
7515 return sal.symtab->compunit ()->objfile ()->arch ();
7516
7517 return NULL;
7518 }
7519
7520 /* Call this routine when stepping and nexting to enable a breakpoint
7521 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7522 initiated the operation. */
7523
7524 void
7525 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7526 {
7527 int thread = tp->global_num;
7528
7529 /* To avoid having to rescan all objfile symbols at every step,
7530 we maintain a list of continually-inserted but always disabled
7531 longjmp "master" breakpoints. Here, we simply create momentary
7532 clones of those and enable them for the requested thread. */
7533 for (breakpoint *b : all_breakpoints_safe ())
7534 if (b->pspace == current_program_space
7535 && (b->type == bp_longjmp_master
7536 || b->type == bp_exception_master))
7537 {
7538 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7539 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7540 after their removal. */
7541 momentary_breakpoint_from_master (b, type, 1, thread);
7542 }
7543
7544 tp->initiating_frame = frame;
7545 }
7546
7547 /* Delete all longjmp breakpoints from THREAD. */
7548 void
7549 delete_longjmp_breakpoint (int thread)
7550 {
7551 for (breakpoint *b : all_breakpoints_safe ())
7552 if (b->type == bp_longjmp || b->type == bp_exception)
7553 {
7554 if (b->thread == thread)
7555 delete_breakpoint (b);
7556 }
7557 }
7558
7559 void
7560 delete_longjmp_breakpoint_at_next_stop (int thread)
7561 {
7562 for (breakpoint *b : all_breakpoints_safe ())
7563 if (b->type == bp_longjmp || b->type == bp_exception)
7564 {
7565 if (b->thread == thread)
7566 b->disposition = disp_del_at_next_stop;
7567 }
7568 }
7569
7570 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7571 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7572 pointer to any of them. Return NULL if this system cannot place longjmp
7573 breakpoints. */
7574
7575 struct breakpoint *
7576 set_longjmp_breakpoint_for_call_dummy (void)
7577 {
7578 breakpoint *retval = nullptr;
7579
7580 for (breakpoint *b : all_breakpoints ())
7581 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7582 {
7583 int thread = inferior_thread ()->global_num;
7584 breakpoint *new_b
7585 = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7586 1, thread);
7587
7588 /* Link NEW_B into the chain of RETVAL breakpoints. */
7589
7590 gdb_assert (new_b->related_breakpoint == new_b);
7591 if (retval == NULL)
7592 retval = new_b;
7593 new_b->related_breakpoint = retval;
7594 while (retval->related_breakpoint != new_b->related_breakpoint)
7595 retval = retval->related_breakpoint;
7596 retval->related_breakpoint = new_b;
7597 }
7598
7599 return retval;
7600 }
7601
7602 /* Verify all existing dummy frames and their associated breakpoints for
7603 TP. Remove those which can no longer be found in the current frame
7604 stack.
7605
7606 If the unwind fails then there is not sufficient information to discard
7607 dummy frames. In this case, elide the clean up and the dummy frames will
7608 be cleaned up next time this function is called from a location where
7609 unwinding is possible. */
7610
7611 void
7612 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7613 {
7614 /* We would need to delete breakpoints other than the current one while
7615 iterating, so all_breakpoints_safe is not sufficient to make that safe.
7616 Save all breakpoints to delete in that set and delete them at the end. */
7617 std::unordered_set<breakpoint *> to_delete;
7618
7619 for (struct breakpoint *b : all_breakpoints ())
7620 {
7621 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7622 {
7623 struct breakpoint *dummy_b = b->related_breakpoint;
7624
7625 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7626 chained off b->related_breakpoint. */
7627 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7628 dummy_b = dummy_b->related_breakpoint;
7629
7630 /* If there was no bp_call_dummy breakpoint then there's nothing
7631 more to do. Or, if the dummy frame associated with the
7632 bp_call_dummy is still on the stack then we need to leave this
7633 bp_call_dummy in place. */
7634 if (dummy_b->type != bp_call_dummy
7635 || frame_find_by_id (dummy_b->frame_id) != NULL)
7636 continue;
7637
7638 /* We didn't find the dummy frame on the stack, this could be
7639 because we have longjmp'd to a stack frame that is previous to
7640 the dummy frame, or it could be because the stack unwind is
7641 broken at some point between the longjmp frame and the dummy
7642 frame.
7643
7644 Next we figure out why the stack unwind stopped. If it looks
7645 like the unwind is complete then we assume the dummy frame has
7646 been jumped over, however, if the unwind stopped for an
7647 unexpected reason then we assume the stack unwind is currently
7648 broken, and that we will (eventually) return to the dummy
7649 frame.
7650
7651 It might be tempting to consider using frame_id_inner here, but
7652 that is not safe. There is no guarantee that the stack frames
7653 we are looking at here are even on the same stack as the
7654 original dummy frame, hence frame_id_inner can't be used. See
7655 the comments on frame_id_inner for more details. */
7656 bool unwind_finished_unexpectedly = false;
7657 for (frame_info_ptr fi = get_current_frame (); fi != nullptr; )
7658 {
7659 frame_info_ptr prev = get_prev_frame (fi);
7660 if (prev == nullptr)
7661 {
7662 /* FI is the last stack frame. Why did this frame not
7663 unwind further? */
7664 auto stop_reason = get_frame_unwind_stop_reason (fi);
7665 if (stop_reason != UNWIND_NO_REASON
7666 && stop_reason != UNWIND_OUTERMOST)
7667 unwind_finished_unexpectedly = true;
7668 }
7669 fi = prev;
7670 }
7671 if (unwind_finished_unexpectedly)
7672 continue;
7673
7674 dummy_frame_discard (dummy_b->frame_id, tp);
7675
7676 for (breakpoint *related_breakpoint = b->related_breakpoint;
7677 related_breakpoint != b;
7678 related_breakpoint = related_breakpoint->related_breakpoint)
7679 to_delete.insert (b->related_breakpoint);
7680
7681 to_delete.insert (b);
7682 }
7683 }
7684
7685 for (breakpoint *b : to_delete)
7686 delete_breakpoint (b);
7687 }
7688
7689 void
7690 enable_overlay_breakpoints (void)
7691 {
7692 for (breakpoint *b : all_breakpoints ())
7693 if (b->type == bp_overlay_event)
7694 {
7695 b->enable_state = bp_enabled;
7696 update_global_location_list (UGLL_MAY_INSERT);
7697 overlay_events_enabled = 1;
7698 }
7699 }
7700
7701 void
7702 disable_overlay_breakpoints (void)
7703 {
7704 for (breakpoint *b : all_breakpoints ())
7705 if (b->type == bp_overlay_event)
7706 {
7707 b->enable_state = bp_disabled;
7708 update_global_location_list (UGLL_DONT_INSERT);
7709 overlay_events_enabled = 0;
7710 }
7711 }
7712
7713 /* Set an active std::terminate breakpoint for each std::terminate
7714 master breakpoint. */
7715 void
7716 set_std_terminate_breakpoint (void)
7717 {
7718 for (breakpoint *b : all_breakpoints_safe ())
7719 if (b->pspace == current_program_space
7720 && b->type == bp_std_terminate_master)
7721 {
7722 momentary_breakpoint_from_master (b, bp_std_terminate, 1,
7723 inferior_thread ()->global_num);
7724 }
7725 }
7726
7727 /* Delete all the std::terminate breakpoints. */
7728 void
7729 delete_std_terminate_breakpoint (void)
7730 {
7731 for (breakpoint *b : all_breakpoints_safe ())
7732 if (b->type == bp_std_terminate)
7733 delete_breakpoint (b);
7734 }
7735
7736 struct breakpoint *
7737 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7738 {
7739 struct breakpoint *b;
7740
7741 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
7742
7743 b->enable_state = bp_enabled;
7744 /* locspec has to be used or breakpoint_re_set will delete me. */
7745 b->locspec = new_address_location_spec (b->loc->address, NULL, 0);
7746
7747 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7748
7749 return b;
7750 }
7751
7752 struct lang_and_radix
7753 {
7754 enum language lang;
7755 int radix;
7756 };
7757
7758 /* Create a breakpoint for JIT code registration and unregistration. */
7759
7760 struct breakpoint *
7761 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7762 {
7763 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
7764 }
7765
7766 /* Remove JIT code registration and unregistration breakpoint(s). */
7767
7768 void
7769 remove_jit_event_breakpoints (void)
7770 {
7771 for (breakpoint *b : all_breakpoints_safe ())
7772 if (b->type == bp_jit_event
7773 && b->loc->pspace == current_program_space)
7774 delete_breakpoint (b);
7775 }
7776
7777 void
7778 remove_solib_event_breakpoints (void)
7779 {
7780 for (breakpoint *b : all_breakpoints_safe ())
7781 if (b->type == bp_shlib_event
7782 && b->loc->pspace == current_program_space)
7783 delete_breakpoint (b);
7784 }
7785
7786 /* See breakpoint.h. */
7787
7788 void
7789 remove_solib_event_breakpoints_at_next_stop (void)
7790 {
7791 for (breakpoint *b : all_breakpoints_safe ())
7792 if (b->type == bp_shlib_event
7793 && b->loc->pspace == current_program_space)
7794 b->disposition = disp_del_at_next_stop;
7795 }
7796
7797 /* Helper for create_solib_event_breakpoint /
7798 create_and_insert_solib_event_breakpoint. Allows specifying which
7799 INSERT_MODE to pass through to update_global_location_list. */
7800
7801 static struct breakpoint *
7802 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7803 enum ugll_insert_mode insert_mode)
7804 {
7805 struct breakpoint *b;
7806
7807 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
7808 update_global_location_list_nothrow (insert_mode);
7809 return b;
7810 }
7811
7812 struct breakpoint *
7813 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7814 {
7815 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7816 }
7817
7818 /* See breakpoint.h. */
7819
7820 struct breakpoint *
7821 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7822 {
7823 struct breakpoint *b;
7824
7825 /* Explicitly tell update_global_location_list to insert
7826 locations. */
7827 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7828 if (!b->loc->inserted)
7829 {
7830 delete_breakpoint (b);
7831 return NULL;
7832 }
7833 return b;
7834 }
7835
7836 /* Disable any breakpoints that are on code in shared libraries. Only
7837 apply to enabled breakpoints, disabled ones can just stay disabled. */
7838
7839 void
7840 disable_breakpoints_in_shlibs (void)
7841 {
7842 for (bp_location *loc : all_bp_locations ())
7843 {
7844 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7845 struct breakpoint *b = loc->owner;
7846
7847 /* We apply the check to all breakpoints, including disabled for
7848 those with loc->duplicate set. This is so that when breakpoint
7849 becomes enabled, or the duplicate is removed, gdb will try to
7850 insert all breakpoints. If we don't set shlib_disabled here,
7851 we'll try to insert those breakpoints and fail. */
7852 if (((b->type == bp_breakpoint)
7853 || (b->type == bp_jit_event)
7854 || (b->type == bp_hardware_breakpoint)
7855 || (is_tracepoint (b)))
7856 && loc->pspace == current_program_space
7857 && !loc->shlib_disabled
7858 && solib_name_from_address (loc->pspace, loc->address)
7859 )
7860 {
7861 loc->shlib_disabled = 1;
7862 }
7863 }
7864 }
7865
7866 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7867 notification of unloaded_shlib. Only apply to enabled breakpoints,
7868 disabled ones can just stay disabled. */
7869
7870 static void
7871 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7872 {
7873 bool disabled_shlib_breaks = false;
7874
7875 for (bp_location *loc : all_bp_locations ())
7876 {
7877 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7878 struct breakpoint *b = loc->owner;
7879
7880 if (solib->pspace == loc->pspace
7881 && !loc->shlib_disabled
7882 && (((b->type == bp_breakpoint
7883 || b->type == bp_jit_event
7884 || b->type == bp_hardware_breakpoint)
7885 && (loc->loc_type == bp_loc_hardware_breakpoint
7886 || loc->loc_type == bp_loc_software_breakpoint))
7887 || is_tracepoint (b))
7888 && solib_contains_address_p (solib, loc->address))
7889 {
7890 loc->shlib_disabled = 1;
7891 /* At this point, we cannot rely on remove_breakpoint
7892 succeeding so we must mark the breakpoint as not inserted
7893 to prevent future errors occurring in remove_breakpoints. */
7894 loc->inserted = 0;
7895
7896 /* This may cause duplicate notifications for the same breakpoint. */
7897 gdb::observers::breakpoint_modified.notify (b);
7898
7899 if (!disabled_shlib_breaks)
7900 {
7901 target_terminal::ours_for_output ();
7902 warning (_("Temporarily disabling breakpoints "
7903 "for unloaded shared library \"%s\""),
7904 solib->so_name);
7905 }
7906 disabled_shlib_breaks = true;
7907 }
7908 }
7909 }
7910
7911 /* Disable any breakpoints and tracepoints in OBJFILE upon
7912 notification of free_objfile. Only apply to enabled breakpoints,
7913 disabled ones can just stay disabled. */
7914
7915 static void
7916 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7917 {
7918 if (objfile == NULL)
7919 return;
7920
7921 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7922 managed by the user with add-symbol-file/remove-symbol-file.
7923 Similarly to how breakpoints in shared libraries are handled in
7924 response to "nosharedlibrary", mark breakpoints in such modules
7925 shlib_disabled so they end up uninserted on the next global
7926 location list update. Shared libraries not loaded by the user
7927 aren't handled here -- they're already handled in
7928 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7929 solib_unloaded observer. We skip objfiles that are not
7930 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7931 main objfile). */
7932 if ((objfile->flags & OBJF_SHARED) == 0
7933 || (objfile->flags & OBJF_USERLOADED) == 0)
7934 return;
7935
7936 for (breakpoint *b : all_breakpoints ())
7937 {
7938 bool bp_modified = false;
7939
7940 if (!is_breakpoint (b) && !is_tracepoint (b))
7941 continue;
7942
7943 for (bp_location *loc : b->locations ())
7944 {
7945 CORE_ADDR loc_addr = loc->address;
7946
7947 if (loc->loc_type != bp_loc_hardware_breakpoint
7948 && loc->loc_type != bp_loc_software_breakpoint)
7949 continue;
7950
7951 if (loc->shlib_disabled != 0)
7952 continue;
7953
7954 if (objfile->pspace != loc->pspace)
7955 continue;
7956
7957 if (loc->loc_type != bp_loc_hardware_breakpoint
7958 && loc->loc_type != bp_loc_software_breakpoint)
7959 continue;
7960
7961 if (is_addr_in_objfile (loc_addr, objfile))
7962 {
7963 loc->shlib_disabled = 1;
7964 /* At this point, we don't know whether the object was
7965 unmapped from the inferior or not, so leave the
7966 inserted flag alone. We'll handle failure to
7967 uninsert quietly, in case the object was indeed
7968 unmapped. */
7969
7970 mark_breakpoint_location_modified (loc);
7971
7972 bp_modified = true;
7973 }
7974 }
7975
7976 if (bp_modified)
7977 gdb::observers::breakpoint_modified.notify (b);
7978 }
7979 }
7980
7981 /* See breakpoint.h. */
7982
7983 breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
7984 bool temp, const char *cond_string_)
7985 : type (bptype),
7986 disposition (temp ? disp_del : disp_donttouch),
7987 gdbarch (gdbarch_),
7988 language (current_language->la_language),
7989 input_radix (::input_radix),
7990 cond_string (cond_string_ != nullptr
7991 ? make_unique_xstrdup (cond_string_)
7992 : nullptr),
7993 related_breakpoint (this)
7994 {
7995 }
7996
7997 /* See breakpoint.h. */
7998
7999 catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
8000 const char *cond_string)
8001 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
8002 {
8003 add_dummy_location (this, current_program_space);
8004
8005 pspace = current_program_space;
8006 }
8007
8008 breakpoint *
8009 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8010 {
8011 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8012 set_breakpoint_number (internal, b);
8013 if (is_tracepoint (b))
8014 set_tracepoint_count (breakpoint_count);
8015 if (!internal)
8016 mention (b);
8017 gdb::observers::breakpoint_created.notify (b);
8018
8019 if (update_gll)
8020 update_global_location_list (UGLL_MAY_INSERT);
8021
8022 return b;
8023 }
8024
8025 static int
8026 hw_breakpoint_used_count (void)
8027 {
8028 int i = 0;
8029
8030 for (breakpoint *b : all_breakpoints ())
8031 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8032 for (bp_location *bl : b->locations ())
8033 {
8034 /* Special types of hardware breakpoints may use more than
8035 one register. */
8036 i += b->resources_needed (bl);
8037 }
8038
8039 return i;
8040 }
8041
8042 /* Returns the resources B would use if it were a hardware
8043 watchpoint. */
8044
8045 static int
8046 hw_watchpoint_use_count (struct breakpoint *b)
8047 {
8048 int i = 0;
8049
8050 if (!breakpoint_enabled (b))
8051 return 0;
8052
8053 for (bp_location *bl : b->locations ())
8054 {
8055 /* Special types of hardware watchpoints may use more than
8056 one register. */
8057 i += b->resources_needed (bl);
8058 }
8059
8060 return i;
8061 }
8062
8063 /* Returns the sum the used resources of all hardware watchpoints of
8064 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8065 the sum of the used resources of all hardware watchpoints of other
8066 types _not_ TYPE. */
8067
8068 static int
8069 hw_watchpoint_used_count_others (struct breakpoint *except,
8070 enum bptype type, int *other_type_used)
8071 {
8072 int i = 0;
8073
8074 *other_type_used = 0;
8075 for (breakpoint *b : all_breakpoints ())
8076 {
8077 if (b == except)
8078 continue;
8079 if (!breakpoint_enabled (b))
8080 continue;
8081
8082 if (b->type == type)
8083 i += hw_watchpoint_use_count (b);
8084 else if (is_hardware_watchpoint (b))
8085 *other_type_used = 1;
8086 }
8087
8088 return i;
8089 }
8090
8091 void
8092 disable_watchpoints_before_interactive_call_start (void)
8093 {
8094 for (breakpoint *b : all_breakpoints ())
8095 if (is_watchpoint (b) && breakpoint_enabled (b))
8096 {
8097 b->enable_state = bp_call_disabled;
8098 update_global_location_list (UGLL_DONT_INSERT);
8099 }
8100 }
8101
8102 void
8103 enable_watchpoints_after_interactive_call_stop (void)
8104 {
8105 for (breakpoint *b : all_breakpoints ())
8106 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8107 {
8108 b->enable_state = bp_enabled;
8109 update_global_location_list (UGLL_MAY_INSERT);
8110 }
8111 }
8112
8113 void
8114 disable_breakpoints_before_startup (void)
8115 {
8116 current_program_space->executing_startup = 1;
8117 update_global_location_list (UGLL_DONT_INSERT);
8118 }
8119
8120 void
8121 enable_breakpoints_after_startup (void)
8122 {
8123 current_program_space->executing_startup = 0;
8124 breakpoint_re_set ();
8125 }
8126
8127 /* Allocate a new momentary breakpoint. */
8128
8129 template<typename... Arg>
8130 static momentary_breakpoint *
8131 new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
8132 Arg&&... args)
8133 {
8134 if (type == bp_longjmp || type == bp_exception)
8135 return new longjmp_breakpoint (gdbarch, type,
8136 std::forward<Arg> (args)...);
8137 else
8138 return new momentary_breakpoint (gdbarch, type,
8139 std::forward<Arg> (args)...);
8140 }
8141
8142 /* Set a momentary breakpoint of type TYPE at address specified by
8143 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8144 frame. */
8145
8146 breakpoint_up
8147 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8148 struct frame_id frame_id, enum bptype type)
8149 {
8150 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8151 tail-called one. */
8152 gdb_assert (!frame_id_artificial_p (frame_id));
8153
8154 std::unique_ptr<momentary_breakpoint> b
8155 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
8156 inferior_thread ()->global_num));
8157
8158 b->add_location (sal);
8159
8160 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
8161
8162 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8163
8164 return bp;
8165 }
8166
8167 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8168 The new breakpoint will have type TYPE, use OPS as its
8169 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8170
8171 static struct breakpoint *
8172 momentary_breakpoint_from_master (struct breakpoint *orig,
8173 enum bptype type,
8174 int loc_enabled,
8175 int thread)
8176 {
8177 std::unique_ptr<breakpoint> copy
8178 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
8179 orig->frame_id, thread));
8180 copy->loc = copy->allocate_location ();
8181 set_breakpoint_location_function (copy->loc);
8182
8183 copy->loc->gdbarch = orig->loc->gdbarch;
8184 copy->loc->requested_address = orig->loc->requested_address;
8185 copy->loc->address = orig->loc->address;
8186 copy->loc->section = orig->loc->section;
8187 copy->loc->pspace = orig->loc->pspace;
8188 copy->loc->probe = orig->loc->probe;
8189 copy->loc->line_number = orig->loc->line_number;
8190 copy->loc->symtab = orig->loc->symtab;
8191 copy->loc->enabled = loc_enabled;
8192
8193 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
8194 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8195 return b;
8196 }
8197
8198 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8199 ORIG is NULL. */
8200
8201 struct breakpoint *
8202 clone_momentary_breakpoint (struct breakpoint *orig)
8203 {
8204 /* If there's nothing to clone, then return nothing. */
8205 if (orig == NULL)
8206 return NULL;
8207
8208 return momentary_breakpoint_from_master (orig, orig->type, 0,
8209 orig->thread);
8210 }
8211
8212 breakpoint_up
8213 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8214 enum bptype type)
8215 {
8216 struct symtab_and_line sal;
8217
8218 sal = find_pc_line (pc, 0);
8219 sal.pc = pc;
8220 sal.section = find_pc_overlay (pc);
8221 sal.explicit_pc = 1;
8222
8223 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8224 }
8225 \f
8226
8227 /* Tell the user we have just set a breakpoint B. */
8228
8229 static void
8230 mention (const breakpoint *b)
8231 {
8232 b->print_mention ();
8233 current_uiout->text ("\n");
8234 }
8235 \f
8236
8237 static bool bp_loc_is_permanent (struct bp_location *loc);
8238
8239 /* Handle "set breakpoint auto-hw on".
8240
8241 If the explicitly specified breakpoint type is not hardware
8242 breakpoint, check the memory map to see whether the breakpoint
8243 address is in read-only memory.
8244
8245 - location type is not hardware breakpoint, memory is read-only.
8246 We change the type of the location to hardware breakpoint.
8247
8248 - location type is hardware breakpoint, memory is read-write. This
8249 means we've previously made the location hardware one, but then the
8250 memory map changed, so we undo.
8251 */
8252
8253 static void
8254 handle_automatic_hardware_breakpoints (bp_location *bl)
8255 {
8256 if (automatic_hardware_breakpoints
8257 && bl->owner->type != bp_hardware_breakpoint
8258 && (bl->loc_type == bp_loc_software_breakpoint
8259 || bl->loc_type == bp_loc_hardware_breakpoint))
8260 {
8261 /* When breakpoints are removed, remove_breakpoints will use
8262 location types we've just set here, the only possible problem
8263 is that memory map has changed during running program, but
8264 it's not going to work anyway with current gdb. */
8265 mem_region *mr = lookup_mem_region (bl->address);
8266
8267 if (mr != nullptr)
8268 {
8269 enum bp_loc_type new_type;
8270
8271 if (mr->attrib.mode != MEM_RW)
8272 new_type = bp_loc_hardware_breakpoint;
8273 else
8274 new_type = bp_loc_software_breakpoint;
8275
8276 if (new_type != bl->loc_type)
8277 {
8278 static bool said = false;
8279
8280 bl->loc_type = new_type;
8281 if (!said)
8282 {
8283 gdb_printf (_("Note: automatically using "
8284 "hardware breakpoints for "
8285 "read-only addresses.\n"));
8286 said = true;
8287 }
8288 }
8289 }
8290 }
8291 }
8292
8293 bp_location *
8294 code_breakpoint::add_location (const symtab_and_line &sal)
8295 {
8296 struct bp_location *new_loc, **tmp;
8297 CORE_ADDR adjusted_address;
8298 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8299
8300 if (loc_gdbarch == NULL)
8301 loc_gdbarch = gdbarch;
8302
8303 /* Adjust the breakpoint's address prior to allocating a location.
8304 Once we call allocate_location(), that mostly uninitialized
8305 location will be placed on the location chain. Adjustment of the
8306 breakpoint may cause target_read_memory() to be called and we do
8307 not want its scan of the location chain to find a breakpoint and
8308 location that's only been partially initialized. */
8309 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8310 sal.pc, type,
8311 sal.pspace);
8312
8313 /* Sort the locations by their ADDRESS. */
8314 new_loc = allocate_location ();
8315 for (tmp = &(loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8316 tmp = &((*tmp)->next))
8317 ;
8318 new_loc->next = *tmp;
8319 *tmp = new_loc;
8320
8321 new_loc->requested_address = sal.pc;
8322 new_loc->address = adjusted_address;
8323 new_loc->pspace = sal.pspace;
8324 new_loc->probe.prob = sal.prob;
8325 new_loc->probe.objfile = sal.objfile;
8326 gdb_assert (new_loc->pspace != NULL);
8327 new_loc->section = sal.section;
8328 new_loc->gdbarch = loc_gdbarch;
8329 new_loc->line_number = sal.line;
8330 new_loc->symtab = sal.symtab;
8331 new_loc->symbol = sal.symbol;
8332 new_loc->msymbol = sal.msymbol;
8333 new_loc->objfile = sal.objfile;
8334
8335 set_breakpoint_location_function (new_loc);
8336
8337 /* While by definition, permanent breakpoints are already present in the
8338 code, we don't mark the location as inserted. Normally one would expect
8339 that GDB could rely on that breakpoint instruction to stop the program,
8340 thus removing the need to insert its own breakpoint, except that executing
8341 the breakpoint instruction can kill the target instead of reporting a
8342 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8343 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8344 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8345 breakpoint be inserted normally results in QEMU knowing about the GDB
8346 breakpoint, and thus trap before the breakpoint instruction is executed.
8347 (If GDB later needs to continue execution past the permanent breakpoint,
8348 it manually increments the PC, thus avoiding executing the breakpoint
8349 instruction.) */
8350 if (bp_loc_is_permanent (new_loc))
8351 new_loc->permanent = 1;
8352
8353 return new_loc;
8354 }
8355 \f
8356
8357 /* Return true if LOC is pointing to a permanent breakpoint,
8358 return false otherwise. */
8359
8360 static bool
8361 bp_loc_is_permanent (struct bp_location *loc)
8362 {
8363 gdb_assert (loc != NULL);
8364
8365 /* If we have a non-breakpoint-backed catchpoint or a software
8366 watchpoint, just return 0. We should not attempt to read from
8367 the addresses the locations of these breakpoint types point to.
8368 gdbarch_program_breakpoint_here_p, below, will attempt to read
8369 memory. */
8370 if (!bl_address_is_meaningful (loc))
8371 return false;
8372
8373 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8374 switch_to_program_space_and_thread (loc->pspace);
8375 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8376 }
8377
8378 /* Build a command list for the dprintf corresponding to the current
8379 settings of the dprintf style options. */
8380
8381 static void
8382 update_dprintf_command_list (struct breakpoint *b)
8383 {
8384 const char *dprintf_args = b->extra_string.get ();
8385 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8386
8387 if (!dprintf_args)
8388 return;
8389
8390 dprintf_args = skip_spaces (dprintf_args);
8391
8392 /* Allow a comma, as it may have terminated a location, but don't
8393 insist on it. */
8394 if (*dprintf_args == ',')
8395 ++dprintf_args;
8396 dprintf_args = skip_spaces (dprintf_args);
8397
8398 if (*dprintf_args != '"')
8399 error (_("Bad format string, missing '\"'."));
8400
8401 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8402 printf_line = xstrprintf ("printf %s", dprintf_args);
8403 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8404 {
8405 if (dprintf_function.empty ())
8406 error (_("No function supplied for dprintf call"));
8407
8408 if (!dprintf_channel.empty ())
8409 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8410 dprintf_function.c_str (),
8411 dprintf_channel.c_str (),
8412 dprintf_args);
8413 else
8414 printf_line = xstrprintf ("call (void) %s (%s)",
8415 dprintf_function.c_str (),
8416 dprintf_args);
8417 }
8418 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8419 {
8420 if (target_can_run_breakpoint_commands ())
8421 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8422 else
8423 {
8424 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8425 printf_line = xstrprintf ("printf %s", dprintf_args);
8426 }
8427 }
8428 else
8429 internal_error (_("Invalid dprintf style."));
8430
8431 gdb_assert (printf_line != NULL);
8432
8433 /* Manufacture a printf sequence. */
8434 struct command_line *printf_cmd_line
8435 = new struct command_line (simple_control, printf_line.release ());
8436 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8437 command_lines_deleter ()));
8438 }
8439
8440 /* Update all dprintf commands, making their command lists reflect
8441 current style settings. */
8442
8443 static void
8444 update_dprintf_commands (const char *args, int from_tty,
8445 struct cmd_list_element *c)
8446 {
8447 for (breakpoint *b : all_breakpoints ())
8448 if (b->type == bp_dprintf)
8449 update_dprintf_command_list (b);
8450 }
8451
8452 code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
8453 enum bptype type_,
8454 gdb::array_view<const symtab_and_line> sals,
8455 location_spec_up &&locspec_,
8456 gdb::unique_xmalloc_ptr<char> filter_,
8457 gdb::unique_xmalloc_ptr<char> cond_string_,
8458 gdb::unique_xmalloc_ptr<char> extra_string_,
8459 enum bpdisp disposition_,
8460 int thread_, int task_, int ignore_count_,
8461 int from_tty,
8462 int enabled_, unsigned flags,
8463 int display_canonical_)
8464 : breakpoint (gdbarch_, type_)
8465 {
8466 int i;
8467
8468 if (type == bp_hardware_breakpoint)
8469 {
8470 int target_resources_ok;
8471
8472 i = hw_breakpoint_used_count ();
8473 target_resources_ok =
8474 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8475 i + 1, 0);
8476 if (target_resources_ok == 0)
8477 error (_("No hardware breakpoint support in the target."));
8478 else if (target_resources_ok < 0)
8479 error (_("Hardware breakpoints used exceeds limit."));
8480 }
8481
8482 gdb_assert (!sals.empty ());
8483
8484 /* At most one of thread or task can be set on any breakpoint. */
8485 gdb_assert (thread == -1 || task == -1);
8486 thread = thread_;
8487 task = task_;
8488
8489 cond_string = std::move (cond_string_);
8490 extra_string = std::move (extra_string_);
8491 ignore_count = ignore_count_;
8492 enable_state = enabled_ ? bp_enabled : bp_disabled;
8493 disposition = disposition_;
8494
8495 if (type == bp_static_tracepoint
8496 || type == bp_static_marker_tracepoint)
8497 {
8498 auto *t = gdb::checked_static_cast<struct tracepoint *> (this);
8499 struct static_tracepoint_marker marker;
8500
8501 if (strace_marker_p (this))
8502 {
8503 /* We already know the marker exists, otherwise, we wouldn't
8504 see a sal for it. */
8505 const char *p = &locspec_->to_string ()[3];
8506 const char *endp;
8507
8508 p = skip_spaces (p);
8509
8510 endp = skip_to_space (p);
8511
8512 t->static_trace_marker_id.assign (p, endp - p);
8513
8514 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8515 t->static_trace_marker_id.c_str ());
8516 }
8517 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8518 {
8519 t->static_trace_marker_id = std::move (marker.str_id);
8520
8521 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8522 t->static_trace_marker_id.c_str ());
8523 }
8524 else
8525 warning (_("Couldn't determine the static tracepoint marker to probe"));
8526 }
8527
8528 for (const auto &sal : sals)
8529 {
8530 if (from_tty)
8531 {
8532 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8533 if (loc_gdbarch == nullptr)
8534 loc_gdbarch = gdbarch;
8535
8536 describe_other_breakpoints (loc_gdbarch,
8537 sal.pspace, sal.pc, sal.section, thread);
8538 }
8539
8540 bp_location *new_loc = add_location (sal);
8541 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8542 new_loc->inserted = 1;
8543
8544 /* Do not set breakpoint locations conditions yet. As locations
8545 are inserted, they get sorted based on their addresses. Let
8546 the list stabilize to have reliable location numbers. */
8547
8548 /* Dynamic printf requires and uses additional arguments on the
8549 command line, otherwise it's an error. */
8550 if (type == bp_dprintf)
8551 {
8552 if (extra_string != nullptr)
8553 update_dprintf_command_list (this);
8554 else
8555 error (_("Format string required"));
8556 }
8557 else if (extra_string != nullptr)
8558 error (_("Garbage '%s' at end of command"), extra_string.get ());
8559 }
8560
8561 /* The order of the locations is now stable. Set the location
8562 condition using the location's number. */
8563 int loc_num = 1;
8564 for (bp_location *bl : locations ())
8565 {
8566 if (cond_string != nullptr)
8567 set_breakpoint_location_condition (cond_string.get (), bl,
8568 number, loc_num);
8569
8570 ++loc_num;
8571 }
8572
8573 display_canonical = display_canonical_;
8574 if (locspec_ != nullptr)
8575 locspec = std::move (locspec_);
8576 else
8577 locspec = new_address_location_spec (this->loc->address, NULL, 0);
8578 filter = std::move (filter_);
8579 }
8580
8581 static void
8582 create_breakpoint_sal (struct gdbarch *gdbarch,
8583 gdb::array_view<const symtab_and_line> sals,
8584 location_spec_up &&locspec,
8585 gdb::unique_xmalloc_ptr<char> filter,
8586 gdb::unique_xmalloc_ptr<char> cond_string,
8587 gdb::unique_xmalloc_ptr<char> extra_string,
8588 enum bptype type, enum bpdisp disposition,
8589 int thread, int task, int ignore_count,
8590 int from_tty,
8591 int enabled, int internal, unsigned flags,
8592 int display_canonical)
8593 {
8594 std::unique_ptr<code_breakpoint> b
8595 = new_breakpoint_from_type (gdbarch,
8596 type,
8597 sals,
8598 std::move (locspec),
8599 std::move (filter),
8600 std::move (cond_string),
8601 std::move (extra_string),
8602 disposition,
8603 thread, task, ignore_count,
8604 from_tty,
8605 enabled, flags,
8606 display_canonical);
8607
8608 install_breakpoint (internal, std::move (b), 0);
8609 }
8610
8611 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8612 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8613 value. COND_STRING, if not NULL, specified the condition to be
8614 used for all breakpoints. Essentially the only case where
8615 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8616 function. In that case, it's still not possible to specify
8617 separate conditions for different overloaded functions, so
8618 we take just a single condition string.
8619
8620 NOTE: If the function succeeds, the caller is expected to cleanup
8621 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8622 array contents). If the function fails (error() is called), the
8623 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8624 COND and SALS arrays and each of those arrays contents. */
8625
8626 static void
8627 create_breakpoints_sal (struct gdbarch *gdbarch,
8628 struct linespec_result *canonical,
8629 gdb::unique_xmalloc_ptr<char> cond_string,
8630 gdb::unique_xmalloc_ptr<char> extra_string,
8631 enum bptype type, enum bpdisp disposition,
8632 int thread, int task, int ignore_count,
8633 int from_tty,
8634 int enabled, int internal, unsigned flags)
8635 {
8636 if (canonical->pre_expanded)
8637 gdb_assert (canonical->lsals.size () == 1);
8638
8639 for (const auto &lsal : canonical->lsals)
8640 {
8641 /* Note that 'location' can be NULL in the case of a plain
8642 'break', without arguments. */
8643 location_spec_up locspec
8644 = (canonical->locspec != nullptr
8645 ? canonical->locspec->clone ()
8646 : nullptr);
8647 gdb::unique_xmalloc_ptr<char> filter_string
8648 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8649
8650 create_breakpoint_sal (gdbarch, lsal.sals,
8651 std::move (locspec),
8652 std::move (filter_string),
8653 std::move (cond_string),
8654 std::move (extra_string),
8655 type, disposition,
8656 thread, task, ignore_count,
8657 from_tty, enabled, internal, flags,
8658 canonical->special_display);
8659 }
8660 }
8661
8662 /* Parse LOCSPEC which is assumed to be a SAL specification possibly
8663 followed by conditionals. On return, SALS contains an array of SAL
8664 addresses found. LOCSPEC points to the end of the SAL (for
8665 linespec locspecs).
8666
8667 The array and the line spec strings are allocated on the heap, it is
8668 the caller's responsibility to free them. */
8669
8670 static void
8671 parse_breakpoint_sals (location_spec *locspec,
8672 struct linespec_result *canonical)
8673 {
8674 struct symtab_and_line cursal;
8675
8676 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8677 {
8678 const char *spec = as_linespec_location_spec (locspec)->spec_string;
8679
8680 if (spec == NULL)
8681 {
8682 /* The last displayed codepoint, if it's valid, is our default
8683 breakpoint address. */
8684 if (last_displayed_sal_is_valid ())
8685 {
8686 /* Set sal's pspace, pc, symtab, and line to the values
8687 corresponding to the last call to print_frame_info.
8688 Be sure to reinitialize LINE with NOTCURRENT == 0
8689 as the breakpoint line number is inappropriate otherwise.
8690 find_pc_line would adjust PC, re-set it back. */
8691 symtab_and_line sal = get_last_displayed_sal ();
8692 CORE_ADDR pc = sal.pc;
8693
8694 sal = find_pc_line (pc, 0);
8695
8696 /* "break" without arguments is equivalent to "break *PC"
8697 where PC is the last displayed codepoint's address. So
8698 make sure to set sal.explicit_pc to prevent GDB from
8699 trying to expand the list of sals to include all other
8700 instances with the same symtab and line. */
8701 sal.pc = pc;
8702 sal.explicit_pc = 1;
8703
8704 struct linespec_sals lsal;
8705 lsal.sals = {sal};
8706 lsal.canonical = NULL;
8707
8708 canonical->lsals.push_back (std::move (lsal));
8709 return;
8710 }
8711 else
8712 error (_("No default breakpoint address now."));
8713 }
8714 }
8715
8716 /* Force almost all breakpoints to be in terms of the
8717 current_source_symtab (which is decode_line_1's default).
8718 This should produce the results we want almost all of the
8719 time while leaving default_breakpoint_* alone.
8720
8721 ObjC: However, don't match an Objective-C method name which
8722 may have a '+' or '-' succeeded by a '['. */
8723 cursal = get_current_source_symtab_and_line ();
8724 if (last_displayed_sal_is_valid ())
8725 {
8726 const char *spec = NULL;
8727
8728 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8729 spec = as_linespec_location_spec (locspec)->spec_string;
8730
8731 if (!cursal.symtab
8732 || (spec != NULL
8733 && strchr ("+-", spec[0]) != NULL
8734 && spec[1] != '['))
8735 {
8736 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
8737 get_last_displayed_symtab (),
8738 get_last_displayed_line (),
8739 canonical, NULL, NULL);
8740 return;
8741 }
8742 }
8743
8744 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
8745 cursal.symtab, cursal.line, canonical, NULL, NULL);
8746 }
8747
8748
8749 /* Convert each SAL into a real PC. Verify that the PC can be
8750 inserted as a breakpoint. If it can't throw an error. */
8751
8752 static void
8753 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
8754 {
8755 for (auto &sal : sals)
8756 resolve_sal_pc (&sal);
8757 }
8758
8759 /* Fast tracepoints may have restrictions on valid locations. For
8760 instance, a fast tracepoint using a jump instead of a trap will
8761 likely have to overwrite more bytes than a trap would, and so can
8762 only be placed where the instruction is longer than the jump, or a
8763 multi-instruction sequence does not have a jump into the middle of
8764 it, etc. */
8765
8766 static void
8767 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8768 gdb::array_view<const symtab_and_line> sals)
8769 {
8770 for (const auto &sal : sals)
8771 {
8772 struct gdbarch *sarch;
8773
8774 sarch = get_sal_arch (sal);
8775 /* We fall back to GDBARCH if there is no architecture
8776 associated with SAL. */
8777 if (sarch == NULL)
8778 sarch = gdbarch;
8779 std::string msg;
8780 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
8781 error (_("May not have a fast tracepoint at %s%s"),
8782 paddress (sarch, sal.pc), msg.c_str ());
8783 }
8784 }
8785
8786 /* Given TOK, a string specification of condition and thread, as
8787 accepted by the 'break' command, extract the condition
8788 string and thread number and set *COND_STRING and *THREAD.
8789 PC identifies the context at which the condition should be parsed.
8790 If no condition is found, *COND_STRING is set to NULL.
8791 If no thread is found, *THREAD is set to -1. */
8792
8793 static void
8794 find_condition_and_thread (const char *tok, CORE_ADDR pc,
8795 gdb::unique_xmalloc_ptr<char> *cond_string,
8796 int *thread, int *task,
8797 gdb::unique_xmalloc_ptr<char> *rest)
8798 {
8799 cond_string->reset ();
8800 *thread = -1;
8801 *task = -1;
8802 rest->reset ();
8803 bool force = false;
8804
8805 while (tok && *tok)
8806 {
8807 const char *end_tok;
8808 int toklen;
8809 const char *cond_start = NULL;
8810 const char *cond_end = NULL;
8811
8812 tok = skip_spaces (tok);
8813
8814 if ((*tok == '"' || *tok == ',') && rest)
8815 {
8816 rest->reset (savestring (tok, strlen (tok)));
8817 return;
8818 }
8819
8820 end_tok = skip_to_space (tok);
8821
8822 toklen = end_tok - tok;
8823
8824 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8825 {
8826 tok = cond_start = end_tok + 1;
8827 try
8828 {
8829 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8830 }
8831 catch (const gdb_exception_error &)
8832 {
8833 if (!force)
8834 throw;
8835 else
8836 tok = tok + strlen (tok);
8837 }
8838 cond_end = tok;
8839 cond_string->reset (savestring (cond_start, cond_end - cond_start));
8840 }
8841 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8842 {
8843 tok = tok + toklen;
8844 force = true;
8845 }
8846 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8847 {
8848 const char *tmptok;
8849 struct thread_info *thr;
8850
8851 if (*thread != -1)
8852 error(_("You can specify only one thread."));
8853
8854 if (*task != -1)
8855 error (_("You can specify only one of thread or task."));
8856
8857 tok = end_tok + 1;
8858 thr = parse_thread_id (tok, &tmptok);
8859 if (tok == tmptok)
8860 error (_("Junk after thread keyword."));
8861 *thread = thr->global_num;
8862 tok = tmptok;
8863 }
8864 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8865 {
8866 char *tmptok;
8867
8868 if (*task != -1)
8869 error(_("You can specify only one task."));
8870
8871 if (*thread != -1)
8872 error (_("You can specify only one of thread or task."));
8873
8874 tok = end_tok + 1;
8875 *task = strtol (tok, &tmptok, 0);
8876 if (tok == tmptok)
8877 error (_("Junk after task keyword."));
8878 if (!valid_task_id (*task))
8879 error (_("Unknown task %d."), *task);
8880 tok = tmptok;
8881 }
8882 else if (rest)
8883 {
8884 rest->reset (savestring (tok, strlen (tok)));
8885 return;
8886 }
8887 else
8888 error (_("Junk at end of arguments."));
8889 }
8890 }
8891
8892 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
8893 succeeds. The parsed values are written to COND_STRING, THREAD,
8894 TASK, and REST. See the comment of 'find_condition_and_thread'
8895 for the description of these parameters and INPUT. */
8896
8897 static void
8898 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
8899 const char *input,
8900 gdb::unique_xmalloc_ptr<char> *cond_string,
8901 int *thread, int *task,
8902 gdb::unique_xmalloc_ptr<char> *rest)
8903 {
8904 int num_failures = 0;
8905 for (auto &sal : sals)
8906 {
8907 gdb::unique_xmalloc_ptr<char> cond;
8908 int thread_id = -1;
8909 int task_id = -1;
8910 gdb::unique_xmalloc_ptr<char> remaining;
8911
8912 /* Here we want to parse 'arg' to separate condition from thread
8913 number. But because parsing happens in a context and the
8914 contexts of sals might be different, try each until there is
8915 success. Finding one successful parse is sufficient for our
8916 goal. When setting the breakpoint we'll re-parse the
8917 condition in the context of each sal. */
8918 try
8919 {
8920 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8921 &task_id, &remaining);
8922 *cond_string = std::move (cond);
8923 /* At most one of thread or task can be set. */
8924 gdb_assert (thread_id == -1 || task_id == -1);
8925 *thread = thread_id;
8926 *task = task_id;
8927 *rest = std::move (remaining);
8928 break;
8929 }
8930 catch (const gdb_exception_error &e)
8931 {
8932 num_failures++;
8933 /* If no sal remains, do not continue. */
8934 if (num_failures == sals.size ())
8935 throw;
8936 }
8937 }
8938 }
8939
8940 /* Decode a static tracepoint marker spec. */
8941
8942 static std::vector<symtab_and_line>
8943 decode_static_tracepoint_spec (const char **arg_p)
8944 {
8945 const char *p = &(*arg_p)[3];
8946 const char *endp;
8947
8948 p = skip_spaces (p);
8949
8950 endp = skip_to_space (p);
8951
8952 std::string marker_str (p, endp - p);
8953
8954 std::vector<static_tracepoint_marker> markers
8955 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8956 if (markers.empty ())
8957 error (_("No known static tracepoint marker named %s"),
8958 marker_str.c_str ());
8959
8960 std::vector<symtab_and_line> sals;
8961 sals.reserve (markers.size ());
8962
8963 for (const static_tracepoint_marker &marker : markers)
8964 {
8965 symtab_and_line sal = find_pc_line (marker.address, 0);
8966 sal.pc = marker.address;
8967 sals.push_back (sal);
8968 }
8969
8970 *arg_p = endp;
8971 return sals;
8972 }
8973
8974 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8975 according to IS_TRACEPOINT. */
8976
8977 static const struct breakpoint_ops *
8978 breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
8979 bool is_tracepoint)
8980 {
8981 if (is_tracepoint)
8982 {
8983 if (locspec_type == PROBE_LOCATION_SPEC)
8984 return &tracepoint_probe_breakpoint_ops;
8985 else
8986 return &code_breakpoint_ops;
8987 }
8988 else
8989 {
8990 if (locspec_type == PROBE_LOCATION_SPEC)
8991 return &bkpt_probe_breakpoint_ops;
8992 else
8993 return &code_breakpoint_ops;
8994 }
8995 }
8996
8997 /* See breakpoint.h. */
8998
8999 const struct breakpoint_ops *
9000 breakpoint_ops_for_location_spec (const location_spec *locspec,
9001 bool is_tracepoint)
9002 {
9003 if (locspec != nullptr)
9004 return (breakpoint_ops_for_location_spec_type
9005 (locspec->type (), is_tracepoint));
9006 return &code_breakpoint_ops;
9007 }
9008
9009 /* See breakpoint.h. */
9010
9011 int
9012 create_breakpoint (struct gdbarch *gdbarch,
9013 location_spec *locspec,
9014 const char *cond_string,
9015 int thread, const char *extra_string,
9016 bool force_condition, int parse_extra,
9017 int tempflag, enum bptype type_wanted,
9018 int ignore_count,
9019 enum auto_boolean pending_break_support,
9020 const struct breakpoint_ops *ops,
9021 int from_tty, int enabled, int internal,
9022 unsigned flags)
9023 {
9024 struct linespec_result canonical;
9025 bool pending = false;
9026 int task = -1;
9027 int prev_bkpt_count = breakpoint_count;
9028
9029 gdb_assert (ops != NULL);
9030
9031 /* If extra_string isn't useful, set it to NULL. */
9032 if (extra_string != NULL && *extra_string == '\0')
9033 extra_string = NULL;
9034
9035 try
9036 {
9037 ops->create_sals_from_location_spec (locspec, &canonical);
9038 }
9039 catch (const gdb_exception_error &e)
9040 {
9041 /* If caller is interested in rc value from parse, set
9042 value. */
9043 if (e.error == NOT_FOUND_ERROR)
9044 {
9045 /* If pending breakpoint support is turned off, throw
9046 error. */
9047
9048 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9049 throw;
9050
9051 exception_print (gdb_stderr, e);
9052
9053 /* If pending breakpoint support is auto query and the user
9054 selects no, then simply return the error code. */
9055 if (pending_break_support == AUTO_BOOLEAN_AUTO
9056 && !nquery (_("Make %s pending on future shared library load? "),
9057 bptype_string (type_wanted)))
9058 return 0;
9059
9060 /* At this point, either the user was queried about setting
9061 a pending breakpoint and selected yes, or pending
9062 breakpoint behavior is on and thus a pending breakpoint
9063 is defaulted on behalf of the user. */
9064 pending = true;
9065 }
9066 else
9067 throw;
9068 }
9069
9070 if (!pending && canonical.lsals.empty ())
9071 return 0;
9072
9073 /* Resolve all line numbers to PC's and verify that the addresses
9074 are ok for the target. */
9075 if (!pending)
9076 {
9077 for (auto &lsal : canonical.lsals)
9078 breakpoint_sals_to_pc (lsal.sals);
9079 }
9080
9081 /* Fast tracepoints may have additional restrictions on location. */
9082 if (!pending && type_wanted == bp_fast_tracepoint)
9083 {
9084 for (const auto &lsal : canonical.lsals)
9085 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9086 }
9087
9088 /* Verify that condition can be parsed, before setting any
9089 breakpoints. Allocate a separate condition expression for each
9090 breakpoint. */
9091 if (!pending)
9092 {
9093 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9094 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9095
9096 if (parse_extra)
9097 {
9098 gdb::unique_xmalloc_ptr<char> rest;
9099 gdb::unique_xmalloc_ptr<char> cond;
9100
9101 const linespec_sals &lsal = canonical.lsals[0];
9102
9103 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9104 &cond, &thread, &task, &rest);
9105 cond_string_copy = std::move (cond);
9106 extra_string_copy = std::move (rest);
9107 }
9108 else
9109 {
9110 if (type_wanted != bp_dprintf
9111 && extra_string != NULL && *extra_string != '\0')
9112 error (_("Garbage '%s' at end of location"), extra_string);
9113
9114 /* Check the validity of the condition. We should error out
9115 if the condition is invalid at all of the locations and
9116 if it is not forced. In the PARSE_EXTRA case above, this
9117 check is done when parsing the EXTRA_STRING. */
9118 if (cond_string != nullptr && !force_condition)
9119 {
9120 int num_failures = 0;
9121 const linespec_sals &lsal = canonical.lsals[0];
9122 for (const auto &sal : lsal.sals)
9123 {
9124 const char *cond = cond_string;
9125 try
9126 {
9127 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9128 /* One success is sufficient to keep going. */
9129 break;
9130 }
9131 catch (const gdb_exception_error &)
9132 {
9133 num_failures++;
9134 /* If this is the last sal, error out. */
9135 if (num_failures == lsal.sals.size ())
9136 throw;
9137 }
9138 }
9139 }
9140
9141 /* Create a private copy of condition string. */
9142 if (cond_string)
9143 cond_string_copy.reset (xstrdup (cond_string));
9144 /* Create a private copy of any extra string. */
9145 if (extra_string)
9146 extra_string_copy.reset (xstrdup (extra_string));
9147 }
9148
9149 ops->create_breakpoints_sal (gdbarch, &canonical,
9150 std::move (cond_string_copy),
9151 std::move (extra_string_copy),
9152 type_wanted,
9153 tempflag ? disp_del : disp_donttouch,
9154 thread, task, ignore_count,
9155 from_tty, enabled, internal, flags);
9156 }
9157 else
9158 {
9159 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
9160 type_wanted);
9161 b->locspec = locspec->clone ();
9162
9163 if (parse_extra)
9164 b->cond_string = NULL;
9165 else
9166 {
9167 /* Create a private copy of condition string. */
9168 b->cond_string.reset (cond_string != NULL
9169 ? xstrdup (cond_string)
9170 : NULL);
9171 b->thread = thread;
9172 }
9173
9174 /* Create a private copy of any extra string. */
9175 b->extra_string.reset (extra_string != NULL
9176 ? xstrdup (extra_string)
9177 : NULL);
9178 b->ignore_count = ignore_count;
9179 b->disposition = tempflag ? disp_del : disp_donttouch;
9180 b->condition_not_parsed = 1;
9181 b->enable_state = enabled ? bp_enabled : bp_disabled;
9182 if ((type_wanted != bp_breakpoint
9183 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9184 b->pspace = current_program_space;
9185
9186 install_breakpoint (internal, std::move (b), 0);
9187 }
9188
9189 if (canonical.lsals.size () > 1)
9190 {
9191 warning (_("Multiple breakpoints were set.\nUse the "
9192 "\"delete\" command to delete unwanted breakpoints."));
9193 prev_breakpoint_count = prev_bkpt_count;
9194 }
9195
9196 update_global_location_list (UGLL_MAY_INSERT);
9197
9198 return 1;
9199 }
9200
9201 /* Set a breakpoint.
9202 ARG is a string describing breakpoint address,
9203 condition, and thread.
9204 FLAG specifies if a breakpoint is hardware on,
9205 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9206 and BP_TEMPFLAG. */
9207
9208 static void
9209 break_command_1 (const char *arg, int flag, int from_tty)
9210 {
9211 int tempflag = flag & BP_TEMPFLAG;
9212 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9213 ? bp_hardware_breakpoint
9214 : bp_breakpoint);
9215
9216 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9217 const struct breakpoint_ops *ops
9218 = breakpoint_ops_for_location_spec (locspec.get (),
9219 false /* is_tracepoint */);
9220
9221 create_breakpoint (get_current_arch (),
9222 locspec.get (),
9223 NULL, 0, arg, false, 1 /* parse arg */,
9224 tempflag, type_wanted,
9225 0 /* Ignore count */,
9226 pending_break_support,
9227 ops,
9228 from_tty,
9229 1 /* enabled */,
9230 0 /* internal */,
9231 0);
9232 }
9233
9234 /* Helper function for break_command_1 and disassemble_command. */
9235
9236 void
9237 resolve_sal_pc (struct symtab_and_line *sal)
9238 {
9239 CORE_ADDR pc;
9240
9241 if (sal->pc == 0 && sal->symtab != NULL)
9242 {
9243 if (!find_line_pc (sal->symtab, sal->line, &pc))
9244 error (_("No line %d in file \"%s\"."),
9245 sal->line, symtab_to_filename_for_display (sal->symtab));
9246 sal->pc = pc;
9247
9248 /* If this SAL corresponds to a breakpoint inserted using a line
9249 number, then skip the function prologue if necessary. */
9250 if (sal->explicit_line)
9251 skip_prologue_sal (sal);
9252 }
9253
9254 if (sal->section == 0 && sal->symtab != NULL)
9255 {
9256 const struct blockvector *bv;
9257 const struct block *b;
9258 struct symbol *sym;
9259
9260 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9261 sal->symtab->compunit ());
9262 if (bv != NULL)
9263 {
9264 sym = b->linkage_function ();
9265 if (sym != NULL)
9266 sal->section
9267 = sym->obj_section (sal->symtab->compunit ()->objfile ());
9268 else
9269 {
9270 /* It really is worthwhile to have the section, so we'll
9271 just have to look harder. This case can be executed
9272 if we have line numbers but no functions (as can
9273 happen in assembly source). */
9274
9275 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9276 switch_to_program_space_and_thread (sal->pspace);
9277
9278 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9279 if (msym.minsym)
9280 sal->section = msym.obj_section ();
9281 }
9282 }
9283 }
9284 }
9285
9286 void
9287 break_command (const char *arg, int from_tty)
9288 {
9289 break_command_1 (arg, 0, from_tty);
9290 }
9291
9292 void
9293 tbreak_command (const char *arg, int from_tty)
9294 {
9295 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9296 }
9297
9298 static void
9299 hbreak_command (const char *arg, int from_tty)
9300 {
9301 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9302 }
9303
9304 static void
9305 thbreak_command (const char *arg, int from_tty)
9306 {
9307 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9308 }
9309
9310 /* The dynamic printf command is mostly like a regular breakpoint, but
9311 with a prewired command list consisting of a single output command,
9312 built from extra arguments supplied on the dprintf command
9313 line. */
9314
9315 static void
9316 dprintf_command (const char *arg, int from_tty)
9317 {
9318 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9319
9320 /* If non-NULL, ARG should have been advanced past the location;
9321 the next character must be ','. */
9322 if (arg != NULL)
9323 {
9324 if (arg[0] != ',' || arg[1] == '\0')
9325 error (_("Format string required"));
9326 else
9327 {
9328 /* Skip the comma. */
9329 ++arg;
9330 }
9331 }
9332
9333 create_breakpoint (get_current_arch (),
9334 locspec.get (),
9335 NULL, 0, arg, false, 1 /* parse arg */,
9336 0, bp_dprintf,
9337 0 /* Ignore count */,
9338 pending_break_support,
9339 &code_breakpoint_ops,
9340 from_tty,
9341 1 /* enabled */,
9342 0 /* internal */,
9343 0);
9344 }
9345
9346 static void
9347 agent_printf_command (const char *arg, int from_tty)
9348 {
9349 error (_("May only run agent-printf on the target"));
9350 }
9351
9352 /* Implement the "breakpoint_hit" method for ranged breakpoints. */
9353
9354 int
9355 ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9356 const address_space *aspace,
9357 CORE_ADDR bp_addr,
9358 const target_waitstatus &ws)
9359 {
9360 if (ws.kind () != TARGET_WAITKIND_STOPPED
9361 || ws.sig () != GDB_SIGNAL_TRAP)
9362 return 0;
9363
9364 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9365 bl->length, aspace, bp_addr);
9366 }
9367
9368 /* Implement the "resources_needed" method for ranged breakpoints. */
9369
9370 int
9371 ranged_breakpoint::resources_needed (const struct bp_location *bl)
9372 {
9373 return target_ranged_break_num_registers ();
9374 }
9375
9376 /* Implement the "print_it" method for ranged breakpoints. */
9377
9378 enum print_stop_action
9379 ranged_breakpoint::print_it (const bpstat *bs) const
9380 {
9381 struct bp_location *bl = loc;
9382 struct ui_out *uiout = current_uiout;
9383
9384 gdb_assert (type == bp_hardware_breakpoint);
9385
9386 /* Ranged breakpoints have only one location. */
9387 gdb_assert (bl && bl->next == NULL);
9388
9389 annotate_breakpoint (number);
9390
9391 maybe_print_thread_hit_breakpoint (uiout);
9392
9393 if (disposition == disp_del)
9394 uiout->text ("Temporary ranged breakpoint ");
9395 else
9396 uiout->text ("Ranged breakpoint ");
9397 if (uiout->is_mi_like_p ())
9398 {
9399 uiout->field_string ("reason",
9400 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9401 uiout->field_string ("disp", bpdisp_text (disposition));
9402 }
9403 print_num_locno (bs, uiout);
9404 uiout->text (", ");
9405
9406 return PRINT_SRC_AND_LOC;
9407 }
9408
9409 /* Implement the "print_one" method for ranged breakpoints. */
9410
9411 bool
9412 ranged_breakpoint::print_one (bp_location **last_loc) const
9413 {
9414 struct bp_location *bl = loc;
9415 struct value_print_options opts;
9416 struct ui_out *uiout = current_uiout;
9417
9418 /* Ranged breakpoints have only one location. */
9419 gdb_assert (bl && bl->next == NULL);
9420
9421 get_user_print_options (&opts);
9422
9423 if (opts.addressprint)
9424 /* We don't print the address range here, it will be printed later
9425 by ranged_breakpoint::print_one_detail. */
9426 uiout->field_skip ("addr");
9427 annotate_field (5);
9428 print_breakpoint_location (this, bl);
9429 *last_loc = bl;
9430
9431 return true;
9432 }
9433
9434 /* Implement the "print_one_detail" method for ranged breakpoints. */
9435
9436 void
9437 ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
9438 {
9439 CORE_ADDR address_start, address_end;
9440 struct bp_location *bl = loc;
9441 string_file stb;
9442
9443 gdb_assert (bl);
9444
9445 address_start = bl->address;
9446 address_end = address_start + bl->length - 1;
9447
9448 uiout->text ("\taddress range: ");
9449 stb.printf ("[%s, %s]",
9450 print_core_address (bl->gdbarch, address_start),
9451 print_core_address (bl->gdbarch, address_end));
9452 uiout->field_stream ("addr", stb);
9453 uiout->text ("\n");
9454 }
9455
9456 /* Implement the "print_mention" method for ranged breakpoints. */
9457
9458 void
9459 ranged_breakpoint::print_mention () const
9460 {
9461 struct bp_location *bl = loc;
9462 struct ui_out *uiout = current_uiout;
9463
9464 gdb_assert (bl);
9465 gdb_assert (type == bp_hardware_breakpoint);
9466
9467 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9468 number, paddress (bl->gdbarch, bl->address),
9469 paddress (bl->gdbarch, bl->address + bl->length - 1));
9470 }
9471
9472 /* Implement the "print_recreate" method for ranged breakpoints. */
9473
9474 void
9475 ranged_breakpoint::print_recreate (struct ui_file *fp) const
9476 {
9477 gdb_printf (fp, "break-range %s, %s",
9478 locspec->to_string (),
9479 locspec_range_end->to_string ());
9480 print_recreate_thread (fp);
9481 }
9482
9483 /* Find the address where the end of the breakpoint range should be
9484 placed, given the SAL of the end of the range. This is so that if
9485 the user provides a line number, the end of the range is set to the
9486 last instruction of the given line. */
9487
9488 static CORE_ADDR
9489 find_breakpoint_range_end (struct symtab_and_line sal)
9490 {
9491 CORE_ADDR end;
9492
9493 /* If the user provided a PC value, use it. Otherwise,
9494 find the address of the end of the given location. */
9495 if (sal.explicit_pc)
9496 end = sal.pc;
9497 else
9498 {
9499 int ret;
9500 CORE_ADDR start;
9501
9502 ret = find_line_pc_range (sal, &start, &end);
9503 if (!ret)
9504 error (_("Could not find location of the end of the range."));
9505
9506 /* find_line_pc_range returns the start of the next line. */
9507 end--;
9508 }
9509
9510 return end;
9511 }
9512
9513 /* Implement the "break-range" CLI command. */
9514
9515 static void
9516 break_range_command (const char *arg, int from_tty)
9517 {
9518 const char *arg_start;
9519 struct linespec_result canonical_start, canonical_end;
9520 int bp_count, can_use_bp, length;
9521 CORE_ADDR end;
9522
9523 /* We don't support software ranged breakpoints. */
9524 if (target_ranged_break_num_registers () < 0)
9525 error (_("This target does not support hardware ranged breakpoints."));
9526
9527 bp_count = hw_breakpoint_used_count ();
9528 bp_count += target_ranged_break_num_registers ();
9529 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9530 bp_count, 0);
9531 if (can_use_bp < 0)
9532 error (_("Hardware breakpoints used exceeds limit."));
9533
9534 arg = skip_spaces (arg);
9535 if (arg == NULL || arg[0] == '\0')
9536 error(_("No address range specified."));
9537
9538 arg_start = arg;
9539 location_spec_up start_locspec
9540 = string_to_location_spec (&arg, current_language);
9541 parse_breakpoint_sals (start_locspec.get (), &canonical_start);
9542
9543 if (arg[0] != ',')
9544 error (_("Too few arguments."));
9545 else if (canonical_start.lsals.empty ())
9546 error (_("Could not find location of the beginning of the range."));
9547
9548 const linespec_sals &lsal_start = canonical_start.lsals[0];
9549
9550 if (canonical_start.lsals.size () > 1
9551 || lsal_start.sals.size () != 1)
9552 error (_("Cannot create a ranged breakpoint with multiple locations."));
9553
9554 const symtab_and_line &sal_start = lsal_start.sals[0];
9555 std::string addr_string_start (arg_start, arg - arg_start);
9556
9557 arg++; /* Skip the comma. */
9558 arg = skip_spaces (arg);
9559
9560 /* Parse the end location specification. */
9561
9562 arg_start = arg;
9563
9564 /* We call decode_line_full directly here instead of using
9565 parse_breakpoint_sals because we need to specify the start
9566 location spec's symtab and line as the default symtab and line
9567 for the end of the range. This makes it possible to have ranges
9568 like "foo.c:27, +14", where +14 means 14 lines from the start
9569 location spec. */
9570 location_spec_up end_locspec
9571 = string_to_location_spec (&arg, current_language);
9572 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9573 sal_start.symtab, sal_start.line,
9574 &canonical_end, NULL, NULL);
9575
9576 if (canonical_end.lsals.empty ())
9577 error (_("Could not find location of the end of the range."));
9578
9579 const linespec_sals &lsal_end = canonical_end.lsals[0];
9580 if (canonical_end.lsals.size () > 1
9581 || lsal_end.sals.size () != 1)
9582 error (_("Cannot create a ranged breakpoint with multiple locations."));
9583
9584 const symtab_and_line &sal_end = lsal_end.sals[0];
9585
9586 end = find_breakpoint_range_end (sal_end);
9587 if (sal_start.pc > end)
9588 error (_("Invalid address range, end precedes start."));
9589
9590 length = end - sal_start.pc + 1;
9591 if (length < 0)
9592 /* Length overflowed. */
9593 error (_("Address range too large."));
9594 else if (length == 1)
9595 {
9596 /* This range is simple enough to be handled by
9597 the `hbreak' command. */
9598 hbreak_command (&addr_string_start[0], 1);
9599
9600 return;
9601 }
9602
9603 /* Now set up the breakpoint and install it. */
9604
9605 std::unique_ptr<breakpoint> br
9606 (new ranged_breakpoint (get_current_arch (),
9607 sal_start, length,
9608 std::move (start_locspec),
9609 std::move (end_locspec)));
9610
9611 install_breakpoint (false, std::move (br), true);
9612 }
9613
9614 /* Return non-zero if EXP is verified as constant. Returned zero
9615 means EXP is variable. Also the constant detection may fail for
9616 some constant expressions and in such case still falsely return
9617 zero. */
9618
9619 static bool
9620 watchpoint_exp_is_const (const struct expression *exp)
9621 {
9622 return exp->op->constant_p ();
9623 }
9624
9625 /* Implement the "re_set" method for watchpoints. */
9626
9627 void
9628 watchpoint::re_set ()
9629 {
9630 /* Watchpoint can be either on expression using entirely global
9631 variables, or it can be on local variables.
9632
9633 Watchpoints of the first kind are never auto-deleted, and even
9634 persist across program restarts. Since they can use variables
9635 from shared libraries, we need to reparse expression as libraries
9636 are loaded and unloaded.
9637
9638 Watchpoints on local variables can also change meaning as result
9639 of solib event. For example, if a watchpoint uses both a local
9640 and a global variables in expression, it's a local watchpoint,
9641 but unloading of a shared library will make the expression
9642 invalid. This is not a very common use case, but we still
9643 re-evaluate expression, to avoid surprises to the user.
9644
9645 Note that for local watchpoints, we re-evaluate it only if
9646 watchpoints frame id is still valid. If it's not, it means the
9647 watchpoint is out of scope and will be deleted soon. In fact,
9648 I'm not sure we'll ever be called in this case.
9649
9650 If a local watchpoint's frame id is still valid, then
9651 exp_valid_block is likewise valid, and we can safely use it.
9652
9653 Don't do anything about disabled watchpoints, since they will be
9654 reevaluated again when enabled. */
9655 update_watchpoint (this, true /* reparse */);
9656 }
9657
9658 /* Implement the "insert" method for hardware watchpoints. */
9659
9660 int
9661 watchpoint::insert_location (struct bp_location *bl)
9662 {
9663 int length = exact ? 1 : bl->length;
9664
9665 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9666 cond_exp.get ());
9667 }
9668
9669 /* Implement the "remove" method for hardware watchpoints. */
9670
9671 int
9672 watchpoint::remove_location (struct bp_location *bl,
9673 enum remove_bp_reason reason)
9674 {
9675 int length = exact ? 1 : bl->length;
9676
9677 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9678 cond_exp.get ());
9679 }
9680
9681 int
9682 watchpoint::breakpoint_hit (const struct bp_location *bl,
9683 const address_space *aspace, CORE_ADDR bp_addr,
9684 const target_waitstatus &ws)
9685 {
9686 struct breakpoint *b = bl->owner;
9687
9688 /* Continuable hardware watchpoints are treated as non-existent if the
9689 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9690 some data address). Otherwise gdb won't stop on a break instruction
9691 in the code (not from a breakpoint) when a hardware watchpoint has
9692 been defined. Also skip watchpoints which we know did not trigger
9693 (did not match the data address). */
9694 if (is_hardware_watchpoint (b)
9695 && watchpoint_triggered == watch_triggered_no)
9696 return 0;
9697
9698 return 1;
9699 }
9700
9701 void
9702 watchpoint::check_status (bpstat *bs)
9703 {
9704 gdb_assert (is_watchpoint (bs->breakpoint_at));
9705
9706 bpstat_check_watchpoint (bs);
9707 }
9708
9709 /* Implement the "resources_needed" method for hardware
9710 watchpoints. */
9711
9712 int
9713 watchpoint::resources_needed (const struct bp_location *bl)
9714 {
9715 int length = exact? 1 : bl->length;
9716
9717 return target_region_ok_for_hw_watchpoint (bl->address, length);
9718 }
9719
9720 /* Implement the "works_in_software_mode" method for hardware
9721 watchpoints. */
9722
9723 bool
9724 watchpoint::works_in_software_mode () const
9725 {
9726 /* Read and access watchpoints only work with hardware support. */
9727 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9728 }
9729
9730 enum print_stop_action
9731 watchpoint::print_it (const bpstat *bs) const
9732 {
9733 struct breakpoint *b;
9734 enum print_stop_action result;
9735 struct ui_out *uiout = current_uiout;
9736
9737 gdb_assert (bs->bp_location_at != NULL);
9738
9739 b = bs->breakpoint_at;
9740
9741 annotate_watchpoint (b->number);
9742 maybe_print_thread_hit_breakpoint (uiout);
9743
9744 string_file stb;
9745
9746 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9747 switch (b->type)
9748 {
9749 case bp_watchpoint:
9750 case bp_hardware_watchpoint:
9751 if (uiout->is_mi_like_p ())
9752 uiout->field_string
9753 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9754 mention (b);
9755 tuple_emitter.emplace (uiout, "value");
9756 uiout->text ("\nOld value = ");
9757 watchpoint_value_print (bs->old_val.get (), &stb);
9758 uiout->field_stream ("old", stb);
9759 uiout->text ("\nNew value = ");
9760 watchpoint_value_print (val.get (), &stb);
9761 uiout->field_stream ("new", stb);
9762 uiout->text ("\n");
9763 /* More than one watchpoint may have been triggered. */
9764 result = PRINT_UNKNOWN;
9765 break;
9766
9767 case bp_read_watchpoint:
9768 if (uiout->is_mi_like_p ())
9769 uiout->field_string
9770 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9771 mention (b);
9772 tuple_emitter.emplace (uiout, "value");
9773 uiout->text ("\nValue = ");
9774 watchpoint_value_print (val.get (), &stb);
9775 uiout->field_stream ("value", stb);
9776 uiout->text ("\n");
9777 result = PRINT_UNKNOWN;
9778 break;
9779
9780 case bp_access_watchpoint:
9781 if (bs->old_val != NULL)
9782 {
9783 if (uiout->is_mi_like_p ())
9784 uiout->field_string
9785 ("reason",
9786 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9787 mention (b);
9788 tuple_emitter.emplace (uiout, "value");
9789 uiout->text ("\nOld value = ");
9790 watchpoint_value_print (bs->old_val.get (), &stb);
9791 uiout->field_stream ("old", stb);
9792 uiout->text ("\nNew value = ");
9793 }
9794 else
9795 {
9796 mention (b);
9797 if (uiout->is_mi_like_p ())
9798 uiout->field_string
9799 ("reason",
9800 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9801 tuple_emitter.emplace (uiout, "value");
9802 uiout->text ("\nValue = ");
9803 }
9804 watchpoint_value_print (val.get (), &stb);
9805 uiout->field_stream ("new", stb);
9806 uiout->text ("\n");
9807 result = PRINT_UNKNOWN;
9808 break;
9809 default:
9810 result = PRINT_UNKNOWN;
9811 }
9812
9813 return result;
9814 }
9815
9816 /* Implement the "print_mention" method for hardware watchpoints. */
9817
9818 void
9819 watchpoint::print_mention () const
9820 {
9821 struct ui_out *uiout = current_uiout;
9822 const char *tuple_name;
9823
9824 switch (type)
9825 {
9826 case bp_watchpoint:
9827 uiout->text ("Watchpoint ");
9828 tuple_name = "wpt";
9829 break;
9830 case bp_hardware_watchpoint:
9831 uiout->text ("Hardware watchpoint ");
9832 tuple_name = "wpt";
9833 break;
9834 case bp_read_watchpoint:
9835 uiout->text ("Hardware read watchpoint ");
9836 tuple_name = "hw-rwpt";
9837 break;
9838 case bp_access_watchpoint:
9839 uiout->text ("Hardware access (read/write) watchpoint ");
9840 tuple_name = "hw-awpt";
9841 break;
9842 default:
9843 internal_error (_("Invalid hardware watchpoint type."));
9844 }
9845
9846 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9847 uiout->field_signed ("number", number);
9848 uiout->text (": ");
9849 uiout->field_string ("exp", exp_string.get ());
9850 }
9851
9852 /* Implement the "print_recreate" method for watchpoints. */
9853
9854 void
9855 watchpoint::print_recreate (struct ui_file *fp) const
9856 {
9857 switch (type)
9858 {
9859 case bp_watchpoint:
9860 case bp_hardware_watchpoint:
9861 gdb_printf (fp, "watch");
9862 break;
9863 case bp_read_watchpoint:
9864 gdb_printf (fp, "rwatch");
9865 break;
9866 case bp_access_watchpoint:
9867 gdb_printf (fp, "awatch");
9868 break;
9869 default:
9870 internal_error (_("Invalid watchpoint type."));
9871 }
9872
9873 gdb_printf (fp, " %s", exp_string.get ());
9874 print_recreate_thread (fp);
9875 }
9876
9877 /* Implement the "explains_signal" method for watchpoints. */
9878
9879 bool
9880 watchpoint::explains_signal (enum gdb_signal sig)
9881 {
9882 /* A software watchpoint cannot cause a signal other than
9883 GDB_SIGNAL_TRAP. */
9884 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
9885 return false;
9886
9887 return true;
9888 }
9889
9890 struct masked_watchpoint : public watchpoint
9891 {
9892 using watchpoint::watchpoint;
9893
9894 int insert_location (struct bp_location *) override;
9895 int remove_location (struct bp_location *,
9896 enum remove_bp_reason reason) override;
9897 int resources_needed (const struct bp_location *) override;
9898 bool works_in_software_mode () const override;
9899 enum print_stop_action print_it (const bpstat *bs) const override;
9900 void print_one_detail (struct ui_out *) const override;
9901 void print_mention () const override;
9902 void print_recreate (struct ui_file *fp) const override;
9903 };
9904
9905 /* Implement the "insert" method for masked hardware watchpoints. */
9906
9907 int
9908 masked_watchpoint::insert_location (struct bp_location *bl)
9909 {
9910 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
9911 bl->watchpoint_type);
9912 }
9913
9914 /* Implement the "remove" method for masked hardware watchpoints. */
9915
9916 int
9917 masked_watchpoint::remove_location (struct bp_location *bl,
9918 enum remove_bp_reason reason)
9919 {
9920 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
9921 bl->watchpoint_type);
9922 }
9923
9924 /* Implement the "resources_needed" method for masked hardware
9925 watchpoints. */
9926
9927 int
9928 masked_watchpoint::resources_needed (const struct bp_location *bl)
9929 {
9930 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
9931 }
9932
9933 /* Implement the "works_in_software_mode" method for masked hardware
9934 watchpoints. */
9935
9936 bool
9937 masked_watchpoint::works_in_software_mode () const
9938 {
9939 return false;
9940 }
9941
9942 /* Implement the "print_it" method for masked hardware
9943 watchpoints. */
9944
9945 enum print_stop_action
9946 masked_watchpoint::print_it (const bpstat *bs) const
9947 {
9948 struct breakpoint *b = bs->breakpoint_at;
9949 struct ui_out *uiout = current_uiout;
9950
9951 /* Masked watchpoints have only one location. */
9952 gdb_assert (b->loc && b->loc->next == NULL);
9953
9954 annotate_watchpoint (b->number);
9955 maybe_print_thread_hit_breakpoint (uiout);
9956
9957 switch (b->type)
9958 {
9959 case bp_hardware_watchpoint:
9960 if (uiout->is_mi_like_p ())
9961 uiout->field_string
9962 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9963 break;
9964
9965 case bp_read_watchpoint:
9966 if (uiout->is_mi_like_p ())
9967 uiout->field_string
9968 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9969 break;
9970
9971 case bp_access_watchpoint:
9972 if (uiout->is_mi_like_p ())
9973 uiout->field_string
9974 ("reason",
9975 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9976 break;
9977 default:
9978 internal_error (_("Invalid hardware watchpoint type."));
9979 }
9980
9981 mention (b);
9982 uiout->text (_("\n\
9983 Check the underlying instruction at PC for the memory\n\
9984 address and value which triggered this watchpoint.\n"));
9985 uiout->text ("\n");
9986
9987 /* More than one watchpoint may have been triggered. */
9988 return PRINT_UNKNOWN;
9989 }
9990
9991 /* Implement the "print_one_detail" method for masked hardware
9992 watchpoints. */
9993
9994 void
9995 masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9996 {
9997 /* Masked watchpoints have only one location. */
9998 gdb_assert (loc && loc->next == NULL);
9999
10000 uiout->text ("\tmask ");
10001 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
10002 uiout->text ("\n");
10003 }
10004
10005 /* Implement the "print_mention" method for masked hardware
10006 watchpoints. */
10007
10008 void
10009 masked_watchpoint::print_mention () const
10010 {
10011 struct ui_out *uiout = current_uiout;
10012 const char *tuple_name;
10013
10014 switch (type)
10015 {
10016 case bp_hardware_watchpoint:
10017 uiout->text ("Masked hardware watchpoint ");
10018 tuple_name = "wpt";
10019 break;
10020 case bp_read_watchpoint:
10021 uiout->text ("Masked hardware read watchpoint ");
10022 tuple_name = "hw-rwpt";
10023 break;
10024 case bp_access_watchpoint:
10025 uiout->text ("Masked hardware access (read/write) watchpoint ");
10026 tuple_name = "hw-awpt";
10027 break;
10028 default:
10029 internal_error (_("Invalid hardware watchpoint type."));
10030 }
10031
10032 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10033 uiout->field_signed ("number", number);
10034 uiout->text (": ");
10035 uiout->field_string ("exp", exp_string.get ());
10036 }
10037
10038 /* Implement the "print_recreate" method for masked hardware
10039 watchpoints. */
10040
10041 void
10042 masked_watchpoint::print_recreate (struct ui_file *fp) const
10043 {
10044 switch (type)
10045 {
10046 case bp_hardware_watchpoint:
10047 gdb_printf (fp, "watch");
10048 break;
10049 case bp_read_watchpoint:
10050 gdb_printf (fp, "rwatch");
10051 break;
10052 case bp_access_watchpoint:
10053 gdb_printf (fp, "awatch");
10054 break;
10055 default:
10056 internal_error (_("Invalid hardware watchpoint type."));
10057 }
10058
10059 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
10060 phex (hw_wp_mask, sizeof (CORE_ADDR)));
10061 print_recreate_thread (fp);
10062 }
10063
10064 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10065
10066 static bool
10067 is_masked_watchpoint (const struct breakpoint *b)
10068 {
10069 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
10070 }
10071
10072 /* accessflag: hw_write: watch write,
10073 hw_read: watch read,
10074 hw_access: watch access (read or write) */
10075 static void
10076 watch_command_1 (const char *arg, int accessflag, int from_tty,
10077 bool just_location, bool internal)
10078 {
10079 struct breakpoint *scope_breakpoint = NULL;
10080 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10081 struct value *result;
10082 int saved_bitpos = 0, saved_bitsize = 0;
10083 const char *exp_start = NULL;
10084 const char *exp_end = NULL;
10085 const char *tok, *end_tok;
10086 int toklen = -1;
10087 const char *cond_start = NULL;
10088 const char *cond_end = NULL;
10089 enum bptype bp_type;
10090 int thread = -1;
10091 /* Flag to indicate whether we are going to use masks for
10092 the hardware watchpoint. */
10093 bool use_mask = false;
10094 CORE_ADDR mask = 0;
10095 int task = -1;
10096
10097 /* Make sure that we actually have parameters to parse. */
10098 if (arg != NULL && arg[0] != '\0')
10099 {
10100 const char *value_start;
10101
10102 exp_end = arg + strlen (arg);
10103
10104 /* Look for "parameter value" pairs at the end
10105 of the arguments string. */
10106 for (tok = exp_end - 1; tok > arg; tok--)
10107 {
10108 /* Skip whitespace at the end of the argument list. */
10109 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10110 tok--;
10111
10112 /* Find the beginning of the last token.
10113 This is the value of the parameter. */
10114 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10115 tok--;
10116 value_start = tok + 1;
10117
10118 /* Skip whitespace. */
10119 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10120 tok--;
10121
10122 end_tok = tok;
10123
10124 /* Find the beginning of the second to last token.
10125 This is the parameter itself. */
10126 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10127 tok--;
10128 tok++;
10129 toklen = end_tok - tok + 1;
10130
10131 if (toklen == 6 && startswith (tok, "thread"))
10132 {
10133 struct thread_info *thr;
10134 /* At this point we've found a "thread" token, which means
10135 the user is trying to set a watchpoint that triggers
10136 only in a specific thread. */
10137 const char *endp;
10138
10139 if (thread != -1)
10140 error(_("You can specify only one thread."));
10141
10142 if (task != -1)
10143 error (_("You can specify only one of thread or task."));
10144
10145 /* Extract the thread ID from the next token. */
10146 thr = parse_thread_id (value_start, &endp);
10147
10148 /* Check if the user provided a valid thread ID. */
10149 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10150 invalid_thread_id_error (value_start);
10151
10152 thread = thr->global_num;
10153 }
10154 else if (toklen == 4 && startswith (tok, "task"))
10155 {
10156 char *tmp;
10157
10158 if (task != -1)
10159 error(_("You can specify only one task."));
10160
10161 if (thread != -1)
10162 error (_("You can specify only one of thread or task."));
10163
10164 task = strtol (value_start, &tmp, 0);
10165 if (tmp == value_start)
10166 error (_("Junk after task keyword."));
10167 if (!valid_task_id (task))
10168 error (_("Unknown task %d."), task);
10169 }
10170 else if (toklen == 4 && startswith (tok, "mask"))
10171 {
10172 /* We've found a "mask" token, which means the user wants to
10173 create a hardware watchpoint that is going to have the mask
10174 facility. */
10175 struct value *mask_value;
10176
10177 if (use_mask)
10178 error(_("You can specify only one mask."));
10179
10180 use_mask = just_location = true;
10181
10182 scoped_value_mark mark;
10183 mask_value = parse_to_comma_and_eval (&value_start);
10184 mask = value_as_address (mask_value);
10185 }
10186 else
10187 /* We didn't recognize what we found. We should stop here. */
10188 break;
10189
10190 /* Truncate the string and get rid of the "parameter value" pair before
10191 the arguments string is parsed by the parse_exp_1 function. */
10192 exp_end = tok;
10193 }
10194 }
10195 else
10196 exp_end = arg;
10197
10198 /* Parse the rest of the arguments. From here on out, everything
10199 is in terms of a newly allocated string instead of the original
10200 ARG. */
10201 std::string expression (arg, exp_end - arg);
10202 exp_start = arg = expression.c_str ();
10203 innermost_block_tracker tracker;
10204 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10205 exp_end = arg;
10206 /* Remove trailing whitespace from the expression before saving it.
10207 This makes the eventual display of the expression string a bit
10208 prettier. */
10209 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10210 --exp_end;
10211
10212 /* Checking if the expression is not constant. */
10213 if (watchpoint_exp_is_const (exp.get ()))
10214 {
10215 int len;
10216
10217 len = exp_end - exp_start;
10218 while (len > 0 && isspace (exp_start[len - 1]))
10219 len--;
10220 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10221 }
10222
10223 exp_valid_block = tracker.block ();
10224 struct value *mark = value_mark ();
10225 struct value *val_as_value = nullptr;
10226 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10227 just_location);
10228
10229 if (val_as_value != NULL && just_location)
10230 {
10231 saved_bitpos = val_as_value->bitpos ();
10232 saved_bitsize = val_as_value->bitsize ();
10233 }
10234
10235 value_ref_ptr val;
10236 if (just_location)
10237 {
10238 int ret;
10239
10240 exp_valid_block = NULL;
10241 val = release_value (value_addr (result));
10242 value_free_to_mark (mark);
10243
10244 if (use_mask)
10245 {
10246 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10247 mask);
10248 if (ret == -1)
10249 error (_("This target does not support masked watchpoints."));
10250 else if (ret == -2)
10251 error (_("Invalid mask or memory region."));
10252 }
10253 }
10254 else if (val_as_value != NULL)
10255 val = release_value (val_as_value);
10256
10257 tok = skip_spaces (arg);
10258 end_tok = skip_to_space (tok);
10259
10260 toklen = end_tok - tok;
10261 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10262 {
10263 tok = cond_start = end_tok + 1;
10264 innermost_block_tracker if_tracker;
10265 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10266
10267 /* The watchpoint expression may not be local, but the condition
10268 may still be. E.g.: `watch global if local > 0'. */
10269 cond_exp_valid_block = if_tracker.block ();
10270
10271 cond_end = tok;
10272 }
10273 if (*tok)
10274 error (_("Junk at end of command."));
10275
10276 frame_info_ptr wp_frame = block_innermost_frame (exp_valid_block);
10277
10278 /* Save this because create_internal_breakpoint below invalidates
10279 'wp_frame'. */
10280 frame_id watchpoint_frame = get_frame_id (wp_frame);
10281
10282 /* If the expression is "local", then set up a "watchpoint scope"
10283 breakpoint at the point where we've left the scope of the watchpoint
10284 expression. Create the scope breakpoint before the watchpoint, so
10285 that we will encounter it first in bpstat_stop_status. */
10286 if (exp_valid_block != NULL && wp_frame != NULL)
10287 {
10288 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10289
10290 if (frame_id_p (caller_frame_id))
10291 {
10292 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10293 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10294
10295 scope_breakpoint
10296 = create_internal_breakpoint (caller_arch, caller_pc,
10297 bp_watchpoint_scope);
10298
10299 /* create_internal_breakpoint could invalidate WP_FRAME. */
10300 wp_frame = NULL;
10301
10302 scope_breakpoint->enable_state = bp_enabled;
10303
10304 /* Automatically delete the breakpoint when it hits. */
10305 scope_breakpoint->disposition = disp_del;
10306
10307 /* Only break in the proper frame (help with recursion). */
10308 scope_breakpoint->frame_id = caller_frame_id;
10309
10310 /* Set the address at which we will stop. */
10311 scope_breakpoint->loc->gdbarch = caller_arch;
10312 scope_breakpoint->loc->requested_address = caller_pc;
10313 scope_breakpoint->loc->address
10314 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10315 scope_breakpoint->loc->requested_address,
10316 scope_breakpoint->type,
10317 current_program_space);
10318 }
10319 }
10320
10321 /* Now set up the breakpoint. We create all watchpoints as hardware
10322 watchpoints here even if hardware watchpoints are turned off, a call
10323 to update_watchpoint later in this function will cause the type to
10324 drop back to bp_watchpoint (software watchpoint) if required. */
10325
10326 if (accessflag == hw_read)
10327 bp_type = bp_read_watchpoint;
10328 else if (accessflag == hw_access)
10329 bp_type = bp_access_watchpoint;
10330 else
10331 bp_type = bp_hardware_watchpoint;
10332
10333 std::unique_ptr<watchpoint> w;
10334 if (use_mask)
10335 w.reset (new masked_watchpoint (nullptr, bp_type));
10336 else
10337 w.reset (new watchpoint (nullptr, bp_type));
10338
10339 /* At most one of thread or task can be set on a watchpoint. */
10340 gdb_assert (thread == -1 || task == -1);
10341 w->thread = thread;
10342 w->task = task;
10343 w->disposition = disp_donttouch;
10344 w->pspace = current_program_space;
10345 w->exp = std::move (exp);
10346 w->exp_valid_block = exp_valid_block;
10347 w->cond_exp_valid_block = cond_exp_valid_block;
10348 if (just_location)
10349 {
10350 struct type *t = val->type ();
10351 CORE_ADDR addr = value_as_address (val.get ());
10352
10353 w->exp_string_reparse
10354 = current_language->watch_location_expression (t, addr);
10355
10356 w->exp_string = xstrprintf ("-location %.*s",
10357 (int) (exp_end - exp_start), exp_start);
10358 }
10359 else
10360 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10361
10362 if (use_mask)
10363 {
10364 w->hw_wp_mask = mask;
10365 }
10366 else
10367 {
10368 w->val = val;
10369 w->val_bitpos = saved_bitpos;
10370 w->val_bitsize = saved_bitsize;
10371 w->val_valid = true;
10372 }
10373
10374 if (cond_start)
10375 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10376 else
10377 w->cond_string = 0;
10378
10379 if (frame_id_p (watchpoint_frame))
10380 {
10381 w->watchpoint_frame = watchpoint_frame;
10382 w->watchpoint_thread = inferior_ptid;
10383 }
10384 else
10385 {
10386 w->watchpoint_frame = null_frame_id;
10387 w->watchpoint_thread = null_ptid;
10388 }
10389
10390 if (scope_breakpoint != NULL)
10391 {
10392 /* The scope breakpoint is related to the watchpoint. We will
10393 need to act on them together. */
10394 w->related_breakpoint = scope_breakpoint;
10395 scope_breakpoint->related_breakpoint = w.get ();
10396 }
10397
10398 if (!just_location)
10399 value_free_to_mark (mark);
10400
10401 /* Finally update the new watchpoint. This creates the locations
10402 that should be inserted. */
10403 update_watchpoint (w.get (), true /* reparse */);
10404
10405 install_breakpoint (internal, std::move (w), 1);
10406 }
10407
10408 /* Return count of debug registers needed to watch the given expression.
10409 If the watchpoint cannot be handled in hardware return zero. */
10410
10411 static int
10412 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10413 {
10414 int found_memory_cnt = 0;
10415
10416 /* Did the user specifically forbid us to use hardware watchpoints? */
10417 if (!can_use_hw_watchpoints)
10418 return 0;
10419
10420 gdb_assert (!vals.empty ());
10421 struct value *head = vals[0].get ();
10422
10423 /* Make sure that the value of the expression depends only upon
10424 memory contents, and values computed from them within GDB. If we
10425 find any register references or function calls, we can't use a
10426 hardware watchpoint.
10427
10428 The idea here is that evaluating an expression generates a series
10429 of values, one holding the value of every subexpression. (The
10430 expression a*b+c has five subexpressions: a, b, a*b, c, and
10431 a*b+c.) GDB's values hold almost enough information to establish
10432 the criteria given above --- they identify memory lvalues,
10433 register lvalues, computed values, etcetera. So we can evaluate
10434 the expression, and then scan the chain of values that leaves
10435 behind to decide whether we can detect any possible change to the
10436 expression's final value using only hardware watchpoints.
10437
10438 However, I don't think that the values returned by inferior
10439 function calls are special in any way. So this function may not
10440 notice that an expression involving an inferior function call
10441 can't be watched with hardware watchpoints. FIXME. */
10442 for (const value_ref_ptr &iter : vals)
10443 {
10444 struct value *v = iter.get ();
10445
10446 if (v->lval () == lval_memory)
10447 {
10448 if (v != head && v->lazy ())
10449 /* A lazy memory lvalue in the chain is one that GDB never
10450 needed to fetch; we either just used its address (e.g.,
10451 `a' in `a.b') or we never needed it at all (e.g., `a'
10452 in `a,b'). This doesn't apply to HEAD; if that is
10453 lazy then it was not readable, but watch it anyway. */
10454 ;
10455 else
10456 {
10457 /* Ahh, memory we actually used! Check if we can cover
10458 it with hardware watchpoints. */
10459 struct type *vtype = check_typedef (v->type ());
10460
10461 /* We only watch structs and arrays if user asked for it
10462 explicitly, never if they just happen to appear in a
10463 middle of some value chain. */
10464 if (v == head
10465 || (vtype->code () != TYPE_CODE_STRUCT
10466 && vtype->code () != TYPE_CODE_ARRAY))
10467 {
10468 CORE_ADDR vaddr = v->address ();
10469 int len;
10470 int num_regs;
10471
10472 len = (target_exact_watchpoints
10473 && is_scalar_type_recursive (vtype))?
10474 1 : v->type ()->length ();
10475
10476 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10477 if (!num_regs)
10478 return 0;
10479 else
10480 found_memory_cnt += num_regs;
10481 }
10482 }
10483 }
10484 else if (v->lval () != not_lval && !v->deprecated_modifiable ())
10485 return 0; /* These are values from the history (e.g., $1). */
10486 else if (v->lval () == lval_register)
10487 return 0; /* Cannot watch a register with a HW watchpoint. */
10488 }
10489
10490 /* The expression itself looks suitable for using a hardware
10491 watchpoint, but give the target machine a chance to reject it. */
10492 return found_memory_cnt;
10493 }
10494
10495 void
10496 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10497 {
10498 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10499 }
10500
10501 /* Options for the watch, awatch, and rwatch commands. */
10502
10503 struct watch_options
10504 {
10505 /* For -location. */
10506 bool location = false;
10507 };
10508
10509 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10510
10511 Historically GDB always accepted both '-location' and '-l' flags for
10512 these commands (both flags being synonyms). When converting to the
10513 newer option scheme only '-location' is added here. That's fine (for
10514 backward compatibility) as any non-ambiguous prefix of a flag will be
10515 accepted, so '-l', '-loc', are now all accepted.
10516
10517 What this means is that, if in the future, we add any new flag here
10518 that starts with '-l' then this will break backward compatibility, so
10519 please, don't do that! */
10520
10521 static const gdb::option::option_def watch_option_defs[] = {
10522 gdb::option::flag_option_def<watch_options> {
10523 "location",
10524 [] (watch_options *opt) { return &opt->location; },
10525 N_("\
10526 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10527 -l can be used as a short form of -location."),
10528 },
10529 };
10530
10531 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10532 commands. */
10533
10534 static gdb::option::option_def_group
10535 make_watch_options_def_group (watch_options *opts)
10536 {
10537 return {{watch_option_defs}, opts};
10538 }
10539
10540 /* A helper function that looks for the "-location" argument and then
10541 calls watch_command_1. */
10542
10543 static void
10544 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10545 {
10546 watch_options opts;
10547 auto grp = make_watch_options_def_group (&opts);
10548 gdb::option::process_options
10549 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10550 if (arg != nullptr && *arg == '\0')
10551 arg = nullptr;
10552
10553 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10554 }
10555
10556 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10557 static void
10558 watch_command_completer (struct cmd_list_element *ignore,
10559 completion_tracker &tracker,
10560 const char *text, const char * /*word*/)
10561 {
10562 const auto group = make_watch_options_def_group (nullptr);
10563 if (gdb::option::complete_options
10564 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10565 return;
10566
10567 const char *word = advance_to_expression_complete_word_point (tracker, text);
10568 expression_completer (ignore, tracker, text, word);
10569 }
10570
10571 static void
10572 watch_command (const char *arg, int from_tty)
10573 {
10574 watch_maybe_just_location (arg, hw_write, from_tty);
10575 }
10576
10577 void
10578 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10579 {
10580 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10581 }
10582
10583 static void
10584 rwatch_command (const char *arg, int from_tty)
10585 {
10586 watch_maybe_just_location (arg, hw_read, from_tty);
10587 }
10588
10589 void
10590 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
10591 {
10592 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10593 }
10594
10595 static void
10596 awatch_command (const char *arg, int from_tty)
10597 {
10598 watch_maybe_just_location (arg, hw_access, from_tty);
10599 }
10600 \f
10601
10602 /* Data for the FSM that manages the until(location)/advance commands
10603 in infcmd.c. Here because it uses the mechanisms of
10604 breakpoints. */
10605
10606 struct until_break_fsm : public thread_fsm
10607 {
10608 /* The thread that was current when the command was executed. */
10609 int thread;
10610
10611 /* The breakpoint set at the return address in the caller frame,
10612 plus breakpoints at all the destination locations. */
10613 std::vector<breakpoint_up> breakpoints;
10614
10615 until_break_fsm (struct interp *cmd_interp, int thread,
10616 std::vector<breakpoint_up> &&breakpoints)
10617 : thread_fsm (cmd_interp),
10618 thread (thread),
10619 breakpoints (std::move (breakpoints))
10620 {
10621 }
10622
10623 void clean_up (struct thread_info *thread) override;
10624 bool should_stop (struct thread_info *thread) override;
10625 enum async_reply_reason do_async_reply_reason () override;
10626 };
10627
10628 /* Implementation of the 'should_stop' FSM method for the
10629 until(location)/advance commands. */
10630
10631 bool
10632 until_break_fsm::should_stop (struct thread_info *tp)
10633 {
10634 for (const breakpoint_up &bp : breakpoints)
10635 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10636 bp.get ()) != NULL)
10637 {
10638 set_finished ();
10639 break;
10640 }
10641
10642 return true;
10643 }
10644
10645 /* Implementation of the 'clean_up' FSM method for the
10646 until(location)/advance commands. */
10647
10648 void
10649 until_break_fsm::clean_up (struct thread_info *)
10650 {
10651 /* Clean up our temporary breakpoints. */
10652 breakpoints.clear ();
10653 delete_longjmp_breakpoint (thread);
10654 }
10655
10656 /* Implementation of the 'async_reply_reason' FSM method for the
10657 until(location)/advance commands. */
10658
10659 enum async_reply_reason
10660 until_break_fsm::do_async_reply_reason ()
10661 {
10662 return EXEC_ASYNC_LOCATION_REACHED;
10663 }
10664
10665 void
10666 until_break_command (const char *arg, int from_tty, int anywhere)
10667 {
10668 frame_info_ptr frame;
10669 struct gdbarch *frame_gdbarch;
10670 struct frame_id stack_frame_id;
10671 struct frame_id caller_frame_id;
10672 int thread;
10673 struct thread_info *tp;
10674
10675 clear_proceed_status (0);
10676
10677 /* Set a breakpoint where the user wants it and at return from
10678 this function. */
10679
10680 location_spec_up locspec = string_to_location_spec (&arg, current_language);
10681
10682 std::vector<symtab_and_line> sals
10683 = (last_displayed_sal_is_valid ()
10684 ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10685 get_last_displayed_symtab (),
10686 get_last_displayed_line ())
10687 : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
10688 NULL, NULL, 0));
10689
10690 if (sals.empty ())
10691 error (_("Couldn't get information on specified line."));
10692
10693 if (*arg)
10694 error (_("Junk at end of arguments."));
10695
10696 tp = inferior_thread ();
10697 thread = tp->global_num;
10698
10699 /* Note linespec handling above invalidates the frame chain.
10700 Installing a breakpoint also invalidates the frame chain (as it
10701 may need to switch threads), so do any frame handling before
10702 that. */
10703
10704 frame = get_selected_frame (NULL);
10705 frame_gdbarch = get_frame_arch (frame);
10706 stack_frame_id = get_stack_frame_id (frame);
10707 caller_frame_id = frame_unwind_caller_id (frame);
10708
10709 /* Keep within the current frame, or in frames called by the current
10710 one. */
10711
10712 std::vector<breakpoint_up> breakpoints;
10713
10714 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10715
10716 if (frame_id_p (caller_frame_id))
10717 {
10718 struct symtab_and_line sal2;
10719 struct gdbarch *caller_gdbarch;
10720
10721 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10722 sal2.pc = frame_unwind_caller_pc (frame);
10723 caller_gdbarch = frame_unwind_caller_arch (frame);
10724
10725 breakpoint_up caller_breakpoint
10726 = set_momentary_breakpoint (caller_gdbarch, sal2,
10727 caller_frame_id, bp_until);
10728 breakpoints.emplace_back (std::move (caller_breakpoint));
10729
10730 set_longjmp_breakpoint (tp, stack_frame_id);
10731 lj_deleter.emplace (thread);
10732 }
10733
10734 /* set_momentary_breakpoint could invalidate FRAME. */
10735 frame = NULL;
10736
10737 /* If the user told us to continue until a specified location, we
10738 don't specify a frame at which we need to stop. Otherwise,
10739 specify the selected frame, because we want to stop only at the
10740 very same frame. */
10741 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10742
10743 for (symtab_and_line &sal : sals)
10744 {
10745 resolve_sal_pc (&sal);
10746
10747 breakpoint_up location_breakpoint
10748 = set_momentary_breakpoint (frame_gdbarch, sal,
10749 stop_frame_id, bp_until);
10750 breakpoints.emplace_back (std::move (location_breakpoint));
10751 }
10752
10753 tp->set_thread_fsm
10754 (std::unique_ptr<thread_fsm>
10755 (new until_break_fsm (command_interp (), tp->global_num,
10756 std::move (breakpoints))));
10757
10758 if (lj_deleter)
10759 lj_deleter->release ();
10760
10761 proceed (-1, GDB_SIGNAL_DEFAULT);
10762 }
10763
10764 \f
10765
10766 /* Compare two breakpoints and return a strcmp-like result. */
10767
10768 static int
10769 compare_breakpoints (const breakpoint *a, const breakpoint *b)
10770 {
10771 uintptr_t ua = (uintptr_t) a;
10772 uintptr_t ub = (uintptr_t) b;
10773
10774 if (a->number < b->number)
10775 return -1;
10776 else if (a->number > b->number)
10777 return 1;
10778
10779 /* Now sort by address, in case we see, e..g, two breakpoints with
10780 the number 0. */
10781 if (ua < ub)
10782 return -1;
10783 return ua > ub ? 1 : 0;
10784 }
10785
10786 /* Delete breakpoints by address or line. */
10787
10788 static void
10789 clear_command (const char *arg, int from_tty)
10790 {
10791 int default_match;
10792
10793 std::vector<symtab_and_line> decoded_sals;
10794 symtab_and_line last_sal;
10795 gdb::array_view<symtab_and_line> sals;
10796 if (arg)
10797 {
10798 decoded_sals
10799 = decode_line_with_current_source (arg,
10800 (DECODE_LINE_FUNFIRSTLINE
10801 | DECODE_LINE_LIST_MODE));
10802 default_match = 0;
10803 sals = decoded_sals;
10804 }
10805 else
10806 {
10807 /* Set sal's line, symtab, pc, and pspace to the values
10808 corresponding to the last call to print_frame_info. If the
10809 codepoint is not valid, this will set all the fields to 0. */
10810 last_sal = get_last_displayed_sal ();
10811 if (last_sal.symtab == 0)
10812 error (_("No source file specified."));
10813
10814 default_match = 1;
10815 sals = last_sal;
10816 }
10817
10818 /* We don't call resolve_sal_pc here. That's not as bad as it
10819 seems, because all existing breakpoints typically have both
10820 file/line and pc set. So, if clear is given file/line, we can
10821 match this to existing breakpoint without obtaining pc at all.
10822
10823 We only support clearing given the address explicitly
10824 present in breakpoint table. Say, we've set breakpoint
10825 at file:line. There were several PC values for that file:line,
10826 due to optimization, all in one block.
10827
10828 We've picked one PC value. If "clear" is issued with another
10829 PC corresponding to the same file:line, the breakpoint won't
10830 be cleared. We probably can still clear the breakpoint, but
10831 since the other PC value is never presented to user, user
10832 can only find it by guessing, and it does not seem important
10833 to support that. */
10834
10835 /* For each line spec given, delete bps which correspond to it. Do
10836 it in two passes, solely to preserve the current behavior that
10837 from_tty is forced true if we delete more than one
10838 breakpoint. */
10839
10840 std::vector<struct breakpoint *> found;
10841 for (const auto &sal : sals)
10842 {
10843 const char *sal_fullname;
10844
10845 /* If exact pc given, clear bpts at that pc.
10846 If line given (pc == 0), clear all bpts on specified line.
10847 If defaulting, clear all bpts on default line
10848 or at default pc.
10849
10850 defaulting sal.pc != 0 tests to do
10851
10852 0 1 pc
10853 1 1 pc _and_ line
10854 0 0 line
10855 1 0 <can't happen> */
10856
10857 sal_fullname = (sal.symtab == NULL
10858 ? NULL : symtab_to_fullname (sal.symtab));
10859
10860 /* Find all matching breakpoints and add them to 'found'. */
10861 for (breakpoint *b : all_breakpoints ())
10862 {
10863 int match = 0;
10864 /* Are we going to delete b? */
10865 if (b->type != bp_none && !is_watchpoint (b)
10866 && user_breakpoint_p (b))
10867 {
10868 for (bp_location *loc : b->locations ())
10869 {
10870 /* If the user specified file:line, don't allow a PC
10871 match. This matches historical gdb behavior. */
10872 int pc_match = (!sal.explicit_line
10873 && sal.pc
10874 && (loc->pspace == sal.pspace)
10875 && (loc->address == sal.pc)
10876 && (!section_is_overlay (loc->section)
10877 || loc->section == sal.section));
10878 int line_match = 0;
10879
10880 if ((default_match || sal.explicit_line)
10881 && loc->symtab != NULL
10882 && sal_fullname != NULL
10883 && sal.pspace == loc->pspace
10884 && loc->line_number == sal.line
10885 && filename_cmp (symtab_to_fullname (loc->symtab),
10886 sal_fullname) == 0)
10887 line_match = 1;
10888
10889 if (pc_match || line_match)
10890 {
10891 match = 1;
10892 break;
10893 }
10894 }
10895 }
10896
10897 if (match)
10898 found.push_back (b);
10899 }
10900 }
10901
10902 /* Now go thru the 'found' chain and delete them. */
10903 if (found.empty ())
10904 {
10905 if (arg)
10906 error (_("No breakpoint at %s."), arg);
10907 else
10908 error (_("No breakpoint at this line."));
10909 }
10910
10911 /* Remove duplicates from the vec. */
10912 std::sort (found.begin (), found.end (),
10913 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10914 {
10915 return compare_breakpoints (bp_a, bp_b) < 0;
10916 });
10917 found.erase (std::unique (found.begin (), found.end (),
10918 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10919 {
10920 return compare_breakpoints (bp_a, bp_b) == 0;
10921 }),
10922 found.end ());
10923
10924 if (found.size () > 1)
10925 from_tty = 1; /* Always report if deleted more than one. */
10926 if (from_tty)
10927 {
10928 if (found.size () == 1)
10929 gdb_printf (_("Deleted breakpoint "));
10930 else
10931 gdb_printf (_("Deleted breakpoints "));
10932 }
10933
10934 for (breakpoint *iter : found)
10935 {
10936 if (from_tty)
10937 gdb_printf ("%d ", iter->number);
10938 delete_breakpoint (iter);
10939 }
10940 if (from_tty)
10941 gdb_putc ('\n');
10942 }
10943 \f
10944 /* Delete breakpoint in BS if they are `delete' breakpoints and
10945 all breakpoints that are marked for deletion, whether hit or not.
10946 This is called after any breakpoint is hit, or after errors. */
10947
10948 void
10949 breakpoint_auto_delete (bpstat *bs)
10950 {
10951 for (; bs; bs = bs->next)
10952 if (bs->breakpoint_at
10953 && bs->breakpoint_at->disposition == disp_del
10954 && bs->stop)
10955 delete_breakpoint (bs->breakpoint_at);
10956
10957 for (breakpoint *b : all_breakpoints_safe ())
10958 if (b->disposition == disp_del_at_next_stop)
10959 delete_breakpoint (b);
10960 }
10961
10962 /* A comparison function for bp_location AP and BP being interfaced to
10963 std::sort. Sort elements primarily by their ADDRESS (no matter what
10964 bl_address_is_meaningful says), secondarily by ordering first
10965 permanent elements and terciarily just ensuring the array is sorted
10966 stable way despite std::sort being an unstable algorithm. */
10967
10968 static int
10969 bp_location_is_less_than (const bp_location *a, const bp_location *b)
10970 {
10971 if (a->address != b->address)
10972 return a->address < b->address;
10973
10974 /* Sort locations at the same address by their pspace number, keeping
10975 locations of the same inferior (in a multi-inferior environment)
10976 grouped. */
10977
10978 if (a->pspace->num != b->pspace->num)
10979 return a->pspace->num < b->pspace->num;
10980
10981 /* Sort permanent breakpoints first. */
10982 if (a->permanent != b->permanent)
10983 return a->permanent > b->permanent;
10984
10985 /* Sort by type in order to make duplicate determination easier.
10986 See update_global_location_list. This is kept in sync with
10987 breakpoint_locations_match. */
10988 if (a->loc_type < b->loc_type)
10989 return true;
10990
10991 /* Likewise, for range-breakpoints, sort by length. */
10992 if (a->loc_type == bp_loc_hardware_breakpoint
10993 && b->loc_type == bp_loc_hardware_breakpoint
10994 && a->length < b->length)
10995 return true;
10996
10997 /* Make the internal GDB representation stable across GDB runs
10998 where A and B memory inside GDB can differ. Breakpoint locations of
10999 the same type at the same address can be sorted in arbitrary order. */
11000
11001 if (a->owner->number != b->owner->number)
11002 return a->owner->number < b->owner->number;
11003
11004 return a < b;
11005 }
11006
11007 /* Set bp_locations_placed_address_before_address_max and
11008 bp_locations_shadow_len_after_address_max according to the current
11009 content of the bp_locations array. */
11010
11011 static void
11012 bp_locations_target_extensions_update (void)
11013 {
11014 bp_locations_placed_address_before_address_max = 0;
11015 bp_locations_shadow_len_after_address_max = 0;
11016
11017 for (bp_location *bl : all_bp_locations ())
11018 {
11019 CORE_ADDR start, end, addr;
11020
11021 if (!bp_location_has_shadow (bl))
11022 continue;
11023
11024 start = bl->target_info.placed_address;
11025 end = start + bl->target_info.shadow_len;
11026
11027 gdb_assert (bl->address >= start);
11028 addr = bl->address - start;
11029 if (addr > bp_locations_placed_address_before_address_max)
11030 bp_locations_placed_address_before_address_max = addr;
11031
11032 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11033
11034 gdb_assert (bl->address < end);
11035 addr = end - bl->address;
11036 if (addr > bp_locations_shadow_len_after_address_max)
11037 bp_locations_shadow_len_after_address_max = addr;
11038 }
11039 }
11040
11041 /* Download tracepoint locations if they haven't been. */
11042
11043 static void
11044 download_tracepoint_locations (void)
11045 {
11046 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11047
11048 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11049
11050 for (breakpoint *b : all_tracepoints ())
11051 {
11052 struct tracepoint *t;
11053 bool bp_location_downloaded = false;
11054
11055 if ((b->type == bp_fast_tracepoint
11056 ? !may_insert_fast_tracepoints
11057 : !may_insert_tracepoints))
11058 continue;
11059
11060 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11061 {
11062 if (target_can_download_tracepoint ())
11063 can_download_tracepoint = TRIBOOL_TRUE;
11064 else
11065 can_download_tracepoint = TRIBOOL_FALSE;
11066 }
11067
11068 if (can_download_tracepoint == TRIBOOL_FALSE)
11069 break;
11070
11071 for (bp_location *bl : b->locations ())
11072 {
11073 /* In tracepoint, locations are _never_ duplicated, so
11074 should_be_inserted is equivalent to
11075 unduplicated_should_be_inserted. */
11076 if (!should_be_inserted (bl) || bl->inserted)
11077 continue;
11078
11079 switch_to_program_space_and_thread (bl->pspace);
11080
11081 target_download_tracepoint (bl);
11082
11083 bl->inserted = 1;
11084 bp_location_downloaded = true;
11085 }
11086 t = (struct tracepoint *) b;
11087 t->number_on_target = b->number;
11088 if (bp_location_downloaded)
11089 gdb::observers::breakpoint_modified.notify (b);
11090 }
11091 }
11092
11093 /* Swap the insertion/duplication state between two locations. */
11094
11095 static void
11096 swap_insertion (struct bp_location *left, struct bp_location *right)
11097 {
11098 const int left_inserted = left->inserted;
11099 const int left_duplicate = left->duplicate;
11100 const int left_needs_update = left->needs_update;
11101 const struct bp_target_info left_target_info = left->target_info;
11102
11103 /* Locations of tracepoints can never be duplicated. */
11104 if (is_tracepoint (left->owner))
11105 gdb_assert (!left->duplicate);
11106 if (is_tracepoint (right->owner))
11107 gdb_assert (!right->duplicate);
11108
11109 left->inserted = right->inserted;
11110 left->duplicate = right->duplicate;
11111 left->needs_update = right->needs_update;
11112 left->target_info = right->target_info;
11113 right->inserted = left_inserted;
11114 right->duplicate = left_duplicate;
11115 right->needs_update = left_needs_update;
11116 right->target_info = left_target_info;
11117 }
11118
11119 /* Force the re-insertion of the locations at ADDRESS. This is called
11120 once a new/deleted/modified duplicate location is found and we are evaluating
11121 conditions on the target's side. Such conditions need to be updated on
11122 the target. */
11123
11124 static void
11125 force_breakpoint_reinsertion (struct bp_location *bl)
11126 {
11127 CORE_ADDR address = 0;
11128 int pspace_num;
11129
11130 address = bl->address;
11131 pspace_num = bl->pspace->num;
11132
11133 /* This is only meaningful if the target is
11134 evaluating conditions and if the user has
11135 opted for condition evaluation on the target's
11136 side. */
11137 if (gdb_evaluates_breakpoint_condition_p ()
11138 || !target_supports_evaluation_of_breakpoint_conditions ())
11139 return;
11140
11141 /* Flag all breakpoint locations with this address and
11142 the same program space as the location
11143 as "its condition has changed". We need to
11144 update the conditions on the target's side. */
11145 for (bp_location *loc : all_bp_locations_at_addr (address))
11146 {
11147 if (!is_breakpoint (loc->owner)
11148 || pspace_num != loc->pspace->num)
11149 continue;
11150
11151 /* Flag the location appropriately. We use a different state to
11152 let everyone know that we already updated the set of locations
11153 with addr bl->address and program space bl->pspace. This is so
11154 we don't have to keep calling these functions just to mark locations
11155 that have already been marked. */
11156 loc->condition_changed = condition_updated;
11157
11158 /* Free the agent expression bytecode as well. We will compute
11159 it later on. */
11160 loc->cond_bytecode.reset ();
11161 }
11162 }
11163
11164 /* Called whether new breakpoints are created, or existing breakpoints
11165 deleted, to update the global location list and recompute which
11166 locations are duplicate of which.
11167
11168 The INSERT_MODE flag determines whether locations may not, may, or
11169 shall be inserted now. See 'enum ugll_insert_mode' for more
11170 info. */
11171
11172 static void
11173 update_global_location_list (enum ugll_insert_mode insert_mode)
11174 {
11175 /* Last breakpoint location address that was marked for update. */
11176 CORE_ADDR last_addr = 0;
11177 /* Last breakpoint location program space that was marked for update. */
11178 int last_pspace_num = -1;
11179
11180 /* Used in the duplicates detection below. When iterating over all
11181 bp_locations, points to the first bp_location of a given address.
11182 Breakpoints and watchpoints of different types are never
11183 duplicates of each other. Keep one pointer for each type of
11184 breakpoint/watchpoint, so we only need to loop over all locations
11185 once. */
11186 struct bp_location *bp_loc_first; /* breakpoint */
11187 struct bp_location *wp_loc_first; /* hardware watchpoint */
11188 struct bp_location *awp_loc_first; /* access watchpoint */
11189 struct bp_location *rwp_loc_first; /* read watchpoint */
11190
11191 /* Saved former bp_locations array which we compare against the newly
11192 built bp_locations from the current state of ALL_BREAKPOINTS. */
11193 std::vector<bp_location *> old_locations = std::move (bp_locations);
11194 bp_locations.clear ();
11195
11196 for (breakpoint *b : all_breakpoints ())
11197 for (bp_location *loc : b->locations ())
11198 bp_locations.push_back (loc);
11199
11200 /* See if we need to "upgrade" a software breakpoint to a hardware
11201 breakpoint. Do this before deciding whether locations are
11202 duplicates. Also do this before sorting because sorting order
11203 depends on location type. */
11204 for (bp_location *loc : bp_locations)
11205 if (!loc->inserted && should_be_inserted (loc))
11206 handle_automatic_hardware_breakpoints (loc);
11207
11208 std::sort (bp_locations.begin (), bp_locations.end (),
11209 bp_location_is_less_than);
11210
11211 bp_locations_target_extensions_update ();
11212
11213 /* Identify bp_location instances that are no longer present in the
11214 new list, and therefore should be freed. Note that it's not
11215 necessary that those locations should be removed from inferior --
11216 if there's another location at the same address (previously
11217 marked as duplicate), we don't need to remove/insert the
11218 location.
11219
11220 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11221 and former bp_location array state respectively. */
11222
11223 size_t loc_i = 0;
11224 for (bp_location *old_loc : old_locations)
11225 {
11226 /* Tells if 'old_loc' is found among the new locations. If
11227 not, we have to free it. */
11228 bool found_object = false;
11229 /* Tells if the location should remain inserted in the target. */
11230 bool keep_in_target = false;
11231 bool removed = false;
11232
11233 /* Skip LOCP entries which will definitely never be needed.
11234 Stop either at or being the one matching OLD_LOC. */
11235 while (loc_i < bp_locations.size ()
11236 && bp_locations[loc_i]->address < old_loc->address)
11237 loc_i++;
11238
11239 for (size_t loc2_i = loc_i;
11240 (loc2_i < bp_locations.size ()
11241 && bp_locations[loc2_i]->address == old_loc->address);
11242 loc2_i++)
11243 {
11244 /* Check if this is a new/duplicated location or a duplicated
11245 location that had its condition modified. If so, we want to send
11246 its condition to the target if evaluation of conditions is taking
11247 place there. */
11248 if (bp_locations[loc2_i]->condition_changed == condition_modified
11249 && (last_addr != old_loc->address
11250 || last_pspace_num != old_loc->pspace->num))
11251 {
11252 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11253 last_pspace_num = old_loc->pspace->num;
11254 }
11255
11256 if (bp_locations[loc2_i] == old_loc)
11257 found_object = true;
11258 }
11259
11260 /* We have already handled this address, update it so that we don't
11261 have to go through updates again. */
11262 last_addr = old_loc->address;
11263
11264 /* Target-side condition evaluation: Handle deleted locations. */
11265 if (!found_object)
11266 force_breakpoint_reinsertion (old_loc);
11267
11268 /* If this location is no longer present, and inserted, look if
11269 there's maybe a new location at the same address. If so,
11270 mark that one inserted, and don't remove this one. This is
11271 needed so that we don't have a time window where a breakpoint
11272 at certain location is not inserted. */
11273
11274 if (old_loc->inserted)
11275 {
11276 /* If the location is inserted now, we might have to remove
11277 it. */
11278
11279 if (found_object && should_be_inserted (old_loc))
11280 {
11281 /* The location is still present in the location list,
11282 and still should be inserted. Don't do anything. */
11283 keep_in_target = true;
11284 }
11285 else
11286 {
11287 /* This location still exists, but it won't be kept in the
11288 target since it may have been disabled. We proceed to
11289 remove its target-side condition. */
11290
11291 /* The location is either no longer present, or got
11292 disabled. See if there's another location at the
11293 same address, in which case we don't need to remove
11294 this one from the target. */
11295
11296 /* OLD_LOC comes from existing struct breakpoint. */
11297 if (bl_address_is_meaningful (old_loc))
11298 {
11299 for (size_t loc2_i = loc_i;
11300 (loc2_i < bp_locations.size ()
11301 && bp_locations[loc2_i]->address == old_loc->address);
11302 loc2_i++)
11303 {
11304 bp_location *loc2 = bp_locations[loc2_i];
11305
11306 if (loc2 == old_loc)
11307 continue;
11308
11309 if (breakpoint_locations_match (loc2, old_loc))
11310 {
11311 /* Read watchpoint locations are switched to
11312 access watchpoints, if the former are not
11313 supported, but the latter are. */
11314 if (is_hardware_watchpoint (old_loc->owner))
11315 {
11316 gdb_assert (is_hardware_watchpoint (loc2->owner));
11317 loc2->watchpoint_type = old_loc->watchpoint_type;
11318 }
11319
11320 /* loc2 is a duplicated location. We need to check
11321 if it should be inserted in case it will be
11322 unduplicated. */
11323 if (unduplicated_should_be_inserted (loc2))
11324 {
11325 swap_insertion (old_loc, loc2);
11326 keep_in_target = true;
11327 break;
11328 }
11329 }
11330 }
11331 }
11332 }
11333
11334 if (!keep_in_target)
11335 {
11336 if (remove_breakpoint (old_loc))
11337 {
11338 /* This is just about all we can do. We could keep
11339 this location on the global list, and try to
11340 remove it next time, but there's no particular
11341 reason why we will succeed next time.
11342
11343 Note that at this point, old_loc->owner is still
11344 valid, as delete_breakpoint frees the breakpoint
11345 only after calling us. */
11346 warning (_("error removing breakpoint %d at %s"),
11347 old_loc->owner->number,
11348 paddress (old_loc->gdbarch, old_loc->address));
11349 }
11350 removed = true;
11351 }
11352 }
11353
11354 if (!found_object)
11355 {
11356 if (removed && target_is_non_stop_p ()
11357 && need_moribund_for_location_type (old_loc))
11358 {
11359 /* This location was removed from the target. In
11360 non-stop mode, a race condition is possible where
11361 we've removed a breakpoint, but stop events for that
11362 breakpoint are already queued and will arrive later.
11363 We apply an heuristic to be able to distinguish such
11364 SIGTRAPs from other random SIGTRAPs: we keep this
11365 breakpoint location for a bit, and will retire it
11366 after we see some number of events. The theory here
11367 is that reporting of events should, "on the average",
11368 be fair, so after a while we'll see events from all
11369 threads that have anything of interest, and no longer
11370 need to keep this breakpoint location around. We
11371 don't hold locations forever so to reduce chances of
11372 mistaking a non-breakpoint SIGTRAP for a breakpoint
11373 SIGTRAP.
11374
11375 The heuristic failing can be disastrous on
11376 decr_pc_after_break targets.
11377
11378 On decr_pc_after_break targets, like e.g., x86-linux,
11379 if we fail to recognize a late breakpoint SIGTRAP,
11380 because events_till_retirement has reached 0 too
11381 soon, we'll fail to do the PC adjustment, and report
11382 a random SIGTRAP to the user. When the user resumes
11383 the inferior, it will most likely immediately crash
11384 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11385 corrupted, because of being resumed e.g., in the
11386 middle of a multi-byte instruction, or skipped a
11387 one-byte instruction. This was actually seen happen
11388 on native x86-linux, and should be less rare on
11389 targets that do not support new thread events, like
11390 remote, due to the heuristic depending on
11391 thread_count.
11392
11393 Mistaking a random SIGTRAP for a breakpoint trap
11394 causes similar symptoms (PC adjustment applied when
11395 it shouldn't), but then again, playing with SIGTRAPs
11396 behind the debugger's back is asking for trouble.
11397
11398 Since hardware watchpoint traps are always
11399 distinguishable from other traps, so we don't need to
11400 apply keep hardware watchpoint moribund locations
11401 around. We simply always ignore hardware watchpoint
11402 traps we can no longer explain. */
11403
11404 process_stratum_target *proc_target = nullptr;
11405 for (inferior *inf : all_inferiors ())
11406 if (inf->pspace == old_loc->pspace)
11407 {
11408 proc_target = inf->process_target ();
11409 break;
11410 }
11411 if (proc_target != nullptr)
11412 old_loc->events_till_retirement
11413 = 3 * (thread_count (proc_target) + 1);
11414 else
11415 old_loc->events_till_retirement = 1;
11416 old_loc->owner = NULL;
11417
11418 moribund_locations.push_back (old_loc);
11419 }
11420 else
11421 {
11422 old_loc->owner = NULL;
11423 decref_bp_location (&old_loc);
11424 }
11425 }
11426 }
11427
11428 /* Rescan breakpoints at the same address and section, marking the
11429 first one as "first" and any others as "duplicates". This is so
11430 that the bpt instruction is only inserted once. If we have a
11431 permanent breakpoint at the same place as BPT, make that one the
11432 official one, and the rest as duplicates. Permanent breakpoints
11433 are sorted first for the same address.
11434
11435 Do the same for hardware watchpoints, but also considering the
11436 watchpoint's type (regular/access/read) and length. */
11437
11438 bp_loc_first = NULL;
11439 wp_loc_first = NULL;
11440 awp_loc_first = NULL;
11441 rwp_loc_first = NULL;
11442
11443 for (bp_location *loc : all_bp_locations ())
11444 {
11445 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11446 non-NULL. */
11447 struct bp_location **loc_first_p;
11448 breakpoint *b = loc->owner;
11449
11450 if (!unduplicated_should_be_inserted (loc)
11451 || !bl_address_is_meaningful (loc)
11452 /* Don't detect duplicate for tracepoint locations because they are
11453 never duplicated. See the comments in field `duplicate' of
11454 `struct bp_location'. */
11455 || is_tracepoint (b))
11456 {
11457 /* Clear the condition modification flag. */
11458 loc->condition_changed = condition_unchanged;
11459 continue;
11460 }
11461
11462 if (b->type == bp_hardware_watchpoint)
11463 loc_first_p = &wp_loc_first;
11464 else if (b->type == bp_read_watchpoint)
11465 loc_first_p = &rwp_loc_first;
11466 else if (b->type == bp_access_watchpoint)
11467 loc_first_p = &awp_loc_first;
11468 else
11469 loc_first_p = &bp_loc_first;
11470
11471 if (*loc_first_p == NULL
11472 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11473 || !breakpoint_locations_match (loc, *loc_first_p))
11474 {
11475 *loc_first_p = loc;
11476 loc->duplicate = 0;
11477
11478 if (is_breakpoint (loc->owner) && loc->condition_changed)
11479 {
11480 loc->needs_update = 1;
11481 /* Clear the condition modification flag. */
11482 loc->condition_changed = condition_unchanged;
11483 }
11484 continue;
11485 }
11486
11487
11488 /* This and the above ensure the invariant that the first location
11489 is not duplicated, and is the inserted one.
11490 All following are marked as duplicated, and are not inserted. */
11491 if (loc->inserted)
11492 swap_insertion (loc, *loc_first_p);
11493 loc->duplicate = 1;
11494
11495 /* Clear the condition modification flag. */
11496 loc->condition_changed = condition_unchanged;
11497 }
11498
11499 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11500 {
11501 if (insert_mode != UGLL_DONT_INSERT)
11502 insert_breakpoint_locations ();
11503 else
11504 {
11505 /* Even though the caller told us to not insert new
11506 locations, we may still need to update conditions on the
11507 target's side of breakpoints that were already inserted
11508 if the target is evaluating breakpoint conditions. We
11509 only update conditions for locations that are marked
11510 "needs_update". */
11511 update_inserted_breakpoint_locations ();
11512 }
11513 }
11514
11515 if (insert_mode != UGLL_DONT_INSERT)
11516 download_tracepoint_locations ();
11517 }
11518
11519 void
11520 breakpoint_retire_moribund (void)
11521 {
11522 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11523 {
11524 struct bp_location *loc = moribund_locations[ix];
11525 if (--(loc->events_till_retirement) == 0)
11526 {
11527 decref_bp_location (&loc);
11528 unordered_remove (moribund_locations, ix);
11529 --ix;
11530 }
11531 }
11532 }
11533
11534 static void
11535 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11536 {
11537
11538 try
11539 {
11540 update_global_location_list (insert_mode);
11541 }
11542 catch (const gdb_exception_error &e)
11543 {
11544 }
11545 }
11546
11547 /* Clear BKP from a BPS. */
11548
11549 static void
11550 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
11551 {
11552 bpstat *bs;
11553
11554 for (bs = bps; bs; bs = bs->next)
11555 if (bs->breakpoint_at == bpt)
11556 {
11557 bs->breakpoint_at = NULL;
11558 bs->old_val = NULL;
11559 /* bs->commands will be freed later. */
11560 }
11561 }
11562
11563 /* Callback for iterate_over_threads. */
11564 static int
11565 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11566 {
11567 struct breakpoint *bpt = (struct breakpoint *) data;
11568
11569 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11570 return 0;
11571 }
11572
11573 /* See breakpoint.h. */
11574
11575 void
11576 code_breakpoint::say_where () const
11577 {
11578 struct value_print_options opts;
11579
11580 get_user_print_options (&opts);
11581
11582 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11583 single string. */
11584 if (loc == NULL)
11585 {
11586 /* For pending locations, the output differs slightly based
11587 on extra_string. If this is non-NULL, it contains either
11588 a condition or dprintf arguments. */
11589 if (extra_string == NULL)
11590 {
11591 gdb_printf (_(" (%s) pending."), locspec->to_string ());
11592 }
11593 else if (type == bp_dprintf)
11594 {
11595 gdb_printf (_(" (%s,%s) pending."),
11596 locspec->to_string (),
11597 extra_string.get ());
11598 }
11599 else
11600 {
11601 gdb_printf (_(" (%s %s) pending."),
11602 locspec->to_string (),
11603 extra_string.get ());
11604 }
11605 }
11606 else
11607 {
11608 if (opts.addressprint || loc->symtab == NULL)
11609 gdb_printf (" at %ps",
11610 styled_string (address_style.style (),
11611 paddress (loc->gdbarch,
11612 loc->address)));
11613 if (loc->symtab != NULL)
11614 {
11615 /* If there is a single location, we can print the location
11616 more nicely. */
11617 if (loc->next == NULL)
11618 {
11619 const char *filename
11620 = symtab_to_filename_for_display (loc->symtab);
11621 gdb_printf (": file %ps, line %d.",
11622 styled_string (file_name_style.style (),
11623 filename),
11624 loc->line_number);
11625 }
11626 else
11627 /* This is not ideal, but each location may have a
11628 different file name, and this at least reflects the
11629 real situation somewhat. */
11630 gdb_printf (": %s.", locspec->to_string ());
11631 }
11632
11633 if (loc->next)
11634 {
11635 struct bp_location *iter = loc;
11636 int n = 0;
11637 for (; iter; iter = iter->next)
11638 ++n;
11639 gdb_printf (" (%d locations)", n);
11640 }
11641 }
11642 }
11643
11644 /* See breakpoint.h. */
11645
11646 bp_location_range breakpoint::locations () const
11647 {
11648 return bp_location_range (this->loc);
11649 }
11650
11651 struct bp_location *
11652 breakpoint::allocate_location ()
11653 {
11654 return new bp_location (this);
11655 }
11656
11657 #define internal_error_pure_virtual_called() \
11658 gdb_assert_not_reached ("pure virtual function called")
11659
11660 int
11661 breakpoint::insert_location (struct bp_location *bl)
11662 {
11663 internal_error_pure_virtual_called ();
11664 }
11665
11666 int
11667 breakpoint::remove_location (struct bp_location *bl,
11668 enum remove_bp_reason reason)
11669 {
11670 internal_error_pure_virtual_called ();
11671 }
11672
11673 int
11674 breakpoint::breakpoint_hit (const struct bp_location *bl,
11675 const address_space *aspace,
11676 CORE_ADDR bp_addr,
11677 const target_waitstatus &ws)
11678 {
11679 internal_error_pure_virtual_called ();
11680 }
11681
11682 int
11683 breakpoint::resources_needed (const struct bp_location *bl)
11684 {
11685 internal_error_pure_virtual_called ();
11686 }
11687
11688 enum print_stop_action
11689 breakpoint::print_it (const bpstat *bs) const
11690 {
11691 internal_error_pure_virtual_called ();
11692 }
11693
11694 void
11695 breakpoint::print_mention () const
11696 {
11697 internal_error_pure_virtual_called ();
11698 }
11699
11700 void
11701 breakpoint::print_recreate (struct ui_file *fp) const
11702 {
11703 internal_error_pure_virtual_called ();
11704 }
11705
11706 /* Default breakpoint_ops methods. */
11707
11708 void
11709 code_breakpoint::re_set ()
11710 {
11711 /* FIXME: is this still reachable? */
11712 if (breakpoint_location_spec_empty_p (this))
11713 {
11714 /* Anything without a location can't be re-set. */
11715 delete_breakpoint (this);
11716 return;
11717 }
11718
11719 re_set_default ();
11720 }
11721
11722 int
11723 code_breakpoint::insert_location (struct bp_location *bl)
11724 {
11725 CORE_ADDR addr = bl->target_info.reqstd_address;
11726
11727 bl->target_info.kind = breakpoint_kind (bl, &addr);
11728 bl->target_info.placed_address = addr;
11729
11730 int result;
11731 if (bl->loc_type == bp_loc_hardware_breakpoint)
11732 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
11733 else
11734 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11735
11736 if (result == 0 && bl->probe.prob != nullptr)
11737 {
11738 /* The insertion was successful, now let's set the probe's semaphore
11739 if needed. */
11740 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11741 }
11742
11743 return result;
11744 }
11745
11746 int
11747 code_breakpoint::remove_location (struct bp_location *bl,
11748 enum remove_bp_reason reason)
11749 {
11750 if (bl->probe.prob != nullptr)
11751 {
11752 /* Let's clear the semaphore before removing the location. */
11753 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11754 }
11755
11756 if (bl->loc_type == bp_loc_hardware_breakpoint)
11757 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11758 else
11759 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
11760 }
11761
11762 int
11763 code_breakpoint::breakpoint_hit (const struct bp_location *bl,
11764 const address_space *aspace,
11765 CORE_ADDR bp_addr,
11766 const target_waitstatus &ws)
11767 {
11768 if (ws.kind () != TARGET_WAITKIND_STOPPED
11769 || ws.sig () != GDB_SIGNAL_TRAP)
11770 return 0;
11771
11772 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11773 aspace, bp_addr))
11774 return 0;
11775
11776 if (overlay_debugging /* unmapped overlay section */
11777 && section_is_overlay (bl->section)
11778 && !section_is_mapped (bl->section))
11779 return 0;
11780
11781 return 1;
11782 }
11783
11784 int
11785 dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
11786 const address_space *aspace,
11787 CORE_ADDR bp_addr,
11788 const target_waitstatus &ws)
11789 {
11790 if (dprintf_style == dprintf_style_agent
11791 && target_can_run_breakpoint_commands ())
11792 {
11793 /* An agent-style dprintf never causes a stop. If we see a trap
11794 for this address it must be for a breakpoint that happens to
11795 be set at the same address. */
11796 return 0;
11797 }
11798
11799 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
11800 }
11801
11802 int
11803 ordinary_breakpoint::resources_needed (const struct bp_location *bl)
11804 {
11805 gdb_assert (type == bp_hardware_breakpoint);
11806
11807 return 1;
11808 }
11809
11810 enum print_stop_action
11811 ordinary_breakpoint::print_it (const bpstat *bs) const
11812 {
11813 const struct bp_location *bl;
11814 int bp_temp;
11815 struct ui_out *uiout = current_uiout;
11816
11817 bl = bs->bp_location_at.get ();
11818
11819 bp_temp = disposition == disp_del;
11820 if (bl->address != bl->requested_address)
11821 breakpoint_adjustment_warning (bl->requested_address,
11822 bl->address,
11823 number, true);
11824 annotate_breakpoint (number);
11825 maybe_print_thread_hit_breakpoint (uiout);
11826
11827 if (uiout->is_mi_like_p ())
11828 {
11829 uiout->field_string ("reason",
11830 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11831 uiout->field_string ("disp", bpdisp_text (disposition));
11832 }
11833
11834 if (bp_temp)
11835 uiout->text ("Temporary breakpoint ");
11836 else
11837 uiout->text ("Breakpoint ");
11838 print_num_locno (bs, uiout);
11839 uiout->text (", ");
11840
11841 return PRINT_SRC_AND_LOC;
11842 }
11843
11844 void
11845 ordinary_breakpoint::print_mention () const
11846 {
11847 if (current_uiout->is_mi_like_p ())
11848 return;
11849
11850 switch (type)
11851 {
11852 case bp_breakpoint:
11853 case bp_gnu_ifunc_resolver:
11854 if (disposition == disp_del)
11855 gdb_printf (_("Temporary breakpoint"));
11856 else
11857 gdb_printf (_("Breakpoint"));
11858 gdb_printf (_(" %d"), number);
11859 if (type == bp_gnu_ifunc_resolver)
11860 gdb_printf (_(" at gnu-indirect-function resolver"));
11861 break;
11862 case bp_hardware_breakpoint:
11863 gdb_printf (_("Hardware assisted breakpoint %d"), number);
11864 break;
11865 case bp_dprintf:
11866 gdb_printf (_("Dprintf %d"), number);
11867 break;
11868 }
11869
11870 say_where ();
11871 }
11872
11873 void
11874 ordinary_breakpoint::print_recreate (struct ui_file *fp) const
11875 {
11876 if (type == bp_breakpoint && disposition == disp_del)
11877 gdb_printf (fp, "tbreak");
11878 else if (type == bp_breakpoint)
11879 gdb_printf (fp, "break");
11880 else if (type == bp_hardware_breakpoint
11881 && disposition == disp_del)
11882 gdb_printf (fp, "thbreak");
11883 else if (type == bp_hardware_breakpoint)
11884 gdb_printf (fp, "hbreak");
11885 else
11886 internal_error (_("unhandled breakpoint type %d"), (int) type);
11887
11888 gdb_printf (fp, " %s", locspec->to_string ());
11889
11890 /* Print out extra_string if this breakpoint is pending. It might
11891 contain, for example, conditions that were set by the user. */
11892 if (loc == NULL && extra_string != NULL)
11893 gdb_printf (fp, " %s", extra_string.get ());
11894
11895 print_recreate_thread (fp);
11896 }
11897
11898 std::vector<symtab_and_line>
11899 code_breakpoint::decode_location_spec (location_spec *locspec,
11900 program_space *search_pspace)
11901 {
11902 if (locspec->type () == PROBE_LOCATION_SPEC)
11903 return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
11904
11905 struct linespec_result canonical;
11906
11907 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
11908 NULL, 0, &canonical, multiple_symbols_all,
11909 filter.get ());
11910
11911 /* We should get 0 or 1 resulting SALs. */
11912 gdb_assert (canonical.lsals.size () < 2);
11913
11914 if (!canonical.lsals.empty ())
11915 {
11916 const linespec_sals &lsal = canonical.lsals[0];
11917 return std::move (lsal.sals);
11918 }
11919 return {};
11920 }
11921
11922 /* Virtual table for internal breakpoints. */
11923
11924 void
11925 internal_breakpoint::re_set ()
11926 {
11927 switch (type)
11928 {
11929 /* Delete overlay event and longjmp master breakpoints; they
11930 will be reset later by breakpoint_re_set. */
11931 case bp_overlay_event:
11932 case bp_longjmp_master:
11933 case bp_std_terminate_master:
11934 case bp_exception_master:
11935 delete_breakpoint (this);
11936 break;
11937
11938 /* This breakpoint is special, it's set up when the inferior
11939 starts and we really don't want to touch it. */
11940 case bp_shlib_event:
11941
11942 /* Like bp_shlib_event, this breakpoint type is special. Once
11943 it is set up, we do not want to touch it. */
11944 case bp_thread_event:
11945 break;
11946 }
11947 }
11948
11949 void
11950 internal_breakpoint::check_status (bpstat *bs)
11951 {
11952 if (type == bp_shlib_event)
11953 {
11954 /* If requested, stop when the dynamic linker notifies GDB of
11955 events. This allows the user to get control and place
11956 breakpoints in initializer routines for dynamically loaded
11957 objects (among other things). */
11958 bs->stop = stop_on_solib_events != 0;
11959 bs->print = stop_on_solib_events != 0;
11960 }
11961 else
11962 bs->stop = false;
11963 }
11964
11965 enum print_stop_action
11966 internal_breakpoint::print_it (const bpstat *bs) const
11967 {
11968 switch (type)
11969 {
11970 case bp_shlib_event:
11971 /* Did we stop because the user set the stop_on_solib_events
11972 variable? (If so, we report this as a generic, "Stopped due
11973 to shlib event" message.) */
11974 print_solib_event (false);
11975 break;
11976
11977 case bp_thread_event:
11978 /* Not sure how we will get here.
11979 GDB should not stop for these breakpoints. */
11980 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
11981 break;
11982
11983 case bp_overlay_event:
11984 /* By analogy with the thread event, GDB should not stop for these. */
11985 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
11986 break;
11987
11988 case bp_longjmp_master:
11989 /* These should never be enabled. */
11990 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
11991 break;
11992
11993 case bp_std_terminate_master:
11994 /* These should never be enabled. */
11995 gdb_printf (_("std::terminate Master Breakpoint: "
11996 "gdb should not stop!\n"));
11997 break;
11998
11999 case bp_exception_master:
12000 /* These should never be enabled. */
12001 gdb_printf (_("Exception Master Breakpoint: "
12002 "gdb should not stop!\n"));
12003 break;
12004 }
12005
12006 return PRINT_NOTHING;
12007 }
12008
12009 void
12010 internal_breakpoint::print_mention () const
12011 {
12012 /* Nothing to mention. These breakpoints are internal. */
12013 }
12014
12015 /* Virtual table for momentary breakpoints */
12016
12017 void
12018 momentary_breakpoint::re_set ()
12019 {
12020 /* Keep temporary breakpoints, which can be encountered when we step
12021 over a dlopen call and solib_add is resetting the breakpoints.
12022 Otherwise these should have been blown away via the cleanup chain
12023 or by breakpoint_init_inferior when we rerun the executable. */
12024 }
12025
12026 void
12027 momentary_breakpoint::check_status (bpstat *bs)
12028 {
12029 /* Nothing. The point of these breakpoints is causing a stop. */
12030 }
12031
12032 enum print_stop_action
12033 momentary_breakpoint::print_it (const bpstat *bs) const
12034 {
12035 return PRINT_UNKNOWN;
12036 }
12037
12038 void
12039 momentary_breakpoint::print_mention () const
12040 {
12041 /* Nothing to mention. These breakpoints are internal. */
12042 }
12043
12044 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12045
12046 It gets cleared already on the removal of the first one of such placed
12047 breakpoints. This is OK as they get all removed altogether. */
12048
12049 longjmp_breakpoint::~longjmp_breakpoint ()
12050 {
12051 thread_info *tp = find_thread_global_id (this->thread);
12052
12053 if (tp != NULL)
12054 tp->initiating_frame = null_frame_id;
12055 }
12056
12057 static void
12058 bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
12059 struct linespec_result *canonical)
12060
12061 {
12062 struct linespec_sals lsal;
12063
12064 lsal.sals = parse_probes (locspec, NULL, canonical);
12065 lsal.canonical = xstrdup (canonical->locspec->to_string ());
12066 canonical->lsals.push_back (std::move (lsal));
12067 }
12068
12069 static std::vector<symtab_and_line>
12070 bkpt_probe_decode_location_spec (struct breakpoint *b,
12071 location_spec *locspec,
12072 program_space *search_pspace)
12073 {
12074 std::vector<symtab_and_line> sals
12075 = parse_probes (locspec, search_pspace, NULL);
12076 if (sals.empty ())
12077 error (_("probe not found"));
12078 return sals;
12079 }
12080
12081 int
12082 tracepoint::breakpoint_hit (const struct bp_location *bl,
12083 const address_space *aspace, CORE_ADDR bp_addr,
12084 const target_waitstatus &ws)
12085 {
12086 /* By definition, the inferior does not report stops at
12087 tracepoints. */
12088 return 0;
12089 }
12090
12091 void
12092 tracepoint::print_one_detail (struct ui_out *uiout) const
12093 {
12094 if (!static_trace_marker_id.empty ())
12095 {
12096 gdb_assert (type == bp_static_tracepoint
12097 || type == bp_static_marker_tracepoint);
12098
12099 uiout->message ("\tmarker id is %pF\n",
12100 string_field ("static-tracepoint-marker-string-id",
12101 static_trace_marker_id.c_str ()));
12102 }
12103 }
12104
12105 void
12106 tracepoint::print_mention () const
12107 {
12108 if (current_uiout->is_mi_like_p ())
12109 return;
12110
12111 switch (type)
12112 {
12113 case bp_tracepoint:
12114 gdb_printf (_("Tracepoint"));
12115 gdb_printf (_(" %d"), number);
12116 break;
12117 case bp_fast_tracepoint:
12118 gdb_printf (_("Fast tracepoint"));
12119 gdb_printf (_(" %d"), number);
12120 break;
12121 case bp_static_tracepoint:
12122 case bp_static_marker_tracepoint:
12123 gdb_printf (_("Static tracepoint"));
12124 gdb_printf (_(" %d"), number);
12125 break;
12126 default:
12127 internal_error (_("unhandled tracepoint type %d"), (int) type);
12128 }
12129
12130 say_where ();
12131 }
12132
12133 void
12134 tracepoint::print_recreate (struct ui_file *fp) const
12135 {
12136 if (type == bp_fast_tracepoint)
12137 gdb_printf (fp, "ftrace");
12138 else if (type == bp_static_tracepoint
12139 || type == bp_static_marker_tracepoint)
12140 gdb_printf (fp, "strace");
12141 else if (type == bp_tracepoint)
12142 gdb_printf (fp, "trace");
12143 else
12144 internal_error (_("unhandled tracepoint type %d"), (int) type);
12145
12146 gdb_printf (fp, " %s", locspec->to_string ());
12147 print_recreate_thread (fp);
12148
12149 if (pass_count)
12150 gdb_printf (fp, " passcount %d\n", pass_count);
12151 }
12152
12153 /* Virtual table for tracepoints on static probes. */
12154
12155 static void
12156 tracepoint_probe_create_sals_from_location_spec
12157 (location_spec *locspec,
12158 struct linespec_result *canonical)
12159 {
12160 /* We use the same method for breakpoint on probes. */
12161 bkpt_probe_create_sals_from_location_spec (locspec, canonical);
12162 }
12163
12164 void
12165 dprintf_breakpoint::re_set ()
12166 {
12167 re_set_default ();
12168
12169 /* extra_string should never be non-NULL for dprintf. */
12170 gdb_assert (extra_string != NULL);
12171
12172 /* 1 - connect to target 1, that can run breakpoint commands.
12173 2 - create a dprintf, which resolves fine.
12174 3 - disconnect from target 1
12175 4 - connect to target 2, that can NOT run breakpoint commands.
12176
12177 After steps #3/#4, you'll want the dprintf command list to
12178 be updated, because target 1 and 2 may well return different
12179 answers for target_can_run_breakpoint_commands().
12180 Given absence of finer grained resetting, we get to do
12181 it all the time. */
12182 if (extra_string != NULL)
12183 update_dprintf_command_list (this);
12184 }
12185
12186 /* Implement the "print_recreate" method for dprintf. */
12187
12188 void
12189 dprintf_breakpoint::print_recreate (struct ui_file *fp) const
12190 {
12191 gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
12192 print_recreate_thread (fp);
12193 }
12194
12195 /* Implement the "after_condition_true" method for dprintf.
12196
12197 dprintf's are implemented with regular commands in their command
12198 list, but we run the commands here instead of before presenting the
12199 stop to the user, as dprintf's don't actually cause a stop. This
12200 also makes it so that the commands of multiple dprintfs at the same
12201 address are all handled. */
12202
12203 void
12204 dprintf_breakpoint::after_condition_true (struct bpstat *bs)
12205 {
12206 /* dprintf's never cause a stop. This wasn't set in the
12207 check_status hook instead because that would make the dprintf's
12208 condition not be evaluated. */
12209 bs->stop = false;
12210
12211 /* Run the command list here. Take ownership of it instead of
12212 copying. We never want these commands to run later in
12213 bpstat_do_actions, if a breakpoint that causes a stop happens to
12214 be set at same address as this dprintf, or even if running the
12215 commands here throws. */
12216 counted_command_line cmds = std::move (bs->commands);
12217 gdb_assert (cmds != nullptr);
12218 execute_control_commands (cmds.get (), 0);
12219 }
12220
12221 /* The breakpoint_ops structure to be used on static tracepoints with
12222 markers (`-m'). */
12223
12224 static void
12225 strace_marker_create_sals_from_location_spec (location_spec *locspec,
12226 struct linespec_result *canonical)
12227 {
12228 struct linespec_sals lsal;
12229 const char *arg_start, *arg;
12230
12231 arg = arg_start = as_linespec_location_spec (locspec)->spec_string;
12232 lsal.sals = decode_static_tracepoint_spec (&arg);
12233
12234 std::string str (arg_start, arg - arg_start);
12235 const char *ptr = str.c_str ();
12236 canonical->locspec
12237 = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
12238
12239 lsal.canonical = xstrdup (canonical->locspec->to_string ());
12240 canonical->lsals.push_back (std::move (lsal));
12241 }
12242
12243 static void
12244 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12245 struct linespec_result *canonical,
12246 gdb::unique_xmalloc_ptr<char> cond_string,
12247 gdb::unique_xmalloc_ptr<char> extra_string,
12248 enum bptype type_wanted,
12249 enum bpdisp disposition,
12250 int thread,
12251 int task, int ignore_count,
12252 int from_tty, int enabled,
12253 int internal, unsigned flags)
12254 {
12255 const linespec_sals &lsal = canonical->lsals[0];
12256
12257 /* If the user is creating a static tracepoint by marker id
12258 (strace -m MARKER_ID), then store the sals index, so that
12259 breakpoint_re_set can try to match up which of the newly
12260 found markers corresponds to this one, and, don't try to
12261 expand multiple locations for each sal, given than SALS
12262 already should contain all sals for MARKER_ID. */
12263
12264 for (size_t i = 0; i < lsal.sals.size (); i++)
12265 {
12266 location_spec_up locspec = canonical->locspec->clone ();
12267
12268 std::unique_ptr<tracepoint> tp
12269 (new tracepoint (gdbarch,
12270 type_wanted,
12271 lsal.sals[i],
12272 std::move (locspec),
12273 NULL,
12274 std::move (cond_string),
12275 std::move (extra_string),
12276 disposition,
12277 thread, task, ignore_count,
12278 from_tty, enabled, flags,
12279 canonical->special_display));
12280
12281 /* Given that its possible to have multiple markers with
12282 the same string id, if the user is creating a static
12283 tracepoint by marker id ("strace -m MARKER_ID"), then
12284 store the sals index, so that breakpoint_re_set can
12285 try to match up which of the newly found markers
12286 corresponds to this one */
12287 tp->static_trace_marker_id_idx = i;
12288
12289 install_breakpoint (internal, std::move (tp), 0);
12290 }
12291 }
12292
12293 std::vector<symtab_and_line>
12294 static_marker_tracepoint::decode_location_spec (location_spec *locspec,
12295 program_space *search_pspace)
12296 {
12297 const char *s = as_linespec_location_spec (locspec)->spec_string;
12298
12299 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12300 if (sals.size () > static_trace_marker_id_idx)
12301 {
12302 sals[0] = sals[static_trace_marker_id_idx];
12303 sals.resize (1);
12304 return sals;
12305 }
12306 else
12307 error (_("marker %s not found"), static_trace_marker_id.c_str ());
12308 }
12309
12310 /* Static tracepoints with marker (`-m'). */
12311 static struct breakpoint_ops strace_marker_breakpoint_ops =
12312 {
12313 strace_marker_create_sals_from_location_spec,
12314 strace_marker_create_breakpoints_sal,
12315 };
12316
12317 static bool
12318 strace_marker_p (struct breakpoint *b)
12319 {
12320 return b->type == bp_static_marker_tracepoint;
12321 }
12322
12323 /* Delete a breakpoint and clean up all traces of it in the data
12324 structures. */
12325
12326 void
12327 delete_breakpoint (struct breakpoint *bpt)
12328 {
12329 gdb_assert (bpt != NULL);
12330
12331 /* Has this bp already been deleted? This can happen because
12332 multiple lists can hold pointers to bp's. bpstat lists are
12333 especial culprits.
12334
12335 One example of this happening is a watchpoint's scope bp. When
12336 the scope bp triggers, we notice that the watchpoint is out of
12337 scope, and delete it. We also delete its scope bp. But the
12338 scope bp is marked "auto-deleting", and is already on a bpstat.
12339 That bpstat is then checked for auto-deleting bp's, which are
12340 deleted.
12341
12342 A real solution to this problem might involve reference counts in
12343 bp's, and/or giving them pointers back to their referencing
12344 bpstat's, and teaching delete_breakpoint to only free a bp's
12345 storage when no more references were extent. A cheaper bandaid
12346 was chosen. */
12347 if (bpt->type == bp_none)
12348 return;
12349
12350 /* At least avoid this stale reference until the reference counting
12351 of breakpoints gets resolved. */
12352 if (bpt->related_breakpoint != bpt)
12353 {
12354 struct breakpoint *related;
12355 struct watchpoint *w;
12356
12357 if (bpt->type == bp_watchpoint_scope)
12358 w = (struct watchpoint *) bpt->related_breakpoint;
12359 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12360 w = (struct watchpoint *) bpt;
12361 else
12362 w = NULL;
12363 if (w != NULL)
12364 watchpoint_del_at_next_stop (w);
12365
12366 /* Unlink bpt from the bpt->related_breakpoint ring. */
12367 for (related = bpt; related->related_breakpoint != bpt;
12368 related = related->related_breakpoint);
12369 related->related_breakpoint = bpt->related_breakpoint;
12370 bpt->related_breakpoint = bpt;
12371 }
12372
12373 /* watch_command_1 creates a watchpoint but only sets its number if
12374 update_watchpoint succeeds in creating its bp_locations. If there's
12375 a problem in that process, we'll be asked to delete the half-created
12376 watchpoint. In that case, don't announce the deletion. */
12377 if (bpt->number)
12378 gdb::observers::breakpoint_deleted.notify (bpt);
12379
12380 if (breakpoint_chain == bpt)
12381 breakpoint_chain = bpt->next;
12382
12383 for (breakpoint *b : all_breakpoints ())
12384 if (b->next == bpt)
12385 {
12386 b->next = bpt->next;
12387 break;
12388 }
12389
12390 /* Be sure no bpstat's are pointing at the breakpoint after it's
12391 been freed. */
12392 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12393 in all threads for now. Note that we cannot just remove bpstats
12394 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12395 commands are associated with the bpstat; if we remove it here,
12396 then the later call to bpstat_do_actions (&stop_bpstat); in
12397 event-top.c won't do anything, and temporary breakpoints with
12398 commands won't work. */
12399
12400 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12401
12402 /* Now that breakpoint is removed from breakpoint list, update the
12403 global location list. This will remove locations that used to
12404 belong to this breakpoint. Do this before freeing the breakpoint
12405 itself, since remove_breakpoint looks at location's owner. It
12406 might be better design to have location completely
12407 self-contained, but it's not the case now. */
12408 update_global_location_list (UGLL_DONT_INSERT);
12409
12410 /* On the chance that someone will soon try again to delete this
12411 same bp, we mark it as deleted before freeing its storage. */
12412 bpt->type = bp_none;
12413 delete bpt;
12414 }
12415
12416 /* Iterator function to call a user-provided callback function once
12417 for each of B and its related breakpoints. */
12418
12419 static void
12420 iterate_over_related_breakpoints (struct breakpoint *b,
12421 gdb::function_view<void (breakpoint *)> function)
12422 {
12423 struct breakpoint *related;
12424
12425 related = b;
12426 do
12427 {
12428 struct breakpoint *next;
12429
12430 /* FUNCTION may delete RELATED. */
12431 next = related->related_breakpoint;
12432
12433 if (next == related)
12434 {
12435 /* RELATED is the last ring entry. */
12436 function (related);
12437
12438 /* FUNCTION may have deleted it, so we'd never reach back to
12439 B. There's nothing left to do anyway, so just break
12440 out. */
12441 break;
12442 }
12443 else
12444 function (related);
12445
12446 related = next;
12447 }
12448 while (related != b);
12449 }
12450
12451 static void
12452 delete_command (const char *arg, int from_tty)
12453 {
12454 dont_repeat ();
12455
12456 if (arg == 0)
12457 {
12458 int breaks_to_delete = 0;
12459
12460 /* Delete all breakpoints if no argument. Do not delete
12461 internal breakpoints, these have to be deleted with an
12462 explicit breakpoint number argument. */
12463 for (breakpoint *b : all_breakpoints ())
12464 if (user_breakpoint_p (b))
12465 {
12466 breaks_to_delete = 1;
12467 break;
12468 }
12469
12470 /* Ask user only if there are some breakpoints to delete. */
12471 if (!from_tty
12472 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
12473 for (breakpoint *b : all_breakpoints_safe ())
12474 if (user_breakpoint_p (b))
12475 delete_breakpoint (b);
12476 }
12477 else
12478 map_breakpoint_numbers
12479 (arg, [&] (breakpoint *br)
12480 {
12481 iterate_over_related_breakpoints (br, delete_breakpoint);
12482 });
12483 }
12484
12485 /* Return true if all locations of B bound to PSPACE are pending. If
12486 PSPACE is NULL, all locations of all program spaces are
12487 considered. */
12488
12489 static bool
12490 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
12491 {
12492 for (bp_location *loc : b->locations ())
12493 if ((pspace == NULL
12494 || loc->pspace == pspace)
12495 && !loc->shlib_disabled
12496 && !loc->pspace->executing_startup)
12497 return false;
12498 return true;
12499 }
12500
12501 /* Subroutine of update_breakpoint_locations to simplify it.
12502 Return true if multiple fns in list LOC have the same name.
12503 Null names are ignored. */
12504
12505 static bool
12506 ambiguous_names_p (struct bp_location *loc)
12507 {
12508 struct bp_location *l;
12509 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
12510 xcalloc, xfree));
12511
12512 for (l = loc; l != NULL; l = l->next)
12513 {
12514 const char **slot;
12515 const char *name = l->function_name.get ();
12516
12517 /* Allow for some names to be NULL, ignore them. */
12518 if (name == NULL)
12519 continue;
12520
12521 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
12522 INSERT);
12523 /* NOTE: We can assume slot != NULL here because xcalloc never
12524 returns NULL. */
12525 if (*slot != NULL)
12526 return true;
12527 *slot = name;
12528 }
12529
12530 return false;
12531 }
12532
12533 /* When symbols change, it probably means the sources changed as well,
12534 and it might mean the static tracepoint markers are no longer at
12535 the same address or line numbers they used to be at last we
12536 checked. Losing your static tracepoints whenever you rebuild is
12537 undesirable. This function tries to resync/rematch gdb static
12538 tracepoints with the markers on the target, for static tracepoints
12539 that have not been set by marker id. Static tracepoint that have
12540 been set by marker id are reset by marker id in breakpoint_re_set.
12541 The heuristic is:
12542
12543 1) For a tracepoint set at a specific address, look for a marker at
12544 the old PC. If one is found there, assume to be the same marker.
12545 If the name / string id of the marker found is different from the
12546 previous known name, assume that means the user renamed the marker
12547 in the sources, and output a warning.
12548
12549 2) For a tracepoint set at a given line number, look for a marker
12550 at the new address of the old line number. If one is found there,
12551 assume to be the same marker. If the name / string id of the
12552 marker found is different from the previous known name, assume that
12553 means the user renamed the marker in the sources, and output a
12554 warning.
12555
12556 3) If a marker is no longer found at the same address or line, it
12557 may mean the marker no longer exists. But it may also just mean
12558 the code changed a bit. Maybe the user added a few lines of code
12559 that made the marker move up or down (in line number terms). Ask
12560 the target for info about the marker with the string id as we knew
12561 it. If found, update line number and address in the matching
12562 static tracepoint. This will get confused if there's more than one
12563 marker with the same ID (possible in UST, although unadvised
12564 precisely because it confuses tools). */
12565
12566 static struct symtab_and_line
12567 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12568 {
12569 struct tracepoint *tp = (struct tracepoint *) b;
12570 struct static_tracepoint_marker marker;
12571 CORE_ADDR pc;
12572
12573 pc = sal.pc;
12574 if (sal.line)
12575 find_line_pc (sal.symtab, sal.line, &pc);
12576
12577 if (target_static_tracepoint_marker_at (pc, &marker))
12578 {
12579 if (tp->static_trace_marker_id != marker.str_id)
12580 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12581 b->number, tp->static_trace_marker_id.c_str (),
12582 marker.str_id.c_str ());
12583
12584 tp->static_trace_marker_id = std::move (marker.str_id);
12585
12586 return sal;
12587 }
12588
12589 /* Old marker wasn't found on target at lineno. Try looking it up
12590 by string ID. */
12591 if (!sal.explicit_pc
12592 && sal.line != 0
12593 && sal.symtab != NULL
12594 && !tp->static_trace_marker_id.empty ())
12595 {
12596 std::vector<static_tracepoint_marker> markers
12597 = target_static_tracepoint_markers_by_strid
12598 (tp->static_trace_marker_id.c_str ());
12599
12600 if (!markers.empty ())
12601 {
12602 struct symbol *sym;
12603 struct static_tracepoint_marker *tpmarker;
12604 struct ui_out *uiout = current_uiout;
12605
12606 tpmarker = &markers[0];
12607
12608 tp->static_trace_marker_id = std::move (tpmarker->str_id);
12609
12610 warning (_("marker for static tracepoint %d (%s) not "
12611 "found at previous line number"),
12612 b->number, tp->static_trace_marker_id.c_str ());
12613
12614 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
12615 sym = find_pc_sect_function (tpmarker->address, NULL);
12616 uiout->text ("Now in ");
12617 if (sym)
12618 {
12619 uiout->field_string ("func", sym->print_name (),
12620 function_name_style.style ());
12621 uiout->text (" at ");
12622 }
12623 uiout->field_string ("file",
12624 symtab_to_filename_for_display (sal2.symtab),
12625 file_name_style.style ());
12626 uiout->text (":");
12627
12628 if (uiout->is_mi_like_p ())
12629 {
12630 const char *fullname = symtab_to_fullname (sal2.symtab);
12631
12632 uiout->field_string ("fullname", fullname);
12633 }
12634
12635 uiout->field_signed ("line", sal2.line);
12636 uiout->text ("\n");
12637
12638 b->loc->line_number = sal2.line;
12639 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
12640
12641 std::unique_ptr<explicit_location_spec> els
12642 (new explicit_location_spec ());
12643 els->source_filename
12644 = xstrdup (symtab_to_filename_for_display (sal2.symtab));
12645 els->line_offset.offset = b->loc->line_number;
12646 els->line_offset.sign = LINE_OFFSET_NONE;
12647
12648 b->locspec = std::move (els);
12649
12650 /* Might be nice to check if function changed, and warn if
12651 so. */
12652 }
12653 }
12654 return sal;
12655 }
12656
12657 /* Returns true iff locations A and B are sufficiently same that
12658 we don't need to report breakpoint as changed. */
12659
12660 static bool
12661 locations_are_equal (struct bp_location *a, struct bp_location *b)
12662 {
12663 while (a && b)
12664 {
12665 if (a->address != b->address)
12666 return false;
12667
12668 if (a->shlib_disabled != b->shlib_disabled)
12669 return false;
12670
12671 if (a->enabled != b->enabled)
12672 return false;
12673
12674 if (a->disabled_by_cond != b->disabled_by_cond)
12675 return false;
12676
12677 a = a->next;
12678 b = b->next;
12679 }
12680
12681 if ((a == NULL) != (b == NULL))
12682 return false;
12683
12684 return true;
12685 }
12686
12687 /* Split all locations of B that are bound to PSPACE out of B's
12688 location list to a separate list and return that list's head. If
12689 PSPACE is NULL, hoist out all locations of B. */
12690
12691 static struct bp_location *
12692 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12693 {
12694 struct bp_location head;
12695 struct bp_location *i = b->loc;
12696 struct bp_location **i_link = &b->loc;
12697 struct bp_location *hoisted = &head;
12698
12699 if (pspace == NULL)
12700 {
12701 i = b->loc;
12702 b->loc = NULL;
12703 return i;
12704 }
12705
12706 head.next = NULL;
12707
12708 while (i != NULL)
12709 {
12710 if (i->pspace == pspace)
12711 {
12712 *i_link = i->next;
12713 i->next = NULL;
12714 hoisted->next = i;
12715 hoisted = i;
12716 }
12717 else
12718 i_link = &i->next;
12719 i = *i_link;
12720 }
12721
12722 return head.next;
12723 }
12724
12725 /* Create new breakpoint locations for B (a hardware or software
12726 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12727 zero, then B is a ranged breakpoint. Only recreates locations for
12728 FILTER_PSPACE. Locations of other program spaces are left
12729 untouched. */
12730
12731 void
12732 update_breakpoint_locations (code_breakpoint *b,
12733 struct program_space *filter_pspace,
12734 gdb::array_view<const symtab_and_line> sals,
12735 gdb::array_view<const symtab_and_line> sals_end)
12736 {
12737 struct bp_location *existing_locations;
12738
12739 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
12740 {
12741 /* Ranged breakpoints have only one start location and one end
12742 location. */
12743 b->enable_state = bp_disabled;
12744 gdb_printf (gdb_stderr,
12745 _("Could not reset ranged breakpoint %d: "
12746 "multiple locations found\n"),
12747 b->number);
12748 return;
12749 }
12750
12751 /* If there's no new locations, and all existing locations are
12752 pending, don't do anything. This optimizes the common case where
12753 all locations are in the same shared library, that was unloaded.
12754 We'd like to retain the location, so that when the library is
12755 loaded again, we don't loose the enabled/disabled status of the
12756 individual locations. */
12757 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
12758 return;
12759
12760 existing_locations = hoist_existing_locations (b, filter_pspace);
12761
12762 for (const auto &sal : sals)
12763 {
12764 struct bp_location *new_loc;
12765
12766 switch_to_program_space_and_thread (sal.pspace);
12767
12768 new_loc = b->add_location (sal);
12769
12770 /* Reparse conditions, they might contain references to the
12771 old symtab. */
12772 if (b->cond_string != NULL)
12773 {
12774 const char *s;
12775
12776 s = b->cond_string.get ();
12777 try
12778 {
12779 new_loc->cond = parse_exp_1 (&s, sal.pc,
12780 block_for_pc (sal.pc),
12781 0);
12782 }
12783 catch (const gdb_exception_error &e)
12784 {
12785 new_loc->disabled_by_cond = true;
12786 }
12787 }
12788
12789 if (!sals_end.empty ())
12790 {
12791 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
12792
12793 new_loc->length = end - sals[0].pc + 1;
12794 }
12795 }
12796
12797 /* If possible, carry over 'disable' status from existing
12798 breakpoints. */
12799 {
12800 struct bp_location *e = existing_locations;
12801 /* If there are multiple breakpoints with the same function name,
12802 e.g. for inline functions, comparing function names won't work.
12803 Instead compare pc addresses; this is just a heuristic as things
12804 may have moved, but in practice it gives the correct answer
12805 often enough until a better solution is found. */
12806 int have_ambiguous_names = ambiguous_names_p (b->loc);
12807
12808 for (; e; e = e->next)
12809 {
12810 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
12811 {
12812 if (have_ambiguous_names)
12813 {
12814 for (bp_location *l : b->locations ())
12815 {
12816 /* Ignore software vs hardware location type at
12817 this point, because with "set breakpoint
12818 auto-hw", after a re-set, locations that were
12819 hardware can end up as software, or vice versa.
12820 As mentioned above, this is an heuristic and in
12821 practice should give the correct answer often
12822 enough. */
12823 if (breakpoint_locations_match (e, l, true))
12824 {
12825 l->enabled = e->enabled;
12826 l->disabled_by_cond = e->disabled_by_cond;
12827 break;
12828 }
12829 }
12830 }
12831 else
12832 {
12833 for (bp_location *l : b->locations ())
12834 if (l->function_name
12835 && strcmp (e->function_name.get (),
12836 l->function_name.get ()) == 0)
12837 {
12838 l->enabled = e->enabled;
12839 l->disabled_by_cond = e->disabled_by_cond;
12840 break;
12841 }
12842 }
12843 }
12844 }
12845 }
12846
12847 if (!locations_are_equal (existing_locations, b->loc))
12848 gdb::observers::breakpoint_modified.notify (b);
12849 }
12850
12851 /* Find the SaL locations corresponding to the given LOCSPEC.
12852 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12853
12854 std::vector<symtab_and_line>
12855 code_breakpoint::location_spec_to_sals (location_spec *locspec,
12856 struct program_space *search_pspace,
12857 int *found)
12858 {
12859 struct gdb_exception exception;
12860
12861 std::vector<symtab_and_line> sals;
12862
12863 try
12864 {
12865 sals = decode_location_spec (locspec, search_pspace);
12866 }
12867 catch (gdb_exception_error &e)
12868 {
12869 int not_found_and_ok = false;
12870
12871 /* For pending breakpoints, it's expected that parsing will
12872 fail until the right shared library is loaded. User has
12873 already told to create pending breakpoints and don't need
12874 extra messages. If breakpoint is in bp_shlib_disabled
12875 state, then user already saw the message about that
12876 breakpoint being disabled, and don't want to see more
12877 errors. */
12878 if (e.error == NOT_FOUND_ERROR
12879 && (condition_not_parsed
12880 || (loc != NULL
12881 && search_pspace != NULL
12882 && loc->pspace != search_pspace)
12883 || (loc && loc->shlib_disabled)
12884 || (loc && loc->pspace->executing_startup)
12885 || enable_state == bp_disabled))
12886 not_found_and_ok = true;
12887
12888 if (!not_found_and_ok)
12889 {
12890 /* We surely don't want to warn about the same breakpoint
12891 10 times. One solution, implemented here, is disable
12892 the breakpoint on error. Another solution would be to
12893 have separate 'warning emitted' flag. Since this
12894 happens only when a binary has changed, I don't know
12895 which approach is better. */
12896 enable_state = bp_disabled;
12897 throw;
12898 }
12899
12900 exception = std::move (e);
12901 }
12902
12903 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
12904 {
12905 for (auto &sal : sals)
12906 resolve_sal_pc (&sal);
12907 if (condition_not_parsed && extra_string != NULL)
12908 {
12909 gdb::unique_xmalloc_ptr<char> local_cond, local_extra;
12910 int local_thread, local_task;
12911
12912 find_condition_and_thread_for_sals (sals, extra_string.get (),
12913 &local_cond, &local_thread,
12914 &local_task, &local_extra);
12915 gdb_assert (cond_string == nullptr);
12916 if (local_cond != nullptr)
12917 cond_string = std::move (local_cond);
12918 thread = local_thread;
12919 task = local_task;
12920 if (local_extra != nullptr)
12921 extra_string = std::move (local_extra);
12922 condition_not_parsed = 0;
12923 }
12924
12925 if (type == bp_static_tracepoint)
12926 sals[0] = update_static_tracepoint (this, sals[0]);
12927
12928 *found = 1;
12929 }
12930 else
12931 *found = 0;
12932
12933 return sals;
12934 }
12935
12936 /* The default re_set method, for typical hardware or software
12937 breakpoints. Reevaluate the breakpoint and recreate its
12938 locations. */
12939
12940 void
12941 code_breakpoint::re_set_default ()
12942 {
12943 struct program_space *filter_pspace = current_program_space;
12944 std::vector<symtab_and_line> expanded, expanded_end;
12945
12946 int found;
12947 std::vector<symtab_and_line> sals = location_spec_to_sals (locspec.get (),
12948 filter_pspace,
12949 &found);
12950 if (found)
12951 expanded = std::move (sals);
12952
12953 if (locspec_range_end != nullptr)
12954 {
12955 std::vector<symtab_and_line> sals_end
12956 = location_spec_to_sals (locspec_range_end.get (),
12957 filter_pspace, &found);
12958 if (found)
12959 expanded_end = std::move (sals_end);
12960 }
12961
12962 update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
12963 }
12964
12965 /* Default method for creating SALs from an address string. It basically
12966 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12967
12968 static void
12969 create_sals_from_location_spec_default (location_spec *locspec,
12970 struct linespec_result *canonical)
12971 {
12972 parse_breakpoint_sals (locspec, canonical);
12973 }
12974
12975 /* Reset a breakpoint. */
12976
12977 static void
12978 breakpoint_re_set_one (breakpoint *b)
12979 {
12980 input_radix = b->input_radix;
12981 set_language (b->language);
12982
12983 b->re_set ();
12984 }
12985
12986 /* Re-set breakpoint locations for the current program space.
12987 Locations bound to other program spaces are left untouched. */
12988
12989 void
12990 breakpoint_re_set (void)
12991 {
12992 {
12993 scoped_restore_current_language save_language;
12994 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
12995 scoped_restore_current_pspace_and_thread restore_pspace_thread;
12996
12997 /* breakpoint_re_set_one sets the current_language to the language
12998 of the breakpoint it is resetting (see prepare_re_set_context)
12999 before re-evaluating the breakpoint's location. This change can
13000 unfortunately get undone by accident if the language_mode is set
13001 to auto, and we either switch frames, or more likely in this context,
13002 we select the current frame.
13003
13004 We prevent this by temporarily turning the language_mode to
13005 language_mode_manual. We restore it once all breakpoints
13006 have been reset. */
13007 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13008 language_mode = language_mode_manual;
13009
13010 /* Note: we must not try to insert locations until after all
13011 breakpoints have been re-set. Otherwise, e.g., when re-setting
13012 breakpoint 1, we'd insert the locations of breakpoint 2, which
13013 hadn't been re-set yet, and thus may have stale locations. */
13014
13015 for (breakpoint *b : all_breakpoints_safe ())
13016 {
13017 try
13018 {
13019 breakpoint_re_set_one (b);
13020 }
13021 catch (const gdb_exception &ex)
13022 {
13023 exception_fprintf (gdb_stderr, ex,
13024 "Error in re-setting breakpoint %d: ",
13025 b->number);
13026 }
13027 }
13028
13029 jit_breakpoint_re_set ();
13030 }
13031
13032 create_overlay_event_breakpoint ();
13033 create_longjmp_master_breakpoint ();
13034 create_std_terminate_master_breakpoint ();
13035 create_exception_master_breakpoint ();
13036
13037 /* Now we can insert. */
13038 update_global_location_list (UGLL_MAY_INSERT);
13039 }
13040 \f
13041 /* Reset the thread number of this breakpoint:
13042
13043 - If the breakpoint is for all threads, leave it as-is.
13044 - Else, reset it to the current thread for inferior_ptid. */
13045 void
13046 breakpoint_re_set_thread (struct breakpoint *b)
13047 {
13048 if (b->thread != -1)
13049 {
13050 b->thread = inferior_thread ()->global_num;
13051
13052 /* We're being called after following a fork. The new fork is
13053 selected as current, and unless this was a vfork will have a
13054 different program space from the original thread. Reset that
13055 as well. */
13056 b->loc->pspace = current_program_space;
13057 }
13058 }
13059
13060 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13061 If from_tty is nonzero, it prints a message to that effect,
13062 which ends with a period (no newline). */
13063
13064 void
13065 set_ignore_count (int bptnum, int count, int from_tty)
13066 {
13067 if (count < 0)
13068 count = 0;
13069
13070 for (breakpoint *b : all_breakpoints ())
13071 if (b->number == bptnum)
13072 {
13073 if (is_tracepoint (b))
13074 {
13075 if (from_tty && count != 0)
13076 gdb_printf (_("Ignore count ignored for tracepoint %d."),
13077 bptnum);
13078 return;
13079 }
13080
13081 b->ignore_count = count;
13082 if (from_tty)
13083 {
13084 if (count == 0)
13085 gdb_printf (_("Will stop next time "
13086 "breakpoint %d is reached."),
13087 bptnum);
13088 else if (count == 1)
13089 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
13090 bptnum);
13091 else
13092 gdb_printf (_("Will ignore next %d "
13093 "crossings of breakpoint %d."),
13094 count, bptnum);
13095 }
13096 gdb::observers::breakpoint_modified.notify (b);
13097 return;
13098 }
13099
13100 error (_("No breakpoint number %d."), bptnum);
13101 }
13102
13103 /* Command to set ignore-count of breakpoint N to COUNT. */
13104
13105 static void
13106 ignore_command (const char *args, int from_tty)
13107 {
13108 const char *p = args;
13109 int num;
13110
13111 if (p == 0)
13112 error_no_arg (_("a breakpoint number"));
13113
13114 num = get_number (&p);
13115 if (num == 0)
13116 error (_("bad breakpoint number: '%s'"), args);
13117 if (*p == 0)
13118 error (_("Second argument (specified ignore-count) is missing."));
13119
13120 set_ignore_count (num,
13121 longest_to_int (value_as_long (parse_and_eval (p))),
13122 from_tty);
13123 if (from_tty)
13124 gdb_printf ("\n");
13125 }
13126 \f
13127
13128 /* Call FUNCTION on each of the breakpoints with numbers in the range
13129 defined by BP_NUM_RANGE (an inclusive range). */
13130
13131 static void
13132 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13133 gdb::function_view<void (breakpoint *)> function)
13134 {
13135 if (bp_num_range.first == 0)
13136 {
13137 warning (_("bad breakpoint number at or near '%d'"),
13138 bp_num_range.first);
13139 }
13140 else
13141 {
13142 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13143 {
13144 bool match = false;
13145
13146 for (breakpoint *b : all_breakpoints_safe ())
13147 if (b->number == i)
13148 {
13149 match = true;
13150 function (b);
13151 break;
13152 }
13153 if (!match)
13154 gdb_printf (_("No breakpoint number %d.\n"), i);
13155 }
13156 }
13157 }
13158
13159 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13160 ARGS. */
13161
13162 static void
13163 map_breakpoint_numbers (const char *args,
13164 gdb::function_view<void (breakpoint *)> function)
13165 {
13166 if (args == NULL || *args == '\0')
13167 error_no_arg (_("one or more breakpoint numbers"));
13168
13169 number_or_range_parser parser (args);
13170
13171 while (!parser.finished ())
13172 {
13173 int num = parser.get_number ();
13174 map_breakpoint_number_range (std::make_pair (num, num), function);
13175 }
13176 }
13177
13178 /* Return the breakpoint location structure corresponding to the
13179 BP_NUM and LOC_NUM values. */
13180
13181 static struct bp_location *
13182 find_location_by_number (int bp_num, int loc_num)
13183 {
13184 breakpoint *b = get_breakpoint (bp_num);
13185
13186 if (!b || b->number != bp_num)
13187 error (_("Bad breakpoint number '%d'"), bp_num);
13188
13189 if (loc_num == 0)
13190 error (_("Bad breakpoint location number '%d'"), loc_num);
13191
13192 int n = 0;
13193 for (bp_location *loc : b->locations ())
13194 if (++n == loc_num)
13195 return loc;
13196
13197 error (_("Bad breakpoint location number '%d'"), loc_num);
13198 }
13199
13200 /* Modes of operation for extract_bp_num. */
13201 enum class extract_bp_kind
13202 {
13203 /* Extracting a breakpoint number. */
13204 bp,
13205
13206 /* Extracting a location number. */
13207 loc,
13208 };
13209
13210 /* Extract a breakpoint or location number (as determined by KIND)
13211 from the string starting at START. TRAILER is a character which
13212 can be found after the number. If you don't want a trailer, use
13213 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13214 string. This always returns a positive integer. */
13215
13216 static int
13217 extract_bp_num (extract_bp_kind kind, const char *start,
13218 int trailer, const char **end_out = NULL)
13219 {
13220 const char *end = start;
13221 int num = get_number_trailer (&end, trailer);
13222 if (num < 0)
13223 error (kind == extract_bp_kind::bp
13224 ? _("Negative breakpoint number '%.*s'")
13225 : _("Negative breakpoint location number '%.*s'"),
13226 int (end - start), start);
13227 if (num == 0)
13228 error (kind == extract_bp_kind::bp
13229 ? _("Bad breakpoint number '%.*s'")
13230 : _("Bad breakpoint location number '%.*s'"),
13231 int (end - start), start);
13232
13233 if (end_out != NULL)
13234 *end_out = end;
13235 return num;
13236 }
13237
13238 /* Extract a breakpoint or location range (as determined by KIND) in
13239 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13240 representing the (inclusive) range. The returned pair's elements
13241 are always positive integers. */
13242
13243 static std::pair<int, int>
13244 extract_bp_or_bp_range (extract_bp_kind kind,
13245 const std::string &arg,
13246 std::string::size_type arg_offset)
13247 {
13248 std::pair<int, int> range;
13249 const char *bp_loc = &arg[arg_offset];
13250 std::string::size_type dash = arg.find ('-', arg_offset);
13251 if (dash != std::string::npos)
13252 {
13253 /* bp_loc is a range (x-z). */
13254 if (arg.length () == dash + 1)
13255 error (kind == extract_bp_kind::bp
13256 ? _("Bad breakpoint number at or near: '%s'")
13257 : _("Bad breakpoint location number at or near: '%s'"),
13258 bp_loc);
13259
13260 const char *end;
13261 const char *start_first = bp_loc;
13262 const char *start_second = &arg[dash + 1];
13263 range.first = extract_bp_num (kind, start_first, '-');
13264 range.second = extract_bp_num (kind, start_second, '\0', &end);
13265
13266 if (range.first > range.second)
13267 error (kind == extract_bp_kind::bp
13268 ? _("Inverted breakpoint range at '%.*s'")
13269 : _("Inverted breakpoint location range at '%.*s'"),
13270 int (end - start_first), start_first);
13271 }
13272 else
13273 {
13274 /* bp_loc is a single value. */
13275 range.first = extract_bp_num (kind, bp_loc, '\0');
13276 range.second = range.first;
13277 }
13278 return range;
13279 }
13280
13281 /* Extract the breakpoint/location range specified by ARG. Returns
13282 the breakpoint range in BP_NUM_RANGE, and the location range in
13283 BP_LOC_RANGE.
13284
13285 ARG may be in any of the following forms:
13286
13287 x where 'x' is a breakpoint number.
13288 x-y where 'x' and 'y' specify a breakpoint numbers range.
13289 x.y where 'x' is a breakpoint number and 'y' a location number.
13290 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13291 location number range.
13292 */
13293
13294 static void
13295 extract_bp_number_and_location (const std::string &arg,
13296 std::pair<int, int> &bp_num_range,
13297 std::pair<int, int> &bp_loc_range)
13298 {
13299 std::string::size_type dot = arg.find ('.');
13300
13301 if (dot != std::string::npos)
13302 {
13303 /* Handle 'x.y' and 'x.y-z' cases. */
13304
13305 if (arg.length () == dot + 1 || dot == 0)
13306 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
13307
13308 bp_num_range.first
13309 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13310 bp_num_range.second = bp_num_range.first;
13311
13312 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13313 arg, dot + 1);
13314 }
13315 else
13316 {
13317 /* Handle x and x-y cases. */
13318
13319 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
13320 bp_loc_range.first = 0;
13321 bp_loc_range.second = 0;
13322 }
13323 }
13324
13325 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13326 specifies whether to enable or disable. */
13327
13328 static void
13329 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13330 {
13331 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13332 if (loc != NULL)
13333 {
13334 if (loc->disabled_by_cond && enable)
13335 error (_("Breakpoint %d's condition is invalid at location %d, "
13336 "cannot enable."), bp_num, loc_num);
13337
13338 if (loc->enabled != enable)
13339 {
13340 loc->enabled = enable;
13341 mark_breakpoint_location_modified (loc);
13342 }
13343 if (target_supports_enable_disable_tracepoint ()
13344 && current_trace_status ()->running && loc->owner
13345 && is_tracepoint (loc->owner))
13346 target_disable_tracepoint (loc);
13347 }
13348 update_global_location_list (UGLL_DONT_INSERT);
13349
13350 gdb::observers::breakpoint_modified.notify (loc->owner);
13351 }
13352
13353 /* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
13354 owner. 1-based indexing. -1 signals NOT FOUND. */
13355
13356 static int
13357 find_loc_num_by_location (const bp_location *loc)
13358 {
13359 if (loc != nullptr && loc->owner != nullptr)
13360 {
13361 /* Locations use 1-based indexing. */
13362 int loc_num = 1;
13363 for (bp_location *it : loc->owner->locations ())
13364 {
13365 if (it == loc)
13366 return loc_num;
13367 loc_num++;
13368 }
13369 }
13370 return -1;
13371 }
13372
13373 /* Enable or disable a breakpoint location LOC. ENABLE
13374 specifies whether to enable or disable. */
13375
13376 void
13377 enable_disable_bp_location (bp_location *loc, bool enable)
13378 {
13379 if (loc == nullptr)
13380 error (_("Breakpoint location is invalid."));
13381
13382 if (loc->owner == nullptr)
13383 error (_("Breakpoint location does not have an owner breakpoint."));
13384
13385 if (loc->disabled_by_cond && enable)
13386 {
13387 int loc_num = find_loc_num_by_location (loc);
13388 if (loc_num == -1)
13389 error (_("Breakpoint location LOC_NUM could not be found."));
13390 else
13391 error (_("Breakpoint %d's condition is invalid at location %d, "
13392 "cannot enable."), loc->owner->number, loc_num);
13393 }
13394
13395 if (loc->enabled != enable)
13396 {
13397 loc->enabled = enable;
13398 mark_breakpoint_location_modified (loc);
13399 }
13400
13401 if (target_supports_enable_disable_tracepoint ()
13402 && current_trace_status ()->running && loc->owner
13403 && is_tracepoint (loc->owner))
13404 target_disable_tracepoint (loc);
13405
13406 update_global_location_list (UGLL_DONT_INSERT);
13407 gdb::observers::breakpoint_modified.notify (loc->owner);
13408 }
13409
13410 /* Enable or disable a range of breakpoint locations. BP_NUM is the
13411 number of the breakpoint, and BP_LOC_RANGE specifies the
13412 (inclusive) range of location numbers of that breakpoint to
13413 enable/disable. ENABLE specifies whether to enable or disable the
13414 location. */
13415
13416 static void
13417 enable_disable_breakpoint_location_range (int bp_num,
13418 std::pair<int, int> &bp_loc_range,
13419 bool enable)
13420 {
13421 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13422 enable_disable_bp_num_loc (bp_num, i, enable);
13423 }
13424
13425 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13426 If from_tty is nonzero, it prints a message to that effect,
13427 which ends with a period (no newline). */
13428
13429 void
13430 disable_breakpoint (struct breakpoint *bpt)
13431 {
13432 /* Never disable a watchpoint scope breakpoint; we want to
13433 hit them when we leave scope so we can delete both the
13434 watchpoint and its scope breakpoint at that time. */
13435 if (bpt->type == bp_watchpoint_scope)
13436 return;
13437
13438 bpt->enable_state = bp_disabled;
13439
13440 /* Mark breakpoint locations modified. */
13441 mark_breakpoint_modified (bpt);
13442
13443 if (target_supports_enable_disable_tracepoint ()
13444 && current_trace_status ()->running && is_tracepoint (bpt))
13445 {
13446 for (bp_location *location : bpt->locations ())
13447 target_disable_tracepoint (location);
13448 }
13449
13450 update_global_location_list (UGLL_DONT_INSERT);
13451
13452 gdb::observers::breakpoint_modified.notify (bpt);
13453 }
13454
13455 /* Enable or disable the breakpoint(s) or breakpoint location(s)
13456 specified in ARGS. ARGS may be in any of the formats handled by
13457 extract_bp_number_and_location. ENABLE specifies whether to enable
13458 or disable the breakpoints/locations. */
13459
13460 static void
13461 enable_disable_command (const char *args, int from_tty, bool enable)
13462 {
13463 if (args == 0)
13464 {
13465 for (breakpoint *bpt : all_breakpoints ())
13466 if (user_breakpoint_p (bpt))
13467 {
13468 if (enable)
13469 enable_breakpoint (bpt);
13470 else
13471 disable_breakpoint (bpt);
13472 }
13473 }
13474 else
13475 {
13476 std::string num = extract_arg (&args);
13477
13478 while (!num.empty ())
13479 {
13480 std::pair<int, int> bp_num_range, bp_loc_range;
13481
13482 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13483
13484 if (bp_loc_range.first == bp_loc_range.second
13485 && (bp_loc_range.first == 0
13486 || (bp_loc_range.first == 1
13487 && bp_num_range.first == bp_num_range.second
13488 && !has_multiple_locations (bp_num_range.first))))
13489 {
13490 /* Handle breakpoint ids with formats 'x' or 'x-z'
13491 or 'y.1' where y has only one code location. */
13492 map_breakpoint_number_range (bp_num_range,
13493 enable
13494 ? enable_breakpoint
13495 : disable_breakpoint);
13496 }
13497 else
13498 {
13499 /* Handle breakpoint ids with formats 'x.y' or
13500 'x.y-z'. */
13501 enable_disable_breakpoint_location_range
13502 (bp_num_range.first, bp_loc_range, enable);
13503 }
13504 num = extract_arg (&args);
13505 }
13506 }
13507 }
13508
13509 /* The disable command disables the specified breakpoints/locations
13510 (or all defined breakpoints) so they're no longer effective in
13511 stopping the inferior. ARGS may be in any of the forms defined in
13512 extract_bp_number_and_location. */
13513
13514 static void
13515 disable_command (const char *args, int from_tty)
13516 {
13517 enable_disable_command (args, from_tty, false);
13518 }
13519
13520 static void
13521 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13522 int count)
13523 {
13524 int target_resources_ok;
13525
13526 if (bpt->type == bp_hardware_breakpoint)
13527 {
13528 int i;
13529 i = hw_breakpoint_used_count ();
13530 target_resources_ok =
13531 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13532 i + 1, 0);
13533 if (target_resources_ok == 0)
13534 error (_("No hardware breakpoint support in the target."));
13535 else if (target_resources_ok < 0)
13536 error (_("Hardware breakpoints used exceeds limit."));
13537 }
13538
13539 if (is_watchpoint (bpt))
13540 {
13541 /* Initialize it just to avoid a GCC false warning. */
13542 enum enable_state orig_enable_state = bp_disabled;
13543
13544 try
13545 {
13546 struct watchpoint *w = (struct watchpoint *) bpt;
13547
13548 orig_enable_state = bpt->enable_state;
13549 bpt->enable_state = bp_enabled;
13550 update_watchpoint (w, true /* reparse */);
13551 }
13552 catch (const gdb_exception_error &e)
13553 {
13554 bpt->enable_state = orig_enable_state;
13555 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13556 bpt->number);
13557 return;
13558 }
13559 }
13560
13561 bpt->enable_state = bp_enabled;
13562
13563 /* Mark breakpoint locations modified. */
13564 mark_breakpoint_modified (bpt);
13565
13566 if (target_supports_enable_disable_tracepoint ()
13567 && current_trace_status ()->running && is_tracepoint (bpt))
13568 {
13569 for (bp_location *location : bpt->locations ())
13570 target_enable_tracepoint (location);
13571 }
13572
13573 bpt->disposition = disposition;
13574 bpt->enable_count = count;
13575 update_global_location_list (UGLL_MAY_INSERT);
13576
13577 gdb::observers::breakpoint_modified.notify (bpt);
13578 }
13579
13580
13581 void
13582 enable_breakpoint (struct breakpoint *bpt)
13583 {
13584 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13585 }
13586
13587 /* The enable command enables the specified breakpoints/locations (or
13588 all defined breakpoints) so they once again become (or continue to
13589 be) effective in stopping the inferior. ARGS may be in any of the
13590 forms defined in extract_bp_number_and_location. */
13591
13592 static void
13593 enable_command (const char *args, int from_tty)
13594 {
13595 enable_disable_command (args, from_tty, true);
13596 }
13597
13598 static void
13599 enable_once_command (const char *args, int from_tty)
13600 {
13601 map_breakpoint_numbers
13602 (args, [&] (breakpoint *b)
13603 {
13604 iterate_over_related_breakpoints
13605 (b, [&] (breakpoint *bpt)
13606 {
13607 enable_breakpoint_disp (bpt, disp_disable, 1);
13608 });
13609 });
13610 }
13611
13612 static void
13613 enable_count_command (const char *args, int from_tty)
13614 {
13615 int count;
13616
13617 if (args == NULL)
13618 error_no_arg (_("hit count"));
13619
13620 count = get_number (&args);
13621
13622 map_breakpoint_numbers
13623 (args, [&] (breakpoint *b)
13624 {
13625 iterate_over_related_breakpoints
13626 (b, [&] (breakpoint *bpt)
13627 {
13628 enable_breakpoint_disp (bpt, disp_disable, count);
13629 });
13630 });
13631 }
13632
13633 static void
13634 enable_delete_command (const char *args, int from_tty)
13635 {
13636 map_breakpoint_numbers
13637 (args, [&] (breakpoint *b)
13638 {
13639 iterate_over_related_breakpoints
13640 (b, [&] (breakpoint *bpt)
13641 {
13642 enable_breakpoint_disp (bpt, disp_del, 1);
13643 });
13644 });
13645 }
13646 \f
13647 /* Invalidate last known value of any hardware watchpoint if
13648 the memory which that value represents has been written to by
13649 GDB itself. */
13650
13651 static void
13652 invalidate_bp_value_on_memory_change (struct inferior *inferior,
13653 CORE_ADDR addr, ssize_t len,
13654 const bfd_byte *data)
13655 {
13656 for (breakpoint *bp : all_breakpoints ())
13657 if (bp->enable_state == bp_enabled
13658 && bp->type == bp_hardware_watchpoint)
13659 {
13660 struct watchpoint *wp = (struct watchpoint *) bp;
13661
13662 if (wp->val_valid && wp->val != nullptr)
13663 {
13664 for (bp_location *loc : bp->locations ())
13665 if (loc->loc_type == bp_loc_hardware_watchpoint
13666 && loc->address + loc->length > addr
13667 && addr + len > loc->address)
13668 {
13669 wp->val = NULL;
13670 wp->val_valid = false;
13671 }
13672 }
13673 }
13674 }
13675
13676 /* Create and insert a breakpoint for software single step. */
13677
13678 void
13679 insert_single_step_breakpoint (struct gdbarch *gdbarch,
13680 const address_space *aspace,
13681 CORE_ADDR next_pc)
13682 {
13683 struct thread_info *tp = inferior_thread ();
13684 struct symtab_and_line sal;
13685 CORE_ADDR pc = next_pc;
13686
13687 if (tp->control.single_step_breakpoints == NULL)
13688 {
13689 std::unique_ptr<breakpoint> b
13690 (new momentary_breakpoint (gdbarch, bp_single_step,
13691 current_program_space,
13692 null_frame_id,
13693 tp->global_num));
13694
13695 tp->control.single_step_breakpoints
13696 = add_to_breakpoint_chain (std::move (b));
13697 }
13698
13699 sal = find_pc_line (pc, 0);
13700 sal.pc = pc;
13701 sal.section = find_pc_overlay (pc);
13702 sal.explicit_pc = 1;
13703
13704 auto *ss_bp
13705 = (gdb::checked_static_cast<momentary_breakpoint *>
13706 (tp->control.single_step_breakpoints));
13707 ss_bp->add_location (sal);
13708
13709 update_global_location_list (UGLL_INSERT);
13710 }
13711
13712 /* Insert single step breakpoints according to the current state. */
13713
13714 int
13715 insert_single_step_breakpoints (struct gdbarch *gdbarch)
13716 {
13717 struct regcache *regcache = get_current_regcache ();
13718 std::vector<CORE_ADDR> next_pcs;
13719
13720 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
13721
13722 if (!next_pcs.empty ())
13723 {
13724 frame_info_ptr frame = get_current_frame ();
13725 const address_space *aspace = get_frame_address_space (frame);
13726
13727 for (CORE_ADDR pc : next_pcs)
13728 insert_single_step_breakpoint (gdbarch, aspace, pc);
13729
13730 return 1;
13731 }
13732 else
13733 return 0;
13734 }
13735
13736 /* See breakpoint.h. */
13737
13738 int
13739 breakpoint_has_location_inserted_here (struct breakpoint *bp,
13740 const address_space *aspace,
13741 CORE_ADDR pc)
13742 {
13743 for (bp_location *loc : bp->locations ())
13744 if (loc->inserted
13745 && breakpoint_location_address_match (loc, aspace, pc))
13746 return 1;
13747
13748 return 0;
13749 }
13750
13751 /* Check whether a software single-step breakpoint is inserted at
13752 PC. */
13753
13754 int
13755 single_step_breakpoint_inserted_here_p (const address_space *aspace,
13756 CORE_ADDR pc)
13757 {
13758 for (breakpoint *bpt : all_breakpoints ())
13759 {
13760 if (bpt->type == bp_single_step
13761 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13762 return 1;
13763 }
13764 return 0;
13765 }
13766
13767 /* Tracepoint-specific operations. */
13768
13769 /* Set tracepoint count to NUM. */
13770 static void
13771 set_tracepoint_count (int num)
13772 {
13773 tracepoint_count = num;
13774 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
13775 }
13776
13777 static void
13778 trace_command (const char *arg, int from_tty)
13779 {
13780 location_spec_up locspec = string_to_location_spec (&arg,
13781 current_language);
13782 const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
13783 (locspec.get (), true /* is_tracepoint */);
13784
13785 create_breakpoint (get_current_arch (),
13786 locspec.get (),
13787 NULL, 0, arg, false, 1 /* parse arg */,
13788 0 /* tempflag */,
13789 bp_tracepoint /* type_wanted */,
13790 0 /* Ignore count */,
13791 pending_break_support,
13792 ops,
13793 from_tty,
13794 1 /* enabled */,
13795 0 /* internal */, 0);
13796 }
13797
13798 static void
13799 ftrace_command (const char *arg, int from_tty)
13800 {
13801 location_spec_up locspec = string_to_location_spec (&arg,
13802 current_language);
13803 create_breakpoint (get_current_arch (),
13804 locspec.get (),
13805 NULL, 0, arg, false, 1 /* parse arg */,
13806 0 /* tempflag */,
13807 bp_fast_tracepoint /* type_wanted */,
13808 0 /* Ignore count */,
13809 pending_break_support,
13810 &code_breakpoint_ops,
13811 from_tty,
13812 1 /* enabled */,
13813 0 /* internal */, 0);
13814 }
13815
13816 /* strace command implementation. Creates a static tracepoint. */
13817
13818 static void
13819 strace_command (const char *arg, int from_tty)
13820 {
13821 const struct breakpoint_ops *ops;
13822 location_spec_up locspec;
13823 enum bptype type;
13824
13825 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13826 or with a normal static tracepoint. */
13827 if (arg && startswith (arg, "-m") && isspace (arg[2]))
13828 {
13829 ops = &strace_marker_breakpoint_ops;
13830 locspec = new_linespec_location_spec (&arg,
13831 symbol_name_match_type::FULL);
13832 type = bp_static_marker_tracepoint;
13833 }
13834 else
13835 {
13836 ops = &code_breakpoint_ops;
13837 locspec = string_to_location_spec (&arg, current_language);
13838 type = bp_static_tracepoint;
13839 }
13840
13841 create_breakpoint (get_current_arch (),
13842 locspec.get (),
13843 NULL, 0, arg, false, 1 /* parse arg */,
13844 0 /* tempflag */,
13845 type /* type_wanted */,
13846 0 /* Ignore count */,
13847 pending_break_support,
13848 ops,
13849 from_tty,
13850 1 /* enabled */,
13851 0 /* internal */, 0);
13852 }
13853
13854 /* Set up a fake reader function that gets command lines from a linked
13855 list that was acquired during tracepoint uploading. */
13856
13857 static struct uploaded_tp *this_utp;
13858 static int next_cmd;
13859
13860 static const char *
13861 read_uploaded_action (std::string &buffer)
13862 {
13863 char *rslt = nullptr;
13864
13865 if (next_cmd < this_utp->cmd_strings.size ())
13866 {
13867 rslt = this_utp->cmd_strings[next_cmd].get ();
13868 next_cmd++;
13869 }
13870
13871 return rslt;
13872 }
13873
13874 /* Given information about a tracepoint as recorded on a target (which
13875 can be either a live system or a trace file), attempt to create an
13876 equivalent GDB tracepoint. This is not a reliable process, since
13877 the target does not necessarily have all the information used when
13878 the tracepoint was originally defined. */
13879
13880 struct tracepoint *
13881 create_tracepoint_from_upload (struct uploaded_tp *utp)
13882 {
13883 const char *addr_str;
13884 char small_buf[100];
13885 struct tracepoint *tp;
13886
13887 if (utp->at_string)
13888 addr_str = utp->at_string.get ();
13889 else
13890 {
13891 /* In the absence of a source location, fall back to raw
13892 address. Since there is no way to confirm that the address
13893 means the same thing as when the trace was started, warn the
13894 user. */
13895 warning (_("Uploaded tracepoint %d has no "
13896 "source location, using raw address"),
13897 utp->number);
13898 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
13899 addr_str = small_buf;
13900 }
13901
13902 /* There's not much we can do with a sequence of bytecodes. */
13903 if (utp->cond && !utp->cond_string)
13904 warning (_("Uploaded tracepoint %d condition "
13905 "has no source form, ignoring it"),
13906 utp->number);
13907
13908 location_spec_up locspec = string_to_location_spec (&addr_str,
13909 current_language);
13910 if (!create_breakpoint (get_current_arch (),
13911 locspec.get (),
13912 utp->cond_string.get (), -1, addr_str,
13913 false /* force_condition */,
13914 0 /* parse cond/thread */,
13915 0 /* tempflag */,
13916 utp->type /* type_wanted */,
13917 0 /* Ignore count */,
13918 pending_break_support,
13919 &code_breakpoint_ops,
13920 0 /* from_tty */,
13921 utp->enabled /* enabled */,
13922 0 /* internal */,
13923 CREATE_BREAKPOINT_FLAGS_INSERTED))
13924 return NULL;
13925
13926 /* Get the tracepoint we just created. */
13927 tp = get_tracepoint (tracepoint_count);
13928 gdb_assert (tp != NULL);
13929
13930 if (utp->pass > 0)
13931 {
13932 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
13933 tp->number);
13934
13935 trace_pass_command (small_buf, 0);
13936 }
13937
13938 /* If we have uploaded versions of the original commands, set up a
13939 special-purpose "reader" function and call the usual command line
13940 reader, then pass the result to the breakpoint command-setting
13941 function. */
13942 if (!utp->cmd_strings.empty ())
13943 {
13944 counted_command_line cmd_list;
13945
13946 this_utp = utp;
13947 next_cmd = 0;
13948
13949 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
13950
13951 breakpoint_set_commands (tp, std::move (cmd_list));
13952 }
13953 else if (!utp->actions.empty ()
13954 || !utp->step_actions.empty ())
13955 warning (_("Uploaded tracepoint %d actions "
13956 "have no source form, ignoring them"),
13957 utp->number);
13958
13959 /* Copy any status information that might be available. */
13960 tp->hit_count = utp->hit_count;
13961 tp->traceframe_usage = utp->traceframe_usage;
13962
13963 return tp;
13964 }
13965
13966 /* Print information on tracepoint number TPNUM_EXP, or all if
13967 omitted. */
13968
13969 static void
13970 info_tracepoints_command (const char *args, int from_tty)
13971 {
13972 struct ui_out *uiout = current_uiout;
13973 int num_printed;
13974
13975 num_printed = breakpoint_1 (args, false, is_tracepoint);
13976
13977 if (num_printed == 0)
13978 {
13979 if (args == NULL || *args == '\0')
13980 uiout->message ("No tracepoints.\n");
13981 else
13982 uiout->message ("No tracepoint matching '%s'.\n", args);
13983 }
13984
13985 default_collect_info ();
13986 }
13987
13988 /* The 'enable trace' command enables tracepoints.
13989 Not supported by all targets. */
13990 static void
13991 enable_trace_command (const char *args, int from_tty)
13992 {
13993 enable_command (args, from_tty);
13994 }
13995
13996 /* The 'disable trace' command disables tracepoints.
13997 Not supported by all targets. */
13998 static void
13999 disable_trace_command (const char *args, int from_tty)
14000 {
14001 disable_command (args, from_tty);
14002 }
14003
14004 /* Remove a tracepoint (or all if no argument). */
14005 static void
14006 delete_trace_command (const char *arg, int from_tty)
14007 {
14008 dont_repeat ();
14009
14010 if (arg == 0)
14011 {
14012 int breaks_to_delete = 0;
14013
14014 /* Delete all breakpoints if no argument.
14015 Do not delete internal or call-dummy breakpoints, these
14016 have to be deleted with an explicit breakpoint number
14017 argument. */
14018 for (breakpoint *tp : all_tracepoints ())
14019 if (is_tracepoint (tp) && user_breakpoint_p (tp))
14020 {
14021 breaks_to_delete = 1;
14022 break;
14023 }
14024
14025 /* Ask user only if there are some breakpoints to delete. */
14026 if (!from_tty
14027 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14028 {
14029 for (breakpoint *b : all_breakpoints_safe ())
14030 if (is_tracepoint (b) && user_breakpoint_p (b))
14031 delete_breakpoint (b);
14032 }
14033 }
14034 else
14035 map_breakpoint_numbers
14036 (arg, [&] (breakpoint *br)
14037 {
14038 iterate_over_related_breakpoints (br, delete_breakpoint);
14039 });
14040 }
14041
14042 /* Helper function for trace_pass_command. */
14043
14044 static void
14045 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14046 {
14047 tp->pass_count = count;
14048 gdb::observers::breakpoint_modified.notify (tp);
14049 if (from_tty)
14050 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
14051 tp->number, count);
14052 }
14053
14054 /* Set passcount for tracepoint.
14055
14056 First command argument is passcount, second is tracepoint number.
14057 If tracepoint number omitted, apply to most recently defined.
14058 Also accepts special argument "all". */
14059
14060 static void
14061 trace_pass_command (const char *args, int from_tty)
14062 {
14063 struct tracepoint *t1;
14064 ULONGEST count;
14065
14066 if (args == 0 || *args == 0)
14067 error (_("passcount command requires an "
14068 "argument (count + optional TP num)"));
14069
14070 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14071
14072 args = skip_spaces (args);
14073 if (*args && strncasecmp (args, "all", 3) == 0)
14074 {
14075 args += 3; /* Skip special argument "all". */
14076 if (*args)
14077 error (_("Junk at end of arguments."));
14078
14079 for (breakpoint *b : all_tracepoints ())
14080 {
14081 t1 = (struct tracepoint *) b;
14082 trace_pass_set_count (t1, count, from_tty);
14083 }
14084 }
14085 else if (*args == '\0')
14086 {
14087 t1 = get_tracepoint_by_number (&args, NULL);
14088 if (t1)
14089 trace_pass_set_count (t1, count, from_tty);
14090 }
14091 else
14092 {
14093 number_or_range_parser parser (args);
14094 while (!parser.finished ())
14095 {
14096 t1 = get_tracepoint_by_number (&args, &parser);
14097 if (t1)
14098 trace_pass_set_count (t1, count, from_tty);
14099 }
14100 }
14101 }
14102
14103 struct tracepoint *
14104 get_tracepoint (int num)
14105 {
14106 for (breakpoint *t : all_tracepoints ())
14107 if (t->number == num)
14108 return (struct tracepoint *) t;
14109
14110 return NULL;
14111 }
14112
14113 /* Find the tracepoint with the given target-side number (which may be
14114 different from the tracepoint number after disconnecting and
14115 reconnecting). */
14116
14117 struct tracepoint *
14118 get_tracepoint_by_number_on_target (int num)
14119 {
14120 for (breakpoint *b : all_tracepoints ())
14121 {
14122 struct tracepoint *t = (struct tracepoint *) b;
14123
14124 if (t->number_on_target == num)
14125 return t;
14126 }
14127
14128 return NULL;
14129 }
14130
14131 /* Utility: parse a tracepoint number and look it up in the list.
14132 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14133 If the argument is missing, the most recent tracepoint
14134 (tracepoint_count) is returned. */
14135
14136 struct tracepoint *
14137 get_tracepoint_by_number (const char **arg,
14138 number_or_range_parser *parser)
14139 {
14140 int tpnum;
14141 const char *instring = arg == NULL ? NULL : *arg;
14142
14143 if (parser != NULL)
14144 {
14145 gdb_assert (!parser->finished ());
14146 tpnum = parser->get_number ();
14147 }
14148 else if (arg == NULL || *arg == NULL || ! **arg)
14149 tpnum = tracepoint_count;
14150 else
14151 tpnum = get_number (arg);
14152
14153 if (tpnum <= 0)
14154 {
14155 if (instring && *instring)
14156 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14157 instring);
14158 else
14159 gdb_printf (_("No previous tracepoint\n"));
14160 return NULL;
14161 }
14162
14163 for (breakpoint *t : all_tracepoints ())
14164 if (t->number == tpnum)
14165 return (struct tracepoint *) t;
14166
14167 gdb_printf ("No tracepoint number %d.\n", tpnum);
14168 return NULL;
14169 }
14170
14171 void
14172 breakpoint::print_recreate_thread (struct ui_file *fp) const
14173 {
14174 if (thread != -1)
14175 {
14176 struct thread_info *thr = find_thread_global_id (thread);
14177 gdb_printf (fp, " thread %s", print_full_thread_id (thr));
14178 }
14179
14180 if (task != -1)
14181 gdb_printf (fp, " task %d", task);
14182
14183 gdb_printf (fp, "\n");
14184 }
14185
14186 /* Save information on user settable breakpoints (watchpoints, etc) to
14187 a new script file named FILENAME. If FILTER is non-NULL, call it
14188 on each breakpoint and only include the ones for which it returns
14189 true. */
14190
14191 static void
14192 save_breakpoints (const char *filename, int from_tty,
14193 bool (*filter) (const struct breakpoint *))
14194 {
14195 bool any = false;
14196 int extra_trace_bits = 0;
14197
14198 if (filename == 0 || *filename == 0)
14199 error (_("Argument required (file name in which to save)"));
14200
14201 /* See if we have anything to save. */
14202 for (breakpoint *tp : all_breakpoints ())
14203 {
14204 /* Skip internal and momentary breakpoints. */
14205 if (!user_breakpoint_p (tp))
14206 continue;
14207
14208 /* If we have a filter, only save the breakpoints it accepts. */
14209 if (filter && !filter (tp))
14210 continue;
14211
14212 any = true;
14213
14214 if (is_tracepoint (tp))
14215 {
14216 extra_trace_bits = 1;
14217
14218 /* We can stop searching. */
14219 break;
14220 }
14221 }
14222
14223 if (!any)
14224 {
14225 warning (_("Nothing to save."));
14226 return;
14227 }
14228
14229 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14230
14231 stdio_file fp;
14232
14233 if (!fp.open (expanded_filename.get (), "w"))
14234 error (_("Unable to open file '%s' for saving (%s)"),
14235 expanded_filename.get (), safe_strerror (errno));
14236
14237 if (extra_trace_bits)
14238 save_trace_state_variables (&fp);
14239
14240 for (breakpoint *tp : all_breakpoints ())
14241 {
14242 /* Skip internal and momentary breakpoints. */
14243 if (!user_breakpoint_p (tp))
14244 continue;
14245
14246 /* If we have a filter, only save the breakpoints it accepts. */
14247 if (filter && !filter (tp))
14248 continue;
14249
14250 tp->print_recreate (&fp);
14251
14252 /* Note, we can't rely on tp->number for anything, as we can't
14253 assume the recreated breakpoint numbers will match. Use $bpnum
14254 instead. */
14255
14256 if (tp->cond_string)
14257 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
14258
14259 if (tp->ignore_count)
14260 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
14261
14262 if (tp->type != bp_dprintf && tp->commands)
14263 {
14264 fp.puts (" commands\n");
14265
14266 ui_out_redirect_pop redir (current_uiout, &fp);
14267 print_command_lines (current_uiout, tp->commands.get (), 2);
14268
14269 fp.puts (" end\n");
14270 }
14271
14272 if (tp->enable_state == bp_disabled)
14273 fp.puts ("disable $bpnum\n");
14274
14275 /* If this is a multi-location breakpoint, check if the locations
14276 should be individually disabled. Watchpoint locations are
14277 special, and not user visible. */
14278 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14279 {
14280 int n = 1;
14281
14282 for (bp_location *loc : tp->locations ())
14283 {
14284 if (!loc->enabled)
14285 fp.printf ("disable $bpnum.%d\n", n);
14286
14287 n++;
14288 }
14289 }
14290 }
14291
14292 if (extra_trace_bits && !default_collect.empty ())
14293 fp.printf ("set default-collect %s\n", default_collect.c_str ());
14294
14295 if (from_tty)
14296 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
14297 }
14298
14299 /* The `save breakpoints' command. */
14300
14301 static void
14302 save_breakpoints_command (const char *args, int from_tty)
14303 {
14304 save_breakpoints (args, from_tty, NULL);
14305 }
14306
14307 /* The `save tracepoints' command. */
14308
14309 static void
14310 save_tracepoints_command (const char *args, int from_tty)
14311 {
14312 save_breakpoints (args, from_tty, is_tracepoint);
14313 }
14314
14315 \f
14316 /* This help string is used to consolidate all the help string for specifying
14317 locations used by several commands. */
14318
14319 #define LOCATION_SPEC_HELP_STRING \
14320 "Linespecs are colon-separated lists of location parameters, such as\n\
14321 source filename, function name, label name, and line number.\n\
14322 Example: To specify the start of a label named \"the_top\" in the\n\
14323 function \"fact\" in the file \"factorial.c\", use\n\
14324 \"factorial.c:fact:the_top\".\n\
14325 \n\
14326 Address locations begin with \"*\" and specify an exact address in the\n\
14327 program. Example: To specify the fourth byte past the start function\n\
14328 \"main\", use \"*main + 4\".\n\
14329 \n\
14330 Explicit locations are similar to linespecs but use an option/argument\n\
14331 syntax to specify location parameters.\n\
14332 Example: To specify the start of the label named \"the_top\" in the\n\
14333 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
14334 -function fact -label the_top\".\n\
14335 \n\
14336 By default, a specified function is matched against the program's\n\
14337 functions in all scopes. For C++, this means in all namespaces and\n\
14338 classes. For Ada, this means in all packages. E.g., in C++,\n\
14339 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14340 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
14341 specified name as a complete fully-qualified name instead."
14342
14343 /* This help string is used for the break, hbreak, tbreak and thbreak
14344 commands. It is defined as a macro to prevent duplication.
14345 COMMAND should be a string constant containing the name of the
14346 command. */
14347
14348 #define BREAK_ARGS_HELP(command) \
14349 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14350 \t[-force-condition] [if CONDITION]\n\
14351 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14352 probe point. Accepted values are `-probe' (for a generic, automatically\n\
14353 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14354 `-probe-dtrace' (for a DTrace probe).\n\
14355 LOCATION may be a linespec, address, or explicit location as described\n\
14356 below.\n\
14357 \n\
14358 With no LOCATION, uses current execution address of the selected\n\
14359 stack frame. This is useful for breaking on return to a stack frame.\n\
14360 \n\
14361 THREADNUM is the number from \"info threads\".\n\
14362 CONDITION is a boolean expression.\n\
14363 \n\
14364 With the \"-force-condition\" flag, the condition is defined even when\n\
14365 it is invalid for all current locations.\n\
14366 \n" LOCATION_SPEC_HELP_STRING "\n\n\
14367 Multiple breakpoints at one place are permitted, and useful if their\n\
14368 conditions are different.\n\
14369 \n\
14370 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14371
14372 /* List of subcommands for "catch". */
14373 static struct cmd_list_element *catch_cmdlist;
14374
14375 /* List of subcommands for "tcatch". */
14376 static struct cmd_list_element *tcatch_cmdlist;
14377
14378 void
14379 add_catch_command (const char *name, const char *docstring,
14380 cmd_func_ftype *func,
14381 completer_ftype *completer,
14382 void *user_data_catch,
14383 void *user_data_tcatch)
14384 {
14385 struct cmd_list_element *command;
14386
14387 command = add_cmd (name, class_breakpoint, docstring,
14388 &catch_cmdlist);
14389 command->func = func;
14390 command->set_context (user_data_catch);
14391 set_cmd_completer (command, completer);
14392
14393 command = add_cmd (name, class_breakpoint, docstring,
14394 &tcatch_cmdlist);
14395 command->func = func;
14396 command->set_context (user_data_tcatch);
14397 set_cmd_completer (command, completer);
14398 }
14399
14400 /* False if any of the breakpoint's locations could be a location where
14401 functions have been inlined, true otherwise. */
14402
14403 static bool
14404 is_non_inline_function (struct breakpoint *b)
14405 {
14406 /* The shared library event breakpoint is set on the address of a
14407 non-inline function. */
14408 return (b->type == bp_shlib_event);
14409 }
14410
14411 /* Nonzero if the specified PC cannot be a location where functions
14412 have been inlined. */
14413
14414 int
14415 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
14416 const target_waitstatus &ws)
14417 {
14418 for (breakpoint *b : all_breakpoints ())
14419 {
14420 if (!is_non_inline_function (b))
14421 continue;
14422
14423 for (bp_location *bl : b->locations ())
14424 {
14425 if (!bl->shlib_disabled
14426 && bpstat_check_location (bl, aspace, pc, ws))
14427 return 1;
14428 }
14429 }
14430
14431 return 0;
14432 }
14433
14434 /* Remove any references to OBJFILE which is going to be freed. */
14435
14436 void
14437 breakpoint_free_objfile (struct objfile *objfile)
14438 {
14439 for (bp_location *loc : all_bp_locations ())
14440 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
14441 loc->symtab = NULL;
14442 }
14443
14444 /* Chain containing all defined "enable breakpoint" subcommands. */
14445
14446 static struct cmd_list_element *enablebreaklist = NULL;
14447
14448 /* See breakpoint.h. */
14449
14450 cmd_list_element *commands_cmd_element = nullptr;
14451
14452 void _initialize_breakpoint ();
14453 void
14454 _initialize_breakpoint ()
14455 {
14456 struct cmd_list_element *c;
14457
14458 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14459 "breakpoint");
14460 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14461 "breakpoint");
14462 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14463 "breakpoint");
14464
14465 breakpoint_chain = 0;
14466 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14467 before a breakpoint is set. */
14468 breakpoint_count = 0;
14469
14470 tracepoint_count = 0;
14471
14472 add_com ("ignore", class_breakpoint, ignore_command, _("\
14473 Set ignore-count of breakpoint number N to COUNT.\n\
14474 Usage is `ignore N COUNT'."));
14475
14476 commands_cmd_element = add_com ("commands", class_breakpoint,
14477 commands_command, _("\
14478 Set commands to be executed when the given breakpoints are hit.\n\
14479 Give a space-separated breakpoint list as argument after \"commands\".\n\
14480 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14481 (e.g. `5-7').\n\
14482 With no argument, the targeted breakpoint is the last one set.\n\
14483 The commands themselves follow starting on the next line.\n\
14484 Type a line containing \"end\" to indicate the end of them.\n\
14485 Give \"silent\" as the first line to make the breakpoint silent;\n\
14486 then no output is printed when it is hit, except what the commands print."));
14487
14488 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14489 static std::string condition_command_help
14490 = gdb::option::build_help (_("\
14491 Specify breakpoint number N to break only if COND is true.\n\
14492 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
14493 is an expression to be evaluated whenever breakpoint N is reached.\n\
14494 \n\
14495 Options:\n\
14496 %OPTIONS%"), cc_opts);
14497
14498 c = add_com ("condition", class_breakpoint, condition_command,
14499 condition_command_help.c_str ());
14500 set_cmd_completer_handle_brkchars (c, condition_completer);
14501
14502 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
14503 Set a temporary breakpoint.\n\
14504 Like \"break\" except the breakpoint is only temporary,\n\
14505 so it will be deleted when hit. Equivalent to \"break\" followed\n\
14506 by using \"enable delete\" on the breakpoint number.\n\
14507 \n"
14508 BREAK_ARGS_HELP ("tbreak")));
14509 set_cmd_completer (c, location_completer);
14510
14511 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
14512 Set a hardware assisted breakpoint.\n\
14513 Like \"break\" except the breakpoint requires hardware support,\n\
14514 some target hardware may not have this support.\n\
14515 \n"
14516 BREAK_ARGS_HELP ("hbreak")));
14517 set_cmd_completer (c, location_completer);
14518
14519 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
14520 Set a temporary hardware assisted breakpoint.\n\
14521 Like \"hbreak\" except the breakpoint is only temporary,\n\
14522 so it will be deleted when hit.\n\
14523 \n"
14524 BREAK_ARGS_HELP ("thbreak")));
14525 set_cmd_completer (c, location_completer);
14526
14527 cmd_list_element *enable_cmd
14528 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14529 Enable all or some breakpoints.\n\
14530 Usage: enable [BREAKPOINTNUM]...\n\
14531 Give breakpoint numbers (separated by spaces) as arguments.\n\
14532 With no subcommand, breakpoints are enabled until you command otherwise.\n\
14533 This is used to cancel the effect of the \"disable\" command.\n\
14534 With a subcommand you can enable temporarily."),
14535 &enablelist, 1, &cmdlist);
14536
14537 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
14538
14539 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
14540 Enable all or some breakpoints.\n\
14541 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
14542 Give breakpoint numbers (separated by spaces) as arguments.\n\
14543 This is used to cancel the effect of the \"disable\" command.\n\
14544 May be abbreviated to simply \"enable\"."),
14545 &enablebreaklist, 1, &enablelist);
14546
14547 add_cmd ("once", no_class, enable_once_command, _("\
14548 Enable some breakpoints for one hit.\n\
14549 Usage: enable breakpoints once BREAKPOINTNUM...\n\
14550 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14551 &enablebreaklist);
14552
14553 add_cmd ("delete", no_class, enable_delete_command, _("\
14554 Enable some breakpoints and delete when hit.\n\
14555 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
14556 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14557 &enablebreaklist);
14558
14559 add_cmd ("count", no_class, enable_count_command, _("\
14560 Enable some breakpoints for COUNT hits.\n\
14561 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
14562 If a breakpoint is hit while enabled in this fashion,\n\
14563 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14564 &enablebreaklist);
14565
14566 add_cmd ("delete", no_class, enable_delete_command, _("\
14567 Enable some breakpoints and delete when hit.\n\
14568 Usage: enable delete BREAKPOINTNUM...\n\
14569 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14570 &enablelist);
14571
14572 add_cmd ("once", no_class, enable_once_command, _("\
14573 Enable some breakpoints for one hit.\n\
14574 Usage: enable once BREAKPOINTNUM...\n\
14575 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14576 &enablelist);
14577
14578 add_cmd ("count", no_class, enable_count_command, _("\
14579 Enable some breakpoints for COUNT hits.\n\
14580 Usage: enable count COUNT BREAKPOINTNUM...\n\
14581 If a breakpoint is hit while enabled in this fashion,\n\
14582 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14583 &enablelist);
14584
14585 cmd_list_element *disable_cmd
14586 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14587 Disable all or some breakpoints.\n\
14588 Usage: disable [BREAKPOINTNUM]...\n\
14589 Arguments are breakpoint numbers with spaces in between.\n\
14590 To disable all breakpoints, give no argument.\n\
14591 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
14592 &disablelist, 1, &cmdlist);
14593 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14594 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
14595
14596 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
14597 Disable all or some breakpoints.\n\
14598 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
14599 Arguments are breakpoint numbers with spaces in between.\n\
14600 To disable all breakpoints, give no argument.\n\
14601 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
14602 This command may be abbreviated \"disable\"."),
14603 &disablelist);
14604
14605 cmd_list_element *delete_cmd
14606 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14607 Delete all or some breakpoints.\n\
14608 Usage: delete [BREAKPOINTNUM]...\n\
14609 Arguments are breakpoint numbers with spaces in between.\n\
14610 To delete all breakpoints, give no argument.\n\
14611 \n\
14612 Also a prefix command for deletion of other GDB objects."),
14613 &deletelist, 1, &cmdlist);
14614 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14615 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
14616
14617 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
14618 Delete all or some breakpoints or auto-display expressions.\n\
14619 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
14620 Arguments are breakpoint numbers with spaces in between.\n\
14621 To delete all breakpoints, give no argument.\n\
14622 This command may be abbreviated \"delete\"."),
14623 &deletelist);
14624
14625 cmd_list_element *clear_cmd
14626 = add_com ("clear", class_breakpoint, clear_command, _("\
14627 Clear breakpoint at specified location.\n\
14628 Argument may be a linespec, explicit, or address location as described below.\n\
14629 \n\
14630 With no argument, clears all breakpoints in the line that the selected frame\n\
14631 is executing in.\n"
14632 "\n" LOCATION_SPEC_HELP_STRING "\n\n\
14633 See also the \"delete\" command which clears breakpoints by number."));
14634 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
14635
14636 cmd_list_element *break_cmd
14637 = add_com ("break", class_breakpoint, break_command, _("\
14638 Set breakpoint at specified location.\n"
14639 BREAK_ARGS_HELP ("break")));
14640 set_cmd_completer (break_cmd, location_completer);
14641
14642 add_com_alias ("b", break_cmd, class_run, 1);
14643 add_com_alias ("br", break_cmd, class_run, 1);
14644 add_com_alias ("bre", break_cmd, class_run, 1);
14645 add_com_alias ("brea", break_cmd, class_run, 1);
14646
14647 cmd_list_element *info_breakpoints_cmd
14648 = add_info ("breakpoints", info_breakpoints_command, _("\
14649 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
14650 The \"Type\" column indicates one of:\n\
14651 \tbreakpoint - normal breakpoint\n\
14652 \twatchpoint - watchpoint\n\
14653 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14654 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14655 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14656 address and file/line number respectively.\n\
14657 \n\
14658 Convenience variable \"$_\" and default examine address for \"x\"\n\
14659 are set to the address of the last breakpoint listed unless the command\n\
14660 is prefixed with \"server \".\n\n\
14661 Convenience variable \"$bpnum\" contains the number of the last\n\
14662 breakpoint set."));
14663
14664 add_info_alias ("b", info_breakpoints_cmd, 1);
14665
14666 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14667 Status of all breakpoints, or breakpoint number NUMBER.\n\
14668 The \"Type\" column indicates one of:\n\
14669 \tbreakpoint - normal breakpoint\n\
14670 \twatchpoint - watchpoint\n\
14671 \tlongjmp - internal breakpoint used to step through longjmp()\n\
14672 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14673 \tuntil - internal breakpoint used by the \"until\" command\n\
14674 \tfinish - internal breakpoint used by the \"finish\" command\n\
14675 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14676 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14677 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14678 address and file/line number respectively.\n\
14679 \n\
14680 Convenience variable \"$_\" and default examine address for \"x\"\n\
14681 are set to the address of the last breakpoint listed unless the command\n\
14682 is prefixed with \"server \".\n\n\
14683 Convenience variable \"$bpnum\" contains the number of the last\n\
14684 breakpoint set."),
14685 &maintenanceinfolist);
14686
14687 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
14688 Set catchpoints to catch events."),
14689 &catch_cmdlist,
14690 0/*allow-unknown*/, &cmdlist);
14691
14692 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
14693 Set temporary catchpoints to catch events."),
14694 &tcatch_cmdlist,
14695 0/*allow-unknown*/, &cmdlist);
14696
14697 const auto opts = make_watch_options_def_group (nullptr);
14698
14699 static const std::string watch_help = gdb::option::build_help (_("\
14700 Set a watchpoint for EXPRESSION.\n\
14701 Usage: watch [-location] EXPRESSION\n\
14702 \n\
14703 Options:\n\
14704 %OPTIONS%\n\
14705 \n\
14706 A watchpoint stops execution of your program whenever the value of\n\
14707 an expression changes."), opts);
14708 c = add_com ("watch", class_breakpoint, watch_command,
14709 watch_help.c_str ());
14710 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14711
14712 static const std::string rwatch_help = gdb::option::build_help (_("\
14713 Set a read watchpoint for EXPRESSION.\n\
14714 Usage: rwatch [-location] EXPRESSION\n\
14715 \n\
14716 Options:\n\
14717 %OPTIONS%\n\
14718 \n\
14719 A read watchpoint stops execution of your program whenever the value of\n\
14720 an expression is read."), opts);
14721 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14722 rwatch_help.c_str ());
14723 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14724
14725 static const std::string awatch_help = gdb::option::build_help (_("\
14726 Set an access watchpoint for EXPRESSION.\n\
14727 Usage: awatch [-location] EXPRESSION\n\
14728 \n\
14729 Options:\n\
14730 %OPTIONS%\n\
14731 \n\
14732 An access watchpoint stops execution of your program whenever the value\n\
14733 of an expression is either read or written."), opts);
14734 c = add_com ("awatch", class_breakpoint, awatch_command,
14735 awatch_help.c_str ());
14736 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14737
14738 add_info ("watchpoints", info_watchpoints_command, _("\
14739 Status of specified watchpoints (all watchpoints if no argument)."));
14740
14741 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14742 respond to changes - contrary to the description. */
14743 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14744 &can_use_hw_watchpoints, _("\
14745 Set debugger's willingness to use watchpoint hardware."), _("\
14746 Show debugger's willingness to use watchpoint hardware."), _("\
14747 If zero, gdb will not use hardware for new watchpoints, even if\n\
14748 such is available. (However, any hardware watchpoints that were\n\
14749 created before setting this to nonzero, will continue to use watchpoint\n\
14750 hardware.)"),
14751 NULL,
14752 show_can_use_hw_watchpoints,
14753 &setlist, &showlist);
14754
14755 can_use_hw_watchpoints = 1;
14756
14757 /* Tracepoint manipulation commands. */
14758
14759 cmd_list_element *trace_cmd
14760 = add_com ("trace", class_breakpoint, trace_command, _("\
14761 Set a tracepoint at specified location.\n\
14762 \n"
14763 BREAK_ARGS_HELP ("trace") "\n\
14764 Do \"help tracepoints\" for info on other tracepoint commands."));
14765 set_cmd_completer (trace_cmd, location_completer);
14766
14767 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14768 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14769 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14770 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
14771
14772 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
14773 Set a fast tracepoint at specified location.\n\
14774 \n"
14775 BREAK_ARGS_HELP ("ftrace") "\n\
14776 Do \"help tracepoints\" for info on other tracepoint commands."));
14777 set_cmd_completer (c, location_completer);
14778
14779 c = add_com ("strace", class_breakpoint, strace_command, _("\
14780 Set a static tracepoint at location or marker.\n\
14781 \n\
14782 strace [LOCATION] [if CONDITION]\n\
14783 LOCATION may be a linespec, explicit, or address location (described below) \n\
14784 or -m MARKER_ID.\n\n\
14785 If a marker id is specified, probe the marker with that name. With\n\
14786 no LOCATION, uses current execution address of the selected stack frame.\n\
14787 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14788 This collects arbitrary user data passed in the probe point call to the\n\
14789 tracing library. You can inspect it when analyzing the trace buffer,\n\
14790 by printing the $_sdata variable like any other convenience variable.\n\
14791 \n\
14792 CONDITION is a boolean expression.\n\
14793 \n" LOCATION_SPEC_HELP_STRING "\n\n\
14794 Multiple tracepoints at one place are permitted, and useful if their\n\
14795 conditions are different.\n\
14796 \n\
14797 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14798 Do \"help tracepoints\" for info on other tracepoint commands."));
14799 set_cmd_completer (c, location_completer);
14800
14801 cmd_list_element *info_tracepoints_cmd
14802 = add_info ("tracepoints", info_tracepoints_command, _("\
14803 Status of specified tracepoints (all tracepoints if no argument).\n\
14804 Convenience variable \"$tpnum\" contains the number of the\n\
14805 last tracepoint set."));
14806
14807 add_info_alias ("tp", info_tracepoints_cmd, 1);
14808
14809 cmd_list_element *delete_tracepoints_cmd
14810 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
14811 Delete specified tracepoints.\n\
14812 Arguments are tracepoint numbers, separated by spaces.\n\
14813 No argument means delete all tracepoints."),
14814 &deletelist);
14815 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
14816
14817 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14818 Disable specified tracepoints.\n\
14819 Arguments are tracepoint numbers, separated by spaces.\n\
14820 No argument means disable all tracepoints."),
14821 &disablelist);
14822 deprecate_cmd (c, "disable");
14823
14824 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14825 Enable specified tracepoints.\n\
14826 Arguments are tracepoint numbers, separated by spaces.\n\
14827 No argument means enable all tracepoints."),
14828 &enablelist);
14829 deprecate_cmd (c, "enable");
14830
14831 add_com ("passcount", class_trace, trace_pass_command, _("\
14832 Set the passcount for a tracepoint.\n\
14833 The trace will end when the tracepoint has been passed 'count' times.\n\
14834 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14835 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14836
14837 add_basic_prefix_cmd ("save", class_breakpoint,
14838 _("Save breakpoint definitions as a script."),
14839 &save_cmdlist,
14840 0/*allow-unknown*/, &cmdlist);
14841
14842 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14843 Save current breakpoint definitions as a script.\n\
14844 This includes all types of breakpoints (breakpoints, watchpoints,\n\
14845 catchpoints, tracepoints). Use the 'source' command in another debug\n\
14846 session to restore them."),
14847 &save_cmdlist);
14848 set_cmd_completer (c, filename_completer);
14849
14850 cmd_list_element *save_tracepoints_cmd
14851 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
14852 Save current tracepoint definitions as a script.\n\
14853 Use the 'source' command in another debug session to restore them."),
14854 &save_cmdlist);
14855 set_cmd_completer (save_tracepoints_cmd, filename_completer);
14856
14857 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
14858 deprecate_cmd (c, "save tracepoints");
14859
14860 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14861 _("\
14862 Breakpoint specific settings.\n\
14863 Configure various breakpoint-specific variables such as\n\
14864 pending breakpoint behavior."),
14865 _("\
14866 Breakpoint specific settings.\n\
14867 Configure various breakpoint-specific variables such as\n\
14868 pending breakpoint behavior."),
14869 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14870 &setlist, &showlist);
14871
14872 add_setshow_auto_boolean_cmd ("pending", no_class,
14873 &pending_break_support, _("\
14874 Set debugger's behavior regarding pending breakpoints."), _("\
14875 Show debugger's behavior regarding pending breakpoints."), _("\
14876 If on, an unrecognized breakpoint location will cause gdb to create a\n\
14877 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14878 an error. If auto, an unrecognized breakpoint location results in a\n\
14879 user-query to see if a pending breakpoint should be created."),
14880 NULL,
14881 show_pending_break_support,
14882 &breakpoint_set_cmdlist,
14883 &breakpoint_show_cmdlist);
14884
14885 pending_break_support = AUTO_BOOLEAN_AUTO;
14886
14887 add_setshow_boolean_cmd ("auto-hw", no_class,
14888 &automatic_hardware_breakpoints, _("\
14889 Set automatic usage of hardware breakpoints."), _("\
14890 Show automatic usage of hardware breakpoints."), _("\
14891 If set, the debugger will automatically use hardware breakpoints for\n\
14892 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14893 a warning will be emitted for such breakpoints."),
14894 NULL,
14895 show_automatic_hardware_breakpoints,
14896 &breakpoint_set_cmdlist,
14897 &breakpoint_show_cmdlist);
14898
14899 add_setshow_boolean_cmd ("always-inserted", class_support,
14900 &always_inserted_mode, _("\
14901 Set mode for inserting breakpoints."), _("\
14902 Show mode for inserting breakpoints."), _("\
14903 When this mode is on, breakpoints are inserted immediately as soon as\n\
14904 they're created, kept inserted even when execution stops, and removed\n\
14905 only when the user deletes them. When this mode is off (the default),\n\
14906 breakpoints are inserted only when execution continues, and removed\n\
14907 when execution stops."),
14908 NULL,
14909 &show_always_inserted_mode,
14910 &breakpoint_set_cmdlist,
14911 &breakpoint_show_cmdlist);
14912
14913 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14914 condition_evaluation_enums,
14915 &condition_evaluation_mode_1, _("\
14916 Set mode of breakpoint condition evaluation."), _("\
14917 Show mode of breakpoint condition evaluation."), _("\
14918 When this is set to \"host\", breakpoint conditions will be\n\
14919 evaluated on the host's side by GDB. When it is set to \"target\",\n\
14920 breakpoint conditions will be downloaded to the target (if the target\n\
14921 supports such feature) and conditions will be evaluated on the target's side.\n\
14922 If this is set to \"auto\" (default), this will be automatically set to\n\
14923 \"target\" if it supports condition evaluation, otherwise it will\n\
14924 be set to \"host\"."),
14925 &set_condition_evaluation_mode,
14926 &show_condition_evaluation_mode,
14927 &breakpoint_set_cmdlist,
14928 &breakpoint_show_cmdlist);
14929
14930 add_com ("break-range", class_breakpoint, break_range_command, _("\
14931 Set a breakpoint for an address range.\n\
14932 break-range START-LOCATION, END-LOCATION\n\
14933 where START-LOCATION and END-LOCATION can be one of the following:\n\
14934 LINENUM, for that line in the current file,\n\
14935 FILE:LINENUM, for that line in that file,\n\
14936 +OFFSET, for that number of lines after the current line\n\
14937 or the start of the range\n\
14938 FUNCTION, for the first line in that function,\n\
14939 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14940 *ADDRESS, for the instruction at that address.\n\
14941 \n\
14942 The breakpoint will stop execution of the inferior whenever it executes\n\
14943 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14944 range (including START-LOCATION and END-LOCATION)."));
14945
14946 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
14947 Set a dynamic printf at specified location.\n\
14948 dprintf location,format string,arg1,arg2,...\n\
14949 location may be a linespec, explicit, or address location.\n"
14950 "\n" LOCATION_SPEC_HELP_STRING));
14951 set_cmd_completer (c, location_completer);
14952
14953 add_setshow_enum_cmd ("dprintf-style", class_support,
14954 dprintf_style_enums, &dprintf_style, _("\
14955 Set the style of usage for dynamic printf."), _("\
14956 Show the style of usage for dynamic printf."), _("\
14957 This setting chooses how GDB will do a dynamic printf.\n\
14958 If the value is \"gdb\", then the printing is done by GDB to its own\n\
14959 console, as with the \"printf\" command.\n\
14960 If the value is \"call\", the print is done by calling a function in your\n\
14961 program; by default printf(), but you can choose a different function or\n\
14962 output stream by setting dprintf-function and dprintf-channel."),
14963 update_dprintf_commands, NULL,
14964 &setlist, &showlist);
14965
14966 add_setshow_string_cmd ("dprintf-function", class_support,
14967 &dprintf_function, _("\
14968 Set the function to use for dynamic printf."), _("\
14969 Show the function to use for dynamic printf."), NULL,
14970 update_dprintf_commands, NULL,
14971 &setlist, &showlist);
14972
14973 add_setshow_string_cmd ("dprintf-channel", class_support,
14974 &dprintf_channel, _("\
14975 Set the channel to use for dynamic printf."), _("\
14976 Show the channel to use for dynamic printf."), NULL,
14977 update_dprintf_commands, NULL,
14978 &setlist, &showlist);
14979
14980 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14981 &disconnected_dprintf, _("\
14982 Set whether dprintf continues after GDB disconnects."), _("\
14983 Show whether dprintf continues after GDB disconnects."), _("\
14984 Use this to let dprintf commands continue to hit and produce output\n\
14985 even if GDB disconnects or detaches from the target."),
14986 NULL,
14987 NULL,
14988 &setlist, &showlist);
14989
14990 add_com ("agent-printf", class_vars, agent_printf_command, _("\
14991 Target agent only formatted printing, like the C \"printf\" function.\n\
14992 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14993 This supports most C printf format specifications, like %s, %d, etc.\n\
14994 This is useful for formatted output in user-defined commands."));
14995
14996 automatic_hardware_breakpoints = true;
14997
14998 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14999 "breakpoint");
15000 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
15001 "breakpoint");
15002 }