gdb: fix use-after-free in check_longjmp_breakpoint_for_call_dummy
[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 }
1848
1849 /* Extract a bitfield value from value VAL using the bit parameters contained in
1850 watchpoint W. */
1851
1852 static struct value *
1853 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1854 {
1855 struct value *bit_val;
1856
1857 if (val == NULL)
1858 return NULL;
1859
1860 bit_val = value::allocate (val->type ());
1861
1862 val->unpack_bitfield (bit_val,
1863 w->val_bitpos,
1864 w->val_bitsize,
1865 val->contents_for_printing ().data (),
1866 val->offset ());
1867
1868 return bit_val;
1869 }
1870
1871 /* Allocate a dummy location and add it to B. This is required
1872 because bpstat_stop_status requires a location to be able to report
1873 stops. */
1874
1875 static void
1876 add_dummy_location (struct breakpoint *b,
1877 struct program_space *pspace)
1878 {
1879 gdb_assert (b->loc == NULL);
1880
1881 b->loc = new bp_location (b, bp_loc_other);
1882 b->loc->pspace = pspace;
1883 }
1884
1885 /* Assuming that B is a watchpoint:
1886 - Reparse watchpoint expression, if REPARSE is true
1887 - Evaluate expression and store the result in B->val
1888 - Evaluate the condition if there is one, and store the result
1889 in b->loc->cond.
1890 - Update the list of values that must be watched in B->loc.
1891
1892 If the watchpoint disposition is disp_del_at_next_stop, then do
1893 nothing. If this is local watchpoint that is out of scope, delete
1894 it.
1895
1896 Even with `set breakpoint always-inserted on' the watchpoints are
1897 removed + inserted on each stop here. Normal breakpoints must
1898 never be removed because they might be missed by a running thread
1899 when debugging in non-stop mode. On the other hand, hardware
1900 watchpoints (is_hardware_watchpoint; processed here) are specific
1901 to each LWP since they are stored in each LWP's hardware debug
1902 registers. Therefore, such LWP must be stopped first in order to
1903 be able to modify its hardware watchpoints.
1904
1905 Hardware watchpoints must be reset exactly once after being
1906 presented to the user. It cannot be done sooner, because it would
1907 reset the data used to present the watchpoint hit to the user. And
1908 it must not be done later because it could display the same single
1909 watchpoint hit during multiple GDB stops. Note that the latter is
1910 relevant only to the hardware watchpoint types bp_read_watchpoint
1911 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1912 not user-visible - its hit is suppressed if the memory content has
1913 not changed.
1914
1915 The following constraints influence the location where we can reset
1916 hardware watchpoints:
1917
1918 * target_stopped_by_watchpoint and target_stopped_data_address are
1919 called several times when GDB stops.
1920
1921 [linux]
1922 * Multiple hardware watchpoints can be hit at the same time,
1923 causing GDB to stop. GDB only presents one hardware watchpoint
1924 hit at a time as the reason for stopping, and all the other hits
1925 are presented later, one after the other, each time the user
1926 requests the execution to be resumed. Execution is not resumed
1927 for the threads still having pending hit event stored in
1928 LWP_INFO->STATUS. While the watchpoint is already removed from
1929 the inferior on the first stop the thread hit event is kept being
1930 reported from its cached value by linux_nat_stopped_data_address
1931 until the real thread resume happens after the watchpoint gets
1932 presented and thus its LWP_INFO->STATUS gets reset.
1933
1934 Therefore the hardware watchpoint hit can get safely reset on the
1935 watchpoint removal from inferior. */
1936
1937 static void
1938 update_watchpoint (struct watchpoint *b, bool reparse)
1939 {
1940 bool within_current_scope;
1941
1942 /* If this is a local watchpoint, we only want to check if the
1943 watchpoint frame is in scope if the current thread is the thread
1944 that was used to create the watchpoint. */
1945 if (!watchpoint_in_thread_scope (b))
1946 return;
1947
1948 if (b->disposition == disp_del_at_next_stop)
1949 return;
1950
1951 gdb::optional<scoped_restore_selected_frame> restore_frame;
1952
1953 /* Determine if the watchpoint is within scope. */
1954 if (b->exp_valid_block == NULL)
1955 within_current_scope = true;
1956 else
1957 {
1958 frame_info_ptr fi = get_current_frame ();
1959 struct gdbarch *frame_arch = get_frame_arch (fi);
1960 CORE_ADDR frame_pc = get_frame_pc (fi);
1961
1962 /* If we're at a point where the stack has been destroyed
1963 (e.g. in a function epilogue), unwinding may not work
1964 properly. Do not attempt to recreate locations at this
1965 point. See similar comments in watchpoint_check. */
1966 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1967 return;
1968
1969 /* Save the current frame's ID so we can restore it after
1970 evaluating the watchpoint expression on its own frame. */
1971 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1972 took a frame parameter, so that we didn't have to change the
1973 selected frame. */
1974 restore_frame.emplace ();
1975
1976 fi = frame_find_by_id (b->watchpoint_frame);
1977 within_current_scope = (fi != NULL);
1978 if (within_current_scope)
1979 select_frame (fi);
1980 }
1981
1982 /* We don't free locations. They are stored in the bp_location array
1983 and update_global_location_list will eventually delete them and
1984 remove breakpoints if needed. */
1985 b->loc = NULL;
1986
1987 if (within_current_scope && reparse)
1988 {
1989 const char *s;
1990
1991 b->exp.reset ();
1992 s = (b->exp_string_reparse
1993 ? b->exp_string_reparse.get ()
1994 : b->exp_string.get ());
1995 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1996 /* If the meaning of expression itself changed, the old value is
1997 no longer relevant. We don't want to report a watchpoint hit
1998 to the user when the old value and the new value may actually
1999 be completely different objects. */
2000 b->val = NULL;
2001 b->val_valid = false;
2002
2003 /* Note that unlike with breakpoints, the watchpoint's condition
2004 expression is stored in the breakpoint object, not in the
2005 locations (re)created below. */
2006 if (b->cond_string != NULL)
2007 {
2008 b->cond_exp.reset ();
2009
2010 s = b->cond_string.get ();
2011 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
2012 }
2013 }
2014
2015 /* If we failed to parse the expression, for example because
2016 it refers to a global variable in a not-yet-loaded shared library,
2017 don't try to insert watchpoint. We don't automatically delete
2018 such watchpoint, though, since failure to parse expression
2019 is different from out-of-scope watchpoint. */
2020 if (!target_has_execution ())
2021 {
2022 /* Without execution, memory can't change. No use to try and
2023 set watchpoint locations. The watchpoint will be reset when
2024 the target gains execution, through breakpoint_re_set. */
2025 if (!can_use_hw_watchpoints)
2026 {
2027 if (b->works_in_software_mode ())
2028 b->type = bp_watchpoint;
2029 else
2030 error (_("Can't set read/access watchpoint when "
2031 "hardware watchpoints are disabled."));
2032 }
2033 }
2034 else if (within_current_scope && b->exp)
2035 {
2036 std::vector<value_ref_ptr> val_chain;
2037 struct value *v, *result;
2038 struct program_space *frame_pspace;
2039
2040 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
2041 &val_chain, false);
2042
2043 /* Avoid setting b->val if it's already set. The meaning of
2044 b->val is 'the last value' user saw, and we should update
2045 it only if we reported that last value to user. As it
2046 happens, the code that reports it updates b->val directly.
2047 We don't keep track of the memory value for masked
2048 watchpoints. */
2049 if (!b->val_valid && !is_masked_watchpoint (b))
2050 {
2051 if (b->val_bitsize != 0)
2052 v = extract_bitfield_from_watchpoint_value (b, v);
2053 b->val = release_value (v);
2054 b->val_valid = true;
2055 }
2056
2057 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2058
2059 /* Look at each value on the value chain. */
2060 gdb_assert (!val_chain.empty ());
2061 for (const value_ref_ptr &iter : val_chain)
2062 {
2063 v = iter.get ();
2064
2065 /* If it's a memory location, and GDB actually needed
2066 its contents to evaluate the expression, then we
2067 must watch it. If the first value returned is
2068 still lazy, that means an error occurred reading it;
2069 watch it anyway in case it becomes readable. */
2070 if (v->lval () == lval_memory
2071 && (v == val_chain[0] || ! v->lazy ()))
2072 {
2073 struct type *vtype = check_typedef (v->type ());
2074
2075 /* We only watch structs and arrays if user asked
2076 for it explicitly, never if they just happen to
2077 appear in the middle of some value chain. */
2078 if (v == result
2079 || (vtype->code () != TYPE_CODE_STRUCT
2080 && vtype->code () != TYPE_CODE_ARRAY))
2081 {
2082 CORE_ADDR addr;
2083 enum target_hw_bp_type type;
2084 struct bp_location *loc, **tmp;
2085 int bitpos = 0, bitsize = 0;
2086
2087 if (v->bitsize () != 0)
2088 {
2089 /* Extract the bit parameters out from the bitfield
2090 sub-expression. */
2091 bitpos = v->bitpos ();
2092 bitsize = v->bitsize ();
2093 }
2094 else if (v == result && b->val_bitsize != 0)
2095 {
2096 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2097 lvalue whose bit parameters are saved in the fields
2098 VAL_BITPOS and VAL_BITSIZE. */
2099 bitpos = b->val_bitpos;
2100 bitsize = b->val_bitsize;
2101 }
2102
2103 addr = v->address ();
2104 if (bitsize != 0)
2105 {
2106 /* Skip the bytes that don't contain the bitfield. */
2107 addr += bitpos / 8;
2108 }
2109
2110 type = hw_write;
2111 if (b->type == bp_read_watchpoint)
2112 type = hw_read;
2113 else if (b->type == bp_access_watchpoint)
2114 type = hw_access;
2115
2116 loc = b->allocate_location ();
2117 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2118 ;
2119 *tmp = loc;
2120 loc->gdbarch = v->type ()->arch ();
2121
2122 loc->pspace = frame_pspace;
2123 loc->address
2124 = gdbarch_remove_non_address_bits (loc->gdbarch, addr);
2125
2126 if (bitsize != 0)
2127 {
2128 /* Just cover the bytes that make up the bitfield. */
2129 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2130 }
2131 else
2132 loc->length = v->type ()->length ();
2133
2134 loc->watchpoint_type = type;
2135 }
2136 }
2137 }
2138
2139 /* Helper function to bundle possibly emitting a warning along with
2140 changing the type of B to bp_watchpoint. */
2141 auto change_type_to_bp_watchpoint = [] (breakpoint *bp)
2142 {
2143 /* Only warn for breakpoints that have been assigned a +ve number,
2144 anything else is either an internal watchpoint (which we don't
2145 currently create) or has not yet been finalized, in which case
2146 this change of type will be occurring before the user is told
2147 the type of this watchpoint. */
2148 if (bp->type == bp_hardware_watchpoint && bp->number > 0)
2149 warning (_("watchpoint %d downgraded to software watchpoint"),
2150 bp->number);
2151 bp->type = bp_watchpoint;
2152 };
2153
2154 /* Change the type of breakpoint between hardware assisted or
2155 an ordinary watchpoint depending on the hardware support and
2156 free hardware slots. Recheck the number of free hardware slots
2157 as the value chain may have changed. */
2158 {
2159 int reg_cnt;
2160 enum bp_loc_type loc_type;
2161
2162 reg_cnt = can_use_hardware_watchpoint (val_chain);
2163
2164 if (reg_cnt)
2165 {
2166 int i, target_resources_ok, other_type_used;
2167 enum bptype type;
2168
2169 /* Use an exact watchpoint when there's only one memory region to be
2170 watched, and only one debug register is needed to watch it. */
2171 b->exact = target_exact_watchpoints && reg_cnt == 1;
2172
2173 /* We need to determine how many resources are already
2174 used for all other hardware watchpoints plus this one
2175 to see if we still have enough resources to also fit
2176 this watchpoint in as well. */
2177
2178 /* If this is a software watchpoint, we try to turn it
2179 to a hardware one -- count resources as if B was of
2180 hardware watchpoint type. */
2181 type = b->type;
2182 if (type == bp_watchpoint)
2183 type = bp_hardware_watchpoint;
2184
2185 /* This watchpoint may or may not have been placed on
2186 the list yet at this point (it won't be in the list
2187 if we're trying to create it for the first time,
2188 through watch_command), so always account for it
2189 manually. */
2190
2191 /* Count resources used by all watchpoints except B. */
2192 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2193
2194 /* Add in the resources needed for B. */
2195 i += hw_watchpoint_use_count (b);
2196
2197 target_resources_ok
2198 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2199 if (target_resources_ok <= 0)
2200 {
2201 bool sw_mode = b->works_in_software_mode ();
2202
2203 if (target_resources_ok == 0 && !sw_mode)
2204 error (_("Target does not support this type of "
2205 "hardware watchpoint."));
2206 else if (target_resources_ok < 0 && !sw_mode)
2207 error (_("There are not enough available hardware "
2208 "resources for this watchpoint."));
2209
2210 /* Downgrade to software watchpoint. */
2211 change_type_to_bp_watchpoint (b);
2212 }
2213 else
2214 {
2215 /* If this was a software watchpoint, we've just
2216 found we have enough resources to turn it to a
2217 hardware watchpoint. Otherwise, this is a
2218 nop. */
2219 b->type = type;
2220 }
2221 }
2222 else if (!b->works_in_software_mode ())
2223 {
2224 if (!can_use_hw_watchpoints)
2225 error (_("Can't set read/access watchpoint when "
2226 "hardware watchpoints are disabled."));
2227 else
2228 error (_("Expression cannot be implemented with "
2229 "read/access watchpoint."));
2230 }
2231 else
2232 change_type_to_bp_watchpoint (b);
2233
2234 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
2235 : bp_loc_hardware_watchpoint);
2236 for (bp_location *bl : b->locations ())
2237 bl->loc_type = loc_type;
2238 }
2239
2240 /* If a software watchpoint is not watching any memory, then the
2241 above left it without any location set up. But,
2242 bpstat_stop_status requires a location to be able to report
2243 stops, so make sure there's at least a dummy one. */
2244 if (b->type == bp_watchpoint && b->loc == NULL)
2245 add_dummy_location (b, frame_pspace);
2246 }
2247 else if (!within_current_scope)
2248 {
2249 gdb_printf (_("\
2250 Watchpoint %d deleted because the program has left the block\n\
2251 in which its expression is valid.\n"),
2252 b->number);
2253 watchpoint_del_at_next_stop (b);
2254 }
2255 }
2256
2257 /* Returns true iff breakpoint location should be
2258 inserted in the inferior. We don't differentiate the type of BL's owner
2259 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2260 breakpoint_ops is not defined, because in insert_bp_location,
2261 tracepoint's insert_location will not be called. */
2262
2263 static bool
2264 should_be_inserted (struct bp_location *bl)
2265 {
2266 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2267 return false;
2268
2269 if (bl->owner->disposition == disp_del_at_next_stop)
2270 return false;
2271
2272 if (!bl->enabled || bl->disabled_by_cond
2273 || bl->shlib_disabled || bl->duplicate)
2274 return false;
2275
2276 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2277 return false;
2278
2279 /* This is set for example, when we're attached to the parent of a
2280 vfork, and have detached from the child. The child is running
2281 free, and we expect it to do an exec or exit, at which point the
2282 OS makes the parent schedulable again (and the target reports
2283 that the vfork is done). Until the child is done with the shared
2284 memory region, do not insert breakpoints in the parent, otherwise
2285 the child could still trip on the parent's breakpoints. Since
2286 the parent is blocked anyway, it won't miss any breakpoint. */
2287 if (bl->pspace->breakpoints_not_allowed)
2288 return false;
2289
2290 /* Don't insert a breakpoint if we're trying to step past its
2291 location, except if the breakpoint is a single-step breakpoint,
2292 and the breakpoint's thread is the thread which is stepping past
2293 a breakpoint. */
2294 if ((bl->loc_type == bp_loc_software_breakpoint
2295 || bl->loc_type == bp_loc_hardware_breakpoint)
2296 && stepping_past_instruction_at (bl->pspace->aspace,
2297 bl->address)
2298 /* The single-step breakpoint may be inserted at the location
2299 we're trying to step if the instruction branches to itself.
2300 However, the instruction won't be executed at all and it may
2301 break the semantics of the instruction, for example, the
2302 instruction is a conditional branch or updates some flags.
2303 We can't fix it unless GDB is able to emulate the instruction
2304 or switch to displaced stepping. */
2305 && !(bl->owner->type == bp_single_step
2306 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2307 {
2308 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2309 paddress (bl->gdbarch, bl->address));
2310 return false;
2311 }
2312
2313 /* Don't insert watchpoints if we're trying to step past the
2314 instruction that triggered one. */
2315 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2316 && stepping_past_nonsteppable_watchpoint ())
2317 {
2318 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2319 "skipping watchpoint at %s:%d",
2320 paddress (bl->gdbarch, bl->address), bl->length);
2321 return false;
2322 }
2323
2324 return true;
2325 }
2326
2327 /* Same as should_be_inserted but does the check assuming
2328 that the location is not duplicated. */
2329
2330 static bool
2331 unduplicated_should_be_inserted (struct bp_location *bl)
2332 {
2333 scoped_restore restore_bl_duplicate
2334 = make_scoped_restore (&bl->duplicate, 0);
2335
2336 return should_be_inserted (bl);
2337 }
2338
2339 /* Parses a conditional described by an expression COND into an
2340 agent expression bytecode suitable for evaluation
2341 by the bytecode interpreter. Return NULL if there was
2342 any error during parsing. */
2343
2344 static agent_expr_up
2345 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2346 {
2347 if (cond == NULL)
2348 return NULL;
2349
2350 agent_expr_up aexpr;
2351
2352 /* We don't want to stop processing, so catch any errors
2353 that may show up. */
2354 try
2355 {
2356 aexpr = gen_eval_for_expr (scope, cond);
2357 }
2358
2359 catch (const gdb_exception_error &ex)
2360 {
2361 /* If we got here, it means the condition could not be parsed to a valid
2362 bytecode expression and thus can't be evaluated on the target's side.
2363 It's no use iterating through the conditions. */
2364 }
2365
2366 /* We have a valid agent expression. */
2367 return aexpr;
2368 }
2369
2370 /* Based on location BL, create a list of breakpoint conditions to be
2371 passed on to the target. If we have duplicated locations with different
2372 conditions, we will add such conditions to the list. The idea is that the
2373 target will evaluate the list of conditions and will only notify GDB when
2374 one of them is true. */
2375
2376 static void
2377 build_target_condition_list (struct bp_location *bl)
2378 {
2379 bool null_condition_or_parse_error = false;
2380 int modified = bl->needs_update;
2381
2382 /* Release conditions left over from a previous insert. */
2383 bl->target_info.conditions.clear ();
2384
2385 /* This is only meaningful if the target is
2386 evaluating conditions and if the user has
2387 opted for condition evaluation on the target's
2388 side. */
2389 if (gdb_evaluates_breakpoint_condition_p ()
2390 || !target_supports_evaluation_of_breakpoint_conditions ())
2391 return;
2392
2393 auto loc_range = all_bp_locations_at_addr (bl->address);
2394
2395 /* Do a first pass to check for locations with no assigned
2396 conditions or conditions that fail to parse to a valid agent
2397 expression bytecode. If any of these happen, then it's no use to
2398 send conditions to the target since this location will always
2399 trigger and generate a response back to GDB. Note we consider
2400 all locations at the same address irrespective of type, i.e.,
2401 even if the locations aren't considered duplicates (e.g.,
2402 software breakpoint and hardware breakpoint at the same
2403 address). */
2404 for (bp_location *loc : loc_range)
2405 {
2406 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2407 {
2408 if (modified)
2409 {
2410 /* Re-parse the conditions since something changed. In that
2411 case we already freed the condition bytecodes (see
2412 force_breakpoint_reinsertion). We just
2413 need to parse the condition to bytecodes again. */
2414 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2415 loc->cond.get ());
2416 }
2417
2418 /* If we have a NULL bytecode expression, it means something
2419 went wrong or we have a null condition expression. */
2420 if (!loc->cond_bytecode)
2421 {
2422 null_condition_or_parse_error = true;
2423 break;
2424 }
2425 }
2426 }
2427
2428 /* If any of these happened, it means we will have to evaluate the conditions
2429 for the location's address on gdb's side. It is no use keeping bytecodes
2430 for all the other duplicate locations, thus we free all of them here.
2431
2432 This is so we have a finer control over which locations' conditions are
2433 being evaluated by GDB or the remote stub. */
2434 if (null_condition_or_parse_error)
2435 {
2436 for (bp_location *loc : loc_range)
2437 {
2438 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2439 {
2440 /* Only go as far as the first NULL bytecode is
2441 located. */
2442 if (!loc->cond_bytecode)
2443 return;
2444
2445 loc->cond_bytecode.reset ();
2446 }
2447 }
2448 }
2449
2450 /* No NULL conditions or failed bytecode generation. Build a
2451 condition list for this location's address. If we have software
2452 and hardware locations at the same address, they aren't
2453 considered duplicates, but we still marge all the conditions
2454 anyway, as it's simpler, and doesn't really make a practical
2455 difference. */
2456 for (bp_location *loc : loc_range)
2457 if (loc->cond
2458 && is_breakpoint (loc->owner)
2459 && loc->pspace->num == bl->pspace->num
2460 && loc->owner->enable_state == bp_enabled
2461 && loc->enabled
2462 && !loc->disabled_by_cond)
2463 {
2464 /* Add the condition to the vector. This will be used later
2465 to send the conditions to the target. */
2466 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2467 }
2468
2469 return;
2470 }
2471
2472 /* Parses a command described by string CMD into an agent expression
2473 bytecode suitable for evaluation by the bytecode interpreter.
2474 Return NULL if there was any error during parsing. */
2475
2476 static agent_expr_up
2477 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2478 {
2479 const char *cmdrest;
2480 const char *format_start, *format_end;
2481 struct gdbarch *gdbarch = get_current_arch ();
2482
2483 if (cmd == NULL)
2484 return NULL;
2485
2486 cmdrest = cmd;
2487
2488 if (*cmdrest == ',')
2489 ++cmdrest;
2490 cmdrest = skip_spaces (cmdrest);
2491
2492 if (*cmdrest++ != '"')
2493 error (_("No format string following the location"));
2494
2495 format_start = cmdrest;
2496
2497 format_pieces fpieces (&cmdrest);
2498
2499 format_end = cmdrest;
2500
2501 if (*cmdrest++ != '"')
2502 error (_("Bad format string, non-terminated '\"'."));
2503
2504 cmdrest = skip_spaces (cmdrest);
2505
2506 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2507 error (_("Invalid argument syntax"));
2508
2509 if (*cmdrest == ',')
2510 cmdrest++;
2511 cmdrest = skip_spaces (cmdrest);
2512
2513 /* For each argument, make an expression. */
2514
2515 std::vector<struct expression *> argvec;
2516 while (*cmdrest != '\0')
2517 {
2518 const char *cmd1;
2519
2520 cmd1 = cmdrest;
2521 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2522 argvec.push_back (expr.release ());
2523 cmdrest = cmd1;
2524 if (*cmdrest == ',')
2525 ++cmdrest;
2526 }
2527
2528 agent_expr_up aexpr;
2529
2530 /* We don't want to stop processing, so catch any errors
2531 that may show up. */
2532 try
2533 {
2534 aexpr = gen_printf (scope, gdbarch, 0, 0,
2535 format_start, format_end - format_start,
2536 argvec.size (), argvec.data ());
2537 }
2538 catch (const gdb_exception_error &ex)
2539 {
2540 /* If we got here, it means the command could not be parsed to a valid
2541 bytecode expression and thus can't be evaluated on the target's side.
2542 It's no use iterating through the other commands. */
2543 }
2544
2545 /* We have a valid agent expression, return it. */
2546 return aexpr;
2547 }
2548
2549 /* Based on location BL, create a list of breakpoint commands to be
2550 passed on to the target. If we have duplicated locations with
2551 different commands, we will add any such to the list. */
2552
2553 static void
2554 build_target_command_list (struct bp_location *bl)
2555 {
2556 bool null_command_or_parse_error = false;
2557 int modified = bl->needs_update;
2558
2559 /* Clear commands left over from a previous insert. */
2560 bl->target_info.tcommands.clear ();
2561
2562 if (!target_can_run_breakpoint_commands ())
2563 return;
2564
2565 /* For now, limit to agent-style dprintf breakpoints. */
2566 if (dprintf_style != dprintf_style_agent)
2567 return;
2568
2569 auto loc_range = all_bp_locations_at_addr (bl->address);
2570
2571 /* For now, if we have any location at the same address that isn't a
2572 dprintf, don't install the target-side commands, as that would
2573 make the breakpoint not be reported to the core, and we'd lose
2574 control. */
2575 for (bp_location *loc : loc_range)
2576 if (is_breakpoint (loc->owner)
2577 && loc->pspace->num == bl->pspace->num
2578 && loc->owner->type != bp_dprintf)
2579 return;
2580
2581 /* Do a first pass to check for locations with no assigned
2582 conditions or conditions that fail to parse to a valid agent expression
2583 bytecode. If any of these happen, then it's no use to send conditions
2584 to the target since this location will always trigger and generate a
2585 response back to GDB. */
2586 for (bp_location *loc : loc_range)
2587 {
2588 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2589 {
2590 if (modified)
2591 {
2592 /* Re-parse the commands since something changed. In that
2593 case we already freed the command bytecodes (see
2594 force_breakpoint_reinsertion). We just
2595 need to parse the command to bytecodes again. */
2596 loc->cmd_bytecode
2597 = parse_cmd_to_aexpr (bl->address,
2598 loc->owner->extra_string.get ());
2599 }
2600
2601 /* If we have a NULL bytecode expression, it means something
2602 went wrong or we have a null command expression. */
2603 if (!loc->cmd_bytecode)
2604 {
2605 null_command_or_parse_error = true;
2606 break;
2607 }
2608 }
2609 }
2610
2611 /* If anything failed, then we're not doing target-side commands,
2612 and so clean up. */
2613 if (null_command_or_parse_error)
2614 {
2615 for (bp_location *loc : loc_range)
2616 if (is_breakpoint (loc->owner)
2617 && loc->pspace->num == bl->pspace->num)
2618 {
2619 /* Only go as far as the first NULL bytecode is
2620 located. */
2621 if (loc->cmd_bytecode == NULL)
2622 return;
2623
2624 loc->cmd_bytecode.reset ();
2625 }
2626 }
2627
2628 /* No NULL commands or failed bytecode generation. Build a command
2629 list for all duplicate locations at this location's address.
2630 Note that here we must care for whether the breakpoint location
2631 types are considered duplicates, otherwise, say, if we have a
2632 software and hardware location at the same address, the target
2633 could end up running the commands twice. For the moment, we only
2634 support targets-side commands with dprintf, but it doesn't hurt
2635 to be pedantically correct in case that changes. */
2636 for (bp_location *loc : loc_range)
2637 if (breakpoint_locations_match (bl, loc)
2638 && loc->owner->extra_string
2639 && is_breakpoint (loc->owner)
2640 && loc->pspace->num == bl->pspace->num
2641 && loc->owner->enable_state == bp_enabled
2642 && loc->enabled
2643 && !loc->disabled_by_cond)
2644 {
2645 /* Add the command to the vector. This will be used later
2646 to send the commands to the target. */
2647 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2648 }
2649
2650 bl->target_info.persist = 0;
2651 /* Maybe flag this location as persistent. */
2652 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2653 bl->target_info.persist = 1;
2654 }
2655
2656 /* Return the kind of breakpoint on address *ADDR. Get the kind
2657 of breakpoint according to ADDR except single-step breakpoint.
2658 Get the kind of single-step breakpoint according to the current
2659 registers state. */
2660
2661 static int
2662 breakpoint_kind (const struct bp_location *bl, CORE_ADDR *addr)
2663 {
2664 if (bl->owner->type == bp_single_step)
2665 {
2666 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2667 struct regcache *regcache;
2668
2669 regcache = get_thread_regcache (thr);
2670
2671 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2672 regcache, addr);
2673 }
2674 else
2675 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2676 }
2677
2678 /* Rethrow the currently handled exception, if it's a TARGET_CLOSE_ERROR.
2679 E is either the currently handled exception, or a copy, or a sliced copy,
2680 so we can't rethrow that one, but we can use it to inspect the properties
2681 of the currently handled exception. */
2682
2683 static void
2684 rethrow_on_target_close_error (const gdb_exception &e)
2685 {
2686 if (e.reason == 0)
2687 return;
2688 /* Can't set the breakpoint. */
2689
2690 if (e.error != TARGET_CLOSE_ERROR)
2691 return;
2692
2693 /* If the target has closed then it will have deleted any breakpoints
2694 inserted within the target inferior, as a result any further attempts
2695 to interact with the breakpoint objects is not possible. Just rethrow
2696 the error. Don't use e to rethrow, to prevent object slicing of the
2697 exception. */
2698 throw;
2699 }
2700
2701 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2702 location. Any error messages are printed to TMP_ERROR_STREAM; and
2703 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2704 Returns 0 for success, 1 if the bp_location type is not supported or
2705 -1 for failure.
2706
2707 NOTE drow/2003-09-09: This routine could be broken down to an
2708 object-style method for each breakpoint or catchpoint type. */
2709 static int
2710 insert_bp_location (struct bp_location *bl,
2711 struct ui_file *tmp_error_stream,
2712 int *disabled_breaks,
2713 int *hw_breakpoint_error,
2714 int *hw_bp_error_explained_already)
2715 {
2716 gdb_exception bp_excpt;
2717
2718 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2719 return 0;
2720
2721 /* Note we don't initialize bl->target_info, as that wipes out
2722 the breakpoint location's shadow_contents if the breakpoint
2723 is still inserted at that location. This in turn breaks
2724 target_read_memory which depends on these buffers when
2725 a memory read is requested at the breakpoint location:
2726 Once the target_info has been wiped, we fail to see that
2727 we have a breakpoint inserted at that address and thus
2728 read the breakpoint instead of returning the data saved in
2729 the breakpoint location's shadow contents. */
2730 bl->target_info.reqstd_address = bl->address;
2731 bl->target_info.placed_address_space = bl->pspace->aspace;
2732 bl->target_info.length = bl->length;
2733
2734 /* When working with target-side conditions, we must pass all the conditions
2735 for the same breakpoint address down to the target since GDB will not
2736 insert those locations. With a list of breakpoint conditions, the target
2737 can decide when to stop and notify GDB. */
2738
2739 if (is_breakpoint (bl->owner))
2740 {
2741 build_target_condition_list (bl);
2742 build_target_command_list (bl);
2743 /* Reset the modification marker. */
2744 bl->needs_update = 0;
2745 }
2746
2747 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2748 set at a read-only address, then a breakpoint location will have
2749 been changed to hardware breakpoint before we get here. If it is
2750 "off" however, error out before actually trying to insert the
2751 breakpoint, with a nicer error message. */
2752 if (bl->loc_type == bp_loc_software_breakpoint
2753 && !automatic_hardware_breakpoints)
2754 {
2755 mem_region *mr = lookup_mem_region (bl->address);
2756
2757 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2758 {
2759 gdb_printf (tmp_error_stream,
2760 _("Cannot insert breakpoint %d.\n"
2761 "Cannot set software breakpoint "
2762 "at read-only address %s\n"),
2763 bl->owner->number,
2764 paddress (bl->gdbarch, bl->address));
2765 return 1;
2766 }
2767 }
2768
2769 if (bl->loc_type == bp_loc_software_breakpoint
2770 || bl->loc_type == bp_loc_hardware_breakpoint)
2771 {
2772 /* First check to see if we have to handle an overlay. */
2773 if (overlay_debugging == ovly_off
2774 || bl->section == NULL
2775 || !(section_is_overlay (bl->section)))
2776 {
2777 /* No overlay handling: just set the breakpoint. */
2778 try
2779 {
2780 int val;
2781
2782 val = bl->owner->insert_location (bl);
2783 if (val)
2784 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2785 }
2786 catch (gdb_exception &e)
2787 {
2788 rethrow_on_target_close_error (e);
2789 bp_excpt = std::move (e);
2790 }
2791 }
2792 else
2793 {
2794 /* This breakpoint is in an overlay section.
2795 Shall we set a breakpoint at the LMA? */
2796 if (!overlay_events_enabled)
2797 {
2798 /* Yes -- overlay event support is not active,
2799 so we must try to set a breakpoint at the LMA.
2800 This will not work for a hardware breakpoint. */
2801 if (bl->loc_type == bp_loc_hardware_breakpoint)
2802 warning (_("hardware breakpoint %d not supported in overlay!"),
2803 bl->owner->number);
2804 else
2805 {
2806 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2807 bl->section);
2808 /* Set a software (trap) breakpoint at the LMA. */
2809 bl->overlay_target_info = bl->target_info;
2810 bl->overlay_target_info.reqstd_address = addr;
2811
2812 /* No overlay handling: just set the breakpoint. */
2813 try
2814 {
2815 int val;
2816
2817 bl->overlay_target_info.kind
2818 = breakpoint_kind (bl, &addr);
2819 bl->overlay_target_info.placed_address = addr;
2820 val = target_insert_breakpoint (bl->gdbarch,
2821 &bl->overlay_target_info);
2822 if (val)
2823 bp_excpt
2824 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2825 }
2826 catch (gdb_exception &e)
2827 {
2828 rethrow_on_target_close_error (e);
2829 bp_excpt = std::move (e);
2830 }
2831
2832 if (bp_excpt.reason != 0)
2833 gdb_printf (tmp_error_stream,
2834 "Overlay breakpoint %d "
2835 "failed: in ROM?\n",
2836 bl->owner->number);
2837 }
2838 }
2839 /* Shall we set a breakpoint at the VMA? */
2840 if (section_is_mapped (bl->section))
2841 {
2842 /* Yes. This overlay section is mapped into memory. */
2843 try
2844 {
2845 int val;
2846
2847 val = bl->owner->insert_location (bl);
2848 if (val)
2849 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2850 }
2851 catch (gdb_exception_error &e)
2852 {
2853 rethrow_on_target_close_error (e);
2854 bp_excpt = std::move (e);
2855 }
2856 }
2857 else
2858 {
2859 /* No. This breakpoint will not be inserted.
2860 No error, but do not mark the bp as 'inserted'. */
2861 return 0;
2862 }
2863 }
2864
2865 if (bp_excpt.reason != 0)
2866 {
2867 /* Can't set the breakpoint. */
2868 gdb_assert (bl->owner != nullptr);
2869
2870 /* In some cases, we might not be able to insert a
2871 breakpoint in a shared library that has already been
2872 removed, but we have not yet processed the shlib unload
2873 event. Unfortunately, some targets that implement
2874 breakpoint insertion themselves can't tell why the
2875 breakpoint insertion failed (e.g., the remote target
2876 doesn't define error codes), so we must treat generic
2877 errors as memory errors. */
2878 if (bp_excpt.reason == RETURN_ERROR
2879 && (bp_excpt.error == GENERIC_ERROR
2880 || bp_excpt.error == MEMORY_ERROR)
2881 && bl->loc_type == bp_loc_software_breakpoint
2882 && (solib_name_from_address (bl->pspace, bl->address)
2883 || shared_objfile_contains_address_p (bl->pspace,
2884 bl->address)))
2885 {
2886 /* See also: disable_breakpoints_in_shlibs. */
2887 bl->shlib_disabled = 1;
2888 gdb::observers::breakpoint_modified.notify (bl->owner);
2889 if (!*disabled_breaks)
2890 {
2891 gdb_printf (tmp_error_stream,
2892 "Cannot insert breakpoint %d.\n",
2893 bl->owner->number);
2894 gdb_printf (tmp_error_stream,
2895 "Temporarily disabling shared "
2896 "library breakpoints:\n");
2897 }
2898 *disabled_breaks = 1;
2899 gdb_printf (tmp_error_stream,
2900 "breakpoint #%d\n", bl->owner->number);
2901 return 0;
2902 }
2903 else
2904 {
2905 if (bl->loc_type == bp_loc_hardware_breakpoint)
2906 {
2907 *hw_breakpoint_error = 1;
2908 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2909 gdb_printf (tmp_error_stream,
2910 "Cannot insert hardware breakpoint %d%s",
2911 bl->owner->number,
2912 bp_excpt.message ? ":" : ".\n");
2913 if (bp_excpt.message != NULL)
2914 gdb_printf (tmp_error_stream, "%s.\n",
2915 bp_excpt.what ());
2916 }
2917 else
2918 {
2919 if (bp_excpt.message == NULL)
2920 {
2921 std::string message
2922 = memory_error_message (TARGET_XFER_E_IO,
2923 bl->gdbarch, bl->address);
2924
2925 gdb_printf (tmp_error_stream,
2926 "Cannot insert breakpoint %d.\n"
2927 "%s\n",
2928 bl->owner->number, message.c_str ());
2929 }
2930 else
2931 {
2932 gdb_printf (tmp_error_stream,
2933 "Cannot insert breakpoint %d: %s\n",
2934 bl->owner->number,
2935 bp_excpt.what ());
2936 }
2937 }
2938 return 1;
2939
2940 }
2941 }
2942 else
2943 bl->inserted = 1;
2944
2945 return 0;
2946 }
2947
2948 else if (bl->loc_type == bp_loc_hardware_watchpoint
2949 && bl->owner->disposition != disp_del_at_next_stop)
2950 {
2951 int val;
2952
2953 val = bl->owner->insert_location (bl);
2954
2955 /* If trying to set a read-watchpoint, and it turns out it's not
2956 supported, try emulating one with an access watchpoint. */
2957 if (val == 1 && bl->watchpoint_type == hw_read)
2958 {
2959 /* But don't try to insert it, if there's already another
2960 hw_access location that would be considered a duplicate
2961 of this one. */
2962 for (bp_location *loc : all_bp_locations ())
2963 if (loc != bl
2964 && loc->watchpoint_type == hw_access
2965 && watchpoint_locations_match (bl, loc))
2966 {
2967 bl->duplicate = 1;
2968 bl->inserted = 1;
2969 bl->target_info = loc->target_info;
2970 bl->watchpoint_type = hw_access;
2971 val = 0;
2972 break;
2973 }
2974
2975 if (val == 1)
2976 {
2977 bl->watchpoint_type = hw_access;
2978 val = bl->owner->insert_location (bl);
2979
2980 if (val)
2981 /* Back to the original value. */
2982 bl->watchpoint_type = hw_read;
2983 }
2984 }
2985
2986 bl->inserted = (val == 0);
2987 }
2988
2989 else if (bl->owner->type == bp_catchpoint)
2990 {
2991 int val;
2992
2993 val = bl->owner->insert_location (bl);
2994 if (val)
2995 {
2996 bl->owner->enable_state = bp_disabled;
2997
2998 if (val == 1)
2999 warning (_("\
3000 Error inserting catchpoint %d: Your system does not support this type\n\
3001 of catchpoint."), bl->owner->number);
3002 else
3003 warning (_("Error inserting catchpoint %d."), bl->owner->number);
3004 }
3005
3006 bl->inserted = (val == 0);
3007
3008 /* We've already printed an error message if there was a problem
3009 inserting this catchpoint, and we've disabled the catchpoint,
3010 so just return success. */
3011 return 0;
3012 }
3013
3014 return 0;
3015 }
3016
3017 /* This function is called when program space PSPACE is about to be
3018 deleted. It takes care of updating breakpoints to not reference
3019 PSPACE anymore. */
3020
3021 void
3022 breakpoint_program_space_exit (struct program_space *pspace)
3023 {
3024 /* Remove any breakpoint that was set through this program space. */
3025 for (breakpoint *b : all_breakpoints_safe ())
3026 if (b->pspace == pspace)
3027 delete_breakpoint (b);
3028
3029 /* Breakpoints set through other program spaces could have locations
3030 bound to PSPACE as well. Remove those. */
3031 for (bp_location *loc : all_bp_locations ())
3032 {
3033 struct bp_location *tmp;
3034
3035 if (loc->pspace == pspace)
3036 {
3037 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3038 if (loc->owner->loc == loc)
3039 loc->owner->loc = loc->next;
3040 else
3041 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3042 if (tmp->next == loc)
3043 {
3044 tmp->next = loc->next;
3045 break;
3046 }
3047 }
3048 }
3049
3050 /* Now update the global location list to permanently delete the
3051 removed locations above. */
3052 update_global_location_list (UGLL_DONT_INSERT);
3053 }
3054
3055 /* Make sure all breakpoints are inserted in inferior.
3056 Throws exception on any error.
3057 A breakpoint that is already inserted won't be inserted
3058 again, so calling this function twice is safe. */
3059 void
3060 insert_breakpoints (void)
3061 {
3062 for (breakpoint *bpt : all_breakpoints ())
3063 if (is_hardware_watchpoint (bpt))
3064 {
3065 struct watchpoint *w = (struct watchpoint *) bpt;
3066
3067 update_watchpoint (w, false /* don't reparse. */);
3068 }
3069
3070 /* Updating watchpoints creates new locations, so update the global
3071 location list. Explicitly tell ugll to insert locations and
3072 ignore breakpoints_always_inserted_mode. Also,
3073 update_global_location_list tries to "upgrade" software
3074 breakpoints to hardware breakpoints to handle "set breakpoint
3075 auto-hw", so we need to call it even if we don't have new
3076 locations. */
3077 update_global_location_list (UGLL_INSERT);
3078 }
3079
3080 /* This is used when we need to synch breakpoint conditions between GDB and the
3081 target. It is the case with deleting and disabling of breakpoints when using
3082 always-inserted mode. */
3083
3084 static void
3085 update_inserted_breakpoint_locations (void)
3086 {
3087 int error_flag = 0;
3088 int val = 0;
3089 int disabled_breaks = 0;
3090 int hw_breakpoint_error = 0;
3091 int hw_bp_details_reported = 0;
3092
3093 string_file tmp_error_stream;
3094
3095 /* Explicitly mark the warning -- this will only be printed if
3096 there was an error. */
3097 tmp_error_stream.puts ("Warning:\n");
3098
3099 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3100
3101 for (bp_location *bl : all_bp_locations ())
3102 {
3103 /* We only want to update software breakpoints and hardware
3104 breakpoints. */
3105 if (!is_breakpoint (bl->owner))
3106 continue;
3107
3108 /* We only want to update locations that are already inserted
3109 and need updating. This is to avoid unwanted insertion during
3110 deletion of breakpoints. */
3111 if (!bl->inserted || !bl->needs_update)
3112 continue;
3113
3114 switch_to_program_space_and_thread (bl->pspace);
3115
3116 /* For targets that support global breakpoints, there's no need
3117 to select an inferior to insert breakpoint to. In fact, even
3118 if we aren't attached to any process yet, we should still
3119 insert breakpoints. */
3120 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3121 && (inferior_ptid == null_ptid || !target_has_execution ()))
3122 continue;
3123
3124 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3125 &hw_breakpoint_error, &hw_bp_details_reported);
3126 if (val)
3127 error_flag = val;
3128 }
3129
3130 if (error_flag)
3131 {
3132 target_terminal::ours_for_output ();
3133 error (("%s"), tmp_error_stream.c_str ());
3134 }
3135 }
3136
3137 /* Used when starting or continuing the program. */
3138
3139 static void
3140 insert_breakpoint_locations (void)
3141 {
3142 int error_flag = 0;
3143 int val = 0;
3144 int disabled_breaks = 0;
3145 int hw_breakpoint_error = 0;
3146 int hw_bp_error_explained_already = 0;
3147
3148 string_file tmp_error_stream;
3149
3150 /* Explicitly mark the warning -- this will only be printed if
3151 there was an error. */
3152 tmp_error_stream.puts ("Warning:\n");
3153
3154 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3155
3156 for (bp_location *bl : all_bp_locations ())
3157 {
3158 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3159 continue;
3160
3161 /* There is no point inserting thread-specific breakpoints if
3162 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3163 has BL->OWNER always non-NULL. */
3164 if (bl->owner->thread != -1
3165 && !valid_global_thread_id (bl->owner->thread))
3166 continue;
3167
3168 switch_to_program_space_and_thread (bl->pspace);
3169
3170 /* For targets that support global breakpoints, there's no need
3171 to select an inferior to insert breakpoint to. In fact, even
3172 if we aren't attached to any process yet, we should still
3173 insert breakpoints. */
3174 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3175 && (inferior_ptid == null_ptid || !target_has_execution ()))
3176 continue;
3177
3178 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3179 &hw_breakpoint_error, &hw_bp_error_explained_already);
3180 if (val)
3181 error_flag = val;
3182 }
3183
3184 /* If we failed to insert all locations of a watchpoint, remove
3185 them, as half-inserted watchpoint is of limited use. */
3186 for (breakpoint *bpt : all_breakpoints ())
3187 {
3188 bool some_failed = false;
3189
3190 if (!is_hardware_watchpoint (bpt))
3191 continue;
3192
3193 if (!breakpoint_enabled (bpt))
3194 continue;
3195
3196 if (bpt->disposition == disp_del_at_next_stop)
3197 continue;
3198
3199 for (bp_location *loc : bpt->locations ())
3200 if (!loc->inserted && should_be_inserted (loc))
3201 {
3202 some_failed = true;
3203 break;
3204 }
3205
3206 if (some_failed)
3207 {
3208 for (bp_location *loc : bpt->locations ())
3209 if (loc->inserted)
3210 remove_breakpoint (loc);
3211
3212 hw_breakpoint_error = 1;
3213 tmp_error_stream.printf ("Could not insert "
3214 "hardware watchpoint %d.\n",
3215 bpt->number);
3216 error_flag = -1;
3217 }
3218 }
3219
3220 if (error_flag)
3221 {
3222 /* If a hardware breakpoint or watchpoint was inserted, add a
3223 message about possibly exhausted resources. */
3224 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3225 {
3226 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3227 You may have requested too many hardware breakpoints/watchpoints.\n");
3228 }
3229 target_terminal::ours_for_output ();
3230 error (("%s"), tmp_error_stream.c_str ());
3231 }
3232 }
3233
3234 /* Used when the program stops.
3235 Returns zero if successful, or non-zero if there was a problem
3236 removing a breakpoint location. */
3237
3238 int
3239 remove_breakpoints (void)
3240 {
3241 int val = 0;
3242
3243 for (bp_location *bl : all_bp_locations ())
3244 if (bl->inserted && !is_tracepoint (bl->owner))
3245 val |= remove_breakpoint (bl);
3246
3247 return val;
3248 }
3249
3250 /* When a thread exits, remove breakpoints that are related to
3251 that thread. */
3252
3253 static void
3254 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3255 {
3256 for (breakpoint *b : all_breakpoints_safe ())
3257 {
3258 if (b->thread == tp->global_num && user_breakpoint_p (b))
3259 {
3260 gdb_printf (_("\
3261 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3262 b->number, print_thread_id (tp));
3263 delete_breakpoint (b);
3264 }
3265 }
3266 }
3267
3268 /* See breakpoint.h. */
3269
3270 void
3271 remove_breakpoints_inf (inferior *inf)
3272 {
3273 int val;
3274
3275 for (bp_location *bl : all_bp_locations ())
3276 {
3277 if (bl->pspace != inf->pspace)
3278 continue;
3279
3280 if (bl->inserted && !bl->target_info.persist)
3281 {
3282 val = remove_breakpoint (bl);
3283 if (val != 0)
3284 return;
3285 }
3286 }
3287 }
3288
3289 static int internal_breakpoint_number = -1;
3290
3291 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3292 If INTERNAL is non-zero, the breakpoint number will be populated
3293 from internal_breakpoint_number and that variable decremented.
3294 Otherwise the breakpoint number will be populated from
3295 breakpoint_count and that value incremented. Internal breakpoints
3296 do not set the internal var bpnum. */
3297 static void
3298 set_breakpoint_number (int internal, struct breakpoint *b)
3299 {
3300 if (internal)
3301 b->number = internal_breakpoint_number--;
3302 else
3303 {
3304 set_breakpoint_count (breakpoint_count + 1);
3305 b->number = breakpoint_count;
3306 }
3307 }
3308
3309 /* Create a TYPE breakpoint on ADDRESS from an object file with GDBARCH. */
3310
3311 static struct breakpoint *
3312 create_internal_breakpoint (struct gdbarch *gdbarch,
3313 CORE_ADDR address, enum bptype type)
3314 {
3315 std::unique_ptr<internal_breakpoint> b
3316 (new internal_breakpoint (gdbarch, type, address));
3317
3318 b->number = internal_breakpoint_number--;
3319
3320 return add_to_breakpoint_chain (std::move (b));
3321 }
3322
3323 /* Create a TYPE breakpoint on minimal symbol MSYM from an object file with
3324 GDBARCH. */
3325
3326 static struct breakpoint *
3327 create_internal_breakpoint (struct gdbarch *gdbarch,
3328 struct bound_minimal_symbol &msym, enum bptype type)
3329 {
3330 CORE_ADDR address;
3331
3332 address = msym.value_address ();
3333
3334 address = gdbarch_convert_from_func_ptr_addr
3335 (gdbarch, address, current_inferior ()->top_target ());
3336
3337 /* Note that we're not using gdbarch_addr_bits_remove here, because that's
3338 related to addresses in $pc. We're getting the address from the
3339 minimal symbol table. */
3340
3341 /* Is gdbarch_deprecated_function_start_offset needed here? Or is that dealt
3342 with elsewhere? Needs testing on vax. */
3343
3344 if (gdbarch_skip_entrypoint_p (gdbarch))
3345 address = gdbarch_skip_entrypoint (gdbarch, address);
3346
3347 return create_internal_breakpoint (gdbarch, address, type);
3348 }
3349
3350 static const char *const longjmp_names[] =
3351 {
3352 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3353 };
3354 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3355
3356 /* Per-objfile data private to breakpoint.c. */
3357 struct breakpoint_objfile_data
3358 {
3359 /* Minimal symbol for "_ovly_debug_event" (if any). */
3360 struct bound_minimal_symbol overlay_msym;
3361
3362 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3363 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3364
3365 /* True if we have looked for longjmp probes. */
3366 int longjmp_searched = 0;
3367
3368 /* SystemTap probe points for longjmp (if any). These are non-owning
3369 references. */
3370 std::vector<probe *> longjmp_probes;
3371
3372 /* Minimal symbol for "std::terminate()" (if any). */
3373 struct bound_minimal_symbol terminate_msym;
3374
3375 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3376 struct bound_minimal_symbol exception_msym;
3377
3378 /* True if we have looked for exception probes. */
3379 int exception_searched = 0;
3380
3381 /* SystemTap probe points for unwinding (if any). These are non-owning
3382 references. */
3383 std::vector<probe *> exception_probes;
3384 };
3385
3386 static const registry<objfile>::key<breakpoint_objfile_data>
3387 breakpoint_objfile_key;
3388
3389 /* Minimal symbol not found sentinel. */
3390 static struct minimal_symbol msym_not_found;
3391
3392 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3393
3394 static bool
3395 msym_not_found_p (const struct minimal_symbol *msym)
3396 {
3397 return msym == &msym_not_found;
3398 }
3399
3400 /* Return per-objfile data needed by breakpoint.c.
3401 Allocate the data if necessary. */
3402
3403 static struct breakpoint_objfile_data *
3404 get_breakpoint_objfile_data (struct objfile *objfile)
3405 {
3406 struct breakpoint_objfile_data *bp_objfile_data;
3407
3408 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3409 if (bp_objfile_data == NULL)
3410 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3411 return bp_objfile_data;
3412 }
3413
3414 static void
3415 create_overlay_event_breakpoint (void)
3416 {
3417 const char *const func_name = "_ovly_debug_event";
3418
3419 for (objfile *objfile : current_program_space->objfiles ())
3420 {
3421 struct breakpoint *b;
3422 struct breakpoint_objfile_data *bp_objfile_data;
3423 CORE_ADDR addr;
3424
3425 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3426
3427 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3428 continue;
3429
3430 if (bp_objfile_data->overlay_msym.minsym == NULL)
3431 {
3432 struct bound_minimal_symbol m;
3433
3434 m = lookup_minimal_symbol_text (func_name, objfile);
3435 if (m.minsym == NULL)
3436 {
3437 /* Avoid future lookups in this objfile. */
3438 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3439 continue;
3440 }
3441 bp_objfile_data->overlay_msym = m;
3442 }
3443
3444 addr = bp_objfile_data->overlay_msym.value_address ();
3445 b = create_internal_breakpoint (objfile->arch (), addr,
3446 bp_overlay_event);
3447 b->locspec = new_explicit_location_spec_function (func_name);
3448
3449 if (overlay_debugging == ovly_auto)
3450 {
3451 b->enable_state = bp_enabled;
3452 overlay_events_enabled = 1;
3453 }
3454 else
3455 {
3456 b->enable_state = bp_disabled;
3457 overlay_events_enabled = 0;
3458 }
3459 }
3460 }
3461
3462 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3463 true if a breakpoint was installed. */
3464
3465 static bool
3466 create_longjmp_master_breakpoint_probe (objfile *objfile)
3467 {
3468 struct gdbarch *gdbarch = objfile->arch ();
3469 struct breakpoint_objfile_data *bp_objfile_data
3470 = get_breakpoint_objfile_data (objfile);
3471
3472 if (!bp_objfile_data->longjmp_searched)
3473 {
3474 std::vector<probe *> ret
3475 = find_probes_in_objfile (objfile, "libc", "longjmp");
3476
3477 if (!ret.empty ())
3478 {
3479 /* We are only interested in checking one element. */
3480 probe *p = ret[0];
3481
3482 if (!p->can_evaluate_arguments ())
3483 {
3484 /* We cannot use the probe interface here,
3485 because it does not know how to evaluate
3486 arguments. */
3487 ret.clear ();
3488 }
3489 }
3490 bp_objfile_data->longjmp_probes = ret;
3491 bp_objfile_data->longjmp_searched = 1;
3492 }
3493
3494 if (bp_objfile_data->longjmp_probes.empty ())
3495 return false;
3496
3497 for (probe *p : bp_objfile_data->longjmp_probes)
3498 {
3499 struct breakpoint *b;
3500
3501 b = create_internal_breakpoint (gdbarch,
3502 p->get_relocated_address (objfile),
3503 bp_longjmp_master);
3504 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
3505 b->enable_state = bp_disabled;
3506 }
3507
3508 return true;
3509 }
3510
3511 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3512 Return true if at least one breakpoint was installed. */
3513
3514 static bool
3515 create_longjmp_master_breakpoint_names (objfile *objfile)
3516 {
3517 struct gdbarch *gdbarch = objfile->arch ();
3518 if (!gdbarch_get_longjmp_target_p (gdbarch))
3519 return false;
3520
3521 struct breakpoint_objfile_data *bp_objfile_data
3522 = get_breakpoint_objfile_data (objfile);
3523 unsigned int installed_bp = 0;
3524
3525 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3526 {
3527 struct breakpoint *b;
3528 const char *func_name;
3529 CORE_ADDR addr;
3530
3531 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3532 continue;
3533
3534 func_name = longjmp_names[i];
3535 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3536 {
3537 struct bound_minimal_symbol m;
3538
3539 m = lookup_minimal_symbol_text (func_name, objfile);
3540 if (m.minsym == NULL)
3541 {
3542 /* Prevent future lookups in this objfile. */
3543 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3544 continue;
3545 }
3546 bp_objfile_data->longjmp_msym[i] = m;
3547 }
3548
3549 addr = bp_objfile_data->longjmp_msym[i].value_address ();
3550 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
3551 b->locspec = new_explicit_location_spec_function (func_name);
3552 b->enable_state = bp_disabled;
3553 installed_bp++;
3554 }
3555
3556 return installed_bp > 0;
3557 }
3558
3559 /* Create a master longjmp breakpoint. */
3560
3561 static void
3562 create_longjmp_master_breakpoint (void)
3563 {
3564 scoped_restore_current_program_space restore_pspace;
3565
3566 for (struct program_space *pspace : program_spaces)
3567 {
3568 set_current_program_space (pspace);
3569
3570 for (objfile *obj : current_program_space->objfiles ())
3571 {
3572 /* Skip separate debug object, it's handled in the loop below. */
3573 if (obj->separate_debug_objfile_backlink != nullptr)
3574 continue;
3575
3576 /* Try a probe kind breakpoint on main objfile. */
3577 if (create_longjmp_master_breakpoint_probe (obj))
3578 continue;
3579
3580 /* Try longjmp_names kind breakpoints on main and separate_debug
3581 objfiles. */
3582 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3583 if (create_longjmp_master_breakpoint_names (debug_objfile))
3584 break;
3585 }
3586 }
3587 }
3588
3589 /* Create a master std::terminate breakpoint. */
3590 static void
3591 create_std_terminate_master_breakpoint (void)
3592 {
3593 const char *const func_name = "std::terminate()";
3594
3595 scoped_restore_current_program_space restore_pspace;
3596
3597 for (struct program_space *pspace : program_spaces)
3598 {
3599 set_current_program_space (pspace);
3600
3601 for (objfile *objfile : current_program_space->objfiles ())
3602 {
3603 struct breakpoint *b;
3604 struct breakpoint_objfile_data *bp_objfile_data;
3605
3606 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3607
3608 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3609 continue;
3610
3611 if (bp_objfile_data->terminate_msym.minsym == NULL)
3612 {
3613 struct bound_minimal_symbol m;
3614
3615 m = lookup_minimal_symbol (func_name, NULL, objfile);
3616 if (m.minsym == NULL || (m.minsym->type () != mst_text
3617 && m.minsym->type () != mst_file_text))
3618 {
3619 /* Prevent future lookups in this objfile. */
3620 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3621 continue;
3622 }
3623 bp_objfile_data->terminate_msym = m;
3624 }
3625
3626 b = create_internal_breakpoint (objfile->arch (),
3627 bp_objfile_data->terminate_msym,
3628 bp_std_terminate_master);
3629 b->locspec = new_explicit_location_spec_function (func_name);
3630 b->enable_state = bp_disabled;
3631 }
3632 }
3633 }
3634
3635 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3636 probe. Return true if a breakpoint was installed. */
3637
3638 static bool
3639 create_exception_master_breakpoint_probe (objfile *objfile)
3640 {
3641 struct breakpoint *b;
3642 struct gdbarch *gdbarch;
3643 struct breakpoint_objfile_data *bp_objfile_data;
3644
3645 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3646
3647 /* We prefer the SystemTap probe point if it exists. */
3648 if (!bp_objfile_data->exception_searched)
3649 {
3650 std::vector<probe *> ret
3651 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3652
3653 if (!ret.empty ())
3654 {
3655 /* We are only interested in checking one element. */
3656 probe *p = ret[0];
3657
3658 if (!p->can_evaluate_arguments ())
3659 {
3660 /* We cannot use the probe interface here, because it does
3661 not know how to evaluate arguments. */
3662 ret.clear ();
3663 }
3664 }
3665 bp_objfile_data->exception_probes = ret;
3666 bp_objfile_data->exception_searched = 1;
3667 }
3668
3669 if (bp_objfile_data->exception_probes.empty ())
3670 return false;
3671
3672 gdbarch = objfile->arch ();
3673
3674 for (probe *p : bp_objfile_data->exception_probes)
3675 {
3676 b = create_internal_breakpoint (gdbarch,
3677 p->get_relocated_address (objfile),
3678 bp_exception_master);
3679 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
3680 b->enable_state = bp_disabled;
3681 }
3682
3683 return true;
3684 }
3685
3686 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3687 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3688
3689 static bool
3690 create_exception_master_breakpoint_hook (objfile *objfile)
3691 {
3692 const char *const func_name = "_Unwind_DebugHook";
3693 struct breakpoint *b;
3694 struct gdbarch *gdbarch;
3695 struct breakpoint_objfile_data *bp_objfile_data;
3696
3697 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3698
3699 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3700 return false;
3701
3702 gdbarch = objfile->arch ();
3703
3704 if (bp_objfile_data->exception_msym.minsym == NULL)
3705 {
3706 struct bound_minimal_symbol debug_hook;
3707
3708 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3709 if (debug_hook.minsym == NULL)
3710 {
3711 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3712 return false;
3713 }
3714
3715 bp_objfile_data->exception_msym = debug_hook;
3716 }
3717
3718 b = create_internal_breakpoint (gdbarch, bp_objfile_data->exception_msym,
3719 bp_exception_master);
3720 b->locspec = new_explicit_location_spec_function (func_name);
3721 b->enable_state = bp_disabled;
3722
3723 return true;
3724 }
3725
3726 /* Install a master breakpoint on the unwinder's debug hook. */
3727
3728 static void
3729 create_exception_master_breakpoint (void)
3730 {
3731 for (objfile *obj : current_program_space->objfiles ())
3732 {
3733 /* Skip separate debug object. */
3734 if (obj->separate_debug_objfile_backlink)
3735 continue;
3736
3737 /* Try a probe kind breakpoint. */
3738 if (create_exception_master_breakpoint_probe (obj))
3739 continue;
3740
3741 /* Iterate over main and separate debug objects and try an
3742 _Unwind_DebugHook kind breakpoint. */
3743 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3744 if (create_exception_master_breakpoint_hook (debug_objfile))
3745 break;
3746 }
3747 }
3748
3749 /* Does B have a location spec? */
3750
3751 static bool
3752 breakpoint_location_spec_empty_p (const struct breakpoint *b)
3753 {
3754 return (b->locspec != nullptr && b->locspec->empty_p ());
3755 }
3756
3757 void
3758 update_breakpoints_after_exec (void)
3759 {
3760 /* We're about to delete breakpoints from GDB's lists. If the
3761 INSERTED flag is true, GDB will try to lift the breakpoints by
3762 writing the breakpoints' "shadow contents" back into memory. The
3763 "shadow contents" are NOT valid after an exec, so GDB should not
3764 do that. Instead, the target is responsible from marking
3765 breakpoints out as soon as it detects an exec. We don't do that
3766 here instead, because there may be other attempts to delete
3767 breakpoints after detecting an exec and before reaching here. */
3768 for (bp_location *bploc : all_bp_locations ())
3769 if (bploc->pspace == current_program_space)
3770 gdb_assert (!bploc->inserted);
3771
3772 for (breakpoint *b : all_breakpoints_safe ())
3773 {
3774 if (b->pspace != current_program_space)
3775 continue;
3776
3777 /* Solib breakpoints must be explicitly reset after an exec(). */
3778 if (b->type == bp_shlib_event)
3779 {
3780 delete_breakpoint (b);
3781 continue;
3782 }
3783
3784 /* JIT breakpoints must be explicitly reset after an exec(). */
3785 if (b->type == bp_jit_event)
3786 {
3787 delete_breakpoint (b);
3788 continue;
3789 }
3790
3791 /* Thread event breakpoints must be set anew after an exec(),
3792 as must overlay event and longjmp master breakpoints. */
3793 if (b->type == bp_thread_event || b->type == bp_overlay_event
3794 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3795 || b->type == bp_exception_master)
3796 {
3797 delete_breakpoint (b);
3798 continue;
3799 }
3800
3801 /* Step-resume breakpoints are meaningless after an exec(). */
3802 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3803 {
3804 delete_breakpoint (b);
3805 continue;
3806 }
3807
3808 /* Just like single-step breakpoints. */
3809 if (b->type == bp_single_step)
3810 {
3811 delete_breakpoint (b);
3812 continue;
3813 }
3814
3815 /* Longjmp and longjmp-resume breakpoints are also meaningless
3816 after an exec. */
3817 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3818 || b->type == bp_longjmp_call_dummy
3819 || b->type == bp_exception || b->type == bp_exception_resume)
3820 {
3821 delete_breakpoint (b);
3822 continue;
3823 }
3824
3825 if (b->type == bp_catchpoint)
3826 {
3827 /* For now, none of the bp_catchpoint breakpoints need to
3828 do anything at this point. In the future, if some of
3829 the catchpoints need to something, we will need to add
3830 a new method, and call this method from here. */
3831 continue;
3832 }
3833
3834 /* bp_finish is a special case. The only way we ought to be able
3835 to see one of these when an exec() has happened, is if the user
3836 caught a vfork, and then said "finish". Ordinarily a finish just
3837 carries them to the call-site of the current callee, by setting
3838 a temporary bp there and resuming. But in this case, the finish
3839 will carry them entirely through the vfork & exec.
3840
3841 We don't want to allow a bp_finish to remain inserted now. But
3842 we can't safely delete it, 'cause finish_command has a handle to
3843 the bp on a bpstat, and will later want to delete it. There's a
3844 chance (and I've seen it happen) that if we delete the bp_finish
3845 here, that its storage will get reused by the time finish_command
3846 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3847 We really must allow finish_command to delete a bp_finish.
3848
3849 In the absence of a general solution for the "how do we know
3850 it's safe to delete something others may have handles to?"
3851 problem, what we'll do here is just uninsert the bp_finish, and
3852 let finish_command delete it.
3853
3854 (We know the bp_finish is "doomed" in the sense that it's
3855 momentary, and will be deleted as soon as finish_command sees
3856 the inferior stopped. So it doesn't matter that the bp's
3857 address is probably bogus in the new a.out, unlike e.g., the
3858 solib breakpoints.) */
3859
3860 if (b->type == bp_finish)
3861 {
3862 continue;
3863 }
3864
3865 /* Without a symbolic address, we have little hope of the
3866 pre-exec() address meaning the same thing in the post-exec()
3867 a.out. */
3868 if (breakpoint_location_spec_empty_p (b))
3869 {
3870 delete_breakpoint (b);
3871 continue;
3872 }
3873 }
3874 }
3875
3876 int
3877 detach_breakpoints (ptid_t ptid)
3878 {
3879 int val = 0;
3880 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3881 struct inferior *inf = current_inferior ();
3882
3883 if (ptid.pid () == inferior_ptid.pid ())
3884 error (_("Cannot detach breakpoints of inferior_ptid"));
3885
3886 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3887 inferior_ptid = ptid;
3888 for (bp_location *bl : all_bp_locations ())
3889 {
3890 if (bl->pspace != inf->pspace)
3891 continue;
3892
3893 /* This function must physically remove breakpoints locations
3894 from the specified ptid, without modifying the breakpoint
3895 package's state. Locations of type bp_loc_other and
3896 bp_loc_software_watchpoint are only maintained at GDB side,
3897 so there is no need to remove them. Moreover, removing these
3898 would modify the breakpoint package's state. */
3899 if (bl->loc_type == bp_loc_other
3900 || bl->loc_type == bp_loc_software_watchpoint)
3901 continue;
3902
3903 if (bl->inserted)
3904 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3905 }
3906
3907 return val;
3908 }
3909
3910 /* Remove the breakpoint location BL from the current address space.
3911 Note that this is used to detach breakpoints from a child fork.
3912 When we get here, the child isn't in the inferior list, and neither
3913 do we have objects to represent its address space --- we should
3914 *not* look at bl->pspace->aspace here. */
3915
3916 static int
3917 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3918 {
3919 int val;
3920
3921 /* BL is never in moribund_locations by our callers. */
3922 gdb_assert (bl->owner != NULL);
3923
3924 /* The type of none suggests that owner is actually deleted.
3925 This should not ever happen. */
3926 gdb_assert (bl->owner->type != bp_none);
3927
3928 if (bl->loc_type == bp_loc_software_breakpoint
3929 || bl->loc_type == bp_loc_hardware_breakpoint)
3930 {
3931 /* "Normal" instruction breakpoint: either the standard
3932 trap-instruction bp (bp_breakpoint), or a
3933 bp_hardware_breakpoint. */
3934
3935 /* First check to see if we have to handle an overlay. */
3936 if (overlay_debugging == ovly_off
3937 || bl->section == NULL
3938 || !(section_is_overlay (bl->section)))
3939 {
3940 /* No overlay handling: just remove the breakpoint. */
3941
3942 /* If we're trying to uninsert a memory breakpoint that we
3943 know is set in a dynamic object that is marked
3944 shlib_disabled, then either the dynamic object was
3945 removed with "remove-symbol-file" or with
3946 "nosharedlibrary". In the former case, we don't know
3947 whether another dynamic object might have loaded over the
3948 breakpoint's address -- the user might well let us know
3949 about it next with add-symbol-file (the whole point of
3950 add-symbol-file is letting the user manually maintain a
3951 list of dynamically loaded objects). If we have the
3952 breakpoint's shadow memory, that is, this is a software
3953 breakpoint managed by GDB, check whether the breakpoint
3954 is still inserted in memory, to avoid overwriting wrong
3955 code with stale saved shadow contents. Note that HW
3956 breakpoints don't have shadow memory, as they're
3957 implemented using a mechanism that is not dependent on
3958 being able to modify the target's memory, and as such
3959 they should always be removed. */
3960 if (bl->shlib_disabled
3961 && bl->target_info.shadow_len != 0
3962 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3963 val = 0;
3964 else
3965 val = bl->owner->remove_location (bl, reason);
3966 }
3967 else
3968 {
3969 /* This breakpoint is in an overlay section.
3970 Did we set a breakpoint at the LMA? */
3971 if (!overlay_events_enabled)
3972 {
3973 /* Yes -- overlay event support is not active, so we
3974 should have set a breakpoint at the LMA. Remove it.
3975 */
3976 /* Ignore any failures: if the LMA is in ROM, we will
3977 have already warned when we failed to insert it. */
3978 if (bl->loc_type == bp_loc_hardware_breakpoint)
3979 target_remove_hw_breakpoint (bl->gdbarch,
3980 &bl->overlay_target_info);
3981 else
3982 target_remove_breakpoint (bl->gdbarch,
3983 &bl->overlay_target_info,
3984 reason);
3985 }
3986 /* Did we set a breakpoint at the VMA?
3987 If so, we will have marked the breakpoint 'inserted'. */
3988 if (bl->inserted)
3989 {
3990 /* Yes -- remove it. Previously we did not bother to
3991 remove the breakpoint if the section had been
3992 unmapped, but let's not rely on that being safe. We
3993 don't know what the overlay manager might do. */
3994
3995 /* However, we should remove *software* breakpoints only
3996 if the section is still mapped, or else we overwrite
3997 wrong code with the saved shadow contents. */
3998 if (bl->loc_type == bp_loc_hardware_breakpoint
3999 || section_is_mapped (bl->section))
4000 val = bl->owner->remove_location (bl, reason);
4001 else
4002 val = 0;
4003 }
4004 else
4005 {
4006 /* No -- not inserted, so no need to remove. No error. */
4007 val = 0;
4008 }
4009 }
4010
4011 /* In some cases, we might not be able to remove a breakpoint in
4012 a shared library that has already been removed, but we have
4013 not yet processed the shlib unload event. Similarly for an
4014 unloaded add-symbol-file object - the user might not yet have
4015 had the chance to remove-symbol-file it. shlib_disabled will
4016 be set if the library/object has already been removed, but
4017 the breakpoint hasn't been uninserted yet, e.g., after
4018 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4019 always-inserted mode. */
4020 if (val
4021 && (bl->loc_type == bp_loc_software_breakpoint
4022 && (bl->shlib_disabled
4023 || solib_name_from_address (bl->pspace, bl->address)
4024 || shared_objfile_contains_address_p (bl->pspace,
4025 bl->address))))
4026 val = 0;
4027
4028 if (val)
4029 return val;
4030 bl->inserted = (reason == DETACH_BREAKPOINT);
4031 }
4032 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4033 {
4034 bl->inserted = (reason == DETACH_BREAKPOINT);
4035 bl->owner->remove_location (bl, reason);
4036
4037 /* Failure to remove any of the hardware watchpoints comes here. */
4038 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4039 warning (_("Could not remove hardware watchpoint %d."),
4040 bl->owner->number);
4041 }
4042 else if (bl->owner->type == bp_catchpoint
4043 && breakpoint_enabled (bl->owner)
4044 && !bl->duplicate)
4045 {
4046 val = bl->owner->remove_location (bl, reason);
4047 if (val)
4048 return val;
4049
4050 bl->inserted = (reason == DETACH_BREAKPOINT);
4051 }
4052
4053 return 0;
4054 }
4055
4056 static int
4057 remove_breakpoint (struct bp_location *bl)
4058 {
4059 /* BL is never in moribund_locations by our callers. */
4060 gdb_assert (bl->owner != NULL);
4061
4062 /* The type of none suggests that owner is actually deleted.
4063 This should not ever happen. */
4064 gdb_assert (bl->owner->type != bp_none);
4065
4066 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4067
4068 switch_to_program_space_and_thread (bl->pspace);
4069
4070 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4071 }
4072
4073 /* Clear the "inserted" flag in all breakpoints. */
4074
4075 void
4076 mark_breakpoints_out (void)
4077 {
4078 for (bp_location *bl : all_bp_locations ())
4079 if (bl->pspace == current_program_space)
4080 bl->inserted = 0;
4081 }
4082
4083 /* Clear the "inserted" flag in all breakpoints and delete any
4084 breakpoints which should go away between runs of the program.
4085
4086 Plus other such housekeeping that has to be done for breakpoints
4087 between runs.
4088
4089 Note: this function gets called at the end of a run (by
4090 generic_mourn_inferior) and when a run begins (by
4091 init_wait_for_inferior). */
4092
4093
4094
4095 void
4096 breakpoint_init_inferior (enum inf_context context)
4097 {
4098 struct program_space *pspace = current_program_space;
4099
4100 /* If breakpoint locations are shared across processes, then there's
4101 nothing to do. */
4102 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4103 return;
4104
4105 mark_breakpoints_out ();
4106
4107 for (breakpoint *b : all_breakpoints_safe ())
4108 {
4109 if (b->loc && b->loc->pspace != pspace)
4110 continue;
4111
4112 switch (b->type)
4113 {
4114 case bp_call_dummy:
4115 case bp_longjmp_call_dummy:
4116
4117 /* If the call dummy breakpoint is at the entry point it will
4118 cause problems when the inferior is rerun, so we better get
4119 rid of it. */
4120
4121 case bp_watchpoint_scope:
4122
4123 /* Also get rid of scope breakpoints. */
4124
4125 case bp_shlib_event:
4126
4127 /* Also remove solib event breakpoints. Their addresses may
4128 have changed since the last time we ran the program.
4129 Actually we may now be debugging against different target;
4130 and so the solib backend that installed this breakpoint may
4131 not be used in by the target. E.g.,
4132
4133 (gdb) file prog-linux
4134 (gdb) run # native linux target
4135 ...
4136 (gdb) kill
4137 (gdb) file prog-win.exe
4138 (gdb) tar rem :9999 # remote Windows gdbserver.
4139 */
4140
4141 case bp_step_resume:
4142
4143 /* Also remove step-resume breakpoints. */
4144
4145 case bp_single_step:
4146
4147 /* Also remove single-step breakpoints. */
4148
4149 delete_breakpoint (b);
4150 break;
4151
4152 case bp_watchpoint:
4153 case bp_hardware_watchpoint:
4154 case bp_read_watchpoint:
4155 case bp_access_watchpoint:
4156 {
4157 struct watchpoint *w = (struct watchpoint *) b;
4158
4159 /* Likewise for watchpoints on local expressions. */
4160 if (w->exp_valid_block != NULL)
4161 delete_breakpoint (b);
4162 else
4163 {
4164 /* Get rid of existing locations, which are no longer
4165 valid. New ones will be created in
4166 update_watchpoint, when the inferior is restarted.
4167 The next update_global_location_list call will
4168 garbage collect them. */
4169 b->loc = NULL;
4170
4171 if (context == inf_starting)
4172 {
4173 /* Reset val field to force reread of starting value in
4174 insert_breakpoints. */
4175 w->val.reset (nullptr);
4176 w->val_valid = false;
4177 }
4178 }
4179 }
4180 break;
4181 default:
4182 break;
4183 }
4184 }
4185
4186 /* Get rid of the moribund locations. */
4187 for (bp_location *bl : moribund_locations)
4188 decref_bp_location (&bl);
4189 moribund_locations.clear ();
4190 }
4191
4192 /* These functions concern about actual breakpoints inserted in the
4193 target --- to e.g. check if we need to do decr_pc adjustment or if
4194 we need to hop over the bkpt --- so we check for address space
4195 match, not program space. */
4196
4197 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4198 exists at PC. It returns ordinary_breakpoint_here if it's an
4199 ordinary breakpoint, or permanent_breakpoint_here if it's a
4200 permanent breakpoint.
4201 - When continuing from a location with an ordinary breakpoint, we
4202 actually single step once before calling insert_breakpoints.
4203 - When continuing from a location with a permanent breakpoint, we
4204 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4205 the target, to advance the PC past the breakpoint. */
4206
4207 enum breakpoint_here
4208 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4209 {
4210 bool any_breakpoint_here = false;
4211
4212 for (bp_location *bl : all_bp_locations ())
4213 {
4214 if (bl->loc_type != bp_loc_software_breakpoint
4215 && bl->loc_type != bp_loc_hardware_breakpoint)
4216 continue;
4217
4218 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4219 if ((breakpoint_enabled (bl->owner)
4220 || bl->permanent)
4221 && breakpoint_location_address_match (bl, aspace, pc))
4222 {
4223 if (overlay_debugging
4224 && section_is_overlay (bl->section)
4225 && !section_is_mapped (bl->section))
4226 continue; /* unmapped overlay -- can't be a match */
4227 else if (bl->permanent)
4228 return permanent_breakpoint_here;
4229 else
4230 any_breakpoint_here = true;
4231 }
4232 }
4233
4234 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4235 }
4236
4237 /* See breakpoint.h. */
4238
4239 int
4240 breakpoint_in_range_p (const address_space *aspace,
4241 CORE_ADDR addr, ULONGEST len)
4242 {
4243 for (bp_location *bl : all_bp_locations ())
4244 {
4245 if (bl->loc_type != bp_loc_software_breakpoint
4246 && bl->loc_type != bp_loc_hardware_breakpoint)
4247 continue;
4248
4249 if ((breakpoint_enabled (bl->owner)
4250 || bl->permanent)
4251 && breakpoint_location_address_range_overlap (bl, aspace,
4252 addr, len))
4253 {
4254 if (overlay_debugging
4255 && section_is_overlay (bl->section)
4256 && !section_is_mapped (bl->section))
4257 {
4258 /* Unmapped overlay -- can't be a match. */
4259 continue;
4260 }
4261
4262 return 1;
4263 }
4264 }
4265
4266 return 0;
4267 }
4268
4269 /* Return true if there's a moribund breakpoint at PC. */
4270
4271 int
4272 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4273 {
4274 for (bp_location *loc : moribund_locations)
4275 if (breakpoint_location_address_match (loc, aspace, pc))
4276 return 1;
4277
4278 return 0;
4279 }
4280
4281 /* Returns true iff BL is inserted at PC, in address space ASPACE. */
4282
4283 static bool
4284 bp_location_inserted_here_p (const struct bp_location *bl,
4285 const address_space *aspace, CORE_ADDR pc)
4286 {
4287 if (bl->inserted
4288 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4289 aspace, pc))
4290 {
4291 /* An unmapped overlay can't be a match. */
4292 return !(overlay_debugging
4293 && section_is_overlay (bl->section)
4294 && !section_is_mapped (bl->section));
4295 }
4296 return false;
4297 }
4298
4299 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4300
4301 int
4302 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4303 {
4304 for (bp_location *bl : all_bp_locations_at_addr (pc))
4305 {
4306 if (bl->loc_type != bp_loc_software_breakpoint
4307 && bl->loc_type != bp_loc_hardware_breakpoint)
4308 continue;
4309
4310 if (bp_location_inserted_here_p (bl, aspace, pc))
4311 return 1;
4312 }
4313 return 0;
4314 }
4315
4316 /* This function returns non-zero iff there is a software breakpoint
4317 inserted at PC. */
4318
4319 int
4320 software_breakpoint_inserted_here_p (const address_space *aspace,
4321 CORE_ADDR pc)
4322 {
4323 for (bp_location *bl : all_bp_locations_at_addr (pc))
4324 {
4325 if (bl->loc_type != bp_loc_software_breakpoint)
4326 continue;
4327
4328 if (bp_location_inserted_here_p (bl, aspace, pc))
4329 return 1;
4330 }
4331
4332 return 0;
4333 }
4334
4335 /* See breakpoint.h. */
4336
4337 int
4338 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4339 CORE_ADDR pc)
4340 {
4341 for (bp_location *bl : all_bp_locations_at_addr (pc))
4342 {
4343 if (bl->loc_type != bp_loc_hardware_breakpoint)
4344 continue;
4345
4346 if (bp_location_inserted_here_p (bl, aspace, pc))
4347 return 1;
4348 }
4349
4350 return 0;
4351 }
4352
4353 int
4354 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4355 CORE_ADDR addr, ULONGEST len)
4356 {
4357 for (breakpoint *bpt : all_breakpoints ())
4358 {
4359 if (bpt->type != bp_hardware_watchpoint
4360 && bpt->type != bp_access_watchpoint)
4361 continue;
4362
4363 if (!breakpoint_enabled (bpt))
4364 continue;
4365
4366 for (bp_location *loc : bpt->locations ())
4367 if (loc->pspace->aspace == aspace && loc->inserted)
4368 {
4369 CORE_ADDR l, h;
4370
4371 /* Check for intersection. */
4372 l = std::max<CORE_ADDR> (loc->address, addr);
4373 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4374 if (l < h)
4375 return 1;
4376 }
4377 }
4378 return 0;
4379 }
4380
4381 /* See breakpoint.h. */
4382
4383 bool
4384 is_catchpoint (struct breakpoint *b)
4385 {
4386 return (b->type == bp_catchpoint);
4387 }
4388
4389 /* Clear a bpstat so that it says we are not at any breakpoint.
4390 Also free any storage that is part of a bpstat. */
4391
4392 void
4393 bpstat_clear (bpstat **bsp)
4394 {
4395 bpstat *p;
4396 bpstat *q;
4397
4398 if (bsp == 0)
4399 return;
4400 p = *bsp;
4401 while (p != NULL)
4402 {
4403 q = p->next;
4404 delete p;
4405 p = q;
4406 }
4407 *bsp = NULL;
4408 }
4409
4410 bpstat::bpstat (const bpstat &other)
4411 : next (NULL),
4412 bp_location_at (other.bp_location_at),
4413 breakpoint_at (other.breakpoint_at),
4414 commands (other.commands),
4415 print (other.print),
4416 stop (other.stop),
4417 print_it (other.print_it)
4418 {
4419 if (other.old_val != NULL)
4420 old_val = release_value (other.old_val->copy ());
4421 }
4422
4423 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4424 is part of the bpstat is copied as well. */
4425
4426 bpstat *
4427 bpstat_copy (bpstat *bs)
4428 {
4429 bpstat *p = nullptr;
4430 bpstat *tmp;
4431 bpstat *retval = nullptr;
4432
4433 if (bs == NULL)
4434 return bs;
4435
4436 for (; bs != NULL; bs = bs->next)
4437 {
4438 tmp = new bpstat (*bs);
4439
4440 if (p == NULL)
4441 /* This is the first thing in the chain. */
4442 retval = tmp;
4443 else
4444 p->next = tmp;
4445 p = tmp;
4446 }
4447 p->next = NULL;
4448 return retval;
4449 }
4450
4451 /* Find the bpstat associated with this breakpoint. */
4452
4453 bpstat *
4454 bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
4455 {
4456 if (bsp == NULL)
4457 return NULL;
4458
4459 for (; bsp != NULL; bsp = bsp->next)
4460 {
4461 if (bsp->breakpoint_at == breakpoint)
4462 return bsp;
4463 }
4464 return NULL;
4465 }
4466
4467 /* See breakpoint.h. */
4468
4469 bool
4470 bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
4471 {
4472 for (; bsp != NULL; bsp = bsp->next)
4473 {
4474 if (bsp->breakpoint_at == NULL)
4475 {
4476 /* A moribund location can never explain a signal other than
4477 GDB_SIGNAL_TRAP. */
4478 if (sig == GDB_SIGNAL_TRAP)
4479 return true;
4480 }
4481 else
4482 {
4483 if (bsp->breakpoint_at->explains_signal (sig))
4484 return true;
4485 }
4486 }
4487
4488 return false;
4489 }
4490
4491 /* See breakpoint.h. */
4492
4493 int
4494 bpstat_num (bpstat **bsp, int *num)
4495 {
4496 struct breakpoint *b;
4497
4498 if ((*bsp) == NULL)
4499 return 0; /* No more breakpoint values */
4500
4501 /* We assume we'll never have several bpstats that correspond to a
4502 single breakpoint -- otherwise, this function might return the
4503 same number more than once and this will look ugly. */
4504 b = (*bsp)->breakpoint_at;
4505 *bsp = (*bsp)->next;
4506 if (b == NULL)
4507 return -1; /* breakpoint that's been deleted since */
4508
4509 *num = b->number; /* We have its number */
4510 return 1;
4511 }
4512
4513 /* See breakpoint.h */
4514
4515 int
4516 bpstat_locno (const bpstat *bs)
4517 {
4518 const struct breakpoint *b = bs->breakpoint_at;
4519 const struct bp_location *bl = bs->bp_location_at.get ();
4520
4521 int locno = 0;
4522
4523 if (b != nullptr && b->loc != nullptr && b->loc->next != nullptr)
4524 {
4525 const bp_location *bl_i;
4526
4527 for (bl_i = b->loc;
4528 bl_i != bl && bl_i->next != nullptr;
4529 bl_i = bl_i->next)
4530 locno++;
4531
4532 if (bl_i == bl)
4533 locno++;
4534 else
4535 {
4536 warning (_("location number not found for breakpoint %d address %s."),
4537 b->number, paddress (bl->gdbarch, bl->address));
4538 locno = 0;
4539 }
4540 }
4541
4542 return locno;
4543 }
4544
4545 /* See breakpoint.h. */
4546
4547 void
4548 print_num_locno (const bpstat *bs, struct ui_out *uiout)
4549 {
4550 struct breakpoint *b = bs->breakpoint_at;
4551
4552 if (b == nullptr)
4553 uiout->text (_("deleted breakpoint"));
4554 else
4555 {
4556 uiout->field_signed ("bkptno", b->number);
4557
4558 int locno = bpstat_locno (bs);
4559 if (locno != 0)
4560 uiout->message (".%pF", signed_field ("locno", locno));
4561 }
4562 }
4563
4564 /* See breakpoint.h. */
4565
4566 void
4567 bpstat_clear_actions (void)
4568 {
4569 bpstat *bs;
4570
4571 if (inferior_ptid == null_ptid)
4572 return;
4573
4574 thread_info *tp = inferior_thread ();
4575 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4576 {
4577 bs->commands = NULL;
4578 bs->old_val.reset (nullptr);
4579 }
4580 }
4581
4582 /* Called when a command is about to proceed the inferior. */
4583
4584 static void
4585 breakpoint_about_to_proceed (void)
4586 {
4587 if (inferior_ptid != null_ptid)
4588 {
4589 struct thread_info *tp = inferior_thread ();
4590
4591 /* Allow inferior function calls in breakpoint commands to not
4592 interrupt the command list. When the call finishes
4593 successfully, the inferior will be standing at the same
4594 breakpoint as if nothing happened. */
4595 if (tp->control.in_infcall)
4596 return;
4597 }
4598
4599 breakpoint_proceeded = 1;
4600 }
4601
4602 /* Return true iff CMD as the first line of a command sequence is `silent'
4603 or its equivalent. */
4604
4605 static bool
4606 command_line_is_silent (struct command_line *cmd)
4607 {
4608 return cmd && (strcmp ("silent", cmd->line) == 0);
4609 }
4610
4611 /* Sets the $_hit_bpnum and $_hit_locno to bpnum and locno.
4612 A locno 0 is changed to 1 to e.g. let the user do
4613 (gdb) disable $_hit_bpnum.$_hit_locno
4614 for a single location breakpoint. */
4615
4616 static void
4617 set_hit_convenience_vars (int bpnum, int locno)
4618 {
4619 set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), bpnum);
4620 set_internalvar_integer (lookup_internalvar ("_hit_locno"),
4621 (locno > 0 ? locno : 1));
4622 }
4623
4624 /* Execute all the commands associated with all the breakpoints at
4625 this location. Any of these commands could cause the process to
4626 proceed beyond this point, etc. We look out for such changes by
4627 checking the global "breakpoint_proceeded" after each command.
4628
4629 Returns true if a breakpoint command resumed the inferior. In that
4630 case, it is the caller's responsibility to recall it again with the
4631 bpstat of the current thread. */
4632
4633 static bool
4634 bpstat_do_actions_1 (bpstat **bsp)
4635 {
4636 bpstat *bs;
4637 bool again = false;
4638
4639 /* Avoid endless recursion if a `source' command is contained
4640 in bs->commands. */
4641 if (executing_breakpoint_commands)
4642 return false;
4643
4644 scoped_restore save_executing
4645 = make_scoped_restore (&executing_breakpoint_commands, 1);
4646
4647 scoped_restore preventer = prevent_dont_repeat ();
4648
4649 /* This pointer will iterate over the list of bpstat's. */
4650 bs = *bsp;
4651
4652 /* The $_hit_* convenience variables are set before running the
4653 commands of BS. In case we have several bs, after the loop,
4654 we set again the variables to the first printed bpnum and locno.
4655 For multiple breakpoints, this ensures the variables are set to the
4656 breakpoint printed for the user. */
4657 int printed_hit_bpnum = -1;
4658 int printed_hit_locno = -1;
4659
4660 breakpoint_proceeded = 0;
4661 for (; bs != NULL; bs = bs->next)
4662 {
4663 struct command_line *cmd = NULL;
4664
4665 /* Set the _hit_* convenience variables before running BS's commands. */
4666 {
4667 const struct breakpoint *b = bs->breakpoint_at;
4668 if (b != nullptr)
4669 {
4670 int locno = bpstat_locno (bs);
4671
4672 set_hit_convenience_vars (b->number, locno);
4673 if (printed_hit_locno == -1 && bs->print)
4674 {
4675 printed_hit_bpnum = b->number;
4676 printed_hit_locno = locno;
4677 }
4678 }
4679 }
4680
4681 /* Take ownership of the BSP's command tree, if it has one.
4682
4683 The command tree could legitimately contain commands like
4684 'step' and 'next', which call clear_proceed_status, which
4685 frees the bpstat BS and its command tree. To make sure this doesn't
4686 free the tree we're executing out from under us, we need to
4687 take ownership of the tree ourselves. Since a given bpstat's
4688 commands are only executed once, we don't need to copy it; we
4689 can clear the pointer in the bpstat, and make sure we free
4690 the tree when we're done. */
4691 counted_command_line ccmd = bs->commands;
4692 bs->commands = NULL;
4693 if (ccmd != NULL)
4694 cmd = ccmd.get ();
4695 if (command_line_is_silent (cmd))
4696 {
4697 /* The action has been already done by bpstat_stop_status. */
4698 cmd = cmd->next;
4699 }
4700
4701 while (cmd != NULL)
4702 {
4703 execute_control_command (cmd);
4704 /* After execute_control_command, if breakpoint_proceeded is true,
4705 BS has been freed and cannot be accessed anymore. */
4706
4707 if (breakpoint_proceeded)
4708 break;
4709 else
4710 cmd = cmd->next;
4711 }
4712
4713 if (breakpoint_proceeded)
4714 {
4715 if (current_ui->async)
4716 /* If we are in async mode, then the target might be still
4717 running, not stopped at any breakpoint, so nothing for
4718 us to do here -- just return to the event loop. */
4719 ;
4720 else
4721 /* In sync mode, when execute_control_command returns
4722 we're already standing on the next breakpoint.
4723 Breakpoint commands for that stop were not run, since
4724 execute_command does not run breakpoint commands --
4725 only command_line_handler does, but that one is not
4726 involved in execution of breakpoint commands. So, we
4727 can now execute breakpoint commands. It should be
4728 noted that making execute_command do bpstat actions is
4729 not an option -- in this case we'll have recursive
4730 invocation of bpstat for each breakpoint with a
4731 command, and can easily blow up GDB stack. Instead, we
4732 return true, which will trigger the caller to recall us
4733 with the new stop_bpstat. */
4734 again = true;
4735 break;
4736 }
4737 }
4738
4739 /* Now that we have executed the commands of all bs, set the _hit_*
4740 convenience variables to the printed values. */
4741 if (printed_hit_locno != -1)
4742 set_hit_convenience_vars (printed_hit_bpnum, printed_hit_locno);
4743
4744 return again;
4745 }
4746
4747 /* Helper for bpstat_do_actions. Get the current thread, if there's
4748 one, is alive and has execution. Return NULL otherwise. */
4749
4750 static thread_info *
4751 get_bpstat_thread ()
4752 {
4753 if (inferior_ptid == null_ptid || !target_has_execution ())
4754 return NULL;
4755
4756 thread_info *tp = inferior_thread ();
4757 if (tp->state == THREAD_EXITED || tp->executing ())
4758 return NULL;
4759 return tp;
4760 }
4761
4762 void
4763 bpstat_do_actions (void)
4764 {
4765 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4766 thread_info *tp;
4767
4768 /* Do any commands attached to breakpoint we are stopped at. */
4769 while ((tp = get_bpstat_thread ()) != NULL)
4770 {
4771 /* Since in sync mode, bpstat_do_actions may resume the
4772 inferior, and only return when it is stopped at the next
4773 breakpoint, we keep doing breakpoint actions until it returns
4774 false to indicate the inferior was not resumed. */
4775 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4776 break;
4777 }
4778
4779 cleanup_if_error.release ();
4780 }
4781
4782 /* Print out the (old or new) value associated with a watchpoint. */
4783
4784 static void
4785 watchpoint_value_print (struct value *val, struct ui_file *stream)
4786 {
4787 if (val == NULL)
4788 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4789 else
4790 {
4791 struct value_print_options opts;
4792 get_user_print_options (&opts);
4793 value_print (val, stream, &opts);
4794 }
4795 }
4796
4797 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4798 debugging multiple threads. */
4799
4800 void
4801 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4802 {
4803 if (uiout->is_mi_like_p ())
4804 return;
4805
4806 uiout->text ("\n");
4807
4808 if (show_thread_that_caused_stop ())
4809 {
4810 struct thread_info *thr = inferior_thread ();
4811
4812 uiout->text ("Thread ");
4813 uiout->field_string ("thread-id", print_thread_id (thr));
4814
4815 const char *name = thread_name (thr);
4816 if (name != NULL)
4817 {
4818 uiout->text (" \"");
4819 uiout->field_string ("name", name);
4820 uiout->text ("\"");
4821 }
4822
4823 uiout->text (" hit ");
4824 }
4825 }
4826
4827 /* Generic routine for printing messages indicating why we
4828 stopped. The behavior of this function depends on the value
4829 'print_it' in the bpstat structure. Under some circumstances we
4830 may decide not to print anything here and delegate the task to
4831 normal_stop(). */
4832
4833 static enum print_stop_action
4834 print_bp_stop_message (bpstat *bs)
4835 {
4836 switch (bs->print_it)
4837 {
4838 case print_it_noop:
4839 /* Nothing should be printed for this bpstat entry. */
4840 return PRINT_UNKNOWN;
4841 break;
4842
4843 case print_it_done:
4844 /* We still want to print the frame, but we already printed the
4845 relevant messages. */
4846 return PRINT_SRC_AND_LOC;
4847 break;
4848
4849 case print_it_normal:
4850 {
4851 struct breakpoint *b = bs->breakpoint_at;
4852
4853 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4854 which has since been deleted. */
4855 if (b == NULL)
4856 return PRINT_UNKNOWN;
4857
4858 /* Normal case. Call the breakpoint's print_it method. */
4859 return b->print_it (bs);
4860 }
4861 break;
4862
4863 default:
4864 internal_error (_("print_bp_stop_message: unrecognized enum value"));
4865 break;
4866 }
4867 }
4868
4869 /* See breakpoint.h. */
4870
4871 void
4872 print_solib_event (bool is_catchpoint)
4873 {
4874 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4875 bool any_added = !current_program_space->added_solibs.empty ();
4876
4877 if (!is_catchpoint)
4878 {
4879 if (any_added || any_deleted)
4880 current_uiout->text (_("Stopped due to shared library event:\n"));
4881 else
4882 current_uiout->text (_("Stopped due to shared library event (no "
4883 "libraries added or removed)\n"));
4884 }
4885
4886 if (current_uiout->is_mi_like_p ())
4887 current_uiout->field_string ("reason",
4888 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4889
4890 if (any_deleted)
4891 {
4892 current_uiout->text (_(" Inferior unloaded "));
4893 ui_out_emit_list list_emitter (current_uiout, "removed");
4894 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4895 {
4896 const std::string &name = current_program_space->deleted_solibs[ix];
4897
4898 if (ix > 0)
4899 current_uiout->text (" ");
4900 current_uiout->field_string ("library", name);
4901 current_uiout->text ("\n");
4902 }
4903 }
4904
4905 if (any_added)
4906 {
4907 current_uiout->text (_(" Inferior loaded "));
4908 ui_out_emit_list list_emitter (current_uiout, "added");
4909 bool first = true;
4910 for (so_list *iter : current_program_space->added_solibs)
4911 {
4912 if (!first)
4913 current_uiout->text (" ");
4914 first = false;
4915 current_uiout->field_string ("library", iter->so_name);
4916 current_uiout->text ("\n");
4917 }
4918 }
4919 }
4920
4921 /* Print a message indicating what happened. This is called from
4922 normal_stop(). The input to this routine is the head of the bpstat
4923 list - a list of the eventpoints that caused this stop. KIND is
4924 the target_waitkind for the stopping event. This
4925 routine calls the generic print routine for printing a message
4926 about reasons for stopping. This will print (for example) the
4927 "Breakpoint n," part of the output. The return value of this
4928 routine is one of:
4929
4930 PRINT_UNKNOWN: Means we printed nothing.
4931 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4932 code to print the location. An example is
4933 "Breakpoint 1, " which should be followed by
4934 the location.
4935 PRINT_SRC_ONLY: Means we printed something, but there is no need
4936 to also print the location part of the message.
4937 An example is the catch/throw messages, which
4938 don't require a location appended to the end.
4939 PRINT_NOTHING: We have done some printing and we don't need any
4940 further info to be printed. */
4941
4942 enum print_stop_action
4943 bpstat_print (bpstat *bs, target_waitkind kind)
4944 {
4945 enum print_stop_action val;
4946
4947 /* Maybe another breakpoint in the chain caused us to stop.
4948 (Currently all watchpoints go on the bpstat whether hit or not.
4949 That probably could (should) be changed, provided care is taken
4950 with respect to bpstat_explains_signal). */
4951 for (; bs; bs = bs->next)
4952 {
4953 val = print_bp_stop_message (bs);
4954 if (val == PRINT_SRC_ONLY
4955 || val == PRINT_SRC_AND_LOC
4956 || val == PRINT_NOTHING)
4957 return val;
4958 }
4959
4960 /* If we had hit a shared library event breakpoint,
4961 print_bp_stop_message would print out this message. If we hit an
4962 OS-level shared library event, do the same thing. */
4963 if (kind == TARGET_WAITKIND_LOADED)
4964 {
4965 print_solib_event (false);
4966 return PRINT_NOTHING;
4967 }
4968
4969 /* We reached the end of the chain, or we got a null BS to start
4970 with and nothing was printed. */
4971 return PRINT_UNKNOWN;
4972 }
4973
4974 /* Evaluate the boolean expression EXP and return the result. */
4975
4976 static bool
4977 breakpoint_cond_eval (expression *exp)
4978 {
4979 scoped_value_mark mark;
4980 return value_true (exp->evaluate ());
4981 }
4982
4983 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4984
4985 bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
4986 : next (NULL),
4987 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4988 breakpoint_at (bl->owner),
4989 commands (NULL),
4990 print (0),
4991 stop (0),
4992 print_it (print_it_normal)
4993 {
4994 **bs_link_pointer = this;
4995 *bs_link_pointer = &next;
4996 }
4997
4998 bpstat::bpstat ()
4999 : next (NULL),
5000 breakpoint_at (NULL),
5001 commands (NULL),
5002 print (0),
5003 stop (0),
5004 print_it (print_it_normal)
5005 {
5006 }
5007 \f
5008 /* The target has stopped with waitstatus WS. Check if any hardware
5009 watchpoints have triggered, according to the target. */
5010
5011 int
5012 watchpoints_triggered (const target_waitstatus &ws)
5013 {
5014 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
5015 CORE_ADDR addr;
5016
5017 if (!stopped_by_watchpoint)
5018 {
5019 /* We were not stopped by a watchpoint. Mark all watchpoints
5020 as not triggered. */
5021 for (breakpoint *b : all_breakpoints ())
5022 if (is_hardware_watchpoint (b))
5023 {
5024 struct watchpoint *w = (struct watchpoint *) b;
5025
5026 w->watchpoint_triggered = watch_triggered_no;
5027 }
5028
5029 return 0;
5030 }
5031
5032 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
5033 {
5034 /* We were stopped by a watchpoint, but we don't know where.
5035 Mark all watchpoints as unknown. */
5036 for (breakpoint *b : all_breakpoints ())
5037 if (is_hardware_watchpoint (b))
5038 {
5039 struct watchpoint *w = (struct watchpoint *) b;
5040
5041 w->watchpoint_triggered = watch_triggered_unknown;
5042 }
5043
5044 return 1;
5045 }
5046
5047 /* The target could report the data address. Mark watchpoints
5048 affected by this data address as triggered, and all others as not
5049 triggered. */
5050
5051 for (breakpoint *b : all_breakpoints ())
5052 if (is_hardware_watchpoint (b))
5053 {
5054 struct watchpoint *w = (struct watchpoint *) b;
5055
5056 w->watchpoint_triggered = watch_triggered_no;
5057 for (bp_location *loc : b->locations ())
5058 {
5059 if (is_masked_watchpoint (b))
5060 {
5061 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5062 CORE_ADDR start = loc->address & w->hw_wp_mask;
5063
5064 if (newaddr == start)
5065 {
5066 w->watchpoint_triggered = watch_triggered_yes;
5067 break;
5068 }
5069 }
5070 /* Exact match not required. Within range is sufficient. */
5071 else if (target_watchpoint_addr_within_range
5072 (current_inferior ()->top_target (), addr, loc->address,
5073 loc->length))
5074 {
5075 w->watchpoint_triggered = watch_triggered_yes;
5076 break;
5077 }
5078 }
5079 }
5080
5081 return 1;
5082 }
5083
5084 /* Possible return values for watchpoint_check. */
5085 enum wp_check_result
5086 {
5087 /* The watchpoint has been deleted. */
5088 WP_DELETED = 1,
5089
5090 /* The value has changed. */
5091 WP_VALUE_CHANGED = 2,
5092
5093 /* The value has not changed. */
5094 WP_VALUE_NOT_CHANGED = 3,
5095
5096 /* Ignore this watchpoint, no matter if the value changed or not. */
5097 WP_IGNORE = 4,
5098 };
5099
5100 #define BP_TEMPFLAG 1
5101 #define BP_HARDWAREFLAG 2
5102
5103 /* Evaluate watchpoint condition expression and check if its value
5104 changed. */
5105
5106 static wp_check_result
5107 watchpoint_check (bpstat *bs)
5108 {
5109 struct watchpoint *b;
5110 frame_info_ptr fr;
5111 bool within_current_scope;
5112
5113 /* BS is built from an existing struct breakpoint. */
5114 gdb_assert (bs->breakpoint_at != NULL);
5115 b = (struct watchpoint *) bs->breakpoint_at;
5116
5117 /* If this is a local watchpoint, we only want to check if the
5118 watchpoint frame is in scope if the current thread is the thread
5119 that was used to create the watchpoint. */
5120 if (!watchpoint_in_thread_scope (b))
5121 return WP_IGNORE;
5122
5123 if (b->exp_valid_block == NULL)
5124 within_current_scope = true;
5125 else
5126 {
5127 frame_info_ptr frame = get_current_frame ();
5128 struct gdbarch *frame_arch = get_frame_arch (frame);
5129 CORE_ADDR frame_pc = get_frame_pc (frame);
5130
5131 /* stack_frame_destroyed_p() returns a non-zero value if we're
5132 still in the function but the stack frame has already been
5133 invalidated. Since we can't rely on the values of local
5134 variables after the stack has been destroyed, we are treating
5135 the watchpoint in that state as `not changed' without further
5136 checking. Don't mark watchpoints as changed if the current
5137 frame is in an epilogue - even if they are in some other
5138 frame, our view of the stack is likely to be wrong and
5139 frame_find_by_id could error out. */
5140 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5141 return WP_IGNORE;
5142
5143 fr = frame_find_by_id (b->watchpoint_frame);
5144 within_current_scope = (fr != NULL);
5145
5146 /* If we've gotten confused in the unwinder, we might have
5147 returned a frame that can't describe this variable. */
5148 if (within_current_scope)
5149 {
5150 struct symbol *function;
5151
5152 function = get_frame_function (fr);
5153 if (function == NULL
5154 || !function->value_block ()->contains (b->exp_valid_block))
5155 within_current_scope = false;
5156 }
5157
5158 if (within_current_scope)
5159 /* If we end up stopping, the current frame will get selected
5160 in normal_stop. So this call to select_frame won't affect
5161 the user. */
5162 select_frame (fr);
5163 }
5164
5165 if (within_current_scope)
5166 {
5167 /* We use value_{,free_to_}mark because it could be a *long*
5168 time before we return to the command level and call
5169 free_all_values. We can't call free_all_values because we
5170 might be in the middle of evaluating a function call. */
5171
5172 struct value *mark;
5173 struct value *new_val;
5174
5175 if (is_masked_watchpoint (b))
5176 /* Since we don't know the exact trigger address (from
5177 stopped_data_address), just tell the user we've triggered
5178 a mask watchpoint. */
5179 return WP_VALUE_CHANGED;
5180
5181 mark = value_mark ();
5182 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5183 NULL, NULL, false);
5184
5185 if (b->val_bitsize != 0)
5186 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5187
5188 /* We use value_equal_contents instead of value_equal because
5189 the latter coerces an array to a pointer, thus comparing just
5190 the address of the array instead of its contents. This is
5191 not what we want. */
5192 if ((b->val != NULL) != (new_val != NULL)
5193 || (b->val != NULL && !value_equal_contents (b->val.get (),
5194 new_val)))
5195 {
5196 bs->old_val = b->val;
5197 b->val = release_value (new_val);
5198 b->val_valid = true;
5199 if (new_val != NULL)
5200 value_free_to_mark (mark);
5201 return WP_VALUE_CHANGED;
5202 }
5203 else
5204 {
5205 /* Nothing changed. */
5206 value_free_to_mark (mark);
5207 return WP_VALUE_NOT_CHANGED;
5208 }
5209 }
5210 else
5211 {
5212 /* This seems like the only logical thing to do because
5213 if we temporarily ignored the watchpoint, then when
5214 we reenter the block in which it is valid it contains
5215 garbage (in the case of a function, it may have two
5216 garbage values, one before and one after the prologue).
5217 So we can't even detect the first assignment to it and
5218 watch after that (since the garbage may or may not equal
5219 the first value assigned). */
5220 /* We print all the stop information in
5221 breakpointprint_it, but in this case, by the time we
5222 call breakpoint->print_it this bp will be deleted
5223 already. So we have no choice but print the information
5224 here. */
5225
5226 SWITCH_THRU_ALL_UIS ()
5227 {
5228 struct ui_out *uiout = current_uiout;
5229
5230 if (uiout->is_mi_like_p ())
5231 uiout->field_string
5232 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5233 uiout->message ("\nWatchpoint %pF deleted because the program has "
5234 "left the block in\n"
5235 "which its expression is valid.\n",
5236 signed_field ("wpnum", b->number));
5237 }
5238
5239 /* Make sure the watchpoint's commands aren't executed. */
5240 b->commands = NULL;
5241 watchpoint_del_at_next_stop (b);
5242
5243 return WP_DELETED;
5244 }
5245 }
5246
5247 /* Return true if it looks like target has stopped due to hitting
5248 breakpoint location BL. This function does not check if we should
5249 stop, only if BL explains the stop. */
5250
5251 static bool
5252 bpstat_check_location (const struct bp_location *bl,
5253 const address_space *aspace, CORE_ADDR bp_addr,
5254 const target_waitstatus &ws)
5255 {
5256 struct breakpoint *b = bl->owner;
5257
5258 /* BL is from an existing breakpoint. */
5259 gdb_assert (b != NULL);
5260
5261 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
5262 }
5263
5264 /* Determine if the watched values have actually changed, and we
5265 should stop. If not, set BS->stop to false. */
5266
5267 static void
5268 bpstat_check_watchpoint (bpstat *bs)
5269 {
5270 const struct bp_location *bl;
5271 struct watchpoint *b;
5272
5273 /* BS is built for existing struct breakpoint. */
5274 bl = bs->bp_location_at.get ();
5275 gdb_assert (bl != NULL);
5276 b = (struct watchpoint *) bs->breakpoint_at;
5277 gdb_assert (b != NULL);
5278
5279 {
5280 bool must_check_value = false;
5281
5282 if (b->type == bp_watchpoint)
5283 /* For a software watchpoint, we must always check the
5284 watched value. */
5285 must_check_value = true;
5286 else if (b->watchpoint_triggered == watch_triggered_yes)
5287 /* We have a hardware watchpoint (read, write, or access)
5288 and the target earlier reported an address watched by
5289 this watchpoint. */
5290 must_check_value = true;
5291 else if (b->watchpoint_triggered == watch_triggered_unknown
5292 && b->type == bp_hardware_watchpoint)
5293 /* We were stopped by a hardware watchpoint, but the target could
5294 not report the data address. We must check the watchpoint's
5295 value. Access and read watchpoints are out of luck; without
5296 a data address, we can't figure it out. */
5297 must_check_value = true;
5298
5299 if (must_check_value)
5300 {
5301 wp_check_result e;
5302
5303 try
5304 {
5305 e = watchpoint_check (bs);
5306 }
5307 catch (const gdb_exception_error &ex)
5308 {
5309 exception_fprintf (gdb_stderr, ex,
5310 "Error evaluating expression "
5311 "for watchpoint %d\n",
5312 b->number);
5313
5314 SWITCH_THRU_ALL_UIS ()
5315 {
5316 gdb_printf (_("Watchpoint %d deleted.\n"),
5317 b->number);
5318 }
5319 watchpoint_del_at_next_stop (b);
5320 e = WP_DELETED;
5321 }
5322
5323 switch (e)
5324 {
5325 case WP_DELETED:
5326 /* We've already printed what needs to be printed. */
5327 bs->print_it = print_it_done;
5328 /* Stop. */
5329 break;
5330 case WP_IGNORE:
5331 bs->print_it = print_it_noop;
5332 bs->stop = false;
5333 break;
5334 case WP_VALUE_CHANGED:
5335 if (b->type == bp_read_watchpoint)
5336 {
5337 /* There are two cases to consider here:
5338
5339 1. We're watching the triggered memory for reads.
5340 In that case, trust the target, and always report
5341 the watchpoint hit to the user. Even though
5342 reads don't cause value changes, the value may
5343 have changed since the last time it was read, and
5344 since we're not trapping writes, we will not see
5345 those, and as such we should ignore our notion of
5346 old value.
5347
5348 2. We're watching the triggered memory for both
5349 reads and writes. There are two ways this may
5350 happen:
5351
5352 2.1. This is a target that can't break on data
5353 reads only, but can break on accesses (reads or
5354 writes), such as e.g., x86. We detect this case
5355 at the time we try to insert read watchpoints.
5356
5357 2.2. Otherwise, the target supports read
5358 watchpoints, but, the user set an access or write
5359 watchpoint watching the same memory as this read
5360 watchpoint.
5361
5362 If we're watching memory writes as well as reads,
5363 ignore watchpoint hits when we find that the
5364 value hasn't changed, as reads don't cause
5365 changes. This still gives false positives when
5366 the program writes the same value to memory as
5367 what there was already in memory (we will confuse
5368 it for a read), but it's much better than
5369 nothing. */
5370
5371 int other_write_watchpoint = 0;
5372
5373 if (bl->watchpoint_type == hw_read)
5374 {
5375 for (breakpoint *other_b : all_breakpoints ())
5376 if (other_b->type == bp_hardware_watchpoint
5377 || other_b->type == bp_access_watchpoint)
5378 {
5379 struct watchpoint *other_w =
5380 (struct watchpoint *) other_b;
5381
5382 if (other_w->watchpoint_triggered
5383 == watch_triggered_yes)
5384 {
5385 other_write_watchpoint = 1;
5386 break;
5387 }
5388 }
5389 }
5390
5391 if (other_write_watchpoint
5392 || bl->watchpoint_type == hw_access)
5393 {
5394 /* We're watching the same memory for writes,
5395 and the value changed since the last time we
5396 updated it, so this trap must be for a write.
5397 Ignore it. */
5398 bs->print_it = print_it_noop;
5399 bs->stop = false;
5400 }
5401 }
5402 break;
5403 case WP_VALUE_NOT_CHANGED:
5404 if (b->type == bp_hardware_watchpoint
5405 || b->type == bp_watchpoint)
5406 {
5407 /* Don't stop: write watchpoints shouldn't fire if
5408 the value hasn't changed. */
5409 bs->print_it = print_it_noop;
5410 bs->stop = false;
5411 }
5412 /* Stop. */
5413 break;
5414 default:
5415 /* Can't happen. */
5416 break;
5417 }
5418 }
5419 else /* !must_check_value */
5420 {
5421 /* This is a case where some watchpoint(s) triggered, but
5422 not at the address of this watchpoint, or else no
5423 watchpoint triggered after all. So don't print
5424 anything for this watchpoint. */
5425 bs->print_it = print_it_noop;
5426 bs->stop = false;
5427 }
5428 }
5429 }
5430
5431 /* For breakpoints that are currently marked as telling gdb to stop,
5432 check conditions (condition proper, frame, thread and ignore count)
5433 of breakpoint referred to by BS. If we should not stop for this
5434 breakpoint, set BS->stop to 0. */
5435
5436 static void
5437 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5438 {
5439 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
5440
5441 const struct bp_location *bl;
5442 struct breakpoint *b;
5443 /* Assume stop. */
5444 bool condition_result = true;
5445 struct expression *cond;
5446
5447 gdb_assert (bs->stop);
5448
5449 /* BS is built for existing struct breakpoint. */
5450 bl = bs->bp_location_at.get ();
5451 gdb_assert (bl != NULL);
5452 b = bs->breakpoint_at;
5453 gdb_assert (b != NULL);
5454
5455 infrun_debug_printf ("thread = %s, breakpoint %d.%d",
5456 thread->ptid.to_string ().c_str (),
5457 b->number, find_loc_num_by_location (bl));
5458
5459 /* Even if the target evaluated the condition on its end and notified GDB, we
5460 need to do so again since GDB does not know if we stopped due to a
5461 breakpoint or a single step breakpoint. */
5462
5463 if (frame_id_p (b->frame_id)
5464 && b->frame_id != get_stack_frame_id (get_current_frame ()))
5465 {
5466 infrun_debug_printf ("incorrect frame %s not %s, not stopping",
5467 get_stack_frame_id (get_current_frame ()).to_string ().c_str (),
5468 b->frame_id.to_string ().c_str ());
5469 bs->stop = false;
5470 return;
5471 }
5472
5473 /* If this is a thread/task-specific breakpoint, don't waste cpu
5474 evaluating the condition if this isn't the specified
5475 thread/task. */
5476 if ((b->thread != -1 && b->thread != thread->global_num)
5477 || (b->task != -1 && b->task != ada_get_task_number (thread)))
5478 {
5479 infrun_debug_printf ("incorrect thread or task, not stopping");
5480 bs->stop = false;
5481 return;
5482 }
5483
5484 /* Evaluate extension language breakpoints that have a "stop" method
5485 implemented. */
5486 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5487
5488 if (is_watchpoint (b))
5489 {
5490 struct watchpoint *w = (struct watchpoint *) b;
5491
5492 cond = w->cond_exp.get ();
5493 }
5494 else
5495 cond = bl->cond.get ();
5496
5497 if (cond != nullptr && b->disposition != disp_del_at_next_stop)
5498 {
5499 bool within_current_scope = true;
5500 struct watchpoint * w;
5501
5502 /* We use scoped_value_mark because it could be a long time
5503 before we return to the command level and call
5504 free_all_values. We can't call free_all_values because we
5505 might be in the middle of evaluating a function call. */
5506 scoped_value_mark mark;
5507
5508 if (is_watchpoint (b))
5509 w = (struct watchpoint *) b;
5510 else
5511 w = NULL;
5512
5513 /* Need to select the frame, with all that implies so that
5514 the conditions will have the right context. Because we
5515 use the frame, we will not see an inlined function's
5516 variables when we arrive at a breakpoint at the start
5517 of the inlined function; the current frame will be the
5518 call site. */
5519 if (w == NULL || w->cond_exp_valid_block == NULL)
5520 select_frame (get_current_frame ());
5521 else
5522 {
5523 frame_info_ptr frame;
5524
5525 /* For local watchpoint expressions, which particular
5526 instance of a local is being watched matters, so we
5527 keep track of the frame to evaluate the expression
5528 in. To evaluate the condition however, it doesn't
5529 really matter which instantiation of the function
5530 where the condition makes sense triggers the
5531 watchpoint. This allows an expression like "watch
5532 global if q > 10" set in `func', catch writes to
5533 global on all threads that call `func', or catch
5534 writes on all recursive calls of `func' by a single
5535 thread. We simply always evaluate the condition in
5536 the innermost frame that's executing where it makes
5537 sense to evaluate the condition. It seems
5538 intuitive. */
5539 frame = block_innermost_frame (w->cond_exp_valid_block);
5540 if (frame != NULL)
5541 select_frame (frame);
5542 else
5543 within_current_scope = false;
5544 }
5545 CORE_ADDR pc_before_check = get_frame_pc (get_selected_frame (nullptr));
5546 if (within_current_scope)
5547 {
5548 try
5549 {
5550 condition_result = breakpoint_cond_eval (cond);
5551 }
5552 catch (const gdb_exception_error &ex)
5553 {
5554 exception_fprintf (gdb_stderr, ex,
5555 "Error in testing condition for breakpoint %d:\n",
5556 b->number);
5557
5558 /* If the pc value changed as a result of evaluating the
5559 condition then we probably stopped within an inferior
5560 function call due to some unexpected stop, e.g. the thread
5561 hit another breakpoint, or the thread received an
5562 unexpected signal. In this case we don't want to also
5563 print the information about this breakpoint. */
5564 CORE_ADDR pc_after_check
5565 = get_frame_pc (get_selected_frame (nullptr));
5566 if (pc_before_check != pc_after_check)
5567 bs->print = 0;
5568 }
5569 }
5570 else
5571 {
5572 warning (_("Watchpoint condition cannot be tested "
5573 "in the current scope"));
5574 /* If we failed to set the right context for this
5575 watchpoint, unconditionally report it. */
5576 }
5577 /* FIXME-someday, should give breakpoint #. */
5578 }
5579
5580 if (cond != nullptr && !condition_result)
5581 {
5582 infrun_debug_printf ("condition_result = false, not stopping");
5583 bs->stop = false;
5584 return;
5585 }
5586 else if (b->ignore_count > 0)
5587 {
5588 infrun_debug_printf ("ignore count %d, not stopping",
5589 b->ignore_count);
5590 b->ignore_count--;
5591 bs->stop = false;
5592 /* Increase the hit count even though we don't stop. */
5593 ++(b->hit_count);
5594 gdb::observers::breakpoint_modified.notify (b);
5595 return;
5596 }
5597
5598 if (bs->stop)
5599 infrun_debug_printf ("stopping at this breakpoint");
5600 else
5601 infrun_debug_printf ("not stopping at this breakpoint");
5602 }
5603
5604 /* Returns true if we need to track moribund locations of LOC's type
5605 on the current target. */
5606
5607 static bool
5608 need_moribund_for_location_type (const struct bp_location *loc)
5609 {
5610 return ((loc->loc_type == bp_loc_software_breakpoint
5611 && !target_supports_stopped_by_sw_breakpoint ())
5612 || (loc->loc_type == bp_loc_hardware_breakpoint
5613 && !target_supports_stopped_by_hw_breakpoint ()));
5614 }
5615
5616 /* See breakpoint.h. */
5617
5618 bpstat *
5619 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5620 const target_waitstatus &ws)
5621 {
5622 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5623
5624 for (breakpoint *b : all_breakpoints ())
5625 {
5626 if (!breakpoint_enabled (b))
5627 continue;
5628
5629 for (bp_location *bl : b->locations ())
5630 {
5631 /* For hardware watchpoints, we look only at the first
5632 location. The watchpoint_check function will work on the
5633 entire expression, not the individual locations. For
5634 read watchpoints, the watchpoints_triggered function has
5635 checked all locations already. */
5636 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5637 break;
5638
5639 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5640 continue;
5641
5642 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5643 continue;
5644
5645 /* Come here if it's a watchpoint, or if the break address
5646 matches. */
5647
5648 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
5649 explain stop. */
5650
5651 /* Assume we stop. Should we find a watchpoint that is not
5652 actually triggered, or if the condition of the breakpoint
5653 evaluates as false, we'll reset 'stop' to 0. */
5654 bs->stop = true;
5655 bs->print = true;
5656
5657 /* If this is a scope breakpoint, mark the associated
5658 watchpoint as triggered so that we will handle the
5659 out-of-scope event. We'll get to the watchpoint next
5660 iteration. */
5661 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5662 {
5663 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5664
5665 w->watchpoint_triggered = watch_triggered_yes;
5666 }
5667 }
5668 }
5669
5670 /* Check if a moribund breakpoint explains the stop. */
5671 if (!target_supports_stopped_by_sw_breakpoint ()
5672 || !target_supports_stopped_by_hw_breakpoint ())
5673 {
5674 for (bp_location *loc : moribund_locations)
5675 {
5676 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5677 && need_moribund_for_location_type (loc))
5678 {
5679 bpstat *bs = new bpstat (loc, &bs_link);
5680 /* For hits of moribund locations, we should just proceed. */
5681 bs->stop = false;
5682 bs->print = false;
5683 bs->print_it = print_it_noop;
5684 }
5685 }
5686 }
5687
5688 return bs_head;
5689 }
5690
5691 /* See breakpoint.h. */
5692
5693 bpstat *
5694 bpstat_stop_status (const address_space *aspace,
5695 CORE_ADDR bp_addr, thread_info *thread,
5696 const target_waitstatus &ws,
5697 bpstat *stop_chain)
5698 {
5699 struct breakpoint *b = NULL;
5700 /* First item of allocated bpstat's. */
5701 bpstat *bs_head = stop_chain;
5702 bpstat *bs;
5703 int need_remove_insert;
5704 int removed_any;
5705
5706 /* First, build the bpstat chain with locations that explain a
5707 target stop, while being careful to not set the target running,
5708 as that may invalidate locations (in particular watchpoint
5709 locations are recreated). Resuming will happen here with
5710 breakpoint conditions or watchpoint expressions that include
5711 inferior function calls. */
5712 if (bs_head == NULL)
5713 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5714
5715 /* A bit of special processing for shlib breakpoints. We need to
5716 process solib loading here, so that the lists of loaded and
5717 unloaded libraries are correct before we handle "catch load" and
5718 "catch unload". */
5719 for (bs = bs_head; bs != NULL; bs = bs->next)
5720 {
5721 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5722 {
5723 handle_solib_event ();
5724 break;
5725 }
5726 }
5727
5728 /* Now go through the locations that caused the target to stop, and
5729 check whether we're interested in reporting this stop to higher
5730 layers, or whether we should resume the target transparently. */
5731
5732 removed_any = 0;
5733
5734 for (bs = bs_head; bs != NULL; bs = bs->next)
5735 {
5736 if (!bs->stop)
5737 continue;
5738
5739 b = bs->breakpoint_at;
5740 b->check_status (bs);
5741 if (bs->stop)
5742 {
5743 bpstat_check_breakpoint_conditions (bs, thread);
5744
5745 if (bs->stop)
5746 {
5747 ++(b->hit_count);
5748
5749 /* We will stop here. */
5750 if (b->disposition == disp_disable)
5751 {
5752 --(b->enable_count);
5753 if (b->enable_count <= 0)
5754 b->enable_state = bp_disabled;
5755 removed_any = 1;
5756 }
5757 gdb::observers::breakpoint_modified.notify (b);
5758 if (b->silent)
5759 bs->print = false;
5760 bs->commands = b->commands;
5761 if (command_line_is_silent (bs->commands
5762 ? bs->commands.get () : NULL))
5763 bs->print = false;
5764
5765 b->after_condition_true (bs);
5766 }
5767
5768 }
5769
5770 /* Print nothing for this entry if we don't stop or don't
5771 print. */
5772 if (!bs->stop || !bs->print)
5773 bs->print_it = print_it_noop;
5774 }
5775
5776 /* If we aren't stopping, the value of some hardware watchpoint may
5777 not have changed, but the intermediate memory locations we are
5778 watching may have. Don't bother if we're stopping; this will get
5779 done later. */
5780 need_remove_insert = 0;
5781 if (! bpstat_causes_stop (bs_head))
5782 for (bs = bs_head; bs != NULL; bs = bs->next)
5783 if (!bs->stop
5784 && bs->breakpoint_at
5785 && is_hardware_watchpoint (bs->breakpoint_at))
5786 {
5787 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5788
5789 update_watchpoint (w, false /* don't reparse. */);
5790 need_remove_insert = 1;
5791 }
5792
5793 if (need_remove_insert)
5794 update_global_location_list (UGLL_MAY_INSERT);
5795 else if (removed_any)
5796 update_global_location_list (UGLL_DONT_INSERT);
5797
5798 return bs_head;
5799 }
5800
5801 /* See breakpoint.h. */
5802
5803 bpstat *
5804 bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5805 thread_info *thread, const target_waitstatus &ws)
5806 {
5807 gdb_assert (!target_stopped_by_watchpoint ());
5808
5809 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5810 previous stop to avoid confusing bpstat_stop_status. */
5811 watchpoints_triggered (ws);
5812
5813 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5814 }
5815
5816 static void
5817 handle_jit_event (CORE_ADDR address)
5818 {
5819 struct gdbarch *gdbarch;
5820
5821 infrun_debug_printf ("handling bp_jit_event");
5822
5823 /* Switch terminal for any messages produced by
5824 breakpoint_re_set. */
5825 target_terminal::ours_for_output ();
5826
5827 gdbarch = get_frame_arch (get_current_frame ());
5828 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5829 thus it is expected that its objectfile can be found through
5830 minimal symbol lookup. If it doesn't work (and assert fails), it
5831 most likely means that `jit_breakpoint_re_set` was changes and this
5832 function needs to be updated too. */
5833 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5834 gdb_assert (jit_bp_sym.objfile != nullptr);
5835 objfile *objfile = jit_bp_sym.objfile;
5836 if (objfile->separate_debug_objfile_backlink)
5837 objfile = objfile->separate_debug_objfile_backlink;
5838 jit_event_handler (gdbarch, objfile);
5839
5840 target_terminal::inferior ();
5841 }
5842
5843 /* Prepare WHAT final decision for infrun. */
5844
5845 /* Decide what infrun needs to do with this bpstat. */
5846
5847 struct bpstat_what
5848 bpstat_what (bpstat *bs_head)
5849 {
5850 struct bpstat_what retval;
5851 bpstat *bs;
5852
5853 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5854 retval.call_dummy = STOP_NONE;
5855 retval.is_longjmp = false;
5856
5857 for (bs = bs_head; bs != NULL; bs = bs->next)
5858 {
5859 /* Extract this BS's action. After processing each BS, we check
5860 if its action overrides all we've seem so far. */
5861 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5862 enum bptype bptype;
5863
5864 if (bs->breakpoint_at == NULL)
5865 {
5866 /* I suspect this can happen if it was a momentary
5867 breakpoint which has since been deleted. */
5868 bptype = bp_none;
5869 }
5870 else
5871 bptype = bs->breakpoint_at->type;
5872
5873 switch (bptype)
5874 {
5875 case bp_none:
5876 break;
5877 case bp_breakpoint:
5878 case bp_hardware_breakpoint:
5879 case bp_single_step:
5880 case bp_until:
5881 case bp_finish:
5882 case bp_shlib_event:
5883 if (bs->stop)
5884 {
5885 if (bs->print)
5886 this_action = BPSTAT_WHAT_STOP_NOISY;
5887 else
5888 this_action = BPSTAT_WHAT_STOP_SILENT;
5889 }
5890 else
5891 this_action = BPSTAT_WHAT_SINGLE;
5892 break;
5893 case bp_watchpoint:
5894 case bp_hardware_watchpoint:
5895 case bp_read_watchpoint:
5896 case bp_access_watchpoint:
5897 if (bs->stop)
5898 {
5899 if (bs->print)
5900 this_action = BPSTAT_WHAT_STOP_NOISY;
5901 else
5902 this_action = BPSTAT_WHAT_STOP_SILENT;
5903 }
5904 else
5905 {
5906 /* There was a watchpoint, but we're not stopping.
5907 This requires no further action. */
5908 }
5909 break;
5910 case bp_longjmp:
5911 case bp_longjmp_call_dummy:
5912 case bp_exception:
5913 if (bs->stop)
5914 {
5915 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5916 retval.is_longjmp = bptype != bp_exception;
5917 }
5918 else
5919 this_action = BPSTAT_WHAT_SINGLE;
5920 break;
5921 case bp_longjmp_resume:
5922 case bp_exception_resume:
5923 if (bs->stop)
5924 {
5925 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5926 retval.is_longjmp = bptype == bp_longjmp_resume;
5927 }
5928 else
5929 this_action = BPSTAT_WHAT_SINGLE;
5930 break;
5931 case bp_step_resume:
5932 if (bs->stop)
5933 this_action = BPSTAT_WHAT_STEP_RESUME;
5934 else
5935 {
5936 /* It is for the wrong frame. */
5937 this_action = BPSTAT_WHAT_SINGLE;
5938 }
5939 break;
5940 case bp_hp_step_resume:
5941 if (bs->stop)
5942 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5943 else
5944 {
5945 /* It is for the wrong frame. */
5946 this_action = BPSTAT_WHAT_SINGLE;
5947 }
5948 break;
5949 case bp_watchpoint_scope:
5950 case bp_thread_event:
5951 case bp_overlay_event:
5952 case bp_longjmp_master:
5953 case bp_std_terminate_master:
5954 case bp_exception_master:
5955 this_action = BPSTAT_WHAT_SINGLE;
5956 break;
5957 case bp_catchpoint:
5958 if (bs->stop)
5959 {
5960 if (bs->print)
5961 this_action = BPSTAT_WHAT_STOP_NOISY;
5962 else
5963 this_action = BPSTAT_WHAT_STOP_SILENT;
5964 }
5965 else
5966 {
5967 /* Some catchpoints are implemented with breakpoints.
5968 For those, we need to step over the breakpoint. */
5969 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
5970 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
5971 this_action = BPSTAT_WHAT_SINGLE;
5972 }
5973 break;
5974 case bp_jit_event:
5975 this_action = BPSTAT_WHAT_SINGLE;
5976 break;
5977 case bp_call_dummy:
5978 /* Make sure the action is stop (silent or noisy),
5979 so infrun.c pops the dummy frame. */
5980 retval.call_dummy = STOP_STACK_DUMMY;
5981 this_action = BPSTAT_WHAT_STOP_SILENT;
5982 break;
5983 case bp_std_terminate:
5984 /* Make sure the action is stop (silent or noisy),
5985 so infrun.c pops the dummy frame. */
5986 retval.call_dummy = STOP_STD_TERMINATE;
5987 this_action = BPSTAT_WHAT_STOP_SILENT;
5988 break;
5989 case bp_tracepoint:
5990 case bp_fast_tracepoint:
5991 case bp_static_tracepoint:
5992 case bp_static_marker_tracepoint:
5993 /* Tracepoint hits should not be reported back to GDB, and
5994 if one got through somehow, it should have been filtered
5995 out already. */
5996 internal_error (_("bpstat_what: tracepoint encountered"));
5997 break;
5998 case bp_gnu_ifunc_resolver:
5999 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
6000 this_action = BPSTAT_WHAT_SINGLE;
6001 break;
6002 case bp_gnu_ifunc_resolver_return:
6003 /* The breakpoint will be removed, execution will restart from the
6004 PC of the former breakpoint. */
6005 this_action = BPSTAT_WHAT_KEEP_CHECKING;
6006 break;
6007
6008 case bp_dprintf:
6009 if (bs->stop)
6010 this_action = BPSTAT_WHAT_STOP_SILENT;
6011 else
6012 this_action = BPSTAT_WHAT_SINGLE;
6013 break;
6014
6015 default:
6016 internal_error (_("bpstat_what: unhandled bptype %d"), (int) bptype);
6017 }
6018
6019 retval.main_action = std::max (retval.main_action, this_action);
6020 }
6021
6022 return retval;
6023 }
6024
6025 void
6026 bpstat_run_callbacks (bpstat *bs_head)
6027 {
6028 bpstat *bs;
6029
6030 for (bs = bs_head; bs != NULL; bs = bs->next)
6031 {
6032 struct breakpoint *b = bs->breakpoint_at;
6033
6034 if (b == NULL)
6035 continue;
6036 switch (b->type)
6037 {
6038 case bp_jit_event:
6039 handle_jit_event (bs->bp_location_at->address);
6040 break;
6041 case bp_gnu_ifunc_resolver:
6042 gnu_ifunc_resolver_stop ((code_breakpoint *) b);
6043 break;
6044 case bp_gnu_ifunc_resolver_return:
6045 gnu_ifunc_resolver_return_stop ((code_breakpoint *) b);
6046 break;
6047 }
6048 }
6049 }
6050
6051 /* See breakpoint.h. */
6052
6053 bool
6054 bpstat_should_step ()
6055 {
6056 for (breakpoint *b : all_breakpoints ())
6057 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6058 return true;
6059
6060 return false;
6061 }
6062
6063 /* See breakpoint.h. */
6064
6065 bool
6066 bpstat_causes_stop (bpstat *bs)
6067 {
6068 for (; bs != NULL; bs = bs->next)
6069 if (bs->stop)
6070 return true;
6071
6072 return false;
6073 }
6074
6075 \f
6076
6077 /* Compute a number of spaces suitable to indent the next line
6078 so it starts at the position corresponding to the table column
6079 named COL_NAME in the currently active table of UIOUT. */
6080
6081 static int
6082 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6083 {
6084 int i, total_width, width, align;
6085 const char *text;
6086
6087 total_width = 0;
6088 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
6089 {
6090 if (strcmp (text, col_name) == 0)
6091 return total_width;
6092
6093 total_width += width + 1;
6094 }
6095
6096 return 0;
6097 }
6098
6099 /* Determine if the locations of this breakpoint will have their conditions
6100 evaluated by the target, host or a mix of both. Returns the following:
6101
6102 "host": Host evals condition.
6103 "host or target": Host or Target evals condition.
6104 "target": Target evals condition.
6105 */
6106
6107 static const char *
6108 bp_condition_evaluator (const breakpoint *b)
6109 {
6110 char host_evals = 0;
6111 char target_evals = 0;
6112
6113 if (!b)
6114 return NULL;
6115
6116 if (!is_breakpoint (b))
6117 return NULL;
6118
6119 if (gdb_evaluates_breakpoint_condition_p ()
6120 || !target_supports_evaluation_of_breakpoint_conditions ())
6121 return condition_evaluation_host;
6122
6123 for (bp_location *bl : b->locations ())
6124 {
6125 if (bl->cond_bytecode)
6126 target_evals++;
6127 else
6128 host_evals++;
6129 }
6130
6131 if (host_evals && target_evals)
6132 return condition_evaluation_both;
6133 else if (target_evals)
6134 return condition_evaluation_target;
6135 else
6136 return condition_evaluation_host;
6137 }
6138
6139 /* Determine the breakpoint location's condition evaluator. This is
6140 similar to bp_condition_evaluator, but for locations. */
6141
6142 static const char *
6143 bp_location_condition_evaluator (const struct bp_location *bl)
6144 {
6145 if (bl && !is_breakpoint (bl->owner))
6146 return NULL;
6147
6148 if (gdb_evaluates_breakpoint_condition_p ()
6149 || !target_supports_evaluation_of_breakpoint_conditions ())
6150 return condition_evaluation_host;
6151
6152 if (bl && bl->cond_bytecode)
6153 return condition_evaluation_target;
6154 else
6155 return condition_evaluation_host;
6156 }
6157
6158 /* Print the LOC location out of the list of B->LOC locations. */
6159
6160 static void
6161 print_breakpoint_location (const breakpoint *b,
6162 struct bp_location *loc)
6163 {
6164 struct ui_out *uiout = current_uiout;
6165
6166 scoped_restore_current_program_space restore_pspace;
6167
6168 if (loc != NULL && loc->shlib_disabled)
6169 loc = NULL;
6170
6171 if (loc != NULL)
6172 set_current_program_space (loc->pspace);
6173
6174 if (b->display_canonical)
6175 uiout->field_string ("what", b->locspec->to_string ());
6176 else if (loc && loc->symtab)
6177 {
6178 const struct symbol *sym = loc->symbol;
6179
6180 if (sym)
6181 {
6182 uiout->text ("in ");
6183 uiout->field_string ("func", sym->print_name (),
6184 function_name_style.style ());
6185 uiout->text (" ");
6186 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6187 uiout->text ("at ");
6188 }
6189 uiout->field_string ("file",
6190 symtab_to_filename_for_display (loc->symtab),
6191 file_name_style.style ());
6192 uiout->text (":");
6193
6194 if (uiout->is_mi_like_p ())
6195 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6196
6197 uiout->field_signed ("line", loc->line_number);
6198 }
6199 else if (loc)
6200 {
6201 string_file stb;
6202
6203 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6204 demangle, "");
6205 uiout->field_stream ("at", stb);
6206 }
6207 else
6208 {
6209 uiout->field_string ("pending", b->locspec->to_string ());
6210 /* If extra_string is available, it could be holding a condition
6211 or dprintf arguments. In either case, make sure it is printed,
6212 too, but only for non-MI streams. */
6213 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6214 {
6215 if (b->type == bp_dprintf)
6216 uiout->text (",");
6217 else
6218 uiout->text (" ");
6219 uiout->text (b->extra_string.get ());
6220 }
6221 }
6222
6223 if (loc && is_breakpoint (b)
6224 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6225 && bp_condition_evaluator (b) == condition_evaluation_both)
6226 {
6227 uiout->text (" (");
6228 uiout->field_string ("evaluated-by",
6229 bp_location_condition_evaluator (loc));
6230 uiout->text (")");
6231 }
6232 }
6233
6234 static const char *
6235 bptype_string (enum bptype type)
6236 {
6237 struct ep_type_description
6238 {
6239 enum bptype type;
6240 const char *description;
6241 };
6242 static struct ep_type_description bptypes[] =
6243 {
6244 {bp_none, "?deleted?"},
6245 {bp_breakpoint, "breakpoint"},
6246 {bp_hardware_breakpoint, "hw breakpoint"},
6247 {bp_single_step, "sw single-step"},
6248 {bp_until, "until"},
6249 {bp_finish, "finish"},
6250 {bp_watchpoint, "watchpoint"},
6251 {bp_hardware_watchpoint, "hw watchpoint"},
6252 {bp_read_watchpoint, "read watchpoint"},
6253 {bp_access_watchpoint, "acc watchpoint"},
6254 {bp_longjmp, "longjmp"},
6255 {bp_longjmp_resume, "longjmp resume"},
6256 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6257 {bp_exception, "exception"},
6258 {bp_exception_resume, "exception resume"},
6259 {bp_step_resume, "step resume"},
6260 {bp_hp_step_resume, "high-priority step resume"},
6261 {bp_watchpoint_scope, "watchpoint scope"},
6262 {bp_call_dummy, "call dummy"},
6263 {bp_std_terminate, "std::terminate"},
6264 {bp_shlib_event, "shlib events"},
6265 {bp_thread_event, "thread events"},
6266 {bp_overlay_event, "overlay events"},
6267 {bp_longjmp_master, "longjmp master"},
6268 {bp_std_terminate_master, "std::terminate master"},
6269 {bp_exception_master, "exception master"},
6270 {bp_catchpoint, "catchpoint"},
6271 {bp_tracepoint, "tracepoint"},
6272 {bp_fast_tracepoint, "fast tracepoint"},
6273 {bp_static_tracepoint, "static tracepoint"},
6274 {bp_static_marker_tracepoint, "static marker tracepoint"},
6275 {bp_dprintf, "dprintf"},
6276 {bp_jit_event, "jit events"},
6277 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6278 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6279 };
6280
6281 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6282 || ((int) type != bptypes[(int) type].type))
6283 internal_error (_("bptypes table does not describe type #%d."),
6284 (int) type);
6285
6286 return bptypes[(int) type].description;
6287 }
6288
6289 /* For MI, output a field named 'thread-groups' with a list as the value.
6290 For CLI, prefix the list with the string 'inf'. */
6291
6292 static void
6293 output_thread_groups (struct ui_out *uiout,
6294 const char *field_name,
6295 const std::vector<int> &inf_nums,
6296 int mi_only)
6297 {
6298 int is_mi = uiout->is_mi_like_p ();
6299
6300 /* For backward compatibility, don't display inferiors in CLI unless
6301 there are several. Always display them for MI. */
6302 if (!is_mi && mi_only)
6303 return;
6304
6305 ui_out_emit_list list_emitter (uiout, field_name);
6306
6307 for (size_t i = 0; i < inf_nums.size (); i++)
6308 {
6309 if (is_mi)
6310 {
6311 char mi_group[10];
6312
6313 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6314 uiout->field_string (NULL, mi_group);
6315 }
6316 else
6317 {
6318 if (i == 0)
6319 uiout->text (" inf ");
6320 else
6321 uiout->text (", ");
6322
6323 uiout->text (plongest (inf_nums[i]));
6324 }
6325 }
6326 }
6327
6328 /* See breakpoint.h. */
6329
6330 bool fix_breakpoint_script_output_globally = false;
6331
6332 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6333 instead of going via breakpoint_ops::print_one. This makes "maint
6334 info breakpoints" show the software breakpoint locations of
6335 catchpoints, which are considered internal implementation
6336 detail. Returns true if RAW_LOC is false and if the breakpoint's
6337 print_one method did something; false otherwise. */
6338
6339 static bool
6340 print_one_breakpoint_location (struct breakpoint *b,
6341 struct bp_location *loc,
6342 int loc_number,
6343 struct bp_location **last_loc,
6344 int allflag, bool raw_loc)
6345 {
6346 struct command_line *l;
6347 static char bpenables[] = "nynny";
6348
6349 struct ui_out *uiout = current_uiout;
6350 bool header_of_multiple = false;
6351 bool part_of_multiple = (loc != NULL);
6352 struct value_print_options opts;
6353
6354 get_user_print_options (&opts);
6355
6356 gdb_assert (!loc || loc_number != 0);
6357 /* See comment in print_one_breakpoint concerning treatment of
6358 breakpoints with single disabled location. */
6359 if (loc == NULL
6360 && (b->loc != NULL
6361 && (b->loc->next != NULL
6362 || !b->loc->enabled || b->loc->disabled_by_cond)))
6363 header_of_multiple = true;
6364 if (loc == NULL)
6365 loc = b->loc;
6366
6367 annotate_record ();
6368
6369 /* 1 */
6370 annotate_field (0);
6371 if (part_of_multiple)
6372 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6373 else
6374 uiout->field_signed ("number", b->number);
6375
6376 /* 2 */
6377 annotate_field (1);
6378 if (part_of_multiple)
6379 uiout->field_skip ("type");
6380 else
6381 uiout->field_string ("type", bptype_string (b->type));
6382
6383 /* 3 */
6384 annotate_field (2);
6385 if (part_of_multiple)
6386 uiout->field_skip ("disp");
6387 else
6388 uiout->field_string ("disp", bpdisp_text (b->disposition));
6389
6390 /* 4 */
6391 annotate_field (3);
6392 if (part_of_multiple)
6393 {
6394 /* For locations that are disabled because of an invalid
6395 condition, display "N*" on the CLI, where "*" refers to a
6396 footnote below the table. For MI, simply display a "N"
6397 without a footnote. On the CLI, for enabled locations whose
6398 breakpoint is disabled, display "y-". */
6399 auto get_enable_state = [uiout, loc] () -> const char *
6400 {
6401 if (uiout->is_mi_like_p ())
6402 {
6403 if (loc->disabled_by_cond)
6404 return "N";
6405 else if (!loc->enabled)
6406 return "n";
6407 else
6408 return "y";
6409 }
6410 else
6411 {
6412 if (loc->disabled_by_cond)
6413 return "N*";
6414 else if (!loc->enabled)
6415 return "n";
6416 else if (!breakpoint_enabled (loc->owner))
6417 return "y-";
6418 else
6419 return "y";
6420 }
6421 };
6422 uiout->field_string ("enabled", get_enable_state ());
6423 }
6424 else
6425 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6426
6427 /* 5 and 6 */
6428 bool result = false;
6429 if (!raw_loc && b->print_one (last_loc))
6430 result = true;
6431 else
6432 {
6433 if (is_watchpoint (b))
6434 {
6435 struct watchpoint *w = (struct watchpoint *) b;
6436
6437 /* Field 4, the address, is omitted (which makes the columns
6438 not line up too nicely with the headers, but the effect
6439 is relatively readable). */
6440 if (opts.addressprint)
6441 uiout->field_skip ("addr");
6442 annotate_field (5);
6443 uiout->field_string ("what", w->exp_string.get ());
6444 }
6445 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6446 || is_ada_exception_catchpoint (b))
6447 {
6448 if (opts.addressprint)
6449 {
6450 annotate_field (4);
6451 if (header_of_multiple)
6452 uiout->field_string ("addr", "<MULTIPLE>",
6453 metadata_style.style ());
6454 else if (b->loc == NULL || loc->shlib_disabled)
6455 uiout->field_string ("addr", "<PENDING>",
6456 metadata_style.style ());
6457 else
6458 uiout->field_core_addr ("addr",
6459 loc->gdbarch, loc->address);
6460 }
6461 annotate_field (5);
6462 if (!header_of_multiple)
6463 print_breakpoint_location (b, loc);
6464 if (b->loc)
6465 *last_loc = b->loc;
6466 }
6467 }
6468
6469 if (loc != NULL && !header_of_multiple)
6470 {
6471 std::vector<int> inf_nums;
6472 int mi_only = 1;
6473
6474 for (inferior *inf : all_inferiors ())
6475 {
6476 if (inf->pspace == loc->pspace)
6477 inf_nums.push_back (inf->num);
6478 }
6479
6480 /* For backward compatibility, don't display inferiors in CLI unless
6481 there are several. Always display for MI. */
6482 if (allflag
6483 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6484 && (program_spaces.size () > 1
6485 || number_of_inferiors () > 1)
6486 /* LOC is for existing B, it cannot be in
6487 moribund_locations and thus having NULL OWNER. */
6488 && loc->owner->type != bp_catchpoint))
6489 mi_only = 0;
6490 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6491 }
6492
6493 /* In the MI output, each location of a thread or task specific
6494 breakpoint includes the relevant thread or task ID. This is done for
6495 backwards compatibility reasons.
6496
6497 For the CLI output, the thread/task information is printed on a
6498 separate line, see the 'stop only in thread' and 'stop only in task'
6499 output below. */
6500 if (part_of_multiple && uiout->is_mi_like_p ())
6501 {
6502 if (b->thread != -1)
6503 uiout->field_signed ("thread", b->thread);
6504 else if (b->task != -1)
6505 uiout->field_signed ("task", b->task);
6506 }
6507
6508 uiout->text ("\n");
6509
6510 if (!part_of_multiple)
6511 b->print_one_detail (uiout);
6512
6513 if (part_of_multiple && frame_id_p (b->frame_id))
6514 {
6515 annotate_field (6);
6516 uiout->text ("\tstop only in stack frame at ");
6517 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6518 the frame ID. */
6519 uiout->field_core_addr ("frame",
6520 b->gdbarch, b->frame_id.stack_addr);
6521 uiout->text ("\n");
6522 }
6523
6524 if (!part_of_multiple && b->cond_string)
6525 {
6526 annotate_field (7);
6527 if (is_tracepoint (b))
6528 uiout->text ("\ttrace only if ");
6529 else
6530 uiout->text ("\tstop only if ");
6531 uiout->field_string ("cond", b->cond_string.get ());
6532
6533 /* Print whether the target is doing the breakpoint's condition
6534 evaluation. If GDB is doing the evaluation, don't print anything. */
6535 if (is_breakpoint (b)
6536 && breakpoint_condition_evaluation_mode ()
6537 == condition_evaluation_target)
6538 {
6539 uiout->message (" (%pF evals)",
6540 string_field ("evaluated-by",
6541 bp_condition_evaluator (b)));
6542 }
6543 uiout->text ("\n");
6544 }
6545
6546 if (!part_of_multiple && b->thread != -1)
6547 {
6548 /* FIXME should make an annotation for this. */
6549 uiout->text ("\tstop only in thread ");
6550 if (uiout->is_mi_like_p ())
6551 uiout->field_signed ("thread", b->thread);
6552 else
6553 {
6554 struct thread_info *thr = find_thread_global_id (b->thread);
6555
6556 uiout->field_string ("thread", print_thread_id (thr));
6557 }
6558 uiout->text ("\n");
6559 }
6560
6561 if (!part_of_multiple && b->task != -1)
6562 {
6563 uiout->text ("\tstop only in task ");
6564 uiout->field_signed ("task", b->task);
6565 uiout->text ("\n");
6566 }
6567
6568 if (!part_of_multiple)
6569 {
6570 if (b->hit_count)
6571 {
6572 /* FIXME should make an annotation for this. */
6573 if (is_catchpoint (b))
6574 uiout->text ("\tcatchpoint");
6575 else if (is_tracepoint (b))
6576 uiout->text ("\ttracepoint");
6577 else
6578 uiout->text ("\tbreakpoint");
6579 uiout->text (" already hit ");
6580 uiout->field_signed ("times", b->hit_count);
6581 if (b->hit_count == 1)
6582 uiout->text (" time\n");
6583 else
6584 uiout->text (" times\n");
6585 }
6586 else
6587 {
6588 /* Output the count also if it is zero, but only if this is mi. */
6589 if (uiout->is_mi_like_p ())
6590 uiout->field_signed ("times", b->hit_count);
6591 }
6592 }
6593
6594 if (!part_of_multiple && b->ignore_count)
6595 {
6596 annotate_field (8);
6597 uiout->message ("\tignore next %pF hits\n",
6598 signed_field ("ignore", b->ignore_count));
6599 }
6600
6601 /* Note that an enable count of 1 corresponds to "enable once"
6602 behavior, which is reported by the combination of enablement and
6603 disposition, so we don't need to mention it here. */
6604 if (!part_of_multiple && b->enable_count > 1)
6605 {
6606 annotate_field (8);
6607 uiout->text ("\tdisable after ");
6608 /* Tweak the wording to clarify that ignore and enable counts
6609 are distinct, and have additive effect. */
6610 if (b->ignore_count)
6611 uiout->text ("additional ");
6612 else
6613 uiout->text ("next ");
6614 uiout->field_signed ("enable", b->enable_count);
6615 uiout->text (" hits\n");
6616 }
6617
6618 if (!part_of_multiple && is_tracepoint (b))
6619 {
6620 struct tracepoint *tp = (struct tracepoint *) b;
6621
6622 if (tp->traceframe_usage)
6623 {
6624 uiout->text ("\ttrace buffer usage ");
6625 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6626 uiout->text (" bytes\n");
6627 }
6628 }
6629
6630 l = b->commands ? b->commands.get () : NULL;
6631 if (!part_of_multiple && l)
6632 {
6633 annotate_field (9);
6634
6635 bool use_fixed_output =
6636 (uiout->test_flags (fix_breakpoint_script_output)
6637 || fix_breakpoint_script_output_globally);
6638
6639 gdb::optional<ui_out_emit_tuple> tuple_emitter;
6640 gdb::optional<ui_out_emit_list> list_emitter;
6641
6642 if (use_fixed_output)
6643 list_emitter.emplace (uiout, "script");
6644 else
6645 tuple_emitter.emplace (uiout, "script");
6646
6647 print_command_lines (uiout, l, 4);
6648 }
6649
6650 if (is_tracepoint (b))
6651 {
6652 struct tracepoint *t = (struct tracepoint *) b;
6653
6654 if (!part_of_multiple && t->pass_count)
6655 {
6656 annotate_field (10);
6657 uiout->text ("\tpass count ");
6658 uiout->field_signed ("pass", t->pass_count);
6659 uiout->text (" \n");
6660 }
6661
6662 /* Don't display it when tracepoint or tracepoint location is
6663 pending. */
6664 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6665 {
6666 annotate_field (11);
6667
6668 if (uiout->is_mi_like_p ())
6669 uiout->field_string ("installed",
6670 loc->inserted ? "y" : "n");
6671 else
6672 {
6673 if (loc->inserted)
6674 uiout->text ("\t");
6675 else
6676 uiout->text ("\tnot ");
6677 uiout->text ("installed on target\n");
6678 }
6679 }
6680 }
6681
6682 if (uiout->is_mi_like_p () && !part_of_multiple)
6683 {
6684 if (is_watchpoint (b))
6685 {
6686 struct watchpoint *w = (struct watchpoint *) b;
6687
6688 uiout->field_string ("original-location", w->exp_string.get ());
6689 }
6690 else if (b->locspec != nullptr)
6691 {
6692 const char *str = b->locspec->to_string ();
6693 if (str != nullptr)
6694 uiout->field_string ("original-location", str);
6695 }
6696 }
6697
6698 return result;
6699 }
6700
6701 /* See breakpoint.h. */
6702
6703 bool fix_multi_location_breakpoint_output_globally = false;
6704
6705 static void
6706 print_one_breakpoint (struct breakpoint *b,
6707 struct bp_location **last_loc,
6708 int allflag)
6709 {
6710 struct ui_out *uiout = current_uiout;
6711 bool use_fixed_output
6712 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6713 || fix_multi_location_breakpoint_output_globally);
6714
6715 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6716 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6717 allflag, false);
6718
6719 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6720 are outside. */
6721 if (!use_fixed_output)
6722 bkpt_tuple_emitter.reset ();
6723
6724 /* If this breakpoint has custom print function,
6725 it's already printed. Otherwise, print individual
6726 locations, if any. */
6727 if (!printed || allflag)
6728 {
6729 /* If breakpoint has a single location that is disabled, we
6730 print it as if it had several locations, since otherwise it's
6731 hard to represent "breakpoint enabled, location disabled"
6732 situation.
6733
6734 Note that while hardware watchpoints have several locations
6735 internally, that's not a property exposed to users.
6736
6737 Likewise, while catchpoints may be implemented with
6738 breakpoints (e.g., catch throw), that's not a property
6739 exposed to users. We do however display the internal
6740 breakpoint locations with "maint info breakpoints". */
6741 if (!is_hardware_watchpoint (b)
6742 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6743 || is_ada_exception_catchpoint (b))
6744 && (allflag
6745 || (b->loc && (b->loc->next
6746 || !b->loc->enabled
6747 || b->loc->disabled_by_cond))))
6748 {
6749 gdb::optional<ui_out_emit_list> locations_list;
6750
6751 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6752 MI record. For later versions, place breakpoint locations in a
6753 list. */
6754 if (uiout->is_mi_like_p () && use_fixed_output)
6755 locations_list.emplace (uiout, "locations");
6756
6757 int n = 1;
6758 for (bp_location *loc : b->locations ())
6759 {
6760 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6761 print_one_breakpoint_location (b, loc, n, last_loc,
6762 allflag, allflag);
6763 n++;
6764 }
6765 }
6766 }
6767 }
6768
6769 static int
6770 breakpoint_address_bits (struct breakpoint *b)
6771 {
6772 int print_address_bits = 0;
6773
6774 for (bp_location *loc : b->locations ())
6775 {
6776 if (!bl_address_is_meaningful (loc))
6777 continue;
6778
6779 int addr_bit = gdbarch_addr_bit (loc->gdbarch);
6780 if (addr_bit > print_address_bits)
6781 print_address_bits = addr_bit;
6782 }
6783
6784 return print_address_bits;
6785 }
6786
6787 /* See breakpoint.h. */
6788
6789 void
6790 print_breakpoint (breakpoint *b)
6791 {
6792 struct bp_location *dummy_loc = NULL;
6793 print_one_breakpoint (b, &dummy_loc, 0);
6794 }
6795
6796 /* Return true if this breakpoint was set by the user, false if it is
6797 internal or momentary. */
6798
6799 int
6800 user_breakpoint_p (struct breakpoint *b)
6801 {
6802 return b->number > 0;
6803 }
6804
6805 /* See breakpoint.h. */
6806
6807 int
6808 pending_breakpoint_p (struct breakpoint *b)
6809 {
6810 return b->loc == NULL;
6811 }
6812
6813 /* Print information on breakpoints (including watchpoints and tracepoints).
6814
6815 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6816 understood by number_or_range_parser. Only breakpoints included in this
6817 list are then printed.
6818
6819 If SHOW_INTERNAL is true, print internal breakpoints.
6820
6821 If FILTER is non-NULL, call it on each breakpoint and only include the
6822 ones for which it returns true.
6823
6824 Return the total number of breakpoints listed. */
6825
6826 static int
6827 breakpoint_1 (const char *bp_num_list, bool show_internal,
6828 bool (*filter) (const struct breakpoint *))
6829 {
6830 struct bp_location *last_loc = NULL;
6831 int nr_printable_breakpoints;
6832 struct value_print_options opts;
6833 int print_address_bits = 0;
6834 int print_type_col_width = 14;
6835 struct ui_out *uiout = current_uiout;
6836 bool has_disabled_by_cond_location = false;
6837
6838 get_user_print_options (&opts);
6839
6840 /* Compute the number of rows in the table, as well as the size
6841 required for address fields. */
6842 nr_printable_breakpoints = 0;
6843 for (breakpoint *b : all_breakpoints ())
6844 {
6845 /* If we have a filter, only list the breakpoints it accepts. */
6846 if (filter && !filter (b))
6847 continue;
6848
6849 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6850 accept. Skip the others. */
6851 if (bp_num_list != NULL && *bp_num_list != '\0')
6852 {
6853 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6854 continue;
6855 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6856 continue;
6857 }
6858
6859 if (show_internal || user_breakpoint_p (b))
6860 {
6861 int addr_bit, type_len;
6862
6863 addr_bit = breakpoint_address_bits (b);
6864 if (addr_bit > print_address_bits)
6865 print_address_bits = addr_bit;
6866
6867 type_len = strlen (bptype_string (b->type));
6868 if (type_len > print_type_col_width)
6869 print_type_col_width = type_len;
6870
6871 nr_printable_breakpoints++;
6872 }
6873 }
6874
6875 {
6876 ui_out_emit_table table_emitter (uiout,
6877 opts.addressprint ? 6 : 5,
6878 nr_printable_breakpoints,
6879 "BreakpointTable");
6880
6881 if (nr_printable_breakpoints > 0)
6882 annotate_breakpoints_headers ();
6883 if (nr_printable_breakpoints > 0)
6884 annotate_field (0);
6885 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6886 if (nr_printable_breakpoints > 0)
6887 annotate_field (1);
6888 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6889 if (nr_printable_breakpoints > 0)
6890 annotate_field (2);
6891 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6892 if (nr_printable_breakpoints > 0)
6893 annotate_field (3);
6894 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6895 if (opts.addressprint)
6896 {
6897 if (nr_printable_breakpoints > 0)
6898 annotate_field (4);
6899 if (print_address_bits <= 32)
6900 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6901 else
6902 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6903 }
6904 if (nr_printable_breakpoints > 0)
6905 annotate_field (5);
6906 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6907 uiout->table_body ();
6908 if (nr_printable_breakpoints > 0)
6909 annotate_breakpoints_table ();
6910
6911 for (breakpoint *b : all_breakpoints ())
6912 {
6913 QUIT;
6914 /* If we have a filter, only list the breakpoints it accepts. */
6915 if (filter && !filter (b))
6916 continue;
6917
6918 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6919 accept. Skip the others. */
6920
6921 if (bp_num_list != NULL && *bp_num_list != '\0')
6922 {
6923 if (show_internal) /* maintenance info breakpoint */
6924 {
6925 if (parse_and_eval_long (bp_num_list) != b->number)
6926 continue;
6927 }
6928 else /* all others */
6929 {
6930 if (!number_is_in_list (bp_num_list, b->number))
6931 continue;
6932 }
6933 }
6934 /* We only print out user settable breakpoints unless the
6935 show_internal is set. */
6936 if (show_internal || user_breakpoint_p (b))
6937 {
6938 print_one_breakpoint (b, &last_loc, show_internal);
6939 for (bp_location *loc : b->locations ())
6940 if (loc->disabled_by_cond)
6941 has_disabled_by_cond_location = true;
6942 }
6943 }
6944 }
6945
6946 if (nr_printable_breakpoints == 0)
6947 {
6948 /* If there's a filter, let the caller decide how to report
6949 empty list. */
6950 if (!filter)
6951 {
6952 if (bp_num_list == NULL || *bp_num_list == '\0')
6953 uiout->message ("No breakpoints or watchpoints.\n");
6954 else
6955 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6956 bp_num_list);
6957 }
6958 }
6959 else
6960 {
6961 if (last_loc && !server_command)
6962 set_next_address (last_loc->gdbarch, last_loc->address);
6963
6964 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6965 uiout->message (_("(*): Breakpoint condition is invalid at this "
6966 "location.\n"));
6967 }
6968
6969 /* FIXME? Should this be moved up so that it is only called when
6970 there have been breakpoints? */
6971 annotate_breakpoints_table_end ();
6972
6973 return nr_printable_breakpoints;
6974 }
6975
6976 /* Display the value of default-collect in a way that is generally
6977 compatible with the breakpoint list. */
6978
6979 static void
6980 default_collect_info (void)
6981 {
6982 struct ui_out *uiout = current_uiout;
6983
6984 /* If it has no value (which is frequently the case), say nothing; a
6985 message like "No default-collect." gets in user's face when it's
6986 not wanted. */
6987 if (default_collect.empty ())
6988 return;
6989
6990 /* The following phrase lines up nicely with per-tracepoint collect
6991 actions. */
6992 uiout->text ("default collect ");
6993 uiout->field_string ("default-collect", default_collect);
6994 uiout->text (" \n");
6995 }
6996
6997 static void
6998 info_breakpoints_command (const char *args, int from_tty)
6999 {
7000 breakpoint_1 (args, false, NULL);
7001
7002 default_collect_info ();
7003 }
7004
7005 static void
7006 info_watchpoints_command (const char *args, int from_tty)
7007 {
7008 int num_printed = breakpoint_1 (args, false, is_watchpoint);
7009 struct ui_out *uiout = current_uiout;
7010
7011 if (num_printed == 0)
7012 {
7013 if (args == NULL || *args == '\0')
7014 uiout->message ("No watchpoints.\n");
7015 else
7016 uiout->message ("No watchpoint matching '%s'.\n", args);
7017 }
7018 }
7019
7020 static void
7021 maintenance_info_breakpoints (const char *args, int from_tty)
7022 {
7023 breakpoint_1 (args, true, NULL);
7024
7025 default_collect_info ();
7026 }
7027
7028 static bool
7029 breakpoint_has_pc (struct breakpoint *b,
7030 struct program_space *pspace,
7031 CORE_ADDR pc, struct obj_section *section)
7032 {
7033 for (bp_location *bl : b->locations ())
7034 {
7035 if (bl->pspace == pspace
7036 && bl->address == pc
7037 && (!overlay_debugging || bl->section == section))
7038 return true;
7039 }
7040 return false;
7041 }
7042
7043 /* See breakpoint.h. */
7044
7045 void
7046 describe_other_breakpoints (struct gdbarch *gdbarch,
7047 struct program_space *pspace, CORE_ADDR pc,
7048 struct obj_section *section, int thread)
7049 {
7050 int others = 0;
7051
7052 for (breakpoint *b : all_breakpoints ())
7053 others += (user_breakpoint_p (b)
7054 && breakpoint_has_pc (b, pspace, pc, section));
7055
7056 if (others > 0)
7057 {
7058 if (others == 1)
7059 gdb_printf (_("Note: breakpoint "));
7060 else /* if (others == ???) */
7061 gdb_printf (_("Note: breakpoints "));
7062 for (breakpoint *b : all_breakpoints ())
7063 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7064 {
7065 others--;
7066 gdb_printf ("%d", b->number);
7067 if (b->thread == -1 && thread != -1)
7068 gdb_printf (" (all threads)");
7069 else if (b->thread != -1)
7070 {
7071 struct thread_info *thr = find_thread_global_id (b->thread);
7072 gdb_printf (" (thread %s)", print_thread_id (thr));
7073 }
7074 else if (b->task != -1)
7075 gdb_printf (" (task %d)", b->task);
7076 gdb_printf ("%s%s ",
7077 ((b->enable_state == bp_disabled
7078 || b->enable_state == bp_call_disabled)
7079 ? " (disabled)"
7080 : ""),
7081 (others > 1) ? ","
7082 : ((others == 1) ? " and" : ""));
7083 }
7084 current_uiout->message (_("also set at pc %ps.\n"),
7085 styled_string (address_style.style (),
7086 paddress (gdbarch, pc)));
7087 }
7088 }
7089 \f
7090
7091 /* Return true iff it is meaningful to use the address member of LOC.
7092 For some breakpoint types, the locations' address members are
7093 irrelevant and it makes no sense to attempt to compare them to
7094 other addresses (or use them for any other purpose either).
7095
7096 More specifically, software watchpoints and catchpoints that are
7097 not backed by breakpoints always have a zero valued location
7098 address and we don't want to mark breakpoints of any of these types
7099 to be a duplicate of an actual breakpoint location at address
7100 zero. */
7101
7102 static bool
7103 bl_address_is_meaningful (bp_location *loc)
7104 {
7105 return loc->loc_type != bp_loc_other;
7106 }
7107
7108 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7109 true if LOC1 and LOC2 represent the same watchpoint location. */
7110
7111 static bool
7112 watchpoint_locations_match (const struct bp_location *loc1,
7113 const struct bp_location *loc2)
7114 {
7115 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7116 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7117
7118 /* Both of them must exist. */
7119 gdb_assert (w1 != NULL);
7120 gdb_assert (w2 != NULL);
7121
7122 /* If the target can evaluate the condition expression in hardware,
7123 then we we need to insert both watchpoints even if they are at
7124 the same place. Otherwise the watchpoint will only trigger when
7125 the condition of whichever watchpoint was inserted evaluates to
7126 true, not giving a chance for GDB to check the condition of the
7127 other watchpoint. */
7128 if ((w1->cond_exp
7129 && target_can_accel_watchpoint_condition (loc1->address,
7130 loc1->length,
7131 loc1->watchpoint_type,
7132 w1->cond_exp.get ()))
7133 || (w2->cond_exp
7134 && target_can_accel_watchpoint_condition (loc2->address,
7135 loc2->length,
7136 loc2->watchpoint_type,
7137 w2->cond_exp.get ())))
7138 return false;
7139
7140 /* Note that this checks the owner's type, not the location's. In
7141 case the target does not support read watchpoints, but does
7142 support access watchpoints, we'll have bp_read_watchpoint
7143 watchpoints with hw_access locations. Those should be considered
7144 duplicates of hw_read locations. The hw_read locations will
7145 become hw_access locations later. */
7146 return (loc1->owner->type == loc2->owner->type
7147 && loc1->pspace->aspace == loc2->pspace->aspace
7148 && loc1->address == loc2->address
7149 && loc1->length == loc2->length);
7150 }
7151
7152 /* See breakpoint.h. */
7153
7154 int
7155 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
7156 const address_space *aspace2, CORE_ADDR addr2)
7157 {
7158 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7159 || aspace1 == aspace2)
7160 && addr1 == addr2);
7161 }
7162
7163 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7164 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7165 matches ASPACE2. On targets that have global breakpoints, the address
7166 space doesn't really matter. */
7167
7168 static bool
7169 breakpoint_address_match_range (const address_space *aspace1,
7170 CORE_ADDR addr1,
7171 int len1, const address_space *aspace2,
7172 CORE_ADDR addr2)
7173 {
7174 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7175 || aspace1 == aspace2)
7176 && addr2 >= addr1 && addr2 < addr1 + len1);
7177 }
7178
7179 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7180 a ranged breakpoint. In most targets, a match happens only if ASPACE
7181 matches the breakpoint's address space. On targets that have global
7182 breakpoints, the address space doesn't really matter. */
7183
7184 static bool
7185 breakpoint_location_address_match (struct bp_location *bl,
7186 const address_space *aspace,
7187 CORE_ADDR addr)
7188 {
7189 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7190 aspace, addr)
7191 || (bl->length
7192 && breakpoint_address_match_range (bl->pspace->aspace,
7193 bl->address, bl->length,
7194 aspace, addr)));
7195 }
7196
7197 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7198 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7199 match happens only if ASPACE matches the breakpoint's address
7200 space. On targets that have global breakpoints, the address space
7201 doesn't really matter. */
7202
7203 static bool
7204 breakpoint_location_address_range_overlap (struct bp_location *bl,
7205 const address_space *aspace,
7206 CORE_ADDR addr, int len)
7207 {
7208 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7209 || bl->pspace->aspace == aspace)
7210 {
7211 int bl_len = bl->length != 0 ? bl->length : 1;
7212
7213 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7214 return 1;
7215 }
7216 return 0;
7217 }
7218
7219 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7220 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7221 true, otherwise returns false. */
7222
7223 static bool
7224 tracepoint_locations_match (const struct bp_location *loc1,
7225 const struct bp_location *loc2)
7226 {
7227 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7228 /* Since tracepoint locations are never duplicated with others', tracepoint
7229 locations at the same address of different tracepoints are regarded as
7230 different locations. */
7231 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7232 else
7233 return false;
7234 }
7235
7236 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7237 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7238 the same location. If SW_HW_BPS_MATCH is true, then software
7239 breakpoint locations and hardware breakpoint locations match,
7240 otherwise they don't. */
7241
7242 static bool
7243 breakpoint_locations_match (const struct bp_location *loc1,
7244 const struct bp_location *loc2,
7245 bool sw_hw_bps_match)
7246 {
7247 int hw_point1, hw_point2;
7248
7249 /* Both of them must not be in moribund_locations. */
7250 gdb_assert (loc1->owner != NULL);
7251 gdb_assert (loc2->owner != NULL);
7252
7253 hw_point1 = is_hardware_watchpoint (loc1->owner);
7254 hw_point2 = is_hardware_watchpoint (loc2->owner);
7255
7256 if (hw_point1 != hw_point2)
7257 return false;
7258 else if (hw_point1)
7259 return watchpoint_locations_match (loc1, loc2);
7260 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7261 return tracepoint_locations_match (loc1, loc2);
7262 else
7263 /* We compare bp_location.length in order to cover ranged
7264 breakpoints. Keep this in sync with
7265 bp_location_is_less_than. */
7266 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7267 loc2->pspace->aspace, loc2->address)
7268 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7269 && loc1->length == loc2->length);
7270 }
7271
7272 static void
7273 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7274 int bnum, bool have_bnum)
7275 {
7276 /* The longest string possibly returned by hex_string_custom
7277 is 50 chars. These must be at least that big for safety. */
7278 char astr1[64];
7279 char astr2[64];
7280
7281 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7282 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7283 if (have_bnum)
7284 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7285 bnum, astr1, astr2);
7286 else
7287 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7288 }
7289
7290 /* Adjust a breakpoint's address to account for architectural
7291 constraints on breakpoint placement. Return the adjusted address.
7292 Note: Very few targets require this kind of adjustment. For most
7293 targets, this function is simply the identity function. */
7294
7295 static CORE_ADDR
7296 adjust_breakpoint_address (struct gdbarch *gdbarch,
7297 CORE_ADDR bpaddr, enum bptype bptype,
7298 struct program_space *pspace)
7299 {
7300 gdb_assert (pspace != nullptr);
7301
7302 if (bptype == bp_watchpoint
7303 || bptype == bp_hardware_watchpoint
7304 || bptype == bp_read_watchpoint
7305 || bptype == bp_access_watchpoint
7306 || bptype == bp_catchpoint)
7307 {
7308 /* Watchpoints and the various bp_catch_* eventpoints should not
7309 have their addresses modified. */
7310 return bpaddr;
7311 }
7312 else if (bptype == bp_single_step)
7313 {
7314 /* Single-step breakpoints should not have their addresses
7315 modified. If there's any architectural constrain that
7316 applies to this address, then it should have already been
7317 taken into account when the breakpoint was created in the
7318 first place. If we didn't do this, stepping through e.g.,
7319 Thumb-2 IT blocks would break. */
7320 return bpaddr;
7321 }
7322 else
7323 {
7324 CORE_ADDR adjusted_bpaddr = bpaddr;
7325
7326 /* Some targets have architectural constraints on the placement
7327 of breakpoint instructions. Obtain the adjusted address. */
7328 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7329 {
7330 /* Targets that implement this adjustment function will likely
7331 inspect either the symbol table, target memory at BPADDR, or
7332 even state registers, so ensure a suitable thread (and its
7333 associated program space) are currently selected. */
7334 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7335 switch_to_program_space_and_thread (pspace);
7336 adjusted_bpaddr
7337 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7338 }
7339
7340 adjusted_bpaddr
7341 = gdbarch_remove_non_address_bits (gdbarch, adjusted_bpaddr);
7342
7343 /* An adjusted breakpoint address can significantly alter
7344 a user's expectations. Print a warning if an adjustment
7345 is required. */
7346 if (adjusted_bpaddr != bpaddr)
7347 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, false);
7348
7349 return adjusted_bpaddr;
7350 }
7351 }
7352
7353 static bp_loc_type
7354 bp_location_from_bp_type (bptype type)
7355 {
7356 switch (type)
7357 {
7358 case bp_breakpoint:
7359 case bp_single_step:
7360 case bp_until:
7361 case bp_finish:
7362 case bp_longjmp:
7363 case bp_longjmp_resume:
7364 case bp_longjmp_call_dummy:
7365 case bp_exception:
7366 case bp_exception_resume:
7367 case bp_step_resume:
7368 case bp_hp_step_resume:
7369 case bp_watchpoint_scope:
7370 case bp_call_dummy:
7371 case bp_std_terminate:
7372 case bp_shlib_event:
7373 case bp_thread_event:
7374 case bp_overlay_event:
7375 case bp_jit_event:
7376 case bp_longjmp_master:
7377 case bp_std_terminate_master:
7378 case bp_exception_master:
7379 case bp_gnu_ifunc_resolver:
7380 case bp_gnu_ifunc_resolver_return:
7381 case bp_dprintf:
7382 return bp_loc_software_breakpoint;
7383
7384 case bp_hardware_breakpoint:
7385 return bp_loc_hardware_breakpoint;
7386
7387 case bp_hardware_watchpoint:
7388 case bp_read_watchpoint:
7389 case bp_access_watchpoint:
7390 return bp_loc_hardware_watchpoint;
7391
7392 case bp_watchpoint:
7393 return bp_loc_software_watchpoint;
7394
7395 case bp_tracepoint:
7396 case bp_fast_tracepoint:
7397 case bp_static_tracepoint:
7398 case bp_static_marker_tracepoint:
7399 return bp_loc_tracepoint;
7400
7401 case bp_catchpoint:
7402 return bp_loc_other;
7403
7404 default:
7405 internal_error (_("unknown breakpoint type"));
7406 }
7407 }
7408
7409 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7410 {
7411 this->owner = owner;
7412 this->cond_bytecode = NULL;
7413 this->shlib_disabled = 0;
7414 this->enabled = 1;
7415 this->disabled_by_cond = false;
7416
7417 this->loc_type = type;
7418
7419 if (this->loc_type == bp_loc_software_breakpoint
7420 || this->loc_type == bp_loc_hardware_breakpoint)
7421 mark_breakpoint_location_modified (this);
7422
7423 incref ();
7424 }
7425
7426 bp_location::bp_location (breakpoint *owner)
7427 : bp_location::bp_location (owner,
7428 bp_location_from_bp_type (owner->type))
7429 {
7430 }
7431
7432 /* Decrement reference count. If the reference count reaches 0,
7433 destroy the bp_location. Sets *BLP to NULL. */
7434
7435 static void
7436 decref_bp_location (struct bp_location **blp)
7437 {
7438 bp_location_ref_policy::decref (*blp);
7439 *blp = NULL;
7440 }
7441
7442 /* Add breakpoint B at the end of the global breakpoint chain. */
7443
7444 static breakpoint *
7445 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7446 {
7447 struct breakpoint *b1;
7448 struct breakpoint *result = b.get ();
7449
7450 /* Add this breakpoint to the end of the chain so that a list of
7451 breakpoints will come out in order of increasing numbers. */
7452
7453 b1 = breakpoint_chain;
7454 if (b1 == 0)
7455 breakpoint_chain = b.release ();
7456 else
7457 {
7458 while (b1->next)
7459 b1 = b1->next;
7460 b1->next = b.release ();
7461 }
7462
7463 return result;
7464 }
7465
7466 /* Initialize loc->function_name. */
7467
7468 static void
7469 set_breakpoint_location_function (struct bp_location *loc)
7470 {
7471 gdb_assert (loc->owner != NULL);
7472
7473 if (loc->owner->type == bp_breakpoint
7474 || loc->owner->type == bp_hardware_breakpoint
7475 || is_tracepoint (loc->owner))
7476 {
7477 const char *function_name;
7478
7479 if (loc->msymbol != NULL
7480 && (loc->msymbol->type () == mst_text_gnu_ifunc
7481 || loc->msymbol->type () == mst_data_gnu_ifunc))
7482 {
7483 struct breakpoint *b = loc->owner;
7484
7485 function_name = loc->msymbol->linkage_name ();
7486
7487 if (b->type == bp_breakpoint && b->loc == loc
7488 && loc->next == NULL && b->related_breakpoint == b)
7489 {
7490 /* Create only the whole new breakpoint of this type but do not
7491 mess more complicated breakpoints with multiple locations. */
7492 b->type = bp_gnu_ifunc_resolver;
7493 /* Remember the resolver's address for use by the return
7494 breakpoint. */
7495 loc->related_address = loc->address;
7496 }
7497 }
7498 else
7499 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7500
7501 if (function_name)
7502 loc->function_name = make_unique_xstrdup (function_name);
7503 }
7504 }
7505
7506 /* Attempt to determine architecture of location identified by SAL. */
7507 struct gdbarch *
7508 get_sal_arch (struct symtab_and_line sal)
7509 {
7510 if (sal.section)
7511 return sal.section->objfile->arch ();
7512 if (sal.symtab)
7513 return sal.symtab->compunit ()->objfile ()->arch ();
7514
7515 return NULL;
7516 }
7517
7518 /* Call this routine when stepping and nexting to enable a breakpoint
7519 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7520 initiated the operation. */
7521
7522 void
7523 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7524 {
7525 int thread = tp->global_num;
7526
7527 /* To avoid having to rescan all objfile symbols at every step,
7528 we maintain a list of continually-inserted but always disabled
7529 longjmp "master" breakpoints. Here, we simply create momentary
7530 clones of those and enable them for the requested thread. */
7531 for (breakpoint *b : all_breakpoints_safe ())
7532 if (b->pspace == current_program_space
7533 && (b->type == bp_longjmp_master
7534 || b->type == bp_exception_master))
7535 {
7536 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7537 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7538 after their removal. */
7539 momentary_breakpoint_from_master (b, type, 1, thread);
7540 }
7541
7542 tp->initiating_frame = frame;
7543 }
7544
7545 /* Delete all longjmp breakpoints from THREAD. */
7546 void
7547 delete_longjmp_breakpoint (int thread)
7548 {
7549 for (breakpoint *b : all_breakpoints_safe ())
7550 if (b->type == bp_longjmp || b->type == bp_exception)
7551 {
7552 if (b->thread == thread)
7553 delete_breakpoint (b);
7554 }
7555 }
7556
7557 void
7558 delete_longjmp_breakpoint_at_next_stop (int thread)
7559 {
7560 for (breakpoint *b : all_breakpoints_safe ())
7561 if (b->type == bp_longjmp || b->type == bp_exception)
7562 {
7563 if (b->thread == thread)
7564 b->disposition = disp_del_at_next_stop;
7565 }
7566 }
7567
7568 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7569 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7570 pointer to any of them. Return NULL if this system cannot place longjmp
7571 breakpoints. */
7572
7573 struct breakpoint *
7574 set_longjmp_breakpoint_for_call_dummy (void)
7575 {
7576 breakpoint *retval = nullptr;
7577
7578 for (breakpoint *b : all_breakpoints ())
7579 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7580 {
7581 int thread = inferior_thread ()->global_num;
7582 breakpoint *new_b
7583 = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7584 1, thread);
7585
7586 /* Link NEW_B into the chain of RETVAL breakpoints. */
7587
7588 gdb_assert (new_b->related_breakpoint == new_b);
7589 if (retval == NULL)
7590 retval = new_b;
7591 new_b->related_breakpoint = retval;
7592 while (retval->related_breakpoint != new_b->related_breakpoint)
7593 retval = retval->related_breakpoint;
7594 retval->related_breakpoint = new_b;
7595 }
7596
7597 return retval;
7598 }
7599
7600 /* Verify all existing dummy frames and their associated breakpoints for
7601 TP. Remove those which can no longer be found in the current frame
7602 stack.
7603
7604 If the unwind fails then there is not sufficient information to discard
7605 dummy frames. In this case, elide the clean up and the dummy frames will
7606 be cleaned up next time this function is called from a location where
7607 unwinding is possible. */
7608
7609 void
7610 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7611 {
7612 /* We would need to delete breakpoints other than the current one while
7613 iterating, so all_breakpoints_safe is not sufficient to make that safe.
7614 Save all breakpoints to delete in that set and delete them at the end. */
7615 std::unordered_set<breakpoint *> to_delete;
7616
7617 for (struct breakpoint *b : all_breakpoints ())
7618 {
7619 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7620 {
7621 struct breakpoint *dummy_b = b->related_breakpoint;
7622
7623 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7624 chained off b->related_breakpoint. */
7625 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7626 dummy_b = dummy_b->related_breakpoint;
7627
7628 /* If there was no bp_call_dummy breakpoint then there's nothing
7629 more to do. Or, if the dummy frame associated with the
7630 bp_call_dummy is still on the stack then we need to leave this
7631 bp_call_dummy in place. */
7632 if (dummy_b->type != bp_call_dummy
7633 || frame_find_by_id (dummy_b->frame_id) != NULL)
7634 continue;
7635
7636 /* We didn't find the dummy frame on the stack, this could be
7637 because we have longjmp'd to a stack frame that is previous to
7638 the dummy frame, or it could be because the stack unwind is
7639 broken at some point between the longjmp frame and the dummy
7640 frame.
7641
7642 Next we figure out why the stack unwind stopped. If it looks
7643 like the unwind is complete then we assume the dummy frame has
7644 been jumped over, however, if the unwind stopped for an
7645 unexpected reason then we assume the stack unwind is currently
7646 broken, and that we will (eventually) return to the dummy
7647 frame.
7648
7649 It might be tempting to consider using frame_id_inner here, but
7650 that is not safe. There is no guarantee that the stack frames
7651 we are looking at here are even on the same stack as the
7652 original dummy frame, hence frame_id_inner can't be used. See
7653 the comments on frame_id_inner for more details. */
7654 bool unwind_finished_unexpectedly = false;
7655 for (frame_info_ptr fi = get_current_frame (); fi != nullptr; )
7656 {
7657 frame_info_ptr prev = get_prev_frame (fi);
7658 if (prev == nullptr)
7659 {
7660 /* FI is the last stack frame. Why did this frame not
7661 unwind further? */
7662 auto stop_reason = get_frame_unwind_stop_reason (fi);
7663 if (stop_reason != UNWIND_NO_REASON
7664 && stop_reason != UNWIND_OUTERMOST)
7665 unwind_finished_unexpectedly = true;
7666 }
7667 fi = prev;
7668 }
7669 if (unwind_finished_unexpectedly)
7670 continue;
7671
7672 dummy_frame_discard (dummy_b->frame_id, tp);
7673
7674 for (breakpoint *related_breakpoint = b->related_breakpoint;
7675 related_breakpoint != b;
7676 related_breakpoint = related_breakpoint->related_breakpoint)
7677 to_delete.insert (b->related_breakpoint);
7678
7679 to_delete.insert (b);
7680 }
7681 }
7682
7683 for (breakpoint *b : to_delete)
7684 delete_breakpoint (b);
7685 }
7686
7687 void
7688 enable_overlay_breakpoints (void)
7689 {
7690 for (breakpoint *b : all_breakpoints ())
7691 if (b->type == bp_overlay_event)
7692 {
7693 b->enable_state = bp_enabled;
7694 update_global_location_list (UGLL_MAY_INSERT);
7695 overlay_events_enabled = 1;
7696 }
7697 }
7698
7699 void
7700 disable_overlay_breakpoints (void)
7701 {
7702 for (breakpoint *b : all_breakpoints ())
7703 if (b->type == bp_overlay_event)
7704 {
7705 b->enable_state = bp_disabled;
7706 update_global_location_list (UGLL_DONT_INSERT);
7707 overlay_events_enabled = 0;
7708 }
7709 }
7710
7711 /* Set an active std::terminate breakpoint for each std::terminate
7712 master breakpoint. */
7713 void
7714 set_std_terminate_breakpoint (void)
7715 {
7716 for (breakpoint *b : all_breakpoints_safe ())
7717 if (b->pspace == current_program_space
7718 && b->type == bp_std_terminate_master)
7719 {
7720 momentary_breakpoint_from_master (b, bp_std_terminate, 1,
7721 inferior_thread ()->global_num);
7722 }
7723 }
7724
7725 /* Delete all the std::terminate breakpoints. */
7726 void
7727 delete_std_terminate_breakpoint (void)
7728 {
7729 for (breakpoint *b : all_breakpoints_safe ())
7730 if (b->type == bp_std_terminate)
7731 delete_breakpoint (b);
7732 }
7733
7734 struct breakpoint *
7735 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7736 {
7737 struct breakpoint *b;
7738
7739 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
7740
7741 b->enable_state = bp_enabled;
7742 /* locspec has to be used or breakpoint_re_set will delete me. */
7743 b->locspec = new_address_location_spec (b->loc->address, NULL, 0);
7744
7745 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7746
7747 return b;
7748 }
7749
7750 struct lang_and_radix
7751 {
7752 enum language lang;
7753 int radix;
7754 };
7755
7756 /* Create a breakpoint for JIT code registration and unregistration. */
7757
7758 struct breakpoint *
7759 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7760 {
7761 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
7762 }
7763
7764 /* Remove JIT code registration and unregistration breakpoint(s). */
7765
7766 void
7767 remove_jit_event_breakpoints (void)
7768 {
7769 for (breakpoint *b : all_breakpoints_safe ())
7770 if (b->type == bp_jit_event
7771 && b->loc->pspace == current_program_space)
7772 delete_breakpoint (b);
7773 }
7774
7775 void
7776 remove_solib_event_breakpoints (void)
7777 {
7778 for (breakpoint *b : all_breakpoints_safe ())
7779 if (b->type == bp_shlib_event
7780 && b->loc->pspace == current_program_space)
7781 delete_breakpoint (b);
7782 }
7783
7784 /* See breakpoint.h. */
7785
7786 void
7787 remove_solib_event_breakpoints_at_next_stop (void)
7788 {
7789 for (breakpoint *b : all_breakpoints_safe ())
7790 if (b->type == bp_shlib_event
7791 && b->loc->pspace == current_program_space)
7792 b->disposition = disp_del_at_next_stop;
7793 }
7794
7795 /* Helper for create_solib_event_breakpoint /
7796 create_and_insert_solib_event_breakpoint. Allows specifying which
7797 INSERT_MODE to pass through to update_global_location_list. */
7798
7799 static struct breakpoint *
7800 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7801 enum ugll_insert_mode insert_mode)
7802 {
7803 struct breakpoint *b;
7804
7805 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
7806 update_global_location_list_nothrow (insert_mode);
7807 return b;
7808 }
7809
7810 struct breakpoint *
7811 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7812 {
7813 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7814 }
7815
7816 /* See breakpoint.h. */
7817
7818 struct breakpoint *
7819 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7820 {
7821 struct breakpoint *b;
7822
7823 /* Explicitly tell update_global_location_list to insert
7824 locations. */
7825 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7826 if (!b->loc->inserted)
7827 {
7828 delete_breakpoint (b);
7829 return NULL;
7830 }
7831 return b;
7832 }
7833
7834 /* Disable any breakpoints that are on code in shared libraries. Only
7835 apply to enabled breakpoints, disabled ones can just stay disabled. */
7836
7837 void
7838 disable_breakpoints_in_shlibs (void)
7839 {
7840 for (bp_location *loc : all_bp_locations ())
7841 {
7842 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7843 struct breakpoint *b = loc->owner;
7844
7845 /* We apply the check to all breakpoints, including disabled for
7846 those with loc->duplicate set. This is so that when breakpoint
7847 becomes enabled, or the duplicate is removed, gdb will try to
7848 insert all breakpoints. If we don't set shlib_disabled here,
7849 we'll try to insert those breakpoints and fail. */
7850 if (((b->type == bp_breakpoint)
7851 || (b->type == bp_jit_event)
7852 || (b->type == bp_hardware_breakpoint)
7853 || (is_tracepoint (b)))
7854 && loc->pspace == current_program_space
7855 && !loc->shlib_disabled
7856 && solib_name_from_address (loc->pspace, loc->address)
7857 )
7858 {
7859 loc->shlib_disabled = 1;
7860 }
7861 }
7862 }
7863
7864 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7865 notification of unloaded_shlib. Only apply to enabled breakpoints,
7866 disabled ones can just stay disabled. */
7867
7868 static void
7869 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7870 {
7871 bool disabled_shlib_breaks = false;
7872
7873 for (bp_location *loc : all_bp_locations ())
7874 {
7875 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7876 struct breakpoint *b = loc->owner;
7877
7878 if (solib->pspace == loc->pspace
7879 && !loc->shlib_disabled
7880 && (((b->type == bp_breakpoint
7881 || b->type == bp_jit_event
7882 || b->type == bp_hardware_breakpoint)
7883 && (loc->loc_type == bp_loc_hardware_breakpoint
7884 || loc->loc_type == bp_loc_software_breakpoint))
7885 || is_tracepoint (b))
7886 && solib_contains_address_p (solib, loc->address))
7887 {
7888 loc->shlib_disabled = 1;
7889 /* At this point, we cannot rely on remove_breakpoint
7890 succeeding so we must mark the breakpoint as not inserted
7891 to prevent future errors occurring in remove_breakpoints. */
7892 loc->inserted = 0;
7893
7894 /* This may cause duplicate notifications for the same breakpoint. */
7895 gdb::observers::breakpoint_modified.notify (b);
7896
7897 if (!disabled_shlib_breaks)
7898 {
7899 target_terminal::ours_for_output ();
7900 warning (_("Temporarily disabling breakpoints "
7901 "for unloaded shared library \"%s\""),
7902 solib->so_name);
7903 }
7904 disabled_shlib_breaks = true;
7905 }
7906 }
7907 }
7908
7909 /* Disable any breakpoints and tracepoints in OBJFILE upon
7910 notification of free_objfile. Only apply to enabled breakpoints,
7911 disabled ones can just stay disabled. */
7912
7913 static void
7914 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7915 {
7916 if (objfile == NULL)
7917 return;
7918
7919 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7920 managed by the user with add-symbol-file/remove-symbol-file.
7921 Similarly to how breakpoints in shared libraries are handled in
7922 response to "nosharedlibrary", mark breakpoints in such modules
7923 shlib_disabled so they end up uninserted on the next global
7924 location list update. Shared libraries not loaded by the user
7925 aren't handled here -- they're already handled in
7926 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7927 solib_unloaded observer. We skip objfiles that are not
7928 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7929 main objfile). */
7930 if ((objfile->flags & OBJF_SHARED) == 0
7931 || (objfile->flags & OBJF_USERLOADED) == 0)
7932 return;
7933
7934 for (breakpoint *b : all_breakpoints ())
7935 {
7936 bool bp_modified = false;
7937
7938 if (!is_breakpoint (b) && !is_tracepoint (b))
7939 continue;
7940
7941 for (bp_location *loc : b->locations ())
7942 {
7943 CORE_ADDR loc_addr = loc->address;
7944
7945 if (loc->loc_type != bp_loc_hardware_breakpoint
7946 && loc->loc_type != bp_loc_software_breakpoint)
7947 continue;
7948
7949 if (loc->shlib_disabled != 0)
7950 continue;
7951
7952 if (objfile->pspace != loc->pspace)
7953 continue;
7954
7955 if (loc->loc_type != bp_loc_hardware_breakpoint
7956 && loc->loc_type != bp_loc_software_breakpoint)
7957 continue;
7958
7959 if (is_addr_in_objfile (loc_addr, objfile))
7960 {
7961 loc->shlib_disabled = 1;
7962 /* At this point, we don't know whether the object was
7963 unmapped from the inferior or not, so leave the
7964 inserted flag alone. We'll handle failure to
7965 uninsert quietly, in case the object was indeed
7966 unmapped. */
7967
7968 mark_breakpoint_location_modified (loc);
7969
7970 bp_modified = true;
7971 }
7972 }
7973
7974 if (bp_modified)
7975 gdb::observers::breakpoint_modified.notify (b);
7976 }
7977 }
7978
7979 /* See breakpoint.h. */
7980
7981 breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
7982 bool temp, const char *cond_string_)
7983 : type (bptype),
7984 disposition (temp ? disp_del : disp_donttouch),
7985 gdbarch (gdbarch_),
7986 language (current_language->la_language),
7987 input_radix (::input_radix),
7988 cond_string (cond_string_ != nullptr
7989 ? make_unique_xstrdup (cond_string_)
7990 : nullptr),
7991 related_breakpoint (this)
7992 {
7993 }
7994
7995 /* See breakpoint.h. */
7996
7997 catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
7998 const char *cond_string)
7999 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
8000 {
8001 add_dummy_location (this, current_program_space);
8002
8003 pspace = current_program_space;
8004 }
8005
8006 breakpoint *
8007 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8008 {
8009 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8010 set_breakpoint_number (internal, b);
8011 if (is_tracepoint (b))
8012 set_tracepoint_count (breakpoint_count);
8013 if (!internal)
8014 mention (b);
8015 gdb::observers::breakpoint_created.notify (b);
8016
8017 if (update_gll)
8018 update_global_location_list (UGLL_MAY_INSERT);
8019
8020 return b;
8021 }
8022
8023 static int
8024 hw_breakpoint_used_count (void)
8025 {
8026 int i = 0;
8027
8028 for (breakpoint *b : all_breakpoints ())
8029 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8030 for (bp_location *bl : b->locations ())
8031 {
8032 /* Special types of hardware breakpoints may use more than
8033 one register. */
8034 i += b->resources_needed (bl);
8035 }
8036
8037 return i;
8038 }
8039
8040 /* Returns the resources B would use if it were a hardware
8041 watchpoint. */
8042
8043 static int
8044 hw_watchpoint_use_count (struct breakpoint *b)
8045 {
8046 int i = 0;
8047
8048 if (!breakpoint_enabled (b))
8049 return 0;
8050
8051 for (bp_location *bl : b->locations ())
8052 {
8053 /* Special types of hardware watchpoints may use more than
8054 one register. */
8055 i += b->resources_needed (bl);
8056 }
8057
8058 return i;
8059 }
8060
8061 /* Returns the sum the used resources of all hardware watchpoints of
8062 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8063 the sum of the used resources of all hardware watchpoints of other
8064 types _not_ TYPE. */
8065
8066 static int
8067 hw_watchpoint_used_count_others (struct breakpoint *except,
8068 enum bptype type, int *other_type_used)
8069 {
8070 int i = 0;
8071
8072 *other_type_used = 0;
8073 for (breakpoint *b : all_breakpoints ())
8074 {
8075 if (b == except)
8076 continue;
8077 if (!breakpoint_enabled (b))
8078 continue;
8079
8080 if (b->type == type)
8081 i += hw_watchpoint_use_count (b);
8082 else if (is_hardware_watchpoint (b))
8083 *other_type_used = 1;
8084 }
8085
8086 return i;
8087 }
8088
8089 void
8090 disable_watchpoints_before_interactive_call_start (void)
8091 {
8092 for (breakpoint *b : all_breakpoints ())
8093 if (is_watchpoint (b) && breakpoint_enabled (b))
8094 {
8095 b->enable_state = bp_call_disabled;
8096 update_global_location_list (UGLL_DONT_INSERT);
8097 }
8098 }
8099
8100 void
8101 enable_watchpoints_after_interactive_call_stop (void)
8102 {
8103 for (breakpoint *b : all_breakpoints ())
8104 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8105 {
8106 b->enable_state = bp_enabled;
8107 update_global_location_list (UGLL_MAY_INSERT);
8108 }
8109 }
8110
8111 void
8112 disable_breakpoints_before_startup (void)
8113 {
8114 current_program_space->executing_startup = 1;
8115 update_global_location_list (UGLL_DONT_INSERT);
8116 }
8117
8118 void
8119 enable_breakpoints_after_startup (void)
8120 {
8121 current_program_space->executing_startup = 0;
8122 breakpoint_re_set ();
8123 }
8124
8125 /* Allocate a new momentary breakpoint. */
8126
8127 template<typename... Arg>
8128 static momentary_breakpoint *
8129 new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
8130 Arg&&... args)
8131 {
8132 if (type == bp_longjmp || type == bp_exception)
8133 return new longjmp_breakpoint (gdbarch, type,
8134 std::forward<Arg> (args)...);
8135 else
8136 return new momentary_breakpoint (gdbarch, type,
8137 std::forward<Arg> (args)...);
8138 }
8139
8140 /* Set a momentary breakpoint of type TYPE at address specified by
8141 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8142 frame. */
8143
8144 breakpoint_up
8145 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8146 struct frame_id frame_id, enum bptype type)
8147 {
8148 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8149 tail-called one. */
8150 gdb_assert (!frame_id_artificial_p (frame_id));
8151
8152 std::unique_ptr<momentary_breakpoint> b
8153 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
8154 inferior_thread ()->global_num));
8155
8156 b->add_location (sal);
8157
8158 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
8159
8160 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8161
8162 return bp;
8163 }
8164
8165 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8166 The new breakpoint will have type TYPE, use OPS as its
8167 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8168
8169 static struct breakpoint *
8170 momentary_breakpoint_from_master (struct breakpoint *orig,
8171 enum bptype type,
8172 int loc_enabled,
8173 int thread)
8174 {
8175 std::unique_ptr<breakpoint> copy
8176 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
8177 orig->frame_id, thread));
8178 copy->loc = copy->allocate_location ();
8179 set_breakpoint_location_function (copy->loc);
8180
8181 copy->loc->gdbarch = orig->loc->gdbarch;
8182 copy->loc->requested_address = orig->loc->requested_address;
8183 copy->loc->address = orig->loc->address;
8184 copy->loc->section = orig->loc->section;
8185 copy->loc->pspace = orig->loc->pspace;
8186 copy->loc->probe = orig->loc->probe;
8187 copy->loc->line_number = orig->loc->line_number;
8188 copy->loc->symtab = orig->loc->symtab;
8189 copy->loc->enabled = loc_enabled;
8190
8191 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
8192 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8193 return b;
8194 }
8195
8196 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8197 ORIG is NULL. */
8198
8199 struct breakpoint *
8200 clone_momentary_breakpoint (struct breakpoint *orig)
8201 {
8202 /* If there's nothing to clone, then return nothing. */
8203 if (orig == NULL)
8204 return NULL;
8205
8206 return momentary_breakpoint_from_master (orig, orig->type, 0,
8207 orig->thread);
8208 }
8209
8210 breakpoint_up
8211 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8212 enum bptype type)
8213 {
8214 struct symtab_and_line sal;
8215
8216 sal = find_pc_line (pc, 0);
8217 sal.pc = pc;
8218 sal.section = find_pc_overlay (pc);
8219 sal.explicit_pc = 1;
8220
8221 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8222 }
8223 \f
8224
8225 /* Tell the user we have just set a breakpoint B. */
8226
8227 static void
8228 mention (const breakpoint *b)
8229 {
8230 b->print_mention ();
8231 current_uiout->text ("\n");
8232 }
8233 \f
8234
8235 static bool bp_loc_is_permanent (struct bp_location *loc);
8236
8237 /* Handle "set breakpoint auto-hw on".
8238
8239 If the explicitly specified breakpoint type is not hardware
8240 breakpoint, check the memory map to see whether the breakpoint
8241 address is in read-only memory.
8242
8243 - location type is not hardware breakpoint, memory is read-only.
8244 We change the type of the location to hardware breakpoint.
8245
8246 - location type is hardware breakpoint, memory is read-write. This
8247 means we've previously made the location hardware one, but then the
8248 memory map changed, so we undo.
8249 */
8250
8251 static void
8252 handle_automatic_hardware_breakpoints (bp_location *bl)
8253 {
8254 if (automatic_hardware_breakpoints
8255 && bl->owner->type != bp_hardware_breakpoint
8256 && (bl->loc_type == bp_loc_software_breakpoint
8257 || bl->loc_type == bp_loc_hardware_breakpoint))
8258 {
8259 /* When breakpoints are removed, remove_breakpoints will use
8260 location types we've just set here, the only possible problem
8261 is that memory map has changed during running program, but
8262 it's not going to work anyway with current gdb. */
8263 mem_region *mr = lookup_mem_region (bl->address);
8264
8265 if (mr != nullptr)
8266 {
8267 enum bp_loc_type new_type;
8268
8269 if (mr->attrib.mode != MEM_RW)
8270 new_type = bp_loc_hardware_breakpoint;
8271 else
8272 new_type = bp_loc_software_breakpoint;
8273
8274 if (new_type != bl->loc_type)
8275 {
8276 static bool said = false;
8277
8278 bl->loc_type = new_type;
8279 if (!said)
8280 {
8281 gdb_printf (_("Note: automatically using "
8282 "hardware breakpoints for "
8283 "read-only addresses.\n"));
8284 said = true;
8285 }
8286 }
8287 }
8288 }
8289 }
8290
8291 bp_location *
8292 code_breakpoint::add_location (const symtab_and_line &sal)
8293 {
8294 struct bp_location *new_loc, **tmp;
8295 CORE_ADDR adjusted_address;
8296 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8297
8298 if (loc_gdbarch == NULL)
8299 loc_gdbarch = gdbarch;
8300
8301 /* Adjust the breakpoint's address prior to allocating a location.
8302 Once we call allocate_location(), that mostly uninitialized
8303 location will be placed on the location chain. Adjustment of the
8304 breakpoint may cause target_read_memory() to be called and we do
8305 not want its scan of the location chain to find a breakpoint and
8306 location that's only been partially initialized. */
8307 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8308 sal.pc, type,
8309 sal.pspace);
8310
8311 /* Sort the locations by their ADDRESS. */
8312 new_loc = allocate_location ();
8313 for (tmp = &(loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8314 tmp = &((*tmp)->next))
8315 ;
8316 new_loc->next = *tmp;
8317 *tmp = new_loc;
8318
8319 new_loc->requested_address = sal.pc;
8320 new_loc->address = adjusted_address;
8321 new_loc->pspace = sal.pspace;
8322 new_loc->probe.prob = sal.prob;
8323 new_loc->probe.objfile = sal.objfile;
8324 gdb_assert (new_loc->pspace != NULL);
8325 new_loc->section = sal.section;
8326 new_loc->gdbarch = loc_gdbarch;
8327 new_loc->line_number = sal.line;
8328 new_loc->symtab = sal.symtab;
8329 new_loc->symbol = sal.symbol;
8330 new_loc->msymbol = sal.msymbol;
8331 new_loc->objfile = sal.objfile;
8332
8333 set_breakpoint_location_function (new_loc);
8334
8335 /* While by definition, permanent breakpoints are already present in the
8336 code, we don't mark the location as inserted. Normally one would expect
8337 that GDB could rely on that breakpoint instruction to stop the program,
8338 thus removing the need to insert its own breakpoint, except that executing
8339 the breakpoint instruction can kill the target instead of reporting a
8340 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8341 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8342 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8343 breakpoint be inserted normally results in QEMU knowing about the GDB
8344 breakpoint, and thus trap before the breakpoint instruction is executed.
8345 (If GDB later needs to continue execution past the permanent breakpoint,
8346 it manually increments the PC, thus avoiding executing the breakpoint
8347 instruction.) */
8348 if (bp_loc_is_permanent (new_loc))
8349 new_loc->permanent = 1;
8350
8351 return new_loc;
8352 }
8353 \f
8354
8355 /* Return true if LOC is pointing to a permanent breakpoint,
8356 return false otherwise. */
8357
8358 static bool
8359 bp_loc_is_permanent (struct bp_location *loc)
8360 {
8361 gdb_assert (loc != NULL);
8362
8363 /* If we have a non-breakpoint-backed catchpoint or a software
8364 watchpoint, just return 0. We should not attempt to read from
8365 the addresses the locations of these breakpoint types point to.
8366 gdbarch_program_breakpoint_here_p, below, will attempt to read
8367 memory. */
8368 if (!bl_address_is_meaningful (loc))
8369 return false;
8370
8371 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8372 switch_to_program_space_and_thread (loc->pspace);
8373 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8374 }
8375
8376 /* Build a command list for the dprintf corresponding to the current
8377 settings of the dprintf style options. */
8378
8379 static void
8380 update_dprintf_command_list (struct breakpoint *b)
8381 {
8382 const char *dprintf_args = b->extra_string.get ();
8383 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8384
8385 if (!dprintf_args)
8386 return;
8387
8388 dprintf_args = skip_spaces (dprintf_args);
8389
8390 /* Allow a comma, as it may have terminated a location, but don't
8391 insist on it. */
8392 if (*dprintf_args == ',')
8393 ++dprintf_args;
8394 dprintf_args = skip_spaces (dprintf_args);
8395
8396 if (*dprintf_args != '"')
8397 error (_("Bad format string, missing '\"'."));
8398
8399 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8400 printf_line = xstrprintf ("printf %s", dprintf_args);
8401 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8402 {
8403 if (dprintf_function.empty ())
8404 error (_("No function supplied for dprintf call"));
8405
8406 if (!dprintf_channel.empty ())
8407 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8408 dprintf_function.c_str (),
8409 dprintf_channel.c_str (),
8410 dprintf_args);
8411 else
8412 printf_line = xstrprintf ("call (void) %s (%s)",
8413 dprintf_function.c_str (),
8414 dprintf_args);
8415 }
8416 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8417 {
8418 if (target_can_run_breakpoint_commands ())
8419 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8420 else
8421 {
8422 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8423 printf_line = xstrprintf ("printf %s", dprintf_args);
8424 }
8425 }
8426 else
8427 internal_error (_("Invalid dprintf style."));
8428
8429 gdb_assert (printf_line != NULL);
8430
8431 /* Manufacture a printf sequence. */
8432 struct command_line *printf_cmd_line
8433 = new struct command_line (simple_control, printf_line.release ());
8434 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8435 command_lines_deleter ()));
8436 }
8437
8438 /* Update all dprintf commands, making their command lists reflect
8439 current style settings. */
8440
8441 static void
8442 update_dprintf_commands (const char *args, int from_tty,
8443 struct cmd_list_element *c)
8444 {
8445 for (breakpoint *b : all_breakpoints ())
8446 if (b->type == bp_dprintf)
8447 update_dprintf_command_list (b);
8448 }
8449
8450 code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
8451 enum bptype type_,
8452 gdb::array_view<const symtab_and_line> sals,
8453 location_spec_up &&locspec_,
8454 gdb::unique_xmalloc_ptr<char> filter_,
8455 gdb::unique_xmalloc_ptr<char> cond_string_,
8456 gdb::unique_xmalloc_ptr<char> extra_string_,
8457 enum bpdisp disposition_,
8458 int thread_, int task_, int ignore_count_,
8459 int from_tty,
8460 int enabled_, unsigned flags,
8461 int display_canonical_)
8462 : breakpoint (gdbarch_, type_)
8463 {
8464 int i;
8465
8466 if (type == bp_hardware_breakpoint)
8467 {
8468 int target_resources_ok;
8469
8470 i = hw_breakpoint_used_count ();
8471 target_resources_ok =
8472 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8473 i + 1, 0);
8474 if (target_resources_ok == 0)
8475 error (_("No hardware breakpoint support in the target."));
8476 else if (target_resources_ok < 0)
8477 error (_("Hardware breakpoints used exceeds limit."));
8478 }
8479
8480 gdb_assert (!sals.empty ());
8481
8482 /* At most one of thread or task can be set on any breakpoint. */
8483 gdb_assert (thread == -1 || task == -1);
8484 thread = thread_;
8485 task = task_;
8486
8487 cond_string = std::move (cond_string_);
8488 extra_string = std::move (extra_string_);
8489 ignore_count = ignore_count_;
8490 enable_state = enabled_ ? bp_enabled : bp_disabled;
8491 disposition = disposition_;
8492
8493 if (type == bp_static_tracepoint
8494 || type == bp_static_marker_tracepoint)
8495 {
8496 auto *t = gdb::checked_static_cast<struct tracepoint *> (this);
8497 struct static_tracepoint_marker marker;
8498
8499 if (strace_marker_p (this))
8500 {
8501 /* We already know the marker exists, otherwise, we wouldn't
8502 see a sal for it. */
8503 const char *p = &locspec_->to_string ()[3];
8504 const char *endp;
8505
8506 p = skip_spaces (p);
8507
8508 endp = skip_to_space (p);
8509
8510 t->static_trace_marker_id.assign (p, endp - p);
8511
8512 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8513 t->static_trace_marker_id.c_str ());
8514 }
8515 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8516 {
8517 t->static_trace_marker_id = std::move (marker.str_id);
8518
8519 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8520 t->static_trace_marker_id.c_str ());
8521 }
8522 else
8523 warning (_("Couldn't determine the static tracepoint marker to probe"));
8524 }
8525
8526 for (const auto &sal : sals)
8527 {
8528 if (from_tty)
8529 {
8530 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8531 if (loc_gdbarch == nullptr)
8532 loc_gdbarch = gdbarch;
8533
8534 describe_other_breakpoints (loc_gdbarch,
8535 sal.pspace, sal.pc, sal.section, thread);
8536 }
8537
8538 bp_location *new_loc = add_location (sal);
8539 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8540 new_loc->inserted = 1;
8541
8542 /* Do not set breakpoint locations conditions yet. As locations
8543 are inserted, they get sorted based on their addresses. Let
8544 the list stabilize to have reliable location numbers. */
8545
8546 /* Dynamic printf requires and uses additional arguments on the
8547 command line, otherwise it's an error. */
8548 if (type == bp_dprintf)
8549 {
8550 if (extra_string != nullptr)
8551 update_dprintf_command_list (this);
8552 else
8553 error (_("Format string required"));
8554 }
8555 else if (extra_string != nullptr)
8556 error (_("Garbage '%s' at end of command"), extra_string.get ());
8557 }
8558
8559 /* The order of the locations is now stable. Set the location
8560 condition using the location's number. */
8561 int loc_num = 1;
8562 for (bp_location *bl : locations ())
8563 {
8564 if (cond_string != nullptr)
8565 set_breakpoint_location_condition (cond_string.get (), bl,
8566 number, loc_num);
8567
8568 ++loc_num;
8569 }
8570
8571 display_canonical = display_canonical_;
8572 if (locspec_ != nullptr)
8573 locspec = std::move (locspec_);
8574 else
8575 locspec = new_address_location_spec (this->loc->address, NULL, 0);
8576 filter = std::move (filter_);
8577 }
8578
8579 static void
8580 create_breakpoint_sal (struct gdbarch *gdbarch,
8581 gdb::array_view<const symtab_and_line> sals,
8582 location_spec_up &&locspec,
8583 gdb::unique_xmalloc_ptr<char> filter,
8584 gdb::unique_xmalloc_ptr<char> cond_string,
8585 gdb::unique_xmalloc_ptr<char> extra_string,
8586 enum bptype type, enum bpdisp disposition,
8587 int thread, int task, int ignore_count,
8588 int from_tty,
8589 int enabled, int internal, unsigned flags,
8590 int display_canonical)
8591 {
8592 std::unique_ptr<code_breakpoint> b
8593 = new_breakpoint_from_type (gdbarch,
8594 type,
8595 sals,
8596 std::move (locspec),
8597 std::move (filter),
8598 std::move (cond_string),
8599 std::move (extra_string),
8600 disposition,
8601 thread, task, ignore_count,
8602 from_tty,
8603 enabled, flags,
8604 display_canonical);
8605
8606 install_breakpoint (internal, std::move (b), 0);
8607 }
8608
8609 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8610 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8611 value. COND_STRING, if not NULL, specified the condition to be
8612 used for all breakpoints. Essentially the only case where
8613 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8614 function. In that case, it's still not possible to specify
8615 separate conditions for different overloaded functions, so
8616 we take just a single condition string.
8617
8618 NOTE: If the function succeeds, the caller is expected to cleanup
8619 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8620 array contents). If the function fails (error() is called), the
8621 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8622 COND and SALS arrays and each of those arrays contents. */
8623
8624 static void
8625 create_breakpoints_sal (struct gdbarch *gdbarch,
8626 struct linespec_result *canonical,
8627 gdb::unique_xmalloc_ptr<char> cond_string,
8628 gdb::unique_xmalloc_ptr<char> extra_string,
8629 enum bptype type, enum bpdisp disposition,
8630 int thread, int task, int ignore_count,
8631 int from_tty,
8632 int enabled, int internal, unsigned flags)
8633 {
8634 if (canonical->pre_expanded)
8635 gdb_assert (canonical->lsals.size () == 1);
8636
8637 for (const auto &lsal : canonical->lsals)
8638 {
8639 /* Note that 'location' can be NULL in the case of a plain
8640 'break', without arguments. */
8641 location_spec_up locspec
8642 = (canonical->locspec != nullptr
8643 ? canonical->locspec->clone ()
8644 : nullptr);
8645 gdb::unique_xmalloc_ptr<char> filter_string
8646 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8647
8648 create_breakpoint_sal (gdbarch, lsal.sals,
8649 std::move (locspec),
8650 std::move (filter_string),
8651 std::move (cond_string),
8652 std::move (extra_string),
8653 type, disposition,
8654 thread, task, ignore_count,
8655 from_tty, enabled, internal, flags,
8656 canonical->special_display);
8657 }
8658 }
8659
8660 /* Parse LOCSPEC which is assumed to be a SAL specification possibly
8661 followed by conditionals. On return, SALS contains an array of SAL
8662 addresses found. LOCSPEC points to the end of the SAL (for
8663 linespec locspecs).
8664
8665 The array and the line spec strings are allocated on the heap, it is
8666 the caller's responsibility to free them. */
8667
8668 static void
8669 parse_breakpoint_sals (location_spec *locspec,
8670 struct linespec_result *canonical)
8671 {
8672 struct symtab_and_line cursal;
8673
8674 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8675 {
8676 const char *spec = as_linespec_location_spec (locspec)->spec_string;
8677
8678 if (spec == NULL)
8679 {
8680 /* The last displayed codepoint, if it's valid, is our default
8681 breakpoint address. */
8682 if (last_displayed_sal_is_valid ())
8683 {
8684 /* Set sal's pspace, pc, symtab, and line to the values
8685 corresponding to the last call to print_frame_info.
8686 Be sure to reinitialize LINE with NOTCURRENT == 0
8687 as the breakpoint line number is inappropriate otherwise.
8688 find_pc_line would adjust PC, re-set it back. */
8689 symtab_and_line sal = get_last_displayed_sal ();
8690 CORE_ADDR pc = sal.pc;
8691
8692 sal = find_pc_line (pc, 0);
8693
8694 /* "break" without arguments is equivalent to "break *PC"
8695 where PC is the last displayed codepoint's address. So
8696 make sure to set sal.explicit_pc to prevent GDB from
8697 trying to expand the list of sals to include all other
8698 instances with the same symtab and line. */
8699 sal.pc = pc;
8700 sal.explicit_pc = 1;
8701
8702 struct linespec_sals lsal;
8703 lsal.sals = {sal};
8704 lsal.canonical = NULL;
8705
8706 canonical->lsals.push_back (std::move (lsal));
8707 return;
8708 }
8709 else
8710 error (_("No default breakpoint address now."));
8711 }
8712 }
8713
8714 /* Force almost all breakpoints to be in terms of the
8715 current_source_symtab (which is decode_line_1's default).
8716 This should produce the results we want almost all of the
8717 time while leaving default_breakpoint_* alone.
8718
8719 ObjC: However, don't match an Objective-C method name which
8720 may have a '+' or '-' succeeded by a '['. */
8721 cursal = get_current_source_symtab_and_line ();
8722 if (last_displayed_sal_is_valid ())
8723 {
8724 const char *spec = NULL;
8725
8726 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8727 spec = as_linespec_location_spec (locspec)->spec_string;
8728
8729 if (!cursal.symtab
8730 || (spec != NULL
8731 && strchr ("+-", spec[0]) != NULL
8732 && spec[1] != '['))
8733 {
8734 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
8735 get_last_displayed_symtab (),
8736 get_last_displayed_line (),
8737 canonical, NULL, NULL);
8738 return;
8739 }
8740 }
8741
8742 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
8743 cursal.symtab, cursal.line, canonical, NULL, NULL);
8744 }
8745
8746
8747 /* Convert each SAL into a real PC. Verify that the PC can be
8748 inserted as a breakpoint. If it can't throw an error. */
8749
8750 static void
8751 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
8752 {
8753 for (auto &sal : sals)
8754 resolve_sal_pc (&sal);
8755 }
8756
8757 /* Fast tracepoints may have restrictions on valid locations. For
8758 instance, a fast tracepoint using a jump instead of a trap will
8759 likely have to overwrite more bytes than a trap would, and so can
8760 only be placed where the instruction is longer than the jump, or a
8761 multi-instruction sequence does not have a jump into the middle of
8762 it, etc. */
8763
8764 static void
8765 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8766 gdb::array_view<const symtab_and_line> sals)
8767 {
8768 for (const auto &sal : sals)
8769 {
8770 struct gdbarch *sarch;
8771
8772 sarch = get_sal_arch (sal);
8773 /* We fall back to GDBARCH if there is no architecture
8774 associated with SAL. */
8775 if (sarch == NULL)
8776 sarch = gdbarch;
8777 std::string msg;
8778 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
8779 error (_("May not have a fast tracepoint at %s%s"),
8780 paddress (sarch, sal.pc), msg.c_str ());
8781 }
8782 }
8783
8784 /* Given TOK, a string specification of condition and thread, as
8785 accepted by the 'break' command, extract the condition
8786 string and thread number and set *COND_STRING and *THREAD.
8787 PC identifies the context at which the condition should be parsed.
8788 If no condition is found, *COND_STRING is set to NULL.
8789 If no thread is found, *THREAD is set to -1. */
8790
8791 static void
8792 find_condition_and_thread (const char *tok, CORE_ADDR pc,
8793 gdb::unique_xmalloc_ptr<char> *cond_string,
8794 int *thread, int *task,
8795 gdb::unique_xmalloc_ptr<char> *rest)
8796 {
8797 cond_string->reset ();
8798 *thread = -1;
8799 *task = -1;
8800 rest->reset ();
8801 bool force = false;
8802
8803 while (tok && *tok)
8804 {
8805 const char *end_tok;
8806 int toklen;
8807 const char *cond_start = NULL;
8808 const char *cond_end = NULL;
8809
8810 tok = skip_spaces (tok);
8811
8812 if ((*tok == '"' || *tok == ',') && rest)
8813 {
8814 rest->reset (savestring (tok, strlen (tok)));
8815 return;
8816 }
8817
8818 end_tok = skip_to_space (tok);
8819
8820 toklen = end_tok - tok;
8821
8822 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8823 {
8824 tok = cond_start = end_tok + 1;
8825 try
8826 {
8827 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8828 }
8829 catch (const gdb_exception_error &)
8830 {
8831 if (!force)
8832 throw;
8833 else
8834 tok = tok + strlen (tok);
8835 }
8836 cond_end = tok;
8837 cond_string->reset (savestring (cond_start, cond_end - cond_start));
8838 }
8839 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8840 {
8841 tok = tok + toklen;
8842 force = true;
8843 }
8844 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8845 {
8846 const char *tmptok;
8847 struct thread_info *thr;
8848
8849 if (*thread != -1)
8850 error(_("You can specify only one thread."));
8851
8852 if (*task != -1)
8853 error (_("You can specify only one of thread or task."));
8854
8855 tok = end_tok + 1;
8856 thr = parse_thread_id (tok, &tmptok);
8857 if (tok == tmptok)
8858 error (_("Junk after thread keyword."));
8859 *thread = thr->global_num;
8860 tok = tmptok;
8861 }
8862 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8863 {
8864 char *tmptok;
8865
8866 if (*task != -1)
8867 error(_("You can specify only one task."));
8868
8869 if (*thread != -1)
8870 error (_("You can specify only one of thread or task."));
8871
8872 tok = end_tok + 1;
8873 *task = strtol (tok, &tmptok, 0);
8874 if (tok == tmptok)
8875 error (_("Junk after task keyword."));
8876 if (!valid_task_id (*task))
8877 error (_("Unknown task %d."), *task);
8878 tok = tmptok;
8879 }
8880 else if (rest)
8881 {
8882 rest->reset (savestring (tok, strlen (tok)));
8883 return;
8884 }
8885 else
8886 error (_("Junk at end of arguments."));
8887 }
8888 }
8889
8890 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
8891 succeeds. The parsed values are written to COND_STRING, THREAD,
8892 TASK, and REST. See the comment of 'find_condition_and_thread'
8893 for the description of these parameters and INPUT. */
8894
8895 static void
8896 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
8897 const char *input,
8898 gdb::unique_xmalloc_ptr<char> *cond_string,
8899 int *thread, int *task,
8900 gdb::unique_xmalloc_ptr<char> *rest)
8901 {
8902 int num_failures = 0;
8903 for (auto &sal : sals)
8904 {
8905 gdb::unique_xmalloc_ptr<char> cond;
8906 int thread_id = -1;
8907 int task_id = -1;
8908 gdb::unique_xmalloc_ptr<char> remaining;
8909
8910 /* Here we want to parse 'arg' to separate condition from thread
8911 number. But because parsing happens in a context and the
8912 contexts of sals might be different, try each until there is
8913 success. Finding one successful parse is sufficient for our
8914 goal. When setting the breakpoint we'll re-parse the
8915 condition in the context of each sal. */
8916 try
8917 {
8918 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8919 &task_id, &remaining);
8920 *cond_string = std::move (cond);
8921 /* At most one of thread or task can be set. */
8922 gdb_assert (thread_id == -1 || task_id == -1);
8923 *thread = thread_id;
8924 *task = task_id;
8925 *rest = std::move (remaining);
8926 break;
8927 }
8928 catch (const gdb_exception_error &e)
8929 {
8930 num_failures++;
8931 /* If no sal remains, do not continue. */
8932 if (num_failures == sals.size ())
8933 throw;
8934 }
8935 }
8936 }
8937
8938 /* Decode a static tracepoint marker spec. */
8939
8940 static std::vector<symtab_and_line>
8941 decode_static_tracepoint_spec (const char **arg_p)
8942 {
8943 const char *p = &(*arg_p)[3];
8944 const char *endp;
8945
8946 p = skip_spaces (p);
8947
8948 endp = skip_to_space (p);
8949
8950 std::string marker_str (p, endp - p);
8951
8952 std::vector<static_tracepoint_marker> markers
8953 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8954 if (markers.empty ())
8955 error (_("No known static tracepoint marker named %s"),
8956 marker_str.c_str ());
8957
8958 std::vector<symtab_and_line> sals;
8959 sals.reserve (markers.size ());
8960
8961 for (const static_tracepoint_marker &marker : markers)
8962 {
8963 symtab_and_line sal = find_pc_line (marker.address, 0);
8964 sal.pc = marker.address;
8965 sals.push_back (sal);
8966 }
8967
8968 *arg_p = endp;
8969 return sals;
8970 }
8971
8972 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8973 according to IS_TRACEPOINT. */
8974
8975 static const struct breakpoint_ops *
8976 breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
8977 bool is_tracepoint)
8978 {
8979 if (is_tracepoint)
8980 {
8981 if (locspec_type == PROBE_LOCATION_SPEC)
8982 return &tracepoint_probe_breakpoint_ops;
8983 else
8984 return &code_breakpoint_ops;
8985 }
8986 else
8987 {
8988 if (locspec_type == PROBE_LOCATION_SPEC)
8989 return &bkpt_probe_breakpoint_ops;
8990 else
8991 return &code_breakpoint_ops;
8992 }
8993 }
8994
8995 /* See breakpoint.h. */
8996
8997 const struct breakpoint_ops *
8998 breakpoint_ops_for_location_spec (const location_spec *locspec,
8999 bool is_tracepoint)
9000 {
9001 if (locspec != nullptr)
9002 return (breakpoint_ops_for_location_spec_type
9003 (locspec->type (), is_tracepoint));
9004 return &code_breakpoint_ops;
9005 }
9006
9007 /* See breakpoint.h. */
9008
9009 int
9010 create_breakpoint (struct gdbarch *gdbarch,
9011 location_spec *locspec,
9012 const char *cond_string,
9013 int thread, const char *extra_string,
9014 bool force_condition, int parse_extra,
9015 int tempflag, enum bptype type_wanted,
9016 int ignore_count,
9017 enum auto_boolean pending_break_support,
9018 const struct breakpoint_ops *ops,
9019 int from_tty, int enabled, int internal,
9020 unsigned flags)
9021 {
9022 struct linespec_result canonical;
9023 bool pending = false;
9024 int task = -1;
9025 int prev_bkpt_count = breakpoint_count;
9026
9027 gdb_assert (ops != NULL);
9028
9029 /* If extra_string isn't useful, set it to NULL. */
9030 if (extra_string != NULL && *extra_string == '\0')
9031 extra_string = NULL;
9032
9033 try
9034 {
9035 ops->create_sals_from_location_spec (locspec, &canonical);
9036 }
9037 catch (const gdb_exception_error &e)
9038 {
9039 /* If caller is interested in rc value from parse, set
9040 value. */
9041 if (e.error == NOT_FOUND_ERROR)
9042 {
9043 /* If pending breakpoint support is turned off, throw
9044 error. */
9045
9046 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9047 throw;
9048
9049 exception_print (gdb_stderr, e);
9050
9051 /* If pending breakpoint support is auto query and the user
9052 selects no, then simply return the error code. */
9053 if (pending_break_support == AUTO_BOOLEAN_AUTO
9054 && !nquery (_("Make %s pending on future shared library load? "),
9055 bptype_string (type_wanted)))
9056 return 0;
9057
9058 /* At this point, either the user was queried about setting
9059 a pending breakpoint and selected yes, or pending
9060 breakpoint behavior is on and thus a pending breakpoint
9061 is defaulted on behalf of the user. */
9062 pending = true;
9063 }
9064 else
9065 throw;
9066 }
9067
9068 if (!pending && canonical.lsals.empty ())
9069 return 0;
9070
9071 /* Resolve all line numbers to PC's and verify that the addresses
9072 are ok for the target. */
9073 if (!pending)
9074 {
9075 for (auto &lsal : canonical.lsals)
9076 breakpoint_sals_to_pc (lsal.sals);
9077 }
9078
9079 /* Fast tracepoints may have additional restrictions on location. */
9080 if (!pending && type_wanted == bp_fast_tracepoint)
9081 {
9082 for (const auto &lsal : canonical.lsals)
9083 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9084 }
9085
9086 /* Verify that condition can be parsed, before setting any
9087 breakpoints. Allocate a separate condition expression for each
9088 breakpoint. */
9089 if (!pending)
9090 {
9091 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9092 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9093
9094 if (parse_extra)
9095 {
9096 gdb::unique_xmalloc_ptr<char> rest;
9097 gdb::unique_xmalloc_ptr<char> cond;
9098
9099 const linespec_sals &lsal = canonical.lsals[0];
9100
9101 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9102 &cond, &thread, &task, &rest);
9103 cond_string_copy = std::move (cond);
9104 extra_string_copy = std::move (rest);
9105 }
9106 else
9107 {
9108 if (type_wanted != bp_dprintf
9109 && extra_string != NULL && *extra_string != '\0')
9110 error (_("Garbage '%s' at end of location"), extra_string);
9111
9112 /* Check the validity of the condition. We should error out
9113 if the condition is invalid at all of the locations and
9114 if it is not forced. In the PARSE_EXTRA case above, this
9115 check is done when parsing the EXTRA_STRING. */
9116 if (cond_string != nullptr && !force_condition)
9117 {
9118 int num_failures = 0;
9119 const linespec_sals &lsal = canonical.lsals[0];
9120 for (const auto &sal : lsal.sals)
9121 {
9122 const char *cond = cond_string;
9123 try
9124 {
9125 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9126 /* One success is sufficient to keep going. */
9127 break;
9128 }
9129 catch (const gdb_exception_error &)
9130 {
9131 num_failures++;
9132 /* If this is the last sal, error out. */
9133 if (num_failures == lsal.sals.size ())
9134 throw;
9135 }
9136 }
9137 }
9138
9139 /* Create a private copy of condition string. */
9140 if (cond_string)
9141 cond_string_copy.reset (xstrdup (cond_string));
9142 /* Create a private copy of any extra string. */
9143 if (extra_string)
9144 extra_string_copy.reset (xstrdup (extra_string));
9145 }
9146
9147 ops->create_breakpoints_sal (gdbarch, &canonical,
9148 std::move (cond_string_copy),
9149 std::move (extra_string_copy),
9150 type_wanted,
9151 tempflag ? disp_del : disp_donttouch,
9152 thread, task, ignore_count,
9153 from_tty, enabled, internal, flags);
9154 }
9155 else
9156 {
9157 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
9158 type_wanted);
9159 b->locspec = locspec->clone ();
9160
9161 if (parse_extra)
9162 b->cond_string = NULL;
9163 else
9164 {
9165 /* Create a private copy of condition string. */
9166 b->cond_string.reset (cond_string != NULL
9167 ? xstrdup (cond_string)
9168 : NULL);
9169 b->thread = thread;
9170 }
9171
9172 /* Create a private copy of any extra string. */
9173 b->extra_string.reset (extra_string != NULL
9174 ? xstrdup (extra_string)
9175 : NULL);
9176 b->ignore_count = ignore_count;
9177 b->disposition = tempflag ? disp_del : disp_donttouch;
9178 b->condition_not_parsed = 1;
9179 b->enable_state = enabled ? bp_enabled : bp_disabled;
9180 if ((type_wanted != bp_breakpoint
9181 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9182 b->pspace = current_program_space;
9183
9184 install_breakpoint (internal, std::move (b), 0);
9185 }
9186
9187 if (canonical.lsals.size () > 1)
9188 {
9189 warning (_("Multiple breakpoints were set.\nUse the "
9190 "\"delete\" command to delete unwanted breakpoints."));
9191 prev_breakpoint_count = prev_bkpt_count;
9192 }
9193
9194 update_global_location_list (UGLL_MAY_INSERT);
9195
9196 return 1;
9197 }
9198
9199 /* Set a breakpoint.
9200 ARG is a string describing breakpoint address,
9201 condition, and thread.
9202 FLAG specifies if a breakpoint is hardware on,
9203 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9204 and BP_TEMPFLAG. */
9205
9206 static void
9207 break_command_1 (const char *arg, int flag, int from_tty)
9208 {
9209 int tempflag = flag & BP_TEMPFLAG;
9210 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9211 ? bp_hardware_breakpoint
9212 : bp_breakpoint);
9213
9214 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9215 const struct breakpoint_ops *ops
9216 = breakpoint_ops_for_location_spec (locspec.get (),
9217 false /* is_tracepoint */);
9218
9219 create_breakpoint (get_current_arch (),
9220 locspec.get (),
9221 NULL, 0, arg, false, 1 /* parse arg */,
9222 tempflag, type_wanted,
9223 0 /* Ignore count */,
9224 pending_break_support,
9225 ops,
9226 from_tty,
9227 1 /* enabled */,
9228 0 /* internal */,
9229 0);
9230 }
9231
9232 /* Helper function for break_command_1 and disassemble_command. */
9233
9234 void
9235 resolve_sal_pc (struct symtab_and_line *sal)
9236 {
9237 CORE_ADDR pc;
9238
9239 if (sal->pc == 0 && sal->symtab != NULL)
9240 {
9241 if (!find_line_pc (sal->symtab, sal->line, &pc))
9242 error (_("No line %d in file \"%s\"."),
9243 sal->line, symtab_to_filename_for_display (sal->symtab));
9244 sal->pc = pc;
9245
9246 /* If this SAL corresponds to a breakpoint inserted using a line
9247 number, then skip the function prologue if necessary. */
9248 if (sal->explicit_line)
9249 skip_prologue_sal (sal);
9250 }
9251
9252 if (sal->section == 0 && sal->symtab != NULL)
9253 {
9254 const struct blockvector *bv;
9255 const struct block *b;
9256 struct symbol *sym;
9257
9258 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9259 sal->symtab->compunit ());
9260 if (bv != NULL)
9261 {
9262 sym = b->linkage_function ();
9263 if (sym != NULL)
9264 sal->section
9265 = sym->obj_section (sal->symtab->compunit ()->objfile ());
9266 else
9267 {
9268 /* It really is worthwhile to have the section, so we'll
9269 just have to look harder. This case can be executed
9270 if we have line numbers but no functions (as can
9271 happen in assembly source). */
9272
9273 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9274 switch_to_program_space_and_thread (sal->pspace);
9275
9276 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9277 if (msym.minsym)
9278 sal->section = msym.obj_section ();
9279 }
9280 }
9281 }
9282 }
9283
9284 void
9285 break_command (const char *arg, int from_tty)
9286 {
9287 break_command_1 (arg, 0, from_tty);
9288 }
9289
9290 void
9291 tbreak_command (const char *arg, int from_tty)
9292 {
9293 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9294 }
9295
9296 static void
9297 hbreak_command (const char *arg, int from_tty)
9298 {
9299 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9300 }
9301
9302 static void
9303 thbreak_command (const char *arg, int from_tty)
9304 {
9305 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9306 }
9307
9308 /* The dynamic printf command is mostly like a regular breakpoint, but
9309 with a prewired command list consisting of a single output command,
9310 built from extra arguments supplied on the dprintf command
9311 line. */
9312
9313 static void
9314 dprintf_command (const char *arg, int from_tty)
9315 {
9316 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9317
9318 /* If non-NULL, ARG should have been advanced past the location;
9319 the next character must be ','. */
9320 if (arg != NULL)
9321 {
9322 if (arg[0] != ',' || arg[1] == '\0')
9323 error (_("Format string required"));
9324 else
9325 {
9326 /* Skip the comma. */
9327 ++arg;
9328 }
9329 }
9330
9331 create_breakpoint (get_current_arch (),
9332 locspec.get (),
9333 NULL, 0, arg, false, 1 /* parse arg */,
9334 0, bp_dprintf,
9335 0 /* Ignore count */,
9336 pending_break_support,
9337 &code_breakpoint_ops,
9338 from_tty,
9339 1 /* enabled */,
9340 0 /* internal */,
9341 0);
9342 }
9343
9344 static void
9345 agent_printf_command (const char *arg, int from_tty)
9346 {
9347 error (_("May only run agent-printf on the target"));
9348 }
9349
9350 /* Implement the "breakpoint_hit" method for ranged breakpoints. */
9351
9352 int
9353 ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9354 const address_space *aspace,
9355 CORE_ADDR bp_addr,
9356 const target_waitstatus &ws)
9357 {
9358 if (ws.kind () != TARGET_WAITKIND_STOPPED
9359 || ws.sig () != GDB_SIGNAL_TRAP)
9360 return 0;
9361
9362 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9363 bl->length, aspace, bp_addr);
9364 }
9365
9366 /* Implement the "resources_needed" method for ranged breakpoints. */
9367
9368 int
9369 ranged_breakpoint::resources_needed (const struct bp_location *bl)
9370 {
9371 return target_ranged_break_num_registers ();
9372 }
9373
9374 /* Implement the "print_it" method for ranged breakpoints. */
9375
9376 enum print_stop_action
9377 ranged_breakpoint::print_it (const bpstat *bs) const
9378 {
9379 struct bp_location *bl = loc;
9380 struct ui_out *uiout = current_uiout;
9381
9382 gdb_assert (type == bp_hardware_breakpoint);
9383
9384 /* Ranged breakpoints have only one location. */
9385 gdb_assert (bl && bl->next == NULL);
9386
9387 annotate_breakpoint (number);
9388
9389 maybe_print_thread_hit_breakpoint (uiout);
9390
9391 if (disposition == disp_del)
9392 uiout->text ("Temporary ranged breakpoint ");
9393 else
9394 uiout->text ("Ranged breakpoint ");
9395 if (uiout->is_mi_like_p ())
9396 {
9397 uiout->field_string ("reason",
9398 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9399 uiout->field_string ("disp", bpdisp_text (disposition));
9400 }
9401 print_num_locno (bs, uiout);
9402 uiout->text (", ");
9403
9404 return PRINT_SRC_AND_LOC;
9405 }
9406
9407 /* Implement the "print_one" method for ranged breakpoints. */
9408
9409 bool
9410 ranged_breakpoint::print_one (bp_location **last_loc) const
9411 {
9412 struct bp_location *bl = loc;
9413 struct value_print_options opts;
9414 struct ui_out *uiout = current_uiout;
9415
9416 /* Ranged breakpoints have only one location. */
9417 gdb_assert (bl && bl->next == NULL);
9418
9419 get_user_print_options (&opts);
9420
9421 if (opts.addressprint)
9422 /* We don't print the address range here, it will be printed later
9423 by ranged_breakpoint::print_one_detail. */
9424 uiout->field_skip ("addr");
9425 annotate_field (5);
9426 print_breakpoint_location (this, bl);
9427 *last_loc = bl;
9428
9429 return true;
9430 }
9431
9432 /* Implement the "print_one_detail" method for ranged breakpoints. */
9433
9434 void
9435 ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
9436 {
9437 CORE_ADDR address_start, address_end;
9438 struct bp_location *bl = loc;
9439 string_file stb;
9440
9441 gdb_assert (bl);
9442
9443 address_start = bl->address;
9444 address_end = address_start + bl->length - 1;
9445
9446 uiout->text ("\taddress range: ");
9447 stb.printf ("[%s, %s]",
9448 print_core_address (bl->gdbarch, address_start),
9449 print_core_address (bl->gdbarch, address_end));
9450 uiout->field_stream ("addr", stb);
9451 uiout->text ("\n");
9452 }
9453
9454 /* Implement the "print_mention" method for ranged breakpoints. */
9455
9456 void
9457 ranged_breakpoint::print_mention () const
9458 {
9459 struct bp_location *bl = loc;
9460 struct ui_out *uiout = current_uiout;
9461
9462 gdb_assert (bl);
9463 gdb_assert (type == bp_hardware_breakpoint);
9464
9465 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9466 number, paddress (bl->gdbarch, bl->address),
9467 paddress (bl->gdbarch, bl->address + bl->length - 1));
9468 }
9469
9470 /* Implement the "print_recreate" method for ranged breakpoints. */
9471
9472 void
9473 ranged_breakpoint::print_recreate (struct ui_file *fp) const
9474 {
9475 gdb_printf (fp, "break-range %s, %s",
9476 locspec->to_string (),
9477 locspec_range_end->to_string ());
9478 print_recreate_thread (fp);
9479 }
9480
9481 /* Find the address where the end of the breakpoint range should be
9482 placed, given the SAL of the end of the range. This is so that if
9483 the user provides a line number, the end of the range is set to the
9484 last instruction of the given line. */
9485
9486 static CORE_ADDR
9487 find_breakpoint_range_end (struct symtab_and_line sal)
9488 {
9489 CORE_ADDR end;
9490
9491 /* If the user provided a PC value, use it. Otherwise,
9492 find the address of the end of the given location. */
9493 if (sal.explicit_pc)
9494 end = sal.pc;
9495 else
9496 {
9497 int ret;
9498 CORE_ADDR start;
9499
9500 ret = find_line_pc_range (sal, &start, &end);
9501 if (!ret)
9502 error (_("Could not find location of the end of the range."));
9503
9504 /* find_line_pc_range returns the start of the next line. */
9505 end--;
9506 }
9507
9508 return end;
9509 }
9510
9511 /* Implement the "break-range" CLI command. */
9512
9513 static void
9514 break_range_command (const char *arg, int from_tty)
9515 {
9516 const char *arg_start;
9517 struct linespec_result canonical_start, canonical_end;
9518 int bp_count, can_use_bp, length;
9519 CORE_ADDR end;
9520
9521 /* We don't support software ranged breakpoints. */
9522 if (target_ranged_break_num_registers () < 0)
9523 error (_("This target does not support hardware ranged breakpoints."));
9524
9525 bp_count = hw_breakpoint_used_count ();
9526 bp_count += target_ranged_break_num_registers ();
9527 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9528 bp_count, 0);
9529 if (can_use_bp < 0)
9530 error (_("Hardware breakpoints used exceeds limit."));
9531
9532 arg = skip_spaces (arg);
9533 if (arg == NULL || arg[0] == '\0')
9534 error(_("No address range specified."));
9535
9536 arg_start = arg;
9537 location_spec_up start_locspec
9538 = string_to_location_spec (&arg, current_language);
9539 parse_breakpoint_sals (start_locspec.get (), &canonical_start);
9540
9541 if (arg[0] != ',')
9542 error (_("Too few arguments."));
9543 else if (canonical_start.lsals.empty ())
9544 error (_("Could not find location of the beginning of the range."));
9545
9546 const linespec_sals &lsal_start = canonical_start.lsals[0];
9547
9548 if (canonical_start.lsals.size () > 1
9549 || lsal_start.sals.size () != 1)
9550 error (_("Cannot create a ranged breakpoint with multiple locations."));
9551
9552 const symtab_and_line &sal_start = lsal_start.sals[0];
9553 std::string addr_string_start (arg_start, arg - arg_start);
9554
9555 arg++; /* Skip the comma. */
9556 arg = skip_spaces (arg);
9557
9558 /* Parse the end location specification. */
9559
9560 arg_start = arg;
9561
9562 /* We call decode_line_full directly here instead of using
9563 parse_breakpoint_sals because we need to specify the start
9564 location spec's symtab and line as the default symtab and line
9565 for the end of the range. This makes it possible to have ranges
9566 like "foo.c:27, +14", where +14 means 14 lines from the start
9567 location spec. */
9568 location_spec_up end_locspec
9569 = string_to_location_spec (&arg, current_language);
9570 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9571 sal_start.symtab, sal_start.line,
9572 &canonical_end, NULL, NULL);
9573
9574 if (canonical_end.lsals.empty ())
9575 error (_("Could not find location of the end of the range."));
9576
9577 const linespec_sals &lsal_end = canonical_end.lsals[0];
9578 if (canonical_end.lsals.size () > 1
9579 || lsal_end.sals.size () != 1)
9580 error (_("Cannot create a ranged breakpoint with multiple locations."));
9581
9582 const symtab_and_line &sal_end = lsal_end.sals[0];
9583
9584 end = find_breakpoint_range_end (sal_end);
9585 if (sal_start.pc > end)
9586 error (_("Invalid address range, end precedes start."));
9587
9588 length = end - sal_start.pc + 1;
9589 if (length < 0)
9590 /* Length overflowed. */
9591 error (_("Address range too large."));
9592 else if (length == 1)
9593 {
9594 /* This range is simple enough to be handled by
9595 the `hbreak' command. */
9596 hbreak_command (&addr_string_start[0], 1);
9597
9598 return;
9599 }
9600
9601 /* Now set up the breakpoint and install it. */
9602
9603 std::unique_ptr<breakpoint> br
9604 (new ranged_breakpoint (get_current_arch (),
9605 sal_start, length,
9606 std::move (start_locspec),
9607 std::move (end_locspec)));
9608
9609 install_breakpoint (false, std::move (br), true);
9610 }
9611
9612 /* Return non-zero if EXP is verified as constant. Returned zero
9613 means EXP is variable. Also the constant detection may fail for
9614 some constant expressions and in such case still falsely return
9615 zero. */
9616
9617 static bool
9618 watchpoint_exp_is_const (const struct expression *exp)
9619 {
9620 return exp->op->constant_p ();
9621 }
9622
9623 /* Implement the "re_set" method for watchpoints. */
9624
9625 void
9626 watchpoint::re_set ()
9627 {
9628 /* Watchpoint can be either on expression using entirely global
9629 variables, or it can be on local variables.
9630
9631 Watchpoints of the first kind are never auto-deleted, and even
9632 persist across program restarts. Since they can use variables
9633 from shared libraries, we need to reparse expression as libraries
9634 are loaded and unloaded.
9635
9636 Watchpoints on local variables can also change meaning as result
9637 of solib event. For example, if a watchpoint uses both a local
9638 and a global variables in expression, it's a local watchpoint,
9639 but unloading of a shared library will make the expression
9640 invalid. This is not a very common use case, but we still
9641 re-evaluate expression, to avoid surprises to the user.
9642
9643 Note that for local watchpoints, we re-evaluate it only if
9644 watchpoints frame id is still valid. If it's not, it means the
9645 watchpoint is out of scope and will be deleted soon. In fact,
9646 I'm not sure we'll ever be called in this case.
9647
9648 If a local watchpoint's frame id is still valid, then
9649 exp_valid_block is likewise valid, and we can safely use it.
9650
9651 Don't do anything about disabled watchpoints, since they will be
9652 reevaluated again when enabled. */
9653 update_watchpoint (this, true /* reparse */);
9654 }
9655
9656 /* Implement the "insert" method for hardware watchpoints. */
9657
9658 int
9659 watchpoint::insert_location (struct bp_location *bl)
9660 {
9661 int length = exact ? 1 : bl->length;
9662
9663 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9664 cond_exp.get ());
9665 }
9666
9667 /* Implement the "remove" method for hardware watchpoints. */
9668
9669 int
9670 watchpoint::remove_location (struct bp_location *bl,
9671 enum remove_bp_reason reason)
9672 {
9673 int length = exact ? 1 : bl->length;
9674
9675 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9676 cond_exp.get ());
9677 }
9678
9679 int
9680 watchpoint::breakpoint_hit (const struct bp_location *bl,
9681 const address_space *aspace, CORE_ADDR bp_addr,
9682 const target_waitstatus &ws)
9683 {
9684 struct breakpoint *b = bl->owner;
9685
9686 /* Continuable hardware watchpoints are treated as non-existent if the
9687 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9688 some data address). Otherwise gdb won't stop on a break instruction
9689 in the code (not from a breakpoint) when a hardware watchpoint has
9690 been defined. Also skip watchpoints which we know did not trigger
9691 (did not match the data address). */
9692 if (is_hardware_watchpoint (b)
9693 && watchpoint_triggered == watch_triggered_no)
9694 return 0;
9695
9696 return 1;
9697 }
9698
9699 void
9700 watchpoint::check_status (bpstat *bs)
9701 {
9702 gdb_assert (is_watchpoint (bs->breakpoint_at));
9703
9704 bpstat_check_watchpoint (bs);
9705 }
9706
9707 /* Implement the "resources_needed" method for hardware
9708 watchpoints. */
9709
9710 int
9711 watchpoint::resources_needed (const struct bp_location *bl)
9712 {
9713 int length = exact? 1 : bl->length;
9714
9715 return target_region_ok_for_hw_watchpoint (bl->address, length);
9716 }
9717
9718 /* Implement the "works_in_software_mode" method for hardware
9719 watchpoints. */
9720
9721 bool
9722 watchpoint::works_in_software_mode () const
9723 {
9724 /* Read and access watchpoints only work with hardware support. */
9725 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9726 }
9727
9728 enum print_stop_action
9729 watchpoint::print_it (const bpstat *bs) const
9730 {
9731 struct breakpoint *b;
9732 enum print_stop_action result;
9733 struct ui_out *uiout = current_uiout;
9734
9735 gdb_assert (bs->bp_location_at != NULL);
9736
9737 b = bs->breakpoint_at;
9738
9739 annotate_watchpoint (b->number);
9740 maybe_print_thread_hit_breakpoint (uiout);
9741
9742 string_file stb;
9743
9744 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9745 switch (b->type)
9746 {
9747 case bp_watchpoint:
9748 case bp_hardware_watchpoint:
9749 if (uiout->is_mi_like_p ())
9750 uiout->field_string
9751 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9752 mention (b);
9753 tuple_emitter.emplace (uiout, "value");
9754 uiout->text ("\nOld value = ");
9755 watchpoint_value_print (bs->old_val.get (), &stb);
9756 uiout->field_stream ("old", stb);
9757 uiout->text ("\nNew value = ");
9758 watchpoint_value_print (val.get (), &stb);
9759 uiout->field_stream ("new", stb);
9760 uiout->text ("\n");
9761 /* More than one watchpoint may have been triggered. */
9762 result = PRINT_UNKNOWN;
9763 break;
9764
9765 case bp_read_watchpoint:
9766 if (uiout->is_mi_like_p ())
9767 uiout->field_string
9768 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9769 mention (b);
9770 tuple_emitter.emplace (uiout, "value");
9771 uiout->text ("\nValue = ");
9772 watchpoint_value_print (val.get (), &stb);
9773 uiout->field_stream ("value", stb);
9774 uiout->text ("\n");
9775 result = PRINT_UNKNOWN;
9776 break;
9777
9778 case bp_access_watchpoint:
9779 if (bs->old_val != NULL)
9780 {
9781 if (uiout->is_mi_like_p ())
9782 uiout->field_string
9783 ("reason",
9784 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9785 mention (b);
9786 tuple_emitter.emplace (uiout, "value");
9787 uiout->text ("\nOld value = ");
9788 watchpoint_value_print (bs->old_val.get (), &stb);
9789 uiout->field_stream ("old", stb);
9790 uiout->text ("\nNew value = ");
9791 }
9792 else
9793 {
9794 mention (b);
9795 if (uiout->is_mi_like_p ())
9796 uiout->field_string
9797 ("reason",
9798 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9799 tuple_emitter.emplace (uiout, "value");
9800 uiout->text ("\nValue = ");
9801 }
9802 watchpoint_value_print (val.get (), &stb);
9803 uiout->field_stream ("new", stb);
9804 uiout->text ("\n");
9805 result = PRINT_UNKNOWN;
9806 break;
9807 default:
9808 result = PRINT_UNKNOWN;
9809 }
9810
9811 return result;
9812 }
9813
9814 /* Implement the "print_mention" method for hardware watchpoints. */
9815
9816 void
9817 watchpoint::print_mention () const
9818 {
9819 struct ui_out *uiout = current_uiout;
9820 const char *tuple_name;
9821
9822 switch (type)
9823 {
9824 case bp_watchpoint:
9825 uiout->text ("Watchpoint ");
9826 tuple_name = "wpt";
9827 break;
9828 case bp_hardware_watchpoint:
9829 uiout->text ("Hardware watchpoint ");
9830 tuple_name = "wpt";
9831 break;
9832 case bp_read_watchpoint:
9833 uiout->text ("Hardware read watchpoint ");
9834 tuple_name = "hw-rwpt";
9835 break;
9836 case bp_access_watchpoint:
9837 uiout->text ("Hardware access (read/write) watchpoint ");
9838 tuple_name = "hw-awpt";
9839 break;
9840 default:
9841 internal_error (_("Invalid hardware watchpoint type."));
9842 }
9843
9844 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9845 uiout->field_signed ("number", number);
9846 uiout->text (": ");
9847 uiout->field_string ("exp", exp_string.get ());
9848 }
9849
9850 /* Implement the "print_recreate" method for watchpoints. */
9851
9852 void
9853 watchpoint::print_recreate (struct ui_file *fp) const
9854 {
9855 switch (type)
9856 {
9857 case bp_watchpoint:
9858 case bp_hardware_watchpoint:
9859 gdb_printf (fp, "watch");
9860 break;
9861 case bp_read_watchpoint:
9862 gdb_printf (fp, "rwatch");
9863 break;
9864 case bp_access_watchpoint:
9865 gdb_printf (fp, "awatch");
9866 break;
9867 default:
9868 internal_error (_("Invalid watchpoint type."));
9869 }
9870
9871 gdb_printf (fp, " %s", exp_string.get ());
9872 print_recreate_thread (fp);
9873 }
9874
9875 /* Implement the "explains_signal" method for watchpoints. */
9876
9877 bool
9878 watchpoint::explains_signal (enum gdb_signal sig)
9879 {
9880 /* A software watchpoint cannot cause a signal other than
9881 GDB_SIGNAL_TRAP. */
9882 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
9883 return false;
9884
9885 return true;
9886 }
9887
9888 struct masked_watchpoint : public watchpoint
9889 {
9890 using watchpoint::watchpoint;
9891
9892 int insert_location (struct bp_location *) override;
9893 int remove_location (struct bp_location *,
9894 enum remove_bp_reason reason) override;
9895 int resources_needed (const struct bp_location *) override;
9896 bool works_in_software_mode () const override;
9897 enum print_stop_action print_it (const bpstat *bs) const override;
9898 void print_one_detail (struct ui_out *) const override;
9899 void print_mention () const override;
9900 void print_recreate (struct ui_file *fp) const override;
9901 };
9902
9903 /* Implement the "insert" method for masked hardware watchpoints. */
9904
9905 int
9906 masked_watchpoint::insert_location (struct bp_location *bl)
9907 {
9908 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
9909 bl->watchpoint_type);
9910 }
9911
9912 /* Implement the "remove" method for masked hardware watchpoints. */
9913
9914 int
9915 masked_watchpoint::remove_location (struct bp_location *bl,
9916 enum remove_bp_reason reason)
9917 {
9918 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
9919 bl->watchpoint_type);
9920 }
9921
9922 /* Implement the "resources_needed" method for masked hardware
9923 watchpoints. */
9924
9925 int
9926 masked_watchpoint::resources_needed (const struct bp_location *bl)
9927 {
9928 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
9929 }
9930
9931 /* Implement the "works_in_software_mode" method for masked hardware
9932 watchpoints. */
9933
9934 bool
9935 masked_watchpoint::works_in_software_mode () const
9936 {
9937 return false;
9938 }
9939
9940 /* Implement the "print_it" method for masked hardware
9941 watchpoints. */
9942
9943 enum print_stop_action
9944 masked_watchpoint::print_it (const bpstat *bs) const
9945 {
9946 struct breakpoint *b = bs->breakpoint_at;
9947 struct ui_out *uiout = current_uiout;
9948
9949 /* Masked watchpoints have only one location. */
9950 gdb_assert (b->loc && b->loc->next == NULL);
9951
9952 annotate_watchpoint (b->number);
9953 maybe_print_thread_hit_breakpoint (uiout);
9954
9955 switch (b->type)
9956 {
9957 case bp_hardware_watchpoint:
9958 if (uiout->is_mi_like_p ())
9959 uiout->field_string
9960 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9961 break;
9962
9963 case bp_read_watchpoint:
9964 if (uiout->is_mi_like_p ())
9965 uiout->field_string
9966 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9967 break;
9968
9969 case bp_access_watchpoint:
9970 if (uiout->is_mi_like_p ())
9971 uiout->field_string
9972 ("reason",
9973 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9974 break;
9975 default:
9976 internal_error (_("Invalid hardware watchpoint type."));
9977 }
9978
9979 mention (b);
9980 uiout->text (_("\n\
9981 Check the underlying instruction at PC for the memory\n\
9982 address and value which triggered this watchpoint.\n"));
9983 uiout->text ("\n");
9984
9985 /* More than one watchpoint may have been triggered. */
9986 return PRINT_UNKNOWN;
9987 }
9988
9989 /* Implement the "print_one_detail" method for masked hardware
9990 watchpoints. */
9991
9992 void
9993 masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9994 {
9995 /* Masked watchpoints have only one location. */
9996 gdb_assert (loc && loc->next == NULL);
9997
9998 uiout->text ("\tmask ");
9999 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
10000 uiout->text ("\n");
10001 }
10002
10003 /* Implement the "print_mention" method for masked hardware
10004 watchpoints. */
10005
10006 void
10007 masked_watchpoint::print_mention () const
10008 {
10009 struct ui_out *uiout = current_uiout;
10010 const char *tuple_name;
10011
10012 switch (type)
10013 {
10014 case bp_hardware_watchpoint:
10015 uiout->text ("Masked hardware watchpoint ");
10016 tuple_name = "wpt";
10017 break;
10018 case bp_read_watchpoint:
10019 uiout->text ("Masked hardware read watchpoint ");
10020 tuple_name = "hw-rwpt";
10021 break;
10022 case bp_access_watchpoint:
10023 uiout->text ("Masked hardware access (read/write) watchpoint ");
10024 tuple_name = "hw-awpt";
10025 break;
10026 default:
10027 internal_error (_("Invalid hardware watchpoint type."));
10028 }
10029
10030 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10031 uiout->field_signed ("number", number);
10032 uiout->text (": ");
10033 uiout->field_string ("exp", exp_string.get ());
10034 }
10035
10036 /* Implement the "print_recreate" method for masked hardware
10037 watchpoints. */
10038
10039 void
10040 masked_watchpoint::print_recreate (struct ui_file *fp) const
10041 {
10042 switch (type)
10043 {
10044 case bp_hardware_watchpoint:
10045 gdb_printf (fp, "watch");
10046 break;
10047 case bp_read_watchpoint:
10048 gdb_printf (fp, "rwatch");
10049 break;
10050 case bp_access_watchpoint:
10051 gdb_printf (fp, "awatch");
10052 break;
10053 default:
10054 internal_error (_("Invalid hardware watchpoint type."));
10055 }
10056
10057 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
10058 phex (hw_wp_mask, sizeof (CORE_ADDR)));
10059 print_recreate_thread (fp);
10060 }
10061
10062 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10063
10064 static bool
10065 is_masked_watchpoint (const struct breakpoint *b)
10066 {
10067 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
10068 }
10069
10070 /* accessflag: hw_write: watch write,
10071 hw_read: watch read,
10072 hw_access: watch access (read or write) */
10073 static void
10074 watch_command_1 (const char *arg, int accessflag, int from_tty,
10075 bool just_location, bool internal)
10076 {
10077 struct breakpoint *scope_breakpoint = NULL;
10078 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10079 struct value *result;
10080 int saved_bitpos = 0, saved_bitsize = 0;
10081 const char *exp_start = NULL;
10082 const char *exp_end = NULL;
10083 const char *tok, *end_tok;
10084 int toklen = -1;
10085 const char *cond_start = NULL;
10086 const char *cond_end = NULL;
10087 enum bptype bp_type;
10088 int thread = -1;
10089 /* Flag to indicate whether we are going to use masks for
10090 the hardware watchpoint. */
10091 bool use_mask = false;
10092 CORE_ADDR mask = 0;
10093 int task = -1;
10094
10095 /* Make sure that we actually have parameters to parse. */
10096 if (arg != NULL && arg[0] != '\0')
10097 {
10098 const char *value_start;
10099
10100 exp_end = arg + strlen (arg);
10101
10102 /* Look for "parameter value" pairs at the end
10103 of the arguments string. */
10104 for (tok = exp_end - 1; tok > arg; tok--)
10105 {
10106 /* Skip whitespace at the end of the argument list. */
10107 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10108 tok--;
10109
10110 /* Find the beginning of the last token.
10111 This is the value of the parameter. */
10112 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10113 tok--;
10114 value_start = tok + 1;
10115
10116 /* Skip whitespace. */
10117 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10118 tok--;
10119
10120 end_tok = tok;
10121
10122 /* Find the beginning of the second to last token.
10123 This is the parameter itself. */
10124 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10125 tok--;
10126 tok++;
10127 toklen = end_tok - tok + 1;
10128
10129 if (toklen == 6 && startswith (tok, "thread"))
10130 {
10131 struct thread_info *thr;
10132 /* At this point we've found a "thread" token, which means
10133 the user is trying to set a watchpoint that triggers
10134 only in a specific thread. */
10135 const char *endp;
10136
10137 if (thread != -1)
10138 error(_("You can specify only one thread."));
10139
10140 if (task != -1)
10141 error (_("You can specify only one of thread or task."));
10142
10143 /* Extract the thread ID from the next token. */
10144 thr = parse_thread_id (value_start, &endp);
10145
10146 /* Check if the user provided a valid thread ID. */
10147 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10148 invalid_thread_id_error (value_start);
10149
10150 thread = thr->global_num;
10151 }
10152 else if (toklen == 4 && startswith (tok, "task"))
10153 {
10154 char *tmp;
10155
10156 if (task != -1)
10157 error(_("You can specify only one task."));
10158
10159 if (thread != -1)
10160 error (_("You can specify only one of thread or task."));
10161
10162 task = strtol (value_start, &tmp, 0);
10163 if (tmp == value_start)
10164 error (_("Junk after task keyword."));
10165 if (!valid_task_id (task))
10166 error (_("Unknown task %d."), task);
10167 }
10168 else if (toklen == 4 && startswith (tok, "mask"))
10169 {
10170 /* We've found a "mask" token, which means the user wants to
10171 create a hardware watchpoint that is going to have the mask
10172 facility. */
10173 struct value *mask_value;
10174
10175 if (use_mask)
10176 error(_("You can specify only one mask."));
10177
10178 use_mask = just_location = true;
10179
10180 scoped_value_mark mark;
10181 mask_value = parse_to_comma_and_eval (&value_start);
10182 mask = value_as_address (mask_value);
10183 }
10184 else
10185 /* We didn't recognize what we found. We should stop here. */
10186 break;
10187
10188 /* Truncate the string and get rid of the "parameter value" pair before
10189 the arguments string is parsed by the parse_exp_1 function. */
10190 exp_end = tok;
10191 }
10192 }
10193 else
10194 exp_end = arg;
10195
10196 /* Parse the rest of the arguments. From here on out, everything
10197 is in terms of a newly allocated string instead of the original
10198 ARG. */
10199 std::string expression (arg, exp_end - arg);
10200 exp_start = arg = expression.c_str ();
10201 innermost_block_tracker tracker;
10202 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10203 exp_end = arg;
10204 /* Remove trailing whitespace from the expression before saving it.
10205 This makes the eventual display of the expression string a bit
10206 prettier. */
10207 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10208 --exp_end;
10209
10210 /* Checking if the expression is not constant. */
10211 if (watchpoint_exp_is_const (exp.get ()))
10212 {
10213 int len;
10214
10215 len = exp_end - exp_start;
10216 while (len > 0 && isspace (exp_start[len - 1]))
10217 len--;
10218 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10219 }
10220
10221 exp_valid_block = tracker.block ();
10222 struct value *mark = value_mark ();
10223 struct value *val_as_value = nullptr;
10224 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10225 just_location);
10226
10227 if (val_as_value != NULL && just_location)
10228 {
10229 saved_bitpos = val_as_value->bitpos ();
10230 saved_bitsize = val_as_value->bitsize ();
10231 }
10232
10233 value_ref_ptr val;
10234 if (just_location)
10235 {
10236 int ret;
10237
10238 exp_valid_block = NULL;
10239 val = release_value (value_addr (result));
10240 value_free_to_mark (mark);
10241
10242 if (use_mask)
10243 {
10244 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10245 mask);
10246 if (ret == -1)
10247 error (_("This target does not support masked watchpoints."));
10248 else if (ret == -2)
10249 error (_("Invalid mask or memory region."));
10250 }
10251 }
10252 else if (val_as_value != NULL)
10253 val = release_value (val_as_value);
10254
10255 tok = skip_spaces (arg);
10256 end_tok = skip_to_space (tok);
10257
10258 toklen = end_tok - tok;
10259 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10260 {
10261 tok = cond_start = end_tok + 1;
10262 innermost_block_tracker if_tracker;
10263 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10264
10265 /* The watchpoint expression may not be local, but the condition
10266 may still be. E.g.: `watch global if local > 0'. */
10267 cond_exp_valid_block = if_tracker.block ();
10268
10269 cond_end = tok;
10270 }
10271 if (*tok)
10272 error (_("Junk at end of command."));
10273
10274 frame_info_ptr wp_frame = block_innermost_frame (exp_valid_block);
10275
10276 /* Save this because create_internal_breakpoint below invalidates
10277 'wp_frame'. */
10278 frame_id watchpoint_frame = get_frame_id (wp_frame);
10279
10280 /* If the expression is "local", then set up a "watchpoint scope"
10281 breakpoint at the point where we've left the scope of the watchpoint
10282 expression. Create the scope breakpoint before the watchpoint, so
10283 that we will encounter it first in bpstat_stop_status. */
10284 if (exp_valid_block != NULL && wp_frame != NULL)
10285 {
10286 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10287
10288 if (frame_id_p (caller_frame_id))
10289 {
10290 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10291 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10292
10293 scope_breakpoint
10294 = create_internal_breakpoint (caller_arch, caller_pc,
10295 bp_watchpoint_scope);
10296
10297 /* create_internal_breakpoint could invalidate WP_FRAME. */
10298 wp_frame = NULL;
10299
10300 scope_breakpoint->enable_state = bp_enabled;
10301
10302 /* Automatically delete the breakpoint when it hits. */
10303 scope_breakpoint->disposition = disp_del;
10304
10305 /* Only break in the proper frame (help with recursion). */
10306 scope_breakpoint->frame_id = caller_frame_id;
10307
10308 /* Set the address at which we will stop. */
10309 scope_breakpoint->loc->gdbarch = caller_arch;
10310 scope_breakpoint->loc->requested_address = caller_pc;
10311 scope_breakpoint->loc->address
10312 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10313 scope_breakpoint->loc->requested_address,
10314 scope_breakpoint->type,
10315 current_program_space);
10316 }
10317 }
10318
10319 /* Now set up the breakpoint. We create all watchpoints as hardware
10320 watchpoints here even if hardware watchpoints are turned off, a call
10321 to update_watchpoint later in this function will cause the type to
10322 drop back to bp_watchpoint (software watchpoint) if required. */
10323
10324 if (accessflag == hw_read)
10325 bp_type = bp_read_watchpoint;
10326 else if (accessflag == hw_access)
10327 bp_type = bp_access_watchpoint;
10328 else
10329 bp_type = bp_hardware_watchpoint;
10330
10331 std::unique_ptr<watchpoint> w;
10332 if (use_mask)
10333 w.reset (new masked_watchpoint (nullptr, bp_type));
10334 else
10335 w.reset (new watchpoint (nullptr, bp_type));
10336
10337 /* At most one of thread or task can be set on a watchpoint. */
10338 gdb_assert (thread == -1 || task == -1);
10339 w->thread = thread;
10340 w->task = task;
10341 w->disposition = disp_donttouch;
10342 w->pspace = current_program_space;
10343 w->exp = std::move (exp);
10344 w->exp_valid_block = exp_valid_block;
10345 w->cond_exp_valid_block = cond_exp_valid_block;
10346 if (just_location)
10347 {
10348 struct type *t = val->type ();
10349 CORE_ADDR addr = value_as_address (val.get ());
10350
10351 w->exp_string_reparse
10352 = current_language->watch_location_expression (t, addr);
10353
10354 w->exp_string = xstrprintf ("-location %.*s",
10355 (int) (exp_end - exp_start), exp_start);
10356 }
10357 else
10358 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10359
10360 if (use_mask)
10361 {
10362 w->hw_wp_mask = mask;
10363 }
10364 else
10365 {
10366 w->val = val;
10367 w->val_bitpos = saved_bitpos;
10368 w->val_bitsize = saved_bitsize;
10369 w->val_valid = true;
10370 }
10371
10372 if (cond_start)
10373 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10374 else
10375 w->cond_string = 0;
10376
10377 if (frame_id_p (watchpoint_frame))
10378 {
10379 w->watchpoint_frame = watchpoint_frame;
10380 w->watchpoint_thread = inferior_ptid;
10381 }
10382 else
10383 {
10384 w->watchpoint_frame = null_frame_id;
10385 w->watchpoint_thread = null_ptid;
10386 }
10387
10388 if (scope_breakpoint != NULL)
10389 {
10390 /* The scope breakpoint is related to the watchpoint. We will
10391 need to act on them together. */
10392 w->related_breakpoint = scope_breakpoint;
10393 scope_breakpoint->related_breakpoint = w.get ();
10394 }
10395
10396 if (!just_location)
10397 value_free_to_mark (mark);
10398
10399 /* Finally update the new watchpoint. This creates the locations
10400 that should be inserted. */
10401 update_watchpoint (w.get (), true /* reparse */);
10402
10403 install_breakpoint (internal, std::move (w), 1);
10404 }
10405
10406 /* Return count of debug registers needed to watch the given expression.
10407 If the watchpoint cannot be handled in hardware return zero. */
10408
10409 static int
10410 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10411 {
10412 int found_memory_cnt = 0;
10413
10414 /* Did the user specifically forbid us to use hardware watchpoints? */
10415 if (!can_use_hw_watchpoints)
10416 return 0;
10417
10418 gdb_assert (!vals.empty ());
10419 struct value *head = vals[0].get ();
10420
10421 /* Make sure that the value of the expression depends only upon
10422 memory contents, and values computed from them within GDB. If we
10423 find any register references or function calls, we can't use a
10424 hardware watchpoint.
10425
10426 The idea here is that evaluating an expression generates a series
10427 of values, one holding the value of every subexpression. (The
10428 expression a*b+c has five subexpressions: a, b, a*b, c, and
10429 a*b+c.) GDB's values hold almost enough information to establish
10430 the criteria given above --- they identify memory lvalues,
10431 register lvalues, computed values, etcetera. So we can evaluate
10432 the expression, and then scan the chain of values that leaves
10433 behind to decide whether we can detect any possible change to the
10434 expression's final value using only hardware watchpoints.
10435
10436 However, I don't think that the values returned by inferior
10437 function calls are special in any way. So this function may not
10438 notice that an expression involving an inferior function call
10439 can't be watched with hardware watchpoints. FIXME. */
10440 for (const value_ref_ptr &iter : vals)
10441 {
10442 struct value *v = iter.get ();
10443
10444 if (v->lval () == lval_memory)
10445 {
10446 if (v != head && v->lazy ())
10447 /* A lazy memory lvalue in the chain is one that GDB never
10448 needed to fetch; we either just used its address (e.g.,
10449 `a' in `a.b') or we never needed it at all (e.g., `a'
10450 in `a,b'). This doesn't apply to HEAD; if that is
10451 lazy then it was not readable, but watch it anyway. */
10452 ;
10453 else
10454 {
10455 /* Ahh, memory we actually used! Check if we can cover
10456 it with hardware watchpoints. */
10457 struct type *vtype = check_typedef (v->type ());
10458
10459 /* We only watch structs and arrays if user asked for it
10460 explicitly, never if they just happen to appear in a
10461 middle of some value chain. */
10462 if (v == head
10463 || (vtype->code () != TYPE_CODE_STRUCT
10464 && vtype->code () != TYPE_CODE_ARRAY))
10465 {
10466 CORE_ADDR vaddr = v->address ();
10467 int len;
10468 int num_regs;
10469
10470 len = (target_exact_watchpoints
10471 && is_scalar_type_recursive (vtype))?
10472 1 : v->type ()->length ();
10473
10474 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10475 if (!num_regs)
10476 return 0;
10477 else
10478 found_memory_cnt += num_regs;
10479 }
10480 }
10481 }
10482 else if (v->lval () != not_lval && !v->deprecated_modifiable ())
10483 return 0; /* These are values from the history (e.g., $1). */
10484 else if (v->lval () == lval_register)
10485 return 0; /* Cannot watch a register with a HW watchpoint. */
10486 }
10487
10488 /* The expression itself looks suitable for using a hardware
10489 watchpoint, but give the target machine a chance to reject it. */
10490 return found_memory_cnt;
10491 }
10492
10493 void
10494 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10495 {
10496 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10497 }
10498
10499 /* Options for the watch, awatch, and rwatch commands. */
10500
10501 struct watch_options
10502 {
10503 /* For -location. */
10504 bool location = false;
10505 };
10506
10507 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10508
10509 Historically GDB always accepted both '-location' and '-l' flags for
10510 these commands (both flags being synonyms). When converting to the
10511 newer option scheme only '-location' is added here. That's fine (for
10512 backward compatibility) as any non-ambiguous prefix of a flag will be
10513 accepted, so '-l', '-loc', are now all accepted.
10514
10515 What this means is that, if in the future, we add any new flag here
10516 that starts with '-l' then this will break backward compatibility, so
10517 please, don't do that! */
10518
10519 static const gdb::option::option_def watch_option_defs[] = {
10520 gdb::option::flag_option_def<watch_options> {
10521 "location",
10522 [] (watch_options *opt) { return &opt->location; },
10523 N_("\
10524 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10525 -l can be used as a short form of -location."),
10526 },
10527 };
10528
10529 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10530 commands. */
10531
10532 static gdb::option::option_def_group
10533 make_watch_options_def_group (watch_options *opts)
10534 {
10535 return {{watch_option_defs}, opts};
10536 }
10537
10538 /* A helper function that looks for the "-location" argument and then
10539 calls watch_command_1. */
10540
10541 static void
10542 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10543 {
10544 watch_options opts;
10545 auto grp = make_watch_options_def_group (&opts);
10546 gdb::option::process_options
10547 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10548 if (arg != nullptr && *arg == '\0')
10549 arg = nullptr;
10550
10551 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10552 }
10553
10554 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10555 static void
10556 watch_command_completer (struct cmd_list_element *ignore,
10557 completion_tracker &tracker,
10558 const char *text, const char * /*word*/)
10559 {
10560 const auto group = make_watch_options_def_group (nullptr);
10561 if (gdb::option::complete_options
10562 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10563 return;
10564
10565 const char *word = advance_to_expression_complete_word_point (tracker, text);
10566 expression_completer (ignore, tracker, text, word);
10567 }
10568
10569 static void
10570 watch_command (const char *arg, int from_tty)
10571 {
10572 watch_maybe_just_location (arg, hw_write, from_tty);
10573 }
10574
10575 void
10576 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10577 {
10578 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10579 }
10580
10581 static void
10582 rwatch_command (const char *arg, int from_tty)
10583 {
10584 watch_maybe_just_location (arg, hw_read, from_tty);
10585 }
10586
10587 void
10588 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
10589 {
10590 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10591 }
10592
10593 static void
10594 awatch_command (const char *arg, int from_tty)
10595 {
10596 watch_maybe_just_location (arg, hw_access, from_tty);
10597 }
10598 \f
10599
10600 /* Data for the FSM that manages the until(location)/advance commands
10601 in infcmd.c. Here because it uses the mechanisms of
10602 breakpoints. */
10603
10604 struct until_break_fsm : public thread_fsm
10605 {
10606 /* The thread that was current when the command was executed. */
10607 int thread;
10608
10609 /* The breakpoint set at the return address in the caller frame,
10610 plus breakpoints at all the destination locations. */
10611 std::vector<breakpoint_up> breakpoints;
10612
10613 until_break_fsm (struct interp *cmd_interp, int thread,
10614 std::vector<breakpoint_up> &&breakpoints)
10615 : thread_fsm (cmd_interp),
10616 thread (thread),
10617 breakpoints (std::move (breakpoints))
10618 {
10619 }
10620
10621 void clean_up (struct thread_info *thread) override;
10622 bool should_stop (struct thread_info *thread) override;
10623 enum async_reply_reason do_async_reply_reason () override;
10624 };
10625
10626 /* Implementation of the 'should_stop' FSM method for the
10627 until(location)/advance commands. */
10628
10629 bool
10630 until_break_fsm::should_stop (struct thread_info *tp)
10631 {
10632 for (const breakpoint_up &bp : breakpoints)
10633 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10634 bp.get ()) != NULL)
10635 {
10636 set_finished ();
10637 break;
10638 }
10639
10640 return true;
10641 }
10642
10643 /* Implementation of the 'clean_up' FSM method for the
10644 until(location)/advance commands. */
10645
10646 void
10647 until_break_fsm::clean_up (struct thread_info *)
10648 {
10649 /* Clean up our temporary breakpoints. */
10650 breakpoints.clear ();
10651 delete_longjmp_breakpoint (thread);
10652 }
10653
10654 /* Implementation of the 'async_reply_reason' FSM method for the
10655 until(location)/advance commands. */
10656
10657 enum async_reply_reason
10658 until_break_fsm::do_async_reply_reason ()
10659 {
10660 return EXEC_ASYNC_LOCATION_REACHED;
10661 }
10662
10663 void
10664 until_break_command (const char *arg, int from_tty, int anywhere)
10665 {
10666 frame_info_ptr frame;
10667 struct gdbarch *frame_gdbarch;
10668 struct frame_id stack_frame_id;
10669 struct frame_id caller_frame_id;
10670 int thread;
10671 struct thread_info *tp;
10672
10673 clear_proceed_status (0);
10674
10675 /* Set a breakpoint where the user wants it and at return from
10676 this function. */
10677
10678 location_spec_up locspec = string_to_location_spec (&arg, current_language);
10679
10680 std::vector<symtab_and_line> sals
10681 = (last_displayed_sal_is_valid ()
10682 ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10683 get_last_displayed_symtab (),
10684 get_last_displayed_line ())
10685 : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
10686 NULL, NULL, 0));
10687
10688 if (sals.empty ())
10689 error (_("Couldn't get information on specified line."));
10690
10691 if (*arg)
10692 error (_("Junk at end of arguments."));
10693
10694 tp = inferior_thread ();
10695 thread = tp->global_num;
10696
10697 /* Note linespec handling above invalidates the frame chain.
10698 Installing a breakpoint also invalidates the frame chain (as it
10699 may need to switch threads), so do any frame handling before
10700 that. */
10701
10702 frame = get_selected_frame (NULL);
10703 frame_gdbarch = get_frame_arch (frame);
10704 stack_frame_id = get_stack_frame_id (frame);
10705 caller_frame_id = frame_unwind_caller_id (frame);
10706
10707 /* Keep within the current frame, or in frames called by the current
10708 one. */
10709
10710 std::vector<breakpoint_up> breakpoints;
10711
10712 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10713
10714 if (frame_id_p (caller_frame_id))
10715 {
10716 struct symtab_and_line sal2;
10717 struct gdbarch *caller_gdbarch;
10718
10719 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10720 sal2.pc = frame_unwind_caller_pc (frame);
10721 caller_gdbarch = frame_unwind_caller_arch (frame);
10722
10723 breakpoint_up caller_breakpoint
10724 = set_momentary_breakpoint (caller_gdbarch, sal2,
10725 caller_frame_id, bp_until);
10726 breakpoints.emplace_back (std::move (caller_breakpoint));
10727
10728 set_longjmp_breakpoint (tp, stack_frame_id);
10729 lj_deleter.emplace (thread);
10730 }
10731
10732 /* set_momentary_breakpoint could invalidate FRAME. */
10733 frame = NULL;
10734
10735 /* If the user told us to continue until a specified location, we
10736 don't specify a frame at which we need to stop. Otherwise,
10737 specify the selected frame, because we want to stop only at the
10738 very same frame. */
10739 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10740
10741 for (symtab_and_line &sal : sals)
10742 {
10743 resolve_sal_pc (&sal);
10744
10745 breakpoint_up location_breakpoint
10746 = set_momentary_breakpoint (frame_gdbarch, sal,
10747 stop_frame_id, bp_until);
10748 breakpoints.emplace_back (std::move (location_breakpoint));
10749 }
10750
10751 tp->set_thread_fsm
10752 (std::unique_ptr<thread_fsm>
10753 (new until_break_fsm (command_interp (), tp->global_num,
10754 std::move (breakpoints))));
10755
10756 if (lj_deleter)
10757 lj_deleter->release ();
10758
10759 proceed (-1, GDB_SIGNAL_DEFAULT);
10760 }
10761
10762 \f
10763
10764 /* Compare two breakpoints and return a strcmp-like result. */
10765
10766 static int
10767 compare_breakpoints (const breakpoint *a, const breakpoint *b)
10768 {
10769 uintptr_t ua = (uintptr_t) a;
10770 uintptr_t ub = (uintptr_t) b;
10771
10772 if (a->number < b->number)
10773 return -1;
10774 else if (a->number > b->number)
10775 return 1;
10776
10777 /* Now sort by address, in case we see, e..g, two breakpoints with
10778 the number 0. */
10779 if (ua < ub)
10780 return -1;
10781 return ua > ub ? 1 : 0;
10782 }
10783
10784 /* Delete breakpoints by address or line. */
10785
10786 static void
10787 clear_command (const char *arg, int from_tty)
10788 {
10789 int default_match;
10790
10791 std::vector<symtab_and_line> decoded_sals;
10792 symtab_and_line last_sal;
10793 gdb::array_view<symtab_and_line> sals;
10794 if (arg)
10795 {
10796 decoded_sals
10797 = decode_line_with_current_source (arg,
10798 (DECODE_LINE_FUNFIRSTLINE
10799 | DECODE_LINE_LIST_MODE));
10800 default_match = 0;
10801 sals = decoded_sals;
10802 }
10803 else
10804 {
10805 /* Set sal's line, symtab, pc, and pspace to the values
10806 corresponding to the last call to print_frame_info. If the
10807 codepoint is not valid, this will set all the fields to 0. */
10808 last_sal = get_last_displayed_sal ();
10809 if (last_sal.symtab == 0)
10810 error (_("No source file specified."));
10811
10812 default_match = 1;
10813 sals = last_sal;
10814 }
10815
10816 /* We don't call resolve_sal_pc here. That's not as bad as it
10817 seems, because all existing breakpoints typically have both
10818 file/line and pc set. So, if clear is given file/line, we can
10819 match this to existing breakpoint without obtaining pc at all.
10820
10821 We only support clearing given the address explicitly
10822 present in breakpoint table. Say, we've set breakpoint
10823 at file:line. There were several PC values for that file:line,
10824 due to optimization, all in one block.
10825
10826 We've picked one PC value. If "clear" is issued with another
10827 PC corresponding to the same file:line, the breakpoint won't
10828 be cleared. We probably can still clear the breakpoint, but
10829 since the other PC value is never presented to user, user
10830 can only find it by guessing, and it does not seem important
10831 to support that. */
10832
10833 /* For each line spec given, delete bps which correspond to it. Do
10834 it in two passes, solely to preserve the current behavior that
10835 from_tty is forced true if we delete more than one
10836 breakpoint. */
10837
10838 std::vector<struct breakpoint *> found;
10839 for (const auto &sal : sals)
10840 {
10841 const char *sal_fullname;
10842
10843 /* If exact pc given, clear bpts at that pc.
10844 If line given (pc == 0), clear all bpts on specified line.
10845 If defaulting, clear all bpts on default line
10846 or at default pc.
10847
10848 defaulting sal.pc != 0 tests to do
10849
10850 0 1 pc
10851 1 1 pc _and_ line
10852 0 0 line
10853 1 0 <can't happen> */
10854
10855 sal_fullname = (sal.symtab == NULL
10856 ? NULL : symtab_to_fullname (sal.symtab));
10857
10858 /* Find all matching breakpoints and add them to 'found'. */
10859 for (breakpoint *b : all_breakpoints ())
10860 {
10861 int match = 0;
10862 /* Are we going to delete b? */
10863 if (b->type != bp_none && !is_watchpoint (b)
10864 && user_breakpoint_p (b))
10865 {
10866 for (bp_location *loc : b->locations ())
10867 {
10868 /* If the user specified file:line, don't allow a PC
10869 match. This matches historical gdb behavior. */
10870 int pc_match = (!sal.explicit_line
10871 && sal.pc
10872 && (loc->pspace == sal.pspace)
10873 && (loc->address == sal.pc)
10874 && (!section_is_overlay (loc->section)
10875 || loc->section == sal.section));
10876 int line_match = 0;
10877
10878 if ((default_match || sal.explicit_line)
10879 && loc->symtab != NULL
10880 && sal_fullname != NULL
10881 && sal.pspace == loc->pspace
10882 && loc->line_number == sal.line
10883 && filename_cmp (symtab_to_fullname (loc->symtab),
10884 sal_fullname) == 0)
10885 line_match = 1;
10886
10887 if (pc_match || line_match)
10888 {
10889 match = 1;
10890 break;
10891 }
10892 }
10893 }
10894
10895 if (match)
10896 found.push_back (b);
10897 }
10898 }
10899
10900 /* Now go thru the 'found' chain and delete them. */
10901 if (found.empty ())
10902 {
10903 if (arg)
10904 error (_("No breakpoint at %s."), arg);
10905 else
10906 error (_("No breakpoint at this line."));
10907 }
10908
10909 /* Remove duplicates from the vec. */
10910 std::sort (found.begin (), found.end (),
10911 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10912 {
10913 return compare_breakpoints (bp_a, bp_b) < 0;
10914 });
10915 found.erase (std::unique (found.begin (), found.end (),
10916 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10917 {
10918 return compare_breakpoints (bp_a, bp_b) == 0;
10919 }),
10920 found.end ());
10921
10922 if (found.size () > 1)
10923 from_tty = 1; /* Always report if deleted more than one. */
10924 if (from_tty)
10925 {
10926 if (found.size () == 1)
10927 gdb_printf (_("Deleted breakpoint "));
10928 else
10929 gdb_printf (_("Deleted breakpoints "));
10930 }
10931
10932 for (breakpoint *iter : found)
10933 {
10934 if (from_tty)
10935 gdb_printf ("%d ", iter->number);
10936 delete_breakpoint (iter);
10937 }
10938 if (from_tty)
10939 gdb_putc ('\n');
10940 }
10941 \f
10942 /* Delete breakpoint in BS if they are `delete' breakpoints and
10943 all breakpoints that are marked for deletion, whether hit or not.
10944 This is called after any breakpoint is hit, or after errors. */
10945
10946 void
10947 breakpoint_auto_delete (bpstat *bs)
10948 {
10949 for (; bs; bs = bs->next)
10950 if (bs->breakpoint_at
10951 && bs->breakpoint_at->disposition == disp_del
10952 && bs->stop)
10953 delete_breakpoint (bs->breakpoint_at);
10954
10955 for (breakpoint *b : all_breakpoints_safe ())
10956 if (b->disposition == disp_del_at_next_stop)
10957 delete_breakpoint (b);
10958 }
10959
10960 /* A comparison function for bp_location AP and BP being interfaced to
10961 std::sort. Sort elements primarily by their ADDRESS (no matter what
10962 bl_address_is_meaningful says), secondarily by ordering first
10963 permanent elements and terciarily just ensuring the array is sorted
10964 stable way despite std::sort being an unstable algorithm. */
10965
10966 static int
10967 bp_location_is_less_than (const bp_location *a, const bp_location *b)
10968 {
10969 if (a->address != b->address)
10970 return a->address < b->address;
10971
10972 /* Sort locations at the same address by their pspace number, keeping
10973 locations of the same inferior (in a multi-inferior environment)
10974 grouped. */
10975
10976 if (a->pspace->num != b->pspace->num)
10977 return a->pspace->num < b->pspace->num;
10978
10979 /* Sort permanent breakpoints first. */
10980 if (a->permanent != b->permanent)
10981 return a->permanent > b->permanent;
10982
10983 /* Sort by type in order to make duplicate determination easier.
10984 See update_global_location_list. This is kept in sync with
10985 breakpoint_locations_match. */
10986 if (a->loc_type < b->loc_type)
10987 return true;
10988
10989 /* Likewise, for range-breakpoints, sort by length. */
10990 if (a->loc_type == bp_loc_hardware_breakpoint
10991 && b->loc_type == bp_loc_hardware_breakpoint
10992 && a->length < b->length)
10993 return true;
10994
10995 /* Make the internal GDB representation stable across GDB runs
10996 where A and B memory inside GDB can differ. Breakpoint locations of
10997 the same type at the same address can be sorted in arbitrary order. */
10998
10999 if (a->owner->number != b->owner->number)
11000 return a->owner->number < b->owner->number;
11001
11002 return a < b;
11003 }
11004
11005 /* Set bp_locations_placed_address_before_address_max and
11006 bp_locations_shadow_len_after_address_max according to the current
11007 content of the bp_locations array. */
11008
11009 static void
11010 bp_locations_target_extensions_update (void)
11011 {
11012 bp_locations_placed_address_before_address_max = 0;
11013 bp_locations_shadow_len_after_address_max = 0;
11014
11015 for (bp_location *bl : all_bp_locations ())
11016 {
11017 CORE_ADDR start, end, addr;
11018
11019 if (!bp_location_has_shadow (bl))
11020 continue;
11021
11022 start = bl->target_info.placed_address;
11023 end = start + bl->target_info.shadow_len;
11024
11025 gdb_assert (bl->address >= start);
11026 addr = bl->address - start;
11027 if (addr > bp_locations_placed_address_before_address_max)
11028 bp_locations_placed_address_before_address_max = addr;
11029
11030 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11031
11032 gdb_assert (bl->address < end);
11033 addr = end - bl->address;
11034 if (addr > bp_locations_shadow_len_after_address_max)
11035 bp_locations_shadow_len_after_address_max = addr;
11036 }
11037 }
11038
11039 /* Download tracepoint locations if they haven't been. */
11040
11041 static void
11042 download_tracepoint_locations (void)
11043 {
11044 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11045
11046 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11047
11048 for (breakpoint *b : all_tracepoints ())
11049 {
11050 struct tracepoint *t;
11051 bool bp_location_downloaded = false;
11052
11053 if ((b->type == bp_fast_tracepoint
11054 ? !may_insert_fast_tracepoints
11055 : !may_insert_tracepoints))
11056 continue;
11057
11058 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11059 {
11060 if (target_can_download_tracepoint ())
11061 can_download_tracepoint = TRIBOOL_TRUE;
11062 else
11063 can_download_tracepoint = TRIBOOL_FALSE;
11064 }
11065
11066 if (can_download_tracepoint == TRIBOOL_FALSE)
11067 break;
11068
11069 for (bp_location *bl : b->locations ())
11070 {
11071 /* In tracepoint, locations are _never_ duplicated, so
11072 should_be_inserted is equivalent to
11073 unduplicated_should_be_inserted. */
11074 if (!should_be_inserted (bl) || bl->inserted)
11075 continue;
11076
11077 switch_to_program_space_and_thread (bl->pspace);
11078
11079 target_download_tracepoint (bl);
11080
11081 bl->inserted = 1;
11082 bp_location_downloaded = true;
11083 }
11084 t = (struct tracepoint *) b;
11085 t->number_on_target = b->number;
11086 if (bp_location_downloaded)
11087 gdb::observers::breakpoint_modified.notify (b);
11088 }
11089 }
11090
11091 /* Swap the insertion/duplication state between two locations. */
11092
11093 static void
11094 swap_insertion (struct bp_location *left, struct bp_location *right)
11095 {
11096 const int left_inserted = left->inserted;
11097 const int left_duplicate = left->duplicate;
11098 const int left_needs_update = left->needs_update;
11099 const struct bp_target_info left_target_info = left->target_info;
11100
11101 /* Locations of tracepoints can never be duplicated. */
11102 if (is_tracepoint (left->owner))
11103 gdb_assert (!left->duplicate);
11104 if (is_tracepoint (right->owner))
11105 gdb_assert (!right->duplicate);
11106
11107 left->inserted = right->inserted;
11108 left->duplicate = right->duplicate;
11109 left->needs_update = right->needs_update;
11110 left->target_info = right->target_info;
11111 right->inserted = left_inserted;
11112 right->duplicate = left_duplicate;
11113 right->needs_update = left_needs_update;
11114 right->target_info = left_target_info;
11115 }
11116
11117 /* Force the re-insertion of the locations at ADDRESS. This is called
11118 once a new/deleted/modified duplicate location is found and we are evaluating
11119 conditions on the target's side. Such conditions need to be updated on
11120 the target. */
11121
11122 static void
11123 force_breakpoint_reinsertion (struct bp_location *bl)
11124 {
11125 CORE_ADDR address = 0;
11126 int pspace_num;
11127
11128 address = bl->address;
11129 pspace_num = bl->pspace->num;
11130
11131 /* This is only meaningful if the target is
11132 evaluating conditions and if the user has
11133 opted for condition evaluation on the target's
11134 side. */
11135 if (gdb_evaluates_breakpoint_condition_p ()
11136 || !target_supports_evaluation_of_breakpoint_conditions ())
11137 return;
11138
11139 /* Flag all breakpoint locations with this address and
11140 the same program space as the location
11141 as "its condition has changed". We need to
11142 update the conditions on the target's side. */
11143 for (bp_location *loc : all_bp_locations_at_addr (address))
11144 {
11145 if (!is_breakpoint (loc->owner)
11146 || pspace_num != loc->pspace->num)
11147 continue;
11148
11149 /* Flag the location appropriately. We use a different state to
11150 let everyone know that we already updated the set of locations
11151 with addr bl->address and program space bl->pspace. This is so
11152 we don't have to keep calling these functions just to mark locations
11153 that have already been marked. */
11154 loc->condition_changed = condition_updated;
11155
11156 /* Free the agent expression bytecode as well. We will compute
11157 it later on. */
11158 loc->cond_bytecode.reset ();
11159 }
11160 }
11161
11162 /* Called whether new breakpoints are created, or existing breakpoints
11163 deleted, to update the global location list and recompute which
11164 locations are duplicate of which.
11165
11166 The INSERT_MODE flag determines whether locations may not, may, or
11167 shall be inserted now. See 'enum ugll_insert_mode' for more
11168 info. */
11169
11170 static void
11171 update_global_location_list (enum ugll_insert_mode insert_mode)
11172 {
11173 /* Last breakpoint location address that was marked for update. */
11174 CORE_ADDR last_addr = 0;
11175 /* Last breakpoint location program space that was marked for update. */
11176 int last_pspace_num = -1;
11177
11178 /* Used in the duplicates detection below. When iterating over all
11179 bp_locations, points to the first bp_location of a given address.
11180 Breakpoints and watchpoints of different types are never
11181 duplicates of each other. Keep one pointer for each type of
11182 breakpoint/watchpoint, so we only need to loop over all locations
11183 once. */
11184 struct bp_location *bp_loc_first; /* breakpoint */
11185 struct bp_location *wp_loc_first; /* hardware watchpoint */
11186 struct bp_location *awp_loc_first; /* access watchpoint */
11187 struct bp_location *rwp_loc_first; /* read watchpoint */
11188
11189 /* Saved former bp_locations array which we compare against the newly
11190 built bp_locations from the current state of ALL_BREAKPOINTS. */
11191 std::vector<bp_location *> old_locations = std::move (bp_locations);
11192 bp_locations.clear ();
11193
11194 for (breakpoint *b : all_breakpoints ())
11195 for (bp_location *loc : b->locations ())
11196 bp_locations.push_back (loc);
11197
11198 /* See if we need to "upgrade" a software breakpoint to a hardware
11199 breakpoint. Do this before deciding whether locations are
11200 duplicates. Also do this before sorting because sorting order
11201 depends on location type. */
11202 for (bp_location *loc : bp_locations)
11203 if (!loc->inserted && should_be_inserted (loc))
11204 handle_automatic_hardware_breakpoints (loc);
11205
11206 std::sort (bp_locations.begin (), bp_locations.end (),
11207 bp_location_is_less_than);
11208
11209 bp_locations_target_extensions_update ();
11210
11211 /* Identify bp_location instances that are no longer present in the
11212 new list, and therefore should be freed. Note that it's not
11213 necessary that those locations should be removed from inferior --
11214 if there's another location at the same address (previously
11215 marked as duplicate), we don't need to remove/insert the
11216 location.
11217
11218 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11219 and former bp_location array state respectively. */
11220
11221 size_t loc_i = 0;
11222 for (bp_location *old_loc : old_locations)
11223 {
11224 /* Tells if 'old_loc' is found among the new locations. If
11225 not, we have to free it. */
11226 bool found_object = false;
11227 /* Tells if the location should remain inserted in the target. */
11228 bool keep_in_target = false;
11229 bool removed = false;
11230
11231 /* Skip LOCP entries which will definitely never be needed.
11232 Stop either at or being the one matching OLD_LOC. */
11233 while (loc_i < bp_locations.size ()
11234 && bp_locations[loc_i]->address < old_loc->address)
11235 loc_i++;
11236
11237 for (size_t loc2_i = loc_i;
11238 (loc2_i < bp_locations.size ()
11239 && bp_locations[loc2_i]->address == old_loc->address);
11240 loc2_i++)
11241 {
11242 /* Check if this is a new/duplicated location or a duplicated
11243 location that had its condition modified. If so, we want to send
11244 its condition to the target if evaluation of conditions is taking
11245 place there. */
11246 if (bp_locations[loc2_i]->condition_changed == condition_modified
11247 && (last_addr != old_loc->address
11248 || last_pspace_num != old_loc->pspace->num))
11249 {
11250 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11251 last_pspace_num = old_loc->pspace->num;
11252 }
11253
11254 if (bp_locations[loc2_i] == old_loc)
11255 found_object = true;
11256 }
11257
11258 /* We have already handled this address, update it so that we don't
11259 have to go through updates again. */
11260 last_addr = old_loc->address;
11261
11262 /* Target-side condition evaluation: Handle deleted locations. */
11263 if (!found_object)
11264 force_breakpoint_reinsertion (old_loc);
11265
11266 /* If this location is no longer present, and inserted, look if
11267 there's maybe a new location at the same address. If so,
11268 mark that one inserted, and don't remove this one. This is
11269 needed so that we don't have a time window where a breakpoint
11270 at certain location is not inserted. */
11271
11272 if (old_loc->inserted)
11273 {
11274 /* If the location is inserted now, we might have to remove
11275 it. */
11276
11277 if (found_object && should_be_inserted (old_loc))
11278 {
11279 /* The location is still present in the location list,
11280 and still should be inserted. Don't do anything. */
11281 keep_in_target = true;
11282 }
11283 else
11284 {
11285 /* This location still exists, but it won't be kept in the
11286 target since it may have been disabled. We proceed to
11287 remove its target-side condition. */
11288
11289 /* The location is either no longer present, or got
11290 disabled. See if there's another location at the
11291 same address, in which case we don't need to remove
11292 this one from the target. */
11293
11294 /* OLD_LOC comes from existing struct breakpoint. */
11295 if (bl_address_is_meaningful (old_loc))
11296 {
11297 for (size_t loc2_i = loc_i;
11298 (loc2_i < bp_locations.size ()
11299 && bp_locations[loc2_i]->address == old_loc->address);
11300 loc2_i++)
11301 {
11302 bp_location *loc2 = bp_locations[loc2_i];
11303
11304 if (loc2 == old_loc)
11305 continue;
11306
11307 if (breakpoint_locations_match (loc2, old_loc))
11308 {
11309 /* Read watchpoint locations are switched to
11310 access watchpoints, if the former are not
11311 supported, but the latter are. */
11312 if (is_hardware_watchpoint (old_loc->owner))
11313 {
11314 gdb_assert (is_hardware_watchpoint (loc2->owner));
11315 loc2->watchpoint_type = old_loc->watchpoint_type;
11316 }
11317
11318 /* loc2 is a duplicated location. We need to check
11319 if it should be inserted in case it will be
11320 unduplicated. */
11321 if (unduplicated_should_be_inserted (loc2))
11322 {
11323 swap_insertion (old_loc, loc2);
11324 keep_in_target = true;
11325 break;
11326 }
11327 }
11328 }
11329 }
11330 }
11331
11332 if (!keep_in_target)
11333 {
11334 if (remove_breakpoint (old_loc))
11335 {
11336 /* This is just about all we can do. We could keep
11337 this location on the global list, and try to
11338 remove it next time, but there's no particular
11339 reason why we will succeed next time.
11340
11341 Note that at this point, old_loc->owner is still
11342 valid, as delete_breakpoint frees the breakpoint
11343 only after calling us. */
11344 gdb_printf (_("warning: Error removing "
11345 "breakpoint %d\n"),
11346 old_loc->owner->number);
11347 }
11348 removed = true;
11349 }
11350 }
11351
11352 if (!found_object)
11353 {
11354 if (removed && target_is_non_stop_p ()
11355 && need_moribund_for_location_type (old_loc))
11356 {
11357 /* This location was removed from the target. In
11358 non-stop mode, a race condition is possible where
11359 we've removed a breakpoint, but stop events for that
11360 breakpoint are already queued and will arrive later.
11361 We apply an heuristic to be able to distinguish such
11362 SIGTRAPs from other random SIGTRAPs: we keep this
11363 breakpoint location for a bit, and will retire it
11364 after we see some number of events. The theory here
11365 is that reporting of events should, "on the average",
11366 be fair, so after a while we'll see events from all
11367 threads that have anything of interest, and no longer
11368 need to keep this breakpoint location around. We
11369 don't hold locations forever so to reduce chances of
11370 mistaking a non-breakpoint SIGTRAP for a breakpoint
11371 SIGTRAP.
11372
11373 The heuristic failing can be disastrous on
11374 decr_pc_after_break targets.
11375
11376 On decr_pc_after_break targets, like e.g., x86-linux,
11377 if we fail to recognize a late breakpoint SIGTRAP,
11378 because events_till_retirement has reached 0 too
11379 soon, we'll fail to do the PC adjustment, and report
11380 a random SIGTRAP to the user. When the user resumes
11381 the inferior, it will most likely immediately crash
11382 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11383 corrupted, because of being resumed e.g., in the
11384 middle of a multi-byte instruction, or skipped a
11385 one-byte instruction. This was actually seen happen
11386 on native x86-linux, and should be less rare on
11387 targets that do not support new thread events, like
11388 remote, due to the heuristic depending on
11389 thread_count.
11390
11391 Mistaking a random SIGTRAP for a breakpoint trap
11392 causes similar symptoms (PC adjustment applied when
11393 it shouldn't), but then again, playing with SIGTRAPs
11394 behind the debugger's back is asking for trouble.
11395
11396 Since hardware watchpoint traps are always
11397 distinguishable from other traps, so we don't need to
11398 apply keep hardware watchpoint moribund locations
11399 around. We simply always ignore hardware watchpoint
11400 traps we can no longer explain. */
11401
11402 process_stratum_target *proc_target = nullptr;
11403 for (inferior *inf : all_inferiors ())
11404 if (inf->pspace == old_loc->pspace)
11405 {
11406 proc_target = inf->process_target ();
11407 break;
11408 }
11409 if (proc_target != nullptr)
11410 old_loc->events_till_retirement
11411 = 3 * (thread_count (proc_target) + 1);
11412 else
11413 old_loc->events_till_retirement = 1;
11414 old_loc->owner = NULL;
11415
11416 moribund_locations.push_back (old_loc);
11417 }
11418 else
11419 {
11420 old_loc->owner = NULL;
11421 decref_bp_location (&old_loc);
11422 }
11423 }
11424 }
11425
11426 /* Rescan breakpoints at the same address and section, marking the
11427 first one as "first" and any others as "duplicates". This is so
11428 that the bpt instruction is only inserted once. If we have a
11429 permanent breakpoint at the same place as BPT, make that one the
11430 official one, and the rest as duplicates. Permanent breakpoints
11431 are sorted first for the same address.
11432
11433 Do the same for hardware watchpoints, but also considering the
11434 watchpoint's type (regular/access/read) and length. */
11435
11436 bp_loc_first = NULL;
11437 wp_loc_first = NULL;
11438 awp_loc_first = NULL;
11439 rwp_loc_first = NULL;
11440
11441 for (bp_location *loc : all_bp_locations ())
11442 {
11443 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11444 non-NULL. */
11445 struct bp_location **loc_first_p;
11446 breakpoint *b = loc->owner;
11447
11448 if (!unduplicated_should_be_inserted (loc)
11449 || !bl_address_is_meaningful (loc)
11450 /* Don't detect duplicate for tracepoint locations because they are
11451 never duplicated. See the comments in field `duplicate' of
11452 `struct bp_location'. */
11453 || is_tracepoint (b))
11454 {
11455 /* Clear the condition modification flag. */
11456 loc->condition_changed = condition_unchanged;
11457 continue;
11458 }
11459
11460 if (b->type == bp_hardware_watchpoint)
11461 loc_first_p = &wp_loc_first;
11462 else if (b->type == bp_read_watchpoint)
11463 loc_first_p = &rwp_loc_first;
11464 else if (b->type == bp_access_watchpoint)
11465 loc_first_p = &awp_loc_first;
11466 else
11467 loc_first_p = &bp_loc_first;
11468
11469 if (*loc_first_p == NULL
11470 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11471 || !breakpoint_locations_match (loc, *loc_first_p))
11472 {
11473 *loc_first_p = loc;
11474 loc->duplicate = 0;
11475
11476 if (is_breakpoint (loc->owner) && loc->condition_changed)
11477 {
11478 loc->needs_update = 1;
11479 /* Clear the condition modification flag. */
11480 loc->condition_changed = condition_unchanged;
11481 }
11482 continue;
11483 }
11484
11485
11486 /* This and the above ensure the invariant that the first location
11487 is not duplicated, and is the inserted one.
11488 All following are marked as duplicated, and are not inserted. */
11489 if (loc->inserted)
11490 swap_insertion (loc, *loc_first_p);
11491 loc->duplicate = 1;
11492
11493 /* Clear the condition modification flag. */
11494 loc->condition_changed = condition_unchanged;
11495 }
11496
11497 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11498 {
11499 if (insert_mode != UGLL_DONT_INSERT)
11500 insert_breakpoint_locations ();
11501 else
11502 {
11503 /* Even though the caller told us to not insert new
11504 locations, we may still need to update conditions on the
11505 target's side of breakpoints that were already inserted
11506 if the target is evaluating breakpoint conditions. We
11507 only update conditions for locations that are marked
11508 "needs_update". */
11509 update_inserted_breakpoint_locations ();
11510 }
11511 }
11512
11513 if (insert_mode != UGLL_DONT_INSERT)
11514 download_tracepoint_locations ();
11515 }
11516
11517 void
11518 breakpoint_retire_moribund (void)
11519 {
11520 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11521 {
11522 struct bp_location *loc = moribund_locations[ix];
11523 if (--(loc->events_till_retirement) == 0)
11524 {
11525 decref_bp_location (&loc);
11526 unordered_remove (moribund_locations, ix);
11527 --ix;
11528 }
11529 }
11530 }
11531
11532 static void
11533 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11534 {
11535
11536 try
11537 {
11538 update_global_location_list (insert_mode);
11539 }
11540 catch (const gdb_exception_error &e)
11541 {
11542 }
11543 }
11544
11545 /* Clear BKP from a BPS. */
11546
11547 static void
11548 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
11549 {
11550 bpstat *bs;
11551
11552 for (bs = bps; bs; bs = bs->next)
11553 if (bs->breakpoint_at == bpt)
11554 {
11555 bs->breakpoint_at = NULL;
11556 bs->old_val = NULL;
11557 /* bs->commands will be freed later. */
11558 }
11559 }
11560
11561 /* Callback for iterate_over_threads. */
11562 static int
11563 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11564 {
11565 struct breakpoint *bpt = (struct breakpoint *) data;
11566
11567 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11568 return 0;
11569 }
11570
11571 /* See breakpoint.h. */
11572
11573 void
11574 code_breakpoint::say_where () const
11575 {
11576 struct value_print_options opts;
11577
11578 get_user_print_options (&opts);
11579
11580 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11581 single string. */
11582 if (loc == NULL)
11583 {
11584 /* For pending locations, the output differs slightly based
11585 on extra_string. If this is non-NULL, it contains either
11586 a condition or dprintf arguments. */
11587 if (extra_string == NULL)
11588 {
11589 gdb_printf (_(" (%s) pending."), locspec->to_string ());
11590 }
11591 else if (type == bp_dprintf)
11592 {
11593 gdb_printf (_(" (%s,%s) pending."),
11594 locspec->to_string (),
11595 extra_string.get ());
11596 }
11597 else
11598 {
11599 gdb_printf (_(" (%s %s) pending."),
11600 locspec->to_string (),
11601 extra_string.get ());
11602 }
11603 }
11604 else
11605 {
11606 if (opts.addressprint || loc->symtab == NULL)
11607 gdb_printf (" at %ps",
11608 styled_string (address_style.style (),
11609 paddress (loc->gdbarch,
11610 loc->address)));
11611 if (loc->symtab != NULL)
11612 {
11613 /* If there is a single location, we can print the location
11614 more nicely. */
11615 if (loc->next == NULL)
11616 {
11617 const char *filename
11618 = symtab_to_filename_for_display (loc->symtab);
11619 gdb_printf (": file %ps, line %d.",
11620 styled_string (file_name_style.style (),
11621 filename),
11622 loc->line_number);
11623 }
11624 else
11625 /* This is not ideal, but each location may have a
11626 different file name, and this at least reflects the
11627 real situation somewhat. */
11628 gdb_printf (": %s.", locspec->to_string ());
11629 }
11630
11631 if (loc->next)
11632 {
11633 struct bp_location *iter = loc;
11634 int n = 0;
11635 for (; iter; iter = iter->next)
11636 ++n;
11637 gdb_printf (" (%d locations)", n);
11638 }
11639 }
11640 }
11641
11642 /* See breakpoint.h. */
11643
11644 bp_location_range breakpoint::locations () const
11645 {
11646 return bp_location_range (this->loc);
11647 }
11648
11649 struct bp_location *
11650 breakpoint::allocate_location ()
11651 {
11652 return new bp_location (this);
11653 }
11654
11655 #define internal_error_pure_virtual_called() \
11656 gdb_assert_not_reached ("pure virtual function called")
11657
11658 int
11659 breakpoint::insert_location (struct bp_location *bl)
11660 {
11661 internal_error_pure_virtual_called ();
11662 }
11663
11664 int
11665 breakpoint::remove_location (struct bp_location *bl,
11666 enum remove_bp_reason reason)
11667 {
11668 internal_error_pure_virtual_called ();
11669 }
11670
11671 int
11672 breakpoint::breakpoint_hit (const struct bp_location *bl,
11673 const address_space *aspace,
11674 CORE_ADDR bp_addr,
11675 const target_waitstatus &ws)
11676 {
11677 internal_error_pure_virtual_called ();
11678 }
11679
11680 int
11681 breakpoint::resources_needed (const struct bp_location *bl)
11682 {
11683 internal_error_pure_virtual_called ();
11684 }
11685
11686 enum print_stop_action
11687 breakpoint::print_it (const bpstat *bs) const
11688 {
11689 internal_error_pure_virtual_called ();
11690 }
11691
11692 void
11693 breakpoint::print_mention () const
11694 {
11695 internal_error_pure_virtual_called ();
11696 }
11697
11698 void
11699 breakpoint::print_recreate (struct ui_file *fp) const
11700 {
11701 internal_error_pure_virtual_called ();
11702 }
11703
11704 /* Default breakpoint_ops methods. */
11705
11706 void
11707 code_breakpoint::re_set ()
11708 {
11709 /* FIXME: is this still reachable? */
11710 if (breakpoint_location_spec_empty_p (this))
11711 {
11712 /* Anything without a location can't be re-set. */
11713 delete_breakpoint (this);
11714 return;
11715 }
11716
11717 re_set_default ();
11718 }
11719
11720 int
11721 code_breakpoint::insert_location (struct bp_location *bl)
11722 {
11723 CORE_ADDR addr = bl->target_info.reqstd_address;
11724
11725 bl->target_info.kind = breakpoint_kind (bl, &addr);
11726 bl->target_info.placed_address = addr;
11727
11728 int result;
11729 if (bl->loc_type == bp_loc_hardware_breakpoint)
11730 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
11731 else
11732 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11733
11734 if (result == 0 && bl->probe.prob != nullptr)
11735 {
11736 /* The insertion was successful, now let's set the probe's semaphore
11737 if needed. */
11738 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11739 }
11740
11741 return result;
11742 }
11743
11744 int
11745 code_breakpoint::remove_location (struct bp_location *bl,
11746 enum remove_bp_reason reason)
11747 {
11748 if (bl->probe.prob != nullptr)
11749 {
11750 /* Let's clear the semaphore before removing the location. */
11751 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11752 }
11753
11754 if (bl->loc_type == bp_loc_hardware_breakpoint)
11755 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11756 else
11757 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
11758 }
11759
11760 int
11761 code_breakpoint::breakpoint_hit (const struct bp_location *bl,
11762 const address_space *aspace,
11763 CORE_ADDR bp_addr,
11764 const target_waitstatus &ws)
11765 {
11766 if (ws.kind () != TARGET_WAITKIND_STOPPED
11767 || ws.sig () != GDB_SIGNAL_TRAP)
11768 return 0;
11769
11770 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11771 aspace, bp_addr))
11772 return 0;
11773
11774 if (overlay_debugging /* unmapped overlay section */
11775 && section_is_overlay (bl->section)
11776 && !section_is_mapped (bl->section))
11777 return 0;
11778
11779 return 1;
11780 }
11781
11782 int
11783 dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
11784 const address_space *aspace,
11785 CORE_ADDR bp_addr,
11786 const target_waitstatus &ws)
11787 {
11788 if (dprintf_style == dprintf_style_agent
11789 && target_can_run_breakpoint_commands ())
11790 {
11791 /* An agent-style dprintf never causes a stop. If we see a trap
11792 for this address it must be for a breakpoint that happens to
11793 be set at the same address. */
11794 return 0;
11795 }
11796
11797 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
11798 }
11799
11800 int
11801 ordinary_breakpoint::resources_needed (const struct bp_location *bl)
11802 {
11803 gdb_assert (type == bp_hardware_breakpoint);
11804
11805 return 1;
11806 }
11807
11808 enum print_stop_action
11809 ordinary_breakpoint::print_it (const bpstat *bs) const
11810 {
11811 const struct bp_location *bl;
11812 int bp_temp;
11813 struct ui_out *uiout = current_uiout;
11814
11815 bl = bs->bp_location_at.get ();
11816
11817 bp_temp = disposition == disp_del;
11818 if (bl->address != bl->requested_address)
11819 breakpoint_adjustment_warning (bl->requested_address,
11820 bl->address,
11821 number, true);
11822 annotate_breakpoint (number);
11823 maybe_print_thread_hit_breakpoint (uiout);
11824
11825 if (uiout->is_mi_like_p ())
11826 {
11827 uiout->field_string ("reason",
11828 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11829 uiout->field_string ("disp", bpdisp_text (disposition));
11830 }
11831
11832 if (bp_temp)
11833 uiout->text ("Temporary breakpoint ");
11834 else
11835 uiout->text ("Breakpoint ");
11836 print_num_locno (bs, uiout);
11837 uiout->text (", ");
11838
11839 return PRINT_SRC_AND_LOC;
11840 }
11841
11842 void
11843 ordinary_breakpoint::print_mention () const
11844 {
11845 if (current_uiout->is_mi_like_p ())
11846 return;
11847
11848 switch (type)
11849 {
11850 case bp_breakpoint:
11851 case bp_gnu_ifunc_resolver:
11852 if (disposition == disp_del)
11853 gdb_printf (_("Temporary breakpoint"));
11854 else
11855 gdb_printf (_("Breakpoint"));
11856 gdb_printf (_(" %d"), number);
11857 if (type == bp_gnu_ifunc_resolver)
11858 gdb_printf (_(" at gnu-indirect-function resolver"));
11859 break;
11860 case bp_hardware_breakpoint:
11861 gdb_printf (_("Hardware assisted breakpoint %d"), number);
11862 break;
11863 case bp_dprintf:
11864 gdb_printf (_("Dprintf %d"), number);
11865 break;
11866 }
11867
11868 say_where ();
11869 }
11870
11871 void
11872 ordinary_breakpoint::print_recreate (struct ui_file *fp) const
11873 {
11874 if (type == bp_breakpoint && disposition == disp_del)
11875 gdb_printf (fp, "tbreak");
11876 else if (type == bp_breakpoint)
11877 gdb_printf (fp, "break");
11878 else if (type == bp_hardware_breakpoint
11879 && disposition == disp_del)
11880 gdb_printf (fp, "thbreak");
11881 else if (type == bp_hardware_breakpoint)
11882 gdb_printf (fp, "hbreak");
11883 else
11884 internal_error (_("unhandled breakpoint type %d"), (int) type);
11885
11886 gdb_printf (fp, " %s", locspec->to_string ());
11887
11888 /* Print out extra_string if this breakpoint is pending. It might
11889 contain, for example, conditions that were set by the user. */
11890 if (loc == NULL && extra_string != NULL)
11891 gdb_printf (fp, " %s", extra_string.get ());
11892
11893 print_recreate_thread (fp);
11894 }
11895
11896 std::vector<symtab_and_line>
11897 code_breakpoint::decode_location_spec (location_spec *locspec,
11898 program_space *search_pspace)
11899 {
11900 if (locspec->type () == PROBE_LOCATION_SPEC)
11901 return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
11902
11903 struct linespec_result canonical;
11904
11905 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
11906 NULL, 0, &canonical, multiple_symbols_all,
11907 filter.get ());
11908
11909 /* We should get 0 or 1 resulting SALs. */
11910 gdb_assert (canonical.lsals.size () < 2);
11911
11912 if (!canonical.lsals.empty ())
11913 {
11914 const linespec_sals &lsal = canonical.lsals[0];
11915 return std::move (lsal.sals);
11916 }
11917 return {};
11918 }
11919
11920 /* Virtual table for internal breakpoints. */
11921
11922 void
11923 internal_breakpoint::re_set ()
11924 {
11925 switch (type)
11926 {
11927 /* Delete overlay event and longjmp master breakpoints; they
11928 will be reset later by breakpoint_re_set. */
11929 case bp_overlay_event:
11930 case bp_longjmp_master:
11931 case bp_std_terminate_master:
11932 case bp_exception_master:
11933 delete_breakpoint (this);
11934 break;
11935
11936 /* This breakpoint is special, it's set up when the inferior
11937 starts and we really don't want to touch it. */
11938 case bp_shlib_event:
11939
11940 /* Like bp_shlib_event, this breakpoint type is special. Once
11941 it is set up, we do not want to touch it. */
11942 case bp_thread_event:
11943 break;
11944 }
11945 }
11946
11947 void
11948 internal_breakpoint::check_status (bpstat *bs)
11949 {
11950 if (type == bp_shlib_event)
11951 {
11952 /* If requested, stop when the dynamic linker notifies GDB of
11953 events. This allows the user to get control and place
11954 breakpoints in initializer routines for dynamically loaded
11955 objects (among other things). */
11956 bs->stop = stop_on_solib_events != 0;
11957 bs->print = stop_on_solib_events != 0;
11958 }
11959 else
11960 bs->stop = false;
11961 }
11962
11963 enum print_stop_action
11964 internal_breakpoint::print_it (const bpstat *bs) const
11965 {
11966 switch (type)
11967 {
11968 case bp_shlib_event:
11969 /* Did we stop because the user set the stop_on_solib_events
11970 variable? (If so, we report this as a generic, "Stopped due
11971 to shlib event" message.) */
11972 print_solib_event (false);
11973 break;
11974
11975 case bp_thread_event:
11976 /* Not sure how we will get here.
11977 GDB should not stop for these breakpoints. */
11978 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
11979 break;
11980
11981 case bp_overlay_event:
11982 /* By analogy with the thread event, GDB should not stop for these. */
11983 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
11984 break;
11985
11986 case bp_longjmp_master:
11987 /* These should never be enabled. */
11988 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
11989 break;
11990
11991 case bp_std_terminate_master:
11992 /* These should never be enabled. */
11993 gdb_printf (_("std::terminate Master Breakpoint: "
11994 "gdb should not stop!\n"));
11995 break;
11996
11997 case bp_exception_master:
11998 /* These should never be enabled. */
11999 gdb_printf (_("Exception Master Breakpoint: "
12000 "gdb should not stop!\n"));
12001 break;
12002 }
12003
12004 return PRINT_NOTHING;
12005 }
12006
12007 void
12008 internal_breakpoint::print_mention () const
12009 {
12010 /* Nothing to mention. These breakpoints are internal. */
12011 }
12012
12013 /* Virtual table for momentary breakpoints */
12014
12015 void
12016 momentary_breakpoint::re_set ()
12017 {
12018 /* Keep temporary breakpoints, which can be encountered when we step
12019 over a dlopen call and solib_add is resetting the breakpoints.
12020 Otherwise these should have been blown away via the cleanup chain
12021 or by breakpoint_init_inferior when we rerun the executable. */
12022 }
12023
12024 void
12025 momentary_breakpoint::check_status (bpstat *bs)
12026 {
12027 /* Nothing. The point of these breakpoints is causing a stop. */
12028 }
12029
12030 enum print_stop_action
12031 momentary_breakpoint::print_it (const bpstat *bs) const
12032 {
12033 return PRINT_UNKNOWN;
12034 }
12035
12036 void
12037 momentary_breakpoint::print_mention () const
12038 {
12039 /* Nothing to mention. These breakpoints are internal. */
12040 }
12041
12042 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12043
12044 It gets cleared already on the removal of the first one of such placed
12045 breakpoints. This is OK as they get all removed altogether. */
12046
12047 longjmp_breakpoint::~longjmp_breakpoint ()
12048 {
12049 thread_info *tp = find_thread_global_id (this->thread);
12050
12051 if (tp != NULL)
12052 tp->initiating_frame = null_frame_id;
12053 }
12054
12055 static void
12056 bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
12057 struct linespec_result *canonical)
12058
12059 {
12060 struct linespec_sals lsal;
12061
12062 lsal.sals = parse_probes (locspec, NULL, canonical);
12063 lsal.canonical = xstrdup (canonical->locspec->to_string ());
12064 canonical->lsals.push_back (std::move (lsal));
12065 }
12066
12067 static std::vector<symtab_and_line>
12068 bkpt_probe_decode_location_spec (struct breakpoint *b,
12069 location_spec *locspec,
12070 program_space *search_pspace)
12071 {
12072 std::vector<symtab_and_line> sals
12073 = parse_probes (locspec, search_pspace, NULL);
12074 if (sals.empty ())
12075 error (_("probe not found"));
12076 return sals;
12077 }
12078
12079 int
12080 tracepoint::breakpoint_hit (const struct bp_location *bl,
12081 const address_space *aspace, CORE_ADDR bp_addr,
12082 const target_waitstatus &ws)
12083 {
12084 /* By definition, the inferior does not report stops at
12085 tracepoints. */
12086 return 0;
12087 }
12088
12089 void
12090 tracepoint::print_one_detail (struct ui_out *uiout) const
12091 {
12092 if (!static_trace_marker_id.empty ())
12093 {
12094 gdb_assert (type == bp_static_tracepoint
12095 || type == bp_static_marker_tracepoint);
12096
12097 uiout->message ("\tmarker id is %pF\n",
12098 string_field ("static-tracepoint-marker-string-id",
12099 static_trace_marker_id.c_str ()));
12100 }
12101 }
12102
12103 void
12104 tracepoint::print_mention () const
12105 {
12106 if (current_uiout->is_mi_like_p ())
12107 return;
12108
12109 switch (type)
12110 {
12111 case bp_tracepoint:
12112 gdb_printf (_("Tracepoint"));
12113 gdb_printf (_(" %d"), number);
12114 break;
12115 case bp_fast_tracepoint:
12116 gdb_printf (_("Fast tracepoint"));
12117 gdb_printf (_(" %d"), number);
12118 break;
12119 case bp_static_tracepoint:
12120 case bp_static_marker_tracepoint:
12121 gdb_printf (_("Static tracepoint"));
12122 gdb_printf (_(" %d"), number);
12123 break;
12124 default:
12125 internal_error (_("unhandled tracepoint type %d"), (int) type);
12126 }
12127
12128 say_where ();
12129 }
12130
12131 void
12132 tracepoint::print_recreate (struct ui_file *fp) const
12133 {
12134 if (type == bp_fast_tracepoint)
12135 gdb_printf (fp, "ftrace");
12136 else if (type == bp_static_tracepoint
12137 || type == bp_static_marker_tracepoint)
12138 gdb_printf (fp, "strace");
12139 else if (type == bp_tracepoint)
12140 gdb_printf (fp, "trace");
12141 else
12142 internal_error (_("unhandled tracepoint type %d"), (int) type);
12143
12144 gdb_printf (fp, " %s", locspec->to_string ());
12145 print_recreate_thread (fp);
12146
12147 if (pass_count)
12148 gdb_printf (fp, " passcount %d\n", pass_count);
12149 }
12150
12151 /* Virtual table for tracepoints on static probes. */
12152
12153 static void
12154 tracepoint_probe_create_sals_from_location_spec
12155 (location_spec *locspec,
12156 struct linespec_result *canonical)
12157 {
12158 /* We use the same method for breakpoint on probes. */
12159 bkpt_probe_create_sals_from_location_spec (locspec, canonical);
12160 }
12161
12162 void
12163 dprintf_breakpoint::re_set ()
12164 {
12165 re_set_default ();
12166
12167 /* extra_string should never be non-NULL for dprintf. */
12168 gdb_assert (extra_string != NULL);
12169
12170 /* 1 - connect to target 1, that can run breakpoint commands.
12171 2 - create a dprintf, which resolves fine.
12172 3 - disconnect from target 1
12173 4 - connect to target 2, that can NOT run breakpoint commands.
12174
12175 After steps #3/#4, you'll want the dprintf command list to
12176 be updated, because target 1 and 2 may well return different
12177 answers for target_can_run_breakpoint_commands().
12178 Given absence of finer grained resetting, we get to do
12179 it all the time. */
12180 if (extra_string != NULL)
12181 update_dprintf_command_list (this);
12182 }
12183
12184 /* Implement the "print_recreate" method for dprintf. */
12185
12186 void
12187 dprintf_breakpoint::print_recreate (struct ui_file *fp) const
12188 {
12189 gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
12190 print_recreate_thread (fp);
12191 }
12192
12193 /* Implement the "after_condition_true" method for dprintf.
12194
12195 dprintf's are implemented with regular commands in their command
12196 list, but we run the commands here instead of before presenting the
12197 stop to the user, as dprintf's don't actually cause a stop. This
12198 also makes it so that the commands of multiple dprintfs at the same
12199 address are all handled. */
12200
12201 void
12202 dprintf_breakpoint::after_condition_true (struct bpstat *bs)
12203 {
12204 /* dprintf's never cause a stop. This wasn't set in the
12205 check_status hook instead because that would make the dprintf's
12206 condition not be evaluated. */
12207 bs->stop = false;
12208
12209 /* Run the command list here. Take ownership of it instead of
12210 copying. We never want these commands to run later in
12211 bpstat_do_actions, if a breakpoint that causes a stop happens to
12212 be set at same address as this dprintf, or even if running the
12213 commands here throws. */
12214 counted_command_line cmds = std::move (bs->commands);
12215 gdb_assert (cmds != nullptr);
12216 execute_control_commands (cmds.get (), 0);
12217 }
12218
12219 /* The breakpoint_ops structure to be used on static tracepoints with
12220 markers (`-m'). */
12221
12222 static void
12223 strace_marker_create_sals_from_location_spec (location_spec *locspec,
12224 struct linespec_result *canonical)
12225 {
12226 struct linespec_sals lsal;
12227 const char *arg_start, *arg;
12228
12229 arg = arg_start = as_linespec_location_spec (locspec)->spec_string;
12230 lsal.sals = decode_static_tracepoint_spec (&arg);
12231
12232 std::string str (arg_start, arg - arg_start);
12233 const char *ptr = str.c_str ();
12234 canonical->locspec
12235 = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
12236
12237 lsal.canonical = xstrdup (canonical->locspec->to_string ());
12238 canonical->lsals.push_back (std::move (lsal));
12239 }
12240
12241 static void
12242 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12243 struct linespec_result *canonical,
12244 gdb::unique_xmalloc_ptr<char> cond_string,
12245 gdb::unique_xmalloc_ptr<char> extra_string,
12246 enum bptype type_wanted,
12247 enum bpdisp disposition,
12248 int thread,
12249 int task, int ignore_count,
12250 int from_tty, int enabled,
12251 int internal, unsigned flags)
12252 {
12253 const linespec_sals &lsal = canonical->lsals[0];
12254
12255 /* If the user is creating a static tracepoint by marker id
12256 (strace -m MARKER_ID), then store the sals index, so that
12257 breakpoint_re_set can try to match up which of the newly
12258 found markers corresponds to this one, and, don't try to
12259 expand multiple locations for each sal, given than SALS
12260 already should contain all sals for MARKER_ID. */
12261
12262 for (size_t i = 0; i < lsal.sals.size (); i++)
12263 {
12264 location_spec_up locspec = canonical->locspec->clone ();
12265
12266 std::unique_ptr<tracepoint> tp
12267 (new tracepoint (gdbarch,
12268 type_wanted,
12269 lsal.sals[i],
12270 std::move (locspec),
12271 NULL,
12272 std::move (cond_string),
12273 std::move (extra_string),
12274 disposition,
12275 thread, task, ignore_count,
12276 from_tty, enabled, flags,
12277 canonical->special_display));
12278
12279 /* Given that its possible to have multiple markers with
12280 the same string id, if the user is creating a static
12281 tracepoint by marker id ("strace -m MARKER_ID"), then
12282 store the sals index, so that breakpoint_re_set can
12283 try to match up which of the newly found markers
12284 corresponds to this one */
12285 tp->static_trace_marker_id_idx = i;
12286
12287 install_breakpoint (internal, std::move (tp), 0);
12288 }
12289 }
12290
12291 std::vector<symtab_and_line>
12292 static_marker_tracepoint::decode_location_spec (location_spec *locspec,
12293 program_space *search_pspace)
12294 {
12295 const char *s = as_linespec_location_spec (locspec)->spec_string;
12296
12297 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12298 if (sals.size () > static_trace_marker_id_idx)
12299 {
12300 sals[0] = sals[static_trace_marker_id_idx];
12301 sals.resize (1);
12302 return sals;
12303 }
12304 else
12305 error (_("marker %s not found"), static_trace_marker_id.c_str ());
12306 }
12307
12308 /* Static tracepoints with marker (`-m'). */
12309 static struct breakpoint_ops strace_marker_breakpoint_ops =
12310 {
12311 strace_marker_create_sals_from_location_spec,
12312 strace_marker_create_breakpoints_sal,
12313 };
12314
12315 static bool
12316 strace_marker_p (struct breakpoint *b)
12317 {
12318 return b->type == bp_static_marker_tracepoint;
12319 }
12320
12321 /* Delete a breakpoint and clean up all traces of it in the data
12322 structures. */
12323
12324 void
12325 delete_breakpoint (struct breakpoint *bpt)
12326 {
12327 gdb_assert (bpt != NULL);
12328
12329 /* Has this bp already been deleted? This can happen because
12330 multiple lists can hold pointers to bp's. bpstat lists are
12331 especial culprits.
12332
12333 One example of this happening is a watchpoint's scope bp. When
12334 the scope bp triggers, we notice that the watchpoint is out of
12335 scope, and delete it. We also delete its scope bp. But the
12336 scope bp is marked "auto-deleting", and is already on a bpstat.
12337 That bpstat is then checked for auto-deleting bp's, which are
12338 deleted.
12339
12340 A real solution to this problem might involve reference counts in
12341 bp's, and/or giving them pointers back to their referencing
12342 bpstat's, and teaching delete_breakpoint to only free a bp's
12343 storage when no more references were extent. A cheaper bandaid
12344 was chosen. */
12345 if (bpt->type == bp_none)
12346 return;
12347
12348 /* At least avoid this stale reference until the reference counting
12349 of breakpoints gets resolved. */
12350 if (bpt->related_breakpoint != bpt)
12351 {
12352 struct breakpoint *related;
12353 struct watchpoint *w;
12354
12355 if (bpt->type == bp_watchpoint_scope)
12356 w = (struct watchpoint *) bpt->related_breakpoint;
12357 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12358 w = (struct watchpoint *) bpt;
12359 else
12360 w = NULL;
12361 if (w != NULL)
12362 watchpoint_del_at_next_stop (w);
12363
12364 /* Unlink bpt from the bpt->related_breakpoint ring. */
12365 for (related = bpt; related->related_breakpoint != bpt;
12366 related = related->related_breakpoint);
12367 related->related_breakpoint = bpt->related_breakpoint;
12368 bpt->related_breakpoint = bpt;
12369 }
12370
12371 /* watch_command_1 creates a watchpoint but only sets its number if
12372 update_watchpoint succeeds in creating its bp_locations. If there's
12373 a problem in that process, we'll be asked to delete the half-created
12374 watchpoint. In that case, don't announce the deletion. */
12375 if (bpt->number)
12376 gdb::observers::breakpoint_deleted.notify (bpt);
12377
12378 if (breakpoint_chain == bpt)
12379 breakpoint_chain = bpt->next;
12380
12381 for (breakpoint *b : all_breakpoints ())
12382 if (b->next == bpt)
12383 {
12384 b->next = bpt->next;
12385 break;
12386 }
12387
12388 /* Be sure no bpstat's are pointing at the breakpoint after it's
12389 been freed. */
12390 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12391 in all threads for now. Note that we cannot just remove bpstats
12392 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12393 commands are associated with the bpstat; if we remove it here,
12394 then the later call to bpstat_do_actions (&stop_bpstat); in
12395 event-top.c won't do anything, and temporary breakpoints with
12396 commands won't work. */
12397
12398 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12399
12400 /* Now that breakpoint is removed from breakpoint list, update the
12401 global location list. This will remove locations that used to
12402 belong to this breakpoint. Do this before freeing the breakpoint
12403 itself, since remove_breakpoint looks at location's owner. It
12404 might be better design to have location completely
12405 self-contained, but it's not the case now. */
12406 update_global_location_list (UGLL_DONT_INSERT);
12407
12408 /* On the chance that someone will soon try again to delete this
12409 same bp, we mark it as deleted before freeing its storage. */
12410 bpt->type = bp_none;
12411 delete bpt;
12412 }
12413
12414 /* Iterator function to call a user-provided callback function once
12415 for each of B and its related breakpoints. */
12416
12417 static void
12418 iterate_over_related_breakpoints (struct breakpoint *b,
12419 gdb::function_view<void (breakpoint *)> function)
12420 {
12421 struct breakpoint *related;
12422
12423 related = b;
12424 do
12425 {
12426 struct breakpoint *next;
12427
12428 /* FUNCTION may delete RELATED. */
12429 next = related->related_breakpoint;
12430
12431 if (next == related)
12432 {
12433 /* RELATED is the last ring entry. */
12434 function (related);
12435
12436 /* FUNCTION may have deleted it, so we'd never reach back to
12437 B. There's nothing left to do anyway, so just break
12438 out. */
12439 break;
12440 }
12441 else
12442 function (related);
12443
12444 related = next;
12445 }
12446 while (related != b);
12447 }
12448
12449 static void
12450 delete_command (const char *arg, int from_tty)
12451 {
12452 dont_repeat ();
12453
12454 if (arg == 0)
12455 {
12456 int breaks_to_delete = 0;
12457
12458 /* Delete all breakpoints if no argument. Do not delete
12459 internal breakpoints, these have to be deleted with an
12460 explicit breakpoint number argument. */
12461 for (breakpoint *b : all_breakpoints ())
12462 if (user_breakpoint_p (b))
12463 {
12464 breaks_to_delete = 1;
12465 break;
12466 }
12467
12468 /* Ask user only if there are some breakpoints to delete. */
12469 if (!from_tty
12470 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
12471 for (breakpoint *b : all_breakpoints_safe ())
12472 if (user_breakpoint_p (b))
12473 delete_breakpoint (b);
12474 }
12475 else
12476 map_breakpoint_numbers
12477 (arg, [&] (breakpoint *br)
12478 {
12479 iterate_over_related_breakpoints (br, delete_breakpoint);
12480 });
12481 }
12482
12483 /* Return true if all locations of B bound to PSPACE are pending. If
12484 PSPACE is NULL, all locations of all program spaces are
12485 considered. */
12486
12487 static bool
12488 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
12489 {
12490 for (bp_location *loc : b->locations ())
12491 if ((pspace == NULL
12492 || loc->pspace == pspace)
12493 && !loc->shlib_disabled
12494 && !loc->pspace->executing_startup)
12495 return false;
12496 return true;
12497 }
12498
12499 /* Subroutine of update_breakpoint_locations to simplify it.
12500 Return true if multiple fns in list LOC have the same name.
12501 Null names are ignored. */
12502
12503 static bool
12504 ambiguous_names_p (struct bp_location *loc)
12505 {
12506 struct bp_location *l;
12507 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
12508 xcalloc, xfree));
12509
12510 for (l = loc; l != NULL; l = l->next)
12511 {
12512 const char **slot;
12513 const char *name = l->function_name.get ();
12514
12515 /* Allow for some names to be NULL, ignore them. */
12516 if (name == NULL)
12517 continue;
12518
12519 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
12520 INSERT);
12521 /* NOTE: We can assume slot != NULL here because xcalloc never
12522 returns NULL. */
12523 if (*slot != NULL)
12524 return true;
12525 *slot = name;
12526 }
12527
12528 return false;
12529 }
12530
12531 /* When symbols change, it probably means the sources changed as well,
12532 and it might mean the static tracepoint markers are no longer at
12533 the same address or line numbers they used to be at last we
12534 checked. Losing your static tracepoints whenever you rebuild is
12535 undesirable. This function tries to resync/rematch gdb static
12536 tracepoints with the markers on the target, for static tracepoints
12537 that have not been set by marker id. Static tracepoint that have
12538 been set by marker id are reset by marker id in breakpoint_re_set.
12539 The heuristic is:
12540
12541 1) For a tracepoint set at a specific address, look for a marker at
12542 the old PC. If one is found there, assume to be the same marker.
12543 If the name / string id of the marker found is different from the
12544 previous known name, assume that means the user renamed the marker
12545 in the sources, and output a warning.
12546
12547 2) For a tracepoint set at a given line number, look for a marker
12548 at the new address of the old line number. If one is found there,
12549 assume to be the same marker. If the name / string id of the
12550 marker found is different from the previous known name, assume that
12551 means the user renamed the marker in the sources, and output a
12552 warning.
12553
12554 3) If a marker is no longer found at the same address or line, it
12555 may mean the marker no longer exists. But it may also just mean
12556 the code changed a bit. Maybe the user added a few lines of code
12557 that made the marker move up or down (in line number terms). Ask
12558 the target for info about the marker with the string id as we knew
12559 it. If found, update line number and address in the matching
12560 static tracepoint. This will get confused if there's more than one
12561 marker with the same ID (possible in UST, although unadvised
12562 precisely because it confuses tools). */
12563
12564 static struct symtab_and_line
12565 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12566 {
12567 struct tracepoint *tp = (struct tracepoint *) b;
12568 struct static_tracepoint_marker marker;
12569 CORE_ADDR pc;
12570
12571 pc = sal.pc;
12572 if (sal.line)
12573 find_line_pc (sal.symtab, sal.line, &pc);
12574
12575 if (target_static_tracepoint_marker_at (pc, &marker))
12576 {
12577 if (tp->static_trace_marker_id != marker.str_id)
12578 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12579 b->number, tp->static_trace_marker_id.c_str (),
12580 marker.str_id.c_str ());
12581
12582 tp->static_trace_marker_id = std::move (marker.str_id);
12583
12584 return sal;
12585 }
12586
12587 /* Old marker wasn't found on target at lineno. Try looking it up
12588 by string ID. */
12589 if (!sal.explicit_pc
12590 && sal.line != 0
12591 && sal.symtab != NULL
12592 && !tp->static_trace_marker_id.empty ())
12593 {
12594 std::vector<static_tracepoint_marker> markers
12595 = target_static_tracepoint_markers_by_strid
12596 (tp->static_trace_marker_id.c_str ());
12597
12598 if (!markers.empty ())
12599 {
12600 struct symbol *sym;
12601 struct static_tracepoint_marker *tpmarker;
12602 struct ui_out *uiout = current_uiout;
12603
12604 tpmarker = &markers[0];
12605
12606 tp->static_trace_marker_id = std::move (tpmarker->str_id);
12607
12608 warning (_("marker for static tracepoint %d (%s) not "
12609 "found at previous line number"),
12610 b->number, tp->static_trace_marker_id.c_str ());
12611
12612 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
12613 sym = find_pc_sect_function (tpmarker->address, NULL);
12614 uiout->text ("Now in ");
12615 if (sym)
12616 {
12617 uiout->field_string ("func", sym->print_name (),
12618 function_name_style.style ());
12619 uiout->text (" at ");
12620 }
12621 uiout->field_string ("file",
12622 symtab_to_filename_for_display (sal2.symtab),
12623 file_name_style.style ());
12624 uiout->text (":");
12625
12626 if (uiout->is_mi_like_p ())
12627 {
12628 const char *fullname = symtab_to_fullname (sal2.symtab);
12629
12630 uiout->field_string ("fullname", fullname);
12631 }
12632
12633 uiout->field_signed ("line", sal2.line);
12634 uiout->text ("\n");
12635
12636 b->loc->line_number = sal2.line;
12637 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
12638
12639 std::unique_ptr<explicit_location_spec> els
12640 (new explicit_location_spec ());
12641 els->source_filename
12642 = xstrdup (symtab_to_filename_for_display (sal2.symtab));
12643 els->line_offset.offset = b->loc->line_number;
12644 els->line_offset.sign = LINE_OFFSET_NONE;
12645
12646 b->locspec = std::move (els);
12647
12648 /* Might be nice to check if function changed, and warn if
12649 so. */
12650 }
12651 }
12652 return sal;
12653 }
12654
12655 /* Returns true iff locations A and B are sufficiently same that
12656 we don't need to report breakpoint as changed. */
12657
12658 static bool
12659 locations_are_equal (struct bp_location *a, struct bp_location *b)
12660 {
12661 while (a && b)
12662 {
12663 if (a->address != b->address)
12664 return false;
12665
12666 if (a->shlib_disabled != b->shlib_disabled)
12667 return false;
12668
12669 if (a->enabled != b->enabled)
12670 return false;
12671
12672 if (a->disabled_by_cond != b->disabled_by_cond)
12673 return false;
12674
12675 a = a->next;
12676 b = b->next;
12677 }
12678
12679 if ((a == NULL) != (b == NULL))
12680 return false;
12681
12682 return true;
12683 }
12684
12685 /* Split all locations of B that are bound to PSPACE out of B's
12686 location list to a separate list and return that list's head. If
12687 PSPACE is NULL, hoist out all locations of B. */
12688
12689 static struct bp_location *
12690 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12691 {
12692 struct bp_location head;
12693 struct bp_location *i = b->loc;
12694 struct bp_location **i_link = &b->loc;
12695 struct bp_location *hoisted = &head;
12696
12697 if (pspace == NULL)
12698 {
12699 i = b->loc;
12700 b->loc = NULL;
12701 return i;
12702 }
12703
12704 head.next = NULL;
12705
12706 while (i != NULL)
12707 {
12708 if (i->pspace == pspace)
12709 {
12710 *i_link = i->next;
12711 i->next = NULL;
12712 hoisted->next = i;
12713 hoisted = i;
12714 }
12715 else
12716 i_link = &i->next;
12717 i = *i_link;
12718 }
12719
12720 return head.next;
12721 }
12722
12723 /* Create new breakpoint locations for B (a hardware or software
12724 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12725 zero, then B is a ranged breakpoint. Only recreates locations for
12726 FILTER_PSPACE. Locations of other program spaces are left
12727 untouched. */
12728
12729 void
12730 update_breakpoint_locations (code_breakpoint *b,
12731 struct program_space *filter_pspace,
12732 gdb::array_view<const symtab_and_line> sals,
12733 gdb::array_view<const symtab_and_line> sals_end)
12734 {
12735 struct bp_location *existing_locations;
12736
12737 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
12738 {
12739 /* Ranged breakpoints have only one start location and one end
12740 location. */
12741 b->enable_state = bp_disabled;
12742 gdb_printf (gdb_stderr,
12743 _("Could not reset ranged breakpoint %d: "
12744 "multiple locations found\n"),
12745 b->number);
12746 return;
12747 }
12748
12749 /* If there's no new locations, and all existing locations are
12750 pending, don't do anything. This optimizes the common case where
12751 all locations are in the same shared library, that was unloaded.
12752 We'd like to retain the location, so that when the library is
12753 loaded again, we don't loose the enabled/disabled status of the
12754 individual locations. */
12755 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
12756 return;
12757
12758 existing_locations = hoist_existing_locations (b, filter_pspace);
12759
12760 for (const auto &sal : sals)
12761 {
12762 struct bp_location *new_loc;
12763
12764 switch_to_program_space_and_thread (sal.pspace);
12765
12766 new_loc = b->add_location (sal);
12767
12768 /* Reparse conditions, they might contain references to the
12769 old symtab. */
12770 if (b->cond_string != NULL)
12771 {
12772 const char *s;
12773
12774 s = b->cond_string.get ();
12775 try
12776 {
12777 new_loc->cond = parse_exp_1 (&s, sal.pc,
12778 block_for_pc (sal.pc),
12779 0);
12780 }
12781 catch (const gdb_exception_error &e)
12782 {
12783 new_loc->disabled_by_cond = true;
12784 }
12785 }
12786
12787 if (!sals_end.empty ())
12788 {
12789 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
12790
12791 new_loc->length = end - sals[0].pc + 1;
12792 }
12793 }
12794
12795 /* If possible, carry over 'disable' status from existing
12796 breakpoints. */
12797 {
12798 struct bp_location *e = existing_locations;
12799 /* If there are multiple breakpoints with the same function name,
12800 e.g. for inline functions, comparing function names won't work.
12801 Instead compare pc addresses; this is just a heuristic as things
12802 may have moved, but in practice it gives the correct answer
12803 often enough until a better solution is found. */
12804 int have_ambiguous_names = ambiguous_names_p (b->loc);
12805
12806 for (; e; e = e->next)
12807 {
12808 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
12809 {
12810 if (have_ambiguous_names)
12811 {
12812 for (bp_location *l : b->locations ())
12813 {
12814 /* Ignore software vs hardware location type at
12815 this point, because with "set breakpoint
12816 auto-hw", after a re-set, locations that were
12817 hardware can end up as software, or vice versa.
12818 As mentioned above, this is an heuristic and in
12819 practice should give the correct answer often
12820 enough. */
12821 if (breakpoint_locations_match (e, l, true))
12822 {
12823 l->enabled = e->enabled;
12824 l->disabled_by_cond = e->disabled_by_cond;
12825 break;
12826 }
12827 }
12828 }
12829 else
12830 {
12831 for (bp_location *l : b->locations ())
12832 if (l->function_name
12833 && strcmp (e->function_name.get (),
12834 l->function_name.get ()) == 0)
12835 {
12836 l->enabled = e->enabled;
12837 l->disabled_by_cond = e->disabled_by_cond;
12838 break;
12839 }
12840 }
12841 }
12842 }
12843 }
12844
12845 if (!locations_are_equal (existing_locations, b->loc))
12846 gdb::observers::breakpoint_modified.notify (b);
12847 }
12848
12849 /* Find the SaL locations corresponding to the given LOCSPEC.
12850 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12851
12852 std::vector<symtab_and_line>
12853 code_breakpoint::location_spec_to_sals (location_spec *locspec,
12854 struct program_space *search_pspace,
12855 int *found)
12856 {
12857 struct gdb_exception exception;
12858
12859 std::vector<symtab_and_line> sals;
12860
12861 try
12862 {
12863 sals = decode_location_spec (locspec, search_pspace);
12864 }
12865 catch (gdb_exception_error &e)
12866 {
12867 int not_found_and_ok = false;
12868
12869 /* For pending breakpoints, it's expected that parsing will
12870 fail until the right shared library is loaded. User has
12871 already told to create pending breakpoints and don't need
12872 extra messages. If breakpoint is in bp_shlib_disabled
12873 state, then user already saw the message about that
12874 breakpoint being disabled, and don't want to see more
12875 errors. */
12876 if (e.error == NOT_FOUND_ERROR
12877 && (condition_not_parsed
12878 || (loc != NULL
12879 && search_pspace != NULL
12880 && loc->pspace != search_pspace)
12881 || (loc && loc->shlib_disabled)
12882 || (loc && loc->pspace->executing_startup)
12883 || enable_state == bp_disabled))
12884 not_found_and_ok = true;
12885
12886 if (!not_found_and_ok)
12887 {
12888 /* We surely don't want to warn about the same breakpoint
12889 10 times. One solution, implemented here, is disable
12890 the breakpoint on error. Another solution would be to
12891 have separate 'warning emitted' flag. Since this
12892 happens only when a binary has changed, I don't know
12893 which approach is better. */
12894 enable_state = bp_disabled;
12895 throw;
12896 }
12897
12898 exception = std::move (e);
12899 }
12900
12901 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
12902 {
12903 for (auto &sal : sals)
12904 resolve_sal_pc (&sal);
12905 if (condition_not_parsed && extra_string != NULL)
12906 {
12907 gdb::unique_xmalloc_ptr<char> local_cond, local_extra;
12908 int local_thread, local_task;
12909
12910 find_condition_and_thread_for_sals (sals, extra_string.get (),
12911 &local_cond, &local_thread,
12912 &local_task, &local_extra);
12913 gdb_assert (cond_string == nullptr);
12914 if (local_cond != nullptr)
12915 cond_string = std::move (local_cond);
12916 thread = local_thread;
12917 task = local_task;
12918 if (local_extra != nullptr)
12919 extra_string = std::move (local_extra);
12920 condition_not_parsed = 0;
12921 }
12922
12923 if (type == bp_static_tracepoint)
12924 sals[0] = update_static_tracepoint (this, sals[0]);
12925
12926 *found = 1;
12927 }
12928 else
12929 *found = 0;
12930
12931 return sals;
12932 }
12933
12934 /* The default re_set method, for typical hardware or software
12935 breakpoints. Reevaluate the breakpoint and recreate its
12936 locations. */
12937
12938 void
12939 code_breakpoint::re_set_default ()
12940 {
12941 struct program_space *filter_pspace = current_program_space;
12942 std::vector<symtab_and_line> expanded, expanded_end;
12943
12944 int found;
12945 std::vector<symtab_and_line> sals = location_spec_to_sals (locspec.get (),
12946 filter_pspace,
12947 &found);
12948 if (found)
12949 expanded = std::move (sals);
12950
12951 if (locspec_range_end != nullptr)
12952 {
12953 std::vector<symtab_and_line> sals_end
12954 = location_spec_to_sals (locspec_range_end.get (),
12955 filter_pspace, &found);
12956 if (found)
12957 expanded_end = std::move (sals_end);
12958 }
12959
12960 update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
12961 }
12962
12963 /* Default method for creating SALs from an address string. It basically
12964 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12965
12966 static void
12967 create_sals_from_location_spec_default (location_spec *locspec,
12968 struct linespec_result *canonical)
12969 {
12970 parse_breakpoint_sals (locspec, canonical);
12971 }
12972
12973 /* Reset a breakpoint. */
12974
12975 static void
12976 breakpoint_re_set_one (breakpoint *b)
12977 {
12978 input_radix = b->input_radix;
12979 set_language (b->language);
12980
12981 b->re_set ();
12982 }
12983
12984 /* Re-set breakpoint locations for the current program space.
12985 Locations bound to other program spaces are left untouched. */
12986
12987 void
12988 breakpoint_re_set (void)
12989 {
12990 {
12991 scoped_restore_current_language save_language;
12992 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
12993 scoped_restore_current_pspace_and_thread restore_pspace_thread;
12994
12995 /* breakpoint_re_set_one sets the current_language to the language
12996 of the breakpoint it is resetting (see prepare_re_set_context)
12997 before re-evaluating the breakpoint's location. This change can
12998 unfortunately get undone by accident if the language_mode is set
12999 to auto, and we either switch frames, or more likely in this context,
13000 we select the current frame.
13001
13002 We prevent this by temporarily turning the language_mode to
13003 language_mode_manual. We restore it once all breakpoints
13004 have been reset. */
13005 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13006 language_mode = language_mode_manual;
13007
13008 /* Note: we must not try to insert locations until after all
13009 breakpoints have been re-set. Otherwise, e.g., when re-setting
13010 breakpoint 1, we'd insert the locations of breakpoint 2, which
13011 hadn't been re-set yet, and thus may have stale locations. */
13012
13013 for (breakpoint *b : all_breakpoints_safe ())
13014 {
13015 try
13016 {
13017 breakpoint_re_set_one (b);
13018 }
13019 catch (const gdb_exception &ex)
13020 {
13021 exception_fprintf (gdb_stderr, ex,
13022 "Error in re-setting breakpoint %d: ",
13023 b->number);
13024 }
13025 }
13026
13027 jit_breakpoint_re_set ();
13028 }
13029
13030 create_overlay_event_breakpoint ();
13031 create_longjmp_master_breakpoint ();
13032 create_std_terminate_master_breakpoint ();
13033 create_exception_master_breakpoint ();
13034
13035 /* Now we can insert. */
13036 update_global_location_list (UGLL_MAY_INSERT);
13037 }
13038 \f
13039 /* Reset the thread number of this breakpoint:
13040
13041 - If the breakpoint is for all threads, leave it as-is.
13042 - Else, reset it to the current thread for inferior_ptid. */
13043 void
13044 breakpoint_re_set_thread (struct breakpoint *b)
13045 {
13046 if (b->thread != -1)
13047 {
13048 b->thread = inferior_thread ()->global_num;
13049
13050 /* We're being called after following a fork. The new fork is
13051 selected as current, and unless this was a vfork will have a
13052 different program space from the original thread. Reset that
13053 as well. */
13054 b->loc->pspace = current_program_space;
13055 }
13056 }
13057
13058 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13059 If from_tty is nonzero, it prints a message to that effect,
13060 which ends with a period (no newline). */
13061
13062 void
13063 set_ignore_count (int bptnum, int count, int from_tty)
13064 {
13065 if (count < 0)
13066 count = 0;
13067
13068 for (breakpoint *b : all_breakpoints ())
13069 if (b->number == bptnum)
13070 {
13071 if (is_tracepoint (b))
13072 {
13073 if (from_tty && count != 0)
13074 gdb_printf (_("Ignore count ignored for tracepoint %d."),
13075 bptnum);
13076 return;
13077 }
13078
13079 b->ignore_count = count;
13080 if (from_tty)
13081 {
13082 if (count == 0)
13083 gdb_printf (_("Will stop next time "
13084 "breakpoint %d is reached."),
13085 bptnum);
13086 else if (count == 1)
13087 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
13088 bptnum);
13089 else
13090 gdb_printf (_("Will ignore next %d "
13091 "crossings of breakpoint %d."),
13092 count, bptnum);
13093 }
13094 gdb::observers::breakpoint_modified.notify (b);
13095 return;
13096 }
13097
13098 error (_("No breakpoint number %d."), bptnum);
13099 }
13100
13101 /* Command to set ignore-count of breakpoint N to COUNT. */
13102
13103 static void
13104 ignore_command (const char *args, int from_tty)
13105 {
13106 const char *p = args;
13107 int num;
13108
13109 if (p == 0)
13110 error_no_arg (_("a breakpoint number"));
13111
13112 num = get_number (&p);
13113 if (num == 0)
13114 error (_("bad breakpoint number: '%s'"), args);
13115 if (*p == 0)
13116 error (_("Second argument (specified ignore-count) is missing."));
13117
13118 set_ignore_count (num,
13119 longest_to_int (value_as_long (parse_and_eval (p))),
13120 from_tty);
13121 if (from_tty)
13122 gdb_printf ("\n");
13123 }
13124 \f
13125
13126 /* Call FUNCTION on each of the breakpoints with numbers in the range
13127 defined by BP_NUM_RANGE (an inclusive range). */
13128
13129 static void
13130 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13131 gdb::function_view<void (breakpoint *)> function)
13132 {
13133 if (bp_num_range.first == 0)
13134 {
13135 warning (_("bad breakpoint number at or near '%d'"),
13136 bp_num_range.first);
13137 }
13138 else
13139 {
13140 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13141 {
13142 bool match = false;
13143
13144 for (breakpoint *b : all_breakpoints_safe ())
13145 if (b->number == i)
13146 {
13147 match = true;
13148 function (b);
13149 break;
13150 }
13151 if (!match)
13152 gdb_printf (_("No breakpoint number %d.\n"), i);
13153 }
13154 }
13155 }
13156
13157 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13158 ARGS. */
13159
13160 static void
13161 map_breakpoint_numbers (const char *args,
13162 gdb::function_view<void (breakpoint *)> function)
13163 {
13164 if (args == NULL || *args == '\0')
13165 error_no_arg (_("one or more breakpoint numbers"));
13166
13167 number_or_range_parser parser (args);
13168
13169 while (!parser.finished ())
13170 {
13171 int num = parser.get_number ();
13172 map_breakpoint_number_range (std::make_pair (num, num), function);
13173 }
13174 }
13175
13176 /* Return the breakpoint location structure corresponding to the
13177 BP_NUM and LOC_NUM values. */
13178
13179 static struct bp_location *
13180 find_location_by_number (int bp_num, int loc_num)
13181 {
13182 breakpoint *b = get_breakpoint (bp_num);
13183
13184 if (!b || b->number != bp_num)
13185 error (_("Bad breakpoint number '%d'"), bp_num);
13186
13187 if (loc_num == 0)
13188 error (_("Bad breakpoint location number '%d'"), loc_num);
13189
13190 int n = 0;
13191 for (bp_location *loc : b->locations ())
13192 if (++n == loc_num)
13193 return loc;
13194
13195 error (_("Bad breakpoint location number '%d'"), loc_num);
13196 }
13197
13198 /* Modes of operation for extract_bp_num. */
13199 enum class extract_bp_kind
13200 {
13201 /* Extracting a breakpoint number. */
13202 bp,
13203
13204 /* Extracting a location number. */
13205 loc,
13206 };
13207
13208 /* Extract a breakpoint or location number (as determined by KIND)
13209 from the string starting at START. TRAILER is a character which
13210 can be found after the number. If you don't want a trailer, use
13211 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13212 string. This always returns a positive integer. */
13213
13214 static int
13215 extract_bp_num (extract_bp_kind kind, const char *start,
13216 int trailer, const char **end_out = NULL)
13217 {
13218 const char *end = start;
13219 int num = get_number_trailer (&end, trailer);
13220 if (num < 0)
13221 error (kind == extract_bp_kind::bp
13222 ? _("Negative breakpoint number '%.*s'")
13223 : _("Negative breakpoint location number '%.*s'"),
13224 int (end - start), start);
13225 if (num == 0)
13226 error (kind == extract_bp_kind::bp
13227 ? _("Bad breakpoint number '%.*s'")
13228 : _("Bad breakpoint location number '%.*s'"),
13229 int (end - start), start);
13230
13231 if (end_out != NULL)
13232 *end_out = end;
13233 return num;
13234 }
13235
13236 /* Extract a breakpoint or location range (as determined by KIND) in
13237 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13238 representing the (inclusive) range. The returned pair's elements
13239 are always positive integers. */
13240
13241 static std::pair<int, int>
13242 extract_bp_or_bp_range (extract_bp_kind kind,
13243 const std::string &arg,
13244 std::string::size_type arg_offset)
13245 {
13246 std::pair<int, int> range;
13247 const char *bp_loc = &arg[arg_offset];
13248 std::string::size_type dash = arg.find ('-', arg_offset);
13249 if (dash != std::string::npos)
13250 {
13251 /* bp_loc is a range (x-z). */
13252 if (arg.length () == dash + 1)
13253 error (kind == extract_bp_kind::bp
13254 ? _("Bad breakpoint number at or near: '%s'")
13255 : _("Bad breakpoint location number at or near: '%s'"),
13256 bp_loc);
13257
13258 const char *end;
13259 const char *start_first = bp_loc;
13260 const char *start_second = &arg[dash + 1];
13261 range.first = extract_bp_num (kind, start_first, '-');
13262 range.second = extract_bp_num (kind, start_second, '\0', &end);
13263
13264 if (range.first > range.second)
13265 error (kind == extract_bp_kind::bp
13266 ? _("Inverted breakpoint range at '%.*s'")
13267 : _("Inverted breakpoint location range at '%.*s'"),
13268 int (end - start_first), start_first);
13269 }
13270 else
13271 {
13272 /* bp_loc is a single value. */
13273 range.first = extract_bp_num (kind, bp_loc, '\0');
13274 range.second = range.first;
13275 }
13276 return range;
13277 }
13278
13279 /* Extract the breakpoint/location range specified by ARG. Returns
13280 the breakpoint range in BP_NUM_RANGE, and the location range in
13281 BP_LOC_RANGE.
13282
13283 ARG may be in any of the following forms:
13284
13285 x where 'x' is a breakpoint number.
13286 x-y where 'x' and 'y' specify a breakpoint numbers range.
13287 x.y where 'x' is a breakpoint number and 'y' a location number.
13288 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13289 location number range.
13290 */
13291
13292 static void
13293 extract_bp_number_and_location (const std::string &arg,
13294 std::pair<int, int> &bp_num_range,
13295 std::pair<int, int> &bp_loc_range)
13296 {
13297 std::string::size_type dot = arg.find ('.');
13298
13299 if (dot != std::string::npos)
13300 {
13301 /* Handle 'x.y' and 'x.y-z' cases. */
13302
13303 if (arg.length () == dot + 1 || dot == 0)
13304 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
13305
13306 bp_num_range.first
13307 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13308 bp_num_range.second = bp_num_range.first;
13309
13310 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13311 arg, dot + 1);
13312 }
13313 else
13314 {
13315 /* Handle x and x-y cases. */
13316
13317 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
13318 bp_loc_range.first = 0;
13319 bp_loc_range.second = 0;
13320 }
13321 }
13322
13323 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13324 specifies whether to enable or disable. */
13325
13326 static void
13327 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13328 {
13329 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13330 if (loc != NULL)
13331 {
13332 if (loc->disabled_by_cond && enable)
13333 error (_("Breakpoint %d's condition is invalid at location %d, "
13334 "cannot enable."), bp_num, loc_num);
13335
13336 if (loc->enabled != enable)
13337 {
13338 loc->enabled = enable;
13339 mark_breakpoint_location_modified (loc);
13340 }
13341 if (target_supports_enable_disable_tracepoint ()
13342 && current_trace_status ()->running && loc->owner
13343 && is_tracepoint (loc->owner))
13344 target_disable_tracepoint (loc);
13345 }
13346 update_global_location_list (UGLL_DONT_INSERT);
13347
13348 gdb::observers::breakpoint_modified.notify (loc->owner);
13349 }
13350
13351 /* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
13352 owner. 1-based indexing. -1 signals NOT FOUND. */
13353
13354 static int
13355 find_loc_num_by_location (const bp_location *loc)
13356 {
13357 if (loc != nullptr && loc->owner != nullptr)
13358 {
13359 /* Locations use 1-based indexing. */
13360 int loc_num = 1;
13361 for (bp_location *it : loc->owner->locations ())
13362 {
13363 if (it == loc)
13364 return loc_num;
13365 loc_num++;
13366 }
13367 }
13368 return -1;
13369 }
13370
13371 /* Enable or disable a breakpoint location LOC. ENABLE
13372 specifies whether to enable or disable. */
13373
13374 void
13375 enable_disable_bp_location (bp_location *loc, bool enable)
13376 {
13377 if (loc == nullptr)
13378 error (_("Breakpoint location is invalid."));
13379
13380 if (loc->owner == nullptr)
13381 error (_("Breakpoint location does not have an owner breakpoint."));
13382
13383 if (loc->disabled_by_cond && enable)
13384 {
13385 int loc_num = find_loc_num_by_location (loc);
13386 if (loc_num == -1)
13387 error (_("Breakpoint location LOC_NUM could not be found."));
13388 else
13389 error (_("Breakpoint %d's condition is invalid at location %d, "
13390 "cannot enable."), loc->owner->number, loc_num);
13391 }
13392
13393 if (loc->enabled != enable)
13394 {
13395 loc->enabled = enable;
13396 mark_breakpoint_location_modified (loc);
13397 }
13398
13399 if (target_supports_enable_disable_tracepoint ()
13400 && current_trace_status ()->running && loc->owner
13401 && is_tracepoint (loc->owner))
13402 target_disable_tracepoint (loc);
13403
13404 update_global_location_list (UGLL_DONT_INSERT);
13405 gdb::observers::breakpoint_modified.notify (loc->owner);
13406 }
13407
13408 /* Enable or disable a range of breakpoint locations. BP_NUM is the
13409 number of the breakpoint, and BP_LOC_RANGE specifies the
13410 (inclusive) range of location numbers of that breakpoint to
13411 enable/disable. ENABLE specifies whether to enable or disable the
13412 location. */
13413
13414 static void
13415 enable_disable_breakpoint_location_range (int bp_num,
13416 std::pair<int, int> &bp_loc_range,
13417 bool enable)
13418 {
13419 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13420 enable_disable_bp_num_loc (bp_num, i, enable);
13421 }
13422
13423 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13424 If from_tty is nonzero, it prints a message to that effect,
13425 which ends with a period (no newline). */
13426
13427 void
13428 disable_breakpoint (struct breakpoint *bpt)
13429 {
13430 /* Never disable a watchpoint scope breakpoint; we want to
13431 hit them when we leave scope so we can delete both the
13432 watchpoint and its scope breakpoint at that time. */
13433 if (bpt->type == bp_watchpoint_scope)
13434 return;
13435
13436 bpt->enable_state = bp_disabled;
13437
13438 /* Mark breakpoint locations modified. */
13439 mark_breakpoint_modified (bpt);
13440
13441 if (target_supports_enable_disable_tracepoint ()
13442 && current_trace_status ()->running && is_tracepoint (bpt))
13443 {
13444 for (bp_location *location : bpt->locations ())
13445 target_disable_tracepoint (location);
13446 }
13447
13448 update_global_location_list (UGLL_DONT_INSERT);
13449
13450 gdb::observers::breakpoint_modified.notify (bpt);
13451 }
13452
13453 /* Enable or disable the breakpoint(s) or breakpoint location(s)
13454 specified in ARGS. ARGS may be in any of the formats handled by
13455 extract_bp_number_and_location. ENABLE specifies whether to enable
13456 or disable the breakpoints/locations. */
13457
13458 static void
13459 enable_disable_command (const char *args, int from_tty, bool enable)
13460 {
13461 if (args == 0)
13462 {
13463 for (breakpoint *bpt : all_breakpoints ())
13464 if (user_breakpoint_p (bpt))
13465 {
13466 if (enable)
13467 enable_breakpoint (bpt);
13468 else
13469 disable_breakpoint (bpt);
13470 }
13471 }
13472 else
13473 {
13474 std::string num = extract_arg (&args);
13475
13476 while (!num.empty ())
13477 {
13478 std::pair<int, int> bp_num_range, bp_loc_range;
13479
13480 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13481
13482 if (bp_loc_range.first == bp_loc_range.second
13483 && (bp_loc_range.first == 0
13484 || (bp_loc_range.first == 1
13485 && bp_num_range.first == bp_num_range.second
13486 && !has_multiple_locations (bp_num_range.first))))
13487 {
13488 /* Handle breakpoint ids with formats 'x' or 'x-z'
13489 or 'y.1' where y has only one code location. */
13490 map_breakpoint_number_range (bp_num_range,
13491 enable
13492 ? enable_breakpoint
13493 : disable_breakpoint);
13494 }
13495 else
13496 {
13497 /* Handle breakpoint ids with formats 'x.y' or
13498 'x.y-z'. */
13499 enable_disable_breakpoint_location_range
13500 (bp_num_range.first, bp_loc_range, enable);
13501 }
13502 num = extract_arg (&args);
13503 }
13504 }
13505 }
13506
13507 /* The disable command disables the specified breakpoints/locations
13508 (or all defined breakpoints) so they're no longer effective in
13509 stopping the inferior. ARGS may be in any of the forms defined in
13510 extract_bp_number_and_location. */
13511
13512 static void
13513 disable_command (const char *args, int from_tty)
13514 {
13515 enable_disable_command (args, from_tty, false);
13516 }
13517
13518 static void
13519 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13520 int count)
13521 {
13522 int target_resources_ok;
13523
13524 if (bpt->type == bp_hardware_breakpoint)
13525 {
13526 int i;
13527 i = hw_breakpoint_used_count ();
13528 target_resources_ok =
13529 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13530 i + 1, 0);
13531 if (target_resources_ok == 0)
13532 error (_("No hardware breakpoint support in the target."));
13533 else if (target_resources_ok < 0)
13534 error (_("Hardware breakpoints used exceeds limit."));
13535 }
13536
13537 if (is_watchpoint (bpt))
13538 {
13539 /* Initialize it just to avoid a GCC false warning. */
13540 enum enable_state orig_enable_state = bp_disabled;
13541
13542 try
13543 {
13544 struct watchpoint *w = (struct watchpoint *) bpt;
13545
13546 orig_enable_state = bpt->enable_state;
13547 bpt->enable_state = bp_enabled;
13548 update_watchpoint (w, true /* reparse */);
13549 }
13550 catch (const gdb_exception_error &e)
13551 {
13552 bpt->enable_state = orig_enable_state;
13553 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13554 bpt->number);
13555 return;
13556 }
13557 }
13558
13559 bpt->enable_state = bp_enabled;
13560
13561 /* Mark breakpoint locations modified. */
13562 mark_breakpoint_modified (bpt);
13563
13564 if (target_supports_enable_disable_tracepoint ()
13565 && current_trace_status ()->running && is_tracepoint (bpt))
13566 {
13567 for (bp_location *location : bpt->locations ())
13568 target_enable_tracepoint (location);
13569 }
13570
13571 bpt->disposition = disposition;
13572 bpt->enable_count = count;
13573 update_global_location_list (UGLL_MAY_INSERT);
13574
13575 gdb::observers::breakpoint_modified.notify (bpt);
13576 }
13577
13578
13579 void
13580 enable_breakpoint (struct breakpoint *bpt)
13581 {
13582 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13583 }
13584
13585 /* The enable command enables the specified breakpoints/locations (or
13586 all defined breakpoints) so they once again become (or continue to
13587 be) effective in stopping the inferior. ARGS may be in any of the
13588 forms defined in extract_bp_number_and_location. */
13589
13590 static void
13591 enable_command (const char *args, int from_tty)
13592 {
13593 enable_disable_command (args, from_tty, true);
13594 }
13595
13596 static void
13597 enable_once_command (const char *args, int from_tty)
13598 {
13599 map_breakpoint_numbers
13600 (args, [&] (breakpoint *b)
13601 {
13602 iterate_over_related_breakpoints
13603 (b, [&] (breakpoint *bpt)
13604 {
13605 enable_breakpoint_disp (bpt, disp_disable, 1);
13606 });
13607 });
13608 }
13609
13610 static void
13611 enable_count_command (const char *args, int from_tty)
13612 {
13613 int count;
13614
13615 if (args == NULL)
13616 error_no_arg (_("hit count"));
13617
13618 count = get_number (&args);
13619
13620 map_breakpoint_numbers
13621 (args, [&] (breakpoint *b)
13622 {
13623 iterate_over_related_breakpoints
13624 (b, [&] (breakpoint *bpt)
13625 {
13626 enable_breakpoint_disp (bpt, disp_disable, count);
13627 });
13628 });
13629 }
13630
13631 static void
13632 enable_delete_command (const char *args, int from_tty)
13633 {
13634 map_breakpoint_numbers
13635 (args, [&] (breakpoint *b)
13636 {
13637 iterate_over_related_breakpoints
13638 (b, [&] (breakpoint *bpt)
13639 {
13640 enable_breakpoint_disp (bpt, disp_del, 1);
13641 });
13642 });
13643 }
13644 \f
13645 /* Invalidate last known value of any hardware watchpoint if
13646 the memory which that value represents has been written to by
13647 GDB itself. */
13648
13649 static void
13650 invalidate_bp_value_on_memory_change (struct inferior *inferior,
13651 CORE_ADDR addr, ssize_t len,
13652 const bfd_byte *data)
13653 {
13654 for (breakpoint *bp : all_breakpoints ())
13655 if (bp->enable_state == bp_enabled
13656 && bp->type == bp_hardware_watchpoint)
13657 {
13658 struct watchpoint *wp = (struct watchpoint *) bp;
13659
13660 if (wp->val_valid && wp->val != nullptr)
13661 {
13662 for (bp_location *loc : bp->locations ())
13663 if (loc->loc_type == bp_loc_hardware_watchpoint
13664 && loc->address + loc->length > addr
13665 && addr + len > loc->address)
13666 {
13667 wp->val = NULL;
13668 wp->val_valid = false;
13669 }
13670 }
13671 }
13672 }
13673
13674 /* Create and insert a breakpoint for software single step. */
13675
13676 void
13677 insert_single_step_breakpoint (struct gdbarch *gdbarch,
13678 const address_space *aspace,
13679 CORE_ADDR next_pc)
13680 {
13681 struct thread_info *tp = inferior_thread ();
13682 struct symtab_and_line sal;
13683 CORE_ADDR pc = next_pc;
13684
13685 if (tp->control.single_step_breakpoints == NULL)
13686 {
13687 std::unique_ptr<breakpoint> b
13688 (new momentary_breakpoint (gdbarch, bp_single_step,
13689 current_program_space,
13690 null_frame_id,
13691 tp->global_num));
13692
13693 tp->control.single_step_breakpoints
13694 = add_to_breakpoint_chain (std::move (b));
13695 }
13696
13697 sal = find_pc_line (pc, 0);
13698 sal.pc = pc;
13699 sal.section = find_pc_overlay (pc);
13700 sal.explicit_pc = 1;
13701
13702 auto *ss_bp
13703 = (gdb::checked_static_cast<momentary_breakpoint *>
13704 (tp->control.single_step_breakpoints));
13705 ss_bp->add_location (sal);
13706
13707 update_global_location_list (UGLL_INSERT);
13708 }
13709
13710 /* Insert single step breakpoints according to the current state. */
13711
13712 int
13713 insert_single_step_breakpoints (struct gdbarch *gdbarch)
13714 {
13715 struct regcache *regcache = get_current_regcache ();
13716 std::vector<CORE_ADDR> next_pcs;
13717
13718 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
13719
13720 if (!next_pcs.empty ())
13721 {
13722 frame_info_ptr frame = get_current_frame ();
13723 const address_space *aspace = get_frame_address_space (frame);
13724
13725 for (CORE_ADDR pc : next_pcs)
13726 insert_single_step_breakpoint (gdbarch, aspace, pc);
13727
13728 return 1;
13729 }
13730 else
13731 return 0;
13732 }
13733
13734 /* See breakpoint.h. */
13735
13736 int
13737 breakpoint_has_location_inserted_here (struct breakpoint *bp,
13738 const address_space *aspace,
13739 CORE_ADDR pc)
13740 {
13741 for (bp_location *loc : bp->locations ())
13742 if (loc->inserted
13743 && breakpoint_location_address_match (loc, aspace, pc))
13744 return 1;
13745
13746 return 0;
13747 }
13748
13749 /* Check whether a software single-step breakpoint is inserted at
13750 PC. */
13751
13752 int
13753 single_step_breakpoint_inserted_here_p (const address_space *aspace,
13754 CORE_ADDR pc)
13755 {
13756 for (breakpoint *bpt : all_breakpoints ())
13757 {
13758 if (bpt->type == bp_single_step
13759 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13760 return 1;
13761 }
13762 return 0;
13763 }
13764
13765 /* Tracepoint-specific operations. */
13766
13767 /* Set tracepoint count to NUM. */
13768 static void
13769 set_tracepoint_count (int num)
13770 {
13771 tracepoint_count = num;
13772 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
13773 }
13774
13775 static void
13776 trace_command (const char *arg, int from_tty)
13777 {
13778 location_spec_up locspec = string_to_location_spec (&arg,
13779 current_language);
13780 const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
13781 (locspec.get (), true /* is_tracepoint */);
13782
13783 create_breakpoint (get_current_arch (),
13784 locspec.get (),
13785 NULL, 0, arg, false, 1 /* parse arg */,
13786 0 /* tempflag */,
13787 bp_tracepoint /* type_wanted */,
13788 0 /* Ignore count */,
13789 pending_break_support,
13790 ops,
13791 from_tty,
13792 1 /* enabled */,
13793 0 /* internal */, 0);
13794 }
13795
13796 static void
13797 ftrace_command (const char *arg, int from_tty)
13798 {
13799 location_spec_up locspec = string_to_location_spec (&arg,
13800 current_language);
13801 create_breakpoint (get_current_arch (),
13802 locspec.get (),
13803 NULL, 0, arg, false, 1 /* parse arg */,
13804 0 /* tempflag */,
13805 bp_fast_tracepoint /* type_wanted */,
13806 0 /* Ignore count */,
13807 pending_break_support,
13808 &code_breakpoint_ops,
13809 from_tty,
13810 1 /* enabled */,
13811 0 /* internal */, 0);
13812 }
13813
13814 /* strace command implementation. Creates a static tracepoint. */
13815
13816 static void
13817 strace_command (const char *arg, int from_tty)
13818 {
13819 const struct breakpoint_ops *ops;
13820 location_spec_up locspec;
13821 enum bptype type;
13822
13823 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13824 or with a normal static tracepoint. */
13825 if (arg && startswith (arg, "-m") && isspace (arg[2]))
13826 {
13827 ops = &strace_marker_breakpoint_ops;
13828 locspec = new_linespec_location_spec (&arg,
13829 symbol_name_match_type::FULL);
13830 type = bp_static_marker_tracepoint;
13831 }
13832 else
13833 {
13834 ops = &code_breakpoint_ops;
13835 locspec = string_to_location_spec (&arg, current_language);
13836 type = bp_static_tracepoint;
13837 }
13838
13839 create_breakpoint (get_current_arch (),
13840 locspec.get (),
13841 NULL, 0, arg, false, 1 /* parse arg */,
13842 0 /* tempflag */,
13843 type /* type_wanted */,
13844 0 /* Ignore count */,
13845 pending_break_support,
13846 ops,
13847 from_tty,
13848 1 /* enabled */,
13849 0 /* internal */, 0);
13850 }
13851
13852 /* Set up a fake reader function that gets command lines from a linked
13853 list that was acquired during tracepoint uploading. */
13854
13855 static struct uploaded_tp *this_utp;
13856 static int next_cmd;
13857
13858 static const char *
13859 read_uploaded_action (std::string &buffer)
13860 {
13861 char *rslt = nullptr;
13862
13863 if (next_cmd < this_utp->cmd_strings.size ())
13864 {
13865 rslt = this_utp->cmd_strings[next_cmd].get ();
13866 next_cmd++;
13867 }
13868
13869 return rslt;
13870 }
13871
13872 /* Given information about a tracepoint as recorded on a target (which
13873 can be either a live system or a trace file), attempt to create an
13874 equivalent GDB tracepoint. This is not a reliable process, since
13875 the target does not necessarily have all the information used when
13876 the tracepoint was originally defined. */
13877
13878 struct tracepoint *
13879 create_tracepoint_from_upload (struct uploaded_tp *utp)
13880 {
13881 const char *addr_str;
13882 char small_buf[100];
13883 struct tracepoint *tp;
13884
13885 if (utp->at_string)
13886 addr_str = utp->at_string.get ();
13887 else
13888 {
13889 /* In the absence of a source location, fall back to raw
13890 address. Since there is no way to confirm that the address
13891 means the same thing as when the trace was started, warn the
13892 user. */
13893 warning (_("Uploaded tracepoint %d has no "
13894 "source location, using raw address"),
13895 utp->number);
13896 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
13897 addr_str = small_buf;
13898 }
13899
13900 /* There's not much we can do with a sequence of bytecodes. */
13901 if (utp->cond && !utp->cond_string)
13902 warning (_("Uploaded tracepoint %d condition "
13903 "has no source form, ignoring it"),
13904 utp->number);
13905
13906 location_spec_up locspec = string_to_location_spec (&addr_str,
13907 current_language);
13908 if (!create_breakpoint (get_current_arch (),
13909 locspec.get (),
13910 utp->cond_string.get (), -1, addr_str,
13911 false /* force_condition */,
13912 0 /* parse cond/thread */,
13913 0 /* tempflag */,
13914 utp->type /* type_wanted */,
13915 0 /* Ignore count */,
13916 pending_break_support,
13917 &code_breakpoint_ops,
13918 0 /* from_tty */,
13919 utp->enabled /* enabled */,
13920 0 /* internal */,
13921 CREATE_BREAKPOINT_FLAGS_INSERTED))
13922 return NULL;
13923
13924 /* Get the tracepoint we just created. */
13925 tp = get_tracepoint (tracepoint_count);
13926 gdb_assert (tp != NULL);
13927
13928 if (utp->pass > 0)
13929 {
13930 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
13931 tp->number);
13932
13933 trace_pass_command (small_buf, 0);
13934 }
13935
13936 /* If we have uploaded versions of the original commands, set up a
13937 special-purpose "reader" function and call the usual command line
13938 reader, then pass the result to the breakpoint command-setting
13939 function. */
13940 if (!utp->cmd_strings.empty ())
13941 {
13942 counted_command_line cmd_list;
13943
13944 this_utp = utp;
13945 next_cmd = 0;
13946
13947 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
13948
13949 breakpoint_set_commands (tp, std::move (cmd_list));
13950 }
13951 else if (!utp->actions.empty ()
13952 || !utp->step_actions.empty ())
13953 warning (_("Uploaded tracepoint %d actions "
13954 "have no source form, ignoring them"),
13955 utp->number);
13956
13957 /* Copy any status information that might be available. */
13958 tp->hit_count = utp->hit_count;
13959 tp->traceframe_usage = utp->traceframe_usage;
13960
13961 return tp;
13962 }
13963
13964 /* Print information on tracepoint number TPNUM_EXP, or all if
13965 omitted. */
13966
13967 static void
13968 info_tracepoints_command (const char *args, int from_tty)
13969 {
13970 struct ui_out *uiout = current_uiout;
13971 int num_printed;
13972
13973 num_printed = breakpoint_1 (args, false, is_tracepoint);
13974
13975 if (num_printed == 0)
13976 {
13977 if (args == NULL || *args == '\0')
13978 uiout->message ("No tracepoints.\n");
13979 else
13980 uiout->message ("No tracepoint matching '%s'.\n", args);
13981 }
13982
13983 default_collect_info ();
13984 }
13985
13986 /* The 'enable trace' command enables tracepoints.
13987 Not supported by all targets. */
13988 static void
13989 enable_trace_command (const char *args, int from_tty)
13990 {
13991 enable_command (args, from_tty);
13992 }
13993
13994 /* The 'disable trace' command disables tracepoints.
13995 Not supported by all targets. */
13996 static void
13997 disable_trace_command (const char *args, int from_tty)
13998 {
13999 disable_command (args, from_tty);
14000 }
14001
14002 /* Remove a tracepoint (or all if no argument). */
14003 static void
14004 delete_trace_command (const char *arg, int from_tty)
14005 {
14006 dont_repeat ();
14007
14008 if (arg == 0)
14009 {
14010 int breaks_to_delete = 0;
14011
14012 /* Delete all breakpoints if no argument.
14013 Do not delete internal or call-dummy breakpoints, these
14014 have to be deleted with an explicit breakpoint number
14015 argument. */
14016 for (breakpoint *tp : all_tracepoints ())
14017 if (is_tracepoint (tp) && user_breakpoint_p (tp))
14018 {
14019 breaks_to_delete = 1;
14020 break;
14021 }
14022
14023 /* Ask user only if there are some breakpoints to delete. */
14024 if (!from_tty
14025 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14026 {
14027 for (breakpoint *b : all_breakpoints_safe ())
14028 if (is_tracepoint (b) && user_breakpoint_p (b))
14029 delete_breakpoint (b);
14030 }
14031 }
14032 else
14033 map_breakpoint_numbers
14034 (arg, [&] (breakpoint *br)
14035 {
14036 iterate_over_related_breakpoints (br, delete_breakpoint);
14037 });
14038 }
14039
14040 /* Helper function for trace_pass_command. */
14041
14042 static void
14043 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14044 {
14045 tp->pass_count = count;
14046 gdb::observers::breakpoint_modified.notify (tp);
14047 if (from_tty)
14048 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
14049 tp->number, count);
14050 }
14051
14052 /* Set passcount for tracepoint.
14053
14054 First command argument is passcount, second is tracepoint number.
14055 If tracepoint number omitted, apply to most recently defined.
14056 Also accepts special argument "all". */
14057
14058 static void
14059 trace_pass_command (const char *args, int from_tty)
14060 {
14061 struct tracepoint *t1;
14062 ULONGEST count;
14063
14064 if (args == 0 || *args == 0)
14065 error (_("passcount command requires an "
14066 "argument (count + optional TP num)"));
14067
14068 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14069
14070 args = skip_spaces (args);
14071 if (*args && strncasecmp (args, "all", 3) == 0)
14072 {
14073 args += 3; /* Skip special argument "all". */
14074 if (*args)
14075 error (_("Junk at end of arguments."));
14076
14077 for (breakpoint *b : all_tracepoints ())
14078 {
14079 t1 = (struct tracepoint *) b;
14080 trace_pass_set_count (t1, count, from_tty);
14081 }
14082 }
14083 else if (*args == '\0')
14084 {
14085 t1 = get_tracepoint_by_number (&args, NULL);
14086 if (t1)
14087 trace_pass_set_count (t1, count, from_tty);
14088 }
14089 else
14090 {
14091 number_or_range_parser parser (args);
14092 while (!parser.finished ())
14093 {
14094 t1 = get_tracepoint_by_number (&args, &parser);
14095 if (t1)
14096 trace_pass_set_count (t1, count, from_tty);
14097 }
14098 }
14099 }
14100
14101 struct tracepoint *
14102 get_tracepoint (int num)
14103 {
14104 for (breakpoint *t : all_tracepoints ())
14105 if (t->number == num)
14106 return (struct tracepoint *) t;
14107
14108 return NULL;
14109 }
14110
14111 /* Find the tracepoint with the given target-side number (which may be
14112 different from the tracepoint number after disconnecting and
14113 reconnecting). */
14114
14115 struct tracepoint *
14116 get_tracepoint_by_number_on_target (int num)
14117 {
14118 for (breakpoint *b : all_tracepoints ())
14119 {
14120 struct tracepoint *t = (struct tracepoint *) b;
14121
14122 if (t->number_on_target == num)
14123 return t;
14124 }
14125
14126 return NULL;
14127 }
14128
14129 /* Utility: parse a tracepoint number and look it up in the list.
14130 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14131 If the argument is missing, the most recent tracepoint
14132 (tracepoint_count) is returned. */
14133
14134 struct tracepoint *
14135 get_tracepoint_by_number (const char **arg,
14136 number_or_range_parser *parser)
14137 {
14138 int tpnum;
14139 const char *instring = arg == NULL ? NULL : *arg;
14140
14141 if (parser != NULL)
14142 {
14143 gdb_assert (!parser->finished ());
14144 tpnum = parser->get_number ();
14145 }
14146 else if (arg == NULL || *arg == NULL || ! **arg)
14147 tpnum = tracepoint_count;
14148 else
14149 tpnum = get_number (arg);
14150
14151 if (tpnum <= 0)
14152 {
14153 if (instring && *instring)
14154 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14155 instring);
14156 else
14157 gdb_printf (_("No previous tracepoint\n"));
14158 return NULL;
14159 }
14160
14161 for (breakpoint *t : all_tracepoints ())
14162 if (t->number == tpnum)
14163 return (struct tracepoint *) t;
14164
14165 gdb_printf ("No tracepoint number %d.\n", tpnum);
14166 return NULL;
14167 }
14168
14169 void
14170 breakpoint::print_recreate_thread (struct ui_file *fp) const
14171 {
14172 if (thread != -1)
14173 {
14174 struct thread_info *thr = find_thread_global_id (thread);
14175 gdb_printf (fp, " thread %s", print_full_thread_id (thr));
14176 }
14177
14178 if (task != -1)
14179 gdb_printf (fp, " task %d", task);
14180
14181 gdb_printf (fp, "\n");
14182 }
14183
14184 /* Save information on user settable breakpoints (watchpoints, etc) to
14185 a new script file named FILENAME. If FILTER is non-NULL, call it
14186 on each breakpoint and only include the ones for which it returns
14187 true. */
14188
14189 static void
14190 save_breakpoints (const char *filename, int from_tty,
14191 bool (*filter) (const struct breakpoint *))
14192 {
14193 bool any = false;
14194 int extra_trace_bits = 0;
14195
14196 if (filename == 0 || *filename == 0)
14197 error (_("Argument required (file name in which to save)"));
14198
14199 /* See if we have anything to save. */
14200 for (breakpoint *tp : all_breakpoints ())
14201 {
14202 /* Skip internal and momentary breakpoints. */
14203 if (!user_breakpoint_p (tp))
14204 continue;
14205
14206 /* If we have a filter, only save the breakpoints it accepts. */
14207 if (filter && !filter (tp))
14208 continue;
14209
14210 any = true;
14211
14212 if (is_tracepoint (tp))
14213 {
14214 extra_trace_bits = 1;
14215
14216 /* We can stop searching. */
14217 break;
14218 }
14219 }
14220
14221 if (!any)
14222 {
14223 warning (_("Nothing to save."));
14224 return;
14225 }
14226
14227 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14228
14229 stdio_file fp;
14230
14231 if (!fp.open (expanded_filename.get (), "w"))
14232 error (_("Unable to open file '%s' for saving (%s)"),
14233 expanded_filename.get (), safe_strerror (errno));
14234
14235 if (extra_trace_bits)
14236 save_trace_state_variables (&fp);
14237
14238 for (breakpoint *tp : all_breakpoints ())
14239 {
14240 /* Skip internal and momentary breakpoints. */
14241 if (!user_breakpoint_p (tp))
14242 continue;
14243
14244 /* If we have a filter, only save the breakpoints it accepts. */
14245 if (filter && !filter (tp))
14246 continue;
14247
14248 tp->print_recreate (&fp);
14249
14250 /* Note, we can't rely on tp->number for anything, as we can't
14251 assume the recreated breakpoint numbers will match. Use $bpnum
14252 instead. */
14253
14254 if (tp->cond_string)
14255 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
14256
14257 if (tp->ignore_count)
14258 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
14259
14260 if (tp->type != bp_dprintf && tp->commands)
14261 {
14262 fp.puts (" commands\n");
14263
14264 ui_out_redirect_pop redir (current_uiout, &fp);
14265 print_command_lines (current_uiout, tp->commands.get (), 2);
14266
14267 fp.puts (" end\n");
14268 }
14269
14270 if (tp->enable_state == bp_disabled)
14271 fp.puts ("disable $bpnum\n");
14272
14273 /* If this is a multi-location breakpoint, check if the locations
14274 should be individually disabled. Watchpoint locations are
14275 special, and not user visible. */
14276 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14277 {
14278 int n = 1;
14279
14280 for (bp_location *loc : tp->locations ())
14281 {
14282 if (!loc->enabled)
14283 fp.printf ("disable $bpnum.%d\n", n);
14284
14285 n++;
14286 }
14287 }
14288 }
14289
14290 if (extra_trace_bits && !default_collect.empty ())
14291 fp.printf ("set default-collect %s\n", default_collect.c_str ());
14292
14293 if (from_tty)
14294 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
14295 }
14296
14297 /* The `save breakpoints' command. */
14298
14299 static void
14300 save_breakpoints_command (const char *args, int from_tty)
14301 {
14302 save_breakpoints (args, from_tty, NULL);
14303 }
14304
14305 /* The `save tracepoints' command. */
14306
14307 static void
14308 save_tracepoints_command (const char *args, int from_tty)
14309 {
14310 save_breakpoints (args, from_tty, is_tracepoint);
14311 }
14312
14313 \f
14314 /* This help string is used to consolidate all the help string for specifying
14315 locations used by several commands. */
14316
14317 #define LOCATION_SPEC_HELP_STRING \
14318 "Linespecs are colon-separated lists of location parameters, such as\n\
14319 source filename, function name, label name, and line number.\n\
14320 Example: To specify the start of a label named \"the_top\" in the\n\
14321 function \"fact\" in the file \"factorial.c\", use\n\
14322 \"factorial.c:fact:the_top\".\n\
14323 \n\
14324 Address locations begin with \"*\" and specify an exact address in the\n\
14325 program. Example: To specify the fourth byte past the start function\n\
14326 \"main\", use \"*main + 4\".\n\
14327 \n\
14328 Explicit locations are similar to linespecs but use an option/argument\n\
14329 syntax to specify location parameters.\n\
14330 Example: To specify the start of the label named \"the_top\" in the\n\
14331 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
14332 -function fact -label the_top\".\n\
14333 \n\
14334 By default, a specified function is matched against the program's\n\
14335 functions in all scopes. For C++, this means in all namespaces and\n\
14336 classes. For Ada, this means in all packages. E.g., in C++,\n\
14337 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14338 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
14339 specified name as a complete fully-qualified name instead."
14340
14341 /* This help string is used for the break, hbreak, tbreak and thbreak
14342 commands. It is defined as a macro to prevent duplication.
14343 COMMAND should be a string constant containing the name of the
14344 command. */
14345
14346 #define BREAK_ARGS_HELP(command) \
14347 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14348 \t[-force-condition] [if CONDITION]\n\
14349 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14350 probe point. Accepted values are `-probe' (for a generic, automatically\n\
14351 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14352 `-probe-dtrace' (for a DTrace probe).\n\
14353 LOCATION may be a linespec, address, or explicit location as described\n\
14354 below.\n\
14355 \n\
14356 With no LOCATION, uses current execution address of the selected\n\
14357 stack frame. This is useful for breaking on return to a stack frame.\n\
14358 \n\
14359 THREADNUM is the number from \"info threads\".\n\
14360 CONDITION is a boolean expression.\n\
14361 \n\
14362 With the \"-force-condition\" flag, the condition is defined even when\n\
14363 it is invalid for all current locations.\n\
14364 \n" LOCATION_SPEC_HELP_STRING "\n\n\
14365 Multiple breakpoints at one place are permitted, and useful if their\n\
14366 conditions are different.\n\
14367 \n\
14368 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14369
14370 /* List of subcommands for "catch". */
14371 static struct cmd_list_element *catch_cmdlist;
14372
14373 /* List of subcommands for "tcatch". */
14374 static struct cmd_list_element *tcatch_cmdlist;
14375
14376 void
14377 add_catch_command (const char *name, const char *docstring,
14378 cmd_func_ftype *func,
14379 completer_ftype *completer,
14380 void *user_data_catch,
14381 void *user_data_tcatch)
14382 {
14383 struct cmd_list_element *command;
14384
14385 command = add_cmd (name, class_breakpoint, docstring,
14386 &catch_cmdlist);
14387 command->func = func;
14388 command->set_context (user_data_catch);
14389 set_cmd_completer (command, completer);
14390
14391 command = add_cmd (name, class_breakpoint, docstring,
14392 &tcatch_cmdlist);
14393 command->func = func;
14394 command->set_context (user_data_tcatch);
14395 set_cmd_completer (command, completer);
14396 }
14397
14398 /* False if any of the breakpoint's locations could be a location where
14399 functions have been inlined, true otherwise. */
14400
14401 static bool
14402 is_non_inline_function (struct breakpoint *b)
14403 {
14404 /* The shared library event breakpoint is set on the address of a
14405 non-inline function. */
14406 return (b->type == bp_shlib_event);
14407 }
14408
14409 /* Nonzero if the specified PC cannot be a location where functions
14410 have been inlined. */
14411
14412 int
14413 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
14414 const target_waitstatus &ws)
14415 {
14416 for (breakpoint *b : all_breakpoints ())
14417 {
14418 if (!is_non_inline_function (b))
14419 continue;
14420
14421 for (bp_location *bl : b->locations ())
14422 {
14423 if (!bl->shlib_disabled
14424 && bpstat_check_location (bl, aspace, pc, ws))
14425 return 1;
14426 }
14427 }
14428
14429 return 0;
14430 }
14431
14432 /* Remove any references to OBJFILE which is going to be freed. */
14433
14434 void
14435 breakpoint_free_objfile (struct objfile *objfile)
14436 {
14437 for (bp_location *loc : all_bp_locations ())
14438 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
14439 loc->symtab = NULL;
14440 }
14441
14442 /* Chain containing all defined "enable breakpoint" subcommands. */
14443
14444 static struct cmd_list_element *enablebreaklist = NULL;
14445
14446 /* See breakpoint.h. */
14447
14448 cmd_list_element *commands_cmd_element = nullptr;
14449
14450 void _initialize_breakpoint ();
14451 void
14452 _initialize_breakpoint ()
14453 {
14454 struct cmd_list_element *c;
14455
14456 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14457 "breakpoint");
14458 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14459 "breakpoint");
14460 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14461 "breakpoint");
14462
14463 breakpoint_chain = 0;
14464 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14465 before a breakpoint is set. */
14466 breakpoint_count = 0;
14467
14468 tracepoint_count = 0;
14469
14470 add_com ("ignore", class_breakpoint, ignore_command, _("\
14471 Set ignore-count of breakpoint number N to COUNT.\n\
14472 Usage is `ignore N COUNT'."));
14473
14474 commands_cmd_element = add_com ("commands", class_breakpoint,
14475 commands_command, _("\
14476 Set commands to be executed when the given breakpoints are hit.\n\
14477 Give a space-separated breakpoint list as argument after \"commands\".\n\
14478 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14479 (e.g. `5-7').\n\
14480 With no argument, the targeted breakpoint is the last one set.\n\
14481 The commands themselves follow starting on the next line.\n\
14482 Type a line containing \"end\" to indicate the end of them.\n\
14483 Give \"silent\" as the first line to make the breakpoint silent;\n\
14484 then no output is printed when it is hit, except what the commands print."));
14485
14486 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14487 static std::string condition_command_help
14488 = gdb::option::build_help (_("\
14489 Specify breakpoint number N to break only if COND is true.\n\
14490 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
14491 is an expression to be evaluated whenever breakpoint N is reached.\n\
14492 \n\
14493 Options:\n\
14494 %OPTIONS%"), cc_opts);
14495
14496 c = add_com ("condition", class_breakpoint, condition_command,
14497 condition_command_help.c_str ());
14498 set_cmd_completer_handle_brkchars (c, condition_completer);
14499
14500 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
14501 Set a temporary breakpoint.\n\
14502 Like \"break\" except the breakpoint is only temporary,\n\
14503 so it will be deleted when hit. Equivalent to \"break\" followed\n\
14504 by using \"enable delete\" on the breakpoint number.\n\
14505 \n"
14506 BREAK_ARGS_HELP ("tbreak")));
14507 set_cmd_completer (c, location_completer);
14508
14509 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
14510 Set a hardware assisted breakpoint.\n\
14511 Like \"break\" except the breakpoint requires hardware support,\n\
14512 some target hardware may not have this support.\n\
14513 \n"
14514 BREAK_ARGS_HELP ("hbreak")));
14515 set_cmd_completer (c, location_completer);
14516
14517 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
14518 Set a temporary hardware assisted breakpoint.\n\
14519 Like \"hbreak\" except the breakpoint is only temporary,\n\
14520 so it will be deleted when hit.\n\
14521 \n"
14522 BREAK_ARGS_HELP ("thbreak")));
14523 set_cmd_completer (c, location_completer);
14524
14525 cmd_list_element *enable_cmd
14526 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14527 Enable all or some breakpoints.\n\
14528 Usage: enable [BREAKPOINTNUM]...\n\
14529 Give breakpoint numbers (separated by spaces) as arguments.\n\
14530 With no subcommand, breakpoints are enabled until you command otherwise.\n\
14531 This is used to cancel the effect of the \"disable\" command.\n\
14532 With a subcommand you can enable temporarily."),
14533 &enablelist, 1, &cmdlist);
14534
14535 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
14536
14537 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
14538 Enable all or some breakpoints.\n\
14539 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
14540 Give breakpoint numbers (separated by spaces) as arguments.\n\
14541 This is used to cancel the effect of the \"disable\" command.\n\
14542 May be abbreviated to simply \"enable\"."),
14543 &enablebreaklist, 1, &enablelist);
14544
14545 add_cmd ("once", no_class, enable_once_command, _("\
14546 Enable some breakpoints for one hit.\n\
14547 Usage: enable breakpoints once BREAKPOINTNUM...\n\
14548 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14549 &enablebreaklist);
14550
14551 add_cmd ("delete", no_class, enable_delete_command, _("\
14552 Enable some breakpoints and delete when hit.\n\
14553 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
14554 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14555 &enablebreaklist);
14556
14557 add_cmd ("count", no_class, enable_count_command, _("\
14558 Enable some breakpoints for COUNT hits.\n\
14559 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
14560 If a breakpoint is hit while enabled in this fashion,\n\
14561 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14562 &enablebreaklist);
14563
14564 add_cmd ("delete", no_class, enable_delete_command, _("\
14565 Enable some breakpoints and delete when hit.\n\
14566 Usage: enable delete BREAKPOINTNUM...\n\
14567 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14568 &enablelist);
14569
14570 add_cmd ("once", no_class, enable_once_command, _("\
14571 Enable some breakpoints for one hit.\n\
14572 Usage: enable once BREAKPOINTNUM...\n\
14573 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14574 &enablelist);
14575
14576 add_cmd ("count", no_class, enable_count_command, _("\
14577 Enable some breakpoints for COUNT hits.\n\
14578 Usage: enable count COUNT BREAKPOINTNUM...\n\
14579 If a breakpoint is hit while enabled in this fashion,\n\
14580 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14581 &enablelist);
14582
14583 cmd_list_element *disable_cmd
14584 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14585 Disable all or some breakpoints.\n\
14586 Usage: disable [BREAKPOINTNUM]...\n\
14587 Arguments are breakpoint numbers with spaces in between.\n\
14588 To disable all breakpoints, give no argument.\n\
14589 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
14590 &disablelist, 1, &cmdlist);
14591 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14592 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
14593
14594 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
14595 Disable all or some breakpoints.\n\
14596 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
14597 Arguments are breakpoint numbers with spaces in between.\n\
14598 To disable all breakpoints, give no argument.\n\
14599 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
14600 This command may be abbreviated \"disable\"."),
14601 &disablelist);
14602
14603 cmd_list_element *delete_cmd
14604 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14605 Delete all or some breakpoints.\n\
14606 Usage: delete [BREAKPOINTNUM]...\n\
14607 Arguments are breakpoint numbers with spaces in between.\n\
14608 To delete all breakpoints, give no argument.\n\
14609 \n\
14610 Also a prefix command for deletion of other GDB objects."),
14611 &deletelist, 1, &cmdlist);
14612 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14613 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
14614
14615 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
14616 Delete all or some breakpoints or auto-display expressions.\n\
14617 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
14618 Arguments are breakpoint numbers with spaces in between.\n\
14619 To delete all breakpoints, give no argument.\n\
14620 This command may be abbreviated \"delete\"."),
14621 &deletelist);
14622
14623 cmd_list_element *clear_cmd
14624 = add_com ("clear", class_breakpoint, clear_command, _("\
14625 Clear breakpoint at specified location.\n\
14626 Argument may be a linespec, explicit, or address location as described below.\n\
14627 \n\
14628 With no argument, clears all breakpoints in the line that the selected frame\n\
14629 is executing in.\n"
14630 "\n" LOCATION_SPEC_HELP_STRING "\n\n\
14631 See also the \"delete\" command which clears breakpoints by number."));
14632 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
14633
14634 cmd_list_element *break_cmd
14635 = add_com ("break", class_breakpoint, break_command, _("\
14636 Set breakpoint at specified location.\n"
14637 BREAK_ARGS_HELP ("break")));
14638 set_cmd_completer (break_cmd, location_completer);
14639
14640 add_com_alias ("b", break_cmd, class_run, 1);
14641 add_com_alias ("br", break_cmd, class_run, 1);
14642 add_com_alias ("bre", break_cmd, class_run, 1);
14643 add_com_alias ("brea", break_cmd, class_run, 1);
14644
14645 cmd_list_element *info_breakpoints_cmd
14646 = add_info ("breakpoints", info_breakpoints_command, _("\
14647 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
14648 The \"Type\" column indicates one of:\n\
14649 \tbreakpoint - normal breakpoint\n\
14650 \twatchpoint - watchpoint\n\
14651 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14652 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14653 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14654 address and file/line number respectively.\n\
14655 \n\
14656 Convenience variable \"$_\" and default examine address for \"x\"\n\
14657 are set to the address of the last breakpoint listed unless the command\n\
14658 is prefixed with \"server \".\n\n\
14659 Convenience variable \"$bpnum\" contains the number of the last\n\
14660 breakpoint set."));
14661
14662 add_info_alias ("b", info_breakpoints_cmd, 1);
14663
14664 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14665 Status of all breakpoints, or breakpoint number NUMBER.\n\
14666 The \"Type\" column indicates one of:\n\
14667 \tbreakpoint - normal breakpoint\n\
14668 \twatchpoint - watchpoint\n\
14669 \tlongjmp - internal breakpoint used to step through longjmp()\n\
14670 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14671 \tuntil - internal breakpoint used by the \"until\" command\n\
14672 \tfinish - internal breakpoint used by the \"finish\" command\n\
14673 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14674 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14675 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14676 address and file/line number respectively.\n\
14677 \n\
14678 Convenience variable \"$_\" and default examine address for \"x\"\n\
14679 are set to the address of the last breakpoint listed unless the command\n\
14680 is prefixed with \"server \".\n\n\
14681 Convenience variable \"$bpnum\" contains the number of the last\n\
14682 breakpoint set."),
14683 &maintenanceinfolist);
14684
14685 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
14686 Set catchpoints to catch events."),
14687 &catch_cmdlist,
14688 0/*allow-unknown*/, &cmdlist);
14689
14690 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
14691 Set temporary catchpoints to catch events."),
14692 &tcatch_cmdlist,
14693 0/*allow-unknown*/, &cmdlist);
14694
14695 const auto opts = make_watch_options_def_group (nullptr);
14696
14697 static const std::string watch_help = gdb::option::build_help (_("\
14698 Set a watchpoint for EXPRESSION.\n\
14699 Usage: watch [-location] EXPRESSION\n\
14700 \n\
14701 Options:\n\
14702 %OPTIONS%\n\
14703 \n\
14704 A watchpoint stops execution of your program whenever the value of\n\
14705 an expression changes."), opts);
14706 c = add_com ("watch", class_breakpoint, watch_command,
14707 watch_help.c_str ());
14708 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14709
14710 static const std::string rwatch_help = gdb::option::build_help (_("\
14711 Set a read watchpoint for EXPRESSION.\n\
14712 Usage: rwatch [-location] EXPRESSION\n\
14713 \n\
14714 Options:\n\
14715 %OPTIONS%\n\
14716 \n\
14717 A read watchpoint stops execution of your program whenever the value of\n\
14718 an expression is read."), opts);
14719 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14720 rwatch_help.c_str ());
14721 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14722
14723 static const std::string awatch_help = gdb::option::build_help (_("\
14724 Set an access watchpoint for EXPRESSION.\n\
14725 Usage: awatch [-location] EXPRESSION\n\
14726 \n\
14727 Options:\n\
14728 %OPTIONS%\n\
14729 \n\
14730 An access watchpoint stops execution of your program whenever the value\n\
14731 of an expression is either read or written."), opts);
14732 c = add_com ("awatch", class_breakpoint, awatch_command,
14733 awatch_help.c_str ());
14734 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14735
14736 add_info ("watchpoints", info_watchpoints_command, _("\
14737 Status of specified watchpoints (all watchpoints if no argument)."));
14738
14739 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14740 respond to changes - contrary to the description. */
14741 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14742 &can_use_hw_watchpoints, _("\
14743 Set debugger's willingness to use watchpoint hardware."), _("\
14744 Show debugger's willingness to use watchpoint hardware."), _("\
14745 If zero, gdb will not use hardware for new watchpoints, even if\n\
14746 such is available. (However, any hardware watchpoints that were\n\
14747 created before setting this to nonzero, will continue to use watchpoint\n\
14748 hardware.)"),
14749 NULL,
14750 show_can_use_hw_watchpoints,
14751 &setlist, &showlist);
14752
14753 can_use_hw_watchpoints = 1;
14754
14755 /* Tracepoint manipulation commands. */
14756
14757 cmd_list_element *trace_cmd
14758 = add_com ("trace", class_breakpoint, trace_command, _("\
14759 Set a tracepoint at specified location.\n\
14760 \n"
14761 BREAK_ARGS_HELP ("trace") "\n\
14762 Do \"help tracepoints\" for info on other tracepoint commands."));
14763 set_cmd_completer (trace_cmd, location_completer);
14764
14765 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14766 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14767 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14768 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
14769
14770 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
14771 Set a fast tracepoint at specified location.\n\
14772 \n"
14773 BREAK_ARGS_HELP ("ftrace") "\n\
14774 Do \"help tracepoints\" for info on other tracepoint commands."));
14775 set_cmd_completer (c, location_completer);
14776
14777 c = add_com ("strace", class_breakpoint, strace_command, _("\
14778 Set a static tracepoint at location or marker.\n\
14779 \n\
14780 strace [LOCATION] [if CONDITION]\n\
14781 LOCATION may be a linespec, explicit, or address location (described below) \n\
14782 or -m MARKER_ID.\n\n\
14783 If a marker id is specified, probe the marker with that name. With\n\
14784 no LOCATION, uses current execution address of the selected stack frame.\n\
14785 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14786 This collects arbitrary user data passed in the probe point call to the\n\
14787 tracing library. You can inspect it when analyzing the trace buffer,\n\
14788 by printing the $_sdata variable like any other convenience variable.\n\
14789 \n\
14790 CONDITION is a boolean expression.\n\
14791 \n" LOCATION_SPEC_HELP_STRING "\n\n\
14792 Multiple tracepoints at one place are permitted, and useful if their\n\
14793 conditions are different.\n\
14794 \n\
14795 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14796 Do \"help tracepoints\" for info on other tracepoint commands."));
14797 set_cmd_completer (c, location_completer);
14798
14799 cmd_list_element *info_tracepoints_cmd
14800 = add_info ("tracepoints", info_tracepoints_command, _("\
14801 Status of specified tracepoints (all tracepoints if no argument).\n\
14802 Convenience variable \"$tpnum\" contains the number of the\n\
14803 last tracepoint set."));
14804
14805 add_info_alias ("tp", info_tracepoints_cmd, 1);
14806
14807 cmd_list_element *delete_tracepoints_cmd
14808 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
14809 Delete specified tracepoints.\n\
14810 Arguments are tracepoint numbers, separated by spaces.\n\
14811 No argument means delete all tracepoints."),
14812 &deletelist);
14813 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
14814
14815 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14816 Disable specified tracepoints.\n\
14817 Arguments are tracepoint numbers, separated by spaces.\n\
14818 No argument means disable all tracepoints."),
14819 &disablelist);
14820 deprecate_cmd (c, "disable");
14821
14822 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14823 Enable specified tracepoints.\n\
14824 Arguments are tracepoint numbers, separated by spaces.\n\
14825 No argument means enable all tracepoints."),
14826 &enablelist);
14827 deprecate_cmd (c, "enable");
14828
14829 add_com ("passcount", class_trace, trace_pass_command, _("\
14830 Set the passcount for a tracepoint.\n\
14831 The trace will end when the tracepoint has been passed 'count' times.\n\
14832 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14833 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14834
14835 add_basic_prefix_cmd ("save", class_breakpoint,
14836 _("Save breakpoint definitions as a script."),
14837 &save_cmdlist,
14838 0/*allow-unknown*/, &cmdlist);
14839
14840 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14841 Save current breakpoint definitions as a script.\n\
14842 This includes all types of breakpoints (breakpoints, watchpoints,\n\
14843 catchpoints, tracepoints). Use the 'source' command in another debug\n\
14844 session to restore them."),
14845 &save_cmdlist);
14846 set_cmd_completer (c, filename_completer);
14847
14848 cmd_list_element *save_tracepoints_cmd
14849 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
14850 Save current tracepoint definitions as a script.\n\
14851 Use the 'source' command in another debug session to restore them."),
14852 &save_cmdlist);
14853 set_cmd_completer (save_tracepoints_cmd, filename_completer);
14854
14855 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
14856 deprecate_cmd (c, "save tracepoints");
14857
14858 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14859 _("\
14860 Breakpoint specific settings.\n\
14861 Configure various breakpoint-specific variables such as\n\
14862 pending breakpoint behavior."),
14863 _("\
14864 Breakpoint specific settings.\n\
14865 Configure various breakpoint-specific variables such as\n\
14866 pending breakpoint behavior."),
14867 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14868 &setlist, &showlist);
14869
14870 add_setshow_auto_boolean_cmd ("pending", no_class,
14871 &pending_break_support, _("\
14872 Set debugger's behavior regarding pending breakpoints."), _("\
14873 Show debugger's behavior regarding pending breakpoints."), _("\
14874 If on, an unrecognized breakpoint location will cause gdb to create a\n\
14875 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14876 an error. If auto, an unrecognized breakpoint location results in a\n\
14877 user-query to see if a pending breakpoint should be created."),
14878 NULL,
14879 show_pending_break_support,
14880 &breakpoint_set_cmdlist,
14881 &breakpoint_show_cmdlist);
14882
14883 pending_break_support = AUTO_BOOLEAN_AUTO;
14884
14885 add_setshow_boolean_cmd ("auto-hw", no_class,
14886 &automatic_hardware_breakpoints, _("\
14887 Set automatic usage of hardware breakpoints."), _("\
14888 Show automatic usage of hardware breakpoints."), _("\
14889 If set, the debugger will automatically use hardware breakpoints for\n\
14890 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14891 a warning will be emitted for such breakpoints."),
14892 NULL,
14893 show_automatic_hardware_breakpoints,
14894 &breakpoint_set_cmdlist,
14895 &breakpoint_show_cmdlist);
14896
14897 add_setshow_boolean_cmd ("always-inserted", class_support,
14898 &always_inserted_mode, _("\
14899 Set mode for inserting breakpoints."), _("\
14900 Show mode for inserting breakpoints."), _("\
14901 When this mode is on, breakpoints are inserted immediately as soon as\n\
14902 they're created, kept inserted even when execution stops, and removed\n\
14903 only when the user deletes them. When this mode is off (the default),\n\
14904 breakpoints are inserted only when execution continues, and removed\n\
14905 when execution stops."),
14906 NULL,
14907 &show_always_inserted_mode,
14908 &breakpoint_set_cmdlist,
14909 &breakpoint_show_cmdlist);
14910
14911 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14912 condition_evaluation_enums,
14913 &condition_evaluation_mode_1, _("\
14914 Set mode of breakpoint condition evaluation."), _("\
14915 Show mode of breakpoint condition evaluation."), _("\
14916 When this is set to \"host\", breakpoint conditions will be\n\
14917 evaluated on the host's side by GDB. When it is set to \"target\",\n\
14918 breakpoint conditions will be downloaded to the target (if the target\n\
14919 supports such feature) and conditions will be evaluated on the target's side.\n\
14920 If this is set to \"auto\" (default), this will be automatically set to\n\
14921 \"target\" if it supports condition evaluation, otherwise it will\n\
14922 be set to \"host\"."),
14923 &set_condition_evaluation_mode,
14924 &show_condition_evaluation_mode,
14925 &breakpoint_set_cmdlist,
14926 &breakpoint_show_cmdlist);
14927
14928 add_com ("break-range", class_breakpoint, break_range_command, _("\
14929 Set a breakpoint for an address range.\n\
14930 break-range START-LOCATION, END-LOCATION\n\
14931 where START-LOCATION and END-LOCATION can be one of the following:\n\
14932 LINENUM, for that line in the current file,\n\
14933 FILE:LINENUM, for that line in that file,\n\
14934 +OFFSET, for that number of lines after the current line\n\
14935 or the start of the range\n\
14936 FUNCTION, for the first line in that function,\n\
14937 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14938 *ADDRESS, for the instruction at that address.\n\
14939 \n\
14940 The breakpoint will stop execution of the inferior whenever it executes\n\
14941 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14942 range (including START-LOCATION and END-LOCATION)."));
14943
14944 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
14945 Set a dynamic printf at specified location.\n\
14946 dprintf location,format string,arg1,arg2,...\n\
14947 location may be a linespec, explicit, or address location.\n"
14948 "\n" LOCATION_SPEC_HELP_STRING));
14949 set_cmd_completer (c, location_completer);
14950
14951 add_setshow_enum_cmd ("dprintf-style", class_support,
14952 dprintf_style_enums, &dprintf_style, _("\
14953 Set the style of usage for dynamic printf."), _("\
14954 Show the style of usage for dynamic printf."), _("\
14955 This setting chooses how GDB will do a dynamic printf.\n\
14956 If the value is \"gdb\", then the printing is done by GDB to its own\n\
14957 console, as with the \"printf\" command.\n\
14958 If the value is \"call\", the print is done by calling a function in your\n\
14959 program; by default printf(), but you can choose a different function or\n\
14960 output stream by setting dprintf-function and dprintf-channel."),
14961 update_dprintf_commands, NULL,
14962 &setlist, &showlist);
14963
14964 add_setshow_string_cmd ("dprintf-function", class_support,
14965 &dprintf_function, _("\
14966 Set the function to use for dynamic printf."), _("\
14967 Show the function to use for dynamic printf."), NULL,
14968 update_dprintf_commands, NULL,
14969 &setlist, &showlist);
14970
14971 add_setshow_string_cmd ("dprintf-channel", class_support,
14972 &dprintf_channel, _("\
14973 Set the channel to use for dynamic printf."), _("\
14974 Show the channel to use for dynamic printf."), NULL,
14975 update_dprintf_commands, NULL,
14976 &setlist, &showlist);
14977
14978 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14979 &disconnected_dprintf, _("\
14980 Set whether dprintf continues after GDB disconnects."), _("\
14981 Show whether dprintf continues after GDB disconnects."), _("\
14982 Use this to let dprintf commands continue to hit and produce output\n\
14983 even if GDB disconnects or detaches from the target."),
14984 NULL,
14985 NULL,
14986 &setlist, &showlist);
14987
14988 add_com ("agent-printf", class_vars, agent_printf_command, _("\
14989 Target agent only formatted printing, like the C \"printf\" function.\n\
14990 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14991 This supports most C printf format specifications, like %s, %d, etc.\n\
14992 This is useful for formatted output in user-defined commands."));
14993
14994 automatic_hardware_breakpoints = true;
14995
14996 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14997 "breakpoint");
14998 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
14999 "breakpoint");
15000 }