Automatic date update in version.in
[binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdbsupport/gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "stack.h"
63 #include "ax-gdb.h"
64 #include "dummy-frame.h"
65 #include "interps.h"
66 #include "gdbsupport/format.h"
67 #include "thread-fsm.h"
68 #include "tid-parse.h"
69 #include "cli/cli-style.h"
70 #include "cli/cli-decode.h"
71
72 /* readline include files */
73 #include "readline/tilde.h"
74
75 /* readline defines this. */
76 #undef savestring
77
78 #include "mi/mi-common.h"
79 #include "extension.h"
80 #include <algorithm>
81 #include "progspace-and-thread.h"
82 #include "gdbsupport/array-view.h"
83 #include "gdbsupport/gdb_optional.h"
84
85 /* Prototypes for local functions. */
86
87 static void map_breakpoint_numbers (const char *,
88 gdb::function_view<void (breakpoint *)>);
89
90 static void
91 create_sals_from_location_spec_default (location_spec *locspec,
92 linespec_result *canonical);
93
94 static void create_breakpoints_sal (struct gdbarch *,
95 struct linespec_result *,
96 gdb::unique_xmalloc_ptr<char>,
97 gdb::unique_xmalloc_ptr<char>,
98 enum bptype,
99 enum bpdisp, int, int,
100 int,
101 int, int, int, unsigned);
102
103 static std::vector<symtab_and_line> decode_location_spec_default
104 (struct breakpoint *b, struct location_spec *locspec,
105 struct program_space *search_pspace);
106
107 static int can_use_hardware_watchpoint
108 (const std::vector<value_ref_ptr> &vals);
109
110 static void mention (const breakpoint *);
111
112 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
113
114 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
115
116 static struct breakpoint *
117 momentary_breakpoint_from_master (struct breakpoint *orig,
118 enum bptype type,
119 int loc_enabled, int thread);
120
121 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
122
123 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
124 CORE_ADDR bpaddr,
125 enum bptype bptype,
126 struct program_space *pspace);
127
128 static int watchpoint_locations_match (struct bp_location *loc1,
129 struct bp_location *loc2);
130
131 static int breakpoint_locations_match (struct bp_location *loc1,
132 struct bp_location *loc2,
133 bool sw_hw_bps_match = false);
134
135 static int breakpoint_location_address_match (struct bp_location *bl,
136 const struct address_space *aspace,
137 CORE_ADDR addr);
138
139 static int breakpoint_location_address_range_overlap (struct bp_location *,
140 const address_space *,
141 CORE_ADDR, int);
142
143 static int remove_breakpoint (struct bp_location *);
144 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
145
146 static enum print_stop_action print_bp_stop_message (bpstat *bs);
147
148 static int hw_breakpoint_used_count (void);
149
150 static int hw_watchpoint_use_count (struct breakpoint *);
151
152 static int hw_watchpoint_used_count_others (struct breakpoint *except,
153 enum bptype type,
154 int *other_type_used);
155
156 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
157 int count);
158
159 static void decref_bp_location (struct bp_location **loc);
160
161 static std::vector<symtab_and_line> bkpt_probe_decode_location_spec
162 (struct breakpoint *b,
163 location_spec *locspec,
164 struct program_space *search_pspace);
165
166 static bool bl_address_is_meaningful (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 1 if B refers to a static tracepoint set by marker ("-m"), zero
216 otherwise. */
217
218 static int 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 int
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 /* Walk the following statement or block through all breakpoints.
616 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
617 current breakpoint. */
618
619 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
620 for (B = breakpoint_chain; \
621 B ? (TMP=B->next, 1): 0; \
622 B = TMP)
623
624 /* Chains of all breakpoints defined. */
625
626 static struct breakpoint *breakpoint_chain;
627
628 /* See breakpoint.h. */
629
630 breakpoint_range
631 all_breakpoints ()
632 {
633 return breakpoint_range (breakpoint_chain);
634 }
635
636 /* See breakpoint.h. */
637
638 breakpoint_safe_range
639 all_breakpoints_safe ()
640 {
641 return breakpoint_safe_range (all_breakpoints ());
642 }
643
644 /* See breakpoint.h. */
645
646 tracepoint_range
647 all_tracepoints ()
648 {
649 return tracepoint_range (breakpoint_chain);
650 }
651
652 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
653
654 static std::vector<bp_location *> bp_locations;
655
656 /* See breakpoint.h. */
657
658 const std::vector<bp_location *> &
659 all_bp_locations ()
660 {
661 return bp_locations;
662 }
663
664 /* Range to iterate over breakpoint locations at a given address. */
665
666 struct bp_locations_at_addr_range
667 {
668 using iterator = std::vector<bp_location *>::iterator;
669
670 bp_locations_at_addr_range (CORE_ADDR addr)
671 {
672 struct compare
673 {
674 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
675 { return loc->address < addr_; }
676
677 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
678 { return addr_ < loc->address; }
679 };
680
681 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
682 addr, compare ());
683
684 m_begin = it_pair.first;
685 m_end = it_pair.second;
686 }
687
688 iterator begin () const
689 { return m_begin; }
690
691 iterator end () const
692 { return m_end; }
693
694 private:
695 iterator m_begin;
696 iterator m_end;
697 };
698
699 /* Return a range to iterate over all breakpoint locations exactly at address
700 ADDR.
701
702 If it's needed to iterate multiple times on the same range, it's possible
703 to save the range in a local variable and use it multiple times:
704
705 auto range = all_bp_locations_at_addr (addr);
706
707 for (bp_location *loc : range)
708 // use loc
709
710 for (bp_location *loc : range)
711 // use loc
712
713 This saves a bit of time, as it avoids re-doing the binary searches to find
714 the range's boundaries. Just remember not to change the bp_locations vector
715 in the mean time, as it could make the range's iterators stale. */
716
717 static bp_locations_at_addr_range
718 all_bp_locations_at_addr (CORE_ADDR addr)
719 {
720 return bp_locations_at_addr_range (addr);
721 }
722
723 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
724 ADDRESS for the current elements of BP_LOCATIONS which get a valid
725 result from bp_location_has_shadow. You can use it for roughly
726 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
727 an address you need to read. */
728
729 static CORE_ADDR bp_locations_placed_address_before_address_max;
730
731 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
732 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
733 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
734 You can use it for roughly limiting the subrange of BP_LOCATIONS to
735 scan for shadow bytes for an address you need to read. */
736
737 static CORE_ADDR bp_locations_shadow_len_after_address_max;
738
739 /* The locations that no longer correspond to any breakpoint, unlinked
740 from the bp_locations array, but for which a hit may still be
741 reported by a target. */
742 static std::vector<bp_location *> moribund_locations;
743
744 /* Number of last breakpoint made. */
745
746 static int breakpoint_count;
747
748 /* The value of `breakpoint_count' before the last command that
749 created breakpoints. If the last (break-like) command created more
750 than one breakpoint, then the difference between BREAKPOINT_COUNT
751 and PREV_BREAKPOINT_COUNT is more than one. */
752 static int prev_breakpoint_count;
753
754 /* Number of last tracepoint made. */
755
756 static int tracepoint_count;
757
758 static struct cmd_list_element *breakpoint_set_cmdlist;
759 static struct cmd_list_element *breakpoint_show_cmdlist;
760 struct cmd_list_element *save_cmdlist;
761
762 /* Return whether a breakpoint is an active enabled breakpoint. */
763 static int
764 breakpoint_enabled (struct breakpoint *b)
765 {
766 return (b->enable_state == bp_enabled);
767 }
768
769 /* Set breakpoint count to NUM. */
770
771 static void
772 set_breakpoint_count (int num)
773 {
774 prev_breakpoint_count = breakpoint_count;
775 breakpoint_count = num;
776 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
777 }
778
779 /* Used by `start_rbreak_breakpoints' below, to record the current
780 breakpoint count before "rbreak" creates any breakpoint. */
781 static int rbreak_start_breakpoint_count;
782
783 /* Called at the start an "rbreak" command to record the first
784 breakpoint made. */
785
786 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
787 {
788 rbreak_start_breakpoint_count = breakpoint_count;
789 }
790
791 /* Called at the end of an "rbreak" command to record the last
792 breakpoint made. */
793
794 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
795 {
796 prev_breakpoint_count = rbreak_start_breakpoint_count;
797 }
798
799 /* Used in run_command to zero the hit count when a new run starts. */
800
801 void
802 clear_breakpoint_hit_counts (void)
803 {
804 for (breakpoint *b : all_breakpoints ())
805 b->hit_count = 0;
806 }
807
808 \f
809 /* Return the breakpoint with the specified number, or NULL
810 if the number does not refer to an existing breakpoint. */
811
812 struct breakpoint *
813 get_breakpoint (int num)
814 {
815 for (breakpoint *b : all_breakpoints ())
816 if (b->number == num)
817 return b;
818
819 return nullptr;
820 }
821
822 \f
823
824 /* Mark locations as "conditions have changed" in case the target supports
825 evaluating conditions on its side. */
826
827 static void
828 mark_breakpoint_modified (struct breakpoint *b)
829 {
830 /* This is only meaningful if the target is
831 evaluating conditions and if the user has
832 opted for condition evaluation on the target's
833 side. */
834 if (gdb_evaluates_breakpoint_condition_p ()
835 || !target_supports_evaluation_of_breakpoint_conditions ())
836 return;
837
838 if (!is_breakpoint (b))
839 return;
840
841 for (bp_location *loc : b->locations ())
842 loc->condition_changed = condition_modified;
843 }
844
845 /* Mark location as "conditions have changed" in case the target supports
846 evaluating conditions on its side. */
847
848 static void
849 mark_breakpoint_location_modified (struct bp_location *loc)
850 {
851 /* This is only meaningful if the target is
852 evaluating conditions and if the user has
853 opted for condition evaluation on the target's
854 side. */
855 if (gdb_evaluates_breakpoint_condition_p ()
856 || !target_supports_evaluation_of_breakpoint_conditions ())
857
858 return;
859
860 if (!is_breakpoint (loc->owner))
861 return;
862
863 loc->condition_changed = condition_modified;
864 }
865
866 /* Sets the condition-evaluation mode using the static global
867 condition_evaluation_mode. */
868
869 static void
870 set_condition_evaluation_mode (const char *args, int from_tty,
871 struct cmd_list_element *c)
872 {
873 const char *old_mode, *new_mode;
874
875 if ((condition_evaluation_mode_1 == condition_evaluation_target)
876 && !target_supports_evaluation_of_breakpoint_conditions ())
877 {
878 condition_evaluation_mode_1 = condition_evaluation_mode;
879 warning (_("Target does not support breakpoint condition evaluation.\n"
880 "Using host evaluation mode instead."));
881 return;
882 }
883
884 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
885 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
886
887 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
888 settings was "auto". */
889 condition_evaluation_mode = condition_evaluation_mode_1;
890
891 /* Only update the mode if the user picked a different one. */
892 if (new_mode != old_mode)
893 {
894 /* If the user switched to a different evaluation mode, we
895 need to synch the changes with the target as follows:
896
897 "host" -> "target": Send all (valid) conditions to the target.
898 "target" -> "host": Remove all the conditions from the target.
899 */
900
901 if (new_mode == condition_evaluation_target)
902 {
903 /* Mark everything modified and synch conditions with the
904 target. */
905 for (bp_location *loc : all_bp_locations ())
906 mark_breakpoint_location_modified (loc);
907 }
908 else
909 {
910 /* Manually mark non-duplicate locations to synch conditions
911 with the target. We do this to remove all the conditions the
912 target knows about. */
913 for (bp_location *loc : all_bp_locations ())
914 if (is_breakpoint (loc->owner) && loc->inserted)
915 loc->needs_update = 1;
916 }
917
918 /* Do the update. */
919 update_global_location_list (UGLL_MAY_INSERT);
920 }
921
922 return;
923 }
924
925 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
926 what "auto" is translating to. */
927
928 static void
929 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
930 struct cmd_list_element *c, const char *value)
931 {
932 if (condition_evaluation_mode == condition_evaluation_auto)
933 gdb_printf (file,
934 _("Breakpoint condition evaluation "
935 "mode is %s (currently %s).\n"),
936 value,
937 breakpoint_condition_evaluation_mode ());
938 else
939 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
940 value);
941 }
942
943 /* Parse COND_STRING in the context of LOC and set as the condition
944 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
945 the number of LOC within its owner. In case of parsing error, mark
946 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
947
948 static void
949 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
950 int bp_num, int loc_num)
951 {
952 bool has_junk = false;
953 try
954 {
955 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
956 block_for_pc (loc->address), 0);
957 if (*cond_string != 0)
958 has_junk = true;
959 else
960 {
961 loc->cond = std::move (new_exp);
962 if (loc->disabled_by_cond && loc->enabled)
963 gdb_printf (_("Breakpoint %d's condition is now valid at "
964 "location %d, enabling.\n"),
965 bp_num, loc_num);
966
967 loc->disabled_by_cond = false;
968 }
969 }
970 catch (const gdb_exception_error &e)
971 {
972 if (loc->enabled)
973 {
974 /* Warn if a user-enabled location is now becoming disabled-by-cond.
975 BP_NUM is 0 if the breakpoint is being defined for the first
976 time using the "break ... if ..." command, and non-zero if
977 already defined. */
978 if (bp_num != 0)
979 warning (_("failed to validate condition at location %d.%d, "
980 "disabling:\n %s"), bp_num, loc_num, e.what ());
981 else
982 warning (_("failed to validate condition at location %d, "
983 "disabling:\n %s"), loc_num, e.what ());
984 }
985
986 loc->disabled_by_cond = true;
987 }
988
989 if (has_junk)
990 error (_("Garbage '%s' follows condition"), cond_string);
991 }
992
993 void
994 set_breakpoint_condition (struct breakpoint *b, const char *exp,
995 int from_tty, bool force)
996 {
997 if (*exp == 0)
998 {
999 b->cond_string.reset ();
1000
1001 if (is_watchpoint (b))
1002 static_cast<watchpoint *> (b)->cond_exp.reset ();
1003 else
1004 {
1005 int loc_num = 1;
1006 for (bp_location *loc : b->locations ())
1007 {
1008 loc->cond.reset ();
1009 if (loc->disabled_by_cond && loc->enabled)
1010 gdb_printf (_("Breakpoint %d's condition is now valid at "
1011 "location %d, enabling.\n"),
1012 b->number, loc_num);
1013 loc->disabled_by_cond = false;
1014 loc_num++;
1015
1016 /* No need to free the condition agent expression
1017 bytecode (if we have one). We will handle this
1018 when we go through update_global_location_list. */
1019 }
1020 }
1021
1022 if (from_tty)
1023 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
1024 }
1025 else
1026 {
1027 if (is_watchpoint (b))
1028 {
1029 innermost_block_tracker tracker;
1030 const char *arg = exp;
1031 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
1032 if (*arg != 0)
1033 error (_("Junk at end of expression"));
1034 watchpoint *w = static_cast<watchpoint *> (b);
1035 w->cond_exp = std::move (new_exp);
1036 w->cond_exp_valid_block = tracker.block ();
1037 }
1038 else
1039 {
1040 /* Parse and set condition expressions. We make two passes.
1041 In the first, we parse the condition string to see if it
1042 is valid in at least one location. If so, the condition
1043 would be accepted. So we go ahead and set the locations'
1044 conditions. In case no valid case is found, we throw
1045 the error and the condition string will be rejected.
1046 This two-pass approach is taken to avoid setting the
1047 state of locations in case of a reject. */
1048 for (bp_location *loc : b->locations ())
1049 {
1050 try
1051 {
1052 const char *arg = exp;
1053 parse_exp_1 (&arg, loc->address,
1054 block_for_pc (loc->address), 0);
1055 if (*arg != 0)
1056 error (_("Junk at end of expression"));
1057 break;
1058 }
1059 catch (const gdb_exception_error &e)
1060 {
1061 /* Condition string is invalid. If this happens to
1062 be the last loc, abandon (if not forced) or continue
1063 (if forced). */
1064 if (loc->next == nullptr && !force)
1065 throw;
1066 }
1067 }
1068
1069 /* If we reach here, the condition is valid at some locations. */
1070 int loc_num = 1;
1071 for (bp_location *loc : b->locations ())
1072 {
1073 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
1074 loc_num++;
1075 }
1076 }
1077
1078 /* We know that the new condition parsed successfully. The
1079 condition string of the breakpoint can be safely updated. */
1080 b->cond_string = make_unique_xstrdup (exp);
1081 b->condition_not_parsed = 0;
1082 }
1083 mark_breakpoint_modified (b);
1084
1085 gdb::observers::breakpoint_modified.notify (b);
1086 }
1087
1088 /* See breakpoint.h. */
1089
1090 void
1091 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1092 bool force)
1093 {
1094 for (breakpoint *b : all_breakpoints ())
1095 if (b->number == bpnum)
1096 {
1097 /* Check if this breakpoint has a "stop" method implemented in an
1098 extension language. This method and conditions entered into GDB
1099 from the CLI are mutually exclusive. */
1100 const struct extension_language_defn *extlang
1101 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1102
1103 if (extlang != NULL)
1104 {
1105 error (_("Only one stop condition allowed. There is currently"
1106 " a %s stop condition defined for this breakpoint."),
1107 ext_lang_capitalized_name (extlang));
1108 }
1109 set_breakpoint_condition (b, exp, from_tty, force);
1110
1111 if (is_breakpoint (b))
1112 update_global_location_list (UGLL_MAY_INSERT);
1113
1114 return;
1115 }
1116
1117 error (_("No breakpoint number %d."), bpnum);
1118 }
1119
1120 /* The options for the "condition" command. */
1121
1122 struct condition_command_opts
1123 {
1124 /* For "-force". */
1125 bool force_condition = false;
1126 };
1127
1128 static const gdb::option::option_def condition_command_option_defs[] = {
1129
1130 gdb::option::flag_option_def<condition_command_opts> {
1131 "force",
1132 [] (condition_command_opts *opts) { return &opts->force_condition; },
1133 N_("Set the condition even if it is invalid for all current locations."),
1134 },
1135
1136 };
1137
1138 /* Create an option_def_group for the "condition" options, with
1139 CC_OPTS as context. */
1140
1141 static inline gdb::option::option_def_group
1142 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1143 {
1144 return {{condition_command_option_defs}, cc_opts};
1145 }
1146
1147 /* Completion for the "condition" command. */
1148
1149 static void
1150 condition_completer (struct cmd_list_element *cmd,
1151 completion_tracker &tracker,
1152 const char *text, const char * /*word*/)
1153 {
1154 bool has_no_arguments = (*text == '\0');
1155 condition_command_opts cc_opts;
1156 const auto group = make_condition_command_options_def_group (&cc_opts);
1157 if (gdb::option::complete_options
1158 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1159 return;
1160
1161 text = skip_spaces (text);
1162 const char *space = skip_to_space (text);
1163 if (*space == '\0')
1164 {
1165 int len;
1166
1167 if (text[0] == '$')
1168 {
1169 tracker.advance_custom_word_point_by (1);
1170 /* We don't support completion of history indices. */
1171 if (!isdigit (text[1]))
1172 complete_internalvar (tracker, &text[1]);
1173 return;
1174 }
1175
1176 /* Suggest the "-force" flag if no arguments are given. If
1177 arguments were passed, they either already include the flag,
1178 or we are beyond the point of suggesting it because it's
1179 positionally the first argument. */
1180 if (has_no_arguments)
1181 gdb::option::complete_on_all_options (tracker, group);
1182
1183 /* We're completing the breakpoint number. */
1184 len = strlen (text);
1185
1186 for (breakpoint *b : all_breakpoints ())
1187 {
1188 char number[50];
1189
1190 xsnprintf (number, sizeof (number), "%d", b->number);
1191
1192 if (strncmp (number, text, len) == 0)
1193 tracker.add_completion (make_unique_xstrdup (number));
1194 }
1195
1196 return;
1197 }
1198
1199 /* We're completing the expression part. Skip the breakpoint num. */
1200 const char *exp_start = skip_spaces (space);
1201 tracker.advance_custom_word_point_by (exp_start - text);
1202 text = exp_start;
1203 const char *word = advance_to_expression_complete_word_point (tracker, text);
1204 expression_completer (cmd, tracker, text, word);
1205 }
1206
1207 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1208
1209 static void
1210 condition_command (const char *arg, int from_tty)
1211 {
1212 const char *p;
1213 int bnum;
1214
1215 if (arg == 0)
1216 error_no_arg (_("breakpoint number"));
1217
1218 p = arg;
1219
1220 /* Check if the "-force" flag was passed. */
1221 condition_command_opts cc_opts;
1222 const auto group = make_condition_command_options_def_group (&cc_opts);
1223 gdb::option::process_options
1224 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1225
1226 bnum = get_number (&p);
1227 if (bnum == 0)
1228 error (_("Bad breakpoint argument: '%s'"), arg);
1229
1230 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1231 }
1232
1233 /* Check that COMMAND do not contain commands that are suitable
1234 only for tracepoints and not suitable for ordinary breakpoints.
1235 Throw if any such commands is found. */
1236
1237 static void
1238 check_no_tracepoint_commands (struct command_line *commands)
1239 {
1240 struct command_line *c;
1241
1242 for (c = commands; c; c = c->next)
1243 {
1244 if (c->control_type == while_stepping_control)
1245 error (_("The 'while-stepping' command can "
1246 "only be used for tracepoints"));
1247
1248 check_no_tracepoint_commands (c->body_list_0.get ());
1249 check_no_tracepoint_commands (c->body_list_1.get ());
1250
1251 /* Not that command parsing removes leading whitespace and comment
1252 lines and also empty lines. So, we only need to check for
1253 command directly. */
1254 if (strstr (c->line, "collect ") == c->line)
1255 error (_("The 'collect' command can only be used for tracepoints"));
1256
1257 if (strstr (c->line, "teval ") == c->line)
1258 error (_("The 'teval' command can only be used for tracepoints"));
1259 }
1260 }
1261
1262 struct longjmp_breakpoint : public momentary_breakpoint
1263 {
1264 using momentary_breakpoint::momentary_breakpoint;
1265
1266 ~longjmp_breakpoint () override;
1267 };
1268
1269 /* Encapsulate tests for different types of tracepoints. */
1270
1271 static bool
1272 is_tracepoint_type (bptype type)
1273 {
1274 return (type == bp_tracepoint
1275 || type == bp_fast_tracepoint
1276 || type == bp_static_tracepoint
1277 || type == bp_static_marker_tracepoint);
1278 }
1279
1280 /* See breakpoint.h. */
1281
1282 bool
1283 is_tracepoint (const struct breakpoint *b)
1284 {
1285 return is_tracepoint_type (b->type);
1286 }
1287
1288 /* Factory function to create an appropriate instance of breakpoint given
1289 TYPE. */
1290
1291 template<typename... Arg>
1292 static std::unique_ptr<code_breakpoint>
1293 new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type,
1294 Arg&&... args)
1295 {
1296 code_breakpoint *b;
1297
1298 switch (type)
1299 {
1300 case bp_breakpoint:
1301 case bp_hardware_breakpoint:
1302 b = new ordinary_breakpoint (gdbarch, type,
1303 std::forward<Arg> (args)...);
1304 break;
1305
1306 case bp_fast_tracepoint:
1307 case bp_static_tracepoint:
1308 case bp_tracepoint:
1309 b = new tracepoint (gdbarch, type,
1310 std::forward<Arg> (args)...);
1311 break;
1312
1313 case bp_static_marker_tracepoint:
1314 b = new static_marker_tracepoint (gdbarch, type,
1315 std::forward<Arg> (args)...);
1316 break;
1317
1318 case bp_dprintf:
1319 b = new dprintf_breakpoint (gdbarch, type,
1320 std::forward<Arg> (args)...);
1321 break;
1322
1323 default:
1324 gdb_assert_not_reached ("invalid type");
1325 }
1326
1327 return std::unique_ptr<code_breakpoint> (b);
1328 }
1329
1330 /* A helper function that validates that COMMANDS are valid for a
1331 breakpoint. This function will throw an exception if a problem is
1332 found. */
1333
1334 static void
1335 validate_commands_for_breakpoint (struct breakpoint *b,
1336 struct command_line *commands)
1337 {
1338 if (is_tracepoint (b))
1339 {
1340 struct tracepoint *t = (struct tracepoint *) b;
1341 struct command_line *c;
1342 struct command_line *while_stepping = 0;
1343
1344 /* Reset the while-stepping step count. The previous commands
1345 might have included a while-stepping action, while the new
1346 ones might not. */
1347 t->step_count = 0;
1348
1349 /* We need to verify that each top-level element of commands is
1350 valid for tracepoints, that there's at most one
1351 while-stepping element, and that the while-stepping's body
1352 has valid tracing commands excluding nested while-stepping.
1353 We also need to validate the tracepoint action line in the
1354 context of the tracepoint --- validate_actionline actually
1355 has side effects, like setting the tracepoint's
1356 while-stepping STEP_COUNT, in addition to checking if the
1357 collect/teval actions parse and make sense in the
1358 tracepoint's context. */
1359 for (c = commands; c; c = c->next)
1360 {
1361 if (c->control_type == while_stepping_control)
1362 {
1363 if (b->type == bp_fast_tracepoint)
1364 error (_("The 'while-stepping' command "
1365 "cannot be used for fast tracepoint"));
1366 else if (b->type == bp_static_tracepoint
1367 || b->type == bp_static_marker_tracepoint)
1368 error (_("The 'while-stepping' command "
1369 "cannot be used for static tracepoint"));
1370
1371 if (while_stepping)
1372 error (_("The 'while-stepping' command "
1373 "can be used only once"));
1374 else
1375 while_stepping = c;
1376 }
1377
1378 validate_actionline (c->line, b);
1379 }
1380 if (while_stepping)
1381 {
1382 struct command_line *c2;
1383
1384 gdb_assert (while_stepping->body_list_1 == nullptr);
1385 c2 = while_stepping->body_list_0.get ();
1386 for (; c2; c2 = c2->next)
1387 {
1388 if (c2->control_type == while_stepping_control)
1389 error (_("The 'while-stepping' command cannot be nested"));
1390 }
1391 }
1392 }
1393 else
1394 {
1395 check_no_tracepoint_commands (commands);
1396 }
1397 }
1398
1399 /* Return a vector of all the static tracepoints set at ADDR. The
1400 caller is responsible for releasing the vector. */
1401
1402 std::vector<breakpoint *>
1403 static_tracepoints_here (CORE_ADDR addr)
1404 {
1405 std::vector<breakpoint *> found;
1406
1407 for (breakpoint *b : all_breakpoints ())
1408 if (b->type == bp_static_tracepoint
1409 || b->type == bp_static_marker_tracepoint)
1410 {
1411 for (bp_location *loc : b->locations ())
1412 if (loc->address == addr)
1413 found.push_back (b);
1414 }
1415
1416 return found;
1417 }
1418
1419 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1420 validate that only allowed commands are included. */
1421
1422 void
1423 breakpoint_set_commands (struct breakpoint *b,
1424 counted_command_line &&commands)
1425 {
1426 validate_commands_for_breakpoint (b, commands.get ());
1427
1428 b->commands = std::move (commands);
1429 gdb::observers::breakpoint_modified.notify (b);
1430 }
1431
1432 /* Set the internal `silent' flag on the breakpoint. Note that this
1433 is not the same as the "silent" that may appear in the breakpoint's
1434 commands. */
1435
1436 void
1437 breakpoint_set_silent (struct breakpoint *b, int silent)
1438 {
1439 int old_silent = b->silent;
1440
1441 b->silent = silent;
1442 if (old_silent != silent)
1443 gdb::observers::breakpoint_modified.notify (b);
1444 }
1445
1446 /* Set the thread for this breakpoint. If THREAD is -1, make the
1447 breakpoint work for any thread. */
1448
1449 void
1450 breakpoint_set_thread (struct breakpoint *b, int thread)
1451 {
1452 int old_thread = b->thread;
1453
1454 b->thread = thread;
1455 if (old_thread != thread)
1456 gdb::observers::breakpoint_modified.notify (b);
1457 }
1458
1459 /* Set the task for this breakpoint. If TASK is 0, make the
1460 breakpoint work for any task. */
1461
1462 void
1463 breakpoint_set_task (struct breakpoint *b, int task)
1464 {
1465 int old_task = b->task;
1466
1467 b->task = task;
1468 if (old_task != task)
1469 gdb::observers::breakpoint_modified.notify (b);
1470 }
1471
1472 static void
1473 commands_command_1 (const char *arg, int from_tty,
1474 struct command_line *control)
1475 {
1476 counted_command_line cmd;
1477 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1478 NULL after the call to read_command_lines if the user provides an empty
1479 list of command by just typing "end". */
1480 bool cmd_read = false;
1481
1482 std::string new_arg;
1483
1484 if (arg == NULL || !*arg)
1485 {
1486 /* Argument not explicitly given. Synthesize it. */
1487 if (breakpoint_count - prev_breakpoint_count > 1)
1488 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1489 breakpoint_count);
1490 else if (breakpoint_count > 0)
1491 new_arg = string_printf ("%d", breakpoint_count);
1492 }
1493 else
1494 {
1495 /* Create a copy of ARG. This is needed because the "commands"
1496 command may be coming from a script. In that case, the read
1497 line buffer is going to be overwritten in the lambda of
1498 'map_breakpoint_numbers' below when reading the next line
1499 before we are are done parsing the breakpoint numbers. */
1500 new_arg = arg;
1501 }
1502 arg = new_arg.c_str ();
1503
1504 map_breakpoint_numbers
1505 (arg, [&] (breakpoint *b)
1506 {
1507 if (!cmd_read)
1508 {
1509 gdb_assert (cmd == NULL);
1510 if (control != NULL)
1511 cmd = control->body_list_0;
1512 else
1513 {
1514 std::string str
1515 = string_printf (_("Type commands for breakpoint(s) "
1516 "%s, one per line."),
1517 arg);
1518
1519 auto do_validate = [=] (const char *line)
1520 {
1521 validate_actionline (line, b);
1522 };
1523 gdb::function_view<void (const char *)> validator;
1524 if (is_tracepoint (b))
1525 validator = do_validate;
1526
1527 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1528 }
1529 cmd_read = true;
1530 }
1531
1532 /* If a breakpoint was on the list more than once, we don't need to
1533 do anything. */
1534 if (b->commands != cmd)
1535 {
1536 validate_commands_for_breakpoint (b, cmd.get ());
1537 b->commands = cmd;
1538 gdb::observers::breakpoint_modified.notify (b);
1539 }
1540 });
1541 }
1542
1543 static void
1544 commands_command (const char *arg, int from_tty)
1545 {
1546 commands_command_1 (arg, from_tty, NULL);
1547 }
1548
1549 /* Like commands_command, but instead of reading the commands from
1550 input stream, takes them from an already parsed command structure.
1551
1552 This is used by cli-script.c to DTRT with breakpoint commands
1553 that are part of if and while bodies. */
1554 enum command_control_type
1555 commands_from_control_command (const char *arg, struct command_line *cmd)
1556 {
1557 commands_command_1 (arg, 0, cmd);
1558 return simple_control;
1559 }
1560
1561 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1562
1563 static int
1564 bp_location_has_shadow (struct bp_location *bl)
1565 {
1566 if (bl->loc_type != bp_loc_software_breakpoint)
1567 return 0;
1568 if (!bl->inserted)
1569 return 0;
1570 if (bl->target_info.shadow_len == 0)
1571 /* BL isn't valid, or doesn't shadow memory. */
1572 return 0;
1573 return 1;
1574 }
1575
1576 /* Update BUF, which is LEN bytes read from the target address
1577 MEMADDR, by replacing a memory breakpoint with its shadowed
1578 contents.
1579
1580 If READBUF is not NULL, this buffer must not overlap with the of
1581 the breakpoint location's shadow_contents buffer. Otherwise, a
1582 failed assertion internal error will be raised. */
1583
1584 static void
1585 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1586 const gdb_byte *writebuf_org,
1587 ULONGEST memaddr, LONGEST len,
1588 struct bp_target_info *target_info,
1589 struct gdbarch *gdbarch)
1590 {
1591 /* Now do full processing of the found relevant range of elements. */
1592 CORE_ADDR bp_addr = 0;
1593 int bp_size = 0;
1594 int bptoffset = 0;
1595
1596 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1597 current_program_space->aspace, 0))
1598 {
1599 /* The breakpoint is inserted in a different address space. */
1600 return;
1601 }
1602
1603 /* Addresses and length of the part of the breakpoint that
1604 we need to copy. */
1605 bp_addr = target_info->placed_address;
1606 bp_size = target_info->shadow_len;
1607
1608 if (bp_addr + bp_size <= memaddr)
1609 {
1610 /* The breakpoint is entirely before the chunk of memory we are
1611 reading. */
1612 return;
1613 }
1614
1615 if (bp_addr >= memaddr + len)
1616 {
1617 /* The breakpoint is entirely after the chunk of memory we are
1618 reading. */
1619 return;
1620 }
1621
1622 /* Offset within shadow_contents. */
1623 if (bp_addr < memaddr)
1624 {
1625 /* Only copy the second part of the breakpoint. */
1626 bp_size -= memaddr - bp_addr;
1627 bptoffset = memaddr - bp_addr;
1628 bp_addr = memaddr;
1629 }
1630
1631 if (bp_addr + bp_size > memaddr + len)
1632 {
1633 /* Only copy the first part of the breakpoint. */
1634 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1635 }
1636
1637 if (readbuf != NULL)
1638 {
1639 /* Verify that the readbuf buffer does not overlap with the
1640 shadow_contents buffer. */
1641 gdb_assert (target_info->shadow_contents >= readbuf + len
1642 || readbuf >= (target_info->shadow_contents
1643 + target_info->shadow_len));
1644
1645 /* Update the read buffer with this inserted breakpoint's
1646 shadow. */
1647 memcpy (readbuf + bp_addr - memaddr,
1648 target_info->shadow_contents + bptoffset, bp_size);
1649 }
1650 else
1651 {
1652 const unsigned char *bp;
1653 CORE_ADDR addr = target_info->reqstd_address;
1654 int placed_size;
1655
1656 /* Update the shadow with what we want to write to memory. */
1657 memcpy (target_info->shadow_contents + bptoffset,
1658 writebuf_org + bp_addr - memaddr, bp_size);
1659
1660 /* Determine appropriate breakpoint contents and size for this
1661 address. */
1662 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1663
1664 /* Update the final write buffer with this inserted
1665 breakpoint's INSN. */
1666 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1667 }
1668 }
1669
1670 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1671 by replacing any memory breakpoints with their shadowed contents.
1672
1673 If READBUF is not NULL, this buffer must not overlap with any of
1674 the breakpoint location's shadow_contents buffers. Otherwise,
1675 a failed assertion internal error will be raised.
1676
1677 The range of shadowed area by each bp_location is:
1678 bl->address - bp_locations_placed_address_before_address_max
1679 up to bl->address + bp_locations_shadow_len_after_address_max
1680 The range we were requested to resolve shadows for is:
1681 memaddr ... memaddr + len
1682 Thus the safe cutoff boundaries for performance optimization are
1683 memaddr + len <= (bl->address
1684 - bp_locations_placed_address_before_address_max)
1685 and:
1686 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1687
1688 void
1689 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1690 const gdb_byte *writebuf_org,
1691 ULONGEST memaddr, LONGEST len)
1692 {
1693 /* Left boundary, right boundary and median element of our binary
1694 search. */
1695 unsigned bc_l, bc_r, bc;
1696
1697 /* Find BC_L which is a leftmost element which may affect BUF
1698 content. It is safe to report lower value but a failure to
1699 report higher one. */
1700
1701 bc_l = 0;
1702 bc_r = bp_locations.size ();
1703 while (bc_l + 1 < bc_r)
1704 {
1705 struct bp_location *bl;
1706
1707 bc = (bc_l + bc_r) / 2;
1708 bl = bp_locations[bc];
1709
1710 /* Check first BL->ADDRESS will not overflow due to the added
1711 constant. Then advance the left boundary only if we are sure
1712 the BC element can in no way affect the BUF content (MEMADDR
1713 to MEMADDR + LEN range).
1714
1715 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1716 offset so that we cannot miss a breakpoint with its shadow
1717 range tail still reaching MEMADDR. */
1718
1719 if ((bl->address + bp_locations_shadow_len_after_address_max
1720 >= bl->address)
1721 && (bl->address + bp_locations_shadow_len_after_address_max
1722 <= memaddr))
1723 bc_l = bc;
1724 else
1725 bc_r = bc;
1726 }
1727
1728 /* Due to the binary search above, we need to make sure we pick the
1729 first location that's at BC_L's address. E.g., if there are
1730 multiple locations at the same address, BC_L may end up pointing
1731 at a duplicate location, and miss the "master"/"inserted"
1732 location. Say, given locations L1, L2 and L3 at addresses A and
1733 B:
1734
1735 L1@A, L2@A, L3@B, ...
1736
1737 BC_L could end up pointing at location L2, while the "master"
1738 location could be L1. Since the `loc->inserted' flag is only set
1739 on "master" locations, we'd forget to restore the shadow of L1
1740 and L2. */
1741 while (bc_l > 0
1742 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1743 bc_l--;
1744
1745 /* Now do full processing of the found relevant range of elements. */
1746
1747 for (bc = bc_l; bc < bp_locations.size (); bc++)
1748 {
1749 struct bp_location *bl = bp_locations[bc];
1750
1751 /* bp_location array has BL->OWNER always non-NULL. */
1752 if (bl->owner->type == bp_none)
1753 warning (_("reading through apparently deleted breakpoint #%d?"),
1754 bl->owner->number);
1755
1756 /* Performance optimization: any further element can no longer affect BUF
1757 content. */
1758
1759 if (bl->address >= bp_locations_placed_address_before_address_max
1760 && (memaddr + len
1761 <= (bl->address
1762 - bp_locations_placed_address_before_address_max)))
1763 break;
1764
1765 if (!bp_location_has_shadow (bl))
1766 continue;
1767
1768 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1769 memaddr, len, &bl->target_info, bl->gdbarch);
1770 }
1771 }
1772
1773 /* See breakpoint.h. */
1774
1775 bool
1776 is_breakpoint (const struct breakpoint *bpt)
1777 {
1778 return (bpt->type == bp_breakpoint
1779 || bpt->type == bp_hardware_breakpoint
1780 || bpt->type == bp_dprintf);
1781 }
1782
1783 /* Return true if BPT is of any hardware watchpoint kind. */
1784
1785 static bool
1786 is_hardware_watchpoint (const struct breakpoint *bpt)
1787 {
1788 return (bpt->type == bp_hardware_watchpoint
1789 || bpt->type == bp_read_watchpoint
1790 || bpt->type == bp_access_watchpoint);
1791 }
1792
1793 /* See breakpoint.h. */
1794
1795 bool
1796 is_watchpoint (const struct breakpoint *bpt)
1797 {
1798 return (is_hardware_watchpoint (bpt)
1799 || bpt->type == bp_watchpoint);
1800 }
1801
1802 /* Returns true if the current thread and its running state are safe
1803 to evaluate or update watchpoint B. Watchpoints on local
1804 expressions need to be evaluated in the context of the thread that
1805 was current when the watchpoint was created, and, that thread needs
1806 to be stopped to be able to select the correct frame context.
1807 Watchpoints on global expressions can be evaluated on any thread,
1808 and in any state. It is presently left to the target allowing
1809 memory accesses when threads are running. */
1810
1811 static int
1812 watchpoint_in_thread_scope (struct watchpoint *b)
1813 {
1814 return (b->pspace == current_program_space
1815 && (b->watchpoint_thread == null_ptid
1816 || (inferior_ptid == b->watchpoint_thread
1817 && !inferior_thread ()->executing ())));
1818 }
1819
1820 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1821 associated bp_watchpoint_scope breakpoint. */
1822
1823 static void
1824 watchpoint_del_at_next_stop (struct watchpoint *w)
1825 {
1826 if (w->related_breakpoint != w)
1827 {
1828 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1829 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1830 w->related_breakpoint->disposition = disp_del_at_next_stop;
1831 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1832 w->related_breakpoint = w;
1833 }
1834 w->disposition = disp_del_at_next_stop;
1835 }
1836
1837 /* Extract a bitfield value from value VAL using the bit parameters contained in
1838 watchpoint W. */
1839
1840 static struct value *
1841 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1842 {
1843 struct value *bit_val;
1844
1845 if (val == NULL)
1846 return NULL;
1847
1848 bit_val = allocate_value (value_type (val));
1849
1850 unpack_value_bitfield (bit_val,
1851 w->val_bitpos,
1852 w->val_bitsize,
1853 value_contents_for_printing (val).data (),
1854 value_offset (val),
1855 val);
1856
1857 return bit_val;
1858 }
1859
1860 /* Allocate a dummy location and add it to B. This is required
1861 because bpstat_stop_status requires a location to be able to report
1862 stops. */
1863
1864 static void
1865 add_dummy_location (struct breakpoint *b,
1866 struct program_space *pspace)
1867 {
1868 gdb_assert (b->loc == NULL);
1869
1870 b->loc = new bp_location (b, bp_loc_other);
1871 b->loc->pspace = pspace;
1872 }
1873
1874 /* Assuming that B is a watchpoint:
1875 - Reparse watchpoint expression, if REPARSE is non-zero
1876 - Evaluate expression and store the result in B->val
1877 - Evaluate the condition if there is one, and store the result
1878 in b->loc->cond.
1879 - Update the list of values that must be watched in B->loc.
1880
1881 If the watchpoint disposition is disp_del_at_next_stop, then do
1882 nothing. If this is local watchpoint that is out of scope, delete
1883 it.
1884
1885 Even with `set breakpoint always-inserted on' the watchpoints are
1886 removed + inserted on each stop here. Normal breakpoints must
1887 never be removed because they might be missed by a running thread
1888 when debugging in non-stop mode. On the other hand, hardware
1889 watchpoints (is_hardware_watchpoint; processed here) are specific
1890 to each LWP since they are stored in each LWP's hardware debug
1891 registers. Therefore, such LWP must be stopped first in order to
1892 be able to modify its hardware watchpoints.
1893
1894 Hardware watchpoints must be reset exactly once after being
1895 presented to the user. It cannot be done sooner, because it would
1896 reset the data used to present the watchpoint hit to the user. And
1897 it must not be done later because it could display the same single
1898 watchpoint hit during multiple GDB stops. Note that the latter is
1899 relevant only to the hardware watchpoint types bp_read_watchpoint
1900 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1901 not user-visible - its hit is suppressed if the memory content has
1902 not changed.
1903
1904 The following constraints influence the location where we can reset
1905 hardware watchpoints:
1906
1907 * target_stopped_by_watchpoint and target_stopped_data_address are
1908 called several times when GDB stops.
1909
1910 [linux]
1911 * Multiple hardware watchpoints can be hit at the same time,
1912 causing GDB to stop. GDB only presents one hardware watchpoint
1913 hit at a time as the reason for stopping, and all the other hits
1914 are presented later, one after the other, each time the user
1915 requests the execution to be resumed. Execution is not resumed
1916 for the threads still having pending hit event stored in
1917 LWP_INFO->STATUS. While the watchpoint is already removed from
1918 the inferior on the first stop the thread hit event is kept being
1919 reported from its cached value by linux_nat_stopped_data_address
1920 until the real thread resume happens after the watchpoint gets
1921 presented and thus its LWP_INFO->STATUS gets reset.
1922
1923 Therefore the hardware watchpoint hit can get safely reset on the
1924 watchpoint removal from inferior. */
1925
1926 static void
1927 update_watchpoint (struct watchpoint *b, int reparse)
1928 {
1929 int within_current_scope;
1930 struct frame_id saved_frame_id;
1931 int frame_saved;
1932
1933 /* If this is a local watchpoint, we only want to check if the
1934 watchpoint frame is in scope if the current thread is the thread
1935 that was used to create the watchpoint. */
1936 if (!watchpoint_in_thread_scope (b))
1937 return;
1938
1939 if (b->disposition == disp_del_at_next_stop)
1940 return;
1941
1942 frame_saved = 0;
1943
1944 /* Determine if the watchpoint is within scope. */
1945 if (b->exp_valid_block == NULL)
1946 within_current_scope = 1;
1947 else
1948 {
1949 struct frame_info *fi = get_current_frame ();
1950 struct gdbarch *frame_arch = get_frame_arch (fi);
1951 CORE_ADDR frame_pc = get_frame_pc (fi);
1952
1953 /* If we're at a point where the stack has been destroyed
1954 (e.g. in a function epilogue), unwinding may not work
1955 properly. Do not attempt to recreate locations at this
1956 point. See similar comments in watchpoint_check. */
1957 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1958 return;
1959
1960 /* Save the current frame's ID so we can restore it after
1961 evaluating the watchpoint expression on its own frame. */
1962 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1963 took a frame parameter, so that we didn't have to change the
1964 selected frame. */
1965 frame_saved = 1;
1966 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1967
1968 fi = frame_find_by_id (b->watchpoint_frame);
1969 within_current_scope = (fi != NULL);
1970 if (within_current_scope)
1971 select_frame (fi);
1972 }
1973
1974 /* We don't free locations. They are stored in the bp_location array
1975 and update_global_location_list will eventually delete them and
1976 remove breakpoints if needed. */
1977 b->loc = NULL;
1978
1979 if (within_current_scope && reparse)
1980 {
1981 const char *s;
1982
1983 b->exp.reset ();
1984 s = (b->exp_string_reparse
1985 ? b->exp_string_reparse.get ()
1986 : b->exp_string.get ());
1987 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1988 /* If the meaning of expression itself changed, the old value is
1989 no longer relevant. We don't want to report a watchpoint hit
1990 to the user when the old value and the new value may actually
1991 be completely different objects. */
1992 b->val = NULL;
1993 b->val_valid = false;
1994
1995 /* Note that unlike with breakpoints, the watchpoint's condition
1996 expression is stored in the breakpoint object, not in the
1997 locations (re)created below. */
1998 if (b->cond_string != NULL)
1999 {
2000 b->cond_exp.reset ();
2001
2002 s = b->cond_string.get ();
2003 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
2004 }
2005 }
2006
2007 /* If we failed to parse the expression, for example because
2008 it refers to a global variable in a not-yet-loaded shared library,
2009 don't try to insert watchpoint. We don't automatically delete
2010 such watchpoint, though, since failure to parse expression
2011 is different from out-of-scope watchpoint. */
2012 if (!target_has_execution ())
2013 {
2014 /* Without execution, memory can't change. No use to try and
2015 set watchpoint locations. The watchpoint will be reset when
2016 the target gains execution, through breakpoint_re_set. */
2017 if (!can_use_hw_watchpoints)
2018 {
2019 if (b->works_in_software_mode ())
2020 b->type = bp_watchpoint;
2021 else
2022 error (_("Can't set read/access watchpoint when "
2023 "hardware watchpoints are disabled."));
2024 }
2025 }
2026 else if (within_current_scope && b->exp)
2027 {
2028 std::vector<value_ref_ptr> val_chain;
2029 struct value *v, *result;
2030 struct program_space *frame_pspace;
2031
2032 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
2033 &val_chain, false);
2034
2035 /* Avoid setting b->val if it's already set. The meaning of
2036 b->val is 'the last value' user saw, and we should update
2037 it only if we reported that last value to user. As it
2038 happens, the code that reports it updates b->val directly.
2039 We don't keep track of the memory value for masked
2040 watchpoints. */
2041 if (!b->val_valid && !is_masked_watchpoint (b))
2042 {
2043 if (b->val_bitsize != 0)
2044 v = extract_bitfield_from_watchpoint_value (b, v);
2045 b->val = release_value (v);
2046 b->val_valid = true;
2047 }
2048
2049 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2050
2051 /* Look at each value on the value chain. */
2052 gdb_assert (!val_chain.empty ());
2053 for (const value_ref_ptr &iter : val_chain)
2054 {
2055 v = iter.get ();
2056
2057 /* If it's a memory location, and GDB actually needed
2058 its contents to evaluate the expression, then we
2059 must watch it. If the first value returned is
2060 still lazy, that means an error occurred reading it;
2061 watch it anyway in case it becomes readable. */
2062 if (VALUE_LVAL (v) == lval_memory
2063 && (v == val_chain[0] || ! value_lazy (v)))
2064 {
2065 struct type *vtype = check_typedef (value_type (v));
2066
2067 /* We only watch structs and arrays if user asked
2068 for it explicitly, never if they just happen to
2069 appear in the middle of some value chain. */
2070 if (v == result
2071 || (vtype->code () != TYPE_CODE_STRUCT
2072 && vtype->code () != TYPE_CODE_ARRAY))
2073 {
2074 CORE_ADDR addr;
2075 enum target_hw_bp_type type;
2076 struct bp_location *loc, **tmp;
2077 int bitpos = 0, bitsize = 0;
2078
2079 if (value_bitsize (v) != 0)
2080 {
2081 /* Extract the bit parameters out from the bitfield
2082 sub-expression. */
2083 bitpos = value_bitpos (v);
2084 bitsize = value_bitsize (v);
2085 }
2086 else if (v == result && b->val_bitsize != 0)
2087 {
2088 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2089 lvalue whose bit parameters are saved in the fields
2090 VAL_BITPOS and VAL_BITSIZE. */
2091 bitpos = b->val_bitpos;
2092 bitsize = b->val_bitsize;
2093 }
2094
2095 addr = value_address (v);
2096 if (bitsize != 0)
2097 {
2098 /* Skip the bytes that don't contain the bitfield. */
2099 addr += bitpos / 8;
2100 }
2101
2102 type = hw_write;
2103 if (b->type == bp_read_watchpoint)
2104 type = hw_read;
2105 else if (b->type == bp_access_watchpoint)
2106 type = hw_access;
2107
2108 loc = b->allocate_location ();
2109 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2110 ;
2111 *tmp = loc;
2112 loc->gdbarch = value_type (v)->arch ();
2113
2114 loc->pspace = frame_pspace;
2115 loc->address = address_significant (loc->gdbarch, addr);
2116
2117 if (bitsize != 0)
2118 {
2119 /* Just cover the bytes that make up the bitfield. */
2120 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2121 }
2122 else
2123 loc->length = TYPE_LENGTH (value_type (v));
2124
2125 loc->watchpoint_type = type;
2126 }
2127 }
2128 }
2129
2130 /* Change the type of breakpoint between hardware assisted or
2131 an ordinary watchpoint depending on the hardware support
2132 and free hardware slots. REPARSE is set when the inferior
2133 is started. */
2134 if (reparse)
2135 {
2136 int reg_cnt;
2137 enum bp_loc_type loc_type;
2138
2139 reg_cnt = can_use_hardware_watchpoint (val_chain);
2140
2141 if (reg_cnt)
2142 {
2143 int i, target_resources_ok, other_type_used;
2144 enum bptype type;
2145
2146 /* Use an exact watchpoint when there's only one memory region to be
2147 watched, and only one debug register is needed to watch it. */
2148 b->exact = target_exact_watchpoints && reg_cnt == 1;
2149
2150 /* We need to determine how many resources are already
2151 used for all other hardware watchpoints plus this one
2152 to see if we still have enough resources to also fit
2153 this watchpoint in as well. */
2154
2155 /* If this is a software watchpoint, we try to turn it
2156 to a hardware one -- count resources as if B was of
2157 hardware watchpoint type. */
2158 type = b->type;
2159 if (type == bp_watchpoint)
2160 type = bp_hardware_watchpoint;
2161
2162 /* This watchpoint may or may not have been placed on
2163 the list yet at this point (it won't be in the list
2164 if we're trying to create it for the first time,
2165 through watch_command), so always account for it
2166 manually. */
2167
2168 /* Count resources used by all watchpoints except B. */
2169 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2170
2171 /* Add in the resources needed for B. */
2172 i += hw_watchpoint_use_count (b);
2173
2174 target_resources_ok
2175 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2176 if (target_resources_ok <= 0)
2177 {
2178 bool sw_mode = b->works_in_software_mode ();
2179
2180 if (target_resources_ok == 0 && !sw_mode)
2181 error (_("Target does not support this type of "
2182 "hardware watchpoint."));
2183 else if (target_resources_ok < 0 && !sw_mode)
2184 error (_("There are not enough available hardware "
2185 "resources for this watchpoint."));
2186
2187 /* Downgrade to software watchpoint. */
2188 b->type = bp_watchpoint;
2189 }
2190 else
2191 {
2192 /* If this was a software watchpoint, we've just
2193 found we have enough resources to turn it to a
2194 hardware watchpoint. Otherwise, this is a
2195 nop. */
2196 b->type = type;
2197 }
2198 }
2199 else if (!b->works_in_software_mode ())
2200 {
2201 if (!can_use_hw_watchpoints)
2202 error (_("Can't set read/access watchpoint when "
2203 "hardware watchpoints are disabled."));
2204 else
2205 error (_("Expression cannot be implemented with "
2206 "read/access watchpoint."));
2207 }
2208 else
2209 b->type = bp_watchpoint;
2210
2211 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
2212 : bp_loc_hardware_watchpoint);
2213 for (bp_location *bl : b->locations ())
2214 bl->loc_type = loc_type;
2215 }
2216
2217 /* If a software watchpoint is not watching any memory, then the
2218 above left it without any location set up. But,
2219 bpstat_stop_status requires a location to be able to report
2220 stops, so make sure there's at least a dummy one. */
2221 if (b->type == bp_watchpoint && b->loc == NULL)
2222 add_dummy_location (b, frame_pspace);
2223 }
2224 else if (!within_current_scope)
2225 {
2226 gdb_printf (_("\
2227 Watchpoint %d deleted because the program has left the block\n\
2228 in which its expression is valid.\n"),
2229 b->number);
2230 watchpoint_del_at_next_stop (b);
2231 }
2232
2233 /* Restore the selected frame. */
2234 if (frame_saved)
2235 select_frame (frame_find_by_id (saved_frame_id));
2236 }
2237
2238
2239 /* Returns 1 iff breakpoint location should be
2240 inserted in the inferior. We don't differentiate the type of BL's owner
2241 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2242 breakpoint_ops is not defined, because in insert_bp_location,
2243 tracepoint's insert_location will not be called. */
2244 static int
2245 should_be_inserted (struct bp_location *bl)
2246 {
2247 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2248 return 0;
2249
2250 if (bl->owner->disposition == disp_del_at_next_stop)
2251 return 0;
2252
2253 if (!bl->enabled || bl->disabled_by_cond
2254 || bl->shlib_disabled || bl->duplicate)
2255 return 0;
2256
2257 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2258 return 0;
2259
2260 /* This is set for example, when we're attached to the parent of a
2261 vfork, and have detached from the child. The child is running
2262 free, and we expect it to do an exec or exit, at which point the
2263 OS makes the parent schedulable again (and the target reports
2264 that the vfork is done). Until the child is done with the shared
2265 memory region, do not insert breakpoints in the parent, otherwise
2266 the child could still trip on the parent's breakpoints. Since
2267 the parent is blocked anyway, it won't miss any breakpoint. */
2268 if (bl->pspace->breakpoints_not_allowed)
2269 return 0;
2270
2271 /* Don't insert a breakpoint if we're trying to step past its
2272 location, except if the breakpoint is a single-step breakpoint,
2273 and the breakpoint's thread is the thread which is stepping past
2274 a breakpoint. */
2275 if ((bl->loc_type == bp_loc_software_breakpoint
2276 || bl->loc_type == bp_loc_hardware_breakpoint)
2277 && stepping_past_instruction_at (bl->pspace->aspace,
2278 bl->address)
2279 /* The single-step breakpoint may be inserted at the location
2280 we're trying to step if the instruction branches to itself.
2281 However, the instruction won't be executed at all and it may
2282 break the semantics of the instruction, for example, the
2283 instruction is a conditional branch or updates some flags.
2284 We can't fix it unless GDB is able to emulate the instruction
2285 or switch to displaced stepping. */
2286 && !(bl->owner->type == bp_single_step
2287 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2288 {
2289 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2290 paddress (bl->gdbarch, bl->address));
2291 return 0;
2292 }
2293
2294 /* Don't insert watchpoints if we're trying to step past the
2295 instruction that triggered one. */
2296 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2297 && stepping_past_nonsteppable_watchpoint ())
2298 {
2299 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2300 "skipping watchpoint at %s:%d",
2301 paddress (bl->gdbarch, bl->address), bl->length);
2302 return 0;
2303 }
2304
2305 return 1;
2306 }
2307
2308 /* Same as should_be_inserted but does the check assuming
2309 that the location is not duplicated. */
2310
2311 static int
2312 unduplicated_should_be_inserted (struct bp_location *bl)
2313 {
2314 int result;
2315 const int save_duplicate = bl->duplicate;
2316
2317 bl->duplicate = 0;
2318 result = should_be_inserted (bl);
2319 bl->duplicate = save_duplicate;
2320 return result;
2321 }
2322
2323 /* Parses a conditional described by an expression COND into an
2324 agent expression bytecode suitable for evaluation
2325 by the bytecode interpreter. Return NULL if there was
2326 any error during parsing. */
2327
2328 static agent_expr_up
2329 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2330 {
2331 if (cond == NULL)
2332 return NULL;
2333
2334 agent_expr_up aexpr;
2335
2336 /* We don't want to stop processing, so catch any errors
2337 that may show up. */
2338 try
2339 {
2340 aexpr = gen_eval_for_expr (scope, cond);
2341 }
2342
2343 catch (const gdb_exception_error &ex)
2344 {
2345 /* If we got here, it means the condition could not be parsed to a valid
2346 bytecode expression and thus can't be evaluated on the target's side.
2347 It's no use iterating through the conditions. */
2348 }
2349
2350 /* We have a valid agent expression. */
2351 return aexpr;
2352 }
2353
2354 /* Based on location BL, create a list of breakpoint conditions to be
2355 passed on to the target. If we have duplicated locations with different
2356 conditions, we will add such conditions to the list. The idea is that the
2357 target will evaluate the list of conditions and will only notify GDB when
2358 one of them is true. */
2359
2360 static void
2361 build_target_condition_list (struct bp_location *bl)
2362 {
2363 int null_condition_or_parse_error = 0;
2364 int modified = bl->needs_update;
2365
2366 /* Release conditions left over from a previous insert. */
2367 bl->target_info.conditions.clear ();
2368
2369 /* This is only meaningful if the target is
2370 evaluating conditions and if the user has
2371 opted for condition evaluation on the target's
2372 side. */
2373 if (gdb_evaluates_breakpoint_condition_p ()
2374 || !target_supports_evaluation_of_breakpoint_conditions ())
2375 return;
2376
2377 auto loc_range = all_bp_locations_at_addr (bl->address);
2378
2379 /* Do a first pass to check for locations with no assigned
2380 conditions or conditions that fail to parse to a valid agent
2381 expression bytecode. If any of these happen, then it's no use to
2382 send conditions to the target since this location will always
2383 trigger and generate a response back to GDB. Note we consider
2384 all locations at the same address irrespective of type, i.e.,
2385 even if the locations aren't considered duplicates (e.g.,
2386 software breakpoint and hardware breakpoint at the same
2387 address). */
2388 for (bp_location *loc : loc_range)
2389 {
2390 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2391 {
2392 if (modified)
2393 {
2394 /* Re-parse the conditions since something changed. In that
2395 case we already freed the condition bytecodes (see
2396 force_breakpoint_reinsertion). We just
2397 need to parse the condition to bytecodes again. */
2398 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2399 loc->cond.get ());
2400 }
2401
2402 /* If we have a NULL bytecode expression, it means something
2403 went wrong or we have a null condition expression. */
2404 if (!loc->cond_bytecode)
2405 {
2406 null_condition_or_parse_error = 1;
2407 break;
2408 }
2409 }
2410 }
2411
2412 /* If any of these happened, it means we will have to evaluate the conditions
2413 for the location's address on gdb's side. It is no use keeping bytecodes
2414 for all the other duplicate locations, thus we free all of them here.
2415
2416 This is so we have a finer control over which locations' conditions are
2417 being evaluated by GDB or the remote stub. */
2418 if (null_condition_or_parse_error)
2419 {
2420 for (bp_location *loc : loc_range)
2421 {
2422 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2423 {
2424 /* Only go as far as the first NULL bytecode is
2425 located. */
2426 if (!loc->cond_bytecode)
2427 return;
2428
2429 loc->cond_bytecode.reset ();
2430 }
2431 }
2432 }
2433
2434 /* No NULL conditions or failed bytecode generation. Build a
2435 condition list for this location's address. If we have software
2436 and hardware locations at the same address, they aren't
2437 considered duplicates, but we still marge all the conditions
2438 anyway, as it's simpler, and doesn't really make a practical
2439 difference. */
2440 for (bp_location *loc : loc_range)
2441 if (loc->cond
2442 && is_breakpoint (loc->owner)
2443 && loc->pspace->num == bl->pspace->num
2444 && loc->owner->enable_state == bp_enabled
2445 && loc->enabled
2446 && !loc->disabled_by_cond)
2447 {
2448 /* Add the condition to the vector. This will be used later
2449 to send the conditions to the target. */
2450 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2451 }
2452
2453 return;
2454 }
2455
2456 /* Parses a command described by string CMD into an agent expression
2457 bytecode suitable for evaluation by the bytecode interpreter.
2458 Return NULL if there was any error during parsing. */
2459
2460 static agent_expr_up
2461 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2462 {
2463 const char *cmdrest;
2464 const char *format_start, *format_end;
2465 struct gdbarch *gdbarch = get_current_arch ();
2466
2467 if (cmd == NULL)
2468 return NULL;
2469
2470 cmdrest = cmd;
2471
2472 if (*cmdrest == ',')
2473 ++cmdrest;
2474 cmdrest = skip_spaces (cmdrest);
2475
2476 if (*cmdrest++ != '"')
2477 error (_("No format string following the location"));
2478
2479 format_start = cmdrest;
2480
2481 format_pieces fpieces (&cmdrest);
2482
2483 format_end = cmdrest;
2484
2485 if (*cmdrest++ != '"')
2486 error (_("Bad format string, non-terminated '\"'."));
2487
2488 cmdrest = skip_spaces (cmdrest);
2489
2490 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2491 error (_("Invalid argument syntax"));
2492
2493 if (*cmdrest == ',')
2494 cmdrest++;
2495 cmdrest = skip_spaces (cmdrest);
2496
2497 /* For each argument, make an expression. */
2498
2499 std::vector<struct expression *> argvec;
2500 while (*cmdrest != '\0')
2501 {
2502 const char *cmd1;
2503
2504 cmd1 = cmdrest;
2505 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2506 argvec.push_back (expr.release ());
2507 cmdrest = cmd1;
2508 if (*cmdrest == ',')
2509 ++cmdrest;
2510 }
2511
2512 agent_expr_up aexpr;
2513
2514 /* We don't want to stop processing, so catch any errors
2515 that may show up. */
2516 try
2517 {
2518 aexpr = gen_printf (scope, gdbarch, 0, 0,
2519 format_start, format_end - format_start,
2520 argvec.size (), argvec.data ());
2521 }
2522 catch (const gdb_exception_error &ex)
2523 {
2524 /* If we got here, it means the command could not be parsed to a valid
2525 bytecode expression and thus can't be evaluated on the target's side.
2526 It's no use iterating through the other commands. */
2527 }
2528
2529 /* We have a valid agent expression, return it. */
2530 return aexpr;
2531 }
2532
2533 /* Based on location BL, create a list of breakpoint commands to be
2534 passed on to the target. If we have duplicated locations with
2535 different commands, we will add any such to the list. */
2536
2537 static void
2538 build_target_command_list (struct bp_location *bl)
2539 {
2540 int null_command_or_parse_error = 0;
2541 int modified = bl->needs_update;
2542
2543 /* Clear commands left over from a previous insert. */
2544 bl->target_info.tcommands.clear ();
2545
2546 if (!target_can_run_breakpoint_commands ())
2547 return;
2548
2549 /* For now, limit to agent-style dprintf breakpoints. */
2550 if (dprintf_style != dprintf_style_agent)
2551 return;
2552
2553 auto loc_range = all_bp_locations_at_addr (bl->address);
2554
2555 /* For now, if we have any location at the same address that isn't a
2556 dprintf, don't install the target-side commands, as that would
2557 make the breakpoint not be reported to the core, and we'd lose
2558 control. */
2559 for (bp_location *loc : loc_range)
2560 if (is_breakpoint (loc->owner)
2561 && loc->pspace->num == bl->pspace->num
2562 && loc->owner->type != bp_dprintf)
2563 return;
2564
2565 /* Do a first pass to check for locations with no assigned
2566 conditions or conditions that fail to parse to a valid agent expression
2567 bytecode. If any of these happen, then it's no use to send conditions
2568 to the target since this location will always trigger and generate a
2569 response back to GDB. */
2570 for (bp_location *loc : loc_range)
2571 {
2572 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2573 {
2574 if (modified)
2575 {
2576 /* Re-parse the commands since something changed. In that
2577 case we already freed the command bytecodes (see
2578 force_breakpoint_reinsertion). We just
2579 need to parse the command to bytecodes again. */
2580 loc->cmd_bytecode
2581 = parse_cmd_to_aexpr (bl->address,
2582 loc->owner->extra_string.get ());
2583 }
2584
2585 /* If we have a NULL bytecode expression, it means something
2586 went wrong or we have a null command expression. */
2587 if (!loc->cmd_bytecode)
2588 {
2589 null_command_or_parse_error = 1;
2590 break;
2591 }
2592 }
2593 }
2594
2595 /* If anything failed, then we're not doing target-side commands,
2596 and so clean up. */
2597 if (null_command_or_parse_error)
2598 {
2599 for (bp_location *loc : loc_range)
2600 if (is_breakpoint (loc->owner)
2601 && loc->pspace->num == bl->pspace->num)
2602 {
2603 /* Only go as far as the first NULL bytecode is
2604 located. */
2605 if (loc->cmd_bytecode == NULL)
2606 return;
2607
2608 loc->cmd_bytecode.reset ();
2609 }
2610 }
2611
2612 /* No NULL commands or failed bytecode generation. Build a command
2613 list for all duplicate locations at this location's address.
2614 Note that here we must care for whether the breakpoint location
2615 types are considered duplicates, otherwise, say, if we have a
2616 software and hardware location at the same address, the target
2617 could end up running the commands twice. For the moment, we only
2618 support targets-side commands with dprintf, but it doesn't hurt
2619 to be pedantically correct in case that changes. */
2620 for (bp_location *loc : loc_range)
2621 if (breakpoint_locations_match (bl, loc)
2622 && loc->owner->extra_string
2623 && is_breakpoint (loc->owner)
2624 && loc->pspace->num == bl->pspace->num
2625 && loc->owner->enable_state == bp_enabled
2626 && loc->enabled
2627 && !loc->disabled_by_cond)
2628 {
2629 /* Add the command to the vector. This will be used later
2630 to send the commands to the target. */
2631 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2632 }
2633
2634 bl->target_info.persist = 0;
2635 /* Maybe flag this location as persistent. */
2636 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2637 bl->target_info.persist = 1;
2638 }
2639
2640 /* Return the kind of breakpoint on address *ADDR. Get the kind
2641 of breakpoint according to ADDR except single-step breakpoint.
2642 Get the kind of single-step breakpoint according to the current
2643 registers state. */
2644
2645 static int
2646 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2647 {
2648 if (bl->owner->type == bp_single_step)
2649 {
2650 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2651 struct regcache *regcache;
2652
2653 regcache = get_thread_regcache (thr);
2654
2655 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2656 regcache, addr);
2657 }
2658 else
2659 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2660 }
2661
2662 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2663 location. Any error messages are printed to TMP_ERROR_STREAM; and
2664 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2665 Returns 0 for success, 1 if the bp_location type is not supported or
2666 -1 for failure.
2667
2668 NOTE drow/2003-09-09: This routine could be broken down to an
2669 object-style method for each breakpoint or catchpoint type. */
2670 static int
2671 insert_bp_location (struct bp_location *bl,
2672 struct ui_file *tmp_error_stream,
2673 int *disabled_breaks,
2674 int *hw_breakpoint_error,
2675 int *hw_bp_error_explained_already)
2676 {
2677 gdb_exception bp_excpt;
2678
2679 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2680 return 0;
2681
2682 /* Note we don't initialize bl->target_info, as that wipes out
2683 the breakpoint location's shadow_contents if the breakpoint
2684 is still inserted at that location. This in turn breaks
2685 target_read_memory which depends on these buffers when
2686 a memory read is requested at the breakpoint location:
2687 Once the target_info has been wiped, we fail to see that
2688 we have a breakpoint inserted at that address and thus
2689 read the breakpoint instead of returning the data saved in
2690 the breakpoint location's shadow contents. */
2691 bl->target_info.reqstd_address = bl->address;
2692 bl->target_info.placed_address_space = bl->pspace->aspace;
2693 bl->target_info.length = bl->length;
2694
2695 /* When working with target-side conditions, we must pass all the conditions
2696 for the same breakpoint address down to the target since GDB will not
2697 insert those locations. With a list of breakpoint conditions, the target
2698 can decide when to stop and notify GDB. */
2699
2700 if (is_breakpoint (bl->owner))
2701 {
2702 build_target_condition_list (bl);
2703 build_target_command_list (bl);
2704 /* Reset the modification marker. */
2705 bl->needs_update = 0;
2706 }
2707
2708 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2709 set at a read-only address, then a breakpoint location will have
2710 been changed to hardware breakpoint before we get here. If it is
2711 "off" however, error out before actually trying to insert the
2712 breakpoint, with a nicer error message. */
2713 if (bl->loc_type == bp_loc_software_breakpoint
2714 && !automatic_hardware_breakpoints)
2715 {
2716 mem_region *mr = lookup_mem_region (bl->address);
2717
2718 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2719 {
2720 gdb_printf (tmp_error_stream,
2721 _("Cannot insert breakpoint %d.\n"
2722 "Cannot set software breakpoint "
2723 "at read-only address %s\n"),
2724 bl->owner->number,
2725 paddress (bl->gdbarch, bl->address));
2726 return 1;
2727 }
2728 }
2729
2730 if (bl->loc_type == bp_loc_software_breakpoint
2731 || bl->loc_type == bp_loc_hardware_breakpoint)
2732 {
2733 /* First check to see if we have to handle an overlay. */
2734 if (overlay_debugging == ovly_off
2735 || bl->section == NULL
2736 || !(section_is_overlay (bl->section)))
2737 {
2738 /* No overlay handling: just set the breakpoint. */
2739 try
2740 {
2741 int val;
2742
2743 val = bl->owner->insert_location (bl);
2744 if (val)
2745 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2746 }
2747 catch (gdb_exception &e)
2748 {
2749 bp_excpt = std::move (e);
2750 }
2751 }
2752 else
2753 {
2754 /* This breakpoint is in an overlay section.
2755 Shall we set a breakpoint at the LMA? */
2756 if (!overlay_events_enabled)
2757 {
2758 /* Yes -- overlay event support is not active,
2759 so we must try to set a breakpoint at the LMA.
2760 This will not work for a hardware breakpoint. */
2761 if (bl->loc_type == bp_loc_hardware_breakpoint)
2762 warning (_("hardware breakpoint %d not supported in overlay!"),
2763 bl->owner->number);
2764 else
2765 {
2766 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2767 bl->section);
2768 /* Set a software (trap) breakpoint at the LMA. */
2769 bl->overlay_target_info = bl->target_info;
2770 bl->overlay_target_info.reqstd_address = addr;
2771
2772 /* No overlay handling: just set the breakpoint. */
2773 try
2774 {
2775 int val;
2776
2777 bl->overlay_target_info.kind
2778 = breakpoint_kind (bl, &addr);
2779 bl->overlay_target_info.placed_address = addr;
2780 val = target_insert_breakpoint (bl->gdbarch,
2781 &bl->overlay_target_info);
2782 if (val)
2783 bp_excpt
2784 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2785 }
2786 catch (gdb_exception &e)
2787 {
2788 bp_excpt = std::move (e);
2789 }
2790
2791 if (bp_excpt.reason != 0)
2792 gdb_printf (tmp_error_stream,
2793 "Overlay breakpoint %d "
2794 "failed: in ROM?\n",
2795 bl->owner->number);
2796 }
2797 }
2798 /* Shall we set a breakpoint at the VMA? */
2799 if (section_is_mapped (bl->section))
2800 {
2801 /* Yes. This overlay section is mapped into memory. */
2802 try
2803 {
2804 int val;
2805
2806 val = bl->owner->insert_location (bl);
2807 if (val)
2808 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2809 }
2810 catch (gdb_exception &e)
2811 {
2812 bp_excpt = std::move (e);
2813 }
2814 }
2815 else
2816 {
2817 /* No. This breakpoint will not be inserted.
2818 No error, but do not mark the bp as 'inserted'. */
2819 return 0;
2820 }
2821 }
2822
2823 if (bp_excpt.reason != 0)
2824 {
2825 /* Can't set the breakpoint. */
2826
2827 /* If the target has closed then it will have deleted any
2828 breakpoints inserted within the target inferior, as a result
2829 any further attempts to interact with the breakpoint objects
2830 is not possible. Just rethrow the error. */
2831 if (bp_excpt.error == TARGET_CLOSE_ERROR)
2832 throw bp_excpt;
2833 gdb_assert (bl->owner != nullptr);
2834
2835 /* In some cases, we might not be able to insert a
2836 breakpoint in a shared library that has already been
2837 removed, but we have not yet processed the shlib unload
2838 event. Unfortunately, some targets that implement
2839 breakpoint insertion themselves can't tell why the
2840 breakpoint insertion failed (e.g., the remote target
2841 doesn't define error codes), so we must treat generic
2842 errors as memory errors. */
2843 if (bp_excpt.reason == RETURN_ERROR
2844 && (bp_excpt.error == GENERIC_ERROR
2845 || bp_excpt.error == MEMORY_ERROR)
2846 && bl->loc_type == bp_loc_software_breakpoint
2847 && (solib_name_from_address (bl->pspace, bl->address)
2848 || shared_objfile_contains_address_p (bl->pspace,
2849 bl->address)))
2850 {
2851 /* See also: disable_breakpoints_in_shlibs. */
2852 bl->shlib_disabled = 1;
2853 gdb::observers::breakpoint_modified.notify (bl->owner);
2854 if (!*disabled_breaks)
2855 {
2856 gdb_printf (tmp_error_stream,
2857 "Cannot insert breakpoint %d.\n",
2858 bl->owner->number);
2859 gdb_printf (tmp_error_stream,
2860 "Temporarily disabling shared "
2861 "library breakpoints:\n");
2862 }
2863 *disabled_breaks = 1;
2864 gdb_printf (tmp_error_stream,
2865 "breakpoint #%d\n", bl->owner->number);
2866 return 0;
2867 }
2868 else
2869 {
2870 if (bl->loc_type == bp_loc_hardware_breakpoint)
2871 {
2872 *hw_breakpoint_error = 1;
2873 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2874 gdb_printf (tmp_error_stream,
2875 "Cannot insert hardware breakpoint %d%s",
2876 bl->owner->number,
2877 bp_excpt.message ? ":" : ".\n");
2878 if (bp_excpt.message != NULL)
2879 gdb_printf (tmp_error_stream, "%s.\n",
2880 bp_excpt.what ());
2881 }
2882 else
2883 {
2884 if (bp_excpt.message == NULL)
2885 {
2886 std::string message
2887 = memory_error_message (TARGET_XFER_E_IO,
2888 bl->gdbarch, bl->address);
2889
2890 gdb_printf (tmp_error_stream,
2891 "Cannot insert breakpoint %d.\n"
2892 "%s\n",
2893 bl->owner->number, message.c_str ());
2894 }
2895 else
2896 {
2897 gdb_printf (tmp_error_stream,
2898 "Cannot insert breakpoint %d: %s\n",
2899 bl->owner->number,
2900 bp_excpt.what ());
2901 }
2902 }
2903 return 1;
2904
2905 }
2906 }
2907 else
2908 bl->inserted = 1;
2909
2910 return 0;
2911 }
2912
2913 else if (bl->loc_type == bp_loc_hardware_watchpoint
2914 /* NOTE drow/2003-09-08: This state only exists for removing
2915 watchpoints. It's not clear that it's necessary... */
2916 && bl->owner->disposition != disp_del_at_next_stop)
2917 {
2918 int val;
2919
2920 val = bl->owner->insert_location (bl);
2921
2922 /* If trying to set a read-watchpoint, and it turns out it's not
2923 supported, try emulating one with an access watchpoint. */
2924 if (val == 1 && bl->watchpoint_type == hw_read)
2925 {
2926 /* But don't try to insert it, if there's already another
2927 hw_access location that would be considered a duplicate
2928 of this one. */
2929 for (bp_location *loc : all_bp_locations ())
2930 if (loc != bl
2931 && loc->watchpoint_type == hw_access
2932 && watchpoint_locations_match (bl, loc))
2933 {
2934 bl->duplicate = 1;
2935 bl->inserted = 1;
2936 bl->target_info = loc->target_info;
2937 bl->watchpoint_type = hw_access;
2938 val = 0;
2939 break;
2940 }
2941
2942 if (val == 1)
2943 {
2944 bl->watchpoint_type = hw_access;
2945 val = bl->owner->insert_location (bl);
2946
2947 if (val)
2948 /* Back to the original value. */
2949 bl->watchpoint_type = hw_read;
2950 }
2951 }
2952
2953 bl->inserted = (val == 0);
2954 }
2955
2956 else if (bl->owner->type == bp_catchpoint)
2957 {
2958 int val;
2959
2960 val = bl->owner->insert_location (bl);
2961 if (val)
2962 {
2963 bl->owner->enable_state = bp_disabled;
2964
2965 if (val == 1)
2966 warning (_("\
2967 Error inserting catchpoint %d: Your system does not support this type\n\
2968 of catchpoint."), bl->owner->number);
2969 else
2970 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2971 }
2972
2973 bl->inserted = (val == 0);
2974
2975 /* We've already printed an error message if there was a problem
2976 inserting this catchpoint, and we've disabled the catchpoint,
2977 so just return success. */
2978 return 0;
2979 }
2980
2981 return 0;
2982 }
2983
2984 /* This function is called when program space PSPACE is about to be
2985 deleted. It takes care of updating breakpoints to not reference
2986 PSPACE anymore. */
2987
2988 void
2989 breakpoint_program_space_exit (struct program_space *pspace)
2990 {
2991 /* Remove any breakpoint that was set through this program space. */
2992 for (breakpoint *b : all_breakpoints_safe ())
2993 if (b->pspace == pspace)
2994 delete_breakpoint (b);
2995
2996 /* Breakpoints set through other program spaces could have locations
2997 bound to PSPACE as well. Remove those. */
2998 for (bp_location *loc : all_bp_locations ())
2999 {
3000 struct bp_location *tmp;
3001
3002 if (loc->pspace == pspace)
3003 {
3004 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3005 if (loc->owner->loc == loc)
3006 loc->owner->loc = loc->next;
3007 else
3008 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3009 if (tmp->next == loc)
3010 {
3011 tmp->next = loc->next;
3012 break;
3013 }
3014 }
3015 }
3016
3017 /* Now update the global location list to permanently delete the
3018 removed locations above. */
3019 update_global_location_list (UGLL_DONT_INSERT);
3020 }
3021
3022 /* Make sure all breakpoints are inserted in inferior.
3023 Throws exception on any error.
3024 A breakpoint that is already inserted won't be inserted
3025 again, so calling this function twice is safe. */
3026 void
3027 insert_breakpoints (void)
3028 {
3029 for (breakpoint *bpt : all_breakpoints ())
3030 if (is_hardware_watchpoint (bpt))
3031 {
3032 struct watchpoint *w = (struct watchpoint *) bpt;
3033
3034 update_watchpoint (w, 0 /* don't reparse. */);
3035 }
3036
3037 /* Updating watchpoints creates new locations, so update the global
3038 location list. Explicitly tell ugll to insert locations and
3039 ignore breakpoints_always_inserted_mode. Also,
3040 update_global_location_list tries to "upgrade" software
3041 breakpoints to hardware breakpoints to handle "set breakpoint
3042 auto-hw", so we need to call it even if we don't have new
3043 locations. */
3044 update_global_location_list (UGLL_INSERT);
3045 }
3046
3047 /* This is used when we need to synch breakpoint conditions between GDB and the
3048 target. It is the case with deleting and disabling of breakpoints when using
3049 always-inserted mode. */
3050
3051 static void
3052 update_inserted_breakpoint_locations (void)
3053 {
3054 int error_flag = 0;
3055 int val = 0;
3056 int disabled_breaks = 0;
3057 int hw_breakpoint_error = 0;
3058 int hw_bp_details_reported = 0;
3059
3060 string_file tmp_error_stream;
3061
3062 /* Explicitly mark the warning -- this will only be printed if
3063 there was an error. */
3064 tmp_error_stream.puts ("Warning:\n");
3065
3066 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3067
3068 for (bp_location *bl : all_bp_locations ())
3069 {
3070 /* We only want to update software breakpoints and hardware
3071 breakpoints. */
3072 if (!is_breakpoint (bl->owner))
3073 continue;
3074
3075 /* We only want to update locations that are already inserted
3076 and need updating. This is to avoid unwanted insertion during
3077 deletion of breakpoints. */
3078 if (!bl->inserted || !bl->needs_update)
3079 continue;
3080
3081 switch_to_program_space_and_thread (bl->pspace);
3082
3083 /* For targets that support global breakpoints, there's no need
3084 to select an inferior to insert breakpoint to. In fact, even
3085 if we aren't attached to any process yet, we should still
3086 insert breakpoints. */
3087 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3088 && (inferior_ptid == null_ptid || !target_has_execution ()))
3089 continue;
3090
3091 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3092 &hw_breakpoint_error, &hw_bp_details_reported);
3093 if (val)
3094 error_flag = val;
3095 }
3096
3097 if (error_flag)
3098 {
3099 target_terminal::ours_for_output ();
3100 error_stream (tmp_error_stream);
3101 }
3102 }
3103
3104 /* Used when starting or continuing the program. */
3105
3106 static void
3107 insert_breakpoint_locations (void)
3108 {
3109 int error_flag = 0;
3110 int val = 0;
3111 int disabled_breaks = 0;
3112 int hw_breakpoint_error = 0;
3113 int hw_bp_error_explained_already = 0;
3114
3115 string_file tmp_error_stream;
3116
3117 /* Explicitly mark the warning -- this will only be printed if
3118 there was an error. */
3119 tmp_error_stream.puts ("Warning:\n");
3120
3121 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3122
3123 for (bp_location *bl : all_bp_locations ())
3124 {
3125 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3126 continue;
3127
3128 /* There is no point inserting thread-specific breakpoints if
3129 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3130 has BL->OWNER always non-NULL. */
3131 if (bl->owner->thread != -1
3132 && !valid_global_thread_id (bl->owner->thread))
3133 continue;
3134
3135 switch_to_program_space_and_thread (bl->pspace);
3136
3137 /* For targets that support global breakpoints, there's no need
3138 to select an inferior to insert breakpoint to. In fact, even
3139 if we aren't attached to any process yet, we should still
3140 insert breakpoints. */
3141 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3142 && (inferior_ptid == null_ptid || !target_has_execution ()))
3143 continue;
3144
3145 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3146 &hw_breakpoint_error, &hw_bp_error_explained_already);
3147 if (val)
3148 error_flag = val;
3149 }
3150
3151 /* If we failed to insert all locations of a watchpoint, remove
3152 them, as half-inserted watchpoint is of limited use. */
3153 for (breakpoint *bpt : all_breakpoints ())
3154 {
3155 int some_failed = 0;
3156
3157 if (!is_hardware_watchpoint (bpt))
3158 continue;
3159
3160 if (!breakpoint_enabled (bpt))
3161 continue;
3162
3163 if (bpt->disposition == disp_del_at_next_stop)
3164 continue;
3165
3166 for (bp_location *loc : bpt->locations ())
3167 if (!loc->inserted && should_be_inserted (loc))
3168 {
3169 some_failed = 1;
3170 break;
3171 }
3172
3173 if (some_failed)
3174 {
3175 for (bp_location *loc : bpt->locations ())
3176 if (loc->inserted)
3177 remove_breakpoint (loc);
3178
3179 hw_breakpoint_error = 1;
3180 tmp_error_stream.printf ("Could not insert "
3181 "hardware watchpoint %d.\n",
3182 bpt->number);
3183 error_flag = -1;
3184 }
3185 }
3186
3187 if (error_flag)
3188 {
3189 /* If a hardware breakpoint or watchpoint was inserted, add a
3190 message about possibly exhausted resources. */
3191 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3192 {
3193 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3194 You may have requested too many hardware breakpoints/watchpoints.\n");
3195 }
3196 target_terminal::ours_for_output ();
3197 error_stream (tmp_error_stream);
3198 }
3199 }
3200
3201 /* Used when the program stops.
3202 Returns zero if successful, or non-zero if there was a problem
3203 removing a breakpoint location. */
3204
3205 int
3206 remove_breakpoints (void)
3207 {
3208 int val = 0;
3209
3210 for (bp_location *bl : all_bp_locations ())
3211 if (bl->inserted && !is_tracepoint (bl->owner))
3212 val |= remove_breakpoint (bl);
3213
3214 return val;
3215 }
3216
3217 /* When a thread exits, remove breakpoints that are related to
3218 that thread. */
3219
3220 static void
3221 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3222 {
3223 for (breakpoint *b : all_breakpoints_safe ())
3224 {
3225 if (b->thread == tp->global_num && user_breakpoint_p (b))
3226 {
3227 b->disposition = disp_del_at_next_stop;
3228
3229 gdb_printf (_("\
3230 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3231 b->number, print_thread_id (tp));
3232
3233 /* Hide it from the user. */
3234 b->number = 0;
3235 }
3236 }
3237 }
3238
3239 /* See breakpoint.h. */
3240
3241 void
3242 remove_breakpoints_inf (inferior *inf)
3243 {
3244 int val;
3245
3246 for (bp_location *bl : all_bp_locations ())
3247 {
3248 if (bl->pspace != inf->pspace)
3249 continue;
3250
3251 if (bl->inserted && !bl->target_info.persist)
3252 {
3253 val = remove_breakpoint (bl);
3254 if (val != 0)
3255 return;
3256 }
3257 }
3258 }
3259
3260 static int internal_breakpoint_number = -1;
3261
3262 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3263 If INTERNAL is non-zero, the breakpoint number will be populated
3264 from internal_breakpoint_number and that variable decremented.
3265 Otherwise the breakpoint number will be populated from
3266 breakpoint_count and that value incremented. Internal breakpoints
3267 do not set the internal var bpnum. */
3268 static void
3269 set_breakpoint_number (int internal, struct breakpoint *b)
3270 {
3271 if (internal)
3272 b->number = internal_breakpoint_number--;
3273 else
3274 {
3275 set_breakpoint_count (breakpoint_count + 1);
3276 b->number = breakpoint_count;
3277 }
3278 }
3279
3280 static struct breakpoint *
3281 create_internal_breakpoint (struct gdbarch *gdbarch,
3282 CORE_ADDR address, enum bptype type)
3283 {
3284 std::unique_ptr<internal_breakpoint> b
3285 (new internal_breakpoint (gdbarch, type, address));
3286
3287 b->number = internal_breakpoint_number--;
3288
3289 return add_to_breakpoint_chain (std::move (b));
3290 }
3291
3292 static const char *const longjmp_names[] =
3293 {
3294 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3295 };
3296 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3297
3298 /* Per-objfile data private to breakpoint.c. */
3299 struct breakpoint_objfile_data
3300 {
3301 /* Minimal symbol for "_ovly_debug_event" (if any). */
3302 struct bound_minimal_symbol overlay_msym;
3303
3304 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3305 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3306
3307 /* True if we have looked for longjmp probes. */
3308 int longjmp_searched = 0;
3309
3310 /* SystemTap probe points for longjmp (if any). These are non-owning
3311 references. */
3312 std::vector<probe *> longjmp_probes;
3313
3314 /* Minimal symbol for "std::terminate()" (if any). */
3315 struct bound_minimal_symbol terminate_msym;
3316
3317 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3318 struct bound_minimal_symbol exception_msym;
3319
3320 /* True if we have looked for exception probes. */
3321 int exception_searched = 0;
3322
3323 /* SystemTap probe points for unwinding (if any). These are non-owning
3324 references. */
3325 std::vector<probe *> exception_probes;
3326 };
3327
3328 static const registry<objfile>::key<breakpoint_objfile_data>
3329 breakpoint_objfile_key;
3330
3331 /* Minimal symbol not found sentinel. */
3332 static struct minimal_symbol msym_not_found;
3333
3334 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3335
3336 static int
3337 msym_not_found_p (const struct minimal_symbol *msym)
3338 {
3339 return msym == &msym_not_found;
3340 }
3341
3342 /* Return per-objfile data needed by breakpoint.c.
3343 Allocate the data if necessary. */
3344
3345 static struct breakpoint_objfile_data *
3346 get_breakpoint_objfile_data (struct objfile *objfile)
3347 {
3348 struct breakpoint_objfile_data *bp_objfile_data;
3349
3350 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3351 if (bp_objfile_data == NULL)
3352 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3353 return bp_objfile_data;
3354 }
3355
3356 static void
3357 create_overlay_event_breakpoint (void)
3358 {
3359 const char *const func_name = "_ovly_debug_event";
3360
3361 for (objfile *objfile : current_program_space->objfiles ())
3362 {
3363 struct breakpoint *b;
3364 struct breakpoint_objfile_data *bp_objfile_data;
3365 CORE_ADDR addr;
3366
3367 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3368
3369 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3370 continue;
3371
3372 if (bp_objfile_data->overlay_msym.minsym == NULL)
3373 {
3374 struct bound_minimal_symbol m;
3375
3376 m = lookup_minimal_symbol_text (func_name, objfile);
3377 if (m.minsym == NULL)
3378 {
3379 /* Avoid future lookups in this objfile. */
3380 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3381 continue;
3382 }
3383 bp_objfile_data->overlay_msym = m;
3384 }
3385
3386 addr = bp_objfile_data->overlay_msym.value_address ();
3387 b = create_internal_breakpoint (objfile->arch (), addr,
3388 bp_overlay_event);
3389 b->locspec = new_explicit_location_spec_function (func_name);
3390
3391 if (overlay_debugging == ovly_auto)
3392 {
3393 b->enable_state = bp_enabled;
3394 overlay_events_enabled = 1;
3395 }
3396 else
3397 {
3398 b->enable_state = bp_disabled;
3399 overlay_events_enabled = 0;
3400 }
3401 }
3402 }
3403
3404 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3405 true if a breakpoint was installed. */
3406
3407 static bool
3408 create_longjmp_master_breakpoint_probe (objfile *objfile)
3409 {
3410 struct gdbarch *gdbarch = objfile->arch ();
3411 struct breakpoint_objfile_data *bp_objfile_data
3412 = get_breakpoint_objfile_data (objfile);
3413
3414 if (!bp_objfile_data->longjmp_searched)
3415 {
3416 std::vector<probe *> ret
3417 = find_probes_in_objfile (objfile, "libc", "longjmp");
3418
3419 if (!ret.empty ())
3420 {
3421 /* We are only interested in checking one element. */
3422 probe *p = ret[0];
3423
3424 if (!p->can_evaluate_arguments ())
3425 {
3426 /* We cannot use the probe interface here,
3427 because it does not know how to evaluate
3428 arguments. */
3429 ret.clear ();
3430 }
3431 }
3432 bp_objfile_data->longjmp_probes = ret;
3433 bp_objfile_data->longjmp_searched = 1;
3434 }
3435
3436 if (bp_objfile_data->longjmp_probes.empty ())
3437 return false;
3438
3439 for (probe *p : bp_objfile_data->longjmp_probes)
3440 {
3441 struct breakpoint *b;
3442
3443 b = create_internal_breakpoint (gdbarch,
3444 p->get_relocated_address (objfile),
3445 bp_longjmp_master);
3446 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
3447 b->enable_state = bp_disabled;
3448 }
3449
3450 return true;
3451 }
3452
3453 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3454 Return true if at least one breakpoint was installed. */
3455
3456 static bool
3457 create_longjmp_master_breakpoint_names (objfile *objfile)
3458 {
3459 struct gdbarch *gdbarch = objfile->arch ();
3460 if (!gdbarch_get_longjmp_target_p (gdbarch))
3461 return false;
3462
3463 struct breakpoint_objfile_data *bp_objfile_data
3464 = get_breakpoint_objfile_data (objfile);
3465 unsigned int installed_bp = 0;
3466
3467 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3468 {
3469 struct breakpoint *b;
3470 const char *func_name;
3471 CORE_ADDR addr;
3472
3473 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3474 continue;
3475
3476 func_name = longjmp_names[i];
3477 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3478 {
3479 struct bound_minimal_symbol m;
3480
3481 m = lookup_minimal_symbol_text (func_name, objfile);
3482 if (m.minsym == NULL)
3483 {
3484 /* Prevent future lookups in this objfile. */
3485 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3486 continue;
3487 }
3488 bp_objfile_data->longjmp_msym[i] = m;
3489 }
3490
3491 addr = bp_objfile_data->longjmp_msym[i].value_address ();
3492 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
3493 b->locspec = new_explicit_location_spec_function (func_name);
3494 b->enable_state = bp_disabled;
3495 installed_bp++;
3496 }
3497
3498 return installed_bp > 0;
3499 }
3500
3501 /* Create a master longjmp breakpoint. */
3502
3503 static void
3504 create_longjmp_master_breakpoint (void)
3505 {
3506 scoped_restore_current_program_space restore_pspace;
3507
3508 for (struct program_space *pspace : program_spaces)
3509 {
3510 set_current_program_space (pspace);
3511
3512 for (objfile *obj : current_program_space->objfiles ())
3513 {
3514 /* Skip separate debug object, it's handled in the loop below. */
3515 if (obj->separate_debug_objfile_backlink != nullptr)
3516 continue;
3517
3518 /* Try a probe kind breakpoint on main objfile. */
3519 if (create_longjmp_master_breakpoint_probe (obj))
3520 continue;
3521
3522 /* Try longjmp_names kind breakpoints on main and separate_debug
3523 objfiles. */
3524 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3525 if (create_longjmp_master_breakpoint_names (debug_objfile))
3526 break;
3527 }
3528 }
3529 }
3530
3531 /* Create a master std::terminate breakpoint. */
3532 static void
3533 create_std_terminate_master_breakpoint (void)
3534 {
3535 const char *const func_name = "std::terminate()";
3536
3537 scoped_restore_current_program_space restore_pspace;
3538
3539 for (struct program_space *pspace : program_spaces)
3540 {
3541 CORE_ADDR addr;
3542
3543 set_current_program_space (pspace);
3544
3545 for (objfile *objfile : current_program_space->objfiles ())
3546 {
3547 struct breakpoint *b;
3548 struct breakpoint_objfile_data *bp_objfile_data;
3549
3550 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3551
3552 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3553 continue;
3554
3555 if (bp_objfile_data->terminate_msym.minsym == NULL)
3556 {
3557 struct bound_minimal_symbol m;
3558
3559 m = lookup_minimal_symbol (func_name, NULL, objfile);
3560 if (m.minsym == NULL || (m.minsym->type () != mst_text
3561 && m.minsym->type () != mst_file_text))
3562 {
3563 /* Prevent future lookups in this objfile. */
3564 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3565 continue;
3566 }
3567 bp_objfile_data->terminate_msym = m;
3568 }
3569
3570 addr = bp_objfile_data->terminate_msym.value_address ();
3571 b = create_internal_breakpoint (objfile->arch (), addr,
3572 bp_std_terminate_master);
3573 b->locspec = new_explicit_location_spec_function (func_name);
3574 b->enable_state = bp_disabled;
3575 }
3576 }
3577 }
3578
3579 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3580 probe. Return true if a breakpoint was installed. */
3581
3582 static bool
3583 create_exception_master_breakpoint_probe (objfile *objfile)
3584 {
3585 struct breakpoint *b;
3586 struct gdbarch *gdbarch;
3587 struct breakpoint_objfile_data *bp_objfile_data;
3588
3589 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3590
3591 /* We prefer the SystemTap probe point if it exists. */
3592 if (!bp_objfile_data->exception_searched)
3593 {
3594 std::vector<probe *> ret
3595 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3596
3597 if (!ret.empty ())
3598 {
3599 /* We are only interested in checking one element. */
3600 probe *p = ret[0];
3601
3602 if (!p->can_evaluate_arguments ())
3603 {
3604 /* We cannot use the probe interface here, because it does
3605 not know how to evaluate arguments. */
3606 ret.clear ();
3607 }
3608 }
3609 bp_objfile_data->exception_probes = ret;
3610 bp_objfile_data->exception_searched = 1;
3611 }
3612
3613 if (bp_objfile_data->exception_probes.empty ())
3614 return false;
3615
3616 gdbarch = objfile->arch ();
3617
3618 for (probe *p : bp_objfile_data->exception_probes)
3619 {
3620 b = create_internal_breakpoint (gdbarch,
3621 p->get_relocated_address (objfile),
3622 bp_exception_master);
3623 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
3624 b->enable_state = bp_disabled;
3625 }
3626
3627 return true;
3628 }
3629
3630 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3631 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3632
3633 static bool
3634 create_exception_master_breakpoint_hook (objfile *objfile)
3635 {
3636 const char *const func_name = "_Unwind_DebugHook";
3637 struct breakpoint *b;
3638 struct gdbarch *gdbarch;
3639 struct breakpoint_objfile_data *bp_objfile_data;
3640 CORE_ADDR addr;
3641
3642 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3643
3644 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3645 return false;
3646
3647 gdbarch = objfile->arch ();
3648
3649 if (bp_objfile_data->exception_msym.minsym == NULL)
3650 {
3651 struct bound_minimal_symbol debug_hook;
3652
3653 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3654 if (debug_hook.minsym == NULL)
3655 {
3656 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3657 return false;
3658 }
3659
3660 bp_objfile_data->exception_msym = debug_hook;
3661 }
3662
3663 addr = bp_objfile_data->exception_msym.value_address ();
3664 addr = gdbarch_convert_from_func_ptr_addr
3665 (gdbarch, addr, current_inferior ()->top_target ());
3666 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
3667 b->locspec = new_explicit_location_spec_function (func_name);
3668 b->enable_state = bp_disabled;
3669
3670 return true;
3671 }
3672
3673 /* Install a master breakpoint on the unwinder's debug hook. */
3674
3675 static void
3676 create_exception_master_breakpoint (void)
3677 {
3678 for (objfile *obj : current_program_space->objfiles ())
3679 {
3680 /* Skip separate debug object. */
3681 if (obj->separate_debug_objfile_backlink)
3682 continue;
3683
3684 /* Try a probe kind breakpoint. */
3685 if (create_exception_master_breakpoint_probe (obj))
3686 continue;
3687
3688 /* Iterate over main and separate debug objects and try an
3689 _Unwind_DebugHook kind breakpoint. */
3690 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3691 if (create_exception_master_breakpoint_hook (debug_objfile))
3692 break;
3693 }
3694 }
3695
3696 /* Does B have a location spec? */
3697
3698 static int
3699 breakpoint_location_spec_empty_p (const struct breakpoint *b)
3700 {
3701 return (b->locspec != nullptr && b->locspec->empty_p ());
3702 }
3703
3704 void
3705 update_breakpoints_after_exec (void)
3706 {
3707 /* We're about to delete breakpoints from GDB's lists. If the
3708 INSERTED flag is true, GDB will try to lift the breakpoints by
3709 writing the breakpoints' "shadow contents" back into memory. The
3710 "shadow contents" are NOT valid after an exec, so GDB should not
3711 do that. Instead, the target is responsible from marking
3712 breakpoints out as soon as it detects an exec. We don't do that
3713 here instead, because there may be other attempts to delete
3714 breakpoints after detecting an exec and before reaching here. */
3715 for (bp_location *bploc : all_bp_locations ())
3716 if (bploc->pspace == current_program_space)
3717 gdb_assert (!bploc->inserted);
3718
3719 for (breakpoint *b : all_breakpoints_safe ())
3720 {
3721 if (b->pspace != current_program_space)
3722 continue;
3723
3724 /* Solib breakpoints must be explicitly reset after an exec(). */
3725 if (b->type == bp_shlib_event)
3726 {
3727 delete_breakpoint (b);
3728 continue;
3729 }
3730
3731 /* JIT breakpoints must be explicitly reset after an exec(). */
3732 if (b->type == bp_jit_event)
3733 {
3734 delete_breakpoint (b);
3735 continue;
3736 }
3737
3738 /* Thread event breakpoints must be set anew after an exec(),
3739 as must overlay event and longjmp master breakpoints. */
3740 if (b->type == bp_thread_event || b->type == bp_overlay_event
3741 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3742 || b->type == bp_exception_master)
3743 {
3744 delete_breakpoint (b);
3745 continue;
3746 }
3747
3748 /* Step-resume breakpoints are meaningless after an exec(). */
3749 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3750 {
3751 delete_breakpoint (b);
3752 continue;
3753 }
3754
3755 /* Just like single-step breakpoints. */
3756 if (b->type == bp_single_step)
3757 {
3758 delete_breakpoint (b);
3759 continue;
3760 }
3761
3762 /* Longjmp and longjmp-resume breakpoints are also meaningless
3763 after an exec. */
3764 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3765 || b->type == bp_longjmp_call_dummy
3766 || b->type == bp_exception || b->type == bp_exception_resume)
3767 {
3768 delete_breakpoint (b);
3769 continue;
3770 }
3771
3772 if (b->type == bp_catchpoint)
3773 {
3774 /* For now, none of the bp_catchpoint breakpoints need to
3775 do anything at this point. In the future, if some of
3776 the catchpoints need to something, we will need to add
3777 a new method, and call this method from here. */
3778 continue;
3779 }
3780
3781 /* bp_finish is a special case. The only way we ought to be able
3782 to see one of these when an exec() has happened, is if the user
3783 caught a vfork, and then said "finish". Ordinarily a finish just
3784 carries them to the call-site of the current callee, by setting
3785 a temporary bp there and resuming. But in this case, the finish
3786 will carry them entirely through the vfork & exec.
3787
3788 We don't want to allow a bp_finish to remain inserted now. But
3789 we can't safely delete it, 'cause finish_command has a handle to
3790 the bp on a bpstat, and will later want to delete it. There's a
3791 chance (and I've seen it happen) that if we delete the bp_finish
3792 here, that its storage will get reused by the time finish_command
3793 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3794 We really must allow finish_command to delete a bp_finish.
3795
3796 In the absence of a general solution for the "how do we know
3797 it's safe to delete something others may have handles to?"
3798 problem, what we'll do here is just uninsert the bp_finish, and
3799 let finish_command delete it.
3800
3801 (We know the bp_finish is "doomed" in the sense that it's
3802 momentary, and will be deleted as soon as finish_command sees
3803 the inferior stopped. So it doesn't matter that the bp's
3804 address is probably bogus in the new a.out, unlike e.g., the
3805 solib breakpoints.) */
3806
3807 if (b->type == bp_finish)
3808 {
3809 continue;
3810 }
3811
3812 /* Without a symbolic address, we have little hope of the
3813 pre-exec() address meaning the same thing in the post-exec()
3814 a.out. */
3815 if (breakpoint_location_spec_empty_p (b))
3816 {
3817 delete_breakpoint (b);
3818 continue;
3819 }
3820 }
3821 }
3822
3823 int
3824 detach_breakpoints (ptid_t ptid)
3825 {
3826 int val = 0;
3827 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3828 struct inferior *inf = current_inferior ();
3829
3830 if (ptid.pid () == inferior_ptid.pid ())
3831 error (_("Cannot detach breakpoints of inferior_ptid"));
3832
3833 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3834 inferior_ptid = ptid;
3835 for (bp_location *bl : all_bp_locations ())
3836 {
3837 if (bl->pspace != inf->pspace)
3838 continue;
3839
3840 /* This function must physically remove breakpoints locations
3841 from the specified ptid, without modifying the breakpoint
3842 package's state. Locations of type bp_loc_other and
3843 bp_loc_software_watchpoint are only maintained at GDB side,
3844 so there is no need to remove them. Moreover, removing these
3845 would modify the breakpoint package's state. */
3846 if (bl->loc_type == bp_loc_other
3847 || bl->loc_type == bp_loc_software_watchpoint)
3848 continue;
3849
3850 if (bl->inserted)
3851 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3852 }
3853
3854 return val;
3855 }
3856
3857 /* Remove the breakpoint location BL from the current address space.
3858 Note that this is used to detach breakpoints from a child fork.
3859 When we get here, the child isn't in the inferior list, and neither
3860 do we have objects to represent its address space --- we should
3861 *not* look at bl->pspace->aspace here. */
3862
3863 static int
3864 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3865 {
3866 int val;
3867
3868 /* BL is never in moribund_locations by our callers. */
3869 gdb_assert (bl->owner != NULL);
3870
3871 /* The type of none suggests that owner is actually deleted.
3872 This should not ever happen. */
3873 gdb_assert (bl->owner->type != bp_none);
3874
3875 if (bl->loc_type == bp_loc_software_breakpoint
3876 || bl->loc_type == bp_loc_hardware_breakpoint)
3877 {
3878 /* "Normal" instruction breakpoint: either the standard
3879 trap-instruction bp (bp_breakpoint), or a
3880 bp_hardware_breakpoint. */
3881
3882 /* First check to see if we have to handle an overlay. */
3883 if (overlay_debugging == ovly_off
3884 || bl->section == NULL
3885 || !(section_is_overlay (bl->section)))
3886 {
3887 /* No overlay handling: just remove the breakpoint. */
3888
3889 /* If we're trying to uninsert a memory breakpoint that we
3890 know is set in a dynamic object that is marked
3891 shlib_disabled, then either the dynamic object was
3892 removed with "remove-symbol-file" or with
3893 "nosharedlibrary". In the former case, we don't know
3894 whether another dynamic object might have loaded over the
3895 breakpoint's address -- the user might well let us know
3896 about it next with add-symbol-file (the whole point of
3897 add-symbol-file is letting the user manually maintain a
3898 list of dynamically loaded objects). If we have the
3899 breakpoint's shadow memory, that is, this is a software
3900 breakpoint managed by GDB, check whether the breakpoint
3901 is still inserted in memory, to avoid overwriting wrong
3902 code with stale saved shadow contents. Note that HW
3903 breakpoints don't have shadow memory, as they're
3904 implemented using a mechanism that is not dependent on
3905 being able to modify the target's memory, and as such
3906 they should always be removed. */
3907 if (bl->shlib_disabled
3908 && bl->target_info.shadow_len != 0
3909 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3910 val = 0;
3911 else
3912 val = bl->owner->remove_location (bl, reason);
3913 }
3914 else
3915 {
3916 /* This breakpoint is in an overlay section.
3917 Did we set a breakpoint at the LMA? */
3918 if (!overlay_events_enabled)
3919 {
3920 /* Yes -- overlay event support is not active, so we
3921 should have set a breakpoint at the LMA. Remove it.
3922 */
3923 /* Ignore any failures: if the LMA is in ROM, we will
3924 have already warned when we failed to insert it. */
3925 if (bl->loc_type == bp_loc_hardware_breakpoint)
3926 target_remove_hw_breakpoint (bl->gdbarch,
3927 &bl->overlay_target_info);
3928 else
3929 target_remove_breakpoint (bl->gdbarch,
3930 &bl->overlay_target_info,
3931 reason);
3932 }
3933 /* Did we set a breakpoint at the VMA?
3934 If so, we will have marked the breakpoint 'inserted'. */
3935 if (bl->inserted)
3936 {
3937 /* Yes -- remove it. Previously we did not bother to
3938 remove the breakpoint if the section had been
3939 unmapped, but let's not rely on that being safe. We
3940 don't know what the overlay manager might do. */
3941
3942 /* However, we should remove *software* breakpoints only
3943 if the section is still mapped, or else we overwrite
3944 wrong code with the saved shadow contents. */
3945 if (bl->loc_type == bp_loc_hardware_breakpoint
3946 || section_is_mapped (bl->section))
3947 val = bl->owner->remove_location (bl, reason);
3948 else
3949 val = 0;
3950 }
3951 else
3952 {
3953 /* No -- not inserted, so no need to remove. No error. */
3954 val = 0;
3955 }
3956 }
3957
3958 /* In some cases, we might not be able to remove a breakpoint in
3959 a shared library that has already been removed, but we have
3960 not yet processed the shlib unload event. Similarly for an
3961 unloaded add-symbol-file object - the user might not yet have
3962 had the chance to remove-symbol-file it. shlib_disabled will
3963 be set if the library/object has already been removed, but
3964 the breakpoint hasn't been uninserted yet, e.g., after
3965 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3966 always-inserted mode. */
3967 if (val
3968 && (bl->loc_type == bp_loc_software_breakpoint
3969 && (bl->shlib_disabled
3970 || solib_name_from_address (bl->pspace, bl->address)
3971 || shared_objfile_contains_address_p (bl->pspace,
3972 bl->address))))
3973 val = 0;
3974
3975 if (val)
3976 return val;
3977 bl->inserted = (reason == DETACH_BREAKPOINT);
3978 }
3979 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3980 {
3981 bl->inserted = (reason == DETACH_BREAKPOINT);
3982 bl->owner->remove_location (bl, reason);
3983
3984 /* Failure to remove any of the hardware watchpoints comes here. */
3985 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3986 warning (_("Could not remove hardware watchpoint %d."),
3987 bl->owner->number);
3988 }
3989 else if (bl->owner->type == bp_catchpoint
3990 && breakpoint_enabled (bl->owner)
3991 && !bl->duplicate)
3992 {
3993 val = bl->owner->remove_location (bl, reason);
3994 if (val)
3995 return val;
3996
3997 bl->inserted = (reason == DETACH_BREAKPOINT);
3998 }
3999
4000 return 0;
4001 }
4002
4003 static int
4004 remove_breakpoint (struct bp_location *bl)
4005 {
4006 /* BL is never in moribund_locations by our callers. */
4007 gdb_assert (bl->owner != NULL);
4008
4009 /* The type of none suggests that owner is actually deleted.
4010 This should not ever happen. */
4011 gdb_assert (bl->owner->type != bp_none);
4012
4013 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4014
4015 switch_to_program_space_and_thread (bl->pspace);
4016
4017 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4018 }
4019
4020 /* Clear the "inserted" flag in all breakpoints. */
4021
4022 void
4023 mark_breakpoints_out (void)
4024 {
4025 for (bp_location *bl : all_bp_locations ())
4026 if (bl->pspace == current_program_space)
4027 bl->inserted = 0;
4028 }
4029
4030 /* Clear the "inserted" flag in all breakpoints and delete any
4031 breakpoints which should go away between runs of the program.
4032
4033 Plus other such housekeeping that has to be done for breakpoints
4034 between runs.
4035
4036 Note: this function gets called at the end of a run (by
4037 generic_mourn_inferior) and when a run begins (by
4038 init_wait_for_inferior). */
4039
4040
4041
4042 void
4043 breakpoint_init_inferior (enum inf_context context)
4044 {
4045 struct program_space *pspace = current_program_space;
4046
4047 /* If breakpoint locations are shared across processes, then there's
4048 nothing to do. */
4049 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4050 return;
4051
4052 mark_breakpoints_out ();
4053
4054 for (breakpoint *b : all_breakpoints_safe ())
4055 {
4056 if (b->loc && b->loc->pspace != pspace)
4057 continue;
4058
4059 switch (b->type)
4060 {
4061 case bp_call_dummy:
4062 case bp_longjmp_call_dummy:
4063
4064 /* If the call dummy breakpoint is at the entry point it will
4065 cause problems when the inferior is rerun, so we better get
4066 rid of it. */
4067
4068 case bp_watchpoint_scope:
4069
4070 /* Also get rid of scope breakpoints. */
4071
4072 case bp_shlib_event:
4073
4074 /* Also remove solib event breakpoints. Their addresses may
4075 have changed since the last time we ran the program.
4076 Actually we may now be debugging against different target;
4077 and so the solib backend that installed this breakpoint may
4078 not be used in by the target. E.g.,
4079
4080 (gdb) file prog-linux
4081 (gdb) run # native linux target
4082 ...
4083 (gdb) kill
4084 (gdb) file prog-win.exe
4085 (gdb) tar rem :9999 # remote Windows gdbserver.
4086 */
4087
4088 case bp_step_resume:
4089
4090 /* Also remove step-resume breakpoints. */
4091
4092 case bp_single_step:
4093
4094 /* Also remove single-step breakpoints. */
4095
4096 delete_breakpoint (b);
4097 break;
4098
4099 case bp_watchpoint:
4100 case bp_hardware_watchpoint:
4101 case bp_read_watchpoint:
4102 case bp_access_watchpoint:
4103 {
4104 struct watchpoint *w = (struct watchpoint *) b;
4105
4106 /* Likewise for watchpoints on local expressions. */
4107 if (w->exp_valid_block != NULL)
4108 delete_breakpoint (b);
4109 else
4110 {
4111 /* Get rid of existing locations, which are no longer
4112 valid. New ones will be created in
4113 update_watchpoint, when the inferior is restarted.
4114 The next update_global_location_list call will
4115 garbage collect them. */
4116 b->loc = NULL;
4117
4118 if (context == inf_starting)
4119 {
4120 /* Reset val field to force reread of starting value in
4121 insert_breakpoints. */
4122 w->val.reset (nullptr);
4123 w->val_valid = false;
4124 }
4125 }
4126 }
4127 break;
4128 default:
4129 break;
4130 }
4131 }
4132
4133 /* Get rid of the moribund locations. */
4134 for (bp_location *bl : moribund_locations)
4135 decref_bp_location (&bl);
4136 moribund_locations.clear ();
4137 }
4138
4139 /* These functions concern about actual breakpoints inserted in the
4140 target --- to e.g. check if we need to do decr_pc adjustment or if
4141 we need to hop over the bkpt --- so we check for address space
4142 match, not program space. */
4143
4144 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4145 exists at PC. It returns ordinary_breakpoint_here if it's an
4146 ordinary breakpoint, or permanent_breakpoint_here if it's a
4147 permanent breakpoint.
4148 - When continuing from a location with an ordinary breakpoint, we
4149 actually single step once before calling insert_breakpoints.
4150 - When continuing from a location with a permanent breakpoint, we
4151 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4152 the target, to advance the PC past the breakpoint. */
4153
4154 enum breakpoint_here
4155 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4156 {
4157 int any_breakpoint_here = 0;
4158
4159 for (bp_location *bl : all_bp_locations ())
4160 {
4161 if (bl->loc_type != bp_loc_software_breakpoint
4162 && bl->loc_type != bp_loc_hardware_breakpoint)
4163 continue;
4164
4165 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4166 if ((breakpoint_enabled (bl->owner)
4167 || bl->permanent)
4168 && breakpoint_location_address_match (bl, aspace, pc))
4169 {
4170 if (overlay_debugging
4171 && section_is_overlay (bl->section)
4172 && !section_is_mapped (bl->section))
4173 continue; /* unmapped overlay -- can't be a match */
4174 else if (bl->permanent)
4175 return permanent_breakpoint_here;
4176 else
4177 any_breakpoint_here = 1;
4178 }
4179 }
4180
4181 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4182 }
4183
4184 /* See breakpoint.h. */
4185
4186 int
4187 breakpoint_in_range_p (const address_space *aspace,
4188 CORE_ADDR addr, ULONGEST len)
4189 {
4190 for (bp_location *bl : all_bp_locations ())
4191 {
4192 if (bl->loc_type != bp_loc_software_breakpoint
4193 && bl->loc_type != bp_loc_hardware_breakpoint)
4194 continue;
4195
4196 if ((breakpoint_enabled (bl->owner)
4197 || bl->permanent)
4198 && breakpoint_location_address_range_overlap (bl, aspace,
4199 addr, len))
4200 {
4201 if (overlay_debugging
4202 && section_is_overlay (bl->section)
4203 && !section_is_mapped (bl->section))
4204 {
4205 /* Unmapped overlay -- can't be a match. */
4206 continue;
4207 }
4208
4209 return 1;
4210 }
4211 }
4212
4213 return 0;
4214 }
4215
4216 /* Return true if there's a moribund breakpoint at PC. */
4217
4218 int
4219 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4220 {
4221 for (bp_location *loc : moribund_locations)
4222 if (breakpoint_location_address_match (loc, aspace, pc))
4223 return 1;
4224
4225 return 0;
4226 }
4227
4228 /* Returns non-zero iff BL is inserted at PC, in address space
4229 ASPACE. */
4230
4231 static int
4232 bp_location_inserted_here_p (struct bp_location *bl,
4233 const address_space *aspace, CORE_ADDR pc)
4234 {
4235 if (bl->inserted
4236 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4237 aspace, pc))
4238 {
4239 if (overlay_debugging
4240 && section_is_overlay (bl->section)
4241 && !section_is_mapped (bl->section))
4242 return 0; /* unmapped overlay -- can't be a match */
4243 else
4244 return 1;
4245 }
4246 return 0;
4247 }
4248
4249 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4250
4251 int
4252 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4253 {
4254 for (bp_location *bl : all_bp_locations_at_addr (pc))
4255 {
4256 if (bl->loc_type != bp_loc_software_breakpoint
4257 && bl->loc_type != bp_loc_hardware_breakpoint)
4258 continue;
4259
4260 if (bp_location_inserted_here_p (bl, aspace, pc))
4261 return 1;
4262 }
4263 return 0;
4264 }
4265
4266 /* This function returns non-zero iff there is a software breakpoint
4267 inserted at PC. */
4268
4269 int
4270 software_breakpoint_inserted_here_p (const address_space *aspace,
4271 CORE_ADDR pc)
4272 {
4273 for (bp_location *bl : all_bp_locations_at_addr (pc))
4274 {
4275 if (bl->loc_type != bp_loc_software_breakpoint)
4276 continue;
4277
4278 if (bp_location_inserted_here_p (bl, aspace, pc))
4279 return 1;
4280 }
4281
4282 return 0;
4283 }
4284
4285 /* See breakpoint.h. */
4286
4287 int
4288 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4289 CORE_ADDR pc)
4290 {
4291 for (bp_location *bl : all_bp_locations_at_addr (pc))
4292 {
4293 if (bl->loc_type != bp_loc_hardware_breakpoint)
4294 continue;
4295
4296 if (bp_location_inserted_here_p (bl, aspace, pc))
4297 return 1;
4298 }
4299
4300 return 0;
4301 }
4302
4303 int
4304 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4305 CORE_ADDR addr, ULONGEST len)
4306 {
4307 for (breakpoint *bpt : all_breakpoints ())
4308 {
4309 if (bpt->type != bp_hardware_watchpoint
4310 && bpt->type != bp_access_watchpoint)
4311 continue;
4312
4313 if (!breakpoint_enabled (bpt))
4314 continue;
4315
4316 for (bp_location *loc : bpt->locations ())
4317 if (loc->pspace->aspace == aspace && loc->inserted)
4318 {
4319 CORE_ADDR l, h;
4320
4321 /* Check for intersection. */
4322 l = std::max<CORE_ADDR> (loc->address, addr);
4323 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4324 if (l < h)
4325 return 1;
4326 }
4327 }
4328 return 0;
4329 }
4330
4331 /* See breakpoint.h. */
4332
4333 bool
4334 is_catchpoint (struct breakpoint *b)
4335 {
4336 return (b->type == bp_catchpoint);
4337 }
4338
4339 /* Clear a bpstat so that it says we are not at any breakpoint.
4340 Also free any storage that is part of a bpstat. */
4341
4342 void
4343 bpstat_clear (bpstat **bsp)
4344 {
4345 bpstat *p;
4346 bpstat *q;
4347
4348 if (bsp == 0)
4349 return;
4350 p = *bsp;
4351 while (p != NULL)
4352 {
4353 q = p->next;
4354 delete p;
4355 p = q;
4356 }
4357 *bsp = NULL;
4358 }
4359
4360 bpstat::bpstat (const bpstat &other)
4361 : next (NULL),
4362 bp_location_at (other.bp_location_at),
4363 breakpoint_at (other.breakpoint_at),
4364 commands (other.commands),
4365 print (other.print),
4366 stop (other.stop),
4367 print_it (other.print_it)
4368 {
4369 if (other.old_val != NULL)
4370 old_val = release_value (value_copy (other.old_val.get ()));
4371 }
4372
4373 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4374 is part of the bpstat is copied as well. */
4375
4376 bpstat *
4377 bpstat_copy (bpstat *bs)
4378 {
4379 bpstat *p = nullptr;
4380 bpstat *tmp;
4381 bpstat *retval = nullptr;
4382
4383 if (bs == NULL)
4384 return bs;
4385
4386 for (; bs != NULL; bs = bs->next)
4387 {
4388 tmp = new bpstat (*bs);
4389
4390 if (p == NULL)
4391 /* This is the first thing in the chain. */
4392 retval = tmp;
4393 else
4394 p->next = tmp;
4395 p = tmp;
4396 }
4397 p->next = NULL;
4398 return retval;
4399 }
4400
4401 /* Find the bpstat associated with this breakpoint. */
4402
4403 bpstat *
4404 bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
4405 {
4406 if (bsp == NULL)
4407 return NULL;
4408
4409 for (; bsp != NULL; bsp = bsp->next)
4410 {
4411 if (bsp->breakpoint_at == breakpoint)
4412 return bsp;
4413 }
4414 return NULL;
4415 }
4416
4417 /* See breakpoint.h. */
4418
4419 bool
4420 bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
4421 {
4422 for (; bsp != NULL; bsp = bsp->next)
4423 {
4424 if (bsp->breakpoint_at == NULL)
4425 {
4426 /* A moribund location can never explain a signal other than
4427 GDB_SIGNAL_TRAP. */
4428 if (sig == GDB_SIGNAL_TRAP)
4429 return true;
4430 }
4431 else
4432 {
4433 if (bsp->breakpoint_at->explains_signal (sig))
4434 return true;
4435 }
4436 }
4437
4438 return false;
4439 }
4440
4441 /* Put in *NUM the breakpoint number of the first breakpoint we are
4442 stopped at. *BSP upon return is a bpstat which points to the
4443 remaining breakpoints stopped at (but which is not guaranteed to be
4444 good for anything but further calls to bpstat_num).
4445
4446 Return 0 if passed a bpstat which does not indicate any breakpoints.
4447 Return -1 if stopped at a breakpoint that has been deleted since
4448 we set it.
4449 Return 1 otherwise. */
4450
4451 int
4452 bpstat_num (bpstat **bsp, int *num)
4453 {
4454 struct breakpoint *b;
4455
4456 if ((*bsp) == NULL)
4457 return 0; /* No more breakpoint values */
4458
4459 /* We assume we'll never have several bpstats that correspond to a
4460 single breakpoint -- otherwise, this function might return the
4461 same number more than once and this will look ugly. */
4462 b = (*bsp)->breakpoint_at;
4463 *bsp = (*bsp)->next;
4464 if (b == NULL)
4465 return -1; /* breakpoint that's been deleted since */
4466
4467 *num = b->number; /* We have its number */
4468 return 1;
4469 }
4470
4471 /* See breakpoint.h. */
4472
4473 void
4474 bpstat_clear_actions (void)
4475 {
4476 bpstat *bs;
4477
4478 if (inferior_ptid == null_ptid)
4479 return;
4480
4481 thread_info *tp = inferior_thread ();
4482 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4483 {
4484 bs->commands = NULL;
4485 bs->old_val.reset (nullptr);
4486 }
4487 }
4488
4489 /* Called when a command is about to proceed the inferior. */
4490
4491 static void
4492 breakpoint_about_to_proceed (void)
4493 {
4494 if (inferior_ptid != null_ptid)
4495 {
4496 struct thread_info *tp = inferior_thread ();
4497
4498 /* Allow inferior function calls in breakpoint commands to not
4499 interrupt the command list. When the call finishes
4500 successfully, the inferior will be standing at the same
4501 breakpoint as if nothing happened. */
4502 if (tp->control.in_infcall)
4503 return;
4504 }
4505
4506 breakpoint_proceeded = 1;
4507 }
4508
4509 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4510 or its equivalent. */
4511
4512 static int
4513 command_line_is_silent (struct command_line *cmd)
4514 {
4515 return cmd && (strcmp ("silent", cmd->line) == 0);
4516 }
4517
4518 /* Execute all the commands associated with all the breakpoints at
4519 this location. Any of these commands could cause the process to
4520 proceed beyond this point, etc. We look out for such changes by
4521 checking the global "breakpoint_proceeded" after each command.
4522
4523 Returns true if a breakpoint command resumed the inferior. In that
4524 case, it is the caller's responsibility to recall it again with the
4525 bpstat of the current thread. */
4526
4527 static int
4528 bpstat_do_actions_1 (bpstat **bsp)
4529 {
4530 bpstat *bs;
4531 int again = 0;
4532
4533 /* Avoid endless recursion if a `source' command is contained
4534 in bs->commands. */
4535 if (executing_breakpoint_commands)
4536 return 0;
4537
4538 scoped_restore save_executing
4539 = make_scoped_restore (&executing_breakpoint_commands, 1);
4540
4541 scoped_restore preventer = prevent_dont_repeat ();
4542
4543 /* This pointer will iterate over the list of bpstat's. */
4544 bs = *bsp;
4545
4546 breakpoint_proceeded = 0;
4547 for (; bs != NULL; bs = bs->next)
4548 {
4549 struct command_line *cmd = NULL;
4550
4551 /* Take ownership of the BSP's command tree, if it has one.
4552
4553 The command tree could legitimately contain commands like
4554 'step' and 'next', which call clear_proceed_status, which
4555 frees stop_bpstat's command tree. To make sure this doesn't
4556 free the tree we're executing out from under us, we need to
4557 take ownership of the tree ourselves. Since a given bpstat's
4558 commands are only executed once, we don't need to copy it; we
4559 can clear the pointer in the bpstat, and make sure we free
4560 the tree when we're done. */
4561 counted_command_line ccmd = bs->commands;
4562 bs->commands = NULL;
4563 if (ccmd != NULL)
4564 cmd = ccmd.get ();
4565 if (command_line_is_silent (cmd))
4566 {
4567 /* The action has been already done by bpstat_stop_status. */
4568 cmd = cmd->next;
4569 }
4570
4571 while (cmd != NULL)
4572 {
4573 execute_control_command (cmd);
4574
4575 if (breakpoint_proceeded)
4576 break;
4577 else
4578 cmd = cmd->next;
4579 }
4580
4581 if (breakpoint_proceeded)
4582 {
4583 if (current_ui->async)
4584 /* If we are in async mode, then the target might be still
4585 running, not stopped at any breakpoint, so nothing for
4586 us to do here -- just return to the event loop. */
4587 ;
4588 else
4589 /* In sync mode, when execute_control_command returns
4590 we're already standing on the next breakpoint.
4591 Breakpoint commands for that stop were not run, since
4592 execute_command does not run breakpoint commands --
4593 only command_line_handler does, but that one is not
4594 involved in execution of breakpoint commands. So, we
4595 can now execute breakpoint commands. It should be
4596 noted that making execute_command do bpstat actions is
4597 not an option -- in this case we'll have recursive
4598 invocation of bpstat for each breakpoint with a
4599 command, and can easily blow up GDB stack. Instead, we
4600 return true, which will trigger the caller to recall us
4601 with the new stop_bpstat. */
4602 again = 1;
4603 break;
4604 }
4605 }
4606 return again;
4607 }
4608
4609 /* Helper for bpstat_do_actions. Get the current thread, if there's
4610 one, is alive and has execution. Return NULL otherwise. */
4611
4612 static thread_info *
4613 get_bpstat_thread ()
4614 {
4615 if (inferior_ptid == null_ptid || !target_has_execution ())
4616 return NULL;
4617
4618 thread_info *tp = inferior_thread ();
4619 if (tp->state == THREAD_EXITED || tp->executing ())
4620 return NULL;
4621 return tp;
4622 }
4623
4624 void
4625 bpstat_do_actions (void)
4626 {
4627 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4628 thread_info *tp;
4629
4630 /* Do any commands attached to breakpoint we are stopped at. */
4631 while ((tp = get_bpstat_thread ()) != NULL)
4632 {
4633 /* Since in sync mode, bpstat_do_actions may resume the
4634 inferior, and only return when it is stopped at the next
4635 breakpoint, we keep doing breakpoint actions until it returns
4636 false to indicate the inferior was not resumed. */
4637 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4638 break;
4639 }
4640
4641 cleanup_if_error.release ();
4642 }
4643
4644 /* Print out the (old or new) value associated with a watchpoint. */
4645
4646 static void
4647 watchpoint_value_print (struct value *val, struct ui_file *stream)
4648 {
4649 if (val == NULL)
4650 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4651 else
4652 {
4653 struct value_print_options opts;
4654 get_user_print_options (&opts);
4655 value_print (val, stream, &opts);
4656 }
4657 }
4658
4659 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4660 debugging multiple threads. */
4661
4662 void
4663 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4664 {
4665 if (uiout->is_mi_like_p ())
4666 return;
4667
4668 uiout->text ("\n");
4669
4670 if (show_thread_that_caused_stop ())
4671 {
4672 struct thread_info *thr = inferior_thread ();
4673
4674 uiout->text ("Thread ");
4675 uiout->field_string ("thread-id", print_thread_id (thr));
4676
4677 const char *name = thread_name (thr);
4678 if (name != NULL)
4679 {
4680 uiout->text (" \"");
4681 uiout->field_string ("name", name);
4682 uiout->text ("\"");
4683 }
4684
4685 uiout->text (" hit ");
4686 }
4687 }
4688
4689 /* Generic routine for printing messages indicating why we
4690 stopped. The behavior of this function depends on the value
4691 'print_it' in the bpstat structure. Under some circumstances we
4692 may decide not to print anything here and delegate the task to
4693 normal_stop(). */
4694
4695 static enum print_stop_action
4696 print_bp_stop_message (bpstat *bs)
4697 {
4698 switch (bs->print_it)
4699 {
4700 case print_it_noop:
4701 /* Nothing should be printed for this bpstat entry. */
4702 return PRINT_UNKNOWN;
4703 break;
4704
4705 case print_it_done:
4706 /* We still want to print the frame, but we already printed the
4707 relevant messages. */
4708 return PRINT_SRC_AND_LOC;
4709 break;
4710
4711 case print_it_normal:
4712 {
4713 struct breakpoint *b = bs->breakpoint_at;
4714
4715 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4716 which has since been deleted. */
4717 if (b == NULL)
4718 return PRINT_UNKNOWN;
4719
4720 /* Normal case. Call the breakpoint's print_it method. */
4721 return b->print_it (bs);
4722 }
4723 break;
4724
4725 default:
4726 internal_error (__FILE__, __LINE__,
4727 _("print_bp_stop_message: unrecognized enum value"));
4728 break;
4729 }
4730 }
4731
4732 /* See breakpoint.h. */
4733
4734 void
4735 print_solib_event (bool is_catchpoint)
4736 {
4737 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4738 bool any_added = !current_program_space->added_solibs.empty ();
4739
4740 if (!is_catchpoint)
4741 {
4742 if (any_added || any_deleted)
4743 current_uiout->text (_("Stopped due to shared library event:\n"));
4744 else
4745 current_uiout->text (_("Stopped due to shared library event (no "
4746 "libraries added or removed)\n"));
4747 }
4748
4749 if (current_uiout->is_mi_like_p ())
4750 current_uiout->field_string ("reason",
4751 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4752
4753 if (any_deleted)
4754 {
4755 current_uiout->text (_(" Inferior unloaded "));
4756 ui_out_emit_list list_emitter (current_uiout, "removed");
4757 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4758 {
4759 const std::string &name = current_program_space->deleted_solibs[ix];
4760
4761 if (ix > 0)
4762 current_uiout->text (" ");
4763 current_uiout->field_string ("library", name);
4764 current_uiout->text ("\n");
4765 }
4766 }
4767
4768 if (any_added)
4769 {
4770 current_uiout->text (_(" Inferior loaded "));
4771 ui_out_emit_list list_emitter (current_uiout, "added");
4772 bool first = true;
4773 for (so_list *iter : current_program_space->added_solibs)
4774 {
4775 if (!first)
4776 current_uiout->text (" ");
4777 first = false;
4778 current_uiout->field_string ("library", iter->so_name);
4779 current_uiout->text ("\n");
4780 }
4781 }
4782 }
4783
4784 /* Print a message indicating what happened. This is called from
4785 normal_stop(). The input to this routine is the head of the bpstat
4786 list - a list of the eventpoints that caused this stop. KIND is
4787 the target_waitkind for the stopping event. This
4788 routine calls the generic print routine for printing a message
4789 about reasons for stopping. This will print (for example) the
4790 "Breakpoint n," part of the output. The return value of this
4791 routine is one of:
4792
4793 PRINT_UNKNOWN: Means we printed nothing.
4794 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4795 code to print the location. An example is
4796 "Breakpoint 1, " which should be followed by
4797 the location.
4798 PRINT_SRC_ONLY: Means we printed something, but there is no need
4799 to also print the location part of the message.
4800 An example is the catch/throw messages, which
4801 don't require a location appended to the end.
4802 PRINT_NOTHING: We have done some printing and we don't need any
4803 further info to be printed. */
4804
4805 enum print_stop_action
4806 bpstat_print (bpstat *bs, target_waitkind kind)
4807 {
4808 enum print_stop_action val;
4809
4810 /* Maybe another breakpoint in the chain caused us to stop.
4811 (Currently all watchpoints go on the bpstat whether hit or not.
4812 That probably could (should) be changed, provided care is taken
4813 with respect to bpstat_explains_signal). */
4814 for (; bs; bs = bs->next)
4815 {
4816 val = print_bp_stop_message (bs);
4817 if (val == PRINT_SRC_ONLY
4818 || val == PRINT_SRC_AND_LOC
4819 || val == PRINT_NOTHING)
4820 return val;
4821 }
4822
4823 /* If we had hit a shared library event breakpoint,
4824 print_bp_stop_message would print out this message. If we hit an
4825 OS-level shared library event, do the same thing. */
4826 if (kind == TARGET_WAITKIND_LOADED)
4827 {
4828 print_solib_event (false);
4829 return PRINT_NOTHING;
4830 }
4831
4832 /* We reached the end of the chain, or we got a null BS to start
4833 with and nothing was printed. */
4834 return PRINT_UNKNOWN;
4835 }
4836
4837 /* Evaluate the boolean expression EXP and return the result. */
4838
4839 static bool
4840 breakpoint_cond_eval (expression *exp)
4841 {
4842 struct value *mark = value_mark ();
4843 bool res = value_true (evaluate_expression (exp));
4844
4845 value_free_to_mark (mark);
4846 return res;
4847 }
4848
4849 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4850
4851 bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
4852 : next (NULL),
4853 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4854 breakpoint_at (bl->owner),
4855 commands (NULL),
4856 print (0),
4857 stop (0),
4858 print_it (print_it_normal)
4859 {
4860 **bs_link_pointer = this;
4861 *bs_link_pointer = &next;
4862 }
4863
4864 bpstat::bpstat ()
4865 : next (NULL),
4866 breakpoint_at (NULL),
4867 commands (NULL),
4868 print (0),
4869 stop (0),
4870 print_it (print_it_normal)
4871 {
4872 }
4873 \f
4874 /* The target has stopped with waitstatus WS. Check if any hardware
4875 watchpoints have triggered, according to the target. */
4876
4877 int
4878 watchpoints_triggered (const target_waitstatus &ws)
4879 {
4880 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4881 CORE_ADDR addr;
4882
4883 if (!stopped_by_watchpoint)
4884 {
4885 /* We were not stopped by a watchpoint. Mark all watchpoints
4886 as not triggered. */
4887 for (breakpoint *b : all_breakpoints ())
4888 if (is_hardware_watchpoint (b))
4889 {
4890 struct watchpoint *w = (struct watchpoint *) b;
4891
4892 w->watchpoint_triggered = watch_triggered_no;
4893 }
4894
4895 return 0;
4896 }
4897
4898 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4899 {
4900 /* We were stopped by a watchpoint, but we don't know where.
4901 Mark all watchpoints as unknown. */
4902 for (breakpoint *b : all_breakpoints ())
4903 if (is_hardware_watchpoint (b))
4904 {
4905 struct watchpoint *w = (struct watchpoint *) b;
4906
4907 w->watchpoint_triggered = watch_triggered_unknown;
4908 }
4909
4910 return 1;
4911 }
4912
4913 /* The target could report the data address. Mark watchpoints
4914 affected by this data address as triggered, and all others as not
4915 triggered. */
4916
4917 for (breakpoint *b : all_breakpoints ())
4918 if (is_hardware_watchpoint (b))
4919 {
4920 struct watchpoint *w = (struct watchpoint *) b;
4921
4922 w->watchpoint_triggered = watch_triggered_no;
4923 for (bp_location *loc : b->locations ())
4924 {
4925 if (is_masked_watchpoint (b))
4926 {
4927 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4928 CORE_ADDR start = loc->address & w->hw_wp_mask;
4929
4930 if (newaddr == start)
4931 {
4932 w->watchpoint_triggered = watch_triggered_yes;
4933 break;
4934 }
4935 }
4936 /* Exact match not required. Within range is sufficient. */
4937 else if (target_watchpoint_addr_within_range
4938 (current_inferior ()->top_target (), addr, loc->address,
4939 loc->length))
4940 {
4941 w->watchpoint_triggered = watch_triggered_yes;
4942 break;
4943 }
4944 }
4945 }
4946
4947 return 1;
4948 }
4949
4950 /* Possible return values for watchpoint_check. */
4951 enum wp_check_result
4952 {
4953 /* The watchpoint has been deleted. */
4954 WP_DELETED = 1,
4955
4956 /* The value has changed. */
4957 WP_VALUE_CHANGED = 2,
4958
4959 /* The value has not changed. */
4960 WP_VALUE_NOT_CHANGED = 3,
4961
4962 /* Ignore this watchpoint, no matter if the value changed or not. */
4963 WP_IGNORE = 4,
4964 };
4965
4966 #define BP_TEMPFLAG 1
4967 #define BP_HARDWAREFLAG 2
4968
4969 /* Evaluate watchpoint condition expression and check if its value
4970 changed. */
4971
4972 static wp_check_result
4973 watchpoint_check (bpstat *bs)
4974 {
4975 struct watchpoint *b;
4976 struct frame_info *fr;
4977 int within_current_scope;
4978
4979 /* BS is built from an existing struct breakpoint. */
4980 gdb_assert (bs->breakpoint_at != NULL);
4981 b = (struct watchpoint *) bs->breakpoint_at;
4982
4983 /* If this is a local watchpoint, we only want to check if the
4984 watchpoint frame is in scope if the current thread is the thread
4985 that was used to create the watchpoint. */
4986 if (!watchpoint_in_thread_scope (b))
4987 return WP_IGNORE;
4988
4989 if (b->exp_valid_block == NULL)
4990 within_current_scope = 1;
4991 else
4992 {
4993 struct frame_info *frame = get_current_frame ();
4994 struct gdbarch *frame_arch = get_frame_arch (frame);
4995 CORE_ADDR frame_pc = get_frame_pc (frame);
4996
4997 /* stack_frame_destroyed_p() returns a non-zero value if we're
4998 still in the function but the stack frame has already been
4999 invalidated. Since we can't rely on the values of local
5000 variables after the stack has been destroyed, we are treating
5001 the watchpoint in that state as `not changed' without further
5002 checking. Don't mark watchpoints as changed if the current
5003 frame is in an epilogue - even if they are in some other
5004 frame, our view of the stack is likely to be wrong and
5005 frame_find_by_id could error out. */
5006 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5007 return WP_IGNORE;
5008
5009 fr = frame_find_by_id (b->watchpoint_frame);
5010 within_current_scope = (fr != NULL);
5011
5012 /* If we've gotten confused in the unwinder, we might have
5013 returned a frame that can't describe this variable. */
5014 if (within_current_scope)
5015 {
5016 struct symbol *function;
5017
5018 function = get_frame_function (fr);
5019 if (function == NULL
5020 || !contained_in (b->exp_valid_block, function->value_block ()))
5021 within_current_scope = 0;
5022 }
5023
5024 if (within_current_scope)
5025 /* If we end up stopping, the current frame will get selected
5026 in normal_stop. So this call to select_frame won't affect
5027 the user. */
5028 select_frame (fr);
5029 }
5030
5031 if (within_current_scope)
5032 {
5033 /* We use value_{,free_to_}mark because it could be a *long*
5034 time before we return to the command level and call
5035 free_all_values. We can't call free_all_values because we
5036 might be in the middle of evaluating a function call. */
5037
5038 struct value *mark;
5039 struct value *new_val;
5040
5041 if (is_masked_watchpoint (b))
5042 /* Since we don't know the exact trigger address (from
5043 stopped_data_address), just tell the user we've triggered
5044 a mask watchpoint. */
5045 return WP_VALUE_CHANGED;
5046
5047 mark = value_mark ();
5048 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5049 NULL, NULL, false);
5050
5051 if (b->val_bitsize != 0)
5052 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5053
5054 /* We use value_equal_contents instead of value_equal because
5055 the latter coerces an array to a pointer, thus comparing just
5056 the address of the array instead of its contents. This is
5057 not what we want. */
5058 if ((b->val != NULL) != (new_val != NULL)
5059 || (b->val != NULL && !value_equal_contents (b->val.get (),
5060 new_val)))
5061 {
5062 bs->old_val = b->val;
5063 b->val = release_value (new_val);
5064 b->val_valid = true;
5065 if (new_val != NULL)
5066 value_free_to_mark (mark);
5067 return WP_VALUE_CHANGED;
5068 }
5069 else
5070 {
5071 /* Nothing changed. */
5072 value_free_to_mark (mark);
5073 return WP_VALUE_NOT_CHANGED;
5074 }
5075 }
5076 else
5077 {
5078 /* This seems like the only logical thing to do because
5079 if we temporarily ignored the watchpoint, then when
5080 we reenter the block in which it is valid it contains
5081 garbage (in the case of a function, it may have two
5082 garbage values, one before and one after the prologue).
5083 So we can't even detect the first assignment to it and
5084 watch after that (since the garbage may or may not equal
5085 the first value assigned). */
5086 /* We print all the stop information in
5087 breakpointprint_it, but in this case, by the time we
5088 call breakpoint->print_it this bp will be deleted
5089 already. So we have no choice but print the information
5090 here. */
5091
5092 SWITCH_THRU_ALL_UIS ()
5093 {
5094 struct ui_out *uiout = current_uiout;
5095
5096 if (uiout->is_mi_like_p ())
5097 uiout->field_string
5098 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5099 uiout->message ("\nWatchpoint %pF deleted because the program has "
5100 "left the block in\n"
5101 "which its expression is valid.\n",
5102 signed_field ("wpnum", b->number));
5103 }
5104
5105 /* Make sure the watchpoint's commands aren't executed. */
5106 b->commands = NULL;
5107 watchpoint_del_at_next_stop (b);
5108
5109 return WP_DELETED;
5110 }
5111 }
5112
5113 /* Return true if it looks like target has stopped due to hitting
5114 breakpoint location BL. This function does not check if we should
5115 stop, only if BL explains the stop. */
5116
5117 static int
5118 bpstat_check_location (const struct bp_location *bl,
5119 const address_space *aspace, CORE_ADDR bp_addr,
5120 const target_waitstatus &ws)
5121 {
5122 struct breakpoint *b = bl->owner;
5123
5124 /* BL is from an existing breakpoint. */
5125 gdb_assert (b != NULL);
5126
5127 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
5128 }
5129
5130 /* Determine if the watched values have actually changed, and we
5131 should stop. If not, set BS->stop to 0. */
5132
5133 static void
5134 bpstat_check_watchpoint (bpstat *bs)
5135 {
5136 const struct bp_location *bl;
5137 struct watchpoint *b;
5138
5139 /* BS is built for existing struct breakpoint. */
5140 bl = bs->bp_location_at.get ();
5141 gdb_assert (bl != NULL);
5142 b = (struct watchpoint *) bs->breakpoint_at;
5143 gdb_assert (b != NULL);
5144
5145 {
5146 int must_check_value = 0;
5147
5148 if (b->type == bp_watchpoint)
5149 /* For a software watchpoint, we must always check the
5150 watched value. */
5151 must_check_value = 1;
5152 else if (b->watchpoint_triggered == watch_triggered_yes)
5153 /* We have a hardware watchpoint (read, write, or access)
5154 and the target earlier reported an address watched by
5155 this watchpoint. */
5156 must_check_value = 1;
5157 else if (b->watchpoint_triggered == watch_triggered_unknown
5158 && b->type == bp_hardware_watchpoint)
5159 /* We were stopped by a hardware watchpoint, but the target could
5160 not report the data address. We must check the watchpoint's
5161 value. Access and read watchpoints are out of luck; without
5162 a data address, we can't figure it out. */
5163 must_check_value = 1;
5164
5165 if (must_check_value)
5166 {
5167 wp_check_result e;
5168
5169 try
5170 {
5171 e = watchpoint_check (bs);
5172 }
5173 catch (const gdb_exception &ex)
5174 {
5175 exception_fprintf (gdb_stderr, ex,
5176 "Error evaluating expression "
5177 "for watchpoint %d\n",
5178 b->number);
5179
5180 SWITCH_THRU_ALL_UIS ()
5181 {
5182 gdb_printf (_("Watchpoint %d deleted.\n"),
5183 b->number);
5184 }
5185 watchpoint_del_at_next_stop (b);
5186 e = WP_DELETED;
5187 }
5188
5189 switch (e)
5190 {
5191 case WP_DELETED:
5192 /* We've already printed what needs to be printed. */
5193 bs->print_it = print_it_done;
5194 /* Stop. */
5195 break;
5196 case WP_IGNORE:
5197 bs->print_it = print_it_noop;
5198 bs->stop = 0;
5199 break;
5200 case WP_VALUE_CHANGED:
5201 if (b->type == bp_read_watchpoint)
5202 {
5203 /* There are two cases to consider here:
5204
5205 1. We're watching the triggered memory for reads.
5206 In that case, trust the target, and always report
5207 the watchpoint hit to the user. Even though
5208 reads don't cause value changes, the value may
5209 have changed since the last time it was read, and
5210 since we're not trapping writes, we will not see
5211 those, and as such we should ignore our notion of
5212 old value.
5213
5214 2. We're watching the triggered memory for both
5215 reads and writes. There are two ways this may
5216 happen:
5217
5218 2.1. This is a target that can't break on data
5219 reads only, but can break on accesses (reads or
5220 writes), such as e.g., x86. We detect this case
5221 at the time we try to insert read watchpoints.
5222
5223 2.2. Otherwise, the target supports read
5224 watchpoints, but, the user set an access or write
5225 watchpoint watching the same memory as this read
5226 watchpoint.
5227
5228 If we're watching memory writes as well as reads,
5229 ignore watchpoint hits when we find that the
5230 value hasn't changed, as reads don't cause
5231 changes. This still gives false positives when
5232 the program writes the same value to memory as
5233 what there was already in memory (we will confuse
5234 it for a read), but it's much better than
5235 nothing. */
5236
5237 int other_write_watchpoint = 0;
5238
5239 if (bl->watchpoint_type == hw_read)
5240 {
5241 for (breakpoint *other_b : all_breakpoints ())
5242 if (other_b->type == bp_hardware_watchpoint
5243 || other_b->type == bp_access_watchpoint)
5244 {
5245 struct watchpoint *other_w =
5246 (struct watchpoint *) other_b;
5247
5248 if (other_w->watchpoint_triggered
5249 == watch_triggered_yes)
5250 {
5251 other_write_watchpoint = 1;
5252 break;
5253 }
5254 }
5255 }
5256
5257 if (other_write_watchpoint
5258 || bl->watchpoint_type == hw_access)
5259 {
5260 /* We're watching the same memory for writes,
5261 and the value changed since the last time we
5262 updated it, so this trap must be for a write.
5263 Ignore it. */
5264 bs->print_it = print_it_noop;
5265 bs->stop = 0;
5266 }
5267 }
5268 break;
5269 case WP_VALUE_NOT_CHANGED:
5270 if (b->type == bp_hardware_watchpoint
5271 || b->type == bp_watchpoint)
5272 {
5273 /* Don't stop: write watchpoints shouldn't fire if
5274 the value hasn't changed. */
5275 bs->print_it = print_it_noop;
5276 bs->stop = 0;
5277 }
5278 /* Stop. */
5279 break;
5280 default:
5281 /* Can't happen. */
5282 break;
5283 }
5284 }
5285 else /* must_check_value == 0 */
5286 {
5287 /* This is a case where some watchpoint(s) triggered, but
5288 not at the address of this watchpoint, or else no
5289 watchpoint triggered after all. So don't print
5290 anything for this watchpoint. */
5291 bs->print_it = print_it_noop;
5292 bs->stop = 0;
5293 }
5294 }
5295 }
5296
5297 /* For breakpoints that are currently marked as telling gdb to stop,
5298 check conditions (condition proper, frame, thread and ignore count)
5299 of breakpoint referred to by BS. If we should not stop for this
5300 breakpoint, set BS->stop to 0. */
5301
5302 static void
5303 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5304 {
5305 const struct bp_location *bl;
5306 struct breakpoint *b;
5307 /* Assume stop. */
5308 bool condition_result = true;
5309 struct expression *cond;
5310
5311 gdb_assert (bs->stop);
5312
5313 /* BS is built for existing struct breakpoint. */
5314 bl = bs->bp_location_at.get ();
5315 gdb_assert (bl != NULL);
5316 b = bs->breakpoint_at;
5317 gdb_assert (b != NULL);
5318
5319 /* Even if the target evaluated the condition on its end and notified GDB, we
5320 need to do so again since GDB does not know if we stopped due to a
5321 breakpoint or a single step breakpoint. */
5322
5323 if (frame_id_p (b->frame_id)
5324 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5325 {
5326 bs->stop = 0;
5327 return;
5328 }
5329
5330 /* If this is a thread/task-specific breakpoint, don't waste cpu
5331 evaluating the condition if this isn't the specified
5332 thread/task. */
5333 if ((b->thread != -1 && b->thread != thread->global_num)
5334 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5335 {
5336 bs->stop = 0;
5337 return;
5338 }
5339
5340 /* Evaluate extension language breakpoints that have a "stop" method
5341 implemented. */
5342 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5343
5344 if (is_watchpoint (b))
5345 {
5346 struct watchpoint *w = (struct watchpoint *) b;
5347
5348 cond = w->cond_exp.get ();
5349 }
5350 else
5351 cond = bl->cond.get ();
5352
5353 if (cond && b->disposition != disp_del_at_next_stop)
5354 {
5355 int within_current_scope = 1;
5356 struct watchpoint * w;
5357
5358 /* We use value_mark and value_free_to_mark because it could
5359 be a long time before we return to the command level and
5360 call free_all_values. We can't call free_all_values
5361 because we might be in the middle of evaluating a
5362 function call. */
5363 struct value *mark = value_mark ();
5364
5365 if (is_watchpoint (b))
5366 w = (struct watchpoint *) b;
5367 else
5368 w = NULL;
5369
5370 /* Need to select the frame, with all that implies so that
5371 the conditions will have the right context. Because we
5372 use the frame, we will not see an inlined function's
5373 variables when we arrive at a breakpoint at the start
5374 of the inlined function; the current frame will be the
5375 call site. */
5376 if (w == NULL || w->cond_exp_valid_block == NULL)
5377 select_frame (get_current_frame ());
5378 else
5379 {
5380 struct frame_info *frame;
5381
5382 /* For local watchpoint expressions, which particular
5383 instance of a local is being watched matters, so we
5384 keep track of the frame to evaluate the expression
5385 in. To evaluate the condition however, it doesn't
5386 really matter which instantiation of the function
5387 where the condition makes sense triggers the
5388 watchpoint. This allows an expression like "watch
5389 global if q > 10" set in `func', catch writes to
5390 global on all threads that call `func', or catch
5391 writes on all recursive calls of `func' by a single
5392 thread. We simply always evaluate the condition in
5393 the innermost frame that's executing where it makes
5394 sense to evaluate the condition. It seems
5395 intuitive. */
5396 frame = block_innermost_frame (w->cond_exp_valid_block);
5397 if (frame != NULL)
5398 select_frame (frame);
5399 else
5400 within_current_scope = 0;
5401 }
5402 if (within_current_scope)
5403 {
5404 try
5405 {
5406 condition_result = breakpoint_cond_eval (cond);
5407 }
5408 catch (const gdb_exception &ex)
5409 {
5410 exception_fprintf (gdb_stderr, ex,
5411 "Error in testing breakpoint condition:\n");
5412 }
5413 }
5414 else
5415 {
5416 warning (_("Watchpoint condition cannot be tested "
5417 "in the current scope"));
5418 /* If we failed to set the right context for this
5419 watchpoint, unconditionally report it. */
5420 }
5421 /* FIXME-someday, should give breakpoint #. */
5422 value_free_to_mark (mark);
5423 }
5424
5425 if (cond && !condition_result)
5426 {
5427 bs->stop = 0;
5428 }
5429 else if (b->ignore_count > 0)
5430 {
5431 b->ignore_count--;
5432 bs->stop = 0;
5433 /* Increase the hit count even though we don't stop. */
5434 ++(b->hit_count);
5435 gdb::observers::breakpoint_modified.notify (b);
5436 }
5437 }
5438
5439 /* Returns true if we need to track moribund locations of LOC's type
5440 on the current target. */
5441
5442 static int
5443 need_moribund_for_location_type (struct bp_location *loc)
5444 {
5445 return ((loc->loc_type == bp_loc_software_breakpoint
5446 && !target_supports_stopped_by_sw_breakpoint ())
5447 || (loc->loc_type == bp_loc_hardware_breakpoint
5448 && !target_supports_stopped_by_hw_breakpoint ()));
5449 }
5450
5451 /* See breakpoint.h. */
5452
5453 bpstat *
5454 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5455 const target_waitstatus &ws)
5456 {
5457 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5458
5459 for (breakpoint *b : all_breakpoints ())
5460 {
5461 if (!breakpoint_enabled (b))
5462 continue;
5463
5464 for (bp_location *bl : b->locations ())
5465 {
5466 /* For hardware watchpoints, we look only at the first
5467 location. The watchpoint_check function will work on the
5468 entire expression, not the individual locations. For
5469 read watchpoints, the watchpoints_triggered function has
5470 checked all locations already. */
5471 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5472 break;
5473
5474 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5475 continue;
5476
5477 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5478 continue;
5479
5480 /* Come here if it's a watchpoint, or if the break address
5481 matches. */
5482
5483 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
5484 explain stop. */
5485
5486 /* Assume we stop. Should we find a watchpoint that is not
5487 actually triggered, or if the condition of the breakpoint
5488 evaluates as false, we'll reset 'stop' to 0. */
5489 bs->stop = 1;
5490 bs->print = 1;
5491
5492 /* If this is a scope breakpoint, mark the associated
5493 watchpoint as triggered so that we will handle the
5494 out-of-scope event. We'll get to the watchpoint next
5495 iteration. */
5496 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5497 {
5498 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5499
5500 w->watchpoint_triggered = watch_triggered_yes;
5501 }
5502 }
5503 }
5504
5505 /* Check if a moribund breakpoint explains the stop. */
5506 if (!target_supports_stopped_by_sw_breakpoint ()
5507 || !target_supports_stopped_by_hw_breakpoint ())
5508 {
5509 for (bp_location *loc : moribund_locations)
5510 {
5511 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5512 && need_moribund_for_location_type (loc))
5513 {
5514 bpstat *bs = new bpstat (loc, &bs_link);
5515 /* For hits of moribund locations, we should just proceed. */
5516 bs->stop = 0;
5517 bs->print = 0;
5518 bs->print_it = print_it_noop;
5519 }
5520 }
5521 }
5522
5523 return bs_head;
5524 }
5525
5526 /* See breakpoint.h. */
5527
5528 bpstat *
5529 bpstat_stop_status (const address_space *aspace,
5530 CORE_ADDR bp_addr, thread_info *thread,
5531 const target_waitstatus &ws,
5532 bpstat *stop_chain)
5533 {
5534 struct breakpoint *b = NULL;
5535 /* First item of allocated bpstat's. */
5536 bpstat *bs_head = stop_chain;
5537 bpstat *bs;
5538 int need_remove_insert;
5539 int removed_any;
5540
5541 /* First, build the bpstat chain with locations that explain a
5542 target stop, while being careful to not set the target running,
5543 as that may invalidate locations (in particular watchpoint
5544 locations are recreated). Resuming will happen here with
5545 breakpoint conditions or watchpoint expressions that include
5546 inferior function calls. */
5547 if (bs_head == NULL)
5548 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5549
5550 /* A bit of special processing for shlib breakpoints. We need to
5551 process solib loading here, so that the lists of loaded and
5552 unloaded libraries are correct before we handle "catch load" and
5553 "catch unload". */
5554 for (bs = bs_head; bs != NULL; bs = bs->next)
5555 {
5556 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5557 {
5558 handle_solib_event ();
5559 break;
5560 }
5561 }
5562
5563 /* Now go through the locations that caused the target to stop, and
5564 check whether we're interested in reporting this stop to higher
5565 layers, or whether we should resume the target transparently. */
5566
5567 removed_any = 0;
5568
5569 for (bs = bs_head; bs != NULL; bs = bs->next)
5570 {
5571 if (!bs->stop)
5572 continue;
5573
5574 b = bs->breakpoint_at;
5575 b->check_status (bs);
5576 if (bs->stop)
5577 {
5578 bpstat_check_breakpoint_conditions (bs, thread);
5579
5580 if (bs->stop)
5581 {
5582 ++(b->hit_count);
5583
5584 /* We will stop here. */
5585 if (b->disposition == disp_disable)
5586 {
5587 --(b->enable_count);
5588 if (b->enable_count <= 0)
5589 b->enable_state = bp_disabled;
5590 removed_any = 1;
5591 }
5592 gdb::observers::breakpoint_modified.notify (b);
5593 if (b->silent)
5594 bs->print = 0;
5595 bs->commands = b->commands;
5596 if (command_line_is_silent (bs->commands
5597 ? bs->commands.get () : NULL))
5598 bs->print = 0;
5599
5600 b->after_condition_true (bs);
5601 }
5602
5603 }
5604
5605 /* Print nothing for this entry if we don't stop or don't
5606 print. */
5607 if (!bs->stop || !bs->print)
5608 bs->print_it = print_it_noop;
5609 }
5610
5611 /* If we aren't stopping, the value of some hardware watchpoint may
5612 not have changed, but the intermediate memory locations we are
5613 watching may have. Don't bother if we're stopping; this will get
5614 done later. */
5615 need_remove_insert = 0;
5616 if (! bpstat_causes_stop (bs_head))
5617 for (bs = bs_head; bs != NULL; bs = bs->next)
5618 if (!bs->stop
5619 && bs->breakpoint_at
5620 && is_hardware_watchpoint (bs->breakpoint_at))
5621 {
5622 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5623
5624 update_watchpoint (w, 0 /* don't reparse. */);
5625 need_remove_insert = 1;
5626 }
5627
5628 if (need_remove_insert)
5629 update_global_location_list (UGLL_MAY_INSERT);
5630 else if (removed_any)
5631 update_global_location_list (UGLL_DONT_INSERT);
5632
5633 return bs_head;
5634 }
5635
5636 /* See breakpoint.h. */
5637
5638 bpstat *
5639 bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5640 thread_info *thread, const target_waitstatus &ws)
5641 {
5642 gdb_assert (!target_stopped_by_watchpoint ());
5643
5644 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5645 previous stop to avoid confusing bpstat_stop_status. */
5646 watchpoints_triggered (ws);
5647
5648 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5649 }
5650
5651 static void
5652 handle_jit_event (CORE_ADDR address)
5653 {
5654 struct gdbarch *gdbarch;
5655
5656 infrun_debug_printf ("handling bp_jit_event");
5657
5658 /* Switch terminal for any messages produced by
5659 breakpoint_re_set. */
5660 target_terminal::ours_for_output ();
5661
5662 gdbarch = get_frame_arch (get_current_frame ());
5663 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5664 thus it is expected that its objectfile can be found through
5665 minimal symbol lookup. If it doesn't work (and assert fails), it
5666 most likely means that `jit_breakpoint_re_set` was changes and this
5667 function needs to be updated too. */
5668 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5669 gdb_assert (jit_bp_sym.objfile != nullptr);
5670 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5671
5672 target_terminal::inferior ();
5673 }
5674
5675 /* Prepare WHAT final decision for infrun. */
5676
5677 /* Decide what infrun needs to do with this bpstat. */
5678
5679 struct bpstat_what
5680 bpstat_what (bpstat *bs_head)
5681 {
5682 struct bpstat_what retval;
5683 bpstat *bs;
5684
5685 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5686 retval.call_dummy = STOP_NONE;
5687 retval.is_longjmp = false;
5688
5689 for (bs = bs_head; bs != NULL; bs = bs->next)
5690 {
5691 /* Extract this BS's action. After processing each BS, we check
5692 if its action overrides all we've seem so far. */
5693 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5694 enum bptype bptype;
5695
5696 if (bs->breakpoint_at == NULL)
5697 {
5698 /* I suspect this can happen if it was a momentary
5699 breakpoint which has since been deleted. */
5700 bptype = bp_none;
5701 }
5702 else
5703 bptype = bs->breakpoint_at->type;
5704
5705 switch (bptype)
5706 {
5707 case bp_none:
5708 break;
5709 case bp_breakpoint:
5710 case bp_hardware_breakpoint:
5711 case bp_single_step:
5712 case bp_until:
5713 case bp_finish:
5714 case bp_shlib_event:
5715 if (bs->stop)
5716 {
5717 if (bs->print)
5718 this_action = BPSTAT_WHAT_STOP_NOISY;
5719 else
5720 this_action = BPSTAT_WHAT_STOP_SILENT;
5721 }
5722 else
5723 this_action = BPSTAT_WHAT_SINGLE;
5724 break;
5725 case bp_watchpoint:
5726 case bp_hardware_watchpoint:
5727 case bp_read_watchpoint:
5728 case bp_access_watchpoint:
5729 if (bs->stop)
5730 {
5731 if (bs->print)
5732 this_action = BPSTAT_WHAT_STOP_NOISY;
5733 else
5734 this_action = BPSTAT_WHAT_STOP_SILENT;
5735 }
5736 else
5737 {
5738 /* There was a watchpoint, but we're not stopping.
5739 This requires no further action. */
5740 }
5741 break;
5742 case bp_longjmp:
5743 case bp_longjmp_call_dummy:
5744 case bp_exception:
5745 if (bs->stop)
5746 {
5747 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5748 retval.is_longjmp = bptype != bp_exception;
5749 }
5750 else
5751 this_action = BPSTAT_WHAT_SINGLE;
5752 break;
5753 case bp_longjmp_resume:
5754 case bp_exception_resume:
5755 if (bs->stop)
5756 {
5757 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5758 retval.is_longjmp = bptype == bp_longjmp_resume;
5759 }
5760 else
5761 this_action = BPSTAT_WHAT_SINGLE;
5762 break;
5763 case bp_step_resume:
5764 if (bs->stop)
5765 this_action = BPSTAT_WHAT_STEP_RESUME;
5766 else
5767 {
5768 /* It is for the wrong frame. */
5769 this_action = BPSTAT_WHAT_SINGLE;
5770 }
5771 break;
5772 case bp_hp_step_resume:
5773 if (bs->stop)
5774 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5775 else
5776 {
5777 /* It is for the wrong frame. */
5778 this_action = BPSTAT_WHAT_SINGLE;
5779 }
5780 break;
5781 case bp_watchpoint_scope:
5782 case bp_thread_event:
5783 case bp_overlay_event:
5784 case bp_longjmp_master:
5785 case bp_std_terminate_master:
5786 case bp_exception_master:
5787 this_action = BPSTAT_WHAT_SINGLE;
5788 break;
5789 case bp_catchpoint:
5790 if (bs->stop)
5791 {
5792 if (bs->print)
5793 this_action = BPSTAT_WHAT_STOP_NOISY;
5794 else
5795 this_action = BPSTAT_WHAT_STOP_SILENT;
5796 }
5797 else
5798 {
5799 /* Some catchpoints are implemented with breakpoints.
5800 For those, we need to step over the breakpoint. */
5801 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
5802 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
5803 this_action = BPSTAT_WHAT_SINGLE;
5804 }
5805 break;
5806 case bp_jit_event:
5807 this_action = BPSTAT_WHAT_SINGLE;
5808 break;
5809 case bp_call_dummy:
5810 /* Make sure the action is stop (silent or noisy),
5811 so infrun.c pops the dummy frame. */
5812 retval.call_dummy = STOP_STACK_DUMMY;
5813 this_action = BPSTAT_WHAT_STOP_SILENT;
5814 break;
5815 case bp_std_terminate:
5816 /* Make sure the action is stop (silent or noisy),
5817 so infrun.c pops the dummy frame. */
5818 retval.call_dummy = STOP_STD_TERMINATE;
5819 this_action = BPSTAT_WHAT_STOP_SILENT;
5820 break;
5821 case bp_tracepoint:
5822 case bp_fast_tracepoint:
5823 case bp_static_tracepoint:
5824 case bp_static_marker_tracepoint:
5825 /* Tracepoint hits should not be reported back to GDB, and
5826 if one got through somehow, it should have been filtered
5827 out already. */
5828 internal_error (__FILE__, __LINE__,
5829 _("bpstat_what: tracepoint encountered"));
5830 break;
5831 case bp_gnu_ifunc_resolver:
5832 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5833 this_action = BPSTAT_WHAT_SINGLE;
5834 break;
5835 case bp_gnu_ifunc_resolver_return:
5836 /* The breakpoint will be removed, execution will restart from the
5837 PC of the former breakpoint. */
5838 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5839 break;
5840
5841 case bp_dprintf:
5842 if (bs->stop)
5843 this_action = BPSTAT_WHAT_STOP_SILENT;
5844 else
5845 this_action = BPSTAT_WHAT_SINGLE;
5846 break;
5847
5848 default:
5849 internal_error (__FILE__, __LINE__,
5850 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5851 }
5852
5853 retval.main_action = std::max (retval.main_action, this_action);
5854 }
5855
5856 return retval;
5857 }
5858
5859 void
5860 bpstat_run_callbacks (bpstat *bs_head)
5861 {
5862 bpstat *bs;
5863
5864 for (bs = bs_head; bs != NULL; bs = bs->next)
5865 {
5866 struct breakpoint *b = bs->breakpoint_at;
5867
5868 if (b == NULL)
5869 continue;
5870 switch (b->type)
5871 {
5872 case bp_jit_event:
5873 handle_jit_event (bs->bp_location_at->address);
5874 break;
5875 case bp_gnu_ifunc_resolver:
5876 gnu_ifunc_resolver_stop ((code_breakpoint *) b);
5877 break;
5878 case bp_gnu_ifunc_resolver_return:
5879 gnu_ifunc_resolver_return_stop ((code_breakpoint *) b);
5880 break;
5881 }
5882 }
5883 }
5884
5885 /* See breakpoint.h. */
5886
5887 bool
5888 bpstat_should_step ()
5889 {
5890 for (breakpoint *b : all_breakpoints ())
5891 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5892 return true;
5893
5894 return false;
5895 }
5896
5897 /* See breakpoint.h. */
5898
5899 bool
5900 bpstat_causes_stop (bpstat *bs)
5901 {
5902 for (; bs != NULL; bs = bs->next)
5903 if (bs->stop)
5904 return true;
5905
5906 return false;
5907 }
5908
5909 \f
5910
5911 /* Compute a number of spaces suitable to indent the next line
5912 so it starts at the position corresponding to the table column
5913 named COL_NAME in the currently active table of UIOUT. */
5914
5915 static int
5916 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5917 {
5918 int i, total_width, width, align;
5919 const char *text;
5920
5921 total_width = 0;
5922 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5923 {
5924 if (strcmp (text, col_name) == 0)
5925 return total_width;
5926
5927 total_width += width + 1;
5928 }
5929
5930 return 0;
5931 }
5932
5933 /* Determine if the locations of this breakpoint will have their conditions
5934 evaluated by the target, host or a mix of both. Returns the following:
5935
5936 "host": Host evals condition.
5937 "host or target": Host or Target evals condition.
5938 "target": Target evals condition.
5939 */
5940
5941 static const char *
5942 bp_condition_evaluator (const breakpoint *b)
5943 {
5944 char host_evals = 0;
5945 char target_evals = 0;
5946
5947 if (!b)
5948 return NULL;
5949
5950 if (!is_breakpoint (b))
5951 return NULL;
5952
5953 if (gdb_evaluates_breakpoint_condition_p ()
5954 || !target_supports_evaluation_of_breakpoint_conditions ())
5955 return condition_evaluation_host;
5956
5957 for (bp_location *bl : b->locations ())
5958 {
5959 if (bl->cond_bytecode)
5960 target_evals++;
5961 else
5962 host_evals++;
5963 }
5964
5965 if (host_evals && target_evals)
5966 return condition_evaluation_both;
5967 else if (target_evals)
5968 return condition_evaluation_target;
5969 else
5970 return condition_evaluation_host;
5971 }
5972
5973 /* Determine the breakpoint location's condition evaluator. This is
5974 similar to bp_condition_evaluator, but for locations. */
5975
5976 static const char *
5977 bp_location_condition_evaluator (struct bp_location *bl)
5978 {
5979 if (bl && !is_breakpoint (bl->owner))
5980 return NULL;
5981
5982 if (gdb_evaluates_breakpoint_condition_p ()
5983 || !target_supports_evaluation_of_breakpoint_conditions ())
5984 return condition_evaluation_host;
5985
5986 if (bl && bl->cond_bytecode)
5987 return condition_evaluation_target;
5988 else
5989 return condition_evaluation_host;
5990 }
5991
5992 /* Print the LOC location out of the list of B->LOC locations. */
5993
5994 static void
5995 print_breakpoint_location (const breakpoint *b,
5996 struct bp_location *loc)
5997 {
5998 struct ui_out *uiout = current_uiout;
5999
6000 scoped_restore_current_program_space restore_pspace;
6001
6002 if (loc != NULL && loc->shlib_disabled)
6003 loc = NULL;
6004
6005 if (loc != NULL)
6006 set_current_program_space (loc->pspace);
6007
6008 if (b->display_canonical)
6009 uiout->field_string ("what", b->locspec->to_string ());
6010 else if (loc && loc->symtab)
6011 {
6012 const struct symbol *sym = loc->symbol;
6013
6014 if (sym)
6015 {
6016 uiout->text ("in ");
6017 uiout->field_string ("func", sym->print_name (),
6018 function_name_style.style ());
6019 uiout->text (" ");
6020 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6021 uiout->text ("at ");
6022 }
6023 uiout->field_string ("file",
6024 symtab_to_filename_for_display (loc->symtab),
6025 file_name_style.style ());
6026 uiout->text (":");
6027
6028 if (uiout->is_mi_like_p ())
6029 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6030
6031 uiout->field_signed ("line", loc->line_number);
6032 }
6033 else if (loc)
6034 {
6035 string_file stb;
6036
6037 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6038 demangle, "");
6039 uiout->field_stream ("at", stb);
6040 }
6041 else
6042 {
6043 uiout->field_string ("pending", b->locspec->to_string ());
6044 /* If extra_string is available, it could be holding a condition
6045 or dprintf arguments. In either case, make sure it is printed,
6046 too, but only for non-MI streams. */
6047 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6048 {
6049 if (b->type == bp_dprintf)
6050 uiout->text (",");
6051 else
6052 uiout->text (" ");
6053 uiout->text (b->extra_string.get ());
6054 }
6055 }
6056
6057 if (loc && is_breakpoint (b)
6058 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6059 && bp_condition_evaluator (b) == condition_evaluation_both)
6060 {
6061 uiout->text (" (");
6062 uiout->field_string ("evaluated-by",
6063 bp_location_condition_evaluator (loc));
6064 uiout->text (")");
6065 }
6066 }
6067
6068 static const char *
6069 bptype_string (enum bptype type)
6070 {
6071 struct ep_type_description
6072 {
6073 enum bptype type;
6074 const char *description;
6075 };
6076 static struct ep_type_description bptypes[] =
6077 {
6078 {bp_none, "?deleted?"},
6079 {bp_breakpoint, "breakpoint"},
6080 {bp_hardware_breakpoint, "hw breakpoint"},
6081 {bp_single_step, "sw single-step"},
6082 {bp_until, "until"},
6083 {bp_finish, "finish"},
6084 {bp_watchpoint, "watchpoint"},
6085 {bp_hardware_watchpoint, "hw watchpoint"},
6086 {bp_read_watchpoint, "read watchpoint"},
6087 {bp_access_watchpoint, "acc watchpoint"},
6088 {bp_longjmp, "longjmp"},
6089 {bp_longjmp_resume, "longjmp resume"},
6090 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6091 {bp_exception, "exception"},
6092 {bp_exception_resume, "exception resume"},
6093 {bp_step_resume, "step resume"},
6094 {bp_hp_step_resume, "high-priority step resume"},
6095 {bp_watchpoint_scope, "watchpoint scope"},
6096 {bp_call_dummy, "call dummy"},
6097 {bp_std_terminate, "std::terminate"},
6098 {bp_shlib_event, "shlib events"},
6099 {bp_thread_event, "thread events"},
6100 {bp_overlay_event, "overlay events"},
6101 {bp_longjmp_master, "longjmp master"},
6102 {bp_std_terminate_master, "std::terminate master"},
6103 {bp_exception_master, "exception master"},
6104 {bp_catchpoint, "catchpoint"},
6105 {bp_tracepoint, "tracepoint"},
6106 {bp_fast_tracepoint, "fast tracepoint"},
6107 {bp_static_tracepoint, "static tracepoint"},
6108 {bp_static_marker_tracepoint, "static marker tracepoint"},
6109 {bp_dprintf, "dprintf"},
6110 {bp_jit_event, "jit events"},
6111 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6112 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6113 };
6114
6115 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6116 || ((int) type != bptypes[(int) type].type))
6117 internal_error (__FILE__, __LINE__,
6118 _("bptypes table does not describe type #%d."),
6119 (int) type);
6120
6121 return bptypes[(int) type].description;
6122 }
6123
6124 /* For MI, output a field named 'thread-groups' with a list as the value.
6125 For CLI, prefix the list with the string 'inf'. */
6126
6127 static void
6128 output_thread_groups (struct ui_out *uiout,
6129 const char *field_name,
6130 const std::vector<int> &inf_nums,
6131 int mi_only)
6132 {
6133 int is_mi = uiout->is_mi_like_p ();
6134
6135 /* For backward compatibility, don't display inferiors in CLI unless
6136 there are several. Always display them for MI. */
6137 if (!is_mi && mi_only)
6138 return;
6139
6140 ui_out_emit_list list_emitter (uiout, field_name);
6141
6142 for (size_t i = 0; i < inf_nums.size (); i++)
6143 {
6144 if (is_mi)
6145 {
6146 char mi_group[10];
6147
6148 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6149 uiout->field_string (NULL, mi_group);
6150 }
6151 else
6152 {
6153 if (i == 0)
6154 uiout->text (" inf ");
6155 else
6156 uiout->text (", ");
6157
6158 uiout->text (plongest (inf_nums[i]));
6159 }
6160 }
6161 }
6162
6163 /* See breakpoint.h. */
6164
6165 bool fix_breakpoint_script_output_globally = false;
6166
6167 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6168 instead of going via breakpoint_ops::print_one. This makes "maint
6169 info breakpoints" show the software breakpoint locations of
6170 catchpoints, which are considered internal implementation
6171 detail. Returns true if RAW_LOC is false and if the breakpoint's
6172 print_one method did something; false otherwise. */
6173
6174 static bool
6175 print_one_breakpoint_location (struct breakpoint *b,
6176 struct bp_location *loc,
6177 int loc_number,
6178 struct bp_location **last_loc,
6179 int allflag, bool raw_loc)
6180 {
6181 struct command_line *l;
6182 static char bpenables[] = "nynny";
6183
6184 struct ui_out *uiout = current_uiout;
6185 int header_of_multiple = 0;
6186 int part_of_multiple = (loc != NULL);
6187 struct value_print_options opts;
6188
6189 get_user_print_options (&opts);
6190
6191 gdb_assert (!loc || loc_number != 0);
6192 /* See comment in print_one_breakpoint concerning treatment of
6193 breakpoints with single disabled location. */
6194 if (loc == NULL
6195 && (b->loc != NULL
6196 && (b->loc->next != NULL
6197 || !b->loc->enabled || b->loc->disabled_by_cond)))
6198 header_of_multiple = 1;
6199 if (loc == NULL)
6200 loc = b->loc;
6201
6202 annotate_record ();
6203
6204 /* 1 */
6205 annotate_field (0);
6206 if (part_of_multiple)
6207 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6208 else
6209 uiout->field_signed ("number", b->number);
6210
6211 /* 2 */
6212 annotate_field (1);
6213 if (part_of_multiple)
6214 uiout->field_skip ("type");
6215 else
6216 uiout->field_string ("type", bptype_string (b->type));
6217
6218 /* 3 */
6219 annotate_field (2);
6220 if (part_of_multiple)
6221 uiout->field_skip ("disp");
6222 else
6223 uiout->field_string ("disp", bpdisp_text (b->disposition));
6224
6225 /* 4 */
6226 annotate_field (3);
6227 if (part_of_multiple)
6228 {
6229 /* For locations that are disabled because of an invalid
6230 condition, display "N*" on the CLI, where "*" refers to a
6231 footnote below the table. For MI, simply display a "N"
6232 without a footnote. On the CLI, for enabled locations whose
6233 breakpoint is disabled, display "y-". */
6234 auto get_enable_state = [uiout, loc] () -> const char *
6235 {
6236 if (uiout->is_mi_like_p ())
6237 {
6238 if (loc->disabled_by_cond)
6239 return "N";
6240 else if (!loc->enabled)
6241 return "n";
6242 else
6243 return "y";
6244 }
6245 else
6246 {
6247 if (loc->disabled_by_cond)
6248 return "N*";
6249 else if (!loc->enabled)
6250 return "n";
6251 else if (!breakpoint_enabled (loc->owner))
6252 return "y-";
6253 else
6254 return "y";
6255 }
6256 };
6257 uiout->field_string ("enabled", get_enable_state ());
6258 }
6259 else
6260 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6261
6262 /* 5 and 6 */
6263 bool result = false;
6264 if (!raw_loc && b->print_one (last_loc))
6265 result = true;
6266 else
6267 {
6268 if (is_watchpoint (b))
6269 {
6270 struct watchpoint *w = (struct watchpoint *) b;
6271
6272 /* Field 4, the address, is omitted (which makes the columns
6273 not line up too nicely with the headers, but the effect
6274 is relatively readable). */
6275 if (opts.addressprint)
6276 uiout->field_skip ("addr");
6277 annotate_field (5);
6278 uiout->field_string ("what", w->exp_string.get ());
6279 }
6280 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6281 || is_ada_exception_catchpoint (b))
6282 {
6283 if (opts.addressprint)
6284 {
6285 annotate_field (4);
6286 if (header_of_multiple)
6287 uiout->field_string ("addr", "<MULTIPLE>",
6288 metadata_style.style ());
6289 else if (b->loc == NULL || loc->shlib_disabled)
6290 uiout->field_string ("addr", "<PENDING>",
6291 metadata_style.style ());
6292 else
6293 uiout->field_core_addr ("addr",
6294 loc->gdbarch, loc->address);
6295 }
6296 annotate_field (5);
6297 if (!header_of_multiple)
6298 print_breakpoint_location (b, loc);
6299 if (b->loc)
6300 *last_loc = b->loc;
6301 }
6302 }
6303
6304 if (loc != NULL && !header_of_multiple)
6305 {
6306 std::vector<int> inf_nums;
6307 int mi_only = 1;
6308
6309 for (inferior *inf : all_inferiors ())
6310 {
6311 if (inf->pspace == loc->pspace)
6312 inf_nums.push_back (inf->num);
6313 }
6314
6315 /* For backward compatibility, don't display inferiors in CLI unless
6316 there are several. Always display for MI. */
6317 if (allflag
6318 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6319 && (program_spaces.size () > 1
6320 || number_of_inferiors () > 1)
6321 /* LOC is for existing B, it cannot be in
6322 moribund_locations and thus having NULL OWNER. */
6323 && loc->owner->type != bp_catchpoint))
6324 mi_only = 0;
6325 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6326 }
6327
6328 if (!part_of_multiple)
6329 {
6330 if (b->thread != -1)
6331 {
6332 /* FIXME: This seems to be redundant and lost here; see the
6333 "stop only in" line a little further down. */
6334 uiout->text (" thread ");
6335 uiout->field_signed ("thread", b->thread);
6336 }
6337 else if (b->task != 0)
6338 {
6339 uiout->text (" task ");
6340 uiout->field_signed ("task", b->task);
6341 }
6342 }
6343
6344 uiout->text ("\n");
6345
6346 if (!part_of_multiple)
6347 b->print_one_detail (uiout);
6348
6349 if (part_of_multiple && frame_id_p (b->frame_id))
6350 {
6351 annotate_field (6);
6352 uiout->text ("\tstop only in stack frame at ");
6353 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6354 the frame ID. */
6355 uiout->field_core_addr ("frame",
6356 b->gdbarch, b->frame_id.stack_addr);
6357 uiout->text ("\n");
6358 }
6359
6360 if (!part_of_multiple && b->cond_string)
6361 {
6362 annotate_field (7);
6363 if (is_tracepoint (b))
6364 uiout->text ("\ttrace only if ");
6365 else
6366 uiout->text ("\tstop only if ");
6367 uiout->field_string ("cond", b->cond_string.get ());
6368
6369 /* Print whether the target is doing the breakpoint's condition
6370 evaluation. If GDB is doing the evaluation, don't print anything. */
6371 if (is_breakpoint (b)
6372 && breakpoint_condition_evaluation_mode ()
6373 == condition_evaluation_target)
6374 {
6375 uiout->message (" (%pF evals)",
6376 string_field ("evaluated-by",
6377 bp_condition_evaluator (b)));
6378 }
6379 uiout->text ("\n");
6380 }
6381
6382 if (!part_of_multiple && b->thread != -1)
6383 {
6384 /* FIXME should make an annotation for this. */
6385 uiout->text ("\tstop only in thread ");
6386 if (uiout->is_mi_like_p ())
6387 uiout->field_signed ("thread", b->thread);
6388 else
6389 {
6390 struct thread_info *thr = find_thread_global_id (b->thread);
6391
6392 uiout->field_string ("thread", print_thread_id (thr));
6393 }
6394 uiout->text ("\n");
6395 }
6396
6397 if (!part_of_multiple)
6398 {
6399 if (b->hit_count)
6400 {
6401 /* FIXME should make an annotation for this. */
6402 if (is_catchpoint (b))
6403 uiout->text ("\tcatchpoint");
6404 else if (is_tracepoint (b))
6405 uiout->text ("\ttracepoint");
6406 else
6407 uiout->text ("\tbreakpoint");
6408 uiout->text (" already hit ");
6409 uiout->field_signed ("times", b->hit_count);
6410 if (b->hit_count == 1)
6411 uiout->text (" time\n");
6412 else
6413 uiout->text (" times\n");
6414 }
6415 else
6416 {
6417 /* Output the count also if it is zero, but only if this is mi. */
6418 if (uiout->is_mi_like_p ())
6419 uiout->field_signed ("times", b->hit_count);
6420 }
6421 }
6422
6423 if (!part_of_multiple && b->ignore_count)
6424 {
6425 annotate_field (8);
6426 uiout->message ("\tignore next %pF hits\n",
6427 signed_field ("ignore", b->ignore_count));
6428 }
6429
6430 /* Note that an enable count of 1 corresponds to "enable once"
6431 behavior, which is reported by the combination of enablement and
6432 disposition, so we don't need to mention it here. */
6433 if (!part_of_multiple && b->enable_count > 1)
6434 {
6435 annotate_field (8);
6436 uiout->text ("\tdisable after ");
6437 /* Tweak the wording to clarify that ignore and enable counts
6438 are distinct, and have additive effect. */
6439 if (b->ignore_count)
6440 uiout->text ("additional ");
6441 else
6442 uiout->text ("next ");
6443 uiout->field_signed ("enable", b->enable_count);
6444 uiout->text (" hits\n");
6445 }
6446
6447 if (!part_of_multiple && is_tracepoint (b))
6448 {
6449 struct tracepoint *tp = (struct tracepoint *) b;
6450
6451 if (tp->traceframe_usage)
6452 {
6453 uiout->text ("\ttrace buffer usage ");
6454 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6455 uiout->text (" bytes\n");
6456 }
6457 }
6458
6459 l = b->commands ? b->commands.get () : NULL;
6460 if (!part_of_multiple && l)
6461 {
6462 annotate_field (9);
6463
6464 bool use_fixed_output =
6465 (uiout->test_flags (fix_breakpoint_script_output)
6466 || fix_breakpoint_script_output_globally);
6467
6468 gdb::optional<ui_out_emit_tuple> tuple_emitter;
6469 gdb::optional<ui_out_emit_list> list_emitter;
6470
6471 if (use_fixed_output)
6472 list_emitter.emplace (uiout, "script");
6473 else
6474 tuple_emitter.emplace (uiout, "script");
6475
6476 print_command_lines (uiout, l, 4);
6477 }
6478
6479 if (is_tracepoint (b))
6480 {
6481 struct tracepoint *t = (struct tracepoint *) b;
6482
6483 if (!part_of_multiple && t->pass_count)
6484 {
6485 annotate_field (10);
6486 uiout->text ("\tpass count ");
6487 uiout->field_signed ("pass", t->pass_count);
6488 uiout->text (" \n");
6489 }
6490
6491 /* Don't display it when tracepoint or tracepoint location is
6492 pending. */
6493 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6494 {
6495 annotate_field (11);
6496
6497 if (uiout->is_mi_like_p ())
6498 uiout->field_string ("installed",
6499 loc->inserted ? "y" : "n");
6500 else
6501 {
6502 if (loc->inserted)
6503 uiout->text ("\t");
6504 else
6505 uiout->text ("\tnot ");
6506 uiout->text ("installed on target\n");
6507 }
6508 }
6509 }
6510
6511 if (uiout->is_mi_like_p () && !part_of_multiple)
6512 {
6513 if (is_watchpoint (b))
6514 {
6515 struct watchpoint *w = (struct watchpoint *) b;
6516
6517 uiout->field_string ("original-location", w->exp_string.get ());
6518 }
6519 else if (b->locspec != nullptr)
6520 {
6521 const char *str = b->locspec->to_string ();
6522 if (str != nullptr)
6523 uiout->field_string ("original-location", str);
6524 }
6525 }
6526
6527 return result;
6528 }
6529
6530 /* See breakpoint.h. */
6531
6532 bool fix_multi_location_breakpoint_output_globally = false;
6533
6534 static void
6535 print_one_breakpoint (struct breakpoint *b,
6536 struct bp_location **last_loc,
6537 int allflag)
6538 {
6539 struct ui_out *uiout = current_uiout;
6540 bool use_fixed_output
6541 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6542 || fix_multi_location_breakpoint_output_globally);
6543
6544 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6545 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6546 allflag, false);
6547
6548 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6549 are outside. */
6550 if (!use_fixed_output)
6551 bkpt_tuple_emitter.reset ();
6552
6553 /* If this breakpoint has custom print function,
6554 it's already printed. Otherwise, print individual
6555 locations, if any. */
6556 if (!printed || allflag)
6557 {
6558 /* If breakpoint has a single location that is disabled, we
6559 print it as if it had several locations, since otherwise it's
6560 hard to represent "breakpoint enabled, location disabled"
6561 situation.
6562
6563 Note that while hardware watchpoints have several locations
6564 internally, that's not a property exposed to users.
6565
6566 Likewise, while catchpoints may be implemented with
6567 breakpoints (e.g., catch throw), that's not a property
6568 exposed to users. We do however display the internal
6569 breakpoint locations with "maint info breakpoints". */
6570 if (!is_hardware_watchpoint (b)
6571 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6572 || is_ada_exception_catchpoint (b))
6573 && (allflag
6574 || (b->loc && (b->loc->next
6575 || !b->loc->enabled
6576 || b->loc->disabled_by_cond))))
6577 {
6578 gdb::optional<ui_out_emit_list> locations_list;
6579
6580 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6581 MI record. For later versions, place breakpoint locations in a
6582 list. */
6583 if (uiout->is_mi_like_p () && use_fixed_output)
6584 locations_list.emplace (uiout, "locations");
6585
6586 int n = 1;
6587 for (bp_location *loc : b->locations ())
6588 {
6589 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6590 print_one_breakpoint_location (b, loc, n, last_loc,
6591 allflag, allflag);
6592 n++;
6593 }
6594 }
6595 }
6596 }
6597
6598 static int
6599 breakpoint_address_bits (struct breakpoint *b)
6600 {
6601 int print_address_bits = 0;
6602
6603 for (bp_location *loc : b->locations ())
6604 {
6605 if (!bl_address_is_meaningful (loc))
6606 continue;
6607
6608 int addr_bit = gdbarch_addr_bit (loc->gdbarch);
6609 if (addr_bit > print_address_bits)
6610 print_address_bits = addr_bit;
6611 }
6612
6613 return print_address_bits;
6614 }
6615
6616 /* See breakpoint.h. */
6617
6618 void
6619 print_breakpoint (breakpoint *b)
6620 {
6621 struct bp_location *dummy_loc = NULL;
6622 print_one_breakpoint (b, &dummy_loc, 0);
6623 }
6624
6625 /* Return true if this breakpoint was set by the user, false if it is
6626 internal or momentary. */
6627
6628 int
6629 user_breakpoint_p (struct breakpoint *b)
6630 {
6631 return b->number > 0;
6632 }
6633
6634 /* See breakpoint.h. */
6635
6636 int
6637 pending_breakpoint_p (struct breakpoint *b)
6638 {
6639 return b->loc == NULL;
6640 }
6641
6642 /* Print information on breakpoints (including watchpoints and tracepoints).
6643
6644 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6645 understood by number_or_range_parser. Only breakpoints included in this
6646 list are then printed.
6647
6648 If SHOW_INTERNAL is true, print internal breakpoints.
6649
6650 If FILTER is non-NULL, call it on each breakpoint and only include the
6651 ones for which it returns true.
6652
6653 Return the total number of breakpoints listed. */
6654
6655 static int
6656 breakpoint_1 (const char *bp_num_list, bool show_internal,
6657 bool (*filter) (const struct breakpoint *))
6658 {
6659 struct bp_location *last_loc = NULL;
6660 int nr_printable_breakpoints;
6661 struct value_print_options opts;
6662 int print_address_bits = 0;
6663 int print_type_col_width = 14;
6664 struct ui_out *uiout = current_uiout;
6665 bool has_disabled_by_cond_location = false;
6666
6667 get_user_print_options (&opts);
6668
6669 /* Compute the number of rows in the table, as well as the size
6670 required for address fields. */
6671 nr_printable_breakpoints = 0;
6672 for (breakpoint *b : all_breakpoints ())
6673 {
6674 /* If we have a filter, only list the breakpoints it accepts. */
6675 if (filter && !filter (b))
6676 continue;
6677
6678 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6679 accept. Skip the others. */
6680 if (bp_num_list != NULL && *bp_num_list != '\0')
6681 {
6682 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6683 continue;
6684 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6685 continue;
6686 }
6687
6688 if (show_internal || user_breakpoint_p (b))
6689 {
6690 int addr_bit, type_len;
6691
6692 addr_bit = breakpoint_address_bits (b);
6693 if (addr_bit > print_address_bits)
6694 print_address_bits = addr_bit;
6695
6696 type_len = strlen (bptype_string (b->type));
6697 if (type_len > print_type_col_width)
6698 print_type_col_width = type_len;
6699
6700 nr_printable_breakpoints++;
6701 }
6702 }
6703
6704 {
6705 ui_out_emit_table table_emitter (uiout,
6706 opts.addressprint ? 6 : 5,
6707 nr_printable_breakpoints,
6708 "BreakpointTable");
6709
6710 if (nr_printable_breakpoints > 0)
6711 annotate_breakpoints_headers ();
6712 if (nr_printable_breakpoints > 0)
6713 annotate_field (0);
6714 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6715 if (nr_printable_breakpoints > 0)
6716 annotate_field (1);
6717 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6718 if (nr_printable_breakpoints > 0)
6719 annotate_field (2);
6720 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6721 if (nr_printable_breakpoints > 0)
6722 annotate_field (3);
6723 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6724 if (opts.addressprint)
6725 {
6726 if (nr_printable_breakpoints > 0)
6727 annotate_field (4);
6728 if (print_address_bits <= 32)
6729 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6730 else
6731 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6732 }
6733 if (nr_printable_breakpoints > 0)
6734 annotate_field (5);
6735 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6736 uiout->table_body ();
6737 if (nr_printable_breakpoints > 0)
6738 annotate_breakpoints_table ();
6739
6740 for (breakpoint *b : all_breakpoints ())
6741 {
6742 QUIT;
6743 /* If we have a filter, only list the breakpoints it accepts. */
6744 if (filter && !filter (b))
6745 continue;
6746
6747 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6748 accept. Skip the others. */
6749
6750 if (bp_num_list != NULL && *bp_num_list != '\0')
6751 {
6752 if (show_internal) /* maintenance info breakpoint */
6753 {
6754 if (parse_and_eval_long (bp_num_list) != b->number)
6755 continue;
6756 }
6757 else /* all others */
6758 {
6759 if (!number_is_in_list (bp_num_list, b->number))
6760 continue;
6761 }
6762 }
6763 /* We only print out user settable breakpoints unless the
6764 show_internal is set. */
6765 if (show_internal || user_breakpoint_p (b))
6766 {
6767 print_one_breakpoint (b, &last_loc, show_internal);
6768 for (bp_location *loc : b->locations ())
6769 if (loc->disabled_by_cond)
6770 has_disabled_by_cond_location = true;
6771 }
6772 }
6773 }
6774
6775 if (nr_printable_breakpoints == 0)
6776 {
6777 /* If there's a filter, let the caller decide how to report
6778 empty list. */
6779 if (!filter)
6780 {
6781 if (bp_num_list == NULL || *bp_num_list == '\0')
6782 uiout->message ("No breakpoints or watchpoints.\n");
6783 else
6784 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6785 bp_num_list);
6786 }
6787 }
6788 else
6789 {
6790 if (last_loc && !server_command)
6791 set_next_address (last_loc->gdbarch, last_loc->address);
6792
6793 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6794 uiout->message (_("(*): Breakpoint condition is invalid at this "
6795 "location.\n"));
6796 }
6797
6798 /* FIXME? Should this be moved up so that it is only called when
6799 there have been breakpoints? */
6800 annotate_breakpoints_table_end ();
6801
6802 return nr_printable_breakpoints;
6803 }
6804
6805 /* Display the value of default-collect in a way that is generally
6806 compatible with the breakpoint list. */
6807
6808 static void
6809 default_collect_info (void)
6810 {
6811 struct ui_out *uiout = current_uiout;
6812
6813 /* If it has no value (which is frequently the case), say nothing; a
6814 message like "No default-collect." gets in user's face when it's
6815 not wanted. */
6816 if (default_collect.empty ())
6817 return;
6818
6819 /* The following phrase lines up nicely with per-tracepoint collect
6820 actions. */
6821 uiout->text ("default collect ");
6822 uiout->field_string ("default-collect", default_collect);
6823 uiout->text (" \n");
6824 }
6825
6826 static void
6827 info_breakpoints_command (const char *args, int from_tty)
6828 {
6829 breakpoint_1 (args, false, NULL);
6830
6831 default_collect_info ();
6832 }
6833
6834 static void
6835 info_watchpoints_command (const char *args, int from_tty)
6836 {
6837 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6838 struct ui_out *uiout = current_uiout;
6839
6840 if (num_printed == 0)
6841 {
6842 if (args == NULL || *args == '\0')
6843 uiout->message ("No watchpoints.\n");
6844 else
6845 uiout->message ("No watchpoint matching '%s'.\n", args);
6846 }
6847 }
6848
6849 static void
6850 maintenance_info_breakpoints (const char *args, int from_tty)
6851 {
6852 breakpoint_1 (args, true, NULL);
6853
6854 default_collect_info ();
6855 }
6856
6857 static int
6858 breakpoint_has_pc (struct breakpoint *b,
6859 struct program_space *pspace,
6860 CORE_ADDR pc, struct obj_section *section)
6861 {
6862 for (bp_location *bl : b->locations ())
6863 {
6864 if (bl->pspace == pspace
6865 && bl->address == pc
6866 && (!overlay_debugging || bl->section == section))
6867 return 1;
6868 }
6869 return 0;
6870 }
6871
6872 /* See breakpoint.h. */
6873
6874 void
6875 describe_other_breakpoints (struct gdbarch *gdbarch,
6876 struct program_space *pspace, CORE_ADDR pc,
6877 struct obj_section *section, int thread)
6878 {
6879 int others = 0;
6880
6881 for (breakpoint *b : all_breakpoints ())
6882 others += (user_breakpoint_p (b)
6883 && breakpoint_has_pc (b, pspace, pc, section));
6884
6885 if (others > 0)
6886 {
6887 if (others == 1)
6888 gdb_printf (_("Note: breakpoint "));
6889 else /* if (others == ???) */
6890 gdb_printf (_("Note: breakpoints "));
6891 for (breakpoint *b : all_breakpoints ())
6892 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6893 {
6894 others--;
6895 gdb_printf ("%d", b->number);
6896 if (b->thread == -1 && thread != -1)
6897 gdb_printf (" (all threads)");
6898 else if (b->thread != -1)
6899 gdb_printf (" (thread %d)", b->thread);
6900 gdb_printf ("%s%s ",
6901 ((b->enable_state == bp_disabled
6902 || b->enable_state == bp_call_disabled)
6903 ? " (disabled)"
6904 : ""),
6905 (others > 1) ? ","
6906 : ((others == 1) ? " and" : ""));
6907 }
6908 current_uiout->message (_("also set at pc %ps.\n"),
6909 styled_string (address_style.style (),
6910 paddress (gdbarch, pc)));
6911 }
6912 }
6913 \f
6914
6915 /* Return true iff it is meaningful to use the address member of LOC.
6916 For some breakpoint types, the locations' address members are
6917 irrelevant and it makes no sense to attempt to compare them to
6918 other addresses (or use them for any other purpose either).
6919
6920 More specifically, software watchpoints and catchpoints that are
6921 not backed by breakpoints always have a zero valued location
6922 address and we don't want to mark breakpoints of any of these types
6923 to be a duplicate of an actual breakpoint location at address
6924 zero. */
6925
6926 static bool
6927 bl_address_is_meaningful (bp_location *loc)
6928 {
6929 return loc->loc_type != bp_loc_other;
6930 }
6931
6932 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6933 true if LOC1 and LOC2 represent the same watchpoint location. */
6934
6935 static int
6936 watchpoint_locations_match (struct bp_location *loc1,
6937 struct bp_location *loc2)
6938 {
6939 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6940 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6941
6942 /* Both of them must exist. */
6943 gdb_assert (w1 != NULL);
6944 gdb_assert (w2 != NULL);
6945
6946 /* If the target can evaluate the condition expression in hardware,
6947 then we we need to insert both watchpoints even if they are at
6948 the same place. Otherwise the watchpoint will only trigger when
6949 the condition of whichever watchpoint was inserted evaluates to
6950 true, not giving a chance for GDB to check the condition of the
6951 other watchpoint. */
6952 if ((w1->cond_exp
6953 && target_can_accel_watchpoint_condition (loc1->address,
6954 loc1->length,
6955 loc1->watchpoint_type,
6956 w1->cond_exp.get ()))
6957 || (w2->cond_exp
6958 && target_can_accel_watchpoint_condition (loc2->address,
6959 loc2->length,
6960 loc2->watchpoint_type,
6961 w2->cond_exp.get ())))
6962 return 0;
6963
6964 /* Note that this checks the owner's type, not the location's. In
6965 case the target does not support read watchpoints, but does
6966 support access watchpoints, we'll have bp_read_watchpoint
6967 watchpoints with hw_access locations. Those should be considered
6968 duplicates of hw_read locations. The hw_read locations will
6969 become hw_access locations later. */
6970 return (loc1->owner->type == loc2->owner->type
6971 && loc1->pspace->aspace == loc2->pspace->aspace
6972 && loc1->address == loc2->address
6973 && loc1->length == loc2->length);
6974 }
6975
6976 /* See breakpoint.h. */
6977
6978 int
6979 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6980 const address_space *aspace2, CORE_ADDR addr2)
6981 {
6982 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6983 || aspace1 == aspace2)
6984 && addr1 == addr2);
6985 }
6986
6987 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6988 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6989 matches ASPACE2. On targets that have global breakpoints, the address
6990 space doesn't really matter. */
6991
6992 static int
6993 breakpoint_address_match_range (const address_space *aspace1,
6994 CORE_ADDR addr1,
6995 int len1, const address_space *aspace2,
6996 CORE_ADDR addr2)
6997 {
6998 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6999 || aspace1 == aspace2)
7000 && addr2 >= addr1 && addr2 < addr1 + len1);
7001 }
7002
7003 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7004 a ranged breakpoint. In most targets, a match happens only if ASPACE
7005 matches the breakpoint's address space. On targets that have global
7006 breakpoints, the address space doesn't really matter. */
7007
7008 static int
7009 breakpoint_location_address_match (struct bp_location *bl,
7010 const address_space *aspace,
7011 CORE_ADDR addr)
7012 {
7013 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7014 aspace, addr)
7015 || (bl->length
7016 && breakpoint_address_match_range (bl->pspace->aspace,
7017 bl->address, bl->length,
7018 aspace, addr)));
7019 }
7020
7021 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7022 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7023 match happens only if ASPACE matches the breakpoint's address
7024 space. On targets that have global breakpoints, the address space
7025 doesn't really matter. */
7026
7027 static int
7028 breakpoint_location_address_range_overlap (struct bp_location *bl,
7029 const address_space *aspace,
7030 CORE_ADDR addr, int len)
7031 {
7032 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7033 || bl->pspace->aspace == aspace)
7034 {
7035 int bl_len = bl->length != 0 ? bl->length : 1;
7036
7037 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7038 return 1;
7039 }
7040 return 0;
7041 }
7042
7043 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7044 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7045 true, otherwise returns false. */
7046
7047 static int
7048 tracepoint_locations_match (struct bp_location *loc1,
7049 struct bp_location *loc2)
7050 {
7051 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7052 /* Since tracepoint locations are never duplicated with others', tracepoint
7053 locations at the same address of different tracepoints are regarded as
7054 different locations. */
7055 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7056 else
7057 return 0;
7058 }
7059
7060 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7061 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7062 the same location. If SW_HW_BPS_MATCH is true, then software
7063 breakpoint locations and hardware breakpoint locations match,
7064 otherwise they don't. */
7065
7066 static int
7067 breakpoint_locations_match (struct bp_location *loc1,
7068 struct bp_location *loc2,
7069 bool sw_hw_bps_match)
7070 {
7071 int hw_point1, hw_point2;
7072
7073 /* Both of them must not be in moribund_locations. */
7074 gdb_assert (loc1->owner != NULL);
7075 gdb_assert (loc2->owner != NULL);
7076
7077 hw_point1 = is_hardware_watchpoint (loc1->owner);
7078 hw_point2 = is_hardware_watchpoint (loc2->owner);
7079
7080 if (hw_point1 != hw_point2)
7081 return 0;
7082 else if (hw_point1)
7083 return watchpoint_locations_match (loc1, loc2);
7084 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7085 return tracepoint_locations_match (loc1, loc2);
7086 else
7087 /* We compare bp_location.length in order to cover ranged
7088 breakpoints. Keep this in sync with
7089 bp_location_is_less_than. */
7090 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7091 loc2->pspace->aspace, loc2->address)
7092 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7093 && loc1->length == loc2->length);
7094 }
7095
7096 static void
7097 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7098 int bnum, int have_bnum)
7099 {
7100 /* The longest string possibly returned by hex_string_custom
7101 is 50 chars. These must be at least that big for safety. */
7102 char astr1[64];
7103 char astr2[64];
7104
7105 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7106 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7107 if (have_bnum)
7108 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7109 bnum, astr1, astr2);
7110 else
7111 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7112 }
7113
7114 /* Adjust a breakpoint's address to account for architectural
7115 constraints on breakpoint placement. Return the adjusted address.
7116 Note: Very few targets require this kind of adjustment. For most
7117 targets, this function is simply the identity function. */
7118
7119 static CORE_ADDR
7120 adjust_breakpoint_address (struct gdbarch *gdbarch,
7121 CORE_ADDR bpaddr, enum bptype bptype,
7122 struct program_space *pspace)
7123 {
7124 gdb_assert (pspace != nullptr);
7125
7126 if (bptype == bp_watchpoint
7127 || bptype == bp_hardware_watchpoint
7128 || bptype == bp_read_watchpoint
7129 || bptype == bp_access_watchpoint
7130 || bptype == bp_catchpoint)
7131 {
7132 /* Watchpoints and the various bp_catch_* eventpoints should not
7133 have their addresses modified. */
7134 return bpaddr;
7135 }
7136 else if (bptype == bp_single_step)
7137 {
7138 /* Single-step breakpoints should not have their addresses
7139 modified. If there's any architectural constrain that
7140 applies to this address, then it should have already been
7141 taken into account when the breakpoint was created in the
7142 first place. If we didn't do this, stepping through e.g.,
7143 Thumb-2 IT blocks would break. */
7144 return bpaddr;
7145 }
7146 else
7147 {
7148 CORE_ADDR adjusted_bpaddr = bpaddr;
7149
7150 /* Some targets have architectural constraints on the placement
7151 of breakpoint instructions. Obtain the adjusted address. */
7152 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7153 {
7154 /* Targets that implement this adjustment function will likely
7155 inspect either the symbol table, target memory at BPADDR, or
7156 even state registers, so ensure a suitable thread (and its
7157 associated program space) are currently selected. */
7158 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7159 switch_to_program_space_and_thread (pspace);
7160 adjusted_bpaddr
7161 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7162 }
7163
7164 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7165
7166 /* An adjusted breakpoint address can significantly alter
7167 a user's expectations. Print a warning if an adjustment
7168 is required. */
7169 if (adjusted_bpaddr != bpaddr)
7170 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7171
7172 return adjusted_bpaddr;
7173 }
7174 }
7175
7176 static bp_loc_type
7177 bp_location_from_bp_type (bptype type)
7178 {
7179 switch (type)
7180 {
7181 case bp_breakpoint:
7182 case bp_single_step:
7183 case bp_until:
7184 case bp_finish:
7185 case bp_longjmp:
7186 case bp_longjmp_resume:
7187 case bp_longjmp_call_dummy:
7188 case bp_exception:
7189 case bp_exception_resume:
7190 case bp_step_resume:
7191 case bp_hp_step_resume:
7192 case bp_watchpoint_scope:
7193 case bp_call_dummy:
7194 case bp_std_terminate:
7195 case bp_shlib_event:
7196 case bp_thread_event:
7197 case bp_overlay_event:
7198 case bp_jit_event:
7199 case bp_longjmp_master:
7200 case bp_std_terminate_master:
7201 case bp_exception_master:
7202 case bp_gnu_ifunc_resolver:
7203 case bp_gnu_ifunc_resolver_return:
7204 case bp_dprintf:
7205 return bp_loc_software_breakpoint;
7206 case bp_hardware_breakpoint:
7207 return bp_loc_hardware_breakpoint;
7208 case bp_hardware_watchpoint:
7209 case bp_read_watchpoint:
7210 case bp_access_watchpoint:
7211 return bp_loc_hardware_watchpoint;
7212 case bp_watchpoint:
7213 return bp_loc_software_watchpoint;
7214 case bp_catchpoint:
7215 case bp_tracepoint:
7216 case bp_fast_tracepoint:
7217 case bp_static_tracepoint:
7218 case bp_static_marker_tracepoint:
7219 return bp_loc_other;
7220 default:
7221 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7222 }
7223 }
7224
7225 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7226 {
7227 this->owner = owner;
7228 this->cond_bytecode = NULL;
7229 this->shlib_disabled = 0;
7230 this->enabled = 1;
7231 this->disabled_by_cond = false;
7232
7233 this->loc_type = type;
7234
7235 if (this->loc_type == bp_loc_software_breakpoint
7236 || this->loc_type == bp_loc_hardware_breakpoint)
7237 mark_breakpoint_location_modified (this);
7238
7239 incref ();
7240 }
7241
7242 bp_location::bp_location (breakpoint *owner)
7243 : bp_location::bp_location (owner,
7244 bp_location_from_bp_type (owner->type))
7245 {
7246 }
7247
7248 /* Decrement reference count. If the reference count reaches 0,
7249 destroy the bp_location. Sets *BLP to NULL. */
7250
7251 static void
7252 decref_bp_location (struct bp_location **blp)
7253 {
7254 bp_location_ref_policy::decref (*blp);
7255 *blp = NULL;
7256 }
7257
7258 /* Add breakpoint B at the end of the global breakpoint chain. */
7259
7260 static breakpoint *
7261 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7262 {
7263 struct breakpoint *b1;
7264 struct breakpoint *result = b.get ();
7265
7266 /* Add this breakpoint to the end of the chain so that a list of
7267 breakpoints will come out in order of increasing numbers. */
7268
7269 b1 = breakpoint_chain;
7270 if (b1 == 0)
7271 breakpoint_chain = b.release ();
7272 else
7273 {
7274 while (b1->next)
7275 b1 = b1->next;
7276 b1->next = b.release ();
7277 }
7278
7279 return result;
7280 }
7281
7282 /* Initialize loc->function_name. */
7283
7284 static void
7285 set_breakpoint_location_function (struct bp_location *loc)
7286 {
7287 gdb_assert (loc->owner != NULL);
7288
7289 if (loc->owner->type == bp_breakpoint
7290 || loc->owner->type == bp_hardware_breakpoint
7291 || is_tracepoint (loc->owner))
7292 {
7293 const char *function_name;
7294
7295 if (loc->msymbol != NULL
7296 && (loc->msymbol->type () == mst_text_gnu_ifunc
7297 || loc->msymbol->type () == mst_data_gnu_ifunc))
7298 {
7299 struct breakpoint *b = loc->owner;
7300
7301 function_name = loc->msymbol->linkage_name ();
7302
7303 if (b->type == bp_breakpoint && b->loc == loc
7304 && loc->next == NULL && b->related_breakpoint == b)
7305 {
7306 /* Create only the whole new breakpoint of this type but do not
7307 mess more complicated breakpoints with multiple locations. */
7308 b->type = bp_gnu_ifunc_resolver;
7309 /* Remember the resolver's address for use by the return
7310 breakpoint. */
7311 loc->related_address = loc->address;
7312 }
7313 }
7314 else
7315 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7316
7317 if (function_name)
7318 loc->function_name = make_unique_xstrdup (function_name);
7319 }
7320 }
7321
7322 /* Attempt to determine architecture of location identified by SAL. */
7323 struct gdbarch *
7324 get_sal_arch (struct symtab_and_line sal)
7325 {
7326 if (sal.section)
7327 return sal.section->objfile->arch ();
7328 if (sal.symtab)
7329 return sal.symtab->compunit ()->objfile ()->arch ();
7330
7331 return NULL;
7332 }
7333
7334 /* Call this routine when stepping and nexting to enable a breakpoint
7335 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7336 initiated the operation. */
7337
7338 void
7339 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7340 {
7341 int thread = tp->global_num;
7342
7343 /* To avoid having to rescan all objfile symbols at every step,
7344 we maintain a list of continually-inserted but always disabled
7345 longjmp "master" breakpoints. Here, we simply create momentary
7346 clones of those and enable them for the requested thread. */
7347 for (breakpoint *b : all_breakpoints_safe ())
7348 if (b->pspace == current_program_space
7349 && (b->type == bp_longjmp_master
7350 || b->type == bp_exception_master))
7351 {
7352 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7353 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7354 after their removal. */
7355 momentary_breakpoint_from_master (b, type, 1, thread);
7356 }
7357
7358 tp->initiating_frame = frame;
7359 }
7360
7361 /* Delete all longjmp breakpoints from THREAD. */
7362 void
7363 delete_longjmp_breakpoint (int thread)
7364 {
7365 for (breakpoint *b : all_breakpoints_safe ())
7366 if (b->type == bp_longjmp || b->type == bp_exception)
7367 {
7368 if (b->thread == thread)
7369 delete_breakpoint (b);
7370 }
7371 }
7372
7373 void
7374 delete_longjmp_breakpoint_at_next_stop (int thread)
7375 {
7376 for (breakpoint *b : all_breakpoints_safe ())
7377 if (b->type == bp_longjmp || b->type == bp_exception)
7378 {
7379 if (b->thread == thread)
7380 b->disposition = disp_del_at_next_stop;
7381 }
7382 }
7383
7384 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7385 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7386 pointer to any of them. Return NULL if this system cannot place longjmp
7387 breakpoints. */
7388
7389 struct breakpoint *
7390 set_longjmp_breakpoint_for_call_dummy (void)
7391 {
7392 breakpoint *retval = nullptr;
7393
7394 for (breakpoint *b : all_breakpoints ())
7395 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7396 {
7397 int thread = inferior_thread ()->global_num;
7398 breakpoint *new_b
7399 = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7400 1, thread);
7401
7402 /* Link NEW_B into the chain of RETVAL breakpoints. */
7403
7404 gdb_assert (new_b->related_breakpoint == new_b);
7405 if (retval == NULL)
7406 retval = new_b;
7407 new_b->related_breakpoint = retval;
7408 while (retval->related_breakpoint != new_b->related_breakpoint)
7409 retval = retval->related_breakpoint;
7410 retval->related_breakpoint = new_b;
7411 }
7412
7413 return retval;
7414 }
7415
7416 /* Verify all existing dummy frames and their associated breakpoints for
7417 TP. Remove those which can no longer be found in the current frame
7418 stack.
7419
7420 If the unwind fails then there is not sufficient information to discard
7421 dummy frames. In this case, elide the clean up and the dummy frames will
7422 be cleaned up next time this function is called from a location where
7423 unwinding is possible. */
7424
7425 void
7426 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7427 {
7428 struct breakpoint *b, *b_tmp;
7429
7430 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7431 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7432 {
7433 struct breakpoint *dummy_b = b->related_breakpoint;
7434
7435 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7436 chained off b->related_breakpoint. */
7437 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7438 dummy_b = dummy_b->related_breakpoint;
7439
7440 /* If there was no bp_call_dummy breakpoint then there's nothing
7441 more to do. Or, if the dummy frame associated with the
7442 bp_call_dummy is still on the stack then we need to leave this
7443 bp_call_dummy in place. */
7444 if (dummy_b->type != bp_call_dummy
7445 || frame_find_by_id (dummy_b->frame_id) != NULL)
7446 continue;
7447
7448 /* We didn't find the dummy frame on the stack, this could be
7449 because we have longjmp'd to a stack frame that is previous to
7450 the dummy frame, or it could be because the stack unwind is
7451 broken at some point between the longjmp frame and the dummy
7452 frame.
7453
7454 Next we figure out why the stack unwind stopped. If it looks
7455 like the unwind is complete then we assume the dummy frame has
7456 been jumped over, however, if the unwind stopped for an
7457 unexpected reason then we assume the stack unwind is currently
7458 broken, and that we will (eventually) return to the dummy
7459 frame.
7460
7461 It might be tempting to consider using frame_id_inner here, but
7462 that is not safe. There is no guarantee that the stack frames
7463 we are looking at here are even on the same stack as the
7464 original dummy frame, hence frame_id_inner can't be used. See
7465 the comments on frame_id_inner for more details. */
7466 bool unwind_finished_unexpectedly = false;
7467 for (struct frame_info *fi = get_current_frame (); fi != nullptr; )
7468 {
7469 struct frame_info *prev = get_prev_frame (fi);
7470 if (prev == nullptr)
7471 {
7472 /* FI is the last stack frame. Why did this frame not
7473 unwind further? */
7474 auto stop_reason = get_frame_unwind_stop_reason (fi);
7475 if (stop_reason != UNWIND_NO_REASON
7476 && stop_reason != UNWIND_OUTERMOST)
7477 unwind_finished_unexpectedly = true;
7478 }
7479 fi = prev;
7480 }
7481 if (unwind_finished_unexpectedly)
7482 continue;
7483
7484 dummy_frame_discard (dummy_b->frame_id, tp);
7485
7486 while (b->related_breakpoint != b)
7487 {
7488 if (b_tmp == b->related_breakpoint)
7489 b_tmp = b->related_breakpoint->next;
7490 delete_breakpoint (b->related_breakpoint);
7491 }
7492 delete_breakpoint (b);
7493 }
7494 }
7495
7496 void
7497 enable_overlay_breakpoints (void)
7498 {
7499 for (breakpoint *b : all_breakpoints ())
7500 if (b->type == bp_overlay_event)
7501 {
7502 b->enable_state = bp_enabled;
7503 update_global_location_list (UGLL_MAY_INSERT);
7504 overlay_events_enabled = 1;
7505 }
7506 }
7507
7508 void
7509 disable_overlay_breakpoints (void)
7510 {
7511 for (breakpoint *b : all_breakpoints ())
7512 if (b->type == bp_overlay_event)
7513 {
7514 b->enable_state = bp_disabled;
7515 update_global_location_list (UGLL_DONT_INSERT);
7516 overlay_events_enabled = 0;
7517 }
7518 }
7519
7520 /* Set an active std::terminate breakpoint for each std::terminate
7521 master breakpoint. */
7522 void
7523 set_std_terminate_breakpoint (void)
7524 {
7525 for (breakpoint *b : all_breakpoints_safe ())
7526 if (b->pspace == current_program_space
7527 && b->type == bp_std_terminate_master)
7528 {
7529 momentary_breakpoint_from_master (b, bp_std_terminate, 1,
7530 inferior_thread ()->global_num);
7531 }
7532 }
7533
7534 /* Delete all the std::terminate breakpoints. */
7535 void
7536 delete_std_terminate_breakpoint (void)
7537 {
7538 for (breakpoint *b : all_breakpoints_safe ())
7539 if (b->type == bp_std_terminate)
7540 delete_breakpoint (b);
7541 }
7542
7543 struct breakpoint *
7544 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7545 {
7546 struct breakpoint *b;
7547
7548 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
7549
7550 b->enable_state = bp_enabled;
7551 /* locspec has to be used or breakpoint_re_set will delete me. */
7552 b->locspec = new_address_location_spec (b->loc->address, NULL, 0);
7553
7554 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7555
7556 return b;
7557 }
7558
7559 struct lang_and_radix
7560 {
7561 enum language lang;
7562 int radix;
7563 };
7564
7565 /* Create a breakpoint for JIT code registration and unregistration. */
7566
7567 struct breakpoint *
7568 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7569 {
7570 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
7571 }
7572
7573 /* Remove JIT code registration and unregistration breakpoint(s). */
7574
7575 void
7576 remove_jit_event_breakpoints (void)
7577 {
7578 for (breakpoint *b : all_breakpoints_safe ())
7579 if (b->type == bp_jit_event
7580 && b->loc->pspace == current_program_space)
7581 delete_breakpoint (b);
7582 }
7583
7584 void
7585 remove_solib_event_breakpoints (void)
7586 {
7587 for (breakpoint *b : all_breakpoints_safe ())
7588 if (b->type == bp_shlib_event
7589 && b->loc->pspace == current_program_space)
7590 delete_breakpoint (b);
7591 }
7592
7593 /* See breakpoint.h. */
7594
7595 void
7596 remove_solib_event_breakpoints_at_next_stop (void)
7597 {
7598 for (breakpoint *b : all_breakpoints_safe ())
7599 if (b->type == bp_shlib_event
7600 && b->loc->pspace == current_program_space)
7601 b->disposition = disp_del_at_next_stop;
7602 }
7603
7604 /* Helper for create_solib_event_breakpoint /
7605 create_and_insert_solib_event_breakpoint. Allows specifying which
7606 INSERT_MODE to pass through to update_global_location_list. */
7607
7608 static struct breakpoint *
7609 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7610 enum ugll_insert_mode insert_mode)
7611 {
7612 struct breakpoint *b;
7613
7614 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
7615 update_global_location_list_nothrow (insert_mode);
7616 return b;
7617 }
7618
7619 struct breakpoint *
7620 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7621 {
7622 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7623 }
7624
7625 /* See breakpoint.h. */
7626
7627 struct breakpoint *
7628 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7629 {
7630 struct breakpoint *b;
7631
7632 /* Explicitly tell update_global_location_list to insert
7633 locations. */
7634 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7635 if (!b->loc->inserted)
7636 {
7637 delete_breakpoint (b);
7638 return NULL;
7639 }
7640 return b;
7641 }
7642
7643 /* Disable any breakpoints that are on code in shared libraries. Only
7644 apply to enabled breakpoints, disabled ones can just stay disabled. */
7645
7646 void
7647 disable_breakpoints_in_shlibs (void)
7648 {
7649 for (bp_location *loc : all_bp_locations ())
7650 {
7651 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7652 struct breakpoint *b = loc->owner;
7653
7654 /* We apply the check to all breakpoints, including disabled for
7655 those with loc->duplicate set. This is so that when breakpoint
7656 becomes enabled, or the duplicate is removed, gdb will try to
7657 insert all breakpoints. If we don't set shlib_disabled here,
7658 we'll try to insert those breakpoints and fail. */
7659 if (((b->type == bp_breakpoint)
7660 || (b->type == bp_jit_event)
7661 || (b->type == bp_hardware_breakpoint)
7662 || (is_tracepoint (b)))
7663 && loc->pspace == current_program_space
7664 && !loc->shlib_disabled
7665 && solib_name_from_address (loc->pspace, loc->address)
7666 )
7667 {
7668 loc->shlib_disabled = 1;
7669 }
7670 }
7671 }
7672
7673 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7674 notification of unloaded_shlib. Only apply to enabled breakpoints,
7675 disabled ones can just stay disabled. */
7676
7677 static void
7678 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7679 {
7680 int disabled_shlib_breaks = 0;
7681
7682 for (bp_location *loc : all_bp_locations ())
7683 {
7684 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7685 struct breakpoint *b = loc->owner;
7686
7687 if (solib->pspace == loc->pspace
7688 && !loc->shlib_disabled
7689 && (((b->type == bp_breakpoint
7690 || b->type == bp_jit_event
7691 || b->type == bp_hardware_breakpoint)
7692 && (loc->loc_type == bp_loc_hardware_breakpoint
7693 || loc->loc_type == bp_loc_software_breakpoint))
7694 || is_tracepoint (b))
7695 && solib_contains_address_p (solib, loc->address))
7696 {
7697 loc->shlib_disabled = 1;
7698 /* At this point, we cannot rely on remove_breakpoint
7699 succeeding so we must mark the breakpoint as not inserted
7700 to prevent future errors occurring in remove_breakpoints. */
7701 loc->inserted = 0;
7702
7703 /* This may cause duplicate notifications for the same breakpoint. */
7704 gdb::observers::breakpoint_modified.notify (b);
7705
7706 if (!disabled_shlib_breaks)
7707 {
7708 target_terminal::ours_for_output ();
7709 warning (_("Temporarily disabling breakpoints "
7710 "for unloaded shared library \"%s\""),
7711 solib->so_name);
7712 }
7713 disabled_shlib_breaks = 1;
7714 }
7715 }
7716 }
7717
7718 /* Disable any breakpoints and tracepoints in OBJFILE upon
7719 notification of free_objfile. Only apply to enabled breakpoints,
7720 disabled ones can just stay disabled. */
7721
7722 static void
7723 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7724 {
7725 if (objfile == NULL)
7726 return;
7727
7728 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7729 managed by the user with add-symbol-file/remove-symbol-file.
7730 Similarly to how breakpoints in shared libraries are handled in
7731 response to "nosharedlibrary", mark breakpoints in such modules
7732 shlib_disabled so they end up uninserted on the next global
7733 location list update. Shared libraries not loaded by the user
7734 aren't handled here -- they're already handled in
7735 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7736 solib_unloaded observer. We skip objfiles that are not
7737 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7738 main objfile). */
7739 if ((objfile->flags & OBJF_SHARED) == 0
7740 || (objfile->flags & OBJF_USERLOADED) == 0)
7741 return;
7742
7743 for (breakpoint *b : all_breakpoints ())
7744 {
7745 int bp_modified = 0;
7746
7747 if (!is_breakpoint (b) && !is_tracepoint (b))
7748 continue;
7749
7750 for (bp_location *loc : b->locations ())
7751 {
7752 CORE_ADDR loc_addr = loc->address;
7753
7754 if (loc->loc_type != bp_loc_hardware_breakpoint
7755 && loc->loc_type != bp_loc_software_breakpoint)
7756 continue;
7757
7758 if (loc->shlib_disabled != 0)
7759 continue;
7760
7761 if (objfile->pspace != loc->pspace)
7762 continue;
7763
7764 if (loc->loc_type != bp_loc_hardware_breakpoint
7765 && loc->loc_type != bp_loc_software_breakpoint)
7766 continue;
7767
7768 if (is_addr_in_objfile (loc_addr, objfile))
7769 {
7770 loc->shlib_disabled = 1;
7771 /* At this point, we don't know whether the object was
7772 unmapped from the inferior or not, so leave the
7773 inserted flag alone. We'll handle failure to
7774 uninsert quietly, in case the object was indeed
7775 unmapped. */
7776
7777 mark_breakpoint_location_modified (loc);
7778
7779 bp_modified = 1;
7780 }
7781 }
7782
7783 if (bp_modified)
7784 gdb::observers::breakpoint_modified.notify (b);
7785 }
7786 }
7787
7788 /* See breakpoint.h. */
7789
7790 breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
7791 bool temp, const char *cond_string_)
7792 : type (bptype),
7793 disposition (temp ? disp_del : disp_donttouch),
7794 gdbarch (gdbarch_),
7795 language (current_language->la_language),
7796 input_radix (::input_radix),
7797 cond_string (cond_string_ != nullptr
7798 ? make_unique_xstrdup (cond_string_)
7799 : nullptr),
7800 related_breakpoint (this)
7801 {
7802 }
7803
7804 /* See breakpoint.h. */
7805
7806 catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
7807 const char *cond_string)
7808 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
7809 {
7810 add_dummy_location (this, current_program_space);
7811
7812 pspace = current_program_space;
7813 }
7814
7815 void
7816 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
7817 {
7818 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
7819 set_breakpoint_number (internal, b);
7820 if (is_tracepoint (b))
7821 set_tracepoint_count (breakpoint_count);
7822 if (!internal)
7823 mention (b);
7824 gdb::observers::breakpoint_created.notify (b);
7825
7826 if (update_gll)
7827 update_global_location_list (UGLL_MAY_INSERT);
7828 }
7829
7830 static int
7831 hw_breakpoint_used_count (void)
7832 {
7833 int i = 0;
7834
7835 for (breakpoint *b : all_breakpoints ())
7836 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
7837 for (bp_location *bl : b->locations ())
7838 {
7839 /* Special types of hardware breakpoints may use more than
7840 one register. */
7841 i += b->resources_needed (bl);
7842 }
7843
7844 return i;
7845 }
7846
7847 /* Returns the resources B would use if it were a hardware
7848 watchpoint. */
7849
7850 static int
7851 hw_watchpoint_use_count (struct breakpoint *b)
7852 {
7853 int i = 0;
7854
7855 if (!breakpoint_enabled (b))
7856 return 0;
7857
7858 for (bp_location *bl : b->locations ())
7859 {
7860 /* Special types of hardware watchpoints may use more than
7861 one register. */
7862 i += b->resources_needed (bl);
7863 }
7864
7865 return i;
7866 }
7867
7868 /* Returns the sum the used resources of all hardware watchpoints of
7869 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
7870 the sum of the used resources of all hardware watchpoints of other
7871 types _not_ TYPE. */
7872
7873 static int
7874 hw_watchpoint_used_count_others (struct breakpoint *except,
7875 enum bptype type, int *other_type_used)
7876 {
7877 int i = 0;
7878
7879 *other_type_used = 0;
7880 for (breakpoint *b : all_breakpoints ())
7881 {
7882 if (b == except)
7883 continue;
7884 if (!breakpoint_enabled (b))
7885 continue;
7886
7887 if (b->type == type)
7888 i += hw_watchpoint_use_count (b);
7889 else if (is_hardware_watchpoint (b))
7890 *other_type_used = 1;
7891 }
7892
7893 return i;
7894 }
7895
7896 void
7897 disable_watchpoints_before_interactive_call_start (void)
7898 {
7899 for (breakpoint *b : all_breakpoints ())
7900 if (is_watchpoint (b) && breakpoint_enabled (b))
7901 {
7902 b->enable_state = bp_call_disabled;
7903 update_global_location_list (UGLL_DONT_INSERT);
7904 }
7905 }
7906
7907 void
7908 enable_watchpoints_after_interactive_call_stop (void)
7909 {
7910 for (breakpoint *b : all_breakpoints ())
7911 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
7912 {
7913 b->enable_state = bp_enabled;
7914 update_global_location_list (UGLL_MAY_INSERT);
7915 }
7916 }
7917
7918 void
7919 disable_breakpoints_before_startup (void)
7920 {
7921 current_program_space->executing_startup = 1;
7922 update_global_location_list (UGLL_DONT_INSERT);
7923 }
7924
7925 void
7926 enable_breakpoints_after_startup (void)
7927 {
7928 current_program_space->executing_startup = 0;
7929 breakpoint_re_set ();
7930 }
7931
7932 /* Allocate a new momentary breakpoint. */
7933
7934 template<typename... Arg>
7935 static momentary_breakpoint *
7936 new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
7937 Arg&&... args)
7938 {
7939 if (type == bp_longjmp || type == bp_exception)
7940 return new longjmp_breakpoint (gdbarch, type,
7941 std::forward<Arg> (args)...);
7942 else
7943 return new momentary_breakpoint (gdbarch, type,
7944 std::forward<Arg> (args)...);
7945 }
7946
7947 /* Set a momentary breakpoint of type TYPE at address specified by
7948 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
7949 frame. */
7950
7951 breakpoint_up
7952 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7953 struct frame_id frame_id, enum bptype type)
7954 {
7955 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
7956 tail-called one. */
7957 gdb_assert (!frame_id_artificial_p (frame_id));
7958
7959 std::unique_ptr<momentary_breakpoint> b
7960 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
7961 inferior_thread ()->global_num));
7962
7963 b->add_location (sal);
7964
7965 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
7966
7967 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7968
7969 return bp;
7970 }
7971
7972 /* Make a momentary breakpoint based on the master breakpoint ORIG.
7973 The new breakpoint will have type TYPE, use OPS as its
7974 breakpoint_ops, and will set enabled to LOC_ENABLED. */
7975
7976 static struct breakpoint *
7977 momentary_breakpoint_from_master (struct breakpoint *orig,
7978 enum bptype type,
7979 int loc_enabled,
7980 int thread)
7981 {
7982 std::unique_ptr<breakpoint> copy
7983 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
7984 orig->frame_id, thread));
7985 copy->loc = copy->allocate_location ();
7986 set_breakpoint_location_function (copy->loc);
7987
7988 copy->loc->gdbarch = orig->loc->gdbarch;
7989 copy->loc->requested_address = orig->loc->requested_address;
7990 copy->loc->address = orig->loc->address;
7991 copy->loc->section = orig->loc->section;
7992 copy->loc->pspace = orig->loc->pspace;
7993 copy->loc->probe = orig->loc->probe;
7994 copy->loc->line_number = orig->loc->line_number;
7995 copy->loc->symtab = orig->loc->symtab;
7996 copy->loc->enabled = loc_enabled;
7997
7998 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
7999 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8000 return b;
8001 }
8002
8003 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8004 ORIG is NULL. */
8005
8006 struct breakpoint *
8007 clone_momentary_breakpoint (struct breakpoint *orig)
8008 {
8009 /* If there's nothing to clone, then return nothing. */
8010 if (orig == NULL)
8011 return NULL;
8012
8013 return momentary_breakpoint_from_master (orig, orig->type, 0,
8014 orig->thread);
8015 }
8016
8017 breakpoint_up
8018 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8019 enum bptype type)
8020 {
8021 struct symtab_and_line sal;
8022
8023 sal = find_pc_line (pc, 0);
8024 sal.pc = pc;
8025 sal.section = find_pc_overlay (pc);
8026 sal.explicit_pc = 1;
8027
8028 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8029 }
8030 \f
8031
8032 /* Tell the user we have just set a breakpoint B. */
8033
8034 static void
8035 mention (const breakpoint *b)
8036 {
8037 b->print_mention ();
8038 current_uiout->text ("\n");
8039 }
8040 \f
8041
8042 static bool bp_loc_is_permanent (struct bp_location *loc);
8043
8044 /* Handle "set breakpoint auto-hw on".
8045
8046 If the explicitly specified breakpoint type is not hardware
8047 breakpoint, check the memory map to see whether the breakpoint
8048 address is in read-only memory.
8049
8050 - location type is not hardware breakpoint, memory is read-only.
8051 We change the type of the location to hardware breakpoint.
8052
8053 - location type is hardware breakpoint, memory is read-write. This
8054 means we've previously made the location hardware one, but then the
8055 memory map changed, so we undo.
8056 */
8057
8058 static void
8059 handle_automatic_hardware_breakpoints (bp_location *bl)
8060 {
8061 if (automatic_hardware_breakpoints
8062 && bl->owner->type != bp_hardware_breakpoint
8063 && (bl->loc_type == bp_loc_software_breakpoint
8064 || bl->loc_type == bp_loc_hardware_breakpoint))
8065 {
8066 /* When breakpoints are removed, remove_breakpoints will use
8067 location types we've just set here, the only possible problem
8068 is that memory map has changed during running program, but
8069 it's not going to work anyway with current gdb. */
8070 mem_region *mr = lookup_mem_region (bl->address);
8071
8072 if (mr != nullptr)
8073 {
8074 enum bp_loc_type new_type;
8075
8076 if (mr->attrib.mode != MEM_RW)
8077 new_type = bp_loc_hardware_breakpoint;
8078 else
8079 new_type = bp_loc_software_breakpoint;
8080
8081 if (new_type != bl->loc_type)
8082 {
8083 static bool said = false;
8084
8085 bl->loc_type = new_type;
8086 if (!said)
8087 {
8088 gdb_printf (_("Note: automatically using "
8089 "hardware breakpoints for "
8090 "read-only addresses.\n"));
8091 said = true;
8092 }
8093 }
8094 }
8095 }
8096 }
8097
8098 bp_location *
8099 code_breakpoint::add_location (const symtab_and_line &sal)
8100 {
8101 struct bp_location *new_loc, **tmp;
8102 CORE_ADDR adjusted_address;
8103 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8104
8105 if (loc_gdbarch == NULL)
8106 loc_gdbarch = gdbarch;
8107
8108 /* Adjust the breakpoint's address prior to allocating a location.
8109 Once we call allocate_location(), that mostly uninitialized
8110 location will be placed on the location chain. Adjustment of the
8111 breakpoint may cause target_read_memory() to be called and we do
8112 not want its scan of the location chain to find a breakpoint and
8113 location that's only been partially initialized. */
8114 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8115 sal.pc, type,
8116 sal.pspace);
8117
8118 /* Sort the locations by their ADDRESS. */
8119 new_loc = allocate_location ();
8120 for (tmp = &(loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8121 tmp = &((*tmp)->next))
8122 ;
8123 new_loc->next = *tmp;
8124 *tmp = new_loc;
8125
8126 new_loc->requested_address = sal.pc;
8127 new_loc->address = adjusted_address;
8128 new_loc->pspace = sal.pspace;
8129 new_loc->probe.prob = sal.prob;
8130 new_loc->probe.objfile = sal.objfile;
8131 gdb_assert (new_loc->pspace != NULL);
8132 new_loc->section = sal.section;
8133 new_loc->gdbarch = loc_gdbarch;
8134 new_loc->line_number = sal.line;
8135 new_loc->symtab = sal.symtab;
8136 new_loc->symbol = sal.symbol;
8137 new_loc->msymbol = sal.msymbol;
8138 new_loc->objfile = sal.objfile;
8139
8140 set_breakpoint_location_function (new_loc);
8141
8142 /* While by definition, permanent breakpoints are already present in the
8143 code, we don't mark the location as inserted. Normally one would expect
8144 that GDB could rely on that breakpoint instruction to stop the program,
8145 thus removing the need to insert its own breakpoint, except that executing
8146 the breakpoint instruction can kill the target instead of reporting a
8147 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8148 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8149 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8150 breakpoint be inserted normally results in QEMU knowing about the GDB
8151 breakpoint, and thus trap before the breakpoint instruction is executed.
8152 (If GDB later needs to continue execution past the permanent breakpoint,
8153 it manually increments the PC, thus avoiding executing the breakpoint
8154 instruction.) */
8155 if (bp_loc_is_permanent (new_loc))
8156 new_loc->permanent = 1;
8157
8158 return new_loc;
8159 }
8160 \f
8161
8162 /* Return true if LOC is pointing to a permanent breakpoint,
8163 return false otherwise. */
8164
8165 static bool
8166 bp_loc_is_permanent (struct bp_location *loc)
8167 {
8168 gdb_assert (loc != NULL);
8169
8170 /* If we have a non-breakpoint-backed catchpoint or a software
8171 watchpoint, just return 0. We should not attempt to read from
8172 the addresses the locations of these breakpoint types point to.
8173 gdbarch_program_breakpoint_here_p, below, will attempt to read
8174 memory. */
8175 if (!bl_address_is_meaningful (loc))
8176 return false;
8177
8178 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8179 switch_to_program_space_and_thread (loc->pspace);
8180 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8181 }
8182
8183 /* Build a command list for the dprintf corresponding to the current
8184 settings of the dprintf style options. */
8185
8186 static void
8187 update_dprintf_command_list (struct breakpoint *b)
8188 {
8189 const char *dprintf_args = b->extra_string.get ();
8190 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8191
8192 if (!dprintf_args)
8193 return;
8194
8195 dprintf_args = skip_spaces (dprintf_args);
8196
8197 /* Allow a comma, as it may have terminated a location, but don't
8198 insist on it. */
8199 if (*dprintf_args == ',')
8200 ++dprintf_args;
8201 dprintf_args = skip_spaces (dprintf_args);
8202
8203 if (*dprintf_args != '"')
8204 error (_("Bad format string, missing '\"'."));
8205
8206 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8207 printf_line = xstrprintf ("printf %s", dprintf_args);
8208 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8209 {
8210 if (dprintf_function.empty ())
8211 error (_("No function supplied for dprintf call"));
8212
8213 if (!dprintf_channel.empty ())
8214 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8215 dprintf_function.c_str (),
8216 dprintf_channel.c_str (),
8217 dprintf_args);
8218 else
8219 printf_line = xstrprintf ("call (void) %s (%s)",
8220 dprintf_function.c_str (),
8221 dprintf_args);
8222 }
8223 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8224 {
8225 if (target_can_run_breakpoint_commands ())
8226 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8227 else
8228 {
8229 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8230 printf_line = xstrprintf ("printf %s", dprintf_args);
8231 }
8232 }
8233 else
8234 internal_error (__FILE__, __LINE__,
8235 _("Invalid dprintf style."));
8236
8237 gdb_assert (printf_line != NULL);
8238
8239 /* Manufacture a printf sequence. */
8240 struct command_line *printf_cmd_line
8241 = new struct command_line (simple_control, printf_line.release ());
8242 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8243 command_lines_deleter ()));
8244 }
8245
8246 /* Update all dprintf commands, making their command lists reflect
8247 current style settings. */
8248
8249 static void
8250 update_dprintf_commands (const char *args, int from_tty,
8251 struct cmd_list_element *c)
8252 {
8253 for (breakpoint *b : all_breakpoints ())
8254 if (b->type == bp_dprintf)
8255 update_dprintf_command_list (b);
8256 }
8257
8258 code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
8259 enum bptype type_,
8260 gdb::array_view<const symtab_and_line> sals,
8261 location_spec_up &&locspec_,
8262 gdb::unique_xmalloc_ptr<char> filter_,
8263 gdb::unique_xmalloc_ptr<char> cond_string_,
8264 gdb::unique_xmalloc_ptr<char> extra_string_,
8265 enum bpdisp disposition_,
8266 int thread_, int task_, int ignore_count_,
8267 int from_tty,
8268 int enabled_, unsigned flags,
8269 int display_canonical_)
8270 : breakpoint (gdbarch_, type_)
8271 {
8272 int i;
8273
8274 if (type == bp_hardware_breakpoint)
8275 {
8276 int target_resources_ok;
8277
8278 i = hw_breakpoint_used_count ();
8279 target_resources_ok =
8280 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8281 i + 1, 0);
8282 if (target_resources_ok == 0)
8283 error (_("No hardware breakpoint support in the target."));
8284 else if (target_resources_ok < 0)
8285 error (_("Hardware breakpoints used exceeds limit."));
8286 }
8287
8288 gdb_assert (!sals.empty ());
8289
8290 thread = thread_;
8291 task = task_;
8292
8293 cond_string = std::move (cond_string_);
8294 extra_string = std::move (extra_string_);
8295 ignore_count = ignore_count_;
8296 enable_state = enabled_ ? bp_enabled : bp_disabled;
8297 disposition = disposition_;
8298
8299 if (type == bp_static_tracepoint
8300 || type == bp_static_marker_tracepoint)
8301 {
8302 auto *t = static_cast<struct tracepoint *> (this);
8303 struct static_tracepoint_marker marker;
8304
8305 if (strace_marker_p (this))
8306 {
8307 /* We already know the marker exists, otherwise, we wouldn't
8308 see a sal for it. */
8309 const char *p = &locspec_->to_string ()[3];
8310 const char *endp;
8311
8312 p = skip_spaces (p);
8313
8314 endp = skip_to_space (p);
8315
8316 t->static_trace_marker_id.assign (p, endp - p);
8317
8318 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8319 t->static_trace_marker_id.c_str ());
8320 }
8321 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8322 {
8323 t->static_trace_marker_id = std::move (marker.str_id);
8324
8325 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8326 t->static_trace_marker_id.c_str ());
8327 }
8328 else
8329 warning (_("Couldn't determine the static tracepoint marker to probe"));
8330 }
8331
8332 for (const auto &sal : sals)
8333 {
8334 if (from_tty)
8335 {
8336 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8337 if (loc_gdbarch == nullptr)
8338 loc_gdbarch = gdbarch;
8339
8340 describe_other_breakpoints (loc_gdbarch,
8341 sal.pspace, sal.pc, sal.section, thread);
8342 }
8343
8344 bp_location *new_loc = add_location (sal);
8345 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8346 new_loc->inserted = 1;
8347
8348 /* Do not set breakpoint locations conditions yet. As locations
8349 are inserted, they get sorted based on their addresses. Let
8350 the list stabilize to have reliable location numbers. */
8351
8352 /* Dynamic printf requires and uses additional arguments on the
8353 command line, otherwise it's an error. */
8354 if (type == bp_dprintf)
8355 {
8356 if (extra_string != nullptr)
8357 update_dprintf_command_list (this);
8358 else
8359 error (_("Format string required"));
8360 }
8361 else if (extra_string != nullptr)
8362 error (_("Garbage '%s' at end of command"), extra_string.get ());
8363 }
8364
8365 /* The order of the locations is now stable. Set the location
8366 condition using the location's number. */
8367 int loc_num = 1;
8368 for (bp_location *bl : locations ())
8369 {
8370 if (cond_string != nullptr)
8371 set_breakpoint_location_condition (cond_string.get (), bl,
8372 number, loc_num);
8373
8374 ++loc_num;
8375 }
8376
8377 display_canonical = display_canonical_;
8378 if (locspec_ != nullptr)
8379 locspec = std::move (locspec_);
8380 else
8381 locspec = new_address_location_spec (this->loc->address, NULL, 0);
8382 filter = std::move (filter_);
8383 }
8384
8385 static void
8386 create_breakpoint_sal (struct gdbarch *gdbarch,
8387 gdb::array_view<const symtab_and_line> sals,
8388 location_spec_up &&locspec,
8389 gdb::unique_xmalloc_ptr<char> filter,
8390 gdb::unique_xmalloc_ptr<char> cond_string,
8391 gdb::unique_xmalloc_ptr<char> extra_string,
8392 enum bptype type, enum bpdisp disposition,
8393 int thread, int task, int ignore_count,
8394 int from_tty,
8395 int enabled, int internal, unsigned flags,
8396 int display_canonical)
8397 {
8398 std::unique_ptr<code_breakpoint> b
8399 = new_breakpoint_from_type (gdbarch,
8400 type,
8401 sals,
8402 std::move (locspec),
8403 std::move (filter),
8404 std::move (cond_string),
8405 std::move (extra_string),
8406 disposition,
8407 thread, task, ignore_count,
8408 from_tty,
8409 enabled, flags,
8410 display_canonical);
8411
8412 install_breakpoint (internal, std::move (b), 0);
8413 }
8414
8415 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8416 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8417 value. COND_STRING, if not NULL, specified the condition to be
8418 used for all breakpoints. Essentially the only case where
8419 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8420 function. In that case, it's still not possible to specify
8421 separate conditions for different overloaded functions, so
8422 we take just a single condition string.
8423
8424 NOTE: If the function succeeds, the caller is expected to cleanup
8425 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8426 array contents). If the function fails (error() is called), the
8427 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8428 COND and SALS arrays and each of those arrays contents. */
8429
8430 static void
8431 create_breakpoints_sal (struct gdbarch *gdbarch,
8432 struct linespec_result *canonical,
8433 gdb::unique_xmalloc_ptr<char> cond_string,
8434 gdb::unique_xmalloc_ptr<char> extra_string,
8435 enum bptype type, enum bpdisp disposition,
8436 int thread, int task, int ignore_count,
8437 int from_tty,
8438 int enabled, int internal, unsigned flags)
8439 {
8440 if (canonical->pre_expanded)
8441 gdb_assert (canonical->lsals.size () == 1);
8442
8443 for (const auto &lsal : canonical->lsals)
8444 {
8445 /* Note that 'location' can be NULL in the case of a plain
8446 'break', without arguments. */
8447 location_spec_up locspec
8448 = (canonical->locspec != nullptr
8449 ? canonical->locspec->clone ()
8450 : nullptr);
8451 gdb::unique_xmalloc_ptr<char> filter_string
8452 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8453
8454 create_breakpoint_sal (gdbarch, lsal.sals,
8455 std::move (locspec),
8456 std::move (filter_string),
8457 std::move (cond_string),
8458 std::move (extra_string),
8459 type, disposition,
8460 thread, task, ignore_count,
8461 from_tty, enabled, internal, flags,
8462 canonical->special_display);
8463 }
8464 }
8465
8466 /* Parse LOCSPEC which is assumed to be a SAL specification possibly
8467 followed by conditionals. On return, SALS contains an array of SAL
8468 addresses found. LOCSPEC points to the end of the SAL (for
8469 linespec locspecs).
8470
8471 The array and the line spec strings are allocated on the heap, it is
8472 the caller's responsibility to free them. */
8473
8474 static void
8475 parse_breakpoint_sals (location_spec *locspec,
8476 struct linespec_result *canonical)
8477 {
8478 struct symtab_and_line cursal;
8479
8480 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8481 {
8482 const char *spec = as_linespec_location_spec (locspec)->spec_string;
8483
8484 if (spec == NULL)
8485 {
8486 /* The last displayed codepoint, if it's valid, is our default
8487 breakpoint address. */
8488 if (last_displayed_sal_is_valid ())
8489 {
8490 /* Set sal's pspace, pc, symtab, and line to the values
8491 corresponding to the last call to print_frame_info.
8492 Be sure to reinitialize LINE with NOTCURRENT == 0
8493 as the breakpoint line number is inappropriate otherwise.
8494 find_pc_line would adjust PC, re-set it back. */
8495 symtab_and_line sal = get_last_displayed_sal ();
8496 CORE_ADDR pc = sal.pc;
8497
8498 sal = find_pc_line (pc, 0);
8499
8500 /* "break" without arguments is equivalent to "break *PC"
8501 where PC is the last displayed codepoint's address. So
8502 make sure to set sal.explicit_pc to prevent GDB from
8503 trying to expand the list of sals to include all other
8504 instances with the same symtab and line. */
8505 sal.pc = pc;
8506 sal.explicit_pc = 1;
8507
8508 struct linespec_sals lsal;
8509 lsal.sals = {sal};
8510 lsal.canonical = NULL;
8511
8512 canonical->lsals.push_back (std::move (lsal));
8513 return;
8514 }
8515 else
8516 error (_("No default breakpoint address now."));
8517 }
8518 }
8519
8520 /* Force almost all breakpoints to be in terms of the
8521 current_source_symtab (which is decode_line_1's default).
8522 This should produce the results we want almost all of the
8523 time while leaving default_breakpoint_* alone.
8524
8525 ObjC: However, don't match an Objective-C method name which
8526 may have a '+' or '-' succeeded by a '['. */
8527 cursal = get_current_source_symtab_and_line ();
8528 if (last_displayed_sal_is_valid ())
8529 {
8530 const char *spec = NULL;
8531
8532 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8533 spec = as_linespec_location_spec (locspec)->spec_string;
8534
8535 if (!cursal.symtab
8536 || (spec != NULL
8537 && strchr ("+-", spec[0]) != NULL
8538 && spec[1] != '['))
8539 {
8540 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
8541 get_last_displayed_symtab (),
8542 get_last_displayed_line (),
8543 canonical, NULL, NULL);
8544 return;
8545 }
8546 }
8547
8548 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
8549 cursal.symtab, cursal.line, canonical, NULL, NULL);
8550 }
8551
8552
8553 /* Convert each SAL into a real PC. Verify that the PC can be
8554 inserted as a breakpoint. If it can't throw an error. */
8555
8556 static void
8557 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
8558 {
8559 for (auto &sal : sals)
8560 resolve_sal_pc (&sal);
8561 }
8562
8563 /* Fast tracepoints may have restrictions on valid locations. For
8564 instance, a fast tracepoint using a jump instead of a trap will
8565 likely have to overwrite more bytes than a trap would, and so can
8566 only be placed where the instruction is longer than the jump, or a
8567 multi-instruction sequence does not have a jump into the middle of
8568 it, etc. */
8569
8570 static void
8571 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8572 gdb::array_view<const symtab_and_line> sals)
8573 {
8574 for (const auto &sal : sals)
8575 {
8576 struct gdbarch *sarch;
8577
8578 sarch = get_sal_arch (sal);
8579 /* We fall back to GDBARCH if there is no architecture
8580 associated with SAL. */
8581 if (sarch == NULL)
8582 sarch = gdbarch;
8583 std::string msg;
8584 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
8585 error (_("May not have a fast tracepoint at %s%s"),
8586 paddress (sarch, sal.pc), msg.c_str ());
8587 }
8588 }
8589
8590 /* Given TOK, a string specification of condition and thread, as
8591 accepted by the 'break' command, extract the condition
8592 string and thread number and set *COND_STRING and *THREAD.
8593 PC identifies the context at which the condition should be parsed.
8594 If no condition is found, *COND_STRING is set to NULL.
8595 If no thread is found, *THREAD is set to -1. */
8596
8597 static void
8598 find_condition_and_thread (const char *tok, CORE_ADDR pc,
8599 gdb::unique_xmalloc_ptr<char> *cond_string,
8600 int *thread, int *task,
8601 gdb::unique_xmalloc_ptr<char> *rest)
8602 {
8603 cond_string->reset ();
8604 *thread = -1;
8605 *task = 0;
8606 rest->reset ();
8607 bool force = false;
8608
8609 while (tok && *tok)
8610 {
8611 const char *end_tok;
8612 int toklen;
8613 const char *cond_start = NULL;
8614 const char *cond_end = NULL;
8615
8616 tok = skip_spaces (tok);
8617
8618 if ((*tok == '"' || *tok == ',') && rest)
8619 {
8620 rest->reset (savestring (tok, strlen (tok)));
8621 return;
8622 }
8623
8624 end_tok = skip_to_space (tok);
8625
8626 toklen = end_tok - tok;
8627
8628 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8629 {
8630 tok = cond_start = end_tok + 1;
8631 try
8632 {
8633 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8634 }
8635 catch (const gdb_exception_error &)
8636 {
8637 if (!force)
8638 throw;
8639 else
8640 tok = tok + strlen (tok);
8641 }
8642 cond_end = tok;
8643 cond_string->reset (savestring (cond_start, cond_end - cond_start));
8644 }
8645 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8646 {
8647 tok = tok + toklen;
8648 force = true;
8649 }
8650 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8651 {
8652 const char *tmptok;
8653 struct thread_info *thr;
8654
8655 tok = end_tok + 1;
8656 thr = parse_thread_id (tok, &tmptok);
8657 if (tok == tmptok)
8658 error (_("Junk after thread keyword."));
8659 *thread = thr->global_num;
8660 tok = tmptok;
8661 }
8662 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8663 {
8664 char *tmptok;
8665
8666 tok = end_tok + 1;
8667 *task = strtol (tok, &tmptok, 0);
8668 if (tok == tmptok)
8669 error (_("Junk after task keyword."));
8670 if (!valid_task_id (*task))
8671 error (_("Unknown task %d."), *task);
8672 tok = tmptok;
8673 }
8674 else if (rest)
8675 {
8676 rest->reset (savestring (tok, strlen (tok)));
8677 return;
8678 }
8679 else
8680 error (_("Junk at end of arguments."));
8681 }
8682 }
8683
8684 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
8685 succeeds. The parsed values are written to COND_STRING, THREAD,
8686 TASK, and REST. See the comment of 'find_condition_and_thread'
8687 for the description of these parameters and INPUT. */
8688
8689 static void
8690 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
8691 const char *input,
8692 gdb::unique_xmalloc_ptr<char> *cond_string,
8693 int *thread, int *task,
8694 gdb::unique_xmalloc_ptr<char> *rest)
8695 {
8696 int num_failures = 0;
8697 for (auto &sal : sals)
8698 {
8699 gdb::unique_xmalloc_ptr<char> cond;
8700 int thread_id = 0;
8701 int task_id = 0;
8702 gdb::unique_xmalloc_ptr<char> remaining;
8703
8704 /* Here we want to parse 'arg' to separate condition from thread
8705 number. But because parsing happens in a context and the
8706 contexts of sals might be different, try each until there is
8707 success. Finding one successful parse is sufficient for our
8708 goal. When setting the breakpoint we'll re-parse the
8709 condition in the context of each sal. */
8710 try
8711 {
8712 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8713 &task_id, &remaining);
8714 *cond_string = std::move (cond);
8715 *thread = thread_id;
8716 *task = task_id;
8717 *rest = std::move (remaining);
8718 break;
8719 }
8720 catch (const gdb_exception_error &e)
8721 {
8722 num_failures++;
8723 /* If no sal remains, do not continue. */
8724 if (num_failures == sals.size ())
8725 throw;
8726 }
8727 }
8728 }
8729
8730 /* Decode a static tracepoint marker spec. */
8731
8732 static std::vector<symtab_and_line>
8733 decode_static_tracepoint_spec (const char **arg_p)
8734 {
8735 const char *p = &(*arg_p)[3];
8736 const char *endp;
8737
8738 p = skip_spaces (p);
8739
8740 endp = skip_to_space (p);
8741
8742 std::string marker_str (p, endp - p);
8743
8744 std::vector<static_tracepoint_marker> markers
8745 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8746 if (markers.empty ())
8747 error (_("No known static tracepoint marker named %s"),
8748 marker_str.c_str ());
8749
8750 std::vector<symtab_and_line> sals;
8751 sals.reserve (markers.size ());
8752
8753 for (const static_tracepoint_marker &marker : markers)
8754 {
8755 symtab_and_line sal = find_pc_line (marker.address, 0);
8756 sal.pc = marker.address;
8757 sals.push_back (sal);
8758 }
8759
8760 *arg_p = endp;
8761 return sals;
8762 }
8763
8764 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8765 according to IS_TRACEPOINT. */
8766
8767 static const struct breakpoint_ops *
8768 breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
8769 bool is_tracepoint)
8770 {
8771 if (is_tracepoint)
8772 {
8773 if (locspec_type == PROBE_LOCATION_SPEC)
8774 return &tracepoint_probe_breakpoint_ops;
8775 else
8776 return &code_breakpoint_ops;
8777 }
8778 else
8779 {
8780 if (locspec_type == PROBE_LOCATION_SPEC)
8781 return &bkpt_probe_breakpoint_ops;
8782 else
8783 return &code_breakpoint_ops;
8784 }
8785 }
8786
8787 /* See breakpoint.h. */
8788
8789 const struct breakpoint_ops *
8790 breakpoint_ops_for_location_spec (const location_spec *locspec,
8791 bool is_tracepoint)
8792 {
8793 if (locspec != nullptr)
8794 return (breakpoint_ops_for_location_spec_type
8795 (locspec->type (), is_tracepoint));
8796 return &code_breakpoint_ops;
8797 }
8798
8799 /* See breakpoint.h. */
8800
8801 int
8802 create_breakpoint (struct gdbarch *gdbarch,
8803 location_spec *locspec,
8804 const char *cond_string,
8805 int thread, const char *extra_string,
8806 bool force_condition, int parse_extra,
8807 int tempflag, enum bptype type_wanted,
8808 int ignore_count,
8809 enum auto_boolean pending_break_support,
8810 const struct breakpoint_ops *ops,
8811 int from_tty, int enabled, int internal,
8812 unsigned flags)
8813 {
8814 struct linespec_result canonical;
8815 int pending = 0;
8816 int task = 0;
8817 int prev_bkpt_count = breakpoint_count;
8818
8819 gdb_assert (ops != NULL);
8820
8821 /* If extra_string isn't useful, set it to NULL. */
8822 if (extra_string != NULL && *extra_string == '\0')
8823 extra_string = NULL;
8824
8825 try
8826 {
8827 ops->create_sals_from_location_spec (locspec, &canonical);
8828 }
8829 catch (const gdb_exception_error &e)
8830 {
8831 /* If caller is interested in rc value from parse, set
8832 value. */
8833 if (e.error == NOT_FOUND_ERROR)
8834 {
8835 /* If pending breakpoint support is turned off, throw
8836 error. */
8837
8838 if (pending_break_support == AUTO_BOOLEAN_FALSE)
8839 throw;
8840
8841 exception_print (gdb_stderr, e);
8842
8843 /* If pending breakpoint support is auto query and the user
8844 selects no, then simply return the error code. */
8845 if (pending_break_support == AUTO_BOOLEAN_AUTO
8846 && !nquery (_("Make %s pending on future shared library load? "),
8847 bptype_string (type_wanted)))
8848 return 0;
8849
8850 /* At this point, either the user was queried about setting
8851 a pending breakpoint and selected yes, or pending
8852 breakpoint behavior is on and thus a pending breakpoint
8853 is defaulted on behalf of the user. */
8854 pending = 1;
8855 }
8856 else
8857 throw;
8858 }
8859
8860 if (!pending && canonical.lsals.empty ())
8861 return 0;
8862
8863 /* Resolve all line numbers to PC's and verify that the addresses
8864 are ok for the target. */
8865 if (!pending)
8866 {
8867 for (auto &lsal : canonical.lsals)
8868 breakpoint_sals_to_pc (lsal.sals);
8869 }
8870
8871 /* Fast tracepoints may have additional restrictions on location. */
8872 if (!pending && type_wanted == bp_fast_tracepoint)
8873 {
8874 for (const auto &lsal : canonical.lsals)
8875 check_fast_tracepoint_sals (gdbarch, lsal.sals);
8876 }
8877
8878 /* Verify that condition can be parsed, before setting any
8879 breakpoints. Allocate a separate condition expression for each
8880 breakpoint. */
8881 if (!pending)
8882 {
8883 gdb::unique_xmalloc_ptr<char> cond_string_copy;
8884 gdb::unique_xmalloc_ptr<char> extra_string_copy;
8885
8886 if (parse_extra)
8887 {
8888 gdb::unique_xmalloc_ptr<char> rest;
8889 gdb::unique_xmalloc_ptr<char> cond;
8890
8891 const linespec_sals &lsal = canonical.lsals[0];
8892
8893 find_condition_and_thread_for_sals (lsal.sals, extra_string,
8894 &cond, &thread, &task, &rest);
8895 cond_string_copy = std::move (cond);
8896 extra_string_copy = std::move (rest);
8897 }
8898 else
8899 {
8900 if (type_wanted != bp_dprintf
8901 && extra_string != NULL && *extra_string != '\0')
8902 error (_("Garbage '%s' at end of location"), extra_string);
8903
8904 /* Check the validity of the condition. We should error out
8905 if the condition is invalid at all of the locations and
8906 if it is not forced. In the PARSE_EXTRA case above, this
8907 check is done when parsing the EXTRA_STRING. */
8908 if (cond_string != nullptr && !force_condition)
8909 {
8910 int num_failures = 0;
8911 const linespec_sals &lsal = canonical.lsals[0];
8912 for (const auto &sal : lsal.sals)
8913 {
8914 const char *cond = cond_string;
8915 try
8916 {
8917 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
8918 /* One success is sufficient to keep going. */
8919 break;
8920 }
8921 catch (const gdb_exception_error &)
8922 {
8923 num_failures++;
8924 /* If this is the last sal, error out. */
8925 if (num_failures == lsal.sals.size ())
8926 throw;
8927 }
8928 }
8929 }
8930
8931 /* Create a private copy of condition string. */
8932 if (cond_string)
8933 cond_string_copy.reset (xstrdup (cond_string));
8934 /* Create a private copy of any extra string. */
8935 if (extra_string)
8936 extra_string_copy.reset (xstrdup (extra_string));
8937 }
8938
8939 ops->create_breakpoints_sal (gdbarch, &canonical,
8940 std::move (cond_string_copy),
8941 std::move (extra_string_copy),
8942 type_wanted,
8943 tempflag ? disp_del : disp_donttouch,
8944 thread, task, ignore_count,
8945 from_tty, enabled, internal, flags);
8946 }
8947 else
8948 {
8949 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
8950 type_wanted);
8951 b->locspec = locspec->clone ();
8952
8953 if (parse_extra)
8954 b->cond_string = NULL;
8955 else
8956 {
8957 /* Create a private copy of condition string. */
8958 b->cond_string.reset (cond_string != NULL
8959 ? xstrdup (cond_string)
8960 : NULL);
8961 b->thread = thread;
8962 }
8963
8964 /* Create a private copy of any extra string. */
8965 b->extra_string.reset (extra_string != NULL
8966 ? xstrdup (extra_string)
8967 : NULL);
8968 b->ignore_count = ignore_count;
8969 b->disposition = tempflag ? disp_del : disp_donttouch;
8970 b->condition_not_parsed = 1;
8971 b->enable_state = enabled ? bp_enabled : bp_disabled;
8972 if ((type_wanted != bp_breakpoint
8973 && type_wanted != bp_hardware_breakpoint) || thread != -1)
8974 b->pspace = current_program_space;
8975
8976 install_breakpoint (internal, std::move (b), 0);
8977 }
8978
8979 if (canonical.lsals.size () > 1)
8980 {
8981 warning (_("Multiple breakpoints were set.\nUse the "
8982 "\"delete\" command to delete unwanted breakpoints."));
8983 prev_breakpoint_count = prev_bkpt_count;
8984 }
8985
8986 update_global_location_list (UGLL_MAY_INSERT);
8987
8988 return 1;
8989 }
8990
8991 /* Set a breakpoint.
8992 ARG is a string describing breakpoint address,
8993 condition, and thread.
8994 FLAG specifies if a breakpoint is hardware on,
8995 and if breakpoint is temporary, using BP_HARDWARE_FLAG
8996 and BP_TEMPFLAG. */
8997
8998 static void
8999 break_command_1 (const char *arg, int flag, int from_tty)
9000 {
9001 int tempflag = flag & BP_TEMPFLAG;
9002 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9003 ? bp_hardware_breakpoint
9004 : bp_breakpoint);
9005
9006 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9007 const struct breakpoint_ops *ops
9008 = breakpoint_ops_for_location_spec (locspec.get (),
9009 false /* is_tracepoint */);
9010
9011 create_breakpoint (get_current_arch (),
9012 locspec.get (),
9013 NULL, 0, arg, false, 1 /* parse arg */,
9014 tempflag, type_wanted,
9015 0 /* Ignore count */,
9016 pending_break_support,
9017 ops,
9018 from_tty,
9019 1 /* enabled */,
9020 0 /* internal */,
9021 0);
9022 }
9023
9024 /* Helper function for break_command_1 and disassemble_command. */
9025
9026 void
9027 resolve_sal_pc (struct symtab_and_line *sal)
9028 {
9029 CORE_ADDR pc;
9030
9031 if (sal->pc == 0 && sal->symtab != NULL)
9032 {
9033 if (!find_line_pc (sal->symtab, sal->line, &pc))
9034 error (_("No line %d in file \"%s\"."),
9035 sal->line, symtab_to_filename_for_display (sal->symtab));
9036 sal->pc = pc;
9037
9038 /* If this SAL corresponds to a breakpoint inserted using a line
9039 number, then skip the function prologue if necessary. */
9040 if (sal->explicit_line)
9041 skip_prologue_sal (sal);
9042 }
9043
9044 if (sal->section == 0 && sal->symtab != NULL)
9045 {
9046 const struct blockvector *bv;
9047 const struct block *b;
9048 struct symbol *sym;
9049
9050 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9051 sal->symtab->compunit ());
9052 if (bv != NULL)
9053 {
9054 sym = block_linkage_function (b);
9055 if (sym != NULL)
9056 {
9057 fixup_symbol_section (sym, sal->symtab->compunit ()->objfile ());
9058 sal->section
9059 = sym->obj_section (sal->symtab->compunit ()->objfile ());
9060 }
9061 else
9062 {
9063 /* It really is worthwhile to have the section, so we'll
9064 just have to look harder. This case can be executed
9065 if we have line numbers but no functions (as can
9066 happen in assembly source). */
9067
9068 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9069 switch_to_program_space_and_thread (sal->pspace);
9070
9071 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9072 if (msym.minsym)
9073 sal->section = msym.obj_section ();
9074 }
9075 }
9076 }
9077 }
9078
9079 void
9080 break_command (const char *arg, int from_tty)
9081 {
9082 break_command_1 (arg, 0, from_tty);
9083 }
9084
9085 void
9086 tbreak_command (const char *arg, int from_tty)
9087 {
9088 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9089 }
9090
9091 static void
9092 hbreak_command (const char *arg, int from_tty)
9093 {
9094 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9095 }
9096
9097 static void
9098 thbreak_command (const char *arg, int from_tty)
9099 {
9100 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9101 }
9102
9103 /* The dynamic printf command is mostly like a regular breakpoint, but
9104 with a prewired command list consisting of a single output command,
9105 built from extra arguments supplied on the dprintf command
9106 line. */
9107
9108 static void
9109 dprintf_command (const char *arg, int from_tty)
9110 {
9111 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9112
9113 /* If non-NULL, ARG should have been advanced past the location;
9114 the next character must be ','. */
9115 if (arg != NULL)
9116 {
9117 if (arg[0] != ',' || arg[1] == '\0')
9118 error (_("Format string required"));
9119 else
9120 {
9121 /* Skip the comma. */
9122 ++arg;
9123 }
9124 }
9125
9126 create_breakpoint (get_current_arch (),
9127 locspec.get (),
9128 NULL, 0, arg, false, 1 /* parse arg */,
9129 0, bp_dprintf,
9130 0 /* Ignore count */,
9131 pending_break_support,
9132 &code_breakpoint_ops,
9133 from_tty,
9134 1 /* enabled */,
9135 0 /* internal */,
9136 0);
9137 }
9138
9139 static void
9140 agent_printf_command (const char *arg, int from_tty)
9141 {
9142 error (_("May only run agent-printf on the target"));
9143 }
9144
9145 /* Implement the "breakpoint_hit" method for ranged breakpoints. */
9146
9147 int
9148 ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9149 const address_space *aspace,
9150 CORE_ADDR bp_addr,
9151 const target_waitstatus &ws)
9152 {
9153 if (ws.kind () != TARGET_WAITKIND_STOPPED
9154 || ws.sig () != GDB_SIGNAL_TRAP)
9155 return 0;
9156
9157 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9158 bl->length, aspace, bp_addr);
9159 }
9160
9161 /* Implement the "resources_needed" method for ranged breakpoints. */
9162
9163 int
9164 ranged_breakpoint::resources_needed (const struct bp_location *bl)
9165 {
9166 return target_ranged_break_num_registers ();
9167 }
9168
9169 /* Implement the "print_it" method for ranged breakpoints. */
9170
9171 enum print_stop_action
9172 ranged_breakpoint::print_it (const bpstat *bs) const
9173 {
9174 struct bp_location *bl = loc;
9175 struct ui_out *uiout = current_uiout;
9176
9177 gdb_assert (type == bp_hardware_breakpoint);
9178
9179 /* Ranged breakpoints have only one location. */
9180 gdb_assert (bl && bl->next == NULL);
9181
9182 annotate_breakpoint (number);
9183
9184 maybe_print_thread_hit_breakpoint (uiout);
9185
9186 if (disposition == disp_del)
9187 uiout->text ("Temporary ranged breakpoint ");
9188 else
9189 uiout->text ("Ranged breakpoint ");
9190 if (uiout->is_mi_like_p ())
9191 {
9192 uiout->field_string ("reason",
9193 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9194 uiout->field_string ("disp", bpdisp_text (disposition));
9195 }
9196 uiout->field_signed ("bkptno", number);
9197 uiout->text (", ");
9198
9199 return PRINT_SRC_AND_LOC;
9200 }
9201
9202 /* Implement the "print_one" method for ranged breakpoints. */
9203
9204 bool
9205 ranged_breakpoint::print_one (bp_location **last_loc) const
9206 {
9207 struct bp_location *bl = loc;
9208 struct value_print_options opts;
9209 struct ui_out *uiout = current_uiout;
9210
9211 /* Ranged breakpoints have only one location. */
9212 gdb_assert (bl && bl->next == NULL);
9213
9214 get_user_print_options (&opts);
9215
9216 if (opts.addressprint)
9217 /* We don't print the address range here, it will be printed later
9218 by ranged_breakpoint::print_one_detail. */
9219 uiout->field_skip ("addr");
9220 annotate_field (5);
9221 print_breakpoint_location (this, bl);
9222 *last_loc = bl;
9223
9224 return true;
9225 }
9226
9227 /* Implement the "print_one_detail" method for ranged breakpoints. */
9228
9229 void
9230 ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
9231 {
9232 CORE_ADDR address_start, address_end;
9233 struct bp_location *bl = loc;
9234 string_file stb;
9235
9236 gdb_assert (bl);
9237
9238 address_start = bl->address;
9239 address_end = address_start + bl->length - 1;
9240
9241 uiout->text ("\taddress range: ");
9242 stb.printf ("[%s, %s]",
9243 print_core_address (bl->gdbarch, address_start),
9244 print_core_address (bl->gdbarch, address_end));
9245 uiout->field_stream ("addr", stb);
9246 uiout->text ("\n");
9247 }
9248
9249 /* Implement the "print_mention" method for ranged breakpoints. */
9250
9251 void
9252 ranged_breakpoint::print_mention () const
9253 {
9254 struct bp_location *bl = loc;
9255 struct ui_out *uiout = current_uiout;
9256
9257 gdb_assert (bl);
9258 gdb_assert (type == bp_hardware_breakpoint);
9259
9260 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9261 number, paddress (bl->gdbarch, bl->address),
9262 paddress (bl->gdbarch, bl->address + bl->length - 1));
9263 }
9264
9265 /* Implement the "print_recreate" method for ranged breakpoints. */
9266
9267 void
9268 ranged_breakpoint::print_recreate (struct ui_file *fp) const
9269 {
9270 gdb_printf (fp, "break-range %s, %s",
9271 locspec->to_string (),
9272 locspec_range_end->to_string ());
9273 print_recreate_thread (fp);
9274 }
9275
9276 /* Find the address where the end of the breakpoint range should be
9277 placed, given the SAL of the end of the range. This is so that if
9278 the user provides a line number, the end of the range is set to the
9279 last instruction of the given line. */
9280
9281 static CORE_ADDR
9282 find_breakpoint_range_end (struct symtab_and_line sal)
9283 {
9284 CORE_ADDR end;
9285
9286 /* If the user provided a PC value, use it. Otherwise,
9287 find the address of the end of the given location. */
9288 if (sal.explicit_pc)
9289 end = sal.pc;
9290 else
9291 {
9292 int ret;
9293 CORE_ADDR start;
9294
9295 ret = find_line_pc_range (sal, &start, &end);
9296 if (!ret)
9297 error (_("Could not find location of the end of the range."));
9298
9299 /* find_line_pc_range returns the start of the next line. */
9300 end--;
9301 }
9302
9303 return end;
9304 }
9305
9306 /* Implement the "break-range" CLI command. */
9307
9308 static void
9309 break_range_command (const char *arg, int from_tty)
9310 {
9311 const char *arg_start;
9312 struct linespec_result canonical_start, canonical_end;
9313 int bp_count, can_use_bp, length;
9314 CORE_ADDR end;
9315
9316 /* We don't support software ranged breakpoints. */
9317 if (target_ranged_break_num_registers () < 0)
9318 error (_("This target does not support hardware ranged breakpoints."));
9319
9320 bp_count = hw_breakpoint_used_count ();
9321 bp_count += target_ranged_break_num_registers ();
9322 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9323 bp_count, 0);
9324 if (can_use_bp < 0)
9325 error (_("Hardware breakpoints used exceeds limit."));
9326
9327 arg = skip_spaces (arg);
9328 if (arg == NULL || arg[0] == '\0')
9329 error(_("No address range specified."));
9330
9331 arg_start = arg;
9332 location_spec_up start_locspec
9333 = string_to_location_spec (&arg, current_language);
9334 parse_breakpoint_sals (start_locspec.get (), &canonical_start);
9335
9336 if (arg[0] != ',')
9337 error (_("Too few arguments."));
9338 else if (canonical_start.lsals.empty ())
9339 error (_("Could not find location of the beginning of the range."));
9340
9341 const linespec_sals &lsal_start = canonical_start.lsals[0];
9342
9343 if (canonical_start.lsals.size () > 1
9344 || lsal_start.sals.size () != 1)
9345 error (_("Cannot create a ranged breakpoint with multiple locations."));
9346
9347 const symtab_and_line &sal_start = lsal_start.sals[0];
9348 std::string addr_string_start (arg_start, arg - arg_start);
9349
9350 arg++; /* Skip the comma. */
9351 arg = skip_spaces (arg);
9352
9353 /* Parse the end location specification. */
9354
9355 arg_start = arg;
9356
9357 /* We call decode_line_full directly here instead of using
9358 parse_breakpoint_sals because we need to specify the start
9359 location spec's symtab and line as the default symtab and line
9360 for the end of the range. This makes it possible to have ranges
9361 like "foo.c:27, +14", where +14 means 14 lines from the start
9362 location spec. */
9363 location_spec_up end_locspec
9364 = string_to_location_spec (&arg, current_language);
9365 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9366 sal_start.symtab, sal_start.line,
9367 &canonical_end, NULL, NULL);
9368
9369 if (canonical_end.lsals.empty ())
9370 error (_("Could not find location of the end of the range."));
9371
9372 const linespec_sals &lsal_end = canonical_end.lsals[0];
9373 if (canonical_end.lsals.size () > 1
9374 || lsal_end.sals.size () != 1)
9375 error (_("Cannot create a ranged breakpoint with multiple locations."));
9376
9377 const symtab_and_line &sal_end = lsal_end.sals[0];
9378
9379 end = find_breakpoint_range_end (sal_end);
9380 if (sal_start.pc > end)
9381 error (_("Invalid address range, end precedes start."));
9382
9383 length = end - sal_start.pc + 1;
9384 if (length < 0)
9385 /* Length overflowed. */
9386 error (_("Address range too large."));
9387 else if (length == 1)
9388 {
9389 /* This range is simple enough to be handled by
9390 the `hbreak' command. */
9391 hbreak_command (&addr_string_start[0], 1);
9392
9393 return;
9394 }
9395
9396 /* Now set up the breakpoint and install it. */
9397
9398 std::unique_ptr<breakpoint> br
9399 (new ranged_breakpoint (get_current_arch (),
9400 sal_start, length,
9401 std::move (start_locspec),
9402 std::move (end_locspec)));
9403
9404 install_breakpoint (false, std::move (br), true);
9405 }
9406
9407 /* Return non-zero if EXP is verified as constant. Returned zero
9408 means EXP is variable. Also the constant detection may fail for
9409 some constant expressions and in such case still falsely return
9410 zero. */
9411
9412 static bool
9413 watchpoint_exp_is_const (const struct expression *exp)
9414 {
9415 return exp->op->constant_p ();
9416 }
9417
9418 /* Implement the "re_set" method for watchpoints. */
9419
9420 void
9421 watchpoint::re_set ()
9422 {
9423 /* Watchpoint can be either on expression using entirely global
9424 variables, or it can be on local variables.
9425
9426 Watchpoints of the first kind are never auto-deleted, and even
9427 persist across program restarts. Since they can use variables
9428 from shared libraries, we need to reparse expression as libraries
9429 are loaded and unloaded.
9430
9431 Watchpoints on local variables can also change meaning as result
9432 of solib event. For example, if a watchpoint uses both a local
9433 and a global variables in expression, it's a local watchpoint,
9434 but unloading of a shared library will make the expression
9435 invalid. This is not a very common use case, but we still
9436 re-evaluate expression, to avoid surprises to the user.
9437
9438 Note that for local watchpoints, we re-evaluate it only if
9439 watchpoints frame id is still valid. If it's not, it means the
9440 watchpoint is out of scope and will be deleted soon. In fact,
9441 I'm not sure we'll ever be called in this case.
9442
9443 If a local watchpoint's frame id is still valid, then
9444 exp_valid_block is likewise valid, and we can safely use it.
9445
9446 Don't do anything about disabled watchpoints, since they will be
9447 reevaluated again when enabled. */
9448 update_watchpoint (this, 1 /* reparse */);
9449 }
9450
9451 /* Implement the "insert" method for hardware watchpoints. */
9452
9453 int
9454 watchpoint::insert_location (struct bp_location *bl)
9455 {
9456 int length = exact ? 1 : bl->length;
9457
9458 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9459 cond_exp.get ());
9460 }
9461
9462 /* Implement the "remove" method for hardware watchpoints. */
9463
9464 int
9465 watchpoint::remove_location (struct bp_location *bl,
9466 enum remove_bp_reason reason)
9467 {
9468 int length = exact ? 1 : bl->length;
9469
9470 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9471 cond_exp.get ());
9472 }
9473
9474 int
9475 watchpoint::breakpoint_hit (const struct bp_location *bl,
9476 const address_space *aspace, CORE_ADDR bp_addr,
9477 const target_waitstatus &ws)
9478 {
9479 struct breakpoint *b = bl->owner;
9480
9481 /* Continuable hardware watchpoints are treated as non-existent if the
9482 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9483 some data address). Otherwise gdb won't stop on a break instruction
9484 in the code (not from a breakpoint) when a hardware watchpoint has
9485 been defined. Also skip watchpoints which we know did not trigger
9486 (did not match the data address). */
9487 if (is_hardware_watchpoint (b)
9488 && watchpoint_triggered == watch_triggered_no)
9489 return 0;
9490
9491 return 1;
9492 }
9493
9494 void
9495 watchpoint::check_status (bpstat *bs)
9496 {
9497 gdb_assert (is_watchpoint (bs->breakpoint_at));
9498
9499 bpstat_check_watchpoint (bs);
9500 }
9501
9502 /* Implement the "resources_needed" method for hardware
9503 watchpoints. */
9504
9505 int
9506 watchpoint::resources_needed (const struct bp_location *bl)
9507 {
9508 int length = exact? 1 : bl->length;
9509
9510 return target_region_ok_for_hw_watchpoint (bl->address, length);
9511 }
9512
9513 /* Implement the "works_in_software_mode" method for hardware
9514 watchpoints. */
9515
9516 bool
9517 watchpoint::works_in_software_mode () const
9518 {
9519 /* Read and access watchpoints only work with hardware support. */
9520 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9521 }
9522
9523 enum print_stop_action
9524 watchpoint::print_it (const bpstat *bs) const
9525 {
9526 struct breakpoint *b;
9527 enum print_stop_action result;
9528 struct ui_out *uiout = current_uiout;
9529
9530 gdb_assert (bs->bp_location_at != NULL);
9531
9532 b = bs->breakpoint_at;
9533
9534 annotate_watchpoint (b->number);
9535 maybe_print_thread_hit_breakpoint (uiout);
9536
9537 string_file stb;
9538
9539 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9540 switch (b->type)
9541 {
9542 case bp_watchpoint:
9543 case bp_hardware_watchpoint:
9544 if (uiout->is_mi_like_p ())
9545 uiout->field_string
9546 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9547 mention (b);
9548 tuple_emitter.emplace (uiout, "value");
9549 uiout->text ("\nOld value = ");
9550 watchpoint_value_print (bs->old_val.get (), &stb);
9551 uiout->field_stream ("old", stb);
9552 uiout->text ("\nNew value = ");
9553 watchpoint_value_print (val.get (), &stb);
9554 uiout->field_stream ("new", stb);
9555 uiout->text ("\n");
9556 /* More than one watchpoint may have been triggered. */
9557 result = PRINT_UNKNOWN;
9558 break;
9559
9560 case bp_read_watchpoint:
9561 if (uiout->is_mi_like_p ())
9562 uiout->field_string
9563 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9564 mention (b);
9565 tuple_emitter.emplace (uiout, "value");
9566 uiout->text ("\nValue = ");
9567 watchpoint_value_print (val.get (), &stb);
9568 uiout->field_stream ("value", stb);
9569 uiout->text ("\n");
9570 result = PRINT_UNKNOWN;
9571 break;
9572
9573 case bp_access_watchpoint:
9574 if (bs->old_val != NULL)
9575 {
9576 if (uiout->is_mi_like_p ())
9577 uiout->field_string
9578 ("reason",
9579 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9580 mention (b);
9581 tuple_emitter.emplace (uiout, "value");
9582 uiout->text ("\nOld value = ");
9583 watchpoint_value_print (bs->old_val.get (), &stb);
9584 uiout->field_stream ("old", stb);
9585 uiout->text ("\nNew value = ");
9586 }
9587 else
9588 {
9589 mention (b);
9590 if (uiout->is_mi_like_p ())
9591 uiout->field_string
9592 ("reason",
9593 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9594 tuple_emitter.emplace (uiout, "value");
9595 uiout->text ("\nValue = ");
9596 }
9597 watchpoint_value_print (val.get (), &stb);
9598 uiout->field_stream ("new", stb);
9599 uiout->text ("\n");
9600 result = PRINT_UNKNOWN;
9601 break;
9602 default:
9603 result = PRINT_UNKNOWN;
9604 }
9605
9606 return result;
9607 }
9608
9609 /* Implement the "print_mention" method for hardware watchpoints. */
9610
9611 void
9612 watchpoint::print_mention () const
9613 {
9614 struct ui_out *uiout = current_uiout;
9615 const char *tuple_name;
9616
9617 switch (type)
9618 {
9619 case bp_watchpoint:
9620 uiout->text ("Watchpoint ");
9621 tuple_name = "wpt";
9622 break;
9623 case bp_hardware_watchpoint:
9624 uiout->text ("Hardware watchpoint ");
9625 tuple_name = "wpt";
9626 break;
9627 case bp_read_watchpoint:
9628 uiout->text ("Hardware read watchpoint ");
9629 tuple_name = "hw-rwpt";
9630 break;
9631 case bp_access_watchpoint:
9632 uiout->text ("Hardware access (read/write) watchpoint ");
9633 tuple_name = "hw-awpt";
9634 break;
9635 default:
9636 internal_error (__FILE__, __LINE__,
9637 _("Invalid hardware watchpoint type."));
9638 }
9639
9640 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9641 uiout->field_signed ("number", number);
9642 uiout->text (": ");
9643 uiout->field_string ("exp", exp_string.get ());
9644 }
9645
9646 /* Implement the "print_recreate" method for watchpoints. */
9647
9648 void
9649 watchpoint::print_recreate (struct ui_file *fp) const
9650 {
9651 switch (type)
9652 {
9653 case bp_watchpoint:
9654 case bp_hardware_watchpoint:
9655 gdb_printf (fp, "watch");
9656 break;
9657 case bp_read_watchpoint:
9658 gdb_printf (fp, "rwatch");
9659 break;
9660 case bp_access_watchpoint:
9661 gdb_printf (fp, "awatch");
9662 break;
9663 default:
9664 internal_error (__FILE__, __LINE__,
9665 _("Invalid watchpoint type."));
9666 }
9667
9668 gdb_printf (fp, " %s", exp_string.get ());
9669 print_recreate_thread (fp);
9670 }
9671
9672 /* Implement the "explains_signal" method for watchpoints. */
9673
9674 bool
9675 watchpoint::explains_signal (enum gdb_signal sig)
9676 {
9677 /* A software watchpoint cannot cause a signal other than
9678 GDB_SIGNAL_TRAP. */
9679 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
9680 return false;
9681
9682 return true;
9683 }
9684
9685 struct masked_watchpoint : public watchpoint
9686 {
9687 using watchpoint::watchpoint;
9688
9689 int insert_location (struct bp_location *) override;
9690 int remove_location (struct bp_location *,
9691 enum remove_bp_reason reason) override;
9692 int resources_needed (const struct bp_location *) override;
9693 bool works_in_software_mode () const override;
9694 enum print_stop_action print_it (const bpstat *bs) const override;
9695 void print_one_detail (struct ui_out *) const override;
9696 void print_mention () const override;
9697 void print_recreate (struct ui_file *fp) const override;
9698 };
9699
9700 /* Implement the "insert" method for masked hardware watchpoints. */
9701
9702 int
9703 masked_watchpoint::insert_location (struct bp_location *bl)
9704 {
9705 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
9706 bl->watchpoint_type);
9707 }
9708
9709 /* Implement the "remove" method for masked hardware watchpoints. */
9710
9711 int
9712 masked_watchpoint::remove_location (struct bp_location *bl,
9713 enum remove_bp_reason reason)
9714 {
9715 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
9716 bl->watchpoint_type);
9717 }
9718
9719 /* Implement the "resources_needed" method for masked hardware
9720 watchpoints. */
9721
9722 int
9723 masked_watchpoint::resources_needed (const struct bp_location *bl)
9724 {
9725 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
9726 }
9727
9728 /* Implement the "works_in_software_mode" method for masked hardware
9729 watchpoints. */
9730
9731 bool
9732 masked_watchpoint::works_in_software_mode () const
9733 {
9734 return false;
9735 }
9736
9737 /* Implement the "print_it" method for masked hardware
9738 watchpoints. */
9739
9740 enum print_stop_action
9741 masked_watchpoint::print_it (const bpstat *bs) const
9742 {
9743 struct breakpoint *b = bs->breakpoint_at;
9744 struct ui_out *uiout = current_uiout;
9745
9746 /* Masked watchpoints have only one location. */
9747 gdb_assert (b->loc && b->loc->next == NULL);
9748
9749 annotate_watchpoint (b->number);
9750 maybe_print_thread_hit_breakpoint (uiout);
9751
9752 switch (b->type)
9753 {
9754 case bp_hardware_watchpoint:
9755 if (uiout->is_mi_like_p ())
9756 uiout->field_string
9757 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9758 break;
9759
9760 case bp_read_watchpoint:
9761 if (uiout->is_mi_like_p ())
9762 uiout->field_string
9763 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9764 break;
9765
9766 case bp_access_watchpoint:
9767 if (uiout->is_mi_like_p ())
9768 uiout->field_string
9769 ("reason",
9770 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9771 break;
9772 default:
9773 internal_error (__FILE__, __LINE__,
9774 _("Invalid hardware watchpoint type."));
9775 }
9776
9777 mention (b);
9778 uiout->text (_("\n\
9779 Check the underlying instruction at PC for the memory\n\
9780 address and value which triggered this watchpoint.\n"));
9781 uiout->text ("\n");
9782
9783 /* More than one watchpoint may have been triggered. */
9784 return PRINT_UNKNOWN;
9785 }
9786
9787 /* Implement the "print_one_detail" method for masked hardware
9788 watchpoints. */
9789
9790 void
9791 masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9792 {
9793 /* Masked watchpoints have only one location. */
9794 gdb_assert (loc && loc->next == NULL);
9795
9796 uiout->text ("\tmask ");
9797 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
9798 uiout->text ("\n");
9799 }
9800
9801 /* Implement the "print_mention" method for masked hardware
9802 watchpoints. */
9803
9804 void
9805 masked_watchpoint::print_mention () const
9806 {
9807 struct ui_out *uiout = current_uiout;
9808 const char *tuple_name;
9809
9810 switch (type)
9811 {
9812 case bp_hardware_watchpoint:
9813 uiout->text ("Masked hardware watchpoint ");
9814 tuple_name = "wpt";
9815 break;
9816 case bp_read_watchpoint:
9817 uiout->text ("Masked hardware read watchpoint ");
9818 tuple_name = "hw-rwpt";
9819 break;
9820 case bp_access_watchpoint:
9821 uiout->text ("Masked hardware access (read/write) watchpoint ");
9822 tuple_name = "hw-awpt";
9823 break;
9824 default:
9825 internal_error (__FILE__, __LINE__,
9826 _("Invalid hardware watchpoint type."));
9827 }
9828
9829 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9830 uiout->field_signed ("number", number);
9831 uiout->text (": ");
9832 uiout->field_string ("exp", exp_string.get ());
9833 }
9834
9835 /* Implement the "print_recreate" method for masked hardware
9836 watchpoints. */
9837
9838 void
9839 masked_watchpoint::print_recreate (struct ui_file *fp) const
9840 {
9841 switch (type)
9842 {
9843 case bp_hardware_watchpoint:
9844 gdb_printf (fp, "watch");
9845 break;
9846 case bp_read_watchpoint:
9847 gdb_printf (fp, "rwatch");
9848 break;
9849 case bp_access_watchpoint:
9850 gdb_printf (fp, "awatch");
9851 break;
9852 default:
9853 internal_error (__FILE__, __LINE__,
9854 _("Invalid hardware watchpoint type."));
9855 }
9856
9857 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
9858 phex (hw_wp_mask, sizeof (CORE_ADDR)));
9859 print_recreate_thread (fp);
9860 }
9861
9862 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
9863
9864 static bool
9865 is_masked_watchpoint (const struct breakpoint *b)
9866 {
9867 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9868 }
9869
9870 /* accessflag: hw_write: watch write,
9871 hw_read: watch read,
9872 hw_access: watch access (read or write) */
9873 static void
9874 watch_command_1 (const char *arg, int accessflag, int from_tty,
9875 bool just_location, bool internal)
9876 {
9877 struct breakpoint *scope_breakpoint = NULL;
9878 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
9879 struct value *result;
9880 int saved_bitpos = 0, saved_bitsize = 0;
9881 const char *exp_start = NULL;
9882 const char *exp_end = NULL;
9883 const char *tok, *end_tok;
9884 int toklen = -1;
9885 const char *cond_start = NULL;
9886 const char *cond_end = NULL;
9887 enum bptype bp_type;
9888 int thread = -1;
9889 /* Flag to indicate whether we are going to use masks for
9890 the hardware watchpoint. */
9891 bool use_mask = false;
9892 CORE_ADDR mask = 0;
9893 int task = 0;
9894
9895 /* Make sure that we actually have parameters to parse. */
9896 if (arg != NULL && arg[0] != '\0')
9897 {
9898 const char *value_start;
9899
9900 exp_end = arg + strlen (arg);
9901
9902 /* Look for "parameter value" pairs at the end
9903 of the arguments string. */
9904 for (tok = exp_end - 1; tok > arg; tok--)
9905 {
9906 /* Skip whitespace at the end of the argument list. */
9907 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9908 tok--;
9909
9910 /* Find the beginning of the last token.
9911 This is the value of the parameter. */
9912 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9913 tok--;
9914 value_start = tok + 1;
9915
9916 /* Skip whitespace. */
9917 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9918 tok--;
9919
9920 end_tok = tok;
9921
9922 /* Find the beginning of the second to last token.
9923 This is the parameter itself. */
9924 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9925 tok--;
9926 tok++;
9927 toklen = end_tok - tok + 1;
9928
9929 if (toklen == 6 && startswith (tok, "thread"))
9930 {
9931 struct thread_info *thr;
9932 /* At this point we've found a "thread" token, which means
9933 the user is trying to set a watchpoint that triggers
9934 only in a specific thread. */
9935 const char *endp;
9936
9937 if (thread != -1)
9938 error(_("You can specify only one thread."));
9939
9940 /* Extract the thread ID from the next token. */
9941 thr = parse_thread_id (value_start, &endp);
9942
9943 /* Check if the user provided a valid thread ID. */
9944 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9945 invalid_thread_id_error (value_start);
9946
9947 thread = thr->global_num;
9948 }
9949 else if (toklen == 4 && startswith (tok, "task"))
9950 {
9951 char *tmp;
9952
9953 task = strtol (value_start, &tmp, 0);
9954 if (tmp == value_start)
9955 error (_("Junk after task keyword."));
9956 if (!valid_task_id (task))
9957 error (_("Unknown task %d."), task);
9958 }
9959 else if (toklen == 4 && startswith (tok, "mask"))
9960 {
9961 /* We've found a "mask" token, which means the user wants to
9962 create a hardware watchpoint that is going to have the mask
9963 facility. */
9964 struct value *mask_value, *mark;
9965
9966 if (use_mask)
9967 error(_("You can specify only one mask."));
9968
9969 use_mask = just_location = true;
9970
9971 mark = value_mark ();
9972 mask_value = parse_to_comma_and_eval (&value_start);
9973 mask = value_as_address (mask_value);
9974 value_free_to_mark (mark);
9975 }
9976 else
9977 /* We didn't recognize what we found. We should stop here. */
9978 break;
9979
9980 /* Truncate the string and get rid of the "parameter value" pair before
9981 the arguments string is parsed by the parse_exp_1 function. */
9982 exp_end = tok;
9983 }
9984 }
9985 else
9986 exp_end = arg;
9987
9988 /* Parse the rest of the arguments. From here on out, everything
9989 is in terms of a newly allocated string instead of the original
9990 ARG. */
9991 std::string expression (arg, exp_end - arg);
9992 exp_start = arg = expression.c_str ();
9993 innermost_block_tracker tracker;
9994 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
9995 exp_end = arg;
9996 /* Remove trailing whitespace from the expression before saving it.
9997 This makes the eventual display of the expression string a bit
9998 prettier. */
9999 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10000 --exp_end;
10001
10002 /* Checking if the expression is not constant. */
10003 if (watchpoint_exp_is_const (exp.get ()))
10004 {
10005 int len;
10006
10007 len = exp_end - exp_start;
10008 while (len > 0 && isspace (exp_start[len - 1]))
10009 len--;
10010 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10011 }
10012
10013 exp_valid_block = tracker.block ();
10014 struct value *mark = value_mark ();
10015 struct value *val_as_value = nullptr;
10016 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10017 just_location);
10018
10019 if (val_as_value != NULL && just_location)
10020 {
10021 saved_bitpos = value_bitpos (val_as_value);
10022 saved_bitsize = value_bitsize (val_as_value);
10023 }
10024
10025 value_ref_ptr val;
10026 if (just_location)
10027 {
10028 int ret;
10029
10030 exp_valid_block = NULL;
10031 val = release_value (value_addr (result));
10032 value_free_to_mark (mark);
10033
10034 if (use_mask)
10035 {
10036 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10037 mask);
10038 if (ret == -1)
10039 error (_("This target does not support masked watchpoints."));
10040 else if (ret == -2)
10041 error (_("Invalid mask or memory region."));
10042 }
10043 }
10044 else if (val_as_value != NULL)
10045 val = release_value (val_as_value);
10046
10047 tok = skip_spaces (arg);
10048 end_tok = skip_to_space (tok);
10049
10050 toklen = end_tok - tok;
10051 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10052 {
10053 tok = cond_start = end_tok + 1;
10054 innermost_block_tracker if_tracker;
10055 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10056
10057 /* The watchpoint expression may not be local, but the condition
10058 may still be. E.g.: `watch global if local > 0'. */
10059 cond_exp_valid_block = if_tracker.block ();
10060
10061 cond_end = tok;
10062 }
10063 if (*tok)
10064 error (_("Junk at end of command."));
10065
10066 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10067
10068 /* Save this because create_internal_breakpoint below invalidates
10069 'wp_frame'. */
10070 frame_id watchpoint_frame = get_frame_id (wp_frame);
10071
10072 /* If the expression is "local", then set up a "watchpoint scope"
10073 breakpoint at the point where we've left the scope of the watchpoint
10074 expression. Create the scope breakpoint before the watchpoint, so
10075 that we will encounter it first in bpstat_stop_status. */
10076 if (exp_valid_block != NULL && wp_frame != NULL)
10077 {
10078 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10079
10080 if (frame_id_p (caller_frame_id))
10081 {
10082 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10083 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10084
10085 scope_breakpoint
10086 = create_internal_breakpoint (caller_arch, caller_pc,
10087 bp_watchpoint_scope);
10088
10089 /* create_internal_breakpoint could invalidate WP_FRAME. */
10090 wp_frame = NULL;
10091
10092 scope_breakpoint->enable_state = bp_enabled;
10093
10094 /* Automatically delete the breakpoint when it hits. */
10095 scope_breakpoint->disposition = disp_del;
10096
10097 /* Only break in the proper frame (help with recursion). */
10098 scope_breakpoint->frame_id = caller_frame_id;
10099
10100 /* Set the address at which we will stop. */
10101 scope_breakpoint->loc->gdbarch = caller_arch;
10102 scope_breakpoint->loc->requested_address = caller_pc;
10103 scope_breakpoint->loc->address
10104 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10105 scope_breakpoint->loc->requested_address,
10106 scope_breakpoint->type,
10107 current_program_space);
10108 }
10109 }
10110
10111 /* Now set up the breakpoint. We create all watchpoints as hardware
10112 watchpoints here even if hardware watchpoints are turned off, a call
10113 to update_watchpoint later in this function will cause the type to
10114 drop back to bp_watchpoint (software watchpoint) if required. */
10115
10116 if (accessflag == hw_read)
10117 bp_type = bp_read_watchpoint;
10118 else if (accessflag == hw_access)
10119 bp_type = bp_access_watchpoint;
10120 else
10121 bp_type = bp_hardware_watchpoint;
10122
10123 std::unique_ptr<watchpoint> w;
10124 if (use_mask)
10125 w.reset (new masked_watchpoint (nullptr, bp_type));
10126 else
10127 w.reset (new watchpoint (nullptr, bp_type));
10128
10129 w->thread = thread;
10130 w->task = task;
10131 w->disposition = disp_donttouch;
10132 w->pspace = current_program_space;
10133 w->exp = std::move (exp);
10134 w->exp_valid_block = exp_valid_block;
10135 w->cond_exp_valid_block = cond_exp_valid_block;
10136 if (just_location)
10137 {
10138 struct type *t = value_type (val.get ());
10139 CORE_ADDR addr = value_as_address (val.get ());
10140
10141 w->exp_string_reparse
10142 = current_language->watch_location_expression (t, addr);
10143
10144 w->exp_string = xstrprintf ("-location %.*s",
10145 (int) (exp_end - exp_start), exp_start);
10146 }
10147 else
10148 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10149
10150 if (use_mask)
10151 {
10152 w->hw_wp_mask = mask;
10153 }
10154 else
10155 {
10156 w->val = val;
10157 w->val_bitpos = saved_bitpos;
10158 w->val_bitsize = saved_bitsize;
10159 w->val_valid = true;
10160 }
10161
10162 if (cond_start)
10163 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10164 else
10165 w->cond_string = 0;
10166
10167 if (frame_id_p (watchpoint_frame))
10168 {
10169 w->watchpoint_frame = watchpoint_frame;
10170 w->watchpoint_thread = inferior_ptid;
10171 }
10172 else
10173 {
10174 w->watchpoint_frame = null_frame_id;
10175 w->watchpoint_thread = null_ptid;
10176 }
10177
10178 if (scope_breakpoint != NULL)
10179 {
10180 /* The scope breakpoint is related to the watchpoint. We will
10181 need to act on them together. */
10182 w->related_breakpoint = scope_breakpoint;
10183 scope_breakpoint->related_breakpoint = w.get ();
10184 }
10185
10186 if (!just_location)
10187 value_free_to_mark (mark);
10188
10189 /* Finally update the new watchpoint. This creates the locations
10190 that should be inserted. */
10191 update_watchpoint (w.get (), 1);
10192
10193 install_breakpoint (internal, std::move (w), 1);
10194 }
10195
10196 /* Return count of debug registers needed to watch the given expression.
10197 If the watchpoint cannot be handled in hardware return zero. */
10198
10199 static int
10200 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10201 {
10202 int found_memory_cnt = 0;
10203
10204 /* Did the user specifically forbid us to use hardware watchpoints? */
10205 if (!can_use_hw_watchpoints)
10206 return 0;
10207
10208 gdb_assert (!vals.empty ());
10209 struct value *head = vals[0].get ();
10210
10211 /* Make sure that the value of the expression depends only upon
10212 memory contents, and values computed from them within GDB. If we
10213 find any register references or function calls, we can't use a
10214 hardware watchpoint.
10215
10216 The idea here is that evaluating an expression generates a series
10217 of values, one holding the value of every subexpression. (The
10218 expression a*b+c has five subexpressions: a, b, a*b, c, and
10219 a*b+c.) GDB's values hold almost enough information to establish
10220 the criteria given above --- they identify memory lvalues,
10221 register lvalues, computed values, etcetera. So we can evaluate
10222 the expression, and then scan the chain of values that leaves
10223 behind to decide whether we can detect any possible change to the
10224 expression's final value using only hardware watchpoints.
10225
10226 However, I don't think that the values returned by inferior
10227 function calls are special in any way. So this function may not
10228 notice that an expression involving an inferior function call
10229 can't be watched with hardware watchpoints. FIXME. */
10230 for (const value_ref_ptr &iter : vals)
10231 {
10232 struct value *v = iter.get ();
10233
10234 if (VALUE_LVAL (v) == lval_memory)
10235 {
10236 if (v != head && value_lazy (v))
10237 /* A lazy memory lvalue in the chain is one that GDB never
10238 needed to fetch; we either just used its address (e.g.,
10239 `a' in `a.b') or we never needed it at all (e.g., `a'
10240 in `a,b'). This doesn't apply to HEAD; if that is
10241 lazy then it was not readable, but watch it anyway. */
10242 ;
10243 else
10244 {
10245 /* Ahh, memory we actually used! Check if we can cover
10246 it with hardware watchpoints. */
10247 struct type *vtype = check_typedef (value_type (v));
10248
10249 /* We only watch structs and arrays if user asked for it
10250 explicitly, never if they just happen to appear in a
10251 middle of some value chain. */
10252 if (v == head
10253 || (vtype->code () != TYPE_CODE_STRUCT
10254 && vtype->code () != TYPE_CODE_ARRAY))
10255 {
10256 CORE_ADDR vaddr = value_address (v);
10257 int len;
10258 int num_regs;
10259
10260 len = (target_exact_watchpoints
10261 && is_scalar_type_recursive (vtype))?
10262 1 : TYPE_LENGTH (value_type (v));
10263
10264 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10265 if (!num_regs)
10266 return 0;
10267 else
10268 found_memory_cnt += num_regs;
10269 }
10270 }
10271 }
10272 else if (VALUE_LVAL (v) != not_lval
10273 && deprecated_value_modifiable (v) == 0)
10274 return 0; /* These are values from the history (e.g., $1). */
10275 else if (VALUE_LVAL (v) == lval_register)
10276 return 0; /* Cannot watch a register with a HW watchpoint. */
10277 }
10278
10279 /* The expression itself looks suitable for using a hardware
10280 watchpoint, but give the target machine a chance to reject it. */
10281 return found_memory_cnt;
10282 }
10283
10284 void
10285 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10286 {
10287 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10288 }
10289
10290 /* Options for the watch, awatch, and rwatch commands. */
10291
10292 struct watch_options
10293 {
10294 /* For -location. */
10295 bool location = false;
10296 };
10297
10298 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10299
10300 Historically GDB always accepted both '-location' and '-l' flags for
10301 these commands (both flags being synonyms). When converting to the
10302 newer option scheme only '-location' is added here. That's fine (for
10303 backward compatibility) as any non-ambiguous prefix of a flag will be
10304 accepted, so '-l', '-loc', are now all accepted.
10305
10306 What this means is that, if in the future, we add any new flag here
10307 that starts with '-l' then this will break backward compatibility, so
10308 please, don't do that! */
10309
10310 static const gdb::option::option_def watch_option_defs[] = {
10311 gdb::option::flag_option_def<watch_options> {
10312 "location",
10313 [] (watch_options *opt) { return &opt->location; },
10314 N_("\
10315 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10316 -l can be used as a short form of -location."),
10317 },
10318 };
10319
10320 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10321 commands. */
10322
10323 static gdb::option::option_def_group
10324 make_watch_options_def_group (watch_options *opts)
10325 {
10326 return {{watch_option_defs}, opts};
10327 }
10328
10329 /* A helper function that looks for the "-location" argument and then
10330 calls watch_command_1. */
10331
10332 static void
10333 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10334 {
10335 watch_options opts;
10336 auto grp = make_watch_options_def_group (&opts);
10337 gdb::option::process_options
10338 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10339 if (arg != nullptr && *arg == '\0')
10340 arg = nullptr;
10341
10342 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10343 }
10344
10345 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10346 static void
10347 watch_command_completer (struct cmd_list_element *ignore,
10348 completion_tracker &tracker,
10349 const char *text, const char * /*word*/)
10350 {
10351 const auto group = make_watch_options_def_group (nullptr);
10352 if (gdb::option::complete_options
10353 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10354 return;
10355
10356 const char *word = advance_to_expression_complete_word_point (tracker, text);
10357 expression_completer (ignore, tracker, text, word);
10358 }
10359
10360 static void
10361 watch_command (const char *arg, int from_tty)
10362 {
10363 watch_maybe_just_location (arg, hw_write, from_tty);
10364 }
10365
10366 void
10367 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10368 {
10369 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10370 }
10371
10372 static void
10373 rwatch_command (const char *arg, int from_tty)
10374 {
10375 watch_maybe_just_location (arg, hw_read, from_tty);
10376 }
10377
10378 void
10379 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
10380 {
10381 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10382 }
10383
10384 static void
10385 awatch_command (const char *arg, int from_tty)
10386 {
10387 watch_maybe_just_location (arg, hw_access, from_tty);
10388 }
10389 \f
10390
10391 /* Data for the FSM that manages the until(location)/advance commands
10392 in infcmd.c. Here because it uses the mechanisms of
10393 breakpoints. */
10394
10395 struct until_break_fsm : public thread_fsm
10396 {
10397 /* The thread that was current when the command was executed. */
10398 int thread;
10399
10400 /* The breakpoint set at the return address in the caller frame,
10401 plus breakpoints at all the destination locations. */
10402 std::vector<breakpoint_up> breakpoints;
10403
10404 until_break_fsm (struct interp *cmd_interp, int thread,
10405 std::vector<breakpoint_up> &&breakpoints)
10406 : thread_fsm (cmd_interp),
10407 thread (thread),
10408 breakpoints (std::move (breakpoints))
10409 {
10410 }
10411
10412 void clean_up (struct thread_info *thread) override;
10413 bool should_stop (struct thread_info *thread) override;
10414 enum async_reply_reason do_async_reply_reason () override;
10415 };
10416
10417 /* Implementation of the 'should_stop' FSM method for the
10418 until(location)/advance commands. */
10419
10420 bool
10421 until_break_fsm::should_stop (struct thread_info *tp)
10422 {
10423 for (const breakpoint_up &bp : breakpoints)
10424 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10425 bp.get ()) != NULL)
10426 {
10427 set_finished ();
10428 break;
10429 }
10430
10431 return true;
10432 }
10433
10434 /* Implementation of the 'clean_up' FSM method for the
10435 until(location)/advance commands. */
10436
10437 void
10438 until_break_fsm::clean_up (struct thread_info *)
10439 {
10440 /* Clean up our temporary breakpoints. */
10441 breakpoints.clear ();
10442 delete_longjmp_breakpoint (thread);
10443 }
10444
10445 /* Implementation of the 'async_reply_reason' FSM method for the
10446 until(location)/advance commands. */
10447
10448 enum async_reply_reason
10449 until_break_fsm::do_async_reply_reason ()
10450 {
10451 return EXEC_ASYNC_LOCATION_REACHED;
10452 }
10453
10454 void
10455 until_break_command (const char *arg, int from_tty, int anywhere)
10456 {
10457 struct frame_info *frame;
10458 struct gdbarch *frame_gdbarch;
10459 struct frame_id stack_frame_id;
10460 struct frame_id caller_frame_id;
10461 int thread;
10462 struct thread_info *tp;
10463
10464 clear_proceed_status (0);
10465
10466 /* Set a breakpoint where the user wants it and at return from
10467 this function. */
10468
10469 location_spec_up locspec = string_to_location_spec (&arg, current_language);
10470
10471 std::vector<symtab_and_line> sals
10472 = (last_displayed_sal_is_valid ()
10473 ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10474 get_last_displayed_symtab (),
10475 get_last_displayed_line ())
10476 : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
10477 NULL, NULL, 0));
10478
10479 if (sals.empty ())
10480 error (_("Couldn't get information on specified line."));
10481
10482 if (*arg)
10483 error (_("Junk at end of arguments."));
10484
10485 tp = inferior_thread ();
10486 thread = tp->global_num;
10487
10488 /* Note linespec handling above invalidates the frame chain.
10489 Installing a breakpoint also invalidates the frame chain (as it
10490 may need to switch threads), so do any frame handling before
10491 that. */
10492
10493 frame = get_selected_frame (NULL);
10494 frame_gdbarch = get_frame_arch (frame);
10495 stack_frame_id = get_stack_frame_id (frame);
10496 caller_frame_id = frame_unwind_caller_id (frame);
10497
10498 /* Keep within the current frame, or in frames called by the current
10499 one. */
10500
10501 std::vector<breakpoint_up> breakpoints;
10502
10503 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10504
10505 if (frame_id_p (caller_frame_id))
10506 {
10507 struct symtab_and_line sal2;
10508 struct gdbarch *caller_gdbarch;
10509
10510 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10511 sal2.pc = frame_unwind_caller_pc (frame);
10512 caller_gdbarch = frame_unwind_caller_arch (frame);
10513
10514 breakpoint_up caller_breakpoint
10515 = set_momentary_breakpoint (caller_gdbarch, sal2,
10516 caller_frame_id, bp_until);
10517 breakpoints.emplace_back (std::move (caller_breakpoint));
10518
10519 set_longjmp_breakpoint (tp, stack_frame_id);
10520 lj_deleter.emplace (thread);
10521 }
10522
10523 /* set_momentary_breakpoint could invalidate FRAME. */
10524 frame = NULL;
10525
10526 /* If the user told us to continue until a specified location, we
10527 don't specify a frame at which we need to stop. Otherwise,
10528 specify the selected frame, because we want to stop only at the
10529 very same frame. */
10530 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10531
10532 for (symtab_and_line &sal : sals)
10533 {
10534 resolve_sal_pc (&sal);
10535
10536 breakpoint_up location_breakpoint
10537 = set_momentary_breakpoint (frame_gdbarch, sal,
10538 stop_frame_id, bp_until);
10539 breakpoints.emplace_back (std::move (location_breakpoint));
10540 }
10541
10542 tp->set_thread_fsm
10543 (std::unique_ptr<thread_fsm>
10544 (new until_break_fsm (command_interp (), tp->global_num,
10545 std::move (breakpoints))));
10546
10547 if (lj_deleter)
10548 lj_deleter->release ();
10549
10550 proceed (-1, GDB_SIGNAL_DEFAULT);
10551 }
10552
10553 \f
10554
10555 /* Compare two breakpoints and return a strcmp-like result. */
10556
10557 static int
10558 compare_breakpoints (const breakpoint *a, const breakpoint *b)
10559 {
10560 uintptr_t ua = (uintptr_t) a;
10561 uintptr_t ub = (uintptr_t) b;
10562
10563 if (a->number < b->number)
10564 return -1;
10565 else if (a->number > b->number)
10566 return 1;
10567
10568 /* Now sort by address, in case we see, e..g, two breakpoints with
10569 the number 0. */
10570 if (ua < ub)
10571 return -1;
10572 return ua > ub ? 1 : 0;
10573 }
10574
10575 /* Delete breakpoints by address or line. */
10576
10577 static void
10578 clear_command (const char *arg, int from_tty)
10579 {
10580 int default_match;
10581
10582 std::vector<symtab_and_line> decoded_sals;
10583 symtab_and_line last_sal;
10584 gdb::array_view<symtab_and_line> sals;
10585 if (arg)
10586 {
10587 decoded_sals
10588 = decode_line_with_current_source (arg,
10589 (DECODE_LINE_FUNFIRSTLINE
10590 | DECODE_LINE_LIST_MODE));
10591 default_match = 0;
10592 sals = decoded_sals;
10593 }
10594 else
10595 {
10596 /* Set sal's line, symtab, pc, and pspace to the values
10597 corresponding to the last call to print_frame_info. If the
10598 codepoint is not valid, this will set all the fields to 0. */
10599 last_sal = get_last_displayed_sal ();
10600 if (last_sal.symtab == 0)
10601 error (_("No source file specified."));
10602
10603 default_match = 1;
10604 sals = last_sal;
10605 }
10606
10607 /* We don't call resolve_sal_pc here. That's not as bad as it
10608 seems, because all existing breakpoints typically have both
10609 file/line and pc set. So, if clear is given file/line, we can
10610 match this to existing breakpoint without obtaining pc at all.
10611
10612 We only support clearing given the address explicitly
10613 present in breakpoint table. Say, we've set breakpoint
10614 at file:line. There were several PC values for that file:line,
10615 due to optimization, all in one block.
10616
10617 We've picked one PC value. If "clear" is issued with another
10618 PC corresponding to the same file:line, the breakpoint won't
10619 be cleared. We probably can still clear the breakpoint, but
10620 since the other PC value is never presented to user, user
10621 can only find it by guessing, and it does not seem important
10622 to support that. */
10623
10624 /* For each line spec given, delete bps which correspond to it. Do
10625 it in two passes, solely to preserve the current behavior that
10626 from_tty is forced true if we delete more than one
10627 breakpoint. */
10628
10629 std::vector<struct breakpoint *> found;
10630 for (const auto &sal : sals)
10631 {
10632 const char *sal_fullname;
10633
10634 /* If exact pc given, clear bpts at that pc.
10635 If line given (pc == 0), clear all bpts on specified line.
10636 If defaulting, clear all bpts on default line
10637 or at default pc.
10638
10639 defaulting sal.pc != 0 tests to do
10640
10641 0 1 pc
10642 1 1 pc _and_ line
10643 0 0 line
10644 1 0 <can't happen> */
10645
10646 sal_fullname = (sal.symtab == NULL
10647 ? NULL : symtab_to_fullname (sal.symtab));
10648
10649 /* Find all matching breakpoints and add them to 'found'. */
10650 for (breakpoint *b : all_breakpoints ())
10651 {
10652 int match = 0;
10653 /* Are we going to delete b? */
10654 if (b->type != bp_none && !is_watchpoint (b)
10655 && user_breakpoint_p (b))
10656 {
10657 for (bp_location *loc : b->locations ())
10658 {
10659 /* If the user specified file:line, don't allow a PC
10660 match. This matches historical gdb behavior. */
10661 int pc_match = (!sal.explicit_line
10662 && sal.pc
10663 && (loc->pspace == sal.pspace)
10664 && (loc->address == sal.pc)
10665 && (!section_is_overlay (loc->section)
10666 || loc->section == sal.section));
10667 int line_match = 0;
10668
10669 if ((default_match || sal.explicit_line)
10670 && loc->symtab != NULL
10671 && sal_fullname != NULL
10672 && sal.pspace == loc->pspace
10673 && loc->line_number == sal.line
10674 && filename_cmp (symtab_to_fullname (loc->symtab),
10675 sal_fullname) == 0)
10676 line_match = 1;
10677
10678 if (pc_match || line_match)
10679 {
10680 match = 1;
10681 break;
10682 }
10683 }
10684 }
10685
10686 if (match)
10687 found.push_back (b);
10688 }
10689 }
10690
10691 /* Now go thru the 'found' chain and delete them. */
10692 if (found.empty ())
10693 {
10694 if (arg)
10695 error (_("No breakpoint at %s."), arg);
10696 else
10697 error (_("No breakpoint at this line."));
10698 }
10699
10700 /* Remove duplicates from the vec. */
10701 std::sort (found.begin (), found.end (),
10702 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10703 {
10704 return compare_breakpoints (bp_a, bp_b) < 0;
10705 });
10706 found.erase (std::unique (found.begin (), found.end (),
10707 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10708 {
10709 return compare_breakpoints (bp_a, bp_b) == 0;
10710 }),
10711 found.end ());
10712
10713 if (found.size () > 1)
10714 from_tty = 1; /* Always report if deleted more than one. */
10715 if (from_tty)
10716 {
10717 if (found.size () == 1)
10718 gdb_printf (_("Deleted breakpoint "));
10719 else
10720 gdb_printf (_("Deleted breakpoints "));
10721 }
10722
10723 for (breakpoint *iter : found)
10724 {
10725 if (from_tty)
10726 gdb_printf ("%d ", iter->number);
10727 delete_breakpoint (iter);
10728 }
10729 if (from_tty)
10730 gdb_putc ('\n');
10731 }
10732 \f
10733 /* Delete breakpoint in BS if they are `delete' breakpoints and
10734 all breakpoints that are marked for deletion, whether hit or not.
10735 This is called after any breakpoint is hit, or after errors. */
10736
10737 void
10738 breakpoint_auto_delete (bpstat *bs)
10739 {
10740 for (; bs; bs = bs->next)
10741 if (bs->breakpoint_at
10742 && bs->breakpoint_at->disposition == disp_del
10743 && bs->stop)
10744 delete_breakpoint (bs->breakpoint_at);
10745
10746 for (breakpoint *b : all_breakpoints_safe ())
10747 if (b->disposition == disp_del_at_next_stop)
10748 delete_breakpoint (b);
10749 }
10750
10751 /* A comparison function for bp_location AP and BP being interfaced to
10752 std::sort. Sort elements primarily by their ADDRESS (no matter what
10753 bl_address_is_meaningful says), secondarily by ordering first
10754 permanent elements and terciarily just ensuring the array is sorted
10755 stable way despite std::sort being an unstable algorithm. */
10756
10757 static int
10758 bp_location_is_less_than (const bp_location *a, const bp_location *b)
10759 {
10760 if (a->address != b->address)
10761 return a->address < b->address;
10762
10763 /* Sort locations at the same address by their pspace number, keeping
10764 locations of the same inferior (in a multi-inferior environment)
10765 grouped. */
10766
10767 if (a->pspace->num != b->pspace->num)
10768 return a->pspace->num < b->pspace->num;
10769
10770 /* Sort permanent breakpoints first. */
10771 if (a->permanent != b->permanent)
10772 return a->permanent > b->permanent;
10773
10774 /* Sort by type in order to make duplicate determination easier.
10775 See update_global_location_list. This is kept in sync with
10776 breakpoint_locations_match. */
10777 if (a->loc_type < b->loc_type)
10778 return true;
10779
10780 /* Likewise, for range-breakpoints, sort by length. */
10781 if (a->loc_type == bp_loc_hardware_breakpoint
10782 && b->loc_type == bp_loc_hardware_breakpoint
10783 && a->length < b->length)
10784 return true;
10785
10786 /* Make the internal GDB representation stable across GDB runs
10787 where A and B memory inside GDB can differ. Breakpoint locations of
10788 the same type at the same address can be sorted in arbitrary order. */
10789
10790 if (a->owner->number != b->owner->number)
10791 return a->owner->number < b->owner->number;
10792
10793 return a < b;
10794 }
10795
10796 /* Set bp_locations_placed_address_before_address_max and
10797 bp_locations_shadow_len_after_address_max according to the current
10798 content of the bp_locations array. */
10799
10800 static void
10801 bp_locations_target_extensions_update (void)
10802 {
10803 bp_locations_placed_address_before_address_max = 0;
10804 bp_locations_shadow_len_after_address_max = 0;
10805
10806 for (bp_location *bl : all_bp_locations ())
10807 {
10808 CORE_ADDR start, end, addr;
10809
10810 if (!bp_location_has_shadow (bl))
10811 continue;
10812
10813 start = bl->target_info.placed_address;
10814 end = start + bl->target_info.shadow_len;
10815
10816 gdb_assert (bl->address >= start);
10817 addr = bl->address - start;
10818 if (addr > bp_locations_placed_address_before_address_max)
10819 bp_locations_placed_address_before_address_max = addr;
10820
10821 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10822
10823 gdb_assert (bl->address < end);
10824 addr = end - bl->address;
10825 if (addr > bp_locations_shadow_len_after_address_max)
10826 bp_locations_shadow_len_after_address_max = addr;
10827 }
10828 }
10829
10830 /* Download tracepoint locations if they haven't been. */
10831
10832 static void
10833 download_tracepoint_locations (void)
10834 {
10835 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
10836
10837 scoped_restore_current_pspace_and_thread restore_pspace_thread;
10838
10839 for (breakpoint *b : all_tracepoints ())
10840 {
10841 struct tracepoint *t;
10842 int bp_location_downloaded = 0;
10843
10844 if ((b->type == bp_fast_tracepoint
10845 ? !may_insert_fast_tracepoints
10846 : !may_insert_tracepoints))
10847 continue;
10848
10849 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
10850 {
10851 if (target_can_download_tracepoint ())
10852 can_download_tracepoint = TRIBOOL_TRUE;
10853 else
10854 can_download_tracepoint = TRIBOOL_FALSE;
10855 }
10856
10857 if (can_download_tracepoint == TRIBOOL_FALSE)
10858 break;
10859
10860 for (bp_location *bl : b->locations ())
10861 {
10862 /* In tracepoint, locations are _never_ duplicated, so
10863 should_be_inserted is equivalent to
10864 unduplicated_should_be_inserted. */
10865 if (!should_be_inserted (bl) || bl->inserted)
10866 continue;
10867
10868 switch_to_program_space_and_thread (bl->pspace);
10869
10870 target_download_tracepoint (bl);
10871
10872 bl->inserted = 1;
10873 bp_location_downloaded = 1;
10874 }
10875 t = (struct tracepoint *) b;
10876 t->number_on_target = b->number;
10877 if (bp_location_downloaded)
10878 gdb::observers::breakpoint_modified.notify (b);
10879 }
10880 }
10881
10882 /* Swap the insertion/duplication state between two locations. */
10883
10884 static void
10885 swap_insertion (struct bp_location *left, struct bp_location *right)
10886 {
10887 const int left_inserted = left->inserted;
10888 const int left_duplicate = left->duplicate;
10889 const int left_needs_update = left->needs_update;
10890 const struct bp_target_info left_target_info = left->target_info;
10891
10892 /* Locations of tracepoints can never be duplicated. */
10893 if (is_tracepoint (left->owner))
10894 gdb_assert (!left->duplicate);
10895 if (is_tracepoint (right->owner))
10896 gdb_assert (!right->duplicate);
10897
10898 left->inserted = right->inserted;
10899 left->duplicate = right->duplicate;
10900 left->needs_update = right->needs_update;
10901 left->target_info = right->target_info;
10902 right->inserted = left_inserted;
10903 right->duplicate = left_duplicate;
10904 right->needs_update = left_needs_update;
10905 right->target_info = left_target_info;
10906 }
10907
10908 /* Force the re-insertion of the locations at ADDRESS. This is called
10909 once a new/deleted/modified duplicate location is found and we are evaluating
10910 conditions on the target's side. Such conditions need to be updated on
10911 the target. */
10912
10913 static void
10914 force_breakpoint_reinsertion (struct bp_location *bl)
10915 {
10916 CORE_ADDR address = 0;
10917 int pspace_num;
10918
10919 address = bl->address;
10920 pspace_num = bl->pspace->num;
10921
10922 /* This is only meaningful if the target is
10923 evaluating conditions and if the user has
10924 opted for condition evaluation on the target's
10925 side. */
10926 if (gdb_evaluates_breakpoint_condition_p ()
10927 || !target_supports_evaluation_of_breakpoint_conditions ())
10928 return;
10929
10930 /* Flag all breakpoint locations with this address and
10931 the same program space as the location
10932 as "its condition has changed". We need to
10933 update the conditions on the target's side. */
10934 for (bp_location *loc : all_bp_locations_at_addr (address))
10935 {
10936 if (!is_breakpoint (loc->owner)
10937 || pspace_num != loc->pspace->num)
10938 continue;
10939
10940 /* Flag the location appropriately. We use a different state to
10941 let everyone know that we already updated the set of locations
10942 with addr bl->address and program space bl->pspace. This is so
10943 we don't have to keep calling these functions just to mark locations
10944 that have already been marked. */
10945 loc->condition_changed = condition_updated;
10946
10947 /* Free the agent expression bytecode as well. We will compute
10948 it later on. */
10949 loc->cond_bytecode.reset ();
10950 }
10951 }
10952
10953 /* Called whether new breakpoints are created, or existing breakpoints
10954 deleted, to update the global location list and recompute which
10955 locations are duplicate of which.
10956
10957 The INSERT_MODE flag determines whether locations may not, may, or
10958 shall be inserted now. See 'enum ugll_insert_mode' for more
10959 info. */
10960
10961 static void
10962 update_global_location_list (enum ugll_insert_mode insert_mode)
10963 {
10964 /* Last breakpoint location address that was marked for update. */
10965 CORE_ADDR last_addr = 0;
10966 /* Last breakpoint location program space that was marked for update. */
10967 int last_pspace_num = -1;
10968
10969 /* Used in the duplicates detection below. When iterating over all
10970 bp_locations, points to the first bp_location of a given address.
10971 Breakpoints and watchpoints of different types are never
10972 duplicates of each other. Keep one pointer for each type of
10973 breakpoint/watchpoint, so we only need to loop over all locations
10974 once. */
10975 struct bp_location *bp_loc_first; /* breakpoint */
10976 struct bp_location *wp_loc_first; /* hardware watchpoint */
10977 struct bp_location *awp_loc_first; /* access watchpoint */
10978 struct bp_location *rwp_loc_first; /* read watchpoint */
10979
10980 /* Saved former bp_locations array which we compare against the newly
10981 built bp_locations from the current state of ALL_BREAKPOINTS. */
10982 std::vector<bp_location *> old_locations = std::move (bp_locations);
10983 bp_locations.clear ();
10984
10985 for (breakpoint *b : all_breakpoints ())
10986 for (bp_location *loc : b->locations ())
10987 bp_locations.push_back (loc);
10988
10989 /* See if we need to "upgrade" a software breakpoint to a hardware
10990 breakpoint. Do this before deciding whether locations are
10991 duplicates. Also do this before sorting because sorting order
10992 depends on location type. */
10993 for (bp_location *loc : bp_locations)
10994 if (!loc->inserted && should_be_inserted (loc))
10995 handle_automatic_hardware_breakpoints (loc);
10996
10997 std::sort (bp_locations.begin (), bp_locations.end (),
10998 bp_location_is_less_than);
10999
11000 bp_locations_target_extensions_update ();
11001
11002 /* Identify bp_location instances that are no longer present in the
11003 new list, and therefore should be freed. Note that it's not
11004 necessary that those locations should be removed from inferior --
11005 if there's another location at the same address (previously
11006 marked as duplicate), we don't need to remove/insert the
11007 location.
11008
11009 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11010 and former bp_location array state respectively. */
11011
11012 size_t loc_i = 0;
11013 for (bp_location *old_loc : old_locations)
11014 {
11015 /* Tells if 'old_loc' is found among the new locations. If
11016 not, we have to free it. */
11017 int found_object = 0;
11018 /* Tells if the location should remain inserted in the target. */
11019 int keep_in_target = 0;
11020 int removed = 0;
11021
11022 /* Skip LOCP entries which will definitely never be needed.
11023 Stop either at or being the one matching OLD_LOC. */
11024 while (loc_i < bp_locations.size ()
11025 && bp_locations[loc_i]->address < old_loc->address)
11026 loc_i++;
11027
11028 for (size_t loc2_i = loc_i;
11029 (loc2_i < bp_locations.size ()
11030 && bp_locations[loc2_i]->address == old_loc->address);
11031 loc2_i++)
11032 {
11033 /* Check if this is a new/duplicated location or a duplicated
11034 location that had its condition modified. If so, we want to send
11035 its condition to the target if evaluation of conditions is taking
11036 place there. */
11037 if (bp_locations[loc2_i]->condition_changed == condition_modified
11038 && (last_addr != old_loc->address
11039 || last_pspace_num != old_loc->pspace->num))
11040 {
11041 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11042 last_pspace_num = old_loc->pspace->num;
11043 }
11044
11045 if (bp_locations[loc2_i] == old_loc)
11046 found_object = 1;
11047 }
11048
11049 /* We have already handled this address, update it so that we don't
11050 have to go through updates again. */
11051 last_addr = old_loc->address;
11052
11053 /* Target-side condition evaluation: Handle deleted locations. */
11054 if (!found_object)
11055 force_breakpoint_reinsertion (old_loc);
11056
11057 /* If this location is no longer present, and inserted, look if
11058 there's maybe a new location at the same address. If so,
11059 mark that one inserted, and don't remove this one. This is
11060 needed so that we don't have a time window where a breakpoint
11061 at certain location is not inserted. */
11062
11063 if (old_loc->inserted)
11064 {
11065 /* If the location is inserted now, we might have to remove
11066 it. */
11067
11068 if (found_object && should_be_inserted (old_loc))
11069 {
11070 /* The location is still present in the location list,
11071 and still should be inserted. Don't do anything. */
11072 keep_in_target = 1;
11073 }
11074 else
11075 {
11076 /* This location still exists, but it won't be kept in the
11077 target since it may have been disabled. We proceed to
11078 remove its target-side condition. */
11079
11080 /* The location is either no longer present, or got
11081 disabled. See if there's another location at the
11082 same address, in which case we don't need to remove
11083 this one from the target. */
11084
11085 /* OLD_LOC comes from existing struct breakpoint. */
11086 if (bl_address_is_meaningful (old_loc))
11087 {
11088 for (size_t loc2_i = loc_i;
11089 (loc2_i < bp_locations.size ()
11090 && bp_locations[loc2_i]->address == old_loc->address);
11091 loc2_i++)
11092 {
11093 bp_location *loc2 = bp_locations[loc2_i];
11094
11095 if (loc2 == old_loc)
11096 continue;
11097
11098 if (breakpoint_locations_match (loc2, old_loc))
11099 {
11100 /* Read watchpoint locations are switched to
11101 access watchpoints, if the former are not
11102 supported, but the latter are. */
11103 if (is_hardware_watchpoint (old_loc->owner))
11104 {
11105 gdb_assert (is_hardware_watchpoint (loc2->owner));
11106 loc2->watchpoint_type = old_loc->watchpoint_type;
11107 }
11108
11109 /* loc2 is a duplicated location. We need to check
11110 if it should be inserted in case it will be
11111 unduplicated. */
11112 if (unduplicated_should_be_inserted (loc2))
11113 {
11114 swap_insertion (old_loc, loc2);
11115 keep_in_target = 1;
11116 break;
11117 }
11118 }
11119 }
11120 }
11121 }
11122
11123 if (!keep_in_target)
11124 {
11125 if (remove_breakpoint (old_loc))
11126 {
11127 /* This is just about all we can do. We could keep
11128 this location on the global list, and try to
11129 remove it next time, but there's no particular
11130 reason why we will succeed next time.
11131
11132 Note that at this point, old_loc->owner is still
11133 valid, as delete_breakpoint frees the breakpoint
11134 only after calling us. */
11135 gdb_printf (_("warning: Error removing "
11136 "breakpoint %d\n"),
11137 old_loc->owner->number);
11138 }
11139 removed = 1;
11140 }
11141 }
11142
11143 if (!found_object)
11144 {
11145 if (removed && target_is_non_stop_p ()
11146 && need_moribund_for_location_type (old_loc))
11147 {
11148 /* This location was removed from the target. In
11149 non-stop mode, a race condition is possible where
11150 we've removed a breakpoint, but stop events for that
11151 breakpoint are already queued and will arrive later.
11152 We apply an heuristic to be able to distinguish such
11153 SIGTRAPs from other random SIGTRAPs: we keep this
11154 breakpoint location for a bit, and will retire it
11155 after we see some number of events. The theory here
11156 is that reporting of events should, "on the average",
11157 be fair, so after a while we'll see events from all
11158 threads that have anything of interest, and no longer
11159 need to keep this breakpoint location around. We
11160 don't hold locations forever so to reduce chances of
11161 mistaking a non-breakpoint SIGTRAP for a breakpoint
11162 SIGTRAP.
11163
11164 The heuristic failing can be disastrous on
11165 decr_pc_after_break targets.
11166
11167 On decr_pc_after_break targets, like e.g., x86-linux,
11168 if we fail to recognize a late breakpoint SIGTRAP,
11169 because events_till_retirement has reached 0 too
11170 soon, we'll fail to do the PC adjustment, and report
11171 a random SIGTRAP to the user. When the user resumes
11172 the inferior, it will most likely immediately crash
11173 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11174 corrupted, because of being resumed e.g., in the
11175 middle of a multi-byte instruction, or skipped a
11176 one-byte instruction. This was actually seen happen
11177 on native x86-linux, and should be less rare on
11178 targets that do not support new thread events, like
11179 remote, due to the heuristic depending on
11180 thread_count.
11181
11182 Mistaking a random SIGTRAP for a breakpoint trap
11183 causes similar symptoms (PC adjustment applied when
11184 it shouldn't), but then again, playing with SIGTRAPs
11185 behind the debugger's back is asking for trouble.
11186
11187 Since hardware watchpoint traps are always
11188 distinguishable from other traps, so we don't need to
11189 apply keep hardware watchpoint moribund locations
11190 around. We simply always ignore hardware watchpoint
11191 traps we can no longer explain. */
11192
11193 process_stratum_target *proc_target = nullptr;
11194 for (inferior *inf : all_inferiors ())
11195 if (inf->pspace == old_loc->pspace)
11196 {
11197 proc_target = inf->process_target ();
11198 break;
11199 }
11200 if (proc_target != nullptr)
11201 old_loc->events_till_retirement
11202 = 3 * (thread_count (proc_target) + 1);
11203 else
11204 old_loc->events_till_retirement = 1;
11205 old_loc->owner = NULL;
11206
11207 moribund_locations.push_back (old_loc);
11208 }
11209 else
11210 {
11211 old_loc->owner = NULL;
11212 decref_bp_location (&old_loc);
11213 }
11214 }
11215 }
11216
11217 /* Rescan breakpoints at the same address and section, marking the
11218 first one as "first" and any others as "duplicates". This is so
11219 that the bpt instruction is only inserted once. If we have a
11220 permanent breakpoint at the same place as BPT, make that one the
11221 official one, and the rest as duplicates. Permanent breakpoints
11222 are sorted first for the same address.
11223
11224 Do the same for hardware watchpoints, but also considering the
11225 watchpoint's type (regular/access/read) and length. */
11226
11227 bp_loc_first = NULL;
11228 wp_loc_first = NULL;
11229 awp_loc_first = NULL;
11230 rwp_loc_first = NULL;
11231
11232 for (bp_location *loc : all_bp_locations ())
11233 {
11234 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11235 non-NULL. */
11236 struct bp_location **loc_first_p;
11237 breakpoint *b = loc->owner;
11238
11239 if (!unduplicated_should_be_inserted (loc)
11240 || !bl_address_is_meaningful (loc)
11241 /* Don't detect duplicate for tracepoint locations because they are
11242 never duplicated. See the comments in field `duplicate' of
11243 `struct bp_location'. */
11244 || is_tracepoint (b))
11245 {
11246 /* Clear the condition modification flag. */
11247 loc->condition_changed = condition_unchanged;
11248 continue;
11249 }
11250
11251 if (b->type == bp_hardware_watchpoint)
11252 loc_first_p = &wp_loc_first;
11253 else if (b->type == bp_read_watchpoint)
11254 loc_first_p = &rwp_loc_first;
11255 else if (b->type == bp_access_watchpoint)
11256 loc_first_p = &awp_loc_first;
11257 else
11258 loc_first_p = &bp_loc_first;
11259
11260 if (*loc_first_p == NULL
11261 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11262 || !breakpoint_locations_match (loc, *loc_first_p))
11263 {
11264 *loc_first_p = loc;
11265 loc->duplicate = 0;
11266
11267 if (is_breakpoint (loc->owner) && loc->condition_changed)
11268 {
11269 loc->needs_update = 1;
11270 /* Clear the condition modification flag. */
11271 loc->condition_changed = condition_unchanged;
11272 }
11273 continue;
11274 }
11275
11276
11277 /* This and the above ensure the invariant that the first location
11278 is not duplicated, and is the inserted one.
11279 All following are marked as duplicated, and are not inserted. */
11280 if (loc->inserted)
11281 swap_insertion (loc, *loc_first_p);
11282 loc->duplicate = 1;
11283
11284 /* Clear the condition modification flag. */
11285 loc->condition_changed = condition_unchanged;
11286 }
11287
11288 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11289 {
11290 if (insert_mode != UGLL_DONT_INSERT)
11291 insert_breakpoint_locations ();
11292 else
11293 {
11294 /* Even though the caller told us to not insert new
11295 locations, we may still need to update conditions on the
11296 target's side of breakpoints that were already inserted
11297 if the target is evaluating breakpoint conditions. We
11298 only update conditions for locations that are marked
11299 "needs_update". */
11300 update_inserted_breakpoint_locations ();
11301 }
11302 }
11303
11304 if (insert_mode != UGLL_DONT_INSERT)
11305 download_tracepoint_locations ();
11306 }
11307
11308 void
11309 breakpoint_retire_moribund (void)
11310 {
11311 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11312 {
11313 struct bp_location *loc = moribund_locations[ix];
11314 if (--(loc->events_till_retirement) == 0)
11315 {
11316 decref_bp_location (&loc);
11317 unordered_remove (moribund_locations, ix);
11318 --ix;
11319 }
11320 }
11321 }
11322
11323 static void
11324 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11325 {
11326
11327 try
11328 {
11329 update_global_location_list (insert_mode);
11330 }
11331 catch (const gdb_exception_error &e)
11332 {
11333 }
11334 }
11335
11336 /* Clear BKP from a BPS. */
11337
11338 static void
11339 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
11340 {
11341 bpstat *bs;
11342
11343 for (bs = bps; bs; bs = bs->next)
11344 if (bs->breakpoint_at == bpt)
11345 {
11346 bs->breakpoint_at = NULL;
11347 bs->old_val = NULL;
11348 /* bs->commands will be freed later. */
11349 }
11350 }
11351
11352 /* Callback for iterate_over_threads. */
11353 static int
11354 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11355 {
11356 struct breakpoint *bpt = (struct breakpoint *) data;
11357
11358 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11359 return 0;
11360 }
11361
11362 /* Helper for breakpoint and tracepoint breakpoint->mention
11363 callbacks. */
11364
11365 static void
11366 say_where (const breakpoint *b)
11367 {
11368 struct value_print_options opts;
11369
11370 get_user_print_options (&opts);
11371
11372 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11373 single string. */
11374 if (b->loc == NULL)
11375 {
11376 /* For pending locations, the output differs slightly based
11377 on b->extra_string. If this is non-NULL, it contains either
11378 a condition or dprintf arguments. */
11379 if (b->extra_string == NULL)
11380 {
11381 gdb_printf (_(" (%s) pending."), b->locspec->to_string ());
11382 }
11383 else if (b->type == bp_dprintf)
11384 {
11385 gdb_printf (_(" (%s,%s) pending."),
11386 b->locspec->to_string (),
11387 b->extra_string.get ());
11388 }
11389 else
11390 {
11391 gdb_printf (_(" (%s %s) pending."),
11392 b->locspec->to_string (),
11393 b->extra_string.get ());
11394 }
11395 }
11396 else
11397 {
11398 if (opts.addressprint || b->loc->symtab == NULL)
11399 gdb_printf (" at %ps",
11400 styled_string (address_style.style (),
11401 paddress (b->loc->gdbarch,
11402 b->loc->address)));
11403 if (b->loc->symtab != NULL)
11404 {
11405 /* If there is a single location, we can print the location
11406 more nicely. */
11407 if (b->loc->next == NULL)
11408 {
11409 const char *filename
11410 = symtab_to_filename_for_display (b->loc->symtab);
11411 gdb_printf (": file %ps, line %d.",
11412 styled_string (file_name_style.style (),
11413 filename),
11414 b->loc->line_number);
11415 }
11416 else
11417 /* This is not ideal, but each location may have a
11418 different file name, and this at least reflects the
11419 real situation somewhat. */
11420 gdb_printf (": %s.", b->locspec->to_string ());
11421 }
11422
11423 if (b->loc->next)
11424 {
11425 struct bp_location *loc = b->loc;
11426 int n = 0;
11427 for (; loc; loc = loc->next)
11428 ++n;
11429 gdb_printf (" (%d locations)", n);
11430 }
11431 }
11432 }
11433
11434 /* See breakpoint.h. */
11435
11436 bp_location_range breakpoint::locations () const
11437 {
11438 return bp_location_range (this->loc);
11439 }
11440
11441 struct bp_location *
11442 breakpoint::allocate_location ()
11443 {
11444 return new bp_location (this);
11445 }
11446
11447 #define internal_error_pure_virtual_called() \
11448 gdb_assert_not_reached ("pure virtual function called")
11449
11450 int
11451 breakpoint::insert_location (struct bp_location *bl)
11452 {
11453 internal_error_pure_virtual_called ();
11454 }
11455
11456 int
11457 breakpoint::remove_location (struct bp_location *bl,
11458 enum remove_bp_reason reason)
11459 {
11460 internal_error_pure_virtual_called ();
11461 }
11462
11463 int
11464 breakpoint::breakpoint_hit (const struct bp_location *bl,
11465 const address_space *aspace,
11466 CORE_ADDR bp_addr,
11467 const target_waitstatus &ws)
11468 {
11469 internal_error_pure_virtual_called ();
11470 }
11471
11472 int
11473 breakpoint::resources_needed (const struct bp_location *bl)
11474 {
11475 internal_error_pure_virtual_called ();
11476 }
11477
11478 enum print_stop_action
11479 breakpoint::print_it (const bpstat *bs) const
11480 {
11481 internal_error_pure_virtual_called ();
11482 }
11483
11484 void
11485 breakpoint::print_mention () const
11486 {
11487 internal_error_pure_virtual_called ();
11488 }
11489
11490 void
11491 breakpoint::print_recreate (struct ui_file *fp) const
11492 {
11493 internal_error_pure_virtual_called ();
11494 }
11495
11496 /* Default breakpoint_ops methods. */
11497
11498 void
11499 code_breakpoint::re_set ()
11500 {
11501 /* FIXME: is this still reachable? */
11502 if (breakpoint_location_spec_empty_p (this))
11503 {
11504 /* Anything without a location can't be re-set. */
11505 delete_breakpoint (this);
11506 return;
11507 }
11508
11509 re_set_default ();
11510 }
11511
11512 int
11513 code_breakpoint::insert_location (struct bp_location *bl)
11514 {
11515 CORE_ADDR addr = bl->target_info.reqstd_address;
11516
11517 bl->target_info.kind = breakpoint_kind (bl, &addr);
11518 bl->target_info.placed_address = addr;
11519
11520 int result;
11521 if (bl->loc_type == bp_loc_hardware_breakpoint)
11522 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
11523 else
11524 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11525
11526 if (result == 0 && bl->probe.prob != nullptr)
11527 {
11528 /* The insertion was successful, now let's set the probe's semaphore
11529 if needed. */
11530 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11531 }
11532
11533 return result;
11534 }
11535
11536 int
11537 code_breakpoint::remove_location (struct bp_location *bl,
11538 enum remove_bp_reason reason)
11539 {
11540 if (bl->probe.prob != nullptr)
11541 {
11542 /* Let's clear the semaphore before removing the location. */
11543 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11544 }
11545
11546 if (bl->loc_type == bp_loc_hardware_breakpoint)
11547 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11548 else
11549 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
11550 }
11551
11552 int
11553 code_breakpoint::breakpoint_hit (const struct bp_location *bl,
11554 const address_space *aspace,
11555 CORE_ADDR bp_addr,
11556 const target_waitstatus &ws)
11557 {
11558 if (ws.kind () != TARGET_WAITKIND_STOPPED
11559 || ws.sig () != GDB_SIGNAL_TRAP)
11560 return 0;
11561
11562 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11563 aspace, bp_addr))
11564 return 0;
11565
11566 if (overlay_debugging /* unmapped overlay section */
11567 && section_is_overlay (bl->section)
11568 && !section_is_mapped (bl->section))
11569 return 0;
11570
11571 return 1;
11572 }
11573
11574 int
11575 dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
11576 const address_space *aspace,
11577 CORE_ADDR bp_addr,
11578 const target_waitstatus &ws)
11579 {
11580 if (dprintf_style == dprintf_style_agent
11581 && target_can_run_breakpoint_commands ())
11582 {
11583 /* An agent-style dprintf never causes a stop. If we see a trap
11584 for this address it must be for a breakpoint that happens to
11585 be set at the same address. */
11586 return 0;
11587 }
11588
11589 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
11590 }
11591
11592 int
11593 ordinary_breakpoint::resources_needed (const struct bp_location *bl)
11594 {
11595 gdb_assert (type == bp_hardware_breakpoint);
11596
11597 return 1;
11598 }
11599
11600 enum print_stop_action
11601 ordinary_breakpoint::print_it (const bpstat *bs) const
11602 {
11603 const struct bp_location *bl;
11604 int bp_temp;
11605 struct ui_out *uiout = current_uiout;
11606
11607 bl = bs->bp_location_at.get ();
11608
11609 bp_temp = disposition == disp_del;
11610 if (bl->address != bl->requested_address)
11611 breakpoint_adjustment_warning (bl->requested_address,
11612 bl->address,
11613 number, 1);
11614 annotate_breakpoint (number);
11615 maybe_print_thread_hit_breakpoint (uiout);
11616
11617 if (uiout->is_mi_like_p ())
11618 {
11619 uiout->field_string ("reason",
11620 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11621 uiout->field_string ("disp", bpdisp_text (disposition));
11622 }
11623 if (bp_temp)
11624 uiout->message ("Temporary breakpoint %pF, ",
11625 signed_field ("bkptno", number));
11626 else
11627 uiout->message ("Breakpoint %pF, ",
11628 signed_field ("bkptno", number));
11629
11630 return PRINT_SRC_AND_LOC;
11631 }
11632
11633 void
11634 ordinary_breakpoint::print_mention () const
11635 {
11636 if (current_uiout->is_mi_like_p ())
11637 return;
11638
11639 switch (type)
11640 {
11641 case bp_breakpoint:
11642 case bp_gnu_ifunc_resolver:
11643 if (disposition == disp_del)
11644 gdb_printf (_("Temporary breakpoint"));
11645 else
11646 gdb_printf (_("Breakpoint"));
11647 gdb_printf (_(" %d"), number);
11648 if (type == bp_gnu_ifunc_resolver)
11649 gdb_printf (_(" at gnu-indirect-function resolver"));
11650 break;
11651 case bp_hardware_breakpoint:
11652 gdb_printf (_("Hardware assisted breakpoint %d"), number);
11653 break;
11654 case bp_dprintf:
11655 gdb_printf (_("Dprintf %d"), number);
11656 break;
11657 }
11658
11659 say_where (this);
11660 }
11661
11662 void
11663 ordinary_breakpoint::print_recreate (struct ui_file *fp) const
11664 {
11665 if (type == bp_breakpoint && disposition == disp_del)
11666 gdb_printf (fp, "tbreak");
11667 else if (type == bp_breakpoint)
11668 gdb_printf (fp, "break");
11669 else if (type == bp_hardware_breakpoint
11670 && disposition == disp_del)
11671 gdb_printf (fp, "thbreak");
11672 else if (type == bp_hardware_breakpoint)
11673 gdb_printf (fp, "hbreak");
11674 else
11675 internal_error (__FILE__, __LINE__,
11676 _("unhandled breakpoint type %d"), (int) type);
11677
11678 gdb_printf (fp, " %s", locspec->to_string ());
11679
11680 /* Print out extra_string if this breakpoint is pending. It might
11681 contain, for example, conditions that were set by the user. */
11682 if (loc == NULL && extra_string != NULL)
11683 gdb_printf (fp, " %s", extra_string.get ());
11684
11685 print_recreate_thread (fp);
11686 }
11687
11688 std::vector<symtab_and_line>
11689 code_breakpoint::decode_location_spec (location_spec *locspec,
11690 program_space *search_pspace)
11691 {
11692 if (locspec->type () == PROBE_LOCATION_SPEC)
11693 return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
11694
11695 return decode_location_spec_default (this, locspec, search_pspace);
11696 }
11697
11698 /* Virtual table for internal breakpoints. */
11699
11700 void
11701 internal_breakpoint::re_set ()
11702 {
11703 switch (type)
11704 {
11705 /* Delete overlay event and longjmp master breakpoints; they
11706 will be reset later by breakpoint_re_set. */
11707 case bp_overlay_event:
11708 case bp_longjmp_master:
11709 case bp_std_terminate_master:
11710 case bp_exception_master:
11711 delete_breakpoint (this);
11712 break;
11713
11714 /* This breakpoint is special, it's set up when the inferior
11715 starts and we really don't want to touch it. */
11716 case bp_shlib_event:
11717
11718 /* Like bp_shlib_event, this breakpoint type is special. Once
11719 it is set up, we do not want to touch it. */
11720 case bp_thread_event:
11721 break;
11722 }
11723 }
11724
11725 void
11726 internal_breakpoint::check_status (bpstat *bs)
11727 {
11728 if (type == bp_shlib_event)
11729 {
11730 /* If requested, stop when the dynamic linker notifies GDB of
11731 events. This allows the user to get control and place
11732 breakpoints in initializer routines for dynamically loaded
11733 objects (among other things). */
11734 bs->stop = stop_on_solib_events;
11735 bs->print = stop_on_solib_events;
11736 }
11737 else
11738 bs->stop = 0;
11739 }
11740
11741 enum print_stop_action
11742 internal_breakpoint::print_it (const bpstat *bs) const
11743 {
11744 switch (type)
11745 {
11746 case bp_shlib_event:
11747 /* Did we stop because the user set the stop_on_solib_events
11748 variable? (If so, we report this as a generic, "Stopped due
11749 to shlib event" message.) */
11750 print_solib_event (false);
11751 break;
11752
11753 case bp_thread_event:
11754 /* Not sure how we will get here.
11755 GDB should not stop for these breakpoints. */
11756 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
11757 break;
11758
11759 case bp_overlay_event:
11760 /* By analogy with the thread event, GDB should not stop for these. */
11761 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
11762 break;
11763
11764 case bp_longjmp_master:
11765 /* These should never be enabled. */
11766 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
11767 break;
11768
11769 case bp_std_terminate_master:
11770 /* These should never be enabled. */
11771 gdb_printf (_("std::terminate Master Breakpoint: "
11772 "gdb should not stop!\n"));
11773 break;
11774
11775 case bp_exception_master:
11776 /* These should never be enabled. */
11777 gdb_printf (_("Exception Master Breakpoint: "
11778 "gdb should not stop!\n"));
11779 break;
11780 }
11781
11782 return PRINT_NOTHING;
11783 }
11784
11785 void
11786 internal_breakpoint::print_mention () const
11787 {
11788 /* Nothing to mention. These breakpoints are internal. */
11789 }
11790
11791 /* Virtual table for momentary breakpoints */
11792
11793 void
11794 momentary_breakpoint::re_set ()
11795 {
11796 /* Keep temporary breakpoints, which can be encountered when we step
11797 over a dlopen call and solib_add is resetting the breakpoints.
11798 Otherwise these should have been blown away via the cleanup chain
11799 or by breakpoint_init_inferior when we rerun the executable. */
11800 }
11801
11802 void
11803 momentary_breakpoint::check_status (bpstat *bs)
11804 {
11805 /* Nothing. The point of these breakpoints is causing a stop. */
11806 }
11807
11808 enum print_stop_action
11809 momentary_breakpoint::print_it (const bpstat *bs) const
11810 {
11811 return PRINT_UNKNOWN;
11812 }
11813
11814 void
11815 momentary_breakpoint::print_mention () const
11816 {
11817 /* Nothing to mention. These breakpoints are internal. */
11818 }
11819
11820 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
11821
11822 It gets cleared already on the removal of the first one of such placed
11823 breakpoints. This is OK as they get all removed altogether. */
11824
11825 longjmp_breakpoint::~longjmp_breakpoint ()
11826 {
11827 thread_info *tp = find_thread_global_id (this->thread);
11828
11829 if (tp != NULL)
11830 tp->initiating_frame = null_frame_id;
11831 }
11832
11833 static void
11834 bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
11835 struct linespec_result *canonical)
11836
11837 {
11838 struct linespec_sals lsal;
11839
11840 lsal.sals = parse_probes (locspec, NULL, canonical);
11841 lsal.canonical = xstrdup (canonical->locspec->to_string ());
11842 canonical->lsals.push_back (std::move (lsal));
11843 }
11844
11845 static std::vector<symtab_and_line>
11846 bkpt_probe_decode_location_spec (struct breakpoint *b,
11847 location_spec *locspec,
11848 program_space *search_pspace)
11849 {
11850 std::vector<symtab_and_line> sals
11851 = parse_probes (locspec, search_pspace, NULL);
11852 if (sals.empty ())
11853 error (_("probe not found"));
11854 return sals;
11855 }
11856
11857 int
11858 tracepoint::breakpoint_hit (const struct bp_location *bl,
11859 const address_space *aspace, CORE_ADDR bp_addr,
11860 const target_waitstatus &ws)
11861 {
11862 /* By definition, the inferior does not report stops at
11863 tracepoints. */
11864 return 0;
11865 }
11866
11867 void
11868 tracepoint::print_one_detail (struct ui_out *uiout) const
11869 {
11870 if (!static_trace_marker_id.empty ())
11871 {
11872 gdb_assert (type == bp_static_tracepoint
11873 || type == bp_static_marker_tracepoint);
11874
11875 uiout->message ("\tmarker id is %pF\n",
11876 string_field ("static-tracepoint-marker-string-id",
11877 static_trace_marker_id.c_str ()));
11878 }
11879 }
11880
11881 void
11882 tracepoint::print_mention () const
11883 {
11884 if (current_uiout->is_mi_like_p ())
11885 return;
11886
11887 switch (type)
11888 {
11889 case bp_tracepoint:
11890 gdb_printf (_("Tracepoint"));
11891 gdb_printf (_(" %d"), number);
11892 break;
11893 case bp_fast_tracepoint:
11894 gdb_printf (_("Fast tracepoint"));
11895 gdb_printf (_(" %d"), number);
11896 break;
11897 case bp_static_tracepoint:
11898 case bp_static_marker_tracepoint:
11899 gdb_printf (_("Static tracepoint"));
11900 gdb_printf (_(" %d"), number);
11901 break;
11902 default:
11903 internal_error (__FILE__, __LINE__,
11904 _("unhandled tracepoint type %d"), (int) type);
11905 }
11906
11907 say_where (this);
11908 }
11909
11910 void
11911 tracepoint::print_recreate (struct ui_file *fp) const
11912 {
11913 if (type == bp_fast_tracepoint)
11914 gdb_printf (fp, "ftrace");
11915 else if (type == bp_static_tracepoint
11916 || type == bp_static_marker_tracepoint)
11917 gdb_printf (fp, "strace");
11918 else if (type == bp_tracepoint)
11919 gdb_printf (fp, "trace");
11920 else
11921 internal_error (__FILE__, __LINE__,
11922 _("unhandled tracepoint type %d"), (int) type);
11923
11924 gdb_printf (fp, " %s", locspec->to_string ());
11925 print_recreate_thread (fp);
11926
11927 if (pass_count)
11928 gdb_printf (fp, " passcount %d\n", pass_count);
11929 }
11930
11931 /* Virtual table for tracepoints on static probes. */
11932
11933 static void
11934 tracepoint_probe_create_sals_from_location_spec
11935 (location_spec *locspec,
11936 struct linespec_result *canonical)
11937 {
11938 /* We use the same method for breakpoint on probes. */
11939 bkpt_probe_create_sals_from_location_spec (locspec, canonical);
11940 }
11941
11942 void
11943 dprintf_breakpoint::re_set ()
11944 {
11945 re_set_default ();
11946
11947 /* extra_string should never be non-NULL for dprintf. */
11948 gdb_assert (extra_string != NULL);
11949
11950 /* 1 - connect to target 1, that can run breakpoint commands.
11951 2 - create a dprintf, which resolves fine.
11952 3 - disconnect from target 1
11953 4 - connect to target 2, that can NOT run breakpoint commands.
11954
11955 After steps #3/#4, you'll want the dprintf command list to
11956 be updated, because target 1 and 2 may well return different
11957 answers for target_can_run_breakpoint_commands().
11958 Given absence of finer grained resetting, we get to do
11959 it all the time. */
11960 if (extra_string != NULL)
11961 update_dprintf_command_list (this);
11962 }
11963
11964 /* Implement the "print_recreate" method for dprintf. */
11965
11966 void
11967 dprintf_breakpoint::print_recreate (struct ui_file *fp) const
11968 {
11969 gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
11970 print_recreate_thread (fp);
11971 }
11972
11973 /* Implement the "after_condition_true" method for dprintf.
11974
11975 dprintf's are implemented with regular commands in their command
11976 list, but we run the commands here instead of before presenting the
11977 stop to the user, as dprintf's don't actually cause a stop. This
11978 also makes it so that the commands of multiple dprintfs at the same
11979 address are all handled. */
11980
11981 void
11982 dprintf_breakpoint::after_condition_true (struct bpstat *bs)
11983 {
11984 /* dprintf's never cause a stop. This wasn't set in the
11985 check_status hook instead because that would make the dprintf's
11986 condition not be evaluated. */
11987 bs->stop = 0;
11988
11989 /* Run the command list here. Take ownership of it instead of
11990 copying. We never want these commands to run later in
11991 bpstat_do_actions, if a breakpoint that causes a stop happens to
11992 be set at same address as this dprintf, or even if running the
11993 commands here throws. */
11994 counted_command_line cmds = std::move (bs->commands);
11995 gdb_assert (cmds != nullptr);
11996 execute_control_commands (cmds.get (), 0);
11997 }
11998
11999 /* The breakpoint_ops structure to be used on static tracepoints with
12000 markers (`-m'). */
12001
12002 static void
12003 strace_marker_create_sals_from_location_spec (location_spec *locspec,
12004 struct linespec_result *canonical)
12005 {
12006 struct linespec_sals lsal;
12007 const char *arg_start, *arg;
12008
12009 arg = arg_start = as_linespec_location_spec (locspec)->spec_string;
12010 lsal.sals = decode_static_tracepoint_spec (&arg);
12011
12012 std::string str (arg_start, arg - arg_start);
12013 const char *ptr = str.c_str ();
12014 canonical->locspec
12015 = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
12016
12017 lsal.canonical = xstrdup (canonical->locspec->to_string ());
12018 canonical->lsals.push_back (std::move (lsal));
12019 }
12020
12021 static void
12022 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12023 struct linespec_result *canonical,
12024 gdb::unique_xmalloc_ptr<char> cond_string,
12025 gdb::unique_xmalloc_ptr<char> extra_string,
12026 enum bptype type_wanted,
12027 enum bpdisp disposition,
12028 int thread,
12029 int task, int ignore_count,
12030 int from_tty, int enabled,
12031 int internal, unsigned flags)
12032 {
12033 const linespec_sals &lsal = canonical->lsals[0];
12034
12035 /* If the user is creating a static tracepoint by marker id
12036 (strace -m MARKER_ID), then store the sals index, so that
12037 breakpoint_re_set can try to match up which of the newly
12038 found markers corresponds to this one, and, don't try to
12039 expand multiple locations for each sal, given than SALS
12040 already should contain all sals for MARKER_ID. */
12041
12042 for (size_t i = 0; i < lsal.sals.size (); i++)
12043 {
12044 location_spec_up locspec = canonical->locspec->clone ();
12045
12046 std::unique_ptr<tracepoint> tp
12047 (new tracepoint (gdbarch,
12048 type_wanted,
12049 lsal.sals[i],
12050 std::move (locspec),
12051 NULL,
12052 std::move (cond_string),
12053 std::move (extra_string),
12054 disposition,
12055 thread, task, ignore_count,
12056 from_tty, enabled, flags,
12057 canonical->special_display));
12058
12059 /* Given that its possible to have multiple markers with
12060 the same string id, if the user is creating a static
12061 tracepoint by marker id ("strace -m MARKER_ID"), then
12062 store the sals index, so that breakpoint_re_set can
12063 try to match up which of the newly found markers
12064 corresponds to this one */
12065 tp->static_trace_marker_id_idx = i;
12066
12067 install_breakpoint (internal, std::move (tp), 0);
12068 }
12069 }
12070
12071 std::vector<symtab_and_line>
12072 static_marker_tracepoint::decode_location_spec (location_spec *locspec,
12073 program_space *search_pspace)
12074 {
12075 const char *s = as_linespec_location_spec (locspec)->spec_string;
12076
12077 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12078 if (sals.size () > static_trace_marker_id_idx)
12079 {
12080 sals[0] = sals[static_trace_marker_id_idx];
12081 sals.resize (1);
12082 return sals;
12083 }
12084 else
12085 error (_("marker %s not found"), static_trace_marker_id.c_str ());
12086 }
12087
12088 /* Static tracepoints with marker (`-m'). */
12089 static struct breakpoint_ops strace_marker_breakpoint_ops =
12090 {
12091 strace_marker_create_sals_from_location_spec,
12092 strace_marker_create_breakpoints_sal,
12093 };
12094
12095 static int
12096 strace_marker_p (struct breakpoint *b)
12097 {
12098 return b->type == bp_static_marker_tracepoint;
12099 }
12100
12101 /* Delete a breakpoint and clean up all traces of it in the data
12102 structures. */
12103
12104 void
12105 delete_breakpoint (struct breakpoint *bpt)
12106 {
12107 gdb_assert (bpt != NULL);
12108
12109 /* Has this bp already been deleted? This can happen because
12110 multiple lists can hold pointers to bp's. bpstat lists are
12111 especial culprits.
12112
12113 One example of this happening is a watchpoint's scope bp. When
12114 the scope bp triggers, we notice that the watchpoint is out of
12115 scope, and delete it. We also delete its scope bp. But the
12116 scope bp is marked "auto-deleting", and is already on a bpstat.
12117 That bpstat is then checked for auto-deleting bp's, which are
12118 deleted.
12119
12120 A real solution to this problem might involve reference counts in
12121 bp's, and/or giving them pointers back to their referencing
12122 bpstat's, and teaching delete_breakpoint to only free a bp's
12123 storage when no more references were extent. A cheaper bandaid
12124 was chosen. */
12125 if (bpt->type == bp_none)
12126 return;
12127
12128 /* At least avoid this stale reference until the reference counting
12129 of breakpoints gets resolved. */
12130 if (bpt->related_breakpoint != bpt)
12131 {
12132 struct breakpoint *related;
12133 struct watchpoint *w;
12134
12135 if (bpt->type == bp_watchpoint_scope)
12136 w = (struct watchpoint *) bpt->related_breakpoint;
12137 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12138 w = (struct watchpoint *) bpt;
12139 else
12140 w = NULL;
12141 if (w != NULL)
12142 watchpoint_del_at_next_stop (w);
12143
12144 /* Unlink bpt from the bpt->related_breakpoint ring. */
12145 for (related = bpt; related->related_breakpoint != bpt;
12146 related = related->related_breakpoint);
12147 related->related_breakpoint = bpt->related_breakpoint;
12148 bpt->related_breakpoint = bpt;
12149 }
12150
12151 /* watch_command_1 creates a watchpoint but only sets its number if
12152 update_watchpoint succeeds in creating its bp_locations. If there's
12153 a problem in that process, we'll be asked to delete the half-created
12154 watchpoint. In that case, don't announce the deletion. */
12155 if (bpt->number)
12156 gdb::observers::breakpoint_deleted.notify (bpt);
12157
12158 if (breakpoint_chain == bpt)
12159 breakpoint_chain = bpt->next;
12160
12161 for (breakpoint *b : all_breakpoints ())
12162 if (b->next == bpt)
12163 {
12164 b->next = bpt->next;
12165 break;
12166 }
12167
12168 /* Be sure no bpstat's are pointing at the breakpoint after it's
12169 been freed. */
12170 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12171 in all threads for now. Note that we cannot just remove bpstats
12172 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12173 commands are associated with the bpstat; if we remove it here,
12174 then the later call to bpstat_do_actions (&stop_bpstat); in
12175 event-top.c won't do anything, and temporary breakpoints with
12176 commands won't work. */
12177
12178 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12179
12180 /* Now that breakpoint is removed from breakpoint list, update the
12181 global location list. This will remove locations that used to
12182 belong to this breakpoint. Do this before freeing the breakpoint
12183 itself, since remove_breakpoint looks at location's owner. It
12184 might be better design to have location completely
12185 self-contained, but it's not the case now. */
12186 update_global_location_list (UGLL_DONT_INSERT);
12187
12188 /* On the chance that someone will soon try again to delete this
12189 same bp, we mark it as deleted before freeing its storage. */
12190 bpt->type = bp_none;
12191 delete bpt;
12192 }
12193
12194 /* Iterator function to call a user-provided callback function once
12195 for each of B and its related breakpoints. */
12196
12197 static void
12198 iterate_over_related_breakpoints (struct breakpoint *b,
12199 gdb::function_view<void (breakpoint *)> function)
12200 {
12201 struct breakpoint *related;
12202
12203 related = b;
12204 do
12205 {
12206 struct breakpoint *next;
12207
12208 /* FUNCTION may delete RELATED. */
12209 next = related->related_breakpoint;
12210
12211 if (next == related)
12212 {
12213 /* RELATED is the last ring entry. */
12214 function (related);
12215
12216 /* FUNCTION may have deleted it, so we'd never reach back to
12217 B. There's nothing left to do anyway, so just break
12218 out. */
12219 break;
12220 }
12221 else
12222 function (related);
12223
12224 related = next;
12225 }
12226 while (related != b);
12227 }
12228
12229 static void
12230 delete_command (const char *arg, int from_tty)
12231 {
12232 dont_repeat ();
12233
12234 if (arg == 0)
12235 {
12236 int breaks_to_delete = 0;
12237
12238 /* Delete all breakpoints if no argument. Do not delete
12239 internal breakpoints, these have to be deleted with an
12240 explicit breakpoint number argument. */
12241 for (breakpoint *b : all_breakpoints ())
12242 if (user_breakpoint_p (b))
12243 {
12244 breaks_to_delete = 1;
12245 break;
12246 }
12247
12248 /* Ask user only if there are some breakpoints to delete. */
12249 if (!from_tty
12250 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
12251 for (breakpoint *b : all_breakpoints_safe ())
12252 if (user_breakpoint_p (b))
12253 delete_breakpoint (b);
12254 }
12255 else
12256 map_breakpoint_numbers
12257 (arg, [&] (breakpoint *br)
12258 {
12259 iterate_over_related_breakpoints (br, delete_breakpoint);
12260 });
12261 }
12262
12263 /* Return true if all locations of B bound to PSPACE are pending. If
12264 PSPACE is NULL, all locations of all program spaces are
12265 considered. */
12266
12267 static int
12268 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
12269 {
12270 for (bp_location *loc : b->locations ())
12271 if ((pspace == NULL
12272 || loc->pspace == pspace)
12273 && !loc->shlib_disabled
12274 && !loc->pspace->executing_startup)
12275 return 0;
12276 return 1;
12277 }
12278
12279 /* Subroutine of update_breakpoint_locations to simplify it.
12280 Return non-zero if multiple fns in list LOC have the same name.
12281 Null names are ignored. */
12282
12283 static int
12284 ambiguous_names_p (struct bp_location *loc)
12285 {
12286 struct bp_location *l;
12287 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
12288 xcalloc, xfree));
12289
12290 for (l = loc; l != NULL; l = l->next)
12291 {
12292 const char **slot;
12293 const char *name = l->function_name.get ();
12294
12295 /* Allow for some names to be NULL, ignore them. */
12296 if (name == NULL)
12297 continue;
12298
12299 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
12300 INSERT);
12301 /* NOTE: We can assume slot != NULL here because xcalloc never
12302 returns NULL. */
12303 if (*slot != NULL)
12304 return 1;
12305 *slot = name;
12306 }
12307
12308 return 0;
12309 }
12310
12311 /* When symbols change, it probably means the sources changed as well,
12312 and it might mean the static tracepoint markers are no longer at
12313 the same address or line numbers they used to be at last we
12314 checked. Losing your static tracepoints whenever you rebuild is
12315 undesirable. This function tries to resync/rematch gdb static
12316 tracepoints with the markers on the target, for static tracepoints
12317 that have not been set by marker id. Static tracepoint that have
12318 been set by marker id are reset by marker id in breakpoint_re_set.
12319 The heuristic is:
12320
12321 1) For a tracepoint set at a specific address, look for a marker at
12322 the old PC. If one is found there, assume to be the same marker.
12323 If the name / string id of the marker found is different from the
12324 previous known name, assume that means the user renamed the marker
12325 in the sources, and output a warning.
12326
12327 2) For a tracepoint set at a given line number, look for a marker
12328 at the new address of the old line number. If one is found there,
12329 assume to be the same marker. If the name / string id of the
12330 marker found is different from the previous known name, assume that
12331 means the user renamed the marker in the sources, and output a
12332 warning.
12333
12334 3) If a marker is no longer found at the same address or line, it
12335 may mean the marker no longer exists. But it may also just mean
12336 the code changed a bit. Maybe the user added a few lines of code
12337 that made the marker move up or down (in line number terms). Ask
12338 the target for info about the marker with the string id as we knew
12339 it. If found, update line number and address in the matching
12340 static tracepoint. This will get confused if there's more than one
12341 marker with the same ID (possible in UST, although unadvised
12342 precisely because it confuses tools). */
12343
12344 static struct symtab_and_line
12345 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12346 {
12347 struct tracepoint *tp = (struct tracepoint *) b;
12348 struct static_tracepoint_marker marker;
12349 CORE_ADDR pc;
12350
12351 pc = sal.pc;
12352 if (sal.line)
12353 find_line_pc (sal.symtab, sal.line, &pc);
12354
12355 if (target_static_tracepoint_marker_at (pc, &marker))
12356 {
12357 if (tp->static_trace_marker_id != marker.str_id)
12358 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12359 b->number, tp->static_trace_marker_id.c_str (),
12360 marker.str_id.c_str ());
12361
12362 tp->static_trace_marker_id = std::move (marker.str_id);
12363
12364 return sal;
12365 }
12366
12367 /* Old marker wasn't found on target at lineno. Try looking it up
12368 by string ID. */
12369 if (!sal.explicit_pc
12370 && sal.line != 0
12371 && sal.symtab != NULL
12372 && !tp->static_trace_marker_id.empty ())
12373 {
12374 std::vector<static_tracepoint_marker> markers
12375 = target_static_tracepoint_markers_by_strid
12376 (tp->static_trace_marker_id.c_str ());
12377
12378 if (!markers.empty ())
12379 {
12380 struct symbol *sym;
12381 struct static_tracepoint_marker *tpmarker;
12382 struct ui_out *uiout = current_uiout;
12383
12384 tpmarker = &markers[0];
12385
12386 tp->static_trace_marker_id = std::move (tpmarker->str_id);
12387
12388 warning (_("marker for static tracepoint %d (%s) not "
12389 "found at previous line number"),
12390 b->number, tp->static_trace_marker_id.c_str ());
12391
12392 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
12393 sym = find_pc_sect_function (tpmarker->address, NULL);
12394 uiout->text ("Now in ");
12395 if (sym)
12396 {
12397 uiout->field_string ("func", sym->print_name (),
12398 function_name_style.style ());
12399 uiout->text (" at ");
12400 }
12401 uiout->field_string ("file",
12402 symtab_to_filename_for_display (sal2.symtab),
12403 file_name_style.style ());
12404 uiout->text (":");
12405
12406 if (uiout->is_mi_like_p ())
12407 {
12408 const char *fullname = symtab_to_fullname (sal2.symtab);
12409
12410 uiout->field_string ("fullname", fullname);
12411 }
12412
12413 uiout->field_signed ("line", sal2.line);
12414 uiout->text ("\n");
12415
12416 b->loc->line_number = sal2.line;
12417 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
12418
12419 std::unique_ptr<explicit_location_spec> els
12420 (new explicit_location_spec ());
12421 els->source_filename
12422 = xstrdup (symtab_to_filename_for_display (sal2.symtab));
12423 els->line_offset.offset = b->loc->line_number;
12424 els->line_offset.sign = LINE_OFFSET_NONE;
12425
12426 b->locspec = std::move (els);
12427
12428 /* Might be nice to check if function changed, and warn if
12429 so. */
12430 }
12431 }
12432 return sal;
12433 }
12434
12435 /* Returns 1 iff locations A and B are sufficiently same that
12436 we don't need to report breakpoint as changed. */
12437
12438 static int
12439 locations_are_equal (struct bp_location *a, struct bp_location *b)
12440 {
12441 while (a && b)
12442 {
12443 if (a->address != b->address)
12444 return 0;
12445
12446 if (a->shlib_disabled != b->shlib_disabled)
12447 return 0;
12448
12449 if (a->enabled != b->enabled)
12450 return 0;
12451
12452 if (a->disabled_by_cond != b->disabled_by_cond)
12453 return 0;
12454
12455 a = a->next;
12456 b = b->next;
12457 }
12458
12459 if ((a == NULL) != (b == NULL))
12460 return 0;
12461
12462 return 1;
12463 }
12464
12465 /* Split all locations of B that are bound to PSPACE out of B's
12466 location list to a separate list and return that list's head. If
12467 PSPACE is NULL, hoist out all locations of B. */
12468
12469 static struct bp_location *
12470 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12471 {
12472 struct bp_location head;
12473 struct bp_location *i = b->loc;
12474 struct bp_location **i_link = &b->loc;
12475 struct bp_location *hoisted = &head;
12476
12477 if (pspace == NULL)
12478 {
12479 i = b->loc;
12480 b->loc = NULL;
12481 return i;
12482 }
12483
12484 head.next = NULL;
12485
12486 while (i != NULL)
12487 {
12488 if (i->pspace == pspace)
12489 {
12490 *i_link = i->next;
12491 i->next = NULL;
12492 hoisted->next = i;
12493 hoisted = i;
12494 }
12495 else
12496 i_link = &i->next;
12497 i = *i_link;
12498 }
12499
12500 return head.next;
12501 }
12502
12503 /* Create new breakpoint locations for B (a hardware or software
12504 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12505 zero, then B is a ranged breakpoint. Only recreates locations for
12506 FILTER_PSPACE. Locations of other program spaces are left
12507 untouched. */
12508
12509 void
12510 update_breakpoint_locations (code_breakpoint *b,
12511 struct program_space *filter_pspace,
12512 gdb::array_view<const symtab_and_line> sals,
12513 gdb::array_view<const symtab_and_line> sals_end)
12514 {
12515 struct bp_location *existing_locations;
12516
12517 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
12518 {
12519 /* Ranged breakpoints have only one start location and one end
12520 location. */
12521 b->enable_state = bp_disabled;
12522 gdb_printf (gdb_stderr,
12523 _("Could not reset ranged breakpoint %d: "
12524 "multiple locations found\n"),
12525 b->number);
12526 return;
12527 }
12528
12529 /* If there's no new locations, and all existing locations are
12530 pending, don't do anything. This optimizes the common case where
12531 all locations are in the same shared library, that was unloaded.
12532 We'd like to retain the location, so that when the library is
12533 loaded again, we don't loose the enabled/disabled status of the
12534 individual locations. */
12535 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
12536 return;
12537
12538 existing_locations = hoist_existing_locations (b, filter_pspace);
12539
12540 for (const auto &sal : sals)
12541 {
12542 struct bp_location *new_loc;
12543
12544 switch_to_program_space_and_thread (sal.pspace);
12545
12546 new_loc = b->add_location (sal);
12547
12548 /* Reparse conditions, they might contain references to the
12549 old symtab. */
12550 if (b->cond_string != NULL)
12551 {
12552 const char *s;
12553
12554 s = b->cond_string.get ();
12555 try
12556 {
12557 new_loc->cond = parse_exp_1 (&s, sal.pc,
12558 block_for_pc (sal.pc),
12559 0);
12560 }
12561 catch (const gdb_exception_error &e)
12562 {
12563 new_loc->disabled_by_cond = true;
12564 }
12565 }
12566
12567 if (!sals_end.empty ())
12568 {
12569 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
12570
12571 new_loc->length = end - sals[0].pc + 1;
12572 }
12573 }
12574
12575 /* If possible, carry over 'disable' status from existing
12576 breakpoints. */
12577 {
12578 struct bp_location *e = existing_locations;
12579 /* If there are multiple breakpoints with the same function name,
12580 e.g. for inline functions, comparing function names won't work.
12581 Instead compare pc addresses; this is just a heuristic as things
12582 may have moved, but in practice it gives the correct answer
12583 often enough until a better solution is found. */
12584 int have_ambiguous_names = ambiguous_names_p (b->loc);
12585
12586 for (; e; e = e->next)
12587 {
12588 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
12589 {
12590 if (have_ambiguous_names)
12591 {
12592 for (bp_location *l : b->locations ())
12593 {
12594 /* Ignore software vs hardware location type at
12595 this point, because with "set breakpoint
12596 auto-hw", after a re-set, locations that were
12597 hardware can end up as software, or vice versa.
12598 As mentioned above, this is an heuristic and in
12599 practice should give the correct answer often
12600 enough. */
12601 if (breakpoint_locations_match (e, l, true))
12602 {
12603 l->enabled = e->enabled;
12604 l->disabled_by_cond = e->disabled_by_cond;
12605 break;
12606 }
12607 }
12608 }
12609 else
12610 {
12611 for (bp_location *l : b->locations ())
12612 if (l->function_name
12613 && strcmp (e->function_name.get (),
12614 l->function_name.get ()) == 0)
12615 {
12616 l->enabled = e->enabled;
12617 l->disabled_by_cond = e->disabled_by_cond;
12618 break;
12619 }
12620 }
12621 }
12622 }
12623 }
12624
12625 if (!locations_are_equal (existing_locations, b->loc))
12626 gdb::observers::breakpoint_modified.notify (b);
12627 }
12628
12629 /* Find the SaL locations corresponding to the given LOCSPEC.
12630 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12631
12632 std::vector<symtab_and_line>
12633 code_breakpoint::location_spec_to_sals (location_spec *locspec,
12634 struct program_space *search_pspace,
12635 int *found)
12636 {
12637 struct gdb_exception exception;
12638
12639 std::vector<symtab_and_line> sals;
12640
12641 try
12642 {
12643 sals = decode_location_spec (locspec, search_pspace);
12644 }
12645 catch (gdb_exception_error &e)
12646 {
12647 int not_found_and_ok = 0;
12648
12649 /* For pending breakpoints, it's expected that parsing will
12650 fail until the right shared library is loaded. User has
12651 already told to create pending breakpoints and don't need
12652 extra messages. If breakpoint is in bp_shlib_disabled
12653 state, then user already saw the message about that
12654 breakpoint being disabled, and don't want to see more
12655 errors. */
12656 if (e.error == NOT_FOUND_ERROR
12657 && (condition_not_parsed
12658 || (loc != NULL
12659 && search_pspace != NULL
12660 && loc->pspace != search_pspace)
12661 || (loc && loc->shlib_disabled)
12662 || (loc && loc->pspace->executing_startup)
12663 || enable_state == bp_disabled))
12664 not_found_and_ok = 1;
12665
12666 if (!not_found_and_ok)
12667 {
12668 /* We surely don't want to warn about the same breakpoint
12669 10 times. One solution, implemented here, is disable
12670 the breakpoint on error. Another solution would be to
12671 have separate 'warning emitted' flag. Since this
12672 happens only when a binary has changed, I don't know
12673 which approach is better. */
12674 enable_state = bp_disabled;
12675 throw;
12676 }
12677
12678 exception = std::move (e);
12679 }
12680
12681 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
12682 {
12683 for (auto &sal : sals)
12684 resolve_sal_pc (&sal);
12685 if (condition_not_parsed && extra_string != NULL)
12686 {
12687 gdb::unique_xmalloc_ptr<char> local_cond, local_extra;
12688 int local_thread, local_task;
12689
12690 find_condition_and_thread_for_sals (sals, extra_string.get (),
12691 &local_cond, &local_thread,
12692 &local_task, &local_extra);
12693 gdb_assert (cond_string == nullptr);
12694 if (local_cond != nullptr)
12695 cond_string = std::move (local_cond);
12696 thread = local_thread;
12697 task = local_task;
12698 if (local_extra != nullptr)
12699 extra_string = std::move (local_extra);
12700 condition_not_parsed = 0;
12701 }
12702
12703 if (type == bp_static_tracepoint)
12704 sals[0] = update_static_tracepoint (this, sals[0]);
12705
12706 *found = 1;
12707 }
12708 else
12709 *found = 0;
12710
12711 return sals;
12712 }
12713
12714 /* The default re_set method, for typical hardware or software
12715 breakpoints. Reevaluate the breakpoint and recreate its
12716 locations. */
12717
12718 void
12719 code_breakpoint::re_set_default ()
12720 {
12721 struct program_space *filter_pspace = current_program_space;
12722 std::vector<symtab_and_line> expanded, expanded_end;
12723
12724 int found;
12725 std::vector<symtab_and_line> sals = location_spec_to_sals (locspec.get (),
12726 filter_pspace,
12727 &found);
12728 if (found)
12729 expanded = std::move (sals);
12730
12731 if (locspec_range_end != nullptr)
12732 {
12733 std::vector<symtab_and_line> sals_end
12734 = location_spec_to_sals (locspec_range_end.get (),
12735 filter_pspace, &found);
12736 if (found)
12737 expanded_end = std::move (sals_end);
12738 }
12739
12740 update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
12741 }
12742
12743 /* Default method for creating SALs from an address string. It basically
12744 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12745
12746 static void
12747 create_sals_from_location_spec_default (location_spec *locspec,
12748 struct linespec_result *canonical)
12749 {
12750 parse_breakpoint_sals (locspec, canonical);
12751 }
12752
12753 /* Decode the line represented by S by calling decode_line_full. This is the
12754 default function for the `decode_location' method of breakpoint_ops. */
12755
12756 static std::vector<symtab_and_line>
12757 decode_location_spec_default (struct breakpoint *b,
12758 location_spec *locspec,
12759 program_space *search_pspace)
12760 {
12761 struct linespec_result canonical;
12762
12763 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
12764 NULL, 0, &canonical, multiple_symbols_all,
12765 b->filter.get ());
12766
12767 /* We should get 0 or 1 resulting SALs. */
12768 gdb_assert (canonical.lsals.size () < 2);
12769
12770 if (!canonical.lsals.empty ())
12771 {
12772 const linespec_sals &lsal = canonical.lsals[0];
12773 return std::move (lsal.sals);
12774 }
12775 return {};
12776 }
12777
12778 /* Reset a breakpoint. */
12779
12780 static void
12781 breakpoint_re_set_one (breakpoint *b)
12782 {
12783 input_radix = b->input_radix;
12784 set_language (b->language);
12785
12786 b->re_set ();
12787 }
12788
12789 /* Re-set breakpoint locations for the current program space.
12790 Locations bound to other program spaces are left untouched. */
12791
12792 void
12793 breakpoint_re_set (void)
12794 {
12795 {
12796 scoped_restore_current_language save_language;
12797 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
12798 scoped_restore_current_pspace_and_thread restore_pspace_thread;
12799
12800 /* breakpoint_re_set_one sets the current_language to the language
12801 of the breakpoint it is resetting (see prepare_re_set_context)
12802 before re-evaluating the breakpoint's location. This change can
12803 unfortunately get undone by accident if the language_mode is set
12804 to auto, and we either switch frames, or more likely in this context,
12805 we select the current frame.
12806
12807 We prevent this by temporarily turning the language_mode to
12808 language_mode_manual. We restore it once all breakpoints
12809 have been reset. */
12810 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
12811 language_mode = language_mode_manual;
12812
12813 /* Note: we must not try to insert locations until after all
12814 breakpoints have been re-set. Otherwise, e.g., when re-setting
12815 breakpoint 1, we'd insert the locations of breakpoint 2, which
12816 hadn't been re-set yet, and thus may have stale locations. */
12817
12818 for (breakpoint *b : all_breakpoints_safe ())
12819 {
12820 try
12821 {
12822 breakpoint_re_set_one (b);
12823 }
12824 catch (const gdb_exception &ex)
12825 {
12826 exception_fprintf (gdb_stderr, ex,
12827 "Error in re-setting breakpoint %d: ",
12828 b->number);
12829 }
12830 }
12831
12832 jit_breakpoint_re_set ();
12833 }
12834
12835 create_overlay_event_breakpoint ();
12836 create_longjmp_master_breakpoint ();
12837 create_std_terminate_master_breakpoint ();
12838 create_exception_master_breakpoint ();
12839
12840 /* Now we can insert. */
12841 update_global_location_list (UGLL_MAY_INSERT);
12842 }
12843 \f
12844 /* Reset the thread number of this breakpoint:
12845
12846 - If the breakpoint is for all threads, leave it as-is.
12847 - Else, reset it to the current thread for inferior_ptid. */
12848 void
12849 breakpoint_re_set_thread (struct breakpoint *b)
12850 {
12851 if (b->thread != -1)
12852 {
12853 b->thread = inferior_thread ()->global_num;
12854
12855 /* We're being called after following a fork. The new fork is
12856 selected as current, and unless this was a vfork will have a
12857 different program space from the original thread. Reset that
12858 as well. */
12859 b->loc->pspace = current_program_space;
12860 }
12861 }
12862
12863 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
12864 If from_tty is nonzero, it prints a message to that effect,
12865 which ends with a period (no newline). */
12866
12867 void
12868 set_ignore_count (int bptnum, int count, int from_tty)
12869 {
12870 if (count < 0)
12871 count = 0;
12872
12873 for (breakpoint *b : all_breakpoints ())
12874 if (b->number == bptnum)
12875 {
12876 if (is_tracepoint (b))
12877 {
12878 if (from_tty && count != 0)
12879 gdb_printf (_("Ignore count ignored for tracepoint %d."),
12880 bptnum);
12881 return;
12882 }
12883
12884 b->ignore_count = count;
12885 if (from_tty)
12886 {
12887 if (count == 0)
12888 gdb_printf (_("Will stop next time "
12889 "breakpoint %d is reached."),
12890 bptnum);
12891 else if (count == 1)
12892 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
12893 bptnum);
12894 else
12895 gdb_printf (_("Will ignore next %d "
12896 "crossings of breakpoint %d."),
12897 count, bptnum);
12898 }
12899 gdb::observers::breakpoint_modified.notify (b);
12900 return;
12901 }
12902
12903 error (_("No breakpoint number %d."), bptnum);
12904 }
12905
12906 /* Command to set ignore-count of breakpoint N to COUNT. */
12907
12908 static void
12909 ignore_command (const char *args, int from_tty)
12910 {
12911 const char *p = args;
12912 int num;
12913
12914 if (p == 0)
12915 error_no_arg (_("a breakpoint number"));
12916
12917 num = get_number (&p);
12918 if (num == 0)
12919 error (_("bad breakpoint number: '%s'"), args);
12920 if (*p == 0)
12921 error (_("Second argument (specified ignore-count) is missing."));
12922
12923 set_ignore_count (num,
12924 longest_to_int (value_as_long (parse_and_eval (p))),
12925 from_tty);
12926 if (from_tty)
12927 gdb_printf ("\n");
12928 }
12929 \f
12930
12931 /* Call FUNCTION on each of the breakpoints with numbers in the range
12932 defined by BP_NUM_RANGE (an inclusive range). */
12933
12934 static void
12935 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
12936 gdb::function_view<void (breakpoint *)> function)
12937 {
12938 if (bp_num_range.first == 0)
12939 {
12940 warning (_("bad breakpoint number at or near '%d'"),
12941 bp_num_range.first);
12942 }
12943 else
12944 {
12945 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
12946 {
12947 bool match = false;
12948
12949 for (breakpoint *b : all_breakpoints_safe ())
12950 if (b->number == i)
12951 {
12952 match = true;
12953 function (b);
12954 break;
12955 }
12956 if (!match)
12957 gdb_printf (_("No breakpoint number %d.\n"), i);
12958 }
12959 }
12960 }
12961
12962 /* Call FUNCTION on each of the breakpoints whose numbers are given in
12963 ARGS. */
12964
12965 static void
12966 map_breakpoint_numbers (const char *args,
12967 gdb::function_view<void (breakpoint *)> function)
12968 {
12969 if (args == NULL || *args == '\0')
12970 error_no_arg (_("one or more breakpoint numbers"));
12971
12972 number_or_range_parser parser (args);
12973
12974 while (!parser.finished ())
12975 {
12976 int num = parser.get_number ();
12977 map_breakpoint_number_range (std::make_pair (num, num), function);
12978 }
12979 }
12980
12981 /* Return the breakpoint location structure corresponding to the
12982 BP_NUM and LOC_NUM values. */
12983
12984 static struct bp_location *
12985 find_location_by_number (int bp_num, int loc_num)
12986 {
12987 breakpoint *b = get_breakpoint (bp_num);
12988
12989 if (!b || b->number != bp_num)
12990 error (_("Bad breakpoint number '%d'"), bp_num);
12991
12992 if (loc_num == 0)
12993 error (_("Bad breakpoint location number '%d'"), loc_num);
12994
12995 int n = 0;
12996 for (bp_location *loc : b->locations ())
12997 if (++n == loc_num)
12998 return loc;
12999
13000 error (_("Bad breakpoint location number '%d'"), loc_num);
13001 }
13002
13003 /* Modes of operation for extract_bp_num. */
13004 enum class extract_bp_kind
13005 {
13006 /* Extracting a breakpoint number. */
13007 bp,
13008
13009 /* Extracting a location number. */
13010 loc,
13011 };
13012
13013 /* Extract a breakpoint or location number (as determined by KIND)
13014 from the string starting at START. TRAILER is a character which
13015 can be found after the number. If you don't want a trailer, use
13016 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13017 string. This always returns a positive integer. */
13018
13019 static int
13020 extract_bp_num (extract_bp_kind kind, const char *start,
13021 int trailer, const char **end_out = NULL)
13022 {
13023 const char *end = start;
13024 int num = get_number_trailer (&end, trailer);
13025 if (num < 0)
13026 error (kind == extract_bp_kind::bp
13027 ? _("Negative breakpoint number '%.*s'")
13028 : _("Negative breakpoint location number '%.*s'"),
13029 int (end - start), start);
13030 if (num == 0)
13031 error (kind == extract_bp_kind::bp
13032 ? _("Bad breakpoint number '%.*s'")
13033 : _("Bad breakpoint location number '%.*s'"),
13034 int (end - start), start);
13035
13036 if (end_out != NULL)
13037 *end_out = end;
13038 return num;
13039 }
13040
13041 /* Extract a breakpoint or location range (as determined by KIND) in
13042 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13043 representing the (inclusive) range. The returned pair's elements
13044 are always positive integers. */
13045
13046 static std::pair<int, int>
13047 extract_bp_or_bp_range (extract_bp_kind kind,
13048 const std::string &arg,
13049 std::string::size_type arg_offset)
13050 {
13051 std::pair<int, int> range;
13052 const char *bp_loc = &arg[arg_offset];
13053 std::string::size_type dash = arg.find ('-', arg_offset);
13054 if (dash != std::string::npos)
13055 {
13056 /* bp_loc is a range (x-z). */
13057 if (arg.length () == dash + 1)
13058 error (kind == extract_bp_kind::bp
13059 ? _("Bad breakpoint number at or near: '%s'")
13060 : _("Bad breakpoint location number at or near: '%s'"),
13061 bp_loc);
13062
13063 const char *end;
13064 const char *start_first = bp_loc;
13065 const char *start_second = &arg[dash + 1];
13066 range.first = extract_bp_num (kind, start_first, '-');
13067 range.second = extract_bp_num (kind, start_second, '\0', &end);
13068
13069 if (range.first > range.second)
13070 error (kind == extract_bp_kind::bp
13071 ? _("Inverted breakpoint range at '%.*s'")
13072 : _("Inverted breakpoint location range at '%.*s'"),
13073 int (end - start_first), start_first);
13074 }
13075 else
13076 {
13077 /* bp_loc is a single value. */
13078 range.first = extract_bp_num (kind, bp_loc, '\0');
13079 range.second = range.first;
13080 }
13081 return range;
13082 }
13083
13084 /* Extract the breakpoint/location range specified by ARG. Returns
13085 the breakpoint range in BP_NUM_RANGE, and the location range in
13086 BP_LOC_RANGE.
13087
13088 ARG may be in any of the following forms:
13089
13090 x where 'x' is a breakpoint number.
13091 x-y where 'x' and 'y' specify a breakpoint numbers range.
13092 x.y where 'x' is a breakpoint number and 'y' a location number.
13093 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13094 location number range.
13095 */
13096
13097 static void
13098 extract_bp_number_and_location (const std::string &arg,
13099 std::pair<int, int> &bp_num_range,
13100 std::pair<int, int> &bp_loc_range)
13101 {
13102 std::string::size_type dot = arg.find ('.');
13103
13104 if (dot != std::string::npos)
13105 {
13106 /* Handle 'x.y' and 'x.y-z' cases. */
13107
13108 if (arg.length () == dot + 1 || dot == 0)
13109 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
13110
13111 bp_num_range.first
13112 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13113 bp_num_range.second = bp_num_range.first;
13114
13115 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13116 arg, dot + 1);
13117 }
13118 else
13119 {
13120 /* Handle x and x-y cases. */
13121
13122 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
13123 bp_loc_range.first = 0;
13124 bp_loc_range.second = 0;
13125 }
13126 }
13127
13128 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13129 specifies whether to enable or disable. */
13130
13131 static void
13132 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13133 {
13134 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13135 if (loc != NULL)
13136 {
13137 if (loc->disabled_by_cond && enable)
13138 error (_("Breakpoint %d's condition is invalid at location %d, "
13139 "cannot enable."), bp_num, loc_num);
13140
13141 if (loc->enabled != enable)
13142 {
13143 loc->enabled = enable;
13144 mark_breakpoint_location_modified (loc);
13145 }
13146 if (target_supports_enable_disable_tracepoint ()
13147 && current_trace_status ()->running && loc->owner
13148 && is_tracepoint (loc->owner))
13149 target_disable_tracepoint (loc);
13150 }
13151 update_global_location_list (UGLL_DONT_INSERT);
13152
13153 gdb::observers::breakpoint_modified.notify (loc->owner);
13154 }
13155
13156 /* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
13157 owner. 1-based indexing. -1 signals NOT FOUND. */
13158
13159 static int
13160 find_loc_num_by_location (bp_location *loc)
13161 {
13162 if (loc != nullptr && loc->owner != nullptr)
13163 {
13164 /* Locations use 1-based indexing. */
13165 int loc_num = 1;
13166 for (bp_location *it : loc->owner->locations ())
13167 {
13168 if (it == loc)
13169 return loc_num;
13170 loc_num++;
13171 }
13172 }
13173 return -1;
13174 }
13175
13176 /* Enable or disable a breakpoint location LOC. ENABLE
13177 specifies whether to enable or disable. */
13178
13179 void
13180 enable_disable_bp_location (bp_location *loc, bool enable)
13181 {
13182 if (loc == nullptr)
13183 error (_("Breakpoint location is invalid."));
13184
13185 if (loc->owner == nullptr)
13186 error (_("Breakpoint location does not have an owner breakpoint."));
13187
13188 if (loc->disabled_by_cond && enable)
13189 {
13190 int loc_num = find_loc_num_by_location (loc);
13191 if (loc_num == -1)
13192 error (_("Breakpoint location LOC_NUM could not be found."));
13193 else
13194 error (_("Breakpoint %d's condition is invalid at location %d, "
13195 "cannot enable."), loc->owner->number, loc_num);
13196 }
13197
13198 if (loc->enabled != enable)
13199 {
13200 loc->enabled = enable;
13201 mark_breakpoint_location_modified (loc);
13202 }
13203
13204 if (target_supports_enable_disable_tracepoint ()
13205 && current_trace_status ()->running && loc->owner
13206 && is_tracepoint (loc->owner))
13207 target_disable_tracepoint (loc);
13208
13209 update_global_location_list (UGLL_DONT_INSERT);
13210 gdb::observers::breakpoint_modified.notify (loc->owner);
13211 }
13212
13213 /* Enable or disable a range of breakpoint locations. BP_NUM is the
13214 number of the breakpoint, and BP_LOC_RANGE specifies the
13215 (inclusive) range of location numbers of that breakpoint to
13216 enable/disable. ENABLE specifies whether to enable or disable the
13217 location. */
13218
13219 static void
13220 enable_disable_breakpoint_location_range (int bp_num,
13221 std::pair<int, int> &bp_loc_range,
13222 bool enable)
13223 {
13224 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13225 enable_disable_bp_num_loc (bp_num, i, enable);
13226 }
13227
13228 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13229 If from_tty is nonzero, it prints a message to that effect,
13230 which ends with a period (no newline). */
13231
13232 void
13233 disable_breakpoint (struct breakpoint *bpt)
13234 {
13235 /* Never disable a watchpoint scope breakpoint; we want to
13236 hit them when we leave scope so we can delete both the
13237 watchpoint and its scope breakpoint at that time. */
13238 if (bpt->type == bp_watchpoint_scope)
13239 return;
13240
13241 bpt->enable_state = bp_disabled;
13242
13243 /* Mark breakpoint locations modified. */
13244 mark_breakpoint_modified (bpt);
13245
13246 if (target_supports_enable_disable_tracepoint ()
13247 && current_trace_status ()->running && is_tracepoint (bpt))
13248 {
13249 for (bp_location *location : bpt->locations ())
13250 target_disable_tracepoint (location);
13251 }
13252
13253 update_global_location_list (UGLL_DONT_INSERT);
13254
13255 gdb::observers::breakpoint_modified.notify (bpt);
13256 }
13257
13258 /* Enable or disable the breakpoint(s) or breakpoint location(s)
13259 specified in ARGS. ARGS may be in any of the formats handled by
13260 extract_bp_number_and_location. ENABLE specifies whether to enable
13261 or disable the breakpoints/locations. */
13262
13263 static void
13264 enable_disable_command (const char *args, int from_tty, bool enable)
13265 {
13266 if (args == 0)
13267 {
13268 for (breakpoint *bpt : all_breakpoints ())
13269 if (user_breakpoint_p (bpt))
13270 {
13271 if (enable)
13272 enable_breakpoint (bpt);
13273 else
13274 disable_breakpoint (bpt);
13275 }
13276 }
13277 else
13278 {
13279 std::string num = extract_arg (&args);
13280
13281 while (!num.empty ())
13282 {
13283 std::pair<int, int> bp_num_range, bp_loc_range;
13284
13285 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13286
13287 if (bp_loc_range.first == bp_loc_range.second
13288 && bp_loc_range.first == 0)
13289 {
13290 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
13291 map_breakpoint_number_range (bp_num_range,
13292 enable
13293 ? enable_breakpoint
13294 : disable_breakpoint);
13295 }
13296 else
13297 {
13298 /* Handle breakpoint ids with formats 'x.y' or
13299 'x.y-z'. */
13300 enable_disable_breakpoint_location_range
13301 (bp_num_range.first, bp_loc_range, enable);
13302 }
13303 num = extract_arg (&args);
13304 }
13305 }
13306 }
13307
13308 /* The disable command disables the specified breakpoints/locations
13309 (or all defined breakpoints) so they're no longer effective in
13310 stopping the inferior. ARGS may be in any of the forms defined in
13311 extract_bp_number_and_location. */
13312
13313 static void
13314 disable_command (const char *args, int from_tty)
13315 {
13316 enable_disable_command (args, from_tty, false);
13317 }
13318
13319 static void
13320 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13321 int count)
13322 {
13323 int target_resources_ok;
13324
13325 if (bpt->type == bp_hardware_breakpoint)
13326 {
13327 int i;
13328 i = hw_breakpoint_used_count ();
13329 target_resources_ok =
13330 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13331 i + 1, 0);
13332 if (target_resources_ok == 0)
13333 error (_("No hardware breakpoint support in the target."));
13334 else if (target_resources_ok < 0)
13335 error (_("Hardware breakpoints used exceeds limit."));
13336 }
13337
13338 if (is_watchpoint (bpt))
13339 {
13340 /* Initialize it just to avoid a GCC false warning. */
13341 enum enable_state orig_enable_state = bp_disabled;
13342
13343 try
13344 {
13345 struct watchpoint *w = (struct watchpoint *) bpt;
13346
13347 orig_enable_state = bpt->enable_state;
13348 bpt->enable_state = bp_enabled;
13349 update_watchpoint (w, 1 /* reparse */);
13350 }
13351 catch (const gdb_exception &e)
13352 {
13353 bpt->enable_state = orig_enable_state;
13354 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13355 bpt->number);
13356 return;
13357 }
13358 }
13359
13360 bpt->enable_state = bp_enabled;
13361
13362 /* Mark breakpoint locations modified. */
13363 mark_breakpoint_modified (bpt);
13364
13365 if (target_supports_enable_disable_tracepoint ()
13366 && current_trace_status ()->running && is_tracepoint (bpt))
13367 {
13368 for (bp_location *location : bpt->locations ())
13369 target_enable_tracepoint (location);
13370 }
13371
13372 bpt->disposition = disposition;
13373 bpt->enable_count = count;
13374 update_global_location_list (UGLL_MAY_INSERT);
13375
13376 gdb::observers::breakpoint_modified.notify (bpt);
13377 }
13378
13379
13380 void
13381 enable_breakpoint (struct breakpoint *bpt)
13382 {
13383 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13384 }
13385
13386 /* The enable command enables the specified breakpoints/locations (or
13387 all defined breakpoints) so they once again become (or continue to
13388 be) effective in stopping the inferior. ARGS may be in any of the
13389 forms defined in extract_bp_number_and_location. */
13390
13391 static void
13392 enable_command (const char *args, int from_tty)
13393 {
13394 enable_disable_command (args, from_tty, true);
13395 }
13396
13397 static void
13398 enable_once_command (const char *args, int from_tty)
13399 {
13400 map_breakpoint_numbers
13401 (args, [&] (breakpoint *b)
13402 {
13403 iterate_over_related_breakpoints
13404 (b, [&] (breakpoint *bpt)
13405 {
13406 enable_breakpoint_disp (bpt, disp_disable, 1);
13407 });
13408 });
13409 }
13410
13411 static void
13412 enable_count_command (const char *args, int from_tty)
13413 {
13414 int count;
13415
13416 if (args == NULL)
13417 error_no_arg (_("hit count"));
13418
13419 count = get_number (&args);
13420
13421 map_breakpoint_numbers
13422 (args, [&] (breakpoint *b)
13423 {
13424 iterate_over_related_breakpoints
13425 (b, [&] (breakpoint *bpt)
13426 {
13427 enable_breakpoint_disp (bpt, disp_disable, count);
13428 });
13429 });
13430 }
13431
13432 static void
13433 enable_delete_command (const char *args, int from_tty)
13434 {
13435 map_breakpoint_numbers
13436 (args, [&] (breakpoint *b)
13437 {
13438 iterate_over_related_breakpoints
13439 (b, [&] (breakpoint *bpt)
13440 {
13441 enable_breakpoint_disp (bpt, disp_del, 1);
13442 });
13443 });
13444 }
13445 \f
13446 /* Invalidate last known value of any hardware watchpoint if
13447 the memory which that value represents has been written to by
13448 GDB itself. */
13449
13450 static void
13451 invalidate_bp_value_on_memory_change (struct inferior *inferior,
13452 CORE_ADDR addr, ssize_t len,
13453 const bfd_byte *data)
13454 {
13455 for (breakpoint *bp : all_breakpoints ())
13456 if (bp->enable_state == bp_enabled
13457 && bp->type == bp_hardware_watchpoint)
13458 {
13459 struct watchpoint *wp = (struct watchpoint *) bp;
13460
13461 if (wp->val_valid && wp->val != nullptr)
13462 {
13463 for (bp_location *loc : bp->locations ())
13464 if (loc->loc_type == bp_loc_hardware_watchpoint
13465 && loc->address + loc->length > addr
13466 && addr + len > loc->address)
13467 {
13468 wp->val = NULL;
13469 wp->val_valid = false;
13470 }
13471 }
13472 }
13473 }
13474
13475 /* Create and insert a breakpoint for software single step. */
13476
13477 void
13478 insert_single_step_breakpoint (struct gdbarch *gdbarch,
13479 const address_space *aspace,
13480 CORE_ADDR next_pc)
13481 {
13482 struct thread_info *tp = inferior_thread ();
13483 struct symtab_and_line sal;
13484 CORE_ADDR pc = next_pc;
13485
13486 if (tp->control.single_step_breakpoints == NULL)
13487 {
13488 std::unique_ptr<breakpoint> b
13489 (new momentary_breakpoint (gdbarch, bp_single_step,
13490 current_program_space,
13491 null_frame_id,
13492 tp->global_num));
13493
13494 tp->control.single_step_breakpoints
13495 = add_to_breakpoint_chain (std::move (b));
13496 }
13497
13498 sal = find_pc_line (pc, 0);
13499 sal.pc = pc;
13500 sal.section = find_pc_overlay (pc);
13501 sal.explicit_pc = 1;
13502
13503 auto *ss_bp
13504 = static_cast<momentary_breakpoint *> (tp->control.single_step_breakpoints);
13505 ss_bp->add_location (sal);
13506
13507 update_global_location_list (UGLL_INSERT);
13508 }
13509
13510 /* Insert single step breakpoints according to the current state. */
13511
13512 int
13513 insert_single_step_breakpoints (struct gdbarch *gdbarch)
13514 {
13515 struct regcache *regcache = get_current_regcache ();
13516 std::vector<CORE_ADDR> next_pcs;
13517
13518 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
13519
13520 if (!next_pcs.empty ())
13521 {
13522 struct frame_info *frame = get_current_frame ();
13523 const address_space *aspace = get_frame_address_space (frame);
13524
13525 for (CORE_ADDR pc : next_pcs)
13526 insert_single_step_breakpoint (gdbarch, aspace, pc);
13527
13528 return 1;
13529 }
13530 else
13531 return 0;
13532 }
13533
13534 /* See breakpoint.h. */
13535
13536 int
13537 breakpoint_has_location_inserted_here (struct breakpoint *bp,
13538 const address_space *aspace,
13539 CORE_ADDR pc)
13540 {
13541 for (bp_location *loc : bp->locations ())
13542 if (loc->inserted
13543 && breakpoint_location_address_match (loc, aspace, pc))
13544 return 1;
13545
13546 return 0;
13547 }
13548
13549 /* Check whether a software single-step breakpoint is inserted at
13550 PC. */
13551
13552 int
13553 single_step_breakpoint_inserted_here_p (const address_space *aspace,
13554 CORE_ADDR pc)
13555 {
13556 for (breakpoint *bpt : all_breakpoints ())
13557 {
13558 if (bpt->type == bp_single_step
13559 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13560 return 1;
13561 }
13562 return 0;
13563 }
13564
13565 /* Tracepoint-specific operations. */
13566
13567 /* Set tracepoint count to NUM. */
13568 static void
13569 set_tracepoint_count (int num)
13570 {
13571 tracepoint_count = num;
13572 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
13573 }
13574
13575 static void
13576 trace_command (const char *arg, int from_tty)
13577 {
13578 location_spec_up locspec = string_to_location_spec (&arg,
13579 current_language);
13580 const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
13581 (locspec.get (), true /* is_tracepoint */);
13582
13583 create_breakpoint (get_current_arch (),
13584 locspec.get (),
13585 NULL, 0, arg, false, 1 /* parse arg */,
13586 0 /* tempflag */,
13587 bp_tracepoint /* type_wanted */,
13588 0 /* Ignore count */,
13589 pending_break_support,
13590 ops,
13591 from_tty,
13592 1 /* enabled */,
13593 0 /* internal */, 0);
13594 }
13595
13596 static void
13597 ftrace_command (const char *arg, int from_tty)
13598 {
13599 location_spec_up locspec = string_to_location_spec (&arg,
13600 current_language);
13601 create_breakpoint (get_current_arch (),
13602 locspec.get (),
13603 NULL, 0, arg, false, 1 /* parse arg */,
13604 0 /* tempflag */,
13605 bp_fast_tracepoint /* type_wanted */,
13606 0 /* Ignore count */,
13607 pending_break_support,
13608 &code_breakpoint_ops,
13609 from_tty,
13610 1 /* enabled */,
13611 0 /* internal */, 0);
13612 }
13613
13614 /* strace command implementation. Creates a static tracepoint. */
13615
13616 static void
13617 strace_command (const char *arg, int from_tty)
13618 {
13619 const struct breakpoint_ops *ops;
13620 location_spec_up locspec;
13621 enum bptype type;
13622
13623 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13624 or with a normal static tracepoint. */
13625 if (arg && startswith (arg, "-m") && isspace (arg[2]))
13626 {
13627 ops = &strace_marker_breakpoint_ops;
13628 locspec = new_linespec_location_spec (&arg,
13629 symbol_name_match_type::FULL);
13630 type = bp_static_marker_tracepoint;
13631 }
13632 else
13633 {
13634 ops = &code_breakpoint_ops;
13635 locspec = string_to_location_spec (&arg, current_language);
13636 type = bp_static_tracepoint;
13637 }
13638
13639 create_breakpoint (get_current_arch (),
13640 locspec.get (),
13641 NULL, 0, arg, false, 1 /* parse arg */,
13642 0 /* tempflag */,
13643 type /* type_wanted */,
13644 0 /* Ignore count */,
13645 pending_break_support,
13646 ops,
13647 from_tty,
13648 1 /* enabled */,
13649 0 /* internal */, 0);
13650 }
13651
13652 /* Set up a fake reader function that gets command lines from a linked
13653 list that was acquired during tracepoint uploading. */
13654
13655 static struct uploaded_tp *this_utp;
13656 static int next_cmd;
13657
13658 static char *
13659 read_uploaded_action (void)
13660 {
13661 char *rslt = nullptr;
13662
13663 if (next_cmd < this_utp->cmd_strings.size ())
13664 {
13665 rslt = this_utp->cmd_strings[next_cmd].get ();
13666 next_cmd++;
13667 }
13668
13669 return rslt;
13670 }
13671
13672 /* Given information about a tracepoint as recorded on a target (which
13673 can be either a live system or a trace file), attempt to create an
13674 equivalent GDB tracepoint. This is not a reliable process, since
13675 the target does not necessarily have all the information used when
13676 the tracepoint was originally defined. */
13677
13678 struct tracepoint *
13679 create_tracepoint_from_upload (struct uploaded_tp *utp)
13680 {
13681 const char *addr_str;
13682 char small_buf[100];
13683 struct tracepoint *tp;
13684
13685 if (utp->at_string)
13686 addr_str = utp->at_string.get ();
13687 else
13688 {
13689 /* In the absence of a source location, fall back to raw
13690 address. Since there is no way to confirm that the address
13691 means the same thing as when the trace was started, warn the
13692 user. */
13693 warning (_("Uploaded tracepoint %d has no "
13694 "source location, using raw address"),
13695 utp->number);
13696 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
13697 addr_str = small_buf;
13698 }
13699
13700 /* There's not much we can do with a sequence of bytecodes. */
13701 if (utp->cond && !utp->cond_string)
13702 warning (_("Uploaded tracepoint %d condition "
13703 "has no source form, ignoring it"),
13704 utp->number);
13705
13706 location_spec_up locspec = string_to_location_spec (&addr_str,
13707 current_language);
13708 if (!create_breakpoint (get_current_arch (),
13709 locspec.get (),
13710 utp->cond_string.get (), -1, addr_str,
13711 false /* force_condition */,
13712 0 /* parse cond/thread */,
13713 0 /* tempflag */,
13714 utp->type /* type_wanted */,
13715 0 /* Ignore count */,
13716 pending_break_support,
13717 &code_breakpoint_ops,
13718 0 /* from_tty */,
13719 utp->enabled /* enabled */,
13720 0 /* internal */,
13721 CREATE_BREAKPOINT_FLAGS_INSERTED))
13722 return NULL;
13723
13724 /* Get the tracepoint we just created. */
13725 tp = get_tracepoint (tracepoint_count);
13726 gdb_assert (tp != NULL);
13727
13728 if (utp->pass > 0)
13729 {
13730 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
13731 tp->number);
13732
13733 trace_pass_command (small_buf, 0);
13734 }
13735
13736 /* If we have uploaded versions of the original commands, set up a
13737 special-purpose "reader" function and call the usual command line
13738 reader, then pass the result to the breakpoint command-setting
13739 function. */
13740 if (!utp->cmd_strings.empty ())
13741 {
13742 counted_command_line cmd_list;
13743
13744 this_utp = utp;
13745 next_cmd = 0;
13746
13747 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
13748
13749 breakpoint_set_commands (tp, std::move (cmd_list));
13750 }
13751 else if (!utp->actions.empty ()
13752 || !utp->step_actions.empty ())
13753 warning (_("Uploaded tracepoint %d actions "
13754 "have no source form, ignoring them"),
13755 utp->number);
13756
13757 /* Copy any status information that might be available. */
13758 tp->hit_count = utp->hit_count;
13759 tp->traceframe_usage = utp->traceframe_usage;
13760
13761 return tp;
13762 }
13763
13764 /* Print information on tracepoint number TPNUM_EXP, or all if
13765 omitted. */
13766
13767 static void
13768 info_tracepoints_command (const char *args, int from_tty)
13769 {
13770 struct ui_out *uiout = current_uiout;
13771 int num_printed;
13772
13773 num_printed = breakpoint_1 (args, false, is_tracepoint);
13774
13775 if (num_printed == 0)
13776 {
13777 if (args == NULL || *args == '\0')
13778 uiout->message ("No tracepoints.\n");
13779 else
13780 uiout->message ("No tracepoint matching '%s'.\n", args);
13781 }
13782
13783 default_collect_info ();
13784 }
13785
13786 /* The 'enable trace' command enables tracepoints.
13787 Not supported by all targets. */
13788 static void
13789 enable_trace_command (const char *args, int from_tty)
13790 {
13791 enable_command (args, from_tty);
13792 }
13793
13794 /* The 'disable trace' command disables tracepoints.
13795 Not supported by all targets. */
13796 static void
13797 disable_trace_command (const char *args, int from_tty)
13798 {
13799 disable_command (args, from_tty);
13800 }
13801
13802 /* Remove a tracepoint (or all if no argument). */
13803 static void
13804 delete_trace_command (const char *arg, int from_tty)
13805 {
13806 dont_repeat ();
13807
13808 if (arg == 0)
13809 {
13810 int breaks_to_delete = 0;
13811
13812 /* Delete all breakpoints if no argument.
13813 Do not delete internal or call-dummy breakpoints, these
13814 have to be deleted with an explicit breakpoint number
13815 argument. */
13816 for (breakpoint *tp : all_tracepoints ())
13817 if (is_tracepoint (tp) && user_breakpoint_p (tp))
13818 {
13819 breaks_to_delete = 1;
13820 break;
13821 }
13822
13823 /* Ask user only if there are some breakpoints to delete. */
13824 if (!from_tty
13825 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13826 {
13827 for (breakpoint *b : all_breakpoints_safe ())
13828 if (is_tracepoint (b) && user_breakpoint_p (b))
13829 delete_breakpoint (b);
13830 }
13831 }
13832 else
13833 map_breakpoint_numbers
13834 (arg, [&] (breakpoint *br)
13835 {
13836 iterate_over_related_breakpoints (br, delete_breakpoint);
13837 });
13838 }
13839
13840 /* Helper function for trace_pass_command. */
13841
13842 static void
13843 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
13844 {
13845 tp->pass_count = count;
13846 gdb::observers::breakpoint_modified.notify (tp);
13847 if (from_tty)
13848 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
13849 tp->number, count);
13850 }
13851
13852 /* Set passcount for tracepoint.
13853
13854 First command argument is passcount, second is tracepoint number.
13855 If tracepoint number omitted, apply to most recently defined.
13856 Also accepts special argument "all". */
13857
13858 static void
13859 trace_pass_command (const char *args, int from_tty)
13860 {
13861 struct tracepoint *t1;
13862 ULONGEST count;
13863
13864 if (args == 0 || *args == 0)
13865 error (_("passcount command requires an "
13866 "argument (count + optional TP num)"));
13867
13868 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
13869
13870 args = skip_spaces (args);
13871 if (*args && strncasecmp (args, "all", 3) == 0)
13872 {
13873 args += 3; /* Skip special argument "all". */
13874 if (*args)
13875 error (_("Junk at end of arguments."));
13876
13877 for (breakpoint *b : all_tracepoints ())
13878 {
13879 t1 = (struct tracepoint *) b;
13880 trace_pass_set_count (t1, count, from_tty);
13881 }
13882 }
13883 else if (*args == '\0')
13884 {
13885 t1 = get_tracepoint_by_number (&args, NULL);
13886 if (t1)
13887 trace_pass_set_count (t1, count, from_tty);
13888 }
13889 else
13890 {
13891 number_or_range_parser parser (args);
13892 while (!parser.finished ())
13893 {
13894 t1 = get_tracepoint_by_number (&args, &parser);
13895 if (t1)
13896 trace_pass_set_count (t1, count, from_tty);
13897 }
13898 }
13899 }
13900
13901 struct tracepoint *
13902 get_tracepoint (int num)
13903 {
13904 for (breakpoint *t : all_tracepoints ())
13905 if (t->number == num)
13906 return (struct tracepoint *) t;
13907
13908 return NULL;
13909 }
13910
13911 /* Find the tracepoint with the given target-side number (which may be
13912 different from the tracepoint number after disconnecting and
13913 reconnecting). */
13914
13915 struct tracepoint *
13916 get_tracepoint_by_number_on_target (int num)
13917 {
13918 for (breakpoint *b : all_tracepoints ())
13919 {
13920 struct tracepoint *t = (struct tracepoint *) b;
13921
13922 if (t->number_on_target == num)
13923 return t;
13924 }
13925
13926 return NULL;
13927 }
13928
13929 /* Utility: parse a tracepoint number and look it up in the list.
13930 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
13931 If the argument is missing, the most recent tracepoint
13932 (tracepoint_count) is returned. */
13933
13934 struct tracepoint *
13935 get_tracepoint_by_number (const char **arg,
13936 number_or_range_parser *parser)
13937 {
13938 int tpnum;
13939 const char *instring = arg == NULL ? NULL : *arg;
13940
13941 if (parser != NULL)
13942 {
13943 gdb_assert (!parser->finished ());
13944 tpnum = parser->get_number ();
13945 }
13946 else if (arg == NULL || *arg == NULL || ! **arg)
13947 tpnum = tracepoint_count;
13948 else
13949 tpnum = get_number (arg);
13950
13951 if (tpnum <= 0)
13952 {
13953 if (instring && *instring)
13954 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
13955 instring);
13956 else
13957 gdb_printf (_("No previous tracepoint\n"));
13958 return NULL;
13959 }
13960
13961 for (breakpoint *t : all_tracepoints ())
13962 if (t->number == tpnum)
13963 return (struct tracepoint *) t;
13964
13965 gdb_printf ("No tracepoint number %d.\n", tpnum);
13966 return NULL;
13967 }
13968
13969 void
13970 breakpoint::print_recreate_thread (struct ui_file *fp) const
13971 {
13972 if (thread != -1)
13973 gdb_printf (fp, " thread %d", thread);
13974
13975 if (task != 0)
13976 gdb_printf (fp, " task %d", task);
13977
13978 gdb_printf (fp, "\n");
13979 }
13980
13981 /* Save information on user settable breakpoints (watchpoints, etc) to
13982 a new script file named FILENAME. If FILTER is non-NULL, call it
13983 on each breakpoint and only include the ones for which it returns
13984 true. */
13985
13986 static void
13987 save_breakpoints (const char *filename, int from_tty,
13988 bool (*filter) (const struct breakpoint *))
13989 {
13990 int any = 0;
13991 int extra_trace_bits = 0;
13992
13993 if (filename == 0 || *filename == 0)
13994 error (_("Argument required (file name in which to save)"));
13995
13996 /* See if we have anything to save. */
13997 for (breakpoint *tp : all_breakpoints ())
13998 {
13999 /* Skip internal and momentary breakpoints. */
14000 if (!user_breakpoint_p (tp))
14001 continue;
14002
14003 /* If we have a filter, only save the breakpoints it accepts. */
14004 if (filter && !filter (tp))
14005 continue;
14006
14007 any = 1;
14008
14009 if (is_tracepoint (tp))
14010 {
14011 extra_trace_bits = 1;
14012
14013 /* We can stop searching. */
14014 break;
14015 }
14016 }
14017
14018 if (!any)
14019 {
14020 warning (_("Nothing to save."));
14021 return;
14022 }
14023
14024 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14025
14026 stdio_file fp;
14027
14028 if (!fp.open (expanded_filename.get (), "w"))
14029 error (_("Unable to open file '%s' for saving (%s)"),
14030 expanded_filename.get (), safe_strerror (errno));
14031
14032 if (extra_trace_bits)
14033 save_trace_state_variables (&fp);
14034
14035 for (breakpoint *tp : all_breakpoints ())
14036 {
14037 /* Skip internal and momentary breakpoints. */
14038 if (!user_breakpoint_p (tp))
14039 continue;
14040
14041 /* If we have a filter, only save the breakpoints it accepts. */
14042 if (filter && !filter (tp))
14043 continue;
14044
14045 tp->print_recreate (&fp);
14046
14047 /* Note, we can't rely on tp->number for anything, as we can't
14048 assume the recreated breakpoint numbers will match. Use $bpnum
14049 instead. */
14050
14051 if (tp->cond_string)
14052 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
14053
14054 if (tp->ignore_count)
14055 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
14056
14057 if (tp->type != bp_dprintf && tp->commands)
14058 {
14059 fp.puts (" commands\n");
14060
14061 ui_out_redirect_pop redir (current_uiout, &fp);
14062 print_command_lines (current_uiout, tp->commands.get (), 2);
14063
14064 fp.puts (" end\n");
14065 }
14066
14067 if (tp->enable_state == bp_disabled)
14068 fp.puts ("disable $bpnum\n");
14069
14070 /* If this is a multi-location breakpoint, check if the locations
14071 should be individually disabled. Watchpoint locations are
14072 special, and not user visible. */
14073 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14074 {
14075 int n = 1;
14076
14077 for (bp_location *loc : tp->locations ())
14078 {
14079 if (!loc->enabled)
14080 fp.printf ("disable $bpnum.%d\n", n);
14081
14082 n++;
14083 }
14084 }
14085 }
14086
14087 if (extra_trace_bits && !default_collect.empty ())
14088 fp.printf ("set default-collect %s\n", default_collect.c_str ());
14089
14090 if (from_tty)
14091 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
14092 }
14093
14094 /* The `save breakpoints' command. */
14095
14096 static void
14097 save_breakpoints_command (const char *args, int from_tty)
14098 {
14099 save_breakpoints (args, from_tty, NULL);
14100 }
14101
14102 /* The `save tracepoints' command. */
14103
14104 static void
14105 save_tracepoints_command (const char *args, int from_tty)
14106 {
14107 save_breakpoints (args, from_tty, is_tracepoint);
14108 }
14109
14110 \f
14111 /* This help string is used to consolidate all the help string for specifying
14112 locations used by several commands. */
14113
14114 #define LOCATION_SPEC_HELP_STRING \
14115 "Linespecs are colon-separated lists of location parameters, such as\n\
14116 source filename, function name, label name, and line number.\n\
14117 Example: To specify the start of a label named \"the_top\" in the\n\
14118 function \"fact\" in the file \"factorial.c\", use\n\
14119 \"factorial.c:fact:the_top\".\n\
14120 \n\
14121 Address locations begin with \"*\" and specify an exact address in the\n\
14122 program. Example: To specify the fourth byte past the start function\n\
14123 \"main\", use \"*main + 4\".\n\
14124 \n\
14125 Explicit locations are similar to linespecs but use an option/argument\n\
14126 syntax to specify location parameters.\n\
14127 Example: To specify the start of the label named \"the_top\" in the\n\
14128 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
14129 -function fact -label the_top\".\n\
14130 \n\
14131 By default, a specified function is matched against the program's\n\
14132 functions in all scopes. For C++, this means in all namespaces and\n\
14133 classes. For Ada, this means in all packages. E.g., in C++,\n\
14134 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14135 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
14136 specified name as a complete fully-qualified name instead."
14137
14138 /* This help string is used for the break, hbreak, tbreak and thbreak
14139 commands. It is defined as a macro to prevent duplication.
14140 COMMAND should be a string constant containing the name of the
14141 command. */
14142
14143 #define BREAK_ARGS_HELP(command) \
14144 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14145 \t[-force-condition] [if CONDITION]\n\
14146 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14147 probe point. Accepted values are `-probe' (for a generic, automatically\n\
14148 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14149 `-probe-dtrace' (for a DTrace probe).\n\
14150 LOCATION may be a linespec, address, or explicit location as described\n\
14151 below.\n\
14152 \n\
14153 With no LOCATION, uses current execution address of the selected\n\
14154 stack frame. This is useful for breaking on return to a stack frame.\n\
14155 \n\
14156 THREADNUM is the number from \"info threads\".\n\
14157 CONDITION is a boolean expression.\n\
14158 \n\
14159 With the \"-force-condition\" flag, the condition is defined even when\n\
14160 it is invalid for all current locations.\n\
14161 \n" LOCATION_SPEC_HELP_STRING "\n\n\
14162 Multiple breakpoints at one place are permitted, and useful if their\n\
14163 conditions are different.\n\
14164 \n\
14165 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14166
14167 /* List of subcommands for "catch". */
14168 static struct cmd_list_element *catch_cmdlist;
14169
14170 /* List of subcommands for "tcatch". */
14171 static struct cmd_list_element *tcatch_cmdlist;
14172
14173 void
14174 add_catch_command (const char *name, const char *docstring,
14175 cmd_func_ftype *func,
14176 completer_ftype *completer,
14177 void *user_data_catch,
14178 void *user_data_tcatch)
14179 {
14180 struct cmd_list_element *command;
14181
14182 command = add_cmd (name, class_breakpoint, docstring,
14183 &catch_cmdlist);
14184 command->func = func;
14185 command->set_context (user_data_catch);
14186 set_cmd_completer (command, completer);
14187
14188 command = add_cmd (name, class_breakpoint, docstring,
14189 &tcatch_cmdlist);
14190 command->func = func;
14191 command->set_context (user_data_tcatch);
14192 set_cmd_completer (command, completer);
14193 }
14194
14195 /* Zero if any of the breakpoint's locations could be a location where
14196 functions have been inlined, nonzero otherwise. */
14197
14198 static int
14199 is_non_inline_function (struct breakpoint *b)
14200 {
14201 /* The shared library event breakpoint is set on the address of a
14202 non-inline function. */
14203 if (b->type == bp_shlib_event)
14204 return 1;
14205
14206 return 0;
14207 }
14208
14209 /* Nonzero if the specified PC cannot be a location where functions
14210 have been inlined. */
14211
14212 int
14213 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
14214 const target_waitstatus &ws)
14215 {
14216 for (breakpoint *b : all_breakpoints ())
14217 {
14218 if (!is_non_inline_function (b))
14219 continue;
14220
14221 for (bp_location *bl : b->locations ())
14222 {
14223 if (!bl->shlib_disabled
14224 && bpstat_check_location (bl, aspace, pc, ws))
14225 return 1;
14226 }
14227 }
14228
14229 return 0;
14230 }
14231
14232 /* Remove any references to OBJFILE which is going to be freed. */
14233
14234 void
14235 breakpoint_free_objfile (struct objfile *objfile)
14236 {
14237 for (bp_location *loc : all_bp_locations ())
14238 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
14239 loc->symtab = NULL;
14240 }
14241
14242 /* Chain containing all defined "enable breakpoint" subcommands. */
14243
14244 static struct cmd_list_element *enablebreaklist = NULL;
14245
14246 /* See breakpoint.h. */
14247
14248 cmd_list_element *commands_cmd_element = nullptr;
14249
14250 void _initialize_breakpoint ();
14251 void
14252 _initialize_breakpoint ()
14253 {
14254 struct cmd_list_element *c;
14255
14256 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14257 "breakpoint");
14258 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14259 "breakpoint");
14260 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14261 "breakpoint");
14262
14263 breakpoint_chain = 0;
14264 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14265 before a breakpoint is set. */
14266 breakpoint_count = 0;
14267
14268 tracepoint_count = 0;
14269
14270 add_com ("ignore", class_breakpoint, ignore_command, _("\
14271 Set ignore-count of breakpoint number N to COUNT.\n\
14272 Usage is `ignore N COUNT'."));
14273
14274 commands_cmd_element = add_com ("commands", class_breakpoint,
14275 commands_command, _("\
14276 Set commands to be executed when the given breakpoints are hit.\n\
14277 Give a space-separated breakpoint list as argument after \"commands\".\n\
14278 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14279 (e.g. `5-7').\n\
14280 With no argument, the targeted breakpoint is the last one set.\n\
14281 The commands themselves follow starting on the next line.\n\
14282 Type a line containing \"end\" to indicate the end of them.\n\
14283 Give \"silent\" as the first line to make the breakpoint silent;\n\
14284 then no output is printed when it is hit, except what the commands print."));
14285
14286 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14287 static std::string condition_command_help
14288 = gdb::option::build_help (_("\
14289 Specify breakpoint number N to break only if COND is true.\n\
14290 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
14291 is an expression to be evaluated whenever breakpoint N is reached.\n\
14292 \n\
14293 Options:\n\
14294 %OPTIONS%"), cc_opts);
14295
14296 c = add_com ("condition", class_breakpoint, condition_command,
14297 condition_command_help.c_str ());
14298 set_cmd_completer_handle_brkchars (c, condition_completer);
14299
14300 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
14301 Set a temporary breakpoint.\n\
14302 Like \"break\" except the breakpoint is only temporary,\n\
14303 so it will be deleted when hit. Equivalent to \"break\" followed\n\
14304 by using \"enable delete\" on the breakpoint number.\n\
14305 \n"
14306 BREAK_ARGS_HELP ("tbreak")));
14307 set_cmd_completer (c, location_completer);
14308
14309 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
14310 Set a hardware assisted breakpoint.\n\
14311 Like \"break\" except the breakpoint requires hardware support,\n\
14312 some target hardware may not have this support.\n\
14313 \n"
14314 BREAK_ARGS_HELP ("hbreak")));
14315 set_cmd_completer (c, location_completer);
14316
14317 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
14318 Set a temporary hardware assisted breakpoint.\n\
14319 Like \"hbreak\" except the breakpoint is only temporary,\n\
14320 so it will be deleted when hit.\n\
14321 \n"
14322 BREAK_ARGS_HELP ("thbreak")));
14323 set_cmd_completer (c, location_completer);
14324
14325 cmd_list_element *enable_cmd
14326 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14327 Enable all or some breakpoints.\n\
14328 Usage: enable [BREAKPOINTNUM]...\n\
14329 Give breakpoint numbers (separated by spaces) as arguments.\n\
14330 With no subcommand, breakpoints are enabled until you command otherwise.\n\
14331 This is used to cancel the effect of the \"disable\" command.\n\
14332 With a subcommand you can enable temporarily."),
14333 &enablelist, 1, &cmdlist);
14334
14335 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
14336
14337 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
14338 Enable all or some breakpoints.\n\
14339 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
14340 Give breakpoint numbers (separated by spaces) as arguments.\n\
14341 This is used to cancel the effect of the \"disable\" command.\n\
14342 May be abbreviated to simply \"enable\"."),
14343 &enablebreaklist, 1, &enablelist);
14344
14345 add_cmd ("once", no_class, enable_once_command, _("\
14346 Enable some breakpoints for one hit.\n\
14347 Usage: enable breakpoints once BREAKPOINTNUM...\n\
14348 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14349 &enablebreaklist);
14350
14351 add_cmd ("delete", no_class, enable_delete_command, _("\
14352 Enable some breakpoints and delete when hit.\n\
14353 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
14354 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14355 &enablebreaklist);
14356
14357 add_cmd ("count", no_class, enable_count_command, _("\
14358 Enable some breakpoints for COUNT hits.\n\
14359 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
14360 If a breakpoint is hit while enabled in this fashion,\n\
14361 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14362 &enablebreaklist);
14363
14364 add_cmd ("delete", no_class, enable_delete_command, _("\
14365 Enable some breakpoints and delete when hit.\n\
14366 Usage: enable delete BREAKPOINTNUM...\n\
14367 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14368 &enablelist);
14369
14370 add_cmd ("once", no_class, enable_once_command, _("\
14371 Enable some breakpoints for one hit.\n\
14372 Usage: enable once BREAKPOINTNUM...\n\
14373 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14374 &enablelist);
14375
14376 add_cmd ("count", no_class, enable_count_command, _("\
14377 Enable some breakpoints for COUNT hits.\n\
14378 Usage: enable count COUNT BREAKPOINTNUM...\n\
14379 If a breakpoint is hit while enabled in this fashion,\n\
14380 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14381 &enablelist);
14382
14383 cmd_list_element *disable_cmd
14384 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14385 Disable all or some breakpoints.\n\
14386 Usage: disable [BREAKPOINTNUM]...\n\
14387 Arguments are breakpoint numbers with spaces in between.\n\
14388 To disable all breakpoints, give no argument.\n\
14389 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
14390 &disablelist, 1, &cmdlist);
14391 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14392 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
14393
14394 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
14395 Disable all or some breakpoints.\n\
14396 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
14397 Arguments are breakpoint numbers with spaces in between.\n\
14398 To disable all breakpoints, give no argument.\n\
14399 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
14400 This command may be abbreviated \"disable\"."),
14401 &disablelist);
14402
14403 cmd_list_element *delete_cmd
14404 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14405 Delete all or some breakpoints.\n\
14406 Usage: delete [BREAKPOINTNUM]...\n\
14407 Arguments are breakpoint numbers with spaces in between.\n\
14408 To delete all breakpoints, give no argument.\n\
14409 \n\
14410 Also a prefix command for deletion of other GDB objects."),
14411 &deletelist, 1, &cmdlist);
14412 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14413 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
14414
14415 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
14416 Delete all or some breakpoints or auto-display expressions.\n\
14417 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
14418 Arguments are breakpoint numbers with spaces in between.\n\
14419 To delete all breakpoints, give no argument.\n\
14420 This command may be abbreviated \"delete\"."),
14421 &deletelist);
14422
14423 cmd_list_element *clear_cmd
14424 = add_com ("clear", class_breakpoint, clear_command, _("\
14425 Clear breakpoint at specified location.\n\
14426 Argument may be a linespec, explicit, or address location as described below.\n\
14427 \n\
14428 With no argument, clears all breakpoints in the line that the selected frame\n\
14429 is executing in.\n"
14430 "\n" LOCATION_SPEC_HELP_STRING "\n\n\
14431 See also the \"delete\" command which clears breakpoints by number."));
14432 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
14433
14434 cmd_list_element *break_cmd
14435 = add_com ("break", class_breakpoint, break_command, _("\
14436 Set breakpoint at specified location.\n"
14437 BREAK_ARGS_HELP ("break")));
14438 set_cmd_completer (break_cmd, location_completer);
14439
14440 add_com_alias ("b", break_cmd, class_run, 1);
14441 add_com_alias ("br", break_cmd, class_run, 1);
14442 add_com_alias ("bre", break_cmd, class_run, 1);
14443 add_com_alias ("brea", break_cmd, class_run, 1);
14444
14445 cmd_list_element *info_breakpoints_cmd
14446 = add_info ("breakpoints", info_breakpoints_command, _("\
14447 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
14448 The \"Type\" column indicates one of:\n\
14449 \tbreakpoint - normal breakpoint\n\
14450 \twatchpoint - watchpoint\n\
14451 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14452 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14453 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14454 address and file/line number respectively.\n\
14455 \n\
14456 Convenience variable \"$_\" and default examine address for \"x\"\n\
14457 are set to the address of the last breakpoint listed unless the command\n\
14458 is prefixed with \"server \".\n\n\
14459 Convenience variable \"$bpnum\" contains the number of the last\n\
14460 breakpoint set."));
14461
14462 add_info_alias ("b", info_breakpoints_cmd, 1);
14463
14464 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14465 Status of all breakpoints, or breakpoint number NUMBER.\n\
14466 The \"Type\" column indicates one of:\n\
14467 \tbreakpoint - normal breakpoint\n\
14468 \twatchpoint - watchpoint\n\
14469 \tlongjmp - internal breakpoint used to step through longjmp()\n\
14470 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14471 \tuntil - internal breakpoint used by the \"until\" command\n\
14472 \tfinish - internal breakpoint used by the \"finish\" command\n\
14473 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14474 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14475 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14476 address and file/line number respectively.\n\
14477 \n\
14478 Convenience variable \"$_\" and default examine address for \"x\"\n\
14479 are set to the address of the last breakpoint listed unless the command\n\
14480 is prefixed with \"server \".\n\n\
14481 Convenience variable \"$bpnum\" contains the number of the last\n\
14482 breakpoint set."),
14483 &maintenanceinfolist);
14484
14485 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
14486 Set catchpoints to catch events."),
14487 &catch_cmdlist,
14488 0/*allow-unknown*/, &cmdlist);
14489
14490 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
14491 Set temporary catchpoints to catch events."),
14492 &tcatch_cmdlist,
14493 0/*allow-unknown*/, &cmdlist);
14494
14495 const auto opts = make_watch_options_def_group (nullptr);
14496
14497 static const std::string watch_help = gdb::option::build_help (_("\
14498 Set a watchpoint for EXPRESSION.\n\
14499 Usage: watch [-location] EXPRESSION\n\
14500 \n\
14501 Options:\n\
14502 %OPTIONS%\n\
14503 \n\
14504 A watchpoint stops execution of your program whenever the value of\n\
14505 an expression changes."), opts);
14506 c = add_com ("watch", class_breakpoint, watch_command,
14507 watch_help.c_str ());
14508 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14509
14510 static const std::string rwatch_help = gdb::option::build_help (_("\
14511 Set a read watchpoint for EXPRESSION.\n\
14512 Usage: rwatch [-location] EXPRESSION\n\
14513 \n\
14514 Options:\n\
14515 %OPTIONS%\n\
14516 \n\
14517 A read watchpoint stops execution of your program whenever the value of\n\
14518 an expression is read."), opts);
14519 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14520 rwatch_help.c_str ());
14521 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14522
14523 static const std::string awatch_help = gdb::option::build_help (_("\
14524 Set an access watchpoint for EXPRESSION.\n\
14525 Usage: awatch [-location] EXPRESSION\n\
14526 \n\
14527 Options:\n\
14528 %OPTIONS%\n\
14529 \n\
14530 An access watchpoint stops execution of your program whenever the value\n\
14531 of an expression is either read or written."), opts);
14532 c = add_com ("awatch", class_breakpoint, awatch_command,
14533 awatch_help.c_str ());
14534 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14535
14536 add_info ("watchpoints", info_watchpoints_command, _("\
14537 Status of specified watchpoints (all watchpoints if no argument)."));
14538
14539 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14540 respond to changes - contrary to the description. */
14541 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14542 &can_use_hw_watchpoints, _("\
14543 Set debugger's willingness to use watchpoint hardware."), _("\
14544 Show debugger's willingness to use watchpoint hardware."), _("\
14545 If zero, gdb will not use hardware for new watchpoints, even if\n\
14546 such is available. (However, any hardware watchpoints that were\n\
14547 created before setting this to nonzero, will continue to use watchpoint\n\
14548 hardware.)"),
14549 NULL,
14550 show_can_use_hw_watchpoints,
14551 &setlist, &showlist);
14552
14553 can_use_hw_watchpoints = 1;
14554
14555 /* Tracepoint manipulation commands. */
14556
14557 cmd_list_element *trace_cmd
14558 = add_com ("trace", class_breakpoint, trace_command, _("\
14559 Set a tracepoint at specified location.\n\
14560 \n"
14561 BREAK_ARGS_HELP ("trace") "\n\
14562 Do \"help tracepoints\" for info on other tracepoint commands."));
14563 set_cmd_completer (trace_cmd, location_completer);
14564
14565 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14566 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14567 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14568 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
14569
14570 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
14571 Set a fast tracepoint at specified location.\n\
14572 \n"
14573 BREAK_ARGS_HELP ("ftrace") "\n\
14574 Do \"help tracepoints\" for info on other tracepoint commands."));
14575 set_cmd_completer (c, location_completer);
14576
14577 c = add_com ("strace", class_breakpoint, strace_command, _("\
14578 Set a static tracepoint at location or marker.\n\
14579 \n\
14580 strace [LOCATION] [if CONDITION]\n\
14581 LOCATION may be a linespec, explicit, or address location (described below) \n\
14582 or -m MARKER_ID.\n\n\
14583 If a marker id is specified, probe the marker with that name. With\n\
14584 no LOCATION, uses current execution address of the selected stack frame.\n\
14585 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14586 This collects arbitrary user data passed in the probe point call to the\n\
14587 tracing library. You can inspect it when analyzing the trace buffer,\n\
14588 by printing the $_sdata variable like any other convenience variable.\n\
14589 \n\
14590 CONDITION is a boolean expression.\n\
14591 \n" LOCATION_SPEC_HELP_STRING "\n\n\
14592 Multiple tracepoints at one place are permitted, and useful if their\n\
14593 conditions are different.\n\
14594 \n\
14595 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14596 Do \"help tracepoints\" for info on other tracepoint commands."));
14597 set_cmd_completer (c, location_completer);
14598
14599 cmd_list_element *info_tracepoints_cmd
14600 = add_info ("tracepoints", info_tracepoints_command, _("\
14601 Status of specified tracepoints (all tracepoints if no argument).\n\
14602 Convenience variable \"$tpnum\" contains the number of the\n\
14603 last tracepoint set."));
14604
14605 add_info_alias ("tp", info_tracepoints_cmd, 1);
14606
14607 cmd_list_element *delete_tracepoints_cmd
14608 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
14609 Delete specified tracepoints.\n\
14610 Arguments are tracepoint numbers, separated by spaces.\n\
14611 No argument means delete all tracepoints."),
14612 &deletelist);
14613 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
14614
14615 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14616 Disable specified tracepoints.\n\
14617 Arguments are tracepoint numbers, separated by spaces.\n\
14618 No argument means disable all tracepoints."),
14619 &disablelist);
14620 deprecate_cmd (c, "disable");
14621
14622 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14623 Enable specified tracepoints.\n\
14624 Arguments are tracepoint numbers, separated by spaces.\n\
14625 No argument means enable all tracepoints."),
14626 &enablelist);
14627 deprecate_cmd (c, "enable");
14628
14629 add_com ("passcount", class_trace, trace_pass_command, _("\
14630 Set the passcount for a tracepoint.\n\
14631 The trace will end when the tracepoint has been passed 'count' times.\n\
14632 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14633 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14634
14635 add_basic_prefix_cmd ("save", class_breakpoint,
14636 _("Save breakpoint definitions as a script."),
14637 &save_cmdlist,
14638 0/*allow-unknown*/, &cmdlist);
14639
14640 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14641 Save current breakpoint definitions as a script.\n\
14642 This includes all types of breakpoints (breakpoints, watchpoints,\n\
14643 catchpoints, tracepoints). Use the 'source' command in another debug\n\
14644 session to restore them."),
14645 &save_cmdlist);
14646 set_cmd_completer (c, filename_completer);
14647
14648 cmd_list_element *save_tracepoints_cmd
14649 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
14650 Save current tracepoint definitions as a script.\n\
14651 Use the 'source' command in another debug session to restore them."),
14652 &save_cmdlist);
14653 set_cmd_completer (save_tracepoints_cmd, filename_completer);
14654
14655 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
14656 deprecate_cmd (c, "save tracepoints");
14657
14658 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14659 _("\
14660 Breakpoint specific settings.\n\
14661 Configure various breakpoint-specific variables such as\n\
14662 pending breakpoint behavior."),
14663 _("\
14664 Breakpoint specific settings.\n\
14665 Configure various breakpoint-specific variables such as\n\
14666 pending breakpoint behavior."),
14667 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14668 &setlist, &showlist);
14669
14670 add_setshow_auto_boolean_cmd ("pending", no_class,
14671 &pending_break_support, _("\
14672 Set debugger's behavior regarding pending breakpoints."), _("\
14673 Show debugger's behavior regarding pending breakpoints."), _("\
14674 If on, an unrecognized breakpoint location will cause gdb to create a\n\
14675 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14676 an error. If auto, an unrecognized breakpoint location results in a\n\
14677 user-query to see if a pending breakpoint should be created."),
14678 NULL,
14679 show_pending_break_support,
14680 &breakpoint_set_cmdlist,
14681 &breakpoint_show_cmdlist);
14682
14683 pending_break_support = AUTO_BOOLEAN_AUTO;
14684
14685 add_setshow_boolean_cmd ("auto-hw", no_class,
14686 &automatic_hardware_breakpoints, _("\
14687 Set automatic usage of hardware breakpoints."), _("\
14688 Show automatic usage of hardware breakpoints."), _("\
14689 If set, the debugger will automatically use hardware breakpoints for\n\
14690 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14691 a warning will be emitted for such breakpoints."),
14692 NULL,
14693 show_automatic_hardware_breakpoints,
14694 &breakpoint_set_cmdlist,
14695 &breakpoint_show_cmdlist);
14696
14697 add_setshow_boolean_cmd ("always-inserted", class_support,
14698 &always_inserted_mode, _("\
14699 Set mode for inserting breakpoints."), _("\
14700 Show mode for inserting breakpoints."), _("\
14701 When this mode is on, breakpoints are inserted immediately as soon as\n\
14702 they're created, kept inserted even when execution stops, and removed\n\
14703 only when the user deletes them. When this mode is off (the default),\n\
14704 breakpoints are inserted only when execution continues, and removed\n\
14705 when execution stops."),
14706 NULL,
14707 &show_always_inserted_mode,
14708 &breakpoint_set_cmdlist,
14709 &breakpoint_show_cmdlist);
14710
14711 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14712 condition_evaluation_enums,
14713 &condition_evaluation_mode_1, _("\
14714 Set mode of breakpoint condition evaluation."), _("\
14715 Show mode of breakpoint condition evaluation."), _("\
14716 When this is set to \"host\", breakpoint conditions will be\n\
14717 evaluated on the host's side by GDB. When it is set to \"target\",\n\
14718 breakpoint conditions will be downloaded to the target (if the target\n\
14719 supports such feature) and conditions will be evaluated on the target's side.\n\
14720 If this is set to \"auto\" (default), this will be automatically set to\n\
14721 \"target\" if it supports condition evaluation, otherwise it will\n\
14722 be set to \"host\"."),
14723 &set_condition_evaluation_mode,
14724 &show_condition_evaluation_mode,
14725 &breakpoint_set_cmdlist,
14726 &breakpoint_show_cmdlist);
14727
14728 add_com ("break-range", class_breakpoint, break_range_command, _("\
14729 Set a breakpoint for an address range.\n\
14730 break-range START-LOCATION, END-LOCATION\n\
14731 where START-LOCATION and END-LOCATION can be one of the following:\n\
14732 LINENUM, for that line in the current file,\n\
14733 FILE:LINENUM, for that line in that file,\n\
14734 +OFFSET, for that number of lines after the current line\n\
14735 or the start of the range\n\
14736 FUNCTION, for the first line in that function,\n\
14737 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14738 *ADDRESS, for the instruction at that address.\n\
14739 \n\
14740 The breakpoint will stop execution of the inferior whenever it executes\n\
14741 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14742 range (including START-LOCATION and END-LOCATION)."));
14743
14744 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
14745 Set a dynamic printf at specified location.\n\
14746 dprintf location,format string,arg1,arg2,...\n\
14747 location may be a linespec, explicit, or address location.\n"
14748 "\n" LOCATION_SPEC_HELP_STRING));
14749 set_cmd_completer (c, location_completer);
14750
14751 add_setshow_enum_cmd ("dprintf-style", class_support,
14752 dprintf_style_enums, &dprintf_style, _("\
14753 Set the style of usage for dynamic printf."), _("\
14754 Show the style of usage for dynamic printf."), _("\
14755 This setting chooses how GDB will do a dynamic printf.\n\
14756 If the value is \"gdb\", then the printing is done by GDB to its own\n\
14757 console, as with the \"printf\" command.\n\
14758 If the value is \"call\", the print is done by calling a function in your\n\
14759 program; by default printf(), but you can choose a different function or\n\
14760 output stream by setting dprintf-function and dprintf-channel."),
14761 update_dprintf_commands, NULL,
14762 &setlist, &showlist);
14763
14764 add_setshow_string_cmd ("dprintf-function", class_support,
14765 &dprintf_function, _("\
14766 Set the function to use for dynamic printf."), _("\
14767 Show the function to use for dynamic printf."), NULL,
14768 update_dprintf_commands, NULL,
14769 &setlist, &showlist);
14770
14771 add_setshow_string_cmd ("dprintf-channel", class_support,
14772 &dprintf_channel, _("\
14773 Set the channel to use for dynamic printf."), _("\
14774 Show the channel to use for dynamic printf."), NULL,
14775 update_dprintf_commands, NULL,
14776 &setlist, &showlist);
14777
14778 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14779 &disconnected_dprintf, _("\
14780 Set whether dprintf continues after GDB disconnects."), _("\
14781 Show whether dprintf continues after GDB disconnects."), _("\
14782 Use this to let dprintf commands continue to hit and produce output\n\
14783 even if GDB disconnects or detaches from the target."),
14784 NULL,
14785 NULL,
14786 &setlist, &showlist);
14787
14788 add_com ("agent-printf", class_vars, agent_printf_command, _("\
14789 Target agent only formatted printing, like the C \"printf\" function.\n\
14790 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14791 This supports most C printf format specifications, like %s, %d, etc.\n\
14792 This is useful for formatted output in user-defined commands."));
14793
14794 automatic_hardware_breakpoints = true;
14795
14796 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14797 "breakpoint");
14798 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
14799 "breakpoint");
14800 }