]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/breakpoint.c
f0f5328fb5ea2aceec71c2054849a65713f04ef0
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "ui.h"
57 #include "valprint.h"
58 #include "jit.h"
59 #include "parser-defs.h"
60 #include "gdbsupport/gdb_regex.h"
61 #include "probe.h"
62 #include "cli/cli-utils.h"
63 #include "stack.h"
64 #include "ax-gdb.h"
65 #include "dummy-frame.h"
66 #include "interps.h"
67 #include "gdbsupport/format.h"
68 #include "thread-fsm.h"
69 #include "tid-parse.h"
70 #include "cli/cli-style.h"
71 #include "cli/cli-decode.h"
72 #include <unordered_set>
73
74 /* readline include files */
75 #include "readline/tilde.h"
76
77 /* readline defines this. */
78 #undef savestring
79
80 #include "mi/mi-common.h"
81 #include "extension.h"
82 #include <algorithm>
83 #include "progspace-and-thread.h"
84 #include "gdbsupport/array-view.h"
85 #include "gdbsupport/gdb_optional.h"
86 #include "gdbsupport/common-utils.h"
87
88 /* Prototypes for local functions. */
89
90 static void map_breakpoint_numbers (const char *,
91 gdb::function_view<void (breakpoint *)>);
92
93 static void
94 create_sals_from_location_spec_default (location_spec *locspec,
95 linespec_result *canonical);
96
97 static void create_breakpoints_sal (struct gdbarch *,
98 struct linespec_result *,
99 gdb::unique_xmalloc_ptr<char>,
100 gdb::unique_xmalloc_ptr<char>,
101 enum bptype,
102 enum bpdisp, int, int, int,
103 int,
104 int, int, int, unsigned);
105
106 static int can_use_hardware_watchpoint
107 (const std::vector<value_ref_ptr> &vals);
108
109 static void mention (const breakpoint *);
110
111 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
112
113 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
114
115 static struct breakpoint *
116 momentary_breakpoint_from_master (struct breakpoint *orig,
117 enum bptype type,
118 int loc_enabled, int thread);
119
120 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, bool);
121
122 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
123 CORE_ADDR bpaddr,
124 enum bptype bptype,
125 struct program_space *pspace);
126
127 static bool watchpoint_locations_match (const struct bp_location *loc1,
128 const struct bp_location *loc2);
129
130 static bool breakpoint_locations_match (const struct bp_location *loc1,
131 const struct bp_location *loc2,
132 bool sw_hw_bps_match = false);
133
134 static bool breakpoint_location_address_match (struct bp_location *bl,
135 const struct address_space *aspace,
136 CORE_ADDR addr);
137
138 static bool breakpoint_location_address_range_overlap (struct bp_location *,
139 const address_space *,
140 CORE_ADDR, int);
141
142 static int remove_breakpoint (struct bp_location *);
143 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
144
145 static enum print_stop_action print_bp_stop_message (bpstat *bs);
146
147 static int hw_breakpoint_used_count (void);
148
149 static int hw_watchpoint_use_count (struct breakpoint *);
150
151 static int hw_watchpoint_used_count_others (struct breakpoint *except,
152 enum bptype type,
153 int *other_type_used);
154
155 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
156 int count);
157
158 static void decref_bp_location (struct bp_location **loc);
159
160 static std::vector<symtab_and_line> bkpt_probe_decode_location_spec
161 (struct breakpoint *b,
162 location_spec *locspec,
163 struct program_space *search_pspace);
164
165 static bool bl_address_is_meaningful (bp_location *loc);
166
167 static int find_loc_num_by_location (const bp_location *loc);
168
169 /* update_global_location_list's modes of operation wrt to whether to
170 insert locations now. */
171 enum ugll_insert_mode
172 {
173 /* Don't insert any breakpoint locations into the inferior, only
174 remove already-inserted locations that no longer should be
175 inserted. Functions that delete a breakpoint or breakpoints
176 should specify this mode, so that deleting a breakpoint doesn't
177 have the side effect of inserting the locations of other
178 breakpoints that are marked not-inserted, but should_be_inserted
179 returns true on them.
180
181 This behavior is useful is situations close to tear-down -- e.g.,
182 after an exec, while the target still has execution, but
183 breakpoint shadows of the previous executable image should *NOT*
184 be restored to the new image; or before detaching, where the
185 target still has execution and wants to delete breakpoints from
186 GDB's lists, and all breakpoints had already been removed from
187 the inferior. */
188 UGLL_DONT_INSERT,
189
190 /* May insert breakpoints iff breakpoints_should_be_inserted_now
191 claims breakpoints should be inserted now. */
192 UGLL_MAY_INSERT,
193
194 /* Insert locations now, irrespective of
195 breakpoints_should_be_inserted_now. E.g., say all threads are
196 stopped right now, and the user did "continue". We need to
197 insert breakpoints _before_ resuming the target, but
198 UGLL_MAY_INSERT wouldn't insert them, because
199 breakpoints_should_be_inserted_now returns false at that point,
200 as no thread is running yet. */
201 UGLL_INSERT
202 };
203
204 /* Return a textual version of INSERT_MODE. */
205
206 static const char *
207 ugll_insert_mode_text (ugll_insert_mode insert_mode)
208 {
209 /* Make sure the compiler warns if a new ugll_insert_mode enumerator is added
210 but not handled here. */
211 DIAGNOSTIC_PUSH
212 DIAGNOSTIC_ERROR_SWITCH
213 switch (insert_mode)
214 {
215 case UGLL_DONT_INSERT:
216 return "UGLL_DONT_INSERT";
217 case UGLL_MAY_INSERT:
218 return "UGLL_MAY_INSERT";
219 case UGLL_INSERT:
220 return "UGLL_INSERT";
221 }
222 DIAGNOSTIC_POP
223
224 gdb_assert_not_reached ("must handle all enum values");
225 }
226
227 /* Return a textual version of REASON. */
228
229 static const char *
230 remove_bp_reason_str (remove_bp_reason reason)
231 {
232 /* Make sure the compiler warns if a new remove_bp_reason enumerator is added
233 but not handled here. */
234 DIAGNOSTIC_PUSH
235 DIAGNOSTIC_ERROR_SWITCH
236 switch (reason)
237 {
238 case REMOVE_BREAKPOINT:
239 return "regular remove";
240 case DETACH_BREAKPOINT:
241 return "detach";
242 }
243 DIAGNOSTIC_POP
244
245 gdb_assert_not_reached ("must handle all enum values");
246 }
247
248 /* Return a textual version of breakpoint location BL describing number,
249 location and address. */
250
251 static std::string
252 breakpoint_location_address_str (const bp_location *bl)
253 {
254 std::string str = string_printf ("Breakpoint %d (%s) at address %s",
255 bl->owner->number,
256 host_address_to_string (bl),
257 paddress (bl->gdbarch, bl->address));
258
259 std::string loc_string = bl->to_string ();
260 if (!loc_string.empty ())
261 str += string_printf (" %s", loc_string.c_str ());
262
263 return str;
264 }
265
266 static void update_global_location_list (enum ugll_insert_mode);
267
268 static void update_global_location_list_nothrow (enum ugll_insert_mode);
269
270 static void insert_breakpoint_locations (void);
271
272 static void trace_pass_command (const char *, int);
273
274 static void set_tracepoint_count (int num);
275
276 static bool is_masked_watchpoint (const struct breakpoint *b);
277
278 /* Return true if B refers to a static tracepoint set by marker ("-m"),
279 zero otherwise. */
280
281 static bool strace_marker_p (struct breakpoint *b);
282
283 static void bkpt_probe_create_sals_from_location_spec
284 (location_spec *locspec,
285 struct linespec_result *canonical);
286 static void tracepoint_probe_create_sals_from_location_spec
287 (location_spec *locspec,
288 struct linespec_result *canonical);
289
290 const struct breakpoint_ops code_breakpoint_ops =
291 {
292 create_sals_from_location_spec_default,
293 create_breakpoints_sal,
294 };
295
296 /* Breakpoints set on probes. */
297 static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
298 {
299 bkpt_probe_create_sals_from_location_spec,
300 create_breakpoints_sal,
301 };
302
303 /* Tracepoints set on probes. */
304 static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
305 {
306 tracepoint_probe_create_sals_from_location_spec,
307 create_breakpoints_sal,
308 };
309
310 /* Implementation of abstract dtors. These must exist to satisfy the
311 linker. */
312
313 breakpoint::~breakpoint ()
314 {
315 }
316
317 code_breakpoint::~code_breakpoint ()
318 {
319 }
320
321 catchpoint::~catchpoint ()
322 {
323 }
324
325 /* The structure to be used in regular breakpoints. */
326 struct ordinary_breakpoint : public code_breakpoint
327 {
328 using code_breakpoint::code_breakpoint;
329
330 int resources_needed (const struct bp_location *) override;
331 enum print_stop_action print_it (const bpstat *bs) const override;
332 void print_mention () const override;
333 void print_recreate (struct ui_file *fp) const override;
334 };
335
336 /* Internal breakpoints. These typically have a lifetime the same as
337 the program, and they end up installed on the breakpoint chain with
338 a negative breakpoint number. They're visible in "maint info
339 breakpoints", but not "info breakpoints". */
340 struct internal_breakpoint : public code_breakpoint
341 {
342 internal_breakpoint (struct gdbarch *gdbarch,
343 enum bptype type, CORE_ADDR address)
344 : code_breakpoint (gdbarch, type)
345 {
346 symtab_and_line sal;
347 sal.pc = address;
348 sal.section = find_pc_overlay (sal.pc);
349 sal.pspace = current_program_space;
350 add_location (sal);
351
352 pspace = current_program_space;
353 disposition = disp_donttouch;
354 }
355
356 void re_set () override;
357 void check_status (struct bpstat *bs) override;
358 enum print_stop_action print_it (const bpstat *bs) const override;
359 void print_mention () const override;
360 };
361
362 /* Momentary breakpoints. These typically have a lifetime of some run
363 control command only, are always thread-specific, and have 0 for
364 breakpoint number. I.e., there can be many momentary breakpoints
365 on the breakpoint chain and they all same the same number (zero).
366 They're visible in "maint info breakpoints", but not "info
367 breakpoints". */
368 struct momentary_breakpoint : public code_breakpoint
369 {
370 momentary_breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
371 program_space *pspace_,
372 const struct frame_id &frame_id_,
373 int thread_)
374 : code_breakpoint (gdbarch_, bptype)
375 {
376 /* If FRAME_ID is valid, it should be a real frame, not an inlined
377 or tail-called one. */
378 gdb_assert (!frame_id_artificial_p (frame_id));
379
380 /* Momentary breakpoints are always thread-specific. */
381 gdb_assert (thread_ > 0);
382
383 pspace = pspace_;
384 enable_state = bp_enabled;
385 disposition = disp_donttouch;
386 frame_id = frame_id_;
387 thread = thread_;
388
389 /* The inferior should have been set by the parent constructor. */
390 gdb_assert (inferior == -1);
391 }
392
393 void re_set () override;
394 void check_status (struct bpstat *bs) override;
395 enum print_stop_action print_it (const bpstat *bs) const override;
396 void print_mention () const override;
397 };
398
399 /* DPrintf breakpoints. */
400 struct dprintf_breakpoint : public ordinary_breakpoint
401 {
402 using ordinary_breakpoint::ordinary_breakpoint;
403
404 void re_set () override;
405 int breakpoint_hit (const struct bp_location *bl,
406 const address_space *aspace,
407 CORE_ADDR bp_addr,
408 const target_waitstatus &ws) override;
409 void print_recreate (struct ui_file *fp) const override;
410 void after_condition_true (struct bpstat *bs) override;
411 };
412
413 /* Ranged breakpoints. */
414 struct ranged_breakpoint : public ordinary_breakpoint
415 {
416 explicit ranged_breakpoint (struct gdbarch *gdbarch,
417 const symtab_and_line &sal_start,
418 int length,
419 location_spec_up start_locspec,
420 location_spec_up end_locspec)
421 : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
422 {
423 bp_location *bl = add_location (sal_start);
424 bl->length = length;
425
426 disposition = disp_donttouch;
427
428 locspec = std::move (start_locspec);
429 locspec_range_end = std::move (end_locspec);
430 }
431
432 int breakpoint_hit (const struct bp_location *bl,
433 const address_space *aspace,
434 CORE_ADDR bp_addr,
435 const target_waitstatus &ws) override;
436 int resources_needed (const struct bp_location *) override;
437 enum print_stop_action print_it (const bpstat *bs) const override;
438 bool print_one (const bp_location **) const override;
439 void print_one_detail (struct ui_out *) const override;
440 void print_mention () const override;
441 void print_recreate (struct ui_file *fp) const override;
442 };
443
444 /* Static tracepoints with marker (`-m'). */
445 struct static_marker_tracepoint : public tracepoint
446 {
447 using tracepoint::tracepoint;
448
449 std::vector<symtab_and_line> decode_location_spec
450 (struct location_spec *locspec,
451 struct program_space *search_pspace) override;
452 };
453
454 /* The style in which to perform a dynamic printf. This is a user
455 option because different output options have different tradeoffs;
456 if GDB does the printing, there is better error handling if there
457 is a problem with any of the arguments, but using an inferior
458 function lets you have special-purpose printers and sending of
459 output to the same place as compiled-in print functions. */
460
461 static const char dprintf_style_gdb[] = "gdb";
462 static const char dprintf_style_call[] = "call";
463 static const char dprintf_style_agent[] = "agent";
464 static const char *const dprintf_style_enums[] = {
465 dprintf_style_gdb,
466 dprintf_style_call,
467 dprintf_style_agent,
468 NULL
469 };
470 static const char *dprintf_style = dprintf_style_gdb;
471
472 /* The function to use for dynamic printf if the preferred style is to
473 call into the inferior. The value is simply a string that is
474 copied into the command, so it can be anything that GDB can
475 evaluate to a callable address, not necessarily a function name. */
476
477 static std::string dprintf_function = "printf";
478
479 /* The channel to use for dynamic printf if the preferred style is to
480 call into the inferior; if a nonempty string, it will be passed to
481 the call as the first argument, with the format string as the
482 second. As with the dprintf function, this can be anything that
483 GDB knows how to evaluate, so in addition to common choices like
484 "stderr", this could be an app-specific expression like
485 "mystreams[curlogger]". */
486
487 static std::string dprintf_channel;
488
489 /* True if dprintf commands should continue to operate even if GDB
490 has disconnected. */
491 static bool disconnected_dprintf = true;
492
493 struct command_line *
494 breakpoint_commands (struct breakpoint *b)
495 {
496 return b->commands ? b->commands.get () : NULL;
497 }
498
499 /* Flag indicating that a command has proceeded the inferior past the
500 current breakpoint. */
501
502 static bool breakpoint_proceeded;
503
504 const char *
505 bpdisp_text (enum bpdisp disp)
506 {
507 /* NOTE: the following values are a part of MI protocol and
508 represent values of 'disp' field returned when inferior stops at
509 a breakpoint. */
510 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
511
512 return bpdisps[(int) disp];
513 }
514
515 /* Prototypes for exported functions. */
516 /* If FALSE, gdb will not use hardware support for watchpoints, even
517 if such is available. */
518 static int can_use_hw_watchpoints;
519
520 static void
521 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
522 struct cmd_list_element *c,
523 const char *value)
524 {
525 gdb_printf (file,
526 _("Debugger's willingness to use "
527 "watchpoint hardware is %s.\n"),
528 value);
529 }
530
531 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
532 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
533 for unrecognized breakpoint locations.
534 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
535 static enum auto_boolean pending_break_support;
536 static void
537 show_pending_break_support (struct ui_file *file, int from_tty,
538 struct cmd_list_element *c,
539 const char *value)
540 {
541 gdb_printf (file,
542 _("Debugger's behavior regarding "
543 "pending breakpoints is %s.\n"),
544 value);
545 }
546
547 /* If true, gdb will automatically use hardware breakpoints for breakpoints
548 set with "break" but falling in read-only memory.
549 If false, gdb will warn about such breakpoints, but won't automatically
550 use hardware breakpoints. */
551 static bool automatic_hardware_breakpoints;
552 static void
553 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
554 struct cmd_list_element *c,
555 const char *value)
556 {
557 gdb_printf (file,
558 _("Automatic usage of hardware breakpoints is %s.\n"),
559 value);
560 }
561
562 /* If on, GDB keeps breakpoints inserted even if the inferior is
563 stopped, and immediately inserts any new breakpoints as soon as
564 they're created. If off (default), GDB keeps breakpoints off of
565 the target as long as possible. That is, it delays inserting
566 breakpoints until the next resume, and removes them again when the
567 target fully stops. This is a bit safer in case GDB crashes while
568 processing user input. */
569 static bool always_inserted_mode = false;
570
571 static void
572 show_always_inserted_mode (struct ui_file *file, int from_tty,
573 struct cmd_list_element *c, const char *value)
574 {
575 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
576 value);
577 }
578
579 /* True if breakpoint debug output is enabled. */
580 static bool debug_breakpoint = false;
581
582 /* Print a "breakpoint" debug statement. */
583 #define breakpoint_debug_printf(fmt, ...) \
584 debug_prefixed_printf_cond (debug_breakpoint, "breakpoint", fmt, \
585 ##__VA_ARGS__)
586
587 /* "show debug breakpoint" implementation. */
588 static void
589 show_debug_breakpoint (struct ui_file *file, int from_tty,
590 struct cmd_list_element *c, const char *value)
591 {
592 gdb_printf (file, _("Breakpoint location debugging is %s.\n"), value);
593 }
594
595 /* See breakpoint.h. */
596
597 int
598 breakpoints_should_be_inserted_now (void)
599 {
600 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
601 {
602 /* If breakpoints are global, they should be inserted even if no
603 thread under gdb's control is running, or even if there are
604 no threads under GDB's control yet. */
605 return 1;
606 }
607 else
608 {
609 if (always_inserted_mode)
610 {
611 /* The user wants breakpoints inserted even if all threads
612 are stopped. */
613 return 1;
614 }
615
616 for (inferior *inf : all_inferiors ())
617 if (inf->has_execution ()
618 && threads_are_executing (inf->process_target ()))
619 return 1;
620
621 /* Don't remove breakpoints yet if, even though all threads are
622 stopped, we still have events to process. */
623 for (thread_info *tp : all_non_exited_threads ())
624 if (tp->resumed () && tp->has_pending_waitstatus ())
625 return 1;
626 }
627 return 0;
628 }
629
630 static const char condition_evaluation_both[] = "host or target";
631
632 /* Modes for breakpoint condition evaluation. */
633 static const char condition_evaluation_auto[] = "auto";
634 static const char condition_evaluation_host[] = "host";
635 static const char condition_evaluation_target[] = "target";
636 static const char *const condition_evaluation_enums[] = {
637 condition_evaluation_auto,
638 condition_evaluation_host,
639 condition_evaluation_target,
640 NULL
641 };
642
643 /* Global that holds the current mode for breakpoint condition evaluation. */
644 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
645
646 /* Global that we use to display information to the user (gets its value from
647 condition_evaluation_mode_1. */
648 static const char *condition_evaluation_mode = condition_evaluation_auto;
649
650 /* Translate a condition evaluation mode MODE into either "host"
651 or "target". This is used mostly to translate from "auto" to the
652 real setting that is being used. It returns the translated
653 evaluation mode. */
654
655 static const char *
656 translate_condition_evaluation_mode (const char *mode)
657 {
658 if (mode == condition_evaluation_auto)
659 {
660 if (target_supports_evaluation_of_breakpoint_conditions ())
661 return condition_evaluation_target;
662 else
663 return condition_evaluation_host;
664 }
665 else
666 return mode;
667 }
668
669 /* Discovers what condition_evaluation_auto translates to. */
670
671 static const char *
672 breakpoint_condition_evaluation_mode (void)
673 {
674 return translate_condition_evaluation_mode (condition_evaluation_mode);
675 }
676
677 /* Return true if GDB should evaluate breakpoint conditions or false
678 otherwise. */
679
680 static bool
681 gdb_evaluates_breakpoint_condition_p (void)
682 {
683 const char *mode = breakpoint_condition_evaluation_mode ();
684
685 return (mode == condition_evaluation_host);
686 }
687
688 /* Are we executing breakpoint commands? */
689 static int executing_breakpoint_commands;
690
691 /* Are overlay event breakpoints enabled? */
692 static int overlay_events_enabled;
693
694 /* See description in breakpoint.h. */
695 bool target_exact_watchpoints = false;
696
697 /* Chains of all breakpoints defined. */
698
699 static intrusive_list<breakpoint> breakpoint_chain;
700
701 /* See breakpoint.h. */
702
703 breakpoint_range
704 all_breakpoints ()
705 {
706 return breakpoint_range (breakpoint_chain.begin (), breakpoint_chain.end ());
707 }
708
709 /* See breakpoint.h. */
710
711 breakpoint_safe_range
712 all_breakpoints_safe ()
713 {
714 return breakpoint_safe_range (all_breakpoints ());
715 }
716
717 /* See breakpoint.h. */
718
719 tracepoint_range
720 all_tracepoints ()
721 {
722 return tracepoint_range (tracepoint_iterator (breakpoint_chain.begin ()),
723 tracepoint_iterator (breakpoint_chain.end ()));
724 }
725
726 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
727
728 static std::vector<bp_location *> bp_locations;
729
730 /* See breakpoint.h. */
731
732 const std::vector<bp_location *> &
733 all_bp_locations ()
734 {
735 return bp_locations;
736 }
737
738 /* Range to iterate over breakpoint locations at a given address. */
739
740 struct bp_locations_at_addr_range
741 {
742 using iterator = std::vector<bp_location *>::iterator;
743
744 bp_locations_at_addr_range (CORE_ADDR addr)
745 {
746 struct compare
747 {
748 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
749 { return loc->address < addr_; }
750
751 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
752 { return addr_ < loc->address; }
753 };
754
755 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
756 addr, compare ());
757
758 m_begin = it_pair.first;
759 m_end = it_pair.second;
760 }
761
762 iterator begin () const
763 { return m_begin; }
764
765 iterator end () const
766 { return m_end; }
767
768 private:
769 iterator m_begin;
770 iterator m_end;
771 };
772
773 /* Return a range to iterate over all breakpoint locations exactly at address
774 ADDR.
775
776 If it's needed to iterate multiple times on the same range, it's possible
777 to save the range in a local variable and use it multiple times:
778
779 auto range = all_bp_locations_at_addr (addr);
780
781 for (bp_location *loc : range)
782 // use loc
783
784 for (bp_location *loc : range)
785 // use loc
786
787 This saves a bit of time, as it avoids re-doing the binary searches to find
788 the range's boundaries. Just remember not to change the bp_locations vector
789 in the mean time, as it could make the range's iterators stale. */
790
791 static bp_locations_at_addr_range
792 all_bp_locations_at_addr (CORE_ADDR addr)
793 {
794 return bp_locations_at_addr_range (addr);
795 }
796
797 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
798 ADDRESS for the current elements of BP_LOCATIONS which get a valid
799 result from bp_location_has_shadow. You can use it for roughly
800 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
801 an address you need to read. */
802
803 static CORE_ADDR bp_locations_placed_address_before_address_max;
804
805 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
806 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
807 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
808 You can use it for roughly limiting the subrange of BP_LOCATIONS to
809 scan for shadow bytes for an address you need to read. */
810
811 static CORE_ADDR bp_locations_shadow_len_after_address_max;
812
813 /* The locations that no longer correspond to any breakpoint, unlinked
814 from the bp_locations array, but for which a hit may still be
815 reported by a target. */
816 static std::vector<bp_location *> moribund_locations;
817
818 /* Number of last breakpoint made. */
819
820 static int breakpoint_count;
821
822 /* The value of `breakpoint_count' before the last command that
823 created breakpoints. If the last (break-like) command created more
824 than one breakpoint, then the difference between BREAKPOINT_COUNT
825 and PREV_BREAKPOINT_COUNT is more than one. */
826 static int prev_breakpoint_count;
827
828 /* Number of last tracepoint made. */
829
830 static int tracepoint_count;
831
832 static struct cmd_list_element *breakpoint_set_cmdlist;
833 static struct cmd_list_element *breakpoint_show_cmdlist;
834 struct cmd_list_element *save_cmdlist;
835
836 /* Return whether a breakpoint is an active enabled breakpoint. */
837 static bool
838 breakpoint_enabled (struct breakpoint *b)
839 {
840 return (b->enable_state == bp_enabled);
841 }
842
843 /* Set breakpoint count to NUM. */
844
845 static void
846 set_breakpoint_count (int num)
847 {
848 prev_breakpoint_count = breakpoint_count;
849 breakpoint_count = num;
850 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
851 }
852
853 /* Used by `start_rbreak_breakpoints' below, to record the current
854 breakpoint count before "rbreak" creates any breakpoint. */
855 static int rbreak_start_breakpoint_count;
856
857 /* Called at the start an "rbreak" command to record the first
858 breakpoint made. */
859
860 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
861 {
862 rbreak_start_breakpoint_count = breakpoint_count;
863 }
864
865 /* Called at the end of an "rbreak" command to record the last
866 breakpoint made. */
867
868 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
869 {
870 prev_breakpoint_count = rbreak_start_breakpoint_count;
871 }
872
873 /* Used in run_command to zero the hit count when a new run starts. */
874
875 void
876 clear_breakpoint_hit_counts (void)
877 {
878 for (breakpoint &b : all_breakpoints ())
879 b.hit_count = 0;
880 }
881
882 \f
883 /* Return the breakpoint with the specified number, or NULL
884 if the number does not refer to an existing breakpoint. */
885
886 struct breakpoint *
887 get_breakpoint (int num)
888 {
889 for (breakpoint &b : all_breakpoints ())
890 if (b.number == num)
891 return &b;
892
893 return nullptr;
894 }
895
896 /* Return TRUE if NUM refer to an existing breakpoint that has
897 multiple code locations. */
898
899 static bool
900 has_multiple_locations (int num)
901 {
902 for (breakpoint &b : all_breakpoints ())
903 if (b.number == num)
904 return b.has_multiple_locations ();
905
906 return false;
907 }
908
909 \f
910
911 /* Mark locations as "conditions have changed" in case the target supports
912 evaluating conditions on its side. */
913
914 static void
915 mark_breakpoint_modified (struct breakpoint *b)
916 {
917 /* This is only meaningful if the target is
918 evaluating conditions and if the user has
919 opted for condition evaluation on the target's
920 side. */
921 if (gdb_evaluates_breakpoint_condition_p ()
922 || !target_supports_evaluation_of_breakpoint_conditions ())
923 return;
924
925 if (!is_breakpoint (b))
926 return;
927
928 for (bp_location &loc : b->locations ())
929 loc.condition_changed = condition_modified;
930 }
931
932 /* Mark location as "conditions have changed" in case the target supports
933 evaluating conditions on its side. */
934
935 static void
936 mark_breakpoint_location_modified (struct bp_location *loc)
937 {
938 /* This is only meaningful if the target is
939 evaluating conditions and if the user has
940 opted for condition evaluation on the target's
941 side. */
942 if (gdb_evaluates_breakpoint_condition_p ()
943 || !target_supports_evaluation_of_breakpoint_conditions ())
944
945 return;
946
947 if (!is_breakpoint (loc->owner))
948 return;
949
950 loc->condition_changed = condition_modified;
951 }
952
953 /* Sets the condition-evaluation mode using the static global
954 condition_evaluation_mode. */
955
956 static void
957 set_condition_evaluation_mode (const char *args, int from_tty,
958 struct cmd_list_element *c)
959 {
960 const char *old_mode, *new_mode;
961
962 if ((condition_evaluation_mode_1 == condition_evaluation_target)
963 && !target_supports_evaluation_of_breakpoint_conditions ())
964 {
965 condition_evaluation_mode_1 = condition_evaluation_mode;
966 warning (_("Target does not support breakpoint condition evaluation.\n"
967 "Using host evaluation mode instead."));
968 return;
969 }
970
971 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
972 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
973
974 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
975 settings was "auto". */
976 condition_evaluation_mode = condition_evaluation_mode_1;
977
978 /* Only update the mode if the user picked a different one. */
979 if (new_mode != old_mode)
980 {
981 /* If the user switched to a different evaluation mode, we
982 need to synch the changes with the target as follows:
983
984 "host" -> "target": Send all (valid) conditions to the target.
985 "target" -> "host": Remove all the conditions from the target.
986 */
987
988 if (new_mode == condition_evaluation_target)
989 {
990 /* Mark everything modified and synch conditions with the
991 target. */
992 for (bp_location *loc : all_bp_locations ())
993 mark_breakpoint_location_modified (loc);
994 }
995 else
996 {
997 /* Manually mark non-duplicate locations to synch conditions
998 with the target. We do this to remove all the conditions the
999 target knows about. */
1000 for (bp_location *loc : all_bp_locations ())
1001 if (is_breakpoint (loc->owner) && loc->inserted)
1002 loc->needs_update = 1;
1003 }
1004
1005 /* Do the update. */
1006 update_global_location_list (UGLL_MAY_INSERT);
1007 }
1008
1009 return;
1010 }
1011
1012 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
1013 what "auto" is translating to. */
1014
1015 static void
1016 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
1017 struct cmd_list_element *c, const char *value)
1018 {
1019 if (condition_evaluation_mode == condition_evaluation_auto)
1020 gdb_printf (file,
1021 _("Breakpoint condition evaluation "
1022 "mode is %s (currently %s).\n"),
1023 value,
1024 breakpoint_condition_evaluation_mode ());
1025 else
1026 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
1027 value);
1028 }
1029
1030 /* Parse COND_STRING in the context of LOC and set as the condition
1031 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
1032 the number of LOC within its owner. In case of parsing error, mark
1033 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
1034
1035 static void
1036 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
1037 int bp_num, int loc_num)
1038 {
1039 bool has_junk = false;
1040 try
1041 {
1042 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
1043 block_for_pc (loc->address), 0);
1044 if (*cond_string != 0)
1045 has_junk = true;
1046 else
1047 {
1048 loc->cond = std::move (new_exp);
1049 if (loc->disabled_by_cond && loc->enabled)
1050 gdb_printf (_("Breakpoint %d's condition is now valid at "
1051 "location %d, enabling.\n"),
1052 bp_num, loc_num);
1053
1054 loc->disabled_by_cond = false;
1055 }
1056 }
1057 catch (const gdb_exception_error &e)
1058 {
1059 if (loc->enabled)
1060 {
1061 /* Warn if a user-enabled location is now becoming disabled-by-cond.
1062 BP_NUM is 0 if the breakpoint is being defined for the first
1063 time using the "break ... if ..." command, and non-zero if
1064 already defined. */
1065 if (bp_num != 0)
1066 warning (_("failed to validate condition at location %d.%d, "
1067 "disabling:\n %s"), bp_num, loc_num, e.what ());
1068 else
1069 warning (_("failed to validate condition at location %d, "
1070 "disabling:\n %s"), loc_num, e.what ());
1071 }
1072
1073 loc->disabled_by_cond = true;
1074 }
1075
1076 if (has_junk)
1077 error (_("Garbage '%s' follows condition"), cond_string);
1078 }
1079
1080 /* See breakpoint.h. */
1081
1082 void
1083 notify_breakpoint_modified (breakpoint *b)
1084 {
1085 interps_notify_breakpoint_modified (b);
1086 gdb::observers::breakpoint_modified.notify (b);
1087 }
1088
1089 void
1090 set_breakpoint_condition (struct breakpoint *b, const char *exp,
1091 int from_tty, bool force)
1092 {
1093 if (*exp == 0)
1094 {
1095 b->cond_string.reset ();
1096
1097 if (is_watchpoint (b))
1098 gdb::checked_static_cast<watchpoint *> (b)->cond_exp.reset ();
1099 else
1100 {
1101 int loc_num = 1;
1102 for (bp_location &loc : b->locations ())
1103 {
1104 loc.cond.reset ();
1105 if (loc.disabled_by_cond && loc.enabled)
1106 gdb_printf (_("Breakpoint %d's condition is now valid at "
1107 "location %d, enabling.\n"),
1108 b->number, loc_num);
1109 loc.disabled_by_cond = false;
1110 loc_num++;
1111
1112 /* No need to free the condition agent expression
1113 bytecode (if we have one). We will handle this
1114 when we go through update_global_location_list. */
1115 }
1116 }
1117
1118 if (from_tty)
1119 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
1120 }
1121 else
1122 {
1123 if (is_watchpoint (b))
1124 {
1125 innermost_block_tracker tracker;
1126 const char *arg = exp;
1127 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
1128 if (*arg != 0)
1129 error (_("Junk at end of expression"));
1130 watchpoint *w = gdb::checked_static_cast<watchpoint *> (b);
1131 w->cond_exp = std::move (new_exp);
1132 w->cond_exp_valid_block = tracker.block ();
1133 }
1134 else
1135 {
1136 /* Parse and set condition expressions. We make two passes.
1137 In the first, we parse the condition string to see if it
1138 is valid in at least one location. If so, the condition
1139 would be accepted. So we go ahead and set the locations'
1140 conditions. In case no valid case is found, we throw
1141 the error and the condition string will be rejected.
1142 This two-pass approach is taken to avoid setting the
1143 state of locations in case of a reject. */
1144 for (const bp_location &loc : b->locations ())
1145 {
1146 try
1147 {
1148 const char *arg = exp;
1149 parse_exp_1 (&arg, loc.address,
1150 block_for_pc (loc.address), 0);
1151 if (*arg != 0)
1152 error (_("Junk at end of expression"));
1153 break;
1154 }
1155 catch (const gdb_exception_error &e)
1156 {
1157 /* Condition string is invalid. If this happens to
1158 be the last loc, abandon (if not forced) or continue
1159 (if forced). */
1160 if (&loc == &b->last_loc () && !force)
1161 throw;
1162 }
1163 }
1164
1165 /* If we reach here, the condition is valid at some locations. */
1166 int loc_num = 1;
1167 for (bp_location &loc : b->locations ())
1168 {
1169 set_breakpoint_location_condition (exp, &loc, b->number, loc_num);
1170 loc_num++;
1171 }
1172 }
1173
1174 /* We know that the new condition parsed successfully. The
1175 condition string of the breakpoint can be safely updated. */
1176 b->cond_string = make_unique_xstrdup (exp);
1177 b->condition_not_parsed = 0;
1178 }
1179 mark_breakpoint_modified (b);
1180
1181 notify_breakpoint_modified (b);
1182 }
1183
1184 /* See breakpoint.h. */
1185
1186 void
1187 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1188 bool force)
1189 {
1190 for (breakpoint &b : all_breakpoints ())
1191 if (b.number == bpnum)
1192 {
1193 /* Check if this breakpoint has a "stop" method implemented in an
1194 extension language. This method and conditions entered into GDB
1195 from the CLI are mutually exclusive. */
1196 const struct extension_language_defn *extlang
1197 = get_breakpoint_cond_ext_lang (&b, EXT_LANG_NONE);
1198
1199 if (extlang != NULL)
1200 {
1201 error (_("Only one stop condition allowed. There is currently"
1202 " a %s stop condition defined for this breakpoint."),
1203 ext_lang_capitalized_name (extlang));
1204 }
1205 set_breakpoint_condition (&b, exp, from_tty, force);
1206
1207 if (is_breakpoint (&b))
1208 update_global_location_list (UGLL_MAY_INSERT);
1209
1210 return;
1211 }
1212
1213 error (_("No breakpoint number %d."), bpnum);
1214 }
1215
1216 /* The options for the "condition" command. */
1217
1218 struct condition_command_opts
1219 {
1220 /* For "-force". */
1221 bool force_condition = false;
1222 };
1223
1224 static const gdb::option::option_def condition_command_option_defs[] = {
1225
1226 gdb::option::flag_option_def<condition_command_opts> {
1227 "force",
1228 [] (condition_command_opts *opts) { return &opts->force_condition; },
1229 N_("Set the condition even if it is invalid for all current locations."),
1230 },
1231
1232 };
1233
1234 /* Create an option_def_group for the "condition" options, with
1235 CC_OPTS as context. */
1236
1237 static inline gdb::option::option_def_group
1238 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1239 {
1240 return {{condition_command_option_defs}, cc_opts};
1241 }
1242
1243 /* Completion for the "condition" command. */
1244
1245 static void
1246 condition_completer (struct cmd_list_element *cmd,
1247 completion_tracker &tracker,
1248 const char *text, const char * /*word*/)
1249 {
1250 bool has_no_arguments = (*text == '\0');
1251 condition_command_opts cc_opts;
1252 const auto group = make_condition_command_options_def_group (&cc_opts);
1253 if (gdb::option::complete_options
1254 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1255 return;
1256
1257 text = skip_spaces (text);
1258 const char *space = skip_to_space (text);
1259 if (*space == '\0')
1260 {
1261 int len;
1262
1263 if (text[0] == '$')
1264 {
1265 tracker.advance_custom_word_point_by (1);
1266 /* We don't support completion of history indices. */
1267 if (!isdigit (text[1]))
1268 complete_internalvar (tracker, &text[1]);
1269 return;
1270 }
1271
1272 /* Suggest the "-force" flag if no arguments are given. If
1273 arguments were passed, they either already include the flag,
1274 or we are beyond the point of suggesting it because it's
1275 positionally the first argument. */
1276 if (has_no_arguments)
1277 gdb::option::complete_on_all_options (tracker, group);
1278
1279 /* We're completing the breakpoint number. */
1280 len = strlen (text);
1281
1282 for (breakpoint &b : all_breakpoints ())
1283 {
1284 char number[50];
1285
1286 xsnprintf (number, sizeof (number), "%d", b.number);
1287
1288 if (strncmp (number, text, len) == 0)
1289 tracker.add_completion (make_unique_xstrdup (number));
1290 }
1291
1292 return;
1293 }
1294
1295 /* We're completing the expression part. Skip the breakpoint num. */
1296 const char *exp_start = skip_spaces (space);
1297 tracker.advance_custom_word_point_by (exp_start - text);
1298 text = exp_start;
1299 const char *word = advance_to_expression_complete_word_point (tracker, text);
1300 expression_completer (cmd, tracker, text, word);
1301 }
1302
1303 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1304
1305 static void
1306 condition_command (const char *arg, int from_tty)
1307 {
1308 const char *p;
1309 int bnum;
1310
1311 if (arg == 0)
1312 error_no_arg (_("breakpoint number"));
1313
1314 p = arg;
1315
1316 /* Check if the "-force" flag was passed. */
1317 condition_command_opts cc_opts;
1318 const auto group = make_condition_command_options_def_group (&cc_opts);
1319 gdb::option::process_options
1320 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1321
1322 bnum = get_number (&p);
1323 if (bnum == 0)
1324 error (_("Bad breakpoint argument: '%s'"), arg);
1325
1326 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1327 }
1328
1329 /* Check that COMMAND do not contain commands that are suitable
1330 only for tracepoints and not suitable for ordinary breakpoints.
1331 Throw if any such commands is found. */
1332
1333 static void
1334 check_no_tracepoint_commands (struct command_line *commands)
1335 {
1336 struct command_line *c;
1337
1338 for (c = commands; c; c = c->next)
1339 {
1340 if (c->control_type == while_stepping_control)
1341 error (_("The 'while-stepping' command can "
1342 "only be used for tracepoints"));
1343
1344 check_no_tracepoint_commands (c->body_list_0.get ());
1345 check_no_tracepoint_commands (c->body_list_1.get ());
1346
1347 /* Not that command parsing removes leading whitespace and comment
1348 lines and also empty lines. So, we only need to check for
1349 command directly. */
1350 if (strstr (c->line, "collect ") == c->line)
1351 error (_("The 'collect' command can only be used for tracepoints"));
1352
1353 if (strstr (c->line, "teval ") == c->line)
1354 error (_("The 'teval' command can only be used for tracepoints"));
1355 }
1356 }
1357
1358 struct longjmp_breakpoint : public momentary_breakpoint
1359 {
1360 using momentary_breakpoint::momentary_breakpoint;
1361
1362 ~longjmp_breakpoint () override;
1363 };
1364
1365 /* Encapsulate tests for different types of tracepoints. */
1366
1367 static bool
1368 is_tracepoint_type (bptype type)
1369 {
1370 return (type == bp_tracepoint
1371 || type == bp_fast_tracepoint
1372 || type == bp_static_tracepoint
1373 || type == bp_static_marker_tracepoint);
1374 }
1375
1376 /* See breakpoint.h. */
1377
1378 bool
1379 is_tracepoint (const struct breakpoint *b)
1380 {
1381 return is_tracepoint_type (b->type);
1382 }
1383
1384 /* Factory function to create an appropriate instance of breakpoint given
1385 TYPE. */
1386
1387 template<typename... Arg>
1388 static std::unique_ptr<code_breakpoint>
1389 new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type,
1390 Arg&&... args)
1391 {
1392 code_breakpoint *b;
1393
1394 switch (type)
1395 {
1396 case bp_breakpoint:
1397 case bp_hardware_breakpoint:
1398 b = new ordinary_breakpoint (gdbarch, type,
1399 std::forward<Arg> (args)...);
1400 break;
1401
1402 case bp_fast_tracepoint:
1403 case bp_static_tracepoint:
1404 case bp_tracepoint:
1405 b = new tracepoint (gdbarch, type,
1406 std::forward<Arg> (args)...);
1407 break;
1408
1409 case bp_static_marker_tracepoint:
1410 b = new static_marker_tracepoint (gdbarch, type,
1411 std::forward<Arg> (args)...);
1412 break;
1413
1414 case bp_dprintf:
1415 b = new dprintf_breakpoint (gdbarch, type,
1416 std::forward<Arg> (args)...);
1417 break;
1418
1419 default:
1420 gdb_assert_not_reached ("invalid type");
1421 }
1422
1423 return std::unique_ptr<code_breakpoint> (b);
1424 }
1425
1426 /* A helper function that validates that COMMANDS are valid for a
1427 breakpoint. This function will throw an exception if a problem is
1428 found. */
1429
1430 static void
1431 validate_commands_for_breakpoint (struct breakpoint *b,
1432 struct command_line *commands)
1433 {
1434 if (is_tracepoint (b))
1435 {
1436 struct tracepoint *t = (struct tracepoint *) b;
1437 struct command_line *c;
1438 struct command_line *while_stepping = 0;
1439
1440 /* Reset the while-stepping step count. The previous commands
1441 might have included a while-stepping action, while the new
1442 ones might not. */
1443 t->step_count = 0;
1444
1445 /* We need to verify that each top-level element of commands is
1446 valid for tracepoints, that there's at most one
1447 while-stepping element, and that the while-stepping's body
1448 has valid tracing commands excluding nested while-stepping.
1449 We also need to validate the tracepoint action line in the
1450 context of the tracepoint --- validate_actionline actually
1451 has side effects, like setting the tracepoint's
1452 while-stepping STEP_COUNT, in addition to checking if the
1453 collect/teval actions parse and make sense in the
1454 tracepoint's context. */
1455 for (c = commands; c; c = c->next)
1456 {
1457 if (c->control_type == while_stepping_control)
1458 {
1459 if (b->type == bp_fast_tracepoint)
1460 error (_("The 'while-stepping' command "
1461 "cannot be used for fast tracepoint"));
1462 else if (b->type == bp_static_tracepoint
1463 || b->type == bp_static_marker_tracepoint)
1464 error (_("The 'while-stepping' command "
1465 "cannot be used for static tracepoint"));
1466
1467 if (while_stepping)
1468 error (_("The 'while-stepping' command "
1469 "can be used only once"));
1470 else
1471 while_stepping = c;
1472 }
1473
1474 validate_actionline (c->line, b);
1475 }
1476 if (while_stepping)
1477 {
1478 struct command_line *c2;
1479
1480 gdb_assert (while_stepping->body_list_1 == nullptr);
1481 c2 = while_stepping->body_list_0.get ();
1482 for (; c2; c2 = c2->next)
1483 {
1484 if (c2->control_type == while_stepping_control)
1485 error (_("The 'while-stepping' command cannot be nested"));
1486 }
1487 }
1488 }
1489 else
1490 {
1491 check_no_tracepoint_commands (commands);
1492 }
1493 }
1494
1495 /* Return a vector of all the static tracepoints set at ADDR. The
1496 caller is responsible for releasing the vector. */
1497
1498 std::vector<breakpoint *>
1499 static_tracepoints_here (CORE_ADDR addr)
1500 {
1501 std::vector<breakpoint *> found;
1502
1503 for (breakpoint &b : all_breakpoints ())
1504 if (b.type == bp_static_tracepoint
1505 || b.type == bp_static_marker_tracepoint)
1506 {
1507 for (bp_location &loc : b.locations ())
1508 if (loc.address == addr)
1509 found.push_back (&b);
1510 }
1511
1512 return found;
1513 }
1514
1515 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1516 validate that only allowed commands are included. */
1517
1518 void
1519 breakpoint_set_commands (struct breakpoint *b,
1520 counted_command_line &&commands)
1521 {
1522 validate_commands_for_breakpoint (b, commands.get ());
1523
1524 b->commands = std::move (commands);
1525 notify_breakpoint_modified (b);
1526 }
1527
1528 /* Set the internal `silent' flag on the breakpoint. Note that this
1529 is not the same as the "silent" that may appear in the breakpoint's
1530 commands. */
1531
1532 void
1533 breakpoint_set_silent (struct breakpoint *b, int silent)
1534 {
1535 int old_silent = b->silent;
1536
1537 b->silent = silent;
1538 if (old_silent != silent)
1539 notify_breakpoint_modified (b);
1540 }
1541
1542 /* See breakpoint.h. */
1543
1544 void
1545 breakpoint_set_thread (struct breakpoint *b, int thread)
1546 {
1547 /* THREAD should be -1, meaning no thread restriction, or it should be a
1548 valid global thread-id, which are greater than zero. */
1549 gdb_assert (thread == -1 || thread > 0);
1550
1551 /* It is not valid to set a thread restriction for a breakpoint that
1552 already has task or inferior restriction. */
1553 gdb_assert (thread == -1 || (b->task == -1 && b->inferior == -1));
1554
1555 int old_thread = b->thread;
1556 b->thread = thread;
1557 if (old_thread != thread)
1558 notify_breakpoint_modified (b);
1559 }
1560
1561 /* See breakpoint.h. */
1562
1563 void
1564 breakpoint_set_inferior (struct breakpoint *b, int inferior)
1565 {
1566 /* INFERIOR should be -1, meaning no inferior restriction, or it should
1567 be a valid inferior number, which are greater than zero. */
1568 gdb_assert (inferior == -1 || inferior > 0);
1569
1570 /* It is not valid to set an inferior restriction for a breakpoint that
1571 already has a task or thread restriction. */
1572 gdb_assert (inferior == -1 || (b->task == -1 && b->thread == -1));
1573
1574 int old_inferior = b->inferior;
1575 b->inferior = inferior;
1576 if (old_inferior != inferior)
1577 notify_breakpoint_modified (b);
1578 }
1579
1580 /* See breakpoint.h. */
1581
1582 void
1583 breakpoint_set_task (struct breakpoint *b, int task)
1584 {
1585 /* TASK should be -1, meaning no task restriction, or it should be a
1586 valid task-id, which are greater than zero. */
1587 gdb_assert (task == -1 || task > 0);
1588
1589 /* It is not valid to set a task restriction for a breakpoint that
1590 already has a thread or inferior restriction. */
1591 gdb_assert (task == -1 || (b->thread == -1 && b->inferior == -1));
1592
1593 int old_task = b->task;
1594 b->task = task;
1595 if (old_task != task)
1596 notify_breakpoint_modified (b);
1597 }
1598
1599 static void
1600 commands_command_1 (const char *arg, int from_tty,
1601 struct command_line *control)
1602 {
1603 counted_command_line cmd;
1604 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1605 NULL after the call to read_command_lines if the user provides an empty
1606 list of command by just typing "end". */
1607 bool cmd_read = false;
1608
1609 std::string new_arg;
1610
1611 if (arg == NULL || !*arg)
1612 {
1613 /* Argument not explicitly given. Synthesize it. */
1614 if (breakpoint_count - prev_breakpoint_count > 1)
1615 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1616 breakpoint_count);
1617 else if (breakpoint_count > 0)
1618 new_arg = string_printf ("%d", breakpoint_count);
1619 }
1620 else
1621 {
1622 /* Create a copy of ARG. This is needed because the "commands"
1623 command may be coming from a script. In that case, the read
1624 line buffer is going to be overwritten in the lambda of
1625 'map_breakpoint_numbers' below when reading the next line
1626 before we are are done parsing the breakpoint numbers. */
1627 new_arg = arg;
1628 }
1629 arg = new_arg.c_str ();
1630
1631 map_breakpoint_numbers
1632 (arg, [&] (breakpoint *b)
1633 {
1634 if (!cmd_read)
1635 {
1636 gdb_assert (cmd == NULL);
1637 if (control != NULL)
1638 cmd = control->body_list_0;
1639 else
1640 {
1641 std::string str
1642 = string_printf (_("Type commands for breakpoint(s) "
1643 "%s, one per line."),
1644 arg);
1645
1646 auto do_validate = [=] (const char *line)
1647 {
1648 validate_actionline (line, b);
1649 };
1650 gdb::function_view<void (const char *)> validator;
1651 if (is_tracepoint (b))
1652 validator = do_validate;
1653
1654 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1655 }
1656 cmd_read = true;
1657 }
1658
1659 /* If a breakpoint was on the list more than once, we don't need to
1660 do anything. */
1661 if (b->commands != cmd)
1662 {
1663 validate_commands_for_breakpoint (b, cmd.get ());
1664 b->commands = cmd;
1665 notify_breakpoint_modified (b);
1666 }
1667 });
1668 }
1669
1670 static void
1671 commands_command (const char *arg, int from_tty)
1672 {
1673 commands_command_1 (arg, from_tty, NULL);
1674 }
1675
1676 /* Like commands_command, but instead of reading the commands from
1677 input stream, takes them from an already parsed command structure.
1678
1679 This is used by cli-script.c to DTRT with breakpoint commands
1680 that are part of if and while bodies. */
1681 enum command_control_type
1682 commands_from_control_command (const char *arg, struct command_line *cmd)
1683 {
1684 commands_command_1 (arg, 0, cmd);
1685 return simple_control;
1686 }
1687
1688 /* Return true if BL->TARGET_INFO contains valid information. */
1689
1690 static bool
1691 bp_location_has_shadow (struct bp_location *bl)
1692 {
1693 if (bl->loc_type != bp_loc_software_breakpoint)
1694 return false;
1695 if (!bl->inserted)
1696 return false;
1697 if (bl->target_info.shadow_len == 0)
1698 /* BL isn't valid, or doesn't shadow memory. */
1699 return false;
1700 return true;
1701 }
1702
1703 /* Update BUF, which is LEN bytes read from the target address
1704 MEMADDR, by replacing a memory breakpoint with its shadowed
1705 contents.
1706
1707 If READBUF is not NULL, this buffer must not overlap with the of
1708 the breakpoint location's shadow_contents buffer. Otherwise, a
1709 failed assertion internal error will be raised. */
1710
1711 static void
1712 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1713 const gdb_byte *writebuf_org,
1714 ULONGEST memaddr, LONGEST len,
1715 struct bp_target_info *target_info,
1716 struct gdbarch *gdbarch)
1717 {
1718 /* Now do full processing of the found relevant range of elements. */
1719 CORE_ADDR bp_addr = 0;
1720 int bp_size = 0;
1721 int bptoffset = 0;
1722
1723 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1724 current_program_space->aspace, 0))
1725 {
1726 /* The breakpoint is inserted in a different address space. */
1727 return;
1728 }
1729
1730 /* Addresses and length of the part of the breakpoint that
1731 we need to copy. */
1732 bp_addr = target_info->placed_address;
1733 bp_size = target_info->shadow_len;
1734
1735 if (bp_addr + bp_size <= memaddr)
1736 {
1737 /* The breakpoint is entirely before the chunk of memory we are
1738 reading. */
1739 return;
1740 }
1741
1742 if (bp_addr >= memaddr + len)
1743 {
1744 /* The breakpoint is entirely after the chunk of memory we are
1745 reading. */
1746 return;
1747 }
1748
1749 /* Offset within shadow_contents. */
1750 if (bp_addr < memaddr)
1751 {
1752 /* Only copy the second part of the breakpoint. */
1753 bp_size -= memaddr - bp_addr;
1754 bptoffset = memaddr - bp_addr;
1755 bp_addr = memaddr;
1756 }
1757
1758 if (bp_addr + bp_size > memaddr + len)
1759 {
1760 /* Only copy the first part of the breakpoint. */
1761 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1762 }
1763
1764 if (readbuf != NULL)
1765 {
1766 /* Verify that the readbuf buffer does not overlap with the
1767 shadow_contents buffer. */
1768 gdb_assert (target_info->shadow_contents >= readbuf + len
1769 || readbuf >= (target_info->shadow_contents
1770 + target_info->shadow_len));
1771
1772 /* Update the read buffer with this inserted breakpoint's
1773 shadow. */
1774 memcpy (readbuf + bp_addr - memaddr,
1775 target_info->shadow_contents + bptoffset, bp_size);
1776 }
1777 else
1778 {
1779 const unsigned char *bp;
1780 CORE_ADDR addr = target_info->reqstd_address;
1781 int placed_size;
1782
1783 /* Update the shadow with what we want to write to memory. */
1784 memcpy (target_info->shadow_contents + bptoffset,
1785 writebuf_org + bp_addr - memaddr, bp_size);
1786
1787 /* Determine appropriate breakpoint contents and size for this
1788 address. */
1789 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1790
1791 /* Update the final write buffer with this inserted
1792 breakpoint's INSN. */
1793 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1794 }
1795 }
1796
1797 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1798 by replacing any memory breakpoints with their shadowed contents.
1799
1800 If READBUF is not NULL, this buffer must not overlap with any of
1801 the breakpoint location's shadow_contents buffers. Otherwise,
1802 a failed assertion internal error will be raised.
1803
1804 The range of shadowed area by each bp_location is:
1805 bl->address - bp_locations_placed_address_before_address_max
1806 up to bl->address + bp_locations_shadow_len_after_address_max
1807 The range we were requested to resolve shadows for is:
1808 memaddr ... memaddr + len
1809 Thus the safe cutoff boundaries for performance optimization are
1810 memaddr + len <= (bl->address
1811 - bp_locations_placed_address_before_address_max)
1812 and:
1813 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1814
1815 void
1816 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1817 const gdb_byte *writebuf_org,
1818 ULONGEST memaddr, LONGEST len)
1819 {
1820 /* Left boundary, right boundary and median element of our binary
1821 search. */
1822 unsigned bc_l, bc_r, bc;
1823
1824 /* Find BC_L which is a leftmost element which may affect BUF
1825 content. It is safe to report lower value but a failure to
1826 report higher one. */
1827
1828 bc_l = 0;
1829 bc_r = bp_locations.size ();
1830 while (bc_l + 1 < bc_r)
1831 {
1832 struct bp_location *bl;
1833
1834 bc = (bc_l + bc_r) / 2;
1835 bl = bp_locations[bc];
1836
1837 /* Check first BL->ADDRESS will not overflow due to the added
1838 constant. Then advance the left boundary only if we are sure
1839 the BC element can in no way affect the BUF content (MEMADDR
1840 to MEMADDR + LEN range).
1841
1842 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1843 offset so that we cannot miss a breakpoint with its shadow
1844 range tail still reaching MEMADDR. */
1845
1846 if ((bl->address + bp_locations_shadow_len_after_address_max
1847 >= bl->address)
1848 && (bl->address + bp_locations_shadow_len_after_address_max
1849 <= memaddr))
1850 bc_l = bc;
1851 else
1852 bc_r = bc;
1853 }
1854
1855 /* Due to the binary search above, we need to make sure we pick the
1856 first location that's at BC_L's address. E.g., if there are
1857 multiple locations at the same address, BC_L may end up pointing
1858 at a duplicate location, and miss the "master"/"inserted"
1859 location. Say, given locations L1, L2 and L3 at addresses A and
1860 B:
1861
1862 L1@A, L2@A, L3@B, ...
1863
1864 BC_L could end up pointing at location L2, while the "master"
1865 location could be L1. Since the `loc->inserted' flag is only set
1866 on "master" locations, we'd forget to restore the shadow of L1
1867 and L2. */
1868 while (bc_l > 0
1869 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1870 bc_l--;
1871
1872 /* Now do full processing of the found relevant range of elements. */
1873
1874 for (bc = bc_l; bc < bp_locations.size (); bc++)
1875 {
1876 struct bp_location *bl = bp_locations[bc];
1877
1878 /* bp_location array has BL->OWNER always non-NULL. */
1879 if (bl->owner->type == bp_none)
1880 warning (_("reading through apparently deleted breakpoint #%d?"),
1881 bl->owner->number);
1882
1883 /* Performance optimization: any further element can no longer affect BUF
1884 content. */
1885
1886 if (bl->address >= bp_locations_placed_address_before_address_max
1887 && (memaddr + len
1888 <= (bl->address
1889 - bp_locations_placed_address_before_address_max)))
1890 break;
1891
1892 if (!bp_location_has_shadow (bl))
1893 continue;
1894
1895 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1896 memaddr, len, &bl->target_info, bl->gdbarch);
1897 }
1898 }
1899
1900 /* See breakpoint.h. */
1901
1902 bool
1903 is_breakpoint (const struct breakpoint *bpt)
1904 {
1905 return (bpt->type == bp_breakpoint
1906 || bpt->type == bp_hardware_breakpoint
1907 || bpt->type == bp_dprintf);
1908 }
1909
1910 /* Return true if BPT is of any hardware watchpoint kind. */
1911
1912 static bool
1913 is_hardware_watchpoint (const struct breakpoint *bpt)
1914 {
1915 return (bpt->type == bp_hardware_watchpoint
1916 || bpt->type == bp_read_watchpoint
1917 || bpt->type == bp_access_watchpoint);
1918 }
1919
1920 /* See breakpoint.h. */
1921
1922 bool
1923 is_watchpoint (const struct breakpoint *bpt)
1924 {
1925 return (is_hardware_watchpoint (bpt)
1926 || bpt->type == bp_watchpoint);
1927 }
1928
1929 /* Returns true if the current thread and its running state are safe
1930 to evaluate or update watchpoint B. Watchpoints on local
1931 expressions need to be evaluated in the context of the thread that
1932 was current when the watchpoint was created, and, that thread needs
1933 to be stopped to be able to select the correct frame context.
1934 Watchpoints on global expressions can be evaluated on any thread,
1935 and in any state. It is presently left to the target allowing
1936 memory accesses when threads are running. */
1937
1938 static bool
1939 watchpoint_in_thread_scope (struct watchpoint *b)
1940 {
1941 return (b->pspace == current_program_space
1942 && (b->watchpoint_thread == null_ptid
1943 || (inferior_ptid == b->watchpoint_thread
1944 && !inferior_thread ()->executing ())));
1945 }
1946
1947 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1948 associated bp_watchpoint_scope breakpoint. */
1949
1950 static void
1951 watchpoint_del_at_next_stop (struct watchpoint *w)
1952 {
1953 if (w->related_breakpoint != w)
1954 {
1955 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1956 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1957 w->related_breakpoint->disposition = disp_del_at_next_stop;
1958 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1959 w->related_breakpoint = w;
1960 }
1961 w->disposition = disp_del_at_next_stop;
1962 disable_breakpoint (w);
1963 }
1964
1965 /* Extract a bitfield value from value VAL using the bit parameters contained in
1966 watchpoint W. */
1967
1968 static struct value *
1969 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1970 {
1971 struct value *bit_val;
1972
1973 if (val == NULL)
1974 return NULL;
1975
1976 bit_val = value::allocate (val->type ());
1977
1978 val->unpack_bitfield (bit_val,
1979 w->val_bitpos,
1980 w->val_bitsize,
1981 val->contents_for_printing ().data (),
1982 val->offset ());
1983
1984 return bit_val;
1985 }
1986
1987 /* Allocate a dummy location and add it to B. This is required
1988 because bpstat_stop_status requires a location to be able to report
1989 stops. */
1990
1991 static void
1992 add_dummy_location (struct breakpoint *b,
1993 struct program_space *pspace)
1994 {
1995 gdb_assert (!b->has_locations ());
1996
1997 bp_location *loc = new bp_location (b, bp_loc_other);
1998 loc->pspace = pspace;
1999 b->add_location (*loc);
2000 }
2001
2002 /* Assuming that B is a watchpoint:
2003 - Reparse watchpoint expression, if REPARSE is true
2004 - Evaluate expression and store the result in B->val
2005 - Evaluate the condition if there is one, and store the result
2006 in b->loc->cond.
2007 - Update the list of values that must be watched in B->loc.
2008
2009 If the watchpoint disposition is disp_del_at_next_stop, then do
2010 nothing. If this is local watchpoint that is out of scope, delete
2011 it.
2012
2013 Even with `set breakpoint always-inserted on' the watchpoints are
2014 removed + inserted on each stop here. Normal breakpoints must
2015 never be removed because they might be missed by a running thread
2016 when debugging in non-stop mode. On the other hand, hardware
2017 watchpoints (is_hardware_watchpoint; processed here) are specific
2018 to each LWP since they are stored in each LWP's hardware debug
2019 registers. Therefore, such LWP must be stopped first in order to
2020 be able to modify its hardware watchpoints.
2021
2022 Hardware watchpoints must be reset exactly once after being
2023 presented to the user. It cannot be done sooner, because it would
2024 reset the data used to present the watchpoint hit to the user. And
2025 it must not be done later because it could display the same single
2026 watchpoint hit during multiple GDB stops. Note that the latter is
2027 relevant only to the hardware watchpoint types bp_read_watchpoint
2028 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
2029 not user-visible - its hit is suppressed if the memory content has
2030 not changed.
2031
2032 The following constraints influence the location where we can reset
2033 hardware watchpoints:
2034
2035 * target_stopped_by_watchpoint and target_stopped_data_address are
2036 called several times when GDB stops.
2037
2038 [linux]
2039 * Multiple hardware watchpoints can be hit at the same time,
2040 causing GDB to stop. GDB only presents one hardware watchpoint
2041 hit at a time as the reason for stopping, and all the other hits
2042 are presented later, one after the other, each time the user
2043 requests the execution to be resumed. Execution is not resumed
2044 for the threads still having pending hit event stored in
2045 LWP_INFO->STATUS. While the watchpoint is already removed from
2046 the inferior on the first stop the thread hit event is kept being
2047 reported from its cached value by linux_nat_stopped_data_address
2048 until the real thread resume happens after the watchpoint gets
2049 presented and thus its LWP_INFO->STATUS gets reset.
2050
2051 Therefore the hardware watchpoint hit can get safely reset on the
2052 watchpoint removal from inferior. */
2053
2054 static void
2055 update_watchpoint (struct watchpoint *b, bool reparse)
2056 {
2057 bool within_current_scope;
2058
2059 /* If this is a local watchpoint, we only want to check if the
2060 watchpoint frame is in scope if the current thread is the thread
2061 that was used to create the watchpoint. */
2062 if (!watchpoint_in_thread_scope (b))
2063 return;
2064
2065 if (b->disposition == disp_del_at_next_stop)
2066 return;
2067
2068 gdb::optional<scoped_restore_selected_frame> restore_frame;
2069
2070 /* Determine if the watchpoint is within scope. */
2071 if (b->exp_valid_block == NULL)
2072 within_current_scope = true;
2073 else
2074 {
2075 frame_info_ptr fi = get_current_frame ();
2076 struct gdbarch *frame_arch = get_frame_arch (fi);
2077 CORE_ADDR frame_pc = get_frame_pc (fi);
2078
2079 /* If we're at a point where the stack has been destroyed
2080 (e.g. in a function epilogue), unwinding may not work
2081 properly. Do not attempt to recreate locations at this
2082 point. See similar comments in watchpoint_check. */
2083 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
2084 return;
2085
2086 /* Save the current frame's ID so we can restore it after
2087 evaluating the watchpoint expression on its own frame. */
2088 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
2089 took a frame parameter, so that we didn't have to change the
2090 selected frame. */
2091 restore_frame.emplace ();
2092
2093 fi = frame_find_by_id (b->watchpoint_frame);
2094 within_current_scope = (fi != NULL);
2095 if (within_current_scope)
2096 select_frame (fi);
2097 }
2098
2099 /* We don't free locations. They are stored in the bp_location array
2100 and update_global_location_list will eventually delete them and
2101 remove breakpoints if needed. */
2102 b->clear_locations ();
2103
2104 if (within_current_scope && reparse)
2105 {
2106 const char *s;
2107
2108 b->exp.reset ();
2109 s = (b->exp_string_reparse
2110 ? b->exp_string_reparse.get ()
2111 : b->exp_string.get ());
2112 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
2113 /* If the meaning of expression itself changed, the old value is
2114 no longer relevant. We don't want to report a watchpoint hit
2115 to the user when the old value and the new value may actually
2116 be completely different objects. */
2117 b->val = NULL;
2118 b->val_valid = false;
2119
2120 /* Note that unlike with breakpoints, the watchpoint's condition
2121 expression is stored in the breakpoint object, not in the
2122 locations (re)created below. */
2123 if (b->cond_string != NULL)
2124 {
2125 b->cond_exp.reset ();
2126
2127 s = b->cond_string.get ();
2128 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
2129 }
2130 }
2131
2132 /* If we failed to parse the expression, for example because
2133 it refers to a global variable in a not-yet-loaded shared library,
2134 don't try to insert watchpoint. We don't automatically delete
2135 such watchpoint, though, since failure to parse expression
2136 is different from out-of-scope watchpoint. */
2137 if (!target_has_execution ())
2138 {
2139 /* Without execution, memory can't change. No use to try and
2140 set watchpoint locations. The watchpoint will be reset when
2141 the target gains execution, through breakpoint_re_set. */
2142 if (!can_use_hw_watchpoints)
2143 {
2144 if (b->works_in_software_mode ())
2145 b->type = bp_watchpoint;
2146 else
2147 error (_("Can't set read/access watchpoint when "
2148 "hardware watchpoints are disabled."));
2149 }
2150 }
2151 else if (within_current_scope && b->exp)
2152 {
2153 std::vector<value_ref_ptr> val_chain;
2154 struct value *v, *result;
2155 struct program_space *frame_pspace;
2156
2157 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
2158 &val_chain, false);
2159
2160 /* Avoid setting b->val if it's already set. The meaning of
2161 b->val is 'the last value' user saw, and we should update
2162 it only if we reported that last value to user. As it
2163 happens, the code that reports it updates b->val directly.
2164 We don't keep track of the memory value for masked
2165 watchpoints. */
2166 if (!b->val_valid && !is_masked_watchpoint (b))
2167 {
2168 if (b->val_bitsize != 0)
2169 v = extract_bitfield_from_watchpoint_value (b, v);
2170 b->val = release_value (v);
2171 b->val_valid = true;
2172 }
2173
2174 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2175
2176 /* Look at each value on the value chain. */
2177 gdb_assert (!val_chain.empty ());
2178 for (const value_ref_ptr &iter : val_chain)
2179 {
2180 v = iter.get ();
2181
2182 /* If it's a memory location, and GDB actually needed
2183 its contents to evaluate the expression, then we
2184 must watch it. If the first value returned is
2185 still lazy, that means an error occurred reading it;
2186 watch it anyway in case it becomes readable. */
2187 if (v->lval () == lval_memory
2188 && (v == val_chain[0] || ! v->lazy ()))
2189 {
2190 struct type *vtype = check_typedef (v->type ());
2191
2192 /* We only watch structs and arrays if user asked
2193 for it explicitly, never if they just happen to
2194 appear in the middle of some value chain. */
2195 if (v == result
2196 || (vtype->code () != TYPE_CODE_STRUCT
2197 && vtype->code () != TYPE_CODE_ARRAY))
2198 {
2199 CORE_ADDR addr;
2200 enum target_hw_bp_type type;
2201 int bitpos = 0, bitsize = 0;
2202
2203 if (v->bitsize () != 0)
2204 {
2205 /* Extract the bit parameters out from the bitfield
2206 sub-expression. */
2207 bitpos = v->bitpos ();
2208 bitsize = v->bitsize ();
2209 }
2210 else if (v == result && b->val_bitsize != 0)
2211 {
2212 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2213 lvalue whose bit parameters are saved in the fields
2214 VAL_BITPOS and VAL_BITSIZE. */
2215 bitpos = b->val_bitpos;
2216 bitsize = b->val_bitsize;
2217 }
2218
2219 addr = v->address ();
2220 if (bitsize != 0)
2221 {
2222 /* Skip the bytes that don't contain the bitfield. */
2223 addr += bitpos / 8;
2224 }
2225
2226 type = hw_write;
2227 if (b->type == bp_read_watchpoint)
2228 type = hw_read;
2229 else if (b->type == bp_access_watchpoint)
2230 type = hw_access;
2231
2232 bp_location *loc = b->allocate_location ();
2233 loc->gdbarch = v->type ()->arch ();
2234 loc->pspace = frame_pspace;
2235 loc->address
2236 = gdbarch_remove_non_address_bits (loc->gdbarch, addr);
2237 b->add_location (*loc);
2238
2239 if (bitsize != 0)
2240 {
2241 /* Just cover the bytes that make up the bitfield. */
2242 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2243 }
2244 else
2245 loc->length = v->type ()->length ();
2246
2247 loc->watchpoint_type = type;
2248 }
2249 }
2250 }
2251
2252 /* Helper function to bundle possibly emitting a warning along with
2253 changing the type of B to bp_watchpoint. */
2254 auto change_type_to_bp_watchpoint = [] (breakpoint *bp)
2255 {
2256 /* Only warn for breakpoints that have been assigned a +ve number,
2257 anything else is either an internal watchpoint (which we don't
2258 currently create) or has not yet been finalized, in which case
2259 this change of type will be occurring before the user is told
2260 the type of this watchpoint. */
2261 if (bp->type == bp_hardware_watchpoint && bp->number > 0)
2262 warning (_("watchpoint %d downgraded to software watchpoint"),
2263 bp->number);
2264 bp->type = bp_watchpoint;
2265 };
2266
2267 /* Change the type of breakpoint between hardware assisted or
2268 an ordinary watchpoint depending on the hardware support and
2269 free hardware slots. Recheck the number of free hardware slots
2270 as the value chain may have changed. */
2271 {
2272 int reg_cnt;
2273 enum bp_loc_type loc_type;
2274
2275 reg_cnt = can_use_hardware_watchpoint (val_chain);
2276
2277 if (reg_cnt)
2278 {
2279 int i, target_resources_ok, other_type_used;
2280 enum bptype type;
2281
2282 /* Use an exact watchpoint when there's only one memory region to be
2283 watched, and only one debug register is needed to watch it. */
2284 b->exact = target_exact_watchpoints && reg_cnt == 1;
2285
2286 /* We need to determine how many resources are already
2287 used for all other hardware watchpoints plus this one
2288 to see if we still have enough resources to also fit
2289 this watchpoint in as well. */
2290
2291 /* If this is a software watchpoint, we try to turn it
2292 to a hardware one -- count resources as if B was of
2293 hardware watchpoint type. */
2294 type = b->type;
2295 if (type == bp_watchpoint)
2296 type = bp_hardware_watchpoint;
2297
2298 /* This watchpoint may or may not have been placed on
2299 the list yet at this point (it won't be in the list
2300 if we're trying to create it for the first time,
2301 through watch_command), so always account for it
2302 manually. */
2303
2304 /* Count resources used by all watchpoints except B. */
2305 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2306
2307 /* Add in the resources needed for B. */
2308 i += hw_watchpoint_use_count (b);
2309
2310 target_resources_ok
2311 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2312 if (target_resources_ok <= 0)
2313 {
2314 bool sw_mode = b->works_in_software_mode ();
2315
2316 if (target_resources_ok == 0 && !sw_mode)
2317 error (_("Target does not support this type of "
2318 "hardware watchpoint."));
2319 else if (target_resources_ok < 0 && !sw_mode)
2320 error (_("There are not enough available hardware "
2321 "resources for this watchpoint."));
2322
2323 /* Downgrade to software watchpoint. */
2324 change_type_to_bp_watchpoint (b);
2325 }
2326 else
2327 {
2328 /* If this was a software watchpoint, we've just
2329 found we have enough resources to turn it to a
2330 hardware watchpoint. Otherwise, this is a
2331 nop. */
2332 b->type = type;
2333 }
2334 }
2335 else if (!b->works_in_software_mode ())
2336 {
2337 if (!can_use_hw_watchpoints)
2338 error (_("Can't set read/access watchpoint when "
2339 "hardware watchpoints are disabled."));
2340 else
2341 error (_("Expression cannot be implemented with "
2342 "read/access watchpoint."));
2343 }
2344 else
2345 change_type_to_bp_watchpoint (b);
2346
2347 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
2348 : bp_loc_hardware_watchpoint);
2349
2350 for (bp_location &bl : b->locations ())
2351 bl.loc_type = loc_type;
2352 }
2353
2354 /* If a software watchpoint is not watching any memory, then the
2355 above left it without any location set up. But,
2356 bpstat_stop_status requires a location to be able to report
2357 stops, so make sure there's at least a dummy one. */
2358 if (b->type == bp_watchpoint && !b->has_locations ())
2359 add_dummy_location (b, frame_pspace);
2360 }
2361 else if (!within_current_scope)
2362 {
2363 gdb_printf (_("\
2364 Watchpoint %d deleted because the program has left the block\n\
2365 in which its expression is valid.\n"),
2366 b->number);
2367 watchpoint_del_at_next_stop (b);
2368 }
2369 }
2370
2371 /* Returns true iff breakpoint location should be
2372 inserted in the inferior. We don't differentiate the type of BL's owner
2373 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2374 breakpoint_ops is not defined, because in insert_bp_location,
2375 tracepoint's insert_location will not be called. */
2376
2377 static bool
2378 should_be_inserted (struct bp_location *bl)
2379 {
2380 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2381 return false;
2382
2383 if (bl->owner->disposition == disp_del_at_next_stop)
2384 return false;
2385
2386 if (!bl->enabled || bl->disabled_by_cond
2387 || bl->shlib_disabled || bl->duplicate)
2388 return false;
2389
2390 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2391 return false;
2392
2393 /* This is set for example, when we're attached to the parent of a
2394 vfork, and have detached from the child. The child is running
2395 free, and we expect it to do an exec or exit, at which point the
2396 OS makes the parent schedulable again (and the target reports
2397 that the vfork is done). Until the child is done with the shared
2398 memory region, do not insert breakpoints in the parent, otherwise
2399 the child could still trip on the parent's breakpoints. Since
2400 the parent is blocked anyway, it won't miss any breakpoint. */
2401 if (bl->pspace->breakpoints_not_allowed)
2402 return false;
2403
2404 /* Don't insert a breakpoint if we're trying to step past its
2405 location, except if the breakpoint is a single-step breakpoint,
2406 and the breakpoint's thread is the thread which is stepping past
2407 a breakpoint. */
2408 if ((bl->loc_type == bp_loc_software_breakpoint
2409 || bl->loc_type == bp_loc_hardware_breakpoint)
2410 && stepping_past_instruction_at (bl->pspace->aspace,
2411 bl->address)
2412 /* The single-step breakpoint may be inserted at the location
2413 we're trying to step if the instruction branches to itself.
2414 However, the instruction won't be executed at all and it may
2415 break the semantics of the instruction, for example, the
2416 instruction is a conditional branch or updates some flags.
2417 We can't fix it unless GDB is able to emulate the instruction
2418 or switch to displaced stepping. */
2419 && !(bl->owner->type == bp_single_step
2420 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2421 {
2422 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2423 paddress (bl->gdbarch, bl->address));
2424 return false;
2425 }
2426
2427 /* Don't insert watchpoints if we're trying to step past the
2428 instruction that triggered one. */
2429 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2430 && stepping_past_nonsteppable_watchpoint ())
2431 {
2432 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2433 "skipping watchpoint at %s:%d",
2434 paddress (bl->gdbarch, bl->address), bl->length);
2435 return false;
2436 }
2437
2438 return true;
2439 }
2440
2441 /* Same as should_be_inserted but does the check assuming
2442 that the location is not duplicated. */
2443
2444 static bool
2445 unduplicated_should_be_inserted (struct bp_location *bl)
2446 {
2447 scoped_restore restore_bl_duplicate
2448 = make_scoped_restore (&bl->duplicate, 0);
2449
2450 return should_be_inserted (bl);
2451 }
2452
2453 /* Parses a conditional described by an expression COND into an
2454 agent expression bytecode suitable for evaluation
2455 by the bytecode interpreter. Return NULL if there was
2456 any error during parsing. */
2457
2458 static agent_expr_up
2459 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2460 {
2461 if (cond == NULL)
2462 return NULL;
2463
2464 agent_expr_up aexpr;
2465
2466 /* We don't want to stop processing, so catch any errors
2467 that may show up. */
2468 try
2469 {
2470 aexpr = gen_eval_for_expr (scope, cond);
2471 }
2472
2473 catch (const gdb_exception_error &ex)
2474 {
2475 /* If we got here, it means the condition could not be parsed to a valid
2476 bytecode expression and thus can't be evaluated on the target's side.
2477 It's no use iterating through the conditions. */
2478 }
2479
2480 /* We have a valid agent expression. */
2481 return aexpr;
2482 }
2483
2484 /* Based on location BL, create a list of breakpoint conditions to be
2485 passed on to the target. If we have duplicated locations with different
2486 conditions, we will add such conditions to the list. The idea is that the
2487 target will evaluate the list of conditions and will only notify GDB when
2488 one of them is true. */
2489
2490 static void
2491 build_target_condition_list (struct bp_location *bl)
2492 {
2493 bool null_condition_or_parse_error = false;
2494 int modified = bl->needs_update;
2495
2496 /* Release conditions left over from a previous insert. */
2497 bl->target_info.conditions.clear ();
2498
2499 /* This is only meaningful if the target is
2500 evaluating conditions and if the user has
2501 opted for condition evaluation on the target's
2502 side. */
2503 if (gdb_evaluates_breakpoint_condition_p ()
2504 || !target_supports_evaluation_of_breakpoint_conditions ())
2505 return;
2506
2507 auto loc_range = all_bp_locations_at_addr (bl->address);
2508
2509 /* Do a first pass to check for locations with no assigned
2510 conditions or conditions that fail to parse to a valid agent
2511 expression bytecode. If any of these happen, then it's no use to
2512 send conditions to the target since this location will always
2513 trigger and generate a response back to GDB. Note we consider
2514 all locations at the same address irrespective of type, i.e.,
2515 even if the locations aren't considered duplicates (e.g.,
2516 software breakpoint and hardware breakpoint at the same
2517 address). */
2518 for (bp_location *loc : loc_range)
2519 {
2520 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2521 {
2522 if (modified)
2523 {
2524 /* Re-parse the conditions since something changed. In that
2525 case we already freed the condition bytecodes (see
2526 force_breakpoint_reinsertion). We just
2527 need to parse the condition to bytecodes again. */
2528 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2529 loc->cond.get ());
2530 }
2531
2532 /* If we have a NULL bytecode expression, it means something
2533 went wrong or we have a null condition expression. */
2534 if (!loc->cond_bytecode)
2535 {
2536 null_condition_or_parse_error = true;
2537 break;
2538 }
2539 }
2540 }
2541
2542 /* If any of these happened, it means we will have to evaluate the conditions
2543 for the location's address on gdb's side. It is no use keeping bytecodes
2544 for all the other duplicate locations, thus we free all of them here.
2545
2546 This is so we have a finer control over which locations' conditions are
2547 being evaluated by GDB or the remote stub. */
2548 if (null_condition_or_parse_error)
2549 {
2550 for (bp_location *loc : loc_range)
2551 {
2552 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2553 {
2554 /* Only go as far as the first NULL bytecode is
2555 located. */
2556 if (!loc->cond_bytecode)
2557 return;
2558
2559 loc->cond_bytecode.reset ();
2560 }
2561 }
2562 }
2563
2564 /* No NULL conditions or failed bytecode generation. Build a
2565 condition list for this location's address. If we have software
2566 and hardware locations at the same address, they aren't
2567 considered duplicates, but we still merge all the conditions
2568 anyway, as it's simpler, and doesn't really make a practical
2569 difference. */
2570 for (bp_location *loc : loc_range)
2571 if (loc->cond
2572 && is_breakpoint (loc->owner)
2573 && loc->pspace->num == bl->pspace->num
2574 && loc->owner->enable_state == bp_enabled
2575 && loc->enabled
2576 && !loc->disabled_by_cond)
2577 {
2578 /* Add the condition to the vector. This will be used later
2579 to send the conditions to the target. */
2580 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2581 }
2582
2583 return;
2584 }
2585
2586 /* Parses a command described by string CMD into an agent expression
2587 bytecode suitable for evaluation by the bytecode interpreter.
2588 Return NULL if there was any error during parsing. */
2589
2590 static agent_expr_up
2591 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2592 {
2593 const char *cmdrest;
2594 const char *format_start, *format_end;
2595 struct gdbarch *gdbarch = get_current_arch ();
2596
2597 if (cmd == NULL)
2598 return NULL;
2599
2600 cmdrest = cmd;
2601
2602 if (*cmdrest == ',')
2603 ++cmdrest;
2604 cmdrest = skip_spaces (cmdrest);
2605
2606 if (*cmdrest++ != '"')
2607 error (_("No format string following the location"));
2608
2609 format_start = cmdrest;
2610
2611 format_pieces fpieces (&cmdrest);
2612
2613 format_end = cmdrest;
2614
2615 if (*cmdrest++ != '"')
2616 error (_("Bad format string, non-terminated '\"'."));
2617
2618 cmdrest = skip_spaces (cmdrest);
2619
2620 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2621 error (_("Invalid argument syntax"));
2622
2623 if (*cmdrest == ',')
2624 cmdrest++;
2625 cmdrest = skip_spaces (cmdrest);
2626
2627 /* For each argument, make an expression. */
2628
2629 std::vector<struct expression *> argvec;
2630 while (*cmdrest != '\0')
2631 {
2632 const char *cmd1;
2633
2634 cmd1 = cmdrest;
2635 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope),
2636 PARSER_COMMA_TERMINATES);
2637 argvec.push_back (expr.release ());
2638 cmdrest = cmd1;
2639 if (*cmdrest == ',')
2640 ++cmdrest;
2641 }
2642
2643 agent_expr_up aexpr;
2644
2645 /* We don't want to stop processing, so catch any errors
2646 that may show up. */
2647 try
2648 {
2649 aexpr = gen_printf (scope, gdbarch, 0, 0,
2650 format_start, format_end - format_start,
2651 argvec.size (), argvec.data ());
2652 }
2653 catch (const gdb_exception_error &ex)
2654 {
2655 /* If we got here, it means the command could not be parsed to a valid
2656 bytecode expression and thus can't be evaluated on the target's side.
2657 It's no use iterating through the other commands. */
2658 }
2659
2660 /* We have a valid agent expression, return it. */
2661 return aexpr;
2662 }
2663
2664 /* Based on location BL, create a list of breakpoint commands to be
2665 passed on to the target. If we have duplicated locations with
2666 different commands, we will add any such to the list. */
2667
2668 static void
2669 build_target_command_list (struct bp_location *bl)
2670 {
2671 bool null_command_or_parse_error = false;
2672 int modified = bl->needs_update;
2673
2674 /* Clear commands left over from a previous insert. */
2675 bl->target_info.tcommands.clear ();
2676
2677 if (!target_can_run_breakpoint_commands ())
2678 return;
2679
2680 /* For now, limit to agent-style dprintf breakpoints. */
2681 if (dprintf_style != dprintf_style_agent)
2682 return;
2683
2684 auto loc_range = all_bp_locations_at_addr (bl->address);
2685
2686 /* For now, if we have any location at the same address that isn't a
2687 dprintf, don't install the target-side commands, as that would
2688 make the breakpoint not be reported to the core, and we'd lose
2689 control. */
2690 for (bp_location *loc : loc_range)
2691 if (is_breakpoint (loc->owner)
2692 && loc->pspace->num == bl->pspace->num
2693 && loc->owner->type != bp_dprintf)
2694 return;
2695
2696 /* Do a first pass to check for locations with no assigned
2697 conditions or conditions that fail to parse to a valid agent expression
2698 bytecode. If any of these happen, then it's no use to send conditions
2699 to the target since this location will always trigger and generate a
2700 response back to GDB. */
2701 for (bp_location *loc : loc_range)
2702 {
2703 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2704 {
2705 if (modified)
2706 {
2707 /* Re-parse the commands since something changed. In that
2708 case we already freed the command bytecodes (see
2709 force_breakpoint_reinsertion). We just
2710 need to parse the command to bytecodes again. */
2711 loc->cmd_bytecode
2712 = parse_cmd_to_aexpr (bl->address,
2713 loc->owner->extra_string.get ());
2714 }
2715
2716 /* If we have a NULL bytecode expression, it means something
2717 went wrong or we have a null command expression. */
2718 if (!loc->cmd_bytecode)
2719 {
2720 null_command_or_parse_error = true;
2721 break;
2722 }
2723 }
2724 }
2725
2726 /* If anything failed, then we're not doing target-side commands,
2727 and so clean up. */
2728 if (null_command_or_parse_error)
2729 {
2730 for (bp_location *loc : loc_range)
2731 if (is_breakpoint (loc->owner)
2732 && loc->pspace->num == bl->pspace->num)
2733 {
2734 /* Only go as far as the first NULL bytecode is
2735 located. */
2736 if (loc->cmd_bytecode == NULL)
2737 return;
2738
2739 loc->cmd_bytecode.reset ();
2740 }
2741 }
2742
2743 /* No NULL commands or failed bytecode generation. Build a command
2744 list for all duplicate locations at this location's address.
2745 Note that here we must care for whether the breakpoint location
2746 types are considered duplicates, otherwise, say, if we have a
2747 software and hardware location at the same address, the target
2748 could end up running the commands twice. For the moment, we only
2749 support targets-side commands with dprintf, but it doesn't hurt
2750 to be pedantically correct in case that changes. */
2751 for (bp_location *loc : loc_range)
2752 if (breakpoint_locations_match (bl, loc)
2753 && loc->owner->extra_string
2754 && is_breakpoint (loc->owner)
2755 && loc->pspace->num == bl->pspace->num
2756 && loc->owner->enable_state == bp_enabled
2757 && loc->enabled
2758 && !loc->disabled_by_cond)
2759 {
2760 /* Add the command to the vector. This will be used later
2761 to send the commands to the target. */
2762 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2763 }
2764
2765 bl->target_info.persist = 0;
2766 /* Maybe flag this location as persistent. */
2767 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2768 bl->target_info.persist = 1;
2769 }
2770
2771 /* Return the kind of breakpoint on address *ADDR. Get the kind
2772 of breakpoint according to ADDR except single-step breakpoint.
2773 Get the kind of single-step breakpoint according to the current
2774 registers state. */
2775
2776 static int
2777 breakpoint_kind (const struct bp_location *bl, CORE_ADDR *addr)
2778 {
2779 if (bl->owner->type == bp_single_step)
2780 {
2781 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2782 struct regcache *regcache;
2783
2784 regcache = get_thread_regcache (thr);
2785
2786 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2787 regcache, addr);
2788 }
2789 else
2790 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2791 }
2792
2793 /* Rethrow the currently handled exception, if it's a TARGET_CLOSE_ERROR.
2794 E is either the currently handled exception, or a copy, or a sliced copy,
2795 so we can't rethrow that one, but we can use it to inspect the properties
2796 of the currently handled exception. */
2797
2798 static void
2799 rethrow_on_target_close_error (const gdb_exception &e)
2800 {
2801 if (e.reason == 0)
2802 return;
2803 /* Can't set the breakpoint. */
2804
2805 if (e.error != TARGET_CLOSE_ERROR)
2806 return;
2807
2808 /* If the target has closed then it will have deleted any breakpoints
2809 inserted within the target inferior, as a result any further attempts
2810 to interact with the breakpoint objects is not possible. Just rethrow
2811 the error. Don't use e to rethrow, to prevent object slicing of the
2812 exception. */
2813 throw;
2814 }
2815
2816 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2817 location. Any error messages are printed to TMP_ERROR_STREAM; and
2818 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2819 Returns 0 for success, 1 if the bp_location type is not supported or
2820 -1 for failure.
2821
2822 NOTE drow/2003-09-09: This routine could be broken down to an
2823 object-style method for each breakpoint or catchpoint type. */
2824 static int
2825 insert_bp_location (struct bp_location *bl,
2826 struct ui_file *tmp_error_stream,
2827 int *disabled_breaks,
2828 int *hw_breakpoint_error,
2829 int *hw_bp_error_explained_already)
2830 {
2831 gdb_exception bp_excpt;
2832
2833 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2834 return 0;
2835
2836 breakpoint_debug_printf ("%s", breakpoint_location_address_str (bl).c_str ());
2837
2838 /* Note we don't initialize bl->target_info, as that wipes out
2839 the breakpoint location's shadow_contents if the breakpoint
2840 is still inserted at that location. This in turn breaks
2841 target_read_memory which depends on these buffers when
2842 a memory read is requested at the breakpoint location:
2843 Once the target_info has been wiped, we fail to see that
2844 we have a breakpoint inserted at that address and thus
2845 read the breakpoint instead of returning the data saved in
2846 the breakpoint location's shadow contents. */
2847 bl->target_info.reqstd_address = bl->address;
2848 bl->target_info.placed_address_space = bl->pspace->aspace;
2849 bl->target_info.length = bl->length;
2850
2851 /* When working with target-side conditions, we must pass all the conditions
2852 for the same breakpoint address down to the target since GDB will not
2853 insert those locations. With a list of breakpoint conditions, the target
2854 can decide when to stop and notify GDB. */
2855
2856 if (is_breakpoint (bl->owner))
2857 {
2858 build_target_condition_list (bl);
2859 build_target_command_list (bl);
2860 /* Reset the modification marker. */
2861 bl->needs_update = 0;
2862 }
2863
2864 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2865 set at a read-only address, then a breakpoint location will have
2866 been changed to hardware breakpoint before we get here. If it is
2867 "off" however, error out before actually trying to insert the
2868 breakpoint, with a nicer error message. */
2869 if (bl->loc_type == bp_loc_software_breakpoint
2870 && !automatic_hardware_breakpoints)
2871 {
2872 mem_region *mr = lookup_mem_region (bl->address);
2873
2874 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2875 {
2876 gdb_printf (tmp_error_stream,
2877 _("Cannot insert breakpoint %d.\n"
2878 "Cannot set software breakpoint "
2879 "at read-only address %s\n"),
2880 bl->owner->number,
2881 paddress (bl->gdbarch, bl->address));
2882 return 1;
2883 }
2884 }
2885
2886 if (bl->loc_type == bp_loc_software_breakpoint
2887 || bl->loc_type == bp_loc_hardware_breakpoint)
2888 {
2889 /* First check to see if we have to handle an overlay. */
2890 if (overlay_debugging == ovly_off
2891 || bl->section == NULL
2892 || !(section_is_overlay (bl->section)))
2893 {
2894 /* No overlay handling: just set the breakpoint. */
2895 try
2896 {
2897 int val;
2898
2899 val = bl->owner->insert_location (bl);
2900 if (val)
2901 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2902 }
2903 catch (gdb_exception &e)
2904 {
2905 rethrow_on_target_close_error (e);
2906 bp_excpt = std::move (e);
2907 }
2908 }
2909 else
2910 {
2911 /* This breakpoint is in an overlay section.
2912 Shall we set a breakpoint at the LMA? */
2913 if (!overlay_events_enabled)
2914 {
2915 /* Yes -- overlay event support is not active,
2916 so we must try to set a breakpoint at the LMA.
2917 This will not work for a hardware breakpoint. */
2918 if (bl->loc_type == bp_loc_hardware_breakpoint)
2919 warning (_("hardware breakpoint %d not supported in overlay!"),
2920 bl->owner->number);
2921 else
2922 {
2923 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2924 bl->section);
2925 /* Set a software (trap) breakpoint at the LMA. */
2926 bl->overlay_target_info = bl->target_info;
2927 bl->overlay_target_info.reqstd_address = addr;
2928
2929 /* No overlay handling: just set the breakpoint. */
2930 try
2931 {
2932 int val;
2933
2934 bl->overlay_target_info.kind
2935 = breakpoint_kind (bl, &addr);
2936 bl->overlay_target_info.placed_address = addr;
2937 val = target_insert_breakpoint (bl->gdbarch,
2938 &bl->overlay_target_info);
2939 if (val)
2940 bp_excpt
2941 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2942 }
2943 catch (gdb_exception &e)
2944 {
2945 rethrow_on_target_close_error (e);
2946 bp_excpt = std::move (e);
2947 }
2948
2949 if (bp_excpt.reason != 0)
2950 gdb_printf (tmp_error_stream,
2951 "Overlay breakpoint %d "
2952 "failed: in ROM?\n",
2953 bl->owner->number);
2954 }
2955 }
2956 /* Shall we set a breakpoint at the VMA? */
2957 if (section_is_mapped (bl->section))
2958 {
2959 /* Yes. This overlay section is mapped into memory. */
2960 try
2961 {
2962 int val;
2963
2964 val = bl->owner->insert_location (bl);
2965 if (val)
2966 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2967 }
2968 catch (gdb_exception_error &e)
2969 {
2970 rethrow_on_target_close_error (e);
2971 bp_excpt = std::move (e);
2972 }
2973 }
2974 else
2975 {
2976 /* No. This breakpoint will not be inserted.
2977 No error, but do not mark the bp as 'inserted'. */
2978 return 0;
2979 }
2980 }
2981
2982 if (bp_excpt.reason != 0)
2983 {
2984 /* Can't set the breakpoint. */
2985 gdb_assert (bl->owner != nullptr);
2986
2987 /* In some cases, we might not be able to insert a
2988 breakpoint in a shared library that has already been
2989 removed, but we have not yet processed the shlib unload
2990 event. Unfortunately, some targets that implement
2991 breakpoint insertion themselves can't tell why the
2992 breakpoint insertion failed (e.g., the remote target
2993 doesn't define error codes), so we must treat generic
2994 errors as memory errors. */
2995 if (bp_excpt.reason == RETURN_ERROR
2996 && (bp_excpt.error == GENERIC_ERROR
2997 || bp_excpt.error == MEMORY_ERROR)
2998 && bl->loc_type == bp_loc_software_breakpoint
2999 && (solib_name_from_address (bl->pspace, bl->address)
3000 || shared_objfile_contains_address_p (bl->pspace,
3001 bl->address)))
3002 {
3003 /* See also: disable_breakpoints_in_shlibs. */
3004 bl->shlib_disabled = 1;
3005 notify_breakpoint_modified (bl->owner);
3006 if (!*disabled_breaks)
3007 {
3008 gdb_printf (tmp_error_stream,
3009 "Cannot insert breakpoint %d.\n",
3010 bl->owner->number);
3011 gdb_printf (tmp_error_stream,
3012 "Temporarily disabling shared "
3013 "library breakpoints:\n");
3014 }
3015 *disabled_breaks = 1;
3016 gdb_printf (tmp_error_stream,
3017 "breakpoint #%d\n", bl->owner->number);
3018 return 0;
3019 }
3020 else
3021 {
3022 if (bl->loc_type == bp_loc_hardware_breakpoint)
3023 {
3024 *hw_breakpoint_error = 1;
3025 *hw_bp_error_explained_already = bp_excpt.message != NULL;
3026 gdb_printf (tmp_error_stream,
3027 "Cannot insert hardware breakpoint %d%s",
3028 bl->owner->number,
3029 bp_excpt.message ? ":" : ".\n");
3030 if (bp_excpt.message != NULL)
3031 gdb_printf (tmp_error_stream, "%s.\n",
3032 bp_excpt.what ());
3033 }
3034 else
3035 {
3036 if (bp_excpt.message == NULL)
3037 {
3038 std::string message
3039 = memory_error_message (TARGET_XFER_E_IO,
3040 bl->gdbarch, bl->address);
3041
3042 gdb_printf (tmp_error_stream,
3043 "Cannot insert breakpoint %d.\n"
3044 "%s\n",
3045 bl->owner->number, message.c_str ());
3046 }
3047 else
3048 {
3049 gdb_printf (tmp_error_stream,
3050 "Cannot insert breakpoint %d: %s\n",
3051 bl->owner->number,
3052 bp_excpt.what ());
3053 }
3054 }
3055 return 1;
3056
3057 }
3058 }
3059 else
3060 bl->inserted = 1;
3061
3062 return 0;
3063 }
3064
3065 else if (bl->loc_type == bp_loc_hardware_watchpoint
3066 && bl->owner->disposition != disp_del_at_next_stop)
3067 {
3068 int val;
3069
3070 val = bl->owner->insert_location (bl);
3071
3072 /* If trying to set a read-watchpoint, and it turns out it's not
3073 supported, try emulating one with an access watchpoint. */
3074 if (val == 1 && bl->watchpoint_type == hw_read)
3075 {
3076 /* But don't try to insert it, if there's already another
3077 hw_access location that would be considered a duplicate
3078 of this one. */
3079 for (bp_location *loc : all_bp_locations ())
3080 if (loc != bl
3081 && loc->watchpoint_type == hw_access
3082 && watchpoint_locations_match (bl, loc))
3083 {
3084 bl->duplicate = 1;
3085 bl->inserted = 1;
3086 bl->target_info = loc->target_info;
3087 bl->watchpoint_type = hw_access;
3088 val = 0;
3089 break;
3090 }
3091
3092 if (val == 1)
3093 {
3094 bl->watchpoint_type = hw_access;
3095 val = bl->owner->insert_location (bl);
3096
3097 if (val)
3098 /* Back to the original value. */
3099 bl->watchpoint_type = hw_read;
3100 }
3101 }
3102
3103 bl->inserted = (val == 0);
3104 }
3105
3106 else if (bl->owner->type == bp_catchpoint)
3107 {
3108 int val;
3109
3110 val = bl->owner->insert_location (bl);
3111 if (val)
3112 {
3113 bl->owner->enable_state = bp_disabled;
3114
3115 if (val == 1)
3116 warning (_("\
3117 Error inserting catchpoint %d: Your system does not support this type\n\
3118 of catchpoint."), bl->owner->number);
3119 else
3120 warning (_("Error inserting catchpoint %d."), bl->owner->number);
3121 }
3122
3123 bl->inserted = (val == 0);
3124
3125 /* We've already printed an error message if there was a problem
3126 inserting this catchpoint, and we've disabled the catchpoint,
3127 so just return success. */
3128 return 0;
3129 }
3130
3131 return 0;
3132 }
3133
3134 /* This function is called when program space PSPACE is about to be
3135 deleted. It takes care of updating breakpoints to not reference
3136 PSPACE anymore. */
3137
3138 void
3139 breakpoint_program_space_exit (struct program_space *pspace)
3140 {
3141 /* Remove any breakpoint that was set through this program space. */
3142 for (breakpoint &b : all_breakpoints_safe ())
3143 if (b.pspace == pspace)
3144 delete_breakpoint (&b);
3145
3146 /* Breakpoints set through other program spaces could have locations
3147 bound to PSPACE as well. Remove those. */
3148 for (bp_location *loc : all_bp_locations ())
3149 if (loc->pspace == pspace)
3150 {
3151 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3152 loc->owner->unadd_location (*loc);
3153 }
3154
3155 /* Now update the global location list to permanently delete the
3156 removed locations above. */
3157 update_global_location_list (UGLL_DONT_INSERT);
3158 }
3159
3160 /* Make sure all breakpoints are inserted in inferior.
3161 Throws exception on any error.
3162 A breakpoint that is already inserted won't be inserted
3163 again, so calling this function twice is safe. */
3164 void
3165 insert_breakpoints (void)
3166 {
3167 for (breakpoint &bpt : all_breakpoints ())
3168 if (is_hardware_watchpoint (&bpt))
3169 {
3170 watchpoint &w = gdb::checked_static_cast<watchpoint &> (bpt);
3171
3172 update_watchpoint (&w, false /* don't reparse. */);
3173 }
3174
3175 /* Updating watchpoints creates new locations, so update the global
3176 location list. Explicitly tell ugll to insert locations and
3177 ignore breakpoints_always_inserted_mode. Also,
3178 update_global_location_list tries to "upgrade" software
3179 breakpoints to hardware breakpoints to handle "set breakpoint
3180 auto-hw", so we need to call it even if we don't have new
3181 locations. */
3182 update_global_location_list (UGLL_INSERT);
3183 }
3184
3185 /* This is used when we need to synch breakpoint conditions between GDB and the
3186 target. It is the case with deleting and disabling of breakpoints when using
3187 always-inserted mode. */
3188
3189 static void
3190 update_inserted_breakpoint_locations (void)
3191 {
3192 int error_flag = 0;
3193 int val = 0;
3194 int disabled_breaks = 0;
3195 int hw_breakpoint_error = 0;
3196 int hw_bp_details_reported = 0;
3197
3198 string_file tmp_error_stream;
3199
3200 /* Explicitly mark the warning -- this will only be printed if
3201 there was an error. */
3202 tmp_error_stream.puts ("Warning:\n");
3203
3204 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3205
3206 for (bp_location *bl : all_bp_locations ())
3207 {
3208 /* We only want to update software breakpoints and hardware
3209 breakpoints. */
3210 if (!is_breakpoint (bl->owner))
3211 continue;
3212
3213 /* We only want to update locations that are already inserted
3214 and need updating. This is to avoid unwanted insertion during
3215 deletion of breakpoints. */
3216 if (!bl->inserted || !bl->needs_update)
3217 continue;
3218
3219 switch_to_program_space_and_thread (bl->pspace);
3220
3221 /* For targets that support global breakpoints, there's no need
3222 to select an inferior to insert breakpoint to. In fact, even
3223 if we aren't attached to any process yet, we should still
3224 insert breakpoints. */
3225 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3226 && (inferior_ptid == null_ptid || !target_has_execution ()))
3227 continue;
3228
3229 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3230 &hw_breakpoint_error, &hw_bp_details_reported);
3231 if (val)
3232 error_flag = val;
3233 }
3234
3235 if (error_flag)
3236 {
3237 target_terminal::ours_for_output ();
3238 error (("%s"), tmp_error_stream.c_str ());
3239 }
3240 }
3241
3242 /* Used when starting or continuing the program. */
3243
3244 static void
3245 insert_breakpoint_locations (void)
3246 {
3247 int error_flag = 0;
3248 int val = 0;
3249 int disabled_breaks = 0;
3250 int hw_breakpoint_error = 0;
3251 int hw_bp_error_explained_already = 0;
3252
3253 string_file tmp_error_stream;
3254
3255 /* Explicitly mark the warning -- this will only be printed if
3256 there was an error. */
3257 tmp_error_stream.puts ("Warning:\n");
3258
3259 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3260
3261 for (bp_location *bl : all_bp_locations ())
3262 {
3263 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3264 continue;
3265
3266 /* There is no point inserting thread-specific breakpoints if
3267 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3268 has BL->OWNER always non-NULL. */
3269 if (bl->owner->thread != -1
3270 && !valid_global_thread_id (bl->owner->thread))
3271 continue;
3272
3273 /* Or inferior specific breakpoints if the inferior no longer
3274 exists. */
3275 if (bl->owner->inferior != -1
3276 && !valid_global_inferior_id (bl->owner->inferior))
3277 continue;
3278
3279 switch_to_program_space_and_thread (bl->pspace);
3280
3281 /* For targets that support global breakpoints, there's no need
3282 to select an inferior to insert breakpoint to. In fact, even
3283 if we aren't attached to any process yet, we should still
3284 insert breakpoints. */
3285 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3286 && (inferior_ptid == null_ptid || !target_has_execution ()))
3287 continue;
3288
3289 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3290 &hw_breakpoint_error, &hw_bp_error_explained_already);
3291 if (val)
3292 error_flag = val;
3293 }
3294
3295 /* If we failed to insert all locations of a watchpoint, remove
3296 them, as half-inserted watchpoint is of limited use. */
3297 for (breakpoint &bpt : all_breakpoints ())
3298 {
3299 bool some_failed = false;
3300
3301 if (!is_hardware_watchpoint (&bpt))
3302 continue;
3303
3304 if (!breakpoint_enabled (&bpt))
3305 continue;
3306
3307 if (bpt.disposition == disp_del_at_next_stop)
3308 continue;
3309
3310 for (bp_location &loc : bpt.locations ())
3311 if (!loc.inserted && should_be_inserted (&loc))
3312 {
3313 some_failed = true;
3314 break;
3315 }
3316
3317 if (some_failed)
3318 {
3319 for (bp_location &loc : bpt.locations ())
3320 if (loc.inserted)
3321 remove_breakpoint (&loc);
3322
3323 hw_breakpoint_error = 1;
3324 tmp_error_stream.printf ("Could not insert "
3325 "hardware watchpoint %d.\n",
3326 bpt.number);
3327 error_flag = -1;
3328 }
3329 }
3330
3331 if (error_flag)
3332 {
3333 /* If a hardware breakpoint or watchpoint was inserted, add a
3334 message about possibly exhausted resources. */
3335 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3336 {
3337 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3338 You may have requested too many hardware breakpoints/watchpoints.\n");
3339 }
3340 target_terminal::ours_for_output ();
3341 error (("%s"), tmp_error_stream.c_str ());
3342 }
3343 }
3344
3345 /* Used when the program stops.
3346 Returns zero if successful, or non-zero if there was a problem
3347 removing a breakpoint location. */
3348
3349 int
3350 remove_breakpoints (void)
3351 {
3352 int val = 0;
3353
3354 for (bp_location *bl : all_bp_locations ())
3355 if (bl->inserted && !is_tracepoint (bl->owner))
3356 val |= remove_breakpoint (bl);
3357
3358 return val;
3359 }
3360
3361 /* When a thread exits, remove breakpoints that are related to
3362 that thread. */
3363
3364 static void
3365 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3366 {
3367 for (breakpoint &b : all_breakpoints_safe ())
3368 {
3369 if (b.thread == tp->global_num && user_breakpoint_p (&b))
3370 {
3371 gdb_printf (_("\
3372 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3373 b.number, print_thread_id (tp));
3374 delete_breakpoint (&b);
3375 }
3376 }
3377 }
3378
3379 /* Called when inferior INF has been removed from GDB. Remove associated
3380 per-inferior breakpoints. */
3381
3382 static void
3383 remove_inferior_breakpoints (struct inferior *inf)
3384 {
3385 for (breakpoint &b : all_breakpoints_safe ())
3386 {
3387 if (b.inferior == inf->num && user_breakpoint_p (&b))
3388 {
3389 /* Tell the user the breakpoint has been deleted. But only for
3390 breakpoints that would not normally have been deleted at the
3391 next stop anyway. */
3392 if (b.disposition != disp_del
3393 && b.disposition != disp_del_at_next_stop)
3394 gdb_printf (_("\
3395 Inferior-specific breakpoint %d deleted - inferior %d has been removed.\n"),
3396 b.number, inf->num);
3397 delete_breakpoint (&b);
3398 }
3399 }
3400 }
3401
3402 /* See breakpoint.h. */
3403
3404 void
3405 remove_breakpoints_inf (inferior *inf)
3406 {
3407 int val;
3408
3409 breakpoint_debug_printf ("inf->num = %d", inf->num);
3410
3411 for (bp_location *bl : all_bp_locations ())
3412 {
3413 if (bl->pspace != inf->pspace)
3414 continue;
3415
3416 if (bl->inserted && !bl->target_info.persist)
3417 {
3418 val = remove_breakpoint (bl);
3419 if (val != 0)
3420 return;
3421 }
3422 }
3423 }
3424
3425 static int internal_breakpoint_number = -1;
3426
3427 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3428 If INTERNAL is non-zero, the breakpoint number will be populated
3429 from internal_breakpoint_number and that variable decremented.
3430 Otherwise the breakpoint number will be populated from
3431 breakpoint_count and that value incremented. Internal breakpoints
3432 do not set the internal var bpnum. */
3433 static void
3434 set_breakpoint_number (int internal, struct breakpoint *b)
3435 {
3436 if (internal)
3437 b->number = internal_breakpoint_number--;
3438 else
3439 {
3440 set_breakpoint_count (breakpoint_count + 1);
3441 b->number = breakpoint_count;
3442 }
3443 }
3444
3445 /* Create a TYPE breakpoint on ADDRESS from an object file with GDBARCH. */
3446
3447 static struct breakpoint *
3448 create_internal_breakpoint (struct gdbarch *gdbarch,
3449 CORE_ADDR address, enum bptype type)
3450 {
3451 std::unique_ptr<internal_breakpoint> b
3452 (new internal_breakpoint (gdbarch, type, address));
3453
3454 b->number = internal_breakpoint_number--;
3455
3456 return add_to_breakpoint_chain (std::move (b));
3457 }
3458
3459 /* Create a TYPE breakpoint on minimal symbol MSYM from an object file with
3460 GDBARCH. */
3461
3462 static struct breakpoint *
3463 create_internal_breakpoint (struct gdbarch *gdbarch,
3464 struct bound_minimal_symbol &msym, enum bptype type)
3465 {
3466 CORE_ADDR address;
3467
3468 address = msym.value_address ();
3469
3470 address = gdbarch_convert_from_func_ptr_addr
3471 (gdbarch, address, current_inferior ()->top_target ());
3472
3473 /* Note that we're not using gdbarch_addr_bits_remove here, because that's
3474 related to addresses in $pc. We're getting the address from the
3475 minimal symbol table. */
3476
3477 /* Is gdbarch_deprecated_function_start_offset needed here? Or is that dealt
3478 with elsewhere? Needs testing on vax. */
3479
3480 if (gdbarch_skip_entrypoint_p (gdbarch))
3481 address = gdbarch_skip_entrypoint (gdbarch, address);
3482
3483 return create_internal_breakpoint (gdbarch, address, type);
3484 }
3485
3486 static const char *const longjmp_names[] =
3487 {
3488 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3489 };
3490 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3491
3492 /* Per-objfile data private to breakpoint.c. */
3493 struct breakpoint_objfile_data
3494 {
3495 /* Minimal symbol for "_ovly_debug_event" (if any). */
3496 struct bound_minimal_symbol overlay_msym;
3497
3498 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3499 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3500
3501 /* True if we have looked for longjmp probes. */
3502 int longjmp_searched = 0;
3503
3504 /* SystemTap probe points for longjmp (if any). These are non-owning
3505 references. */
3506 std::vector<probe *> longjmp_probes;
3507
3508 /* Minimal symbol for "std::terminate()" (if any). */
3509 struct bound_minimal_symbol terminate_msym;
3510
3511 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3512 struct bound_minimal_symbol exception_msym;
3513
3514 /* True if we have looked for exception probes. */
3515 int exception_searched = 0;
3516
3517 /* SystemTap probe points for unwinding (if any). These are non-owning
3518 references. */
3519 std::vector<probe *> exception_probes;
3520 };
3521
3522 static const registry<objfile>::key<breakpoint_objfile_data>
3523 breakpoint_objfile_key;
3524
3525 /* Minimal symbol not found sentinel. */
3526 static struct minimal_symbol msym_not_found;
3527
3528 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3529
3530 static bool
3531 msym_not_found_p (const struct minimal_symbol *msym)
3532 {
3533 return msym == &msym_not_found;
3534 }
3535
3536 /* Return per-objfile data needed by breakpoint.c.
3537 Allocate the data if necessary. */
3538
3539 static struct breakpoint_objfile_data *
3540 get_breakpoint_objfile_data (struct objfile *objfile)
3541 {
3542 struct breakpoint_objfile_data *bp_objfile_data;
3543
3544 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3545 if (bp_objfile_data == NULL)
3546 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3547 return bp_objfile_data;
3548 }
3549
3550 static void
3551 create_overlay_event_breakpoint (void)
3552 {
3553 const char *const func_name = "_ovly_debug_event";
3554
3555 for (objfile *objfile : current_program_space->objfiles ())
3556 {
3557 struct breakpoint *b;
3558 struct breakpoint_objfile_data *bp_objfile_data;
3559 CORE_ADDR addr;
3560
3561 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3562
3563 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3564 continue;
3565
3566 if (bp_objfile_data->overlay_msym.minsym == NULL)
3567 {
3568 struct bound_minimal_symbol m;
3569
3570 m = lookup_minimal_symbol_text (func_name, objfile);
3571 if (m.minsym == NULL)
3572 {
3573 /* Avoid future lookups in this objfile. */
3574 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3575 continue;
3576 }
3577 bp_objfile_data->overlay_msym = m;
3578 }
3579
3580 addr = bp_objfile_data->overlay_msym.value_address ();
3581 b = create_internal_breakpoint (objfile->arch (), addr,
3582 bp_overlay_event);
3583 b->locspec = new_explicit_location_spec_function (func_name);
3584
3585 if (overlay_debugging == ovly_auto)
3586 {
3587 b->enable_state = bp_enabled;
3588 overlay_events_enabled = 1;
3589 }
3590 else
3591 {
3592 b->enable_state = bp_disabled;
3593 overlay_events_enabled = 0;
3594 }
3595 }
3596 }
3597
3598 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3599 true if a breakpoint was installed. */
3600
3601 static bool
3602 create_longjmp_master_breakpoint_probe (objfile *objfile)
3603 {
3604 struct gdbarch *gdbarch = objfile->arch ();
3605 struct breakpoint_objfile_data *bp_objfile_data
3606 = get_breakpoint_objfile_data (objfile);
3607
3608 if (!bp_objfile_data->longjmp_searched)
3609 {
3610 std::vector<probe *> ret
3611 = find_probes_in_objfile (objfile, "libc", "longjmp");
3612
3613 if (!ret.empty ())
3614 {
3615 /* We are only interested in checking one element. */
3616 probe *p = ret[0];
3617
3618 if (!p->can_evaluate_arguments ())
3619 {
3620 /* We cannot use the probe interface here,
3621 because it does not know how to evaluate
3622 arguments. */
3623 ret.clear ();
3624 }
3625 }
3626 bp_objfile_data->longjmp_probes = ret;
3627 bp_objfile_data->longjmp_searched = 1;
3628 }
3629
3630 if (bp_objfile_data->longjmp_probes.empty ())
3631 return false;
3632
3633 for (probe *p : bp_objfile_data->longjmp_probes)
3634 {
3635 struct breakpoint *b;
3636
3637 b = create_internal_breakpoint (gdbarch,
3638 p->get_relocated_address (objfile),
3639 bp_longjmp_master);
3640 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
3641 b->enable_state = bp_disabled;
3642 }
3643
3644 return true;
3645 }
3646
3647 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3648 Return true if at least one breakpoint was installed. */
3649
3650 static bool
3651 create_longjmp_master_breakpoint_names (objfile *objfile)
3652 {
3653 struct gdbarch *gdbarch = objfile->arch ();
3654 if (!gdbarch_get_longjmp_target_p (gdbarch))
3655 return false;
3656
3657 struct breakpoint_objfile_data *bp_objfile_data
3658 = get_breakpoint_objfile_data (objfile);
3659 unsigned int installed_bp = 0;
3660
3661 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3662 {
3663 struct breakpoint *b;
3664 const char *func_name;
3665 CORE_ADDR addr;
3666
3667 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3668 continue;
3669
3670 func_name = longjmp_names[i];
3671 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3672 {
3673 struct bound_minimal_symbol m;
3674
3675 m = lookup_minimal_symbol_text (func_name, objfile);
3676 if (m.minsym == NULL)
3677 {
3678 /* Prevent future lookups in this objfile. */
3679 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3680 continue;
3681 }
3682 bp_objfile_data->longjmp_msym[i] = m;
3683 }
3684
3685 addr = bp_objfile_data->longjmp_msym[i].value_address ();
3686 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
3687 b->locspec = new_explicit_location_spec_function (func_name);
3688 b->enable_state = bp_disabled;
3689 installed_bp++;
3690 }
3691
3692 return installed_bp > 0;
3693 }
3694
3695 /* Create a master longjmp breakpoint. */
3696
3697 static void
3698 create_longjmp_master_breakpoint (void)
3699 {
3700 scoped_restore_current_program_space restore_pspace;
3701
3702 for (struct program_space *pspace : program_spaces)
3703 {
3704 set_current_program_space (pspace);
3705
3706 for (objfile *obj : current_program_space->objfiles ())
3707 {
3708 /* Skip separate debug object, it's handled in the loop below. */
3709 if (obj->separate_debug_objfile_backlink != nullptr)
3710 continue;
3711
3712 /* Try a probe kind breakpoint on main objfile. */
3713 if (create_longjmp_master_breakpoint_probe (obj))
3714 continue;
3715
3716 /* Try longjmp_names kind breakpoints on main and separate_debug
3717 objfiles. */
3718 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3719 if (create_longjmp_master_breakpoint_names (debug_objfile))
3720 break;
3721 }
3722 }
3723 }
3724
3725 /* Create a master std::terminate breakpoint. */
3726 static void
3727 create_std_terminate_master_breakpoint (void)
3728 {
3729 const char *const func_name = "std::terminate()";
3730
3731 scoped_restore_current_program_space restore_pspace;
3732
3733 for (struct program_space *pspace : program_spaces)
3734 {
3735 set_current_program_space (pspace);
3736
3737 for (objfile *objfile : current_program_space->objfiles ())
3738 {
3739 struct breakpoint *b;
3740 struct breakpoint_objfile_data *bp_objfile_data;
3741
3742 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3743
3744 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3745 continue;
3746
3747 if (bp_objfile_data->terminate_msym.minsym == NULL)
3748 {
3749 struct bound_minimal_symbol m;
3750
3751 m = lookup_minimal_symbol (func_name, NULL, objfile);
3752 if (m.minsym == NULL || (m.minsym->type () != mst_text
3753 && m.minsym->type () != mst_file_text))
3754 {
3755 /* Prevent future lookups in this objfile. */
3756 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3757 continue;
3758 }
3759 bp_objfile_data->terminate_msym = m;
3760 }
3761
3762 b = create_internal_breakpoint (objfile->arch (),
3763 bp_objfile_data->terminate_msym,
3764 bp_std_terminate_master);
3765 b->locspec = new_explicit_location_spec_function (func_name);
3766 b->enable_state = bp_disabled;
3767 }
3768 }
3769 }
3770
3771 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3772 probe. Return true if a breakpoint was installed. */
3773
3774 static bool
3775 create_exception_master_breakpoint_probe (objfile *objfile)
3776 {
3777 struct breakpoint *b;
3778 struct gdbarch *gdbarch;
3779 struct breakpoint_objfile_data *bp_objfile_data;
3780
3781 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3782
3783 /* We prefer the SystemTap probe point if it exists. */
3784 if (!bp_objfile_data->exception_searched)
3785 {
3786 std::vector<probe *> ret
3787 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3788
3789 if (!ret.empty ())
3790 {
3791 /* We are only interested in checking one element. */
3792 probe *p = ret[0];
3793
3794 if (!p->can_evaluate_arguments ())
3795 {
3796 /* We cannot use the probe interface here, because it does
3797 not know how to evaluate arguments. */
3798 ret.clear ();
3799 }
3800 }
3801 bp_objfile_data->exception_probes = ret;
3802 bp_objfile_data->exception_searched = 1;
3803 }
3804
3805 if (bp_objfile_data->exception_probes.empty ())
3806 return false;
3807
3808 gdbarch = objfile->arch ();
3809
3810 for (probe *p : bp_objfile_data->exception_probes)
3811 {
3812 b = create_internal_breakpoint (gdbarch,
3813 p->get_relocated_address (objfile),
3814 bp_exception_master);
3815 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
3816 b->enable_state = bp_disabled;
3817 }
3818
3819 return true;
3820 }
3821
3822 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3823 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3824
3825 static bool
3826 create_exception_master_breakpoint_hook (objfile *objfile)
3827 {
3828 const char *const func_name = "_Unwind_DebugHook";
3829 struct breakpoint *b;
3830 struct gdbarch *gdbarch;
3831 struct breakpoint_objfile_data *bp_objfile_data;
3832
3833 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3834
3835 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3836 return false;
3837
3838 gdbarch = objfile->arch ();
3839
3840 if (bp_objfile_data->exception_msym.minsym == NULL)
3841 {
3842 struct bound_minimal_symbol debug_hook;
3843
3844 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3845 if (debug_hook.minsym == NULL)
3846 {
3847 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3848 return false;
3849 }
3850
3851 bp_objfile_data->exception_msym = debug_hook;
3852 }
3853
3854 b = create_internal_breakpoint (gdbarch, bp_objfile_data->exception_msym,
3855 bp_exception_master);
3856 b->locspec = new_explicit_location_spec_function (func_name);
3857 b->enable_state = bp_disabled;
3858
3859 return true;
3860 }
3861
3862 /* Install a master breakpoint on the unwinder's debug hook. */
3863
3864 static void
3865 create_exception_master_breakpoint (void)
3866 {
3867 for (objfile *obj : current_program_space->objfiles ())
3868 {
3869 /* Skip separate debug object. */
3870 if (obj->separate_debug_objfile_backlink)
3871 continue;
3872
3873 /* Try a probe kind breakpoint. */
3874 if (create_exception_master_breakpoint_probe (obj))
3875 continue;
3876
3877 /* Iterate over main and separate debug objects and try an
3878 _Unwind_DebugHook kind breakpoint. */
3879 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3880 if (create_exception_master_breakpoint_hook (debug_objfile))
3881 break;
3882 }
3883 }
3884
3885 /* Does B have a location spec? */
3886
3887 static bool
3888 breakpoint_location_spec_empty_p (const struct breakpoint *b)
3889 {
3890 return (b->locspec != nullptr && b->locspec->empty_p ());
3891 }
3892
3893 void
3894 update_breakpoints_after_exec (void)
3895 {
3896 /* We're about to delete breakpoints from GDB's lists. If the
3897 INSERTED flag is true, GDB will try to lift the breakpoints by
3898 writing the breakpoints' "shadow contents" back into memory. The
3899 "shadow contents" are NOT valid after an exec, so GDB should not
3900 do that. Instead, the target is responsible from marking
3901 breakpoints out as soon as it detects an exec. We don't do that
3902 here instead, because there may be other attempts to delete
3903 breakpoints after detecting an exec and before reaching here. */
3904 for (bp_location *bploc : all_bp_locations ())
3905 if (bploc->pspace == current_program_space)
3906 gdb_assert (!bploc->inserted);
3907
3908 for (breakpoint &b : all_breakpoints_safe ())
3909 {
3910 if (b.pspace != current_program_space)
3911 continue;
3912
3913 /* Solib breakpoints must be explicitly reset after an exec(). */
3914 if (b.type == bp_shlib_event)
3915 {
3916 delete_breakpoint (&b);
3917 continue;
3918 }
3919
3920 /* JIT breakpoints must be explicitly reset after an exec(). */
3921 if (b.type == bp_jit_event)
3922 {
3923 delete_breakpoint (&b);
3924 continue;
3925 }
3926
3927 /* Thread event breakpoints must be set anew after an exec(),
3928 as must overlay event and longjmp master breakpoints. */
3929 if (b.type == bp_thread_event || b.type == bp_overlay_event
3930 || b.type == bp_longjmp_master || b.type == bp_std_terminate_master
3931 || b.type == bp_exception_master)
3932 {
3933 delete_breakpoint (&b);
3934 continue;
3935 }
3936
3937 /* Step-resume breakpoints are meaningless after an exec(). */
3938 if (b.type == bp_step_resume || b.type == bp_hp_step_resume)
3939 {
3940 delete_breakpoint (&b);
3941 continue;
3942 }
3943
3944 /* Just like single-step breakpoints. */
3945 if (b.type == bp_single_step)
3946 {
3947 delete_breakpoint (&b);
3948 continue;
3949 }
3950
3951 /* Longjmp and longjmp-resume breakpoints are also meaningless
3952 after an exec. */
3953 if (b.type == bp_longjmp || b.type == bp_longjmp_resume
3954 || b.type == bp_longjmp_call_dummy
3955 || b.type == bp_exception || b.type == bp_exception_resume)
3956 {
3957 delete_breakpoint (&b);
3958 continue;
3959 }
3960
3961 if (b.type == bp_catchpoint)
3962 {
3963 /* For now, none of the bp_catchpoint breakpoints need to
3964 do anything at this point. In the future, if some of
3965 the catchpoints need to something, we will need to add
3966 a new method, and call this method from here. */
3967 continue;
3968 }
3969
3970 /* bp_finish is a special case. The only way we ought to be able
3971 to see one of these when an exec() has happened, is if the user
3972 caught a vfork, and then said "finish". Ordinarily a finish just
3973 carries them to the call-site of the current callee, by setting
3974 a temporary bp there and resuming. But in this case, the finish
3975 will carry them entirely through the vfork & exec.
3976
3977 We don't want to allow a bp_finish to remain inserted now. But
3978 we can't safely delete it, 'cause finish_command has a handle to
3979 the bp on a bpstat, and will later want to delete it. There's a
3980 chance (and I've seen it happen) that if we delete the bp_finish
3981 here, that its storage will get reused by the time finish_command
3982 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3983 We really must allow finish_command to delete a bp_finish.
3984
3985 In the absence of a general solution for the "how do we know
3986 it's safe to delete something others may have handles to?"
3987 problem, what we'll do here is just uninsert the bp_finish, and
3988 let finish_command delete it.
3989
3990 (We know the bp_finish is "doomed" in the sense that it's
3991 momentary, and will be deleted as soon as finish_command sees
3992 the inferior stopped. So it doesn't matter that the bp's
3993 address is probably bogus in the new a.out, unlike e.g., the
3994 solib breakpoints.) */
3995
3996 if (b.type == bp_finish)
3997 {
3998 continue;
3999 }
4000
4001 /* Without a symbolic address, we have little hope of the
4002 pre-exec() address meaning the same thing in the post-exec()
4003 a.out. */
4004 if (breakpoint_location_spec_empty_p (&b))
4005 {
4006 delete_breakpoint (&b);
4007 continue;
4008 }
4009 }
4010 }
4011
4012 int
4013 detach_breakpoints (ptid_t ptid)
4014 {
4015 int val = 0;
4016 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
4017 struct inferior *inf = current_inferior ();
4018
4019 if (ptid.pid () == inferior_ptid.pid ())
4020 error (_("Cannot detach breakpoints of inferior_ptid"));
4021
4022 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
4023 inferior_ptid = ptid;
4024 for (bp_location *bl : all_bp_locations ())
4025 {
4026 if (bl->pspace != inf->pspace)
4027 continue;
4028
4029 /* This function must physically remove breakpoints locations
4030 from the specified ptid, without modifying the breakpoint
4031 package's state. Locations of type bp_loc_other and
4032 bp_loc_software_watchpoint are only maintained at GDB side,
4033 so there is no need to remove them. Moreover, removing these
4034 would modify the breakpoint package's state. */
4035 if (bl->loc_type == bp_loc_other
4036 || bl->loc_type == bp_loc_software_watchpoint)
4037 continue;
4038
4039 if (bl->inserted)
4040 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
4041 }
4042
4043 return val;
4044 }
4045
4046 /* Remove the breakpoint location BL from the current address space.
4047 Note that this is used to detach breakpoints from a child fork.
4048 When we get here, the child isn't in the inferior list, and neither
4049 do we have objects to represent its address space --- we should
4050 *not* look at bl->pspace->aspace here. */
4051
4052 static int
4053 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
4054 {
4055 breakpoint_debug_printf ("%s due to %s",
4056 breakpoint_location_address_str (bl).c_str (),
4057 remove_bp_reason_str (reason));
4058
4059 int val;
4060
4061 /* BL is never in moribund_locations by our callers. */
4062 gdb_assert (bl->owner != NULL);
4063
4064 /* The type of none suggests that owner is actually deleted.
4065 This should not ever happen. */
4066 gdb_assert (bl->owner->type != bp_none);
4067
4068 if (bl->loc_type == bp_loc_software_breakpoint
4069 || bl->loc_type == bp_loc_hardware_breakpoint)
4070 {
4071 /* "Normal" instruction breakpoint: either the standard
4072 trap-instruction bp (bp_breakpoint), or a
4073 bp_hardware_breakpoint. */
4074
4075 /* First check to see if we have to handle an overlay. */
4076 if (overlay_debugging == ovly_off
4077 || bl->section == NULL
4078 || !(section_is_overlay (bl->section)))
4079 {
4080 /* No overlay handling: just remove the breakpoint. */
4081
4082 /* If we're trying to uninsert a memory breakpoint that we
4083 know is set in a dynamic object that is marked
4084 shlib_disabled, then either the dynamic object was
4085 removed with "remove-symbol-file" or with
4086 "nosharedlibrary". In the former case, we don't know
4087 whether another dynamic object might have loaded over the
4088 breakpoint's address -- the user might well let us know
4089 about it next with add-symbol-file (the whole point of
4090 add-symbol-file is letting the user manually maintain a
4091 list of dynamically loaded objects). If we have the
4092 breakpoint's shadow memory, that is, this is a software
4093 breakpoint managed by GDB, check whether the breakpoint
4094 is still inserted in memory, to avoid overwriting wrong
4095 code with stale saved shadow contents. Note that HW
4096 breakpoints don't have shadow memory, as they're
4097 implemented using a mechanism that is not dependent on
4098 being able to modify the target's memory, and as such
4099 they should always be removed. */
4100 if (bl->shlib_disabled
4101 && bl->target_info.shadow_len != 0
4102 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
4103 val = 0;
4104 else
4105 val = bl->owner->remove_location (bl, reason);
4106 }
4107 else
4108 {
4109 /* This breakpoint is in an overlay section.
4110 Did we set a breakpoint at the LMA? */
4111 if (!overlay_events_enabled)
4112 {
4113 /* Yes -- overlay event support is not active, so we
4114 should have set a breakpoint at the LMA. Remove it.
4115 */
4116 /* Ignore any failures: if the LMA is in ROM, we will
4117 have already warned when we failed to insert it. */
4118 if (bl->loc_type == bp_loc_hardware_breakpoint)
4119 target_remove_hw_breakpoint (bl->gdbarch,
4120 &bl->overlay_target_info);
4121 else
4122 target_remove_breakpoint (bl->gdbarch,
4123 &bl->overlay_target_info,
4124 reason);
4125 }
4126 /* Did we set a breakpoint at the VMA?
4127 If so, we will have marked the breakpoint 'inserted'. */
4128 if (bl->inserted)
4129 {
4130 /* Yes -- remove it. Previously we did not bother to
4131 remove the breakpoint if the section had been
4132 unmapped, but let's not rely on that being safe. We
4133 don't know what the overlay manager might do. */
4134
4135 /* However, we should remove *software* breakpoints only
4136 if the section is still mapped, or else we overwrite
4137 wrong code with the saved shadow contents. */
4138 if (bl->loc_type == bp_loc_hardware_breakpoint
4139 || section_is_mapped (bl->section))
4140 val = bl->owner->remove_location (bl, reason);
4141 else
4142 val = 0;
4143 }
4144 else
4145 {
4146 /* No -- not inserted, so no need to remove. No error. */
4147 val = 0;
4148 }
4149 }
4150
4151 /* In some cases, we might not be able to remove a breakpoint in
4152 a shared library that has already been removed, but we have
4153 not yet processed the shlib unload event. Similarly for an
4154 unloaded add-symbol-file object - the user might not yet have
4155 had the chance to remove-symbol-file it. shlib_disabled will
4156 be set if the library/object has already been removed, but
4157 the breakpoint hasn't been uninserted yet, e.g., after
4158 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4159 always-inserted mode. */
4160 if (val
4161 && (bl->loc_type == bp_loc_software_breakpoint
4162 && (bl->shlib_disabled
4163 || solib_name_from_address (bl->pspace, bl->address)
4164 || shared_objfile_contains_address_p (bl->pspace,
4165 bl->address))))
4166 val = 0;
4167
4168 if (val)
4169 return val;
4170 bl->inserted = (reason == DETACH_BREAKPOINT);
4171 }
4172 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4173 {
4174 bl->inserted = (reason == DETACH_BREAKPOINT);
4175 bl->owner->remove_location (bl, reason);
4176
4177 /* Failure to remove any of the hardware watchpoints comes here. */
4178 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4179 warning (_("Could not remove hardware watchpoint %d."),
4180 bl->owner->number);
4181 }
4182 else if (bl->owner->type == bp_catchpoint
4183 && breakpoint_enabled (bl->owner)
4184 && !bl->duplicate)
4185 {
4186 val = bl->owner->remove_location (bl, reason);
4187 if (val)
4188 return val;
4189
4190 bl->inserted = (reason == DETACH_BREAKPOINT);
4191 }
4192
4193 return 0;
4194 }
4195
4196 static int
4197 remove_breakpoint (struct bp_location *bl)
4198 {
4199 /* BL is never in moribund_locations by our callers. */
4200 gdb_assert (bl->owner != NULL);
4201
4202 /* The type of none suggests that owner is actually deleted.
4203 This should not ever happen. */
4204 gdb_assert (bl->owner->type != bp_none);
4205
4206 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4207
4208 switch_to_program_space_and_thread (bl->pspace);
4209
4210 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4211 }
4212
4213 /* Clear the "inserted" flag in all breakpoints. */
4214
4215 void
4216 mark_breakpoints_out (void)
4217 {
4218 for (bp_location *bl : all_bp_locations ())
4219 if (bl->pspace == current_program_space)
4220 bl->inserted = 0;
4221 }
4222
4223 /* Clear the "inserted" flag in all breakpoints and delete any
4224 breakpoints which should go away between runs of the program.
4225
4226 Plus other such housekeeping that has to be done for breakpoints
4227 between runs.
4228
4229 Note: this function gets called at the end of a run (by
4230 generic_mourn_inferior) and when a run begins (by
4231 init_wait_for_inferior). */
4232
4233
4234
4235 void
4236 breakpoint_init_inferior (enum inf_context context)
4237 {
4238 struct program_space *pspace = current_program_space;
4239
4240 /* If breakpoint locations are shared across processes, then there's
4241 nothing to do. */
4242 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4243 return;
4244
4245 mark_breakpoints_out ();
4246
4247 for (breakpoint &b : all_breakpoints_safe ())
4248 {
4249 if (b.has_locations () && b.first_loc ().pspace != pspace)
4250 continue;
4251
4252 switch (b.type)
4253 {
4254 case bp_call_dummy:
4255 case bp_longjmp_call_dummy:
4256
4257 /* If the call dummy breakpoint is at the entry point it will
4258 cause problems when the inferior is rerun, so we better get
4259 rid of it. */
4260
4261 case bp_watchpoint_scope:
4262
4263 /* Also get rid of scope breakpoints. */
4264
4265 case bp_shlib_event:
4266
4267 /* Also remove solib event breakpoints. Their addresses may
4268 have changed since the last time we ran the program.
4269 Actually we may now be debugging against different target;
4270 and so the solib backend that installed this breakpoint may
4271 not be used in by the target. E.g.,
4272
4273 (gdb) file prog-linux
4274 (gdb) run # native linux target
4275 ...
4276 (gdb) kill
4277 (gdb) file prog-win.exe
4278 (gdb) tar rem :9999 # remote Windows gdbserver.
4279 */
4280
4281 case bp_step_resume:
4282
4283 /* Also remove step-resume breakpoints. */
4284
4285 case bp_single_step:
4286
4287 /* Also remove single-step breakpoints. */
4288
4289 delete_breakpoint (&b);
4290 break;
4291
4292 case bp_watchpoint:
4293 case bp_hardware_watchpoint:
4294 case bp_read_watchpoint:
4295 case bp_access_watchpoint:
4296 {
4297 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
4298
4299 /* Likewise for watchpoints on local expressions. */
4300 if (w.exp_valid_block != NULL)
4301 delete_breakpoint (&b);
4302 else
4303 {
4304 /* Get rid of existing locations, which are no longer
4305 valid. New ones will be created in
4306 update_watchpoint, when the inferior is restarted.
4307 The next update_global_location_list call will
4308 garbage collect them. */
4309 b.clear_locations ();
4310
4311 if (context == inf_starting)
4312 {
4313 /* Reset val field to force reread of starting value in
4314 insert_breakpoints. */
4315 w.val.reset (nullptr);
4316 w.val_valid = false;
4317 }
4318 }
4319 }
4320 break;
4321 default:
4322 break;
4323 }
4324 }
4325
4326 /* Get rid of the moribund locations. */
4327 for (bp_location *bl : moribund_locations)
4328 decref_bp_location (&bl);
4329 moribund_locations.clear ();
4330 }
4331
4332 /* These functions concern about actual breakpoints inserted in the
4333 target --- to e.g. check if we need to do decr_pc adjustment or if
4334 we need to hop over the bkpt --- so we check for address space
4335 match, not program space. */
4336
4337 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4338 exists at PC. It returns ordinary_breakpoint_here if it's an
4339 ordinary breakpoint, or permanent_breakpoint_here if it's a
4340 permanent breakpoint.
4341 - When continuing from a location with an ordinary breakpoint, we
4342 actually single step once before calling insert_breakpoints.
4343 - When continuing from a location with a permanent breakpoint, we
4344 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4345 the target, to advance the PC past the breakpoint. */
4346
4347 enum breakpoint_here
4348 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4349 {
4350 bool any_breakpoint_here = false;
4351
4352 for (bp_location *bl : all_bp_locations ())
4353 {
4354 if (bl->loc_type != bp_loc_software_breakpoint
4355 && bl->loc_type != bp_loc_hardware_breakpoint)
4356 continue;
4357
4358 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4359 if ((breakpoint_enabled (bl->owner)
4360 || bl->permanent)
4361 && breakpoint_location_address_match (bl, aspace, pc))
4362 {
4363 if (overlay_debugging
4364 && section_is_overlay (bl->section)
4365 && !section_is_mapped (bl->section))
4366 continue; /* unmapped overlay -- can't be a match */
4367 else if (bl->permanent)
4368 return permanent_breakpoint_here;
4369 else
4370 any_breakpoint_here = true;
4371 }
4372 }
4373
4374 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4375 }
4376
4377 /* See breakpoint.h. */
4378
4379 int
4380 breakpoint_in_range_p (const address_space *aspace,
4381 CORE_ADDR addr, ULONGEST len)
4382 {
4383 for (bp_location *bl : all_bp_locations ())
4384 {
4385 if (bl->loc_type != bp_loc_software_breakpoint
4386 && bl->loc_type != bp_loc_hardware_breakpoint)
4387 continue;
4388
4389 if ((breakpoint_enabled (bl->owner)
4390 || bl->permanent)
4391 && breakpoint_location_address_range_overlap (bl, aspace,
4392 addr, len))
4393 {
4394 if (overlay_debugging
4395 && section_is_overlay (bl->section)
4396 && !section_is_mapped (bl->section))
4397 {
4398 /* Unmapped overlay -- can't be a match. */
4399 continue;
4400 }
4401
4402 return 1;
4403 }
4404 }
4405
4406 return 0;
4407 }
4408
4409 /* Return true if there's a moribund breakpoint at PC. */
4410
4411 int
4412 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4413 {
4414 for (bp_location *loc : moribund_locations)
4415 if (breakpoint_location_address_match (loc, aspace, pc))
4416 return 1;
4417
4418 return 0;
4419 }
4420
4421 /* Returns true iff BL is inserted at PC, in address space ASPACE. */
4422
4423 static bool
4424 bp_location_inserted_here_p (const struct bp_location *bl,
4425 const address_space *aspace, CORE_ADDR pc)
4426 {
4427 if (bl->inserted
4428 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4429 aspace, pc))
4430 {
4431 /* An unmapped overlay can't be a match. */
4432 return !(overlay_debugging
4433 && section_is_overlay (bl->section)
4434 && !section_is_mapped (bl->section));
4435 }
4436 return false;
4437 }
4438
4439 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4440
4441 int
4442 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4443 {
4444 for (bp_location *bl : all_bp_locations_at_addr (pc))
4445 {
4446 if (bl->loc_type != bp_loc_software_breakpoint
4447 && bl->loc_type != bp_loc_hardware_breakpoint)
4448 continue;
4449
4450 if (bp_location_inserted_here_p (bl, aspace, pc))
4451 return 1;
4452 }
4453 return 0;
4454 }
4455
4456 /* This function returns non-zero iff there is a software breakpoint
4457 inserted at PC. */
4458
4459 int
4460 software_breakpoint_inserted_here_p (const address_space *aspace,
4461 CORE_ADDR pc)
4462 {
4463 for (bp_location *bl : all_bp_locations_at_addr (pc))
4464 {
4465 if (bl->loc_type != bp_loc_software_breakpoint)
4466 continue;
4467
4468 if (bp_location_inserted_here_p (bl, aspace, pc))
4469 return 1;
4470 }
4471
4472 return 0;
4473 }
4474
4475 /* See breakpoint.h. */
4476
4477 int
4478 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4479 CORE_ADDR pc)
4480 {
4481 for (bp_location *bl : all_bp_locations_at_addr (pc))
4482 {
4483 if (bl->loc_type != bp_loc_hardware_breakpoint)
4484 continue;
4485
4486 if (bp_location_inserted_here_p (bl, aspace, pc))
4487 return 1;
4488 }
4489
4490 return 0;
4491 }
4492
4493 int
4494 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4495 CORE_ADDR addr, ULONGEST len)
4496 {
4497 for (breakpoint &bpt : all_breakpoints ())
4498 {
4499 if (bpt.type != bp_hardware_watchpoint
4500 && bpt.type != bp_access_watchpoint)
4501 continue;
4502
4503 if (!breakpoint_enabled (&bpt))
4504 continue;
4505
4506 for (bp_location &loc : bpt.locations ())
4507 if (loc.pspace->aspace == aspace && loc.inserted)
4508 {
4509 CORE_ADDR l, h;
4510
4511 /* Check for intersection. */
4512 l = std::max<CORE_ADDR> (loc.address, addr);
4513 h = std::min<CORE_ADDR> (loc.address + loc.length, addr + len);
4514 if (l < h)
4515 return 1;
4516 }
4517 }
4518 return 0;
4519 }
4520
4521 /* See breakpoint.h. */
4522
4523 bool
4524 is_catchpoint (struct breakpoint *b)
4525 {
4526 return (b->type == bp_catchpoint);
4527 }
4528
4529 /* Clear a bpstat so that it says we are not at any breakpoint.
4530 Also free any storage that is part of a bpstat. */
4531
4532 void
4533 bpstat_clear (bpstat **bsp)
4534 {
4535 bpstat *p;
4536 bpstat *q;
4537
4538 if (bsp == 0)
4539 return;
4540 p = *bsp;
4541 while (p != NULL)
4542 {
4543 q = p->next;
4544 delete p;
4545 p = q;
4546 }
4547 *bsp = NULL;
4548 }
4549
4550 bpstat::bpstat (const bpstat &other)
4551 : next (NULL),
4552 bp_location_at (other.bp_location_at),
4553 breakpoint_at (other.breakpoint_at),
4554 commands (other.commands),
4555 print (other.print),
4556 stop (other.stop),
4557 print_it (other.print_it)
4558 {
4559 if (other.old_val != NULL)
4560 old_val = release_value (other.old_val->copy ());
4561 }
4562
4563 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4564 is part of the bpstat is copied as well. */
4565
4566 bpstat *
4567 bpstat_copy (bpstat *bs)
4568 {
4569 bpstat *p = nullptr;
4570 bpstat *tmp;
4571 bpstat *retval = nullptr;
4572
4573 if (bs == NULL)
4574 return bs;
4575
4576 for (; bs != NULL; bs = bs->next)
4577 {
4578 tmp = new bpstat (*bs);
4579
4580 if (p == NULL)
4581 /* This is the first thing in the chain. */
4582 retval = tmp;
4583 else
4584 p->next = tmp;
4585 p = tmp;
4586 }
4587 p->next = NULL;
4588 return retval;
4589 }
4590
4591 /* Find the bpstat associated with this breakpoint. */
4592
4593 bpstat *
4594 bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
4595 {
4596 if (bsp == NULL)
4597 return NULL;
4598
4599 for (; bsp != NULL; bsp = bsp->next)
4600 {
4601 if (bsp->breakpoint_at == breakpoint)
4602 return bsp;
4603 }
4604 return NULL;
4605 }
4606
4607 /* See breakpoint.h. */
4608
4609 bool
4610 bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
4611 {
4612 for (; bsp != NULL; bsp = bsp->next)
4613 {
4614 if (bsp->breakpoint_at == NULL)
4615 {
4616 /* A moribund location can never explain a signal other than
4617 GDB_SIGNAL_TRAP. */
4618 if (sig == GDB_SIGNAL_TRAP)
4619 return true;
4620 }
4621 else
4622 {
4623 if (bsp->breakpoint_at->explains_signal (sig))
4624 return true;
4625 }
4626 }
4627
4628 return false;
4629 }
4630
4631 /* See breakpoint.h. */
4632
4633 int
4634 bpstat_num (bpstat **bsp, int *num)
4635 {
4636 struct breakpoint *b;
4637
4638 if ((*bsp) == NULL)
4639 return 0; /* No more breakpoint values */
4640
4641 /* We assume we'll never have several bpstats that correspond to a
4642 single breakpoint -- otherwise, this function might return the
4643 same number more than once and this will look ugly. */
4644 b = (*bsp)->breakpoint_at;
4645 *bsp = (*bsp)->next;
4646 if (b == NULL)
4647 return -1; /* breakpoint that's been deleted since */
4648
4649 *num = b->number; /* We have its number */
4650 return 1;
4651 }
4652
4653 /* See breakpoint.h */
4654
4655 int
4656 bpstat_locno (const bpstat *bs)
4657 {
4658 const struct breakpoint *b = bs->breakpoint_at;
4659 const struct bp_location *bl = bs->bp_location_at.get ();
4660
4661 if (b != nullptr && b->has_multiple_locations ())
4662 {
4663 int locno = 1;
4664
4665 for (bp_location &loc : b->locations ())
4666 {
4667 if (bl == &loc)
4668 return locno;
4669
4670 ++locno;
4671 }
4672
4673 warning (_("location number not found for breakpoint %d address %s."),
4674 b->number, paddress (bl->gdbarch, bl->address));
4675 }
4676
4677 return 0;
4678 }
4679
4680 /* See breakpoint.h. */
4681
4682 void
4683 print_num_locno (const bpstat *bs, struct ui_out *uiout)
4684 {
4685 struct breakpoint *b = bs->breakpoint_at;
4686
4687 if (b == nullptr)
4688 uiout->text (_("deleted breakpoint"));
4689 else
4690 {
4691 uiout->field_signed ("bkptno", b->number);
4692
4693 int locno = bpstat_locno (bs);
4694 if (locno != 0)
4695 uiout->message (".%pF", signed_field ("locno", locno));
4696 }
4697 }
4698
4699 /* See breakpoint.h. */
4700
4701 void
4702 bpstat_clear_actions (void)
4703 {
4704 bpstat *bs;
4705
4706 if (inferior_ptid == null_ptid)
4707 return;
4708
4709 thread_info *tp = inferior_thread ();
4710 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4711 {
4712 bs->commands = NULL;
4713 bs->old_val.reset (nullptr);
4714 }
4715 }
4716
4717 /* Called when a command is about to proceed the inferior. */
4718
4719 static void
4720 breakpoint_about_to_proceed (void)
4721 {
4722 if (inferior_ptid != null_ptid)
4723 {
4724 struct thread_info *tp = inferior_thread ();
4725
4726 /* Allow inferior function calls in breakpoint commands to not
4727 interrupt the command list. When the call finishes
4728 successfully, the inferior will be standing at the same
4729 breakpoint as if nothing happened. */
4730 if (tp->control.in_infcall)
4731 return;
4732 }
4733
4734 breakpoint_proceeded = 1;
4735 }
4736
4737 /* Return true iff CMD as the first line of a command sequence is `silent'
4738 or its equivalent. */
4739
4740 static bool
4741 command_line_is_silent (struct command_line *cmd)
4742 {
4743 return cmd && (strcmp ("silent", cmd->line) == 0);
4744 }
4745
4746 /* Sets the $_hit_bpnum and $_hit_locno to bpnum and locno.
4747 A locno 0 is changed to 1 to e.g. let the user do
4748 (gdb) disable $_hit_bpnum.$_hit_locno
4749 for a single location breakpoint. */
4750
4751 static void
4752 set_hit_convenience_vars (int bpnum, int locno)
4753 {
4754 set_internalvar_integer (lookup_internalvar ("_hit_bpnum"), bpnum);
4755 set_internalvar_integer (lookup_internalvar ("_hit_locno"),
4756 (locno > 0 ? locno : 1));
4757 }
4758
4759 /* Execute all the commands associated with all the breakpoints at
4760 this location. Any of these commands could cause the process to
4761 proceed beyond this point, etc. We look out for such changes by
4762 checking the global "breakpoint_proceeded" after each command.
4763
4764 Returns true if a breakpoint command resumed the inferior. In that
4765 case, it is the caller's responsibility to recall it again with the
4766 bpstat of the current thread. */
4767
4768 static bool
4769 bpstat_do_actions_1 (bpstat **bsp)
4770 {
4771 bpstat *bs;
4772 bool again = false;
4773
4774 /* Avoid endless recursion if a `source' command is contained
4775 in bs->commands. */
4776 if (executing_breakpoint_commands)
4777 return false;
4778
4779 scoped_restore save_executing
4780 = make_scoped_restore (&executing_breakpoint_commands, 1);
4781
4782 scoped_restore preventer = prevent_dont_repeat ();
4783
4784 /* This pointer will iterate over the list of bpstat's. */
4785 bs = *bsp;
4786
4787 /* The $_hit_* convenience variables are set before running the
4788 commands of BS. In case we have several bs, after the loop,
4789 we set again the variables to the first printed bpnum and locno.
4790 For multiple breakpoints, this ensures the variables are set to the
4791 breakpoint printed for the user. */
4792 int printed_hit_bpnum = -1;
4793 int printed_hit_locno = -1;
4794
4795 breakpoint_proceeded = 0;
4796 for (; bs != NULL; bs = bs->next)
4797 {
4798 struct command_line *cmd = NULL;
4799
4800 /* Set the _hit_* convenience variables before running BS's commands. */
4801 {
4802 const struct breakpoint *b = bs->breakpoint_at;
4803 if (b != nullptr)
4804 {
4805 int locno = bpstat_locno (bs);
4806
4807 set_hit_convenience_vars (b->number, locno);
4808 if (printed_hit_locno == -1 && bs->print)
4809 {
4810 printed_hit_bpnum = b->number;
4811 printed_hit_locno = locno;
4812 }
4813 }
4814 }
4815
4816 /* Take ownership of the BSP's command tree, if it has one.
4817
4818 The command tree could legitimately contain commands like
4819 'step' and 'next', which call clear_proceed_status, which
4820 frees the bpstat BS and its command tree. To make sure this doesn't
4821 free the tree we're executing out from under us, we need to
4822 take ownership of the tree ourselves. Since a given bpstat's
4823 commands are only executed once, we don't need to copy it; we
4824 can clear the pointer in the bpstat, and make sure we free
4825 the tree when we're done. */
4826 counted_command_line ccmd = bs->commands;
4827 bs->commands = NULL;
4828 if (ccmd != NULL)
4829 cmd = ccmd.get ();
4830 if (command_line_is_silent (cmd))
4831 {
4832 /* The action has been already done by bpstat_stop_status. */
4833 cmd = cmd->next;
4834 }
4835
4836 while (cmd != NULL)
4837 {
4838 execute_control_command (cmd);
4839 /* After execute_control_command, if breakpoint_proceeded is true,
4840 BS has been freed and cannot be accessed anymore. */
4841
4842 if (breakpoint_proceeded)
4843 break;
4844 else
4845 cmd = cmd->next;
4846 }
4847
4848 if (breakpoint_proceeded)
4849 {
4850 if (current_ui->async)
4851 /* If we are in async mode, then the target might be still
4852 running, not stopped at any breakpoint, so nothing for
4853 us to do here -- just return to the event loop. */
4854 ;
4855 else
4856 /* In sync mode, when execute_control_command returns
4857 we're already standing on the next breakpoint.
4858 Breakpoint commands for that stop were not run, since
4859 execute_command does not run breakpoint commands --
4860 only command_line_handler does, but that one is not
4861 involved in execution of breakpoint commands. So, we
4862 can now execute breakpoint commands. It should be
4863 noted that making execute_command do bpstat actions is
4864 not an option -- in this case we'll have recursive
4865 invocation of bpstat for each breakpoint with a
4866 command, and can easily blow up GDB stack. Instead, we
4867 return true, which will trigger the caller to recall us
4868 with the new stop_bpstat. */
4869 again = true;
4870 break;
4871 }
4872 }
4873
4874 /* Now that we have executed the commands of all bs, set the _hit_*
4875 convenience variables to the printed values. */
4876 if (printed_hit_locno != -1)
4877 set_hit_convenience_vars (printed_hit_bpnum, printed_hit_locno);
4878
4879 return again;
4880 }
4881
4882 /* Helper for bpstat_do_actions. Get the current thread, if there's
4883 one, is alive and has execution. Return NULL otherwise. */
4884
4885 static thread_info *
4886 get_bpstat_thread ()
4887 {
4888 if (inferior_ptid == null_ptid || !target_has_execution ())
4889 return NULL;
4890
4891 thread_info *tp = inferior_thread ();
4892 if (tp->state == THREAD_EXITED || tp->executing ())
4893 return NULL;
4894 return tp;
4895 }
4896
4897 void
4898 bpstat_do_actions (void)
4899 {
4900 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4901 thread_info *tp;
4902
4903 /* Do any commands attached to breakpoint we are stopped at. */
4904 while ((tp = get_bpstat_thread ()) != NULL)
4905 {
4906 /* Since in sync mode, bpstat_do_actions may resume the
4907 inferior, and only return when it is stopped at the next
4908 breakpoint, we keep doing breakpoint actions until it returns
4909 false to indicate the inferior was not resumed. */
4910 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4911 break;
4912 }
4913
4914 cleanup_if_error.release ();
4915 }
4916
4917 /* Print out the (old or new) value associated with a watchpoint. */
4918
4919 static void
4920 watchpoint_value_print (struct value *val, struct ui_file *stream)
4921 {
4922 if (val == NULL)
4923 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4924 else
4925 {
4926 struct value_print_options opts;
4927 get_user_print_options (&opts);
4928 value_print (val, stream, &opts);
4929 }
4930 }
4931
4932 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4933 debugging multiple threads. */
4934
4935 void
4936 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4937 {
4938 if (uiout->is_mi_like_p ())
4939 return;
4940
4941 uiout->text ("\n");
4942
4943 if (show_thread_that_caused_stop ())
4944 {
4945 struct thread_info *thr = inferior_thread ();
4946
4947 uiout->text ("Thread ");
4948 uiout->field_string ("thread-id", print_thread_id (thr));
4949
4950 const char *name = thread_name (thr);
4951 if (name != NULL)
4952 {
4953 uiout->text (" \"");
4954 uiout->field_string ("name", name);
4955 uiout->text ("\"");
4956 }
4957
4958 uiout->text (" hit ");
4959 }
4960 }
4961
4962 /* Generic routine for printing messages indicating why we
4963 stopped. The behavior of this function depends on the value
4964 'print_it' in the bpstat structure. Under some circumstances we
4965 may decide not to print anything here and delegate the task to
4966 normal_stop(). */
4967
4968 static enum print_stop_action
4969 print_bp_stop_message (bpstat *bs)
4970 {
4971 switch (bs->print_it)
4972 {
4973 case print_it_noop:
4974 /* Nothing should be printed for this bpstat entry. */
4975 return PRINT_UNKNOWN;
4976 break;
4977
4978 case print_it_done:
4979 /* We still want to print the frame, but we already printed the
4980 relevant messages. */
4981 return PRINT_SRC_AND_LOC;
4982 break;
4983
4984 case print_it_normal:
4985 {
4986 struct breakpoint *b = bs->breakpoint_at;
4987
4988 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4989 which has since been deleted. */
4990 if (b == NULL)
4991 return PRINT_UNKNOWN;
4992
4993 /* Normal case. Call the breakpoint's print_it method. */
4994 return b->print_it (bs);
4995 }
4996 break;
4997
4998 default:
4999 internal_error (_("print_bp_stop_message: unrecognized enum value"));
5000 break;
5001 }
5002 }
5003
5004 /* See breakpoint.h. */
5005
5006 void
5007 print_solib_event (bool is_catchpoint)
5008 {
5009 bool any_deleted = !current_program_space->deleted_solibs.empty ();
5010 bool any_added = !current_program_space->added_solibs.empty ();
5011
5012 if (!is_catchpoint)
5013 {
5014 if (any_added || any_deleted)
5015 current_uiout->text (_("Stopped due to shared library event:\n"));
5016 else
5017 current_uiout->text (_("Stopped due to shared library event (no "
5018 "libraries added or removed)\n"));
5019 }
5020
5021 if (current_uiout->is_mi_like_p ())
5022 current_uiout->field_string ("reason",
5023 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
5024
5025 if (any_deleted)
5026 {
5027 current_uiout->text (_(" Inferior unloaded "));
5028 ui_out_emit_list list_emitter (current_uiout, "removed");
5029 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
5030 {
5031 const std::string &name = current_program_space->deleted_solibs[ix];
5032
5033 if (ix > 0)
5034 current_uiout->text (" ");
5035 current_uiout->field_string ("library", name);
5036 current_uiout->text ("\n");
5037 }
5038 }
5039
5040 if (any_added)
5041 {
5042 current_uiout->text (_(" Inferior loaded "));
5043 ui_out_emit_list list_emitter (current_uiout, "added");
5044 bool first = true;
5045 for (so_list *iter : current_program_space->added_solibs)
5046 {
5047 if (!first)
5048 current_uiout->text (" ");
5049 first = false;
5050 current_uiout->field_string ("library", iter->so_name);
5051 current_uiout->text ("\n");
5052 }
5053 }
5054 }
5055
5056 /* Print a message indicating what happened. This is called from
5057 normal_stop(). The input to this routine is the head of the bpstat
5058 list - a list of the eventpoints that caused this stop. KIND is
5059 the target_waitkind for the stopping event. This
5060 routine calls the generic print routine for printing a message
5061 about reasons for stopping. This will print (for example) the
5062 "Breakpoint n," part of the output. The return value of this
5063 routine is one of:
5064
5065 PRINT_UNKNOWN: Means we printed nothing.
5066 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
5067 code to print the location. An example is
5068 "Breakpoint 1, " which should be followed by
5069 the location.
5070 PRINT_SRC_ONLY: Means we printed something, but there is no need
5071 to also print the location part of the message.
5072 An example is the catch/throw messages, which
5073 don't require a location appended to the end.
5074 PRINT_NOTHING: We have done some printing and we don't need any
5075 further info to be printed. */
5076
5077 enum print_stop_action
5078 bpstat_print (bpstat *bs, target_waitkind kind)
5079 {
5080 enum print_stop_action val;
5081
5082 /* Maybe another breakpoint in the chain caused us to stop.
5083 (Currently all watchpoints go on the bpstat whether hit or not.
5084 That probably could (should) be changed, provided care is taken
5085 with respect to bpstat_explains_signal). */
5086 for (; bs; bs = bs->next)
5087 {
5088 val = print_bp_stop_message (bs);
5089 if (val == PRINT_SRC_ONLY
5090 || val == PRINT_SRC_AND_LOC
5091 || val == PRINT_NOTHING)
5092 return val;
5093 }
5094
5095 /* If we had hit a shared library event breakpoint,
5096 print_bp_stop_message would print out this message. If we hit an
5097 OS-level shared library event, do the same thing. */
5098 if (kind == TARGET_WAITKIND_LOADED)
5099 {
5100 print_solib_event (false);
5101 return PRINT_NOTHING;
5102 }
5103
5104 /* We reached the end of the chain, or we got a null BS to start
5105 with and nothing was printed. */
5106 return PRINT_UNKNOWN;
5107 }
5108
5109 /* Evaluate the boolean expression EXP and return the result. */
5110
5111 static bool
5112 breakpoint_cond_eval (expression *exp)
5113 {
5114 scoped_value_mark mark;
5115 return value_true (exp->evaluate ());
5116 }
5117
5118 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
5119
5120 bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
5121 : next (NULL),
5122 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
5123 breakpoint_at (bl->owner),
5124 commands (NULL),
5125 print (0),
5126 stop (0),
5127 print_it (print_it_normal)
5128 {
5129 **bs_link_pointer = this;
5130 *bs_link_pointer = &next;
5131 }
5132
5133 bpstat::bpstat ()
5134 : next (NULL),
5135 breakpoint_at (NULL),
5136 commands (NULL),
5137 print (0),
5138 stop (0),
5139 print_it (print_it_normal)
5140 {
5141 }
5142 \f
5143 /* The target has stopped with waitstatus WS. Check if any hardware
5144 watchpoints have triggered, according to the target. */
5145
5146 int
5147 watchpoints_triggered (const target_waitstatus &ws)
5148 {
5149 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
5150 CORE_ADDR addr;
5151
5152 if (!stopped_by_watchpoint)
5153 {
5154 /* We were not stopped by a watchpoint. Mark all watchpoints
5155 as not triggered. */
5156 for (breakpoint &b : all_breakpoints ())
5157 if (is_hardware_watchpoint (&b))
5158 {
5159 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
5160
5161 w.watchpoint_triggered = watch_triggered_no;
5162 }
5163
5164 return 0;
5165 }
5166
5167 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
5168 {
5169 /* We were stopped by a watchpoint, but we don't know where.
5170 Mark all watchpoints as unknown. */
5171 for (breakpoint &b : all_breakpoints ())
5172 if (is_hardware_watchpoint (&b))
5173 {
5174 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
5175
5176 w.watchpoint_triggered = watch_triggered_unknown;
5177 }
5178
5179 return 1;
5180 }
5181
5182 /* The target could report the data address. Mark watchpoints
5183 affected by this data address as triggered, and all others as not
5184 triggered. */
5185
5186 for (breakpoint &b : all_breakpoints ())
5187 if (is_hardware_watchpoint (&b))
5188 {
5189 watchpoint &w = gdb::checked_static_cast<watchpoint &> (b);
5190
5191 w.watchpoint_triggered = watch_triggered_no;
5192 for (bp_location &loc : b.locations ())
5193 {
5194 if (is_masked_watchpoint (&b))
5195 {
5196 CORE_ADDR newaddr = addr & w.hw_wp_mask;
5197 CORE_ADDR start = loc.address & w.hw_wp_mask;
5198
5199 if (newaddr == start)
5200 {
5201 w.watchpoint_triggered = watch_triggered_yes;
5202 break;
5203 }
5204 }
5205 /* Exact match not required. Within range is sufficient. */
5206 else if (target_watchpoint_addr_within_range
5207 (current_inferior ()->top_target (), addr, loc.address,
5208 loc.length))
5209 {
5210 w.watchpoint_triggered = watch_triggered_yes;
5211 break;
5212 }
5213 }
5214 }
5215
5216 return 1;
5217 }
5218
5219 /* Possible return values for watchpoint_check. */
5220 enum wp_check_result
5221 {
5222 /* The watchpoint has been deleted. */
5223 WP_DELETED = 1,
5224
5225 /* The value has changed. */
5226 WP_VALUE_CHANGED = 2,
5227
5228 /* The value has not changed. */
5229 WP_VALUE_NOT_CHANGED = 3,
5230
5231 /* Ignore this watchpoint, no matter if the value changed or not. */
5232 WP_IGNORE = 4,
5233 };
5234
5235 #define BP_TEMPFLAG 1
5236 #define BP_HARDWAREFLAG 2
5237
5238 /* Evaluate watchpoint condition expression and check if its value
5239 changed. */
5240
5241 static wp_check_result
5242 watchpoint_check (bpstat *bs)
5243 {
5244 struct watchpoint *b;
5245 frame_info_ptr fr;
5246 bool within_current_scope;
5247
5248 /* BS is built from an existing struct breakpoint. */
5249 gdb_assert (bs->breakpoint_at != NULL);
5250 b = (struct watchpoint *) bs->breakpoint_at;
5251
5252 /* If this is a local watchpoint, we only want to check if the
5253 watchpoint frame is in scope if the current thread is the thread
5254 that was used to create the watchpoint. */
5255 if (!watchpoint_in_thread_scope (b))
5256 return WP_IGNORE;
5257
5258 if (b->exp_valid_block == NULL)
5259 within_current_scope = true;
5260 else
5261 {
5262 frame_info_ptr frame = get_current_frame ();
5263 struct gdbarch *frame_arch = get_frame_arch (frame);
5264 CORE_ADDR frame_pc = get_frame_pc (frame);
5265
5266 /* stack_frame_destroyed_p() returns a non-zero value if we're
5267 still in the function but the stack frame has already been
5268 invalidated. Since we can't rely on the values of local
5269 variables after the stack has been destroyed, we are treating
5270 the watchpoint in that state as `not changed' without further
5271 checking. Don't mark watchpoints as changed if the current
5272 frame is in an epilogue - even if they are in some other
5273 frame, our view of the stack is likely to be wrong and
5274 frame_find_by_id could error out. */
5275 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5276 return WP_IGNORE;
5277
5278 fr = frame_find_by_id (b->watchpoint_frame);
5279 within_current_scope = (fr != NULL);
5280
5281 /* If we've gotten confused in the unwinder, we might have
5282 returned a frame that can't describe this variable. */
5283 if (within_current_scope)
5284 {
5285 struct symbol *function;
5286
5287 function = get_frame_function (fr);
5288 if (function == NULL
5289 || !function->value_block ()->contains (b->exp_valid_block))
5290 within_current_scope = false;
5291 }
5292
5293 if (within_current_scope)
5294 /* If we end up stopping, the current frame will get selected
5295 in normal_stop. So this call to select_frame won't affect
5296 the user. */
5297 select_frame (fr);
5298 }
5299
5300 if (within_current_scope)
5301 {
5302 /* We use value_{,free_to_}mark because it could be a *long*
5303 time before we return to the command level and call
5304 free_all_values. We can't call free_all_values because we
5305 might be in the middle of evaluating a function call. */
5306
5307 struct value *mark;
5308 struct value *new_val;
5309
5310 if (is_masked_watchpoint (b))
5311 /* Since we don't know the exact trigger address (from
5312 stopped_data_address), just tell the user we've triggered
5313 a mask watchpoint. */
5314 return WP_VALUE_CHANGED;
5315
5316 mark = value_mark ();
5317 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5318 NULL, NULL, false);
5319
5320 if (b->val_bitsize != 0)
5321 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5322
5323 /* We use value_equal_contents instead of value_equal because
5324 the latter coerces an array to a pointer, thus comparing just
5325 the address of the array instead of its contents. This is
5326 not what we want. */
5327 if ((b->val != NULL) != (new_val != NULL)
5328 || (b->val != NULL && !value_equal_contents (b->val.get (),
5329 new_val)))
5330 {
5331 bs->old_val = b->val;
5332 b->val = release_value (new_val);
5333 b->val_valid = true;
5334 if (new_val != NULL)
5335 value_free_to_mark (mark);
5336 return WP_VALUE_CHANGED;
5337 }
5338 else
5339 {
5340 /* Nothing changed. */
5341 value_free_to_mark (mark);
5342 return WP_VALUE_NOT_CHANGED;
5343 }
5344 }
5345 else
5346 {
5347 /* This seems like the only logical thing to do because
5348 if we temporarily ignored the watchpoint, then when
5349 we reenter the block in which it is valid it contains
5350 garbage (in the case of a function, it may have two
5351 garbage values, one before and one after the prologue).
5352 So we can't even detect the first assignment to it and
5353 watch after that (since the garbage may or may not equal
5354 the first value assigned). */
5355 /* We print all the stop information in
5356 breakpointprint_it, but in this case, by the time we
5357 call breakpoint->print_it this bp will be deleted
5358 already. So we have no choice but print the information
5359 here. */
5360
5361 SWITCH_THRU_ALL_UIS ()
5362 {
5363 struct ui_out *uiout = current_uiout;
5364
5365 if (uiout->is_mi_like_p ())
5366 uiout->field_string
5367 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5368 uiout->message ("\nWatchpoint %pF deleted because the program has "
5369 "left the block in\n"
5370 "which its expression is valid.\n",
5371 signed_field ("wpnum", b->number));
5372 }
5373
5374 /* Make sure the watchpoint's commands aren't executed. */
5375 b->commands = NULL;
5376 watchpoint_del_at_next_stop (b);
5377
5378 return WP_DELETED;
5379 }
5380 }
5381
5382 /* Return true if it looks like target has stopped due to hitting
5383 breakpoint location BL. This function does not check if we should
5384 stop, only if BL explains the stop. */
5385
5386 static bool
5387 bpstat_check_location (const struct bp_location *bl,
5388 const address_space *aspace, CORE_ADDR bp_addr,
5389 const target_waitstatus &ws)
5390 {
5391 struct breakpoint *b = bl->owner;
5392
5393 /* BL is from an existing breakpoint. */
5394 gdb_assert (b != NULL);
5395
5396 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
5397 }
5398
5399 /* Determine if the watched values have actually changed, and we
5400 should stop. If not, set BS->stop to false. */
5401
5402 static void
5403 bpstat_check_watchpoint (bpstat *bs)
5404 {
5405 const struct bp_location *bl;
5406 struct watchpoint *b;
5407
5408 /* BS is built for existing struct breakpoint. */
5409 bl = bs->bp_location_at.get ();
5410 gdb_assert (bl != NULL);
5411 b = (struct watchpoint *) bs->breakpoint_at;
5412 gdb_assert (b != NULL);
5413
5414 {
5415 bool must_check_value = false;
5416
5417 if (b->type == bp_watchpoint)
5418 /* For a software watchpoint, we must always check the
5419 watched value. */
5420 must_check_value = true;
5421 else if (b->watchpoint_triggered == watch_triggered_yes)
5422 /* We have a hardware watchpoint (read, write, or access)
5423 and the target earlier reported an address watched by
5424 this watchpoint. */
5425 must_check_value = true;
5426 else if (b->watchpoint_triggered == watch_triggered_unknown
5427 && b->type == bp_hardware_watchpoint)
5428 /* We were stopped by a hardware watchpoint, but the target could
5429 not report the data address. We must check the watchpoint's
5430 value. Access and read watchpoints are out of luck; without
5431 a data address, we can't figure it out. */
5432 must_check_value = true;
5433
5434 if (must_check_value)
5435 {
5436 wp_check_result e;
5437
5438 try
5439 {
5440 e = watchpoint_check (bs);
5441 }
5442 catch (const gdb_exception_error &ex)
5443 {
5444 exception_fprintf (gdb_stderr, ex,
5445 "Error evaluating expression "
5446 "for watchpoint %d\n",
5447 b->number);
5448
5449 SWITCH_THRU_ALL_UIS ()
5450 {
5451 gdb_printf (_("Watchpoint %d deleted.\n"),
5452 b->number);
5453 }
5454 watchpoint_del_at_next_stop (b);
5455 e = WP_DELETED;
5456 }
5457
5458 switch (e)
5459 {
5460 case WP_DELETED:
5461 /* We've already printed what needs to be printed. */
5462 bs->print_it = print_it_done;
5463 /* Stop. */
5464 break;
5465 case WP_IGNORE:
5466 bs->print_it = print_it_noop;
5467 bs->stop = false;
5468 break;
5469 case WP_VALUE_CHANGED:
5470 if (b->type == bp_read_watchpoint)
5471 {
5472 /* There are two cases to consider here:
5473
5474 1. We're watching the triggered memory for reads.
5475 In that case, trust the target, and always report
5476 the watchpoint hit to the user. Even though
5477 reads don't cause value changes, the value may
5478 have changed since the last time it was read, and
5479 since we're not trapping writes, we will not see
5480 those, and as such we should ignore our notion of
5481 old value.
5482
5483 2. We're watching the triggered memory for both
5484 reads and writes. There are two ways this may
5485 happen:
5486
5487 2.1. This is a target that can't break on data
5488 reads only, but can break on accesses (reads or
5489 writes), such as e.g., x86. We detect this case
5490 at the time we try to insert read watchpoints.
5491
5492 2.2. Otherwise, the target supports read
5493 watchpoints, but, the user set an access or write
5494 watchpoint watching the same memory as this read
5495 watchpoint.
5496
5497 If we're watching memory writes as well as reads,
5498 ignore watchpoint hits when we find that the
5499 value hasn't changed, as reads don't cause
5500 changes. This still gives false positives when
5501 the program writes the same value to memory as
5502 what there was already in memory (we will confuse
5503 it for a read), but it's much better than
5504 nothing. */
5505
5506 int other_write_watchpoint = 0;
5507
5508 if (bl->watchpoint_type == hw_read)
5509 {
5510 for (breakpoint &other_b : all_breakpoints ())
5511 if (other_b.type == bp_hardware_watchpoint
5512 || other_b.type == bp_access_watchpoint)
5513 {
5514 watchpoint &other_w =
5515 gdb::checked_static_cast<watchpoint &> (other_b);
5516
5517 if (other_w.watchpoint_triggered
5518 == watch_triggered_yes)
5519 {
5520 other_write_watchpoint = 1;
5521 break;
5522 }
5523 }
5524 }
5525
5526 if (other_write_watchpoint
5527 || bl->watchpoint_type == hw_access)
5528 {
5529 /* We're watching the same memory for writes,
5530 and the value changed since the last time we
5531 updated it, so this trap must be for a write.
5532 Ignore it. */
5533 bs->print_it = print_it_noop;
5534 bs->stop = false;
5535 }
5536 }
5537 break;
5538 case WP_VALUE_NOT_CHANGED:
5539 if (b->type == bp_hardware_watchpoint
5540 || b->type == bp_watchpoint)
5541 {
5542 /* Don't stop: write watchpoints shouldn't fire if
5543 the value hasn't changed. */
5544 bs->print_it = print_it_noop;
5545 bs->stop = false;
5546 }
5547 /* Stop. */
5548 break;
5549 default:
5550 /* Can't happen. */
5551 break;
5552 }
5553 }
5554 else /* !must_check_value */
5555 {
5556 /* This is a case where some watchpoint(s) triggered, but
5557 not at the address of this watchpoint, or else no
5558 watchpoint triggered after all. So don't print
5559 anything for this watchpoint. */
5560 bs->print_it = print_it_noop;
5561 bs->stop = false;
5562 }
5563 }
5564 }
5565
5566 /* For breakpoints that are currently marked as telling gdb to stop,
5567 check conditions (condition proper, frame, thread and ignore count)
5568 of breakpoint referred to by BS. If we should not stop for this
5569 breakpoint, set BS->stop to 0. */
5570
5571 static void
5572 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5573 {
5574 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
5575
5576 const struct bp_location *bl;
5577 struct breakpoint *b;
5578 /* Assume stop. */
5579 bool condition_result = true;
5580 struct expression *cond;
5581
5582 gdb_assert (bs->stop);
5583
5584 /* BS is built for existing struct breakpoint. */
5585 bl = bs->bp_location_at.get ();
5586 gdb_assert (bl != NULL);
5587 b = bs->breakpoint_at;
5588 gdb_assert (b != NULL);
5589
5590 infrun_debug_printf ("thread = %s, breakpoint %d.%d",
5591 thread->ptid.to_string ().c_str (),
5592 b->number, find_loc_num_by_location (bl));
5593
5594 /* Even if the target evaluated the condition on its end and notified GDB, we
5595 need to do so again since GDB does not know if we stopped due to a
5596 breakpoint or a single step breakpoint. */
5597
5598 if (frame_id_p (b->frame_id)
5599 && b->frame_id != get_stack_frame_id (get_current_frame ()))
5600 {
5601 infrun_debug_printf ("incorrect frame %s not %s, not stopping",
5602 get_stack_frame_id (get_current_frame ()).to_string ().c_str (),
5603 b->frame_id.to_string ().c_str ());
5604 bs->stop = false;
5605 return;
5606 }
5607
5608 /* If this is a thread/task-specific breakpoint, don't waste cpu
5609 evaluating the condition if this isn't the specified
5610 thread/task. */
5611 if ((b->thread != -1 && b->thread != thread->global_num)
5612 || (b->inferior != -1 && b->inferior != thread->inf->num)
5613 || (b->task != -1 && b->task != ada_get_task_number (thread)))
5614 {
5615 infrun_debug_printf ("incorrect thread or task, not stopping");
5616 bs->stop = false;
5617 return;
5618 }
5619
5620 /* Evaluate extension language breakpoints that have a "stop" method
5621 implemented. */
5622 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5623
5624 if (is_watchpoint (b))
5625 {
5626 struct watchpoint *w = (struct watchpoint *) b;
5627
5628 cond = w->cond_exp.get ();
5629 }
5630 else
5631 cond = bl->cond.get ();
5632
5633 if (cond != nullptr && b->disposition != disp_del_at_next_stop)
5634 {
5635 bool within_current_scope = true;
5636 struct watchpoint * w;
5637
5638 /* We use scoped_value_mark because it could be a long time
5639 before we return to the command level and call
5640 free_all_values. We can't call free_all_values because we
5641 might be in the middle of evaluating a function call. */
5642 scoped_value_mark mark;
5643
5644 if (is_watchpoint (b))
5645 w = (struct watchpoint *) b;
5646 else
5647 w = NULL;
5648
5649 /* Need to select the frame, with all that implies so that
5650 the conditions will have the right context. Because we
5651 use the frame, we will not see an inlined function's
5652 variables when we arrive at a breakpoint at the start
5653 of the inlined function; the current frame will be the
5654 call site. */
5655 if (w == NULL || w->cond_exp_valid_block == NULL)
5656 select_frame (get_current_frame ());
5657 else
5658 {
5659 frame_info_ptr frame;
5660
5661 /* For local watchpoint expressions, which particular
5662 instance of a local is being watched matters, so we
5663 keep track of the frame to evaluate the expression
5664 in. To evaluate the condition however, it doesn't
5665 really matter which instantiation of the function
5666 where the condition makes sense triggers the
5667 watchpoint. This allows an expression like "watch
5668 global if q > 10" set in `func', catch writes to
5669 global on all threads that call `func', or catch
5670 writes on all recursive calls of `func' by a single
5671 thread. We simply always evaluate the condition in
5672 the innermost frame that's executing where it makes
5673 sense to evaluate the condition. It seems
5674 intuitive. */
5675 frame = block_innermost_frame (w->cond_exp_valid_block);
5676 if (frame != NULL)
5677 select_frame (frame);
5678 else
5679 within_current_scope = false;
5680 }
5681 if (within_current_scope)
5682 {
5683 try
5684 {
5685 condition_result = breakpoint_cond_eval (cond);
5686 }
5687 catch (const gdb_exception_error &ex)
5688 {
5689 int locno = bpstat_locno (bs);
5690 if (locno != 0)
5691 exception_fprintf
5692 (gdb_stderr, ex,
5693 "Error in testing condition for breakpoint %d.%d:\n",
5694 b->number, locno);
5695 else
5696 exception_fprintf
5697 (gdb_stderr, ex,
5698 "Error in testing condition for breakpoint %d:\n",
5699 b->number);
5700 }
5701 }
5702 else
5703 {
5704 warning (_("Watchpoint condition cannot be tested "
5705 "in the current scope"));
5706 /* If we failed to set the right context for this
5707 watchpoint, unconditionally report it. */
5708 }
5709 /* FIXME-someday, should give breakpoint #. */
5710 }
5711
5712 if (cond != nullptr && !condition_result)
5713 {
5714 infrun_debug_printf ("condition_result = false, not stopping");
5715 bs->stop = false;
5716 return;
5717 }
5718 else if (b->ignore_count > 0)
5719 {
5720 infrun_debug_printf ("ignore count %d, not stopping",
5721 b->ignore_count);
5722 b->ignore_count--;
5723 bs->stop = false;
5724 /* Increase the hit count even though we don't stop. */
5725 ++(b->hit_count);
5726 notify_breakpoint_modified (b);
5727 return;
5728 }
5729
5730 if (bs->stop)
5731 infrun_debug_printf ("stopping at this breakpoint");
5732 else
5733 infrun_debug_printf ("not stopping at this breakpoint");
5734 }
5735
5736 /* Returns true if we need to track moribund locations of LOC's type
5737 on the current target. */
5738
5739 static bool
5740 need_moribund_for_location_type (const struct bp_location *loc)
5741 {
5742 return ((loc->loc_type == bp_loc_software_breakpoint
5743 && !target_supports_stopped_by_sw_breakpoint ())
5744 || (loc->loc_type == bp_loc_hardware_breakpoint
5745 && !target_supports_stopped_by_hw_breakpoint ()));
5746 }
5747
5748 /* See breakpoint.h. */
5749
5750 bpstat *
5751 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5752 const target_waitstatus &ws)
5753 {
5754 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5755
5756 for (breakpoint &b : all_breakpoints ())
5757 {
5758 if (!breakpoint_enabled (&b))
5759 continue;
5760
5761 for (bp_location &bl : b.locations ())
5762 {
5763 /* For hardware watchpoints, we look only at the first
5764 location. The watchpoint_check function will work on the
5765 entire expression, not the individual locations. For
5766 read watchpoints, the watchpoints_triggered function has
5767 checked all locations already. */
5768 if (b.type == bp_hardware_watchpoint && &bl != &b.first_loc ())
5769 break;
5770
5771 if (!bl.enabled || bl.disabled_by_cond || bl.shlib_disabled)
5772 continue;
5773
5774 if (!bpstat_check_location (&bl, aspace, bp_addr, ws))
5775 continue;
5776
5777 /* Come here if it's a watchpoint, or if the break address
5778 matches. */
5779
5780 bpstat *bs = new bpstat (&bl, &bs_link); /* Alloc a bpstat to
5781 explain stop. */
5782
5783 /* Assume we stop. Should we find a watchpoint that is not
5784 actually triggered, or if the condition of the breakpoint
5785 evaluates as false, we'll reset 'stop' to 0. */
5786 bs->stop = true;
5787 bs->print = true;
5788
5789 /* If this is a scope breakpoint, mark the associated
5790 watchpoint as triggered so that we will handle the
5791 out-of-scope event. We'll get to the watchpoint next
5792 iteration. */
5793 if (b.type == bp_watchpoint_scope && b.related_breakpoint != &b)
5794 {
5795 struct watchpoint *w = (struct watchpoint *) b.related_breakpoint;
5796
5797 w->watchpoint_triggered = watch_triggered_yes;
5798 }
5799 }
5800 }
5801
5802 /* Check if a moribund breakpoint explains the stop. */
5803 if (!target_supports_stopped_by_sw_breakpoint ()
5804 || !target_supports_stopped_by_hw_breakpoint ())
5805 {
5806 for (bp_location *loc : moribund_locations)
5807 {
5808 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5809 && need_moribund_for_location_type (loc))
5810 {
5811 bpstat *bs = new bpstat (loc, &bs_link);
5812 /* For hits of moribund locations, we should just proceed. */
5813 bs->stop = false;
5814 bs->print = false;
5815 bs->print_it = print_it_noop;
5816 }
5817 }
5818 }
5819
5820 return bs_head;
5821 }
5822
5823 /* See breakpoint.h. */
5824
5825 bpstat *
5826 bpstat_stop_status (const address_space *aspace,
5827 CORE_ADDR bp_addr, thread_info *thread,
5828 const target_waitstatus &ws,
5829 bpstat *stop_chain)
5830 {
5831 struct breakpoint *b = NULL;
5832 /* First item of allocated bpstat's. */
5833 bpstat *bs_head = stop_chain;
5834 bpstat *bs;
5835 int need_remove_insert;
5836 int removed_any;
5837
5838 /* First, build the bpstat chain with locations that explain a
5839 target stop, while being careful to not set the target running,
5840 as that may invalidate locations (in particular watchpoint
5841 locations are recreated). Resuming will happen here with
5842 breakpoint conditions or watchpoint expressions that include
5843 inferior function calls. */
5844 if (bs_head == NULL)
5845 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5846
5847 /* A bit of special processing for shlib breakpoints. We need to
5848 process solib loading here, so that the lists of loaded and
5849 unloaded libraries are correct before we handle "catch load" and
5850 "catch unload". */
5851 for (bs = bs_head; bs != NULL; bs = bs->next)
5852 {
5853 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5854 {
5855 handle_solib_event ();
5856 break;
5857 }
5858 }
5859
5860 /* Now go through the locations that caused the target to stop, and
5861 check whether we're interested in reporting this stop to higher
5862 layers, or whether we should resume the target transparently. */
5863
5864 removed_any = 0;
5865
5866 for (bs = bs_head; bs != NULL; bs = bs->next)
5867 {
5868 if (!bs->stop)
5869 continue;
5870
5871 b = bs->breakpoint_at;
5872 b->check_status (bs);
5873 if (bs->stop)
5874 {
5875 bpstat_check_breakpoint_conditions (bs, thread);
5876
5877 if (bs->stop)
5878 {
5879 ++(b->hit_count);
5880
5881 /* We will stop here. */
5882 if (b->disposition == disp_disable)
5883 {
5884 --(b->enable_count);
5885 if (b->enable_count <= 0)
5886 b->enable_state = bp_disabled;
5887 removed_any = 1;
5888 }
5889 notify_breakpoint_modified (b);
5890 if (b->silent)
5891 bs->print = false;
5892 bs->commands = b->commands;
5893 if (command_line_is_silent (bs->commands
5894 ? bs->commands.get () : NULL))
5895 bs->print = false;
5896
5897 b->after_condition_true (bs);
5898 }
5899
5900 }
5901
5902 /* Print nothing for this entry if we don't stop or don't
5903 print. */
5904 if (!bs->stop || !bs->print)
5905 bs->print_it = print_it_noop;
5906 }
5907
5908 /* If we aren't stopping, the value of some hardware watchpoint may
5909 not have changed, but the intermediate memory locations we are
5910 watching may have. Don't bother if we're stopping; this will get
5911 done later. */
5912 need_remove_insert = 0;
5913 if (! bpstat_causes_stop (bs_head))
5914 for (bs = bs_head; bs != NULL; bs = bs->next)
5915 if (!bs->stop
5916 && bs->breakpoint_at
5917 && is_hardware_watchpoint (bs->breakpoint_at))
5918 {
5919 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5920
5921 update_watchpoint (w, false /* don't reparse. */);
5922 need_remove_insert = 1;
5923 }
5924
5925 if (need_remove_insert)
5926 update_global_location_list (UGLL_MAY_INSERT);
5927 else if (removed_any)
5928 update_global_location_list (UGLL_DONT_INSERT);
5929
5930 return bs_head;
5931 }
5932
5933 /* See breakpoint.h. */
5934
5935 bpstat *
5936 bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5937 thread_info *thread, const target_waitstatus &ws)
5938 {
5939 gdb_assert (!target_stopped_by_watchpoint ());
5940
5941 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5942 previous stop to avoid confusing bpstat_stop_status. */
5943 watchpoints_triggered (ws);
5944
5945 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5946 }
5947
5948 static void
5949 handle_jit_event (CORE_ADDR address)
5950 {
5951 struct gdbarch *gdbarch;
5952
5953 infrun_debug_printf ("handling bp_jit_event");
5954
5955 /* Switch terminal for any messages produced by
5956 breakpoint_re_set. */
5957 target_terminal::ours_for_output ();
5958
5959 gdbarch = get_frame_arch (get_current_frame ());
5960 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5961 thus it is expected that its objectfile can be found through
5962 minimal symbol lookup. If it doesn't work (and assert fails), it
5963 most likely means that `jit_breakpoint_re_set` was changes and this
5964 function needs to be updated too. */
5965 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5966 gdb_assert (jit_bp_sym.objfile != nullptr);
5967 objfile *objfile = jit_bp_sym.objfile;
5968 if (objfile->separate_debug_objfile_backlink)
5969 objfile = objfile->separate_debug_objfile_backlink;
5970 jit_event_handler (gdbarch, objfile);
5971
5972 target_terminal::inferior ();
5973 }
5974
5975 /* Prepare WHAT final decision for infrun. */
5976
5977 /* Decide what infrun needs to do with this bpstat. */
5978
5979 struct bpstat_what
5980 bpstat_what (bpstat *bs_head)
5981 {
5982 struct bpstat_what retval;
5983 bpstat *bs;
5984
5985 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5986 retval.call_dummy = STOP_NONE;
5987 retval.is_longjmp = false;
5988
5989 for (bs = bs_head; bs != NULL; bs = bs->next)
5990 {
5991 /* Extract this BS's action. After processing each BS, we check
5992 if its action overrides all we've seem so far. */
5993 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5994 enum bptype bptype;
5995
5996 if (bs->breakpoint_at == NULL)
5997 {
5998 /* I suspect this can happen if it was a momentary
5999 breakpoint which has since been deleted. */
6000 bptype = bp_none;
6001 }
6002 else
6003 bptype = bs->breakpoint_at->type;
6004
6005 switch (bptype)
6006 {
6007 case bp_none:
6008 break;
6009 case bp_breakpoint:
6010 case bp_hardware_breakpoint:
6011 case bp_single_step:
6012 case bp_until:
6013 case bp_finish:
6014 case bp_shlib_event:
6015 if (bs->stop)
6016 {
6017 if (bs->print)
6018 this_action = BPSTAT_WHAT_STOP_NOISY;
6019 else
6020 this_action = BPSTAT_WHAT_STOP_SILENT;
6021 }
6022 else
6023 this_action = BPSTAT_WHAT_SINGLE;
6024 break;
6025 case bp_watchpoint:
6026 case bp_hardware_watchpoint:
6027 case bp_read_watchpoint:
6028 case bp_access_watchpoint:
6029 if (bs->stop)
6030 {
6031 if (bs->print)
6032 this_action = BPSTAT_WHAT_STOP_NOISY;
6033 else
6034 this_action = BPSTAT_WHAT_STOP_SILENT;
6035 }
6036 else
6037 {
6038 /* There was a watchpoint, but we're not stopping.
6039 This requires no further action. */
6040 }
6041 break;
6042 case bp_longjmp:
6043 case bp_longjmp_call_dummy:
6044 case bp_exception:
6045 if (bs->stop)
6046 {
6047 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
6048 retval.is_longjmp = bptype != bp_exception;
6049 }
6050 else
6051 this_action = BPSTAT_WHAT_SINGLE;
6052 break;
6053 case bp_longjmp_resume:
6054 case bp_exception_resume:
6055 if (bs->stop)
6056 {
6057 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
6058 retval.is_longjmp = bptype == bp_longjmp_resume;
6059 }
6060 else
6061 this_action = BPSTAT_WHAT_SINGLE;
6062 break;
6063 case bp_step_resume:
6064 if (bs->stop)
6065 this_action = BPSTAT_WHAT_STEP_RESUME;
6066 else
6067 {
6068 /* It is for the wrong frame. */
6069 this_action = BPSTAT_WHAT_SINGLE;
6070 }
6071 break;
6072 case bp_hp_step_resume:
6073 if (bs->stop)
6074 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
6075 else
6076 {
6077 /* It is for the wrong frame. */
6078 this_action = BPSTAT_WHAT_SINGLE;
6079 }
6080 break;
6081 case bp_watchpoint_scope:
6082 case bp_thread_event:
6083 case bp_overlay_event:
6084 case bp_longjmp_master:
6085 case bp_std_terminate_master:
6086 case bp_exception_master:
6087 this_action = BPSTAT_WHAT_SINGLE;
6088 break;
6089 case bp_catchpoint:
6090 if (bs->stop)
6091 {
6092 if (bs->print)
6093 this_action = BPSTAT_WHAT_STOP_NOISY;
6094 else
6095 this_action = BPSTAT_WHAT_STOP_SILENT;
6096 }
6097 else
6098 {
6099 /* Some catchpoints are implemented with breakpoints.
6100 For those, we need to step over the breakpoint. */
6101 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
6102 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
6103 this_action = BPSTAT_WHAT_SINGLE;
6104 }
6105 break;
6106 case bp_jit_event:
6107 this_action = BPSTAT_WHAT_SINGLE;
6108 break;
6109 case bp_call_dummy:
6110 /* Make sure the action is stop (silent or noisy),
6111 so infrun.c pops the dummy frame. */
6112 retval.call_dummy = STOP_STACK_DUMMY;
6113 this_action = BPSTAT_WHAT_STOP_SILENT;
6114 break;
6115 case bp_std_terminate:
6116 /* Make sure the action is stop (silent or noisy),
6117 so infrun.c pops the dummy frame. */
6118 retval.call_dummy = STOP_STD_TERMINATE;
6119 this_action = BPSTAT_WHAT_STOP_SILENT;
6120 break;
6121 case bp_tracepoint:
6122 case bp_fast_tracepoint:
6123 case bp_static_tracepoint:
6124 case bp_static_marker_tracepoint:
6125 /* Tracepoint hits should not be reported back to GDB, and
6126 if one got through somehow, it should have been filtered
6127 out already. */
6128 internal_error (_("bpstat_what: tracepoint encountered"));
6129 break;
6130 case bp_gnu_ifunc_resolver:
6131 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
6132 this_action = BPSTAT_WHAT_SINGLE;
6133 break;
6134 case bp_gnu_ifunc_resolver_return:
6135 /* The breakpoint will be removed, execution will restart from the
6136 PC of the former breakpoint. */
6137 this_action = BPSTAT_WHAT_KEEP_CHECKING;
6138 break;
6139
6140 case bp_dprintf:
6141 if (bs->stop)
6142 this_action = BPSTAT_WHAT_STOP_SILENT;
6143 else
6144 this_action = BPSTAT_WHAT_SINGLE;
6145 break;
6146
6147 default:
6148 internal_error (_("bpstat_what: unhandled bptype %d"), (int) bptype);
6149 }
6150
6151 retval.main_action = std::max (retval.main_action, this_action);
6152 }
6153
6154 return retval;
6155 }
6156
6157 void
6158 bpstat_run_callbacks (bpstat *bs_head)
6159 {
6160 bpstat *bs;
6161
6162 for (bs = bs_head; bs != NULL; bs = bs->next)
6163 {
6164 struct breakpoint *b = bs->breakpoint_at;
6165
6166 if (b == NULL)
6167 continue;
6168 switch (b->type)
6169 {
6170 case bp_jit_event:
6171 handle_jit_event (bs->bp_location_at->address);
6172 break;
6173 case bp_gnu_ifunc_resolver:
6174 gnu_ifunc_resolver_stop ((code_breakpoint *) b);
6175 break;
6176 case bp_gnu_ifunc_resolver_return:
6177 gnu_ifunc_resolver_return_stop ((code_breakpoint *) b);
6178 break;
6179 }
6180 }
6181 }
6182
6183 /* See breakpoint.h. */
6184
6185 bool
6186 bpstat_should_step ()
6187 {
6188 for (breakpoint &b : all_breakpoints ())
6189 if (breakpoint_enabled (&b)
6190 && b.type == bp_watchpoint
6191 && b.has_locations ())
6192 return true;
6193
6194 return false;
6195 }
6196
6197 /* See breakpoint.h. */
6198
6199 bool
6200 bpstat_causes_stop (bpstat *bs)
6201 {
6202 for (; bs != NULL; bs = bs->next)
6203 if (bs->stop)
6204 return true;
6205
6206 return false;
6207 }
6208
6209 \f
6210
6211 /* Compute a number of spaces suitable to indent the next line
6212 so it starts at the position corresponding to the table column
6213 named COL_NAME in the currently active table of UIOUT. */
6214
6215 static int
6216 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6217 {
6218 int i, total_width, width, align;
6219 const char *text;
6220
6221 total_width = 0;
6222 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
6223 {
6224 if (strcmp (text, col_name) == 0)
6225 return total_width;
6226
6227 total_width += width + 1;
6228 }
6229
6230 return 0;
6231 }
6232
6233 /* Determine if the locations of this breakpoint will have their conditions
6234 evaluated by the target, host or a mix of both. Returns the following:
6235
6236 "host": Host evals condition.
6237 "host or target": Host or Target evals condition.
6238 "target": Target evals condition.
6239 */
6240
6241 static const char *
6242 bp_condition_evaluator (const breakpoint *b)
6243 {
6244 char host_evals = 0;
6245 char target_evals = 0;
6246
6247 if (!b)
6248 return NULL;
6249
6250 if (!is_breakpoint (b))
6251 return NULL;
6252
6253 if (gdb_evaluates_breakpoint_condition_p ()
6254 || !target_supports_evaluation_of_breakpoint_conditions ())
6255 return condition_evaluation_host;
6256
6257 for (bp_location &bl : b->locations ())
6258 {
6259 if (bl.cond_bytecode)
6260 target_evals++;
6261 else
6262 host_evals++;
6263 }
6264
6265 if (host_evals && target_evals)
6266 return condition_evaluation_both;
6267 else if (target_evals)
6268 return condition_evaluation_target;
6269 else
6270 return condition_evaluation_host;
6271 }
6272
6273 /* Determine the breakpoint location's condition evaluator. This is
6274 similar to bp_condition_evaluator, but for locations. */
6275
6276 static const char *
6277 bp_location_condition_evaluator (const struct bp_location *bl)
6278 {
6279 if (bl && !is_breakpoint (bl->owner))
6280 return NULL;
6281
6282 if (gdb_evaluates_breakpoint_condition_p ()
6283 || !target_supports_evaluation_of_breakpoint_conditions ())
6284 return condition_evaluation_host;
6285
6286 if (bl && bl->cond_bytecode)
6287 return condition_evaluation_target;
6288 else
6289 return condition_evaluation_host;
6290 }
6291
6292 /* Print the LOC location out of the list of B->LOC locations. */
6293
6294 static void
6295 print_breakpoint_location (const breakpoint *b, const bp_location *loc)
6296 {
6297 struct ui_out *uiout = current_uiout;
6298
6299 scoped_restore_current_program_space restore_pspace;
6300
6301 if (loc != NULL && loc->shlib_disabled)
6302 loc = NULL;
6303
6304 if (loc != NULL)
6305 set_current_program_space (loc->pspace);
6306
6307 if (b->display_canonical)
6308 uiout->field_string ("what", b->locspec->to_string ());
6309 else if (loc && loc->symtab)
6310 {
6311 const struct symbol *sym = loc->symbol;
6312
6313 if (sym)
6314 {
6315 uiout->text ("in ");
6316 uiout->field_string ("func", sym->print_name (),
6317 function_name_style.style ());
6318 uiout->text (" ");
6319 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6320 uiout->text ("at ");
6321 }
6322 uiout->field_string ("file",
6323 symtab_to_filename_for_display (loc->symtab),
6324 file_name_style.style ());
6325 uiout->text (":");
6326
6327 if (uiout->is_mi_like_p ())
6328 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6329
6330 uiout->field_signed ("line", loc->line_number);
6331 }
6332 else if (loc)
6333 {
6334 string_file stb;
6335
6336 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6337 demangle, "");
6338 uiout->field_stream ("at", stb);
6339 }
6340 else
6341 {
6342 uiout->field_string ("pending", b->locspec->to_string ());
6343 /* If extra_string is available, it could be holding a condition
6344 or dprintf arguments. In either case, make sure it is printed,
6345 too, but only for non-MI streams. */
6346 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6347 {
6348 if (b->type == bp_dprintf)
6349 uiout->text (",");
6350 else
6351 uiout->text (" ");
6352 uiout->text (b->extra_string.get ());
6353 }
6354 }
6355
6356 if (loc && is_breakpoint (b)
6357 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6358 && bp_condition_evaluator (b) == condition_evaluation_both)
6359 {
6360 uiout->text (" (");
6361 uiout->field_string ("evaluated-by",
6362 bp_location_condition_evaluator (loc));
6363 uiout->text (")");
6364 }
6365 }
6366
6367 static const char *
6368 bptype_string (enum bptype type)
6369 {
6370 struct ep_type_description
6371 {
6372 enum bptype type;
6373 const char *description;
6374 };
6375 static struct ep_type_description bptypes[] =
6376 {
6377 {bp_none, "?deleted?"},
6378 {bp_breakpoint, "breakpoint"},
6379 {bp_hardware_breakpoint, "hw breakpoint"},
6380 {bp_single_step, "sw single-step"},
6381 {bp_until, "until"},
6382 {bp_finish, "finish"},
6383 {bp_watchpoint, "watchpoint"},
6384 {bp_hardware_watchpoint, "hw watchpoint"},
6385 {bp_read_watchpoint, "read watchpoint"},
6386 {bp_access_watchpoint, "acc watchpoint"},
6387 {bp_longjmp, "longjmp"},
6388 {bp_longjmp_resume, "longjmp resume"},
6389 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6390 {bp_exception, "exception"},
6391 {bp_exception_resume, "exception resume"},
6392 {bp_step_resume, "step resume"},
6393 {bp_hp_step_resume, "high-priority step resume"},
6394 {bp_watchpoint_scope, "watchpoint scope"},
6395 {bp_call_dummy, "call dummy"},
6396 {bp_std_terminate, "std::terminate"},
6397 {bp_shlib_event, "shlib events"},
6398 {bp_thread_event, "thread events"},
6399 {bp_overlay_event, "overlay events"},
6400 {bp_longjmp_master, "longjmp master"},
6401 {bp_std_terminate_master, "std::terminate master"},
6402 {bp_exception_master, "exception master"},
6403 {bp_catchpoint, "catchpoint"},
6404 {bp_tracepoint, "tracepoint"},
6405 {bp_fast_tracepoint, "fast tracepoint"},
6406 {bp_static_tracepoint, "static tracepoint"},
6407 {bp_static_marker_tracepoint, "static marker tracepoint"},
6408 {bp_dprintf, "dprintf"},
6409 {bp_jit_event, "jit events"},
6410 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6411 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6412 };
6413
6414 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6415 || ((int) type != bptypes[(int) type].type))
6416 internal_error (_("bptypes table does not describe type #%d."),
6417 (int) type);
6418
6419 return bptypes[(int) type].description;
6420 }
6421
6422 /* For MI, output a field named 'thread-groups' with a list as the value.
6423 For CLI, prefix the list with the string 'inf'. */
6424
6425 static void
6426 output_thread_groups (struct ui_out *uiout,
6427 const char *field_name,
6428 const std::vector<int> &inf_nums,
6429 int mi_only)
6430 {
6431 int is_mi = uiout->is_mi_like_p ();
6432
6433 /* For backward compatibility, don't display inferiors in CLI unless
6434 there are several. Always display them for MI. */
6435 if (!is_mi && mi_only)
6436 return;
6437
6438 ui_out_emit_list list_emitter (uiout, field_name);
6439
6440 for (size_t i = 0; i < inf_nums.size (); i++)
6441 {
6442 if (is_mi)
6443 {
6444 char mi_group[10];
6445
6446 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6447 uiout->field_string (NULL, mi_group);
6448 }
6449 else
6450 {
6451 if (i == 0)
6452 uiout->text (" inf ");
6453 else
6454 uiout->text (", ");
6455
6456 uiout->text (plongest (inf_nums[i]));
6457 }
6458 }
6459 }
6460
6461 /* See breakpoint.h. */
6462
6463 bool fix_breakpoint_script_output_globally = false;
6464
6465 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6466 instead of going via breakpoint_ops::print_one. This makes "maint
6467 info breakpoints" show the software breakpoint locations of
6468 catchpoints, which are considered internal implementation
6469 detail. Returns true if RAW_LOC is false and if the breakpoint's
6470 print_one method did something; false otherwise. */
6471
6472 static bool
6473 print_one_breakpoint_location (struct breakpoint *b,
6474 struct bp_location *loc,
6475 int loc_number,
6476 const bp_location **last_loc,
6477 int allflag, bool raw_loc)
6478 {
6479 struct command_line *l;
6480 static char bpenables[] = "nynny";
6481
6482 struct ui_out *uiout = current_uiout;
6483 bool header_of_multiple = false;
6484 bool part_of_multiple = (loc != NULL);
6485 struct value_print_options opts;
6486
6487 get_user_print_options (&opts);
6488
6489 gdb_assert (!loc || loc_number != 0);
6490 /* See comment in print_one_breakpoint concerning treatment of
6491 breakpoints with single disabled location. */
6492 if (loc == NULL
6493 && (b->has_locations ()
6494 && (b->has_multiple_locations ()
6495 || !b->first_loc ().enabled || b->first_loc ().disabled_by_cond)))
6496 header_of_multiple = true;
6497
6498 if (loc == NULL && b->has_locations ())
6499 loc = &b->first_loc ();
6500
6501 annotate_record ();
6502
6503 /* 1 */
6504 annotate_field (0);
6505 if (part_of_multiple)
6506 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6507 else
6508 uiout->field_signed ("number", b->number);
6509
6510 /* 2 */
6511 annotate_field (1);
6512 if (part_of_multiple)
6513 uiout->field_skip ("type");
6514 else
6515 uiout->field_string ("type", bptype_string (b->type));
6516
6517 /* 3 */
6518 annotate_field (2);
6519 if (part_of_multiple)
6520 uiout->field_skip ("disp");
6521 else
6522 uiout->field_string ("disp", bpdisp_text (b->disposition));
6523
6524 /* 4 */
6525 annotate_field (3);
6526 if (part_of_multiple)
6527 {
6528 /* For locations that are disabled because of an invalid
6529 condition, display "N*" on the CLI, where "*" refers to a
6530 footnote below the table. For MI, simply display a "N"
6531 without a footnote. On the CLI, for enabled locations whose
6532 breakpoint is disabled, display "y-". */
6533 auto get_enable_state = [uiout, loc] () -> const char *
6534 {
6535 if (uiout->is_mi_like_p ())
6536 {
6537 if (loc->disabled_by_cond)
6538 return "N";
6539 else if (!loc->enabled)
6540 return "n";
6541 else
6542 return "y";
6543 }
6544 else
6545 {
6546 if (loc->disabled_by_cond)
6547 return "N*";
6548 else if (!loc->enabled)
6549 return "n";
6550 else if (!breakpoint_enabled (loc->owner))
6551 return "y-";
6552 else
6553 return "y";
6554 }
6555 };
6556 uiout->field_string ("enabled", get_enable_state ());
6557 }
6558 else
6559 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6560
6561 /* 5 and 6 */
6562 bool result = false;
6563 if (!raw_loc && b->print_one (last_loc))
6564 result = true;
6565 else
6566 {
6567 if (is_watchpoint (b))
6568 {
6569 struct watchpoint *w = (struct watchpoint *) b;
6570
6571 /* Field 4, the address, is omitted (which makes the columns
6572 not line up too nicely with the headers, but the effect
6573 is relatively readable). */
6574 if (opts.addressprint)
6575 uiout->field_skip ("addr");
6576 annotate_field (5);
6577 uiout->field_string ("what", w->exp_string.get ());
6578 }
6579 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6580 || is_ada_exception_catchpoint (b))
6581 {
6582 if (opts.addressprint)
6583 {
6584 annotate_field (4);
6585 if (header_of_multiple)
6586 uiout->field_string ("addr", "<MULTIPLE>",
6587 metadata_style.style ());
6588 else if (!b->has_locations () || loc->shlib_disabled)
6589 uiout->field_string ("addr", "<PENDING>",
6590 metadata_style.style ());
6591 else
6592 uiout->field_core_addr ("addr",
6593 loc->gdbarch, loc->address);
6594 }
6595 annotate_field (5);
6596 if (!header_of_multiple)
6597 print_breakpoint_location (b, loc);
6598 if (b->has_locations ())
6599 *last_loc = &b->first_loc ();
6600 }
6601 }
6602
6603 if (loc != NULL && !header_of_multiple)
6604 {
6605 std::vector<int> inf_nums;
6606 int mi_only = 1;
6607
6608 for (inferior *inf : all_inferiors ())
6609 {
6610 if (inf->pspace == loc->pspace)
6611 inf_nums.push_back (inf->num);
6612 }
6613
6614 /* For backward compatibility, don't display inferiors in CLI unless
6615 there are several. Always display for MI. */
6616 if (allflag
6617 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6618 && (program_spaces.size () > 1
6619 || number_of_inferiors () > 1)
6620 /* LOC is for existing B, it cannot be in
6621 moribund_locations and thus having NULL OWNER. */
6622 && loc->owner->type != bp_catchpoint))
6623 mi_only = 0;
6624 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6625 }
6626
6627 /* In the MI output, each location of a thread or task specific
6628 breakpoint includes the relevant thread or task ID. This is done for
6629 backwards compatibility reasons.
6630
6631 For the CLI output, the thread/task information is printed on a
6632 separate line, see the 'stop only in thread' and 'stop only in task'
6633 output below. */
6634 if (part_of_multiple && uiout->is_mi_like_p ())
6635 {
6636 if (b->thread != -1)
6637 uiout->field_signed ("thread", b->thread);
6638 else if (b->task != -1)
6639 uiout->field_signed ("task", b->task);
6640 else if (b->inferior != -1)
6641 uiout->field_signed ("inferior", b->inferior);
6642 }
6643
6644 uiout->text ("\n");
6645
6646 if (!part_of_multiple)
6647 b->print_one_detail (uiout);
6648
6649 if (part_of_multiple && frame_id_p (b->frame_id))
6650 {
6651 annotate_field (6);
6652 uiout->text ("\tstop only in stack frame at ");
6653 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6654 the frame ID. */
6655 uiout->field_core_addr ("frame",
6656 b->gdbarch, b->frame_id.stack_addr);
6657 uiout->text ("\n");
6658 }
6659
6660 if (!part_of_multiple && b->cond_string)
6661 {
6662 annotate_field (7);
6663 if (is_tracepoint (b))
6664 uiout->text ("\ttrace only if ");
6665 else
6666 uiout->text ("\tstop only if ");
6667 uiout->field_string ("cond", b->cond_string.get ());
6668
6669 /* Print whether the target is doing the breakpoint's condition
6670 evaluation. If GDB is doing the evaluation, don't print anything. */
6671 if (is_breakpoint (b)
6672 && breakpoint_condition_evaluation_mode ()
6673 == condition_evaluation_target)
6674 {
6675 uiout->message (" (%pF evals)",
6676 string_field ("evaluated-by",
6677 bp_condition_evaluator (b)));
6678 }
6679 uiout->text ("\n");
6680 }
6681
6682 if (!part_of_multiple && b->thread != -1)
6683 {
6684 /* FIXME should make an annotation for this. */
6685 uiout->text ("\tstop only in thread ");
6686 if (uiout->is_mi_like_p ())
6687 uiout->field_signed ("thread", b->thread);
6688 else
6689 {
6690 struct thread_info *thr = find_thread_global_id (b->thread);
6691
6692 uiout->field_string ("thread", print_thread_id (thr));
6693 }
6694 uiout->text ("\n");
6695 }
6696
6697 if (!part_of_multiple && b->task != -1)
6698 {
6699 uiout->text ("\tstop only in task ");
6700 uiout->field_signed ("task", b->task);
6701 uiout->text ("\n");
6702 }
6703
6704 if (!part_of_multiple && b->inferior != -1)
6705 {
6706 uiout->text ("\tstop only in inferior ");
6707 uiout->field_signed ("inferior", b->inferior);
6708 uiout->text ("\n");
6709 }
6710
6711 if (!part_of_multiple)
6712 {
6713 if (b->hit_count)
6714 {
6715 /* FIXME should make an annotation for this. */
6716 if (is_catchpoint (b))
6717 uiout->text ("\tcatchpoint");
6718 else if (is_tracepoint (b))
6719 uiout->text ("\ttracepoint");
6720 else
6721 uiout->text ("\tbreakpoint");
6722 uiout->text (" already hit ");
6723 uiout->field_signed ("times", b->hit_count);
6724 if (b->hit_count == 1)
6725 uiout->text (" time\n");
6726 else
6727 uiout->text (" times\n");
6728 }
6729 else
6730 {
6731 /* Output the count also if it is zero, but only if this is mi. */
6732 if (uiout->is_mi_like_p ())
6733 uiout->field_signed ("times", b->hit_count);
6734 }
6735 }
6736
6737 if (!part_of_multiple && b->ignore_count)
6738 {
6739 annotate_field (8);
6740 uiout->message ("\tignore next %pF hits\n",
6741 signed_field ("ignore", b->ignore_count));
6742 }
6743
6744 /* Note that an enable count of 1 corresponds to "enable once"
6745 behavior, which is reported by the combination of enablement and
6746 disposition, so we don't need to mention it here. */
6747 if (!part_of_multiple && b->enable_count > 1)
6748 {
6749 annotate_field (8);
6750 uiout->text ("\tdisable after ");
6751 /* Tweak the wording to clarify that ignore and enable counts
6752 are distinct, and have additive effect. */
6753 if (b->ignore_count)
6754 uiout->text ("additional ");
6755 else
6756 uiout->text ("next ");
6757 uiout->field_signed ("enable", b->enable_count);
6758 uiout->text (" hits\n");
6759 }
6760
6761 if (!part_of_multiple && is_tracepoint (b))
6762 {
6763 struct tracepoint *tp = (struct tracepoint *) b;
6764
6765 if (tp->traceframe_usage)
6766 {
6767 uiout->text ("\ttrace buffer usage ");
6768 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6769 uiout->text (" bytes\n");
6770 }
6771 }
6772
6773 l = b->commands ? b->commands.get () : NULL;
6774 if (!part_of_multiple && l)
6775 {
6776 annotate_field (9);
6777
6778 bool use_fixed_output =
6779 (uiout->test_flags (fix_breakpoint_script_output)
6780 || fix_breakpoint_script_output_globally);
6781
6782 gdb::optional<ui_out_emit_tuple> tuple_emitter;
6783 gdb::optional<ui_out_emit_list> list_emitter;
6784
6785 if (use_fixed_output)
6786 list_emitter.emplace (uiout, "script");
6787 else
6788 tuple_emitter.emplace (uiout, "script");
6789
6790 print_command_lines (uiout, l, 4);
6791 }
6792
6793 if (is_tracepoint (b))
6794 {
6795 struct tracepoint *t = (struct tracepoint *) b;
6796
6797 if (!part_of_multiple && t->pass_count)
6798 {
6799 annotate_field (10);
6800 uiout->text ("\tpass count ");
6801 uiout->field_signed ("pass", t->pass_count);
6802 uiout->text (" \n");
6803 }
6804
6805 /* Don't display it when tracepoint or tracepoint location is
6806 pending. */
6807 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6808 {
6809 annotate_field (11);
6810
6811 if (uiout->is_mi_like_p ())
6812 uiout->field_string ("installed",
6813 loc->inserted ? "y" : "n");
6814 else
6815 {
6816 if (loc->inserted)
6817 uiout->text ("\t");
6818 else
6819 uiout->text ("\tnot ");
6820 uiout->text ("installed on target\n");
6821 }
6822 }
6823 }
6824
6825 if (uiout->is_mi_like_p () && !part_of_multiple)
6826 {
6827 if (is_watchpoint (b))
6828 {
6829 struct watchpoint *w = (struct watchpoint *) b;
6830
6831 uiout->field_string ("original-location", w->exp_string.get ());
6832 }
6833 else if (b->locspec != nullptr)
6834 {
6835 const char *str = b->locspec->to_string ();
6836 if (str != nullptr)
6837 uiout->field_string ("original-location", str);
6838 }
6839 }
6840
6841 return result;
6842 }
6843
6844 /* See breakpoint.h. */
6845
6846 bool fix_multi_location_breakpoint_output_globally = false;
6847
6848 static void
6849 print_one_breakpoint (breakpoint *b, const bp_location **last_loc, int allflag)
6850 {
6851 struct ui_out *uiout = current_uiout;
6852 bool use_fixed_output
6853 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6854 || fix_multi_location_breakpoint_output_globally);
6855
6856 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6857 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6858 allflag, false);
6859
6860 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6861 are outside. */
6862 if (!use_fixed_output)
6863 bkpt_tuple_emitter.reset ();
6864
6865 /* If this breakpoint has custom print function,
6866 it's already printed. Otherwise, print individual
6867 locations, if any. */
6868 if (!printed || allflag)
6869 {
6870 /* If breakpoint has a single location that is disabled, we
6871 print it as if it had several locations, since otherwise it's
6872 hard to represent "breakpoint enabled, location disabled"
6873 situation.
6874
6875 Note that while hardware watchpoints have several locations
6876 internally, that's not a property exposed to users.
6877
6878 Likewise, while catchpoints may be implemented with
6879 breakpoints (e.g., catch throw), that's not a property
6880 exposed to users. We do however display the internal
6881 breakpoint locations with "maint info breakpoints". */
6882 if (!is_hardware_watchpoint (b)
6883 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6884 || is_ada_exception_catchpoint (b))
6885 && (allflag
6886 || (b->has_locations ()
6887 && (b->has_multiple_locations ()
6888 || !b->first_loc ().enabled
6889 || b->first_loc ().disabled_by_cond))))
6890 {
6891 gdb::optional<ui_out_emit_list> locations_list;
6892
6893 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6894 MI record. For later versions, place breakpoint locations in a
6895 list. */
6896 if (uiout->is_mi_like_p () && use_fixed_output)
6897 locations_list.emplace (uiout, "locations");
6898
6899 int n = 1;
6900 for (bp_location &loc : b->locations ())
6901 {
6902 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6903 print_one_breakpoint_location (b, &loc, n, last_loc,
6904 allflag, allflag);
6905 n++;
6906 }
6907 }
6908 }
6909 }
6910
6911 static int
6912 breakpoint_address_bits (struct breakpoint *b)
6913 {
6914 int print_address_bits = 0;
6915
6916 for (bp_location &loc : b->locations ())
6917 {
6918 if (!bl_address_is_meaningful (&loc))
6919 continue;
6920
6921 int addr_bit = gdbarch_addr_bit (loc.gdbarch);
6922 if (addr_bit > print_address_bits)
6923 print_address_bits = addr_bit;
6924 }
6925
6926 return print_address_bits;
6927 }
6928
6929 /* See breakpoint.h. */
6930
6931 void
6932 print_breakpoint (breakpoint *b)
6933 {
6934 const bp_location *dummy_loc = nullptr;
6935 print_one_breakpoint (b, &dummy_loc, 0);
6936 }
6937
6938 /* Return true if this breakpoint was set by the user, false if it is
6939 internal or momentary. */
6940
6941 int
6942 user_breakpoint_p (struct breakpoint *b)
6943 {
6944 return b->number > 0;
6945 }
6946
6947 /* See breakpoint.h. */
6948
6949 int
6950 pending_breakpoint_p (struct breakpoint *b)
6951 {
6952 return !b->has_locations ();
6953 }
6954
6955 /* Print information on breakpoints (including watchpoints and tracepoints).
6956
6957 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6958 understood by number_or_range_parser. Only breakpoints included in this
6959 list are then printed.
6960
6961 If SHOW_INTERNAL is true, print internal breakpoints.
6962
6963 If FILTER is non-NULL, call it on each breakpoint and only include the
6964 ones for which it returns true.
6965
6966 Return the total number of breakpoints listed. */
6967
6968 static int
6969 breakpoint_1 (const char *bp_num_list, bool show_internal,
6970 bool (*filter) (const struct breakpoint *))
6971 {
6972 const bp_location *last_loc = nullptr;
6973 int nr_printable_breakpoints;
6974 struct value_print_options opts;
6975 int print_address_bits = 0;
6976 int print_type_col_width = 14;
6977 struct ui_out *uiout = current_uiout;
6978 bool has_disabled_by_cond_location = false;
6979
6980 get_user_print_options (&opts);
6981
6982 /* Compute the number of rows in the table, as well as the size
6983 required for address fields. */
6984 nr_printable_breakpoints = 0;
6985 for (breakpoint &b : all_breakpoints ())
6986 {
6987 /* If we have a filter, only list the breakpoints it accepts. */
6988 if (filter && !filter (&b))
6989 continue;
6990
6991 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6992 accept. Skip the others. */
6993 if (bp_num_list != NULL && *bp_num_list != '\0')
6994 {
6995 if (show_internal && parse_and_eval_long (bp_num_list) != b.number)
6996 continue;
6997 if (!show_internal && !number_is_in_list (bp_num_list, b.number))
6998 continue;
6999 }
7000
7001 if (show_internal || user_breakpoint_p (&b))
7002 {
7003 int addr_bit, type_len;
7004
7005 addr_bit = breakpoint_address_bits (&b);
7006 if (addr_bit > print_address_bits)
7007 print_address_bits = addr_bit;
7008
7009 type_len = strlen (bptype_string (b.type));
7010 if (type_len > print_type_col_width)
7011 print_type_col_width = type_len;
7012
7013 nr_printable_breakpoints++;
7014 }
7015 }
7016
7017 {
7018 ui_out_emit_table table_emitter (uiout,
7019 opts.addressprint ? 6 : 5,
7020 nr_printable_breakpoints,
7021 "BreakpointTable");
7022
7023 if (nr_printable_breakpoints > 0)
7024 annotate_breakpoints_headers ();
7025 if (nr_printable_breakpoints > 0)
7026 annotate_field (0);
7027 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
7028 if (nr_printable_breakpoints > 0)
7029 annotate_field (1);
7030 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
7031 if (nr_printable_breakpoints > 0)
7032 annotate_field (2);
7033 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
7034 if (nr_printable_breakpoints > 0)
7035 annotate_field (3);
7036 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
7037 if (opts.addressprint)
7038 {
7039 if (nr_printable_breakpoints > 0)
7040 annotate_field (4);
7041 if (print_address_bits <= 32)
7042 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
7043 else
7044 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
7045 }
7046 if (nr_printable_breakpoints > 0)
7047 annotate_field (5);
7048 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
7049 uiout->table_body ();
7050 if (nr_printable_breakpoints > 0)
7051 annotate_breakpoints_table ();
7052
7053 for (breakpoint &b : all_breakpoints ())
7054 {
7055 QUIT;
7056 /* If we have a filter, only list the breakpoints it accepts. */
7057 if (filter && !filter (&b))
7058 continue;
7059
7060 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
7061 accept. Skip the others. */
7062
7063 if (bp_num_list != NULL && *bp_num_list != '\0')
7064 {
7065 if (show_internal) /* maintenance info breakpoint */
7066 {
7067 if (parse_and_eval_long (bp_num_list) != b.number)
7068 continue;
7069 }
7070 else /* all others */
7071 {
7072 if (!number_is_in_list (bp_num_list, b.number))
7073 continue;
7074 }
7075 }
7076 /* We only print out user settable breakpoints unless the
7077 show_internal is set. */
7078 if (show_internal || user_breakpoint_p (&b))
7079 {
7080 print_one_breakpoint (&b, &last_loc, show_internal);
7081 for (bp_location &loc : b.locations ())
7082 if (loc.disabled_by_cond)
7083 has_disabled_by_cond_location = true;
7084 }
7085 }
7086 }
7087
7088 if (nr_printable_breakpoints == 0)
7089 {
7090 /* If there's a filter, let the caller decide how to report
7091 empty list. */
7092 if (!filter)
7093 {
7094 if (bp_num_list == NULL || *bp_num_list == '\0')
7095 uiout->message ("No breakpoints or watchpoints.\n");
7096 else
7097 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
7098 bp_num_list);
7099 }
7100 }
7101 else
7102 {
7103 if (last_loc && !server_command)
7104 set_next_address (last_loc->gdbarch, last_loc->address);
7105
7106 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
7107 uiout->message (_("(*): Breakpoint condition is invalid at this "
7108 "location.\n"));
7109 }
7110
7111 /* FIXME? Should this be moved up so that it is only called when
7112 there have been breakpoints? */
7113 annotate_breakpoints_table_end ();
7114
7115 return nr_printable_breakpoints;
7116 }
7117
7118 /* Display the value of default-collect in a way that is generally
7119 compatible with the breakpoint list. */
7120
7121 static void
7122 default_collect_info (void)
7123 {
7124 struct ui_out *uiout = current_uiout;
7125
7126 /* If it has no value (which is frequently the case), say nothing; a
7127 message like "No default-collect." gets in user's face when it's
7128 not wanted. */
7129 if (default_collect.empty ())
7130 return;
7131
7132 /* The following phrase lines up nicely with per-tracepoint collect
7133 actions. */
7134 uiout->text ("default collect ");
7135 uiout->field_string ("default-collect", default_collect);
7136 uiout->text (" \n");
7137 }
7138
7139 static void
7140 info_breakpoints_command (const char *args, int from_tty)
7141 {
7142 breakpoint_1 (args, false, NULL);
7143
7144 default_collect_info ();
7145 }
7146
7147 static void
7148 info_watchpoints_command (const char *args, int from_tty)
7149 {
7150 int num_printed = breakpoint_1 (args, false, is_watchpoint);
7151 struct ui_out *uiout = current_uiout;
7152
7153 if (num_printed == 0)
7154 {
7155 if (args == NULL || *args == '\0')
7156 uiout->message ("No watchpoints.\n");
7157 else
7158 uiout->message ("No watchpoint matching '%s'.\n", args);
7159 }
7160 }
7161
7162 static void
7163 maintenance_info_breakpoints (const char *args, int from_tty)
7164 {
7165 breakpoint_1 (args, true, NULL);
7166
7167 default_collect_info ();
7168 }
7169
7170 static bool
7171 breakpoint_has_pc (struct breakpoint *b,
7172 struct program_space *pspace,
7173 CORE_ADDR pc, struct obj_section *section)
7174 {
7175 for (bp_location &bl : b->locations ())
7176 {
7177 if (bl.pspace == pspace
7178 && bl.address == pc
7179 && (!overlay_debugging || bl.section == section))
7180 return true;
7181 }
7182 return false;
7183 }
7184
7185 /* See breakpoint.h. */
7186
7187 void
7188 describe_other_breakpoints (struct gdbarch *gdbarch,
7189 struct program_space *pspace, CORE_ADDR pc,
7190 struct obj_section *section, int thread)
7191 {
7192 int others = 0;
7193
7194 for (breakpoint &b : all_breakpoints ())
7195 others += (user_breakpoint_p (&b)
7196 && breakpoint_has_pc (&b, pspace, pc, section));
7197
7198 if (others > 0)
7199 {
7200 if (others == 1)
7201 gdb_printf (_("Note: breakpoint "));
7202 else /* if (others == ???) */
7203 gdb_printf (_("Note: breakpoints "));
7204 for (breakpoint &b : all_breakpoints ())
7205 if (user_breakpoint_p (&b)
7206 && breakpoint_has_pc (&b, pspace, pc, section))
7207 {
7208 others--;
7209 gdb_printf ("%d", b.number);
7210 if (b.thread == -1 && thread != -1)
7211 gdb_printf (" (all threads)");
7212 else if (b.thread != -1)
7213 {
7214 struct thread_info *thr = find_thread_global_id (b.thread);
7215 gdb_printf (" (thread %s)", print_thread_id (thr));
7216 }
7217 else if (b.task != -1)
7218 gdb_printf (" (task %d)", b.task);
7219 gdb_printf ("%s%s ",
7220 ((b.enable_state == bp_disabled
7221 || b.enable_state == bp_call_disabled)
7222 ? " (disabled)"
7223 : ""),
7224 (others > 1) ? ","
7225 : ((others == 1) ? " and" : ""));
7226 }
7227 current_uiout->message (_("also set at pc %ps.\n"),
7228 styled_string (address_style.style (),
7229 paddress (gdbarch, pc)));
7230 }
7231 }
7232 \f
7233
7234 /* Return true iff it is meaningful to use the address member of LOC.
7235 For some breakpoint types, the locations' address members are
7236 irrelevant and it makes no sense to attempt to compare them to
7237 other addresses (or use them for any other purpose either).
7238
7239 More specifically, software watchpoints and catchpoints that are
7240 not backed by breakpoints always have a zero valued location
7241 address and we don't want to mark breakpoints of any of these types
7242 to be a duplicate of an actual breakpoint location at address
7243 zero. */
7244
7245 static bool
7246 bl_address_is_meaningful (bp_location *loc)
7247 {
7248 return loc->loc_type != bp_loc_other;
7249 }
7250
7251 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7252 true if LOC1 and LOC2 represent the same watchpoint location. */
7253
7254 static bool
7255 watchpoint_locations_match (const struct bp_location *loc1,
7256 const struct bp_location *loc2)
7257 {
7258 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7259 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7260
7261 /* Both of them must exist. */
7262 gdb_assert (w1 != NULL);
7263 gdb_assert (w2 != NULL);
7264
7265 /* If the target can evaluate the condition expression in hardware,
7266 then we we need to insert both watchpoints even if they are at
7267 the same place. Otherwise the watchpoint will only trigger when
7268 the condition of whichever watchpoint was inserted evaluates to
7269 true, not giving a chance for GDB to check the condition of the
7270 other watchpoint. */
7271 if ((w1->cond_exp
7272 && target_can_accel_watchpoint_condition (loc1->address,
7273 loc1->length,
7274 loc1->watchpoint_type,
7275 w1->cond_exp.get ()))
7276 || (w2->cond_exp
7277 && target_can_accel_watchpoint_condition (loc2->address,
7278 loc2->length,
7279 loc2->watchpoint_type,
7280 w2->cond_exp.get ())))
7281 return false;
7282
7283 /* Note that this checks the owner's type, not the location's. In
7284 case the target does not support read watchpoints, but does
7285 support access watchpoints, we'll have bp_read_watchpoint
7286 watchpoints with hw_access locations. Those should be considered
7287 duplicates of hw_read locations. The hw_read locations will
7288 become hw_access locations later. */
7289 return (loc1->owner->type == loc2->owner->type
7290 && loc1->pspace->aspace == loc2->pspace->aspace
7291 && loc1->address == loc2->address
7292 && loc1->length == loc2->length);
7293 }
7294
7295 /* See breakpoint.h. */
7296
7297 int
7298 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
7299 const address_space *aspace2, CORE_ADDR addr2)
7300 {
7301 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7302 || aspace1 == aspace2)
7303 && addr1 == addr2);
7304 }
7305
7306 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7307 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7308 matches ASPACE2. On targets that have global breakpoints, the address
7309 space doesn't really matter. */
7310
7311 static bool
7312 breakpoint_address_match_range (const address_space *aspace1,
7313 CORE_ADDR addr1,
7314 int len1, const address_space *aspace2,
7315 CORE_ADDR addr2)
7316 {
7317 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7318 || aspace1 == aspace2)
7319 && addr2 >= addr1 && addr2 < addr1 + len1);
7320 }
7321
7322 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7323 a ranged breakpoint. In most targets, a match happens only if ASPACE
7324 matches the breakpoint's address space. On targets that have global
7325 breakpoints, the address space doesn't really matter. */
7326
7327 static bool
7328 breakpoint_location_address_match (struct bp_location *bl,
7329 const address_space *aspace,
7330 CORE_ADDR addr)
7331 {
7332 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7333 aspace, addr)
7334 || (bl->length
7335 && breakpoint_address_match_range (bl->pspace->aspace,
7336 bl->address, bl->length,
7337 aspace, addr)));
7338 }
7339
7340 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7341 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7342 match happens only if ASPACE matches the breakpoint's address
7343 space. On targets that have global breakpoints, the address space
7344 doesn't really matter. */
7345
7346 static bool
7347 breakpoint_location_address_range_overlap (struct bp_location *bl,
7348 const address_space *aspace,
7349 CORE_ADDR addr, int len)
7350 {
7351 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7352 || bl->pspace->aspace == aspace)
7353 {
7354 int bl_len = bl->length != 0 ? bl->length : 1;
7355
7356 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7357 return 1;
7358 }
7359 return 0;
7360 }
7361
7362 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7363 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7364 true, otherwise returns false. */
7365
7366 static bool
7367 tracepoint_locations_match (const struct bp_location *loc1,
7368 const struct bp_location *loc2)
7369 {
7370 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7371 /* Since tracepoint locations are never duplicated with others', tracepoint
7372 locations at the same address of different tracepoints are regarded as
7373 different locations. */
7374 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7375 else
7376 return false;
7377 }
7378
7379 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7380 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7381 the same location. If SW_HW_BPS_MATCH is true, then software
7382 breakpoint locations and hardware breakpoint locations match,
7383 otherwise they don't. */
7384
7385 static bool
7386 breakpoint_locations_match (const struct bp_location *loc1,
7387 const struct bp_location *loc2,
7388 bool sw_hw_bps_match)
7389 {
7390 int hw_point1, hw_point2;
7391
7392 /* Both of them must not be in moribund_locations. */
7393 gdb_assert (loc1->owner != NULL);
7394 gdb_assert (loc2->owner != NULL);
7395
7396 hw_point1 = is_hardware_watchpoint (loc1->owner);
7397 hw_point2 = is_hardware_watchpoint (loc2->owner);
7398
7399 if (hw_point1 != hw_point2)
7400 return false;
7401 else if (hw_point1)
7402 return watchpoint_locations_match (loc1, loc2);
7403 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7404 return tracepoint_locations_match (loc1, loc2);
7405 else
7406 /* We compare bp_location.length in order to cover ranged
7407 breakpoints. Keep this in sync with
7408 bp_location_is_less_than. */
7409 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7410 loc2->pspace->aspace, loc2->address)
7411 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7412 && loc1->length == loc2->length);
7413 }
7414
7415 static void
7416 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7417 int bnum, bool have_bnum)
7418 {
7419 /* The longest string possibly returned by hex_string_custom
7420 is 50 chars. These must be at least that big for safety. */
7421 char astr1[64];
7422 char astr2[64];
7423
7424 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7425 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7426 if (have_bnum)
7427 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7428 bnum, astr1, astr2);
7429 else
7430 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7431 }
7432
7433 /* Adjust a breakpoint's address to account for architectural
7434 constraints on breakpoint placement. Return the adjusted address.
7435 Note: Very few targets require this kind of adjustment. For most
7436 targets, this function is simply the identity function. */
7437
7438 static CORE_ADDR
7439 adjust_breakpoint_address (struct gdbarch *gdbarch,
7440 CORE_ADDR bpaddr, enum bptype bptype,
7441 struct program_space *pspace)
7442 {
7443 gdb_assert (pspace != nullptr);
7444
7445 if (bptype == bp_watchpoint
7446 || bptype == bp_hardware_watchpoint
7447 || bptype == bp_read_watchpoint
7448 || bptype == bp_access_watchpoint
7449 || bptype == bp_catchpoint)
7450 {
7451 /* Watchpoints and the various bp_catch_* eventpoints should not
7452 have their addresses modified. */
7453 return bpaddr;
7454 }
7455 else if (bptype == bp_single_step)
7456 {
7457 /* Single-step breakpoints should not have their addresses
7458 modified. If there's any architectural constrain that
7459 applies to this address, then it should have already been
7460 taken into account when the breakpoint was created in the
7461 first place. If we didn't do this, stepping through e.g.,
7462 Thumb-2 IT blocks would break. */
7463 return bpaddr;
7464 }
7465 else
7466 {
7467 CORE_ADDR adjusted_bpaddr = bpaddr;
7468
7469 /* Some targets have architectural constraints on the placement
7470 of breakpoint instructions. Obtain the adjusted address. */
7471 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7472 {
7473 /* Targets that implement this adjustment function will likely
7474 inspect either the symbol table, target memory at BPADDR, or
7475 even state registers, so ensure a suitable thread (and its
7476 associated program space) are currently selected. */
7477 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7478 switch_to_program_space_and_thread (pspace);
7479 adjusted_bpaddr
7480 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7481 }
7482
7483 adjusted_bpaddr
7484 = gdbarch_remove_non_address_bits (gdbarch, adjusted_bpaddr);
7485
7486 /* An adjusted breakpoint address can significantly alter
7487 a user's expectations. Print a warning if an adjustment
7488 is required. */
7489 if (adjusted_bpaddr != bpaddr)
7490 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, false);
7491
7492 return adjusted_bpaddr;
7493 }
7494 }
7495
7496 static bp_loc_type
7497 bp_location_from_bp_type (bptype type)
7498 {
7499 switch (type)
7500 {
7501 case bp_breakpoint:
7502 case bp_single_step:
7503 case bp_until:
7504 case bp_finish:
7505 case bp_longjmp:
7506 case bp_longjmp_resume:
7507 case bp_longjmp_call_dummy:
7508 case bp_exception:
7509 case bp_exception_resume:
7510 case bp_step_resume:
7511 case bp_hp_step_resume:
7512 case bp_watchpoint_scope:
7513 case bp_call_dummy:
7514 case bp_std_terminate:
7515 case bp_shlib_event:
7516 case bp_thread_event:
7517 case bp_overlay_event:
7518 case bp_jit_event:
7519 case bp_longjmp_master:
7520 case bp_std_terminate_master:
7521 case bp_exception_master:
7522 case bp_gnu_ifunc_resolver:
7523 case bp_gnu_ifunc_resolver_return:
7524 case bp_dprintf:
7525 return bp_loc_software_breakpoint;
7526
7527 case bp_hardware_breakpoint:
7528 return bp_loc_hardware_breakpoint;
7529
7530 case bp_hardware_watchpoint:
7531 case bp_read_watchpoint:
7532 case bp_access_watchpoint:
7533 return bp_loc_hardware_watchpoint;
7534
7535 case bp_watchpoint:
7536 return bp_loc_software_watchpoint;
7537
7538 case bp_tracepoint:
7539 case bp_fast_tracepoint:
7540 case bp_static_tracepoint:
7541 case bp_static_marker_tracepoint:
7542 return bp_loc_tracepoint;
7543
7544 case bp_catchpoint:
7545 return bp_loc_other;
7546
7547 default:
7548 internal_error (_("unknown breakpoint type"));
7549 }
7550 }
7551
7552 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7553 {
7554 this->owner = owner;
7555 this->cond_bytecode = NULL;
7556 this->shlib_disabled = 0;
7557 this->enabled = 1;
7558 this->disabled_by_cond = false;
7559
7560 this->loc_type = type;
7561
7562 if (this->loc_type == bp_loc_software_breakpoint
7563 || this->loc_type == bp_loc_hardware_breakpoint)
7564 mark_breakpoint_location_modified (this);
7565
7566 incref ();
7567 }
7568
7569 bp_location::bp_location (breakpoint *owner)
7570 : bp_location::bp_location (owner,
7571 bp_location_from_bp_type (owner->type))
7572 {
7573 }
7574
7575 /* See breakpoint.h. */
7576
7577 std::string
7578 bp_location::to_string () const
7579 {
7580 string_file stb;
7581 ui_out_redirect_pop redir (current_uiout, &stb);
7582 print_breakpoint_location (this->owner, this);
7583 return stb.string ();
7584 }
7585
7586 /* Decrement reference count. If the reference count reaches 0,
7587 destroy the bp_location. Sets *BLP to NULL. */
7588
7589 static void
7590 decref_bp_location (struct bp_location **blp)
7591 {
7592 bp_location_ref_policy::decref (*blp);
7593 *blp = NULL;
7594 }
7595
7596 /* Add breakpoint B at the end of the global breakpoint chain. */
7597
7598 static breakpoint *
7599 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7600 {
7601 /* Add this breakpoint to the end of the chain so that a list of
7602 breakpoints will come out in order of increasing numbers. */
7603
7604 breakpoint_chain.push_back (*b.release ());
7605
7606 return &breakpoint_chain.back ();
7607 }
7608
7609 /* Initialize loc->function_name. */
7610
7611 static void
7612 set_breakpoint_location_function (struct bp_location *loc)
7613 {
7614 gdb_assert (loc->owner != NULL);
7615
7616 if (loc->owner->type == bp_breakpoint
7617 || loc->owner->type == bp_hardware_breakpoint
7618 || is_tracepoint (loc->owner))
7619 {
7620 const char *function_name;
7621
7622 if (loc->msymbol != NULL
7623 && (loc->msymbol->type () == mst_text_gnu_ifunc
7624 || loc->msymbol->type () == mst_data_gnu_ifunc))
7625 {
7626 struct breakpoint *b = loc->owner;
7627
7628 function_name = loc->msymbol->linkage_name ();
7629
7630 if (b->type == bp_breakpoint
7631 && b->has_single_location ()
7632 && b->related_breakpoint == b)
7633 {
7634 /* Create only the whole new breakpoint of this type but do not
7635 mess more complicated breakpoints with multiple locations. */
7636 b->type = bp_gnu_ifunc_resolver;
7637 /* Remember the resolver's address for use by the return
7638 breakpoint. */
7639 loc->related_address = loc->address;
7640 }
7641 }
7642 else
7643 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7644
7645 if (function_name)
7646 loc->function_name = make_unique_xstrdup (function_name);
7647 }
7648 }
7649
7650 /* Attempt to determine architecture of location identified by SAL. */
7651 struct gdbarch *
7652 get_sal_arch (struct symtab_and_line sal)
7653 {
7654 if (sal.section)
7655 return sal.section->objfile->arch ();
7656 if (sal.symtab)
7657 return sal.symtab->compunit ()->objfile ()->arch ();
7658
7659 return NULL;
7660 }
7661
7662 /* Call this routine when stepping and nexting to enable a breakpoint
7663 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7664 initiated the operation. */
7665
7666 void
7667 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7668 {
7669 int thread = tp->global_num;
7670
7671 /* To avoid having to rescan all objfile symbols at every step,
7672 we maintain a list of continually-inserted but always disabled
7673 longjmp "master" breakpoints. Here, we simply create momentary
7674 clones of those and enable them for the requested thread. */
7675 for (breakpoint &b : all_breakpoints_safe ())
7676 if (b.pspace == current_program_space
7677 && (b.type == bp_longjmp_master
7678 || b.type == bp_exception_master))
7679 {
7680 bptype type = b.type == bp_longjmp_master ? bp_longjmp : bp_exception;
7681 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7682 after their removal. */
7683 momentary_breakpoint_from_master (&b, type, 1, thread);
7684 }
7685
7686 tp->initiating_frame = frame;
7687 }
7688
7689 /* Delete all longjmp breakpoints from THREAD. */
7690 void
7691 delete_longjmp_breakpoint (int thread)
7692 {
7693 for (breakpoint &b : all_breakpoints_safe ())
7694 if (b.type == bp_longjmp || b.type == bp_exception)
7695 {
7696 if (b.thread == thread)
7697 {
7698 gdb_assert (b.inferior == -1);
7699 delete_breakpoint (&b);
7700 }
7701 }
7702 }
7703
7704 void
7705 delete_longjmp_breakpoint_at_next_stop (int thread)
7706 {
7707 for (breakpoint &b : all_breakpoints_safe ())
7708 if (b.type == bp_longjmp || b.type == bp_exception)
7709 {
7710 if (b.thread == thread)
7711 {
7712 gdb_assert (b.inferior == -1);
7713 b.disposition = disp_del_at_next_stop;
7714 }
7715 }
7716 }
7717
7718 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7719 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7720 pointer to any of them. Return NULL if this system cannot place longjmp
7721 breakpoints. */
7722
7723 struct breakpoint *
7724 set_longjmp_breakpoint_for_call_dummy (void)
7725 {
7726 breakpoint *retval = nullptr;
7727
7728 for (breakpoint &b : all_breakpoints ())
7729 if (b.pspace == current_program_space && b.type == bp_longjmp_master)
7730 {
7731 int thread = inferior_thread ()->global_num;
7732 breakpoint *new_b
7733 = momentary_breakpoint_from_master (&b, bp_longjmp_call_dummy,
7734 1, thread);
7735
7736 /* Link NEW_B into the chain of RETVAL breakpoints. */
7737
7738 gdb_assert (new_b->related_breakpoint == new_b);
7739 if (retval == NULL)
7740 retval = new_b;
7741 new_b->related_breakpoint = retval;
7742 while (retval->related_breakpoint != new_b->related_breakpoint)
7743 retval = retval->related_breakpoint;
7744 retval->related_breakpoint = new_b;
7745 }
7746
7747 return retval;
7748 }
7749
7750 /* Verify all existing dummy frames and their associated breakpoints for
7751 TP. Remove those which can no longer be found in the current frame
7752 stack.
7753
7754 If the unwind fails then there is not sufficient information to discard
7755 dummy frames. In this case, elide the clean up and the dummy frames will
7756 be cleaned up next time this function is called from a location where
7757 unwinding is possible. */
7758
7759 void
7760 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7761 {
7762 /* We would need to delete breakpoints other than the current one while
7763 iterating, so all_breakpoints_safe is not sufficient to make that safe.
7764 Save all breakpoints to delete in that set and delete them at the end. */
7765 std::unordered_set<breakpoint *> to_delete;
7766
7767 for (struct breakpoint &b : all_breakpoints ())
7768 {
7769 if (b.type == bp_longjmp_call_dummy && b.thread == tp->global_num)
7770 {
7771 gdb_assert (b.inferior == -1);
7772 struct breakpoint *dummy_b = b.related_breakpoint;
7773
7774 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7775 chained off b->related_breakpoint. */
7776 while (dummy_b != &b && dummy_b->type != bp_call_dummy)
7777 dummy_b = dummy_b->related_breakpoint;
7778
7779 /* If there was no bp_call_dummy breakpoint then there's nothing
7780 more to do. Or, if the dummy frame associated with the
7781 bp_call_dummy is still on the stack then we need to leave this
7782 bp_call_dummy in place. */
7783 if (dummy_b->type != bp_call_dummy
7784 || frame_find_by_id (dummy_b->frame_id) != NULL)
7785 continue;
7786
7787 /* We didn't find the dummy frame on the stack, this could be
7788 because we have longjmp'd to a stack frame that is previous to
7789 the dummy frame, or it could be because the stack unwind is
7790 broken at some point between the longjmp frame and the dummy
7791 frame.
7792
7793 Next we figure out why the stack unwind stopped. If it looks
7794 like the unwind is complete then we assume the dummy frame has
7795 been jumped over, however, if the unwind stopped for an
7796 unexpected reason then we assume the stack unwind is currently
7797 broken, and that we will (eventually) return to the dummy
7798 frame.
7799
7800 It might be tempting to consider using frame_id_inner here, but
7801 that is not safe. There is no guarantee that the stack frames
7802 we are looking at here are even on the same stack as the
7803 original dummy frame, hence frame_id_inner can't be used. See
7804 the comments on frame_id_inner for more details. */
7805 bool unwind_finished_unexpectedly = false;
7806 for (frame_info_ptr fi = get_current_frame (); fi != nullptr; )
7807 {
7808 frame_info_ptr prev = get_prev_frame (fi);
7809 if (prev == nullptr)
7810 {
7811 /* FI is the last stack frame. Why did this frame not
7812 unwind further? */
7813 auto stop_reason = get_frame_unwind_stop_reason (fi);
7814 if (stop_reason != UNWIND_NO_REASON
7815 && stop_reason != UNWIND_OUTERMOST)
7816 unwind_finished_unexpectedly = true;
7817 }
7818 fi = prev;
7819 }
7820 if (unwind_finished_unexpectedly)
7821 continue;
7822
7823 dummy_frame_discard (dummy_b->frame_id, tp);
7824
7825 for (breakpoint *related_breakpoint = b.related_breakpoint;
7826 related_breakpoint != &b;
7827 related_breakpoint = related_breakpoint->related_breakpoint)
7828 to_delete.insert (b.related_breakpoint);
7829
7830 to_delete.insert (&b);
7831 }
7832 }
7833
7834 for (breakpoint *b : to_delete)
7835 delete_breakpoint (b);
7836 }
7837
7838 void
7839 enable_overlay_breakpoints (void)
7840 {
7841 for (breakpoint &b : all_breakpoints ())
7842 if (b.type == bp_overlay_event)
7843 {
7844 b.enable_state = bp_enabled;
7845 update_global_location_list (UGLL_MAY_INSERT);
7846 overlay_events_enabled = 1;
7847 }
7848 }
7849
7850 void
7851 disable_overlay_breakpoints (void)
7852 {
7853 for (breakpoint &b : all_breakpoints ())
7854 if (b.type == bp_overlay_event)
7855 {
7856 b.enable_state = bp_disabled;
7857 update_global_location_list (UGLL_DONT_INSERT);
7858 overlay_events_enabled = 0;
7859 }
7860 }
7861
7862 /* Set an active std::terminate breakpoint for each std::terminate
7863 master breakpoint. */
7864 void
7865 set_std_terminate_breakpoint (void)
7866 {
7867 for (breakpoint &b : all_breakpoints_safe ())
7868 if (b.pspace == current_program_space
7869 && b.type == bp_std_terminate_master)
7870 {
7871 momentary_breakpoint_from_master (&b, bp_std_terminate, 1,
7872 inferior_thread ()->global_num);
7873 }
7874 }
7875
7876 /* Delete all the std::terminate breakpoints. */
7877 void
7878 delete_std_terminate_breakpoint (void)
7879 {
7880 for (breakpoint &b : all_breakpoints_safe ())
7881 if (b.type == bp_std_terminate)
7882 delete_breakpoint (&b);
7883 }
7884
7885 struct breakpoint *
7886 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7887 {
7888 struct breakpoint *b;
7889
7890 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
7891
7892 b->enable_state = bp_enabled;
7893 /* locspec has to be used or breakpoint_re_set will delete me. */
7894 b->locspec = new_address_location_spec (b->first_loc ().address, NULL, 0);
7895
7896 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7897
7898 return b;
7899 }
7900
7901 struct lang_and_radix
7902 {
7903 enum language lang;
7904 int radix;
7905 };
7906
7907 /* Create a breakpoint for JIT code registration and unregistration. */
7908
7909 struct breakpoint *
7910 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7911 {
7912 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
7913 }
7914
7915 /* Remove JIT code registration and unregistration breakpoint(s). */
7916
7917 void
7918 remove_jit_event_breakpoints (void)
7919 {
7920 for (breakpoint &b : all_breakpoints_safe ())
7921 if (b.type == bp_jit_event
7922 && b.first_loc ().pspace == current_program_space)
7923 delete_breakpoint (&b);
7924 }
7925
7926 void
7927 remove_solib_event_breakpoints (void)
7928 {
7929 for (breakpoint &b : all_breakpoints_safe ())
7930 if (b.type == bp_shlib_event
7931 && b.first_loc ().pspace == current_program_space)
7932 delete_breakpoint (&b);
7933 }
7934
7935 /* See breakpoint.h. */
7936
7937 void
7938 remove_solib_event_breakpoints_at_next_stop (void)
7939 {
7940 for (breakpoint &b : all_breakpoints_safe ())
7941 if (b.type == bp_shlib_event
7942 && b.first_loc ().pspace == current_program_space)
7943 b.disposition = disp_del_at_next_stop;
7944 }
7945
7946 /* Helper for create_solib_event_breakpoint /
7947 create_and_insert_solib_event_breakpoint. Allows specifying which
7948 INSERT_MODE to pass through to update_global_location_list. */
7949
7950 static struct breakpoint *
7951 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7952 enum ugll_insert_mode insert_mode)
7953 {
7954 struct breakpoint *b;
7955
7956 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
7957 update_global_location_list_nothrow (insert_mode);
7958 return b;
7959 }
7960
7961 struct breakpoint *
7962 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7963 {
7964 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7965 }
7966
7967 /* See breakpoint.h. */
7968
7969 struct breakpoint *
7970 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7971 {
7972 struct breakpoint *b;
7973
7974 /* Explicitly tell update_global_location_list to insert
7975 locations. */
7976 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7977 if (!b->first_loc ().inserted)
7978 {
7979 delete_breakpoint (b);
7980 return NULL;
7981 }
7982 return b;
7983 }
7984
7985 /* Disable any breakpoints that are on code in shared libraries. Only
7986 apply to enabled breakpoints, disabled ones can just stay disabled. */
7987
7988 void
7989 disable_breakpoints_in_shlibs (void)
7990 {
7991 for (bp_location *loc : all_bp_locations ())
7992 {
7993 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7994 struct breakpoint *b = loc->owner;
7995
7996 /* We apply the check to all breakpoints, including disabled for
7997 those with loc->duplicate set. This is so that when breakpoint
7998 becomes enabled, or the duplicate is removed, gdb will try to
7999 insert all breakpoints. If we don't set shlib_disabled here,
8000 we'll try to insert those breakpoints and fail. */
8001 if (((b->type == bp_breakpoint)
8002 || (b->type == bp_jit_event)
8003 || (b->type == bp_hardware_breakpoint)
8004 || (is_tracepoint (b)))
8005 && loc->pspace == current_program_space
8006 && !loc->shlib_disabled
8007 && solib_name_from_address (loc->pspace, loc->address)
8008 )
8009 {
8010 loc->shlib_disabled = 1;
8011 }
8012 }
8013 }
8014
8015 /* Disable any breakpoints and tracepoints that are in SOLIB upon
8016 notification of unloaded_shlib. Only apply to enabled breakpoints,
8017 disabled ones can just stay disabled. */
8018
8019 static void
8020 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
8021 {
8022 bool disabled_shlib_breaks = false;
8023
8024 for (bp_location *loc : all_bp_locations ())
8025 {
8026 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
8027 struct breakpoint *b = loc->owner;
8028
8029 if (solib->pspace == loc->pspace
8030 && !loc->shlib_disabled
8031 && (((b->type == bp_breakpoint
8032 || b->type == bp_jit_event
8033 || b->type == bp_hardware_breakpoint)
8034 && (loc->loc_type == bp_loc_hardware_breakpoint
8035 || loc->loc_type == bp_loc_software_breakpoint))
8036 || is_tracepoint (b))
8037 && solib_contains_address_p (solib, loc->address))
8038 {
8039 loc->shlib_disabled = 1;
8040 /* At this point, we cannot rely on remove_breakpoint
8041 succeeding so we must mark the breakpoint as not inserted
8042 to prevent future errors occurring in remove_breakpoints. */
8043 loc->inserted = 0;
8044
8045 /* This may cause duplicate notifications for the same breakpoint. */
8046 notify_breakpoint_modified (b);
8047
8048 if (!disabled_shlib_breaks)
8049 {
8050 target_terminal::ours_for_output ();
8051 warning (_("Temporarily disabling breakpoints "
8052 "for unloaded shared library \"%s\""),
8053 solib->so_name);
8054 }
8055 disabled_shlib_breaks = true;
8056 }
8057 }
8058 }
8059
8060 /* Disable any breakpoints and tracepoints in OBJFILE upon
8061 notification of free_objfile. Only apply to enabled breakpoints,
8062 disabled ones can just stay disabled. */
8063
8064 static void
8065 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
8066 {
8067 if (objfile == NULL)
8068 return;
8069
8070 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8071 managed by the user with add-symbol-file/remove-symbol-file.
8072 Similarly to how breakpoints in shared libraries are handled in
8073 response to "nosharedlibrary", mark breakpoints in such modules
8074 shlib_disabled so they end up uninserted on the next global
8075 location list update. Shared libraries not loaded by the user
8076 aren't handled here -- they're already handled in
8077 disable_breakpoints_in_unloaded_shlib, called by solib.c's
8078 solib_unloaded observer. We skip objfiles that are not
8079 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8080 main objfile). */
8081 if ((objfile->flags & OBJF_SHARED) == 0
8082 || (objfile->flags & OBJF_USERLOADED) == 0)
8083 return;
8084
8085 for (breakpoint &b : all_breakpoints ())
8086 {
8087 bool bp_modified = false;
8088
8089 if (!is_breakpoint (&b) && !is_tracepoint (&b))
8090 continue;
8091
8092 for (bp_location &loc : b.locations ())
8093 {
8094 CORE_ADDR loc_addr = loc.address;
8095
8096 if (loc.loc_type != bp_loc_hardware_breakpoint
8097 && loc.loc_type != bp_loc_software_breakpoint)
8098 continue;
8099
8100 if (loc.shlib_disabled != 0)
8101 continue;
8102
8103 if (objfile->pspace != loc.pspace)
8104 continue;
8105
8106 if (loc.loc_type != bp_loc_hardware_breakpoint
8107 && loc.loc_type != bp_loc_software_breakpoint)
8108 continue;
8109
8110 if (is_addr_in_objfile (loc_addr, objfile))
8111 {
8112 loc.shlib_disabled = 1;
8113 /* At this point, we don't know whether the object was
8114 unmapped from the inferior or not, so leave the
8115 inserted flag alone. We'll handle failure to
8116 uninsert quietly, in case the object was indeed
8117 unmapped. */
8118
8119 mark_breakpoint_location_modified (&loc);
8120
8121 bp_modified = true;
8122 }
8123 }
8124
8125 if (bp_modified)
8126 notify_breakpoint_modified (&b);
8127 }
8128 }
8129
8130 /* See breakpoint.h. */
8131
8132 breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
8133 bool temp, const char *cond_string_)
8134 : type (bptype),
8135 disposition (temp ? disp_del : disp_donttouch),
8136 gdbarch (gdbarch_),
8137 language (current_language->la_language),
8138 input_radix (::input_radix),
8139 cond_string (cond_string_ != nullptr
8140 ? make_unique_xstrdup (cond_string_)
8141 : nullptr),
8142 related_breakpoint (this)
8143 {
8144 }
8145
8146 /* See breakpoint.h. */
8147
8148 catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
8149 const char *cond_string)
8150 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
8151 {
8152 add_dummy_location (this, current_program_space);
8153
8154 pspace = current_program_space;
8155 }
8156
8157 /* Notify interpreters and observers that breakpoint B was created. */
8158
8159 static void
8160 notify_breakpoint_created (breakpoint *b)
8161 {
8162 interps_notify_breakpoint_created (b);
8163 gdb::observers::breakpoint_created.notify (b);
8164 }
8165
8166 breakpoint *
8167 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
8168 {
8169 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
8170 set_breakpoint_number (internal, b);
8171 if (is_tracepoint (b))
8172 set_tracepoint_count (breakpoint_count);
8173 if (!internal)
8174 mention (b);
8175
8176 notify_breakpoint_created (b);
8177
8178 if (update_gll)
8179 update_global_location_list (UGLL_MAY_INSERT);
8180
8181 return b;
8182 }
8183
8184 static int
8185 hw_breakpoint_used_count (void)
8186 {
8187 int i = 0;
8188
8189 for (breakpoint &b : all_breakpoints ())
8190 if (b.type == bp_hardware_breakpoint && breakpoint_enabled (&b))
8191 for (bp_location &bl : b.locations ())
8192 {
8193 /* Special types of hardware breakpoints may use more than
8194 one register. */
8195 i += b.resources_needed (&bl);
8196 }
8197
8198 return i;
8199 }
8200
8201 /* Returns the resources B would use if it were a hardware
8202 watchpoint. */
8203
8204 static int
8205 hw_watchpoint_use_count (struct breakpoint *b)
8206 {
8207 int i = 0;
8208
8209 if (!breakpoint_enabled (b))
8210 return 0;
8211
8212 for (bp_location &bl : b->locations ())
8213 {
8214 /* Special types of hardware watchpoints may use more than
8215 one register. */
8216 i += b->resources_needed (&bl);
8217 }
8218
8219 return i;
8220 }
8221
8222 /* Returns the sum the used resources of all hardware watchpoints of
8223 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8224 the sum of the used resources of all hardware watchpoints of other
8225 types _not_ TYPE. */
8226
8227 static int
8228 hw_watchpoint_used_count_others (struct breakpoint *except,
8229 enum bptype type, int *other_type_used)
8230 {
8231 int i = 0;
8232
8233 *other_type_used = 0;
8234 for (breakpoint &b : all_breakpoints ())
8235 {
8236 if (&b == except)
8237 continue;
8238 if (!breakpoint_enabled (&b))
8239 continue;
8240
8241 if (b.type == type)
8242 i += hw_watchpoint_use_count (&b);
8243 else if (is_hardware_watchpoint (&b))
8244 *other_type_used = 1;
8245 }
8246
8247 return i;
8248 }
8249
8250 void
8251 disable_watchpoints_before_interactive_call_start (void)
8252 {
8253 for (breakpoint &b : all_breakpoints ())
8254 if (is_watchpoint (&b) && breakpoint_enabled (&b))
8255 {
8256 b.enable_state = bp_call_disabled;
8257 update_global_location_list (UGLL_DONT_INSERT);
8258 }
8259 }
8260
8261 void
8262 enable_watchpoints_after_interactive_call_stop (void)
8263 {
8264 for (breakpoint &b : all_breakpoints ())
8265 if (is_watchpoint (&b) && b.enable_state == bp_call_disabled)
8266 {
8267 b.enable_state = bp_enabled;
8268 update_global_location_list (UGLL_MAY_INSERT);
8269 }
8270 }
8271
8272 void
8273 disable_breakpoints_before_startup (void)
8274 {
8275 current_program_space->executing_startup = 1;
8276 update_global_location_list (UGLL_DONT_INSERT);
8277 }
8278
8279 void
8280 enable_breakpoints_after_startup (void)
8281 {
8282 current_program_space->executing_startup = 0;
8283 breakpoint_re_set ();
8284 }
8285
8286 /* Allocate a new momentary breakpoint. */
8287
8288 template<typename... Arg>
8289 static momentary_breakpoint *
8290 new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
8291 Arg&&... args)
8292 {
8293 if (type == bp_longjmp || type == bp_exception)
8294 return new longjmp_breakpoint (gdbarch, type,
8295 std::forward<Arg> (args)...);
8296 else
8297 return new momentary_breakpoint (gdbarch, type,
8298 std::forward<Arg> (args)...);
8299 }
8300
8301 /* Set a momentary breakpoint of type TYPE at address specified by
8302 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8303 frame. */
8304
8305 breakpoint_up
8306 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8307 struct frame_id frame_id, enum bptype type)
8308 {
8309 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8310 tail-called one. */
8311 gdb_assert (!frame_id_artificial_p (frame_id));
8312
8313 std::unique_ptr<momentary_breakpoint> b
8314 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
8315 inferior_thread ()->global_num));
8316
8317 b->add_location (sal);
8318
8319 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
8320
8321 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8322
8323 return bp;
8324 }
8325
8326 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8327 The new breakpoint will have type TYPE, use OPS as its
8328 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8329
8330 static struct breakpoint *
8331 momentary_breakpoint_from_master (struct breakpoint *orig,
8332 enum bptype type,
8333 int loc_enabled,
8334 int thread)
8335 {
8336 std::unique_ptr<breakpoint> copy
8337 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
8338 orig->frame_id, thread));
8339 const bp_location &orig_loc = orig->first_loc ();
8340 bp_location *copy_loc = copy->allocate_location ();
8341 copy->add_location (*copy_loc);
8342 set_breakpoint_location_function (copy_loc);
8343
8344 copy_loc->gdbarch = orig_loc.gdbarch;
8345 copy_loc->requested_address = orig_loc.requested_address;
8346 copy_loc->address = orig_loc.address;
8347 copy_loc->section = orig_loc.section;
8348 copy_loc->pspace = orig_loc.pspace;
8349 copy_loc->probe = orig_loc.probe;
8350 copy_loc->line_number = orig_loc.line_number;
8351 copy_loc->symtab = orig_loc.symtab;
8352 copy_loc->enabled = loc_enabled;
8353
8354 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
8355 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8356 return b;
8357 }
8358
8359 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8360 ORIG is NULL. */
8361
8362 struct breakpoint *
8363 clone_momentary_breakpoint (struct breakpoint *orig)
8364 {
8365 /* If there's nothing to clone, then return nothing. */
8366 if (orig == NULL)
8367 return NULL;
8368
8369 return momentary_breakpoint_from_master (orig, orig->type, 0,
8370 orig->thread);
8371 }
8372
8373 breakpoint_up
8374 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8375 enum bptype type)
8376 {
8377 struct symtab_and_line sal;
8378
8379 sal = find_pc_line (pc, 0);
8380 sal.pc = pc;
8381 sal.section = find_pc_overlay (pc);
8382 sal.explicit_pc = 1;
8383
8384 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8385 }
8386 \f
8387
8388 /* Tell the user we have just set a breakpoint B. */
8389
8390 static void
8391 mention (const breakpoint *b)
8392 {
8393 b->print_mention ();
8394 current_uiout->text ("\n");
8395 }
8396 \f
8397
8398 static bool bp_loc_is_permanent (struct bp_location *loc);
8399
8400 /* Handle "set breakpoint auto-hw on".
8401
8402 If the explicitly specified breakpoint type is not hardware
8403 breakpoint, check the memory map to see whether the breakpoint
8404 address is in read-only memory.
8405
8406 - location type is not hardware breakpoint, memory is read-only.
8407 We change the type of the location to hardware breakpoint.
8408
8409 - location type is hardware breakpoint, memory is read-write. This
8410 means we've previously made the location hardware one, but then the
8411 memory map changed, so we undo.
8412 */
8413
8414 static void
8415 handle_automatic_hardware_breakpoints (bp_location *bl)
8416 {
8417 if (automatic_hardware_breakpoints
8418 && bl->owner->type != bp_hardware_breakpoint
8419 && (bl->loc_type == bp_loc_software_breakpoint
8420 || bl->loc_type == bp_loc_hardware_breakpoint))
8421 {
8422 /* When breakpoints are removed, remove_breakpoints will use
8423 location types we've just set here, the only possible problem
8424 is that memory map has changed during running program, but
8425 it's not going to work anyway with current gdb. */
8426 mem_region *mr = lookup_mem_region (bl->address);
8427
8428 if (mr != nullptr)
8429 {
8430 enum bp_loc_type new_type;
8431
8432 if (mr->attrib.mode != MEM_RW)
8433 new_type = bp_loc_hardware_breakpoint;
8434 else
8435 new_type = bp_loc_software_breakpoint;
8436
8437 if (new_type != bl->loc_type)
8438 {
8439 static bool said = false;
8440
8441 bl->loc_type = new_type;
8442 if (!said)
8443 {
8444 gdb_printf (_("Note: automatically using "
8445 "hardware breakpoints for "
8446 "read-only addresses.\n"));
8447 said = true;
8448 }
8449 }
8450 }
8451 }
8452 }
8453
8454 bp_location *
8455 code_breakpoint::add_location (const symtab_and_line &sal)
8456 {
8457 CORE_ADDR adjusted_address;
8458 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8459
8460 if (loc_gdbarch == NULL)
8461 loc_gdbarch = gdbarch;
8462
8463 /* Adjust the breakpoint's address prior to allocating a location.
8464 Once we call allocate_location(), that mostly uninitialized
8465 location will be placed on the location chain. Adjustment of the
8466 breakpoint may cause target_read_memory() to be called and we do
8467 not want its scan of the location chain to find a breakpoint and
8468 location that's only been partially initialized. */
8469 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8470 sal.pc, type,
8471 sal.pspace);
8472
8473 /* Sort the locations by their ADDRESS. */
8474 bp_location *new_loc = this->allocate_location ();
8475
8476 new_loc->requested_address = sal.pc;
8477 new_loc->address = adjusted_address;
8478 new_loc->pspace = sal.pspace;
8479 new_loc->probe.prob = sal.prob;
8480 new_loc->probe.objfile = sal.objfile;
8481 gdb_assert (new_loc->pspace != NULL);
8482 new_loc->section = sal.section;
8483 new_loc->gdbarch = loc_gdbarch;
8484 new_loc->line_number = sal.line;
8485 new_loc->symtab = sal.symtab;
8486 new_loc->symbol = sal.symbol;
8487 new_loc->msymbol = sal.msymbol;
8488 new_loc->objfile = sal.objfile;
8489
8490 breakpoint::add_location (*new_loc);
8491
8492 set_breakpoint_location_function (new_loc);
8493
8494 /* While by definition, permanent breakpoints are already present in the
8495 code, we don't mark the location as inserted. Normally one would expect
8496 that GDB could rely on that breakpoint instruction to stop the program,
8497 thus removing the need to insert its own breakpoint, except that executing
8498 the breakpoint instruction can kill the target instead of reporting a
8499 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8500 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8501 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8502 breakpoint be inserted normally results in QEMU knowing about the GDB
8503 breakpoint, and thus trap before the breakpoint instruction is executed.
8504 (If GDB later needs to continue execution past the permanent breakpoint,
8505 it manually increments the PC, thus avoiding executing the breakpoint
8506 instruction.) */
8507 if (bp_loc_is_permanent (new_loc))
8508 new_loc->permanent = 1;
8509
8510 return new_loc;
8511 }
8512 \f
8513
8514 /* Return true if LOC is pointing to a permanent breakpoint,
8515 return false otherwise. */
8516
8517 static bool
8518 bp_loc_is_permanent (struct bp_location *loc)
8519 {
8520 gdb_assert (loc != NULL);
8521
8522 /* If we have a non-breakpoint-backed catchpoint or a software
8523 watchpoint, just return 0. We should not attempt to read from
8524 the addresses the locations of these breakpoint types point to.
8525 gdbarch_program_breakpoint_here_p, below, will attempt to read
8526 memory. */
8527 if (!bl_address_is_meaningful (loc))
8528 return false;
8529
8530 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8531 switch_to_program_space_and_thread (loc->pspace);
8532 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8533 }
8534
8535 /* Build a command list for the dprintf corresponding to the current
8536 settings of the dprintf style options. */
8537
8538 static void
8539 update_dprintf_command_list (struct breakpoint *b)
8540 {
8541 const char *dprintf_args = b->extra_string.get ();
8542 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8543
8544 if (!dprintf_args)
8545 return;
8546
8547 dprintf_args = skip_spaces (dprintf_args);
8548
8549 /* Allow a comma, as it may have terminated a location, but don't
8550 insist on it. */
8551 if (*dprintf_args == ',')
8552 ++dprintf_args;
8553 dprintf_args = skip_spaces (dprintf_args);
8554
8555 if (*dprintf_args != '"')
8556 error (_("Bad format string, missing '\"'."));
8557
8558 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8559 printf_line = xstrprintf ("printf %s", dprintf_args);
8560 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8561 {
8562 if (dprintf_function.empty ())
8563 error (_("No function supplied for dprintf call"));
8564
8565 if (!dprintf_channel.empty ())
8566 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8567 dprintf_function.c_str (),
8568 dprintf_channel.c_str (),
8569 dprintf_args);
8570 else
8571 printf_line = xstrprintf ("call (void) %s (%s)",
8572 dprintf_function.c_str (),
8573 dprintf_args);
8574 }
8575 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8576 {
8577 if (target_can_run_breakpoint_commands ())
8578 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8579 else
8580 {
8581 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8582 printf_line = xstrprintf ("printf %s", dprintf_args);
8583 }
8584 }
8585 else
8586 internal_error (_("Invalid dprintf style."));
8587
8588 gdb_assert (printf_line != NULL);
8589
8590 /* Manufacture a printf sequence. */
8591 struct command_line *printf_cmd_line
8592 = new struct command_line (simple_control, printf_line.release ());
8593 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8594 command_lines_deleter ()));
8595 }
8596
8597 /* Update all dprintf commands, making their command lists reflect
8598 current style settings. */
8599
8600 static void
8601 update_dprintf_commands (const char *args, int from_tty,
8602 struct cmd_list_element *c)
8603 {
8604 for (breakpoint &b : all_breakpoints ())
8605 if (b.type == bp_dprintf)
8606 update_dprintf_command_list (&b);
8607 }
8608
8609 code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
8610 enum bptype type_,
8611 gdb::array_view<const symtab_and_line> sals,
8612 location_spec_up &&locspec_,
8613 gdb::unique_xmalloc_ptr<char> filter_,
8614 gdb::unique_xmalloc_ptr<char> cond_string_,
8615 gdb::unique_xmalloc_ptr<char> extra_string_,
8616 enum bpdisp disposition_,
8617 int thread_, int task_, int inferior_,
8618 int ignore_count_,
8619 int from_tty,
8620 int enabled_, unsigned flags,
8621 int display_canonical_)
8622 : breakpoint (gdbarch_, type_)
8623 {
8624 int i;
8625
8626 if (type == bp_hardware_breakpoint)
8627 {
8628 int target_resources_ok;
8629
8630 i = hw_breakpoint_used_count ();
8631 target_resources_ok =
8632 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8633 i + 1, 0);
8634 if (target_resources_ok == 0)
8635 error (_("No hardware breakpoint support in the target."));
8636 else if (target_resources_ok < 0)
8637 error (_("Hardware breakpoints used exceeds limit."));
8638 }
8639
8640 gdb_assert (!sals.empty ());
8641
8642 /* At most one of thread, task, or inferior can be set on any breakpoint. */
8643 gdb_assert (((thread == -1 ? 0 : 1)
8644 + (task == -1 ? 0 : 1)
8645 + (inferior == -1 ? 0 : 1)) <= 1);
8646
8647 thread = thread_;
8648 task = task_;
8649 inferior = inferior_;
8650
8651 cond_string = std::move (cond_string_);
8652 extra_string = std::move (extra_string_);
8653 ignore_count = ignore_count_;
8654 enable_state = enabled_ ? bp_enabled : bp_disabled;
8655 disposition = disposition_;
8656
8657 if (type == bp_static_tracepoint
8658 || type == bp_static_marker_tracepoint)
8659 {
8660 auto *t = gdb::checked_static_cast<struct tracepoint *> (this);
8661 struct static_tracepoint_marker marker;
8662
8663 if (strace_marker_p (this))
8664 {
8665 /* We already know the marker exists, otherwise, we wouldn't
8666 see a sal for it. */
8667 const char *p = &locspec_->to_string ()[3];
8668 const char *endp;
8669
8670 p = skip_spaces (p);
8671
8672 endp = skip_to_space (p);
8673
8674 t->static_trace_marker_id.assign (p, endp - p);
8675
8676 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8677 t->static_trace_marker_id.c_str ());
8678 }
8679 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8680 {
8681 t->static_trace_marker_id = std::move (marker.str_id);
8682
8683 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8684 t->static_trace_marker_id.c_str ());
8685 }
8686 else
8687 warning (_("Couldn't determine the static tracepoint marker to probe"));
8688 }
8689
8690 for (const auto &sal : sals)
8691 {
8692 if (from_tty)
8693 {
8694 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8695 if (loc_gdbarch == nullptr)
8696 loc_gdbarch = gdbarch;
8697
8698 describe_other_breakpoints (loc_gdbarch,
8699 sal.pspace, sal.pc, sal.section, thread);
8700 }
8701
8702 bp_location *new_loc = add_location (sal);
8703 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8704 new_loc->inserted = 1;
8705
8706 /* Do not set breakpoint locations conditions yet. As locations
8707 are inserted, they get sorted based on their addresses. Let
8708 the list stabilize to have reliable location numbers. */
8709
8710 /* Dynamic printf requires and uses additional arguments on the
8711 command line, otherwise it's an error. */
8712 if (type == bp_dprintf)
8713 {
8714 if (extra_string != nullptr)
8715 update_dprintf_command_list (this);
8716 else
8717 error (_("Format string required"));
8718 }
8719 else if (extra_string != nullptr)
8720 error (_("Garbage '%s' at end of command"), extra_string.get ());
8721 }
8722
8723 /* The order of the locations is now stable. Set the location
8724 condition using the location's number. */
8725 int loc_num = 1;
8726 for (bp_location &bl : locations ())
8727 {
8728 if (cond_string != nullptr)
8729 set_breakpoint_location_condition (cond_string.get (), &bl,
8730 number, loc_num);
8731
8732 ++loc_num;
8733 }
8734
8735 display_canonical = display_canonical_;
8736 if (locspec_ != nullptr)
8737 locspec = std::move (locspec_);
8738 else
8739 locspec = new_address_location_spec (this->first_loc ().address, NULL, 0);
8740 filter = std::move (filter_);
8741 }
8742
8743 static void
8744 create_breakpoint_sal (struct gdbarch *gdbarch,
8745 gdb::array_view<const symtab_and_line> sals,
8746 location_spec_up &&locspec,
8747 gdb::unique_xmalloc_ptr<char> filter,
8748 gdb::unique_xmalloc_ptr<char> cond_string,
8749 gdb::unique_xmalloc_ptr<char> extra_string,
8750 enum bptype type, enum bpdisp disposition,
8751 int thread, int task, int inferior, int ignore_count,
8752 int from_tty,
8753 int enabled, int internal, unsigned flags,
8754 int display_canonical)
8755 {
8756 std::unique_ptr<code_breakpoint> b
8757 = new_breakpoint_from_type (gdbarch,
8758 type,
8759 sals,
8760 std::move (locspec),
8761 std::move (filter),
8762 std::move (cond_string),
8763 std::move (extra_string),
8764 disposition,
8765 thread, task, inferior, ignore_count,
8766 from_tty,
8767 enabled, flags,
8768 display_canonical);
8769
8770 install_breakpoint (internal, std::move (b), 0);
8771 }
8772
8773 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8774 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8775 value. COND_STRING, if not NULL, specified the condition to be
8776 used for all breakpoints. Essentially the only case where
8777 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8778 function. In that case, it's still not possible to specify
8779 separate conditions for different overloaded functions, so
8780 we take just a single condition string.
8781
8782 NOTE: If the function succeeds, the caller is expected to cleanup
8783 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8784 array contents). If the function fails (error() is called), the
8785 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8786 COND and SALS arrays and each of those arrays contents. */
8787
8788 static void
8789 create_breakpoints_sal (struct gdbarch *gdbarch,
8790 struct linespec_result *canonical,
8791 gdb::unique_xmalloc_ptr<char> cond_string,
8792 gdb::unique_xmalloc_ptr<char> extra_string,
8793 enum bptype type, enum bpdisp disposition,
8794 int thread, int task, int inferior,
8795 int ignore_count,
8796 int from_tty,
8797 int enabled, int internal, unsigned flags)
8798 {
8799 if (canonical->pre_expanded)
8800 gdb_assert (canonical->lsals.size () == 1);
8801
8802 for (const auto &lsal : canonical->lsals)
8803 {
8804 /* Note that 'location' can be NULL in the case of a plain
8805 'break', without arguments. */
8806 location_spec_up locspec
8807 = (canonical->locspec != nullptr
8808 ? canonical->locspec->clone ()
8809 : nullptr);
8810 gdb::unique_xmalloc_ptr<char> filter_string
8811 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8812
8813 create_breakpoint_sal (gdbarch, lsal.sals,
8814 std::move (locspec),
8815 std::move (filter_string),
8816 std::move (cond_string),
8817 std::move (extra_string),
8818 type, disposition,
8819 thread, task, inferior, ignore_count,
8820 from_tty, enabled, internal, flags,
8821 canonical->special_display);
8822 }
8823 }
8824
8825 /* Parse LOCSPEC which is assumed to be a SAL specification possibly
8826 followed by conditionals. On return, SALS contains an array of SAL
8827 addresses found. LOCSPEC points to the end of the SAL (for
8828 linespec locspecs).
8829
8830 The array and the line spec strings are allocated on the heap, it is
8831 the caller's responsibility to free them. */
8832
8833 static void
8834 parse_breakpoint_sals (location_spec *locspec,
8835 struct linespec_result *canonical)
8836 {
8837 struct symtab_and_line cursal;
8838
8839 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8840 {
8841 const char *spec = as_linespec_location_spec (locspec)->spec_string;
8842
8843 if (spec == NULL)
8844 {
8845 /* The last displayed codepoint, if it's valid, is our default
8846 breakpoint address. */
8847 if (last_displayed_sal_is_valid ())
8848 {
8849 /* Set sal's pspace, pc, symtab, and line to the values
8850 corresponding to the last call to print_frame_info.
8851 Be sure to reinitialize LINE with NOTCURRENT == 0
8852 as the breakpoint line number is inappropriate otherwise.
8853 find_pc_line would adjust PC, re-set it back. */
8854 symtab_and_line sal = get_last_displayed_sal ();
8855 CORE_ADDR pc = sal.pc;
8856
8857 sal = find_pc_line (pc, 0);
8858
8859 /* "break" without arguments is equivalent to "break *PC"
8860 where PC is the last displayed codepoint's address. So
8861 make sure to set sal.explicit_pc to prevent GDB from
8862 trying to expand the list of sals to include all other
8863 instances with the same symtab and line. */
8864 sal.pc = pc;
8865 sal.explicit_pc = 1;
8866
8867 struct linespec_sals lsal;
8868 lsal.sals = {sal};
8869 lsal.canonical = NULL;
8870
8871 canonical->lsals.push_back (std::move (lsal));
8872 return;
8873 }
8874 else
8875 error (_("No default breakpoint address now."));
8876 }
8877 }
8878
8879 /* Force almost all breakpoints to be in terms of the
8880 current_source_symtab (which is decode_line_1's default).
8881 This should produce the results we want almost all of the
8882 time while leaving default_breakpoint_* alone.
8883
8884 ObjC: However, don't match an Objective-C method name which
8885 may have a '+' or '-' succeeded by a '['. */
8886 cursal = get_current_source_symtab_and_line ();
8887 if (last_displayed_sal_is_valid ())
8888 {
8889 const char *spec = NULL;
8890
8891 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8892 spec = as_linespec_location_spec (locspec)->spec_string;
8893
8894 if (!cursal.symtab
8895 || (spec != NULL
8896 && strchr ("+-", spec[0]) != NULL
8897 && spec[1] != '['))
8898 {
8899 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
8900 get_last_displayed_symtab (),
8901 get_last_displayed_line (),
8902 canonical, NULL, NULL);
8903 return;
8904 }
8905 }
8906
8907 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
8908 cursal.symtab, cursal.line, canonical, NULL, NULL);
8909 }
8910
8911
8912 /* Convert each SAL into a real PC. Verify that the PC can be
8913 inserted as a breakpoint. If it can't throw an error. */
8914
8915 static void
8916 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
8917 {
8918 for (auto &sal : sals)
8919 resolve_sal_pc (&sal);
8920 }
8921
8922 /* Fast tracepoints may have restrictions on valid locations. For
8923 instance, a fast tracepoint using a jump instead of a trap will
8924 likely have to overwrite more bytes than a trap would, and so can
8925 only be placed where the instruction is longer than the jump, or a
8926 multi-instruction sequence does not have a jump into the middle of
8927 it, etc. */
8928
8929 static void
8930 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8931 gdb::array_view<const symtab_and_line> sals)
8932 {
8933 for (const auto &sal : sals)
8934 {
8935 struct gdbarch *sarch;
8936
8937 sarch = get_sal_arch (sal);
8938 /* We fall back to GDBARCH if there is no architecture
8939 associated with SAL. */
8940 if (sarch == NULL)
8941 sarch = gdbarch;
8942 std::string msg;
8943 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
8944 error (_("May not have a fast tracepoint at %s%s"),
8945 paddress (sarch, sal.pc), msg.c_str ());
8946 }
8947 }
8948
8949 /* Given TOK, a string specification of condition and thread, as accepted
8950 by the 'break' command, extract the condition string into *COND_STRING.
8951 If no condition string is found then *COND_STRING is set to nullptr.
8952
8953 If the breakpoint specification has an associated thread, task, or
8954 inferior, these are extracted into *THREAD, *TASK, and *INFERIOR
8955 respectively, otherwise these arguments are set to -1 (for THREAD and
8956 INFERIOR) or 0 (for TASK).
8957
8958 PC identifies the context at which the condition should be parsed. */
8959
8960 static void
8961 find_condition_and_thread (const char *tok, CORE_ADDR pc,
8962 gdb::unique_xmalloc_ptr<char> *cond_string,
8963 int *thread, int *inferior, int *task,
8964 gdb::unique_xmalloc_ptr<char> *rest)
8965 {
8966 cond_string->reset ();
8967 *thread = -1;
8968 *inferior = -1;
8969 *task = -1;
8970 rest->reset ();
8971 bool force = false;
8972
8973 while (tok && *tok)
8974 {
8975 const char *end_tok;
8976 int toklen;
8977 const char *cond_start = NULL;
8978 const char *cond_end = NULL;
8979
8980 tok = skip_spaces (tok);
8981
8982 if ((*tok == '"' || *tok == ',') && rest)
8983 {
8984 rest->reset (savestring (tok, strlen (tok)));
8985 break;
8986 }
8987
8988 end_tok = skip_to_space (tok);
8989
8990 toklen = end_tok - tok;
8991
8992 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8993 {
8994 tok = cond_start = end_tok + 1;
8995 try
8996 {
8997 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8998 }
8999 catch (const gdb_exception_error &)
9000 {
9001 if (!force)
9002 throw;
9003 else
9004 tok = tok + strlen (tok);
9005 }
9006 cond_end = tok;
9007 cond_string->reset (savestring (cond_start, cond_end - cond_start));
9008 }
9009 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9010 {
9011 tok = tok + toklen;
9012 force = true;
9013 }
9014 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9015 {
9016 const char *tmptok;
9017 struct thread_info *thr;
9018
9019 if (*thread != -1)
9020 error(_("You can specify only one thread."));
9021
9022 if (*task != -1)
9023 error (_("You can specify only one of thread or task."));
9024
9025 if (*inferior != -1)
9026 error (_("You can specify only one of inferior or thread."));
9027
9028 tok = end_tok + 1;
9029 thr = parse_thread_id (tok, &tmptok);
9030 if (tok == tmptok)
9031 error (_("Junk after thread keyword."));
9032 *thread = thr->global_num;
9033 tok = tmptok;
9034 }
9035 else if (toklen >= 1 && strncmp (tok, "inferior", toklen) == 0)
9036 {
9037 if (*inferior != -1)
9038 error(_("You can specify only one inferior."));
9039
9040 if (*task != -1)
9041 error (_("You can specify only one of inferior or task."));
9042
9043 if (*thread != -1)
9044 error (_("You can specify only one of inferior or thread."));
9045
9046 char *tmptok;
9047 tok = end_tok + 1;
9048 *inferior = strtol (tok, &tmptok, 0);
9049 if (tok == tmptok)
9050 error (_("Junk after inferior keyword."));
9051 if (!valid_global_inferior_id (*inferior))
9052 error (_("Unknown inferior number %d."), *inferior);
9053 tok = tmptok;
9054 }
9055 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9056 {
9057 char *tmptok;
9058
9059 if (*task != -1)
9060 error(_("You can specify only one task."));
9061
9062 if (*thread != -1)
9063 error (_("You can specify only one of thread or task."));
9064
9065 if (*inferior != -1)
9066 error (_("You can specify only one of inferior or task."));
9067
9068 tok = end_tok + 1;
9069 *task = strtol (tok, &tmptok, 0);
9070 if (tok == tmptok)
9071 error (_("Junk after task keyword."));
9072 if (!valid_task_id (*task))
9073 error (_("Unknown task %d."), *task);
9074 tok = tmptok;
9075 }
9076 else if (rest)
9077 {
9078 rest->reset (savestring (tok, strlen (tok)));
9079 break;
9080 }
9081 else
9082 error (_("Junk at end of arguments."));
9083 }
9084 }
9085
9086 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
9087 succeeds. The parsed values are written to COND_STRING, THREAD,
9088 TASK, and REST. See the comment of 'find_condition_and_thread'
9089 for the description of these parameters and INPUT. */
9090
9091 static void
9092 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9093 const char *input,
9094 gdb::unique_xmalloc_ptr<char> *cond_string,
9095 int *thread, int *inferior, int *task,
9096 gdb::unique_xmalloc_ptr<char> *rest)
9097 {
9098 int num_failures = 0;
9099 for (auto &sal : sals)
9100 {
9101 gdb::unique_xmalloc_ptr<char> cond;
9102 int thread_id = -1;
9103 int inferior_id = -1;
9104 int task_id = -1;
9105 gdb::unique_xmalloc_ptr<char> remaining;
9106
9107 /* Here we want to parse 'arg' to separate condition from thread
9108 number. But because parsing happens in a context and the
9109 contexts of sals might be different, try each until there is
9110 success. Finding one successful parse is sufficient for our
9111 goal. When setting the breakpoint we'll re-parse the
9112 condition in the context of each sal. */
9113 try
9114 {
9115 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9116 &inferior_id, &task_id, &remaining);
9117 *cond_string = std::move (cond);
9118 /* A value of -1 indicates that these fields are unset. At most
9119 one of these fields should be set (to a value other than -1)
9120 at this point. */
9121 gdb_assert (((thread_id == -1 ? 1 : 0)
9122 + (task_id == -1 ? 1 : 0)
9123 + (inferior_id == -1 ? 1 : 0)) >= 2);
9124 *thread = thread_id;
9125 *inferior = inferior_id;
9126 *task = task_id;
9127 *rest = std::move (remaining);
9128 break;
9129 }
9130 catch (const gdb_exception_error &e)
9131 {
9132 num_failures++;
9133 /* If no sal remains, do not continue. */
9134 if (num_failures == sals.size ())
9135 throw;
9136 }
9137 }
9138 }
9139
9140 /* Decode a static tracepoint marker spec. */
9141
9142 static std::vector<symtab_and_line>
9143 decode_static_tracepoint_spec (const char **arg_p)
9144 {
9145 const char *p = &(*arg_p)[3];
9146 const char *endp;
9147
9148 p = skip_spaces (p);
9149
9150 endp = skip_to_space (p);
9151
9152 std::string marker_str (p, endp - p);
9153
9154 std::vector<static_tracepoint_marker> markers
9155 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9156 if (markers.empty ())
9157 error (_("No known static tracepoint marker named %s"),
9158 marker_str.c_str ());
9159
9160 std::vector<symtab_and_line> sals;
9161 sals.reserve (markers.size ());
9162
9163 for (const static_tracepoint_marker &marker : markers)
9164 {
9165 symtab_and_line sal = find_pc_line (marker.address, 0);
9166 sal.pc = marker.address;
9167 sals.push_back (sal);
9168 }
9169
9170 *arg_p = endp;
9171 return sals;
9172 }
9173
9174 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9175 according to IS_TRACEPOINT. */
9176
9177 static const struct breakpoint_ops *
9178 breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
9179 bool is_tracepoint)
9180 {
9181 if (is_tracepoint)
9182 {
9183 if (locspec_type == PROBE_LOCATION_SPEC)
9184 return &tracepoint_probe_breakpoint_ops;
9185 else
9186 return &code_breakpoint_ops;
9187 }
9188 else
9189 {
9190 if (locspec_type == PROBE_LOCATION_SPEC)
9191 return &bkpt_probe_breakpoint_ops;
9192 else
9193 return &code_breakpoint_ops;
9194 }
9195 }
9196
9197 /* See breakpoint.h. */
9198
9199 const struct breakpoint_ops *
9200 breakpoint_ops_for_location_spec (const location_spec *locspec,
9201 bool is_tracepoint)
9202 {
9203 if (locspec != nullptr)
9204 return (breakpoint_ops_for_location_spec_type
9205 (locspec->type (), is_tracepoint));
9206 return &code_breakpoint_ops;
9207 }
9208
9209 /* See breakpoint.h. */
9210
9211 int
9212 create_breakpoint (struct gdbarch *gdbarch,
9213 location_spec *locspec,
9214 const char *cond_string,
9215 int thread, int inferior,
9216 const char *extra_string,
9217 bool force_condition, int parse_extra,
9218 int tempflag, enum bptype type_wanted,
9219 int ignore_count,
9220 enum auto_boolean pending_break_support,
9221 const struct breakpoint_ops *ops,
9222 int from_tty, int enabled, int internal,
9223 unsigned flags)
9224 {
9225 struct linespec_result canonical;
9226 bool pending = false;
9227 int task = -1;
9228 int prev_bkpt_count = breakpoint_count;
9229
9230 gdb_assert (thread == -1 || thread > 0);
9231 gdb_assert (inferior == -1 || inferior > 0);
9232 gdb_assert (thread == -1 || inferior == -1);
9233
9234 gdb_assert (ops != NULL);
9235
9236 /* If extra_string isn't useful, set it to NULL. */
9237 if (extra_string != NULL && *extra_string == '\0')
9238 extra_string = NULL;
9239
9240 try
9241 {
9242 ops->create_sals_from_location_spec (locspec, &canonical);
9243 }
9244 catch (const gdb_exception_error &e)
9245 {
9246 /* If caller is interested in rc value from parse, set
9247 value. */
9248 if (e.error == NOT_FOUND_ERROR)
9249 {
9250 /* If pending breakpoint support is turned off, throw
9251 error. */
9252
9253 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9254 throw;
9255
9256 exception_print (gdb_stderr, e);
9257
9258 /* If pending breakpoint support is auto query and the user
9259 selects no, then simply return the error code. */
9260 if (pending_break_support == AUTO_BOOLEAN_AUTO
9261 && !nquery (_("Make %s pending on future shared library load? "),
9262 bptype_string (type_wanted)))
9263 return 0;
9264
9265 /* At this point, either the user was queried about setting
9266 a pending breakpoint and selected yes, or pending
9267 breakpoint behavior is on and thus a pending breakpoint
9268 is defaulted on behalf of the user. */
9269 pending = true;
9270 }
9271 else
9272 throw;
9273 }
9274
9275 if (!pending && canonical.lsals.empty ())
9276 return 0;
9277
9278 /* Resolve all line numbers to PC's and verify that the addresses
9279 are ok for the target. */
9280 if (!pending)
9281 {
9282 for (auto &lsal : canonical.lsals)
9283 breakpoint_sals_to_pc (lsal.sals);
9284 }
9285
9286 /* Fast tracepoints may have additional restrictions on location. */
9287 if (!pending && type_wanted == bp_fast_tracepoint)
9288 {
9289 for (const auto &lsal : canonical.lsals)
9290 check_fast_tracepoint_sals (gdbarch, lsal.sals);
9291 }
9292
9293 /* Verify that condition can be parsed, before setting any
9294 breakpoints. Allocate a separate condition expression for each
9295 breakpoint. */
9296 if (!pending)
9297 {
9298 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9299 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9300
9301 if (parse_extra)
9302 {
9303 gdb::unique_xmalloc_ptr<char> rest;
9304 gdb::unique_xmalloc_ptr<char> cond;
9305
9306 const linespec_sals &lsal = canonical.lsals[0];
9307
9308 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9309 &cond, &thread, &inferior,
9310 &task, &rest);
9311 cond_string_copy = std::move (cond);
9312 extra_string_copy = std::move (rest);
9313 }
9314 else
9315 {
9316 if (type_wanted != bp_dprintf
9317 && extra_string != NULL && *extra_string != '\0')
9318 error (_("Garbage '%s' at end of location"), extra_string);
9319
9320 /* Check the validity of the condition. We should error out
9321 if the condition is invalid at all of the locations and
9322 if it is not forced. In the PARSE_EXTRA case above, this
9323 check is done when parsing the EXTRA_STRING. */
9324 if (cond_string != nullptr && !force_condition)
9325 {
9326 int num_failures = 0;
9327 const linespec_sals &lsal = canonical.lsals[0];
9328 for (const auto &sal : lsal.sals)
9329 {
9330 const char *cond = cond_string;
9331 try
9332 {
9333 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9334 /* One success is sufficient to keep going. */
9335 break;
9336 }
9337 catch (const gdb_exception_error &)
9338 {
9339 num_failures++;
9340 /* If this is the last sal, error out. */
9341 if (num_failures == lsal.sals.size ())
9342 throw;
9343 }
9344 }
9345 }
9346
9347 /* Create a private copy of condition string. */
9348 if (cond_string)
9349 cond_string_copy.reset (xstrdup (cond_string));
9350 /* Create a private copy of any extra string. */
9351 if (extra_string)
9352 extra_string_copy.reset (xstrdup (extra_string));
9353 }
9354
9355 ops->create_breakpoints_sal (gdbarch, &canonical,
9356 std::move (cond_string_copy),
9357 std::move (extra_string_copy),
9358 type_wanted,
9359 tempflag ? disp_del : disp_donttouch,
9360 thread, task, inferior, ignore_count,
9361 from_tty, enabled, internal, flags);
9362 }
9363 else
9364 {
9365 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
9366 type_wanted);
9367 b->locspec = locspec->clone ();
9368
9369 if (parse_extra)
9370 b->cond_string = NULL;
9371 else
9372 {
9373 /* Create a private copy of condition string. */
9374 b->cond_string.reset (cond_string != NULL
9375 ? xstrdup (cond_string)
9376 : NULL);
9377 b->thread = thread;
9378 }
9379
9380 /* Create a private copy of any extra string. */
9381 b->extra_string.reset (extra_string != NULL
9382 ? xstrdup (extra_string)
9383 : NULL);
9384 b->ignore_count = ignore_count;
9385 b->disposition = tempflag ? disp_del : disp_donttouch;
9386 b->condition_not_parsed = 1;
9387 b->enable_state = enabled ? bp_enabled : bp_disabled;
9388 if ((type_wanted != bp_breakpoint
9389 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9390 b->pspace = current_program_space;
9391
9392 install_breakpoint (internal, std::move (b), 0);
9393 }
9394
9395 if (canonical.lsals.size () > 1)
9396 {
9397 warning (_("Multiple breakpoints were set.\nUse the "
9398 "\"delete\" command to delete unwanted breakpoints."));
9399 prev_breakpoint_count = prev_bkpt_count;
9400 }
9401
9402 update_global_location_list (UGLL_MAY_INSERT);
9403
9404 return 1;
9405 }
9406
9407 /* Set a breakpoint.
9408 ARG is a string describing breakpoint address,
9409 condition, and thread.
9410 FLAG specifies if a breakpoint is hardware on,
9411 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9412 and BP_TEMPFLAG. */
9413
9414 static void
9415 break_command_1 (const char *arg, int flag, int from_tty)
9416 {
9417 int tempflag = flag & BP_TEMPFLAG;
9418 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9419 ? bp_hardware_breakpoint
9420 : bp_breakpoint);
9421
9422 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9423 const struct breakpoint_ops *ops
9424 = breakpoint_ops_for_location_spec (locspec.get (),
9425 false /* is_tracepoint */);
9426
9427 create_breakpoint (get_current_arch (),
9428 locspec.get (),
9429 NULL,
9430 -1 /* thread */, -1 /* inferior */,
9431 arg, false, 1 /* parse arg */,
9432 tempflag, type_wanted,
9433 0 /* Ignore count */,
9434 pending_break_support,
9435 ops,
9436 from_tty,
9437 1 /* enabled */,
9438 0 /* internal */,
9439 0);
9440 }
9441
9442 /* Helper function for break_command_1 and disassemble_command. */
9443
9444 void
9445 resolve_sal_pc (struct symtab_and_line *sal)
9446 {
9447 CORE_ADDR pc;
9448
9449 if (sal->pc == 0 && sal->symtab != NULL)
9450 {
9451 if (!find_line_pc (sal->symtab, sal->line, &pc))
9452 error (_("No line %d in file \"%s\"."),
9453 sal->line, symtab_to_filename_for_display (sal->symtab));
9454 sal->pc = pc;
9455
9456 /* If this SAL corresponds to a breakpoint inserted using a line
9457 number, then skip the function prologue if necessary. */
9458 if (sal->explicit_line)
9459 skip_prologue_sal (sal);
9460 }
9461
9462 if (sal->section == 0 && sal->symtab != NULL)
9463 {
9464 const struct blockvector *bv;
9465 const struct block *b;
9466 struct symbol *sym;
9467
9468 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9469 sal->symtab->compunit ());
9470 if (bv != NULL)
9471 {
9472 sym = b->linkage_function ();
9473 if (sym != NULL)
9474 sal->section
9475 = sym->obj_section (sal->symtab->compunit ()->objfile ());
9476 else
9477 {
9478 /* It really is worthwhile to have the section, so we'll
9479 just have to look harder. This case can be executed
9480 if we have line numbers but no functions (as can
9481 happen in assembly source). */
9482
9483 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9484 switch_to_program_space_and_thread (sal->pspace);
9485
9486 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9487 if (msym.minsym)
9488 sal->section = msym.obj_section ();
9489 }
9490 }
9491 }
9492 }
9493
9494 void
9495 break_command (const char *arg, int from_tty)
9496 {
9497 break_command_1 (arg, 0, from_tty);
9498 }
9499
9500 void
9501 tbreak_command (const char *arg, int from_tty)
9502 {
9503 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9504 }
9505
9506 static void
9507 hbreak_command (const char *arg, int from_tty)
9508 {
9509 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9510 }
9511
9512 static void
9513 thbreak_command (const char *arg, int from_tty)
9514 {
9515 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9516 }
9517
9518 /* The dynamic printf command is mostly like a regular breakpoint, but
9519 with a prewired command list consisting of a single output command,
9520 built from extra arguments supplied on the dprintf command
9521 line. */
9522
9523 static void
9524 dprintf_command (const char *arg, int from_tty)
9525 {
9526 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9527
9528 /* If non-NULL, ARG should have been advanced past the location;
9529 the next character must be ','. */
9530 if (arg != NULL)
9531 {
9532 if (arg[0] != ',' || arg[1] == '\0')
9533 error (_("Format string required"));
9534 else
9535 {
9536 /* Skip the comma. */
9537 ++arg;
9538 }
9539 }
9540
9541 create_breakpoint (get_current_arch (),
9542 locspec.get (),
9543 NULL, -1, -1,
9544 arg, false, 1 /* parse arg */,
9545 0, bp_dprintf,
9546 0 /* Ignore count */,
9547 pending_break_support,
9548 &code_breakpoint_ops,
9549 from_tty,
9550 1 /* enabled */,
9551 0 /* internal */,
9552 0);
9553 }
9554
9555 static void
9556 agent_printf_command (const char *arg, int from_tty)
9557 {
9558 error (_("May only run agent-printf on the target"));
9559 }
9560
9561 /* Implement the "breakpoint_hit" method for ranged breakpoints. */
9562
9563 int
9564 ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9565 const address_space *aspace,
9566 CORE_ADDR bp_addr,
9567 const target_waitstatus &ws)
9568 {
9569 if (ws.kind () != TARGET_WAITKIND_STOPPED
9570 || ws.sig () != GDB_SIGNAL_TRAP)
9571 return 0;
9572
9573 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9574 bl->length, aspace, bp_addr);
9575 }
9576
9577 /* Implement the "resources_needed" method for ranged breakpoints. */
9578
9579 int
9580 ranged_breakpoint::resources_needed (const struct bp_location *bl)
9581 {
9582 return target_ranged_break_num_registers ();
9583 }
9584
9585 /* Implement the "print_it" method for ranged breakpoints. */
9586
9587 enum print_stop_action
9588 ranged_breakpoint::print_it (const bpstat *bs) const
9589 {
9590 struct ui_out *uiout = current_uiout;
9591
9592 gdb_assert (type == bp_hardware_breakpoint);
9593
9594 /* Ranged breakpoints have only one location. */
9595 gdb_assert (this->has_single_location ());
9596
9597 annotate_breakpoint (number);
9598
9599 maybe_print_thread_hit_breakpoint (uiout);
9600
9601 if (disposition == disp_del)
9602 uiout->text ("Temporary ranged breakpoint ");
9603 else
9604 uiout->text ("Ranged breakpoint ");
9605 if (uiout->is_mi_like_p ())
9606 {
9607 uiout->field_string ("reason",
9608 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9609 uiout->field_string ("disp", bpdisp_text (disposition));
9610 }
9611 print_num_locno (bs, uiout);
9612 uiout->text (", ");
9613
9614 return PRINT_SRC_AND_LOC;
9615 }
9616
9617 /* Implement the "print_one" method for ranged breakpoints. */
9618
9619 bool
9620 ranged_breakpoint::print_one (const bp_location **last_loc) const
9621 {
9622 struct value_print_options opts;
9623 struct ui_out *uiout = current_uiout;
9624
9625 /* Ranged breakpoints have only one location. */
9626 gdb_assert (this->has_single_location ());
9627
9628 get_user_print_options (&opts);
9629
9630 if (opts.addressprint)
9631 /* We don't print the address range here, it will be printed later
9632 by ranged_breakpoint::print_one_detail. */
9633 uiout->field_skip ("addr");
9634 annotate_field (5);
9635 print_breakpoint_location (this, &this->first_loc ());
9636 *last_loc = &this->first_loc ();
9637
9638 return true;
9639 }
9640
9641 /* Implement the "print_one_detail" method for ranged breakpoints. */
9642
9643 void
9644 ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
9645 {
9646 CORE_ADDR address_start, address_end;
9647 const bp_location &bl = this->first_loc ();
9648 string_file stb;
9649
9650 address_start = bl.address;
9651 address_end = address_start + bl.length - 1;
9652
9653 uiout->text ("\taddress range: ");
9654 stb.printf ("[%s, %s]",
9655 print_core_address (bl.gdbarch, address_start),
9656 print_core_address (bl.gdbarch, address_end));
9657 uiout->field_stream ("addr", stb);
9658 uiout->text ("\n");
9659 }
9660
9661 /* Implement the "print_mention" method for ranged breakpoints. */
9662
9663 void
9664 ranged_breakpoint::print_mention () const
9665 {
9666 const bp_location &bl = this->first_loc ();
9667 struct ui_out *uiout = current_uiout;
9668
9669 gdb_assert (type == bp_hardware_breakpoint);
9670
9671 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9672 number, paddress (bl.gdbarch, bl.address),
9673 paddress (bl.gdbarch, bl.address + bl.length - 1));
9674 }
9675
9676 /* Implement the "print_recreate" method for ranged breakpoints. */
9677
9678 void
9679 ranged_breakpoint::print_recreate (struct ui_file *fp) const
9680 {
9681 gdb_printf (fp, "break-range %s, %s",
9682 locspec->to_string (),
9683 locspec_range_end->to_string ());
9684 print_recreate_thread (fp);
9685 }
9686
9687 /* Find the address where the end of the breakpoint range should be
9688 placed, given the SAL of the end of the range. This is so that if
9689 the user provides a line number, the end of the range is set to the
9690 last instruction of the given line. */
9691
9692 static CORE_ADDR
9693 find_breakpoint_range_end (struct symtab_and_line sal)
9694 {
9695 CORE_ADDR end;
9696
9697 /* If the user provided a PC value, use it. Otherwise,
9698 find the address of the end of the given location. */
9699 if (sal.explicit_pc)
9700 end = sal.pc;
9701 else
9702 {
9703 int ret;
9704 CORE_ADDR start;
9705
9706 ret = find_line_pc_range (sal, &start, &end);
9707 if (!ret)
9708 error (_("Could not find location of the end of the range."));
9709
9710 /* find_line_pc_range returns the start of the next line. */
9711 end--;
9712 }
9713
9714 return end;
9715 }
9716
9717 /* Implement the "break-range" CLI command. */
9718
9719 static void
9720 break_range_command (const char *arg, int from_tty)
9721 {
9722 const char *arg_start;
9723 struct linespec_result canonical_start, canonical_end;
9724 int bp_count, can_use_bp, length;
9725 CORE_ADDR end;
9726
9727 /* We don't support software ranged breakpoints. */
9728 if (target_ranged_break_num_registers () < 0)
9729 error (_("This target does not support hardware ranged breakpoints."));
9730
9731 bp_count = hw_breakpoint_used_count ();
9732 bp_count += target_ranged_break_num_registers ();
9733 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9734 bp_count, 0);
9735 if (can_use_bp < 0)
9736 error (_("Hardware breakpoints used exceeds limit."));
9737
9738 arg = skip_spaces (arg);
9739 if (arg == NULL || arg[0] == '\0')
9740 error(_("No address range specified."));
9741
9742 arg_start = arg;
9743 location_spec_up start_locspec
9744 = string_to_location_spec (&arg, current_language);
9745 parse_breakpoint_sals (start_locspec.get (), &canonical_start);
9746
9747 if (arg[0] != ',')
9748 error (_("Too few arguments."));
9749 else if (canonical_start.lsals.empty ())
9750 error (_("Could not find location of the beginning of the range."));
9751
9752 const linespec_sals &lsal_start = canonical_start.lsals[0];
9753
9754 if (canonical_start.lsals.size () > 1
9755 || lsal_start.sals.size () != 1)
9756 error (_("Cannot create a ranged breakpoint with multiple locations."));
9757
9758 const symtab_and_line &sal_start = lsal_start.sals[0];
9759 std::string addr_string_start (arg_start, arg - arg_start);
9760
9761 arg++; /* Skip the comma. */
9762 arg = skip_spaces (arg);
9763
9764 /* Parse the end location specification. */
9765
9766 arg_start = arg;
9767
9768 /* We call decode_line_full directly here instead of using
9769 parse_breakpoint_sals because we need to specify the start
9770 location spec's symtab and line as the default symtab and line
9771 for the end of the range. This makes it possible to have ranges
9772 like "foo.c:27, +14", where +14 means 14 lines from the start
9773 location spec. */
9774 location_spec_up end_locspec
9775 = string_to_location_spec (&arg, current_language);
9776 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9777 sal_start.symtab, sal_start.line,
9778 &canonical_end, NULL, NULL);
9779
9780 if (canonical_end.lsals.empty ())
9781 error (_("Could not find location of the end of the range."));
9782
9783 const linespec_sals &lsal_end = canonical_end.lsals[0];
9784 if (canonical_end.lsals.size () > 1
9785 || lsal_end.sals.size () != 1)
9786 error (_("Cannot create a ranged breakpoint with multiple locations."));
9787
9788 const symtab_and_line &sal_end = lsal_end.sals[0];
9789
9790 end = find_breakpoint_range_end (sal_end);
9791 if (sal_start.pc > end)
9792 error (_("Invalid address range, end precedes start."));
9793
9794 length = end - sal_start.pc + 1;
9795 if (length < 0)
9796 /* Length overflowed. */
9797 error (_("Address range too large."));
9798 else if (length == 1)
9799 {
9800 /* This range is simple enough to be handled by
9801 the `hbreak' command. */
9802 hbreak_command (&addr_string_start[0], 1);
9803
9804 return;
9805 }
9806
9807 /* Now set up the breakpoint and install it. */
9808
9809 std::unique_ptr<breakpoint> br
9810 (new ranged_breakpoint (get_current_arch (),
9811 sal_start, length,
9812 std::move (start_locspec),
9813 std::move (end_locspec)));
9814
9815 install_breakpoint (false, std::move (br), true);
9816 }
9817
9818 /* Return non-zero if EXP is verified as constant. Returned zero
9819 means EXP is variable. Also the constant detection may fail for
9820 some constant expressions and in such case still falsely return
9821 zero. */
9822
9823 static bool
9824 watchpoint_exp_is_const (const struct expression *exp)
9825 {
9826 return exp->op->constant_p ();
9827 }
9828
9829 /* Implement the "re_set" method for watchpoints. */
9830
9831 void
9832 watchpoint::re_set ()
9833 {
9834 /* Watchpoint can be either on expression using entirely global
9835 variables, or it can be on local variables.
9836
9837 Watchpoints of the first kind are never auto-deleted, and even
9838 persist across program restarts. Since they can use variables
9839 from shared libraries, we need to reparse expression as libraries
9840 are loaded and unloaded.
9841
9842 Watchpoints on local variables can also change meaning as result
9843 of solib event. For example, if a watchpoint uses both a local
9844 and a global variables in expression, it's a local watchpoint,
9845 but unloading of a shared library will make the expression
9846 invalid. This is not a very common use case, but we still
9847 re-evaluate expression, to avoid surprises to the user.
9848
9849 Note that for local watchpoints, we re-evaluate it only if
9850 watchpoints frame id is still valid. If it's not, it means the
9851 watchpoint is out of scope and will be deleted soon. In fact,
9852 I'm not sure we'll ever be called in this case.
9853
9854 If a local watchpoint's frame id is still valid, then
9855 exp_valid_block is likewise valid, and we can safely use it.
9856
9857 Don't do anything about disabled watchpoints, since they will be
9858 reevaluated again when enabled. */
9859 update_watchpoint (this, true /* reparse */);
9860 }
9861
9862 /* Implement the "insert" method for hardware watchpoints. */
9863
9864 int
9865 watchpoint::insert_location (struct bp_location *bl)
9866 {
9867 int length = exact ? 1 : bl->length;
9868
9869 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9870 cond_exp.get ());
9871 }
9872
9873 /* Implement the "remove" method for hardware watchpoints. */
9874
9875 int
9876 watchpoint::remove_location (struct bp_location *bl,
9877 enum remove_bp_reason reason)
9878 {
9879 int length = exact ? 1 : bl->length;
9880
9881 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9882 cond_exp.get ());
9883 }
9884
9885 int
9886 watchpoint::breakpoint_hit (const struct bp_location *bl,
9887 const address_space *aspace, CORE_ADDR bp_addr,
9888 const target_waitstatus &ws)
9889 {
9890 struct breakpoint *b = bl->owner;
9891
9892 /* Continuable hardware watchpoints are treated as non-existent if the
9893 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9894 some data address). Otherwise gdb won't stop on a break instruction
9895 in the code (not from a breakpoint) when a hardware watchpoint has
9896 been defined. Also skip watchpoints which we know did not trigger
9897 (did not match the data address). */
9898 if (is_hardware_watchpoint (b)
9899 && watchpoint_triggered == watch_triggered_no)
9900 return 0;
9901
9902 return 1;
9903 }
9904
9905 void
9906 watchpoint::check_status (bpstat *bs)
9907 {
9908 bpstat_check_watchpoint (bs);
9909 }
9910
9911 /* Implement the "resources_needed" method for hardware
9912 watchpoints. */
9913
9914 int
9915 watchpoint::resources_needed (const struct bp_location *bl)
9916 {
9917 int length = exact? 1 : bl->length;
9918
9919 return target_region_ok_for_hw_watchpoint (bl->address, length);
9920 }
9921
9922 /* Implement the "works_in_software_mode" method for hardware
9923 watchpoints. */
9924
9925 bool
9926 watchpoint::works_in_software_mode () const
9927 {
9928 /* Read and access watchpoints only work with hardware support. */
9929 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9930 }
9931
9932 enum print_stop_action
9933 watchpoint::print_it (const bpstat *bs) const
9934 {
9935 enum print_stop_action result;
9936 struct ui_out *uiout = current_uiout;
9937
9938 gdb_assert (bs->bp_location_at != NULL);
9939
9940 annotate_watchpoint (this->number);
9941 maybe_print_thread_hit_breakpoint (uiout);
9942
9943 string_file stb;
9944
9945 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9946 switch (this->type)
9947 {
9948 case bp_watchpoint:
9949 case bp_hardware_watchpoint:
9950 if (uiout->is_mi_like_p ())
9951 uiout->field_string
9952 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9953 mention (this);
9954 tuple_emitter.emplace (uiout, "value");
9955 uiout->text ("\nOld value = ");
9956 watchpoint_value_print (bs->old_val.get (), &stb);
9957 uiout->field_stream ("old", stb);
9958 uiout->text ("\nNew value = ");
9959 watchpoint_value_print (val.get (), &stb);
9960 uiout->field_stream ("new", stb);
9961 uiout->text ("\n");
9962 /* More than one watchpoint may have been triggered. */
9963 result = PRINT_UNKNOWN;
9964 break;
9965
9966 case bp_read_watchpoint:
9967 if (uiout->is_mi_like_p ())
9968 uiout->field_string
9969 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9970 mention (this);
9971 tuple_emitter.emplace (uiout, "value");
9972 uiout->text ("\nValue = ");
9973 watchpoint_value_print (val.get (), &stb);
9974 uiout->field_stream ("value", stb);
9975 uiout->text ("\n");
9976 result = PRINT_UNKNOWN;
9977 break;
9978
9979 case bp_access_watchpoint:
9980 if (bs->old_val != NULL)
9981 {
9982 if (uiout->is_mi_like_p ())
9983 uiout->field_string
9984 ("reason",
9985 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9986 mention (this);
9987 tuple_emitter.emplace (uiout, "value");
9988 uiout->text ("\nOld value = ");
9989 watchpoint_value_print (bs->old_val.get (), &stb);
9990 uiout->field_stream ("old", stb);
9991 uiout->text ("\nNew value = ");
9992 }
9993 else
9994 {
9995 mention (this);
9996 if (uiout->is_mi_like_p ())
9997 uiout->field_string
9998 ("reason",
9999 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10000 tuple_emitter.emplace (uiout, "value");
10001 uiout->text ("\nValue = ");
10002 }
10003 watchpoint_value_print (val.get (), &stb);
10004 uiout->field_stream ("new", stb);
10005 uiout->text ("\n");
10006 result = PRINT_UNKNOWN;
10007 break;
10008 default:
10009 result = PRINT_UNKNOWN;
10010 }
10011
10012 return result;
10013 }
10014
10015 /* Implement the "print_mention" method for hardware watchpoints. */
10016
10017 void
10018 watchpoint::print_mention () const
10019 {
10020 struct ui_out *uiout = current_uiout;
10021 const char *tuple_name;
10022
10023 switch (type)
10024 {
10025 case bp_watchpoint:
10026 uiout->text ("Watchpoint ");
10027 tuple_name = "wpt";
10028 break;
10029 case bp_hardware_watchpoint:
10030 uiout->text ("Hardware watchpoint ");
10031 tuple_name = "wpt";
10032 break;
10033 case bp_read_watchpoint:
10034 uiout->text ("Hardware read watchpoint ");
10035 tuple_name = "hw-rwpt";
10036 break;
10037 case bp_access_watchpoint:
10038 uiout->text ("Hardware access (read/write) watchpoint ");
10039 tuple_name = "hw-awpt";
10040 break;
10041 default:
10042 internal_error (_("Invalid hardware watchpoint type."));
10043 }
10044
10045 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10046 uiout->field_signed ("number", number);
10047 uiout->text (": ");
10048 uiout->field_string ("exp", exp_string.get ());
10049 }
10050
10051 /* Implement the "print_recreate" method for watchpoints. */
10052
10053 void
10054 watchpoint::print_recreate (struct ui_file *fp) const
10055 {
10056 switch (type)
10057 {
10058 case bp_watchpoint:
10059 case bp_hardware_watchpoint:
10060 gdb_printf (fp, "watch");
10061 break;
10062 case bp_read_watchpoint:
10063 gdb_printf (fp, "rwatch");
10064 break;
10065 case bp_access_watchpoint:
10066 gdb_printf (fp, "awatch");
10067 break;
10068 default:
10069 internal_error (_("Invalid watchpoint type."));
10070 }
10071
10072 gdb_printf (fp, " %s", exp_string.get ());
10073 print_recreate_thread (fp);
10074 }
10075
10076 /* Implement the "explains_signal" method for watchpoints. */
10077
10078 bool
10079 watchpoint::explains_signal (enum gdb_signal sig)
10080 {
10081 /* A software watchpoint cannot cause a signal other than
10082 GDB_SIGNAL_TRAP. */
10083 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10084 return false;
10085
10086 return true;
10087 }
10088
10089 struct masked_watchpoint : public watchpoint
10090 {
10091 using watchpoint::watchpoint;
10092
10093 int insert_location (struct bp_location *) override;
10094 int remove_location (struct bp_location *,
10095 enum remove_bp_reason reason) override;
10096 int resources_needed (const struct bp_location *) override;
10097 bool works_in_software_mode () const override;
10098 enum print_stop_action print_it (const bpstat *bs) const override;
10099 void print_one_detail (struct ui_out *) const override;
10100 void print_mention () const override;
10101 void print_recreate (struct ui_file *fp) const override;
10102 };
10103
10104 /* Implement the "insert" method for masked hardware watchpoints. */
10105
10106 int
10107 masked_watchpoint::insert_location (struct bp_location *bl)
10108 {
10109 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
10110 bl->watchpoint_type);
10111 }
10112
10113 /* Implement the "remove" method for masked hardware watchpoints. */
10114
10115 int
10116 masked_watchpoint::remove_location (struct bp_location *bl,
10117 enum remove_bp_reason reason)
10118 {
10119 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
10120 bl->watchpoint_type);
10121 }
10122
10123 /* Implement the "resources_needed" method for masked hardware
10124 watchpoints. */
10125
10126 int
10127 masked_watchpoint::resources_needed (const struct bp_location *bl)
10128 {
10129 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
10130 }
10131
10132 /* Implement the "works_in_software_mode" method for masked hardware
10133 watchpoints. */
10134
10135 bool
10136 masked_watchpoint::works_in_software_mode () const
10137 {
10138 return false;
10139 }
10140
10141 /* Implement the "print_it" method for masked hardware
10142 watchpoints. */
10143
10144 enum print_stop_action
10145 masked_watchpoint::print_it (const bpstat *bs) const
10146 {
10147 struct ui_out *uiout = current_uiout;
10148
10149 /* Masked watchpoints have only one location. */
10150 gdb_assert (this->has_single_location ());
10151
10152 annotate_watchpoint (this->number);
10153 maybe_print_thread_hit_breakpoint (uiout);
10154
10155 switch (this->type)
10156 {
10157 case bp_hardware_watchpoint:
10158 if (uiout->is_mi_like_p ())
10159 uiout->field_string
10160 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10161 break;
10162
10163 case bp_read_watchpoint:
10164 if (uiout->is_mi_like_p ())
10165 uiout->field_string
10166 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10167 break;
10168
10169 case bp_access_watchpoint:
10170 if (uiout->is_mi_like_p ())
10171 uiout->field_string
10172 ("reason",
10173 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10174 break;
10175 default:
10176 internal_error (_("Invalid hardware watchpoint type."));
10177 }
10178
10179 mention (this);
10180 uiout->text (_("\n\
10181 Check the underlying instruction at PC for the memory\n\
10182 address and value which triggered this watchpoint.\n"));
10183 uiout->text ("\n");
10184
10185 /* More than one watchpoint may have been triggered. */
10186 return PRINT_UNKNOWN;
10187 }
10188
10189 /* Implement the "print_one_detail" method for masked hardware
10190 watchpoints. */
10191
10192 void
10193 masked_watchpoint::print_one_detail (struct ui_out *uiout) const
10194 {
10195 /* Masked watchpoints have only one location. */
10196 gdb_assert (this->has_single_location ());
10197
10198 uiout->text ("\tmask ");
10199 uiout->field_core_addr ("mask", this->first_loc ().gdbarch, hw_wp_mask);
10200 uiout->text ("\n");
10201 }
10202
10203 /* Implement the "print_mention" method for masked hardware
10204 watchpoints. */
10205
10206 void
10207 masked_watchpoint::print_mention () const
10208 {
10209 struct ui_out *uiout = current_uiout;
10210 const char *tuple_name;
10211
10212 switch (type)
10213 {
10214 case bp_hardware_watchpoint:
10215 uiout->text ("Masked hardware watchpoint ");
10216 tuple_name = "wpt";
10217 break;
10218 case bp_read_watchpoint:
10219 uiout->text ("Masked hardware read watchpoint ");
10220 tuple_name = "hw-rwpt";
10221 break;
10222 case bp_access_watchpoint:
10223 uiout->text ("Masked hardware access (read/write) watchpoint ");
10224 tuple_name = "hw-awpt";
10225 break;
10226 default:
10227 internal_error (_("Invalid hardware watchpoint type."));
10228 }
10229
10230 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10231 uiout->field_signed ("number", number);
10232 uiout->text (": ");
10233 uiout->field_string ("exp", exp_string.get ());
10234 }
10235
10236 /* Implement the "print_recreate" method for masked hardware
10237 watchpoints. */
10238
10239 void
10240 masked_watchpoint::print_recreate (struct ui_file *fp) const
10241 {
10242 switch (type)
10243 {
10244 case bp_hardware_watchpoint:
10245 gdb_printf (fp, "watch");
10246 break;
10247 case bp_read_watchpoint:
10248 gdb_printf (fp, "rwatch");
10249 break;
10250 case bp_access_watchpoint:
10251 gdb_printf (fp, "awatch");
10252 break;
10253 default:
10254 internal_error (_("Invalid hardware watchpoint type."));
10255 }
10256
10257 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
10258 phex (hw_wp_mask, sizeof (CORE_ADDR)));
10259 print_recreate_thread (fp);
10260 }
10261
10262 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10263
10264 static bool
10265 is_masked_watchpoint (const struct breakpoint *b)
10266 {
10267 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
10268 }
10269
10270 /* accessflag: hw_write: watch write,
10271 hw_read: watch read,
10272 hw_access: watch access (read or write) */
10273 static void
10274 watch_command_1 (const char *arg, int accessflag, int from_tty,
10275 bool just_location, bool internal)
10276 {
10277 struct breakpoint *scope_breakpoint = NULL;
10278 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10279 struct value *result;
10280 int saved_bitpos = 0, saved_bitsize = 0;
10281 const char *exp_start = NULL;
10282 const char *exp_end = NULL;
10283 const char *tok, *end_tok;
10284 int toklen = -1;
10285 const char *cond_start = NULL;
10286 const char *cond_end = NULL;
10287 enum bptype bp_type;
10288 int thread = -1;
10289 int inferior = -1;
10290 /* Flag to indicate whether we are going to use masks for
10291 the hardware watchpoint. */
10292 bool use_mask = false;
10293 CORE_ADDR mask = 0;
10294 int task = -1;
10295
10296 /* Make sure that we actually have parameters to parse. */
10297 if (arg != NULL && arg[0] != '\0')
10298 {
10299 const char *value_start;
10300
10301 exp_end = arg + strlen (arg);
10302
10303 /* Look for "parameter value" pairs at the end
10304 of the arguments string. */
10305 for (tok = exp_end - 1; tok > arg; tok--)
10306 {
10307 /* Skip whitespace at the end of the argument list. */
10308 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10309 tok--;
10310
10311 /* Find the beginning of the last token.
10312 This is the value of the parameter. */
10313 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10314 tok--;
10315 value_start = tok + 1;
10316
10317 /* Skip whitespace. */
10318 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10319 tok--;
10320
10321 end_tok = tok;
10322
10323 /* Find the beginning of the second to last token.
10324 This is the parameter itself. */
10325 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10326 tok--;
10327 tok++;
10328 toklen = end_tok - tok + 1;
10329
10330 if (toklen == 6 && startswith (tok, "thread"))
10331 {
10332 struct thread_info *thr;
10333 /* At this point we've found a "thread" token, which means
10334 the user is trying to set a watchpoint that triggers
10335 only in a specific thread. */
10336 const char *endp;
10337
10338 if (thread != -1)
10339 error(_("You can specify only one thread."));
10340
10341 if (task != -1)
10342 error (_("You can specify only one of thread or task."));
10343
10344 if (inferior != -1)
10345 error (_("You can specify only one of inferior or thread."));
10346
10347 /* Extract the thread ID from the next token. */
10348 thr = parse_thread_id (value_start, &endp);
10349 if (value_start == endp)
10350 error (_("Junk after thread keyword."));
10351
10352 thread = thr->global_num;
10353 }
10354 else if (toklen == 4 && startswith (tok, "task"))
10355 {
10356 char *tmp;
10357
10358 if (task != -1)
10359 error(_("You can specify only one task."));
10360
10361 if (thread != -1)
10362 error (_("You can specify only one of thread or task."));
10363
10364 if (inferior != -1)
10365 error (_("You can specify only one of inferior or task."));
10366
10367 task = strtol (value_start, &tmp, 0);
10368 if (tmp == value_start)
10369 error (_("Junk after task keyword."));
10370 if (!valid_task_id (task))
10371 error (_("Unknown task %d."), task);
10372 }
10373 else if (toklen == 8 && startswith (tok, "inferior"))
10374 {
10375 /* Support for watchpoints will be added in a later commit. */
10376 error (_("Cannot use 'inferior' keyword with watchpoints"));
10377 }
10378 else if (toklen == 4 && startswith (tok, "mask"))
10379 {
10380 /* We've found a "mask" token, which means the user wants to
10381 create a hardware watchpoint that is going to have the mask
10382 facility. */
10383 struct value *mask_value;
10384
10385 if (use_mask)
10386 error(_("You can specify only one mask."));
10387
10388 use_mask = just_location = true;
10389
10390 scoped_value_mark mark;
10391 mask_value = parse_to_comma_and_eval (&value_start);
10392 mask = value_as_address (mask_value);
10393 }
10394 else
10395 /* We didn't recognize what we found. We should stop here. */
10396 break;
10397
10398 /* Truncate the string and get rid of the "parameter value" pair before
10399 the arguments string is parsed by the parse_exp_1 function. */
10400 exp_end = tok;
10401 }
10402 }
10403 else
10404 exp_end = arg;
10405
10406 /* Parse the rest of the arguments. From here on out, everything
10407 is in terms of a newly allocated string instead of the original
10408 ARG. */
10409 std::string expression (arg, exp_end - arg);
10410 exp_start = arg = expression.c_str ();
10411 innermost_block_tracker tracker;
10412 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10413 exp_end = arg;
10414 /* Remove trailing whitespace from the expression before saving it.
10415 This makes the eventual display of the expression string a bit
10416 prettier. */
10417 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10418 --exp_end;
10419
10420 /* Checking if the expression is not constant. */
10421 if (watchpoint_exp_is_const (exp.get ()))
10422 {
10423 int len;
10424
10425 len = exp_end - exp_start;
10426 while (len > 0 && isspace (exp_start[len - 1]))
10427 len--;
10428 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10429 }
10430
10431 exp_valid_block = tracker.block ();
10432 struct value *mark = value_mark ();
10433 struct value *val_as_value = nullptr;
10434 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10435 just_location);
10436
10437 if (val_as_value != NULL && just_location)
10438 {
10439 saved_bitpos = val_as_value->bitpos ();
10440 saved_bitsize = val_as_value->bitsize ();
10441 }
10442
10443 value_ref_ptr val;
10444 if (just_location)
10445 {
10446 int ret;
10447
10448 exp_valid_block = NULL;
10449 val = release_value (value_addr (result));
10450 value_free_to_mark (mark);
10451
10452 if (use_mask)
10453 {
10454 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10455 mask);
10456 if (ret == -1)
10457 error (_("This target does not support masked watchpoints."));
10458 else if (ret == -2)
10459 error (_("Invalid mask or memory region."));
10460 }
10461 }
10462 else if (val_as_value != NULL)
10463 val = release_value (val_as_value);
10464
10465 tok = skip_spaces (arg);
10466 end_tok = skip_to_space (tok);
10467
10468 toklen = end_tok - tok;
10469 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10470 {
10471 tok = cond_start = end_tok + 1;
10472 innermost_block_tracker if_tracker;
10473 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10474
10475 /* The watchpoint expression may not be local, but the condition
10476 may still be. E.g.: `watch global if local > 0'. */
10477 cond_exp_valid_block = if_tracker.block ();
10478
10479 cond_end = tok;
10480 }
10481 if (*tok)
10482 error (_("Junk at end of command."));
10483
10484 frame_info_ptr wp_frame = block_innermost_frame (exp_valid_block);
10485
10486 /* Save this because create_internal_breakpoint below invalidates
10487 'wp_frame'. */
10488 frame_id watchpoint_frame = get_frame_id (wp_frame);
10489
10490 /* If the expression is "local", then set up a "watchpoint scope"
10491 breakpoint at the point where we've left the scope of the watchpoint
10492 expression. Create the scope breakpoint before the watchpoint, so
10493 that we will encounter it first in bpstat_stop_status. */
10494 if (exp_valid_block != NULL && wp_frame != NULL)
10495 {
10496 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10497
10498 if (frame_id_p (caller_frame_id))
10499 {
10500 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10501 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10502
10503 scope_breakpoint
10504 = create_internal_breakpoint (caller_arch, caller_pc,
10505 bp_watchpoint_scope);
10506
10507 /* create_internal_breakpoint could invalidate WP_FRAME. */
10508 wp_frame = NULL;
10509
10510 scope_breakpoint->enable_state = bp_enabled;
10511
10512 /* Automatically delete the breakpoint when it hits. */
10513 scope_breakpoint->disposition = disp_del;
10514
10515 /* Only break in the proper frame (help with recursion). */
10516 scope_breakpoint->frame_id = caller_frame_id;
10517
10518 /* Set the address at which we will stop. */
10519 bp_location &loc = scope_breakpoint->first_loc ();
10520 loc.gdbarch = caller_arch;
10521 loc.requested_address = caller_pc;
10522 loc.address
10523 = adjust_breakpoint_address (loc.gdbarch, loc.requested_address,
10524 scope_breakpoint->type,
10525 current_program_space);
10526 }
10527 }
10528
10529 /* Now set up the breakpoint. We create all watchpoints as hardware
10530 watchpoints here even if hardware watchpoints are turned off, a call
10531 to update_watchpoint later in this function will cause the type to
10532 drop back to bp_watchpoint (software watchpoint) if required. */
10533
10534 if (accessflag == hw_read)
10535 bp_type = bp_read_watchpoint;
10536 else if (accessflag == hw_access)
10537 bp_type = bp_access_watchpoint;
10538 else
10539 bp_type = bp_hardware_watchpoint;
10540
10541 std::unique_ptr<watchpoint> w;
10542 if (use_mask)
10543 w.reset (new masked_watchpoint (nullptr, bp_type));
10544 else
10545 w.reset (new watchpoint (nullptr, bp_type));
10546
10547 /* At most one of thread or task can be set on a watchpoint. */
10548 gdb_assert (thread == -1 || task == -1);
10549 w->thread = thread;
10550 w->inferior = inferior;
10551 w->task = task;
10552 w->disposition = disp_donttouch;
10553 w->pspace = current_program_space;
10554 w->exp = std::move (exp);
10555 w->exp_valid_block = exp_valid_block;
10556 w->cond_exp_valid_block = cond_exp_valid_block;
10557 if (just_location)
10558 {
10559 struct type *t = val->type ();
10560 CORE_ADDR addr = value_as_address (val.get ());
10561
10562 w->exp_string_reparse
10563 = current_language->watch_location_expression (t, addr);
10564
10565 w->exp_string = xstrprintf ("-location %.*s",
10566 (int) (exp_end - exp_start), exp_start);
10567 }
10568 else
10569 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10570
10571 if (use_mask)
10572 {
10573 w->hw_wp_mask = mask;
10574 }
10575 else
10576 {
10577 w->val = val;
10578 w->val_bitpos = saved_bitpos;
10579 w->val_bitsize = saved_bitsize;
10580 w->val_valid = true;
10581 }
10582
10583 if (cond_start)
10584 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10585 else
10586 w->cond_string = 0;
10587
10588 if (frame_id_p (watchpoint_frame))
10589 {
10590 w->watchpoint_frame = watchpoint_frame;
10591 w->watchpoint_thread = inferior_ptid;
10592 }
10593 else
10594 {
10595 w->watchpoint_frame = null_frame_id;
10596 w->watchpoint_thread = null_ptid;
10597 }
10598
10599 if (scope_breakpoint != NULL)
10600 {
10601 /* The scope breakpoint is related to the watchpoint. We will
10602 need to act on them together. */
10603 w->related_breakpoint = scope_breakpoint;
10604 scope_breakpoint->related_breakpoint = w.get ();
10605 }
10606
10607 if (!just_location)
10608 value_free_to_mark (mark);
10609
10610 /* Finally update the new watchpoint. This creates the locations
10611 that should be inserted. */
10612 update_watchpoint (w.get (), true /* reparse */);
10613
10614 install_breakpoint (internal, std::move (w), 1);
10615 }
10616
10617 /* Return count of debug registers needed to watch the given expression.
10618 If the watchpoint cannot be handled in hardware return zero. */
10619
10620 static int
10621 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10622 {
10623 int found_memory_cnt = 0;
10624
10625 /* Did the user specifically forbid us to use hardware watchpoints? */
10626 if (!can_use_hw_watchpoints)
10627 return 0;
10628
10629 gdb_assert (!vals.empty ());
10630 struct value *head = vals[0].get ();
10631
10632 /* Make sure that the value of the expression depends only upon
10633 memory contents, and values computed from them within GDB. If we
10634 find any register references or function calls, we can't use a
10635 hardware watchpoint.
10636
10637 The idea here is that evaluating an expression generates a series
10638 of values, one holding the value of every subexpression. (The
10639 expression a*b+c has five subexpressions: a, b, a*b, c, and
10640 a*b+c.) GDB's values hold almost enough information to establish
10641 the criteria given above --- they identify memory lvalues,
10642 register lvalues, computed values, etcetera. So we can evaluate
10643 the expression, and then scan the chain of values that leaves
10644 behind to decide whether we can detect any possible change to the
10645 expression's final value using only hardware watchpoints.
10646
10647 However, I don't think that the values returned by inferior
10648 function calls are special in any way. So this function may not
10649 notice that an expression involving an inferior function call
10650 can't be watched with hardware watchpoints. FIXME. */
10651 for (const value_ref_ptr &iter : vals)
10652 {
10653 struct value *v = iter.get ();
10654
10655 if (v->lval () == lval_memory)
10656 {
10657 if (v != head && v->lazy ())
10658 /* A lazy memory lvalue in the chain is one that GDB never
10659 needed to fetch; we either just used its address (e.g.,
10660 `a' in `a.b') or we never needed it at all (e.g., `a'
10661 in `a,b'). This doesn't apply to HEAD; if that is
10662 lazy then it was not readable, but watch it anyway. */
10663 ;
10664 else
10665 {
10666 /* Ahh, memory we actually used! Check if we can cover
10667 it with hardware watchpoints. */
10668 struct type *vtype = check_typedef (v->type ());
10669
10670 /* We only watch structs and arrays if user asked for it
10671 explicitly, never if they just happen to appear in a
10672 middle of some value chain. */
10673 if (v == head
10674 || (vtype->code () != TYPE_CODE_STRUCT
10675 && vtype->code () != TYPE_CODE_ARRAY))
10676 {
10677 CORE_ADDR vaddr = v->address ();
10678 int len;
10679 int num_regs;
10680
10681 len = (target_exact_watchpoints
10682 && is_scalar_type_recursive (vtype))?
10683 1 : v->type ()->length ();
10684
10685 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10686 if (!num_regs)
10687 return 0;
10688 else
10689 found_memory_cnt += num_regs;
10690 }
10691 }
10692 }
10693 else if (v->lval () != not_lval && !v->deprecated_modifiable ())
10694 return 0; /* These are values from the history (e.g., $1). */
10695 else if (v->lval () == lval_register)
10696 return 0; /* Cannot watch a register with a HW watchpoint. */
10697 }
10698
10699 /* The expression itself looks suitable for using a hardware
10700 watchpoint, but give the target machine a chance to reject it. */
10701 return found_memory_cnt;
10702 }
10703
10704 void
10705 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10706 {
10707 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10708 }
10709
10710 /* Options for the watch, awatch, and rwatch commands. */
10711
10712 struct watch_options
10713 {
10714 /* For -location. */
10715 bool location = false;
10716 };
10717
10718 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10719
10720 Historically GDB always accepted both '-location' and '-l' flags for
10721 these commands (both flags being synonyms). When converting to the
10722 newer option scheme only '-location' is added here. That's fine (for
10723 backward compatibility) as any non-ambiguous prefix of a flag will be
10724 accepted, so '-l', '-loc', are now all accepted.
10725
10726 What this means is that, if in the future, we add any new flag here
10727 that starts with '-l' then this will break backward compatibility, so
10728 please, don't do that! */
10729
10730 static const gdb::option::option_def watch_option_defs[] = {
10731 gdb::option::flag_option_def<watch_options> {
10732 "location",
10733 [] (watch_options *opt) { return &opt->location; },
10734 N_("\
10735 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10736 -l can be used as a short form of -location."),
10737 },
10738 };
10739
10740 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10741 commands. */
10742
10743 static gdb::option::option_def_group
10744 make_watch_options_def_group (watch_options *opts)
10745 {
10746 return {{watch_option_defs}, opts};
10747 }
10748
10749 /* A helper function that looks for the "-location" argument and then
10750 calls watch_command_1. */
10751
10752 static void
10753 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10754 {
10755 watch_options opts;
10756 auto grp = make_watch_options_def_group (&opts);
10757 gdb::option::process_options
10758 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10759 if (arg != nullptr && *arg == '\0')
10760 arg = nullptr;
10761
10762 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10763 }
10764
10765 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10766 static void
10767 watch_command_completer (struct cmd_list_element *ignore,
10768 completion_tracker &tracker,
10769 const char *text, const char * /*word*/)
10770 {
10771 const auto group = make_watch_options_def_group (nullptr);
10772 if (gdb::option::complete_options
10773 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10774 return;
10775
10776 const char *word = advance_to_expression_complete_word_point (tracker, text);
10777 expression_completer (ignore, tracker, text, word);
10778 }
10779
10780 static void
10781 watch_command (const char *arg, int from_tty)
10782 {
10783 watch_maybe_just_location (arg, hw_write, from_tty);
10784 }
10785
10786 void
10787 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10788 {
10789 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10790 }
10791
10792 static void
10793 rwatch_command (const char *arg, int from_tty)
10794 {
10795 watch_maybe_just_location (arg, hw_read, from_tty);
10796 }
10797
10798 void
10799 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
10800 {
10801 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10802 }
10803
10804 static void
10805 awatch_command (const char *arg, int from_tty)
10806 {
10807 watch_maybe_just_location (arg, hw_access, from_tty);
10808 }
10809 \f
10810
10811 /* Data for the FSM that manages the until(location)/advance commands
10812 in infcmd.c. Here because it uses the mechanisms of
10813 breakpoints. */
10814
10815 struct until_break_fsm : public thread_fsm
10816 {
10817 /* The thread that was current when the command was executed. */
10818 int thread;
10819
10820 /* The breakpoint set at the return address in the caller frame,
10821 plus breakpoints at all the destination locations. */
10822 std::vector<breakpoint_up> breakpoints;
10823
10824 until_break_fsm (struct interp *cmd_interp, int thread,
10825 std::vector<breakpoint_up> &&breakpoints)
10826 : thread_fsm (cmd_interp),
10827 thread (thread),
10828 breakpoints (std::move (breakpoints))
10829 {
10830 }
10831
10832 void clean_up (struct thread_info *thread) override;
10833 bool should_stop (struct thread_info *thread) override;
10834 enum async_reply_reason do_async_reply_reason () override;
10835 };
10836
10837 /* Implementation of the 'should_stop' FSM method for the
10838 until(location)/advance commands. */
10839
10840 bool
10841 until_break_fsm::should_stop (struct thread_info *tp)
10842 {
10843 for (const breakpoint_up &bp : breakpoints)
10844 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10845 bp.get ()) != NULL)
10846 {
10847 set_finished ();
10848 break;
10849 }
10850
10851 return true;
10852 }
10853
10854 /* Implementation of the 'clean_up' FSM method for the
10855 until(location)/advance commands. */
10856
10857 void
10858 until_break_fsm::clean_up (struct thread_info *)
10859 {
10860 /* Clean up our temporary breakpoints. */
10861 breakpoints.clear ();
10862 delete_longjmp_breakpoint (thread);
10863 }
10864
10865 /* Implementation of the 'async_reply_reason' FSM method for the
10866 until(location)/advance commands. */
10867
10868 enum async_reply_reason
10869 until_break_fsm::do_async_reply_reason ()
10870 {
10871 return EXEC_ASYNC_LOCATION_REACHED;
10872 }
10873
10874 void
10875 until_break_command (const char *arg, int from_tty, int anywhere)
10876 {
10877 frame_info_ptr frame;
10878 struct gdbarch *frame_gdbarch;
10879 struct frame_id stack_frame_id;
10880 struct frame_id caller_frame_id;
10881 int thread;
10882 struct thread_info *tp;
10883
10884 clear_proceed_status (0);
10885
10886 /* Set a breakpoint where the user wants it and at return from
10887 this function. */
10888
10889 location_spec_up locspec = string_to_location_spec (&arg, current_language);
10890
10891 std::vector<symtab_and_line> sals
10892 = (last_displayed_sal_is_valid ()
10893 ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10894 get_last_displayed_symtab (),
10895 get_last_displayed_line ())
10896 : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
10897 NULL, NULL, 0));
10898
10899 if (sals.empty ())
10900 error (_("Couldn't get information on specified line."));
10901
10902 if (*arg)
10903 error (_("Junk at end of arguments."));
10904
10905 tp = inferior_thread ();
10906 thread = tp->global_num;
10907
10908 /* Note linespec handling above invalidates the frame chain.
10909 Installing a breakpoint also invalidates the frame chain (as it
10910 may need to switch threads), so do any frame handling before
10911 that. */
10912
10913 frame = get_selected_frame (NULL);
10914 frame_gdbarch = get_frame_arch (frame);
10915 stack_frame_id = get_stack_frame_id (frame);
10916 caller_frame_id = frame_unwind_caller_id (frame);
10917
10918 /* Keep within the current frame, or in frames called by the current
10919 one. */
10920
10921 std::vector<breakpoint_up> breakpoints;
10922
10923 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10924
10925 if (frame_id_p (caller_frame_id))
10926 {
10927 struct symtab_and_line sal2;
10928 struct gdbarch *caller_gdbarch;
10929
10930 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10931 sal2.pc = frame_unwind_caller_pc (frame);
10932 caller_gdbarch = frame_unwind_caller_arch (frame);
10933
10934 breakpoint_up caller_breakpoint
10935 = set_momentary_breakpoint (caller_gdbarch, sal2,
10936 caller_frame_id, bp_until);
10937 breakpoints.emplace_back (std::move (caller_breakpoint));
10938
10939 set_longjmp_breakpoint (tp, stack_frame_id);
10940 lj_deleter.emplace (thread);
10941 }
10942
10943 /* set_momentary_breakpoint could invalidate FRAME. */
10944 frame = NULL;
10945
10946 /* If the user told us to continue until a specified location, we
10947 don't specify a frame at which we need to stop. Otherwise,
10948 specify the selected frame, because we want to stop only at the
10949 very same frame. */
10950 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10951
10952 for (symtab_and_line &sal : sals)
10953 {
10954 resolve_sal_pc (&sal);
10955
10956 breakpoint_up location_breakpoint
10957 = set_momentary_breakpoint (frame_gdbarch, sal,
10958 stop_frame_id, bp_until);
10959 breakpoints.emplace_back (std::move (location_breakpoint));
10960 }
10961
10962 tp->set_thread_fsm
10963 (std::unique_ptr<thread_fsm>
10964 (new until_break_fsm (command_interp (), tp->global_num,
10965 std::move (breakpoints))));
10966
10967 if (lj_deleter)
10968 lj_deleter->release ();
10969
10970 proceed (-1, GDB_SIGNAL_DEFAULT);
10971 }
10972
10973 \f
10974
10975 /* Compare two breakpoints and return a strcmp-like result. */
10976
10977 static int
10978 compare_breakpoints (const breakpoint *a, const breakpoint *b)
10979 {
10980 uintptr_t ua = (uintptr_t) a;
10981 uintptr_t ub = (uintptr_t) b;
10982
10983 if (a->number < b->number)
10984 return -1;
10985 else if (a->number > b->number)
10986 return 1;
10987
10988 /* Now sort by address, in case we see, e..g, two breakpoints with
10989 the number 0. */
10990 if (ua < ub)
10991 return -1;
10992 return ua > ub ? 1 : 0;
10993 }
10994
10995 /* Delete breakpoints by address or line. */
10996
10997 static void
10998 clear_command (const char *arg, int from_tty)
10999 {
11000 int default_match;
11001
11002 std::vector<symtab_and_line> decoded_sals;
11003 symtab_and_line last_sal;
11004 gdb::array_view<symtab_and_line> sals;
11005 if (arg)
11006 {
11007 decoded_sals
11008 = decode_line_with_current_source (arg,
11009 (DECODE_LINE_FUNFIRSTLINE
11010 | DECODE_LINE_LIST_MODE));
11011 default_match = 0;
11012 sals = decoded_sals;
11013 }
11014 else
11015 {
11016 /* Set sal's line, symtab, pc, and pspace to the values
11017 corresponding to the last call to print_frame_info. If the
11018 codepoint is not valid, this will set all the fields to 0. */
11019 last_sal = get_last_displayed_sal ();
11020 if (last_sal.symtab == 0)
11021 error (_("No source file specified."));
11022
11023 default_match = 1;
11024 sals = last_sal;
11025 }
11026
11027 /* We don't call resolve_sal_pc here. That's not as bad as it
11028 seems, because all existing breakpoints typically have both
11029 file/line and pc set. So, if clear is given file/line, we can
11030 match this to existing breakpoint without obtaining pc at all.
11031
11032 We only support clearing given the address explicitly
11033 present in breakpoint table. Say, we've set breakpoint
11034 at file:line. There were several PC values for that file:line,
11035 due to optimization, all in one block.
11036
11037 We've picked one PC value. If "clear" is issued with another
11038 PC corresponding to the same file:line, the breakpoint won't
11039 be cleared. We probably can still clear the breakpoint, but
11040 since the other PC value is never presented to user, user
11041 can only find it by guessing, and it does not seem important
11042 to support that. */
11043
11044 /* For each line spec given, delete bps which correspond to it. Do
11045 it in two passes, solely to preserve the current behavior that
11046 from_tty is forced true if we delete more than one
11047 breakpoint. */
11048
11049 std::vector<struct breakpoint *> found;
11050 for (const auto &sal : sals)
11051 {
11052 const char *sal_fullname;
11053
11054 /* If exact pc given, clear bpts at that pc.
11055 If line given (pc == 0), clear all bpts on specified line.
11056 If defaulting, clear all bpts on default line
11057 or at default pc.
11058
11059 defaulting sal.pc != 0 tests to do
11060
11061 0 1 pc
11062 1 1 pc _and_ line
11063 0 0 line
11064 1 0 <can't happen> */
11065
11066 sal_fullname = (sal.symtab == NULL
11067 ? NULL : symtab_to_fullname (sal.symtab));
11068
11069 /* Find all matching breakpoints and add them to 'found'. */
11070 for (breakpoint &b : all_breakpoints ())
11071 {
11072 int match = 0;
11073 /* Are we going to delete b? */
11074 if (b.type != bp_none && !is_watchpoint (&b)
11075 && user_breakpoint_p (&b))
11076 {
11077 for (bp_location &loc : b.locations ())
11078 {
11079 /* If the user specified file:line, don't allow a PC
11080 match. This matches historical gdb behavior. */
11081 int pc_match = (!sal.explicit_line
11082 && sal.pc
11083 && (loc.pspace == sal.pspace)
11084 && (loc.address == sal.pc)
11085 && (!section_is_overlay (loc.section)
11086 || loc.section == sal.section));
11087 int line_match = 0;
11088
11089 if ((default_match || sal.explicit_line)
11090 && loc.symtab != NULL
11091 && sal_fullname != NULL
11092 && sal.pspace == loc.pspace
11093 && loc.line_number == sal.line
11094 && filename_cmp (symtab_to_fullname (loc.symtab),
11095 sal_fullname) == 0)
11096 line_match = 1;
11097
11098 if (pc_match || line_match)
11099 {
11100 match = 1;
11101 break;
11102 }
11103 }
11104 }
11105
11106 if (match)
11107 found.push_back (&b);
11108 }
11109 }
11110
11111 /* Now go thru the 'found' chain and delete them. */
11112 if (found.empty ())
11113 {
11114 if (arg)
11115 error (_("No breakpoint at %s."), arg);
11116 else
11117 error (_("No breakpoint at this line."));
11118 }
11119
11120 /* Remove duplicates from the vec. */
11121 std::sort (found.begin (), found.end (),
11122 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11123 {
11124 return compare_breakpoints (bp_a, bp_b) < 0;
11125 });
11126 found.erase (std::unique (found.begin (), found.end (),
11127 [] (const breakpoint *bp_a, const breakpoint *bp_b)
11128 {
11129 return compare_breakpoints (bp_a, bp_b) == 0;
11130 }),
11131 found.end ());
11132
11133 if (found.size () > 1)
11134 from_tty = 1; /* Always report if deleted more than one. */
11135 if (from_tty)
11136 {
11137 if (found.size () == 1)
11138 gdb_printf (_("Deleted breakpoint "));
11139 else
11140 gdb_printf (_("Deleted breakpoints "));
11141 }
11142
11143 for (breakpoint *iter : found)
11144 {
11145 if (from_tty)
11146 gdb_printf ("%d ", iter->number);
11147 delete_breakpoint (iter);
11148 }
11149 if (from_tty)
11150 gdb_putc ('\n');
11151 }
11152 \f
11153 /* Delete breakpoint in BS if they are `delete' breakpoints and
11154 all breakpoints that are marked for deletion, whether hit or not.
11155 This is called after any breakpoint is hit, or after errors. */
11156
11157 void
11158 breakpoint_auto_delete (bpstat *bs)
11159 {
11160 for (; bs; bs = bs->next)
11161 if (bs->breakpoint_at
11162 && bs->breakpoint_at->disposition == disp_del
11163 && bs->stop)
11164 delete_breakpoint (bs->breakpoint_at);
11165
11166 for (breakpoint &b : all_breakpoints_safe ())
11167 if (b.disposition == disp_del_at_next_stop)
11168 delete_breakpoint (&b);
11169 }
11170
11171 /* A comparison function for bp_location AP and BP being interfaced to
11172 std::sort. Sort elements primarily by their ADDRESS (no matter what
11173 bl_address_is_meaningful says), secondarily by ordering first
11174 permanent elements and tertiarily just ensuring the array is sorted
11175 stable way despite std::sort being an unstable algorithm. */
11176
11177 static int
11178 bp_location_is_less_than (const bp_location *a, const bp_location *b)
11179 {
11180 if (a->address != b->address)
11181 return a->address < b->address;
11182
11183 /* Sort locations at the same address by their pspace number, keeping
11184 locations of the same inferior (in a multi-inferior environment)
11185 grouped. */
11186
11187 if (a->pspace->num != b->pspace->num)
11188 return a->pspace->num < b->pspace->num;
11189
11190 /* Sort permanent breakpoints first. */
11191 if (a->permanent != b->permanent)
11192 return a->permanent > b->permanent;
11193
11194 /* Sort by type in order to make duplicate determination easier.
11195 See update_global_location_list. This is kept in sync with
11196 breakpoint_locations_match. */
11197 if (a->loc_type < b->loc_type)
11198 return true;
11199
11200 /* Likewise, for range-breakpoints, sort by length. */
11201 if (a->loc_type == bp_loc_hardware_breakpoint
11202 && b->loc_type == bp_loc_hardware_breakpoint
11203 && a->length < b->length)
11204 return true;
11205
11206 /* Make the internal GDB representation stable across GDB runs
11207 where A and B memory inside GDB can differ. Breakpoint locations of
11208 the same type at the same address can be sorted in arbitrary order. */
11209
11210 if (a->owner->number != b->owner->number)
11211 return a->owner->number < b->owner->number;
11212
11213 return a < b;
11214 }
11215
11216 /* Set bp_locations_placed_address_before_address_max and
11217 bp_locations_shadow_len_after_address_max according to the current
11218 content of the bp_locations array. */
11219
11220 static void
11221 bp_locations_target_extensions_update (void)
11222 {
11223 bp_locations_placed_address_before_address_max = 0;
11224 bp_locations_shadow_len_after_address_max = 0;
11225
11226 for (bp_location *bl : all_bp_locations ())
11227 {
11228 CORE_ADDR start, end, addr;
11229
11230 if (!bp_location_has_shadow (bl))
11231 continue;
11232
11233 start = bl->target_info.placed_address;
11234 end = start + bl->target_info.shadow_len;
11235
11236 gdb_assert (bl->address >= start);
11237 addr = bl->address - start;
11238 if (addr > bp_locations_placed_address_before_address_max)
11239 bp_locations_placed_address_before_address_max = addr;
11240
11241 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11242
11243 gdb_assert (bl->address < end);
11244 addr = end - bl->address;
11245 if (addr > bp_locations_shadow_len_after_address_max)
11246 bp_locations_shadow_len_after_address_max = addr;
11247 }
11248 }
11249
11250 /* Download tracepoint locations if they haven't been. */
11251
11252 static void
11253 download_tracepoint_locations (void)
11254 {
11255 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
11256
11257 scoped_restore_current_pspace_and_thread restore_pspace_thread;
11258
11259 for (breakpoint &b : all_tracepoints ())
11260 {
11261 bool bp_location_downloaded = false;
11262
11263 if ((b.type == bp_fast_tracepoint
11264 ? !may_insert_fast_tracepoints
11265 : !may_insert_tracepoints))
11266 continue;
11267
11268 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11269 {
11270 if (target_can_download_tracepoint ())
11271 can_download_tracepoint = TRIBOOL_TRUE;
11272 else
11273 can_download_tracepoint = TRIBOOL_FALSE;
11274 }
11275
11276 if (can_download_tracepoint == TRIBOOL_FALSE)
11277 break;
11278
11279 for (bp_location &bl : b.locations ())
11280 {
11281 /* In tracepoint, locations are _never_ duplicated, so
11282 should_be_inserted is equivalent to
11283 unduplicated_should_be_inserted. */
11284 if (!should_be_inserted (&bl) || bl.inserted)
11285 continue;
11286
11287 switch_to_program_space_and_thread (bl.pspace);
11288
11289 target_download_tracepoint (&bl);
11290
11291 bl.inserted = 1;
11292 bp_location_downloaded = true;
11293 }
11294
11295 tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
11296 t.number_on_target = b.number;
11297 if (bp_location_downloaded)
11298 notify_breakpoint_modified (&b);
11299 }
11300 }
11301
11302 /* Swap the insertion/duplication state between two locations. */
11303
11304 static void
11305 swap_insertion (struct bp_location *left, struct bp_location *right)
11306 {
11307 const int left_inserted = left->inserted;
11308 const int left_duplicate = left->duplicate;
11309 const int left_needs_update = left->needs_update;
11310 const struct bp_target_info left_target_info = left->target_info;
11311
11312 /* Locations of tracepoints can never be duplicated. */
11313 if (is_tracepoint (left->owner))
11314 gdb_assert (!left->duplicate);
11315 if (is_tracepoint (right->owner))
11316 gdb_assert (!right->duplicate);
11317
11318 left->inserted = right->inserted;
11319 left->duplicate = right->duplicate;
11320 left->needs_update = right->needs_update;
11321 left->target_info = right->target_info;
11322 right->inserted = left_inserted;
11323 right->duplicate = left_duplicate;
11324 right->needs_update = left_needs_update;
11325 right->target_info = left_target_info;
11326 }
11327
11328 /* Force the re-insertion of the locations at ADDRESS. This is called
11329 once a new/deleted/modified duplicate location is found and we are evaluating
11330 conditions on the target's side. Such conditions need to be updated on
11331 the target. */
11332
11333 static void
11334 force_breakpoint_reinsertion (struct bp_location *bl)
11335 {
11336 CORE_ADDR address = 0;
11337 int pspace_num;
11338
11339 address = bl->address;
11340 pspace_num = bl->pspace->num;
11341
11342 /* This is only meaningful if the target is
11343 evaluating conditions and if the user has
11344 opted for condition evaluation on the target's
11345 side. */
11346 if (gdb_evaluates_breakpoint_condition_p ()
11347 || !target_supports_evaluation_of_breakpoint_conditions ())
11348 return;
11349
11350 /* Flag all breakpoint locations with this address and
11351 the same program space as the location
11352 as "its condition has changed". We need to
11353 update the conditions on the target's side. */
11354 for (bp_location *loc : all_bp_locations_at_addr (address))
11355 {
11356 if (!is_breakpoint (loc->owner)
11357 || pspace_num != loc->pspace->num)
11358 continue;
11359
11360 /* Flag the location appropriately. We use a different state to
11361 let everyone know that we already updated the set of locations
11362 with addr bl->address and program space bl->pspace. This is so
11363 we don't have to keep calling these functions just to mark locations
11364 that have already been marked. */
11365 loc->condition_changed = condition_updated;
11366
11367 /* Free the agent expression bytecode as well. We will compute
11368 it later on. */
11369 loc->cond_bytecode.reset ();
11370 }
11371 }
11372
11373 /* Called whether new breakpoints are created, or existing breakpoints
11374 deleted, to update the global location list and recompute which
11375 locations are duplicate of which.
11376
11377 The INSERT_MODE flag determines whether locations may not, may, or
11378 shall be inserted now. See 'enum ugll_insert_mode' for more
11379 info. */
11380
11381 static void
11382 update_global_location_list (enum ugll_insert_mode insert_mode)
11383 {
11384 /* Last breakpoint location address that was marked for update. */
11385 CORE_ADDR last_addr = 0;
11386 /* Last breakpoint location program space that was marked for update. */
11387 int last_pspace_num = -1;
11388
11389 breakpoint_debug_printf ("insert_mode = %s",
11390 ugll_insert_mode_text (insert_mode));
11391
11392 /* Used in the duplicates detection below. When iterating over all
11393 bp_locations, points to the first bp_location of a given address.
11394 Breakpoints and watchpoints of different types are never
11395 duplicates of each other. Keep one pointer for each type of
11396 breakpoint/watchpoint, so we only need to loop over all locations
11397 once. */
11398 struct bp_location *bp_loc_first; /* breakpoint */
11399 struct bp_location *wp_loc_first; /* hardware watchpoint */
11400 struct bp_location *awp_loc_first; /* access watchpoint */
11401 struct bp_location *rwp_loc_first; /* read watchpoint */
11402
11403 /* Saved former bp_locations array which we compare against the newly
11404 built bp_locations from the current state of ALL_BREAKPOINTS. */
11405 std::vector<bp_location *> old_locations = std::move (bp_locations);
11406 bp_locations.clear ();
11407
11408 for (breakpoint &b : all_breakpoints ())
11409 for (bp_location &loc : b.locations ())
11410 bp_locations.push_back (&loc);
11411
11412 /* See if we need to "upgrade" a software breakpoint to a hardware
11413 breakpoint. Do this before deciding whether locations are
11414 duplicates. Also do this before sorting because sorting order
11415 depends on location type. */
11416 for (bp_location *loc : bp_locations)
11417 if (!loc->inserted && should_be_inserted (loc))
11418 handle_automatic_hardware_breakpoints (loc);
11419
11420 std::sort (bp_locations.begin (), bp_locations.end (),
11421 bp_location_is_less_than);
11422
11423 bp_locations_target_extensions_update ();
11424
11425 /* Identify bp_location instances that are no longer present in the
11426 new list, and therefore should be freed. Note that it's not
11427 necessary that those locations should be removed from inferior --
11428 if there's another location at the same address (previously
11429 marked as duplicate), we don't need to remove/insert the
11430 location.
11431
11432 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11433 and former bp_location array state respectively. */
11434
11435 size_t loc_i = 0;
11436 for (bp_location *old_loc : old_locations)
11437 {
11438 /* Tells if 'old_loc' is found among the new locations. If
11439 not, we have to free it. */
11440 bool found_object = false;
11441 /* Tells if the location should remain inserted in the target. */
11442 bool keep_in_target = false;
11443 bool removed = false;
11444
11445 /* Skip LOCP entries which will definitely never be needed.
11446 Stop either at or being the one matching OLD_LOC. */
11447 while (loc_i < bp_locations.size ()
11448 && bp_locations[loc_i]->address < old_loc->address)
11449 loc_i++;
11450
11451 for (size_t loc2_i = loc_i;
11452 (loc2_i < bp_locations.size ()
11453 && bp_locations[loc2_i]->address == old_loc->address);
11454 loc2_i++)
11455 {
11456 /* Check if this is a new/duplicated location or a duplicated
11457 location that had its condition modified. If so, we want to send
11458 its condition to the target if evaluation of conditions is taking
11459 place there. */
11460 if (bp_locations[loc2_i]->condition_changed == condition_modified
11461 && (last_addr != old_loc->address
11462 || last_pspace_num != old_loc->pspace->num))
11463 {
11464 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11465 last_pspace_num = old_loc->pspace->num;
11466 }
11467
11468 if (bp_locations[loc2_i] == old_loc)
11469 found_object = true;
11470 }
11471
11472 /* We have already handled this address, update it so that we don't
11473 have to go through updates again. */
11474 last_addr = old_loc->address;
11475
11476 /* Target-side condition evaluation: Handle deleted locations. */
11477 if (!found_object)
11478 force_breakpoint_reinsertion (old_loc);
11479
11480 /* If this location is no longer present, and inserted, look if
11481 there's maybe a new location at the same address. If so,
11482 mark that one inserted, and don't remove this one. This is
11483 needed so that we don't have a time window where a breakpoint
11484 at certain location is not inserted. */
11485
11486 if (old_loc->inserted)
11487 {
11488 /* If the location is inserted now, we might have to remove
11489 it. */
11490
11491 if (found_object && should_be_inserted (old_loc))
11492 {
11493 /* The location is still present in the location list,
11494 and still should be inserted. Don't do anything. */
11495 keep_in_target = true;
11496 }
11497 else
11498 {
11499 /* This location still exists, but it won't be kept in the
11500 target since it may have been disabled. We proceed to
11501 remove its target-side condition. */
11502
11503 /* The location is either no longer present, or got
11504 disabled. See if there's another location at the
11505 same address, in which case we don't need to remove
11506 this one from the target. */
11507
11508 /* OLD_LOC comes from existing struct breakpoint. */
11509 if (bl_address_is_meaningful (old_loc))
11510 {
11511 for (size_t loc2_i = loc_i;
11512 (loc2_i < bp_locations.size ()
11513 && bp_locations[loc2_i]->address == old_loc->address);
11514 loc2_i++)
11515 {
11516 bp_location *loc2 = bp_locations[loc2_i];
11517
11518 if (loc2 == old_loc)
11519 continue;
11520
11521 if (breakpoint_locations_match (loc2, old_loc))
11522 {
11523 /* Read watchpoint locations are switched to
11524 access watchpoints, if the former are not
11525 supported, but the latter are. */
11526 if (is_hardware_watchpoint (old_loc->owner))
11527 {
11528 gdb_assert (is_hardware_watchpoint (loc2->owner));
11529 loc2->watchpoint_type = old_loc->watchpoint_type;
11530 }
11531
11532 /* loc2 is a duplicated location. We need to check
11533 if it should be inserted in case it will be
11534 unduplicated. */
11535 if (unduplicated_should_be_inserted (loc2))
11536 {
11537 swap_insertion (old_loc, loc2);
11538 keep_in_target = true;
11539 break;
11540 }
11541 }
11542 }
11543 }
11544 }
11545
11546 if (!keep_in_target)
11547 {
11548 if (remove_breakpoint (old_loc))
11549 {
11550 /* This is just about all we can do. We could keep
11551 this location on the global list, and try to
11552 remove it next time, but there's no particular
11553 reason why we will succeed next time.
11554
11555 Note that at this point, old_loc->owner is still
11556 valid, as delete_breakpoint frees the breakpoint
11557 only after calling us. */
11558 warning (_("error removing breakpoint %d at %s"),
11559 old_loc->owner->number,
11560 paddress (old_loc->gdbarch, old_loc->address));
11561 }
11562 removed = true;
11563 }
11564 }
11565
11566 if (!found_object)
11567 {
11568 if (removed && target_is_non_stop_p ()
11569 && need_moribund_for_location_type (old_loc))
11570 {
11571 /* This location was removed from the target. In
11572 non-stop mode, a race condition is possible where
11573 we've removed a breakpoint, but stop events for that
11574 breakpoint are already queued and will arrive later.
11575 We apply an heuristic to be able to distinguish such
11576 SIGTRAPs from other random SIGTRAPs: we keep this
11577 breakpoint location for a bit, and will retire it
11578 after we see some number of events. The theory here
11579 is that reporting of events should, "on the average",
11580 be fair, so after a while we'll see events from all
11581 threads that have anything of interest, and no longer
11582 need to keep this breakpoint location around. We
11583 don't hold locations forever so to reduce chances of
11584 mistaking a non-breakpoint SIGTRAP for a breakpoint
11585 SIGTRAP.
11586
11587 The heuristic failing can be disastrous on
11588 decr_pc_after_break targets.
11589
11590 On decr_pc_after_break targets, like e.g., x86-linux,
11591 if we fail to recognize a late breakpoint SIGTRAP,
11592 because events_till_retirement has reached 0 too
11593 soon, we'll fail to do the PC adjustment, and report
11594 a random SIGTRAP to the user. When the user resumes
11595 the inferior, it will most likely immediately crash
11596 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11597 corrupted, because of being resumed e.g., in the
11598 middle of a multi-byte instruction, or skipped a
11599 one-byte instruction. This was actually seen happen
11600 on native x86-linux, and should be less rare on
11601 targets that do not support new thread events, like
11602 remote, due to the heuristic depending on
11603 thread_count.
11604
11605 Mistaking a random SIGTRAP for a breakpoint trap
11606 causes similar symptoms (PC adjustment applied when
11607 it shouldn't), but then again, playing with SIGTRAPs
11608 behind the debugger's back is asking for trouble.
11609
11610 Since hardware watchpoint traps are always
11611 distinguishable from other traps, so we don't need to
11612 apply keep hardware watchpoint moribund locations
11613 around. We simply always ignore hardware watchpoint
11614 traps we can no longer explain. */
11615
11616 process_stratum_target *proc_target = nullptr;
11617 for (inferior *inf : all_inferiors ())
11618 if (inf->pspace == old_loc->pspace)
11619 {
11620 proc_target = inf->process_target ();
11621 break;
11622 }
11623 if (proc_target != nullptr)
11624 old_loc->events_till_retirement
11625 = 3 * (thread_count (proc_target) + 1);
11626 else
11627 old_loc->events_till_retirement = 1;
11628 old_loc->owner = NULL;
11629
11630 moribund_locations.push_back (old_loc);
11631 }
11632 else
11633 {
11634 old_loc->owner = NULL;
11635 decref_bp_location (&old_loc);
11636 }
11637 }
11638 }
11639
11640 /* Rescan breakpoints at the same address and section, marking the
11641 first one as "first" and any others as "duplicates". This is so
11642 that the bpt instruction is only inserted once. If we have a
11643 permanent breakpoint at the same place as BPT, make that one the
11644 official one, and the rest as duplicates. Permanent breakpoints
11645 are sorted first for the same address.
11646
11647 Do the same for hardware watchpoints, but also considering the
11648 watchpoint's type (regular/access/read) and length. */
11649
11650 bp_loc_first = NULL;
11651 wp_loc_first = NULL;
11652 awp_loc_first = NULL;
11653 rwp_loc_first = NULL;
11654
11655 for (bp_location *loc : all_bp_locations ())
11656 {
11657 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11658 non-NULL. */
11659 struct bp_location **loc_first_p;
11660 breakpoint *b = loc->owner;
11661
11662 if (!unduplicated_should_be_inserted (loc)
11663 || !bl_address_is_meaningful (loc)
11664 /* Don't detect duplicate for tracepoint locations because they are
11665 never duplicated. See the comments in field `duplicate' of
11666 `struct bp_location'. */
11667 || is_tracepoint (b))
11668 {
11669 /* Clear the condition modification flag. */
11670 loc->condition_changed = condition_unchanged;
11671 continue;
11672 }
11673
11674 if (b->type == bp_hardware_watchpoint)
11675 loc_first_p = &wp_loc_first;
11676 else if (b->type == bp_read_watchpoint)
11677 loc_first_p = &rwp_loc_first;
11678 else if (b->type == bp_access_watchpoint)
11679 loc_first_p = &awp_loc_first;
11680 else
11681 loc_first_p = &bp_loc_first;
11682
11683 if (*loc_first_p == NULL
11684 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11685 || !breakpoint_locations_match (loc, *loc_first_p))
11686 {
11687 *loc_first_p = loc;
11688 loc->duplicate = 0;
11689
11690 if (is_breakpoint (loc->owner) && loc->condition_changed)
11691 {
11692 loc->needs_update = 1;
11693 /* Clear the condition modification flag. */
11694 loc->condition_changed = condition_unchanged;
11695 }
11696 continue;
11697 }
11698
11699
11700 /* This and the above ensure the invariant that the first location
11701 is not duplicated, and is the inserted one.
11702 All following are marked as duplicated, and are not inserted. */
11703 if (loc->inserted)
11704 swap_insertion (loc, *loc_first_p);
11705 loc->duplicate = 1;
11706
11707 /* Clear the condition modification flag. */
11708 loc->condition_changed = condition_unchanged;
11709 }
11710
11711 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11712 {
11713 if (insert_mode != UGLL_DONT_INSERT)
11714 insert_breakpoint_locations ();
11715 else
11716 {
11717 /* Even though the caller told us to not insert new
11718 locations, we may still need to update conditions on the
11719 target's side of breakpoints that were already inserted
11720 if the target is evaluating breakpoint conditions. We
11721 only update conditions for locations that are marked
11722 "needs_update". */
11723 update_inserted_breakpoint_locations ();
11724 }
11725 }
11726
11727 if (insert_mode != UGLL_DONT_INSERT)
11728 download_tracepoint_locations ();
11729 }
11730
11731 void
11732 breakpoint_retire_moribund (void)
11733 {
11734 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11735 {
11736 struct bp_location *loc = moribund_locations[ix];
11737 if (--(loc->events_till_retirement) == 0)
11738 {
11739 decref_bp_location (&loc);
11740 unordered_remove (moribund_locations, ix);
11741 --ix;
11742 }
11743 }
11744 }
11745
11746 static void
11747 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11748 {
11749
11750 try
11751 {
11752 update_global_location_list (insert_mode);
11753 }
11754 catch (const gdb_exception_error &e)
11755 {
11756 }
11757 }
11758
11759 /* Clear BKP from a BPS. */
11760
11761 static void
11762 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
11763 {
11764 bpstat *bs;
11765
11766 for (bs = bps; bs; bs = bs->next)
11767 if (bs->breakpoint_at == bpt)
11768 {
11769 bs->breakpoint_at = NULL;
11770 bs->old_val = NULL;
11771 /* bs->commands will be freed later. */
11772 }
11773 }
11774
11775 /* Callback for iterate_over_threads. */
11776 static int
11777 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11778 {
11779 struct breakpoint *bpt = (struct breakpoint *) data;
11780
11781 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11782 return 0;
11783 }
11784
11785 /* See breakpoint.h. */
11786
11787 void
11788 code_breakpoint::say_where () const
11789 {
11790 struct value_print_options opts;
11791
11792 get_user_print_options (&opts);
11793
11794 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11795 single string. */
11796 if (!this->has_locations ())
11797 {
11798 /* For pending locations, the output differs slightly based
11799 on extra_string. If this is non-NULL, it contains either
11800 a condition or dprintf arguments. */
11801 if (extra_string == NULL)
11802 {
11803 gdb_printf (_(" (%s) pending."), locspec->to_string ());
11804 }
11805 else if (type == bp_dprintf)
11806 {
11807 gdb_printf (_(" (%s,%s) pending."),
11808 locspec->to_string (),
11809 extra_string.get ());
11810 }
11811 else
11812 {
11813 gdb_printf (_(" (%s %s) pending."),
11814 locspec->to_string (),
11815 extra_string.get ());
11816 }
11817 }
11818 else
11819 {
11820 const bp_location &bl = this->first_loc ();
11821 if (opts.addressprint || bl.symtab == nullptr)
11822 gdb_printf (" at %ps",
11823 styled_string (address_style.style (),
11824 paddress (bl.gdbarch,
11825 bl.address)));
11826 if (bl.symtab != NULL)
11827 {
11828 /* If there is a single location, we can print the location
11829 more nicely. */
11830 if (!this->has_multiple_locations ())
11831 {
11832 const char *filename
11833 = symtab_to_filename_for_display (bl.symtab);
11834 gdb_printf (": file %ps, line %d.",
11835 styled_string (file_name_style.style (),
11836 filename),
11837 bl.line_number);
11838 }
11839 else
11840 /* This is not ideal, but each location may have a
11841 different file name, and this at least reflects the
11842 real situation somewhat. */
11843 gdb_printf (": %s.", locspec->to_string ());
11844 }
11845
11846 if (this->has_multiple_locations ())
11847 {
11848 int n = std::distance (m_locations.begin (), m_locations.end ());
11849 gdb_printf (" (%d locations)", n);
11850 }
11851 }
11852 }
11853
11854 /* See breakpoint.h. */
11855
11856 bp_location_range breakpoint::locations () const
11857 {
11858 return bp_location_range (m_locations.begin (), m_locations.end ());
11859 }
11860
11861 struct bp_location *
11862 breakpoint::allocate_location ()
11863 {
11864 return new bp_location (this);
11865 }
11866
11867 /* See breakpoint.h. */
11868
11869 void
11870 breakpoint::add_location (bp_location &loc)
11871 {
11872 gdb_assert (loc.owner == this);
11873 gdb_assert (!loc.is_linked ());
11874
11875 auto ub = std::upper_bound (m_locations.begin (), m_locations.end (),
11876 loc,
11877 [] (const bp_location &left,
11878 const bp_location &right)
11879 { return left.address < right.address; });
11880 m_locations.insert (ub, loc);
11881 }
11882
11883 /* See breakpoint.h. */
11884
11885 void
11886 breakpoint::unadd_location (bp_location &loc)
11887 {
11888 gdb_assert (loc.owner == this);
11889 gdb_assert (loc.is_linked ());
11890
11891 m_locations.erase (m_locations.iterator_to (loc));
11892 }
11893
11894 #define internal_error_pure_virtual_called() \
11895 gdb_assert_not_reached ("pure virtual function called")
11896
11897 int
11898 breakpoint::insert_location (struct bp_location *bl)
11899 {
11900 internal_error_pure_virtual_called ();
11901 }
11902
11903 int
11904 breakpoint::remove_location (struct bp_location *bl,
11905 enum remove_bp_reason reason)
11906 {
11907 internal_error_pure_virtual_called ();
11908 }
11909
11910 int
11911 breakpoint::breakpoint_hit (const struct bp_location *bl,
11912 const address_space *aspace,
11913 CORE_ADDR bp_addr,
11914 const target_waitstatus &ws)
11915 {
11916 internal_error_pure_virtual_called ();
11917 }
11918
11919 int
11920 breakpoint::resources_needed (const struct bp_location *bl)
11921 {
11922 internal_error_pure_virtual_called ();
11923 }
11924
11925 enum print_stop_action
11926 breakpoint::print_it (const bpstat *bs) const
11927 {
11928 internal_error_pure_virtual_called ();
11929 }
11930
11931 void
11932 breakpoint::print_mention () const
11933 {
11934 internal_error_pure_virtual_called ();
11935 }
11936
11937 void
11938 breakpoint::print_recreate (struct ui_file *fp) const
11939 {
11940 internal_error_pure_virtual_called ();
11941 }
11942
11943 /* Default breakpoint_ops methods. */
11944
11945 void
11946 code_breakpoint::re_set ()
11947 {
11948 /* FIXME: is this still reachable? */
11949 if (breakpoint_location_spec_empty_p (this))
11950 {
11951 /* Anything without a location can't be re-set. */
11952 delete_breakpoint (this);
11953 return;
11954 }
11955
11956 re_set_default ();
11957 }
11958
11959 int
11960 code_breakpoint::insert_location (struct bp_location *bl)
11961 {
11962 CORE_ADDR addr = bl->target_info.reqstd_address;
11963
11964 bl->target_info.kind = breakpoint_kind (bl, &addr);
11965 bl->target_info.placed_address = addr;
11966
11967 int result;
11968 if (bl->loc_type == bp_loc_hardware_breakpoint)
11969 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
11970 else
11971 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11972
11973 if (result == 0 && bl->probe.prob != nullptr)
11974 {
11975 /* The insertion was successful, now let's set the probe's semaphore
11976 if needed. */
11977 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11978 }
11979
11980 return result;
11981 }
11982
11983 int
11984 code_breakpoint::remove_location (struct bp_location *bl,
11985 enum remove_bp_reason reason)
11986 {
11987 if (bl->probe.prob != nullptr)
11988 {
11989 /* Let's clear the semaphore before removing the location. */
11990 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11991 }
11992
11993 if (bl->loc_type == bp_loc_hardware_breakpoint)
11994 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11995 else
11996 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
11997 }
11998
11999 int
12000 code_breakpoint::breakpoint_hit (const struct bp_location *bl,
12001 const address_space *aspace,
12002 CORE_ADDR bp_addr,
12003 const target_waitstatus &ws)
12004 {
12005 if (ws.kind () != TARGET_WAITKIND_STOPPED
12006 || ws.sig () != GDB_SIGNAL_TRAP)
12007 return 0;
12008
12009 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12010 aspace, bp_addr))
12011 return 0;
12012
12013 if (overlay_debugging /* unmapped overlay section */
12014 && section_is_overlay (bl->section)
12015 && !section_is_mapped (bl->section))
12016 return 0;
12017
12018 return 1;
12019 }
12020
12021 int
12022 dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
12023 const address_space *aspace,
12024 CORE_ADDR bp_addr,
12025 const target_waitstatus &ws)
12026 {
12027 if (dprintf_style == dprintf_style_agent
12028 && target_can_run_breakpoint_commands ())
12029 {
12030 /* An agent-style dprintf never causes a stop. If we see a trap
12031 for this address it must be for a breakpoint that happens to
12032 be set at the same address. */
12033 return 0;
12034 }
12035
12036 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
12037 }
12038
12039 int
12040 ordinary_breakpoint::resources_needed (const struct bp_location *bl)
12041 {
12042 gdb_assert (type == bp_hardware_breakpoint);
12043
12044 return 1;
12045 }
12046
12047 enum print_stop_action
12048 ordinary_breakpoint::print_it (const bpstat *bs) const
12049 {
12050 const struct bp_location *bl;
12051 int bp_temp;
12052 struct ui_out *uiout = current_uiout;
12053
12054 bl = bs->bp_location_at.get ();
12055
12056 bp_temp = disposition == disp_del;
12057 if (bl->address != bl->requested_address)
12058 breakpoint_adjustment_warning (bl->requested_address,
12059 bl->address,
12060 number, true);
12061 annotate_breakpoint (number);
12062 maybe_print_thread_hit_breakpoint (uiout);
12063
12064 if (uiout->is_mi_like_p ())
12065 {
12066 uiout->field_string ("reason",
12067 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12068 uiout->field_string ("disp", bpdisp_text (disposition));
12069 }
12070
12071 if (bp_temp)
12072 uiout->text ("Temporary breakpoint ");
12073 else
12074 uiout->text ("Breakpoint ");
12075 print_num_locno (bs, uiout);
12076 uiout->text (", ");
12077
12078 return PRINT_SRC_AND_LOC;
12079 }
12080
12081 void
12082 ordinary_breakpoint::print_mention () const
12083 {
12084 if (current_uiout->is_mi_like_p ())
12085 return;
12086
12087 switch (type)
12088 {
12089 case bp_breakpoint:
12090 case bp_gnu_ifunc_resolver:
12091 if (disposition == disp_del)
12092 gdb_printf (_("Temporary breakpoint"));
12093 else
12094 gdb_printf (_("Breakpoint"));
12095 gdb_printf (_(" %d"), number);
12096 if (type == bp_gnu_ifunc_resolver)
12097 gdb_printf (_(" at gnu-indirect-function resolver"));
12098 break;
12099 case bp_hardware_breakpoint:
12100 gdb_printf (_("Hardware assisted breakpoint %d"), number);
12101 break;
12102 case bp_dprintf:
12103 gdb_printf (_("Dprintf %d"), number);
12104 break;
12105 }
12106
12107 say_where ();
12108 }
12109
12110 void
12111 ordinary_breakpoint::print_recreate (struct ui_file *fp) const
12112 {
12113 if (type == bp_breakpoint && disposition == disp_del)
12114 gdb_printf (fp, "tbreak");
12115 else if (type == bp_breakpoint)
12116 gdb_printf (fp, "break");
12117 else if (type == bp_hardware_breakpoint
12118 && disposition == disp_del)
12119 gdb_printf (fp, "thbreak");
12120 else if (type == bp_hardware_breakpoint)
12121 gdb_printf (fp, "hbreak");
12122 else
12123 internal_error (_("unhandled breakpoint type %d"), (int) type);
12124
12125 gdb_printf (fp, " %s", locspec->to_string ());
12126
12127 /* Print out extra_string if this breakpoint is pending. It might
12128 contain, for example, conditions that were set by the user. */
12129 if (!this->has_locations () && extra_string != NULL)
12130 gdb_printf (fp, " %s", extra_string.get ());
12131
12132 print_recreate_thread (fp);
12133 }
12134
12135 std::vector<symtab_and_line>
12136 code_breakpoint::decode_location_spec (location_spec *locspec,
12137 program_space *search_pspace)
12138 {
12139 if (locspec->type () == PROBE_LOCATION_SPEC)
12140 return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
12141
12142 struct linespec_result canonical;
12143
12144 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
12145 NULL, 0, &canonical, multiple_symbols_all,
12146 filter.get ());
12147
12148 /* We should get 0 or 1 resulting SALs. */
12149 gdb_assert (canonical.lsals.size () < 2);
12150
12151 if (!canonical.lsals.empty ())
12152 {
12153 const linespec_sals &lsal = canonical.lsals[0];
12154 return std::move (lsal.sals);
12155 }
12156 return {};
12157 }
12158
12159 /* Virtual table for internal breakpoints. */
12160
12161 void
12162 internal_breakpoint::re_set ()
12163 {
12164 switch (type)
12165 {
12166 /* Delete overlay event and longjmp master breakpoints; they
12167 will be reset later by breakpoint_re_set. */
12168 case bp_overlay_event:
12169 case bp_longjmp_master:
12170 case bp_std_terminate_master:
12171 case bp_exception_master:
12172 delete_breakpoint (this);
12173 break;
12174
12175 /* This breakpoint is special, it's set up when the inferior
12176 starts and we really don't want to touch it. */
12177 case bp_shlib_event:
12178
12179 /* Like bp_shlib_event, this breakpoint type is special. Once
12180 it is set up, we do not want to touch it. */
12181 case bp_thread_event:
12182 break;
12183 }
12184 }
12185
12186 void
12187 internal_breakpoint::check_status (bpstat *bs)
12188 {
12189 if (type == bp_shlib_event)
12190 {
12191 /* If requested, stop when the dynamic linker notifies GDB of
12192 events. This allows the user to get control and place
12193 breakpoints in initializer routines for dynamically loaded
12194 objects (among other things). */
12195 bs->stop = stop_on_solib_events != 0;
12196 bs->print = stop_on_solib_events != 0;
12197 }
12198 else
12199 bs->stop = false;
12200 }
12201
12202 enum print_stop_action
12203 internal_breakpoint::print_it (const bpstat *bs) const
12204 {
12205 switch (type)
12206 {
12207 case bp_shlib_event:
12208 /* Did we stop because the user set the stop_on_solib_events
12209 variable? (If so, we report this as a generic, "Stopped due
12210 to shlib event" message.) */
12211 print_solib_event (false);
12212 break;
12213
12214 case bp_thread_event:
12215 /* Not sure how we will get here.
12216 GDB should not stop for these breakpoints. */
12217 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
12218 break;
12219
12220 case bp_overlay_event:
12221 /* By analogy with the thread event, GDB should not stop for these. */
12222 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12223 break;
12224
12225 case bp_longjmp_master:
12226 /* These should never be enabled. */
12227 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12228 break;
12229
12230 case bp_std_terminate_master:
12231 /* These should never be enabled. */
12232 gdb_printf (_("std::terminate Master Breakpoint: "
12233 "gdb should not stop!\n"));
12234 break;
12235
12236 case bp_exception_master:
12237 /* These should never be enabled. */
12238 gdb_printf (_("Exception Master Breakpoint: "
12239 "gdb should not stop!\n"));
12240 break;
12241 }
12242
12243 return PRINT_NOTHING;
12244 }
12245
12246 void
12247 internal_breakpoint::print_mention () const
12248 {
12249 /* Nothing to mention. These breakpoints are internal. */
12250 }
12251
12252 /* Virtual table for momentary breakpoints */
12253
12254 void
12255 momentary_breakpoint::re_set ()
12256 {
12257 /* Keep temporary breakpoints, which can be encountered when we step
12258 over a dlopen call and solib_add is resetting the breakpoints.
12259 Otherwise these should have been blown away via the cleanup chain
12260 or by breakpoint_init_inferior when we rerun the executable. */
12261 }
12262
12263 void
12264 momentary_breakpoint::check_status (bpstat *bs)
12265 {
12266 /* Nothing. The point of these breakpoints is causing a stop. */
12267 }
12268
12269 enum print_stop_action
12270 momentary_breakpoint::print_it (const bpstat *bs) const
12271 {
12272 return PRINT_UNKNOWN;
12273 }
12274
12275 void
12276 momentary_breakpoint::print_mention () const
12277 {
12278 /* Nothing to mention. These breakpoints are internal. */
12279 }
12280
12281 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12282
12283 It gets cleared already on the removal of the first one of such placed
12284 breakpoints. This is OK as they get all removed altogether. */
12285
12286 longjmp_breakpoint::~longjmp_breakpoint ()
12287 {
12288 thread_info *tp = find_thread_global_id (this->thread);
12289
12290 if (tp != NULL)
12291 tp->initiating_frame = null_frame_id;
12292 }
12293
12294 static void
12295 bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
12296 struct linespec_result *canonical)
12297
12298 {
12299 struct linespec_sals lsal;
12300
12301 lsal.sals = parse_probes (locspec, NULL, canonical);
12302 lsal.canonical = xstrdup (canonical->locspec->to_string ());
12303 canonical->lsals.push_back (std::move (lsal));
12304 }
12305
12306 static std::vector<symtab_and_line>
12307 bkpt_probe_decode_location_spec (struct breakpoint *b,
12308 location_spec *locspec,
12309 program_space *search_pspace)
12310 {
12311 std::vector<symtab_and_line> sals
12312 = parse_probes (locspec, search_pspace, NULL);
12313 if (sals.empty ())
12314 error (_("probe not found"));
12315 return sals;
12316 }
12317
12318 int
12319 tracepoint::breakpoint_hit (const struct bp_location *bl,
12320 const address_space *aspace, CORE_ADDR bp_addr,
12321 const target_waitstatus &ws)
12322 {
12323 /* By definition, the inferior does not report stops at
12324 tracepoints. */
12325 return 0;
12326 }
12327
12328 void
12329 tracepoint::print_one_detail (struct ui_out *uiout) const
12330 {
12331 if (!static_trace_marker_id.empty ())
12332 {
12333 gdb_assert (type == bp_static_tracepoint
12334 || type == bp_static_marker_tracepoint);
12335
12336 uiout->message ("\tmarker id is %pF\n",
12337 string_field ("static-tracepoint-marker-string-id",
12338 static_trace_marker_id.c_str ()));
12339 }
12340 }
12341
12342 void
12343 tracepoint::print_mention () const
12344 {
12345 if (current_uiout->is_mi_like_p ())
12346 return;
12347
12348 switch (type)
12349 {
12350 case bp_tracepoint:
12351 gdb_printf (_("Tracepoint"));
12352 gdb_printf (_(" %d"), number);
12353 break;
12354 case bp_fast_tracepoint:
12355 gdb_printf (_("Fast tracepoint"));
12356 gdb_printf (_(" %d"), number);
12357 break;
12358 case bp_static_tracepoint:
12359 case bp_static_marker_tracepoint:
12360 gdb_printf (_("Static tracepoint"));
12361 gdb_printf (_(" %d"), number);
12362 break;
12363 default:
12364 internal_error (_("unhandled tracepoint type %d"), (int) type);
12365 }
12366
12367 say_where ();
12368 }
12369
12370 void
12371 tracepoint::print_recreate (struct ui_file *fp) const
12372 {
12373 if (type == bp_fast_tracepoint)
12374 gdb_printf (fp, "ftrace");
12375 else if (type == bp_static_tracepoint
12376 || type == bp_static_marker_tracepoint)
12377 gdb_printf (fp, "strace");
12378 else if (type == bp_tracepoint)
12379 gdb_printf (fp, "trace");
12380 else
12381 internal_error (_("unhandled tracepoint type %d"), (int) type);
12382
12383 gdb_printf (fp, " %s", locspec->to_string ());
12384 print_recreate_thread (fp);
12385
12386 if (pass_count)
12387 gdb_printf (fp, " passcount %d\n", pass_count);
12388 }
12389
12390 /* Virtual table for tracepoints on static probes. */
12391
12392 static void
12393 tracepoint_probe_create_sals_from_location_spec
12394 (location_spec *locspec,
12395 struct linespec_result *canonical)
12396 {
12397 /* We use the same method for breakpoint on probes. */
12398 bkpt_probe_create_sals_from_location_spec (locspec, canonical);
12399 }
12400
12401 void
12402 dprintf_breakpoint::re_set ()
12403 {
12404 re_set_default ();
12405
12406 /* extra_string should never be non-NULL for dprintf. */
12407 gdb_assert (extra_string != NULL);
12408
12409 /* 1 - connect to target 1, that can run breakpoint commands.
12410 2 - create a dprintf, which resolves fine.
12411 3 - disconnect from target 1
12412 4 - connect to target 2, that can NOT run breakpoint commands.
12413
12414 After steps #3/#4, you'll want the dprintf command list to
12415 be updated, because target 1 and 2 may well return different
12416 answers for target_can_run_breakpoint_commands().
12417 Given absence of finer grained resetting, we get to do
12418 it all the time. */
12419 if (extra_string != NULL)
12420 update_dprintf_command_list (this);
12421 }
12422
12423 /* Implement the "print_recreate" method for dprintf. */
12424
12425 void
12426 dprintf_breakpoint::print_recreate (struct ui_file *fp) const
12427 {
12428 gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
12429 print_recreate_thread (fp);
12430 }
12431
12432 /* Implement the "after_condition_true" method for dprintf.
12433
12434 dprintf's are implemented with regular commands in their command
12435 list, but we run the commands here instead of before presenting the
12436 stop to the user, as dprintf's don't actually cause a stop. This
12437 also makes it so that the commands of multiple dprintfs at the same
12438 address are all handled. */
12439
12440 void
12441 dprintf_breakpoint::after_condition_true (struct bpstat *bs)
12442 {
12443 /* dprintf's never cause a stop. This wasn't set in the
12444 check_status hook instead because that would make the dprintf's
12445 condition not be evaluated. */
12446 bs->stop = false;
12447
12448 /* Run the command list here. Take ownership of it instead of
12449 copying. We never want these commands to run later in
12450 bpstat_do_actions, if a breakpoint that causes a stop happens to
12451 be set at same address as this dprintf, or even if running the
12452 commands here throws. */
12453 counted_command_line cmds = std::move (bs->commands);
12454 gdb_assert (cmds != nullptr);
12455 execute_control_commands (cmds.get (), 0);
12456 }
12457
12458 /* The breakpoint_ops structure to be used on static tracepoints with
12459 markers (`-m'). */
12460
12461 static void
12462 strace_marker_create_sals_from_location_spec (location_spec *locspec,
12463 struct linespec_result *canonical)
12464 {
12465 struct linespec_sals lsal;
12466 const char *arg_start, *arg;
12467
12468 arg = arg_start = as_linespec_location_spec (locspec)->spec_string;
12469 lsal.sals = decode_static_tracepoint_spec (&arg);
12470
12471 std::string str (arg_start, arg - arg_start);
12472 const char *ptr = str.c_str ();
12473 canonical->locspec
12474 = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
12475
12476 lsal.canonical = xstrdup (canonical->locspec->to_string ());
12477 canonical->lsals.push_back (std::move (lsal));
12478 }
12479
12480 static void
12481 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12482 struct linespec_result *canonical,
12483 gdb::unique_xmalloc_ptr<char> cond_string,
12484 gdb::unique_xmalloc_ptr<char> extra_string,
12485 enum bptype type_wanted,
12486 enum bpdisp disposition,
12487 int thread,
12488 int task, int inferior,
12489 int ignore_count,
12490 int from_tty, int enabled,
12491 int internal, unsigned flags)
12492 {
12493 const linespec_sals &lsal = canonical->lsals[0];
12494
12495 /* If the user is creating a static tracepoint by marker id
12496 (strace -m MARKER_ID), then store the sals index, so that
12497 breakpoint_re_set can try to match up which of the newly
12498 found markers corresponds to this one, and, don't try to
12499 expand multiple locations for each sal, given than SALS
12500 already should contain all sals for MARKER_ID. */
12501
12502 for (size_t i = 0; i < lsal.sals.size (); i++)
12503 {
12504 location_spec_up locspec = canonical->locspec->clone ();
12505
12506 std::unique_ptr<tracepoint> tp
12507 (new tracepoint (gdbarch,
12508 type_wanted,
12509 lsal.sals[i],
12510 std::move (locspec),
12511 NULL,
12512 std::move (cond_string),
12513 std::move (extra_string),
12514 disposition,
12515 thread, task, inferior, ignore_count,
12516 from_tty, enabled, flags,
12517 canonical->special_display));
12518
12519 /* Given that its possible to have multiple markers with
12520 the same string id, if the user is creating a static
12521 tracepoint by marker id ("strace -m MARKER_ID"), then
12522 store the sals index, so that breakpoint_re_set can
12523 try to match up which of the newly found markers
12524 corresponds to this one */
12525 tp->static_trace_marker_id_idx = i;
12526
12527 install_breakpoint (internal, std::move (tp), 0);
12528 }
12529 }
12530
12531 std::vector<symtab_and_line>
12532 static_marker_tracepoint::decode_location_spec (location_spec *locspec,
12533 program_space *search_pspace)
12534 {
12535 const char *s = as_linespec_location_spec (locspec)->spec_string;
12536
12537 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12538 if (sals.size () > static_trace_marker_id_idx)
12539 {
12540 sals[0] = sals[static_trace_marker_id_idx];
12541 sals.resize (1);
12542 return sals;
12543 }
12544 else
12545 error (_("marker %s not found"), static_trace_marker_id.c_str ());
12546 }
12547
12548 /* Static tracepoints with marker (`-m'). */
12549 static struct breakpoint_ops strace_marker_breakpoint_ops =
12550 {
12551 strace_marker_create_sals_from_location_spec,
12552 strace_marker_create_breakpoints_sal,
12553 };
12554
12555 static bool
12556 strace_marker_p (struct breakpoint *b)
12557 {
12558 return b->type == bp_static_marker_tracepoint;
12559 }
12560
12561 /* Notify interpreters and observers that breakpoint B was deleted. */
12562
12563 static void
12564 notify_breakpoint_deleted (breakpoint *b)
12565 {
12566 interps_notify_breakpoint_deleted (b);
12567 gdb::observers::breakpoint_deleted.notify (b);
12568 }
12569
12570 /* Delete a breakpoint and clean up all traces of it in the data
12571 structures. */
12572
12573 void
12574 delete_breakpoint (struct breakpoint *bpt)
12575 {
12576 gdb_assert (bpt != NULL);
12577
12578 /* Has this bp already been deleted? This can happen because
12579 multiple lists can hold pointers to bp's. bpstat lists are
12580 especial culprits.
12581
12582 One example of this happening is a watchpoint's scope bp. When
12583 the scope bp triggers, we notice that the watchpoint is out of
12584 scope, and delete it. We also delete its scope bp. But the
12585 scope bp is marked "auto-deleting", and is already on a bpstat.
12586 That bpstat is then checked for auto-deleting bp's, which are
12587 deleted.
12588
12589 A real solution to this problem might involve reference counts in
12590 bp's, and/or giving them pointers back to their referencing
12591 bpstat's, and teaching delete_breakpoint to only free a bp's
12592 storage when no more references were extent. A cheaper bandaid
12593 was chosen. */
12594 if (bpt->type == bp_none)
12595 return;
12596
12597 /* At least avoid this stale reference until the reference counting
12598 of breakpoints gets resolved. */
12599 if (bpt->related_breakpoint != bpt)
12600 {
12601 struct breakpoint *related;
12602 struct watchpoint *w;
12603
12604 if (bpt->type == bp_watchpoint_scope)
12605 w = (struct watchpoint *) bpt->related_breakpoint;
12606 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12607 w = (struct watchpoint *) bpt;
12608 else
12609 w = NULL;
12610 if (w != NULL)
12611 watchpoint_del_at_next_stop (w);
12612
12613 /* Unlink bpt from the bpt->related_breakpoint ring. */
12614 for (related = bpt; related->related_breakpoint != bpt;
12615 related = related->related_breakpoint);
12616 related->related_breakpoint = bpt->related_breakpoint;
12617 bpt->related_breakpoint = bpt;
12618 }
12619
12620 /* watch_command_1 creates a watchpoint but only sets its number if
12621 update_watchpoint succeeds in creating its bp_locations. If there's
12622 a problem in that process, we'll be asked to delete the half-created
12623 watchpoint. In that case, don't announce the deletion. */
12624 if (bpt->number)
12625 notify_breakpoint_deleted (bpt);
12626
12627 breakpoint_chain.erase (breakpoint_chain.iterator_to (*bpt));
12628
12629 /* Be sure no bpstat's are pointing at the breakpoint after it's
12630 been freed. */
12631 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12632 in all threads for now. Note that we cannot just remove bpstats
12633 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12634 commands are associated with the bpstat; if we remove it here,
12635 then the later call to bpstat_do_actions (&stop_bpstat); in
12636 event-top.c won't do anything, and temporary breakpoints with
12637 commands won't work. */
12638
12639 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12640
12641 /* Now that breakpoint is removed from breakpoint list, update the
12642 global location list. This will remove locations that used to
12643 belong to this breakpoint. Do this before freeing the breakpoint
12644 itself, since remove_breakpoint looks at location's owner. It
12645 might be better design to have location completely
12646 self-contained, but it's not the case now.
12647
12648 Clear the location linked list first, otherwise, the intrusive_list
12649 destructor accesses the locations after they are freed. */
12650 bpt->clear_locations ();
12651 update_global_location_list (UGLL_DONT_INSERT);
12652
12653 /* On the chance that someone will soon try again to delete this
12654 same bp, we mark it as deleted before freeing its storage. */
12655 bpt->type = bp_none;
12656 delete bpt;
12657 }
12658
12659 /* Iterator function to call a user-provided callback function once
12660 for each of B and its related breakpoints. */
12661
12662 static void
12663 iterate_over_related_breakpoints (struct breakpoint *b,
12664 gdb::function_view<void (breakpoint *)> function)
12665 {
12666 struct breakpoint *related;
12667
12668 related = b;
12669 do
12670 {
12671 struct breakpoint *next;
12672
12673 /* FUNCTION may delete RELATED. */
12674 next = related->related_breakpoint;
12675
12676 if (next == related)
12677 {
12678 /* RELATED is the last ring entry. */
12679 function (related);
12680
12681 /* FUNCTION may have deleted it, so we'd never reach back to
12682 B. There's nothing left to do anyway, so just break
12683 out. */
12684 break;
12685 }
12686 else
12687 function (related);
12688
12689 related = next;
12690 }
12691 while (related != b);
12692 }
12693
12694 static void
12695 delete_command (const char *arg, int from_tty)
12696 {
12697 dont_repeat ();
12698
12699 if (arg == 0)
12700 {
12701 int breaks_to_delete = 0;
12702
12703 /* Delete all breakpoints if no argument. Do not delete
12704 internal breakpoints, these have to be deleted with an
12705 explicit breakpoint number argument. */
12706 for (breakpoint &b : all_breakpoints ())
12707 if (user_breakpoint_p (&b))
12708 {
12709 breaks_to_delete = 1;
12710 break;
12711 }
12712
12713 /* Ask user only if there are some breakpoints to delete. */
12714 if (!from_tty
12715 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
12716 for (breakpoint &b : all_breakpoints_safe ())
12717 if (user_breakpoint_p (&b))
12718 delete_breakpoint (&b);
12719 }
12720 else
12721 map_breakpoint_numbers
12722 (arg, [&] (breakpoint *br)
12723 {
12724 iterate_over_related_breakpoints (br, delete_breakpoint);
12725 });
12726 }
12727
12728 /* Return true if all locations of B bound to PSPACE are pending. If
12729 PSPACE is NULL, all locations of all program spaces are
12730 considered. */
12731
12732 static bool
12733 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
12734 {
12735 for (bp_location &loc : b->locations ())
12736 if ((pspace == NULL
12737 || loc.pspace == pspace)
12738 && !loc.shlib_disabled
12739 && !loc.pspace->executing_startup)
12740 return false;
12741 return true;
12742 }
12743
12744 /* Subroutine of update_breakpoint_locations to simplify it.
12745 Return true if multiple fns in list LOCS have the same name.
12746 Null names are ignored. */
12747
12748 static bool
12749 ambiguous_names_p (const bp_location_range &locs)
12750 {
12751 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
12752 xcalloc, xfree));
12753
12754 for (const bp_location &l : locs)
12755 {
12756 const char **slot;
12757 const char *name = l.function_name.get ();
12758
12759 /* Allow for some names to be NULL, ignore them. */
12760 if (name == NULL)
12761 continue;
12762
12763 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
12764 INSERT);
12765 /* NOTE: We can assume slot != NULL here because xcalloc never
12766 returns NULL. */
12767 if (*slot != NULL)
12768 return true;
12769 *slot = name;
12770 }
12771
12772 return false;
12773 }
12774
12775 /* When symbols change, it probably means the sources changed as well,
12776 and it might mean the static tracepoint markers are no longer at
12777 the same address or line numbers they used to be at last we
12778 checked. Losing your static tracepoints whenever you rebuild is
12779 undesirable. This function tries to resync/rematch gdb static
12780 tracepoints with the markers on the target, for static tracepoints
12781 that have not been set by marker id. Static tracepoint that have
12782 been set by marker id are reset by marker id in breakpoint_re_set.
12783 The heuristic is:
12784
12785 1) For a tracepoint set at a specific address, look for a marker at
12786 the old PC. If one is found there, assume to be the same marker.
12787 If the name / string id of the marker found is different from the
12788 previous known name, assume that means the user renamed the marker
12789 in the sources, and output a warning.
12790
12791 2) For a tracepoint set at a given line number, look for a marker
12792 at the new address of the old line number. If one is found there,
12793 assume to be the same marker. If the name / string id of the
12794 marker found is different from the previous known name, assume that
12795 means the user renamed the marker in the sources, and output a
12796 warning.
12797
12798 3) If a marker is no longer found at the same address or line, it
12799 may mean the marker no longer exists. But it may also just mean
12800 the code changed a bit. Maybe the user added a few lines of code
12801 that made the marker move up or down (in line number terms). Ask
12802 the target for info about the marker with the string id as we knew
12803 it. If found, update line number and address in the matching
12804 static tracepoint. This will get confused if there's more than one
12805 marker with the same ID (possible in UST, although unadvised
12806 precisely because it confuses tools). */
12807
12808 static struct symtab_and_line
12809 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12810 {
12811 struct tracepoint *tp = (struct tracepoint *) b;
12812 struct static_tracepoint_marker marker;
12813 CORE_ADDR pc;
12814
12815 pc = sal.pc;
12816 if (sal.line)
12817 find_line_pc (sal.symtab, sal.line, &pc);
12818
12819 if (target_static_tracepoint_marker_at (pc, &marker))
12820 {
12821 if (tp->static_trace_marker_id != marker.str_id)
12822 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12823 b->number, tp->static_trace_marker_id.c_str (),
12824 marker.str_id.c_str ());
12825
12826 tp->static_trace_marker_id = std::move (marker.str_id);
12827
12828 return sal;
12829 }
12830
12831 /* Old marker wasn't found on target at lineno. Try looking it up
12832 by string ID. */
12833 if (!sal.explicit_pc
12834 && sal.line != 0
12835 && sal.symtab != NULL
12836 && !tp->static_trace_marker_id.empty ())
12837 {
12838 std::vector<static_tracepoint_marker> markers
12839 = target_static_tracepoint_markers_by_strid
12840 (tp->static_trace_marker_id.c_str ());
12841
12842 if (!markers.empty ())
12843 {
12844 struct symbol *sym;
12845 struct static_tracepoint_marker *tpmarker;
12846 struct ui_out *uiout = current_uiout;
12847
12848 tpmarker = &markers[0];
12849
12850 tp->static_trace_marker_id = std::move (tpmarker->str_id);
12851
12852 warning (_("marker for static tracepoint %d (%s) not "
12853 "found at previous line number"),
12854 b->number, tp->static_trace_marker_id.c_str ());
12855
12856 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
12857 sym = find_pc_sect_function (tpmarker->address, NULL);
12858 uiout->text ("Now in ");
12859 if (sym)
12860 {
12861 uiout->field_string ("func", sym->print_name (),
12862 function_name_style.style ());
12863 uiout->text (" at ");
12864 }
12865 uiout->field_string ("file",
12866 symtab_to_filename_for_display (sal2.symtab),
12867 file_name_style.style ());
12868 uiout->text (":");
12869
12870 if (uiout->is_mi_like_p ())
12871 {
12872 const char *fullname = symtab_to_fullname (sal2.symtab);
12873
12874 uiout->field_string ("fullname", fullname);
12875 }
12876
12877 uiout->field_signed ("line", sal2.line);
12878 uiout->text ("\n");
12879
12880 b->first_loc ().line_number = sal2.line;
12881 b->first_loc ().symtab = sym != NULL ? sal2.symtab : NULL;
12882
12883 std::unique_ptr<explicit_location_spec> els
12884 (new explicit_location_spec ());
12885 els->source_filename
12886 = xstrdup (symtab_to_filename_for_display (sal2.symtab));
12887 els->line_offset.offset = b->first_loc ().line_number;
12888 els->line_offset.sign = LINE_OFFSET_NONE;
12889
12890 b->locspec = std::move (els);
12891
12892 /* Might be nice to check if function changed, and warn if
12893 so. */
12894 }
12895 }
12896 return sal;
12897 }
12898
12899 /* Returns true iff location lists A and B are sufficiently same that
12900 we don't need to report breakpoint as changed. */
12901
12902 static bool
12903 locations_are_equal (const bp_location_list &a, const bp_location_range &b)
12904 {
12905 auto a_iter = a.begin ();
12906 auto b_iter = b.begin ();
12907
12908 for (; a_iter != a.end () && b_iter != b.end (); ++a_iter, ++b_iter)
12909 {
12910 if (a_iter->address != b_iter->address)
12911 return false;
12912
12913 if (a_iter->shlib_disabled != b_iter->shlib_disabled)
12914 return false;
12915
12916 if (a_iter->enabled != b_iter->enabled)
12917 return false;
12918
12919 if (a_iter->disabled_by_cond != b_iter->disabled_by_cond)
12920 return false;
12921 }
12922
12923 return (a_iter == a.end ()) == (b_iter == b.end ());
12924 }
12925
12926 /* See breakpoint.h. */
12927
12928 bp_location_list
12929 breakpoint::steal_locations (program_space *pspace)
12930 {
12931 if (pspace == NULL)
12932 return std::move (m_locations);
12933
12934 bp_location_list ret;
12935
12936 for (auto it = m_locations.begin (); it != m_locations.end (); )
12937 {
12938 if (it->pspace == pspace)
12939 {
12940 bp_location &loc = *it;
12941 it = m_locations.erase (it);
12942 ret.push_back (loc);
12943 }
12944 else
12945 ++it;
12946 }
12947
12948 return ret;
12949 }
12950
12951 /* Create new breakpoint locations for B (a hardware or software
12952 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12953 zero, then B is a ranged breakpoint. Only recreates locations for
12954 FILTER_PSPACE. Locations of other program spaces are left
12955 untouched. */
12956
12957 void
12958 update_breakpoint_locations (code_breakpoint *b,
12959 struct program_space *filter_pspace,
12960 gdb::array_view<const symtab_and_line> sals,
12961 gdb::array_view<const symtab_and_line> sals_end)
12962 {
12963 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
12964 {
12965 /* Ranged breakpoints have only one start location and one end
12966 location. */
12967 b->enable_state = bp_disabled;
12968 gdb_printf (gdb_stderr,
12969 _("Could not reset ranged breakpoint %d: "
12970 "multiple locations found\n"),
12971 b->number);
12972 return;
12973 }
12974
12975 /* If there's no new locations, and all existing locations are
12976 pending, don't do anything. This optimizes the common case where
12977 all locations are in the same shared library, that was unloaded.
12978 We'd like to retain the location, so that when the library is
12979 loaded again, we don't loose the enabled/disabled status of the
12980 individual locations. */
12981 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
12982 return;
12983
12984 bp_location_list existing_locations = b->steal_locations (filter_pspace);
12985
12986 for (const auto &sal : sals)
12987 {
12988 struct bp_location *new_loc;
12989
12990 switch_to_program_space_and_thread (sal.pspace);
12991
12992 new_loc = b->add_location (sal);
12993
12994 /* Reparse conditions, they might contain references to the
12995 old symtab. */
12996 if (b->cond_string != NULL)
12997 {
12998 const char *s;
12999
13000 s = b->cond_string.get ();
13001 try
13002 {
13003 new_loc->cond = parse_exp_1 (&s, sal.pc,
13004 block_for_pc (sal.pc),
13005 0);
13006 }
13007 catch (const gdb_exception_error &e)
13008 {
13009 new_loc->disabled_by_cond = true;
13010 }
13011 }
13012
13013 if (!sals_end.empty ())
13014 {
13015 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
13016
13017 new_loc->length = end - sals[0].pc + 1;
13018 }
13019 }
13020
13021 /* If possible, carry over 'disable' status from existing
13022 breakpoints. */
13023 {
13024 /* If there are multiple breakpoints with the same function name,
13025 e.g. for inline functions, comparing function names won't work.
13026 Instead compare pc addresses; this is just a heuristic as things
13027 may have moved, but in practice it gives the correct answer
13028 often enough until a better solution is found. */
13029 int have_ambiguous_names = ambiguous_names_p (b->locations ());
13030
13031 for (const bp_location &e : existing_locations)
13032 {
13033 if ((!e.enabled || e.disabled_by_cond) && e.function_name)
13034 {
13035 if (have_ambiguous_names)
13036 {
13037 for (bp_location &l : b->locations ())
13038 {
13039 /* Ignore software vs hardware location type at
13040 this point, because with "set breakpoint
13041 auto-hw", after a re-set, locations that were
13042 hardware can end up as software, or vice versa.
13043 As mentioned above, this is an heuristic and in
13044 practice should give the correct answer often
13045 enough. */
13046 if (breakpoint_locations_match (&e, &l, true))
13047 {
13048 l.enabled = e.enabled;
13049 l.disabled_by_cond = e.disabled_by_cond;
13050 break;
13051 }
13052 }
13053 }
13054 else
13055 {
13056 for (bp_location &l : b->locations ())
13057 if (l.function_name
13058 && strcmp (e.function_name.get (),
13059 l.function_name.get ()) == 0)
13060 {
13061 l.enabled = e.enabled;
13062 l.disabled_by_cond = e.disabled_by_cond;
13063 break;
13064 }
13065 }
13066 }
13067 }
13068 }
13069
13070 if (!locations_are_equal (existing_locations, b->locations ()))
13071 notify_breakpoint_modified (b);
13072 }
13073
13074 /* Find the SaL locations corresponding to the given LOCSPEC.
13075 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13076
13077 std::vector<symtab_and_line>
13078 code_breakpoint::location_spec_to_sals (location_spec *locspec,
13079 struct program_space *search_pspace,
13080 int *found)
13081 {
13082 struct gdb_exception exception;
13083
13084 std::vector<symtab_and_line> sals;
13085
13086 try
13087 {
13088 sals = decode_location_spec (locspec, search_pspace);
13089 }
13090 catch (gdb_exception_error &e)
13091 {
13092 int not_found_and_ok = false;
13093
13094 /* For pending breakpoints, it's expected that parsing will
13095 fail until the right shared library is loaded. User has
13096 already told to create pending breakpoints and don't need
13097 extra messages. If breakpoint is in bp_shlib_disabled
13098 state, then user already saw the message about that
13099 breakpoint being disabled, and don't want to see more
13100 errors. */
13101 if (e.error == NOT_FOUND_ERROR
13102 && (condition_not_parsed
13103 || (this->has_locations ()
13104 && search_pspace != NULL
13105 && this->first_loc ().pspace != search_pspace)
13106 || (this->has_locations () && this->first_loc ().shlib_disabled)
13107 || (this->has_locations ()
13108 && this->first_loc ().pspace->executing_startup)
13109 || enable_state == bp_disabled))
13110 not_found_and_ok = true;
13111
13112 if (!not_found_and_ok)
13113 {
13114 /* We surely don't want to warn about the same breakpoint
13115 10 times. One solution, implemented here, is disable
13116 the breakpoint on error. Another solution would be to
13117 have separate 'warning emitted' flag. Since this
13118 happens only when a binary has changed, I don't know
13119 which approach is better. */
13120 enable_state = bp_disabled;
13121 throw;
13122 }
13123
13124 exception = std::move (e);
13125 }
13126
13127 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13128 {
13129 for (auto &sal : sals)
13130 resolve_sal_pc (&sal);
13131 if (condition_not_parsed && extra_string != NULL)
13132 {
13133 gdb::unique_xmalloc_ptr<char> local_cond, local_extra;
13134 int local_thread, local_task, local_inferior;
13135
13136 find_condition_and_thread_for_sals (sals, extra_string.get (),
13137 &local_cond, &local_thread,
13138 &local_inferior,
13139 &local_task, &local_extra);
13140 gdb_assert (cond_string == nullptr);
13141 if (local_cond != nullptr)
13142 cond_string = std::move (local_cond);
13143 thread = local_thread;
13144 task = local_task;
13145 if (local_extra != nullptr)
13146 extra_string = std::move (local_extra);
13147 condition_not_parsed = 0;
13148 }
13149
13150 if (type == bp_static_tracepoint)
13151 sals[0] = update_static_tracepoint (this, sals[0]);
13152
13153 *found = 1;
13154 }
13155 else
13156 *found = 0;
13157
13158 return sals;
13159 }
13160
13161 /* The default re_set method, for typical hardware or software
13162 breakpoints. Reevaluate the breakpoint and recreate its
13163 locations. */
13164
13165 void
13166 code_breakpoint::re_set_default ()
13167 {
13168 struct program_space *filter_pspace = current_program_space;
13169 std::vector<symtab_and_line> expanded, expanded_end;
13170
13171 int found;
13172 std::vector<symtab_and_line> sals = location_spec_to_sals (locspec.get (),
13173 filter_pspace,
13174 &found);
13175 if (found)
13176 expanded = std::move (sals);
13177
13178 if (locspec_range_end != nullptr)
13179 {
13180 std::vector<symtab_and_line> sals_end
13181 = location_spec_to_sals (locspec_range_end.get (),
13182 filter_pspace, &found);
13183 if (found)
13184 expanded_end = std::move (sals_end);
13185 }
13186
13187 update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
13188 }
13189
13190 /* Default method for creating SALs from an address string. It basically
13191 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13192
13193 static void
13194 create_sals_from_location_spec_default (location_spec *locspec,
13195 struct linespec_result *canonical)
13196 {
13197 parse_breakpoint_sals (locspec, canonical);
13198 }
13199
13200 /* Reset a breakpoint. */
13201
13202 static void
13203 breakpoint_re_set_one (breakpoint *b)
13204 {
13205 input_radix = b->input_radix;
13206 set_language (b->language);
13207
13208 b->re_set ();
13209 }
13210
13211 /* Re-set breakpoint locations for the current program space.
13212 Locations bound to other program spaces are left untouched. */
13213
13214 void
13215 breakpoint_re_set (void)
13216 {
13217 {
13218 scoped_restore_current_language save_language;
13219 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13220 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13221
13222 /* breakpoint_re_set_one sets the current_language to the language
13223 of the breakpoint it is resetting (see prepare_re_set_context)
13224 before re-evaluating the breakpoint's location. This change can
13225 unfortunately get undone by accident if the language_mode is set
13226 to auto, and we either switch frames, or more likely in this context,
13227 we select the current frame.
13228
13229 We prevent this by temporarily turning the language_mode to
13230 language_mode_manual. We restore it once all breakpoints
13231 have been reset. */
13232 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13233 language_mode = language_mode_manual;
13234
13235 /* Note: we must not try to insert locations until after all
13236 breakpoints have been re-set. Otherwise, e.g., when re-setting
13237 breakpoint 1, we'd insert the locations of breakpoint 2, which
13238 hadn't been re-set yet, and thus may have stale locations. */
13239
13240 for (breakpoint &b : all_breakpoints_safe ())
13241 {
13242 try
13243 {
13244 breakpoint_re_set_one (&b);
13245 }
13246 catch (const gdb_exception &ex)
13247 {
13248 exception_fprintf (gdb_stderr, ex,
13249 "Error in re-setting breakpoint %d: ",
13250 b.number);
13251 }
13252 }
13253
13254 jit_breakpoint_re_set ();
13255 }
13256
13257 create_overlay_event_breakpoint ();
13258 create_longjmp_master_breakpoint ();
13259 create_std_terminate_master_breakpoint ();
13260 create_exception_master_breakpoint ();
13261
13262 /* Now we can insert. */
13263 update_global_location_list (UGLL_MAY_INSERT);
13264 }
13265 \f
13266 /* Reset the thread number of this breakpoint:
13267
13268 - If the breakpoint is for all threads, leave it as-is.
13269 - Else, reset it to the current thread for inferior_ptid. */
13270 void
13271 breakpoint_re_set_thread (struct breakpoint *b)
13272 {
13273 if (b->thread != -1)
13274 {
13275 b->thread = inferior_thread ()->global_num;
13276
13277 /* We're being called after following a fork. The new fork is
13278 selected as current, and unless this was a vfork will have a
13279 different program space from the original thread. Reset that
13280 as well. */
13281 b->first_loc ().pspace = current_program_space;
13282 }
13283 }
13284
13285 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13286 If from_tty is nonzero, it prints a message to that effect,
13287 which ends with a period (no newline). */
13288
13289 void
13290 set_ignore_count (int bptnum, int count, int from_tty)
13291 {
13292 if (count < 0)
13293 count = 0;
13294
13295 for (breakpoint &b : all_breakpoints ())
13296 if (b.number == bptnum)
13297 {
13298 if (is_tracepoint (&b))
13299 {
13300 if (from_tty && count != 0)
13301 gdb_printf (_("Ignore count ignored for tracepoint %d."),
13302 bptnum);
13303 return;
13304 }
13305
13306 b.ignore_count = count;
13307 if (from_tty)
13308 {
13309 if (count == 0)
13310 gdb_printf (_("Will stop next time "
13311 "breakpoint %d is reached."),
13312 bptnum);
13313 else if (count == 1)
13314 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
13315 bptnum);
13316 else
13317 gdb_printf (_("Will ignore next %d "
13318 "crossings of breakpoint %d."),
13319 count, bptnum);
13320 }
13321 notify_breakpoint_modified (&b);
13322 return;
13323 }
13324
13325 error (_("No breakpoint number %d."), bptnum);
13326 }
13327
13328 /* Command to set ignore-count of breakpoint N to COUNT. */
13329
13330 static void
13331 ignore_command (const char *args, int from_tty)
13332 {
13333 const char *p = args;
13334 int num;
13335
13336 if (p == 0)
13337 error_no_arg (_("a breakpoint number"));
13338
13339 num = get_number (&p);
13340 if (num == 0)
13341 error (_("bad breakpoint number: '%s'"), args);
13342 if (*p == 0)
13343 error (_("Second argument (specified ignore-count) is missing."));
13344
13345 set_ignore_count (num,
13346 longest_to_int (value_as_long (parse_and_eval (p))),
13347 from_tty);
13348 if (from_tty)
13349 gdb_printf ("\n");
13350 }
13351 \f
13352
13353 /* Call FUNCTION on each of the breakpoints with numbers in the range
13354 defined by BP_NUM_RANGE (an inclusive range). */
13355
13356 static void
13357 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13358 gdb::function_view<void (breakpoint *)> function)
13359 {
13360 if (bp_num_range.first == 0)
13361 {
13362 warning (_("bad breakpoint number at or near '%d'"),
13363 bp_num_range.first);
13364 }
13365 else
13366 {
13367 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13368 {
13369 bool match = false;
13370
13371 for (breakpoint &b : all_breakpoints_safe ())
13372 if (b.number == i)
13373 {
13374 match = true;
13375 function (&b);
13376 break;
13377 }
13378 if (!match)
13379 gdb_printf (_("No breakpoint number %d.\n"), i);
13380 }
13381 }
13382 }
13383
13384 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13385 ARGS. */
13386
13387 static void
13388 map_breakpoint_numbers (const char *args,
13389 gdb::function_view<void (breakpoint *)> function)
13390 {
13391 if (args == NULL || *args == '\0')
13392 error_no_arg (_("one or more breakpoint numbers"));
13393
13394 number_or_range_parser parser (args);
13395
13396 while (!parser.finished ())
13397 {
13398 int num = parser.get_number ();
13399 map_breakpoint_number_range (std::make_pair (num, num), function);
13400 }
13401 }
13402
13403 /* Return the breakpoint location structure corresponding to the
13404 BP_NUM and LOC_NUM values. */
13405
13406 static struct bp_location *
13407 find_location_by_number (int bp_num, int loc_num)
13408 {
13409 breakpoint *b = get_breakpoint (bp_num);
13410
13411 if (!b || b->number != bp_num)
13412 error (_("Bad breakpoint number '%d'"), bp_num);
13413
13414 if (loc_num == 0)
13415 error (_("Bad breakpoint location number '%d'"), loc_num);
13416
13417 int n = 0;
13418 for (bp_location &loc : b->locations ())
13419 if (++n == loc_num)
13420 return &loc;
13421
13422 error (_("Bad breakpoint location number '%d'"), loc_num);
13423 }
13424
13425 /* Modes of operation for extract_bp_num. */
13426 enum class extract_bp_kind
13427 {
13428 /* Extracting a breakpoint number. */
13429 bp,
13430
13431 /* Extracting a location number. */
13432 loc,
13433 };
13434
13435 /* Extract a breakpoint or location number (as determined by KIND)
13436 from the string starting at START. TRAILER is a character which
13437 can be found after the number. If you don't want a trailer, use
13438 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13439 string. This always returns a positive integer. */
13440
13441 static int
13442 extract_bp_num (extract_bp_kind kind, const char *start,
13443 int trailer, const char **end_out = NULL)
13444 {
13445 const char *end = start;
13446 int num = get_number_trailer (&end, trailer);
13447 if (num < 0)
13448 error (kind == extract_bp_kind::bp
13449 ? _("Negative breakpoint number '%.*s'")
13450 : _("Negative breakpoint location number '%.*s'"),
13451 int (end - start), start);
13452 if (num == 0)
13453 error (kind == extract_bp_kind::bp
13454 ? _("Bad breakpoint number '%.*s'")
13455 : _("Bad breakpoint location number '%.*s'"),
13456 int (end - start), start);
13457
13458 if (end_out != NULL)
13459 *end_out = end;
13460 return num;
13461 }
13462
13463 /* Extract a breakpoint or location range (as determined by KIND) in
13464 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13465 representing the (inclusive) range. The returned pair's elements
13466 are always positive integers. */
13467
13468 static std::pair<int, int>
13469 extract_bp_or_bp_range (extract_bp_kind kind,
13470 const std::string &arg,
13471 std::string::size_type arg_offset)
13472 {
13473 std::pair<int, int> range;
13474 const char *bp_loc = &arg[arg_offset];
13475 std::string::size_type dash = arg.find ('-', arg_offset);
13476 if (dash != std::string::npos)
13477 {
13478 /* bp_loc is a range (x-z). */
13479 if (arg.length () == dash + 1)
13480 error (kind == extract_bp_kind::bp
13481 ? _("Bad breakpoint number at or near: '%s'")
13482 : _("Bad breakpoint location number at or near: '%s'"),
13483 bp_loc);
13484
13485 const char *end;
13486 const char *start_first = bp_loc;
13487 const char *start_second = &arg[dash + 1];
13488 range.first = extract_bp_num (kind, start_first, '-');
13489 range.second = extract_bp_num (kind, start_second, '\0', &end);
13490
13491 if (range.first > range.second)
13492 error (kind == extract_bp_kind::bp
13493 ? _("Inverted breakpoint range at '%.*s'")
13494 : _("Inverted breakpoint location range at '%.*s'"),
13495 int (end - start_first), start_first);
13496 }
13497 else
13498 {
13499 /* bp_loc is a single value. */
13500 range.first = extract_bp_num (kind, bp_loc, '\0');
13501 range.second = range.first;
13502 }
13503 return range;
13504 }
13505
13506 /* Extract the breakpoint/location range specified by ARG. Returns
13507 the breakpoint range in BP_NUM_RANGE, and the location range in
13508 BP_LOC_RANGE.
13509
13510 ARG may be in any of the following forms:
13511
13512 x where 'x' is a breakpoint number.
13513 x-y where 'x' and 'y' specify a breakpoint numbers range.
13514 x.y where 'x' is a breakpoint number and 'y' a location number.
13515 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13516 location number range.
13517 */
13518
13519 static void
13520 extract_bp_number_and_location (const std::string &arg,
13521 std::pair<int, int> &bp_num_range,
13522 std::pair<int, int> &bp_loc_range)
13523 {
13524 std::string::size_type dot = arg.find ('.');
13525
13526 if (dot != std::string::npos)
13527 {
13528 /* Handle 'x.y' and 'x.y-z' cases. */
13529
13530 if (arg.length () == dot + 1 || dot == 0)
13531 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
13532
13533 bp_num_range.first
13534 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13535 bp_num_range.second = bp_num_range.first;
13536
13537 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13538 arg, dot + 1);
13539 }
13540 else
13541 {
13542 /* Handle x and x-y cases. */
13543
13544 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
13545 bp_loc_range.first = 0;
13546 bp_loc_range.second = 0;
13547 }
13548 }
13549
13550 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13551 specifies whether to enable or disable. */
13552
13553 static void
13554 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13555 {
13556 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13557 if (loc != NULL)
13558 {
13559 if (loc->disabled_by_cond && enable)
13560 error (_("Breakpoint %d's condition is invalid at location %d, "
13561 "cannot enable."), bp_num, loc_num);
13562
13563 if (loc->enabled != enable)
13564 {
13565 loc->enabled = enable;
13566 mark_breakpoint_location_modified (loc);
13567 }
13568 if (target_supports_enable_disable_tracepoint ()
13569 && current_trace_status ()->running && loc->owner
13570 && is_tracepoint (loc->owner))
13571 target_disable_tracepoint (loc);
13572 }
13573 update_global_location_list (UGLL_DONT_INSERT);
13574
13575 notify_breakpoint_modified (loc->owner);
13576 }
13577
13578 /* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
13579 owner. 1-based indexing. -1 signals NOT FOUND. */
13580
13581 static int
13582 find_loc_num_by_location (const bp_location *loc)
13583 {
13584 if (loc != nullptr && loc->owner != nullptr)
13585 {
13586 /* Locations use 1-based indexing. */
13587 int loc_num = 1;
13588 for (bp_location &it : loc->owner->locations ())
13589 {
13590 if (&it == loc)
13591 return loc_num;
13592 loc_num++;
13593 }
13594 }
13595 return -1;
13596 }
13597
13598 /* Enable or disable a breakpoint location LOC. ENABLE
13599 specifies whether to enable or disable. */
13600
13601 void
13602 enable_disable_bp_location (bp_location *loc, bool enable)
13603 {
13604 if (loc == nullptr)
13605 error (_("Breakpoint location is invalid."));
13606
13607 if (loc->owner == nullptr)
13608 error (_("Breakpoint location does not have an owner breakpoint."));
13609
13610 if (loc->disabled_by_cond && enable)
13611 {
13612 int loc_num = find_loc_num_by_location (loc);
13613 if (loc_num == -1)
13614 error (_("Breakpoint location LOC_NUM could not be found."));
13615 else
13616 error (_("Breakpoint %d's condition is invalid at location %d, "
13617 "cannot enable."), loc->owner->number, loc_num);
13618 }
13619
13620 if (loc->enabled != enable)
13621 {
13622 loc->enabled = enable;
13623 mark_breakpoint_location_modified (loc);
13624 }
13625
13626 if (target_supports_enable_disable_tracepoint ()
13627 && current_trace_status ()->running && loc->owner
13628 && is_tracepoint (loc->owner))
13629 target_disable_tracepoint (loc);
13630
13631 update_global_location_list (UGLL_DONT_INSERT);
13632 notify_breakpoint_modified (loc->owner);
13633 }
13634
13635 /* Enable or disable a range of breakpoint locations. BP_NUM is the
13636 number of the breakpoint, and BP_LOC_RANGE specifies the
13637 (inclusive) range of location numbers of that breakpoint to
13638 enable/disable. ENABLE specifies whether to enable or disable the
13639 location. */
13640
13641 static void
13642 enable_disable_breakpoint_location_range (int bp_num,
13643 std::pair<int, int> &bp_loc_range,
13644 bool enable)
13645 {
13646 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13647 enable_disable_bp_num_loc (bp_num, i, enable);
13648 }
13649
13650 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13651 If from_tty is nonzero, it prints a message to that effect,
13652 which ends with a period (no newline). */
13653
13654 void
13655 disable_breakpoint (struct breakpoint *bpt)
13656 {
13657 /* Never disable a watchpoint scope breakpoint; we want to
13658 hit them when we leave scope so we can delete both the
13659 watchpoint and its scope breakpoint at that time. */
13660 if (bpt->type == bp_watchpoint_scope)
13661 return;
13662
13663 bpt->enable_state = bp_disabled;
13664
13665 /* Mark breakpoint locations modified. */
13666 mark_breakpoint_modified (bpt);
13667
13668 if (target_supports_enable_disable_tracepoint ()
13669 && current_trace_status ()->running && is_tracepoint (bpt))
13670 {
13671 for (bp_location &location : bpt->locations ())
13672 target_disable_tracepoint (&location);
13673 }
13674
13675 update_global_location_list (UGLL_DONT_INSERT);
13676
13677 notify_breakpoint_modified (bpt);
13678 }
13679
13680 /* Enable or disable the breakpoint(s) or breakpoint location(s)
13681 specified in ARGS. ARGS may be in any of the formats handled by
13682 extract_bp_number_and_location. ENABLE specifies whether to enable
13683 or disable the breakpoints/locations. */
13684
13685 static void
13686 enable_disable_command (const char *args, int from_tty, bool enable)
13687 {
13688 if (args == 0)
13689 {
13690 for (breakpoint &bpt : all_breakpoints ())
13691 if (user_breakpoint_p (&bpt))
13692 {
13693 if (enable)
13694 enable_breakpoint (&bpt);
13695 else
13696 disable_breakpoint (&bpt);
13697 }
13698 }
13699 else
13700 {
13701 std::string num = extract_arg (&args);
13702
13703 while (!num.empty ())
13704 {
13705 std::pair<int, int> bp_num_range, bp_loc_range;
13706
13707 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13708
13709 if (bp_loc_range.first == bp_loc_range.second
13710 && (bp_loc_range.first == 0
13711 || (bp_loc_range.first == 1
13712 && bp_num_range.first == bp_num_range.second
13713 && !has_multiple_locations (bp_num_range.first))))
13714 {
13715 /* Handle breakpoint ids with formats 'x' or 'x-z'
13716 or 'y.1' where y has only one code location. */
13717 map_breakpoint_number_range (bp_num_range,
13718 enable
13719 ? enable_breakpoint
13720 : disable_breakpoint);
13721 }
13722 else
13723 {
13724 /* Handle breakpoint ids with formats 'x.y' or
13725 'x.y-z'. */
13726 enable_disable_breakpoint_location_range
13727 (bp_num_range.first, bp_loc_range, enable);
13728 }
13729 num = extract_arg (&args);
13730 }
13731 }
13732 }
13733
13734 /* The disable command disables the specified breakpoints/locations
13735 (or all defined breakpoints) so they're no longer effective in
13736 stopping the inferior. ARGS may be in any of the forms defined in
13737 extract_bp_number_and_location. */
13738
13739 static void
13740 disable_command (const char *args, int from_tty)
13741 {
13742 enable_disable_command (args, from_tty, false);
13743 }
13744
13745 static void
13746 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13747 int count)
13748 {
13749 int target_resources_ok;
13750
13751 if (bpt->type == bp_hardware_breakpoint)
13752 {
13753 int i;
13754 i = hw_breakpoint_used_count ();
13755 target_resources_ok =
13756 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13757 i + 1, 0);
13758 if (target_resources_ok == 0)
13759 error (_("No hardware breakpoint support in the target."));
13760 else if (target_resources_ok < 0)
13761 error (_("Hardware breakpoints used exceeds limit."));
13762 }
13763
13764 if (is_watchpoint (bpt))
13765 {
13766 /* Initialize it just to avoid a GCC false warning. */
13767 enum enable_state orig_enable_state = bp_disabled;
13768
13769 try
13770 {
13771 struct watchpoint *w = (struct watchpoint *) bpt;
13772
13773 orig_enable_state = bpt->enable_state;
13774 bpt->enable_state = bp_enabled;
13775 update_watchpoint (w, true /* reparse */);
13776 }
13777 catch (const gdb_exception_error &e)
13778 {
13779 bpt->enable_state = orig_enable_state;
13780 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13781 bpt->number);
13782 return;
13783 }
13784 }
13785
13786 bpt->enable_state = bp_enabled;
13787
13788 /* Mark breakpoint locations modified. */
13789 mark_breakpoint_modified (bpt);
13790
13791 if (target_supports_enable_disable_tracepoint ()
13792 && current_trace_status ()->running && is_tracepoint (bpt))
13793 {
13794 for (bp_location &location : bpt->locations ())
13795 target_enable_tracepoint (&location);
13796 }
13797
13798 bpt->disposition = disposition;
13799 bpt->enable_count = count;
13800 update_global_location_list (UGLL_MAY_INSERT);
13801
13802 notify_breakpoint_modified (bpt);
13803 }
13804
13805
13806 void
13807 enable_breakpoint (struct breakpoint *bpt)
13808 {
13809 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13810 }
13811
13812 /* The enable command enables the specified breakpoints/locations (or
13813 all defined breakpoints) so they once again become (or continue to
13814 be) effective in stopping the inferior. ARGS may be in any of the
13815 forms defined in extract_bp_number_and_location. */
13816
13817 static void
13818 enable_command (const char *args, int from_tty)
13819 {
13820 enable_disable_command (args, from_tty, true);
13821 }
13822
13823 static void
13824 enable_once_command (const char *args, int from_tty)
13825 {
13826 map_breakpoint_numbers
13827 (args, [&] (breakpoint *b)
13828 {
13829 iterate_over_related_breakpoints
13830 (b, [&] (breakpoint *bpt)
13831 {
13832 enable_breakpoint_disp (bpt, disp_disable, 1);
13833 });
13834 });
13835 }
13836
13837 static void
13838 enable_count_command (const char *args, int from_tty)
13839 {
13840 int count;
13841
13842 if (args == NULL)
13843 error_no_arg (_("hit count"));
13844
13845 count = get_number (&args);
13846
13847 map_breakpoint_numbers
13848 (args, [&] (breakpoint *b)
13849 {
13850 iterate_over_related_breakpoints
13851 (b, [&] (breakpoint *bpt)
13852 {
13853 enable_breakpoint_disp (bpt, disp_disable, count);
13854 });
13855 });
13856 }
13857
13858 static void
13859 enable_delete_command (const char *args, int from_tty)
13860 {
13861 map_breakpoint_numbers
13862 (args, [&] (breakpoint *b)
13863 {
13864 iterate_over_related_breakpoints
13865 (b, [&] (breakpoint *bpt)
13866 {
13867 enable_breakpoint_disp (bpt, disp_del, 1);
13868 });
13869 });
13870 }
13871 \f
13872 /* Invalidate last known value of any hardware watchpoint if
13873 the memory which that value represents has been written to by
13874 GDB itself. */
13875
13876 static void
13877 invalidate_bp_value_on_memory_change (struct inferior *inferior,
13878 CORE_ADDR addr, ssize_t len,
13879 const bfd_byte *data)
13880 {
13881 for (breakpoint &bp : all_breakpoints ())
13882 if (bp.enable_state == bp_enabled
13883 && bp.type == bp_hardware_watchpoint)
13884 {
13885 watchpoint &wp = gdb::checked_static_cast<watchpoint &> (bp);
13886
13887 if (wp.val_valid && wp.val != nullptr)
13888 {
13889 for (bp_location &loc : bp.locations ())
13890 if (loc.loc_type == bp_loc_hardware_watchpoint
13891 && loc.address + loc.length > addr
13892 && addr + len > loc.address)
13893 {
13894 wp.val = NULL;
13895 wp.val_valid = false;
13896 }
13897 }
13898 }
13899 }
13900
13901 /* Create and insert a breakpoint for software single step. */
13902
13903 void
13904 insert_single_step_breakpoint (struct gdbarch *gdbarch,
13905 const address_space *aspace,
13906 CORE_ADDR next_pc)
13907 {
13908 struct thread_info *tp = inferior_thread ();
13909 struct symtab_and_line sal;
13910 CORE_ADDR pc = next_pc;
13911
13912 if (tp->control.single_step_breakpoints == NULL)
13913 {
13914 std::unique_ptr<breakpoint> b
13915 (new momentary_breakpoint (gdbarch, bp_single_step,
13916 current_program_space,
13917 null_frame_id,
13918 tp->global_num));
13919
13920 tp->control.single_step_breakpoints
13921 = add_to_breakpoint_chain (std::move (b));
13922 }
13923
13924 sal = find_pc_line (pc, 0);
13925 sal.pc = pc;
13926 sal.section = find_pc_overlay (pc);
13927 sal.explicit_pc = 1;
13928
13929 auto *ss_bp
13930 = (gdb::checked_static_cast<momentary_breakpoint *>
13931 (tp->control.single_step_breakpoints));
13932 ss_bp->add_location (sal);
13933
13934 update_global_location_list (UGLL_INSERT);
13935 }
13936
13937 /* Insert single step breakpoints according to the current state. */
13938
13939 int
13940 insert_single_step_breakpoints (struct gdbarch *gdbarch)
13941 {
13942 struct regcache *regcache = get_current_regcache ();
13943 std::vector<CORE_ADDR> next_pcs;
13944
13945 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
13946
13947 if (!next_pcs.empty ())
13948 {
13949 frame_info_ptr frame = get_current_frame ();
13950 const address_space *aspace = get_frame_address_space (frame);
13951
13952 for (CORE_ADDR pc : next_pcs)
13953 insert_single_step_breakpoint (gdbarch, aspace, pc);
13954
13955 return 1;
13956 }
13957 else
13958 return 0;
13959 }
13960
13961 /* See breakpoint.h. */
13962
13963 int
13964 breakpoint_has_location_inserted_here (struct breakpoint *bp,
13965 const address_space *aspace,
13966 CORE_ADDR pc)
13967 {
13968 for (bp_location &loc : bp->locations ())
13969 if (loc.inserted
13970 && breakpoint_location_address_match (&loc, aspace, pc))
13971 return 1;
13972
13973 return 0;
13974 }
13975
13976 /* Check whether a software single-step breakpoint is inserted at
13977 PC. */
13978
13979 int
13980 single_step_breakpoint_inserted_here_p (const address_space *aspace,
13981 CORE_ADDR pc)
13982 {
13983 for (breakpoint &bpt : all_breakpoints ())
13984 {
13985 if (bpt.type == bp_single_step
13986 && breakpoint_has_location_inserted_here (&bpt, aspace, pc))
13987 return 1;
13988 }
13989 return 0;
13990 }
13991
13992 /* Tracepoint-specific operations. */
13993
13994 /* Set tracepoint count to NUM. */
13995 static void
13996 set_tracepoint_count (int num)
13997 {
13998 tracepoint_count = num;
13999 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14000 }
14001
14002 static void
14003 trace_command (const char *arg, int from_tty)
14004 {
14005 location_spec_up locspec = string_to_location_spec (&arg,
14006 current_language);
14007 const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
14008 (locspec.get (), true /* is_tracepoint */);
14009
14010 create_breakpoint (get_current_arch (),
14011 locspec.get (),
14012 NULL, -1, -1, arg, false, 1 /* parse arg */,
14013 0 /* tempflag */,
14014 bp_tracepoint /* type_wanted */,
14015 0 /* Ignore count */,
14016 pending_break_support,
14017 ops,
14018 from_tty,
14019 1 /* enabled */,
14020 0 /* internal */, 0);
14021 }
14022
14023 static void
14024 ftrace_command (const char *arg, int from_tty)
14025 {
14026 location_spec_up locspec = string_to_location_spec (&arg,
14027 current_language);
14028 create_breakpoint (get_current_arch (),
14029 locspec.get (),
14030 NULL, -1, -1, arg, false, 1 /* parse arg */,
14031 0 /* tempflag */,
14032 bp_fast_tracepoint /* type_wanted */,
14033 0 /* Ignore count */,
14034 pending_break_support,
14035 &code_breakpoint_ops,
14036 from_tty,
14037 1 /* enabled */,
14038 0 /* internal */, 0);
14039 }
14040
14041 /* strace command implementation. Creates a static tracepoint. */
14042
14043 static void
14044 strace_command (const char *arg, int from_tty)
14045 {
14046 const struct breakpoint_ops *ops;
14047 location_spec_up locspec;
14048 enum bptype type;
14049
14050 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14051 or with a normal static tracepoint. */
14052 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14053 {
14054 ops = &strace_marker_breakpoint_ops;
14055 locspec = new_linespec_location_spec (&arg,
14056 symbol_name_match_type::FULL);
14057 type = bp_static_marker_tracepoint;
14058 }
14059 else
14060 {
14061 ops = &code_breakpoint_ops;
14062 locspec = string_to_location_spec (&arg, current_language);
14063 type = bp_static_tracepoint;
14064 }
14065
14066 create_breakpoint (get_current_arch (),
14067 locspec.get (),
14068 NULL, -1, -1, arg, false, 1 /* parse arg */,
14069 0 /* tempflag */,
14070 type /* type_wanted */,
14071 0 /* Ignore count */,
14072 pending_break_support,
14073 ops,
14074 from_tty,
14075 1 /* enabled */,
14076 0 /* internal */, 0);
14077 }
14078
14079 /* Set up a fake reader function that gets command lines from a linked
14080 list that was acquired during tracepoint uploading. */
14081
14082 static struct uploaded_tp *this_utp;
14083 static int next_cmd;
14084
14085 static const char *
14086 read_uploaded_action (std::string &buffer)
14087 {
14088 char *rslt = nullptr;
14089
14090 if (next_cmd < this_utp->cmd_strings.size ())
14091 {
14092 rslt = this_utp->cmd_strings[next_cmd].get ();
14093 next_cmd++;
14094 }
14095
14096 return rslt;
14097 }
14098
14099 /* Given information about a tracepoint as recorded on a target (which
14100 can be either a live system or a trace file), attempt to create an
14101 equivalent GDB tracepoint. This is not a reliable process, since
14102 the target does not necessarily have all the information used when
14103 the tracepoint was originally defined. */
14104
14105 struct tracepoint *
14106 create_tracepoint_from_upload (struct uploaded_tp *utp)
14107 {
14108 const char *addr_str;
14109 char small_buf[100];
14110 struct tracepoint *tp;
14111
14112 if (utp->at_string)
14113 addr_str = utp->at_string.get ();
14114 else
14115 {
14116 /* In the absence of a source location, fall back to raw
14117 address. Since there is no way to confirm that the address
14118 means the same thing as when the trace was started, warn the
14119 user. */
14120 warning (_("Uploaded tracepoint %d has no "
14121 "source location, using raw address"),
14122 utp->number);
14123 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14124 addr_str = small_buf;
14125 }
14126
14127 /* There's not much we can do with a sequence of bytecodes. */
14128 if (utp->cond && !utp->cond_string)
14129 warning (_("Uploaded tracepoint %d condition "
14130 "has no source form, ignoring it"),
14131 utp->number);
14132
14133 location_spec_up locspec = string_to_location_spec (&addr_str,
14134 current_language);
14135 if (!create_breakpoint (get_current_arch (),
14136 locspec.get (),
14137 utp->cond_string.get (), -1, -1, addr_str,
14138 false /* force_condition */,
14139 0 /* parse cond/thread */,
14140 0 /* tempflag */,
14141 utp->type /* type_wanted */,
14142 0 /* Ignore count */,
14143 pending_break_support,
14144 &code_breakpoint_ops,
14145 0 /* from_tty */,
14146 utp->enabled /* enabled */,
14147 0 /* internal */,
14148 CREATE_BREAKPOINT_FLAGS_INSERTED))
14149 return NULL;
14150
14151 /* Get the tracepoint we just created. */
14152 tp = get_tracepoint (tracepoint_count);
14153 gdb_assert (tp != NULL);
14154
14155 if (utp->pass > 0)
14156 {
14157 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14158 tp->number);
14159
14160 trace_pass_command (small_buf, 0);
14161 }
14162
14163 /* If we have uploaded versions of the original commands, set up a
14164 special-purpose "reader" function and call the usual command line
14165 reader, then pass the result to the breakpoint command-setting
14166 function. */
14167 if (!utp->cmd_strings.empty ())
14168 {
14169 counted_command_line cmd_list;
14170
14171 this_utp = utp;
14172 next_cmd = 0;
14173
14174 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14175
14176 breakpoint_set_commands (tp, std::move (cmd_list));
14177 }
14178 else if (!utp->actions.empty ()
14179 || !utp->step_actions.empty ())
14180 warning (_("Uploaded tracepoint %d actions "
14181 "have no source form, ignoring them"),
14182 utp->number);
14183
14184 /* Copy any status information that might be available. */
14185 tp->hit_count = utp->hit_count;
14186 tp->traceframe_usage = utp->traceframe_usage;
14187
14188 return tp;
14189 }
14190
14191 /* Print information on tracepoint number TPNUM_EXP, or all if
14192 omitted. */
14193
14194 static void
14195 info_tracepoints_command (const char *args, int from_tty)
14196 {
14197 struct ui_out *uiout = current_uiout;
14198 int num_printed;
14199
14200 num_printed = breakpoint_1 (args, false, is_tracepoint);
14201
14202 if (num_printed == 0)
14203 {
14204 if (args == NULL || *args == '\0')
14205 uiout->message ("No tracepoints.\n");
14206 else
14207 uiout->message ("No tracepoint matching '%s'.\n", args);
14208 }
14209
14210 default_collect_info ();
14211 }
14212
14213 /* The 'enable trace' command enables tracepoints.
14214 Not supported by all targets. */
14215 static void
14216 enable_trace_command (const char *args, int from_tty)
14217 {
14218 enable_command (args, from_tty);
14219 }
14220
14221 /* The 'disable trace' command disables tracepoints.
14222 Not supported by all targets. */
14223 static void
14224 disable_trace_command (const char *args, int from_tty)
14225 {
14226 disable_command (args, from_tty);
14227 }
14228
14229 /* Remove a tracepoint (or all if no argument). */
14230 static void
14231 delete_trace_command (const char *arg, int from_tty)
14232 {
14233 dont_repeat ();
14234
14235 if (arg == 0)
14236 {
14237 int breaks_to_delete = 0;
14238
14239 /* Delete all breakpoints if no argument.
14240 Do not delete internal or call-dummy breakpoints, these
14241 have to be deleted with an explicit breakpoint number
14242 argument. */
14243 for (breakpoint &tp : all_tracepoints ())
14244 if (is_tracepoint (&tp) && user_breakpoint_p (&tp))
14245 {
14246 breaks_to_delete = 1;
14247 break;
14248 }
14249
14250 /* Ask user only if there are some breakpoints to delete. */
14251 if (!from_tty
14252 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14253 {
14254 for (breakpoint &b : all_breakpoints_safe ())
14255 if (is_tracepoint (&b) && user_breakpoint_p (&b))
14256 delete_breakpoint (&b);
14257 }
14258 }
14259 else
14260 map_breakpoint_numbers
14261 (arg, [&] (breakpoint *br)
14262 {
14263 iterate_over_related_breakpoints (br, delete_breakpoint);
14264 });
14265 }
14266
14267 /* Helper function for trace_pass_command. */
14268
14269 static void
14270 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14271 {
14272 tp->pass_count = count;
14273 notify_breakpoint_modified (tp);
14274 if (from_tty)
14275 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
14276 tp->number, count);
14277 }
14278
14279 /* Set passcount for tracepoint.
14280
14281 First command argument is passcount, second is tracepoint number.
14282 If tracepoint number omitted, apply to most recently defined.
14283 Also accepts special argument "all". */
14284
14285 static void
14286 trace_pass_command (const char *args, int from_tty)
14287 {
14288 ULONGEST count;
14289
14290 if (args == 0 || *args == 0)
14291 error (_("passcount command requires an "
14292 "argument (count + optional TP num)"));
14293
14294 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14295
14296 args = skip_spaces (args);
14297 if (*args && strncasecmp (args, "all", 3) == 0)
14298 {
14299 args += 3; /* Skip special argument "all". */
14300 if (*args)
14301 error (_("Junk at end of arguments."));
14302
14303 for (breakpoint &b : all_tracepoints ())
14304 {
14305 tracepoint &t1 = gdb::checked_static_cast<tracepoint &> (b);
14306 trace_pass_set_count (&t1, count, from_tty);
14307 }
14308 }
14309 else if (*args == '\0')
14310 {
14311 tracepoint *t1 = get_tracepoint_by_number (&args, NULL);
14312 if (t1)
14313 trace_pass_set_count (t1, count, from_tty);
14314 }
14315 else
14316 {
14317 number_or_range_parser parser (args);
14318 while (!parser.finished ())
14319 {
14320 tracepoint *t1 = get_tracepoint_by_number (&args, &parser);
14321 if (t1)
14322 trace_pass_set_count (t1, count, from_tty);
14323 }
14324 }
14325 }
14326
14327 struct tracepoint *
14328 get_tracepoint (int num)
14329 {
14330 for (breakpoint &t : all_tracepoints ())
14331 if (t.number == num)
14332 return gdb::checked_static_cast<tracepoint *> (&t);
14333
14334 return NULL;
14335 }
14336
14337 /* Find the tracepoint with the given target-side number (which may be
14338 different from the tracepoint number after disconnecting and
14339 reconnecting). */
14340
14341 struct tracepoint *
14342 get_tracepoint_by_number_on_target (int num)
14343 {
14344 for (breakpoint &b : all_tracepoints ())
14345 {
14346 tracepoint &t = gdb::checked_static_cast<tracepoint &> (b);
14347
14348 if (t.number_on_target == num)
14349 return &t;
14350 }
14351
14352 return NULL;
14353 }
14354
14355 /* Utility: parse a tracepoint number and look it up in the list.
14356 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14357 If the argument is missing, the most recent tracepoint
14358 (tracepoint_count) is returned. */
14359
14360 struct tracepoint *
14361 get_tracepoint_by_number (const char **arg,
14362 number_or_range_parser *parser)
14363 {
14364 int tpnum;
14365 const char *instring = arg == NULL ? NULL : *arg;
14366
14367 if (parser != NULL)
14368 {
14369 gdb_assert (!parser->finished ());
14370 tpnum = parser->get_number ();
14371 }
14372 else if (arg == NULL || *arg == NULL || ! **arg)
14373 tpnum = tracepoint_count;
14374 else
14375 tpnum = get_number (arg);
14376
14377 if (tpnum <= 0)
14378 {
14379 if (instring && *instring)
14380 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14381 instring);
14382 else
14383 gdb_printf (_("No previous tracepoint\n"));
14384 return NULL;
14385 }
14386
14387 for (breakpoint &t : all_tracepoints ())
14388 if (t.number == tpnum)
14389 return gdb::checked_static_cast<tracepoint *> (&t);
14390
14391 gdb_printf ("No tracepoint number %d.\n", tpnum);
14392 return NULL;
14393 }
14394
14395 void
14396 breakpoint::print_recreate_thread (struct ui_file *fp) const
14397 {
14398 if (thread != -1)
14399 {
14400 struct thread_info *thr = find_thread_global_id (thread);
14401 gdb_printf (fp, " thread %s", print_full_thread_id (thr));
14402 }
14403
14404 if (task != -1)
14405 gdb_printf (fp, " task %d", task);
14406
14407 gdb_printf (fp, "\n");
14408 }
14409
14410 /* Save information on user settable breakpoints (watchpoints, etc) to
14411 a new script file named FILENAME. If FILTER is non-NULL, call it
14412 on each breakpoint and only include the ones for which it returns
14413 true. */
14414
14415 static void
14416 save_breakpoints (const char *filename, int from_tty,
14417 bool (*filter) (const struct breakpoint *))
14418 {
14419 bool any = false;
14420 int extra_trace_bits = 0;
14421
14422 if (filename == 0 || *filename == 0)
14423 error (_("Argument required (file name in which to save)"));
14424
14425 /* See if we have anything to save. */
14426 for (breakpoint &tp : all_breakpoints ())
14427 {
14428 /* Skip internal and momentary breakpoints. */
14429 if (!user_breakpoint_p (&tp))
14430 continue;
14431
14432 /* If we have a filter, only save the breakpoints it accepts. */
14433 if (filter && !filter (&tp))
14434 continue;
14435
14436 any = true;
14437
14438 if (is_tracepoint (&tp))
14439 {
14440 extra_trace_bits = 1;
14441
14442 /* We can stop searching. */
14443 break;
14444 }
14445 }
14446
14447 if (!any)
14448 {
14449 warning (_("Nothing to save."));
14450 return;
14451 }
14452
14453 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14454
14455 stdio_file fp;
14456
14457 if (!fp.open (expanded_filename.get (), "w"))
14458 error (_("Unable to open file '%s' for saving (%s)"),
14459 expanded_filename.get (), safe_strerror (errno));
14460
14461 if (extra_trace_bits)
14462 save_trace_state_variables (&fp);
14463
14464 for (breakpoint &tp : all_breakpoints ())
14465 {
14466 /* Skip internal and momentary breakpoints. */
14467 if (!user_breakpoint_p (&tp))
14468 continue;
14469
14470 /* If we have a filter, only save the breakpoints it accepts. */
14471 if (filter && !filter (&tp))
14472 continue;
14473
14474 tp.print_recreate (&fp);
14475
14476 /* Note, we can't rely on tp->number for anything, as we can't
14477 assume the recreated breakpoint numbers will match. Use $bpnum
14478 instead. */
14479
14480 if (tp.cond_string)
14481 fp.printf (" condition $bpnum %s\n", tp.cond_string.get ());
14482
14483 if (tp.ignore_count)
14484 fp.printf (" ignore $bpnum %d\n", tp.ignore_count);
14485
14486 if (tp.type != bp_dprintf && tp.commands)
14487 {
14488 fp.puts (" commands\n");
14489
14490 ui_out_redirect_pop redir (current_uiout, &fp);
14491 print_command_lines (current_uiout, tp.commands.get (), 2);
14492
14493 fp.puts (" end\n");
14494 }
14495
14496 if (tp.enable_state == bp_disabled)
14497 fp.puts ("disable $bpnum\n");
14498
14499 /* If this is a multi-location breakpoint, check if the locations
14500 should be individually disabled. Watchpoint locations are
14501 special, and not user visible. */
14502 if (!is_watchpoint (&tp) && tp.has_multiple_locations ())
14503 {
14504 int n = 1;
14505
14506 for (bp_location &loc : tp.locations ())
14507 {
14508 if (!loc.enabled)
14509 fp.printf ("disable $bpnum.%d\n", n);
14510
14511 n++;
14512 }
14513 }
14514 }
14515
14516 if (extra_trace_bits && !default_collect.empty ())
14517 fp.printf ("set default-collect %s\n", default_collect.c_str ());
14518
14519 if (from_tty)
14520 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
14521 }
14522
14523 /* The `save breakpoints' command. */
14524
14525 static void
14526 save_breakpoints_command (const char *args, int from_tty)
14527 {
14528 save_breakpoints (args, from_tty, NULL);
14529 }
14530
14531 /* The `save tracepoints' command. */
14532
14533 static void
14534 save_tracepoints_command (const char *args, int from_tty)
14535 {
14536 save_breakpoints (args, from_tty, is_tracepoint);
14537 }
14538
14539 \f
14540 /* This help string is used to consolidate all the help string for specifying
14541 locations used by several commands. */
14542
14543 #define LOCATION_SPEC_HELP_STRING \
14544 "Linespecs are colon-separated lists of location parameters, such as\n\
14545 source filename, function name, label name, and line number.\n\
14546 Example: To specify the start of a label named \"the_top\" in the\n\
14547 function \"fact\" in the file \"factorial.c\", use\n\
14548 \"factorial.c:fact:the_top\".\n\
14549 \n\
14550 Address locations begin with \"*\" and specify an exact address in the\n\
14551 program. Example: To specify the fourth byte past the start function\n\
14552 \"main\", use \"*main + 4\".\n\
14553 \n\
14554 Explicit locations are similar to linespecs but use an option/argument\n\
14555 syntax to specify location parameters.\n\
14556 Example: To specify the start of the label named \"the_top\" in the\n\
14557 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
14558 -function fact -label the_top\".\n\
14559 \n\
14560 By default, a specified function is matched against the program's\n\
14561 functions in all scopes. For C++, this means in all namespaces and\n\
14562 classes. For Ada, this means in all packages. E.g., in C++,\n\
14563 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14564 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
14565 specified name as a complete fully-qualified name instead."
14566
14567 /* This help string is used for the break, hbreak, tbreak and thbreak
14568 commands. It is defined as a macro to prevent duplication.
14569 COMMAND should be a string constant containing the name of the
14570 command. */
14571
14572 #define BREAK_ARGS_HELP(command) \
14573 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14574 \t[-force-condition] [if CONDITION]\n\
14575 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14576 probe point. Accepted values are `-probe' (for a generic, automatically\n\
14577 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14578 `-probe-dtrace' (for a DTrace probe).\n\
14579 LOCATION may be a linespec, address, or explicit location as described\n\
14580 below.\n\
14581 \n\
14582 With no LOCATION, uses current execution address of the selected\n\
14583 stack frame. This is useful for breaking on return to a stack frame.\n\
14584 \n\
14585 THREADNUM is the number from \"info threads\".\n\
14586 CONDITION is a boolean expression.\n\
14587 \n\
14588 With the \"-force-condition\" flag, the condition is defined even when\n\
14589 it is invalid for all current locations.\n\
14590 \n" LOCATION_SPEC_HELP_STRING "\n\n\
14591 Multiple breakpoints at one place are permitted, and useful if their\n\
14592 conditions are different.\n\
14593 \n\
14594 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14595
14596 /* List of subcommands for "catch". */
14597 static struct cmd_list_element *catch_cmdlist;
14598
14599 /* List of subcommands for "tcatch". */
14600 static struct cmd_list_element *tcatch_cmdlist;
14601
14602 void
14603 add_catch_command (const char *name, const char *docstring,
14604 cmd_func_ftype *func,
14605 completer_ftype *completer,
14606 void *user_data_catch,
14607 void *user_data_tcatch)
14608 {
14609 struct cmd_list_element *command;
14610
14611 command = add_cmd (name, class_breakpoint, docstring,
14612 &catch_cmdlist);
14613 command->func = func;
14614 command->set_context (user_data_catch);
14615 set_cmd_completer (command, completer);
14616
14617 command = add_cmd (name, class_breakpoint, docstring,
14618 &tcatch_cmdlist);
14619 command->func = func;
14620 command->set_context (user_data_tcatch);
14621 set_cmd_completer (command, completer);
14622 }
14623
14624 /* False if any of the breakpoint's locations could be a location where
14625 functions have been inlined, true otherwise. */
14626
14627 static bool
14628 is_non_inline_function (struct breakpoint *b)
14629 {
14630 /* The shared library event breakpoint is set on the address of a
14631 non-inline function. */
14632 return (b->type == bp_shlib_event);
14633 }
14634
14635 /* Nonzero if the specified PC cannot be a location where functions
14636 have been inlined. */
14637
14638 int
14639 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
14640 const target_waitstatus &ws)
14641 {
14642 for (breakpoint &b : all_breakpoints ())
14643 {
14644 if (!is_non_inline_function (&b))
14645 continue;
14646
14647 for (bp_location &bl : b.locations ())
14648 {
14649 if (!bl.shlib_disabled
14650 && bpstat_check_location (&bl, aspace, pc, ws))
14651 return 1;
14652 }
14653 }
14654
14655 return 0;
14656 }
14657
14658 /* Remove any references to OBJFILE which is going to be freed. */
14659
14660 void
14661 breakpoint_free_objfile (struct objfile *objfile)
14662 {
14663 for (bp_location *loc : all_bp_locations ())
14664 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
14665 loc->symtab = NULL;
14666 }
14667
14668 /* Chain containing all defined "enable breakpoint" subcommands. */
14669
14670 static struct cmd_list_element *enablebreaklist = NULL;
14671
14672 /* See breakpoint.h. */
14673
14674 cmd_list_element *commands_cmd_element = nullptr;
14675
14676 void _initialize_breakpoint ();
14677 void
14678 _initialize_breakpoint ()
14679 {
14680 struct cmd_list_element *c;
14681
14682 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14683 "breakpoint");
14684 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14685 "breakpoint");
14686 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14687 "breakpoint");
14688
14689 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14690 before a breakpoint is set. */
14691 breakpoint_count = 0;
14692
14693 tracepoint_count = 0;
14694
14695 add_com ("ignore", class_breakpoint, ignore_command, _("\
14696 Set ignore-count of breakpoint number N to COUNT.\n\
14697 Usage is `ignore N COUNT'."));
14698
14699 commands_cmd_element = add_com ("commands", class_breakpoint,
14700 commands_command, _("\
14701 Set commands to be executed when the given breakpoints are hit.\n\
14702 Give a space-separated breakpoint list as argument after \"commands\".\n\
14703 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14704 (e.g. `5-7').\n\
14705 With no argument, the targeted breakpoint is the last one set.\n\
14706 The commands themselves follow starting on the next line.\n\
14707 Type a line containing \"end\" to indicate the end of them.\n\
14708 Give \"silent\" as the first line to make the breakpoint silent;\n\
14709 then no output is printed when it is hit, except what the commands print."));
14710
14711 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14712 static std::string condition_command_help
14713 = gdb::option::build_help (_("\
14714 Specify breakpoint number N to break only if COND is true.\n\
14715 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
14716 is an expression to be evaluated whenever breakpoint N is reached.\n\
14717 \n\
14718 Options:\n\
14719 %OPTIONS%"), cc_opts);
14720
14721 c = add_com ("condition", class_breakpoint, condition_command,
14722 condition_command_help.c_str ());
14723 set_cmd_completer_handle_brkchars (c, condition_completer);
14724
14725 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
14726 Set a temporary breakpoint.\n\
14727 Like \"break\" except the breakpoint is only temporary,\n\
14728 so it will be deleted when hit. Equivalent to \"break\" followed\n\
14729 by using \"enable delete\" on the breakpoint number.\n\
14730 \n"
14731 BREAK_ARGS_HELP ("tbreak")));
14732 set_cmd_completer (c, location_completer);
14733
14734 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
14735 Set a hardware assisted breakpoint.\n\
14736 Like \"break\" except the breakpoint requires hardware support,\n\
14737 some target hardware may not have this support.\n\
14738 \n"
14739 BREAK_ARGS_HELP ("hbreak")));
14740 set_cmd_completer (c, location_completer);
14741
14742 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
14743 Set a temporary hardware assisted breakpoint.\n\
14744 Like \"hbreak\" except the breakpoint is only temporary,\n\
14745 so it will be deleted when hit.\n\
14746 \n"
14747 BREAK_ARGS_HELP ("thbreak")));
14748 set_cmd_completer (c, location_completer);
14749
14750 cmd_list_element *enable_cmd
14751 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14752 Enable all or some breakpoints.\n\
14753 Usage: enable [BREAKPOINTNUM]...\n\
14754 Give breakpoint numbers (separated by spaces) as arguments.\n\
14755 With no subcommand, breakpoints are enabled until you command otherwise.\n\
14756 This is used to cancel the effect of the \"disable\" command.\n\
14757 With a subcommand you can enable temporarily."),
14758 &enablelist, 1, &cmdlist);
14759
14760 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
14761
14762 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
14763 Enable all or some breakpoints.\n\
14764 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
14765 Give breakpoint numbers (separated by spaces) as arguments.\n\
14766 This is used to cancel the effect of the \"disable\" command.\n\
14767 May be abbreviated to simply \"enable\"."),
14768 &enablebreaklist, 1, &enablelist);
14769
14770 add_cmd ("once", no_class, enable_once_command, _("\
14771 Enable some breakpoints for one hit.\n\
14772 Usage: enable breakpoints once BREAKPOINTNUM...\n\
14773 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14774 &enablebreaklist);
14775
14776 add_cmd ("delete", no_class, enable_delete_command, _("\
14777 Enable some breakpoints and delete when hit.\n\
14778 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
14779 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14780 &enablebreaklist);
14781
14782 add_cmd ("count", no_class, enable_count_command, _("\
14783 Enable some breakpoints for COUNT hits.\n\
14784 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
14785 If a breakpoint is hit while enabled in this fashion,\n\
14786 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14787 &enablebreaklist);
14788
14789 add_cmd ("delete", no_class, enable_delete_command, _("\
14790 Enable some breakpoints and delete when hit.\n\
14791 Usage: enable delete BREAKPOINTNUM...\n\
14792 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14793 &enablelist);
14794
14795 add_cmd ("once", no_class, enable_once_command, _("\
14796 Enable some breakpoints for one hit.\n\
14797 Usage: enable once BREAKPOINTNUM...\n\
14798 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14799 &enablelist);
14800
14801 add_cmd ("count", no_class, enable_count_command, _("\
14802 Enable some breakpoints for COUNT hits.\n\
14803 Usage: enable count COUNT BREAKPOINTNUM...\n\
14804 If a breakpoint is hit while enabled in this fashion,\n\
14805 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14806 &enablelist);
14807
14808 cmd_list_element *disable_cmd
14809 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14810 Disable all or some breakpoints.\n\
14811 Usage: disable [BREAKPOINTNUM]...\n\
14812 Arguments are breakpoint numbers with spaces in between.\n\
14813 To disable all breakpoints, give no argument.\n\
14814 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
14815 &disablelist, 1, &cmdlist);
14816 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14817 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
14818
14819 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
14820 Disable all or some breakpoints.\n\
14821 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
14822 Arguments are breakpoint numbers with spaces in between.\n\
14823 To disable all breakpoints, give no argument.\n\
14824 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
14825 This command may be abbreviated \"disable\"."),
14826 &disablelist);
14827
14828 cmd_list_element *delete_cmd
14829 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14830 Delete all or some breakpoints.\n\
14831 Usage: delete [BREAKPOINTNUM]...\n\
14832 Arguments are breakpoint numbers with spaces in between.\n\
14833 To delete all breakpoints, give no argument.\n\
14834 \n\
14835 Also a prefix command for deletion of other GDB objects."),
14836 &deletelist, 1, &cmdlist);
14837 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14838 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
14839
14840 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
14841 Delete all or some breakpoints or auto-display expressions.\n\
14842 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
14843 Arguments are breakpoint numbers with spaces in between.\n\
14844 To delete all breakpoints, give no argument.\n\
14845 This command may be abbreviated \"delete\"."),
14846 &deletelist);
14847
14848 cmd_list_element *clear_cmd
14849 = add_com ("clear", class_breakpoint, clear_command, _("\
14850 Clear breakpoint at specified location.\n\
14851 Argument may be a linespec, explicit, or address location as described below.\n\
14852 \n\
14853 With no argument, clears all breakpoints in the line that the selected frame\n\
14854 is executing in.\n"
14855 "\n" LOCATION_SPEC_HELP_STRING "\n\n\
14856 See also the \"delete\" command which clears breakpoints by number."));
14857 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
14858
14859 cmd_list_element *break_cmd
14860 = add_com ("break", class_breakpoint, break_command, _("\
14861 Set breakpoint at specified location.\n"
14862 BREAK_ARGS_HELP ("break")));
14863 set_cmd_completer (break_cmd, location_completer);
14864
14865 add_com_alias ("b", break_cmd, class_run, 1);
14866 add_com_alias ("br", break_cmd, class_run, 1);
14867 add_com_alias ("bre", break_cmd, class_run, 1);
14868 add_com_alias ("brea", break_cmd, class_run, 1);
14869
14870 cmd_list_element *info_breakpoints_cmd
14871 = add_info ("breakpoints", info_breakpoints_command, _("\
14872 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
14873 The \"Type\" column indicates one of:\n\
14874 \tbreakpoint - normal breakpoint\n\
14875 \twatchpoint - watchpoint\n\
14876 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14877 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14878 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14879 address and file/line number respectively.\n\
14880 \n\
14881 Convenience variable \"$_\" and default examine address for \"x\"\n\
14882 are set to the address of the last breakpoint listed unless the command\n\
14883 is prefixed with \"server \".\n\n\
14884 Convenience variable \"$bpnum\" contains the number of the last\n\
14885 breakpoint set."));
14886
14887 add_info_alias ("b", info_breakpoints_cmd, 1);
14888
14889 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14890 Status of all breakpoints, or breakpoint number NUMBER.\n\
14891 The \"Type\" column indicates one of:\n\
14892 \tbreakpoint - normal breakpoint\n\
14893 \twatchpoint - watchpoint\n\
14894 \tlongjmp - internal breakpoint used to step through longjmp()\n\
14895 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14896 \tuntil - internal breakpoint used by the \"until\" command\n\
14897 \tfinish - internal breakpoint used by the \"finish\" command\n\
14898 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14899 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14900 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14901 address and file/line number respectively.\n\
14902 \n\
14903 Convenience variable \"$_\" and default examine address for \"x\"\n\
14904 are set to the address of the last breakpoint listed unless the command\n\
14905 is prefixed with \"server \".\n\n\
14906 Convenience variable \"$bpnum\" contains the number of the last\n\
14907 breakpoint set."),
14908 &maintenanceinfolist);
14909
14910 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
14911 Set catchpoints to catch events."),
14912 &catch_cmdlist,
14913 0/*allow-unknown*/, &cmdlist);
14914
14915 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
14916 Set temporary catchpoints to catch events."),
14917 &tcatch_cmdlist,
14918 0/*allow-unknown*/, &cmdlist);
14919
14920 const auto opts = make_watch_options_def_group (nullptr);
14921
14922 static const std::string watch_help = gdb::option::build_help (_("\
14923 Set a watchpoint for EXPRESSION.\n\
14924 Usage: watch [-location] EXPRESSION\n\
14925 \n\
14926 Options:\n\
14927 %OPTIONS%\n\
14928 \n\
14929 A watchpoint stops execution of your program whenever the value of\n\
14930 an expression changes."), opts);
14931 c = add_com ("watch", class_breakpoint, watch_command,
14932 watch_help.c_str ());
14933 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14934
14935 static const std::string rwatch_help = gdb::option::build_help (_("\
14936 Set a read watchpoint for EXPRESSION.\n\
14937 Usage: rwatch [-location] EXPRESSION\n\
14938 \n\
14939 Options:\n\
14940 %OPTIONS%\n\
14941 \n\
14942 A read watchpoint stops execution of your program whenever the value of\n\
14943 an expression is read."), opts);
14944 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14945 rwatch_help.c_str ());
14946 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14947
14948 static const std::string awatch_help = gdb::option::build_help (_("\
14949 Set an access watchpoint for EXPRESSION.\n\
14950 Usage: awatch [-location] EXPRESSION\n\
14951 \n\
14952 Options:\n\
14953 %OPTIONS%\n\
14954 \n\
14955 An access watchpoint stops execution of your program whenever the value\n\
14956 of an expression is either read or written."), opts);
14957 c = add_com ("awatch", class_breakpoint, awatch_command,
14958 awatch_help.c_str ());
14959 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14960
14961 add_info ("watchpoints", info_watchpoints_command, _("\
14962 Status of specified watchpoints (all watchpoints if no argument)."));
14963
14964 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14965 respond to changes - contrary to the description. */
14966 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14967 &can_use_hw_watchpoints, _("\
14968 Set debugger's willingness to use watchpoint hardware."), _("\
14969 Show debugger's willingness to use watchpoint hardware."), _("\
14970 If zero, gdb will not use hardware for new watchpoints, even if\n\
14971 such is available. (However, any hardware watchpoints that were\n\
14972 created before setting this to nonzero, will continue to use watchpoint\n\
14973 hardware.)"),
14974 NULL,
14975 show_can_use_hw_watchpoints,
14976 &setlist, &showlist);
14977
14978 can_use_hw_watchpoints = 1;
14979
14980 /* Tracepoint manipulation commands. */
14981
14982 cmd_list_element *trace_cmd
14983 = add_com ("trace", class_breakpoint, trace_command, _("\
14984 Set a tracepoint at specified location.\n\
14985 \n"
14986 BREAK_ARGS_HELP ("trace") "\n\
14987 Do \"help tracepoints\" for info on other tracepoint commands."));
14988 set_cmd_completer (trace_cmd, location_completer);
14989
14990 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14991 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14992 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14993 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
14994
14995 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
14996 Set a fast tracepoint at specified location.\n\
14997 \n"
14998 BREAK_ARGS_HELP ("ftrace") "\n\
14999 Do \"help tracepoints\" for info on other tracepoint commands."));
15000 set_cmd_completer (c, location_completer);
15001
15002 c = add_com ("strace", class_breakpoint, strace_command, _("\
15003 Set a static tracepoint at location or marker.\n\
15004 \n\
15005 strace [LOCATION] [if CONDITION]\n\
15006 LOCATION may be a linespec, explicit, or address location (described below) \n\
15007 or -m MARKER_ID.\n\n\
15008 If a marker id is specified, probe the marker with that name. With\n\
15009 no LOCATION, uses current execution address of the selected stack frame.\n\
15010 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15011 This collects arbitrary user data passed in the probe point call to the\n\
15012 tracing library. You can inspect it when analyzing the trace buffer,\n\
15013 by printing the $_sdata variable like any other convenience variable.\n\
15014 \n\
15015 CONDITION is a boolean expression.\n\
15016 \n" LOCATION_SPEC_HELP_STRING "\n\n\
15017 Multiple tracepoints at one place are permitted, and useful if their\n\
15018 conditions are different.\n\
15019 \n\
15020 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15021 Do \"help tracepoints\" for info on other tracepoint commands."));
15022 set_cmd_completer (c, location_completer);
15023
15024 cmd_list_element *info_tracepoints_cmd
15025 = add_info ("tracepoints", info_tracepoints_command, _("\
15026 Status of specified tracepoints (all tracepoints if no argument).\n\
15027 Convenience variable \"$tpnum\" contains the number of the\n\
15028 last tracepoint set."));
15029
15030 add_info_alias ("tp", info_tracepoints_cmd, 1);
15031
15032 cmd_list_element *delete_tracepoints_cmd
15033 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15034 Delete specified tracepoints.\n\
15035 Arguments are tracepoint numbers, separated by spaces.\n\
15036 No argument means delete all tracepoints."),
15037 &deletelist);
15038 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
15039
15040 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15041 Disable specified tracepoints.\n\
15042 Arguments are tracepoint numbers, separated by spaces.\n\
15043 No argument means disable all tracepoints."),
15044 &disablelist);
15045 deprecate_cmd (c, "disable");
15046
15047 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15048 Enable specified tracepoints.\n\
15049 Arguments are tracepoint numbers, separated by spaces.\n\
15050 No argument means enable all tracepoints."),
15051 &enablelist);
15052 deprecate_cmd (c, "enable");
15053
15054 add_com ("passcount", class_trace, trace_pass_command, _("\
15055 Set the passcount for a tracepoint.\n\
15056 The trace will end when the tracepoint has been passed 'count' times.\n\
15057 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15058 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15059
15060 add_basic_prefix_cmd ("save", class_breakpoint,
15061 _("Save breakpoint definitions as a script."),
15062 &save_cmdlist,
15063 0/*allow-unknown*/, &cmdlist);
15064
15065 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15066 Save current breakpoint definitions as a script.\n\
15067 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15068 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15069 session to restore them."),
15070 &save_cmdlist);
15071 set_cmd_completer (c, filename_completer);
15072
15073 cmd_list_element *save_tracepoints_cmd
15074 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15075 Save current tracepoint definitions as a script.\n\
15076 Use the 'source' command in another debug session to restore them."),
15077 &save_cmdlist);
15078 set_cmd_completer (save_tracepoints_cmd, filename_completer);
15079
15080 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
15081 deprecate_cmd (c, "save tracepoints");
15082
15083 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
15084 _("\
15085 Breakpoint specific settings.\n\
15086 Configure various breakpoint-specific variables such as\n\
15087 pending breakpoint behavior."),
15088 _("\
15089 Breakpoint specific settings.\n\
15090 Configure various breakpoint-specific variables such as\n\
15091 pending breakpoint behavior."),
15092 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
15093 &setlist, &showlist);
15094
15095 add_setshow_auto_boolean_cmd ("pending", no_class,
15096 &pending_break_support, _("\
15097 Set debugger's behavior regarding pending breakpoints."), _("\
15098 Show debugger's behavior regarding pending breakpoints."), _("\
15099 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15100 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15101 an error. If auto, an unrecognized breakpoint location results in a\n\
15102 user-query to see if a pending breakpoint should be created."),
15103 NULL,
15104 show_pending_break_support,
15105 &breakpoint_set_cmdlist,
15106 &breakpoint_show_cmdlist);
15107
15108 pending_break_support = AUTO_BOOLEAN_AUTO;
15109
15110 add_setshow_boolean_cmd ("auto-hw", no_class,
15111 &automatic_hardware_breakpoints, _("\
15112 Set automatic usage of hardware breakpoints."), _("\
15113 Show automatic usage of hardware breakpoints."), _("\
15114 If set, the debugger will automatically use hardware breakpoints for\n\
15115 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15116 a warning will be emitted for such breakpoints."),
15117 NULL,
15118 show_automatic_hardware_breakpoints,
15119 &breakpoint_set_cmdlist,
15120 &breakpoint_show_cmdlist);
15121
15122 add_setshow_boolean_cmd ("always-inserted", class_support,
15123 &always_inserted_mode, _("\
15124 Set mode for inserting breakpoints."), _("\
15125 Show mode for inserting breakpoints."), _("\
15126 When this mode is on, breakpoints are inserted immediately as soon as\n\
15127 they're created, kept inserted even when execution stops, and removed\n\
15128 only when the user deletes them. When this mode is off (the default),\n\
15129 breakpoints are inserted only when execution continues, and removed\n\
15130 when execution stops."),
15131 NULL,
15132 &show_always_inserted_mode,
15133 &breakpoint_set_cmdlist,
15134 &breakpoint_show_cmdlist);
15135
15136 add_setshow_boolean_cmd ("breakpoint", class_maintenance,
15137 &debug_breakpoint, _("\
15138 Set breakpoint location debugging."), _("\
15139 Show breakpoint location debugging."), _("\
15140 When on, breakpoint location specific debugging is enabled."),
15141 NULL,
15142 show_debug_breakpoint,
15143 &setdebuglist, &showdebuglist);
15144
15145 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15146 condition_evaluation_enums,
15147 &condition_evaluation_mode_1, _("\
15148 Set mode of breakpoint condition evaluation."), _("\
15149 Show mode of breakpoint condition evaluation."), _("\
15150 When this is set to \"host\", breakpoint conditions will be\n\
15151 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15152 breakpoint conditions will be downloaded to the target (if the target\n\
15153 supports such feature) and conditions will be evaluated on the target's side.\n\
15154 If this is set to \"auto\" (default), this will be automatically set to\n\
15155 \"target\" if it supports condition evaluation, otherwise it will\n\
15156 be set to \"host\"."),
15157 &set_condition_evaluation_mode,
15158 &show_condition_evaluation_mode,
15159 &breakpoint_set_cmdlist,
15160 &breakpoint_show_cmdlist);
15161
15162 add_com ("break-range", class_breakpoint, break_range_command, _("\
15163 Set a breakpoint for an address range.\n\
15164 break-range START-LOCATION, END-LOCATION\n\
15165 where START-LOCATION and END-LOCATION can be one of the following:\n\
15166 LINENUM, for that line in the current file,\n\
15167 FILE:LINENUM, for that line in that file,\n\
15168 +OFFSET, for that number of lines after the current line\n\
15169 or the start of the range\n\
15170 FUNCTION, for the first line in that function,\n\
15171 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15172 *ADDRESS, for the instruction at that address.\n\
15173 \n\
15174 The breakpoint will stop execution of the inferior whenever it executes\n\
15175 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15176 range (including START-LOCATION and END-LOCATION)."));
15177
15178 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15179 Set a dynamic printf at specified location.\n\
15180 dprintf location,format string,arg1,arg2,...\n\
15181 location may be a linespec, explicit, or address location.\n"
15182 "\n" LOCATION_SPEC_HELP_STRING));
15183 set_cmd_completer (c, location_completer);
15184
15185 add_setshow_enum_cmd ("dprintf-style", class_support,
15186 dprintf_style_enums, &dprintf_style, _("\
15187 Set the style of usage for dynamic printf."), _("\
15188 Show the style of usage for dynamic printf."), _("\
15189 This setting chooses how GDB will do a dynamic printf.\n\
15190 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15191 console, as with the \"printf\" command.\n\
15192 If the value is \"call\", the print is done by calling a function in your\n\
15193 program; by default printf(), but you can choose a different function or\n\
15194 output stream by setting dprintf-function and dprintf-channel."),
15195 update_dprintf_commands, NULL,
15196 &setlist, &showlist);
15197
15198 add_setshow_string_cmd ("dprintf-function", class_support,
15199 &dprintf_function, _("\
15200 Set the function to use for dynamic printf."), _("\
15201 Show the function to use for dynamic printf."), NULL,
15202 update_dprintf_commands, NULL,
15203 &setlist, &showlist);
15204
15205 add_setshow_string_cmd ("dprintf-channel", class_support,
15206 &dprintf_channel, _("\
15207 Set the channel to use for dynamic printf."), _("\
15208 Show the channel to use for dynamic printf."), NULL,
15209 update_dprintf_commands, NULL,
15210 &setlist, &showlist);
15211
15212 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15213 &disconnected_dprintf, _("\
15214 Set whether dprintf continues after GDB disconnects."), _("\
15215 Show whether dprintf continues after GDB disconnects."), _("\
15216 Use this to let dprintf commands continue to hit and produce output\n\
15217 even if GDB disconnects or detaches from the target."),
15218 NULL,
15219 NULL,
15220 &setlist, &showlist);
15221
15222 add_com ("agent-printf", class_vars, agent_printf_command, _("\
15223 Target agent only formatted printing, like the C \"printf\" function.\n\
15224 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15225 This supports most C printf format specifications, like %s, %d, etc.\n\
15226 This is useful for formatted output in user-defined commands."));
15227
15228 automatic_hardware_breakpoints = true;
15229
15230 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
15231 "breakpoint");
15232 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
15233 "breakpoint");
15234 gdb::observers::inferior_removed.attach (remove_inferior_breakpoints,
15235 "breakpoint");
15236 }