Use scoped_value_mark in more places
[binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "ui-out.h"
48 #include "cli/cli-script.h"
49 #include "block.h"
50 #include "solib.h"
51 #include "solist.h"
52 #include "observable.h"
53 #include "memattr.h"
54 #include "ada-lang.h"
55 #include "top.h"
56 #include "valprint.h"
57 #include "jit.h"
58 #include "parser-defs.h"
59 #include "gdbsupport/gdb_regex.h"
60 #include "probe.h"
61 #include "cli/cli-utils.h"
62 #include "stack.h"
63 #include "ax-gdb.h"
64 #include "dummy-frame.h"
65 #include "interps.h"
66 #include "gdbsupport/format.h"
67 #include "thread-fsm.h"
68 #include "tid-parse.h"
69 #include "cli/cli-style.h"
70 #include "cli/cli-decode.h"
71
72 /* readline include files */
73 #include "readline/tilde.h"
74
75 /* readline defines this. */
76 #undef savestring
77
78 #include "mi/mi-common.h"
79 #include "extension.h"
80 #include <algorithm>
81 #include "progspace-and-thread.h"
82 #include "gdbsupport/array-view.h"
83 #include "gdbsupport/gdb_optional.h"
84
85 /* Prototypes for local functions. */
86
87 static void map_breakpoint_numbers (const char *,
88 gdb::function_view<void (breakpoint *)>);
89
90 static void
91 create_sals_from_location_spec_default (location_spec *locspec,
92 linespec_result *canonical);
93
94 static void create_breakpoints_sal (struct gdbarch *,
95 struct linespec_result *,
96 gdb::unique_xmalloc_ptr<char>,
97 gdb::unique_xmalloc_ptr<char>,
98 enum bptype,
99 enum bpdisp, int, int,
100 int,
101 int, int, int, unsigned);
102
103 static int can_use_hardware_watchpoint
104 (const std::vector<value_ref_ptr> &vals);
105
106 static void mention (const breakpoint *);
107
108 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
109
110 static breakpoint *add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b);
111
112 static struct breakpoint *
113 momentary_breakpoint_from_master (struct breakpoint *orig,
114 enum bptype type,
115 int loc_enabled, int thread);
116
117 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
118
119 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
120 CORE_ADDR bpaddr,
121 enum bptype bptype,
122 struct program_space *pspace);
123
124 static int watchpoint_locations_match (struct bp_location *loc1,
125 struct bp_location *loc2);
126
127 static int breakpoint_locations_match (struct bp_location *loc1,
128 struct bp_location *loc2,
129 bool sw_hw_bps_match = false);
130
131 static int breakpoint_location_address_match (struct bp_location *bl,
132 const struct address_space *aspace,
133 CORE_ADDR addr);
134
135 static int breakpoint_location_address_range_overlap (struct bp_location *,
136 const address_space *,
137 CORE_ADDR, int);
138
139 static int remove_breakpoint (struct bp_location *);
140 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
141
142 static enum print_stop_action print_bp_stop_message (bpstat *bs);
143
144 static int hw_breakpoint_used_count (void);
145
146 static int hw_watchpoint_use_count (struct breakpoint *);
147
148 static int hw_watchpoint_used_count_others (struct breakpoint *except,
149 enum bptype type,
150 int *other_type_used);
151
152 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
153 int count);
154
155 static void decref_bp_location (struct bp_location **loc);
156
157 static std::vector<symtab_and_line> bkpt_probe_decode_location_spec
158 (struct breakpoint *b,
159 location_spec *locspec,
160 struct program_space *search_pspace);
161
162 static bool bl_address_is_meaningful (bp_location *loc);
163
164 static int find_loc_num_by_location (const bp_location *loc);
165
166 /* update_global_location_list's modes of operation wrt to whether to
167 insert locations now. */
168 enum ugll_insert_mode
169 {
170 /* Don't insert any breakpoint locations into the inferior, only
171 remove already-inserted locations that no longer should be
172 inserted. Functions that delete a breakpoint or breakpoints
173 should specify this mode, so that deleting a breakpoint doesn't
174 have the side effect of inserting the locations of other
175 breakpoints that are marked not-inserted, but should_be_inserted
176 returns true on them.
177
178 This behavior is useful is situations close to tear-down -- e.g.,
179 after an exec, while the target still has execution, but
180 breakpoint shadows of the previous executable image should *NOT*
181 be restored to the new image; or before detaching, where the
182 target still has execution and wants to delete breakpoints from
183 GDB's lists, and all breakpoints had already been removed from
184 the inferior. */
185 UGLL_DONT_INSERT,
186
187 /* May insert breakpoints iff breakpoints_should_be_inserted_now
188 claims breakpoints should be inserted now. */
189 UGLL_MAY_INSERT,
190
191 /* Insert locations now, irrespective of
192 breakpoints_should_be_inserted_now. E.g., say all threads are
193 stopped right now, and the user did "continue". We need to
194 insert breakpoints _before_ resuming the target, but
195 UGLL_MAY_INSERT wouldn't insert them, because
196 breakpoints_should_be_inserted_now returns false at that point,
197 as no thread is running yet. */
198 UGLL_INSERT
199 };
200
201 static void update_global_location_list (enum ugll_insert_mode);
202
203 static void update_global_location_list_nothrow (enum ugll_insert_mode);
204
205 static void insert_breakpoint_locations (void);
206
207 static void trace_pass_command (const char *, int);
208
209 static void set_tracepoint_count (int num);
210
211 static bool is_masked_watchpoint (const struct breakpoint *b);
212
213 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
214 otherwise. */
215
216 static int strace_marker_p (struct breakpoint *b);
217
218 static void bkpt_probe_create_sals_from_location_spec
219 (location_spec *locspec,
220 struct linespec_result *canonical);
221 static void tracepoint_probe_create_sals_from_location_spec
222 (location_spec *locspec,
223 struct linespec_result *canonical);
224
225 const struct breakpoint_ops code_breakpoint_ops =
226 {
227 create_sals_from_location_spec_default,
228 create_breakpoints_sal,
229 };
230
231 /* Breakpoints set on probes. */
232 static const struct breakpoint_ops bkpt_probe_breakpoint_ops =
233 {
234 bkpt_probe_create_sals_from_location_spec,
235 create_breakpoints_sal,
236 };
237
238 /* Tracepoints set on probes. */
239 static const struct breakpoint_ops tracepoint_probe_breakpoint_ops =
240 {
241 tracepoint_probe_create_sals_from_location_spec,
242 create_breakpoints_sal,
243 };
244
245 /* Implementation of abstract dtors. These must exist to satisfy the
246 linker. */
247
248 breakpoint::~breakpoint ()
249 {
250 }
251
252 code_breakpoint::~code_breakpoint ()
253 {
254 }
255
256 catchpoint::~catchpoint ()
257 {
258 }
259
260 /* The structure to be used in regular breakpoints. */
261 struct ordinary_breakpoint : public code_breakpoint
262 {
263 using code_breakpoint::code_breakpoint;
264
265 int resources_needed (const struct bp_location *) override;
266 enum print_stop_action print_it (const bpstat *bs) const override;
267 void print_mention () const override;
268 void print_recreate (struct ui_file *fp) const override;
269 };
270
271 /* Internal breakpoints. These typically have a lifetime the same as
272 the program, and they end up installed on the breakpoint chain with
273 a negative breakpoint number. They're visible in "maint info
274 breakpoints", but not "info breakpoints". */
275 struct internal_breakpoint : public code_breakpoint
276 {
277 internal_breakpoint (struct gdbarch *gdbarch,
278 enum bptype type, CORE_ADDR address)
279 : code_breakpoint (gdbarch, type)
280 {
281 symtab_and_line sal;
282 sal.pc = address;
283 sal.section = find_pc_overlay (sal.pc);
284 sal.pspace = current_program_space;
285 add_location (sal);
286
287 pspace = current_program_space;
288 disposition = disp_donttouch;
289 }
290
291 void re_set () override;
292 void check_status (struct bpstat *bs) override;
293 enum print_stop_action print_it (const bpstat *bs) const override;
294 void print_mention () const override;
295 };
296
297 /* Momentary breakpoints. These typically have a lifetime of some run
298 control command only, are always thread-specific, and have 0 for
299 breakpoint number. I.e., there can be many momentary breakpoints
300 on the breakpoint chain and they all same the same number (zero).
301 They're visible in "maint info breakpoints", but not "info
302 breakpoints". */
303 struct momentary_breakpoint : public code_breakpoint
304 {
305 momentary_breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
306 program_space *pspace_,
307 const struct frame_id &frame_id_,
308 int thread_)
309 : code_breakpoint (gdbarch_, bptype)
310 {
311 /* If FRAME_ID is valid, it should be a real frame, not an inlined
312 or tail-called one. */
313 gdb_assert (!frame_id_artificial_p (frame_id));
314
315 /* Momentary breakpoints are always thread-specific. */
316 gdb_assert (thread_ > 0);
317
318 pspace = pspace_;
319 enable_state = bp_enabled;
320 disposition = disp_donttouch;
321 frame_id = frame_id_;
322 thread = thread_;
323 }
324
325 void re_set () override;
326 void check_status (struct bpstat *bs) override;
327 enum print_stop_action print_it (const bpstat *bs) const override;
328 void print_mention () const override;
329 };
330
331 /* DPrintf breakpoints. */
332 struct dprintf_breakpoint : public ordinary_breakpoint
333 {
334 using ordinary_breakpoint::ordinary_breakpoint;
335
336 void re_set () override;
337 int breakpoint_hit (const struct bp_location *bl,
338 const address_space *aspace,
339 CORE_ADDR bp_addr,
340 const target_waitstatus &ws) override;
341 void print_recreate (struct ui_file *fp) const override;
342 void after_condition_true (struct bpstat *bs) override;
343 };
344
345 /* Ranged breakpoints. */
346 struct ranged_breakpoint : public ordinary_breakpoint
347 {
348 explicit ranged_breakpoint (struct gdbarch *gdbarch,
349 const symtab_and_line &sal_start,
350 int length,
351 location_spec_up start_locspec,
352 location_spec_up end_locspec)
353 : ordinary_breakpoint (gdbarch, bp_hardware_breakpoint)
354 {
355 bp_location *bl = add_location (sal_start);
356 bl->length = length;
357
358 disposition = disp_donttouch;
359
360 locspec = std::move (start_locspec);
361 locspec_range_end = std::move (end_locspec);
362 }
363
364 int breakpoint_hit (const struct bp_location *bl,
365 const address_space *aspace,
366 CORE_ADDR bp_addr,
367 const target_waitstatus &ws) override;
368 int resources_needed (const struct bp_location *) override;
369 enum print_stop_action print_it (const bpstat *bs) const override;
370 bool print_one (bp_location **) const override;
371 void print_one_detail (struct ui_out *) const override;
372 void print_mention () const override;
373 void print_recreate (struct ui_file *fp) const override;
374 };
375
376 /* Static tracepoints with marker (`-m'). */
377 struct static_marker_tracepoint : public tracepoint
378 {
379 using tracepoint::tracepoint;
380
381 std::vector<symtab_and_line> decode_location_spec
382 (struct location_spec *locspec,
383 struct program_space *search_pspace) override;
384 };
385
386 /* The style in which to perform a dynamic printf. This is a user
387 option because different output options have different tradeoffs;
388 if GDB does the printing, there is better error handling if there
389 is a problem with any of the arguments, but using an inferior
390 function lets you have special-purpose printers and sending of
391 output to the same place as compiled-in print functions. */
392
393 static const char dprintf_style_gdb[] = "gdb";
394 static const char dprintf_style_call[] = "call";
395 static const char dprintf_style_agent[] = "agent";
396 static const char *const dprintf_style_enums[] = {
397 dprintf_style_gdb,
398 dprintf_style_call,
399 dprintf_style_agent,
400 NULL
401 };
402 static const char *dprintf_style = dprintf_style_gdb;
403
404 /* The function to use for dynamic printf if the preferred style is to
405 call into the inferior. The value is simply a string that is
406 copied into the command, so it can be anything that GDB can
407 evaluate to a callable address, not necessarily a function name. */
408
409 static std::string dprintf_function = "printf";
410
411 /* The channel to use for dynamic printf if the preferred style is to
412 call into the inferior; if a nonempty string, it will be passed to
413 the call as the first argument, with the format string as the
414 second. As with the dprintf function, this can be anything that
415 GDB knows how to evaluate, so in addition to common choices like
416 "stderr", this could be an app-specific expression like
417 "mystreams[curlogger]". */
418
419 static std::string dprintf_channel;
420
421 /* True if dprintf commands should continue to operate even if GDB
422 has disconnected. */
423 static bool disconnected_dprintf = true;
424
425 struct command_line *
426 breakpoint_commands (struct breakpoint *b)
427 {
428 return b->commands ? b->commands.get () : NULL;
429 }
430
431 /* Flag indicating that a command has proceeded the inferior past the
432 current breakpoint. */
433
434 static bool breakpoint_proceeded;
435
436 const char *
437 bpdisp_text (enum bpdisp disp)
438 {
439 /* NOTE: the following values are a part of MI protocol and
440 represent values of 'disp' field returned when inferior stops at
441 a breakpoint. */
442 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
443
444 return bpdisps[(int) disp];
445 }
446
447 /* Prototypes for exported functions. */
448 /* If FALSE, gdb will not use hardware support for watchpoints, even
449 if such is available. */
450 static int can_use_hw_watchpoints;
451
452 static void
453 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
454 struct cmd_list_element *c,
455 const char *value)
456 {
457 gdb_printf (file,
458 _("Debugger's willingness to use "
459 "watchpoint hardware is %s.\n"),
460 value);
461 }
462
463 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
464 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
465 for unrecognized breakpoint locations.
466 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
467 static enum auto_boolean pending_break_support;
468 static void
469 show_pending_break_support (struct ui_file *file, int from_tty,
470 struct cmd_list_element *c,
471 const char *value)
472 {
473 gdb_printf (file,
474 _("Debugger's behavior regarding "
475 "pending breakpoints is %s.\n"),
476 value);
477 }
478
479 /* If true, gdb will automatically use hardware breakpoints for breakpoints
480 set with "break" but falling in read-only memory.
481 If false, gdb will warn about such breakpoints, but won't automatically
482 use hardware breakpoints. */
483 static bool automatic_hardware_breakpoints;
484 static void
485 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
486 struct cmd_list_element *c,
487 const char *value)
488 {
489 gdb_printf (file,
490 _("Automatic usage of hardware breakpoints is %s.\n"),
491 value);
492 }
493
494 /* If on, GDB keeps breakpoints inserted even if the inferior is
495 stopped, and immediately inserts any new breakpoints as soon as
496 they're created. If off (default), GDB keeps breakpoints off of
497 the target as long as possible. That is, it delays inserting
498 breakpoints until the next resume, and removes them again when the
499 target fully stops. This is a bit safer in case GDB crashes while
500 processing user input. */
501 static bool always_inserted_mode = false;
502
503 static void
504 show_always_inserted_mode (struct ui_file *file, int from_tty,
505 struct cmd_list_element *c, const char *value)
506 {
507 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
508 value);
509 }
510
511 /* See breakpoint.h. */
512
513 int
514 breakpoints_should_be_inserted_now (void)
515 {
516 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
517 {
518 /* If breakpoints are global, they should be inserted even if no
519 thread under gdb's control is running, or even if there are
520 no threads under GDB's control yet. */
521 return 1;
522 }
523 else
524 {
525 if (always_inserted_mode)
526 {
527 /* The user wants breakpoints inserted even if all threads
528 are stopped. */
529 return 1;
530 }
531
532 for (inferior *inf : all_inferiors ())
533 if (inf->has_execution ()
534 && threads_are_executing (inf->process_target ()))
535 return 1;
536
537 /* Don't remove breakpoints yet if, even though all threads are
538 stopped, we still have events to process. */
539 for (thread_info *tp : all_non_exited_threads ())
540 if (tp->resumed () && tp->has_pending_waitstatus ())
541 return 1;
542 }
543 return 0;
544 }
545
546 static const char condition_evaluation_both[] = "host or target";
547
548 /* Modes for breakpoint condition evaluation. */
549 static const char condition_evaluation_auto[] = "auto";
550 static const char condition_evaluation_host[] = "host";
551 static const char condition_evaluation_target[] = "target";
552 static const char *const condition_evaluation_enums[] = {
553 condition_evaluation_auto,
554 condition_evaluation_host,
555 condition_evaluation_target,
556 NULL
557 };
558
559 /* Global that holds the current mode for breakpoint condition evaluation. */
560 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
561
562 /* Global that we use to display information to the user (gets its value from
563 condition_evaluation_mode_1. */
564 static const char *condition_evaluation_mode = condition_evaluation_auto;
565
566 /* Translate a condition evaluation mode MODE into either "host"
567 or "target". This is used mostly to translate from "auto" to the
568 real setting that is being used. It returns the translated
569 evaluation mode. */
570
571 static const char *
572 translate_condition_evaluation_mode (const char *mode)
573 {
574 if (mode == condition_evaluation_auto)
575 {
576 if (target_supports_evaluation_of_breakpoint_conditions ())
577 return condition_evaluation_target;
578 else
579 return condition_evaluation_host;
580 }
581 else
582 return mode;
583 }
584
585 /* Discovers what condition_evaluation_auto translates to. */
586
587 static const char *
588 breakpoint_condition_evaluation_mode (void)
589 {
590 return translate_condition_evaluation_mode (condition_evaluation_mode);
591 }
592
593 /* Return true if GDB should evaluate breakpoint conditions or false
594 otherwise. */
595
596 static int
597 gdb_evaluates_breakpoint_condition_p (void)
598 {
599 const char *mode = breakpoint_condition_evaluation_mode ();
600
601 return (mode == condition_evaluation_host);
602 }
603
604 /* Are we executing breakpoint commands? */
605 static int executing_breakpoint_commands;
606
607 /* Are overlay event breakpoints enabled? */
608 static int overlay_events_enabled;
609
610 /* See description in breakpoint.h. */
611 bool target_exact_watchpoints = false;
612
613 /* Walk the following statement or block through all breakpoints.
614 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
615 current breakpoint. */
616
617 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
618 for (B = breakpoint_chain; \
619 B ? (TMP=B->next, 1): 0; \
620 B = TMP)
621
622 /* Chains of all breakpoints defined. */
623
624 static struct breakpoint *breakpoint_chain;
625
626 /* See breakpoint.h. */
627
628 breakpoint_range
629 all_breakpoints ()
630 {
631 return breakpoint_range (breakpoint_chain);
632 }
633
634 /* See breakpoint.h. */
635
636 breakpoint_safe_range
637 all_breakpoints_safe ()
638 {
639 return breakpoint_safe_range (all_breakpoints ());
640 }
641
642 /* See breakpoint.h. */
643
644 tracepoint_range
645 all_tracepoints ()
646 {
647 return tracepoint_range (breakpoint_chain);
648 }
649
650 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
651
652 static std::vector<bp_location *> bp_locations;
653
654 /* See breakpoint.h. */
655
656 const std::vector<bp_location *> &
657 all_bp_locations ()
658 {
659 return bp_locations;
660 }
661
662 /* Range to iterate over breakpoint locations at a given address. */
663
664 struct bp_locations_at_addr_range
665 {
666 using iterator = std::vector<bp_location *>::iterator;
667
668 bp_locations_at_addr_range (CORE_ADDR addr)
669 {
670 struct compare
671 {
672 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
673 { return loc->address < addr_; }
674
675 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
676 { return addr_ < loc->address; }
677 };
678
679 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
680 addr, compare ());
681
682 m_begin = it_pair.first;
683 m_end = it_pair.second;
684 }
685
686 iterator begin () const
687 { return m_begin; }
688
689 iterator end () const
690 { return m_end; }
691
692 private:
693 iterator m_begin;
694 iterator m_end;
695 };
696
697 /* Return a range to iterate over all breakpoint locations exactly at address
698 ADDR.
699
700 If it's needed to iterate multiple times on the same range, it's possible
701 to save the range in a local variable and use it multiple times:
702
703 auto range = all_bp_locations_at_addr (addr);
704
705 for (bp_location *loc : range)
706 // use loc
707
708 for (bp_location *loc : range)
709 // use loc
710
711 This saves a bit of time, as it avoids re-doing the binary searches to find
712 the range's boundaries. Just remember not to change the bp_locations vector
713 in the mean time, as it could make the range's iterators stale. */
714
715 static bp_locations_at_addr_range
716 all_bp_locations_at_addr (CORE_ADDR addr)
717 {
718 return bp_locations_at_addr_range (addr);
719 }
720
721 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
722 ADDRESS for the current elements of BP_LOCATIONS which get a valid
723 result from bp_location_has_shadow. You can use it for roughly
724 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
725 an address you need to read. */
726
727 static CORE_ADDR bp_locations_placed_address_before_address_max;
728
729 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
730 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
731 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
732 You can use it for roughly limiting the subrange of BP_LOCATIONS to
733 scan for shadow bytes for an address you need to read. */
734
735 static CORE_ADDR bp_locations_shadow_len_after_address_max;
736
737 /* The locations that no longer correspond to any breakpoint, unlinked
738 from the bp_locations array, but for which a hit may still be
739 reported by a target. */
740 static std::vector<bp_location *> moribund_locations;
741
742 /* Number of last breakpoint made. */
743
744 static int breakpoint_count;
745
746 /* The value of `breakpoint_count' before the last command that
747 created breakpoints. If the last (break-like) command created more
748 than one breakpoint, then the difference between BREAKPOINT_COUNT
749 and PREV_BREAKPOINT_COUNT is more than one. */
750 static int prev_breakpoint_count;
751
752 /* Number of last tracepoint made. */
753
754 static int tracepoint_count;
755
756 static struct cmd_list_element *breakpoint_set_cmdlist;
757 static struct cmd_list_element *breakpoint_show_cmdlist;
758 struct cmd_list_element *save_cmdlist;
759
760 /* Return whether a breakpoint is an active enabled breakpoint. */
761 static int
762 breakpoint_enabled (struct breakpoint *b)
763 {
764 return (b->enable_state == bp_enabled);
765 }
766
767 /* Set breakpoint count to NUM. */
768
769 static void
770 set_breakpoint_count (int num)
771 {
772 prev_breakpoint_count = breakpoint_count;
773 breakpoint_count = num;
774 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
775 }
776
777 /* Used by `start_rbreak_breakpoints' below, to record the current
778 breakpoint count before "rbreak" creates any breakpoint. */
779 static int rbreak_start_breakpoint_count;
780
781 /* Called at the start an "rbreak" command to record the first
782 breakpoint made. */
783
784 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
785 {
786 rbreak_start_breakpoint_count = breakpoint_count;
787 }
788
789 /* Called at the end of an "rbreak" command to record the last
790 breakpoint made. */
791
792 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
793 {
794 prev_breakpoint_count = rbreak_start_breakpoint_count;
795 }
796
797 /* Used in run_command to zero the hit count when a new run starts. */
798
799 void
800 clear_breakpoint_hit_counts (void)
801 {
802 for (breakpoint *b : all_breakpoints ())
803 b->hit_count = 0;
804 }
805
806 \f
807 /* Return the breakpoint with the specified number, or NULL
808 if the number does not refer to an existing breakpoint. */
809
810 struct breakpoint *
811 get_breakpoint (int num)
812 {
813 for (breakpoint *b : all_breakpoints ())
814 if (b->number == num)
815 return b;
816
817 return nullptr;
818 }
819
820 \f
821
822 /* Mark locations as "conditions have changed" in case the target supports
823 evaluating conditions on its side. */
824
825 static void
826 mark_breakpoint_modified (struct breakpoint *b)
827 {
828 /* This is only meaningful if the target is
829 evaluating conditions and if the user has
830 opted for condition evaluation on the target's
831 side. */
832 if (gdb_evaluates_breakpoint_condition_p ()
833 || !target_supports_evaluation_of_breakpoint_conditions ())
834 return;
835
836 if (!is_breakpoint (b))
837 return;
838
839 for (bp_location *loc : b->locations ())
840 loc->condition_changed = condition_modified;
841 }
842
843 /* Mark location as "conditions have changed" in case the target supports
844 evaluating conditions on its side. */
845
846 static void
847 mark_breakpoint_location_modified (struct bp_location *loc)
848 {
849 /* This is only meaningful if the target is
850 evaluating conditions and if the user has
851 opted for condition evaluation on the target's
852 side. */
853 if (gdb_evaluates_breakpoint_condition_p ()
854 || !target_supports_evaluation_of_breakpoint_conditions ())
855
856 return;
857
858 if (!is_breakpoint (loc->owner))
859 return;
860
861 loc->condition_changed = condition_modified;
862 }
863
864 /* Sets the condition-evaluation mode using the static global
865 condition_evaluation_mode. */
866
867 static void
868 set_condition_evaluation_mode (const char *args, int from_tty,
869 struct cmd_list_element *c)
870 {
871 const char *old_mode, *new_mode;
872
873 if ((condition_evaluation_mode_1 == condition_evaluation_target)
874 && !target_supports_evaluation_of_breakpoint_conditions ())
875 {
876 condition_evaluation_mode_1 = condition_evaluation_mode;
877 warning (_("Target does not support breakpoint condition evaluation.\n"
878 "Using host evaluation mode instead."));
879 return;
880 }
881
882 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
883 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
884
885 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
886 settings was "auto". */
887 condition_evaluation_mode = condition_evaluation_mode_1;
888
889 /* Only update the mode if the user picked a different one. */
890 if (new_mode != old_mode)
891 {
892 /* If the user switched to a different evaluation mode, we
893 need to synch the changes with the target as follows:
894
895 "host" -> "target": Send all (valid) conditions to the target.
896 "target" -> "host": Remove all the conditions from the target.
897 */
898
899 if (new_mode == condition_evaluation_target)
900 {
901 /* Mark everything modified and synch conditions with the
902 target. */
903 for (bp_location *loc : all_bp_locations ())
904 mark_breakpoint_location_modified (loc);
905 }
906 else
907 {
908 /* Manually mark non-duplicate locations to synch conditions
909 with the target. We do this to remove all the conditions the
910 target knows about. */
911 for (bp_location *loc : all_bp_locations ())
912 if (is_breakpoint (loc->owner) && loc->inserted)
913 loc->needs_update = 1;
914 }
915
916 /* Do the update. */
917 update_global_location_list (UGLL_MAY_INSERT);
918 }
919
920 return;
921 }
922
923 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
924 what "auto" is translating to. */
925
926 static void
927 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
928 struct cmd_list_element *c, const char *value)
929 {
930 if (condition_evaluation_mode == condition_evaluation_auto)
931 gdb_printf (file,
932 _("Breakpoint condition evaluation "
933 "mode is %s (currently %s).\n"),
934 value,
935 breakpoint_condition_evaluation_mode ());
936 else
937 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
938 value);
939 }
940
941 /* Parse COND_STRING in the context of LOC and set as the condition
942 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
943 the number of LOC within its owner. In case of parsing error, mark
944 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
945
946 static void
947 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
948 int bp_num, int loc_num)
949 {
950 bool has_junk = false;
951 try
952 {
953 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
954 block_for_pc (loc->address), 0);
955 if (*cond_string != 0)
956 has_junk = true;
957 else
958 {
959 loc->cond = std::move (new_exp);
960 if (loc->disabled_by_cond && loc->enabled)
961 gdb_printf (_("Breakpoint %d's condition is now valid at "
962 "location %d, enabling.\n"),
963 bp_num, loc_num);
964
965 loc->disabled_by_cond = false;
966 }
967 }
968 catch (const gdb_exception_error &e)
969 {
970 if (loc->enabled)
971 {
972 /* Warn if a user-enabled location is now becoming disabled-by-cond.
973 BP_NUM is 0 if the breakpoint is being defined for the first
974 time using the "break ... if ..." command, and non-zero if
975 already defined. */
976 if (bp_num != 0)
977 warning (_("failed to validate condition at location %d.%d, "
978 "disabling:\n %s"), bp_num, loc_num, e.what ());
979 else
980 warning (_("failed to validate condition at location %d, "
981 "disabling:\n %s"), loc_num, e.what ());
982 }
983
984 loc->disabled_by_cond = true;
985 }
986
987 if (has_junk)
988 error (_("Garbage '%s' follows condition"), cond_string);
989 }
990
991 void
992 set_breakpoint_condition (struct breakpoint *b, const char *exp,
993 int from_tty, bool force)
994 {
995 if (*exp == 0)
996 {
997 b->cond_string.reset ();
998
999 if (is_watchpoint (b))
1000 static_cast<watchpoint *> (b)->cond_exp.reset ();
1001 else
1002 {
1003 int loc_num = 1;
1004 for (bp_location *loc : b->locations ())
1005 {
1006 loc->cond.reset ();
1007 if (loc->disabled_by_cond && loc->enabled)
1008 gdb_printf (_("Breakpoint %d's condition is now valid at "
1009 "location %d, enabling.\n"),
1010 b->number, loc_num);
1011 loc->disabled_by_cond = false;
1012 loc_num++;
1013
1014 /* No need to free the condition agent expression
1015 bytecode (if we have one). We will handle this
1016 when we go through update_global_location_list. */
1017 }
1018 }
1019
1020 if (from_tty)
1021 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
1022 }
1023 else
1024 {
1025 if (is_watchpoint (b))
1026 {
1027 innermost_block_tracker tracker;
1028 const char *arg = exp;
1029 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
1030 if (*arg != 0)
1031 error (_("Junk at end of expression"));
1032 watchpoint *w = static_cast<watchpoint *> (b);
1033 w->cond_exp = std::move (new_exp);
1034 w->cond_exp_valid_block = tracker.block ();
1035 }
1036 else
1037 {
1038 /* Parse and set condition expressions. We make two passes.
1039 In the first, we parse the condition string to see if it
1040 is valid in at least one location. If so, the condition
1041 would be accepted. So we go ahead and set the locations'
1042 conditions. In case no valid case is found, we throw
1043 the error and the condition string will be rejected.
1044 This two-pass approach is taken to avoid setting the
1045 state of locations in case of a reject. */
1046 for (bp_location *loc : b->locations ())
1047 {
1048 try
1049 {
1050 const char *arg = exp;
1051 parse_exp_1 (&arg, loc->address,
1052 block_for_pc (loc->address), 0);
1053 if (*arg != 0)
1054 error (_("Junk at end of expression"));
1055 break;
1056 }
1057 catch (const gdb_exception_error &e)
1058 {
1059 /* Condition string is invalid. If this happens to
1060 be the last loc, abandon (if not forced) or continue
1061 (if forced). */
1062 if (loc->next == nullptr && !force)
1063 throw;
1064 }
1065 }
1066
1067 /* If we reach here, the condition is valid at some locations. */
1068 int loc_num = 1;
1069 for (bp_location *loc : b->locations ())
1070 {
1071 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
1072 loc_num++;
1073 }
1074 }
1075
1076 /* We know that the new condition parsed successfully. The
1077 condition string of the breakpoint can be safely updated. */
1078 b->cond_string = make_unique_xstrdup (exp);
1079 b->condition_not_parsed = 0;
1080 }
1081 mark_breakpoint_modified (b);
1082
1083 gdb::observers::breakpoint_modified.notify (b);
1084 }
1085
1086 /* See breakpoint.h. */
1087
1088 void
1089 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
1090 bool force)
1091 {
1092 for (breakpoint *b : all_breakpoints ())
1093 if (b->number == bpnum)
1094 {
1095 /* Check if this breakpoint has a "stop" method implemented in an
1096 extension language. This method and conditions entered into GDB
1097 from the CLI are mutually exclusive. */
1098 const struct extension_language_defn *extlang
1099 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1100
1101 if (extlang != NULL)
1102 {
1103 error (_("Only one stop condition allowed. There is currently"
1104 " a %s stop condition defined for this breakpoint."),
1105 ext_lang_capitalized_name (extlang));
1106 }
1107 set_breakpoint_condition (b, exp, from_tty, force);
1108
1109 if (is_breakpoint (b))
1110 update_global_location_list (UGLL_MAY_INSERT);
1111
1112 return;
1113 }
1114
1115 error (_("No breakpoint number %d."), bpnum);
1116 }
1117
1118 /* The options for the "condition" command. */
1119
1120 struct condition_command_opts
1121 {
1122 /* For "-force". */
1123 bool force_condition = false;
1124 };
1125
1126 static const gdb::option::option_def condition_command_option_defs[] = {
1127
1128 gdb::option::flag_option_def<condition_command_opts> {
1129 "force",
1130 [] (condition_command_opts *opts) { return &opts->force_condition; },
1131 N_("Set the condition even if it is invalid for all current locations."),
1132 },
1133
1134 };
1135
1136 /* Create an option_def_group for the "condition" options, with
1137 CC_OPTS as context. */
1138
1139 static inline gdb::option::option_def_group
1140 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1141 {
1142 return {{condition_command_option_defs}, cc_opts};
1143 }
1144
1145 /* Completion for the "condition" command. */
1146
1147 static void
1148 condition_completer (struct cmd_list_element *cmd,
1149 completion_tracker &tracker,
1150 const char *text, const char * /*word*/)
1151 {
1152 bool has_no_arguments = (*text == '\0');
1153 condition_command_opts cc_opts;
1154 const auto group = make_condition_command_options_def_group (&cc_opts);
1155 if (gdb::option::complete_options
1156 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1157 return;
1158
1159 text = skip_spaces (text);
1160 const char *space = skip_to_space (text);
1161 if (*space == '\0')
1162 {
1163 int len;
1164
1165 if (text[0] == '$')
1166 {
1167 tracker.advance_custom_word_point_by (1);
1168 /* We don't support completion of history indices. */
1169 if (!isdigit (text[1]))
1170 complete_internalvar (tracker, &text[1]);
1171 return;
1172 }
1173
1174 /* Suggest the "-force" flag if no arguments are given. If
1175 arguments were passed, they either already include the flag,
1176 or we are beyond the point of suggesting it because it's
1177 positionally the first argument. */
1178 if (has_no_arguments)
1179 gdb::option::complete_on_all_options (tracker, group);
1180
1181 /* We're completing the breakpoint number. */
1182 len = strlen (text);
1183
1184 for (breakpoint *b : all_breakpoints ())
1185 {
1186 char number[50];
1187
1188 xsnprintf (number, sizeof (number), "%d", b->number);
1189
1190 if (strncmp (number, text, len) == 0)
1191 tracker.add_completion (make_unique_xstrdup (number));
1192 }
1193
1194 return;
1195 }
1196
1197 /* We're completing the expression part. Skip the breakpoint num. */
1198 const char *exp_start = skip_spaces (space);
1199 tracker.advance_custom_word_point_by (exp_start - text);
1200 text = exp_start;
1201 const char *word = advance_to_expression_complete_word_point (tracker, text);
1202 expression_completer (cmd, tracker, text, word);
1203 }
1204
1205 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1206
1207 static void
1208 condition_command (const char *arg, int from_tty)
1209 {
1210 const char *p;
1211 int bnum;
1212
1213 if (arg == 0)
1214 error_no_arg (_("breakpoint number"));
1215
1216 p = arg;
1217
1218 /* Check if the "-force" flag was passed. */
1219 condition_command_opts cc_opts;
1220 const auto group = make_condition_command_options_def_group (&cc_opts);
1221 gdb::option::process_options
1222 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1223
1224 bnum = get_number (&p);
1225 if (bnum == 0)
1226 error (_("Bad breakpoint argument: '%s'"), arg);
1227
1228 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1229 }
1230
1231 /* Check that COMMAND do not contain commands that are suitable
1232 only for tracepoints and not suitable for ordinary breakpoints.
1233 Throw if any such commands is found. */
1234
1235 static void
1236 check_no_tracepoint_commands (struct command_line *commands)
1237 {
1238 struct command_line *c;
1239
1240 for (c = commands; c; c = c->next)
1241 {
1242 if (c->control_type == while_stepping_control)
1243 error (_("The 'while-stepping' command can "
1244 "only be used for tracepoints"));
1245
1246 check_no_tracepoint_commands (c->body_list_0.get ());
1247 check_no_tracepoint_commands (c->body_list_1.get ());
1248
1249 /* Not that command parsing removes leading whitespace and comment
1250 lines and also empty lines. So, we only need to check for
1251 command directly. */
1252 if (strstr (c->line, "collect ") == c->line)
1253 error (_("The 'collect' command can only be used for tracepoints"));
1254
1255 if (strstr (c->line, "teval ") == c->line)
1256 error (_("The 'teval' command can only be used for tracepoints"));
1257 }
1258 }
1259
1260 struct longjmp_breakpoint : public momentary_breakpoint
1261 {
1262 using momentary_breakpoint::momentary_breakpoint;
1263
1264 ~longjmp_breakpoint () override;
1265 };
1266
1267 /* Encapsulate tests for different types of tracepoints. */
1268
1269 static bool
1270 is_tracepoint_type (bptype type)
1271 {
1272 return (type == bp_tracepoint
1273 || type == bp_fast_tracepoint
1274 || type == bp_static_tracepoint
1275 || type == bp_static_marker_tracepoint);
1276 }
1277
1278 /* See breakpoint.h. */
1279
1280 bool
1281 is_tracepoint (const struct breakpoint *b)
1282 {
1283 return is_tracepoint_type (b->type);
1284 }
1285
1286 /* Factory function to create an appropriate instance of breakpoint given
1287 TYPE. */
1288
1289 template<typename... Arg>
1290 static std::unique_ptr<code_breakpoint>
1291 new_breakpoint_from_type (struct gdbarch *gdbarch, bptype type,
1292 Arg&&... args)
1293 {
1294 code_breakpoint *b;
1295
1296 switch (type)
1297 {
1298 case bp_breakpoint:
1299 case bp_hardware_breakpoint:
1300 b = new ordinary_breakpoint (gdbarch, type,
1301 std::forward<Arg> (args)...);
1302 break;
1303
1304 case bp_fast_tracepoint:
1305 case bp_static_tracepoint:
1306 case bp_tracepoint:
1307 b = new tracepoint (gdbarch, type,
1308 std::forward<Arg> (args)...);
1309 break;
1310
1311 case bp_static_marker_tracepoint:
1312 b = new static_marker_tracepoint (gdbarch, type,
1313 std::forward<Arg> (args)...);
1314 break;
1315
1316 case bp_dprintf:
1317 b = new dprintf_breakpoint (gdbarch, type,
1318 std::forward<Arg> (args)...);
1319 break;
1320
1321 default:
1322 gdb_assert_not_reached ("invalid type");
1323 }
1324
1325 return std::unique_ptr<code_breakpoint> (b);
1326 }
1327
1328 /* A helper function that validates that COMMANDS are valid for a
1329 breakpoint. This function will throw an exception if a problem is
1330 found. */
1331
1332 static void
1333 validate_commands_for_breakpoint (struct breakpoint *b,
1334 struct command_line *commands)
1335 {
1336 if (is_tracepoint (b))
1337 {
1338 struct tracepoint *t = (struct tracepoint *) b;
1339 struct command_line *c;
1340 struct command_line *while_stepping = 0;
1341
1342 /* Reset the while-stepping step count. The previous commands
1343 might have included a while-stepping action, while the new
1344 ones might not. */
1345 t->step_count = 0;
1346
1347 /* We need to verify that each top-level element of commands is
1348 valid for tracepoints, that there's at most one
1349 while-stepping element, and that the while-stepping's body
1350 has valid tracing commands excluding nested while-stepping.
1351 We also need to validate the tracepoint action line in the
1352 context of the tracepoint --- validate_actionline actually
1353 has side effects, like setting the tracepoint's
1354 while-stepping STEP_COUNT, in addition to checking if the
1355 collect/teval actions parse and make sense in the
1356 tracepoint's context. */
1357 for (c = commands; c; c = c->next)
1358 {
1359 if (c->control_type == while_stepping_control)
1360 {
1361 if (b->type == bp_fast_tracepoint)
1362 error (_("The 'while-stepping' command "
1363 "cannot be used for fast tracepoint"));
1364 else if (b->type == bp_static_tracepoint
1365 || b->type == bp_static_marker_tracepoint)
1366 error (_("The 'while-stepping' command "
1367 "cannot be used for static tracepoint"));
1368
1369 if (while_stepping)
1370 error (_("The 'while-stepping' command "
1371 "can be used only once"));
1372 else
1373 while_stepping = c;
1374 }
1375
1376 validate_actionline (c->line, b);
1377 }
1378 if (while_stepping)
1379 {
1380 struct command_line *c2;
1381
1382 gdb_assert (while_stepping->body_list_1 == nullptr);
1383 c2 = while_stepping->body_list_0.get ();
1384 for (; c2; c2 = c2->next)
1385 {
1386 if (c2->control_type == while_stepping_control)
1387 error (_("The 'while-stepping' command cannot be nested"));
1388 }
1389 }
1390 }
1391 else
1392 {
1393 check_no_tracepoint_commands (commands);
1394 }
1395 }
1396
1397 /* Return a vector of all the static tracepoints set at ADDR. The
1398 caller is responsible for releasing the vector. */
1399
1400 std::vector<breakpoint *>
1401 static_tracepoints_here (CORE_ADDR addr)
1402 {
1403 std::vector<breakpoint *> found;
1404
1405 for (breakpoint *b : all_breakpoints ())
1406 if (b->type == bp_static_tracepoint
1407 || b->type == bp_static_marker_tracepoint)
1408 {
1409 for (bp_location *loc : b->locations ())
1410 if (loc->address == addr)
1411 found.push_back (b);
1412 }
1413
1414 return found;
1415 }
1416
1417 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1418 validate that only allowed commands are included. */
1419
1420 void
1421 breakpoint_set_commands (struct breakpoint *b,
1422 counted_command_line &&commands)
1423 {
1424 validate_commands_for_breakpoint (b, commands.get ());
1425
1426 b->commands = std::move (commands);
1427 gdb::observers::breakpoint_modified.notify (b);
1428 }
1429
1430 /* Set the internal `silent' flag on the breakpoint. Note that this
1431 is not the same as the "silent" that may appear in the breakpoint's
1432 commands. */
1433
1434 void
1435 breakpoint_set_silent (struct breakpoint *b, int silent)
1436 {
1437 int old_silent = b->silent;
1438
1439 b->silent = silent;
1440 if (old_silent != silent)
1441 gdb::observers::breakpoint_modified.notify (b);
1442 }
1443
1444 /* Set the thread for this breakpoint. If THREAD is -1, make the
1445 breakpoint work for any thread. */
1446
1447 void
1448 breakpoint_set_thread (struct breakpoint *b, int thread)
1449 {
1450 int old_thread = b->thread;
1451
1452 b->thread = thread;
1453 if (old_thread != thread)
1454 gdb::observers::breakpoint_modified.notify (b);
1455 }
1456
1457 /* Set the task for this breakpoint. If TASK is 0, make the
1458 breakpoint work for any task. */
1459
1460 void
1461 breakpoint_set_task (struct breakpoint *b, int task)
1462 {
1463 int old_task = b->task;
1464
1465 b->task = task;
1466 if (old_task != task)
1467 gdb::observers::breakpoint_modified.notify (b);
1468 }
1469
1470 static void
1471 commands_command_1 (const char *arg, int from_tty,
1472 struct command_line *control)
1473 {
1474 counted_command_line cmd;
1475 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1476 NULL after the call to read_command_lines if the user provides an empty
1477 list of command by just typing "end". */
1478 bool cmd_read = false;
1479
1480 std::string new_arg;
1481
1482 if (arg == NULL || !*arg)
1483 {
1484 /* Argument not explicitly given. Synthesize it. */
1485 if (breakpoint_count - prev_breakpoint_count > 1)
1486 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1487 breakpoint_count);
1488 else if (breakpoint_count > 0)
1489 new_arg = string_printf ("%d", breakpoint_count);
1490 }
1491 else
1492 {
1493 /* Create a copy of ARG. This is needed because the "commands"
1494 command may be coming from a script. In that case, the read
1495 line buffer is going to be overwritten in the lambda of
1496 'map_breakpoint_numbers' below when reading the next line
1497 before we are are done parsing the breakpoint numbers. */
1498 new_arg = arg;
1499 }
1500 arg = new_arg.c_str ();
1501
1502 map_breakpoint_numbers
1503 (arg, [&] (breakpoint *b)
1504 {
1505 if (!cmd_read)
1506 {
1507 gdb_assert (cmd == NULL);
1508 if (control != NULL)
1509 cmd = control->body_list_0;
1510 else
1511 {
1512 std::string str
1513 = string_printf (_("Type commands for breakpoint(s) "
1514 "%s, one per line."),
1515 arg);
1516
1517 auto do_validate = [=] (const char *line)
1518 {
1519 validate_actionline (line, b);
1520 };
1521 gdb::function_view<void (const char *)> validator;
1522 if (is_tracepoint (b))
1523 validator = do_validate;
1524
1525 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1526 }
1527 cmd_read = true;
1528 }
1529
1530 /* If a breakpoint was on the list more than once, we don't need to
1531 do anything. */
1532 if (b->commands != cmd)
1533 {
1534 validate_commands_for_breakpoint (b, cmd.get ());
1535 b->commands = cmd;
1536 gdb::observers::breakpoint_modified.notify (b);
1537 }
1538 });
1539 }
1540
1541 static void
1542 commands_command (const char *arg, int from_tty)
1543 {
1544 commands_command_1 (arg, from_tty, NULL);
1545 }
1546
1547 /* Like commands_command, but instead of reading the commands from
1548 input stream, takes them from an already parsed command structure.
1549
1550 This is used by cli-script.c to DTRT with breakpoint commands
1551 that are part of if and while bodies. */
1552 enum command_control_type
1553 commands_from_control_command (const char *arg, struct command_line *cmd)
1554 {
1555 commands_command_1 (arg, 0, cmd);
1556 return simple_control;
1557 }
1558
1559 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1560
1561 static int
1562 bp_location_has_shadow (struct bp_location *bl)
1563 {
1564 if (bl->loc_type != bp_loc_software_breakpoint)
1565 return 0;
1566 if (!bl->inserted)
1567 return 0;
1568 if (bl->target_info.shadow_len == 0)
1569 /* BL isn't valid, or doesn't shadow memory. */
1570 return 0;
1571 return 1;
1572 }
1573
1574 /* Update BUF, which is LEN bytes read from the target address
1575 MEMADDR, by replacing a memory breakpoint with its shadowed
1576 contents.
1577
1578 If READBUF is not NULL, this buffer must not overlap with the of
1579 the breakpoint location's shadow_contents buffer. Otherwise, a
1580 failed assertion internal error will be raised. */
1581
1582 static void
1583 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1584 const gdb_byte *writebuf_org,
1585 ULONGEST memaddr, LONGEST len,
1586 struct bp_target_info *target_info,
1587 struct gdbarch *gdbarch)
1588 {
1589 /* Now do full processing of the found relevant range of elements. */
1590 CORE_ADDR bp_addr = 0;
1591 int bp_size = 0;
1592 int bptoffset = 0;
1593
1594 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1595 current_program_space->aspace, 0))
1596 {
1597 /* The breakpoint is inserted in a different address space. */
1598 return;
1599 }
1600
1601 /* Addresses and length of the part of the breakpoint that
1602 we need to copy. */
1603 bp_addr = target_info->placed_address;
1604 bp_size = target_info->shadow_len;
1605
1606 if (bp_addr + bp_size <= memaddr)
1607 {
1608 /* The breakpoint is entirely before the chunk of memory we are
1609 reading. */
1610 return;
1611 }
1612
1613 if (bp_addr >= memaddr + len)
1614 {
1615 /* The breakpoint is entirely after the chunk of memory we are
1616 reading. */
1617 return;
1618 }
1619
1620 /* Offset within shadow_contents. */
1621 if (bp_addr < memaddr)
1622 {
1623 /* Only copy the second part of the breakpoint. */
1624 bp_size -= memaddr - bp_addr;
1625 bptoffset = memaddr - bp_addr;
1626 bp_addr = memaddr;
1627 }
1628
1629 if (bp_addr + bp_size > memaddr + len)
1630 {
1631 /* Only copy the first part of the breakpoint. */
1632 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1633 }
1634
1635 if (readbuf != NULL)
1636 {
1637 /* Verify that the readbuf buffer does not overlap with the
1638 shadow_contents buffer. */
1639 gdb_assert (target_info->shadow_contents >= readbuf + len
1640 || readbuf >= (target_info->shadow_contents
1641 + target_info->shadow_len));
1642
1643 /* Update the read buffer with this inserted breakpoint's
1644 shadow. */
1645 memcpy (readbuf + bp_addr - memaddr,
1646 target_info->shadow_contents + bptoffset, bp_size);
1647 }
1648 else
1649 {
1650 const unsigned char *bp;
1651 CORE_ADDR addr = target_info->reqstd_address;
1652 int placed_size;
1653
1654 /* Update the shadow with what we want to write to memory. */
1655 memcpy (target_info->shadow_contents + bptoffset,
1656 writebuf_org + bp_addr - memaddr, bp_size);
1657
1658 /* Determine appropriate breakpoint contents and size for this
1659 address. */
1660 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1661
1662 /* Update the final write buffer with this inserted
1663 breakpoint's INSN. */
1664 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1665 }
1666 }
1667
1668 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1669 by replacing any memory breakpoints with their shadowed contents.
1670
1671 If READBUF is not NULL, this buffer must not overlap with any of
1672 the breakpoint location's shadow_contents buffers. Otherwise,
1673 a failed assertion internal error will be raised.
1674
1675 The range of shadowed area by each bp_location is:
1676 bl->address - bp_locations_placed_address_before_address_max
1677 up to bl->address + bp_locations_shadow_len_after_address_max
1678 The range we were requested to resolve shadows for is:
1679 memaddr ... memaddr + len
1680 Thus the safe cutoff boundaries for performance optimization are
1681 memaddr + len <= (bl->address
1682 - bp_locations_placed_address_before_address_max)
1683 and:
1684 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1685
1686 void
1687 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1688 const gdb_byte *writebuf_org,
1689 ULONGEST memaddr, LONGEST len)
1690 {
1691 /* Left boundary, right boundary and median element of our binary
1692 search. */
1693 unsigned bc_l, bc_r, bc;
1694
1695 /* Find BC_L which is a leftmost element which may affect BUF
1696 content. It is safe to report lower value but a failure to
1697 report higher one. */
1698
1699 bc_l = 0;
1700 bc_r = bp_locations.size ();
1701 while (bc_l + 1 < bc_r)
1702 {
1703 struct bp_location *bl;
1704
1705 bc = (bc_l + bc_r) / 2;
1706 bl = bp_locations[bc];
1707
1708 /* Check first BL->ADDRESS will not overflow due to the added
1709 constant. Then advance the left boundary only if we are sure
1710 the BC element can in no way affect the BUF content (MEMADDR
1711 to MEMADDR + LEN range).
1712
1713 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1714 offset so that we cannot miss a breakpoint with its shadow
1715 range tail still reaching MEMADDR. */
1716
1717 if ((bl->address + bp_locations_shadow_len_after_address_max
1718 >= bl->address)
1719 && (bl->address + bp_locations_shadow_len_after_address_max
1720 <= memaddr))
1721 bc_l = bc;
1722 else
1723 bc_r = bc;
1724 }
1725
1726 /* Due to the binary search above, we need to make sure we pick the
1727 first location that's at BC_L's address. E.g., if there are
1728 multiple locations at the same address, BC_L may end up pointing
1729 at a duplicate location, and miss the "master"/"inserted"
1730 location. Say, given locations L1, L2 and L3 at addresses A and
1731 B:
1732
1733 L1@A, L2@A, L3@B, ...
1734
1735 BC_L could end up pointing at location L2, while the "master"
1736 location could be L1. Since the `loc->inserted' flag is only set
1737 on "master" locations, we'd forget to restore the shadow of L1
1738 and L2. */
1739 while (bc_l > 0
1740 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1741 bc_l--;
1742
1743 /* Now do full processing of the found relevant range of elements. */
1744
1745 for (bc = bc_l; bc < bp_locations.size (); bc++)
1746 {
1747 struct bp_location *bl = bp_locations[bc];
1748
1749 /* bp_location array has BL->OWNER always non-NULL. */
1750 if (bl->owner->type == bp_none)
1751 warning (_("reading through apparently deleted breakpoint #%d?"),
1752 bl->owner->number);
1753
1754 /* Performance optimization: any further element can no longer affect BUF
1755 content. */
1756
1757 if (bl->address >= bp_locations_placed_address_before_address_max
1758 && (memaddr + len
1759 <= (bl->address
1760 - bp_locations_placed_address_before_address_max)))
1761 break;
1762
1763 if (!bp_location_has_shadow (bl))
1764 continue;
1765
1766 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1767 memaddr, len, &bl->target_info, bl->gdbarch);
1768 }
1769 }
1770
1771 /* See breakpoint.h. */
1772
1773 bool
1774 is_breakpoint (const struct breakpoint *bpt)
1775 {
1776 return (bpt->type == bp_breakpoint
1777 || bpt->type == bp_hardware_breakpoint
1778 || bpt->type == bp_dprintf);
1779 }
1780
1781 /* Return true if BPT is of any hardware watchpoint kind. */
1782
1783 static bool
1784 is_hardware_watchpoint (const struct breakpoint *bpt)
1785 {
1786 return (bpt->type == bp_hardware_watchpoint
1787 || bpt->type == bp_read_watchpoint
1788 || bpt->type == bp_access_watchpoint);
1789 }
1790
1791 /* See breakpoint.h. */
1792
1793 bool
1794 is_watchpoint (const struct breakpoint *bpt)
1795 {
1796 return (is_hardware_watchpoint (bpt)
1797 || bpt->type == bp_watchpoint);
1798 }
1799
1800 /* Returns true if the current thread and its running state are safe
1801 to evaluate or update watchpoint B. Watchpoints on local
1802 expressions need to be evaluated in the context of the thread that
1803 was current when the watchpoint was created, and, that thread needs
1804 to be stopped to be able to select the correct frame context.
1805 Watchpoints on global expressions can be evaluated on any thread,
1806 and in any state. It is presently left to the target allowing
1807 memory accesses when threads are running. */
1808
1809 static int
1810 watchpoint_in_thread_scope (struct watchpoint *b)
1811 {
1812 return (b->pspace == current_program_space
1813 && (b->watchpoint_thread == null_ptid
1814 || (inferior_ptid == b->watchpoint_thread
1815 && !inferior_thread ()->executing ())));
1816 }
1817
1818 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1819 associated bp_watchpoint_scope breakpoint. */
1820
1821 static void
1822 watchpoint_del_at_next_stop (struct watchpoint *w)
1823 {
1824 if (w->related_breakpoint != w)
1825 {
1826 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1827 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1828 w->related_breakpoint->disposition = disp_del_at_next_stop;
1829 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1830 w->related_breakpoint = w;
1831 }
1832 w->disposition = disp_del_at_next_stop;
1833 }
1834
1835 /* Extract a bitfield value from value VAL using the bit parameters contained in
1836 watchpoint W. */
1837
1838 static struct value *
1839 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1840 {
1841 struct value *bit_val;
1842
1843 if (val == NULL)
1844 return NULL;
1845
1846 bit_val = allocate_value (value_type (val));
1847
1848 unpack_value_bitfield (bit_val,
1849 w->val_bitpos,
1850 w->val_bitsize,
1851 value_contents_for_printing (val).data (),
1852 value_offset (val),
1853 val);
1854
1855 return bit_val;
1856 }
1857
1858 /* Allocate a dummy location and add it to B. This is required
1859 because bpstat_stop_status requires a location to be able to report
1860 stops. */
1861
1862 static void
1863 add_dummy_location (struct breakpoint *b,
1864 struct program_space *pspace)
1865 {
1866 gdb_assert (b->loc == NULL);
1867
1868 b->loc = new bp_location (b, bp_loc_other);
1869 b->loc->pspace = pspace;
1870 }
1871
1872 /* Assuming that B is a watchpoint:
1873 - Reparse watchpoint expression, if REPARSE is non-zero
1874 - Evaluate expression and store the result in B->val
1875 - Evaluate the condition if there is one, and store the result
1876 in b->loc->cond.
1877 - Update the list of values that must be watched in B->loc.
1878
1879 If the watchpoint disposition is disp_del_at_next_stop, then do
1880 nothing. If this is local watchpoint that is out of scope, delete
1881 it.
1882
1883 Even with `set breakpoint always-inserted on' the watchpoints are
1884 removed + inserted on each stop here. Normal breakpoints must
1885 never be removed because they might be missed by a running thread
1886 when debugging in non-stop mode. On the other hand, hardware
1887 watchpoints (is_hardware_watchpoint; processed here) are specific
1888 to each LWP since they are stored in each LWP's hardware debug
1889 registers. Therefore, such LWP must be stopped first in order to
1890 be able to modify its hardware watchpoints.
1891
1892 Hardware watchpoints must be reset exactly once after being
1893 presented to the user. It cannot be done sooner, because it would
1894 reset the data used to present the watchpoint hit to the user. And
1895 it must not be done later because it could display the same single
1896 watchpoint hit during multiple GDB stops. Note that the latter is
1897 relevant only to the hardware watchpoint types bp_read_watchpoint
1898 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1899 not user-visible - its hit is suppressed if the memory content has
1900 not changed.
1901
1902 The following constraints influence the location where we can reset
1903 hardware watchpoints:
1904
1905 * target_stopped_by_watchpoint and target_stopped_data_address are
1906 called several times when GDB stops.
1907
1908 [linux]
1909 * Multiple hardware watchpoints can be hit at the same time,
1910 causing GDB to stop. GDB only presents one hardware watchpoint
1911 hit at a time as the reason for stopping, and all the other hits
1912 are presented later, one after the other, each time the user
1913 requests the execution to be resumed. Execution is not resumed
1914 for the threads still having pending hit event stored in
1915 LWP_INFO->STATUS. While the watchpoint is already removed from
1916 the inferior on the first stop the thread hit event is kept being
1917 reported from its cached value by linux_nat_stopped_data_address
1918 until the real thread resume happens after the watchpoint gets
1919 presented and thus its LWP_INFO->STATUS gets reset.
1920
1921 Therefore the hardware watchpoint hit can get safely reset on the
1922 watchpoint removal from inferior. */
1923
1924 static void
1925 update_watchpoint (struct watchpoint *b, int reparse)
1926 {
1927 int within_current_scope;
1928 struct frame_id saved_frame_id;
1929 int frame_saved;
1930
1931 /* If this is a local watchpoint, we only want to check if the
1932 watchpoint frame is in scope if the current thread is the thread
1933 that was used to create the watchpoint. */
1934 if (!watchpoint_in_thread_scope (b))
1935 return;
1936
1937 if (b->disposition == disp_del_at_next_stop)
1938 return;
1939
1940 frame_saved = 0;
1941
1942 /* Determine if the watchpoint is within scope. */
1943 if (b->exp_valid_block == NULL)
1944 within_current_scope = 1;
1945 else
1946 {
1947 frame_info_ptr fi = get_current_frame ();
1948 struct gdbarch *frame_arch = get_frame_arch (fi);
1949 CORE_ADDR frame_pc = get_frame_pc (fi);
1950
1951 /* If we're at a point where the stack has been destroyed
1952 (e.g. in a function epilogue), unwinding may not work
1953 properly. Do not attempt to recreate locations at this
1954 point. See similar comments in watchpoint_check. */
1955 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1956 return;
1957
1958 /* Save the current frame's ID so we can restore it after
1959 evaluating the watchpoint expression on its own frame. */
1960 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1961 took a frame parameter, so that we didn't have to change the
1962 selected frame. */
1963 frame_saved = 1;
1964 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1965
1966 fi = frame_find_by_id (b->watchpoint_frame);
1967 within_current_scope = (fi != NULL);
1968 if (within_current_scope)
1969 select_frame (fi);
1970 }
1971
1972 /* We don't free locations. They are stored in the bp_location array
1973 and update_global_location_list will eventually delete them and
1974 remove breakpoints if needed. */
1975 b->loc = NULL;
1976
1977 if (within_current_scope && reparse)
1978 {
1979 const char *s;
1980
1981 b->exp.reset ();
1982 s = (b->exp_string_reparse
1983 ? b->exp_string_reparse.get ()
1984 : b->exp_string.get ());
1985 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1986 /* If the meaning of expression itself changed, the old value is
1987 no longer relevant. We don't want to report a watchpoint hit
1988 to the user when the old value and the new value may actually
1989 be completely different objects. */
1990 b->val = NULL;
1991 b->val_valid = false;
1992
1993 /* Note that unlike with breakpoints, the watchpoint's condition
1994 expression is stored in the breakpoint object, not in the
1995 locations (re)created below. */
1996 if (b->cond_string != NULL)
1997 {
1998 b->cond_exp.reset ();
1999
2000 s = b->cond_string.get ();
2001 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
2002 }
2003 }
2004
2005 /* If we failed to parse the expression, for example because
2006 it refers to a global variable in a not-yet-loaded shared library,
2007 don't try to insert watchpoint. We don't automatically delete
2008 such watchpoint, though, since failure to parse expression
2009 is different from out-of-scope watchpoint. */
2010 if (!target_has_execution ())
2011 {
2012 /* Without execution, memory can't change. No use to try and
2013 set watchpoint locations. The watchpoint will be reset when
2014 the target gains execution, through breakpoint_re_set. */
2015 if (!can_use_hw_watchpoints)
2016 {
2017 if (b->works_in_software_mode ())
2018 b->type = bp_watchpoint;
2019 else
2020 error (_("Can't set read/access watchpoint when "
2021 "hardware watchpoints are disabled."));
2022 }
2023 }
2024 else if (within_current_scope && b->exp)
2025 {
2026 std::vector<value_ref_ptr> val_chain;
2027 struct value *v, *result;
2028 struct program_space *frame_pspace;
2029
2030 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
2031 &val_chain, false);
2032
2033 /* Avoid setting b->val if it's already set. The meaning of
2034 b->val is 'the last value' user saw, and we should update
2035 it only if we reported that last value to user. As it
2036 happens, the code that reports it updates b->val directly.
2037 We don't keep track of the memory value for masked
2038 watchpoints. */
2039 if (!b->val_valid && !is_masked_watchpoint (b))
2040 {
2041 if (b->val_bitsize != 0)
2042 v = extract_bitfield_from_watchpoint_value (b, v);
2043 b->val = release_value (v);
2044 b->val_valid = true;
2045 }
2046
2047 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
2048
2049 /* Look at each value on the value chain. */
2050 gdb_assert (!val_chain.empty ());
2051 for (const value_ref_ptr &iter : val_chain)
2052 {
2053 v = iter.get ();
2054
2055 /* If it's a memory location, and GDB actually needed
2056 its contents to evaluate the expression, then we
2057 must watch it. If the first value returned is
2058 still lazy, that means an error occurred reading it;
2059 watch it anyway in case it becomes readable. */
2060 if (VALUE_LVAL (v) == lval_memory
2061 && (v == val_chain[0] || ! value_lazy (v)))
2062 {
2063 struct type *vtype = check_typedef (value_type (v));
2064
2065 /* We only watch structs and arrays if user asked
2066 for it explicitly, never if they just happen to
2067 appear in the middle of some value chain. */
2068 if (v == result
2069 || (vtype->code () != TYPE_CODE_STRUCT
2070 && vtype->code () != TYPE_CODE_ARRAY))
2071 {
2072 CORE_ADDR addr;
2073 enum target_hw_bp_type type;
2074 struct bp_location *loc, **tmp;
2075 int bitpos = 0, bitsize = 0;
2076
2077 if (value_bitsize (v) != 0)
2078 {
2079 /* Extract the bit parameters out from the bitfield
2080 sub-expression. */
2081 bitpos = value_bitpos (v);
2082 bitsize = value_bitsize (v);
2083 }
2084 else if (v == result && b->val_bitsize != 0)
2085 {
2086 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2087 lvalue whose bit parameters are saved in the fields
2088 VAL_BITPOS and VAL_BITSIZE. */
2089 bitpos = b->val_bitpos;
2090 bitsize = b->val_bitsize;
2091 }
2092
2093 addr = value_address (v);
2094 if (bitsize != 0)
2095 {
2096 /* Skip the bytes that don't contain the bitfield. */
2097 addr += bitpos / 8;
2098 }
2099
2100 type = hw_write;
2101 if (b->type == bp_read_watchpoint)
2102 type = hw_read;
2103 else if (b->type == bp_access_watchpoint)
2104 type = hw_access;
2105
2106 loc = b->allocate_location ();
2107 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2108 ;
2109 *tmp = loc;
2110 loc->gdbarch = value_type (v)->arch ();
2111
2112 loc->pspace = frame_pspace;
2113 loc->address = address_significant (loc->gdbarch, addr);
2114
2115 if (bitsize != 0)
2116 {
2117 /* Just cover the bytes that make up the bitfield. */
2118 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2119 }
2120 else
2121 loc->length = value_type (v)->length ();
2122
2123 loc->watchpoint_type = type;
2124 }
2125 }
2126 }
2127
2128 /* Change the type of breakpoint between hardware assisted or
2129 an ordinary watchpoint depending on the hardware support
2130 and free hardware slots. REPARSE is set when the inferior
2131 is started. */
2132 if (reparse)
2133 {
2134 int reg_cnt;
2135 enum bp_loc_type loc_type;
2136
2137 reg_cnt = can_use_hardware_watchpoint (val_chain);
2138
2139 if (reg_cnt)
2140 {
2141 int i, target_resources_ok, other_type_used;
2142 enum bptype type;
2143
2144 /* Use an exact watchpoint when there's only one memory region to be
2145 watched, and only one debug register is needed to watch it. */
2146 b->exact = target_exact_watchpoints && reg_cnt == 1;
2147
2148 /* We need to determine how many resources are already
2149 used for all other hardware watchpoints plus this one
2150 to see if we still have enough resources to also fit
2151 this watchpoint in as well. */
2152
2153 /* If this is a software watchpoint, we try to turn it
2154 to a hardware one -- count resources as if B was of
2155 hardware watchpoint type. */
2156 type = b->type;
2157 if (type == bp_watchpoint)
2158 type = bp_hardware_watchpoint;
2159
2160 /* This watchpoint may or may not have been placed on
2161 the list yet at this point (it won't be in the list
2162 if we're trying to create it for the first time,
2163 through watch_command), so always account for it
2164 manually. */
2165
2166 /* Count resources used by all watchpoints except B. */
2167 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2168
2169 /* Add in the resources needed for B. */
2170 i += hw_watchpoint_use_count (b);
2171
2172 target_resources_ok
2173 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2174 if (target_resources_ok <= 0)
2175 {
2176 bool sw_mode = b->works_in_software_mode ();
2177
2178 if (target_resources_ok == 0 && !sw_mode)
2179 error (_("Target does not support this type of "
2180 "hardware watchpoint."));
2181 else if (target_resources_ok < 0 && !sw_mode)
2182 error (_("There are not enough available hardware "
2183 "resources for this watchpoint."));
2184
2185 /* Downgrade to software watchpoint. */
2186 b->type = bp_watchpoint;
2187 }
2188 else
2189 {
2190 /* If this was a software watchpoint, we've just
2191 found we have enough resources to turn it to a
2192 hardware watchpoint. Otherwise, this is a
2193 nop. */
2194 b->type = type;
2195 }
2196 }
2197 else if (!b->works_in_software_mode ())
2198 {
2199 if (!can_use_hw_watchpoints)
2200 error (_("Can't set read/access watchpoint when "
2201 "hardware watchpoints are disabled."));
2202 else
2203 error (_("Expression cannot be implemented with "
2204 "read/access watchpoint."));
2205 }
2206 else
2207 b->type = bp_watchpoint;
2208
2209 loc_type = (b->type == bp_watchpoint? bp_loc_software_watchpoint
2210 : bp_loc_hardware_watchpoint);
2211 for (bp_location *bl : b->locations ())
2212 bl->loc_type = loc_type;
2213 }
2214
2215 /* If a software watchpoint is not watching any memory, then the
2216 above left it without any location set up. But,
2217 bpstat_stop_status requires a location to be able to report
2218 stops, so make sure there's at least a dummy one. */
2219 if (b->type == bp_watchpoint && b->loc == NULL)
2220 add_dummy_location (b, frame_pspace);
2221 }
2222 else if (!within_current_scope)
2223 {
2224 gdb_printf (_("\
2225 Watchpoint %d deleted because the program has left the block\n\
2226 in which its expression is valid.\n"),
2227 b->number);
2228 watchpoint_del_at_next_stop (b);
2229 }
2230
2231 /* Restore the selected frame. */
2232 if (frame_saved)
2233 select_frame (frame_find_by_id (saved_frame_id));
2234 }
2235
2236
2237 /* Returns 1 iff breakpoint location should be
2238 inserted in the inferior. We don't differentiate the type of BL's owner
2239 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2240 breakpoint_ops is not defined, because in insert_bp_location,
2241 tracepoint's insert_location will not be called. */
2242 static int
2243 should_be_inserted (struct bp_location *bl)
2244 {
2245 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2246 return 0;
2247
2248 if (bl->owner->disposition == disp_del_at_next_stop)
2249 return 0;
2250
2251 if (!bl->enabled || bl->disabled_by_cond
2252 || bl->shlib_disabled || bl->duplicate)
2253 return 0;
2254
2255 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2256 return 0;
2257
2258 /* This is set for example, when we're attached to the parent of a
2259 vfork, and have detached from the child. The child is running
2260 free, and we expect it to do an exec or exit, at which point the
2261 OS makes the parent schedulable again (and the target reports
2262 that the vfork is done). Until the child is done with the shared
2263 memory region, do not insert breakpoints in the parent, otherwise
2264 the child could still trip on the parent's breakpoints. Since
2265 the parent is blocked anyway, it won't miss any breakpoint. */
2266 if (bl->pspace->breakpoints_not_allowed)
2267 return 0;
2268
2269 /* Don't insert a breakpoint if we're trying to step past its
2270 location, except if the breakpoint is a single-step breakpoint,
2271 and the breakpoint's thread is the thread which is stepping past
2272 a breakpoint. */
2273 if ((bl->loc_type == bp_loc_software_breakpoint
2274 || bl->loc_type == bp_loc_hardware_breakpoint)
2275 && stepping_past_instruction_at (bl->pspace->aspace,
2276 bl->address)
2277 /* The single-step breakpoint may be inserted at the location
2278 we're trying to step if the instruction branches to itself.
2279 However, the instruction won't be executed at all and it may
2280 break the semantics of the instruction, for example, the
2281 instruction is a conditional branch or updates some flags.
2282 We can't fix it unless GDB is able to emulate the instruction
2283 or switch to displaced stepping. */
2284 && !(bl->owner->type == bp_single_step
2285 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2286 {
2287 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2288 paddress (bl->gdbarch, bl->address));
2289 return 0;
2290 }
2291
2292 /* Don't insert watchpoints if we're trying to step past the
2293 instruction that triggered one. */
2294 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2295 && stepping_past_nonsteppable_watchpoint ())
2296 {
2297 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2298 "skipping watchpoint at %s:%d",
2299 paddress (bl->gdbarch, bl->address), bl->length);
2300 return 0;
2301 }
2302
2303 return 1;
2304 }
2305
2306 /* Same as should_be_inserted but does the check assuming
2307 that the location is not duplicated. */
2308
2309 static int
2310 unduplicated_should_be_inserted (struct bp_location *bl)
2311 {
2312 int result;
2313 const int save_duplicate = bl->duplicate;
2314
2315 bl->duplicate = 0;
2316 result = should_be_inserted (bl);
2317 bl->duplicate = save_duplicate;
2318 return result;
2319 }
2320
2321 /* Parses a conditional described by an expression COND into an
2322 agent expression bytecode suitable for evaluation
2323 by the bytecode interpreter. Return NULL if there was
2324 any error during parsing. */
2325
2326 static agent_expr_up
2327 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2328 {
2329 if (cond == NULL)
2330 return NULL;
2331
2332 agent_expr_up aexpr;
2333
2334 /* We don't want to stop processing, so catch any errors
2335 that may show up. */
2336 try
2337 {
2338 aexpr = gen_eval_for_expr (scope, cond);
2339 }
2340
2341 catch (const gdb_exception_error &ex)
2342 {
2343 /* If we got here, it means the condition could not be parsed to a valid
2344 bytecode expression and thus can't be evaluated on the target's side.
2345 It's no use iterating through the conditions. */
2346 }
2347
2348 /* We have a valid agent expression. */
2349 return aexpr;
2350 }
2351
2352 /* Based on location BL, create a list of breakpoint conditions to be
2353 passed on to the target. If we have duplicated locations with different
2354 conditions, we will add such conditions to the list. The idea is that the
2355 target will evaluate the list of conditions and will only notify GDB when
2356 one of them is true. */
2357
2358 static void
2359 build_target_condition_list (struct bp_location *bl)
2360 {
2361 int null_condition_or_parse_error = 0;
2362 int modified = bl->needs_update;
2363
2364 /* Release conditions left over from a previous insert. */
2365 bl->target_info.conditions.clear ();
2366
2367 /* This is only meaningful if the target is
2368 evaluating conditions and if the user has
2369 opted for condition evaluation on the target's
2370 side. */
2371 if (gdb_evaluates_breakpoint_condition_p ()
2372 || !target_supports_evaluation_of_breakpoint_conditions ())
2373 return;
2374
2375 auto loc_range = all_bp_locations_at_addr (bl->address);
2376
2377 /* Do a first pass to check for locations with no assigned
2378 conditions or conditions that fail to parse to a valid agent
2379 expression bytecode. If any of these happen, then it's no use to
2380 send conditions to the target since this location will always
2381 trigger and generate a response back to GDB. Note we consider
2382 all locations at the same address irrespective of type, i.e.,
2383 even if the locations aren't considered duplicates (e.g.,
2384 software breakpoint and hardware breakpoint at the same
2385 address). */
2386 for (bp_location *loc : loc_range)
2387 {
2388 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2389 {
2390 if (modified)
2391 {
2392 /* Re-parse the conditions since something changed. In that
2393 case we already freed the condition bytecodes (see
2394 force_breakpoint_reinsertion). We just
2395 need to parse the condition to bytecodes again. */
2396 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2397 loc->cond.get ());
2398 }
2399
2400 /* If we have a NULL bytecode expression, it means something
2401 went wrong or we have a null condition expression. */
2402 if (!loc->cond_bytecode)
2403 {
2404 null_condition_or_parse_error = 1;
2405 break;
2406 }
2407 }
2408 }
2409
2410 /* If any of these happened, it means we will have to evaluate the conditions
2411 for the location's address on gdb's side. It is no use keeping bytecodes
2412 for all the other duplicate locations, thus we free all of them here.
2413
2414 This is so we have a finer control over which locations' conditions are
2415 being evaluated by GDB or the remote stub. */
2416 if (null_condition_or_parse_error)
2417 {
2418 for (bp_location *loc : loc_range)
2419 {
2420 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2421 {
2422 /* Only go as far as the first NULL bytecode is
2423 located. */
2424 if (!loc->cond_bytecode)
2425 return;
2426
2427 loc->cond_bytecode.reset ();
2428 }
2429 }
2430 }
2431
2432 /* No NULL conditions or failed bytecode generation. Build a
2433 condition list for this location's address. If we have software
2434 and hardware locations at the same address, they aren't
2435 considered duplicates, but we still marge all the conditions
2436 anyway, as it's simpler, and doesn't really make a practical
2437 difference. */
2438 for (bp_location *loc : loc_range)
2439 if (loc->cond
2440 && is_breakpoint (loc->owner)
2441 && loc->pspace->num == bl->pspace->num
2442 && loc->owner->enable_state == bp_enabled
2443 && loc->enabled
2444 && !loc->disabled_by_cond)
2445 {
2446 /* Add the condition to the vector. This will be used later
2447 to send the conditions to the target. */
2448 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2449 }
2450
2451 return;
2452 }
2453
2454 /* Parses a command described by string CMD into an agent expression
2455 bytecode suitable for evaluation by the bytecode interpreter.
2456 Return NULL if there was any error during parsing. */
2457
2458 static agent_expr_up
2459 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2460 {
2461 const char *cmdrest;
2462 const char *format_start, *format_end;
2463 struct gdbarch *gdbarch = get_current_arch ();
2464
2465 if (cmd == NULL)
2466 return NULL;
2467
2468 cmdrest = cmd;
2469
2470 if (*cmdrest == ',')
2471 ++cmdrest;
2472 cmdrest = skip_spaces (cmdrest);
2473
2474 if (*cmdrest++ != '"')
2475 error (_("No format string following the location"));
2476
2477 format_start = cmdrest;
2478
2479 format_pieces fpieces (&cmdrest);
2480
2481 format_end = cmdrest;
2482
2483 if (*cmdrest++ != '"')
2484 error (_("Bad format string, non-terminated '\"'."));
2485
2486 cmdrest = skip_spaces (cmdrest);
2487
2488 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2489 error (_("Invalid argument syntax"));
2490
2491 if (*cmdrest == ',')
2492 cmdrest++;
2493 cmdrest = skip_spaces (cmdrest);
2494
2495 /* For each argument, make an expression. */
2496
2497 std::vector<struct expression *> argvec;
2498 while (*cmdrest != '\0')
2499 {
2500 const char *cmd1;
2501
2502 cmd1 = cmdrest;
2503 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2504 argvec.push_back (expr.release ());
2505 cmdrest = cmd1;
2506 if (*cmdrest == ',')
2507 ++cmdrest;
2508 }
2509
2510 agent_expr_up aexpr;
2511
2512 /* We don't want to stop processing, so catch any errors
2513 that may show up. */
2514 try
2515 {
2516 aexpr = gen_printf (scope, gdbarch, 0, 0,
2517 format_start, format_end - format_start,
2518 argvec.size (), argvec.data ());
2519 }
2520 catch (const gdb_exception_error &ex)
2521 {
2522 /* If we got here, it means the command could not be parsed to a valid
2523 bytecode expression and thus can't be evaluated on the target's side.
2524 It's no use iterating through the other commands. */
2525 }
2526
2527 /* We have a valid agent expression, return it. */
2528 return aexpr;
2529 }
2530
2531 /* Based on location BL, create a list of breakpoint commands to be
2532 passed on to the target. If we have duplicated locations with
2533 different commands, we will add any such to the list. */
2534
2535 static void
2536 build_target_command_list (struct bp_location *bl)
2537 {
2538 int null_command_or_parse_error = 0;
2539 int modified = bl->needs_update;
2540
2541 /* Clear commands left over from a previous insert. */
2542 bl->target_info.tcommands.clear ();
2543
2544 if (!target_can_run_breakpoint_commands ())
2545 return;
2546
2547 /* For now, limit to agent-style dprintf breakpoints. */
2548 if (dprintf_style != dprintf_style_agent)
2549 return;
2550
2551 auto loc_range = all_bp_locations_at_addr (bl->address);
2552
2553 /* For now, if we have any location at the same address that isn't a
2554 dprintf, don't install the target-side commands, as that would
2555 make the breakpoint not be reported to the core, and we'd lose
2556 control. */
2557 for (bp_location *loc : loc_range)
2558 if (is_breakpoint (loc->owner)
2559 && loc->pspace->num == bl->pspace->num
2560 && loc->owner->type != bp_dprintf)
2561 return;
2562
2563 /* Do a first pass to check for locations with no assigned
2564 conditions or conditions that fail to parse to a valid agent expression
2565 bytecode. If any of these happen, then it's no use to send conditions
2566 to the target since this location will always trigger and generate a
2567 response back to GDB. */
2568 for (bp_location *loc : loc_range)
2569 {
2570 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2571 {
2572 if (modified)
2573 {
2574 /* Re-parse the commands since something changed. In that
2575 case we already freed the command bytecodes (see
2576 force_breakpoint_reinsertion). We just
2577 need to parse the command to bytecodes again. */
2578 loc->cmd_bytecode
2579 = parse_cmd_to_aexpr (bl->address,
2580 loc->owner->extra_string.get ());
2581 }
2582
2583 /* If we have a NULL bytecode expression, it means something
2584 went wrong or we have a null command expression. */
2585 if (!loc->cmd_bytecode)
2586 {
2587 null_command_or_parse_error = 1;
2588 break;
2589 }
2590 }
2591 }
2592
2593 /* If anything failed, then we're not doing target-side commands,
2594 and so clean up. */
2595 if (null_command_or_parse_error)
2596 {
2597 for (bp_location *loc : loc_range)
2598 if (is_breakpoint (loc->owner)
2599 && loc->pspace->num == bl->pspace->num)
2600 {
2601 /* Only go as far as the first NULL bytecode is
2602 located. */
2603 if (loc->cmd_bytecode == NULL)
2604 return;
2605
2606 loc->cmd_bytecode.reset ();
2607 }
2608 }
2609
2610 /* No NULL commands or failed bytecode generation. Build a command
2611 list for all duplicate locations at this location's address.
2612 Note that here we must care for whether the breakpoint location
2613 types are considered duplicates, otherwise, say, if we have a
2614 software and hardware location at the same address, the target
2615 could end up running the commands twice. For the moment, we only
2616 support targets-side commands with dprintf, but it doesn't hurt
2617 to be pedantically correct in case that changes. */
2618 for (bp_location *loc : loc_range)
2619 if (breakpoint_locations_match (bl, loc)
2620 && loc->owner->extra_string
2621 && is_breakpoint (loc->owner)
2622 && loc->pspace->num == bl->pspace->num
2623 && loc->owner->enable_state == bp_enabled
2624 && loc->enabled
2625 && !loc->disabled_by_cond)
2626 {
2627 /* Add the command to the vector. This will be used later
2628 to send the commands to the target. */
2629 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2630 }
2631
2632 bl->target_info.persist = 0;
2633 /* Maybe flag this location as persistent. */
2634 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2635 bl->target_info.persist = 1;
2636 }
2637
2638 /* Return the kind of breakpoint on address *ADDR. Get the kind
2639 of breakpoint according to ADDR except single-step breakpoint.
2640 Get the kind of single-step breakpoint according to the current
2641 registers state. */
2642
2643 static int
2644 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2645 {
2646 if (bl->owner->type == bp_single_step)
2647 {
2648 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2649 struct regcache *regcache;
2650
2651 regcache = get_thread_regcache (thr);
2652
2653 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2654 regcache, addr);
2655 }
2656 else
2657 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2658 }
2659
2660 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2661 location. Any error messages are printed to TMP_ERROR_STREAM; and
2662 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2663 Returns 0 for success, 1 if the bp_location type is not supported or
2664 -1 for failure.
2665
2666 NOTE drow/2003-09-09: This routine could be broken down to an
2667 object-style method for each breakpoint or catchpoint type. */
2668 static int
2669 insert_bp_location (struct bp_location *bl,
2670 struct ui_file *tmp_error_stream,
2671 int *disabled_breaks,
2672 int *hw_breakpoint_error,
2673 int *hw_bp_error_explained_already)
2674 {
2675 gdb_exception bp_excpt;
2676
2677 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2678 return 0;
2679
2680 /* Note we don't initialize bl->target_info, as that wipes out
2681 the breakpoint location's shadow_contents if the breakpoint
2682 is still inserted at that location. This in turn breaks
2683 target_read_memory which depends on these buffers when
2684 a memory read is requested at the breakpoint location:
2685 Once the target_info has been wiped, we fail to see that
2686 we have a breakpoint inserted at that address and thus
2687 read the breakpoint instead of returning the data saved in
2688 the breakpoint location's shadow contents. */
2689 bl->target_info.reqstd_address = bl->address;
2690 bl->target_info.placed_address_space = bl->pspace->aspace;
2691 bl->target_info.length = bl->length;
2692
2693 /* When working with target-side conditions, we must pass all the conditions
2694 for the same breakpoint address down to the target since GDB will not
2695 insert those locations. With a list of breakpoint conditions, the target
2696 can decide when to stop and notify GDB. */
2697
2698 if (is_breakpoint (bl->owner))
2699 {
2700 build_target_condition_list (bl);
2701 build_target_command_list (bl);
2702 /* Reset the modification marker. */
2703 bl->needs_update = 0;
2704 }
2705
2706 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2707 set at a read-only address, then a breakpoint location will have
2708 been changed to hardware breakpoint before we get here. If it is
2709 "off" however, error out before actually trying to insert the
2710 breakpoint, with a nicer error message. */
2711 if (bl->loc_type == bp_loc_software_breakpoint
2712 && !automatic_hardware_breakpoints)
2713 {
2714 mem_region *mr = lookup_mem_region (bl->address);
2715
2716 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2717 {
2718 gdb_printf (tmp_error_stream,
2719 _("Cannot insert breakpoint %d.\n"
2720 "Cannot set software breakpoint "
2721 "at read-only address %s\n"),
2722 bl->owner->number,
2723 paddress (bl->gdbarch, bl->address));
2724 return 1;
2725 }
2726 }
2727
2728 if (bl->loc_type == bp_loc_software_breakpoint
2729 || bl->loc_type == bp_loc_hardware_breakpoint)
2730 {
2731 /* First check to see if we have to handle an overlay. */
2732 if (overlay_debugging == ovly_off
2733 || bl->section == NULL
2734 || !(section_is_overlay (bl->section)))
2735 {
2736 /* No overlay handling: just set the breakpoint. */
2737 try
2738 {
2739 int val;
2740
2741 val = bl->owner->insert_location (bl);
2742 if (val)
2743 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2744 }
2745 catch (gdb_exception &e)
2746 {
2747 bp_excpt = std::move (e);
2748 }
2749 }
2750 else
2751 {
2752 /* This breakpoint is in an overlay section.
2753 Shall we set a breakpoint at the LMA? */
2754 if (!overlay_events_enabled)
2755 {
2756 /* Yes -- overlay event support is not active,
2757 so we must try to set a breakpoint at the LMA.
2758 This will not work for a hardware breakpoint. */
2759 if (bl->loc_type == bp_loc_hardware_breakpoint)
2760 warning (_("hardware breakpoint %d not supported in overlay!"),
2761 bl->owner->number);
2762 else
2763 {
2764 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2765 bl->section);
2766 /* Set a software (trap) breakpoint at the LMA. */
2767 bl->overlay_target_info = bl->target_info;
2768 bl->overlay_target_info.reqstd_address = addr;
2769
2770 /* No overlay handling: just set the breakpoint. */
2771 try
2772 {
2773 int val;
2774
2775 bl->overlay_target_info.kind
2776 = breakpoint_kind (bl, &addr);
2777 bl->overlay_target_info.placed_address = addr;
2778 val = target_insert_breakpoint (bl->gdbarch,
2779 &bl->overlay_target_info);
2780 if (val)
2781 bp_excpt
2782 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2783 }
2784 catch (gdb_exception &e)
2785 {
2786 bp_excpt = std::move (e);
2787 }
2788
2789 if (bp_excpt.reason != 0)
2790 gdb_printf (tmp_error_stream,
2791 "Overlay breakpoint %d "
2792 "failed: in ROM?\n",
2793 bl->owner->number);
2794 }
2795 }
2796 /* Shall we set a breakpoint at the VMA? */
2797 if (section_is_mapped (bl->section))
2798 {
2799 /* Yes. This overlay section is mapped into memory. */
2800 try
2801 {
2802 int val;
2803
2804 val = bl->owner->insert_location (bl);
2805 if (val)
2806 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2807 }
2808 catch (gdb_exception &e)
2809 {
2810 bp_excpt = std::move (e);
2811 }
2812 }
2813 else
2814 {
2815 /* No. This breakpoint will not be inserted.
2816 No error, but do not mark the bp as 'inserted'. */
2817 return 0;
2818 }
2819 }
2820
2821 if (bp_excpt.reason != 0)
2822 {
2823 /* Can't set the breakpoint. */
2824
2825 /* If the target has closed then it will have deleted any
2826 breakpoints inserted within the target inferior, as a result
2827 any further attempts to interact with the breakpoint objects
2828 is not possible. Just rethrow the error. */
2829 if (bp_excpt.error == TARGET_CLOSE_ERROR)
2830 throw bp_excpt;
2831 gdb_assert (bl->owner != nullptr);
2832
2833 /* In some cases, we might not be able to insert a
2834 breakpoint in a shared library that has already been
2835 removed, but we have not yet processed the shlib unload
2836 event. Unfortunately, some targets that implement
2837 breakpoint insertion themselves can't tell why the
2838 breakpoint insertion failed (e.g., the remote target
2839 doesn't define error codes), so we must treat generic
2840 errors as memory errors. */
2841 if (bp_excpt.reason == RETURN_ERROR
2842 && (bp_excpt.error == GENERIC_ERROR
2843 || bp_excpt.error == MEMORY_ERROR)
2844 && bl->loc_type == bp_loc_software_breakpoint
2845 && (solib_name_from_address (bl->pspace, bl->address)
2846 || shared_objfile_contains_address_p (bl->pspace,
2847 bl->address)))
2848 {
2849 /* See also: disable_breakpoints_in_shlibs. */
2850 bl->shlib_disabled = 1;
2851 gdb::observers::breakpoint_modified.notify (bl->owner);
2852 if (!*disabled_breaks)
2853 {
2854 gdb_printf (tmp_error_stream,
2855 "Cannot insert breakpoint %d.\n",
2856 bl->owner->number);
2857 gdb_printf (tmp_error_stream,
2858 "Temporarily disabling shared "
2859 "library breakpoints:\n");
2860 }
2861 *disabled_breaks = 1;
2862 gdb_printf (tmp_error_stream,
2863 "breakpoint #%d\n", bl->owner->number);
2864 return 0;
2865 }
2866 else
2867 {
2868 if (bl->loc_type == bp_loc_hardware_breakpoint)
2869 {
2870 *hw_breakpoint_error = 1;
2871 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2872 gdb_printf (tmp_error_stream,
2873 "Cannot insert hardware breakpoint %d%s",
2874 bl->owner->number,
2875 bp_excpt.message ? ":" : ".\n");
2876 if (bp_excpt.message != NULL)
2877 gdb_printf (tmp_error_stream, "%s.\n",
2878 bp_excpt.what ());
2879 }
2880 else
2881 {
2882 if (bp_excpt.message == NULL)
2883 {
2884 std::string message
2885 = memory_error_message (TARGET_XFER_E_IO,
2886 bl->gdbarch, bl->address);
2887
2888 gdb_printf (tmp_error_stream,
2889 "Cannot insert breakpoint %d.\n"
2890 "%s\n",
2891 bl->owner->number, message.c_str ());
2892 }
2893 else
2894 {
2895 gdb_printf (tmp_error_stream,
2896 "Cannot insert breakpoint %d: %s\n",
2897 bl->owner->number,
2898 bp_excpt.what ());
2899 }
2900 }
2901 return 1;
2902
2903 }
2904 }
2905 else
2906 bl->inserted = 1;
2907
2908 return 0;
2909 }
2910
2911 else if (bl->loc_type == bp_loc_hardware_watchpoint
2912 /* NOTE drow/2003-09-08: This state only exists for removing
2913 watchpoints. It's not clear that it's necessary... */
2914 && bl->owner->disposition != disp_del_at_next_stop)
2915 {
2916 int val;
2917
2918 val = bl->owner->insert_location (bl);
2919
2920 /* If trying to set a read-watchpoint, and it turns out it's not
2921 supported, try emulating one with an access watchpoint. */
2922 if (val == 1 && bl->watchpoint_type == hw_read)
2923 {
2924 /* But don't try to insert it, if there's already another
2925 hw_access location that would be considered a duplicate
2926 of this one. */
2927 for (bp_location *loc : all_bp_locations ())
2928 if (loc != bl
2929 && loc->watchpoint_type == hw_access
2930 && watchpoint_locations_match (bl, loc))
2931 {
2932 bl->duplicate = 1;
2933 bl->inserted = 1;
2934 bl->target_info = loc->target_info;
2935 bl->watchpoint_type = hw_access;
2936 val = 0;
2937 break;
2938 }
2939
2940 if (val == 1)
2941 {
2942 bl->watchpoint_type = hw_access;
2943 val = bl->owner->insert_location (bl);
2944
2945 if (val)
2946 /* Back to the original value. */
2947 bl->watchpoint_type = hw_read;
2948 }
2949 }
2950
2951 bl->inserted = (val == 0);
2952 }
2953
2954 else if (bl->owner->type == bp_catchpoint)
2955 {
2956 int val;
2957
2958 val = bl->owner->insert_location (bl);
2959 if (val)
2960 {
2961 bl->owner->enable_state = bp_disabled;
2962
2963 if (val == 1)
2964 warning (_("\
2965 Error inserting catchpoint %d: Your system does not support this type\n\
2966 of catchpoint."), bl->owner->number);
2967 else
2968 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2969 }
2970
2971 bl->inserted = (val == 0);
2972
2973 /* We've already printed an error message if there was a problem
2974 inserting this catchpoint, and we've disabled the catchpoint,
2975 so just return success. */
2976 return 0;
2977 }
2978
2979 return 0;
2980 }
2981
2982 /* This function is called when program space PSPACE is about to be
2983 deleted. It takes care of updating breakpoints to not reference
2984 PSPACE anymore. */
2985
2986 void
2987 breakpoint_program_space_exit (struct program_space *pspace)
2988 {
2989 /* Remove any breakpoint that was set through this program space. */
2990 for (breakpoint *b : all_breakpoints_safe ())
2991 if (b->pspace == pspace)
2992 delete_breakpoint (b);
2993
2994 /* Breakpoints set through other program spaces could have locations
2995 bound to PSPACE as well. Remove those. */
2996 for (bp_location *loc : all_bp_locations ())
2997 {
2998 struct bp_location *tmp;
2999
3000 if (loc->pspace == pspace)
3001 {
3002 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3003 if (loc->owner->loc == loc)
3004 loc->owner->loc = loc->next;
3005 else
3006 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3007 if (tmp->next == loc)
3008 {
3009 tmp->next = loc->next;
3010 break;
3011 }
3012 }
3013 }
3014
3015 /* Now update the global location list to permanently delete the
3016 removed locations above. */
3017 update_global_location_list (UGLL_DONT_INSERT);
3018 }
3019
3020 /* Make sure all breakpoints are inserted in inferior.
3021 Throws exception on any error.
3022 A breakpoint that is already inserted won't be inserted
3023 again, so calling this function twice is safe. */
3024 void
3025 insert_breakpoints (void)
3026 {
3027 for (breakpoint *bpt : all_breakpoints ())
3028 if (is_hardware_watchpoint (bpt))
3029 {
3030 struct watchpoint *w = (struct watchpoint *) bpt;
3031
3032 update_watchpoint (w, 0 /* don't reparse. */);
3033 }
3034
3035 /* Updating watchpoints creates new locations, so update the global
3036 location list. Explicitly tell ugll to insert locations and
3037 ignore breakpoints_always_inserted_mode. Also,
3038 update_global_location_list tries to "upgrade" software
3039 breakpoints to hardware breakpoints to handle "set breakpoint
3040 auto-hw", so we need to call it even if we don't have new
3041 locations. */
3042 update_global_location_list (UGLL_INSERT);
3043 }
3044
3045 /* This is used when we need to synch breakpoint conditions between GDB and the
3046 target. It is the case with deleting and disabling of breakpoints when using
3047 always-inserted mode. */
3048
3049 static void
3050 update_inserted_breakpoint_locations (void)
3051 {
3052 int error_flag = 0;
3053 int val = 0;
3054 int disabled_breaks = 0;
3055 int hw_breakpoint_error = 0;
3056 int hw_bp_details_reported = 0;
3057
3058 string_file tmp_error_stream;
3059
3060 /* Explicitly mark the warning -- this will only be printed if
3061 there was an error. */
3062 tmp_error_stream.puts ("Warning:\n");
3063
3064 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3065
3066 for (bp_location *bl : all_bp_locations ())
3067 {
3068 /* We only want to update software breakpoints and hardware
3069 breakpoints. */
3070 if (!is_breakpoint (bl->owner))
3071 continue;
3072
3073 /* We only want to update locations that are already inserted
3074 and need updating. This is to avoid unwanted insertion during
3075 deletion of breakpoints. */
3076 if (!bl->inserted || !bl->needs_update)
3077 continue;
3078
3079 switch_to_program_space_and_thread (bl->pspace);
3080
3081 /* For targets that support global breakpoints, there's no need
3082 to select an inferior to insert breakpoint to. In fact, even
3083 if we aren't attached to any process yet, we should still
3084 insert breakpoints. */
3085 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3086 && (inferior_ptid == null_ptid || !target_has_execution ()))
3087 continue;
3088
3089 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3090 &hw_breakpoint_error, &hw_bp_details_reported);
3091 if (val)
3092 error_flag = val;
3093 }
3094
3095 if (error_flag)
3096 {
3097 target_terminal::ours_for_output ();
3098 error_stream (tmp_error_stream);
3099 }
3100 }
3101
3102 /* Used when starting or continuing the program. */
3103
3104 static void
3105 insert_breakpoint_locations (void)
3106 {
3107 int error_flag = 0;
3108 int val = 0;
3109 int disabled_breaks = 0;
3110 int hw_breakpoint_error = 0;
3111 int hw_bp_error_explained_already = 0;
3112
3113 string_file tmp_error_stream;
3114
3115 /* Explicitly mark the warning -- this will only be printed if
3116 there was an error. */
3117 tmp_error_stream.puts ("Warning:\n");
3118
3119 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3120
3121 for (bp_location *bl : all_bp_locations ())
3122 {
3123 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3124 continue;
3125
3126 /* There is no point inserting thread-specific breakpoints if
3127 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3128 has BL->OWNER always non-NULL. */
3129 if (bl->owner->thread != -1
3130 && !valid_global_thread_id (bl->owner->thread))
3131 continue;
3132
3133 switch_to_program_space_and_thread (bl->pspace);
3134
3135 /* For targets that support global breakpoints, there's no need
3136 to select an inferior to insert breakpoint to. In fact, even
3137 if we aren't attached to any process yet, we should still
3138 insert breakpoints. */
3139 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3140 && (inferior_ptid == null_ptid || !target_has_execution ()))
3141 continue;
3142
3143 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3144 &hw_breakpoint_error, &hw_bp_error_explained_already);
3145 if (val)
3146 error_flag = val;
3147 }
3148
3149 /* If we failed to insert all locations of a watchpoint, remove
3150 them, as half-inserted watchpoint is of limited use. */
3151 for (breakpoint *bpt : all_breakpoints ())
3152 {
3153 int some_failed = 0;
3154
3155 if (!is_hardware_watchpoint (bpt))
3156 continue;
3157
3158 if (!breakpoint_enabled (bpt))
3159 continue;
3160
3161 if (bpt->disposition == disp_del_at_next_stop)
3162 continue;
3163
3164 for (bp_location *loc : bpt->locations ())
3165 if (!loc->inserted && should_be_inserted (loc))
3166 {
3167 some_failed = 1;
3168 break;
3169 }
3170
3171 if (some_failed)
3172 {
3173 for (bp_location *loc : bpt->locations ())
3174 if (loc->inserted)
3175 remove_breakpoint (loc);
3176
3177 hw_breakpoint_error = 1;
3178 tmp_error_stream.printf ("Could not insert "
3179 "hardware watchpoint %d.\n",
3180 bpt->number);
3181 error_flag = -1;
3182 }
3183 }
3184
3185 if (error_flag)
3186 {
3187 /* If a hardware breakpoint or watchpoint was inserted, add a
3188 message about possibly exhausted resources. */
3189 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3190 {
3191 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3192 You may have requested too many hardware breakpoints/watchpoints.\n");
3193 }
3194 target_terminal::ours_for_output ();
3195 error_stream (tmp_error_stream);
3196 }
3197 }
3198
3199 /* Used when the program stops.
3200 Returns zero if successful, or non-zero if there was a problem
3201 removing a breakpoint location. */
3202
3203 int
3204 remove_breakpoints (void)
3205 {
3206 int val = 0;
3207
3208 for (bp_location *bl : all_bp_locations ())
3209 if (bl->inserted && !is_tracepoint (bl->owner))
3210 val |= remove_breakpoint (bl);
3211
3212 return val;
3213 }
3214
3215 /* When a thread exits, remove breakpoints that are related to
3216 that thread. */
3217
3218 static void
3219 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3220 {
3221 for (breakpoint *b : all_breakpoints_safe ())
3222 {
3223 if (b->thread == tp->global_num && user_breakpoint_p (b))
3224 {
3225 b->disposition = disp_del_at_next_stop;
3226
3227 gdb_printf (_("\
3228 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3229 b->number, print_thread_id (tp));
3230
3231 /* Hide it from the user. */
3232 b->number = 0;
3233 }
3234 }
3235 }
3236
3237 /* See breakpoint.h. */
3238
3239 void
3240 remove_breakpoints_inf (inferior *inf)
3241 {
3242 int val;
3243
3244 for (bp_location *bl : all_bp_locations ())
3245 {
3246 if (bl->pspace != inf->pspace)
3247 continue;
3248
3249 if (bl->inserted && !bl->target_info.persist)
3250 {
3251 val = remove_breakpoint (bl);
3252 if (val != 0)
3253 return;
3254 }
3255 }
3256 }
3257
3258 static int internal_breakpoint_number = -1;
3259
3260 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3261 If INTERNAL is non-zero, the breakpoint number will be populated
3262 from internal_breakpoint_number and that variable decremented.
3263 Otherwise the breakpoint number will be populated from
3264 breakpoint_count and that value incremented. Internal breakpoints
3265 do not set the internal var bpnum. */
3266 static void
3267 set_breakpoint_number (int internal, struct breakpoint *b)
3268 {
3269 if (internal)
3270 b->number = internal_breakpoint_number--;
3271 else
3272 {
3273 set_breakpoint_count (breakpoint_count + 1);
3274 b->number = breakpoint_count;
3275 }
3276 }
3277
3278 static struct breakpoint *
3279 create_internal_breakpoint (struct gdbarch *gdbarch,
3280 CORE_ADDR address, enum bptype type)
3281 {
3282 std::unique_ptr<internal_breakpoint> b
3283 (new internal_breakpoint (gdbarch, type, address));
3284
3285 b->number = internal_breakpoint_number--;
3286
3287 return add_to_breakpoint_chain (std::move (b));
3288 }
3289
3290 static const char *const longjmp_names[] =
3291 {
3292 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3293 };
3294 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3295
3296 /* Per-objfile data private to breakpoint.c. */
3297 struct breakpoint_objfile_data
3298 {
3299 /* Minimal symbol for "_ovly_debug_event" (if any). */
3300 struct bound_minimal_symbol overlay_msym;
3301
3302 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3303 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3304
3305 /* True if we have looked for longjmp probes. */
3306 int longjmp_searched = 0;
3307
3308 /* SystemTap probe points for longjmp (if any). These are non-owning
3309 references. */
3310 std::vector<probe *> longjmp_probes;
3311
3312 /* Minimal symbol for "std::terminate()" (if any). */
3313 struct bound_minimal_symbol terminate_msym;
3314
3315 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3316 struct bound_minimal_symbol exception_msym;
3317
3318 /* True if we have looked for exception probes. */
3319 int exception_searched = 0;
3320
3321 /* SystemTap probe points for unwinding (if any). These are non-owning
3322 references. */
3323 std::vector<probe *> exception_probes;
3324 };
3325
3326 static const registry<objfile>::key<breakpoint_objfile_data>
3327 breakpoint_objfile_key;
3328
3329 /* Minimal symbol not found sentinel. */
3330 static struct minimal_symbol msym_not_found;
3331
3332 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3333
3334 static int
3335 msym_not_found_p (const struct minimal_symbol *msym)
3336 {
3337 return msym == &msym_not_found;
3338 }
3339
3340 /* Return per-objfile data needed by breakpoint.c.
3341 Allocate the data if necessary. */
3342
3343 static struct breakpoint_objfile_data *
3344 get_breakpoint_objfile_data (struct objfile *objfile)
3345 {
3346 struct breakpoint_objfile_data *bp_objfile_data;
3347
3348 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3349 if (bp_objfile_data == NULL)
3350 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3351 return bp_objfile_data;
3352 }
3353
3354 static void
3355 create_overlay_event_breakpoint (void)
3356 {
3357 const char *const func_name = "_ovly_debug_event";
3358
3359 for (objfile *objfile : current_program_space->objfiles ())
3360 {
3361 struct breakpoint *b;
3362 struct breakpoint_objfile_data *bp_objfile_data;
3363 CORE_ADDR addr;
3364
3365 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3366
3367 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3368 continue;
3369
3370 if (bp_objfile_data->overlay_msym.minsym == NULL)
3371 {
3372 struct bound_minimal_symbol m;
3373
3374 m = lookup_minimal_symbol_text (func_name, objfile);
3375 if (m.minsym == NULL)
3376 {
3377 /* Avoid future lookups in this objfile. */
3378 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3379 continue;
3380 }
3381 bp_objfile_data->overlay_msym = m;
3382 }
3383
3384 addr = bp_objfile_data->overlay_msym.value_address ();
3385 b = create_internal_breakpoint (objfile->arch (), addr,
3386 bp_overlay_event);
3387 b->locspec = new_explicit_location_spec_function (func_name);
3388
3389 if (overlay_debugging == ovly_auto)
3390 {
3391 b->enable_state = bp_enabled;
3392 overlay_events_enabled = 1;
3393 }
3394 else
3395 {
3396 b->enable_state = bp_disabled;
3397 overlay_events_enabled = 0;
3398 }
3399 }
3400 }
3401
3402 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3403 true if a breakpoint was installed. */
3404
3405 static bool
3406 create_longjmp_master_breakpoint_probe (objfile *objfile)
3407 {
3408 struct gdbarch *gdbarch = objfile->arch ();
3409 struct breakpoint_objfile_data *bp_objfile_data
3410 = get_breakpoint_objfile_data (objfile);
3411
3412 if (!bp_objfile_data->longjmp_searched)
3413 {
3414 std::vector<probe *> ret
3415 = find_probes_in_objfile (objfile, "libc", "longjmp");
3416
3417 if (!ret.empty ())
3418 {
3419 /* We are only interested in checking one element. */
3420 probe *p = ret[0];
3421
3422 if (!p->can_evaluate_arguments ())
3423 {
3424 /* We cannot use the probe interface here,
3425 because it does not know how to evaluate
3426 arguments. */
3427 ret.clear ();
3428 }
3429 }
3430 bp_objfile_data->longjmp_probes = ret;
3431 bp_objfile_data->longjmp_searched = 1;
3432 }
3433
3434 if (bp_objfile_data->longjmp_probes.empty ())
3435 return false;
3436
3437 for (probe *p : bp_objfile_data->longjmp_probes)
3438 {
3439 struct breakpoint *b;
3440
3441 b = create_internal_breakpoint (gdbarch,
3442 p->get_relocated_address (objfile),
3443 bp_longjmp_master);
3444 b->locspec = new_probe_location_spec ("-probe-stap libc:longjmp");
3445 b->enable_state = bp_disabled;
3446 }
3447
3448 return true;
3449 }
3450
3451 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3452 Return true if at least one breakpoint was installed. */
3453
3454 static bool
3455 create_longjmp_master_breakpoint_names (objfile *objfile)
3456 {
3457 struct gdbarch *gdbarch = objfile->arch ();
3458 if (!gdbarch_get_longjmp_target_p (gdbarch))
3459 return false;
3460
3461 struct breakpoint_objfile_data *bp_objfile_data
3462 = get_breakpoint_objfile_data (objfile);
3463 unsigned int installed_bp = 0;
3464
3465 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3466 {
3467 struct breakpoint *b;
3468 const char *func_name;
3469 CORE_ADDR addr;
3470
3471 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3472 continue;
3473
3474 func_name = longjmp_names[i];
3475 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3476 {
3477 struct bound_minimal_symbol m;
3478
3479 m = lookup_minimal_symbol_text (func_name, objfile);
3480 if (m.minsym == NULL)
3481 {
3482 /* Prevent future lookups in this objfile. */
3483 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3484 continue;
3485 }
3486 bp_objfile_data->longjmp_msym[i] = m;
3487 }
3488
3489 addr = bp_objfile_data->longjmp_msym[i].value_address ();
3490 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master);
3491 b->locspec = new_explicit_location_spec_function (func_name);
3492 b->enable_state = bp_disabled;
3493 installed_bp++;
3494 }
3495
3496 return installed_bp > 0;
3497 }
3498
3499 /* Create a master longjmp breakpoint. */
3500
3501 static void
3502 create_longjmp_master_breakpoint (void)
3503 {
3504 scoped_restore_current_program_space restore_pspace;
3505
3506 for (struct program_space *pspace : program_spaces)
3507 {
3508 set_current_program_space (pspace);
3509
3510 for (objfile *obj : current_program_space->objfiles ())
3511 {
3512 /* Skip separate debug object, it's handled in the loop below. */
3513 if (obj->separate_debug_objfile_backlink != nullptr)
3514 continue;
3515
3516 /* Try a probe kind breakpoint on main objfile. */
3517 if (create_longjmp_master_breakpoint_probe (obj))
3518 continue;
3519
3520 /* Try longjmp_names kind breakpoints on main and separate_debug
3521 objfiles. */
3522 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3523 if (create_longjmp_master_breakpoint_names (debug_objfile))
3524 break;
3525 }
3526 }
3527 }
3528
3529 /* Create a master std::terminate breakpoint. */
3530 static void
3531 create_std_terminate_master_breakpoint (void)
3532 {
3533 const char *const func_name = "std::terminate()";
3534
3535 scoped_restore_current_program_space restore_pspace;
3536
3537 for (struct program_space *pspace : program_spaces)
3538 {
3539 CORE_ADDR addr;
3540
3541 set_current_program_space (pspace);
3542
3543 for (objfile *objfile : current_program_space->objfiles ())
3544 {
3545 struct breakpoint *b;
3546 struct breakpoint_objfile_data *bp_objfile_data;
3547
3548 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3549
3550 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3551 continue;
3552
3553 if (bp_objfile_data->terminate_msym.minsym == NULL)
3554 {
3555 struct bound_minimal_symbol m;
3556
3557 m = lookup_minimal_symbol (func_name, NULL, objfile);
3558 if (m.minsym == NULL || (m.minsym->type () != mst_text
3559 && m.minsym->type () != mst_file_text))
3560 {
3561 /* Prevent future lookups in this objfile. */
3562 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3563 continue;
3564 }
3565 bp_objfile_data->terminate_msym = m;
3566 }
3567
3568 addr = bp_objfile_data->terminate_msym.value_address ();
3569 b = create_internal_breakpoint (objfile->arch (), addr,
3570 bp_std_terminate_master);
3571 b->locspec = new_explicit_location_spec_function (func_name);
3572 b->enable_state = bp_disabled;
3573 }
3574 }
3575 }
3576
3577 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3578 probe. Return true if a breakpoint was installed. */
3579
3580 static bool
3581 create_exception_master_breakpoint_probe (objfile *objfile)
3582 {
3583 struct breakpoint *b;
3584 struct gdbarch *gdbarch;
3585 struct breakpoint_objfile_data *bp_objfile_data;
3586
3587 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3588
3589 /* We prefer the SystemTap probe point if it exists. */
3590 if (!bp_objfile_data->exception_searched)
3591 {
3592 std::vector<probe *> ret
3593 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3594
3595 if (!ret.empty ())
3596 {
3597 /* We are only interested in checking one element. */
3598 probe *p = ret[0];
3599
3600 if (!p->can_evaluate_arguments ())
3601 {
3602 /* We cannot use the probe interface here, because it does
3603 not know how to evaluate arguments. */
3604 ret.clear ();
3605 }
3606 }
3607 bp_objfile_data->exception_probes = ret;
3608 bp_objfile_data->exception_searched = 1;
3609 }
3610
3611 if (bp_objfile_data->exception_probes.empty ())
3612 return false;
3613
3614 gdbarch = objfile->arch ();
3615
3616 for (probe *p : bp_objfile_data->exception_probes)
3617 {
3618 b = create_internal_breakpoint (gdbarch,
3619 p->get_relocated_address (objfile),
3620 bp_exception_master);
3621 b->locspec = new_probe_location_spec ("-probe-stap libgcc:unwind");
3622 b->enable_state = bp_disabled;
3623 }
3624
3625 return true;
3626 }
3627
3628 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3629 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3630
3631 static bool
3632 create_exception_master_breakpoint_hook (objfile *objfile)
3633 {
3634 const char *const func_name = "_Unwind_DebugHook";
3635 struct breakpoint *b;
3636 struct gdbarch *gdbarch;
3637 struct breakpoint_objfile_data *bp_objfile_data;
3638 CORE_ADDR addr;
3639
3640 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3641
3642 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3643 return false;
3644
3645 gdbarch = objfile->arch ();
3646
3647 if (bp_objfile_data->exception_msym.minsym == NULL)
3648 {
3649 struct bound_minimal_symbol debug_hook;
3650
3651 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3652 if (debug_hook.minsym == NULL)
3653 {
3654 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3655 return false;
3656 }
3657
3658 bp_objfile_data->exception_msym = debug_hook;
3659 }
3660
3661 addr = bp_objfile_data->exception_msym.value_address ();
3662 addr = gdbarch_convert_from_func_ptr_addr
3663 (gdbarch, addr, current_inferior ()->top_target ());
3664 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master);
3665 b->locspec = new_explicit_location_spec_function (func_name);
3666 b->enable_state = bp_disabled;
3667
3668 return true;
3669 }
3670
3671 /* Install a master breakpoint on the unwinder's debug hook. */
3672
3673 static void
3674 create_exception_master_breakpoint (void)
3675 {
3676 for (objfile *obj : current_program_space->objfiles ())
3677 {
3678 /* Skip separate debug object. */
3679 if (obj->separate_debug_objfile_backlink)
3680 continue;
3681
3682 /* Try a probe kind breakpoint. */
3683 if (create_exception_master_breakpoint_probe (obj))
3684 continue;
3685
3686 /* Iterate over main and separate debug objects and try an
3687 _Unwind_DebugHook kind breakpoint. */
3688 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3689 if (create_exception_master_breakpoint_hook (debug_objfile))
3690 break;
3691 }
3692 }
3693
3694 /* Does B have a location spec? */
3695
3696 static int
3697 breakpoint_location_spec_empty_p (const struct breakpoint *b)
3698 {
3699 return (b->locspec != nullptr && b->locspec->empty_p ());
3700 }
3701
3702 void
3703 update_breakpoints_after_exec (void)
3704 {
3705 /* We're about to delete breakpoints from GDB's lists. If the
3706 INSERTED flag is true, GDB will try to lift the breakpoints by
3707 writing the breakpoints' "shadow contents" back into memory. The
3708 "shadow contents" are NOT valid after an exec, so GDB should not
3709 do that. Instead, the target is responsible from marking
3710 breakpoints out as soon as it detects an exec. We don't do that
3711 here instead, because there may be other attempts to delete
3712 breakpoints after detecting an exec and before reaching here. */
3713 for (bp_location *bploc : all_bp_locations ())
3714 if (bploc->pspace == current_program_space)
3715 gdb_assert (!bploc->inserted);
3716
3717 for (breakpoint *b : all_breakpoints_safe ())
3718 {
3719 if (b->pspace != current_program_space)
3720 continue;
3721
3722 /* Solib breakpoints must be explicitly reset after an exec(). */
3723 if (b->type == bp_shlib_event)
3724 {
3725 delete_breakpoint (b);
3726 continue;
3727 }
3728
3729 /* JIT breakpoints must be explicitly reset after an exec(). */
3730 if (b->type == bp_jit_event)
3731 {
3732 delete_breakpoint (b);
3733 continue;
3734 }
3735
3736 /* Thread event breakpoints must be set anew after an exec(),
3737 as must overlay event and longjmp master breakpoints. */
3738 if (b->type == bp_thread_event || b->type == bp_overlay_event
3739 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3740 || b->type == bp_exception_master)
3741 {
3742 delete_breakpoint (b);
3743 continue;
3744 }
3745
3746 /* Step-resume breakpoints are meaningless after an exec(). */
3747 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3748 {
3749 delete_breakpoint (b);
3750 continue;
3751 }
3752
3753 /* Just like single-step breakpoints. */
3754 if (b->type == bp_single_step)
3755 {
3756 delete_breakpoint (b);
3757 continue;
3758 }
3759
3760 /* Longjmp and longjmp-resume breakpoints are also meaningless
3761 after an exec. */
3762 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3763 || b->type == bp_longjmp_call_dummy
3764 || b->type == bp_exception || b->type == bp_exception_resume)
3765 {
3766 delete_breakpoint (b);
3767 continue;
3768 }
3769
3770 if (b->type == bp_catchpoint)
3771 {
3772 /* For now, none of the bp_catchpoint breakpoints need to
3773 do anything at this point. In the future, if some of
3774 the catchpoints need to something, we will need to add
3775 a new method, and call this method from here. */
3776 continue;
3777 }
3778
3779 /* bp_finish is a special case. The only way we ought to be able
3780 to see one of these when an exec() has happened, is if the user
3781 caught a vfork, and then said "finish". Ordinarily a finish just
3782 carries them to the call-site of the current callee, by setting
3783 a temporary bp there and resuming. But in this case, the finish
3784 will carry them entirely through the vfork & exec.
3785
3786 We don't want to allow a bp_finish to remain inserted now. But
3787 we can't safely delete it, 'cause finish_command has a handle to
3788 the bp on a bpstat, and will later want to delete it. There's a
3789 chance (and I've seen it happen) that if we delete the bp_finish
3790 here, that its storage will get reused by the time finish_command
3791 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3792 We really must allow finish_command to delete a bp_finish.
3793
3794 In the absence of a general solution for the "how do we know
3795 it's safe to delete something others may have handles to?"
3796 problem, what we'll do here is just uninsert the bp_finish, and
3797 let finish_command delete it.
3798
3799 (We know the bp_finish is "doomed" in the sense that it's
3800 momentary, and will be deleted as soon as finish_command sees
3801 the inferior stopped. So it doesn't matter that the bp's
3802 address is probably bogus in the new a.out, unlike e.g., the
3803 solib breakpoints.) */
3804
3805 if (b->type == bp_finish)
3806 {
3807 continue;
3808 }
3809
3810 /* Without a symbolic address, we have little hope of the
3811 pre-exec() address meaning the same thing in the post-exec()
3812 a.out. */
3813 if (breakpoint_location_spec_empty_p (b))
3814 {
3815 delete_breakpoint (b);
3816 continue;
3817 }
3818 }
3819 }
3820
3821 int
3822 detach_breakpoints (ptid_t ptid)
3823 {
3824 int val = 0;
3825 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3826 struct inferior *inf = current_inferior ();
3827
3828 if (ptid.pid () == inferior_ptid.pid ())
3829 error (_("Cannot detach breakpoints of inferior_ptid"));
3830
3831 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3832 inferior_ptid = ptid;
3833 for (bp_location *bl : all_bp_locations ())
3834 {
3835 if (bl->pspace != inf->pspace)
3836 continue;
3837
3838 /* This function must physically remove breakpoints locations
3839 from the specified ptid, without modifying the breakpoint
3840 package's state. Locations of type bp_loc_other and
3841 bp_loc_software_watchpoint are only maintained at GDB side,
3842 so there is no need to remove them. Moreover, removing these
3843 would modify the breakpoint package's state. */
3844 if (bl->loc_type == bp_loc_other
3845 || bl->loc_type == bp_loc_software_watchpoint)
3846 continue;
3847
3848 if (bl->inserted)
3849 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3850 }
3851
3852 return val;
3853 }
3854
3855 /* Remove the breakpoint location BL from the current address space.
3856 Note that this is used to detach breakpoints from a child fork.
3857 When we get here, the child isn't in the inferior list, and neither
3858 do we have objects to represent its address space --- we should
3859 *not* look at bl->pspace->aspace here. */
3860
3861 static int
3862 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3863 {
3864 int val;
3865
3866 /* BL is never in moribund_locations by our callers. */
3867 gdb_assert (bl->owner != NULL);
3868
3869 /* The type of none suggests that owner is actually deleted.
3870 This should not ever happen. */
3871 gdb_assert (bl->owner->type != bp_none);
3872
3873 if (bl->loc_type == bp_loc_software_breakpoint
3874 || bl->loc_type == bp_loc_hardware_breakpoint)
3875 {
3876 /* "Normal" instruction breakpoint: either the standard
3877 trap-instruction bp (bp_breakpoint), or a
3878 bp_hardware_breakpoint. */
3879
3880 /* First check to see if we have to handle an overlay. */
3881 if (overlay_debugging == ovly_off
3882 || bl->section == NULL
3883 || !(section_is_overlay (bl->section)))
3884 {
3885 /* No overlay handling: just remove the breakpoint. */
3886
3887 /* If we're trying to uninsert a memory breakpoint that we
3888 know is set in a dynamic object that is marked
3889 shlib_disabled, then either the dynamic object was
3890 removed with "remove-symbol-file" or with
3891 "nosharedlibrary". In the former case, we don't know
3892 whether another dynamic object might have loaded over the
3893 breakpoint's address -- the user might well let us know
3894 about it next with add-symbol-file (the whole point of
3895 add-symbol-file is letting the user manually maintain a
3896 list of dynamically loaded objects). If we have the
3897 breakpoint's shadow memory, that is, this is a software
3898 breakpoint managed by GDB, check whether the breakpoint
3899 is still inserted in memory, to avoid overwriting wrong
3900 code with stale saved shadow contents. Note that HW
3901 breakpoints don't have shadow memory, as they're
3902 implemented using a mechanism that is not dependent on
3903 being able to modify the target's memory, and as such
3904 they should always be removed. */
3905 if (bl->shlib_disabled
3906 && bl->target_info.shadow_len != 0
3907 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3908 val = 0;
3909 else
3910 val = bl->owner->remove_location (bl, reason);
3911 }
3912 else
3913 {
3914 /* This breakpoint is in an overlay section.
3915 Did we set a breakpoint at the LMA? */
3916 if (!overlay_events_enabled)
3917 {
3918 /* Yes -- overlay event support is not active, so we
3919 should have set a breakpoint at the LMA. Remove it.
3920 */
3921 /* Ignore any failures: if the LMA is in ROM, we will
3922 have already warned when we failed to insert it. */
3923 if (bl->loc_type == bp_loc_hardware_breakpoint)
3924 target_remove_hw_breakpoint (bl->gdbarch,
3925 &bl->overlay_target_info);
3926 else
3927 target_remove_breakpoint (bl->gdbarch,
3928 &bl->overlay_target_info,
3929 reason);
3930 }
3931 /* Did we set a breakpoint at the VMA?
3932 If so, we will have marked the breakpoint 'inserted'. */
3933 if (bl->inserted)
3934 {
3935 /* Yes -- remove it. Previously we did not bother to
3936 remove the breakpoint if the section had been
3937 unmapped, but let's not rely on that being safe. We
3938 don't know what the overlay manager might do. */
3939
3940 /* However, we should remove *software* breakpoints only
3941 if the section is still mapped, or else we overwrite
3942 wrong code with the saved shadow contents. */
3943 if (bl->loc_type == bp_loc_hardware_breakpoint
3944 || section_is_mapped (bl->section))
3945 val = bl->owner->remove_location (bl, reason);
3946 else
3947 val = 0;
3948 }
3949 else
3950 {
3951 /* No -- not inserted, so no need to remove. No error. */
3952 val = 0;
3953 }
3954 }
3955
3956 /* In some cases, we might not be able to remove a breakpoint in
3957 a shared library that has already been removed, but we have
3958 not yet processed the shlib unload event. Similarly for an
3959 unloaded add-symbol-file object - the user might not yet have
3960 had the chance to remove-symbol-file it. shlib_disabled will
3961 be set if the library/object has already been removed, but
3962 the breakpoint hasn't been uninserted yet, e.g., after
3963 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3964 always-inserted mode. */
3965 if (val
3966 && (bl->loc_type == bp_loc_software_breakpoint
3967 && (bl->shlib_disabled
3968 || solib_name_from_address (bl->pspace, bl->address)
3969 || shared_objfile_contains_address_p (bl->pspace,
3970 bl->address))))
3971 val = 0;
3972
3973 if (val)
3974 return val;
3975 bl->inserted = (reason == DETACH_BREAKPOINT);
3976 }
3977 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3978 {
3979 bl->inserted = (reason == DETACH_BREAKPOINT);
3980 bl->owner->remove_location (bl, reason);
3981
3982 /* Failure to remove any of the hardware watchpoints comes here. */
3983 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3984 warning (_("Could not remove hardware watchpoint %d."),
3985 bl->owner->number);
3986 }
3987 else if (bl->owner->type == bp_catchpoint
3988 && breakpoint_enabled (bl->owner)
3989 && !bl->duplicate)
3990 {
3991 val = bl->owner->remove_location (bl, reason);
3992 if (val)
3993 return val;
3994
3995 bl->inserted = (reason == DETACH_BREAKPOINT);
3996 }
3997
3998 return 0;
3999 }
4000
4001 static int
4002 remove_breakpoint (struct bp_location *bl)
4003 {
4004 /* BL is never in moribund_locations by our callers. */
4005 gdb_assert (bl->owner != NULL);
4006
4007 /* The type of none suggests that owner is actually deleted.
4008 This should not ever happen. */
4009 gdb_assert (bl->owner->type != bp_none);
4010
4011 scoped_restore_current_pspace_and_thread restore_pspace_thread;
4012
4013 switch_to_program_space_and_thread (bl->pspace);
4014
4015 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4016 }
4017
4018 /* Clear the "inserted" flag in all breakpoints. */
4019
4020 void
4021 mark_breakpoints_out (void)
4022 {
4023 for (bp_location *bl : all_bp_locations ())
4024 if (bl->pspace == current_program_space)
4025 bl->inserted = 0;
4026 }
4027
4028 /* Clear the "inserted" flag in all breakpoints and delete any
4029 breakpoints which should go away between runs of the program.
4030
4031 Plus other such housekeeping that has to be done for breakpoints
4032 between runs.
4033
4034 Note: this function gets called at the end of a run (by
4035 generic_mourn_inferior) and when a run begins (by
4036 init_wait_for_inferior). */
4037
4038
4039
4040 void
4041 breakpoint_init_inferior (enum inf_context context)
4042 {
4043 struct program_space *pspace = current_program_space;
4044
4045 /* If breakpoint locations are shared across processes, then there's
4046 nothing to do. */
4047 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4048 return;
4049
4050 mark_breakpoints_out ();
4051
4052 for (breakpoint *b : all_breakpoints_safe ())
4053 {
4054 if (b->loc && b->loc->pspace != pspace)
4055 continue;
4056
4057 switch (b->type)
4058 {
4059 case bp_call_dummy:
4060 case bp_longjmp_call_dummy:
4061
4062 /* If the call dummy breakpoint is at the entry point it will
4063 cause problems when the inferior is rerun, so we better get
4064 rid of it. */
4065
4066 case bp_watchpoint_scope:
4067
4068 /* Also get rid of scope breakpoints. */
4069
4070 case bp_shlib_event:
4071
4072 /* Also remove solib event breakpoints. Their addresses may
4073 have changed since the last time we ran the program.
4074 Actually we may now be debugging against different target;
4075 and so the solib backend that installed this breakpoint may
4076 not be used in by the target. E.g.,
4077
4078 (gdb) file prog-linux
4079 (gdb) run # native linux target
4080 ...
4081 (gdb) kill
4082 (gdb) file prog-win.exe
4083 (gdb) tar rem :9999 # remote Windows gdbserver.
4084 */
4085
4086 case bp_step_resume:
4087
4088 /* Also remove step-resume breakpoints. */
4089
4090 case bp_single_step:
4091
4092 /* Also remove single-step breakpoints. */
4093
4094 delete_breakpoint (b);
4095 break;
4096
4097 case bp_watchpoint:
4098 case bp_hardware_watchpoint:
4099 case bp_read_watchpoint:
4100 case bp_access_watchpoint:
4101 {
4102 struct watchpoint *w = (struct watchpoint *) b;
4103
4104 /* Likewise for watchpoints on local expressions. */
4105 if (w->exp_valid_block != NULL)
4106 delete_breakpoint (b);
4107 else
4108 {
4109 /* Get rid of existing locations, which are no longer
4110 valid. New ones will be created in
4111 update_watchpoint, when the inferior is restarted.
4112 The next update_global_location_list call will
4113 garbage collect them. */
4114 b->loc = NULL;
4115
4116 if (context == inf_starting)
4117 {
4118 /* Reset val field to force reread of starting value in
4119 insert_breakpoints. */
4120 w->val.reset (nullptr);
4121 w->val_valid = false;
4122 }
4123 }
4124 }
4125 break;
4126 default:
4127 break;
4128 }
4129 }
4130
4131 /* Get rid of the moribund locations. */
4132 for (bp_location *bl : moribund_locations)
4133 decref_bp_location (&bl);
4134 moribund_locations.clear ();
4135 }
4136
4137 /* These functions concern about actual breakpoints inserted in the
4138 target --- to e.g. check if we need to do decr_pc adjustment or if
4139 we need to hop over the bkpt --- so we check for address space
4140 match, not program space. */
4141
4142 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4143 exists at PC. It returns ordinary_breakpoint_here if it's an
4144 ordinary breakpoint, or permanent_breakpoint_here if it's a
4145 permanent breakpoint.
4146 - When continuing from a location with an ordinary breakpoint, we
4147 actually single step once before calling insert_breakpoints.
4148 - When continuing from a location with a permanent breakpoint, we
4149 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4150 the target, to advance the PC past the breakpoint. */
4151
4152 enum breakpoint_here
4153 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4154 {
4155 int any_breakpoint_here = 0;
4156
4157 for (bp_location *bl : all_bp_locations ())
4158 {
4159 if (bl->loc_type != bp_loc_software_breakpoint
4160 && bl->loc_type != bp_loc_hardware_breakpoint)
4161 continue;
4162
4163 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4164 if ((breakpoint_enabled (bl->owner)
4165 || bl->permanent)
4166 && breakpoint_location_address_match (bl, aspace, pc))
4167 {
4168 if (overlay_debugging
4169 && section_is_overlay (bl->section)
4170 && !section_is_mapped (bl->section))
4171 continue; /* unmapped overlay -- can't be a match */
4172 else if (bl->permanent)
4173 return permanent_breakpoint_here;
4174 else
4175 any_breakpoint_here = 1;
4176 }
4177 }
4178
4179 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4180 }
4181
4182 /* See breakpoint.h. */
4183
4184 int
4185 breakpoint_in_range_p (const address_space *aspace,
4186 CORE_ADDR addr, ULONGEST len)
4187 {
4188 for (bp_location *bl : all_bp_locations ())
4189 {
4190 if (bl->loc_type != bp_loc_software_breakpoint
4191 && bl->loc_type != bp_loc_hardware_breakpoint)
4192 continue;
4193
4194 if ((breakpoint_enabled (bl->owner)
4195 || bl->permanent)
4196 && breakpoint_location_address_range_overlap (bl, aspace,
4197 addr, len))
4198 {
4199 if (overlay_debugging
4200 && section_is_overlay (bl->section)
4201 && !section_is_mapped (bl->section))
4202 {
4203 /* Unmapped overlay -- can't be a match. */
4204 continue;
4205 }
4206
4207 return 1;
4208 }
4209 }
4210
4211 return 0;
4212 }
4213
4214 /* Return true if there's a moribund breakpoint at PC. */
4215
4216 int
4217 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4218 {
4219 for (bp_location *loc : moribund_locations)
4220 if (breakpoint_location_address_match (loc, aspace, pc))
4221 return 1;
4222
4223 return 0;
4224 }
4225
4226 /* Returns non-zero iff BL is inserted at PC, in address space
4227 ASPACE. */
4228
4229 static int
4230 bp_location_inserted_here_p (struct bp_location *bl,
4231 const address_space *aspace, CORE_ADDR pc)
4232 {
4233 if (bl->inserted
4234 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4235 aspace, pc))
4236 {
4237 if (overlay_debugging
4238 && section_is_overlay (bl->section)
4239 && !section_is_mapped (bl->section))
4240 return 0; /* unmapped overlay -- can't be a match */
4241 else
4242 return 1;
4243 }
4244 return 0;
4245 }
4246
4247 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4248
4249 int
4250 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4251 {
4252 for (bp_location *bl : all_bp_locations_at_addr (pc))
4253 {
4254 if (bl->loc_type != bp_loc_software_breakpoint
4255 && bl->loc_type != bp_loc_hardware_breakpoint)
4256 continue;
4257
4258 if (bp_location_inserted_here_p (bl, aspace, pc))
4259 return 1;
4260 }
4261 return 0;
4262 }
4263
4264 /* This function returns non-zero iff there is a software breakpoint
4265 inserted at PC. */
4266
4267 int
4268 software_breakpoint_inserted_here_p (const address_space *aspace,
4269 CORE_ADDR pc)
4270 {
4271 for (bp_location *bl : all_bp_locations_at_addr (pc))
4272 {
4273 if (bl->loc_type != bp_loc_software_breakpoint)
4274 continue;
4275
4276 if (bp_location_inserted_here_p (bl, aspace, pc))
4277 return 1;
4278 }
4279
4280 return 0;
4281 }
4282
4283 /* See breakpoint.h. */
4284
4285 int
4286 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4287 CORE_ADDR pc)
4288 {
4289 for (bp_location *bl : all_bp_locations_at_addr (pc))
4290 {
4291 if (bl->loc_type != bp_loc_hardware_breakpoint)
4292 continue;
4293
4294 if (bp_location_inserted_here_p (bl, aspace, pc))
4295 return 1;
4296 }
4297
4298 return 0;
4299 }
4300
4301 int
4302 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4303 CORE_ADDR addr, ULONGEST len)
4304 {
4305 for (breakpoint *bpt : all_breakpoints ())
4306 {
4307 if (bpt->type != bp_hardware_watchpoint
4308 && bpt->type != bp_access_watchpoint)
4309 continue;
4310
4311 if (!breakpoint_enabled (bpt))
4312 continue;
4313
4314 for (bp_location *loc : bpt->locations ())
4315 if (loc->pspace->aspace == aspace && loc->inserted)
4316 {
4317 CORE_ADDR l, h;
4318
4319 /* Check for intersection. */
4320 l = std::max<CORE_ADDR> (loc->address, addr);
4321 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4322 if (l < h)
4323 return 1;
4324 }
4325 }
4326 return 0;
4327 }
4328
4329 /* See breakpoint.h. */
4330
4331 bool
4332 is_catchpoint (struct breakpoint *b)
4333 {
4334 return (b->type == bp_catchpoint);
4335 }
4336
4337 /* Clear a bpstat so that it says we are not at any breakpoint.
4338 Also free any storage that is part of a bpstat. */
4339
4340 void
4341 bpstat_clear (bpstat **bsp)
4342 {
4343 bpstat *p;
4344 bpstat *q;
4345
4346 if (bsp == 0)
4347 return;
4348 p = *bsp;
4349 while (p != NULL)
4350 {
4351 q = p->next;
4352 delete p;
4353 p = q;
4354 }
4355 *bsp = NULL;
4356 }
4357
4358 bpstat::bpstat (const bpstat &other)
4359 : next (NULL),
4360 bp_location_at (other.bp_location_at),
4361 breakpoint_at (other.breakpoint_at),
4362 commands (other.commands),
4363 print (other.print),
4364 stop (other.stop),
4365 print_it (other.print_it)
4366 {
4367 if (other.old_val != NULL)
4368 old_val = release_value (value_copy (other.old_val.get ()));
4369 }
4370
4371 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4372 is part of the bpstat is copied as well. */
4373
4374 bpstat *
4375 bpstat_copy (bpstat *bs)
4376 {
4377 bpstat *p = nullptr;
4378 bpstat *tmp;
4379 bpstat *retval = nullptr;
4380
4381 if (bs == NULL)
4382 return bs;
4383
4384 for (; bs != NULL; bs = bs->next)
4385 {
4386 tmp = new bpstat (*bs);
4387
4388 if (p == NULL)
4389 /* This is the first thing in the chain. */
4390 retval = tmp;
4391 else
4392 p->next = tmp;
4393 p = tmp;
4394 }
4395 p->next = NULL;
4396 return retval;
4397 }
4398
4399 /* Find the bpstat associated with this breakpoint. */
4400
4401 bpstat *
4402 bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
4403 {
4404 if (bsp == NULL)
4405 return NULL;
4406
4407 for (; bsp != NULL; bsp = bsp->next)
4408 {
4409 if (bsp->breakpoint_at == breakpoint)
4410 return bsp;
4411 }
4412 return NULL;
4413 }
4414
4415 /* See breakpoint.h. */
4416
4417 bool
4418 bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
4419 {
4420 for (; bsp != NULL; bsp = bsp->next)
4421 {
4422 if (bsp->breakpoint_at == NULL)
4423 {
4424 /* A moribund location can never explain a signal other than
4425 GDB_SIGNAL_TRAP. */
4426 if (sig == GDB_SIGNAL_TRAP)
4427 return true;
4428 }
4429 else
4430 {
4431 if (bsp->breakpoint_at->explains_signal (sig))
4432 return true;
4433 }
4434 }
4435
4436 return false;
4437 }
4438
4439 /* Put in *NUM the breakpoint number of the first breakpoint we are
4440 stopped at. *BSP upon return is a bpstat which points to the
4441 remaining breakpoints stopped at (but which is not guaranteed to be
4442 good for anything but further calls to bpstat_num).
4443
4444 Return 0 if passed a bpstat which does not indicate any breakpoints.
4445 Return -1 if stopped at a breakpoint that has been deleted since
4446 we set it.
4447 Return 1 otherwise. */
4448
4449 int
4450 bpstat_num (bpstat **bsp, int *num)
4451 {
4452 struct breakpoint *b;
4453
4454 if ((*bsp) == NULL)
4455 return 0; /* No more breakpoint values */
4456
4457 /* We assume we'll never have several bpstats that correspond to a
4458 single breakpoint -- otherwise, this function might return the
4459 same number more than once and this will look ugly. */
4460 b = (*bsp)->breakpoint_at;
4461 *bsp = (*bsp)->next;
4462 if (b == NULL)
4463 return -1; /* breakpoint that's been deleted since */
4464
4465 *num = b->number; /* We have its number */
4466 return 1;
4467 }
4468
4469 /* See breakpoint.h. */
4470
4471 void
4472 bpstat_clear_actions (void)
4473 {
4474 bpstat *bs;
4475
4476 if (inferior_ptid == null_ptid)
4477 return;
4478
4479 thread_info *tp = inferior_thread ();
4480 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4481 {
4482 bs->commands = NULL;
4483 bs->old_val.reset (nullptr);
4484 }
4485 }
4486
4487 /* Called when a command is about to proceed the inferior. */
4488
4489 static void
4490 breakpoint_about_to_proceed (void)
4491 {
4492 if (inferior_ptid != null_ptid)
4493 {
4494 struct thread_info *tp = inferior_thread ();
4495
4496 /* Allow inferior function calls in breakpoint commands to not
4497 interrupt the command list. When the call finishes
4498 successfully, the inferior will be standing at the same
4499 breakpoint as if nothing happened. */
4500 if (tp->control.in_infcall)
4501 return;
4502 }
4503
4504 breakpoint_proceeded = 1;
4505 }
4506
4507 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4508 or its equivalent. */
4509
4510 static int
4511 command_line_is_silent (struct command_line *cmd)
4512 {
4513 return cmd && (strcmp ("silent", cmd->line) == 0);
4514 }
4515
4516 /* Execute all the commands associated with all the breakpoints at
4517 this location. Any of these commands could cause the process to
4518 proceed beyond this point, etc. We look out for such changes by
4519 checking the global "breakpoint_proceeded" after each command.
4520
4521 Returns true if a breakpoint command resumed the inferior. In that
4522 case, it is the caller's responsibility to recall it again with the
4523 bpstat of the current thread. */
4524
4525 static int
4526 bpstat_do_actions_1 (bpstat **bsp)
4527 {
4528 bpstat *bs;
4529 int again = 0;
4530
4531 /* Avoid endless recursion if a `source' command is contained
4532 in bs->commands. */
4533 if (executing_breakpoint_commands)
4534 return 0;
4535
4536 scoped_restore save_executing
4537 = make_scoped_restore (&executing_breakpoint_commands, 1);
4538
4539 scoped_restore preventer = prevent_dont_repeat ();
4540
4541 /* This pointer will iterate over the list of bpstat's. */
4542 bs = *bsp;
4543
4544 breakpoint_proceeded = 0;
4545 for (; bs != NULL; bs = bs->next)
4546 {
4547 struct command_line *cmd = NULL;
4548
4549 /* Take ownership of the BSP's command tree, if it has one.
4550
4551 The command tree could legitimately contain commands like
4552 'step' and 'next', which call clear_proceed_status, which
4553 frees stop_bpstat's command tree. To make sure this doesn't
4554 free the tree we're executing out from under us, we need to
4555 take ownership of the tree ourselves. Since a given bpstat's
4556 commands are only executed once, we don't need to copy it; we
4557 can clear the pointer in the bpstat, and make sure we free
4558 the tree when we're done. */
4559 counted_command_line ccmd = bs->commands;
4560 bs->commands = NULL;
4561 if (ccmd != NULL)
4562 cmd = ccmd.get ();
4563 if (command_line_is_silent (cmd))
4564 {
4565 /* The action has been already done by bpstat_stop_status. */
4566 cmd = cmd->next;
4567 }
4568
4569 while (cmd != NULL)
4570 {
4571 execute_control_command (cmd);
4572
4573 if (breakpoint_proceeded)
4574 break;
4575 else
4576 cmd = cmd->next;
4577 }
4578
4579 if (breakpoint_proceeded)
4580 {
4581 if (current_ui->async)
4582 /* If we are in async mode, then the target might be still
4583 running, not stopped at any breakpoint, so nothing for
4584 us to do here -- just return to the event loop. */
4585 ;
4586 else
4587 /* In sync mode, when execute_control_command returns
4588 we're already standing on the next breakpoint.
4589 Breakpoint commands for that stop were not run, since
4590 execute_command does not run breakpoint commands --
4591 only command_line_handler does, but that one is not
4592 involved in execution of breakpoint commands. So, we
4593 can now execute breakpoint commands. It should be
4594 noted that making execute_command do bpstat actions is
4595 not an option -- in this case we'll have recursive
4596 invocation of bpstat for each breakpoint with a
4597 command, and can easily blow up GDB stack. Instead, we
4598 return true, which will trigger the caller to recall us
4599 with the new stop_bpstat. */
4600 again = 1;
4601 break;
4602 }
4603 }
4604 return again;
4605 }
4606
4607 /* Helper for bpstat_do_actions. Get the current thread, if there's
4608 one, is alive and has execution. Return NULL otherwise. */
4609
4610 static thread_info *
4611 get_bpstat_thread ()
4612 {
4613 if (inferior_ptid == null_ptid || !target_has_execution ())
4614 return NULL;
4615
4616 thread_info *tp = inferior_thread ();
4617 if (tp->state == THREAD_EXITED || tp->executing ())
4618 return NULL;
4619 return tp;
4620 }
4621
4622 void
4623 bpstat_do_actions (void)
4624 {
4625 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4626 thread_info *tp;
4627
4628 /* Do any commands attached to breakpoint we are stopped at. */
4629 while ((tp = get_bpstat_thread ()) != NULL)
4630 {
4631 /* Since in sync mode, bpstat_do_actions may resume the
4632 inferior, and only return when it is stopped at the next
4633 breakpoint, we keep doing breakpoint actions until it returns
4634 false to indicate the inferior was not resumed. */
4635 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4636 break;
4637 }
4638
4639 cleanup_if_error.release ();
4640 }
4641
4642 /* Print out the (old or new) value associated with a watchpoint. */
4643
4644 static void
4645 watchpoint_value_print (struct value *val, struct ui_file *stream)
4646 {
4647 if (val == NULL)
4648 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4649 else
4650 {
4651 struct value_print_options opts;
4652 get_user_print_options (&opts);
4653 value_print (val, stream, &opts);
4654 }
4655 }
4656
4657 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4658 debugging multiple threads. */
4659
4660 void
4661 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4662 {
4663 if (uiout->is_mi_like_p ())
4664 return;
4665
4666 uiout->text ("\n");
4667
4668 if (show_thread_that_caused_stop ())
4669 {
4670 struct thread_info *thr = inferior_thread ();
4671
4672 uiout->text ("Thread ");
4673 uiout->field_string ("thread-id", print_thread_id (thr));
4674
4675 const char *name = thread_name (thr);
4676 if (name != NULL)
4677 {
4678 uiout->text (" \"");
4679 uiout->field_string ("name", name);
4680 uiout->text ("\"");
4681 }
4682
4683 uiout->text (" hit ");
4684 }
4685 }
4686
4687 /* Generic routine for printing messages indicating why we
4688 stopped. The behavior of this function depends on the value
4689 'print_it' in the bpstat structure. Under some circumstances we
4690 may decide not to print anything here and delegate the task to
4691 normal_stop(). */
4692
4693 static enum print_stop_action
4694 print_bp_stop_message (bpstat *bs)
4695 {
4696 switch (bs->print_it)
4697 {
4698 case print_it_noop:
4699 /* Nothing should be printed for this bpstat entry. */
4700 return PRINT_UNKNOWN;
4701 break;
4702
4703 case print_it_done:
4704 /* We still want to print the frame, but we already printed the
4705 relevant messages. */
4706 return PRINT_SRC_AND_LOC;
4707 break;
4708
4709 case print_it_normal:
4710 {
4711 struct breakpoint *b = bs->breakpoint_at;
4712
4713 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4714 which has since been deleted. */
4715 if (b == NULL)
4716 return PRINT_UNKNOWN;
4717
4718 /* Normal case. Call the breakpoint's print_it method. */
4719 return b->print_it (bs);
4720 }
4721 break;
4722
4723 default:
4724 internal_error (__FILE__, __LINE__,
4725 _("print_bp_stop_message: unrecognized enum value"));
4726 break;
4727 }
4728 }
4729
4730 /* See breakpoint.h. */
4731
4732 void
4733 print_solib_event (bool is_catchpoint)
4734 {
4735 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4736 bool any_added = !current_program_space->added_solibs.empty ();
4737
4738 if (!is_catchpoint)
4739 {
4740 if (any_added || any_deleted)
4741 current_uiout->text (_("Stopped due to shared library event:\n"));
4742 else
4743 current_uiout->text (_("Stopped due to shared library event (no "
4744 "libraries added or removed)\n"));
4745 }
4746
4747 if (current_uiout->is_mi_like_p ())
4748 current_uiout->field_string ("reason",
4749 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4750
4751 if (any_deleted)
4752 {
4753 current_uiout->text (_(" Inferior unloaded "));
4754 ui_out_emit_list list_emitter (current_uiout, "removed");
4755 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4756 {
4757 const std::string &name = current_program_space->deleted_solibs[ix];
4758
4759 if (ix > 0)
4760 current_uiout->text (" ");
4761 current_uiout->field_string ("library", name);
4762 current_uiout->text ("\n");
4763 }
4764 }
4765
4766 if (any_added)
4767 {
4768 current_uiout->text (_(" Inferior loaded "));
4769 ui_out_emit_list list_emitter (current_uiout, "added");
4770 bool first = true;
4771 for (so_list *iter : current_program_space->added_solibs)
4772 {
4773 if (!first)
4774 current_uiout->text (" ");
4775 first = false;
4776 current_uiout->field_string ("library", iter->so_name);
4777 current_uiout->text ("\n");
4778 }
4779 }
4780 }
4781
4782 /* Print a message indicating what happened. This is called from
4783 normal_stop(). The input to this routine is the head of the bpstat
4784 list - a list of the eventpoints that caused this stop. KIND is
4785 the target_waitkind for the stopping event. This
4786 routine calls the generic print routine for printing a message
4787 about reasons for stopping. This will print (for example) the
4788 "Breakpoint n," part of the output. The return value of this
4789 routine is one of:
4790
4791 PRINT_UNKNOWN: Means we printed nothing.
4792 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4793 code to print the location. An example is
4794 "Breakpoint 1, " which should be followed by
4795 the location.
4796 PRINT_SRC_ONLY: Means we printed something, but there is no need
4797 to also print the location part of the message.
4798 An example is the catch/throw messages, which
4799 don't require a location appended to the end.
4800 PRINT_NOTHING: We have done some printing and we don't need any
4801 further info to be printed. */
4802
4803 enum print_stop_action
4804 bpstat_print (bpstat *bs, target_waitkind kind)
4805 {
4806 enum print_stop_action val;
4807
4808 /* Maybe another breakpoint in the chain caused us to stop.
4809 (Currently all watchpoints go on the bpstat whether hit or not.
4810 That probably could (should) be changed, provided care is taken
4811 with respect to bpstat_explains_signal). */
4812 for (; bs; bs = bs->next)
4813 {
4814 val = print_bp_stop_message (bs);
4815 if (val == PRINT_SRC_ONLY
4816 || val == PRINT_SRC_AND_LOC
4817 || val == PRINT_NOTHING)
4818 return val;
4819 }
4820
4821 /* If we had hit a shared library event breakpoint,
4822 print_bp_stop_message would print out this message. If we hit an
4823 OS-level shared library event, do the same thing. */
4824 if (kind == TARGET_WAITKIND_LOADED)
4825 {
4826 print_solib_event (false);
4827 return PRINT_NOTHING;
4828 }
4829
4830 /* We reached the end of the chain, or we got a null BS to start
4831 with and nothing was printed. */
4832 return PRINT_UNKNOWN;
4833 }
4834
4835 /* Evaluate the boolean expression EXP and return the result. */
4836
4837 static bool
4838 breakpoint_cond_eval (expression *exp)
4839 {
4840 scoped_value_mark mark;
4841 return value_true (evaluate_expression (exp));
4842 }
4843
4844 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4845
4846 bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
4847 : next (NULL),
4848 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4849 breakpoint_at (bl->owner),
4850 commands (NULL),
4851 print (0),
4852 stop (0),
4853 print_it (print_it_normal)
4854 {
4855 **bs_link_pointer = this;
4856 *bs_link_pointer = &next;
4857 }
4858
4859 bpstat::bpstat ()
4860 : next (NULL),
4861 breakpoint_at (NULL),
4862 commands (NULL),
4863 print (0),
4864 stop (0),
4865 print_it (print_it_normal)
4866 {
4867 }
4868 \f
4869 /* The target has stopped with waitstatus WS. Check if any hardware
4870 watchpoints have triggered, according to the target. */
4871
4872 int
4873 watchpoints_triggered (const target_waitstatus &ws)
4874 {
4875 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4876 CORE_ADDR addr;
4877
4878 if (!stopped_by_watchpoint)
4879 {
4880 /* We were not stopped by a watchpoint. Mark all watchpoints
4881 as not triggered. */
4882 for (breakpoint *b : all_breakpoints ())
4883 if (is_hardware_watchpoint (b))
4884 {
4885 struct watchpoint *w = (struct watchpoint *) b;
4886
4887 w->watchpoint_triggered = watch_triggered_no;
4888 }
4889
4890 return 0;
4891 }
4892
4893 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4894 {
4895 /* We were stopped by a watchpoint, but we don't know where.
4896 Mark all watchpoints as unknown. */
4897 for (breakpoint *b : all_breakpoints ())
4898 if (is_hardware_watchpoint (b))
4899 {
4900 struct watchpoint *w = (struct watchpoint *) b;
4901
4902 w->watchpoint_triggered = watch_triggered_unknown;
4903 }
4904
4905 return 1;
4906 }
4907
4908 /* The target could report the data address. Mark watchpoints
4909 affected by this data address as triggered, and all others as not
4910 triggered. */
4911
4912 for (breakpoint *b : all_breakpoints ())
4913 if (is_hardware_watchpoint (b))
4914 {
4915 struct watchpoint *w = (struct watchpoint *) b;
4916
4917 w->watchpoint_triggered = watch_triggered_no;
4918 for (bp_location *loc : b->locations ())
4919 {
4920 if (is_masked_watchpoint (b))
4921 {
4922 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4923 CORE_ADDR start = loc->address & w->hw_wp_mask;
4924
4925 if (newaddr == start)
4926 {
4927 w->watchpoint_triggered = watch_triggered_yes;
4928 break;
4929 }
4930 }
4931 /* Exact match not required. Within range is sufficient. */
4932 else if (target_watchpoint_addr_within_range
4933 (current_inferior ()->top_target (), addr, loc->address,
4934 loc->length))
4935 {
4936 w->watchpoint_triggered = watch_triggered_yes;
4937 break;
4938 }
4939 }
4940 }
4941
4942 return 1;
4943 }
4944
4945 /* Possible return values for watchpoint_check. */
4946 enum wp_check_result
4947 {
4948 /* The watchpoint has been deleted. */
4949 WP_DELETED = 1,
4950
4951 /* The value has changed. */
4952 WP_VALUE_CHANGED = 2,
4953
4954 /* The value has not changed. */
4955 WP_VALUE_NOT_CHANGED = 3,
4956
4957 /* Ignore this watchpoint, no matter if the value changed or not. */
4958 WP_IGNORE = 4,
4959 };
4960
4961 #define BP_TEMPFLAG 1
4962 #define BP_HARDWAREFLAG 2
4963
4964 /* Evaluate watchpoint condition expression and check if its value
4965 changed. */
4966
4967 static wp_check_result
4968 watchpoint_check (bpstat *bs)
4969 {
4970 struct watchpoint *b;
4971 frame_info_ptr fr;
4972 int within_current_scope;
4973
4974 /* BS is built from an existing struct breakpoint. */
4975 gdb_assert (bs->breakpoint_at != NULL);
4976 b = (struct watchpoint *) bs->breakpoint_at;
4977
4978 /* If this is a local watchpoint, we only want to check if the
4979 watchpoint frame is in scope if the current thread is the thread
4980 that was used to create the watchpoint. */
4981 if (!watchpoint_in_thread_scope (b))
4982 return WP_IGNORE;
4983
4984 if (b->exp_valid_block == NULL)
4985 within_current_scope = 1;
4986 else
4987 {
4988 frame_info_ptr frame = get_current_frame ();
4989 struct gdbarch *frame_arch = get_frame_arch (frame);
4990 CORE_ADDR frame_pc = get_frame_pc (frame);
4991
4992 /* stack_frame_destroyed_p() returns a non-zero value if we're
4993 still in the function but the stack frame has already been
4994 invalidated. Since we can't rely on the values of local
4995 variables after the stack has been destroyed, we are treating
4996 the watchpoint in that state as `not changed' without further
4997 checking. Don't mark watchpoints as changed if the current
4998 frame is in an epilogue - even if they are in some other
4999 frame, our view of the stack is likely to be wrong and
5000 frame_find_by_id could error out. */
5001 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5002 return WP_IGNORE;
5003
5004 fr = frame_find_by_id (b->watchpoint_frame);
5005 within_current_scope = (fr != NULL);
5006
5007 /* If we've gotten confused in the unwinder, we might have
5008 returned a frame that can't describe this variable. */
5009 if (within_current_scope)
5010 {
5011 struct symbol *function;
5012
5013 function = get_frame_function (fr);
5014 if (function == NULL
5015 || !contained_in (b->exp_valid_block, function->value_block ()))
5016 within_current_scope = 0;
5017 }
5018
5019 if (within_current_scope)
5020 /* If we end up stopping, the current frame will get selected
5021 in normal_stop. So this call to select_frame won't affect
5022 the user. */
5023 select_frame (fr);
5024 }
5025
5026 if (within_current_scope)
5027 {
5028 /* We use value_{,free_to_}mark because it could be a *long*
5029 time before we return to the command level and call
5030 free_all_values. We can't call free_all_values because we
5031 might be in the middle of evaluating a function call. */
5032
5033 struct value *mark;
5034 struct value *new_val;
5035
5036 if (is_masked_watchpoint (b))
5037 /* Since we don't know the exact trigger address (from
5038 stopped_data_address), just tell the user we've triggered
5039 a mask watchpoint. */
5040 return WP_VALUE_CHANGED;
5041
5042 mark = value_mark ();
5043 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5044 NULL, NULL, false);
5045
5046 if (b->val_bitsize != 0)
5047 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5048
5049 /* We use value_equal_contents instead of value_equal because
5050 the latter coerces an array to a pointer, thus comparing just
5051 the address of the array instead of its contents. This is
5052 not what we want. */
5053 if ((b->val != NULL) != (new_val != NULL)
5054 || (b->val != NULL && !value_equal_contents (b->val.get (),
5055 new_val)))
5056 {
5057 bs->old_val = b->val;
5058 b->val = release_value (new_val);
5059 b->val_valid = true;
5060 if (new_val != NULL)
5061 value_free_to_mark (mark);
5062 return WP_VALUE_CHANGED;
5063 }
5064 else
5065 {
5066 /* Nothing changed. */
5067 value_free_to_mark (mark);
5068 return WP_VALUE_NOT_CHANGED;
5069 }
5070 }
5071 else
5072 {
5073 /* This seems like the only logical thing to do because
5074 if we temporarily ignored the watchpoint, then when
5075 we reenter the block in which it is valid it contains
5076 garbage (in the case of a function, it may have two
5077 garbage values, one before and one after the prologue).
5078 So we can't even detect the first assignment to it and
5079 watch after that (since the garbage may or may not equal
5080 the first value assigned). */
5081 /* We print all the stop information in
5082 breakpointprint_it, but in this case, by the time we
5083 call breakpoint->print_it this bp will be deleted
5084 already. So we have no choice but print the information
5085 here. */
5086
5087 SWITCH_THRU_ALL_UIS ()
5088 {
5089 struct ui_out *uiout = current_uiout;
5090
5091 if (uiout->is_mi_like_p ())
5092 uiout->field_string
5093 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5094 uiout->message ("\nWatchpoint %pF deleted because the program has "
5095 "left the block in\n"
5096 "which its expression is valid.\n",
5097 signed_field ("wpnum", b->number));
5098 }
5099
5100 /* Make sure the watchpoint's commands aren't executed. */
5101 b->commands = NULL;
5102 watchpoint_del_at_next_stop (b);
5103
5104 return WP_DELETED;
5105 }
5106 }
5107
5108 /* Return true if it looks like target has stopped due to hitting
5109 breakpoint location BL. This function does not check if we should
5110 stop, only if BL explains the stop. */
5111
5112 static int
5113 bpstat_check_location (const struct bp_location *bl,
5114 const address_space *aspace, CORE_ADDR bp_addr,
5115 const target_waitstatus &ws)
5116 {
5117 struct breakpoint *b = bl->owner;
5118
5119 /* BL is from an existing breakpoint. */
5120 gdb_assert (b != NULL);
5121
5122 return b->breakpoint_hit (bl, aspace, bp_addr, ws);
5123 }
5124
5125 /* Determine if the watched values have actually changed, and we
5126 should stop. If not, set BS->stop to 0. */
5127
5128 static void
5129 bpstat_check_watchpoint (bpstat *bs)
5130 {
5131 const struct bp_location *bl;
5132 struct watchpoint *b;
5133
5134 /* BS is built for existing struct breakpoint. */
5135 bl = bs->bp_location_at.get ();
5136 gdb_assert (bl != NULL);
5137 b = (struct watchpoint *) bs->breakpoint_at;
5138 gdb_assert (b != NULL);
5139
5140 {
5141 int must_check_value = 0;
5142
5143 if (b->type == bp_watchpoint)
5144 /* For a software watchpoint, we must always check the
5145 watched value. */
5146 must_check_value = 1;
5147 else if (b->watchpoint_triggered == watch_triggered_yes)
5148 /* We have a hardware watchpoint (read, write, or access)
5149 and the target earlier reported an address watched by
5150 this watchpoint. */
5151 must_check_value = 1;
5152 else if (b->watchpoint_triggered == watch_triggered_unknown
5153 && b->type == bp_hardware_watchpoint)
5154 /* We were stopped by a hardware watchpoint, but the target could
5155 not report the data address. We must check the watchpoint's
5156 value. Access and read watchpoints are out of luck; without
5157 a data address, we can't figure it out. */
5158 must_check_value = 1;
5159
5160 if (must_check_value)
5161 {
5162 wp_check_result e;
5163
5164 try
5165 {
5166 e = watchpoint_check (bs);
5167 }
5168 catch (const gdb_exception &ex)
5169 {
5170 exception_fprintf (gdb_stderr, ex,
5171 "Error evaluating expression "
5172 "for watchpoint %d\n",
5173 b->number);
5174
5175 SWITCH_THRU_ALL_UIS ()
5176 {
5177 gdb_printf (_("Watchpoint %d deleted.\n"),
5178 b->number);
5179 }
5180 watchpoint_del_at_next_stop (b);
5181 e = WP_DELETED;
5182 }
5183
5184 switch (e)
5185 {
5186 case WP_DELETED:
5187 /* We've already printed what needs to be printed. */
5188 bs->print_it = print_it_done;
5189 /* Stop. */
5190 break;
5191 case WP_IGNORE:
5192 bs->print_it = print_it_noop;
5193 bs->stop = 0;
5194 break;
5195 case WP_VALUE_CHANGED:
5196 if (b->type == bp_read_watchpoint)
5197 {
5198 /* There are two cases to consider here:
5199
5200 1. We're watching the triggered memory for reads.
5201 In that case, trust the target, and always report
5202 the watchpoint hit to the user. Even though
5203 reads don't cause value changes, the value may
5204 have changed since the last time it was read, and
5205 since we're not trapping writes, we will not see
5206 those, and as such we should ignore our notion of
5207 old value.
5208
5209 2. We're watching the triggered memory for both
5210 reads and writes. There are two ways this may
5211 happen:
5212
5213 2.1. This is a target that can't break on data
5214 reads only, but can break on accesses (reads or
5215 writes), such as e.g., x86. We detect this case
5216 at the time we try to insert read watchpoints.
5217
5218 2.2. Otherwise, the target supports read
5219 watchpoints, but, the user set an access or write
5220 watchpoint watching the same memory as this read
5221 watchpoint.
5222
5223 If we're watching memory writes as well as reads,
5224 ignore watchpoint hits when we find that the
5225 value hasn't changed, as reads don't cause
5226 changes. This still gives false positives when
5227 the program writes the same value to memory as
5228 what there was already in memory (we will confuse
5229 it for a read), but it's much better than
5230 nothing. */
5231
5232 int other_write_watchpoint = 0;
5233
5234 if (bl->watchpoint_type == hw_read)
5235 {
5236 for (breakpoint *other_b : all_breakpoints ())
5237 if (other_b->type == bp_hardware_watchpoint
5238 || other_b->type == bp_access_watchpoint)
5239 {
5240 struct watchpoint *other_w =
5241 (struct watchpoint *) other_b;
5242
5243 if (other_w->watchpoint_triggered
5244 == watch_triggered_yes)
5245 {
5246 other_write_watchpoint = 1;
5247 break;
5248 }
5249 }
5250 }
5251
5252 if (other_write_watchpoint
5253 || bl->watchpoint_type == hw_access)
5254 {
5255 /* We're watching the same memory for writes,
5256 and the value changed since the last time we
5257 updated it, so this trap must be for a write.
5258 Ignore it. */
5259 bs->print_it = print_it_noop;
5260 bs->stop = 0;
5261 }
5262 }
5263 break;
5264 case WP_VALUE_NOT_CHANGED:
5265 if (b->type == bp_hardware_watchpoint
5266 || b->type == bp_watchpoint)
5267 {
5268 /* Don't stop: write watchpoints shouldn't fire if
5269 the value hasn't changed. */
5270 bs->print_it = print_it_noop;
5271 bs->stop = 0;
5272 }
5273 /* Stop. */
5274 break;
5275 default:
5276 /* Can't happen. */
5277 break;
5278 }
5279 }
5280 else /* must_check_value == 0 */
5281 {
5282 /* This is a case where some watchpoint(s) triggered, but
5283 not at the address of this watchpoint, or else no
5284 watchpoint triggered after all. So don't print
5285 anything for this watchpoint. */
5286 bs->print_it = print_it_noop;
5287 bs->stop = 0;
5288 }
5289 }
5290 }
5291
5292 /* For breakpoints that are currently marked as telling gdb to stop,
5293 check conditions (condition proper, frame, thread and ignore count)
5294 of breakpoint referred to by BS. If we should not stop for this
5295 breakpoint, set BS->stop to 0. */
5296
5297 static void
5298 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5299 {
5300 INFRUN_SCOPED_DEBUG_ENTER_EXIT;
5301
5302 const struct bp_location *bl;
5303 struct breakpoint *b;
5304 /* Assume stop. */
5305 bool condition_result = true;
5306 struct expression *cond;
5307
5308 gdb_assert (bs->stop);
5309
5310 /* BS is built for existing struct breakpoint. */
5311 bl = bs->bp_location_at.get ();
5312 gdb_assert (bl != NULL);
5313 b = bs->breakpoint_at;
5314 gdb_assert (b != NULL);
5315
5316 infrun_debug_printf ("thread = %s, breakpoint %d.%d",
5317 thread->ptid.to_string ().c_str (),
5318 b->number, find_loc_num_by_location (bl));
5319
5320 /* Even if the target evaluated the condition on its end and notified GDB, we
5321 need to do so again since GDB does not know if we stopped due to a
5322 breakpoint or a single step breakpoint. */
5323
5324 if (frame_id_p (b->frame_id)
5325 && b->frame_id != get_stack_frame_id (get_current_frame ()))
5326 {
5327 infrun_debug_printf ("incorrect frame %s not %s, not stopping",
5328 get_stack_frame_id (get_current_frame ()).to_string ().c_str (),
5329 b->frame_id.to_string ().c_str ());
5330 bs->stop = 0;
5331 return;
5332 }
5333
5334 /* If this is a thread/task-specific breakpoint, don't waste cpu
5335 evaluating the condition if this isn't the specified
5336 thread/task. */
5337 if ((b->thread != -1 && b->thread != thread->global_num)
5338 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5339 {
5340 infrun_debug_printf ("incorrect thread or task, not stopping");
5341 bs->stop = 0;
5342 return;
5343 }
5344
5345 /* Evaluate extension language breakpoints that have a "stop" method
5346 implemented. */
5347 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5348
5349 if (is_watchpoint (b))
5350 {
5351 struct watchpoint *w = (struct watchpoint *) b;
5352
5353 cond = w->cond_exp.get ();
5354 }
5355 else
5356 cond = bl->cond.get ();
5357
5358 if (cond != nullptr && b->disposition != disp_del_at_next_stop)
5359 {
5360 int within_current_scope = 1;
5361 struct watchpoint * w;
5362
5363 /* We use scoped_value_mark because it could be a long time
5364 before we return to the command level and call
5365 free_all_values. We can't call free_all_values because we
5366 might be in the middle of evaluating a function call. */
5367 scoped_value_mark mark;
5368
5369 if (is_watchpoint (b))
5370 w = (struct watchpoint *) b;
5371 else
5372 w = NULL;
5373
5374 /* Need to select the frame, with all that implies so that
5375 the conditions will have the right context. Because we
5376 use the frame, we will not see an inlined function's
5377 variables when we arrive at a breakpoint at the start
5378 of the inlined function; the current frame will be the
5379 call site. */
5380 if (w == NULL || w->cond_exp_valid_block == NULL)
5381 select_frame (get_current_frame ());
5382 else
5383 {
5384 frame_info_ptr frame;
5385
5386 /* For local watchpoint expressions, which particular
5387 instance of a local is being watched matters, so we
5388 keep track of the frame to evaluate the expression
5389 in. To evaluate the condition however, it doesn't
5390 really matter which instantiation of the function
5391 where the condition makes sense triggers the
5392 watchpoint. This allows an expression like "watch
5393 global if q > 10" set in `func', catch writes to
5394 global on all threads that call `func', or catch
5395 writes on all recursive calls of `func' by a single
5396 thread. We simply always evaluate the condition in
5397 the innermost frame that's executing where it makes
5398 sense to evaluate the condition. It seems
5399 intuitive. */
5400 frame = block_innermost_frame (w->cond_exp_valid_block);
5401 if (frame != NULL)
5402 select_frame (frame);
5403 else
5404 within_current_scope = 0;
5405 }
5406 if (within_current_scope)
5407 {
5408 try
5409 {
5410 condition_result = breakpoint_cond_eval (cond);
5411 }
5412 catch (const gdb_exception &ex)
5413 {
5414 exception_fprintf (gdb_stderr, ex,
5415 "Error in testing breakpoint condition:\n");
5416 }
5417 }
5418 else
5419 {
5420 warning (_("Watchpoint condition cannot be tested "
5421 "in the current scope"));
5422 /* If we failed to set the right context for this
5423 watchpoint, unconditionally report it. */
5424 }
5425 /* FIXME-someday, should give breakpoint #. */
5426 }
5427
5428 if (cond != nullptr && !condition_result)
5429 {
5430 infrun_debug_printf ("condition_result = false, not stopping");
5431 bs->stop = 0;
5432 return;
5433 }
5434 else if (b->ignore_count > 0)
5435 {
5436 infrun_debug_printf ("ignore count %d, not stopping",
5437 b->ignore_count);
5438 b->ignore_count--;
5439 bs->stop = 0;
5440 /* Increase the hit count even though we don't stop. */
5441 ++(b->hit_count);
5442 gdb::observers::breakpoint_modified.notify (b);
5443 return;
5444 }
5445
5446 if (bs->stop)
5447 infrun_debug_printf ("stopping at this breakpoint");
5448 else
5449 infrun_debug_printf ("not stopping at this breakpoint");
5450 }
5451
5452 /* Returns true if we need to track moribund locations of LOC's type
5453 on the current target. */
5454
5455 static int
5456 need_moribund_for_location_type (struct bp_location *loc)
5457 {
5458 return ((loc->loc_type == bp_loc_software_breakpoint
5459 && !target_supports_stopped_by_sw_breakpoint ())
5460 || (loc->loc_type == bp_loc_hardware_breakpoint
5461 && !target_supports_stopped_by_hw_breakpoint ()));
5462 }
5463
5464 /* See breakpoint.h. */
5465
5466 bpstat *
5467 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5468 const target_waitstatus &ws)
5469 {
5470 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5471
5472 for (breakpoint *b : all_breakpoints ())
5473 {
5474 if (!breakpoint_enabled (b))
5475 continue;
5476
5477 for (bp_location *bl : b->locations ())
5478 {
5479 /* For hardware watchpoints, we look only at the first
5480 location. The watchpoint_check function will work on the
5481 entire expression, not the individual locations. For
5482 read watchpoints, the watchpoints_triggered function has
5483 checked all locations already. */
5484 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5485 break;
5486
5487 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5488 continue;
5489
5490 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5491 continue;
5492
5493 /* Come here if it's a watchpoint, or if the break address
5494 matches. */
5495
5496 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
5497 explain stop. */
5498
5499 /* Assume we stop. Should we find a watchpoint that is not
5500 actually triggered, or if the condition of the breakpoint
5501 evaluates as false, we'll reset 'stop' to 0. */
5502 bs->stop = 1;
5503 bs->print = 1;
5504
5505 /* If this is a scope breakpoint, mark the associated
5506 watchpoint as triggered so that we will handle the
5507 out-of-scope event. We'll get to the watchpoint next
5508 iteration. */
5509 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5510 {
5511 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5512
5513 w->watchpoint_triggered = watch_triggered_yes;
5514 }
5515 }
5516 }
5517
5518 /* Check if a moribund breakpoint explains the stop. */
5519 if (!target_supports_stopped_by_sw_breakpoint ()
5520 || !target_supports_stopped_by_hw_breakpoint ())
5521 {
5522 for (bp_location *loc : moribund_locations)
5523 {
5524 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5525 && need_moribund_for_location_type (loc))
5526 {
5527 bpstat *bs = new bpstat (loc, &bs_link);
5528 /* For hits of moribund locations, we should just proceed. */
5529 bs->stop = 0;
5530 bs->print = 0;
5531 bs->print_it = print_it_noop;
5532 }
5533 }
5534 }
5535
5536 return bs_head;
5537 }
5538
5539 /* See breakpoint.h. */
5540
5541 bpstat *
5542 bpstat_stop_status (const address_space *aspace,
5543 CORE_ADDR bp_addr, thread_info *thread,
5544 const target_waitstatus &ws,
5545 bpstat *stop_chain)
5546 {
5547 struct breakpoint *b = NULL;
5548 /* First item of allocated bpstat's. */
5549 bpstat *bs_head = stop_chain;
5550 bpstat *bs;
5551 int need_remove_insert;
5552 int removed_any;
5553
5554 /* First, build the bpstat chain with locations that explain a
5555 target stop, while being careful to not set the target running,
5556 as that may invalidate locations (in particular watchpoint
5557 locations are recreated). Resuming will happen here with
5558 breakpoint conditions or watchpoint expressions that include
5559 inferior function calls. */
5560 if (bs_head == NULL)
5561 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5562
5563 /* A bit of special processing for shlib breakpoints. We need to
5564 process solib loading here, so that the lists of loaded and
5565 unloaded libraries are correct before we handle "catch load" and
5566 "catch unload". */
5567 for (bs = bs_head; bs != NULL; bs = bs->next)
5568 {
5569 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5570 {
5571 handle_solib_event ();
5572 break;
5573 }
5574 }
5575
5576 /* Now go through the locations that caused the target to stop, and
5577 check whether we're interested in reporting this stop to higher
5578 layers, or whether we should resume the target transparently. */
5579
5580 removed_any = 0;
5581
5582 for (bs = bs_head; bs != NULL; bs = bs->next)
5583 {
5584 if (!bs->stop)
5585 continue;
5586
5587 b = bs->breakpoint_at;
5588 b->check_status (bs);
5589 if (bs->stop)
5590 {
5591 bpstat_check_breakpoint_conditions (bs, thread);
5592
5593 if (bs->stop)
5594 {
5595 ++(b->hit_count);
5596
5597 /* We will stop here. */
5598 if (b->disposition == disp_disable)
5599 {
5600 --(b->enable_count);
5601 if (b->enable_count <= 0)
5602 b->enable_state = bp_disabled;
5603 removed_any = 1;
5604 }
5605 gdb::observers::breakpoint_modified.notify (b);
5606 if (b->silent)
5607 bs->print = 0;
5608 bs->commands = b->commands;
5609 if (command_line_is_silent (bs->commands
5610 ? bs->commands.get () : NULL))
5611 bs->print = 0;
5612
5613 b->after_condition_true (bs);
5614 }
5615
5616 }
5617
5618 /* Print nothing for this entry if we don't stop or don't
5619 print. */
5620 if (!bs->stop || !bs->print)
5621 bs->print_it = print_it_noop;
5622 }
5623
5624 /* If we aren't stopping, the value of some hardware watchpoint may
5625 not have changed, but the intermediate memory locations we are
5626 watching may have. Don't bother if we're stopping; this will get
5627 done later. */
5628 need_remove_insert = 0;
5629 if (! bpstat_causes_stop (bs_head))
5630 for (bs = bs_head; bs != NULL; bs = bs->next)
5631 if (!bs->stop
5632 && bs->breakpoint_at
5633 && is_hardware_watchpoint (bs->breakpoint_at))
5634 {
5635 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5636
5637 update_watchpoint (w, 0 /* don't reparse. */);
5638 need_remove_insert = 1;
5639 }
5640
5641 if (need_remove_insert)
5642 update_global_location_list (UGLL_MAY_INSERT);
5643 else if (removed_any)
5644 update_global_location_list (UGLL_DONT_INSERT);
5645
5646 return bs_head;
5647 }
5648
5649 /* See breakpoint.h. */
5650
5651 bpstat *
5652 bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5653 thread_info *thread, const target_waitstatus &ws)
5654 {
5655 gdb_assert (!target_stopped_by_watchpoint ());
5656
5657 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5658 previous stop to avoid confusing bpstat_stop_status. */
5659 watchpoints_triggered (ws);
5660
5661 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5662 }
5663
5664 static void
5665 handle_jit_event (CORE_ADDR address)
5666 {
5667 struct gdbarch *gdbarch;
5668
5669 infrun_debug_printf ("handling bp_jit_event");
5670
5671 /* Switch terminal for any messages produced by
5672 breakpoint_re_set. */
5673 target_terminal::ours_for_output ();
5674
5675 gdbarch = get_frame_arch (get_current_frame ());
5676 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5677 thus it is expected that its objectfile can be found through
5678 minimal symbol lookup. If it doesn't work (and assert fails), it
5679 most likely means that `jit_breakpoint_re_set` was changes and this
5680 function needs to be updated too. */
5681 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5682 gdb_assert (jit_bp_sym.objfile != nullptr);
5683 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5684
5685 target_terminal::inferior ();
5686 }
5687
5688 /* Prepare WHAT final decision for infrun. */
5689
5690 /* Decide what infrun needs to do with this bpstat. */
5691
5692 struct bpstat_what
5693 bpstat_what (bpstat *bs_head)
5694 {
5695 struct bpstat_what retval;
5696 bpstat *bs;
5697
5698 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5699 retval.call_dummy = STOP_NONE;
5700 retval.is_longjmp = false;
5701
5702 for (bs = bs_head; bs != NULL; bs = bs->next)
5703 {
5704 /* Extract this BS's action. After processing each BS, we check
5705 if its action overrides all we've seem so far. */
5706 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5707 enum bptype bptype;
5708
5709 if (bs->breakpoint_at == NULL)
5710 {
5711 /* I suspect this can happen if it was a momentary
5712 breakpoint which has since been deleted. */
5713 bptype = bp_none;
5714 }
5715 else
5716 bptype = bs->breakpoint_at->type;
5717
5718 switch (bptype)
5719 {
5720 case bp_none:
5721 break;
5722 case bp_breakpoint:
5723 case bp_hardware_breakpoint:
5724 case bp_single_step:
5725 case bp_until:
5726 case bp_finish:
5727 case bp_shlib_event:
5728 if (bs->stop)
5729 {
5730 if (bs->print)
5731 this_action = BPSTAT_WHAT_STOP_NOISY;
5732 else
5733 this_action = BPSTAT_WHAT_STOP_SILENT;
5734 }
5735 else
5736 this_action = BPSTAT_WHAT_SINGLE;
5737 break;
5738 case bp_watchpoint:
5739 case bp_hardware_watchpoint:
5740 case bp_read_watchpoint:
5741 case bp_access_watchpoint:
5742 if (bs->stop)
5743 {
5744 if (bs->print)
5745 this_action = BPSTAT_WHAT_STOP_NOISY;
5746 else
5747 this_action = BPSTAT_WHAT_STOP_SILENT;
5748 }
5749 else
5750 {
5751 /* There was a watchpoint, but we're not stopping.
5752 This requires no further action. */
5753 }
5754 break;
5755 case bp_longjmp:
5756 case bp_longjmp_call_dummy:
5757 case bp_exception:
5758 if (bs->stop)
5759 {
5760 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5761 retval.is_longjmp = bptype != bp_exception;
5762 }
5763 else
5764 this_action = BPSTAT_WHAT_SINGLE;
5765 break;
5766 case bp_longjmp_resume:
5767 case bp_exception_resume:
5768 if (bs->stop)
5769 {
5770 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5771 retval.is_longjmp = bptype == bp_longjmp_resume;
5772 }
5773 else
5774 this_action = BPSTAT_WHAT_SINGLE;
5775 break;
5776 case bp_step_resume:
5777 if (bs->stop)
5778 this_action = BPSTAT_WHAT_STEP_RESUME;
5779 else
5780 {
5781 /* It is for the wrong frame. */
5782 this_action = BPSTAT_WHAT_SINGLE;
5783 }
5784 break;
5785 case bp_hp_step_resume:
5786 if (bs->stop)
5787 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5788 else
5789 {
5790 /* It is for the wrong frame. */
5791 this_action = BPSTAT_WHAT_SINGLE;
5792 }
5793 break;
5794 case bp_watchpoint_scope:
5795 case bp_thread_event:
5796 case bp_overlay_event:
5797 case bp_longjmp_master:
5798 case bp_std_terminate_master:
5799 case bp_exception_master:
5800 this_action = BPSTAT_WHAT_SINGLE;
5801 break;
5802 case bp_catchpoint:
5803 if (bs->stop)
5804 {
5805 if (bs->print)
5806 this_action = BPSTAT_WHAT_STOP_NOISY;
5807 else
5808 this_action = BPSTAT_WHAT_STOP_SILENT;
5809 }
5810 else
5811 {
5812 /* Some catchpoints are implemented with breakpoints.
5813 For those, we need to step over the breakpoint. */
5814 if (bs->bp_location_at->loc_type == bp_loc_software_breakpoint
5815 || bs->bp_location_at->loc_type == bp_loc_hardware_breakpoint)
5816 this_action = BPSTAT_WHAT_SINGLE;
5817 }
5818 break;
5819 case bp_jit_event:
5820 this_action = BPSTAT_WHAT_SINGLE;
5821 break;
5822 case bp_call_dummy:
5823 /* Make sure the action is stop (silent or noisy),
5824 so infrun.c pops the dummy frame. */
5825 retval.call_dummy = STOP_STACK_DUMMY;
5826 this_action = BPSTAT_WHAT_STOP_SILENT;
5827 break;
5828 case bp_std_terminate:
5829 /* Make sure the action is stop (silent or noisy),
5830 so infrun.c pops the dummy frame. */
5831 retval.call_dummy = STOP_STD_TERMINATE;
5832 this_action = BPSTAT_WHAT_STOP_SILENT;
5833 break;
5834 case bp_tracepoint:
5835 case bp_fast_tracepoint:
5836 case bp_static_tracepoint:
5837 case bp_static_marker_tracepoint:
5838 /* Tracepoint hits should not be reported back to GDB, and
5839 if one got through somehow, it should have been filtered
5840 out already. */
5841 internal_error (__FILE__, __LINE__,
5842 _("bpstat_what: tracepoint encountered"));
5843 break;
5844 case bp_gnu_ifunc_resolver:
5845 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5846 this_action = BPSTAT_WHAT_SINGLE;
5847 break;
5848 case bp_gnu_ifunc_resolver_return:
5849 /* The breakpoint will be removed, execution will restart from the
5850 PC of the former breakpoint. */
5851 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5852 break;
5853
5854 case bp_dprintf:
5855 if (bs->stop)
5856 this_action = BPSTAT_WHAT_STOP_SILENT;
5857 else
5858 this_action = BPSTAT_WHAT_SINGLE;
5859 break;
5860
5861 default:
5862 internal_error (__FILE__, __LINE__,
5863 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5864 }
5865
5866 retval.main_action = std::max (retval.main_action, this_action);
5867 }
5868
5869 return retval;
5870 }
5871
5872 void
5873 bpstat_run_callbacks (bpstat *bs_head)
5874 {
5875 bpstat *bs;
5876
5877 for (bs = bs_head; bs != NULL; bs = bs->next)
5878 {
5879 struct breakpoint *b = bs->breakpoint_at;
5880
5881 if (b == NULL)
5882 continue;
5883 switch (b->type)
5884 {
5885 case bp_jit_event:
5886 handle_jit_event (bs->bp_location_at->address);
5887 break;
5888 case bp_gnu_ifunc_resolver:
5889 gnu_ifunc_resolver_stop ((code_breakpoint *) b);
5890 break;
5891 case bp_gnu_ifunc_resolver_return:
5892 gnu_ifunc_resolver_return_stop ((code_breakpoint *) b);
5893 break;
5894 }
5895 }
5896 }
5897
5898 /* See breakpoint.h. */
5899
5900 bool
5901 bpstat_should_step ()
5902 {
5903 for (breakpoint *b : all_breakpoints ())
5904 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5905 return true;
5906
5907 return false;
5908 }
5909
5910 /* See breakpoint.h. */
5911
5912 bool
5913 bpstat_causes_stop (bpstat *bs)
5914 {
5915 for (; bs != NULL; bs = bs->next)
5916 if (bs->stop)
5917 return true;
5918
5919 return false;
5920 }
5921
5922 \f
5923
5924 /* Compute a number of spaces suitable to indent the next line
5925 so it starts at the position corresponding to the table column
5926 named COL_NAME in the currently active table of UIOUT. */
5927
5928 static int
5929 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5930 {
5931 int i, total_width, width, align;
5932 const char *text;
5933
5934 total_width = 0;
5935 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5936 {
5937 if (strcmp (text, col_name) == 0)
5938 return total_width;
5939
5940 total_width += width + 1;
5941 }
5942
5943 return 0;
5944 }
5945
5946 /* Determine if the locations of this breakpoint will have their conditions
5947 evaluated by the target, host or a mix of both. Returns the following:
5948
5949 "host": Host evals condition.
5950 "host or target": Host or Target evals condition.
5951 "target": Target evals condition.
5952 */
5953
5954 static const char *
5955 bp_condition_evaluator (const breakpoint *b)
5956 {
5957 char host_evals = 0;
5958 char target_evals = 0;
5959
5960 if (!b)
5961 return NULL;
5962
5963 if (!is_breakpoint (b))
5964 return NULL;
5965
5966 if (gdb_evaluates_breakpoint_condition_p ()
5967 || !target_supports_evaluation_of_breakpoint_conditions ())
5968 return condition_evaluation_host;
5969
5970 for (bp_location *bl : b->locations ())
5971 {
5972 if (bl->cond_bytecode)
5973 target_evals++;
5974 else
5975 host_evals++;
5976 }
5977
5978 if (host_evals && target_evals)
5979 return condition_evaluation_both;
5980 else if (target_evals)
5981 return condition_evaluation_target;
5982 else
5983 return condition_evaluation_host;
5984 }
5985
5986 /* Determine the breakpoint location's condition evaluator. This is
5987 similar to bp_condition_evaluator, but for locations. */
5988
5989 static const char *
5990 bp_location_condition_evaluator (struct bp_location *bl)
5991 {
5992 if (bl && !is_breakpoint (bl->owner))
5993 return NULL;
5994
5995 if (gdb_evaluates_breakpoint_condition_p ()
5996 || !target_supports_evaluation_of_breakpoint_conditions ())
5997 return condition_evaluation_host;
5998
5999 if (bl && bl->cond_bytecode)
6000 return condition_evaluation_target;
6001 else
6002 return condition_evaluation_host;
6003 }
6004
6005 /* Print the LOC location out of the list of B->LOC locations. */
6006
6007 static void
6008 print_breakpoint_location (const breakpoint *b,
6009 struct bp_location *loc)
6010 {
6011 struct ui_out *uiout = current_uiout;
6012
6013 scoped_restore_current_program_space restore_pspace;
6014
6015 if (loc != NULL && loc->shlib_disabled)
6016 loc = NULL;
6017
6018 if (loc != NULL)
6019 set_current_program_space (loc->pspace);
6020
6021 if (b->display_canonical)
6022 uiout->field_string ("what", b->locspec->to_string ());
6023 else if (loc && loc->symtab)
6024 {
6025 const struct symbol *sym = loc->symbol;
6026
6027 if (sym)
6028 {
6029 uiout->text ("in ");
6030 uiout->field_string ("func", sym->print_name (),
6031 function_name_style.style ());
6032 uiout->text (" ");
6033 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6034 uiout->text ("at ");
6035 }
6036 uiout->field_string ("file",
6037 symtab_to_filename_for_display (loc->symtab),
6038 file_name_style.style ());
6039 uiout->text (":");
6040
6041 if (uiout->is_mi_like_p ())
6042 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6043
6044 uiout->field_signed ("line", loc->line_number);
6045 }
6046 else if (loc)
6047 {
6048 string_file stb;
6049
6050 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6051 demangle, "");
6052 uiout->field_stream ("at", stb);
6053 }
6054 else
6055 {
6056 uiout->field_string ("pending", b->locspec->to_string ());
6057 /* If extra_string is available, it could be holding a condition
6058 or dprintf arguments. In either case, make sure it is printed,
6059 too, but only for non-MI streams. */
6060 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6061 {
6062 if (b->type == bp_dprintf)
6063 uiout->text (",");
6064 else
6065 uiout->text (" ");
6066 uiout->text (b->extra_string.get ());
6067 }
6068 }
6069
6070 if (loc && is_breakpoint (b)
6071 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6072 && bp_condition_evaluator (b) == condition_evaluation_both)
6073 {
6074 uiout->text (" (");
6075 uiout->field_string ("evaluated-by",
6076 bp_location_condition_evaluator (loc));
6077 uiout->text (")");
6078 }
6079 }
6080
6081 static const char *
6082 bptype_string (enum bptype type)
6083 {
6084 struct ep_type_description
6085 {
6086 enum bptype type;
6087 const char *description;
6088 };
6089 static struct ep_type_description bptypes[] =
6090 {
6091 {bp_none, "?deleted?"},
6092 {bp_breakpoint, "breakpoint"},
6093 {bp_hardware_breakpoint, "hw breakpoint"},
6094 {bp_single_step, "sw single-step"},
6095 {bp_until, "until"},
6096 {bp_finish, "finish"},
6097 {bp_watchpoint, "watchpoint"},
6098 {bp_hardware_watchpoint, "hw watchpoint"},
6099 {bp_read_watchpoint, "read watchpoint"},
6100 {bp_access_watchpoint, "acc watchpoint"},
6101 {bp_longjmp, "longjmp"},
6102 {bp_longjmp_resume, "longjmp resume"},
6103 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6104 {bp_exception, "exception"},
6105 {bp_exception_resume, "exception resume"},
6106 {bp_step_resume, "step resume"},
6107 {bp_hp_step_resume, "high-priority step resume"},
6108 {bp_watchpoint_scope, "watchpoint scope"},
6109 {bp_call_dummy, "call dummy"},
6110 {bp_std_terminate, "std::terminate"},
6111 {bp_shlib_event, "shlib events"},
6112 {bp_thread_event, "thread events"},
6113 {bp_overlay_event, "overlay events"},
6114 {bp_longjmp_master, "longjmp master"},
6115 {bp_std_terminate_master, "std::terminate master"},
6116 {bp_exception_master, "exception master"},
6117 {bp_catchpoint, "catchpoint"},
6118 {bp_tracepoint, "tracepoint"},
6119 {bp_fast_tracepoint, "fast tracepoint"},
6120 {bp_static_tracepoint, "static tracepoint"},
6121 {bp_static_marker_tracepoint, "static marker tracepoint"},
6122 {bp_dprintf, "dprintf"},
6123 {bp_jit_event, "jit events"},
6124 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6125 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6126 };
6127
6128 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6129 || ((int) type != bptypes[(int) type].type))
6130 internal_error (__FILE__, __LINE__,
6131 _("bptypes table does not describe type #%d."),
6132 (int) type);
6133
6134 return bptypes[(int) type].description;
6135 }
6136
6137 /* For MI, output a field named 'thread-groups' with a list as the value.
6138 For CLI, prefix the list with the string 'inf'. */
6139
6140 static void
6141 output_thread_groups (struct ui_out *uiout,
6142 const char *field_name,
6143 const std::vector<int> &inf_nums,
6144 int mi_only)
6145 {
6146 int is_mi = uiout->is_mi_like_p ();
6147
6148 /* For backward compatibility, don't display inferiors in CLI unless
6149 there are several. Always display them for MI. */
6150 if (!is_mi && mi_only)
6151 return;
6152
6153 ui_out_emit_list list_emitter (uiout, field_name);
6154
6155 for (size_t i = 0; i < inf_nums.size (); i++)
6156 {
6157 if (is_mi)
6158 {
6159 char mi_group[10];
6160
6161 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6162 uiout->field_string (NULL, mi_group);
6163 }
6164 else
6165 {
6166 if (i == 0)
6167 uiout->text (" inf ");
6168 else
6169 uiout->text (", ");
6170
6171 uiout->text (plongest (inf_nums[i]));
6172 }
6173 }
6174 }
6175
6176 /* See breakpoint.h. */
6177
6178 bool fix_breakpoint_script_output_globally = false;
6179
6180 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6181 instead of going via breakpoint_ops::print_one. This makes "maint
6182 info breakpoints" show the software breakpoint locations of
6183 catchpoints, which are considered internal implementation
6184 detail. Returns true if RAW_LOC is false and if the breakpoint's
6185 print_one method did something; false otherwise. */
6186
6187 static bool
6188 print_one_breakpoint_location (struct breakpoint *b,
6189 struct bp_location *loc,
6190 int loc_number,
6191 struct bp_location **last_loc,
6192 int allflag, bool raw_loc)
6193 {
6194 struct command_line *l;
6195 static char bpenables[] = "nynny";
6196
6197 struct ui_out *uiout = current_uiout;
6198 int header_of_multiple = 0;
6199 int part_of_multiple = (loc != NULL);
6200 struct value_print_options opts;
6201
6202 get_user_print_options (&opts);
6203
6204 gdb_assert (!loc || loc_number != 0);
6205 /* See comment in print_one_breakpoint concerning treatment of
6206 breakpoints with single disabled location. */
6207 if (loc == NULL
6208 && (b->loc != NULL
6209 && (b->loc->next != NULL
6210 || !b->loc->enabled || b->loc->disabled_by_cond)))
6211 header_of_multiple = 1;
6212 if (loc == NULL)
6213 loc = b->loc;
6214
6215 annotate_record ();
6216
6217 /* 1 */
6218 annotate_field (0);
6219 if (part_of_multiple)
6220 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6221 else
6222 uiout->field_signed ("number", b->number);
6223
6224 /* 2 */
6225 annotate_field (1);
6226 if (part_of_multiple)
6227 uiout->field_skip ("type");
6228 else
6229 uiout->field_string ("type", bptype_string (b->type));
6230
6231 /* 3 */
6232 annotate_field (2);
6233 if (part_of_multiple)
6234 uiout->field_skip ("disp");
6235 else
6236 uiout->field_string ("disp", bpdisp_text (b->disposition));
6237
6238 /* 4 */
6239 annotate_field (3);
6240 if (part_of_multiple)
6241 {
6242 /* For locations that are disabled because of an invalid
6243 condition, display "N*" on the CLI, where "*" refers to a
6244 footnote below the table. For MI, simply display a "N"
6245 without a footnote. On the CLI, for enabled locations whose
6246 breakpoint is disabled, display "y-". */
6247 auto get_enable_state = [uiout, loc] () -> const char *
6248 {
6249 if (uiout->is_mi_like_p ())
6250 {
6251 if (loc->disabled_by_cond)
6252 return "N";
6253 else if (!loc->enabled)
6254 return "n";
6255 else
6256 return "y";
6257 }
6258 else
6259 {
6260 if (loc->disabled_by_cond)
6261 return "N*";
6262 else if (!loc->enabled)
6263 return "n";
6264 else if (!breakpoint_enabled (loc->owner))
6265 return "y-";
6266 else
6267 return "y";
6268 }
6269 };
6270 uiout->field_string ("enabled", get_enable_state ());
6271 }
6272 else
6273 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6274
6275 /* 5 and 6 */
6276 bool result = false;
6277 if (!raw_loc && b->print_one (last_loc))
6278 result = true;
6279 else
6280 {
6281 if (is_watchpoint (b))
6282 {
6283 struct watchpoint *w = (struct watchpoint *) b;
6284
6285 /* Field 4, the address, is omitted (which makes the columns
6286 not line up too nicely with the headers, but the effect
6287 is relatively readable). */
6288 if (opts.addressprint)
6289 uiout->field_skip ("addr");
6290 annotate_field (5);
6291 uiout->field_string ("what", w->exp_string.get ());
6292 }
6293 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6294 || is_ada_exception_catchpoint (b))
6295 {
6296 if (opts.addressprint)
6297 {
6298 annotate_field (4);
6299 if (header_of_multiple)
6300 uiout->field_string ("addr", "<MULTIPLE>",
6301 metadata_style.style ());
6302 else if (b->loc == NULL || loc->shlib_disabled)
6303 uiout->field_string ("addr", "<PENDING>",
6304 metadata_style.style ());
6305 else
6306 uiout->field_core_addr ("addr",
6307 loc->gdbarch, loc->address);
6308 }
6309 annotate_field (5);
6310 if (!header_of_multiple)
6311 print_breakpoint_location (b, loc);
6312 if (b->loc)
6313 *last_loc = b->loc;
6314 }
6315 }
6316
6317 if (loc != NULL && !header_of_multiple)
6318 {
6319 std::vector<int> inf_nums;
6320 int mi_only = 1;
6321
6322 for (inferior *inf : all_inferiors ())
6323 {
6324 if (inf->pspace == loc->pspace)
6325 inf_nums.push_back (inf->num);
6326 }
6327
6328 /* For backward compatibility, don't display inferiors in CLI unless
6329 there are several. Always display for MI. */
6330 if (allflag
6331 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6332 && (program_spaces.size () > 1
6333 || number_of_inferiors () > 1)
6334 /* LOC is for existing B, it cannot be in
6335 moribund_locations and thus having NULL OWNER. */
6336 && loc->owner->type != bp_catchpoint))
6337 mi_only = 0;
6338 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6339 }
6340
6341 if (!part_of_multiple)
6342 {
6343 if (b->thread != -1)
6344 {
6345 /* FIXME: This seems to be redundant and lost here; see the
6346 "stop only in" line a little further down. */
6347 uiout->text (" thread ");
6348 uiout->field_signed ("thread", b->thread);
6349 }
6350 else if (b->task != 0)
6351 {
6352 uiout->text (" task ");
6353 uiout->field_signed ("task", b->task);
6354 }
6355 }
6356
6357 uiout->text ("\n");
6358
6359 if (!part_of_multiple)
6360 b->print_one_detail (uiout);
6361
6362 if (part_of_multiple && frame_id_p (b->frame_id))
6363 {
6364 annotate_field (6);
6365 uiout->text ("\tstop only in stack frame at ");
6366 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6367 the frame ID. */
6368 uiout->field_core_addr ("frame",
6369 b->gdbarch, b->frame_id.stack_addr);
6370 uiout->text ("\n");
6371 }
6372
6373 if (!part_of_multiple && b->cond_string)
6374 {
6375 annotate_field (7);
6376 if (is_tracepoint (b))
6377 uiout->text ("\ttrace only if ");
6378 else
6379 uiout->text ("\tstop only if ");
6380 uiout->field_string ("cond", b->cond_string.get ());
6381
6382 /* Print whether the target is doing the breakpoint's condition
6383 evaluation. If GDB is doing the evaluation, don't print anything. */
6384 if (is_breakpoint (b)
6385 && breakpoint_condition_evaluation_mode ()
6386 == condition_evaluation_target)
6387 {
6388 uiout->message (" (%pF evals)",
6389 string_field ("evaluated-by",
6390 bp_condition_evaluator (b)));
6391 }
6392 uiout->text ("\n");
6393 }
6394
6395 if (!part_of_multiple && b->thread != -1)
6396 {
6397 /* FIXME should make an annotation for this. */
6398 uiout->text ("\tstop only in thread ");
6399 if (uiout->is_mi_like_p ())
6400 uiout->field_signed ("thread", b->thread);
6401 else
6402 {
6403 struct thread_info *thr = find_thread_global_id (b->thread);
6404
6405 uiout->field_string ("thread", print_thread_id (thr));
6406 }
6407 uiout->text ("\n");
6408 }
6409
6410 if (!part_of_multiple)
6411 {
6412 if (b->hit_count)
6413 {
6414 /* FIXME should make an annotation for this. */
6415 if (is_catchpoint (b))
6416 uiout->text ("\tcatchpoint");
6417 else if (is_tracepoint (b))
6418 uiout->text ("\ttracepoint");
6419 else
6420 uiout->text ("\tbreakpoint");
6421 uiout->text (" already hit ");
6422 uiout->field_signed ("times", b->hit_count);
6423 if (b->hit_count == 1)
6424 uiout->text (" time\n");
6425 else
6426 uiout->text (" times\n");
6427 }
6428 else
6429 {
6430 /* Output the count also if it is zero, but only if this is mi. */
6431 if (uiout->is_mi_like_p ())
6432 uiout->field_signed ("times", b->hit_count);
6433 }
6434 }
6435
6436 if (!part_of_multiple && b->ignore_count)
6437 {
6438 annotate_field (8);
6439 uiout->message ("\tignore next %pF hits\n",
6440 signed_field ("ignore", b->ignore_count));
6441 }
6442
6443 /* Note that an enable count of 1 corresponds to "enable once"
6444 behavior, which is reported by the combination of enablement and
6445 disposition, so we don't need to mention it here. */
6446 if (!part_of_multiple && b->enable_count > 1)
6447 {
6448 annotate_field (8);
6449 uiout->text ("\tdisable after ");
6450 /* Tweak the wording to clarify that ignore and enable counts
6451 are distinct, and have additive effect. */
6452 if (b->ignore_count)
6453 uiout->text ("additional ");
6454 else
6455 uiout->text ("next ");
6456 uiout->field_signed ("enable", b->enable_count);
6457 uiout->text (" hits\n");
6458 }
6459
6460 if (!part_of_multiple && is_tracepoint (b))
6461 {
6462 struct tracepoint *tp = (struct tracepoint *) b;
6463
6464 if (tp->traceframe_usage)
6465 {
6466 uiout->text ("\ttrace buffer usage ");
6467 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6468 uiout->text (" bytes\n");
6469 }
6470 }
6471
6472 l = b->commands ? b->commands.get () : NULL;
6473 if (!part_of_multiple && l)
6474 {
6475 annotate_field (9);
6476
6477 bool use_fixed_output =
6478 (uiout->test_flags (fix_breakpoint_script_output)
6479 || fix_breakpoint_script_output_globally);
6480
6481 gdb::optional<ui_out_emit_tuple> tuple_emitter;
6482 gdb::optional<ui_out_emit_list> list_emitter;
6483
6484 if (use_fixed_output)
6485 list_emitter.emplace (uiout, "script");
6486 else
6487 tuple_emitter.emplace (uiout, "script");
6488
6489 print_command_lines (uiout, l, 4);
6490 }
6491
6492 if (is_tracepoint (b))
6493 {
6494 struct tracepoint *t = (struct tracepoint *) b;
6495
6496 if (!part_of_multiple && t->pass_count)
6497 {
6498 annotate_field (10);
6499 uiout->text ("\tpass count ");
6500 uiout->field_signed ("pass", t->pass_count);
6501 uiout->text (" \n");
6502 }
6503
6504 /* Don't display it when tracepoint or tracepoint location is
6505 pending. */
6506 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6507 {
6508 annotate_field (11);
6509
6510 if (uiout->is_mi_like_p ())
6511 uiout->field_string ("installed",
6512 loc->inserted ? "y" : "n");
6513 else
6514 {
6515 if (loc->inserted)
6516 uiout->text ("\t");
6517 else
6518 uiout->text ("\tnot ");
6519 uiout->text ("installed on target\n");
6520 }
6521 }
6522 }
6523
6524 if (uiout->is_mi_like_p () && !part_of_multiple)
6525 {
6526 if (is_watchpoint (b))
6527 {
6528 struct watchpoint *w = (struct watchpoint *) b;
6529
6530 uiout->field_string ("original-location", w->exp_string.get ());
6531 }
6532 else if (b->locspec != nullptr)
6533 {
6534 const char *str = b->locspec->to_string ();
6535 if (str != nullptr)
6536 uiout->field_string ("original-location", str);
6537 }
6538 }
6539
6540 return result;
6541 }
6542
6543 /* See breakpoint.h. */
6544
6545 bool fix_multi_location_breakpoint_output_globally = false;
6546
6547 static void
6548 print_one_breakpoint (struct breakpoint *b,
6549 struct bp_location **last_loc,
6550 int allflag)
6551 {
6552 struct ui_out *uiout = current_uiout;
6553 bool use_fixed_output
6554 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6555 || fix_multi_location_breakpoint_output_globally);
6556
6557 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6558 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6559 allflag, false);
6560
6561 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6562 are outside. */
6563 if (!use_fixed_output)
6564 bkpt_tuple_emitter.reset ();
6565
6566 /* If this breakpoint has custom print function,
6567 it's already printed. Otherwise, print individual
6568 locations, if any. */
6569 if (!printed || allflag)
6570 {
6571 /* If breakpoint has a single location that is disabled, we
6572 print it as if it had several locations, since otherwise it's
6573 hard to represent "breakpoint enabled, location disabled"
6574 situation.
6575
6576 Note that while hardware watchpoints have several locations
6577 internally, that's not a property exposed to users.
6578
6579 Likewise, while catchpoints may be implemented with
6580 breakpoints (e.g., catch throw), that's not a property
6581 exposed to users. We do however display the internal
6582 breakpoint locations with "maint info breakpoints". */
6583 if (!is_hardware_watchpoint (b)
6584 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6585 || is_ada_exception_catchpoint (b))
6586 && (allflag
6587 || (b->loc && (b->loc->next
6588 || !b->loc->enabled
6589 || b->loc->disabled_by_cond))))
6590 {
6591 gdb::optional<ui_out_emit_list> locations_list;
6592
6593 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6594 MI record. For later versions, place breakpoint locations in a
6595 list. */
6596 if (uiout->is_mi_like_p () && use_fixed_output)
6597 locations_list.emplace (uiout, "locations");
6598
6599 int n = 1;
6600 for (bp_location *loc : b->locations ())
6601 {
6602 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6603 print_one_breakpoint_location (b, loc, n, last_loc,
6604 allflag, allflag);
6605 n++;
6606 }
6607 }
6608 }
6609 }
6610
6611 static int
6612 breakpoint_address_bits (struct breakpoint *b)
6613 {
6614 int print_address_bits = 0;
6615
6616 for (bp_location *loc : b->locations ())
6617 {
6618 if (!bl_address_is_meaningful (loc))
6619 continue;
6620
6621 int addr_bit = gdbarch_addr_bit (loc->gdbarch);
6622 if (addr_bit > print_address_bits)
6623 print_address_bits = addr_bit;
6624 }
6625
6626 return print_address_bits;
6627 }
6628
6629 /* See breakpoint.h. */
6630
6631 void
6632 print_breakpoint (breakpoint *b)
6633 {
6634 struct bp_location *dummy_loc = NULL;
6635 print_one_breakpoint (b, &dummy_loc, 0);
6636 }
6637
6638 /* Return true if this breakpoint was set by the user, false if it is
6639 internal or momentary. */
6640
6641 int
6642 user_breakpoint_p (struct breakpoint *b)
6643 {
6644 return b->number > 0;
6645 }
6646
6647 /* See breakpoint.h. */
6648
6649 int
6650 pending_breakpoint_p (struct breakpoint *b)
6651 {
6652 return b->loc == NULL;
6653 }
6654
6655 /* Print information on breakpoints (including watchpoints and tracepoints).
6656
6657 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6658 understood by number_or_range_parser. Only breakpoints included in this
6659 list are then printed.
6660
6661 If SHOW_INTERNAL is true, print internal breakpoints.
6662
6663 If FILTER is non-NULL, call it on each breakpoint and only include the
6664 ones for which it returns true.
6665
6666 Return the total number of breakpoints listed. */
6667
6668 static int
6669 breakpoint_1 (const char *bp_num_list, bool show_internal,
6670 bool (*filter) (const struct breakpoint *))
6671 {
6672 struct bp_location *last_loc = NULL;
6673 int nr_printable_breakpoints;
6674 struct value_print_options opts;
6675 int print_address_bits = 0;
6676 int print_type_col_width = 14;
6677 struct ui_out *uiout = current_uiout;
6678 bool has_disabled_by_cond_location = false;
6679
6680 get_user_print_options (&opts);
6681
6682 /* Compute the number of rows in the table, as well as the size
6683 required for address fields. */
6684 nr_printable_breakpoints = 0;
6685 for (breakpoint *b : all_breakpoints ())
6686 {
6687 /* If we have a filter, only list the breakpoints it accepts. */
6688 if (filter && !filter (b))
6689 continue;
6690
6691 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6692 accept. Skip the others. */
6693 if (bp_num_list != NULL && *bp_num_list != '\0')
6694 {
6695 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6696 continue;
6697 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6698 continue;
6699 }
6700
6701 if (show_internal || user_breakpoint_p (b))
6702 {
6703 int addr_bit, type_len;
6704
6705 addr_bit = breakpoint_address_bits (b);
6706 if (addr_bit > print_address_bits)
6707 print_address_bits = addr_bit;
6708
6709 type_len = strlen (bptype_string (b->type));
6710 if (type_len > print_type_col_width)
6711 print_type_col_width = type_len;
6712
6713 nr_printable_breakpoints++;
6714 }
6715 }
6716
6717 {
6718 ui_out_emit_table table_emitter (uiout,
6719 opts.addressprint ? 6 : 5,
6720 nr_printable_breakpoints,
6721 "BreakpointTable");
6722
6723 if (nr_printable_breakpoints > 0)
6724 annotate_breakpoints_headers ();
6725 if (nr_printable_breakpoints > 0)
6726 annotate_field (0);
6727 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6728 if (nr_printable_breakpoints > 0)
6729 annotate_field (1);
6730 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6731 if (nr_printable_breakpoints > 0)
6732 annotate_field (2);
6733 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6734 if (nr_printable_breakpoints > 0)
6735 annotate_field (3);
6736 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6737 if (opts.addressprint)
6738 {
6739 if (nr_printable_breakpoints > 0)
6740 annotate_field (4);
6741 if (print_address_bits <= 32)
6742 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6743 else
6744 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6745 }
6746 if (nr_printable_breakpoints > 0)
6747 annotate_field (5);
6748 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6749 uiout->table_body ();
6750 if (nr_printable_breakpoints > 0)
6751 annotate_breakpoints_table ();
6752
6753 for (breakpoint *b : all_breakpoints ())
6754 {
6755 QUIT;
6756 /* If we have a filter, only list the breakpoints it accepts. */
6757 if (filter && !filter (b))
6758 continue;
6759
6760 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6761 accept. Skip the others. */
6762
6763 if (bp_num_list != NULL && *bp_num_list != '\0')
6764 {
6765 if (show_internal) /* maintenance info breakpoint */
6766 {
6767 if (parse_and_eval_long (bp_num_list) != b->number)
6768 continue;
6769 }
6770 else /* all others */
6771 {
6772 if (!number_is_in_list (bp_num_list, b->number))
6773 continue;
6774 }
6775 }
6776 /* We only print out user settable breakpoints unless the
6777 show_internal is set. */
6778 if (show_internal || user_breakpoint_p (b))
6779 {
6780 print_one_breakpoint (b, &last_loc, show_internal);
6781 for (bp_location *loc : b->locations ())
6782 if (loc->disabled_by_cond)
6783 has_disabled_by_cond_location = true;
6784 }
6785 }
6786 }
6787
6788 if (nr_printable_breakpoints == 0)
6789 {
6790 /* If there's a filter, let the caller decide how to report
6791 empty list. */
6792 if (!filter)
6793 {
6794 if (bp_num_list == NULL || *bp_num_list == '\0')
6795 uiout->message ("No breakpoints or watchpoints.\n");
6796 else
6797 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6798 bp_num_list);
6799 }
6800 }
6801 else
6802 {
6803 if (last_loc && !server_command)
6804 set_next_address (last_loc->gdbarch, last_loc->address);
6805
6806 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6807 uiout->message (_("(*): Breakpoint condition is invalid at this "
6808 "location.\n"));
6809 }
6810
6811 /* FIXME? Should this be moved up so that it is only called when
6812 there have been breakpoints? */
6813 annotate_breakpoints_table_end ();
6814
6815 return nr_printable_breakpoints;
6816 }
6817
6818 /* Display the value of default-collect in a way that is generally
6819 compatible with the breakpoint list. */
6820
6821 static void
6822 default_collect_info (void)
6823 {
6824 struct ui_out *uiout = current_uiout;
6825
6826 /* If it has no value (which is frequently the case), say nothing; a
6827 message like "No default-collect." gets in user's face when it's
6828 not wanted. */
6829 if (default_collect.empty ())
6830 return;
6831
6832 /* The following phrase lines up nicely with per-tracepoint collect
6833 actions. */
6834 uiout->text ("default collect ");
6835 uiout->field_string ("default-collect", default_collect);
6836 uiout->text (" \n");
6837 }
6838
6839 static void
6840 info_breakpoints_command (const char *args, int from_tty)
6841 {
6842 breakpoint_1 (args, false, NULL);
6843
6844 default_collect_info ();
6845 }
6846
6847 static void
6848 info_watchpoints_command (const char *args, int from_tty)
6849 {
6850 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6851 struct ui_out *uiout = current_uiout;
6852
6853 if (num_printed == 0)
6854 {
6855 if (args == NULL || *args == '\0')
6856 uiout->message ("No watchpoints.\n");
6857 else
6858 uiout->message ("No watchpoint matching '%s'.\n", args);
6859 }
6860 }
6861
6862 static void
6863 maintenance_info_breakpoints (const char *args, int from_tty)
6864 {
6865 breakpoint_1 (args, true, NULL);
6866
6867 default_collect_info ();
6868 }
6869
6870 static int
6871 breakpoint_has_pc (struct breakpoint *b,
6872 struct program_space *pspace,
6873 CORE_ADDR pc, struct obj_section *section)
6874 {
6875 for (bp_location *bl : b->locations ())
6876 {
6877 if (bl->pspace == pspace
6878 && bl->address == pc
6879 && (!overlay_debugging || bl->section == section))
6880 return 1;
6881 }
6882 return 0;
6883 }
6884
6885 /* See breakpoint.h. */
6886
6887 void
6888 describe_other_breakpoints (struct gdbarch *gdbarch,
6889 struct program_space *pspace, CORE_ADDR pc,
6890 struct obj_section *section, int thread)
6891 {
6892 int others = 0;
6893
6894 for (breakpoint *b : all_breakpoints ())
6895 others += (user_breakpoint_p (b)
6896 && breakpoint_has_pc (b, pspace, pc, section));
6897
6898 if (others > 0)
6899 {
6900 if (others == 1)
6901 gdb_printf (_("Note: breakpoint "));
6902 else /* if (others == ???) */
6903 gdb_printf (_("Note: breakpoints "));
6904 for (breakpoint *b : all_breakpoints ())
6905 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6906 {
6907 others--;
6908 gdb_printf ("%d", b->number);
6909 if (b->thread == -1 && thread != -1)
6910 gdb_printf (" (all threads)");
6911 else if (b->thread != -1)
6912 gdb_printf (" (thread %d)", b->thread);
6913 gdb_printf ("%s%s ",
6914 ((b->enable_state == bp_disabled
6915 || b->enable_state == bp_call_disabled)
6916 ? " (disabled)"
6917 : ""),
6918 (others > 1) ? ","
6919 : ((others == 1) ? " and" : ""));
6920 }
6921 current_uiout->message (_("also set at pc %ps.\n"),
6922 styled_string (address_style.style (),
6923 paddress (gdbarch, pc)));
6924 }
6925 }
6926 \f
6927
6928 /* Return true iff it is meaningful to use the address member of LOC.
6929 For some breakpoint types, the locations' address members are
6930 irrelevant and it makes no sense to attempt to compare them to
6931 other addresses (or use them for any other purpose either).
6932
6933 More specifically, software watchpoints and catchpoints that are
6934 not backed by breakpoints always have a zero valued location
6935 address and we don't want to mark breakpoints of any of these types
6936 to be a duplicate of an actual breakpoint location at address
6937 zero. */
6938
6939 static bool
6940 bl_address_is_meaningful (bp_location *loc)
6941 {
6942 return loc->loc_type != bp_loc_other;
6943 }
6944
6945 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6946 true if LOC1 and LOC2 represent the same watchpoint location. */
6947
6948 static int
6949 watchpoint_locations_match (struct bp_location *loc1,
6950 struct bp_location *loc2)
6951 {
6952 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6953 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6954
6955 /* Both of them must exist. */
6956 gdb_assert (w1 != NULL);
6957 gdb_assert (w2 != NULL);
6958
6959 /* If the target can evaluate the condition expression in hardware,
6960 then we we need to insert both watchpoints even if they are at
6961 the same place. Otherwise the watchpoint will only trigger when
6962 the condition of whichever watchpoint was inserted evaluates to
6963 true, not giving a chance for GDB to check the condition of the
6964 other watchpoint. */
6965 if ((w1->cond_exp
6966 && target_can_accel_watchpoint_condition (loc1->address,
6967 loc1->length,
6968 loc1->watchpoint_type,
6969 w1->cond_exp.get ()))
6970 || (w2->cond_exp
6971 && target_can_accel_watchpoint_condition (loc2->address,
6972 loc2->length,
6973 loc2->watchpoint_type,
6974 w2->cond_exp.get ())))
6975 return 0;
6976
6977 /* Note that this checks the owner's type, not the location's. In
6978 case the target does not support read watchpoints, but does
6979 support access watchpoints, we'll have bp_read_watchpoint
6980 watchpoints with hw_access locations. Those should be considered
6981 duplicates of hw_read locations. The hw_read locations will
6982 become hw_access locations later. */
6983 return (loc1->owner->type == loc2->owner->type
6984 && loc1->pspace->aspace == loc2->pspace->aspace
6985 && loc1->address == loc2->address
6986 && loc1->length == loc2->length);
6987 }
6988
6989 /* See breakpoint.h. */
6990
6991 int
6992 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6993 const address_space *aspace2, CORE_ADDR addr2)
6994 {
6995 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6996 || aspace1 == aspace2)
6997 && addr1 == addr2);
6998 }
6999
7000 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7001 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7002 matches ASPACE2. On targets that have global breakpoints, the address
7003 space doesn't really matter. */
7004
7005 static int
7006 breakpoint_address_match_range (const address_space *aspace1,
7007 CORE_ADDR addr1,
7008 int len1, const address_space *aspace2,
7009 CORE_ADDR addr2)
7010 {
7011 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7012 || aspace1 == aspace2)
7013 && addr2 >= addr1 && addr2 < addr1 + len1);
7014 }
7015
7016 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7017 a ranged breakpoint. In most targets, a match happens only if ASPACE
7018 matches the breakpoint's address space. On targets that have global
7019 breakpoints, the address space doesn't really matter. */
7020
7021 static int
7022 breakpoint_location_address_match (struct bp_location *bl,
7023 const address_space *aspace,
7024 CORE_ADDR addr)
7025 {
7026 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7027 aspace, addr)
7028 || (bl->length
7029 && breakpoint_address_match_range (bl->pspace->aspace,
7030 bl->address, bl->length,
7031 aspace, addr)));
7032 }
7033
7034 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7035 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7036 match happens only if ASPACE matches the breakpoint's address
7037 space. On targets that have global breakpoints, the address space
7038 doesn't really matter. */
7039
7040 static int
7041 breakpoint_location_address_range_overlap (struct bp_location *bl,
7042 const address_space *aspace,
7043 CORE_ADDR addr, int len)
7044 {
7045 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7046 || bl->pspace->aspace == aspace)
7047 {
7048 int bl_len = bl->length != 0 ? bl->length : 1;
7049
7050 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7051 return 1;
7052 }
7053 return 0;
7054 }
7055
7056 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7057 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7058 true, otherwise returns false. */
7059
7060 static int
7061 tracepoint_locations_match (struct bp_location *loc1,
7062 struct bp_location *loc2)
7063 {
7064 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7065 /* Since tracepoint locations are never duplicated with others', tracepoint
7066 locations at the same address of different tracepoints are regarded as
7067 different locations. */
7068 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7069 else
7070 return 0;
7071 }
7072
7073 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7074 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7075 the same location. If SW_HW_BPS_MATCH is true, then software
7076 breakpoint locations and hardware breakpoint locations match,
7077 otherwise they don't. */
7078
7079 static int
7080 breakpoint_locations_match (struct bp_location *loc1,
7081 struct bp_location *loc2,
7082 bool sw_hw_bps_match)
7083 {
7084 int hw_point1, hw_point2;
7085
7086 /* Both of them must not be in moribund_locations. */
7087 gdb_assert (loc1->owner != NULL);
7088 gdb_assert (loc2->owner != NULL);
7089
7090 hw_point1 = is_hardware_watchpoint (loc1->owner);
7091 hw_point2 = is_hardware_watchpoint (loc2->owner);
7092
7093 if (hw_point1 != hw_point2)
7094 return 0;
7095 else if (hw_point1)
7096 return watchpoint_locations_match (loc1, loc2);
7097 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7098 return tracepoint_locations_match (loc1, loc2);
7099 else
7100 /* We compare bp_location.length in order to cover ranged
7101 breakpoints. Keep this in sync with
7102 bp_location_is_less_than. */
7103 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7104 loc2->pspace->aspace, loc2->address)
7105 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7106 && loc1->length == loc2->length);
7107 }
7108
7109 static void
7110 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7111 int bnum, int have_bnum)
7112 {
7113 /* The longest string possibly returned by hex_string_custom
7114 is 50 chars. These must be at least that big for safety. */
7115 char astr1[64];
7116 char astr2[64];
7117
7118 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7119 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7120 if (have_bnum)
7121 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7122 bnum, astr1, astr2);
7123 else
7124 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7125 }
7126
7127 /* Adjust a breakpoint's address to account for architectural
7128 constraints on breakpoint placement. Return the adjusted address.
7129 Note: Very few targets require this kind of adjustment. For most
7130 targets, this function is simply the identity function. */
7131
7132 static CORE_ADDR
7133 adjust_breakpoint_address (struct gdbarch *gdbarch,
7134 CORE_ADDR bpaddr, enum bptype bptype,
7135 struct program_space *pspace)
7136 {
7137 gdb_assert (pspace != nullptr);
7138
7139 if (bptype == bp_watchpoint
7140 || bptype == bp_hardware_watchpoint
7141 || bptype == bp_read_watchpoint
7142 || bptype == bp_access_watchpoint
7143 || bptype == bp_catchpoint)
7144 {
7145 /* Watchpoints and the various bp_catch_* eventpoints should not
7146 have their addresses modified. */
7147 return bpaddr;
7148 }
7149 else if (bptype == bp_single_step)
7150 {
7151 /* Single-step breakpoints should not have their addresses
7152 modified. If there's any architectural constrain that
7153 applies to this address, then it should have already been
7154 taken into account when the breakpoint was created in the
7155 first place. If we didn't do this, stepping through e.g.,
7156 Thumb-2 IT blocks would break. */
7157 return bpaddr;
7158 }
7159 else
7160 {
7161 CORE_ADDR adjusted_bpaddr = bpaddr;
7162
7163 /* Some targets have architectural constraints on the placement
7164 of breakpoint instructions. Obtain the adjusted address. */
7165 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7166 {
7167 /* Targets that implement this adjustment function will likely
7168 inspect either the symbol table, target memory at BPADDR, or
7169 even state registers, so ensure a suitable thread (and its
7170 associated program space) are currently selected. */
7171 scoped_restore_current_pspace_and_thread restore_pspace_thread;
7172 switch_to_program_space_and_thread (pspace);
7173 adjusted_bpaddr
7174 = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7175 }
7176
7177 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7178
7179 /* An adjusted breakpoint address can significantly alter
7180 a user's expectations. Print a warning if an adjustment
7181 is required. */
7182 if (adjusted_bpaddr != bpaddr)
7183 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7184
7185 return adjusted_bpaddr;
7186 }
7187 }
7188
7189 static bp_loc_type
7190 bp_location_from_bp_type (bptype type)
7191 {
7192 switch (type)
7193 {
7194 case bp_breakpoint:
7195 case bp_single_step:
7196 case bp_until:
7197 case bp_finish:
7198 case bp_longjmp:
7199 case bp_longjmp_resume:
7200 case bp_longjmp_call_dummy:
7201 case bp_exception:
7202 case bp_exception_resume:
7203 case bp_step_resume:
7204 case bp_hp_step_resume:
7205 case bp_watchpoint_scope:
7206 case bp_call_dummy:
7207 case bp_std_terminate:
7208 case bp_shlib_event:
7209 case bp_thread_event:
7210 case bp_overlay_event:
7211 case bp_jit_event:
7212 case bp_longjmp_master:
7213 case bp_std_terminate_master:
7214 case bp_exception_master:
7215 case bp_gnu_ifunc_resolver:
7216 case bp_gnu_ifunc_resolver_return:
7217 case bp_dprintf:
7218 return bp_loc_software_breakpoint;
7219 case bp_hardware_breakpoint:
7220 return bp_loc_hardware_breakpoint;
7221 case bp_hardware_watchpoint:
7222 case bp_read_watchpoint:
7223 case bp_access_watchpoint:
7224 return bp_loc_hardware_watchpoint;
7225 case bp_watchpoint:
7226 return bp_loc_software_watchpoint;
7227 case bp_catchpoint:
7228 case bp_tracepoint:
7229 case bp_fast_tracepoint:
7230 case bp_static_tracepoint:
7231 case bp_static_marker_tracepoint:
7232 return bp_loc_other;
7233 default:
7234 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7235 }
7236 }
7237
7238 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7239 {
7240 this->owner = owner;
7241 this->cond_bytecode = NULL;
7242 this->shlib_disabled = 0;
7243 this->enabled = 1;
7244 this->disabled_by_cond = false;
7245
7246 this->loc_type = type;
7247
7248 if (this->loc_type == bp_loc_software_breakpoint
7249 || this->loc_type == bp_loc_hardware_breakpoint)
7250 mark_breakpoint_location_modified (this);
7251
7252 incref ();
7253 }
7254
7255 bp_location::bp_location (breakpoint *owner)
7256 : bp_location::bp_location (owner,
7257 bp_location_from_bp_type (owner->type))
7258 {
7259 }
7260
7261 /* Decrement reference count. If the reference count reaches 0,
7262 destroy the bp_location. Sets *BLP to NULL. */
7263
7264 static void
7265 decref_bp_location (struct bp_location **blp)
7266 {
7267 bp_location_ref_policy::decref (*blp);
7268 *blp = NULL;
7269 }
7270
7271 /* Add breakpoint B at the end of the global breakpoint chain. */
7272
7273 static breakpoint *
7274 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7275 {
7276 struct breakpoint *b1;
7277 struct breakpoint *result = b.get ();
7278
7279 /* Add this breakpoint to the end of the chain so that a list of
7280 breakpoints will come out in order of increasing numbers. */
7281
7282 b1 = breakpoint_chain;
7283 if (b1 == 0)
7284 breakpoint_chain = b.release ();
7285 else
7286 {
7287 while (b1->next)
7288 b1 = b1->next;
7289 b1->next = b.release ();
7290 }
7291
7292 return result;
7293 }
7294
7295 /* Initialize loc->function_name. */
7296
7297 static void
7298 set_breakpoint_location_function (struct bp_location *loc)
7299 {
7300 gdb_assert (loc->owner != NULL);
7301
7302 if (loc->owner->type == bp_breakpoint
7303 || loc->owner->type == bp_hardware_breakpoint
7304 || is_tracepoint (loc->owner))
7305 {
7306 const char *function_name;
7307
7308 if (loc->msymbol != NULL
7309 && (loc->msymbol->type () == mst_text_gnu_ifunc
7310 || loc->msymbol->type () == mst_data_gnu_ifunc))
7311 {
7312 struct breakpoint *b = loc->owner;
7313
7314 function_name = loc->msymbol->linkage_name ();
7315
7316 if (b->type == bp_breakpoint && b->loc == loc
7317 && loc->next == NULL && b->related_breakpoint == b)
7318 {
7319 /* Create only the whole new breakpoint of this type but do not
7320 mess more complicated breakpoints with multiple locations. */
7321 b->type = bp_gnu_ifunc_resolver;
7322 /* Remember the resolver's address for use by the return
7323 breakpoint. */
7324 loc->related_address = loc->address;
7325 }
7326 }
7327 else
7328 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7329
7330 if (function_name)
7331 loc->function_name = make_unique_xstrdup (function_name);
7332 }
7333 }
7334
7335 /* Attempt to determine architecture of location identified by SAL. */
7336 struct gdbarch *
7337 get_sal_arch (struct symtab_and_line sal)
7338 {
7339 if (sal.section)
7340 return sal.section->objfile->arch ();
7341 if (sal.symtab)
7342 return sal.symtab->compunit ()->objfile ()->arch ();
7343
7344 return NULL;
7345 }
7346
7347 /* Call this routine when stepping and nexting to enable a breakpoint
7348 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7349 initiated the operation. */
7350
7351 void
7352 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7353 {
7354 int thread = tp->global_num;
7355
7356 /* To avoid having to rescan all objfile symbols at every step,
7357 we maintain a list of continually-inserted but always disabled
7358 longjmp "master" breakpoints. Here, we simply create momentary
7359 clones of those and enable them for the requested thread. */
7360 for (breakpoint *b : all_breakpoints_safe ())
7361 if (b->pspace == current_program_space
7362 && (b->type == bp_longjmp_master
7363 || b->type == bp_exception_master))
7364 {
7365 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7366 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7367 after their removal. */
7368 momentary_breakpoint_from_master (b, type, 1, thread);
7369 }
7370
7371 tp->initiating_frame = frame;
7372 }
7373
7374 /* Delete all longjmp breakpoints from THREAD. */
7375 void
7376 delete_longjmp_breakpoint (int thread)
7377 {
7378 for (breakpoint *b : all_breakpoints_safe ())
7379 if (b->type == bp_longjmp || b->type == bp_exception)
7380 {
7381 if (b->thread == thread)
7382 delete_breakpoint (b);
7383 }
7384 }
7385
7386 void
7387 delete_longjmp_breakpoint_at_next_stop (int thread)
7388 {
7389 for (breakpoint *b : all_breakpoints_safe ())
7390 if (b->type == bp_longjmp || b->type == bp_exception)
7391 {
7392 if (b->thread == thread)
7393 b->disposition = disp_del_at_next_stop;
7394 }
7395 }
7396
7397 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7398 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7399 pointer to any of them. Return NULL if this system cannot place longjmp
7400 breakpoints. */
7401
7402 struct breakpoint *
7403 set_longjmp_breakpoint_for_call_dummy (void)
7404 {
7405 breakpoint *retval = nullptr;
7406
7407 for (breakpoint *b : all_breakpoints ())
7408 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7409 {
7410 int thread = inferior_thread ()->global_num;
7411 breakpoint *new_b
7412 = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7413 1, thread);
7414
7415 /* Link NEW_B into the chain of RETVAL breakpoints. */
7416
7417 gdb_assert (new_b->related_breakpoint == new_b);
7418 if (retval == NULL)
7419 retval = new_b;
7420 new_b->related_breakpoint = retval;
7421 while (retval->related_breakpoint != new_b->related_breakpoint)
7422 retval = retval->related_breakpoint;
7423 retval->related_breakpoint = new_b;
7424 }
7425
7426 return retval;
7427 }
7428
7429 /* Verify all existing dummy frames and their associated breakpoints for
7430 TP. Remove those which can no longer be found in the current frame
7431 stack.
7432
7433 If the unwind fails then there is not sufficient information to discard
7434 dummy frames. In this case, elide the clean up and the dummy frames will
7435 be cleaned up next time this function is called from a location where
7436 unwinding is possible. */
7437
7438 void
7439 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7440 {
7441 struct breakpoint *b, *b_tmp;
7442
7443 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7444 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7445 {
7446 struct breakpoint *dummy_b = b->related_breakpoint;
7447
7448 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7449 chained off b->related_breakpoint. */
7450 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7451 dummy_b = dummy_b->related_breakpoint;
7452
7453 /* If there was no bp_call_dummy breakpoint then there's nothing
7454 more to do. Or, if the dummy frame associated with the
7455 bp_call_dummy is still on the stack then we need to leave this
7456 bp_call_dummy in place. */
7457 if (dummy_b->type != bp_call_dummy
7458 || frame_find_by_id (dummy_b->frame_id) != NULL)
7459 continue;
7460
7461 /* We didn't find the dummy frame on the stack, this could be
7462 because we have longjmp'd to a stack frame that is previous to
7463 the dummy frame, or it could be because the stack unwind is
7464 broken at some point between the longjmp frame and the dummy
7465 frame.
7466
7467 Next we figure out why the stack unwind stopped. If it looks
7468 like the unwind is complete then we assume the dummy frame has
7469 been jumped over, however, if the unwind stopped for an
7470 unexpected reason then we assume the stack unwind is currently
7471 broken, and that we will (eventually) return to the dummy
7472 frame.
7473
7474 It might be tempting to consider using frame_id_inner here, but
7475 that is not safe. There is no guarantee that the stack frames
7476 we are looking at here are even on the same stack as the
7477 original dummy frame, hence frame_id_inner can't be used. See
7478 the comments on frame_id_inner for more details. */
7479 bool unwind_finished_unexpectedly = false;
7480 for (frame_info_ptr fi = get_current_frame (); fi != nullptr; )
7481 {
7482 frame_info_ptr prev = get_prev_frame (fi);
7483 if (prev == nullptr)
7484 {
7485 /* FI is the last stack frame. Why did this frame not
7486 unwind further? */
7487 auto stop_reason = get_frame_unwind_stop_reason (fi);
7488 if (stop_reason != UNWIND_NO_REASON
7489 && stop_reason != UNWIND_OUTERMOST)
7490 unwind_finished_unexpectedly = true;
7491 }
7492 fi = prev;
7493 }
7494 if (unwind_finished_unexpectedly)
7495 continue;
7496
7497 dummy_frame_discard (dummy_b->frame_id, tp);
7498
7499 while (b->related_breakpoint != b)
7500 {
7501 if (b_tmp == b->related_breakpoint)
7502 b_tmp = b->related_breakpoint->next;
7503 delete_breakpoint (b->related_breakpoint);
7504 }
7505 delete_breakpoint (b);
7506 }
7507 }
7508
7509 void
7510 enable_overlay_breakpoints (void)
7511 {
7512 for (breakpoint *b : all_breakpoints ())
7513 if (b->type == bp_overlay_event)
7514 {
7515 b->enable_state = bp_enabled;
7516 update_global_location_list (UGLL_MAY_INSERT);
7517 overlay_events_enabled = 1;
7518 }
7519 }
7520
7521 void
7522 disable_overlay_breakpoints (void)
7523 {
7524 for (breakpoint *b : all_breakpoints ())
7525 if (b->type == bp_overlay_event)
7526 {
7527 b->enable_state = bp_disabled;
7528 update_global_location_list (UGLL_DONT_INSERT);
7529 overlay_events_enabled = 0;
7530 }
7531 }
7532
7533 /* Set an active std::terminate breakpoint for each std::terminate
7534 master breakpoint. */
7535 void
7536 set_std_terminate_breakpoint (void)
7537 {
7538 for (breakpoint *b : all_breakpoints_safe ())
7539 if (b->pspace == current_program_space
7540 && b->type == bp_std_terminate_master)
7541 {
7542 momentary_breakpoint_from_master (b, bp_std_terminate, 1,
7543 inferior_thread ()->global_num);
7544 }
7545 }
7546
7547 /* Delete all the std::terminate breakpoints. */
7548 void
7549 delete_std_terminate_breakpoint (void)
7550 {
7551 for (breakpoint *b : all_breakpoints_safe ())
7552 if (b->type == bp_std_terminate)
7553 delete_breakpoint (b);
7554 }
7555
7556 struct breakpoint *
7557 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7558 {
7559 struct breakpoint *b;
7560
7561 b = create_internal_breakpoint (gdbarch, address, bp_thread_event);
7562
7563 b->enable_state = bp_enabled;
7564 /* locspec has to be used or breakpoint_re_set will delete me. */
7565 b->locspec = new_address_location_spec (b->loc->address, NULL, 0);
7566
7567 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7568
7569 return b;
7570 }
7571
7572 struct lang_and_radix
7573 {
7574 enum language lang;
7575 int radix;
7576 };
7577
7578 /* Create a breakpoint for JIT code registration and unregistration. */
7579
7580 struct breakpoint *
7581 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7582 {
7583 return create_internal_breakpoint (gdbarch, address, bp_jit_event);
7584 }
7585
7586 /* Remove JIT code registration and unregistration breakpoint(s). */
7587
7588 void
7589 remove_jit_event_breakpoints (void)
7590 {
7591 for (breakpoint *b : all_breakpoints_safe ())
7592 if (b->type == bp_jit_event
7593 && b->loc->pspace == current_program_space)
7594 delete_breakpoint (b);
7595 }
7596
7597 void
7598 remove_solib_event_breakpoints (void)
7599 {
7600 for (breakpoint *b : all_breakpoints_safe ())
7601 if (b->type == bp_shlib_event
7602 && b->loc->pspace == current_program_space)
7603 delete_breakpoint (b);
7604 }
7605
7606 /* See breakpoint.h. */
7607
7608 void
7609 remove_solib_event_breakpoints_at_next_stop (void)
7610 {
7611 for (breakpoint *b : all_breakpoints_safe ())
7612 if (b->type == bp_shlib_event
7613 && b->loc->pspace == current_program_space)
7614 b->disposition = disp_del_at_next_stop;
7615 }
7616
7617 /* Helper for create_solib_event_breakpoint /
7618 create_and_insert_solib_event_breakpoint. Allows specifying which
7619 INSERT_MODE to pass through to update_global_location_list. */
7620
7621 static struct breakpoint *
7622 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7623 enum ugll_insert_mode insert_mode)
7624 {
7625 struct breakpoint *b;
7626
7627 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event);
7628 update_global_location_list_nothrow (insert_mode);
7629 return b;
7630 }
7631
7632 struct breakpoint *
7633 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7634 {
7635 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7636 }
7637
7638 /* See breakpoint.h. */
7639
7640 struct breakpoint *
7641 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7642 {
7643 struct breakpoint *b;
7644
7645 /* Explicitly tell update_global_location_list to insert
7646 locations. */
7647 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7648 if (!b->loc->inserted)
7649 {
7650 delete_breakpoint (b);
7651 return NULL;
7652 }
7653 return b;
7654 }
7655
7656 /* Disable any breakpoints that are on code in shared libraries. Only
7657 apply to enabled breakpoints, disabled ones can just stay disabled. */
7658
7659 void
7660 disable_breakpoints_in_shlibs (void)
7661 {
7662 for (bp_location *loc : all_bp_locations ())
7663 {
7664 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7665 struct breakpoint *b = loc->owner;
7666
7667 /* We apply the check to all breakpoints, including disabled for
7668 those with loc->duplicate set. This is so that when breakpoint
7669 becomes enabled, or the duplicate is removed, gdb will try to
7670 insert all breakpoints. If we don't set shlib_disabled here,
7671 we'll try to insert those breakpoints and fail. */
7672 if (((b->type == bp_breakpoint)
7673 || (b->type == bp_jit_event)
7674 || (b->type == bp_hardware_breakpoint)
7675 || (is_tracepoint (b)))
7676 && loc->pspace == current_program_space
7677 && !loc->shlib_disabled
7678 && solib_name_from_address (loc->pspace, loc->address)
7679 )
7680 {
7681 loc->shlib_disabled = 1;
7682 }
7683 }
7684 }
7685
7686 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7687 notification of unloaded_shlib. Only apply to enabled breakpoints,
7688 disabled ones can just stay disabled. */
7689
7690 static void
7691 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7692 {
7693 int disabled_shlib_breaks = 0;
7694
7695 for (bp_location *loc : all_bp_locations ())
7696 {
7697 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7698 struct breakpoint *b = loc->owner;
7699
7700 if (solib->pspace == loc->pspace
7701 && !loc->shlib_disabled
7702 && (((b->type == bp_breakpoint
7703 || b->type == bp_jit_event
7704 || b->type == bp_hardware_breakpoint)
7705 && (loc->loc_type == bp_loc_hardware_breakpoint
7706 || loc->loc_type == bp_loc_software_breakpoint))
7707 || is_tracepoint (b))
7708 && solib_contains_address_p (solib, loc->address))
7709 {
7710 loc->shlib_disabled = 1;
7711 /* At this point, we cannot rely on remove_breakpoint
7712 succeeding so we must mark the breakpoint as not inserted
7713 to prevent future errors occurring in remove_breakpoints. */
7714 loc->inserted = 0;
7715
7716 /* This may cause duplicate notifications for the same breakpoint. */
7717 gdb::observers::breakpoint_modified.notify (b);
7718
7719 if (!disabled_shlib_breaks)
7720 {
7721 target_terminal::ours_for_output ();
7722 warning (_("Temporarily disabling breakpoints "
7723 "for unloaded shared library \"%s\""),
7724 solib->so_name);
7725 }
7726 disabled_shlib_breaks = 1;
7727 }
7728 }
7729 }
7730
7731 /* Disable any breakpoints and tracepoints in OBJFILE upon
7732 notification of free_objfile. Only apply to enabled breakpoints,
7733 disabled ones can just stay disabled. */
7734
7735 static void
7736 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7737 {
7738 if (objfile == NULL)
7739 return;
7740
7741 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7742 managed by the user with add-symbol-file/remove-symbol-file.
7743 Similarly to how breakpoints in shared libraries are handled in
7744 response to "nosharedlibrary", mark breakpoints in such modules
7745 shlib_disabled so they end up uninserted on the next global
7746 location list update. Shared libraries not loaded by the user
7747 aren't handled here -- they're already handled in
7748 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7749 solib_unloaded observer. We skip objfiles that are not
7750 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7751 main objfile). */
7752 if ((objfile->flags & OBJF_SHARED) == 0
7753 || (objfile->flags & OBJF_USERLOADED) == 0)
7754 return;
7755
7756 for (breakpoint *b : all_breakpoints ())
7757 {
7758 int bp_modified = 0;
7759
7760 if (!is_breakpoint (b) && !is_tracepoint (b))
7761 continue;
7762
7763 for (bp_location *loc : b->locations ())
7764 {
7765 CORE_ADDR loc_addr = loc->address;
7766
7767 if (loc->loc_type != bp_loc_hardware_breakpoint
7768 && loc->loc_type != bp_loc_software_breakpoint)
7769 continue;
7770
7771 if (loc->shlib_disabled != 0)
7772 continue;
7773
7774 if (objfile->pspace != loc->pspace)
7775 continue;
7776
7777 if (loc->loc_type != bp_loc_hardware_breakpoint
7778 && loc->loc_type != bp_loc_software_breakpoint)
7779 continue;
7780
7781 if (is_addr_in_objfile (loc_addr, objfile))
7782 {
7783 loc->shlib_disabled = 1;
7784 /* At this point, we don't know whether the object was
7785 unmapped from the inferior or not, so leave the
7786 inserted flag alone. We'll handle failure to
7787 uninsert quietly, in case the object was indeed
7788 unmapped. */
7789
7790 mark_breakpoint_location_modified (loc);
7791
7792 bp_modified = 1;
7793 }
7794 }
7795
7796 if (bp_modified)
7797 gdb::observers::breakpoint_modified.notify (b);
7798 }
7799 }
7800
7801 /* See breakpoint.h. */
7802
7803 breakpoint::breakpoint (struct gdbarch *gdbarch_, enum bptype bptype,
7804 bool temp, const char *cond_string_)
7805 : type (bptype),
7806 disposition (temp ? disp_del : disp_donttouch),
7807 gdbarch (gdbarch_),
7808 language (current_language->la_language),
7809 input_radix (::input_radix),
7810 cond_string (cond_string_ != nullptr
7811 ? make_unique_xstrdup (cond_string_)
7812 : nullptr),
7813 related_breakpoint (this)
7814 {
7815 }
7816
7817 /* See breakpoint.h. */
7818
7819 catchpoint::catchpoint (struct gdbarch *gdbarch, bool temp,
7820 const char *cond_string)
7821 : breakpoint (gdbarch, bp_catchpoint, temp, cond_string)
7822 {
7823 add_dummy_location (this, current_program_space);
7824
7825 pspace = current_program_space;
7826 }
7827
7828 void
7829 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
7830 {
7831 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
7832 set_breakpoint_number (internal, b);
7833 if (is_tracepoint (b))
7834 set_tracepoint_count (breakpoint_count);
7835 if (!internal)
7836 mention (b);
7837 gdb::observers::breakpoint_created.notify (b);
7838
7839 if (update_gll)
7840 update_global_location_list (UGLL_MAY_INSERT);
7841 }
7842
7843 static int
7844 hw_breakpoint_used_count (void)
7845 {
7846 int i = 0;
7847
7848 for (breakpoint *b : all_breakpoints ())
7849 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
7850 for (bp_location *bl : b->locations ())
7851 {
7852 /* Special types of hardware breakpoints may use more than
7853 one register. */
7854 i += b->resources_needed (bl);
7855 }
7856
7857 return i;
7858 }
7859
7860 /* Returns the resources B would use if it were a hardware
7861 watchpoint. */
7862
7863 static int
7864 hw_watchpoint_use_count (struct breakpoint *b)
7865 {
7866 int i = 0;
7867
7868 if (!breakpoint_enabled (b))
7869 return 0;
7870
7871 for (bp_location *bl : b->locations ())
7872 {
7873 /* Special types of hardware watchpoints may use more than
7874 one register. */
7875 i += b->resources_needed (bl);
7876 }
7877
7878 return i;
7879 }
7880
7881 /* Returns the sum the used resources of all hardware watchpoints of
7882 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
7883 the sum of the used resources of all hardware watchpoints of other
7884 types _not_ TYPE. */
7885
7886 static int
7887 hw_watchpoint_used_count_others (struct breakpoint *except,
7888 enum bptype type, int *other_type_used)
7889 {
7890 int i = 0;
7891
7892 *other_type_used = 0;
7893 for (breakpoint *b : all_breakpoints ())
7894 {
7895 if (b == except)
7896 continue;
7897 if (!breakpoint_enabled (b))
7898 continue;
7899
7900 if (b->type == type)
7901 i += hw_watchpoint_use_count (b);
7902 else if (is_hardware_watchpoint (b))
7903 *other_type_used = 1;
7904 }
7905
7906 return i;
7907 }
7908
7909 void
7910 disable_watchpoints_before_interactive_call_start (void)
7911 {
7912 for (breakpoint *b : all_breakpoints ())
7913 if (is_watchpoint (b) && breakpoint_enabled (b))
7914 {
7915 b->enable_state = bp_call_disabled;
7916 update_global_location_list (UGLL_DONT_INSERT);
7917 }
7918 }
7919
7920 void
7921 enable_watchpoints_after_interactive_call_stop (void)
7922 {
7923 for (breakpoint *b : all_breakpoints ())
7924 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
7925 {
7926 b->enable_state = bp_enabled;
7927 update_global_location_list (UGLL_MAY_INSERT);
7928 }
7929 }
7930
7931 void
7932 disable_breakpoints_before_startup (void)
7933 {
7934 current_program_space->executing_startup = 1;
7935 update_global_location_list (UGLL_DONT_INSERT);
7936 }
7937
7938 void
7939 enable_breakpoints_after_startup (void)
7940 {
7941 current_program_space->executing_startup = 0;
7942 breakpoint_re_set ();
7943 }
7944
7945 /* Allocate a new momentary breakpoint. */
7946
7947 template<typename... Arg>
7948 static momentary_breakpoint *
7949 new_momentary_breakpoint (struct gdbarch *gdbarch, enum bptype type,
7950 Arg&&... args)
7951 {
7952 if (type == bp_longjmp || type == bp_exception)
7953 return new longjmp_breakpoint (gdbarch, type,
7954 std::forward<Arg> (args)...);
7955 else
7956 return new momentary_breakpoint (gdbarch, type,
7957 std::forward<Arg> (args)...);
7958 }
7959
7960 /* Set a momentary breakpoint of type TYPE at address specified by
7961 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
7962 frame. */
7963
7964 breakpoint_up
7965 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7966 struct frame_id frame_id, enum bptype type)
7967 {
7968 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
7969 tail-called one. */
7970 gdb_assert (!frame_id_artificial_p (frame_id));
7971
7972 std::unique_ptr<momentary_breakpoint> b
7973 (new_momentary_breakpoint (gdbarch, type, sal.pspace, frame_id,
7974 inferior_thread ()->global_num));
7975
7976 b->add_location (sal);
7977
7978 breakpoint_up bp (add_to_breakpoint_chain (std::move (b)));
7979
7980 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7981
7982 return bp;
7983 }
7984
7985 /* Make a momentary breakpoint based on the master breakpoint ORIG.
7986 The new breakpoint will have type TYPE, use OPS as its
7987 breakpoint_ops, and will set enabled to LOC_ENABLED. */
7988
7989 static struct breakpoint *
7990 momentary_breakpoint_from_master (struct breakpoint *orig,
7991 enum bptype type,
7992 int loc_enabled,
7993 int thread)
7994 {
7995 std::unique_ptr<breakpoint> copy
7996 (new_momentary_breakpoint (orig->gdbarch, type, orig->pspace,
7997 orig->frame_id, thread));
7998 copy->loc = copy->allocate_location ();
7999 set_breakpoint_location_function (copy->loc);
8000
8001 copy->loc->gdbarch = orig->loc->gdbarch;
8002 copy->loc->requested_address = orig->loc->requested_address;
8003 copy->loc->address = orig->loc->address;
8004 copy->loc->section = orig->loc->section;
8005 copy->loc->pspace = orig->loc->pspace;
8006 copy->loc->probe = orig->loc->probe;
8007 copy->loc->line_number = orig->loc->line_number;
8008 copy->loc->symtab = orig->loc->symtab;
8009 copy->loc->enabled = loc_enabled;
8010
8011 breakpoint *b = add_to_breakpoint_chain (std::move (copy));
8012 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8013 return b;
8014 }
8015
8016 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8017 ORIG is NULL. */
8018
8019 struct breakpoint *
8020 clone_momentary_breakpoint (struct breakpoint *orig)
8021 {
8022 /* If there's nothing to clone, then return nothing. */
8023 if (orig == NULL)
8024 return NULL;
8025
8026 return momentary_breakpoint_from_master (orig, orig->type, 0,
8027 orig->thread);
8028 }
8029
8030 breakpoint_up
8031 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8032 enum bptype type)
8033 {
8034 struct symtab_and_line sal;
8035
8036 sal = find_pc_line (pc, 0);
8037 sal.pc = pc;
8038 sal.section = find_pc_overlay (pc);
8039 sal.explicit_pc = 1;
8040
8041 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8042 }
8043 \f
8044
8045 /* Tell the user we have just set a breakpoint B. */
8046
8047 static void
8048 mention (const breakpoint *b)
8049 {
8050 b->print_mention ();
8051 current_uiout->text ("\n");
8052 }
8053 \f
8054
8055 static bool bp_loc_is_permanent (struct bp_location *loc);
8056
8057 /* Handle "set breakpoint auto-hw on".
8058
8059 If the explicitly specified breakpoint type is not hardware
8060 breakpoint, check the memory map to see whether the breakpoint
8061 address is in read-only memory.
8062
8063 - location type is not hardware breakpoint, memory is read-only.
8064 We change the type of the location to hardware breakpoint.
8065
8066 - location type is hardware breakpoint, memory is read-write. This
8067 means we've previously made the location hardware one, but then the
8068 memory map changed, so we undo.
8069 */
8070
8071 static void
8072 handle_automatic_hardware_breakpoints (bp_location *bl)
8073 {
8074 if (automatic_hardware_breakpoints
8075 && bl->owner->type != bp_hardware_breakpoint
8076 && (bl->loc_type == bp_loc_software_breakpoint
8077 || bl->loc_type == bp_loc_hardware_breakpoint))
8078 {
8079 /* When breakpoints are removed, remove_breakpoints will use
8080 location types we've just set here, the only possible problem
8081 is that memory map has changed during running program, but
8082 it's not going to work anyway with current gdb. */
8083 mem_region *mr = lookup_mem_region (bl->address);
8084
8085 if (mr != nullptr)
8086 {
8087 enum bp_loc_type new_type;
8088
8089 if (mr->attrib.mode != MEM_RW)
8090 new_type = bp_loc_hardware_breakpoint;
8091 else
8092 new_type = bp_loc_software_breakpoint;
8093
8094 if (new_type != bl->loc_type)
8095 {
8096 static bool said = false;
8097
8098 bl->loc_type = new_type;
8099 if (!said)
8100 {
8101 gdb_printf (_("Note: automatically using "
8102 "hardware breakpoints for "
8103 "read-only addresses.\n"));
8104 said = true;
8105 }
8106 }
8107 }
8108 }
8109 }
8110
8111 bp_location *
8112 code_breakpoint::add_location (const symtab_and_line &sal)
8113 {
8114 struct bp_location *new_loc, **tmp;
8115 CORE_ADDR adjusted_address;
8116 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8117
8118 if (loc_gdbarch == NULL)
8119 loc_gdbarch = gdbarch;
8120
8121 /* Adjust the breakpoint's address prior to allocating a location.
8122 Once we call allocate_location(), that mostly uninitialized
8123 location will be placed on the location chain. Adjustment of the
8124 breakpoint may cause target_read_memory() to be called and we do
8125 not want its scan of the location chain to find a breakpoint and
8126 location that's only been partially initialized. */
8127 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8128 sal.pc, type,
8129 sal.pspace);
8130
8131 /* Sort the locations by their ADDRESS. */
8132 new_loc = allocate_location ();
8133 for (tmp = &(loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8134 tmp = &((*tmp)->next))
8135 ;
8136 new_loc->next = *tmp;
8137 *tmp = new_loc;
8138
8139 new_loc->requested_address = sal.pc;
8140 new_loc->address = adjusted_address;
8141 new_loc->pspace = sal.pspace;
8142 new_loc->probe.prob = sal.prob;
8143 new_loc->probe.objfile = sal.objfile;
8144 gdb_assert (new_loc->pspace != NULL);
8145 new_loc->section = sal.section;
8146 new_loc->gdbarch = loc_gdbarch;
8147 new_loc->line_number = sal.line;
8148 new_loc->symtab = sal.symtab;
8149 new_loc->symbol = sal.symbol;
8150 new_loc->msymbol = sal.msymbol;
8151 new_loc->objfile = sal.objfile;
8152
8153 set_breakpoint_location_function (new_loc);
8154
8155 /* While by definition, permanent breakpoints are already present in the
8156 code, we don't mark the location as inserted. Normally one would expect
8157 that GDB could rely on that breakpoint instruction to stop the program,
8158 thus removing the need to insert its own breakpoint, except that executing
8159 the breakpoint instruction can kill the target instead of reporting a
8160 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8161 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8162 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8163 breakpoint be inserted normally results in QEMU knowing about the GDB
8164 breakpoint, and thus trap before the breakpoint instruction is executed.
8165 (If GDB later needs to continue execution past the permanent breakpoint,
8166 it manually increments the PC, thus avoiding executing the breakpoint
8167 instruction.) */
8168 if (bp_loc_is_permanent (new_loc))
8169 new_loc->permanent = 1;
8170
8171 return new_loc;
8172 }
8173 \f
8174
8175 /* Return true if LOC is pointing to a permanent breakpoint,
8176 return false otherwise. */
8177
8178 static bool
8179 bp_loc_is_permanent (struct bp_location *loc)
8180 {
8181 gdb_assert (loc != NULL);
8182
8183 /* If we have a non-breakpoint-backed catchpoint or a software
8184 watchpoint, just return 0. We should not attempt to read from
8185 the addresses the locations of these breakpoint types point to.
8186 gdbarch_program_breakpoint_here_p, below, will attempt to read
8187 memory. */
8188 if (!bl_address_is_meaningful (loc))
8189 return false;
8190
8191 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8192 switch_to_program_space_and_thread (loc->pspace);
8193 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8194 }
8195
8196 /* Build a command list for the dprintf corresponding to the current
8197 settings of the dprintf style options. */
8198
8199 static void
8200 update_dprintf_command_list (struct breakpoint *b)
8201 {
8202 const char *dprintf_args = b->extra_string.get ();
8203 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8204
8205 if (!dprintf_args)
8206 return;
8207
8208 dprintf_args = skip_spaces (dprintf_args);
8209
8210 /* Allow a comma, as it may have terminated a location, but don't
8211 insist on it. */
8212 if (*dprintf_args == ',')
8213 ++dprintf_args;
8214 dprintf_args = skip_spaces (dprintf_args);
8215
8216 if (*dprintf_args != '"')
8217 error (_("Bad format string, missing '\"'."));
8218
8219 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8220 printf_line = xstrprintf ("printf %s", dprintf_args);
8221 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8222 {
8223 if (dprintf_function.empty ())
8224 error (_("No function supplied for dprintf call"));
8225
8226 if (!dprintf_channel.empty ())
8227 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8228 dprintf_function.c_str (),
8229 dprintf_channel.c_str (),
8230 dprintf_args);
8231 else
8232 printf_line = xstrprintf ("call (void) %s (%s)",
8233 dprintf_function.c_str (),
8234 dprintf_args);
8235 }
8236 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8237 {
8238 if (target_can_run_breakpoint_commands ())
8239 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8240 else
8241 {
8242 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8243 printf_line = xstrprintf ("printf %s", dprintf_args);
8244 }
8245 }
8246 else
8247 internal_error (__FILE__, __LINE__,
8248 _("Invalid dprintf style."));
8249
8250 gdb_assert (printf_line != NULL);
8251
8252 /* Manufacture a printf sequence. */
8253 struct command_line *printf_cmd_line
8254 = new struct command_line (simple_control, printf_line.release ());
8255 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8256 command_lines_deleter ()));
8257 }
8258
8259 /* Update all dprintf commands, making their command lists reflect
8260 current style settings. */
8261
8262 static void
8263 update_dprintf_commands (const char *args, int from_tty,
8264 struct cmd_list_element *c)
8265 {
8266 for (breakpoint *b : all_breakpoints ())
8267 if (b->type == bp_dprintf)
8268 update_dprintf_command_list (b);
8269 }
8270
8271 code_breakpoint::code_breakpoint (struct gdbarch *gdbarch_,
8272 enum bptype type_,
8273 gdb::array_view<const symtab_and_line> sals,
8274 location_spec_up &&locspec_,
8275 gdb::unique_xmalloc_ptr<char> filter_,
8276 gdb::unique_xmalloc_ptr<char> cond_string_,
8277 gdb::unique_xmalloc_ptr<char> extra_string_,
8278 enum bpdisp disposition_,
8279 int thread_, int task_, int ignore_count_,
8280 int from_tty,
8281 int enabled_, unsigned flags,
8282 int display_canonical_)
8283 : breakpoint (gdbarch_, type_)
8284 {
8285 int i;
8286
8287 if (type == bp_hardware_breakpoint)
8288 {
8289 int target_resources_ok;
8290
8291 i = hw_breakpoint_used_count ();
8292 target_resources_ok =
8293 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8294 i + 1, 0);
8295 if (target_resources_ok == 0)
8296 error (_("No hardware breakpoint support in the target."));
8297 else if (target_resources_ok < 0)
8298 error (_("Hardware breakpoints used exceeds limit."));
8299 }
8300
8301 gdb_assert (!sals.empty ());
8302
8303 thread = thread_;
8304 task = task_;
8305
8306 cond_string = std::move (cond_string_);
8307 extra_string = std::move (extra_string_);
8308 ignore_count = ignore_count_;
8309 enable_state = enabled_ ? bp_enabled : bp_disabled;
8310 disposition = disposition_;
8311
8312 if (type == bp_static_tracepoint
8313 || type == bp_static_marker_tracepoint)
8314 {
8315 auto *t = static_cast<struct tracepoint *> (this);
8316 struct static_tracepoint_marker marker;
8317
8318 if (strace_marker_p (this))
8319 {
8320 /* We already know the marker exists, otherwise, we wouldn't
8321 see a sal for it. */
8322 const char *p = &locspec_->to_string ()[3];
8323 const char *endp;
8324
8325 p = skip_spaces (p);
8326
8327 endp = skip_to_space (p);
8328
8329 t->static_trace_marker_id.assign (p, endp - p);
8330
8331 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8332 t->static_trace_marker_id.c_str ());
8333 }
8334 else if (target_static_tracepoint_marker_at (sals[0].pc, &marker))
8335 {
8336 t->static_trace_marker_id = std::move (marker.str_id);
8337
8338 gdb_printf (_("Probed static tracepoint marker \"%s\"\n"),
8339 t->static_trace_marker_id.c_str ());
8340 }
8341 else
8342 warning (_("Couldn't determine the static tracepoint marker to probe"));
8343 }
8344
8345 for (const auto &sal : sals)
8346 {
8347 if (from_tty)
8348 {
8349 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8350 if (loc_gdbarch == nullptr)
8351 loc_gdbarch = gdbarch;
8352
8353 describe_other_breakpoints (loc_gdbarch,
8354 sal.pspace, sal.pc, sal.section, thread);
8355 }
8356
8357 bp_location *new_loc = add_location (sal);
8358 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8359 new_loc->inserted = 1;
8360
8361 /* Do not set breakpoint locations conditions yet. As locations
8362 are inserted, they get sorted based on their addresses. Let
8363 the list stabilize to have reliable location numbers. */
8364
8365 /* Dynamic printf requires and uses additional arguments on the
8366 command line, otherwise it's an error. */
8367 if (type == bp_dprintf)
8368 {
8369 if (extra_string != nullptr)
8370 update_dprintf_command_list (this);
8371 else
8372 error (_("Format string required"));
8373 }
8374 else if (extra_string != nullptr)
8375 error (_("Garbage '%s' at end of command"), extra_string.get ());
8376 }
8377
8378 /* The order of the locations is now stable. Set the location
8379 condition using the location's number. */
8380 int loc_num = 1;
8381 for (bp_location *bl : locations ())
8382 {
8383 if (cond_string != nullptr)
8384 set_breakpoint_location_condition (cond_string.get (), bl,
8385 number, loc_num);
8386
8387 ++loc_num;
8388 }
8389
8390 display_canonical = display_canonical_;
8391 if (locspec_ != nullptr)
8392 locspec = std::move (locspec_);
8393 else
8394 locspec = new_address_location_spec (this->loc->address, NULL, 0);
8395 filter = std::move (filter_);
8396 }
8397
8398 static void
8399 create_breakpoint_sal (struct gdbarch *gdbarch,
8400 gdb::array_view<const symtab_and_line> sals,
8401 location_spec_up &&locspec,
8402 gdb::unique_xmalloc_ptr<char> filter,
8403 gdb::unique_xmalloc_ptr<char> cond_string,
8404 gdb::unique_xmalloc_ptr<char> extra_string,
8405 enum bptype type, enum bpdisp disposition,
8406 int thread, int task, int ignore_count,
8407 int from_tty,
8408 int enabled, int internal, unsigned flags,
8409 int display_canonical)
8410 {
8411 std::unique_ptr<code_breakpoint> b
8412 = new_breakpoint_from_type (gdbarch,
8413 type,
8414 sals,
8415 std::move (locspec),
8416 std::move (filter),
8417 std::move (cond_string),
8418 std::move (extra_string),
8419 disposition,
8420 thread, task, ignore_count,
8421 from_tty,
8422 enabled, flags,
8423 display_canonical);
8424
8425 install_breakpoint (internal, std::move (b), 0);
8426 }
8427
8428 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8429 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8430 value. COND_STRING, if not NULL, specified the condition to be
8431 used for all breakpoints. Essentially the only case where
8432 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8433 function. In that case, it's still not possible to specify
8434 separate conditions for different overloaded functions, so
8435 we take just a single condition string.
8436
8437 NOTE: If the function succeeds, the caller is expected to cleanup
8438 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8439 array contents). If the function fails (error() is called), the
8440 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8441 COND and SALS arrays and each of those arrays contents. */
8442
8443 static void
8444 create_breakpoints_sal (struct gdbarch *gdbarch,
8445 struct linespec_result *canonical,
8446 gdb::unique_xmalloc_ptr<char> cond_string,
8447 gdb::unique_xmalloc_ptr<char> extra_string,
8448 enum bptype type, enum bpdisp disposition,
8449 int thread, int task, int ignore_count,
8450 int from_tty,
8451 int enabled, int internal, unsigned flags)
8452 {
8453 if (canonical->pre_expanded)
8454 gdb_assert (canonical->lsals.size () == 1);
8455
8456 for (const auto &lsal : canonical->lsals)
8457 {
8458 /* Note that 'location' can be NULL in the case of a plain
8459 'break', without arguments. */
8460 location_spec_up locspec
8461 = (canonical->locspec != nullptr
8462 ? canonical->locspec->clone ()
8463 : nullptr);
8464 gdb::unique_xmalloc_ptr<char> filter_string
8465 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8466
8467 create_breakpoint_sal (gdbarch, lsal.sals,
8468 std::move (locspec),
8469 std::move (filter_string),
8470 std::move (cond_string),
8471 std::move (extra_string),
8472 type, disposition,
8473 thread, task, ignore_count,
8474 from_tty, enabled, internal, flags,
8475 canonical->special_display);
8476 }
8477 }
8478
8479 /* Parse LOCSPEC which is assumed to be a SAL specification possibly
8480 followed by conditionals. On return, SALS contains an array of SAL
8481 addresses found. LOCSPEC points to the end of the SAL (for
8482 linespec locspecs).
8483
8484 The array and the line spec strings are allocated on the heap, it is
8485 the caller's responsibility to free them. */
8486
8487 static void
8488 parse_breakpoint_sals (location_spec *locspec,
8489 struct linespec_result *canonical)
8490 {
8491 struct symtab_and_line cursal;
8492
8493 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8494 {
8495 const char *spec = as_linespec_location_spec (locspec)->spec_string;
8496
8497 if (spec == NULL)
8498 {
8499 /* The last displayed codepoint, if it's valid, is our default
8500 breakpoint address. */
8501 if (last_displayed_sal_is_valid ())
8502 {
8503 /* Set sal's pspace, pc, symtab, and line to the values
8504 corresponding to the last call to print_frame_info.
8505 Be sure to reinitialize LINE with NOTCURRENT == 0
8506 as the breakpoint line number is inappropriate otherwise.
8507 find_pc_line would adjust PC, re-set it back. */
8508 symtab_and_line sal = get_last_displayed_sal ();
8509 CORE_ADDR pc = sal.pc;
8510
8511 sal = find_pc_line (pc, 0);
8512
8513 /* "break" without arguments is equivalent to "break *PC"
8514 where PC is the last displayed codepoint's address. So
8515 make sure to set sal.explicit_pc to prevent GDB from
8516 trying to expand the list of sals to include all other
8517 instances with the same symtab and line. */
8518 sal.pc = pc;
8519 sal.explicit_pc = 1;
8520
8521 struct linespec_sals lsal;
8522 lsal.sals = {sal};
8523 lsal.canonical = NULL;
8524
8525 canonical->lsals.push_back (std::move (lsal));
8526 return;
8527 }
8528 else
8529 error (_("No default breakpoint address now."));
8530 }
8531 }
8532
8533 /* Force almost all breakpoints to be in terms of the
8534 current_source_symtab (which is decode_line_1's default).
8535 This should produce the results we want almost all of the
8536 time while leaving default_breakpoint_* alone.
8537
8538 ObjC: However, don't match an Objective-C method name which
8539 may have a '+' or '-' succeeded by a '['. */
8540 cursal = get_current_source_symtab_and_line ();
8541 if (last_displayed_sal_is_valid ())
8542 {
8543 const char *spec = NULL;
8544
8545 if (locspec->type () == LINESPEC_LOCATION_SPEC)
8546 spec = as_linespec_location_spec (locspec)->spec_string;
8547
8548 if (!cursal.symtab
8549 || (spec != NULL
8550 && strchr ("+-", spec[0]) != NULL
8551 && spec[1] != '['))
8552 {
8553 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
8554 get_last_displayed_symtab (),
8555 get_last_displayed_line (),
8556 canonical, NULL, NULL);
8557 return;
8558 }
8559 }
8560
8561 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, NULL,
8562 cursal.symtab, cursal.line, canonical, NULL, NULL);
8563 }
8564
8565
8566 /* Convert each SAL into a real PC. Verify that the PC can be
8567 inserted as a breakpoint. If it can't throw an error. */
8568
8569 static void
8570 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
8571 {
8572 for (auto &sal : sals)
8573 resolve_sal_pc (&sal);
8574 }
8575
8576 /* Fast tracepoints may have restrictions on valid locations. For
8577 instance, a fast tracepoint using a jump instead of a trap will
8578 likely have to overwrite more bytes than a trap would, and so can
8579 only be placed where the instruction is longer than the jump, or a
8580 multi-instruction sequence does not have a jump into the middle of
8581 it, etc. */
8582
8583 static void
8584 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8585 gdb::array_view<const symtab_and_line> sals)
8586 {
8587 for (const auto &sal : sals)
8588 {
8589 struct gdbarch *sarch;
8590
8591 sarch = get_sal_arch (sal);
8592 /* We fall back to GDBARCH if there is no architecture
8593 associated with SAL. */
8594 if (sarch == NULL)
8595 sarch = gdbarch;
8596 std::string msg;
8597 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
8598 error (_("May not have a fast tracepoint at %s%s"),
8599 paddress (sarch, sal.pc), msg.c_str ());
8600 }
8601 }
8602
8603 /* Given TOK, a string specification of condition and thread, as
8604 accepted by the 'break' command, extract the condition
8605 string and thread number and set *COND_STRING and *THREAD.
8606 PC identifies the context at which the condition should be parsed.
8607 If no condition is found, *COND_STRING is set to NULL.
8608 If no thread is found, *THREAD is set to -1. */
8609
8610 static void
8611 find_condition_and_thread (const char *tok, CORE_ADDR pc,
8612 gdb::unique_xmalloc_ptr<char> *cond_string,
8613 int *thread, int *task,
8614 gdb::unique_xmalloc_ptr<char> *rest)
8615 {
8616 cond_string->reset ();
8617 *thread = -1;
8618 *task = 0;
8619 rest->reset ();
8620 bool force = false;
8621
8622 while (tok && *tok)
8623 {
8624 const char *end_tok;
8625 int toklen;
8626 const char *cond_start = NULL;
8627 const char *cond_end = NULL;
8628
8629 tok = skip_spaces (tok);
8630
8631 if ((*tok == '"' || *tok == ',') && rest)
8632 {
8633 rest->reset (savestring (tok, strlen (tok)));
8634 return;
8635 }
8636
8637 end_tok = skip_to_space (tok);
8638
8639 toklen = end_tok - tok;
8640
8641 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8642 {
8643 tok = cond_start = end_tok + 1;
8644 try
8645 {
8646 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8647 }
8648 catch (const gdb_exception_error &)
8649 {
8650 if (!force)
8651 throw;
8652 else
8653 tok = tok + strlen (tok);
8654 }
8655 cond_end = tok;
8656 cond_string->reset (savestring (cond_start, cond_end - cond_start));
8657 }
8658 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8659 {
8660 tok = tok + toklen;
8661 force = true;
8662 }
8663 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8664 {
8665 const char *tmptok;
8666 struct thread_info *thr;
8667
8668 tok = end_tok + 1;
8669 thr = parse_thread_id (tok, &tmptok);
8670 if (tok == tmptok)
8671 error (_("Junk after thread keyword."));
8672 *thread = thr->global_num;
8673 tok = tmptok;
8674 }
8675 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8676 {
8677 char *tmptok;
8678
8679 tok = end_tok + 1;
8680 *task = strtol (tok, &tmptok, 0);
8681 if (tok == tmptok)
8682 error (_("Junk after task keyword."));
8683 if (!valid_task_id (*task))
8684 error (_("Unknown task %d."), *task);
8685 tok = tmptok;
8686 }
8687 else if (rest)
8688 {
8689 rest->reset (savestring (tok, strlen (tok)));
8690 return;
8691 }
8692 else
8693 error (_("Junk at end of arguments."));
8694 }
8695 }
8696
8697 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
8698 succeeds. The parsed values are written to COND_STRING, THREAD,
8699 TASK, and REST. See the comment of 'find_condition_and_thread'
8700 for the description of these parameters and INPUT. */
8701
8702 static void
8703 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
8704 const char *input,
8705 gdb::unique_xmalloc_ptr<char> *cond_string,
8706 int *thread, int *task,
8707 gdb::unique_xmalloc_ptr<char> *rest)
8708 {
8709 int num_failures = 0;
8710 for (auto &sal : sals)
8711 {
8712 gdb::unique_xmalloc_ptr<char> cond;
8713 int thread_id = 0;
8714 int task_id = 0;
8715 gdb::unique_xmalloc_ptr<char> remaining;
8716
8717 /* Here we want to parse 'arg' to separate condition from thread
8718 number. But because parsing happens in a context and the
8719 contexts of sals might be different, try each until there is
8720 success. Finding one successful parse is sufficient for our
8721 goal. When setting the breakpoint we'll re-parse the
8722 condition in the context of each sal. */
8723 try
8724 {
8725 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8726 &task_id, &remaining);
8727 *cond_string = std::move (cond);
8728 *thread = thread_id;
8729 *task = task_id;
8730 *rest = std::move (remaining);
8731 break;
8732 }
8733 catch (const gdb_exception_error &e)
8734 {
8735 num_failures++;
8736 /* If no sal remains, do not continue. */
8737 if (num_failures == sals.size ())
8738 throw;
8739 }
8740 }
8741 }
8742
8743 /* Decode a static tracepoint marker spec. */
8744
8745 static std::vector<symtab_and_line>
8746 decode_static_tracepoint_spec (const char **arg_p)
8747 {
8748 const char *p = &(*arg_p)[3];
8749 const char *endp;
8750
8751 p = skip_spaces (p);
8752
8753 endp = skip_to_space (p);
8754
8755 std::string marker_str (p, endp - p);
8756
8757 std::vector<static_tracepoint_marker> markers
8758 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8759 if (markers.empty ())
8760 error (_("No known static tracepoint marker named %s"),
8761 marker_str.c_str ());
8762
8763 std::vector<symtab_and_line> sals;
8764 sals.reserve (markers.size ());
8765
8766 for (const static_tracepoint_marker &marker : markers)
8767 {
8768 symtab_and_line sal = find_pc_line (marker.address, 0);
8769 sal.pc = marker.address;
8770 sals.push_back (sal);
8771 }
8772
8773 *arg_p = endp;
8774 return sals;
8775 }
8776
8777 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8778 according to IS_TRACEPOINT. */
8779
8780 static const struct breakpoint_ops *
8781 breakpoint_ops_for_location_spec_type (enum location_spec_type locspec_type,
8782 bool is_tracepoint)
8783 {
8784 if (is_tracepoint)
8785 {
8786 if (locspec_type == PROBE_LOCATION_SPEC)
8787 return &tracepoint_probe_breakpoint_ops;
8788 else
8789 return &code_breakpoint_ops;
8790 }
8791 else
8792 {
8793 if (locspec_type == PROBE_LOCATION_SPEC)
8794 return &bkpt_probe_breakpoint_ops;
8795 else
8796 return &code_breakpoint_ops;
8797 }
8798 }
8799
8800 /* See breakpoint.h. */
8801
8802 const struct breakpoint_ops *
8803 breakpoint_ops_for_location_spec (const location_spec *locspec,
8804 bool is_tracepoint)
8805 {
8806 if (locspec != nullptr)
8807 return (breakpoint_ops_for_location_spec_type
8808 (locspec->type (), is_tracepoint));
8809 return &code_breakpoint_ops;
8810 }
8811
8812 /* See breakpoint.h. */
8813
8814 int
8815 create_breakpoint (struct gdbarch *gdbarch,
8816 location_spec *locspec,
8817 const char *cond_string,
8818 int thread, const char *extra_string,
8819 bool force_condition, int parse_extra,
8820 int tempflag, enum bptype type_wanted,
8821 int ignore_count,
8822 enum auto_boolean pending_break_support,
8823 const struct breakpoint_ops *ops,
8824 int from_tty, int enabled, int internal,
8825 unsigned flags)
8826 {
8827 struct linespec_result canonical;
8828 int pending = 0;
8829 int task = 0;
8830 int prev_bkpt_count = breakpoint_count;
8831
8832 gdb_assert (ops != NULL);
8833
8834 /* If extra_string isn't useful, set it to NULL. */
8835 if (extra_string != NULL && *extra_string == '\0')
8836 extra_string = NULL;
8837
8838 try
8839 {
8840 ops->create_sals_from_location_spec (locspec, &canonical);
8841 }
8842 catch (const gdb_exception_error &e)
8843 {
8844 /* If caller is interested in rc value from parse, set
8845 value. */
8846 if (e.error == NOT_FOUND_ERROR)
8847 {
8848 /* If pending breakpoint support is turned off, throw
8849 error. */
8850
8851 if (pending_break_support == AUTO_BOOLEAN_FALSE)
8852 throw;
8853
8854 exception_print (gdb_stderr, e);
8855
8856 /* If pending breakpoint support is auto query and the user
8857 selects no, then simply return the error code. */
8858 if (pending_break_support == AUTO_BOOLEAN_AUTO
8859 && !nquery (_("Make %s pending on future shared library load? "),
8860 bptype_string (type_wanted)))
8861 return 0;
8862
8863 /* At this point, either the user was queried about setting
8864 a pending breakpoint and selected yes, or pending
8865 breakpoint behavior is on and thus a pending breakpoint
8866 is defaulted on behalf of the user. */
8867 pending = 1;
8868 }
8869 else
8870 throw;
8871 }
8872
8873 if (!pending && canonical.lsals.empty ())
8874 return 0;
8875
8876 /* Resolve all line numbers to PC's and verify that the addresses
8877 are ok for the target. */
8878 if (!pending)
8879 {
8880 for (auto &lsal : canonical.lsals)
8881 breakpoint_sals_to_pc (lsal.sals);
8882 }
8883
8884 /* Fast tracepoints may have additional restrictions on location. */
8885 if (!pending && type_wanted == bp_fast_tracepoint)
8886 {
8887 for (const auto &lsal : canonical.lsals)
8888 check_fast_tracepoint_sals (gdbarch, lsal.sals);
8889 }
8890
8891 /* Verify that condition can be parsed, before setting any
8892 breakpoints. Allocate a separate condition expression for each
8893 breakpoint. */
8894 if (!pending)
8895 {
8896 gdb::unique_xmalloc_ptr<char> cond_string_copy;
8897 gdb::unique_xmalloc_ptr<char> extra_string_copy;
8898
8899 if (parse_extra)
8900 {
8901 gdb::unique_xmalloc_ptr<char> rest;
8902 gdb::unique_xmalloc_ptr<char> cond;
8903
8904 const linespec_sals &lsal = canonical.lsals[0];
8905
8906 find_condition_and_thread_for_sals (lsal.sals, extra_string,
8907 &cond, &thread, &task, &rest);
8908 cond_string_copy = std::move (cond);
8909 extra_string_copy = std::move (rest);
8910 }
8911 else
8912 {
8913 if (type_wanted != bp_dprintf
8914 && extra_string != NULL && *extra_string != '\0')
8915 error (_("Garbage '%s' at end of location"), extra_string);
8916
8917 /* Check the validity of the condition. We should error out
8918 if the condition is invalid at all of the locations and
8919 if it is not forced. In the PARSE_EXTRA case above, this
8920 check is done when parsing the EXTRA_STRING. */
8921 if (cond_string != nullptr && !force_condition)
8922 {
8923 int num_failures = 0;
8924 const linespec_sals &lsal = canonical.lsals[0];
8925 for (const auto &sal : lsal.sals)
8926 {
8927 const char *cond = cond_string;
8928 try
8929 {
8930 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
8931 /* One success is sufficient to keep going. */
8932 break;
8933 }
8934 catch (const gdb_exception_error &)
8935 {
8936 num_failures++;
8937 /* If this is the last sal, error out. */
8938 if (num_failures == lsal.sals.size ())
8939 throw;
8940 }
8941 }
8942 }
8943
8944 /* Create a private copy of condition string. */
8945 if (cond_string)
8946 cond_string_copy.reset (xstrdup (cond_string));
8947 /* Create a private copy of any extra string. */
8948 if (extra_string)
8949 extra_string_copy.reset (xstrdup (extra_string));
8950 }
8951
8952 ops->create_breakpoints_sal (gdbarch, &canonical,
8953 std::move (cond_string_copy),
8954 std::move (extra_string_copy),
8955 type_wanted,
8956 tempflag ? disp_del : disp_donttouch,
8957 thread, task, ignore_count,
8958 from_tty, enabled, internal, flags);
8959 }
8960 else
8961 {
8962 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (gdbarch,
8963 type_wanted);
8964 b->locspec = locspec->clone ();
8965
8966 if (parse_extra)
8967 b->cond_string = NULL;
8968 else
8969 {
8970 /* Create a private copy of condition string. */
8971 b->cond_string.reset (cond_string != NULL
8972 ? xstrdup (cond_string)
8973 : NULL);
8974 b->thread = thread;
8975 }
8976
8977 /* Create a private copy of any extra string. */
8978 b->extra_string.reset (extra_string != NULL
8979 ? xstrdup (extra_string)
8980 : NULL);
8981 b->ignore_count = ignore_count;
8982 b->disposition = tempflag ? disp_del : disp_donttouch;
8983 b->condition_not_parsed = 1;
8984 b->enable_state = enabled ? bp_enabled : bp_disabled;
8985 if ((type_wanted != bp_breakpoint
8986 && type_wanted != bp_hardware_breakpoint) || thread != -1)
8987 b->pspace = current_program_space;
8988
8989 install_breakpoint (internal, std::move (b), 0);
8990 }
8991
8992 if (canonical.lsals.size () > 1)
8993 {
8994 warning (_("Multiple breakpoints were set.\nUse the "
8995 "\"delete\" command to delete unwanted breakpoints."));
8996 prev_breakpoint_count = prev_bkpt_count;
8997 }
8998
8999 update_global_location_list (UGLL_MAY_INSERT);
9000
9001 return 1;
9002 }
9003
9004 /* Set a breakpoint.
9005 ARG is a string describing breakpoint address,
9006 condition, and thread.
9007 FLAG specifies if a breakpoint is hardware on,
9008 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9009 and BP_TEMPFLAG. */
9010
9011 static void
9012 break_command_1 (const char *arg, int flag, int from_tty)
9013 {
9014 int tempflag = flag & BP_TEMPFLAG;
9015 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9016 ? bp_hardware_breakpoint
9017 : bp_breakpoint);
9018
9019 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9020 const struct breakpoint_ops *ops
9021 = breakpoint_ops_for_location_spec (locspec.get (),
9022 false /* is_tracepoint */);
9023
9024 create_breakpoint (get_current_arch (),
9025 locspec.get (),
9026 NULL, 0, arg, false, 1 /* parse arg */,
9027 tempflag, type_wanted,
9028 0 /* Ignore count */,
9029 pending_break_support,
9030 ops,
9031 from_tty,
9032 1 /* enabled */,
9033 0 /* internal */,
9034 0);
9035 }
9036
9037 /* Helper function for break_command_1 and disassemble_command. */
9038
9039 void
9040 resolve_sal_pc (struct symtab_and_line *sal)
9041 {
9042 CORE_ADDR pc;
9043
9044 if (sal->pc == 0 && sal->symtab != NULL)
9045 {
9046 if (!find_line_pc (sal->symtab, sal->line, &pc))
9047 error (_("No line %d in file \"%s\"."),
9048 sal->line, symtab_to_filename_for_display (sal->symtab));
9049 sal->pc = pc;
9050
9051 /* If this SAL corresponds to a breakpoint inserted using a line
9052 number, then skip the function prologue if necessary. */
9053 if (sal->explicit_line)
9054 skip_prologue_sal (sal);
9055 }
9056
9057 if (sal->section == 0 && sal->symtab != NULL)
9058 {
9059 const struct blockvector *bv;
9060 const struct block *b;
9061 struct symbol *sym;
9062
9063 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9064 sal->symtab->compunit ());
9065 if (bv != NULL)
9066 {
9067 sym = block_linkage_function (b);
9068 if (sym != NULL)
9069 {
9070 fixup_symbol_section (sym, sal->symtab->compunit ()->objfile ());
9071 sal->section
9072 = sym->obj_section (sal->symtab->compunit ()->objfile ());
9073 }
9074 else
9075 {
9076 /* It really is worthwhile to have the section, so we'll
9077 just have to look harder. This case can be executed
9078 if we have line numbers but no functions (as can
9079 happen in assembly source). */
9080
9081 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9082 switch_to_program_space_and_thread (sal->pspace);
9083
9084 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9085 if (msym.minsym)
9086 sal->section = msym.obj_section ();
9087 }
9088 }
9089 }
9090 }
9091
9092 void
9093 break_command (const char *arg, int from_tty)
9094 {
9095 break_command_1 (arg, 0, from_tty);
9096 }
9097
9098 void
9099 tbreak_command (const char *arg, int from_tty)
9100 {
9101 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9102 }
9103
9104 static void
9105 hbreak_command (const char *arg, int from_tty)
9106 {
9107 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9108 }
9109
9110 static void
9111 thbreak_command (const char *arg, int from_tty)
9112 {
9113 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9114 }
9115
9116 /* The dynamic printf command is mostly like a regular breakpoint, but
9117 with a prewired command list consisting of a single output command,
9118 built from extra arguments supplied on the dprintf command
9119 line. */
9120
9121 static void
9122 dprintf_command (const char *arg, int from_tty)
9123 {
9124 location_spec_up locspec = string_to_location_spec (&arg, current_language);
9125
9126 /* If non-NULL, ARG should have been advanced past the location;
9127 the next character must be ','. */
9128 if (arg != NULL)
9129 {
9130 if (arg[0] != ',' || arg[1] == '\0')
9131 error (_("Format string required"));
9132 else
9133 {
9134 /* Skip the comma. */
9135 ++arg;
9136 }
9137 }
9138
9139 create_breakpoint (get_current_arch (),
9140 locspec.get (),
9141 NULL, 0, arg, false, 1 /* parse arg */,
9142 0, bp_dprintf,
9143 0 /* Ignore count */,
9144 pending_break_support,
9145 &code_breakpoint_ops,
9146 from_tty,
9147 1 /* enabled */,
9148 0 /* internal */,
9149 0);
9150 }
9151
9152 static void
9153 agent_printf_command (const char *arg, int from_tty)
9154 {
9155 error (_("May only run agent-printf on the target"));
9156 }
9157
9158 /* Implement the "breakpoint_hit" method for ranged breakpoints. */
9159
9160 int
9161 ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
9162 const address_space *aspace,
9163 CORE_ADDR bp_addr,
9164 const target_waitstatus &ws)
9165 {
9166 if (ws.kind () != TARGET_WAITKIND_STOPPED
9167 || ws.sig () != GDB_SIGNAL_TRAP)
9168 return 0;
9169
9170 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9171 bl->length, aspace, bp_addr);
9172 }
9173
9174 /* Implement the "resources_needed" method for ranged breakpoints. */
9175
9176 int
9177 ranged_breakpoint::resources_needed (const struct bp_location *bl)
9178 {
9179 return target_ranged_break_num_registers ();
9180 }
9181
9182 /* Implement the "print_it" method for ranged breakpoints. */
9183
9184 enum print_stop_action
9185 ranged_breakpoint::print_it (const bpstat *bs) const
9186 {
9187 struct bp_location *bl = loc;
9188 struct ui_out *uiout = current_uiout;
9189
9190 gdb_assert (type == bp_hardware_breakpoint);
9191
9192 /* Ranged breakpoints have only one location. */
9193 gdb_assert (bl && bl->next == NULL);
9194
9195 annotate_breakpoint (number);
9196
9197 maybe_print_thread_hit_breakpoint (uiout);
9198
9199 if (disposition == disp_del)
9200 uiout->text ("Temporary ranged breakpoint ");
9201 else
9202 uiout->text ("Ranged breakpoint ");
9203 if (uiout->is_mi_like_p ())
9204 {
9205 uiout->field_string ("reason",
9206 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9207 uiout->field_string ("disp", bpdisp_text (disposition));
9208 }
9209 uiout->field_signed ("bkptno", number);
9210 uiout->text (", ");
9211
9212 return PRINT_SRC_AND_LOC;
9213 }
9214
9215 /* Implement the "print_one" method for ranged breakpoints. */
9216
9217 bool
9218 ranged_breakpoint::print_one (bp_location **last_loc) const
9219 {
9220 struct bp_location *bl = loc;
9221 struct value_print_options opts;
9222 struct ui_out *uiout = current_uiout;
9223
9224 /* Ranged breakpoints have only one location. */
9225 gdb_assert (bl && bl->next == NULL);
9226
9227 get_user_print_options (&opts);
9228
9229 if (opts.addressprint)
9230 /* We don't print the address range here, it will be printed later
9231 by ranged_breakpoint::print_one_detail. */
9232 uiout->field_skip ("addr");
9233 annotate_field (5);
9234 print_breakpoint_location (this, bl);
9235 *last_loc = bl;
9236
9237 return true;
9238 }
9239
9240 /* Implement the "print_one_detail" method for ranged breakpoints. */
9241
9242 void
9243 ranged_breakpoint::print_one_detail (struct ui_out *uiout) const
9244 {
9245 CORE_ADDR address_start, address_end;
9246 struct bp_location *bl = loc;
9247 string_file stb;
9248
9249 gdb_assert (bl);
9250
9251 address_start = bl->address;
9252 address_end = address_start + bl->length - 1;
9253
9254 uiout->text ("\taddress range: ");
9255 stb.printf ("[%s, %s]",
9256 print_core_address (bl->gdbarch, address_start),
9257 print_core_address (bl->gdbarch, address_end));
9258 uiout->field_stream ("addr", stb);
9259 uiout->text ("\n");
9260 }
9261
9262 /* Implement the "print_mention" method for ranged breakpoints. */
9263
9264 void
9265 ranged_breakpoint::print_mention () const
9266 {
9267 struct bp_location *bl = loc;
9268 struct ui_out *uiout = current_uiout;
9269
9270 gdb_assert (bl);
9271 gdb_assert (type == bp_hardware_breakpoint);
9272
9273 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9274 number, paddress (bl->gdbarch, bl->address),
9275 paddress (bl->gdbarch, bl->address + bl->length - 1));
9276 }
9277
9278 /* Implement the "print_recreate" method for ranged breakpoints. */
9279
9280 void
9281 ranged_breakpoint::print_recreate (struct ui_file *fp) const
9282 {
9283 gdb_printf (fp, "break-range %s, %s",
9284 locspec->to_string (),
9285 locspec_range_end->to_string ());
9286 print_recreate_thread (fp);
9287 }
9288
9289 /* Find the address where the end of the breakpoint range should be
9290 placed, given the SAL of the end of the range. This is so that if
9291 the user provides a line number, the end of the range is set to the
9292 last instruction of the given line. */
9293
9294 static CORE_ADDR
9295 find_breakpoint_range_end (struct symtab_and_line sal)
9296 {
9297 CORE_ADDR end;
9298
9299 /* If the user provided a PC value, use it. Otherwise,
9300 find the address of the end of the given location. */
9301 if (sal.explicit_pc)
9302 end = sal.pc;
9303 else
9304 {
9305 int ret;
9306 CORE_ADDR start;
9307
9308 ret = find_line_pc_range (sal, &start, &end);
9309 if (!ret)
9310 error (_("Could not find location of the end of the range."));
9311
9312 /* find_line_pc_range returns the start of the next line. */
9313 end--;
9314 }
9315
9316 return end;
9317 }
9318
9319 /* Implement the "break-range" CLI command. */
9320
9321 static void
9322 break_range_command (const char *arg, int from_tty)
9323 {
9324 const char *arg_start;
9325 struct linespec_result canonical_start, canonical_end;
9326 int bp_count, can_use_bp, length;
9327 CORE_ADDR end;
9328
9329 /* We don't support software ranged breakpoints. */
9330 if (target_ranged_break_num_registers () < 0)
9331 error (_("This target does not support hardware ranged breakpoints."));
9332
9333 bp_count = hw_breakpoint_used_count ();
9334 bp_count += target_ranged_break_num_registers ();
9335 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9336 bp_count, 0);
9337 if (can_use_bp < 0)
9338 error (_("Hardware breakpoints used exceeds limit."));
9339
9340 arg = skip_spaces (arg);
9341 if (arg == NULL || arg[0] == '\0')
9342 error(_("No address range specified."));
9343
9344 arg_start = arg;
9345 location_spec_up start_locspec
9346 = string_to_location_spec (&arg, current_language);
9347 parse_breakpoint_sals (start_locspec.get (), &canonical_start);
9348
9349 if (arg[0] != ',')
9350 error (_("Too few arguments."));
9351 else if (canonical_start.lsals.empty ())
9352 error (_("Could not find location of the beginning of the range."));
9353
9354 const linespec_sals &lsal_start = canonical_start.lsals[0];
9355
9356 if (canonical_start.lsals.size () > 1
9357 || lsal_start.sals.size () != 1)
9358 error (_("Cannot create a ranged breakpoint with multiple locations."));
9359
9360 const symtab_and_line &sal_start = lsal_start.sals[0];
9361 std::string addr_string_start (arg_start, arg - arg_start);
9362
9363 arg++; /* Skip the comma. */
9364 arg = skip_spaces (arg);
9365
9366 /* Parse the end location specification. */
9367
9368 arg_start = arg;
9369
9370 /* We call decode_line_full directly here instead of using
9371 parse_breakpoint_sals because we need to specify the start
9372 location spec's symtab and line as the default symtab and line
9373 for the end of the range. This makes it possible to have ranges
9374 like "foo.c:27, +14", where +14 means 14 lines from the start
9375 location spec. */
9376 location_spec_up end_locspec
9377 = string_to_location_spec (&arg, current_language);
9378 decode_line_full (end_locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9379 sal_start.symtab, sal_start.line,
9380 &canonical_end, NULL, NULL);
9381
9382 if (canonical_end.lsals.empty ())
9383 error (_("Could not find location of the end of the range."));
9384
9385 const linespec_sals &lsal_end = canonical_end.lsals[0];
9386 if (canonical_end.lsals.size () > 1
9387 || lsal_end.sals.size () != 1)
9388 error (_("Cannot create a ranged breakpoint with multiple locations."));
9389
9390 const symtab_and_line &sal_end = lsal_end.sals[0];
9391
9392 end = find_breakpoint_range_end (sal_end);
9393 if (sal_start.pc > end)
9394 error (_("Invalid address range, end precedes start."));
9395
9396 length = end - sal_start.pc + 1;
9397 if (length < 0)
9398 /* Length overflowed. */
9399 error (_("Address range too large."));
9400 else if (length == 1)
9401 {
9402 /* This range is simple enough to be handled by
9403 the `hbreak' command. */
9404 hbreak_command (&addr_string_start[0], 1);
9405
9406 return;
9407 }
9408
9409 /* Now set up the breakpoint and install it. */
9410
9411 std::unique_ptr<breakpoint> br
9412 (new ranged_breakpoint (get_current_arch (),
9413 sal_start, length,
9414 std::move (start_locspec),
9415 std::move (end_locspec)));
9416
9417 install_breakpoint (false, std::move (br), true);
9418 }
9419
9420 /* Return non-zero if EXP is verified as constant. Returned zero
9421 means EXP is variable. Also the constant detection may fail for
9422 some constant expressions and in such case still falsely return
9423 zero. */
9424
9425 static bool
9426 watchpoint_exp_is_const (const struct expression *exp)
9427 {
9428 return exp->op->constant_p ();
9429 }
9430
9431 /* Implement the "re_set" method for watchpoints. */
9432
9433 void
9434 watchpoint::re_set ()
9435 {
9436 /* Watchpoint can be either on expression using entirely global
9437 variables, or it can be on local variables.
9438
9439 Watchpoints of the first kind are never auto-deleted, and even
9440 persist across program restarts. Since they can use variables
9441 from shared libraries, we need to reparse expression as libraries
9442 are loaded and unloaded.
9443
9444 Watchpoints on local variables can also change meaning as result
9445 of solib event. For example, if a watchpoint uses both a local
9446 and a global variables in expression, it's a local watchpoint,
9447 but unloading of a shared library will make the expression
9448 invalid. This is not a very common use case, but we still
9449 re-evaluate expression, to avoid surprises to the user.
9450
9451 Note that for local watchpoints, we re-evaluate it only if
9452 watchpoints frame id is still valid. If it's not, it means the
9453 watchpoint is out of scope and will be deleted soon. In fact,
9454 I'm not sure we'll ever be called in this case.
9455
9456 If a local watchpoint's frame id is still valid, then
9457 exp_valid_block is likewise valid, and we can safely use it.
9458
9459 Don't do anything about disabled watchpoints, since they will be
9460 reevaluated again when enabled. */
9461 update_watchpoint (this, 1 /* reparse */);
9462 }
9463
9464 /* Implement the "insert" method for hardware watchpoints. */
9465
9466 int
9467 watchpoint::insert_location (struct bp_location *bl)
9468 {
9469 int length = exact ? 1 : bl->length;
9470
9471 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9472 cond_exp.get ());
9473 }
9474
9475 /* Implement the "remove" method for hardware watchpoints. */
9476
9477 int
9478 watchpoint::remove_location (struct bp_location *bl,
9479 enum remove_bp_reason reason)
9480 {
9481 int length = exact ? 1 : bl->length;
9482
9483 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9484 cond_exp.get ());
9485 }
9486
9487 int
9488 watchpoint::breakpoint_hit (const struct bp_location *bl,
9489 const address_space *aspace, CORE_ADDR bp_addr,
9490 const target_waitstatus &ws)
9491 {
9492 struct breakpoint *b = bl->owner;
9493
9494 /* Continuable hardware watchpoints are treated as non-existent if the
9495 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9496 some data address). Otherwise gdb won't stop on a break instruction
9497 in the code (not from a breakpoint) when a hardware watchpoint has
9498 been defined. Also skip watchpoints which we know did not trigger
9499 (did not match the data address). */
9500 if (is_hardware_watchpoint (b)
9501 && watchpoint_triggered == watch_triggered_no)
9502 return 0;
9503
9504 return 1;
9505 }
9506
9507 void
9508 watchpoint::check_status (bpstat *bs)
9509 {
9510 gdb_assert (is_watchpoint (bs->breakpoint_at));
9511
9512 bpstat_check_watchpoint (bs);
9513 }
9514
9515 /* Implement the "resources_needed" method for hardware
9516 watchpoints. */
9517
9518 int
9519 watchpoint::resources_needed (const struct bp_location *bl)
9520 {
9521 int length = exact? 1 : bl->length;
9522
9523 return target_region_ok_for_hw_watchpoint (bl->address, length);
9524 }
9525
9526 /* Implement the "works_in_software_mode" method for hardware
9527 watchpoints. */
9528
9529 bool
9530 watchpoint::works_in_software_mode () const
9531 {
9532 /* Read and access watchpoints only work with hardware support. */
9533 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9534 }
9535
9536 enum print_stop_action
9537 watchpoint::print_it (const bpstat *bs) const
9538 {
9539 struct breakpoint *b;
9540 enum print_stop_action result;
9541 struct ui_out *uiout = current_uiout;
9542
9543 gdb_assert (bs->bp_location_at != NULL);
9544
9545 b = bs->breakpoint_at;
9546
9547 annotate_watchpoint (b->number);
9548 maybe_print_thread_hit_breakpoint (uiout);
9549
9550 string_file stb;
9551
9552 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9553 switch (b->type)
9554 {
9555 case bp_watchpoint:
9556 case bp_hardware_watchpoint:
9557 if (uiout->is_mi_like_p ())
9558 uiout->field_string
9559 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9560 mention (b);
9561 tuple_emitter.emplace (uiout, "value");
9562 uiout->text ("\nOld value = ");
9563 watchpoint_value_print (bs->old_val.get (), &stb);
9564 uiout->field_stream ("old", stb);
9565 uiout->text ("\nNew value = ");
9566 watchpoint_value_print (val.get (), &stb);
9567 uiout->field_stream ("new", stb);
9568 uiout->text ("\n");
9569 /* More than one watchpoint may have been triggered. */
9570 result = PRINT_UNKNOWN;
9571 break;
9572
9573 case bp_read_watchpoint:
9574 if (uiout->is_mi_like_p ())
9575 uiout->field_string
9576 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9577 mention (b);
9578 tuple_emitter.emplace (uiout, "value");
9579 uiout->text ("\nValue = ");
9580 watchpoint_value_print (val.get (), &stb);
9581 uiout->field_stream ("value", stb);
9582 uiout->text ("\n");
9583 result = PRINT_UNKNOWN;
9584 break;
9585
9586 case bp_access_watchpoint:
9587 if (bs->old_val != NULL)
9588 {
9589 if (uiout->is_mi_like_p ())
9590 uiout->field_string
9591 ("reason",
9592 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9593 mention (b);
9594 tuple_emitter.emplace (uiout, "value");
9595 uiout->text ("\nOld value = ");
9596 watchpoint_value_print (bs->old_val.get (), &stb);
9597 uiout->field_stream ("old", stb);
9598 uiout->text ("\nNew value = ");
9599 }
9600 else
9601 {
9602 mention (b);
9603 if (uiout->is_mi_like_p ())
9604 uiout->field_string
9605 ("reason",
9606 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9607 tuple_emitter.emplace (uiout, "value");
9608 uiout->text ("\nValue = ");
9609 }
9610 watchpoint_value_print (val.get (), &stb);
9611 uiout->field_stream ("new", stb);
9612 uiout->text ("\n");
9613 result = PRINT_UNKNOWN;
9614 break;
9615 default:
9616 result = PRINT_UNKNOWN;
9617 }
9618
9619 return result;
9620 }
9621
9622 /* Implement the "print_mention" method for hardware watchpoints. */
9623
9624 void
9625 watchpoint::print_mention () const
9626 {
9627 struct ui_out *uiout = current_uiout;
9628 const char *tuple_name;
9629
9630 switch (type)
9631 {
9632 case bp_watchpoint:
9633 uiout->text ("Watchpoint ");
9634 tuple_name = "wpt";
9635 break;
9636 case bp_hardware_watchpoint:
9637 uiout->text ("Hardware watchpoint ");
9638 tuple_name = "wpt";
9639 break;
9640 case bp_read_watchpoint:
9641 uiout->text ("Hardware read watchpoint ");
9642 tuple_name = "hw-rwpt";
9643 break;
9644 case bp_access_watchpoint:
9645 uiout->text ("Hardware access (read/write) watchpoint ");
9646 tuple_name = "hw-awpt";
9647 break;
9648 default:
9649 internal_error (__FILE__, __LINE__,
9650 _("Invalid hardware watchpoint type."));
9651 }
9652
9653 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9654 uiout->field_signed ("number", number);
9655 uiout->text (": ");
9656 uiout->field_string ("exp", exp_string.get ());
9657 }
9658
9659 /* Implement the "print_recreate" method for watchpoints. */
9660
9661 void
9662 watchpoint::print_recreate (struct ui_file *fp) const
9663 {
9664 switch (type)
9665 {
9666 case bp_watchpoint:
9667 case bp_hardware_watchpoint:
9668 gdb_printf (fp, "watch");
9669 break;
9670 case bp_read_watchpoint:
9671 gdb_printf (fp, "rwatch");
9672 break;
9673 case bp_access_watchpoint:
9674 gdb_printf (fp, "awatch");
9675 break;
9676 default:
9677 internal_error (__FILE__, __LINE__,
9678 _("Invalid watchpoint type."));
9679 }
9680
9681 gdb_printf (fp, " %s", exp_string.get ());
9682 print_recreate_thread (fp);
9683 }
9684
9685 /* Implement the "explains_signal" method for watchpoints. */
9686
9687 bool
9688 watchpoint::explains_signal (enum gdb_signal sig)
9689 {
9690 /* A software watchpoint cannot cause a signal other than
9691 GDB_SIGNAL_TRAP. */
9692 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
9693 return false;
9694
9695 return true;
9696 }
9697
9698 struct masked_watchpoint : public watchpoint
9699 {
9700 using watchpoint::watchpoint;
9701
9702 int insert_location (struct bp_location *) override;
9703 int remove_location (struct bp_location *,
9704 enum remove_bp_reason reason) override;
9705 int resources_needed (const struct bp_location *) override;
9706 bool works_in_software_mode () const override;
9707 enum print_stop_action print_it (const bpstat *bs) const override;
9708 void print_one_detail (struct ui_out *) const override;
9709 void print_mention () const override;
9710 void print_recreate (struct ui_file *fp) const override;
9711 };
9712
9713 /* Implement the "insert" method for masked hardware watchpoints. */
9714
9715 int
9716 masked_watchpoint::insert_location (struct bp_location *bl)
9717 {
9718 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
9719 bl->watchpoint_type);
9720 }
9721
9722 /* Implement the "remove" method for masked hardware watchpoints. */
9723
9724 int
9725 masked_watchpoint::remove_location (struct bp_location *bl,
9726 enum remove_bp_reason reason)
9727 {
9728 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
9729 bl->watchpoint_type);
9730 }
9731
9732 /* Implement the "resources_needed" method for masked hardware
9733 watchpoints. */
9734
9735 int
9736 masked_watchpoint::resources_needed (const struct bp_location *bl)
9737 {
9738 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
9739 }
9740
9741 /* Implement the "works_in_software_mode" method for masked hardware
9742 watchpoints. */
9743
9744 bool
9745 masked_watchpoint::works_in_software_mode () const
9746 {
9747 return false;
9748 }
9749
9750 /* Implement the "print_it" method for masked hardware
9751 watchpoints. */
9752
9753 enum print_stop_action
9754 masked_watchpoint::print_it (const bpstat *bs) const
9755 {
9756 struct breakpoint *b = bs->breakpoint_at;
9757 struct ui_out *uiout = current_uiout;
9758
9759 /* Masked watchpoints have only one location. */
9760 gdb_assert (b->loc && b->loc->next == NULL);
9761
9762 annotate_watchpoint (b->number);
9763 maybe_print_thread_hit_breakpoint (uiout);
9764
9765 switch (b->type)
9766 {
9767 case bp_hardware_watchpoint:
9768 if (uiout->is_mi_like_p ())
9769 uiout->field_string
9770 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9771 break;
9772
9773 case bp_read_watchpoint:
9774 if (uiout->is_mi_like_p ())
9775 uiout->field_string
9776 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9777 break;
9778
9779 case bp_access_watchpoint:
9780 if (uiout->is_mi_like_p ())
9781 uiout->field_string
9782 ("reason",
9783 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9784 break;
9785 default:
9786 internal_error (__FILE__, __LINE__,
9787 _("Invalid hardware watchpoint type."));
9788 }
9789
9790 mention (b);
9791 uiout->text (_("\n\
9792 Check the underlying instruction at PC for the memory\n\
9793 address and value which triggered this watchpoint.\n"));
9794 uiout->text ("\n");
9795
9796 /* More than one watchpoint may have been triggered. */
9797 return PRINT_UNKNOWN;
9798 }
9799
9800 /* Implement the "print_one_detail" method for masked hardware
9801 watchpoints. */
9802
9803 void
9804 masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9805 {
9806 /* Masked watchpoints have only one location. */
9807 gdb_assert (loc && loc->next == NULL);
9808
9809 uiout->text ("\tmask ");
9810 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
9811 uiout->text ("\n");
9812 }
9813
9814 /* Implement the "print_mention" method for masked hardware
9815 watchpoints. */
9816
9817 void
9818 masked_watchpoint::print_mention () const
9819 {
9820 struct ui_out *uiout = current_uiout;
9821 const char *tuple_name;
9822
9823 switch (type)
9824 {
9825 case bp_hardware_watchpoint:
9826 uiout->text ("Masked hardware watchpoint ");
9827 tuple_name = "wpt";
9828 break;
9829 case bp_read_watchpoint:
9830 uiout->text ("Masked hardware read watchpoint ");
9831 tuple_name = "hw-rwpt";
9832 break;
9833 case bp_access_watchpoint:
9834 uiout->text ("Masked hardware access (read/write) watchpoint ");
9835 tuple_name = "hw-awpt";
9836 break;
9837 default:
9838 internal_error (__FILE__, __LINE__,
9839 _("Invalid hardware watchpoint type."));
9840 }
9841
9842 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9843 uiout->field_signed ("number", number);
9844 uiout->text (": ");
9845 uiout->field_string ("exp", exp_string.get ());
9846 }
9847
9848 /* Implement the "print_recreate" method for masked hardware
9849 watchpoints. */
9850
9851 void
9852 masked_watchpoint::print_recreate (struct ui_file *fp) const
9853 {
9854 switch (type)
9855 {
9856 case bp_hardware_watchpoint:
9857 gdb_printf (fp, "watch");
9858 break;
9859 case bp_read_watchpoint:
9860 gdb_printf (fp, "rwatch");
9861 break;
9862 case bp_access_watchpoint:
9863 gdb_printf (fp, "awatch");
9864 break;
9865 default:
9866 internal_error (__FILE__, __LINE__,
9867 _("Invalid hardware watchpoint type."));
9868 }
9869
9870 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
9871 phex (hw_wp_mask, sizeof (CORE_ADDR)));
9872 print_recreate_thread (fp);
9873 }
9874
9875 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
9876
9877 static bool
9878 is_masked_watchpoint (const struct breakpoint *b)
9879 {
9880 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9881 }
9882
9883 /* accessflag: hw_write: watch write,
9884 hw_read: watch read,
9885 hw_access: watch access (read or write) */
9886 static void
9887 watch_command_1 (const char *arg, int accessflag, int from_tty,
9888 bool just_location, bool internal)
9889 {
9890 struct breakpoint *scope_breakpoint = NULL;
9891 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
9892 struct value *result;
9893 int saved_bitpos = 0, saved_bitsize = 0;
9894 const char *exp_start = NULL;
9895 const char *exp_end = NULL;
9896 const char *tok, *end_tok;
9897 int toklen = -1;
9898 const char *cond_start = NULL;
9899 const char *cond_end = NULL;
9900 enum bptype bp_type;
9901 int thread = -1;
9902 /* Flag to indicate whether we are going to use masks for
9903 the hardware watchpoint. */
9904 bool use_mask = false;
9905 CORE_ADDR mask = 0;
9906 int task = 0;
9907
9908 /* Make sure that we actually have parameters to parse. */
9909 if (arg != NULL && arg[0] != '\0')
9910 {
9911 const char *value_start;
9912
9913 exp_end = arg + strlen (arg);
9914
9915 /* Look for "parameter value" pairs at the end
9916 of the arguments string. */
9917 for (tok = exp_end - 1; tok > arg; tok--)
9918 {
9919 /* Skip whitespace at the end of the argument list. */
9920 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9921 tok--;
9922
9923 /* Find the beginning of the last token.
9924 This is the value of the parameter. */
9925 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9926 tok--;
9927 value_start = tok + 1;
9928
9929 /* Skip whitespace. */
9930 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9931 tok--;
9932
9933 end_tok = tok;
9934
9935 /* Find the beginning of the second to last token.
9936 This is the parameter itself. */
9937 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9938 tok--;
9939 tok++;
9940 toklen = end_tok - tok + 1;
9941
9942 if (toklen == 6 && startswith (tok, "thread"))
9943 {
9944 struct thread_info *thr;
9945 /* At this point we've found a "thread" token, which means
9946 the user is trying to set a watchpoint that triggers
9947 only in a specific thread. */
9948 const char *endp;
9949
9950 if (thread != -1)
9951 error(_("You can specify only one thread."));
9952
9953 /* Extract the thread ID from the next token. */
9954 thr = parse_thread_id (value_start, &endp);
9955
9956 /* Check if the user provided a valid thread ID. */
9957 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9958 invalid_thread_id_error (value_start);
9959
9960 thread = thr->global_num;
9961 }
9962 else if (toklen == 4 && startswith (tok, "task"))
9963 {
9964 char *tmp;
9965
9966 task = strtol (value_start, &tmp, 0);
9967 if (tmp == value_start)
9968 error (_("Junk after task keyword."));
9969 if (!valid_task_id (task))
9970 error (_("Unknown task %d."), task);
9971 }
9972 else if (toklen == 4 && startswith (tok, "mask"))
9973 {
9974 /* We've found a "mask" token, which means the user wants to
9975 create a hardware watchpoint that is going to have the mask
9976 facility. */
9977 struct value *mask_value;
9978
9979 if (use_mask)
9980 error(_("You can specify only one mask."));
9981
9982 use_mask = just_location = true;
9983
9984 scoped_value_mark mark;
9985 mask_value = parse_to_comma_and_eval (&value_start);
9986 mask = value_as_address (mask_value);
9987 }
9988 else
9989 /* We didn't recognize what we found. We should stop here. */
9990 break;
9991
9992 /* Truncate the string and get rid of the "parameter value" pair before
9993 the arguments string is parsed by the parse_exp_1 function. */
9994 exp_end = tok;
9995 }
9996 }
9997 else
9998 exp_end = arg;
9999
10000 /* Parse the rest of the arguments. From here on out, everything
10001 is in terms of a newly allocated string instead of the original
10002 ARG. */
10003 std::string expression (arg, exp_end - arg);
10004 exp_start = arg = expression.c_str ();
10005 innermost_block_tracker tracker;
10006 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10007 exp_end = arg;
10008 /* Remove trailing whitespace from the expression before saving it.
10009 This makes the eventual display of the expression string a bit
10010 prettier. */
10011 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10012 --exp_end;
10013
10014 /* Checking if the expression is not constant. */
10015 if (watchpoint_exp_is_const (exp.get ()))
10016 {
10017 int len;
10018
10019 len = exp_end - exp_start;
10020 while (len > 0 && isspace (exp_start[len - 1]))
10021 len--;
10022 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10023 }
10024
10025 exp_valid_block = tracker.block ();
10026 struct value *mark = value_mark ();
10027 struct value *val_as_value = nullptr;
10028 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10029 just_location);
10030
10031 if (val_as_value != NULL && just_location)
10032 {
10033 saved_bitpos = value_bitpos (val_as_value);
10034 saved_bitsize = value_bitsize (val_as_value);
10035 }
10036
10037 value_ref_ptr val;
10038 if (just_location)
10039 {
10040 int ret;
10041
10042 exp_valid_block = NULL;
10043 val = release_value (value_addr (result));
10044 value_free_to_mark (mark);
10045
10046 if (use_mask)
10047 {
10048 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10049 mask);
10050 if (ret == -1)
10051 error (_("This target does not support masked watchpoints."));
10052 else if (ret == -2)
10053 error (_("Invalid mask or memory region."));
10054 }
10055 }
10056 else if (val_as_value != NULL)
10057 val = release_value (val_as_value);
10058
10059 tok = skip_spaces (arg);
10060 end_tok = skip_to_space (tok);
10061
10062 toklen = end_tok - tok;
10063 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10064 {
10065 tok = cond_start = end_tok + 1;
10066 innermost_block_tracker if_tracker;
10067 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10068
10069 /* The watchpoint expression may not be local, but the condition
10070 may still be. E.g.: `watch global if local > 0'. */
10071 cond_exp_valid_block = if_tracker.block ();
10072
10073 cond_end = tok;
10074 }
10075 if (*tok)
10076 error (_("Junk at end of command."));
10077
10078 frame_info_ptr wp_frame = block_innermost_frame (exp_valid_block);
10079
10080 /* Save this because create_internal_breakpoint below invalidates
10081 'wp_frame'. */
10082 frame_id watchpoint_frame = get_frame_id (wp_frame);
10083
10084 /* If the expression is "local", then set up a "watchpoint scope"
10085 breakpoint at the point where we've left the scope of the watchpoint
10086 expression. Create the scope breakpoint before the watchpoint, so
10087 that we will encounter it first in bpstat_stop_status. */
10088 if (exp_valid_block != NULL && wp_frame != NULL)
10089 {
10090 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10091
10092 if (frame_id_p (caller_frame_id))
10093 {
10094 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10095 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10096
10097 scope_breakpoint
10098 = create_internal_breakpoint (caller_arch, caller_pc,
10099 bp_watchpoint_scope);
10100
10101 /* create_internal_breakpoint could invalidate WP_FRAME. */
10102 wp_frame = NULL;
10103
10104 scope_breakpoint->enable_state = bp_enabled;
10105
10106 /* Automatically delete the breakpoint when it hits. */
10107 scope_breakpoint->disposition = disp_del;
10108
10109 /* Only break in the proper frame (help with recursion). */
10110 scope_breakpoint->frame_id = caller_frame_id;
10111
10112 /* Set the address at which we will stop. */
10113 scope_breakpoint->loc->gdbarch = caller_arch;
10114 scope_breakpoint->loc->requested_address = caller_pc;
10115 scope_breakpoint->loc->address
10116 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10117 scope_breakpoint->loc->requested_address,
10118 scope_breakpoint->type,
10119 current_program_space);
10120 }
10121 }
10122
10123 /* Now set up the breakpoint. We create all watchpoints as hardware
10124 watchpoints here even if hardware watchpoints are turned off, a call
10125 to update_watchpoint later in this function will cause the type to
10126 drop back to bp_watchpoint (software watchpoint) if required. */
10127
10128 if (accessflag == hw_read)
10129 bp_type = bp_read_watchpoint;
10130 else if (accessflag == hw_access)
10131 bp_type = bp_access_watchpoint;
10132 else
10133 bp_type = bp_hardware_watchpoint;
10134
10135 std::unique_ptr<watchpoint> w;
10136 if (use_mask)
10137 w.reset (new masked_watchpoint (nullptr, bp_type));
10138 else
10139 w.reset (new watchpoint (nullptr, bp_type));
10140
10141 w->thread = thread;
10142 w->task = task;
10143 w->disposition = disp_donttouch;
10144 w->pspace = current_program_space;
10145 w->exp = std::move (exp);
10146 w->exp_valid_block = exp_valid_block;
10147 w->cond_exp_valid_block = cond_exp_valid_block;
10148 if (just_location)
10149 {
10150 struct type *t = value_type (val.get ());
10151 CORE_ADDR addr = value_as_address (val.get ());
10152
10153 w->exp_string_reparse
10154 = current_language->watch_location_expression (t, addr);
10155
10156 w->exp_string = xstrprintf ("-location %.*s",
10157 (int) (exp_end - exp_start), exp_start);
10158 }
10159 else
10160 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10161
10162 if (use_mask)
10163 {
10164 w->hw_wp_mask = mask;
10165 }
10166 else
10167 {
10168 w->val = val;
10169 w->val_bitpos = saved_bitpos;
10170 w->val_bitsize = saved_bitsize;
10171 w->val_valid = true;
10172 }
10173
10174 if (cond_start)
10175 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10176 else
10177 w->cond_string = 0;
10178
10179 if (frame_id_p (watchpoint_frame))
10180 {
10181 w->watchpoint_frame = watchpoint_frame;
10182 w->watchpoint_thread = inferior_ptid;
10183 }
10184 else
10185 {
10186 w->watchpoint_frame = null_frame_id;
10187 w->watchpoint_thread = null_ptid;
10188 }
10189
10190 if (scope_breakpoint != NULL)
10191 {
10192 /* The scope breakpoint is related to the watchpoint. We will
10193 need to act on them together. */
10194 w->related_breakpoint = scope_breakpoint;
10195 scope_breakpoint->related_breakpoint = w.get ();
10196 }
10197
10198 if (!just_location)
10199 value_free_to_mark (mark);
10200
10201 /* Finally update the new watchpoint. This creates the locations
10202 that should be inserted. */
10203 update_watchpoint (w.get (), 1);
10204
10205 install_breakpoint (internal, std::move (w), 1);
10206 }
10207
10208 /* Return count of debug registers needed to watch the given expression.
10209 If the watchpoint cannot be handled in hardware return zero. */
10210
10211 static int
10212 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10213 {
10214 int found_memory_cnt = 0;
10215
10216 /* Did the user specifically forbid us to use hardware watchpoints? */
10217 if (!can_use_hw_watchpoints)
10218 return 0;
10219
10220 gdb_assert (!vals.empty ());
10221 struct value *head = vals[0].get ();
10222
10223 /* Make sure that the value of the expression depends only upon
10224 memory contents, and values computed from them within GDB. If we
10225 find any register references or function calls, we can't use a
10226 hardware watchpoint.
10227
10228 The idea here is that evaluating an expression generates a series
10229 of values, one holding the value of every subexpression. (The
10230 expression a*b+c has five subexpressions: a, b, a*b, c, and
10231 a*b+c.) GDB's values hold almost enough information to establish
10232 the criteria given above --- they identify memory lvalues,
10233 register lvalues, computed values, etcetera. So we can evaluate
10234 the expression, and then scan the chain of values that leaves
10235 behind to decide whether we can detect any possible change to the
10236 expression's final value using only hardware watchpoints.
10237
10238 However, I don't think that the values returned by inferior
10239 function calls are special in any way. So this function may not
10240 notice that an expression involving an inferior function call
10241 can't be watched with hardware watchpoints. FIXME. */
10242 for (const value_ref_ptr &iter : vals)
10243 {
10244 struct value *v = iter.get ();
10245
10246 if (VALUE_LVAL (v) == lval_memory)
10247 {
10248 if (v != head && value_lazy (v))
10249 /* A lazy memory lvalue in the chain is one that GDB never
10250 needed to fetch; we either just used its address (e.g.,
10251 `a' in `a.b') or we never needed it at all (e.g., `a'
10252 in `a,b'). This doesn't apply to HEAD; if that is
10253 lazy then it was not readable, but watch it anyway. */
10254 ;
10255 else
10256 {
10257 /* Ahh, memory we actually used! Check if we can cover
10258 it with hardware watchpoints. */
10259 struct type *vtype = check_typedef (value_type (v));
10260
10261 /* We only watch structs and arrays if user asked for it
10262 explicitly, never if they just happen to appear in a
10263 middle of some value chain. */
10264 if (v == head
10265 || (vtype->code () != TYPE_CODE_STRUCT
10266 && vtype->code () != TYPE_CODE_ARRAY))
10267 {
10268 CORE_ADDR vaddr = value_address (v);
10269 int len;
10270 int num_regs;
10271
10272 len = (target_exact_watchpoints
10273 && is_scalar_type_recursive (vtype))?
10274 1 : value_type (v)->length ();
10275
10276 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10277 if (!num_regs)
10278 return 0;
10279 else
10280 found_memory_cnt += num_regs;
10281 }
10282 }
10283 }
10284 else if (VALUE_LVAL (v) != not_lval
10285 && deprecated_value_modifiable (v) == 0)
10286 return 0; /* These are values from the history (e.g., $1). */
10287 else if (VALUE_LVAL (v) == lval_register)
10288 return 0; /* Cannot watch a register with a HW watchpoint. */
10289 }
10290
10291 /* The expression itself looks suitable for using a hardware
10292 watchpoint, but give the target machine a chance to reject it. */
10293 return found_memory_cnt;
10294 }
10295
10296 void
10297 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10298 {
10299 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10300 }
10301
10302 /* Options for the watch, awatch, and rwatch commands. */
10303
10304 struct watch_options
10305 {
10306 /* For -location. */
10307 bool location = false;
10308 };
10309
10310 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10311
10312 Historically GDB always accepted both '-location' and '-l' flags for
10313 these commands (both flags being synonyms). When converting to the
10314 newer option scheme only '-location' is added here. That's fine (for
10315 backward compatibility) as any non-ambiguous prefix of a flag will be
10316 accepted, so '-l', '-loc', are now all accepted.
10317
10318 What this means is that, if in the future, we add any new flag here
10319 that starts with '-l' then this will break backward compatibility, so
10320 please, don't do that! */
10321
10322 static const gdb::option::option_def watch_option_defs[] = {
10323 gdb::option::flag_option_def<watch_options> {
10324 "location",
10325 [] (watch_options *opt) { return &opt->location; },
10326 N_("\
10327 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10328 -l can be used as a short form of -location."),
10329 },
10330 };
10331
10332 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10333 commands. */
10334
10335 static gdb::option::option_def_group
10336 make_watch_options_def_group (watch_options *opts)
10337 {
10338 return {{watch_option_defs}, opts};
10339 }
10340
10341 /* A helper function that looks for the "-location" argument and then
10342 calls watch_command_1. */
10343
10344 static void
10345 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10346 {
10347 watch_options opts;
10348 auto grp = make_watch_options_def_group (&opts);
10349 gdb::option::process_options
10350 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10351 if (arg != nullptr && *arg == '\0')
10352 arg = nullptr;
10353
10354 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10355 }
10356
10357 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10358 static void
10359 watch_command_completer (struct cmd_list_element *ignore,
10360 completion_tracker &tracker,
10361 const char *text, const char * /*word*/)
10362 {
10363 const auto group = make_watch_options_def_group (nullptr);
10364 if (gdb::option::complete_options
10365 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10366 return;
10367
10368 const char *word = advance_to_expression_complete_word_point (tracker, text);
10369 expression_completer (ignore, tracker, text, word);
10370 }
10371
10372 static void
10373 watch_command (const char *arg, int from_tty)
10374 {
10375 watch_maybe_just_location (arg, hw_write, from_tty);
10376 }
10377
10378 void
10379 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10380 {
10381 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10382 }
10383
10384 static void
10385 rwatch_command (const char *arg, int from_tty)
10386 {
10387 watch_maybe_just_location (arg, hw_read, from_tty);
10388 }
10389
10390 void
10391 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
10392 {
10393 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10394 }
10395
10396 static void
10397 awatch_command (const char *arg, int from_tty)
10398 {
10399 watch_maybe_just_location (arg, hw_access, from_tty);
10400 }
10401 \f
10402
10403 /* Data for the FSM that manages the until(location)/advance commands
10404 in infcmd.c. Here because it uses the mechanisms of
10405 breakpoints. */
10406
10407 struct until_break_fsm : public thread_fsm
10408 {
10409 /* The thread that was current when the command was executed. */
10410 int thread;
10411
10412 /* The breakpoint set at the return address in the caller frame,
10413 plus breakpoints at all the destination locations. */
10414 std::vector<breakpoint_up> breakpoints;
10415
10416 until_break_fsm (struct interp *cmd_interp, int thread,
10417 std::vector<breakpoint_up> &&breakpoints)
10418 : thread_fsm (cmd_interp),
10419 thread (thread),
10420 breakpoints (std::move (breakpoints))
10421 {
10422 }
10423
10424 void clean_up (struct thread_info *thread) override;
10425 bool should_stop (struct thread_info *thread) override;
10426 enum async_reply_reason do_async_reply_reason () override;
10427 };
10428
10429 /* Implementation of the 'should_stop' FSM method for the
10430 until(location)/advance commands. */
10431
10432 bool
10433 until_break_fsm::should_stop (struct thread_info *tp)
10434 {
10435 for (const breakpoint_up &bp : breakpoints)
10436 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10437 bp.get ()) != NULL)
10438 {
10439 set_finished ();
10440 break;
10441 }
10442
10443 return true;
10444 }
10445
10446 /* Implementation of the 'clean_up' FSM method for the
10447 until(location)/advance commands. */
10448
10449 void
10450 until_break_fsm::clean_up (struct thread_info *)
10451 {
10452 /* Clean up our temporary breakpoints. */
10453 breakpoints.clear ();
10454 delete_longjmp_breakpoint (thread);
10455 }
10456
10457 /* Implementation of the 'async_reply_reason' FSM method for the
10458 until(location)/advance commands. */
10459
10460 enum async_reply_reason
10461 until_break_fsm::do_async_reply_reason ()
10462 {
10463 return EXEC_ASYNC_LOCATION_REACHED;
10464 }
10465
10466 void
10467 until_break_command (const char *arg, int from_tty, int anywhere)
10468 {
10469 frame_info_ptr frame;
10470 struct gdbarch *frame_gdbarch;
10471 struct frame_id stack_frame_id;
10472 struct frame_id caller_frame_id;
10473 int thread;
10474 struct thread_info *tp;
10475
10476 clear_proceed_status (0);
10477
10478 /* Set a breakpoint where the user wants it and at return from
10479 this function. */
10480
10481 location_spec_up locspec = string_to_location_spec (&arg, current_language);
10482
10483 std::vector<symtab_and_line> sals
10484 = (last_displayed_sal_is_valid ()
10485 ? decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10486 get_last_displayed_symtab (),
10487 get_last_displayed_line ())
10488 : decode_line_1 (locspec.get (), DECODE_LINE_FUNFIRSTLINE,
10489 NULL, NULL, 0));
10490
10491 if (sals.empty ())
10492 error (_("Couldn't get information on specified line."));
10493
10494 if (*arg)
10495 error (_("Junk at end of arguments."));
10496
10497 tp = inferior_thread ();
10498 thread = tp->global_num;
10499
10500 /* Note linespec handling above invalidates the frame chain.
10501 Installing a breakpoint also invalidates the frame chain (as it
10502 may need to switch threads), so do any frame handling before
10503 that. */
10504
10505 frame = get_selected_frame (NULL);
10506 frame_gdbarch = get_frame_arch (frame);
10507 stack_frame_id = get_stack_frame_id (frame);
10508 caller_frame_id = frame_unwind_caller_id (frame);
10509
10510 /* Keep within the current frame, or in frames called by the current
10511 one. */
10512
10513 std::vector<breakpoint_up> breakpoints;
10514
10515 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10516
10517 if (frame_id_p (caller_frame_id))
10518 {
10519 struct symtab_and_line sal2;
10520 struct gdbarch *caller_gdbarch;
10521
10522 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10523 sal2.pc = frame_unwind_caller_pc (frame);
10524 caller_gdbarch = frame_unwind_caller_arch (frame);
10525
10526 breakpoint_up caller_breakpoint
10527 = set_momentary_breakpoint (caller_gdbarch, sal2,
10528 caller_frame_id, bp_until);
10529 breakpoints.emplace_back (std::move (caller_breakpoint));
10530
10531 set_longjmp_breakpoint (tp, stack_frame_id);
10532 lj_deleter.emplace (thread);
10533 }
10534
10535 /* set_momentary_breakpoint could invalidate FRAME. */
10536 frame = NULL;
10537
10538 /* If the user told us to continue until a specified location, we
10539 don't specify a frame at which we need to stop. Otherwise,
10540 specify the selected frame, because we want to stop only at the
10541 very same frame. */
10542 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10543
10544 for (symtab_and_line &sal : sals)
10545 {
10546 resolve_sal_pc (&sal);
10547
10548 breakpoint_up location_breakpoint
10549 = set_momentary_breakpoint (frame_gdbarch, sal,
10550 stop_frame_id, bp_until);
10551 breakpoints.emplace_back (std::move (location_breakpoint));
10552 }
10553
10554 tp->set_thread_fsm
10555 (std::unique_ptr<thread_fsm>
10556 (new until_break_fsm (command_interp (), tp->global_num,
10557 std::move (breakpoints))));
10558
10559 if (lj_deleter)
10560 lj_deleter->release ();
10561
10562 proceed (-1, GDB_SIGNAL_DEFAULT);
10563 }
10564
10565 \f
10566
10567 /* Compare two breakpoints and return a strcmp-like result. */
10568
10569 static int
10570 compare_breakpoints (const breakpoint *a, const breakpoint *b)
10571 {
10572 uintptr_t ua = (uintptr_t) a;
10573 uintptr_t ub = (uintptr_t) b;
10574
10575 if (a->number < b->number)
10576 return -1;
10577 else if (a->number > b->number)
10578 return 1;
10579
10580 /* Now sort by address, in case we see, e..g, two breakpoints with
10581 the number 0. */
10582 if (ua < ub)
10583 return -1;
10584 return ua > ub ? 1 : 0;
10585 }
10586
10587 /* Delete breakpoints by address or line. */
10588
10589 static void
10590 clear_command (const char *arg, int from_tty)
10591 {
10592 int default_match;
10593
10594 std::vector<symtab_and_line> decoded_sals;
10595 symtab_and_line last_sal;
10596 gdb::array_view<symtab_and_line> sals;
10597 if (arg)
10598 {
10599 decoded_sals
10600 = decode_line_with_current_source (arg,
10601 (DECODE_LINE_FUNFIRSTLINE
10602 | DECODE_LINE_LIST_MODE));
10603 default_match = 0;
10604 sals = decoded_sals;
10605 }
10606 else
10607 {
10608 /* Set sal's line, symtab, pc, and pspace to the values
10609 corresponding to the last call to print_frame_info. If the
10610 codepoint is not valid, this will set all the fields to 0. */
10611 last_sal = get_last_displayed_sal ();
10612 if (last_sal.symtab == 0)
10613 error (_("No source file specified."));
10614
10615 default_match = 1;
10616 sals = last_sal;
10617 }
10618
10619 /* We don't call resolve_sal_pc here. That's not as bad as it
10620 seems, because all existing breakpoints typically have both
10621 file/line and pc set. So, if clear is given file/line, we can
10622 match this to existing breakpoint without obtaining pc at all.
10623
10624 We only support clearing given the address explicitly
10625 present in breakpoint table. Say, we've set breakpoint
10626 at file:line. There were several PC values for that file:line,
10627 due to optimization, all in one block.
10628
10629 We've picked one PC value. If "clear" is issued with another
10630 PC corresponding to the same file:line, the breakpoint won't
10631 be cleared. We probably can still clear the breakpoint, but
10632 since the other PC value is never presented to user, user
10633 can only find it by guessing, and it does not seem important
10634 to support that. */
10635
10636 /* For each line spec given, delete bps which correspond to it. Do
10637 it in two passes, solely to preserve the current behavior that
10638 from_tty is forced true if we delete more than one
10639 breakpoint. */
10640
10641 std::vector<struct breakpoint *> found;
10642 for (const auto &sal : sals)
10643 {
10644 const char *sal_fullname;
10645
10646 /* If exact pc given, clear bpts at that pc.
10647 If line given (pc == 0), clear all bpts on specified line.
10648 If defaulting, clear all bpts on default line
10649 or at default pc.
10650
10651 defaulting sal.pc != 0 tests to do
10652
10653 0 1 pc
10654 1 1 pc _and_ line
10655 0 0 line
10656 1 0 <can't happen> */
10657
10658 sal_fullname = (sal.symtab == NULL
10659 ? NULL : symtab_to_fullname (sal.symtab));
10660
10661 /* Find all matching breakpoints and add them to 'found'. */
10662 for (breakpoint *b : all_breakpoints ())
10663 {
10664 int match = 0;
10665 /* Are we going to delete b? */
10666 if (b->type != bp_none && !is_watchpoint (b)
10667 && user_breakpoint_p (b))
10668 {
10669 for (bp_location *loc : b->locations ())
10670 {
10671 /* If the user specified file:line, don't allow a PC
10672 match. This matches historical gdb behavior. */
10673 int pc_match = (!sal.explicit_line
10674 && sal.pc
10675 && (loc->pspace == sal.pspace)
10676 && (loc->address == sal.pc)
10677 && (!section_is_overlay (loc->section)
10678 || loc->section == sal.section));
10679 int line_match = 0;
10680
10681 if ((default_match || sal.explicit_line)
10682 && loc->symtab != NULL
10683 && sal_fullname != NULL
10684 && sal.pspace == loc->pspace
10685 && loc->line_number == sal.line
10686 && filename_cmp (symtab_to_fullname (loc->symtab),
10687 sal_fullname) == 0)
10688 line_match = 1;
10689
10690 if (pc_match || line_match)
10691 {
10692 match = 1;
10693 break;
10694 }
10695 }
10696 }
10697
10698 if (match)
10699 found.push_back (b);
10700 }
10701 }
10702
10703 /* Now go thru the 'found' chain and delete them. */
10704 if (found.empty ())
10705 {
10706 if (arg)
10707 error (_("No breakpoint at %s."), arg);
10708 else
10709 error (_("No breakpoint at this line."));
10710 }
10711
10712 /* Remove duplicates from the vec. */
10713 std::sort (found.begin (), found.end (),
10714 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10715 {
10716 return compare_breakpoints (bp_a, bp_b) < 0;
10717 });
10718 found.erase (std::unique (found.begin (), found.end (),
10719 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10720 {
10721 return compare_breakpoints (bp_a, bp_b) == 0;
10722 }),
10723 found.end ());
10724
10725 if (found.size () > 1)
10726 from_tty = 1; /* Always report if deleted more than one. */
10727 if (from_tty)
10728 {
10729 if (found.size () == 1)
10730 gdb_printf (_("Deleted breakpoint "));
10731 else
10732 gdb_printf (_("Deleted breakpoints "));
10733 }
10734
10735 for (breakpoint *iter : found)
10736 {
10737 if (from_tty)
10738 gdb_printf ("%d ", iter->number);
10739 delete_breakpoint (iter);
10740 }
10741 if (from_tty)
10742 gdb_putc ('\n');
10743 }
10744 \f
10745 /* Delete breakpoint in BS if they are `delete' breakpoints and
10746 all breakpoints that are marked for deletion, whether hit or not.
10747 This is called after any breakpoint is hit, or after errors. */
10748
10749 void
10750 breakpoint_auto_delete (bpstat *bs)
10751 {
10752 for (; bs; bs = bs->next)
10753 if (bs->breakpoint_at
10754 && bs->breakpoint_at->disposition == disp_del
10755 && bs->stop)
10756 delete_breakpoint (bs->breakpoint_at);
10757
10758 for (breakpoint *b : all_breakpoints_safe ())
10759 if (b->disposition == disp_del_at_next_stop)
10760 delete_breakpoint (b);
10761 }
10762
10763 /* A comparison function for bp_location AP and BP being interfaced to
10764 std::sort. Sort elements primarily by their ADDRESS (no matter what
10765 bl_address_is_meaningful says), secondarily by ordering first
10766 permanent elements and terciarily just ensuring the array is sorted
10767 stable way despite std::sort being an unstable algorithm. */
10768
10769 static int
10770 bp_location_is_less_than (const bp_location *a, const bp_location *b)
10771 {
10772 if (a->address != b->address)
10773 return a->address < b->address;
10774
10775 /* Sort locations at the same address by their pspace number, keeping
10776 locations of the same inferior (in a multi-inferior environment)
10777 grouped. */
10778
10779 if (a->pspace->num != b->pspace->num)
10780 return a->pspace->num < b->pspace->num;
10781
10782 /* Sort permanent breakpoints first. */
10783 if (a->permanent != b->permanent)
10784 return a->permanent > b->permanent;
10785
10786 /* Sort by type in order to make duplicate determination easier.
10787 See update_global_location_list. This is kept in sync with
10788 breakpoint_locations_match. */
10789 if (a->loc_type < b->loc_type)
10790 return true;
10791
10792 /* Likewise, for range-breakpoints, sort by length. */
10793 if (a->loc_type == bp_loc_hardware_breakpoint
10794 && b->loc_type == bp_loc_hardware_breakpoint
10795 && a->length < b->length)
10796 return true;
10797
10798 /* Make the internal GDB representation stable across GDB runs
10799 where A and B memory inside GDB can differ. Breakpoint locations of
10800 the same type at the same address can be sorted in arbitrary order. */
10801
10802 if (a->owner->number != b->owner->number)
10803 return a->owner->number < b->owner->number;
10804
10805 return a < b;
10806 }
10807
10808 /* Set bp_locations_placed_address_before_address_max and
10809 bp_locations_shadow_len_after_address_max according to the current
10810 content of the bp_locations array. */
10811
10812 static void
10813 bp_locations_target_extensions_update (void)
10814 {
10815 bp_locations_placed_address_before_address_max = 0;
10816 bp_locations_shadow_len_after_address_max = 0;
10817
10818 for (bp_location *bl : all_bp_locations ())
10819 {
10820 CORE_ADDR start, end, addr;
10821
10822 if (!bp_location_has_shadow (bl))
10823 continue;
10824
10825 start = bl->target_info.placed_address;
10826 end = start + bl->target_info.shadow_len;
10827
10828 gdb_assert (bl->address >= start);
10829 addr = bl->address - start;
10830 if (addr > bp_locations_placed_address_before_address_max)
10831 bp_locations_placed_address_before_address_max = addr;
10832
10833 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10834
10835 gdb_assert (bl->address < end);
10836 addr = end - bl->address;
10837 if (addr > bp_locations_shadow_len_after_address_max)
10838 bp_locations_shadow_len_after_address_max = addr;
10839 }
10840 }
10841
10842 /* Download tracepoint locations if they haven't been. */
10843
10844 static void
10845 download_tracepoint_locations (void)
10846 {
10847 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
10848
10849 scoped_restore_current_pspace_and_thread restore_pspace_thread;
10850
10851 for (breakpoint *b : all_tracepoints ())
10852 {
10853 struct tracepoint *t;
10854 int bp_location_downloaded = 0;
10855
10856 if ((b->type == bp_fast_tracepoint
10857 ? !may_insert_fast_tracepoints
10858 : !may_insert_tracepoints))
10859 continue;
10860
10861 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
10862 {
10863 if (target_can_download_tracepoint ())
10864 can_download_tracepoint = TRIBOOL_TRUE;
10865 else
10866 can_download_tracepoint = TRIBOOL_FALSE;
10867 }
10868
10869 if (can_download_tracepoint == TRIBOOL_FALSE)
10870 break;
10871
10872 for (bp_location *bl : b->locations ())
10873 {
10874 /* In tracepoint, locations are _never_ duplicated, so
10875 should_be_inserted is equivalent to
10876 unduplicated_should_be_inserted. */
10877 if (!should_be_inserted (bl) || bl->inserted)
10878 continue;
10879
10880 switch_to_program_space_and_thread (bl->pspace);
10881
10882 target_download_tracepoint (bl);
10883
10884 bl->inserted = 1;
10885 bp_location_downloaded = 1;
10886 }
10887 t = (struct tracepoint *) b;
10888 t->number_on_target = b->number;
10889 if (bp_location_downloaded)
10890 gdb::observers::breakpoint_modified.notify (b);
10891 }
10892 }
10893
10894 /* Swap the insertion/duplication state between two locations. */
10895
10896 static void
10897 swap_insertion (struct bp_location *left, struct bp_location *right)
10898 {
10899 const int left_inserted = left->inserted;
10900 const int left_duplicate = left->duplicate;
10901 const int left_needs_update = left->needs_update;
10902 const struct bp_target_info left_target_info = left->target_info;
10903
10904 /* Locations of tracepoints can never be duplicated. */
10905 if (is_tracepoint (left->owner))
10906 gdb_assert (!left->duplicate);
10907 if (is_tracepoint (right->owner))
10908 gdb_assert (!right->duplicate);
10909
10910 left->inserted = right->inserted;
10911 left->duplicate = right->duplicate;
10912 left->needs_update = right->needs_update;
10913 left->target_info = right->target_info;
10914 right->inserted = left_inserted;
10915 right->duplicate = left_duplicate;
10916 right->needs_update = left_needs_update;
10917 right->target_info = left_target_info;
10918 }
10919
10920 /* Force the re-insertion of the locations at ADDRESS. This is called
10921 once a new/deleted/modified duplicate location is found and we are evaluating
10922 conditions on the target's side. Such conditions need to be updated on
10923 the target. */
10924
10925 static void
10926 force_breakpoint_reinsertion (struct bp_location *bl)
10927 {
10928 CORE_ADDR address = 0;
10929 int pspace_num;
10930
10931 address = bl->address;
10932 pspace_num = bl->pspace->num;
10933
10934 /* This is only meaningful if the target is
10935 evaluating conditions and if the user has
10936 opted for condition evaluation on the target's
10937 side. */
10938 if (gdb_evaluates_breakpoint_condition_p ()
10939 || !target_supports_evaluation_of_breakpoint_conditions ())
10940 return;
10941
10942 /* Flag all breakpoint locations with this address and
10943 the same program space as the location
10944 as "its condition has changed". We need to
10945 update the conditions on the target's side. */
10946 for (bp_location *loc : all_bp_locations_at_addr (address))
10947 {
10948 if (!is_breakpoint (loc->owner)
10949 || pspace_num != loc->pspace->num)
10950 continue;
10951
10952 /* Flag the location appropriately. We use a different state to
10953 let everyone know that we already updated the set of locations
10954 with addr bl->address and program space bl->pspace. This is so
10955 we don't have to keep calling these functions just to mark locations
10956 that have already been marked. */
10957 loc->condition_changed = condition_updated;
10958
10959 /* Free the agent expression bytecode as well. We will compute
10960 it later on. */
10961 loc->cond_bytecode.reset ();
10962 }
10963 }
10964
10965 /* Called whether new breakpoints are created, or existing breakpoints
10966 deleted, to update the global location list and recompute which
10967 locations are duplicate of which.
10968
10969 The INSERT_MODE flag determines whether locations may not, may, or
10970 shall be inserted now. See 'enum ugll_insert_mode' for more
10971 info. */
10972
10973 static void
10974 update_global_location_list (enum ugll_insert_mode insert_mode)
10975 {
10976 /* Last breakpoint location address that was marked for update. */
10977 CORE_ADDR last_addr = 0;
10978 /* Last breakpoint location program space that was marked for update. */
10979 int last_pspace_num = -1;
10980
10981 /* Used in the duplicates detection below. When iterating over all
10982 bp_locations, points to the first bp_location of a given address.
10983 Breakpoints and watchpoints of different types are never
10984 duplicates of each other. Keep one pointer for each type of
10985 breakpoint/watchpoint, so we only need to loop over all locations
10986 once. */
10987 struct bp_location *bp_loc_first; /* breakpoint */
10988 struct bp_location *wp_loc_first; /* hardware watchpoint */
10989 struct bp_location *awp_loc_first; /* access watchpoint */
10990 struct bp_location *rwp_loc_first; /* read watchpoint */
10991
10992 /* Saved former bp_locations array which we compare against the newly
10993 built bp_locations from the current state of ALL_BREAKPOINTS. */
10994 std::vector<bp_location *> old_locations = std::move (bp_locations);
10995 bp_locations.clear ();
10996
10997 for (breakpoint *b : all_breakpoints ())
10998 for (bp_location *loc : b->locations ())
10999 bp_locations.push_back (loc);
11000
11001 /* See if we need to "upgrade" a software breakpoint to a hardware
11002 breakpoint. Do this before deciding whether locations are
11003 duplicates. Also do this before sorting because sorting order
11004 depends on location type. */
11005 for (bp_location *loc : bp_locations)
11006 if (!loc->inserted && should_be_inserted (loc))
11007 handle_automatic_hardware_breakpoints (loc);
11008
11009 std::sort (bp_locations.begin (), bp_locations.end (),
11010 bp_location_is_less_than);
11011
11012 bp_locations_target_extensions_update ();
11013
11014 /* Identify bp_location instances that are no longer present in the
11015 new list, and therefore should be freed. Note that it's not
11016 necessary that those locations should be removed from inferior --
11017 if there's another location at the same address (previously
11018 marked as duplicate), we don't need to remove/insert the
11019 location.
11020
11021 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11022 and former bp_location array state respectively. */
11023
11024 size_t loc_i = 0;
11025 for (bp_location *old_loc : old_locations)
11026 {
11027 /* Tells if 'old_loc' is found among the new locations. If
11028 not, we have to free it. */
11029 int found_object = 0;
11030 /* Tells if the location should remain inserted in the target. */
11031 int keep_in_target = 0;
11032 int removed = 0;
11033
11034 /* Skip LOCP entries which will definitely never be needed.
11035 Stop either at or being the one matching OLD_LOC. */
11036 while (loc_i < bp_locations.size ()
11037 && bp_locations[loc_i]->address < old_loc->address)
11038 loc_i++;
11039
11040 for (size_t loc2_i = loc_i;
11041 (loc2_i < bp_locations.size ()
11042 && bp_locations[loc2_i]->address == old_loc->address);
11043 loc2_i++)
11044 {
11045 /* Check if this is a new/duplicated location or a duplicated
11046 location that had its condition modified. If so, we want to send
11047 its condition to the target if evaluation of conditions is taking
11048 place there. */
11049 if (bp_locations[loc2_i]->condition_changed == condition_modified
11050 && (last_addr != old_loc->address
11051 || last_pspace_num != old_loc->pspace->num))
11052 {
11053 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11054 last_pspace_num = old_loc->pspace->num;
11055 }
11056
11057 if (bp_locations[loc2_i] == old_loc)
11058 found_object = 1;
11059 }
11060
11061 /* We have already handled this address, update it so that we don't
11062 have to go through updates again. */
11063 last_addr = old_loc->address;
11064
11065 /* Target-side condition evaluation: Handle deleted locations. */
11066 if (!found_object)
11067 force_breakpoint_reinsertion (old_loc);
11068
11069 /* If this location is no longer present, and inserted, look if
11070 there's maybe a new location at the same address. If so,
11071 mark that one inserted, and don't remove this one. This is
11072 needed so that we don't have a time window where a breakpoint
11073 at certain location is not inserted. */
11074
11075 if (old_loc->inserted)
11076 {
11077 /* If the location is inserted now, we might have to remove
11078 it. */
11079
11080 if (found_object && should_be_inserted (old_loc))
11081 {
11082 /* The location is still present in the location list,
11083 and still should be inserted. Don't do anything. */
11084 keep_in_target = 1;
11085 }
11086 else
11087 {
11088 /* This location still exists, but it won't be kept in the
11089 target since it may have been disabled. We proceed to
11090 remove its target-side condition. */
11091
11092 /* The location is either no longer present, or got
11093 disabled. See if there's another location at the
11094 same address, in which case we don't need to remove
11095 this one from the target. */
11096
11097 /* OLD_LOC comes from existing struct breakpoint. */
11098 if (bl_address_is_meaningful (old_loc))
11099 {
11100 for (size_t loc2_i = loc_i;
11101 (loc2_i < bp_locations.size ()
11102 && bp_locations[loc2_i]->address == old_loc->address);
11103 loc2_i++)
11104 {
11105 bp_location *loc2 = bp_locations[loc2_i];
11106
11107 if (loc2 == old_loc)
11108 continue;
11109
11110 if (breakpoint_locations_match (loc2, old_loc))
11111 {
11112 /* Read watchpoint locations are switched to
11113 access watchpoints, if the former are not
11114 supported, but the latter are. */
11115 if (is_hardware_watchpoint (old_loc->owner))
11116 {
11117 gdb_assert (is_hardware_watchpoint (loc2->owner));
11118 loc2->watchpoint_type = old_loc->watchpoint_type;
11119 }
11120
11121 /* loc2 is a duplicated location. We need to check
11122 if it should be inserted in case it will be
11123 unduplicated. */
11124 if (unduplicated_should_be_inserted (loc2))
11125 {
11126 swap_insertion (old_loc, loc2);
11127 keep_in_target = 1;
11128 break;
11129 }
11130 }
11131 }
11132 }
11133 }
11134
11135 if (!keep_in_target)
11136 {
11137 if (remove_breakpoint (old_loc))
11138 {
11139 /* This is just about all we can do. We could keep
11140 this location on the global list, and try to
11141 remove it next time, but there's no particular
11142 reason why we will succeed next time.
11143
11144 Note that at this point, old_loc->owner is still
11145 valid, as delete_breakpoint frees the breakpoint
11146 only after calling us. */
11147 gdb_printf (_("warning: Error removing "
11148 "breakpoint %d\n"),
11149 old_loc->owner->number);
11150 }
11151 removed = 1;
11152 }
11153 }
11154
11155 if (!found_object)
11156 {
11157 if (removed && target_is_non_stop_p ()
11158 && need_moribund_for_location_type (old_loc))
11159 {
11160 /* This location was removed from the target. In
11161 non-stop mode, a race condition is possible where
11162 we've removed a breakpoint, but stop events for that
11163 breakpoint are already queued and will arrive later.
11164 We apply an heuristic to be able to distinguish such
11165 SIGTRAPs from other random SIGTRAPs: we keep this
11166 breakpoint location for a bit, and will retire it
11167 after we see some number of events. The theory here
11168 is that reporting of events should, "on the average",
11169 be fair, so after a while we'll see events from all
11170 threads that have anything of interest, and no longer
11171 need to keep this breakpoint location around. We
11172 don't hold locations forever so to reduce chances of
11173 mistaking a non-breakpoint SIGTRAP for a breakpoint
11174 SIGTRAP.
11175
11176 The heuristic failing can be disastrous on
11177 decr_pc_after_break targets.
11178
11179 On decr_pc_after_break targets, like e.g., x86-linux,
11180 if we fail to recognize a late breakpoint SIGTRAP,
11181 because events_till_retirement has reached 0 too
11182 soon, we'll fail to do the PC adjustment, and report
11183 a random SIGTRAP to the user. When the user resumes
11184 the inferior, it will most likely immediately crash
11185 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11186 corrupted, because of being resumed e.g., in the
11187 middle of a multi-byte instruction, or skipped a
11188 one-byte instruction. This was actually seen happen
11189 on native x86-linux, and should be less rare on
11190 targets that do not support new thread events, like
11191 remote, due to the heuristic depending on
11192 thread_count.
11193
11194 Mistaking a random SIGTRAP for a breakpoint trap
11195 causes similar symptoms (PC adjustment applied when
11196 it shouldn't), but then again, playing with SIGTRAPs
11197 behind the debugger's back is asking for trouble.
11198
11199 Since hardware watchpoint traps are always
11200 distinguishable from other traps, so we don't need to
11201 apply keep hardware watchpoint moribund locations
11202 around. We simply always ignore hardware watchpoint
11203 traps we can no longer explain. */
11204
11205 process_stratum_target *proc_target = nullptr;
11206 for (inferior *inf : all_inferiors ())
11207 if (inf->pspace == old_loc->pspace)
11208 {
11209 proc_target = inf->process_target ();
11210 break;
11211 }
11212 if (proc_target != nullptr)
11213 old_loc->events_till_retirement
11214 = 3 * (thread_count (proc_target) + 1);
11215 else
11216 old_loc->events_till_retirement = 1;
11217 old_loc->owner = NULL;
11218
11219 moribund_locations.push_back (old_loc);
11220 }
11221 else
11222 {
11223 old_loc->owner = NULL;
11224 decref_bp_location (&old_loc);
11225 }
11226 }
11227 }
11228
11229 /* Rescan breakpoints at the same address and section, marking the
11230 first one as "first" and any others as "duplicates". This is so
11231 that the bpt instruction is only inserted once. If we have a
11232 permanent breakpoint at the same place as BPT, make that one the
11233 official one, and the rest as duplicates. Permanent breakpoints
11234 are sorted first for the same address.
11235
11236 Do the same for hardware watchpoints, but also considering the
11237 watchpoint's type (regular/access/read) and length. */
11238
11239 bp_loc_first = NULL;
11240 wp_loc_first = NULL;
11241 awp_loc_first = NULL;
11242 rwp_loc_first = NULL;
11243
11244 for (bp_location *loc : all_bp_locations ())
11245 {
11246 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11247 non-NULL. */
11248 struct bp_location **loc_first_p;
11249 breakpoint *b = loc->owner;
11250
11251 if (!unduplicated_should_be_inserted (loc)
11252 || !bl_address_is_meaningful (loc)
11253 /* Don't detect duplicate for tracepoint locations because they are
11254 never duplicated. See the comments in field `duplicate' of
11255 `struct bp_location'. */
11256 || is_tracepoint (b))
11257 {
11258 /* Clear the condition modification flag. */
11259 loc->condition_changed = condition_unchanged;
11260 continue;
11261 }
11262
11263 if (b->type == bp_hardware_watchpoint)
11264 loc_first_p = &wp_loc_first;
11265 else if (b->type == bp_read_watchpoint)
11266 loc_first_p = &rwp_loc_first;
11267 else if (b->type == bp_access_watchpoint)
11268 loc_first_p = &awp_loc_first;
11269 else
11270 loc_first_p = &bp_loc_first;
11271
11272 if (*loc_first_p == NULL
11273 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11274 || !breakpoint_locations_match (loc, *loc_first_p))
11275 {
11276 *loc_first_p = loc;
11277 loc->duplicate = 0;
11278
11279 if (is_breakpoint (loc->owner) && loc->condition_changed)
11280 {
11281 loc->needs_update = 1;
11282 /* Clear the condition modification flag. */
11283 loc->condition_changed = condition_unchanged;
11284 }
11285 continue;
11286 }
11287
11288
11289 /* This and the above ensure the invariant that the first location
11290 is not duplicated, and is the inserted one.
11291 All following are marked as duplicated, and are not inserted. */
11292 if (loc->inserted)
11293 swap_insertion (loc, *loc_first_p);
11294 loc->duplicate = 1;
11295
11296 /* Clear the condition modification flag. */
11297 loc->condition_changed = condition_unchanged;
11298 }
11299
11300 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11301 {
11302 if (insert_mode != UGLL_DONT_INSERT)
11303 insert_breakpoint_locations ();
11304 else
11305 {
11306 /* Even though the caller told us to not insert new
11307 locations, we may still need to update conditions on the
11308 target's side of breakpoints that were already inserted
11309 if the target is evaluating breakpoint conditions. We
11310 only update conditions for locations that are marked
11311 "needs_update". */
11312 update_inserted_breakpoint_locations ();
11313 }
11314 }
11315
11316 if (insert_mode != UGLL_DONT_INSERT)
11317 download_tracepoint_locations ();
11318 }
11319
11320 void
11321 breakpoint_retire_moribund (void)
11322 {
11323 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11324 {
11325 struct bp_location *loc = moribund_locations[ix];
11326 if (--(loc->events_till_retirement) == 0)
11327 {
11328 decref_bp_location (&loc);
11329 unordered_remove (moribund_locations, ix);
11330 --ix;
11331 }
11332 }
11333 }
11334
11335 static void
11336 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11337 {
11338
11339 try
11340 {
11341 update_global_location_list (insert_mode);
11342 }
11343 catch (const gdb_exception_error &e)
11344 {
11345 }
11346 }
11347
11348 /* Clear BKP from a BPS. */
11349
11350 static void
11351 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
11352 {
11353 bpstat *bs;
11354
11355 for (bs = bps; bs; bs = bs->next)
11356 if (bs->breakpoint_at == bpt)
11357 {
11358 bs->breakpoint_at = NULL;
11359 bs->old_val = NULL;
11360 /* bs->commands will be freed later. */
11361 }
11362 }
11363
11364 /* Callback for iterate_over_threads. */
11365 static int
11366 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11367 {
11368 struct breakpoint *bpt = (struct breakpoint *) data;
11369
11370 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11371 return 0;
11372 }
11373
11374 /* Helper for breakpoint and tracepoint breakpoint->mention
11375 callbacks. */
11376
11377 static void
11378 say_where (const breakpoint *b)
11379 {
11380 struct value_print_options opts;
11381
11382 get_user_print_options (&opts);
11383
11384 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11385 single string. */
11386 if (b->loc == NULL)
11387 {
11388 /* For pending locations, the output differs slightly based
11389 on b->extra_string. If this is non-NULL, it contains either
11390 a condition or dprintf arguments. */
11391 if (b->extra_string == NULL)
11392 {
11393 gdb_printf (_(" (%s) pending."), b->locspec->to_string ());
11394 }
11395 else if (b->type == bp_dprintf)
11396 {
11397 gdb_printf (_(" (%s,%s) pending."),
11398 b->locspec->to_string (),
11399 b->extra_string.get ());
11400 }
11401 else
11402 {
11403 gdb_printf (_(" (%s %s) pending."),
11404 b->locspec->to_string (),
11405 b->extra_string.get ());
11406 }
11407 }
11408 else
11409 {
11410 if (opts.addressprint || b->loc->symtab == NULL)
11411 gdb_printf (" at %ps",
11412 styled_string (address_style.style (),
11413 paddress (b->loc->gdbarch,
11414 b->loc->address)));
11415 if (b->loc->symtab != NULL)
11416 {
11417 /* If there is a single location, we can print the location
11418 more nicely. */
11419 if (b->loc->next == NULL)
11420 {
11421 const char *filename
11422 = symtab_to_filename_for_display (b->loc->symtab);
11423 gdb_printf (": file %ps, line %d.",
11424 styled_string (file_name_style.style (),
11425 filename),
11426 b->loc->line_number);
11427 }
11428 else
11429 /* This is not ideal, but each location may have a
11430 different file name, and this at least reflects the
11431 real situation somewhat. */
11432 gdb_printf (": %s.", b->locspec->to_string ());
11433 }
11434
11435 if (b->loc->next)
11436 {
11437 struct bp_location *loc = b->loc;
11438 int n = 0;
11439 for (; loc; loc = loc->next)
11440 ++n;
11441 gdb_printf (" (%d locations)", n);
11442 }
11443 }
11444 }
11445
11446 /* See breakpoint.h. */
11447
11448 bp_location_range breakpoint::locations () const
11449 {
11450 return bp_location_range (this->loc);
11451 }
11452
11453 struct bp_location *
11454 breakpoint::allocate_location ()
11455 {
11456 return new bp_location (this);
11457 }
11458
11459 #define internal_error_pure_virtual_called() \
11460 gdb_assert_not_reached ("pure virtual function called")
11461
11462 int
11463 breakpoint::insert_location (struct bp_location *bl)
11464 {
11465 internal_error_pure_virtual_called ();
11466 }
11467
11468 int
11469 breakpoint::remove_location (struct bp_location *bl,
11470 enum remove_bp_reason reason)
11471 {
11472 internal_error_pure_virtual_called ();
11473 }
11474
11475 int
11476 breakpoint::breakpoint_hit (const struct bp_location *bl,
11477 const address_space *aspace,
11478 CORE_ADDR bp_addr,
11479 const target_waitstatus &ws)
11480 {
11481 internal_error_pure_virtual_called ();
11482 }
11483
11484 int
11485 breakpoint::resources_needed (const struct bp_location *bl)
11486 {
11487 internal_error_pure_virtual_called ();
11488 }
11489
11490 enum print_stop_action
11491 breakpoint::print_it (const bpstat *bs) const
11492 {
11493 internal_error_pure_virtual_called ();
11494 }
11495
11496 void
11497 breakpoint::print_mention () const
11498 {
11499 internal_error_pure_virtual_called ();
11500 }
11501
11502 void
11503 breakpoint::print_recreate (struct ui_file *fp) const
11504 {
11505 internal_error_pure_virtual_called ();
11506 }
11507
11508 /* Default breakpoint_ops methods. */
11509
11510 void
11511 code_breakpoint::re_set ()
11512 {
11513 /* FIXME: is this still reachable? */
11514 if (breakpoint_location_spec_empty_p (this))
11515 {
11516 /* Anything without a location can't be re-set. */
11517 delete_breakpoint (this);
11518 return;
11519 }
11520
11521 re_set_default ();
11522 }
11523
11524 int
11525 code_breakpoint::insert_location (struct bp_location *bl)
11526 {
11527 CORE_ADDR addr = bl->target_info.reqstd_address;
11528
11529 bl->target_info.kind = breakpoint_kind (bl, &addr);
11530 bl->target_info.placed_address = addr;
11531
11532 int result;
11533 if (bl->loc_type == bp_loc_hardware_breakpoint)
11534 result = target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
11535 else
11536 result = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11537
11538 if (result == 0 && bl->probe.prob != nullptr)
11539 {
11540 /* The insertion was successful, now let's set the probe's semaphore
11541 if needed. */
11542 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
11543 }
11544
11545 return result;
11546 }
11547
11548 int
11549 code_breakpoint::remove_location (struct bp_location *bl,
11550 enum remove_bp_reason reason)
11551 {
11552 if (bl->probe.prob != nullptr)
11553 {
11554 /* Let's clear the semaphore before removing the location. */
11555 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
11556 }
11557
11558 if (bl->loc_type == bp_loc_hardware_breakpoint)
11559 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11560 else
11561 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
11562 }
11563
11564 int
11565 code_breakpoint::breakpoint_hit (const struct bp_location *bl,
11566 const address_space *aspace,
11567 CORE_ADDR bp_addr,
11568 const target_waitstatus &ws)
11569 {
11570 if (ws.kind () != TARGET_WAITKIND_STOPPED
11571 || ws.sig () != GDB_SIGNAL_TRAP)
11572 return 0;
11573
11574 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11575 aspace, bp_addr))
11576 return 0;
11577
11578 if (overlay_debugging /* unmapped overlay section */
11579 && section_is_overlay (bl->section)
11580 && !section_is_mapped (bl->section))
11581 return 0;
11582
11583 return 1;
11584 }
11585
11586 int
11587 dprintf_breakpoint::breakpoint_hit (const struct bp_location *bl,
11588 const address_space *aspace,
11589 CORE_ADDR bp_addr,
11590 const target_waitstatus &ws)
11591 {
11592 if (dprintf_style == dprintf_style_agent
11593 && target_can_run_breakpoint_commands ())
11594 {
11595 /* An agent-style dprintf never causes a stop. If we see a trap
11596 for this address it must be for a breakpoint that happens to
11597 be set at the same address. */
11598 return 0;
11599 }
11600
11601 return this->ordinary_breakpoint::breakpoint_hit (bl, aspace, bp_addr, ws);
11602 }
11603
11604 int
11605 ordinary_breakpoint::resources_needed (const struct bp_location *bl)
11606 {
11607 gdb_assert (type == bp_hardware_breakpoint);
11608
11609 return 1;
11610 }
11611
11612 enum print_stop_action
11613 ordinary_breakpoint::print_it (const bpstat *bs) const
11614 {
11615 const struct bp_location *bl;
11616 int bp_temp;
11617 struct ui_out *uiout = current_uiout;
11618
11619 bl = bs->bp_location_at.get ();
11620
11621 bp_temp = disposition == disp_del;
11622 if (bl->address != bl->requested_address)
11623 breakpoint_adjustment_warning (bl->requested_address,
11624 bl->address,
11625 number, 1);
11626 annotate_breakpoint (number);
11627 maybe_print_thread_hit_breakpoint (uiout);
11628
11629 if (uiout->is_mi_like_p ())
11630 {
11631 uiout->field_string ("reason",
11632 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11633 uiout->field_string ("disp", bpdisp_text (disposition));
11634 }
11635 if (bp_temp)
11636 uiout->message ("Temporary breakpoint %pF, ",
11637 signed_field ("bkptno", number));
11638 else
11639 uiout->message ("Breakpoint %pF, ",
11640 signed_field ("bkptno", number));
11641
11642 return PRINT_SRC_AND_LOC;
11643 }
11644
11645 void
11646 ordinary_breakpoint::print_mention () const
11647 {
11648 if (current_uiout->is_mi_like_p ())
11649 return;
11650
11651 switch (type)
11652 {
11653 case bp_breakpoint:
11654 case bp_gnu_ifunc_resolver:
11655 if (disposition == disp_del)
11656 gdb_printf (_("Temporary breakpoint"));
11657 else
11658 gdb_printf (_("Breakpoint"));
11659 gdb_printf (_(" %d"), number);
11660 if (type == bp_gnu_ifunc_resolver)
11661 gdb_printf (_(" at gnu-indirect-function resolver"));
11662 break;
11663 case bp_hardware_breakpoint:
11664 gdb_printf (_("Hardware assisted breakpoint %d"), number);
11665 break;
11666 case bp_dprintf:
11667 gdb_printf (_("Dprintf %d"), number);
11668 break;
11669 }
11670
11671 say_where (this);
11672 }
11673
11674 void
11675 ordinary_breakpoint::print_recreate (struct ui_file *fp) const
11676 {
11677 if (type == bp_breakpoint && disposition == disp_del)
11678 gdb_printf (fp, "tbreak");
11679 else if (type == bp_breakpoint)
11680 gdb_printf (fp, "break");
11681 else if (type == bp_hardware_breakpoint
11682 && disposition == disp_del)
11683 gdb_printf (fp, "thbreak");
11684 else if (type == bp_hardware_breakpoint)
11685 gdb_printf (fp, "hbreak");
11686 else
11687 internal_error (__FILE__, __LINE__,
11688 _("unhandled breakpoint type %d"), (int) type);
11689
11690 gdb_printf (fp, " %s", locspec->to_string ());
11691
11692 /* Print out extra_string if this breakpoint is pending. It might
11693 contain, for example, conditions that were set by the user. */
11694 if (loc == NULL && extra_string != NULL)
11695 gdb_printf (fp, " %s", extra_string.get ());
11696
11697 print_recreate_thread (fp);
11698 }
11699
11700 std::vector<symtab_and_line>
11701 code_breakpoint::decode_location_spec (location_spec *locspec,
11702 program_space *search_pspace)
11703 {
11704 if (locspec->type () == PROBE_LOCATION_SPEC)
11705 return bkpt_probe_decode_location_spec (this, locspec, search_pspace);
11706
11707 struct linespec_result canonical;
11708
11709 decode_line_full (locspec, DECODE_LINE_FUNFIRSTLINE, search_pspace,
11710 NULL, 0, &canonical, multiple_symbols_all,
11711 filter.get ());
11712
11713 /* We should get 0 or 1 resulting SALs. */
11714 gdb_assert (canonical.lsals.size () < 2);
11715
11716 if (!canonical.lsals.empty ())
11717 {
11718 const linespec_sals &lsal = canonical.lsals[0];
11719 return std::move (lsal.sals);
11720 }
11721 return {};
11722 }
11723
11724 /* Virtual table for internal breakpoints. */
11725
11726 void
11727 internal_breakpoint::re_set ()
11728 {
11729 switch (type)
11730 {
11731 /* Delete overlay event and longjmp master breakpoints; they
11732 will be reset later by breakpoint_re_set. */
11733 case bp_overlay_event:
11734 case bp_longjmp_master:
11735 case bp_std_terminate_master:
11736 case bp_exception_master:
11737 delete_breakpoint (this);
11738 break;
11739
11740 /* This breakpoint is special, it's set up when the inferior
11741 starts and we really don't want to touch it. */
11742 case bp_shlib_event:
11743
11744 /* Like bp_shlib_event, this breakpoint type is special. Once
11745 it is set up, we do not want to touch it. */
11746 case bp_thread_event:
11747 break;
11748 }
11749 }
11750
11751 void
11752 internal_breakpoint::check_status (bpstat *bs)
11753 {
11754 if (type == bp_shlib_event)
11755 {
11756 /* If requested, stop when the dynamic linker notifies GDB of
11757 events. This allows the user to get control and place
11758 breakpoints in initializer routines for dynamically loaded
11759 objects (among other things). */
11760 bs->stop = stop_on_solib_events;
11761 bs->print = stop_on_solib_events;
11762 }
11763 else
11764 bs->stop = 0;
11765 }
11766
11767 enum print_stop_action
11768 internal_breakpoint::print_it (const bpstat *bs) const
11769 {
11770 switch (type)
11771 {
11772 case bp_shlib_event:
11773 /* Did we stop because the user set the stop_on_solib_events
11774 variable? (If so, we report this as a generic, "Stopped due
11775 to shlib event" message.) */
11776 print_solib_event (false);
11777 break;
11778
11779 case bp_thread_event:
11780 /* Not sure how we will get here.
11781 GDB should not stop for these breakpoints. */
11782 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
11783 break;
11784
11785 case bp_overlay_event:
11786 /* By analogy with the thread event, GDB should not stop for these. */
11787 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
11788 break;
11789
11790 case bp_longjmp_master:
11791 /* These should never be enabled. */
11792 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
11793 break;
11794
11795 case bp_std_terminate_master:
11796 /* These should never be enabled. */
11797 gdb_printf (_("std::terminate Master Breakpoint: "
11798 "gdb should not stop!\n"));
11799 break;
11800
11801 case bp_exception_master:
11802 /* These should never be enabled. */
11803 gdb_printf (_("Exception Master Breakpoint: "
11804 "gdb should not stop!\n"));
11805 break;
11806 }
11807
11808 return PRINT_NOTHING;
11809 }
11810
11811 void
11812 internal_breakpoint::print_mention () const
11813 {
11814 /* Nothing to mention. These breakpoints are internal. */
11815 }
11816
11817 /* Virtual table for momentary breakpoints */
11818
11819 void
11820 momentary_breakpoint::re_set ()
11821 {
11822 /* Keep temporary breakpoints, which can be encountered when we step
11823 over a dlopen call and solib_add is resetting the breakpoints.
11824 Otherwise these should have been blown away via the cleanup chain
11825 or by breakpoint_init_inferior when we rerun the executable. */
11826 }
11827
11828 void
11829 momentary_breakpoint::check_status (bpstat *bs)
11830 {
11831 /* Nothing. The point of these breakpoints is causing a stop. */
11832 }
11833
11834 enum print_stop_action
11835 momentary_breakpoint::print_it (const bpstat *bs) const
11836 {
11837 return PRINT_UNKNOWN;
11838 }
11839
11840 void
11841 momentary_breakpoint::print_mention () const
11842 {
11843 /* Nothing to mention. These breakpoints are internal. */
11844 }
11845
11846 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
11847
11848 It gets cleared already on the removal of the first one of such placed
11849 breakpoints. This is OK as they get all removed altogether. */
11850
11851 longjmp_breakpoint::~longjmp_breakpoint ()
11852 {
11853 thread_info *tp = find_thread_global_id (this->thread);
11854
11855 if (tp != NULL)
11856 tp->initiating_frame = null_frame_id;
11857 }
11858
11859 static void
11860 bkpt_probe_create_sals_from_location_spec (location_spec *locspec,
11861 struct linespec_result *canonical)
11862
11863 {
11864 struct linespec_sals lsal;
11865
11866 lsal.sals = parse_probes (locspec, NULL, canonical);
11867 lsal.canonical = xstrdup (canonical->locspec->to_string ());
11868 canonical->lsals.push_back (std::move (lsal));
11869 }
11870
11871 static std::vector<symtab_and_line>
11872 bkpt_probe_decode_location_spec (struct breakpoint *b,
11873 location_spec *locspec,
11874 program_space *search_pspace)
11875 {
11876 std::vector<symtab_and_line> sals
11877 = parse_probes (locspec, search_pspace, NULL);
11878 if (sals.empty ())
11879 error (_("probe not found"));
11880 return sals;
11881 }
11882
11883 int
11884 tracepoint::breakpoint_hit (const struct bp_location *bl,
11885 const address_space *aspace, CORE_ADDR bp_addr,
11886 const target_waitstatus &ws)
11887 {
11888 /* By definition, the inferior does not report stops at
11889 tracepoints. */
11890 return 0;
11891 }
11892
11893 void
11894 tracepoint::print_one_detail (struct ui_out *uiout) const
11895 {
11896 if (!static_trace_marker_id.empty ())
11897 {
11898 gdb_assert (type == bp_static_tracepoint
11899 || type == bp_static_marker_tracepoint);
11900
11901 uiout->message ("\tmarker id is %pF\n",
11902 string_field ("static-tracepoint-marker-string-id",
11903 static_trace_marker_id.c_str ()));
11904 }
11905 }
11906
11907 void
11908 tracepoint::print_mention () const
11909 {
11910 if (current_uiout->is_mi_like_p ())
11911 return;
11912
11913 switch (type)
11914 {
11915 case bp_tracepoint:
11916 gdb_printf (_("Tracepoint"));
11917 gdb_printf (_(" %d"), number);
11918 break;
11919 case bp_fast_tracepoint:
11920 gdb_printf (_("Fast tracepoint"));
11921 gdb_printf (_(" %d"), number);
11922 break;
11923 case bp_static_tracepoint:
11924 case bp_static_marker_tracepoint:
11925 gdb_printf (_("Static tracepoint"));
11926 gdb_printf (_(" %d"), number);
11927 break;
11928 default:
11929 internal_error (__FILE__, __LINE__,
11930 _("unhandled tracepoint type %d"), (int) type);
11931 }
11932
11933 say_where (this);
11934 }
11935
11936 void
11937 tracepoint::print_recreate (struct ui_file *fp) const
11938 {
11939 if (type == bp_fast_tracepoint)
11940 gdb_printf (fp, "ftrace");
11941 else if (type == bp_static_tracepoint
11942 || type == bp_static_marker_tracepoint)
11943 gdb_printf (fp, "strace");
11944 else if (type == bp_tracepoint)
11945 gdb_printf (fp, "trace");
11946 else
11947 internal_error (__FILE__, __LINE__,
11948 _("unhandled tracepoint type %d"), (int) type);
11949
11950 gdb_printf (fp, " %s", locspec->to_string ());
11951 print_recreate_thread (fp);
11952
11953 if (pass_count)
11954 gdb_printf (fp, " passcount %d\n", pass_count);
11955 }
11956
11957 /* Virtual table for tracepoints on static probes. */
11958
11959 static void
11960 tracepoint_probe_create_sals_from_location_spec
11961 (location_spec *locspec,
11962 struct linespec_result *canonical)
11963 {
11964 /* We use the same method for breakpoint on probes. */
11965 bkpt_probe_create_sals_from_location_spec (locspec, canonical);
11966 }
11967
11968 void
11969 dprintf_breakpoint::re_set ()
11970 {
11971 re_set_default ();
11972
11973 /* extra_string should never be non-NULL for dprintf. */
11974 gdb_assert (extra_string != NULL);
11975
11976 /* 1 - connect to target 1, that can run breakpoint commands.
11977 2 - create a dprintf, which resolves fine.
11978 3 - disconnect from target 1
11979 4 - connect to target 2, that can NOT run breakpoint commands.
11980
11981 After steps #3/#4, you'll want the dprintf command list to
11982 be updated, because target 1 and 2 may well return different
11983 answers for target_can_run_breakpoint_commands().
11984 Given absence of finer grained resetting, we get to do
11985 it all the time. */
11986 if (extra_string != NULL)
11987 update_dprintf_command_list (this);
11988 }
11989
11990 /* Implement the "print_recreate" method for dprintf. */
11991
11992 void
11993 dprintf_breakpoint::print_recreate (struct ui_file *fp) const
11994 {
11995 gdb_printf (fp, "dprintf %s,%s", locspec->to_string (), extra_string.get ());
11996 print_recreate_thread (fp);
11997 }
11998
11999 /* Implement the "after_condition_true" method for dprintf.
12000
12001 dprintf's are implemented with regular commands in their command
12002 list, but we run the commands here instead of before presenting the
12003 stop to the user, as dprintf's don't actually cause a stop. This
12004 also makes it so that the commands of multiple dprintfs at the same
12005 address are all handled. */
12006
12007 void
12008 dprintf_breakpoint::after_condition_true (struct bpstat *bs)
12009 {
12010 /* dprintf's never cause a stop. This wasn't set in the
12011 check_status hook instead because that would make the dprintf's
12012 condition not be evaluated. */
12013 bs->stop = 0;
12014
12015 /* Run the command list here. Take ownership of it instead of
12016 copying. We never want these commands to run later in
12017 bpstat_do_actions, if a breakpoint that causes a stop happens to
12018 be set at same address as this dprintf, or even if running the
12019 commands here throws. */
12020 counted_command_line cmds = std::move (bs->commands);
12021 gdb_assert (cmds != nullptr);
12022 execute_control_commands (cmds.get (), 0);
12023 }
12024
12025 /* The breakpoint_ops structure to be used on static tracepoints with
12026 markers (`-m'). */
12027
12028 static void
12029 strace_marker_create_sals_from_location_spec (location_spec *locspec,
12030 struct linespec_result *canonical)
12031 {
12032 struct linespec_sals lsal;
12033 const char *arg_start, *arg;
12034
12035 arg = arg_start = as_linespec_location_spec (locspec)->spec_string;
12036 lsal.sals = decode_static_tracepoint_spec (&arg);
12037
12038 std::string str (arg_start, arg - arg_start);
12039 const char *ptr = str.c_str ();
12040 canonical->locspec
12041 = new_linespec_location_spec (&ptr, symbol_name_match_type::FULL);
12042
12043 lsal.canonical = xstrdup (canonical->locspec->to_string ());
12044 canonical->lsals.push_back (std::move (lsal));
12045 }
12046
12047 static void
12048 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12049 struct linespec_result *canonical,
12050 gdb::unique_xmalloc_ptr<char> cond_string,
12051 gdb::unique_xmalloc_ptr<char> extra_string,
12052 enum bptype type_wanted,
12053 enum bpdisp disposition,
12054 int thread,
12055 int task, int ignore_count,
12056 int from_tty, int enabled,
12057 int internal, unsigned flags)
12058 {
12059 const linespec_sals &lsal = canonical->lsals[0];
12060
12061 /* If the user is creating a static tracepoint by marker id
12062 (strace -m MARKER_ID), then store the sals index, so that
12063 breakpoint_re_set can try to match up which of the newly
12064 found markers corresponds to this one, and, don't try to
12065 expand multiple locations for each sal, given than SALS
12066 already should contain all sals for MARKER_ID. */
12067
12068 for (size_t i = 0; i < lsal.sals.size (); i++)
12069 {
12070 location_spec_up locspec = canonical->locspec->clone ();
12071
12072 std::unique_ptr<tracepoint> tp
12073 (new tracepoint (gdbarch,
12074 type_wanted,
12075 lsal.sals[i],
12076 std::move (locspec),
12077 NULL,
12078 std::move (cond_string),
12079 std::move (extra_string),
12080 disposition,
12081 thread, task, ignore_count,
12082 from_tty, enabled, flags,
12083 canonical->special_display));
12084
12085 /* Given that its possible to have multiple markers with
12086 the same string id, if the user is creating a static
12087 tracepoint by marker id ("strace -m MARKER_ID"), then
12088 store the sals index, so that breakpoint_re_set can
12089 try to match up which of the newly found markers
12090 corresponds to this one */
12091 tp->static_trace_marker_id_idx = i;
12092
12093 install_breakpoint (internal, std::move (tp), 0);
12094 }
12095 }
12096
12097 std::vector<symtab_and_line>
12098 static_marker_tracepoint::decode_location_spec (location_spec *locspec,
12099 program_space *search_pspace)
12100 {
12101 const char *s = as_linespec_location_spec (locspec)->spec_string;
12102
12103 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12104 if (sals.size () > static_trace_marker_id_idx)
12105 {
12106 sals[0] = sals[static_trace_marker_id_idx];
12107 sals.resize (1);
12108 return sals;
12109 }
12110 else
12111 error (_("marker %s not found"), static_trace_marker_id.c_str ());
12112 }
12113
12114 /* Static tracepoints with marker (`-m'). */
12115 static struct breakpoint_ops strace_marker_breakpoint_ops =
12116 {
12117 strace_marker_create_sals_from_location_spec,
12118 strace_marker_create_breakpoints_sal,
12119 };
12120
12121 static int
12122 strace_marker_p (struct breakpoint *b)
12123 {
12124 return b->type == bp_static_marker_tracepoint;
12125 }
12126
12127 /* Delete a breakpoint and clean up all traces of it in the data
12128 structures. */
12129
12130 void
12131 delete_breakpoint (struct breakpoint *bpt)
12132 {
12133 gdb_assert (bpt != NULL);
12134
12135 /* Has this bp already been deleted? This can happen because
12136 multiple lists can hold pointers to bp's. bpstat lists are
12137 especial culprits.
12138
12139 One example of this happening is a watchpoint's scope bp. When
12140 the scope bp triggers, we notice that the watchpoint is out of
12141 scope, and delete it. We also delete its scope bp. But the
12142 scope bp is marked "auto-deleting", and is already on a bpstat.
12143 That bpstat is then checked for auto-deleting bp's, which are
12144 deleted.
12145
12146 A real solution to this problem might involve reference counts in
12147 bp's, and/or giving them pointers back to their referencing
12148 bpstat's, and teaching delete_breakpoint to only free a bp's
12149 storage when no more references were extent. A cheaper bandaid
12150 was chosen. */
12151 if (bpt->type == bp_none)
12152 return;
12153
12154 /* At least avoid this stale reference until the reference counting
12155 of breakpoints gets resolved. */
12156 if (bpt->related_breakpoint != bpt)
12157 {
12158 struct breakpoint *related;
12159 struct watchpoint *w;
12160
12161 if (bpt->type == bp_watchpoint_scope)
12162 w = (struct watchpoint *) bpt->related_breakpoint;
12163 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12164 w = (struct watchpoint *) bpt;
12165 else
12166 w = NULL;
12167 if (w != NULL)
12168 watchpoint_del_at_next_stop (w);
12169
12170 /* Unlink bpt from the bpt->related_breakpoint ring. */
12171 for (related = bpt; related->related_breakpoint != bpt;
12172 related = related->related_breakpoint);
12173 related->related_breakpoint = bpt->related_breakpoint;
12174 bpt->related_breakpoint = bpt;
12175 }
12176
12177 /* watch_command_1 creates a watchpoint but only sets its number if
12178 update_watchpoint succeeds in creating its bp_locations. If there's
12179 a problem in that process, we'll be asked to delete the half-created
12180 watchpoint. In that case, don't announce the deletion. */
12181 if (bpt->number)
12182 gdb::observers::breakpoint_deleted.notify (bpt);
12183
12184 if (breakpoint_chain == bpt)
12185 breakpoint_chain = bpt->next;
12186
12187 for (breakpoint *b : all_breakpoints ())
12188 if (b->next == bpt)
12189 {
12190 b->next = bpt->next;
12191 break;
12192 }
12193
12194 /* Be sure no bpstat's are pointing at the breakpoint after it's
12195 been freed. */
12196 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12197 in all threads for now. Note that we cannot just remove bpstats
12198 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12199 commands are associated with the bpstat; if we remove it here,
12200 then the later call to bpstat_do_actions (&stop_bpstat); in
12201 event-top.c won't do anything, and temporary breakpoints with
12202 commands won't work. */
12203
12204 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12205
12206 /* Now that breakpoint is removed from breakpoint list, update the
12207 global location list. This will remove locations that used to
12208 belong to this breakpoint. Do this before freeing the breakpoint
12209 itself, since remove_breakpoint looks at location's owner. It
12210 might be better design to have location completely
12211 self-contained, but it's not the case now. */
12212 update_global_location_list (UGLL_DONT_INSERT);
12213
12214 /* On the chance that someone will soon try again to delete this
12215 same bp, we mark it as deleted before freeing its storage. */
12216 bpt->type = bp_none;
12217 delete bpt;
12218 }
12219
12220 /* Iterator function to call a user-provided callback function once
12221 for each of B and its related breakpoints. */
12222
12223 static void
12224 iterate_over_related_breakpoints (struct breakpoint *b,
12225 gdb::function_view<void (breakpoint *)> function)
12226 {
12227 struct breakpoint *related;
12228
12229 related = b;
12230 do
12231 {
12232 struct breakpoint *next;
12233
12234 /* FUNCTION may delete RELATED. */
12235 next = related->related_breakpoint;
12236
12237 if (next == related)
12238 {
12239 /* RELATED is the last ring entry. */
12240 function (related);
12241
12242 /* FUNCTION may have deleted it, so we'd never reach back to
12243 B. There's nothing left to do anyway, so just break
12244 out. */
12245 break;
12246 }
12247 else
12248 function (related);
12249
12250 related = next;
12251 }
12252 while (related != b);
12253 }
12254
12255 static void
12256 delete_command (const char *arg, int from_tty)
12257 {
12258 dont_repeat ();
12259
12260 if (arg == 0)
12261 {
12262 int breaks_to_delete = 0;
12263
12264 /* Delete all breakpoints if no argument. Do not delete
12265 internal breakpoints, these have to be deleted with an
12266 explicit breakpoint number argument. */
12267 for (breakpoint *b : all_breakpoints ())
12268 if (user_breakpoint_p (b))
12269 {
12270 breaks_to_delete = 1;
12271 break;
12272 }
12273
12274 /* Ask user only if there are some breakpoints to delete. */
12275 if (!from_tty
12276 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
12277 for (breakpoint *b : all_breakpoints_safe ())
12278 if (user_breakpoint_p (b))
12279 delete_breakpoint (b);
12280 }
12281 else
12282 map_breakpoint_numbers
12283 (arg, [&] (breakpoint *br)
12284 {
12285 iterate_over_related_breakpoints (br, delete_breakpoint);
12286 });
12287 }
12288
12289 /* Return true if all locations of B bound to PSPACE are pending. If
12290 PSPACE is NULL, all locations of all program spaces are
12291 considered. */
12292
12293 static int
12294 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
12295 {
12296 for (bp_location *loc : b->locations ())
12297 if ((pspace == NULL
12298 || loc->pspace == pspace)
12299 && !loc->shlib_disabled
12300 && !loc->pspace->executing_startup)
12301 return 0;
12302 return 1;
12303 }
12304
12305 /* Subroutine of update_breakpoint_locations to simplify it.
12306 Return non-zero if multiple fns in list LOC have the same name.
12307 Null names are ignored. */
12308
12309 static int
12310 ambiguous_names_p (struct bp_location *loc)
12311 {
12312 struct bp_location *l;
12313 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
12314 xcalloc, xfree));
12315
12316 for (l = loc; l != NULL; l = l->next)
12317 {
12318 const char **slot;
12319 const char *name = l->function_name.get ();
12320
12321 /* Allow for some names to be NULL, ignore them. */
12322 if (name == NULL)
12323 continue;
12324
12325 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
12326 INSERT);
12327 /* NOTE: We can assume slot != NULL here because xcalloc never
12328 returns NULL. */
12329 if (*slot != NULL)
12330 return 1;
12331 *slot = name;
12332 }
12333
12334 return 0;
12335 }
12336
12337 /* When symbols change, it probably means the sources changed as well,
12338 and it might mean the static tracepoint markers are no longer at
12339 the same address or line numbers they used to be at last we
12340 checked. Losing your static tracepoints whenever you rebuild is
12341 undesirable. This function tries to resync/rematch gdb static
12342 tracepoints with the markers on the target, for static tracepoints
12343 that have not been set by marker id. Static tracepoint that have
12344 been set by marker id are reset by marker id in breakpoint_re_set.
12345 The heuristic is:
12346
12347 1) For a tracepoint set at a specific address, look for a marker at
12348 the old PC. If one is found there, assume to be the same marker.
12349 If the name / string id of the marker found is different from the
12350 previous known name, assume that means the user renamed the marker
12351 in the sources, and output a warning.
12352
12353 2) For a tracepoint set at a given line number, look for a marker
12354 at the new address of the old line number. If one is found there,
12355 assume to be the same marker. If the name / string id of the
12356 marker found is different from the previous known name, assume that
12357 means the user renamed the marker in the sources, and output a
12358 warning.
12359
12360 3) If a marker is no longer found at the same address or line, it
12361 may mean the marker no longer exists. But it may also just mean
12362 the code changed a bit. Maybe the user added a few lines of code
12363 that made the marker move up or down (in line number terms). Ask
12364 the target for info about the marker with the string id as we knew
12365 it. If found, update line number and address in the matching
12366 static tracepoint. This will get confused if there's more than one
12367 marker with the same ID (possible in UST, although unadvised
12368 precisely because it confuses tools). */
12369
12370 static struct symtab_and_line
12371 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12372 {
12373 struct tracepoint *tp = (struct tracepoint *) b;
12374 struct static_tracepoint_marker marker;
12375 CORE_ADDR pc;
12376
12377 pc = sal.pc;
12378 if (sal.line)
12379 find_line_pc (sal.symtab, sal.line, &pc);
12380
12381 if (target_static_tracepoint_marker_at (pc, &marker))
12382 {
12383 if (tp->static_trace_marker_id != marker.str_id)
12384 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12385 b->number, tp->static_trace_marker_id.c_str (),
12386 marker.str_id.c_str ());
12387
12388 tp->static_trace_marker_id = std::move (marker.str_id);
12389
12390 return sal;
12391 }
12392
12393 /* Old marker wasn't found on target at lineno. Try looking it up
12394 by string ID. */
12395 if (!sal.explicit_pc
12396 && sal.line != 0
12397 && sal.symtab != NULL
12398 && !tp->static_trace_marker_id.empty ())
12399 {
12400 std::vector<static_tracepoint_marker> markers
12401 = target_static_tracepoint_markers_by_strid
12402 (tp->static_trace_marker_id.c_str ());
12403
12404 if (!markers.empty ())
12405 {
12406 struct symbol *sym;
12407 struct static_tracepoint_marker *tpmarker;
12408 struct ui_out *uiout = current_uiout;
12409
12410 tpmarker = &markers[0];
12411
12412 tp->static_trace_marker_id = std::move (tpmarker->str_id);
12413
12414 warning (_("marker for static tracepoint %d (%s) not "
12415 "found at previous line number"),
12416 b->number, tp->static_trace_marker_id.c_str ());
12417
12418 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
12419 sym = find_pc_sect_function (tpmarker->address, NULL);
12420 uiout->text ("Now in ");
12421 if (sym)
12422 {
12423 uiout->field_string ("func", sym->print_name (),
12424 function_name_style.style ());
12425 uiout->text (" at ");
12426 }
12427 uiout->field_string ("file",
12428 symtab_to_filename_for_display (sal2.symtab),
12429 file_name_style.style ());
12430 uiout->text (":");
12431
12432 if (uiout->is_mi_like_p ())
12433 {
12434 const char *fullname = symtab_to_fullname (sal2.symtab);
12435
12436 uiout->field_string ("fullname", fullname);
12437 }
12438
12439 uiout->field_signed ("line", sal2.line);
12440 uiout->text ("\n");
12441
12442 b->loc->line_number = sal2.line;
12443 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
12444
12445 std::unique_ptr<explicit_location_spec> els
12446 (new explicit_location_spec ());
12447 els->source_filename
12448 = xstrdup (symtab_to_filename_for_display (sal2.symtab));
12449 els->line_offset.offset = b->loc->line_number;
12450 els->line_offset.sign = LINE_OFFSET_NONE;
12451
12452 b->locspec = std::move (els);
12453
12454 /* Might be nice to check if function changed, and warn if
12455 so. */
12456 }
12457 }
12458 return sal;
12459 }
12460
12461 /* Returns 1 iff locations A and B are sufficiently same that
12462 we don't need to report breakpoint as changed. */
12463
12464 static int
12465 locations_are_equal (struct bp_location *a, struct bp_location *b)
12466 {
12467 while (a && b)
12468 {
12469 if (a->address != b->address)
12470 return 0;
12471
12472 if (a->shlib_disabled != b->shlib_disabled)
12473 return 0;
12474
12475 if (a->enabled != b->enabled)
12476 return 0;
12477
12478 if (a->disabled_by_cond != b->disabled_by_cond)
12479 return 0;
12480
12481 a = a->next;
12482 b = b->next;
12483 }
12484
12485 if ((a == NULL) != (b == NULL))
12486 return 0;
12487
12488 return 1;
12489 }
12490
12491 /* Split all locations of B that are bound to PSPACE out of B's
12492 location list to a separate list and return that list's head. If
12493 PSPACE is NULL, hoist out all locations of B. */
12494
12495 static struct bp_location *
12496 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12497 {
12498 struct bp_location head;
12499 struct bp_location *i = b->loc;
12500 struct bp_location **i_link = &b->loc;
12501 struct bp_location *hoisted = &head;
12502
12503 if (pspace == NULL)
12504 {
12505 i = b->loc;
12506 b->loc = NULL;
12507 return i;
12508 }
12509
12510 head.next = NULL;
12511
12512 while (i != NULL)
12513 {
12514 if (i->pspace == pspace)
12515 {
12516 *i_link = i->next;
12517 i->next = NULL;
12518 hoisted->next = i;
12519 hoisted = i;
12520 }
12521 else
12522 i_link = &i->next;
12523 i = *i_link;
12524 }
12525
12526 return head.next;
12527 }
12528
12529 /* Create new breakpoint locations for B (a hardware or software
12530 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12531 zero, then B is a ranged breakpoint. Only recreates locations for
12532 FILTER_PSPACE. Locations of other program spaces are left
12533 untouched. */
12534
12535 void
12536 update_breakpoint_locations (code_breakpoint *b,
12537 struct program_space *filter_pspace,
12538 gdb::array_view<const symtab_and_line> sals,
12539 gdb::array_view<const symtab_and_line> sals_end)
12540 {
12541 struct bp_location *existing_locations;
12542
12543 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
12544 {
12545 /* Ranged breakpoints have only one start location and one end
12546 location. */
12547 b->enable_state = bp_disabled;
12548 gdb_printf (gdb_stderr,
12549 _("Could not reset ranged breakpoint %d: "
12550 "multiple locations found\n"),
12551 b->number);
12552 return;
12553 }
12554
12555 /* If there's no new locations, and all existing locations are
12556 pending, don't do anything. This optimizes the common case where
12557 all locations are in the same shared library, that was unloaded.
12558 We'd like to retain the location, so that when the library is
12559 loaded again, we don't loose the enabled/disabled status of the
12560 individual locations. */
12561 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
12562 return;
12563
12564 existing_locations = hoist_existing_locations (b, filter_pspace);
12565
12566 for (const auto &sal : sals)
12567 {
12568 struct bp_location *new_loc;
12569
12570 switch_to_program_space_and_thread (sal.pspace);
12571
12572 new_loc = b->add_location (sal);
12573
12574 /* Reparse conditions, they might contain references to the
12575 old symtab. */
12576 if (b->cond_string != NULL)
12577 {
12578 const char *s;
12579
12580 s = b->cond_string.get ();
12581 try
12582 {
12583 new_loc->cond = parse_exp_1 (&s, sal.pc,
12584 block_for_pc (sal.pc),
12585 0);
12586 }
12587 catch (const gdb_exception_error &e)
12588 {
12589 new_loc->disabled_by_cond = true;
12590 }
12591 }
12592
12593 if (!sals_end.empty ())
12594 {
12595 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
12596
12597 new_loc->length = end - sals[0].pc + 1;
12598 }
12599 }
12600
12601 /* If possible, carry over 'disable' status from existing
12602 breakpoints. */
12603 {
12604 struct bp_location *e = existing_locations;
12605 /* If there are multiple breakpoints with the same function name,
12606 e.g. for inline functions, comparing function names won't work.
12607 Instead compare pc addresses; this is just a heuristic as things
12608 may have moved, but in practice it gives the correct answer
12609 often enough until a better solution is found. */
12610 int have_ambiguous_names = ambiguous_names_p (b->loc);
12611
12612 for (; e; e = e->next)
12613 {
12614 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
12615 {
12616 if (have_ambiguous_names)
12617 {
12618 for (bp_location *l : b->locations ())
12619 {
12620 /* Ignore software vs hardware location type at
12621 this point, because with "set breakpoint
12622 auto-hw", after a re-set, locations that were
12623 hardware can end up as software, or vice versa.
12624 As mentioned above, this is an heuristic and in
12625 practice should give the correct answer often
12626 enough. */
12627 if (breakpoint_locations_match (e, l, true))
12628 {
12629 l->enabled = e->enabled;
12630 l->disabled_by_cond = e->disabled_by_cond;
12631 break;
12632 }
12633 }
12634 }
12635 else
12636 {
12637 for (bp_location *l : b->locations ())
12638 if (l->function_name
12639 && strcmp (e->function_name.get (),
12640 l->function_name.get ()) == 0)
12641 {
12642 l->enabled = e->enabled;
12643 l->disabled_by_cond = e->disabled_by_cond;
12644 break;
12645 }
12646 }
12647 }
12648 }
12649 }
12650
12651 if (!locations_are_equal (existing_locations, b->loc))
12652 gdb::observers::breakpoint_modified.notify (b);
12653 }
12654
12655 /* Find the SaL locations corresponding to the given LOCSPEC.
12656 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12657
12658 std::vector<symtab_and_line>
12659 code_breakpoint::location_spec_to_sals (location_spec *locspec,
12660 struct program_space *search_pspace,
12661 int *found)
12662 {
12663 struct gdb_exception exception;
12664
12665 std::vector<symtab_and_line> sals;
12666
12667 try
12668 {
12669 sals = decode_location_spec (locspec, search_pspace);
12670 }
12671 catch (gdb_exception_error &e)
12672 {
12673 int not_found_and_ok = 0;
12674
12675 /* For pending breakpoints, it's expected that parsing will
12676 fail until the right shared library is loaded. User has
12677 already told to create pending breakpoints and don't need
12678 extra messages. If breakpoint is in bp_shlib_disabled
12679 state, then user already saw the message about that
12680 breakpoint being disabled, and don't want to see more
12681 errors. */
12682 if (e.error == NOT_FOUND_ERROR
12683 && (condition_not_parsed
12684 || (loc != NULL
12685 && search_pspace != NULL
12686 && loc->pspace != search_pspace)
12687 || (loc && loc->shlib_disabled)
12688 || (loc && loc->pspace->executing_startup)
12689 || enable_state == bp_disabled))
12690 not_found_and_ok = 1;
12691
12692 if (!not_found_and_ok)
12693 {
12694 /* We surely don't want to warn about the same breakpoint
12695 10 times. One solution, implemented here, is disable
12696 the breakpoint on error. Another solution would be to
12697 have separate 'warning emitted' flag. Since this
12698 happens only when a binary has changed, I don't know
12699 which approach is better. */
12700 enable_state = bp_disabled;
12701 throw;
12702 }
12703
12704 exception = std::move (e);
12705 }
12706
12707 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
12708 {
12709 for (auto &sal : sals)
12710 resolve_sal_pc (&sal);
12711 if (condition_not_parsed && extra_string != NULL)
12712 {
12713 gdb::unique_xmalloc_ptr<char> local_cond, local_extra;
12714 int local_thread, local_task;
12715
12716 find_condition_and_thread_for_sals (sals, extra_string.get (),
12717 &local_cond, &local_thread,
12718 &local_task, &local_extra);
12719 gdb_assert (cond_string == nullptr);
12720 if (local_cond != nullptr)
12721 cond_string = std::move (local_cond);
12722 thread = local_thread;
12723 task = local_task;
12724 if (local_extra != nullptr)
12725 extra_string = std::move (local_extra);
12726 condition_not_parsed = 0;
12727 }
12728
12729 if (type == bp_static_tracepoint)
12730 sals[0] = update_static_tracepoint (this, sals[0]);
12731
12732 *found = 1;
12733 }
12734 else
12735 *found = 0;
12736
12737 return sals;
12738 }
12739
12740 /* The default re_set method, for typical hardware or software
12741 breakpoints. Reevaluate the breakpoint and recreate its
12742 locations. */
12743
12744 void
12745 code_breakpoint::re_set_default ()
12746 {
12747 struct program_space *filter_pspace = current_program_space;
12748 std::vector<symtab_and_line> expanded, expanded_end;
12749
12750 int found;
12751 std::vector<symtab_and_line> sals = location_spec_to_sals (locspec.get (),
12752 filter_pspace,
12753 &found);
12754 if (found)
12755 expanded = std::move (sals);
12756
12757 if (locspec_range_end != nullptr)
12758 {
12759 std::vector<symtab_and_line> sals_end
12760 = location_spec_to_sals (locspec_range_end.get (),
12761 filter_pspace, &found);
12762 if (found)
12763 expanded_end = std::move (sals_end);
12764 }
12765
12766 update_breakpoint_locations (this, filter_pspace, expanded, expanded_end);
12767 }
12768
12769 /* Default method for creating SALs from an address string. It basically
12770 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
12771
12772 static void
12773 create_sals_from_location_spec_default (location_spec *locspec,
12774 struct linespec_result *canonical)
12775 {
12776 parse_breakpoint_sals (locspec, canonical);
12777 }
12778
12779 /* Reset a breakpoint. */
12780
12781 static void
12782 breakpoint_re_set_one (breakpoint *b)
12783 {
12784 input_radix = b->input_radix;
12785 set_language (b->language);
12786
12787 b->re_set ();
12788 }
12789
12790 /* Re-set breakpoint locations for the current program space.
12791 Locations bound to other program spaces are left untouched. */
12792
12793 void
12794 breakpoint_re_set (void)
12795 {
12796 {
12797 scoped_restore_current_language save_language;
12798 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
12799 scoped_restore_current_pspace_and_thread restore_pspace_thread;
12800
12801 /* breakpoint_re_set_one sets the current_language to the language
12802 of the breakpoint it is resetting (see prepare_re_set_context)
12803 before re-evaluating the breakpoint's location. This change can
12804 unfortunately get undone by accident if the language_mode is set
12805 to auto, and we either switch frames, or more likely in this context,
12806 we select the current frame.
12807
12808 We prevent this by temporarily turning the language_mode to
12809 language_mode_manual. We restore it once all breakpoints
12810 have been reset. */
12811 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
12812 language_mode = language_mode_manual;
12813
12814 /* Note: we must not try to insert locations until after all
12815 breakpoints have been re-set. Otherwise, e.g., when re-setting
12816 breakpoint 1, we'd insert the locations of breakpoint 2, which
12817 hadn't been re-set yet, and thus may have stale locations. */
12818
12819 for (breakpoint *b : all_breakpoints_safe ())
12820 {
12821 try
12822 {
12823 breakpoint_re_set_one (b);
12824 }
12825 catch (const gdb_exception &ex)
12826 {
12827 exception_fprintf (gdb_stderr, ex,
12828 "Error in re-setting breakpoint %d: ",
12829 b->number);
12830 }
12831 }
12832
12833 jit_breakpoint_re_set ();
12834 }
12835
12836 create_overlay_event_breakpoint ();
12837 create_longjmp_master_breakpoint ();
12838 create_std_terminate_master_breakpoint ();
12839 create_exception_master_breakpoint ();
12840
12841 /* Now we can insert. */
12842 update_global_location_list (UGLL_MAY_INSERT);
12843 }
12844 \f
12845 /* Reset the thread number of this breakpoint:
12846
12847 - If the breakpoint is for all threads, leave it as-is.
12848 - Else, reset it to the current thread for inferior_ptid. */
12849 void
12850 breakpoint_re_set_thread (struct breakpoint *b)
12851 {
12852 if (b->thread != -1)
12853 {
12854 b->thread = inferior_thread ()->global_num;
12855
12856 /* We're being called after following a fork. The new fork is
12857 selected as current, and unless this was a vfork will have a
12858 different program space from the original thread. Reset that
12859 as well. */
12860 b->loc->pspace = current_program_space;
12861 }
12862 }
12863
12864 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
12865 If from_tty is nonzero, it prints a message to that effect,
12866 which ends with a period (no newline). */
12867
12868 void
12869 set_ignore_count (int bptnum, int count, int from_tty)
12870 {
12871 if (count < 0)
12872 count = 0;
12873
12874 for (breakpoint *b : all_breakpoints ())
12875 if (b->number == bptnum)
12876 {
12877 if (is_tracepoint (b))
12878 {
12879 if (from_tty && count != 0)
12880 gdb_printf (_("Ignore count ignored for tracepoint %d."),
12881 bptnum);
12882 return;
12883 }
12884
12885 b->ignore_count = count;
12886 if (from_tty)
12887 {
12888 if (count == 0)
12889 gdb_printf (_("Will stop next time "
12890 "breakpoint %d is reached."),
12891 bptnum);
12892 else if (count == 1)
12893 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
12894 bptnum);
12895 else
12896 gdb_printf (_("Will ignore next %d "
12897 "crossings of breakpoint %d."),
12898 count, bptnum);
12899 }
12900 gdb::observers::breakpoint_modified.notify (b);
12901 return;
12902 }
12903
12904 error (_("No breakpoint number %d."), bptnum);
12905 }
12906
12907 /* Command to set ignore-count of breakpoint N to COUNT. */
12908
12909 static void
12910 ignore_command (const char *args, int from_tty)
12911 {
12912 const char *p = args;
12913 int num;
12914
12915 if (p == 0)
12916 error_no_arg (_("a breakpoint number"));
12917
12918 num = get_number (&p);
12919 if (num == 0)
12920 error (_("bad breakpoint number: '%s'"), args);
12921 if (*p == 0)
12922 error (_("Second argument (specified ignore-count) is missing."));
12923
12924 set_ignore_count (num,
12925 longest_to_int (value_as_long (parse_and_eval (p))),
12926 from_tty);
12927 if (from_tty)
12928 gdb_printf ("\n");
12929 }
12930 \f
12931
12932 /* Call FUNCTION on each of the breakpoints with numbers in the range
12933 defined by BP_NUM_RANGE (an inclusive range). */
12934
12935 static void
12936 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
12937 gdb::function_view<void (breakpoint *)> function)
12938 {
12939 if (bp_num_range.first == 0)
12940 {
12941 warning (_("bad breakpoint number at or near '%d'"),
12942 bp_num_range.first);
12943 }
12944 else
12945 {
12946 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
12947 {
12948 bool match = false;
12949
12950 for (breakpoint *b : all_breakpoints_safe ())
12951 if (b->number == i)
12952 {
12953 match = true;
12954 function (b);
12955 break;
12956 }
12957 if (!match)
12958 gdb_printf (_("No breakpoint number %d.\n"), i);
12959 }
12960 }
12961 }
12962
12963 /* Call FUNCTION on each of the breakpoints whose numbers are given in
12964 ARGS. */
12965
12966 static void
12967 map_breakpoint_numbers (const char *args,
12968 gdb::function_view<void (breakpoint *)> function)
12969 {
12970 if (args == NULL || *args == '\0')
12971 error_no_arg (_("one or more breakpoint numbers"));
12972
12973 number_or_range_parser parser (args);
12974
12975 while (!parser.finished ())
12976 {
12977 int num = parser.get_number ();
12978 map_breakpoint_number_range (std::make_pair (num, num), function);
12979 }
12980 }
12981
12982 /* Return the breakpoint location structure corresponding to the
12983 BP_NUM and LOC_NUM values. */
12984
12985 static struct bp_location *
12986 find_location_by_number (int bp_num, int loc_num)
12987 {
12988 breakpoint *b = get_breakpoint (bp_num);
12989
12990 if (!b || b->number != bp_num)
12991 error (_("Bad breakpoint number '%d'"), bp_num);
12992
12993 if (loc_num == 0)
12994 error (_("Bad breakpoint location number '%d'"), loc_num);
12995
12996 int n = 0;
12997 for (bp_location *loc : b->locations ())
12998 if (++n == loc_num)
12999 return loc;
13000
13001 error (_("Bad breakpoint location number '%d'"), loc_num);
13002 }
13003
13004 /* Modes of operation for extract_bp_num. */
13005 enum class extract_bp_kind
13006 {
13007 /* Extracting a breakpoint number. */
13008 bp,
13009
13010 /* Extracting a location number. */
13011 loc,
13012 };
13013
13014 /* Extract a breakpoint or location number (as determined by KIND)
13015 from the string starting at START. TRAILER is a character which
13016 can be found after the number. If you don't want a trailer, use
13017 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13018 string. This always returns a positive integer. */
13019
13020 static int
13021 extract_bp_num (extract_bp_kind kind, const char *start,
13022 int trailer, const char **end_out = NULL)
13023 {
13024 const char *end = start;
13025 int num = get_number_trailer (&end, trailer);
13026 if (num < 0)
13027 error (kind == extract_bp_kind::bp
13028 ? _("Negative breakpoint number '%.*s'")
13029 : _("Negative breakpoint location number '%.*s'"),
13030 int (end - start), start);
13031 if (num == 0)
13032 error (kind == extract_bp_kind::bp
13033 ? _("Bad breakpoint number '%.*s'")
13034 : _("Bad breakpoint location number '%.*s'"),
13035 int (end - start), start);
13036
13037 if (end_out != NULL)
13038 *end_out = end;
13039 return num;
13040 }
13041
13042 /* Extract a breakpoint or location range (as determined by KIND) in
13043 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13044 representing the (inclusive) range. The returned pair's elements
13045 are always positive integers. */
13046
13047 static std::pair<int, int>
13048 extract_bp_or_bp_range (extract_bp_kind kind,
13049 const std::string &arg,
13050 std::string::size_type arg_offset)
13051 {
13052 std::pair<int, int> range;
13053 const char *bp_loc = &arg[arg_offset];
13054 std::string::size_type dash = arg.find ('-', arg_offset);
13055 if (dash != std::string::npos)
13056 {
13057 /* bp_loc is a range (x-z). */
13058 if (arg.length () == dash + 1)
13059 error (kind == extract_bp_kind::bp
13060 ? _("Bad breakpoint number at or near: '%s'")
13061 : _("Bad breakpoint location number at or near: '%s'"),
13062 bp_loc);
13063
13064 const char *end;
13065 const char *start_first = bp_loc;
13066 const char *start_second = &arg[dash + 1];
13067 range.first = extract_bp_num (kind, start_first, '-');
13068 range.second = extract_bp_num (kind, start_second, '\0', &end);
13069
13070 if (range.first > range.second)
13071 error (kind == extract_bp_kind::bp
13072 ? _("Inverted breakpoint range at '%.*s'")
13073 : _("Inverted breakpoint location range at '%.*s'"),
13074 int (end - start_first), start_first);
13075 }
13076 else
13077 {
13078 /* bp_loc is a single value. */
13079 range.first = extract_bp_num (kind, bp_loc, '\0');
13080 range.second = range.first;
13081 }
13082 return range;
13083 }
13084
13085 /* Extract the breakpoint/location range specified by ARG. Returns
13086 the breakpoint range in BP_NUM_RANGE, and the location range in
13087 BP_LOC_RANGE.
13088
13089 ARG may be in any of the following forms:
13090
13091 x where 'x' is a breakpoint number.
13092 x-y where 'x' and 'y' specify a breakpoint numbers range.
13093 x.y where 'x' is a breakpoint number and 'y' a location number.
13094 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13095 location number range.
13096 */
13097
13098 static void
13099 extract_bp_number_and_location (const std::string &arg,
13100 std::pair<int, int> &bp_num_range,
13101 std::pair<int, int> &bp_loc_range)
13102 {
13103 std::string::size_type dot = arg.find ('.');
13104
13105 if (dot != std::string::npos)
13106 {
13107 /* Handle 'x.y' and 'x.y-z' cases. */
13108
13109 if (arg.length () == dot + 1 || dot == 0)
13110 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
13111
13112 bp_num_range.first
13113 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13114 bp_num_range.second = bp_num_range.first;
13115
13116 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13117 arg, dot + 1);
13118 }
13119 else
13120 {
13121 /* Handle x and x-y cases. */
13122
13123 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
13124 bp_loc_range.first = 0;
13125 bp_loc_range.second = 0;
13126 }
13127 }
13128
13129 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13130 specifies whether to enable or disable. */
13131
13132 static void
13133 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13134 {
13135 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13136 if (loc != NULL)
13137 {
13138 if (loc->disabled_by_cond && enable)
13139 error (_("Breakpoint %d's condition is invalid at location %d, "
13140 "cannot enable."), bp_num, loc_num);
13141
13142 if (loc->enabled != enable)
13143 {
13144 loc->enabled = enable;
13145 mark_breakpoint_location_modified (loc);
13146 }
13147 if (target_supports_enable_disable_tracepoint ()
13148 && current_trace_status ()->running && loc->owner
13149 && is_tracepoint (loc->owner))
13150 target_disable_tracepoint (loc);
13151 }
13152 update_global_location_list (UGLL_DONT_INSERT);
13153
13154 gdb::observers::breakpoint_modified.notify (loc->owner);
13155 }
13156
13157 /* Calculates LOC_NUM for LOC by traversing the bp_location chain of LOC's
13158 owner. 1-based indexing. -1 signals NOT FOUND. */
13159
13160 static int
13161 find_loc_num_by_location (const bp_location *loc)
13162 {
13163 if (loc != nullptr && loc->owner != nullptr)
13164 {
13165 /* Locations use 1-based indexing. */
13166 int loc_num = 1;
13167 for (bp_location *it : loc->owner->locations ())
13168 {
13169 if (it == loc)
13170 return loc_num;
13171 loc_num++;
13172 }
13173 }
13174 return -1;
13175 }
13176
13177 /* Enable or disable a breakpoint location LOC. ENABLE
13178 specifies whether to enable or disable. */
13179
13180 void
13181 enable_disable_bp_location (bp_location *loc, bool enable)
13182 {
13183 if (loc == nullptr)
13184 error (_("Breakpoint location is invalid."));
13185
13186 if (loc->owner == nullptr)
13187 error (_("Breakpoint location does not have an owner breakpoint."));
13188
13189 if (loc->disabled_by_cond && enable)
13190 {
13191 int loc_num = find_loc_num_by_location (loc);
13192 if (loc_num == -1)
13193 error (_("Breakpoint location LOC_NUM could not be found."));
13194 else
13195 error (_("Breakpoint %d's condition is invalid at location %d, "
13196 "cannot enable."), loc->owner->number, loc_num);
13197 }
13198
13199 if (loc->enabled != enable)
13200 {
13201 loc->enabled = enable;
13202 mark_breakpoint_location_modified (loc);
13203 }
13204
13205 if (target_supports_enable_disable_tracepoint ()
13206 && current_trace_status ()->running && loc->owner
13207 && is_tracepoint (loc->owner))
13208 target_disable_tracepoint (loc);
13209
13210 update_global_location_list (UGLL_DONT_INSERT);
13211 gdb::observers::breakpoint_modified.notify (loc->owner);
13212 }
13213
13214 /* Enable or disable a range of breakpoint locations. BP_NUM is the
13215 number of the breakpoint, and BP_LOC_RANGE specifies the
13216 (inclusive) range of location numbers of that breakpoint to
13217 enable/disable. ENABLE specifies whether to enable or disable the
13218 location. */
13219
13220 static void
13221 enable_disable_breakpoint_location_range (int bp_num,
13222 std::pair<int, int> &bp_loc_range,
13223 bool enable)
13224 {
13225 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13226 enable_disable_bp_num_loc (bp_num, i, enable);
13227 }
13228
13229 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13230 If from_tty is nonzero, it prints a message to that effect,
13231 which ends with a period (no newline). */
13232
13233 void
13234 disable_breakpoint (struct breakpoint *bpt)
13235 {
13236 /* Never disable a watchpoint scope breakpoint; we want to
13237 hit them when we leave scope so we can delete both the
13238 watchpoint and its scope breakpoint at that time. */
13239 if (bpt->type == bp_watchpoint_scope)
13240 return;
13241
13242 bpt->enable_state = bp_disabled;
13243
13244 /* Mark breakpoint locations modified. */
13245 mark_breakpoint_modified (bpt);
13246
13247 if (target_supports_enable_disable_tracepoint ()
13248 && current_trace_status ()->running && is_tracepoint (bpt))
13249 {
13250 for (bp_location *location : bpt->locations ())
13251 target_disable_tracepoint (location);
13252 }
13253
13254 update_global_location_list (UGLL_DONT_INSERT);
13255
13256 gdb::observers::breakpoint_modified.notify (bpt);
13257 }
13258
13259 /* Enable or disable the breakpoint(s) or breakpoint location(s)
13260 specified in ARGS. ARGS may be in any of the formats handled by
13261 extract_bp_number_and_location. ENABLE specifies whether to enable
13262 or disable the breakpoints/locations. */
13263
13264 static void
13265 enable_disable_command (const char *args, int from_tty, bool enable)
13266 {
13267 if (args == 0)
13268 {
13269 for (breakpoint *bpt : all_breakpoints ())
13270 if (user_breakpoint_p (bpt))
13271 {
13272 if (enable)
13273 enable_breakpoint (bpt);
13274 else
13275 disable_breakpoint (bpt);
13276 }
13277 }
13278 else
13279 {
13280 std::string num = extract_arg (&args);
13281
13282 while (!num.empty ())
13283 {
13284 std::pair<int, int> bp_num_range, bp_loc_range;
13285
13286 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13287
13288 if (bp_loc_range.first == bp_loc_range.second
13289 && bp_loc_range.first == 0)
13290 {
13291 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
13292 map_breakpoint_number_range (bp_num_range,
13293 enable
13294 ? enable_breakpoint
13295 : disable_breakpoint);
13296 }
13297 else
13298 {
13299 /* Handle breakpoint ids with formats 'x.y' or
13300 'x.y-z'. */
13301 enable_disable_breakpoint_location_range
13302 (bp_num_range.first, bp_loc_range, enable);
13303 }
13304 num = extract_arg (&args);
13305 }
13306 }
13307 }
13308
13309 /* The disable command disables the specified breakpoints/locations
13310 (or all defined breakpoints) so they're no longer effective in
13311 stopping the inferior. ARGS may be in any of the forms defined in
13312 extract_bp_number_and_location. */
13313
13314 static void
13315 disable_command (const char *args, int from_tty)
13316 {
13317 enable_disable_command (args, from_tty, false);
13318 }
13319
13320 static void
13321 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13322 int count)
13323 {
13324 int target_resources_ok;
13325
13326 if (bpt->type == bp_hardware_breakpoint)
13327 {
13328 int i;
13329 i = hw_breakpoint_used_count ();
13330 target_resources_ok =
13331 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13332 i + 1, 0);
13333 if (target_resources_ok == 0)
13334 error (_("No hardware breakpoint support in the target."));
13335 else if (target_resources_ok < 0)
13336 error (_("Hardware breakpoints used exceeds limit."));
13337 }
13338
13339 if (is_watchpoint (bpt))
13340 {
13341 /* Initialize it just to avoid a GCC false warning. */
13342 enum enable_state orig_enable_state = bp_disabled;
13343
13344 try
13345 {
13346 struct watchpoint *w = (struct watchpoint *) bpt;
13347
13348 orig_enable_state = bpt->enable_state;
13349 bpt->enable_state = bp_enabled;
13350 update_watchpoint (w, 1 /* reparse */);
13351 }
13352 catch (const gdb_exception &e)
13353 {
13354 bpt->enable_state = orig_enable_state;
13355 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13356 bpt->number);
13357 return;
13358 }
13359 }
13360
13361 bpt->enable_state = bp_enabled;
13362
13363 /* Mark breakpoint locations modified. */
13364 mark_breakpoint_modified (bpt);
13365
13366 if (target_supports_enable_disable_tracepoint ()
13367 && current_trace_status ()->running && is_tracepoint (bpt))
13368 {
13369 for (bp_location *location : bpt->locations ())
13370 target_enable_tracepoint (location);
13371 }
13372
13373 bpt->disposition = disposition;
13374 bpt->enable_count = count;
13375 update_global_location_list (UGLL_MAY_INSERT);
13376
13377 gdb::observers::breakpoint_modified.notify (bpt);
13378 }
13379
13380
13381 void
13382 enable_breakpoint (struct breakpoint *bpt)
13383 {
13384 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13385 }
13386
13387 /* The enable command enables the specified breakpoints/locations (or
13388 all defined breakpoints) so they once again become (or continue to
13389 be) effective in stopping the inferior. ARGS may be in any of the
13390 forms defined in extract_bp_number_and_location. */
13391
13392 static void
13393 enable_command (const char *args, int from_tty)
13394 {
13395 enable_disable_command (args, from_tty, true);
13396 }
13397
13398 static void
13399 enable_once_command (const char *args, int from_tty)
13400 {
13401 map_breakpoint_numbers
13402 (args, [&] (breakpoint *b)
13403 {
13404 iterate_over_related_breakpoints
13405 (b, [&] (breakpoint *bpt)
13406 {
13407 enable_breakpoint_disp (bpt, disp_disable, 1);
13408 });
13409 });
13410 }
13411
13412 static void
13413 enable_count_command (const char *args, int from_tty)
13414 {
13415 int count;
13416
13417 if (args == NULL)
13418 error_no_arg (_("hit count"));
13419
13420 count = get_number (&args);
13421
13422 map_breakpoint_numbers
13423 (args, [&] (breakpoint *b)
13424 {
13425 iterate_over_related_breakpoints
13426 (b, [&] (breakpoint *bpt)
13427 {
13428 enable_breakpoint_disp (bpt, disp_disable, count);
13429 });
13430 });
13431 }
13432
13433 static void
13434 enable_delete_command (const char *args, int from_tty)
13435 {
13436 map_breakpoint_numbers
13437 (args, [&] (breakpoint *b)
13438 {
13439 iterate_over_related_breakpoints
13440 (b, [&] (breakpoint *bpt)
13441 {
13442 enable_breakpoint_disp (bpt, disp_del, 1);
13443 });
13444 });
13445 }
13446 \f
13447 /* Invalidate last known value of any hardware watchpoint if
13448 the memory which that value represents has been written to by
13449 GDB itself. */
13450
13451 static void
13452 invalidate_bp_value_on_memory_change (struct inferior *inferior,
13453 CORE_ADDR addr, ssize_t len,
13454 const bfd_byte *data)
13455 {
13456 for (breakpoint *bp : all_breakpoints ())
13457 if (bp->enable_state == bp_enabled
13458 && bp->type == bp_hardware_watchpoint)
13459 {
13460 struct watchpoint *wp = (struct watchpoint *) bp;
13461
13462 if (wp->val_valid && wp->val != nullptr)
13463 {
13464 for (bp_location *loc : bp->locations ())
13465 if (loc->loc_type == bp_loc_hardware_watchpoint
13466 && loc->address + loc->length > addr
13467 && addr + len > loc->address)
13468 {
13469 wp->val = NULL;
13470 wp->val_valid = false;
13471 }
13472 }
13473 }
13474 }
13475
13476 /* Create and insert a breakpoint for software single step. */
13477
13478 void
13479 insert_single_step_breakpoint (struct gdbarch *gdbarch,
13480 const address_space *aspace,
13481 CORE_ADDR next_pc)
13482 {
13483 struct thread_info *tp = inferior_thread ();
13484 struct symtab_and_line sal;
13485 CORE_ADDR pc = next_pc;
13486
13487 if (tp->control.single_step_breakpoints == NULL)
13488 {
13489 std::unique_ptr<breakpoint> b
13490 (new momentary_breakpoint (gdbarch, bp_single_step,
13491 current_program_space,
13492 null_frame_id,
13493 tp->global_num));
13494
13495 tp->control.single_step_breakpoints
13496 = add_to_breakpoint_chain (std::move (b));
13497 }
13498
13499 sal = find_pc_line (pc, 0);
13500 sal.pc = pc;
13501 sal.section = find_pc_overlay (pc);
13502 sal.explicit_pc = 1;
13503
13504 auto *ss_bp
13505 = static_cast<momentary_breakpoint *> (tp->control.single_step_breakpoints);
13506 ss_bp->add_location (sal);
13507
13508 update_global_location_list (UGLL_INSERT);
13509 }
13510
13511 /* Insert single step breakpoints according to the current state. */
13512
13513 int
13514 insert_single_step_breakpoints (struct gdbarch *gdbarch)
13515 {
13516 struct regcache *regcache = get_current_regcache ();
13517 std::vector<CORE_ADDR> next_pcs;
13518
13519 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
13520
13521 if (!next_pcs.empty ())
13522 {
13523 frame_info_ptr frame = get_current_frame ();
13524 const address_space *aspace = get_frame_address_space (frame);
13525
13526 for (CORE_ADDR pc : next_pcs)
13527 insert_single_step_breakpoint (gdbarch, aspace, pc);
13528
13529 return 1;
13530 }
13531 else
13532 return 0;
13533 }
13534
13535 /* See breakpoint.h. */
13536
13537 int
13538 breakpoint_has_location_inserted_here (struct breakpoint *bp,
13539 const address_space *aspace,
13540 CORE_ADDR pc)
13541 {
13542 for (bp_location *loc : bp->locations ())
13543 if (loc->inserted
13544 && breakpoint_location_address_match (loc, aspace, pc))
13545 return 1;
13546
13547 return 0;
13548 }
13549
13550 /* Check whether a software single-step breakpoint is inserted at
13551 PC. */
13552
13553 int
13554 single_step_breakpoint_inserted_here_p (const address_space *aspace,
13555 CORE_ADDR pc)
13556 {
13557 for (breakpoint *bpt : all_breakpoints ())
13558 {
13559 if (bpt->type == bp_single_step
13560 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13561 return 1;
13562 }
13563 return 0;
13564 }
13565
13566 /* Tracepoint-specific operations. */
13567
13568 /* Set tracepoint count to NUM. */
13569 static void
13570 set_tracepoint_count (int num)
13571 {
13572 tracepoint_count = num;
13573 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
13574 }
13575
13576 static void
13577 trace_command (const char *arg, int from_tty)
13578 {
13579 location_spec_up locspec = string_to_location_spec (&arg,
13580 current_language);
13581 const struct breakpoint_ops *ops = breakpoint_ops_for_location_spec
13582 (locspec.get (), true /* is_tracepoint */);
13583
13584 create_breakpoint (get_current_arch (),
13585 locspec.get (),
13586 NULL, 0, arg, false, 1 /* parse arg */,
13587 0 /* tempflag */,
13588 bp_tracepoint /* type_wanted */,
13589 0 /* Ignore count */,
13590 pending_break_support,
13591 ops,
13592 from_tty,
13593 1 /* enabled */,
13594 0 /* internal */, 0);
13595 }
13596
13597 static void
13598 ftrace_command (const char *arg, int from_tty)
13599 {
13600 location_spec_up locspec = string_to_location_spec (&arg,
13601 current_language);
13602 create_breakpoint (get_current_arch (),
13603 locspec.get (),
13604 NULL, 0, arg, false, 1 /* parse arg */,
13605 0 /* tempflag */,
13606 bp_fast_tracepoint /* type_wanted */,
13607 0 /* Ignore count */,
13608 pending_break_support,
13609 &code_breakpoint_ops,
13610 from_tty,
13611 1 /* enabled */,
13612 0 /* internal */, 0);
13613 }
13614
13615 /* strace command implementation. Creates a static tracepoint. */
13616
13617 static void
13618 strace_command (const char *arg, int from_tty)
13619 {
13620 const struct breakpoint_ops *ops;
13621 location_spec_up locspec;
13622 enum bptype type;
13623
13624 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13625 or with a normal static tracepoint. */
13626 if (arg && startswith (arg, "-m") && isspace (arg[2]))
13627 {
13628 ops = &strace_marker_breakpoint_ops;
13629 locspec = new_linespec_location_spec (&arg,
13630 symbol_name_match_type::FULL);
13631 type = bp_static_marker_tracepoint;
13632 }
13633 else
13634 {
13635 ops = &code_breakpoint_ops;
13636 locspec = string_to_location_spec (&arg, current_language);
13637 type = bp_static_tracepoint;
13638 }
13639
13640 create_breakpoint (get_current_arch (),
13641 locspec.get (),
13642 NULL, 0, arg, false, 1 /* parse arg */,
13643 0 /* tempflag */,
13644 type /* type_wanted */,
13645 0 /* Ignore count */,
13646 pending_break_support,
13647 ops,
13648 from_tty,
13649 1 /* enabled */,
13650 0 /* internal */, 0);
13651 }
13652
13653 /* Set up a fake reader function that gets command lines from a linked
13654 list that was acquired during tracepoint uploading. */
13655
13656 static struct uploaded_tp *this_utp;
13657 static int next_cmd;
13658
13659 static char *
13660 read_uploaded_action (void)
13661 {
13662 char *rslt = nullptr;
13663
13664 if (next_cmd < this_utp->cmd_strings.size ())
13665 {
13666 rslt = this_utp->cmd_strings[next_cmd].get ();
13667 next_cmd++;
13668 }
13669
13670 return rslt;
13671 }
13672
13673 /* Given information about a tracepoint as recorded on a target (which
13674 can be either a live system or a trace file), attempt to create an
13675 equivalent GDB tracepoint. This is not a reliable process, since
13676 the target does not necessarily have all the information used when
13677 the tracepoint was originally defined. */
13678
13679 struct tracepoint *
13680 create_tracepoint_from_upload (struct uploaded_tp *utp)
13681 {
13682 const char *addr_str;
13683 char small_buf[100];
13684 struct tracepoint *tp;
13685
13686 if (utp->at_string)
13687 addr_str = utp->at_string.get ();
13688 else
13689 {
13690 /* In the absence of a source location, fall back to raw
13691 address. Since there is no way to confirm that the address
13692 means the same thing as when the trace was started, warn the
13693 user. */
13694 warning (_("Uploaded tracepoint %d has no "
13695 "source location, using raw address"),
13696 utp->number);
13697 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
13698 addr_str = small_buf;
13699 }
13700
13701 /* There's not much we can do with a sequence of bytecodes. */
13702 if (utp->cond && !utp->cond_string)
13703 warning (_("Uploaded tracepoint %d condition "
13704 "has no source form, ignoring it"),
13705 utp->number);
13706
13707 location_spec_up locspec = string_to_location_spec (&addr_str,
13708 current_language);
13709 if (!create_breakpoint (get_current_arch (),
13710 locspec.get (),
13711 utp->cond_string.get (), -1, addr_str,
13712 false /* force_condition */,
13713 0 /* parse cond/thread */,
13714 0 /* tempflag */,
13715 utp->type /* type_wanted */,
13716 0 /* Ignore count */,
13717 pending_break_support,
13718 &code_breakpoint_ops,
13719 0 /* from_tty */,
13720 utp->enabled /* enabled */,
13721 0 /* internal */,
13722 CREATE_BREAKPOINT_FLAGS_INSERTED))
13723 return NULL;
13724
13725 /* Get the tracepoint we just created. */
13726 tp = get_tracepoint (tracepoint_count);
13727 gdb_assert (tp != NULL);
13728
13729 if (utp->pass > 0)
13730 {
13731 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
13732 tp->number);
13733
13734 trace_pass_command (small_buf, 0);
13735 }
13736
13737 /* If we have uploaded versions of the original commands, set up a
13738 special-purpose "reader" function and call the usual command line
13739 reader, then pass the result to the breakpoint command-setting
13740 function. */
13741 if (!utp->cmd_strings.empty ())
13742 {
13743 counted_command_line cmd_list;
13744
13745 this_utp = utp;
13746 next_cmd = 0;
13747
13748 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
13749
13750 breakpoint_set_commands (tp, std::move (cmd_list));
13751 }
13752 else if (!utp->actions.empty ()
13753 || !utp->step_actions.empty ())
13754 warning (_("Uploaded tracepoint %d actions "
13755 "have no source form, ignoring them"),
13756 utp->number);
13757
13758 /* Copy any status information that might be available. */
13759 tp->hit_count = utp->hit_count;
13760 tp->traceframe_usage = utp->traceframe_usage;
13761
13762 return tp;
13763 }
13764
13765 /* Print information on tracepoint number TPNUM_EXP, or all if
13766 omitted. */
13767
13768 static void
13769 info_tracepoints_command (const char *args, int from_tty)
13770 {
13771 struct ui_out *uiout = current_uiout;
13772 int num_printed;
13773
13774 num_printed = breakpoint_1 (args, false, is_tracepoint);
13775
13776 if (num_printed == 0)
13777 {
13778 if (args == NULL || *args == '\0')
13779 uiout->message ("No tracepoints.\n");
13780 else
13781 uiout->message ("No tracepoint matching '%s'.\n", args);
13782 }
13783
13784 default_collect_info ();
13785 }
13786
13787 /* The 'enable trace' command enables tracepoints.
13788 Not supported by all targets. */
13789 static void
13790 enable_trace_command (const char *args, int from_tty)
13791 {
13792 enable_command (args, from_tty);
13793 }
13794
13795 /* The 'disable trace' command disables tracepoints.
13796 Not supported by all targets. */
13797 static void
13798 disable_trace_command (const char *args, int from_tty)
13799 {
13800 disable_command (args, from_tty);
13801 }
13802
13803 /* Remove a tracepoint (or all if no argument). */
13804 static void
13805 delete_trace_command (const char *arg, int from_tty)
13806 {
13807 dont_repeat ();
13808
13809 if (arg == 0)
13810 {
13811 int breaks_to_delete = 0;
13812
13813 /* Delete all breakpoints if no argument.
13814 Do not delete internal or call-dummy breakpoints, these
13815 have to be deleted with an explicit breakpoint number
13816 argument. */
13817 for (breakpoint *tp : all_tracepoints ())
13818 if (is_tracepoint (tp) && user_breakpoint_p (tp))
13819 {
13820 breaks_to_delete = 1;
13821 break;
13822 }
13823
13824 /* Ask user only if there are some breakpoints to delete. */
13825 if (!from_tty
13826 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
13827 {
13828 for (breakpoint *b : all_breakpoints_safe ())
13829 if (is_tracepoint (b) && user_breakpoint_p (b))
13830 delete_breakpoint (b);
13831 }
13832 }
13833 else
13834 map_breakpoint_numbers
13835 (arg, [&] (breakpoint *br)
13836 {
13837 iterate_over_related_breakpoints (br, delete_breakpoint);
13838 });
13839 }
13840
13841 /* Helper function for trace_pass_command. */
13842
13843 static void
13844 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
13845 {
13846 tp->pass_count = count;
13847 gdb::observers::breakpoint_modified.notify (tp);
13848 if (from_tty)
13849 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
13850 tp->number, count);
13851 }
13852
13853 /* Set passcount for tracepoint.
13854
13855 First command argument is passcount, second is tracepoint number.
13856 If tracepoint number omitted, apply to most recently defined.
13857 Also accepts special argument "all". */
13858
13859 static void
13860 trace_pass_command (const char *args, int from_tty)
13861 {
13862 struct tracepoint *t1;
13863 ULONGEST count;
13864
13865 if (args == 0 || *args == 0)
13866 error (_("passcount command requires an "
13867 "argument (count + optional TP num)"));
13868
13869 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
13870
13871 args = skip_spaces (args);
13872 if (*args && strncasecmp (args, "all", 3) == 0)
13873 {
13874 args += 3; /* Skip special argument "all". */
13875 if (*args)
13876 error (_("Junk at end of arguments."));
13877
13878 for (breakpoint *b : all_tracepoints ())
13879 {
13880 t1 = (struct tracepoint *) b;
13881 trace_pass_set_count (t1, count, from_tty);
13882 }
13883 }
13884 else if (*args == '\0')
13885 {
13886 t1 = get_tracepoint_by_number (&args, NULL);
13887 if (t1)
13888 trace_pass_set_count (t1, count, from_tty);
13889 }
13890 else
13891 {
13892 number_or_range_parser parser (args);
13893 while (!parser.finished ())
13894 {
13895 t1 = get_tracepoint_by_number (&args, &parser);
13896 if (t1)
13897 trace_pass_set_count (t1, count, from_tty);
13898 }
13899 }
13900 }
13901
13902 struct tracepoint *
13903 get_tracepoint (int num)
13904 {
13905 for (breakpoint *t : all_tracepoints ())
13906 if (t->number == num)
13907 return (struct tracepoint *) t;
13908
13909 return NULL;
13910 }
13911
13912 /* Find the tracepoint with the given target-side number (which may be
13913 different from the tracepoint number after disconnecting and
13914 reconnecting). */
13915
13916 struct tracepoint *
13917 get_tracepoint_by_number_on_target (int num)
13918 {
13919 for (breakpoint *b : all_tracepoints ())
13920 {
13921 struct tracepoint *t = (struct tracepoint *) b;
13922
13923 if (t->number_on_target == num)
13924 return t;
13925 }
13926
13927 return NULL;
13928 }
13929
13930 /* Utility: parse a tracepoint number and look it up in the list.
13931 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
13932 If the argument is missing, the most recent tracepoint
13933 (tracepoint_count) is returned. */
13934
13935 struct tracepoint *
13936 get_tracepoint_by_number (const char **arg,
13937 number_or_range_parser *parser)
13938 {
13939 int tpnum;
13940 const char *instring = arg == NULL ? NULL : *arg;
13941
13942 if (parser != NULL)
13943 {
13944 gdb_assert (!parser->finished ());
13945 tpnum = parser->get_number ();
13946 }
13947 else if (arg == NULL || *arg == NULL || ! **arg)
13948 tpnum = tracepoint_count;
13949 else
13950 tpnum = get_number (arg);
13951
13952 if (tpnum <= 0)
13953 {
13954 if (instring && *instring)
13955 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
13956 instring);
13957 else
13958 gdb_printf (_("No previous tracepoint\n"));
13959 return NULL;
13960 }
13961
13962 for (breakpoint *t : all_tracepoints ())
13963 if (t->number == tpnum)
13964 return (struct tracepoint *) t;
13965
13966 gdb_printf ("No tracepoint number %d.\n", tpnum);
13967 return NULL;
13968 }
13969
13970 void
13971 breakpoint::print_recreate_thread (struct ui_file *fp) const
13972 {
13973 if (thread != -1)
13974 gdb_printf (fp, " thread %d", thread);
13975
13976 if (task != 0)
13977 gdb_printf (fp, " task %d", task);
13978
13979 gdb_printf (fp, "\n");
13980 }
13981
13982 /* Save information on user settable breakpoints (watchpoints, etc) to
13983 a new script file named FILENAME. If FILTER is non-NULL, call it
13984 on each breakpoint and only include the ones for which it returns
13985 true. */
13986
13987 static void
13988 save_breakpoints (const char *filename, int from_tty,
13989 bool (*filter) (const struct breakpoint *))
13990 {
13991 int any = 0;
13992 int extra_trace_bits = 0;
13993
13994 if (filename == 0 || *filename == 0)
13995 error (_("Argument required (file name in which to save)"));
13996
13997 /* See if we have anything to save. */
13998 for (breakpoint *tp : all_breakpoints ())
13999 {
14000 /* Skip internal and momentary breakpoints. */
14001 if (!user_breakpoint_p (tp))
14002 continue;
14003
14004 /* If we have a filter, only save the breakpoints it accepts. */
14005 if (filter && !filter (tp))
14006 continue;
14007
14008 any = 1;
14009
14010 if (is_tracepoint (tp))
14011 {
14012 extra_trace_bits = 1;
14013
14014 /* We can stop searching. */
14015 break;
14016 }
14017 }
14018
14019 if (!any)
14020 {
14021 warning (_("Nothing to save."));
14022 return;
14023 }
14024
14025 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14026
14027 stdio_file fp;
14028
14029 if (!fp.open (expanded_filename.get (), "w"))
14030 error (_("Unable to open file '%s' for saving (%s)"),
14031 expanded_filename.get (), safe_strerror (errno));
14032
14033 if (extra_trace_bits)
14034 save_trace_state_variables (&fp);
14035
14036 for (breakpoint *tp : all_breakpoints ())
14037 {
14038 /* Skip internal and momentary breakpoints. */
14039 if (!user_breakpoint_p (tp))
14040 continue;
14041
14042 /* If we have a filter, only save the breakpoints it accepts. */
14043 if (filter && !filter (tp))
14044 continue;
14045
14046 tp->print_recreate (&fp);
14047
14048 /* Note, we can't rely on tp->number for anything, as we can't
14049 assume the recreated breakpoint numbers will match. Use $bpnum
14050 instead. */
14051
14052 if (tp->cond_string)
14053 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
14054
14055 if (tp->ignore_count)
14056 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
14057
14058 if (tp->type != bp_dprintf && tp->commands)
14059 {
14060 fp.puts (" commands\n");
14061
14062 ui_out_redirect_pop redir (current_uiout, &fp);
14063 print_command_lines (current_uiout, tp->commands.get (), 2);
14064
14065 fp.puts (" end\n");
14066 }
14067
14068 if (tp->enable_state == bp_disabled)
14069 fp.puts ("disable $bpnum\n");
14070
14071 /* If this is a multi-location breakpoint, check if the locations
14072 should be individually disabled. Watchpoint locations are
14073 special, and not user visible. */
14074 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14075 {
14076 int n = 1;
14077
14078 for (bp_location *loc : tp->locations ())
14079 {
14080 if (!loc->enabled)
14081 fp.printf ("disable $bpnum.%d\n", n);
14082
14083 n++;
14084 }
14085 }
14086 }
14087
14088 if (extra_trace_bits && !default_collect.empty ())
14089 fp.printf ("set default-collect %s\n", default_collect.c_str ());
14090
14091 if (from_tty)
14092 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
14093 }
14094
14095 /* The `save breakpoints' command. */
14096
14097 static void
14098 save_breakpoints_command (const char *args, int from_tty)
14099 {
14100 save_breakpoints (args, from_tty, NULL);
14101 }
14102
14103 /* The `save tracepoints' command. */
14104
14105 static void
14106 save_tracepoints_command (const char *args, int from_tty)
14107 {
14108 save_breakpoints (args, from_tty, is_tracepoint);
14109 }
14110
14111 \f
14112 /* This help string is used to consolidate all the help string for specifying
14113 locations used by several commands. */
14114
14115 #define LOCATION_SPEC_HELP_STRING \
14116 "Linespecs are colon-separated lists of location parameters, such as\n\
14117 source filename, function name, label name, and line number.\n\
14118 Example: To specify the start of a label named \"the_top\" in the\n\
14119 function \"fact\" in the file \"factorial.c\", use\n\
14120 \"factorial.c:fact:the_top\".\n\
14121 \n\
14122 Address locations begin with \"*\" and specify an exact address in the\n\
14123 program. Example: To specify the fourth byte past the start function\n\
14124 \"main\", use \"*main + 4\".\n\
14125 \n\
14126 Explicit locations are similar to linespecs but use an option/argument\n\
14127 syntax to specify location parameters.\n\
14128 Example: To specify the start of the label named \"the_top\" in the\n\
14129 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
14130 -function fact -label the_top\".\n\
14131 \n\
14132 By default, a specified function is matched against the program's\n\
14133 functions in all scopes. For C++, this means in all namespaces and\n\
14134 classes. For Ada, this means in all packages. E.g., in C++,\n\
14135 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14136 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
14137 specified name as a complete fully-qualified name instead."
14138
14139 /* This help string is used for the break, hbreak, tbreak and thbreak
14140 commands. It is defined as a macro to prevent duplication.
14141 COMMAND should be a string constant containing the name of the
14142 command. */
14143
14144 #define BREAK_ARGS_HELP(command) \
14145 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14146 \t[-force-condition] [if CONDITION]\n\
14147 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14148 probe point. Accepted values are `-probe' (for a generic, automatically\n\
14149 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14150 `-probe-dtrace' (for a DTrace probe).\n\
14151 LOCATION may be a linespec, address, or explicit location as described\n\
14152 below.\n\
14153 \n\
14154 With no LOCATION, uses current execution address of the selected\n\
14155 stack frame. This is useful for breaking on return to a stack frame.\n\
14156 \n\
14157 THREADNUM is the number from \"info threads\".\n\
14158 CONDITION is a boolean expression.\n\
14159 \n\
14160 With the \"-force-condition\" flag, the condition is defined even when\n\
14161 it is invalid for all current locations.\n\
14162 \n" LOCATION_SPEC_HELP_STRING "\n\n\
14163 Multiple breakpoints at one place are permitted, and useful if their\n\
14164 conditions are different.\n\
14165 \n\
14166 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14167
14168 /* List of subcommands for "catch". */
14169 static struct cmd_list_element *catch_cmdlist;
14170
14171 /* List of subcommands for "tcatch". */
14172 static struct cmd_list_element *tcatch_cmdlist;
14173
14174 void
14175 add_catch_command (const char *name, const char *docstring,
14176 cmd_func_ftype *func,
14177 completer_ftype *completer,
14178 void *user_data_catch,
14179 void *user_data_tcatch)
14180 {
14181 struct cmd_list_element *command;
14182
14183 command = add_cmd (name, class_breakpoint, docstring,
14184 &catch_cmdlist);
14185 command->func = func;
14186 command->set_context (user_data_catch);
14187 set_cmd_completer (command, completer);
14188
14189 command = add_cmd (name, class_breakpoint, docstring,
14190 &tcatch_cmdlist);
14191 command->func = func;
14192 command->set_context (user_data_tcatch);
14193 set_cmd_completer (command, completer);
14194 }
14195
14196 /* Zero if any of the breakpoint's locations could be a location where
14197 functions have been inlined, nonzero otherwise. */
14198
14199 static int
14200 is_non_inline_function (struct breakpoint *b)
14201 {
14202 /* The shared library event breakpoint is set on the address of a
14203 non-inline function. */
14204 if (b->type == bp_shlib_event)
14205 return 1;
14206
14207 return 0;
14208 }
14209
14210 /* Nonzero if the specified PC cannot be a location where functions
14211 have been inlined. */
14212
14213 int
14214 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
14215 const target_waitstatus &ws)
14216 {
14217 for (breakpoint *b : all_breakpoints ())
14218 {
14219 if (!is_non_inline_function (b))
14220 continue;
14221
14222 for (bp_location *bl : b->locations ())
14223 {
14224 if (!bl->shlib_disabled
14225 && bpstat_check_location (bl, aspace, pc, ws))
14226 return 1;
14227 }
14228 }
14229
14230 return 0;
14231 }
14232
14233 /* Remove any references to OBJFILE which is going to be freed. */
14234
14235 void
14236 breakpoint_free_objfile (struct objfile *objfile)
14237 {
14238 for (bp_location *loc : all_bp_locations ())
14239 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
14240 loc->symtab = NULL;
14241 }
14242
14243 /* Chain containing all defined "enable breakpoint" subcommands. */
14244
14245 static struct cmd_list_element *enablebreaklist = NULL;
14246
14247 /* See breakpoint.h. */
14248
14249 cmd_list_element *commands_cmd_element = nullptr;
14250
14251 void _initialize_breakpoint ();
14252 void
14253 _initialize_breakpoint ()
14254 {
14255 struct cmd_list_element *c;
14256
14257 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14258 "breakpoint");
14259 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14260 "breakpoint");
14261 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14262 "breakpoint");
14263
14264 breakpoint_chain = 0;
14265 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14266 before a breakpoint is set. */
14267 breakpoint_count = 0;
14268
14269 tracepoint_count = 0;
14270
14271 add_com ("ignore", class_breakpoint, ignore_command, _("\
14272 Set ignore-count of breakpoint number N to COUNT.\n\
14273 Usage is `ignore N COUNT'."));
14274
14275 commands_cmd_element = add_com ("commands", class_breakpoint,
14276 commands_command, _("\
14277 Set commands to be executed when the given breakpoints are hit.\n\
14278 Give a space-separated breakpoint list as argument after \"commands\".\n\
14279 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14280 (e.g. `5-7').\n\
14281 With no argument, the targeted breakpoint is the last one set.\n\
14282 The commands themselves follow starting on the next line.\n\
14283 Type a line containing \"end\" to indicate the end of them.\n\
14284 Give \"silent\" as the first line to make the breakpoint silent;\n\
14285 then no output is printed when it is hit, except what the commands print."));
14286
14287 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14288 static std::string condition_command_help
14289 = gdb::option::build_help (_("\
14290 Specify breakpoint number N to break only if COND is true.\n\
14291 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
14292 is an expression to be evaluated whenever breakpoint N is reached.\n\
14293 \n\
14294 Options:\n\
14295 %OPTIONS%"), cc_opts);
14296
14297 c = add_com ("condition", class_breakpoint, condition_command,
14298 condition_command_help.c_str ());
14299 set_cmd_completer_handle_brkchars (c, condition_completer);
14300
14301 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
14302 Set a temporary breakpoint.\n\
14303 Like \"break\" except the breakpoint is only temporary,\n\
14304 so it will be deleted when hit. Equivalent to \"break\" followed\n\
14305 by using \"enable delete\" on the breakpoint number.\n\
14306 \n"
14307 BREAK_ARGS_HELP ("tbreak")));
14308 set_cmd_completer (c, location_completer);
14309
14310 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
14311 Set a hardware assisted breakpoint.\n\
14312 Like \"break\" except the breakpoint requires hardware support,\n\
14313 some target hardware may not have this support.\n\
14314 \n"
14315 BREAK_ARGS_HELP ("hbreak")));
14316 set_cmd_completer (c, location_completer);
14317
14318 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
14319 Set a temporary hardware assisted breakpoint.\n\
14320 Like \"hbreak\" except the breakpoint is only temporary,\n\
14321 so it will be deleted when hit.\n\
14322 \n"
14323 BREAK_ARGS_HELP ("thbreak")));
14324 set_cmd_completer (c, location_completer);
14325
14326 cmd_list_element *enable_cmd
14327 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14328 Enable all or some breakpoints.\n\
14329 Usage: enable [BREAKPOINTNUM]...\n\
14330 Give breakpoint numbers (separated by spaces) as arguments.\n\
14331 With no subcommand, breakpoints are enabled until you command otherwise.\n\
14332 This is used to cancel the effect of the \"disable\" command.\n\
14333 With a subcommand you can enable temporarily."),
14334 &enablelist, 1, &cmdlist);
14335
14336 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
14337
14338 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
14339 Enable all or some breakpoints.\n\
14340 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
14341 Give breakpoint numbers (separated by spaces) as arguments.\n\
14342 This is used to cancel the effect of the \"disable\" command.\n\
14343 May be abbreviated to simply \"enable\"."),
14344 &enablebreaklist, 1, &enablelist);
14345
14346 add_cmd ("once", no_class, enable_once_command, _("\
14347 Enable some breakpoints for one hit.\n\
14348 Usage: enable breakpoints once BREAKPOINTNUM...\n\
14349 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14350 &enablebreaklist);
14351
14352 add_cmd ("delete", no_class, enable_delete_command, _("\
14353 Enable some breakpoints and delete when hit.\n\
14354 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
14355 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14356 &enablebreaklist);
14357
14358 add_cmd ("count", no_class, enable_count_command, _("\
14359 Enable some breakpoints for COUNT hits.\n\
14360 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
14361 If a breakpoint is hit while enabled in this fashion,\n\
14362 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14363 &enablebreaklist);
14364
14365 add_cmd ("delete", no_class, enable_delete_command, _("\
14366 Enable some breakpoints and delete when hit.\n\
14367 Usage: enable delete BREAKPOINTNUM...\n\
14368 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14369 &enablelist);
14370
14371 add_cmd ("once", no_class, enable_once_command, _("\
14372 Enable some breakpoints for one hit.\n\
14373 Usage: enable once BREAKPOINTNUM...\n\
14374 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14375 &enablelist);
14376
14377 add_cmd ("count", no_class, enable_count_command, _("\
14378 Enable some breakpoints for COUNT hits.\n\
14379 Usage: enable count COUNT BREAKPOINTNUM...\n\
14380 If a breakpoint is hit while enabled in this fashion,\n\
14381 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14382 &enablelist);
14383
14384 cmd_list_element *disable_cmd
14385 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14386 Disable all or some breakpoints.\n\
14387 Usage: disable [BREAKPOINTNUM]...\n\
14388 Arguments are breakpoint numbers with spaces in between.\n\
14389 To disable all breakpoints, give no argument.\n\
14390 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
14391 &disablelist, 1, &cmdlist);
14392 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14393 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
14394
14395 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
14396 Disable all or some breakpoints.\n\
14397 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
14398 Arguments are breakpoint numbers with spaces in between.\n\
14399 To disable all breakpoints, give no argument.\n\
14400 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
14401 This command may be abbreviated \"disable\"."),
14402 &disablelist);
14403
14404 cmd_list_element *delete_cmd
14405 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14406 Delete all or some breakpoints.\n\
14407 Usage: delete [BREAKPOINTNUM]...\n\
14408 Arguments are breakpoint numbers with spaces in between.\n\
14409 To delete all breakpoints, give no argument.\n\
14410 \n\
14411 Also a prefix command for deletion of other GDB objects."),
14412 &deletelist, 1, &cmdlist);
14413 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14414 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
14415
14416 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
14417 Delete all or some breakpoints or auto-display expressions.\n\
14418 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
14419 Arguments are breakpoint numbers with spaces in between.\n\
14420 To delete all breakpoints, give no argument.\n\
14421 This command may be abbreviated \"delete\"."),
14422 &deletelist);
14423
14424 cmd_list_element *clear_cmd
14425 = add_com ("clear", class_breakpoint, clear_command, _("\
14426 Clear breakpoint at specified location.\n\
14427 Argument may be a linespec, explicit, or address location as described below.\n\
14428 \n\
14429 With no argument, clears all breakpoints in the line that the selected frame\n\
14430 is executing in.\n"
14431 "\n" LOCATION_SPEC_HELP_STRING "\n\n\
14432 See also the \"delete\" command which clears breakpoints by number."));
14433 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
14434
14435 cmd_list_element *break_cmd
14436 = add_com ("break", class_breakpoint, break_command, _("\
14437 Set breakpoint at specified location.\n"
14438 BREAK_ARGS_HELP ("break")));
14439 set_cmd_completer (break_cmd, location_completer);
14440
14441 add_com_alias ("b", break_cmd, class_run, 1);
14442 add_com_alias ("br", break_cmd, class_run, 1);
14443 add_com_alias ("bre", break_cmd, class_run, 1);
14444 add_com_alias ("brea", break_cmd, class_run, 1);
14445
14446 cmd_list_element *info_breakpoints_cmd
14447 = add_info ("breakpoints", info_breakpoints_command, _("\
14448 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
14449 The \"Type\" column indicates one of:\n\
14450 \tbreakpoint - normal breakpoint\n\
14451 \twatchpoint - watchpoint\n\
14452 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14453 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14454 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14455 address and file/line number respectively.\n\
14456 \n\
14457 Convenience variable \"$_\" and default examine address for \"x\"\n\
14458 are set to the address of the last breakpoint listed unless the command\n\
14459 is prefixed with \"server \".\n\n\
14460 Convenience variable \"$bpnum\" contains the number of the last\n\
14461 breakpoint set."));
14462
14463 add_info_alias ("b", info_breakpoints_cmd, 1);
14464
14465 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14466 Status of all breakpoints, or breakpoint number NUMBER.\n\
14467 The \"Type\" column indicates one of:\n\
14468 \tbreakpoint - normal breakpoint\n\
14469 \twatchpoint - watchpoint\n\
14470 \tlongjmp - internal breakpoint used to step through longjmp()\n\
14471 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14472 \tuntil - internal breakpoint used by the \"until\" command\n\
14473 \tfinish - internal breakpoint used by the \"finish\" command\n\
14474 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14475 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14476 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14477 address and file/line number respectively.\n\
14478 \n\
14479 Convenience variable \"$_\" and default examine address for \"x\"\n\
14480 are set to the address of the last breakpoint listed unless the command\n\
14481 is prefixed with \"server \".\n\n\
14482 Convenience variable \"$bpnum\" contains the number of the last\n\
14483 breakpoint set."),
14484 &maintenanceinfolist);
14485
14486 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
14487 Set catchpoints to catch events."),
14488 &catch_cmdlist,
14489 0/*allow-unknown*/, &cmdlist);
14490
14491 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
14492 Set temporary catchpoints to catch events."),
14493 &tcatch_cmdlist,
14494 0/*allow-unknown*/, &cmdlist);
14495
14496 const auto opts = make_watch_options_def_group (nullptr);
14497
14498 static const std::string watch_help = gdb::option::build_help (_("\
14499 Set a watchpoint for EXPRESSION.\n\
14500 Usage: watch [-location] EXPRESSION\n\
14501 \n\
14502 Options:\n\
14503 %OPTIONS%\n\
14504 \n\
14505 A watchpoint stops execution of your program whenever the value of\n\
14506 an expression changes."), opts);
14507 c = add_com ("watch", class_breakpoint, watch_command,
14508 watch_help.c_str ());
14509 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14510
14511 static const std::string rwatch_help = gdb::option::build_help (_("\
14512 Set a read watchpoint for EXPRESSION.\n\
14513 Usage: rwatch [-location] EXPRESSION\n\
14514 \n\
14515 Options:\n\
14516 %OPTIONS%\n\
14517 \n\
14518 A read watchpoint stops execution of your program whenever the value of\n\
14519 an expression is read."), opts);
14520 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14521 rwatch_help.c_str ());
14522 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14523
14524 static const std::string awatch_help = gdb::option::build_help (_("\
14525 Set an access watchpoint for EXPRESSION.\n\
14526 Usage: awatch [-location] EXPRESSION\n\
14527 \n\
14528 Options:\n\
14529 %OPTIONS%\n\
14530 \n\
14531 An access watchpoint stops execution of your program whenever the value\n\
14532 of an expression is either read or written."), opts);
14533 c = add_com ("awatch", class_breakpoint, awatch_command,
14534 awatch_help.c_str ());
14535 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14536
14537 add_info ("watchpoints", info_watchpoints_command, _("\
14538 Status of specified watchpoints (all watchpoints if no argument)."));
14539
14540 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14541 respond to changes - contrary to the description. */
14542 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14543 &can_use_hw_watchpoints, _("\
14544 Set debugger's willingness to use watchpoint hardware."), _("\
14545 Show debugger's willingness to use watchpoint hardware."), _("\
14546 If zero, gdb will not use hardware for new watchpoints, even if\n\
14547 such is available. (However, any hardware watchpoints that were\n\
14548 created before setting this to nonzero, will continue to use watchpoint\n\
14549 hardware.)"),
14550 NULL,
14551 show_can_use_hw_watchpoints,
14552 &setlist, &showlist);
14553
14554 can_use_hw_watchpoints = 1;
14555
14556 /* Tracepoint manipulation commands. */
14557
14558 cmd_list_element *trace_cmd
14559 = add_com ("trace", class_breakpoint, trace_command, _("\
14560 Set a tracepoint at specified location.\n\
14561 \n"
14562 BREAK_ARGS_HELP ("trace") "\n\
14563 Do \"help tracepoints\" for info on other tracepoint commands."));
14564 set_cmd_completer (trace_cmd, location_completer);
14565
14566 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14567 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14568 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14569 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
14570
14571 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
14572 Set a fast tracepoint at specified location.\n\
14573 \n"
14574 BREAK_ARGS_HELP ("ftrace") "\n\
14575 Do \"help tracepoints\" for info on other tracepoint commands."));
14576 set_cmd_completer (c, location_completer);
14577
14578 c = add_com ("strace", class_breakpoint, strace_command, _("\
14579 Set a static tracepoint at location or marker.\n\
14580 \n\
14581 strace [LOCATION] [if CONDITION]\n\
14582 LOCATION may be a linespec, explicit, or address location (described below) \n\
14583 or -m MARKER_ID.\n\n\
14584 If a marker id is specified, probe the marker with that name. With\n\
14585 no LOCATION, uses current execution address of the selected stack frame.\n\
14586 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14587 This collects arbitrary user data passed in the probe point call to the\n\
14588 tracing library. You can inspect it when analyzing the trace buffer,\n\
14589 by printing the $_sdata variable like any other convenience variable.\n\
14590 \n\
14591 CONDITION is a boolean expression.\n\
14592 \n" LOCATION_SPEC_HELP_STRING "\n\n\
14593 Multiple tracepoints at one place are permitted, and useful if their\n\
14594 conditions are different.\n\
14595 \n\
14596 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14597 Do \"help tracepoints\" for info on other tracepoint commands."));
14598 set_cmd_completer (c, location_completer);
14599
14600 cmd_list_element *info_tracepoints_cmd
14601 = add_info ("tracepoints", info_tracepoints_command, _("\
14602 Status of specified tracepoints (all tracepoints if no argument).\n\
14603 Convenience variable \"$tpnum\" contains the number of the\n\
14604 last tracepoint set."));
14605
14606 add_info_alias ("tp", info_tracepoints_cmd, 1);
14607
14608 cmd_list_element *delete_tracepoints_cmd
14609 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
14610 Delete specified tracepoints.\n\
14611 Arguments are tracepoint numbers, separated by spaces.\n\
14612 No argument means delete all tracepoints."),
14613 &deletelist);
14614 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
14615
14616 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
14617 Disable specified tracepoints.\n\
14618 Arguments are tracepoint numbers, separated by spaces.\n\
14619 No argument means disable all tracepoints."),
14620 &disablelist);
14621 deprecate_cmd (c, "disable");
14622
14623 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
14624 Enable specified tracepoints.\n\
14625 Arguments are tracepoint numbers, separated by spaces.\n\
14626 No argument means enable all tracepoints."),
14627 &enablelist);
14628 deprecate_cmd (c, "enable");
14629
14630 add_com ("passcount", class_trace, trace_pass_command, _("\
14631 Set the passcount for a tracepoint.\n\
14632 The trace will end when the tracepoint has been passed 'count' times.\n\
14633 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
14634 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
14635
14636 add_basic_prefix_cmd ("save", class_breakpoint,
14637 _("Save breakpoint definitions as a script."),
14638 &save_cmdlist,
14639 0/*allow-unknown*/, &cmdlist);
14640
14641 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
14642 Save current breakpoint definitions as a script.\n\
14643 This includes all types of breakpoints (breakpoints, watchpoints,\n\
14644 catchpoints, tracepoints). Use the 'source' command in another debug\n\
14645 session to restore them."),
14646 &save_cmdlist);
14647 set_cmd_completer (c, filename_completer);
14648
14649 cmd_list_element *save_tracepoints_cmd
14650 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
14651 Save current tracepoint definitions as a script.\n\
14652 Use the 'source' command in another debug session to restore them."),
14653 &save_cmdlist);
14654 set_cmd_completer (save_tracepoints_cmd, filename_completer);
14655
14656 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
14657 deprecate_cmd (c, "save tracepoints");
14658
14659 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
14660 _("\
14661 Breakpoint specific settings.\n\
14662 Configure various breakpoint-specific variables such as\n\
14663 pending breakpoint behavior."),
14664 _("\
14665 Breakpoint specific settings.\n\
14666 Configure various breakpoint-specific variables such as\n\
14667 pending breakpoint behavior."),
14668 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
14669 &setlist, &showlist);
14670
14671 add_setshow_auto_boolean_cmd ("pending", no_class,
14672 &pending_break_support, _("\
14673 Set debugger's behavior regarding pending breakpoints."), _("\
14674 Show debugger's behavior regarding pending breakpoints."), _("\
14675 If on, an unrecognized breakpoint location will cause gdb to create a\n\
14676 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
14677 an error. If auto, an unrecognized breakpoint location results in a\n\
14678 user-query to see if a pending breakpoint should be created."),
14679 NULL,
14680 show_pending_break_support,
14681 &breakpoint_set_cmdlist,
14682 &breakpoint_show_cmdlist);
14683
14684 pending_break_support = AUTO_BOOLEAN_AUTO;
14685
14686 add_setshow_boolean_cmd ("auto-hw", no_class,
14687 &automatic_hardware_breakpoints, _("\
14688 Set automatic usage of hardware breakpoints."), _("\
14689 Show automatic usage of hardware breakpoints."), _("\
14690 If set, the debugger will automatically use hardware breakpoints for\n\
14691 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
14692 a warning will be emitted for such breakpoints."),
14693 NULL,
14694 show_automatic_hardware_breakpoints,
14695 &breakpoint_set_cmdlist,
14696 &breakpoint_show_cmdlist);
14697
14698 add_setshow_boolean_cmd ("always-inserted", class_support,
14699 &always_inserted_mode, _("\
14700 Set mode for inserting breakpoints."), _("\
14701 Show mode for inserting breakpoints."), _("\
14702 When this mode is on, breakpoints are inserted immediately as soon as\n\
14703 they're created, kept inserted even when execution stops, and removed\n\
14704 only when the user deletes them. When this mode is off (the default),\n\
14705 breakpoints are inserted only when execution continues, and removed\n\
14706 when execution stops."),
14707 NULL,
14708 &show_always_inserted_mode,
14709 &breakpoint_set_cmdlist,
14710 &breakpoint_show_cmdlist);
14711
14712 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
14713 condition_evaluation_enums,
14714 &condition_evaluation_mode_1, _("\
14715 Set mode of breakpoint condition evaluation."), _("\
14716 Show mode of breakpoint condition evaluation."), _("\
14717 When this is set to \"host\", breakpoint conditions will be\n\
14718 evaluated on the host's side by GDB. When it is set to \"target\",\n\
14719 breakpoint conditions will be downloaded to the target (if the target\n\
14720 supports such feature) and conditions will be evaluated on the target's side.\n\
14721 If this is set to \"auto\" (default), this will be automatically set to\n\
14722 \"target\" if it supports condition evaluation, otherwise it will\n\
14723 be set to \"host\"."),
14724 &set_condition_evaluation_mode,
14725 &show_condition_evaluation_mode,
14726 &breakpoint_set_cmdlist,
14727 &breakpoint_show_cmdlist);
14728
14729 add_com ("break-range", class_breakpoint, break_range_command, _("\
14730 Set a breakpoint for an address range.\n\
14731 break-range START-LOCATION, END-LOCATION\n\
14732 where START-LOCATION and END-LOCATION can be one of the following:\n\
14733 LINENUM, for that line in the current file,\n\
14734 FILE:LINENUM, for that line in that file,\n\
14735 +OFFSET, for that number of lines after the current line\n\
14736 or the start of the range\n\
14737 FUNCTION, for the first line in that function,\n\
14738 FILE:FUNCTION, to distinguish among like-named static functions.\n\
14739 *ADDRESS, for the instruction at that address.\n\
14740 \n\
14741 The breakpoint will stop execution of the inferior whenever it executes\n\
14742 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
14743 range (including START-LOCATION and END-LOCATION)."));
14744
14745 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
14746 Set a dynamic printf at specified location.\n\
14747 dprintf location,format string,arg1,arg2,...\n\
14748 location may be a linespec, explicit, or address location.\n"
14749 "\n" LOCATION_SPEC_HELP_STRING));
14750 set_cmd_completer (c, location_completer);
14751
14752 add_setshow_enum_cmd ("dprintf-style", class_support,
14753 dprintf_style_enums, &dprintf_style, _("\
14754 Set the style of usage for dynamic printf."), _("\
14755 Show the style of usage for dynamic printf."), _("\
14756 This setting chooses how GDB will do a dynamic printf.\n\
14757 If the value is \"gdb\", then the printing is done by GDB to its own\n\
14758 console, as with the \"printf\" command.\n\
14759 If the value is \"call\", the print is done by calling a function in your\n\
14760 program; by default printf(), but you can choose a different function or\n\
14761 output stream by setting dprintf-function and dprintf-channel."),
14762 update_dprintf_commands, NULL,
14763 &setlist, &showlist);
14764
14765 add_setshow_string_cmd ("dprintf-function", class_support,
14766 &dprintf_function, _("\
14767 Set the function to use for dynamic printf."), _("\
14768 Show the function to use for dynamic printf."), NULL,
14769 update_dprintf_commands, NULL,
14770 &setlist, &showlist);
14771
14772 add_setshow_string_cmd ("dprintf-channel", class_support,
14773 &dprintf_channel, _("\
14774 Set the channel to use for dynamic printf."), _("\
14775 Show the channel to use for dynamic printf."), NULL,
14776 update_dprintf_commands, NULL,
14777 &setlist, &showlist);
14778
14779 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
14780 &disconnected_dprintf, _("\
14781 Set whether dprintf continues after GDB disconnects."), _("\
14782 Show whether dprintf continues after GDB disconnects."), _("\
14783 Use this to let dprintf commands continue to hit and produce output\n\
14784 even if GDB disconnects or detaches from the target."),
14785 NULL,
14786 NULL,
14787 &setlist, &showlist);
14788
14789 add_com ("agent-printf", class_vars, agent_printf_command, _("\
14790 Target agent only formatted printing, like the C \"printf\" function.\n\
14791 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
14792 This supports most C printf format specifications, like %s, %d, etc.\n\
14793 This is useful for formatted output in user-defined commands."));
14794
14795 automatic_hardware_breakpoints = true;
14796
14797 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
14798 "breakpoint");
14799 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
14800 "breakpoint");
14801 }