Add some new subclasses of breakpoint
[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 breakpoint_re_set_default (struct breakpoint *);
91
92 static void
93 create_sals_from_location_default (struct event_location *location,
94 struct linespec_result *canonical,
95 enum bptype type_wanted);
96
97 static void create_breakpoints_sal_default (struct gdbarch *,
98 struct linespec_result *,
99 gdb::unique_xmalloc_ptr<char>,
100 gdb::unique_xmalloc_ptr<char>,
101 enum bptype,
102 enum bpdisp, int, int,
103 int,
104 const struct breakpoint_ops *,
105 int, int, int, unsigned);
106
107 static std::vector<symtab_and_line> decode_location_default
108 (struct breakpoint *b, struct event_location *location,
109 struct program_space *search_pspace);
110
111 static int can_use_hardware_watchpoint
112 (const std::vector<value_ref_ptr> &vals);
113
114 static void mention (struct breakpoint *);
115
116 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
117 enum bptype,
118 const struct breakpoint_ops *);
119 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
120 const struct symtab_and_line *);
121
122 /* This function is used in gdbtk sources and thus can not be made
123 static. */
124 static struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
125 struct symtab_and_line,
126 enum bptype,
127 const struct breakpoint_ops *);
128
129 static struct breakpoint *
130 momentary_breakpoint_from_master (struct breakpoint *orig,
131 enum bptype type,
132 const struct breakpoint_ops *ops,
133 int loc_enabled);
134
135 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
136
137 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
138 CORE_ADDR bpaddr,
139 enum bptype bptype);
140
141 static void describe_other_breakpoints (struct gdbarch *,
142 struct program_space *, CORE_ADDR,
143 struct obj_section *, int);
144
145 static int watchpoint_locations_match (struct bp_location *loc1,
146 struct bp_location *loc2);
147
148 static int breakpoint_locations_match (struct bp_location *loc1,
149 struct bp_location *loc2,
150 bool sw_hw_bps_match = false);
151
152 static int breakpoint_location_address_match (struct bp_location *bl,
153 const struct address_space *aspace,
154 CORE_ADDR addr);
155
156 static int breakpoint_location_address_range_overlap (struct bp_location *,
157 const address_space *,
158 CORE_ADDR, int);
159
160 static int remove_breakpoint (struct bp_location *);
161 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
162
163 static enum print_stop_action print_bp_stop_message (bpstat *bs);
164
165 static int hw_breakpoint_used_count (void);
166
167 static int hw_watchpoint_use_count (struct breakpoint *);
168
169 static int hw_watchpoint_used_count_others (struct breakpoint *except,
170 enum bptype type,
171 int *other_type_used);
172
173 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
174 int count);
175
176 static void decref_bp_location (struct bp_location **loc);
177
178 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
179
180 /* update_global_location_list's modes of operation wrt to whether to
181 insert locations now. */
182 enum ugll_insert_mode
183 {
184 /* Don't insert any breakpoint locations into the inferior, only
185 remove already-inserted locations that no longer should be
186 inserted. Functions that delete a breakpoint or breakpoints
187 should specify this mode, so that deleting a breakpoint doesn't
188 have the side effect of inserting the locations of other
189 breakpoints that are marked not-inserted, but should_be_inserted
190 returns true on them.
191
192 This behavior is useful is situations close to tear-down -- e.g.,
193 after an exec, while the target still has execution, but
194 breakpoint shadows of the previous executable image should *NOT*
195 be restored to the new image; or before detaching, where the
196 target still has execution and wants to delete breakpoints from
197 GDB's lists, and all breakpoints had already been removed from
198 the inferior. */
199 UGLL_DONT_INSERT,
200
201 /* May insert breakpoints iff breakpoints_should_be_inserted_now
202 claims breakpoints should be inserted now. */
203 UGLL_MAY_INSERT,
204
205 /* Insert locations now, irrespective of
206 breakpoints_should_be_inserted_now. E.g., say all threads are
207 stopped right now, and the user did "continue". We need to
208 insert breakpoints _before_ resuming the target, but
209 UGLL_MAY_INSERT wouldn't insert them, because
210 breakpoints_should_be_inserted_now returns false at that point,
211 as no thread is running yet. */
212 UGLL_INSERT
213 };
214
215 static void update_global_location_list (enum ugll_insert_mode);
216
217 static void update_global_location_list_nothrow (enum ugll_insert_mode);
218
219 static void insert_breakpoint_locations (void);
220
221 static void trace_pass_command (const char *, int);
222
223 static void set_tracepoint_count (int num);
224
225 static bool is_masked_watchpoint (const struct breakpoint *b);
226
227 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
228 otherwise. */
229
230 static int strace_marker_p (struct breakpoint *b);
231
232 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
233 that are implemented on top of software or hardware breakpoints
234 (user breakpoints, internal and momentary breakpoints, etc.). */
235 static struct breakpoint_ops bkpt_base_breakpoint_ops;
236
237 /* Internal breakpoints class type. */
238 static struct breakpoint_ops internal_breakpoint_ops;
239
240 /* Momentary breakpoints class type. */
241 static struct breakpoint_ops momentary_breakpoint_ops;
242
243 /* The breakpoint_ops structure to be used in regular user created
244 breakpoints. */
245 struct breakpoint_ops bkpt_breakpoint_ops;
246
247 /* Breakpoints set on probes. */
248 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
249
250 /* Tracepoints set on probes. */
251 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
252
253 /* Dynamic printf class type. */
254 struct breakpoint_ops dprintf_breakpoint_ops;
255
256 /* The structure to be used in regular breakpoints. */
257 struct ordinary_breakpoint : public base_breakpoint
258 {
259 };
260
261 /* Internal breakpoints. */
262 struct internal_breakpoint : public base_breakpoint
263 {
264 };
265
266 /* Momentary breakpoints. */
267 struct momentary_breakpoint : public base_breakpoint
268 {
269 };
270
271 /* DPrintf breakpoints. */
272 struct dprintf_breakpoint : public base_breakpoint
273 {
274 };
275
276 /* The style in which to perform a dynamic printf. This is a user
277 option because different output options have different tradeoffs;
278 if GDB does the printing, there is better error handling if there
279 is a problem with any of the arguments, but using an inferior
280 function lets you have special-purpose printers and sending of
281 output to the same place as compiled-in print functions. */
282
283 static const char dprintf_style_gdb[] = "gdb";
284 static const char dprintf_style_call[] = "call";
285 static const char dprintf_style_agent[] = "agent";
286 static const char *const dprintf_style_enums[] = {
287 dprintf_style_gdb,
288 dprintf_style_call,
289 dprintf_style_agent,
290 NULL
291 };
292 static const char *dprintf_style = dprintf_style_gdb;
293
294 /* The function to use for dynamic printf if the preferred style is to
295 call into the inferior. The value is simply a string that is
296 copied into the command, so it can be anything that GDB can
297 evaluate to a callable address, not necessarily a function name. */
298
299 static std::string dprintf_function = "printf";
300
301 /* The channel to use for dynamic printf if the preferred style is to
302 call into the inferior; if a nonempty string, it will be passed to
303 the call as the first argument, with the format string as the
304 second. As with the dprintf function, this can be anything that
305 GDB knows how to evaluate, so in addition to common choices like
306 "stderr", this could be an app-specific expression like
307 "mystreams[curlogger]". */
308
309 static std::string dprintf_channel;
310
311 /* True if dprintf commands should continue to operate even if GDB
312 has disconnected. */
313 static bool disconnected_dprintf = true;
314
315 struct command_line *
316 breakpoint_commands (struct breakpoint *b)
317 {
318 return b->commands ? b->commands.get () : NULL;
319 }
320
321 /* Flag indicating that a command has proceeded the inferior past the
322 current breakpoint. */
323
324 static bool breakpoint_proceeded;
325
326 const char *
327 bpdisp_text (enum bpdisp disp)
328 {
329 /* NOTE: the following values are a part of MI protocol and
330 represent values of 'disp' field returned when inferior stops at
331 a breakpoint. */
332 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
333
334 return bpdisps[(int) disp];
335 }
336
337 /* Prototypes for exported functions. */
338 /* If FALSE, gdb will not use hardware support for watchpoints, even
339 if such is available. */
340 static int can_use_hw_watchpoints;
341
342 static void
343 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
344 struct cmd_list_element *c,
345 const char *value)
346 {
347 gdb_printf (file,
348 _("Debugger's willingness to use "
349 "watchpoint hardware is %s.\n"),
350 value);
351 }
352
353 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
354 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
355 for unrecognized breakpoint locations.
356 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
357 static enum auto_boolean pending_break_support;
358 static void
359 show_pending_break_support (struct ui_file *file, int from_tty,
360 struct cmd_list_element *c,
361 const char *value)
362 {
363 gdb_printf (file,
364 _("Debugger's behavior regarding "
365 "pending breakpoints is %s.\n"),
366 value);
367 }
368
369 /* If true, gdb will automatically use hardware breakpoints for breakpoints
370 set with "break" but falling in read-only memory.
371 If false, gdb will warn about such breakpoints, but won't automatically
372 use hardware breakpoints. */
373 static bool automatic_hardware_breakpoints;
374 static void
375 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
376 struct cmd_list_element *c,
377 const char *value)
378 {
379 gdb_printf (file,
380 _("Automatic usage of hardware breakpoints is %s.\n"),
381 value);
382 }
383
384 /* If on, GDB keeps breakpoints inserted even if the inferior is
385 stopped, and immediately inserts any new breakpoints as soon as
386 they're created. If off (default), GDB keeps breakpoints off of
387 the target as long as possible. That is, it delays inserting
388 breakpoints until the next resume, and removes them again when the
389 target fully stops. This is a bit safer in case GDB crashes while
390 processing user input. */
391 static bool always_inserted_mode = false;
392
393 static void
394 show_always_inserted_mode (struct ui_file *file, int from_tty,
395 struct cmd_list_element *c, const char *value)
396 {
397 gdb_printf (file, _("Always inserted breakpoint mode is %s.\n"),
398 value);
399 }
400
401 /* See breakpoint.h. */
402
403 int
404 breakpoints_should_be_inserted_now (void)
405 {
406 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
407 {
408 /* If breakpoints are global, they should be inserted even if no
409 thread under gdb's control is running, or even if there are
410 no threads under GDB's control yet. */
411 return 1;
412 }
413 else
414 {
415 if (always_inserted_mode)
416 {
417 /* The user wants breakpoints inserted even if all threads
418 are stopped. */
419 return 1;
420 }
421
422 for (inferior *inf : all_inferiors ())
423 if (inf->has_execution ()
424 && threads_are_executing (inf->process_target ()))
425 return 1;
426
427 /* Don't remove breakpoints yet if, even though all threads are
428 stopped, we still have events to process. */
429 for (thread_info *tp : all_non_exited_threads ())
430 if (tp->resumed () && tp->has_pending_waitstatus ())
431 return 1;
432 }
433 return 0;
434 }
435
436 static const char condition_evaluation_both[] = "host or target";
437
438 /* Modes for breakpoint condition evaluation. */
439 static const char condition_evaluation_auto[] = "auto";
440 static const char condition_evaluation_host[] = "host";
441 static const char condition_evaluation_target[] = "target";
442 static const char *const condition_evaluation_enums[] = {
443 condition_evaluation_auto,
444 condition_evaluation_host,
445 condition_evaluation_target,
446 NULL
447 };
448
449 /* Global that holds the current mode for breakpoint condition evaluation. */
450 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
451
452 /* Global that we use to display information to the user (gets its value from
453 condition_evaluation_mode_1. */
454 static const char *condition_evaluation_mode = condition_evaluation_auto;
455
456 /* Translate a condition evaluation mode MODE into either "host"
457 or "target". This is used mostly to translate from "auto" to the
458 real setting that is being used. It returns the translated
459 evaluation mode. */
460
461 static const char *
462 translate_condition_evaluation_mode (const char *mode)
463 {
464 if (mode == condition_evaluation_auto)
465 {
466 if (target_supports_evaluation_of_breakpoint_conditions ())
467 return condition_evaluation_target;
468 else
469 return condition_evaluation_host;
470 }
471 else
472 return mode;
473 }
474
475 /* Discovers what condition_evaluation_auto translates to. */
476
477 static const char *
478 breakpoint_condition_evaluation_mode (void)
479 {
480 return translate_condition_evaluation_mode (condition_evaluation_mode);
481 }
482
483 /* Return true if GDB should evaluate breakpoint conditions or false
484 otherwise. */
485
486 static int
487 gdb_evaluates_breakpoint_condition_p (void)
488 {
489 const char *mode = breakpoint_condition_evaluation_mode ();
490
491 return (mode == condition_evaluation_host);
492 }
493
494 /* Are we executing breakpoint commands? */
495 static int executing_breakpoint_commands;
496
497 /* Are overlay event breakpoints enabled? */
498 static int overlay_events_enabled;
499
500 /* See description in breakpoint.h. */
501 bool target_exact_watchpoints = false;
502
503 /* Walk the following statement or block through all breakpoints.
504 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
505 current breakpoint. */
506
507 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
508 for (B = breakpoint_chain; \
509 B ? (TMP=B->next, 1): 0; \
510 B = TMP)
511
512 /* Chains of all breakpoints defined. */
513
514 static struct breakpoint *breakpoint_chain;
515
516 /* See breakpoint.h. */
517
518 breakpoint_range
519 all_breakpoints ()
520 {
521 return breakpoint_range (breakpoint_chain);
522 }
523
524 /* See breakpoint.h. */
525
526 breakpoint_safe_range
527 all_breakpoints_safe ()
528 {
529 return breakpoint_safe_range (all_breakpoints ());
530 }
531
532 /* See breakpoint.h. */
533
534 tracepoint_range
535 all_tracepoints ()
536 {
537 return tracepoint_range (breakpoint_chain);
538 }
539
540 /* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
541
542 static std::vector<bp_location *> bp_locations;
543
544 /* See breakpoint.h. */
545
546 const std::vector<bp_location *> &
547 all_bp_locations ()
548 {
549 return bp_locations;
550 }
551
552 /* Range to iterate over breakpoint locations at a given address. */
553
554 struct bp_locations_at_addr_range
555 {
556 using iterator = std::vector<bp_location *>::iterator;
557
558 bp_locations_at_addr_range (CORE_ADDR addr)
559 {
560 struct compare
561 {
562 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
563 { return loc->address < addr_; }
564
565 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
566 { return addr_ < loc->address; }
567 };
568
569 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
570 addr, compare ());
571
572 m_begin = it_pair.first;
573 m_end = it_pair.second;
574 }
575
576 iterator begin () const
577 { return m_begin; }
578
579 iterator end () const
580 { return m_end; }
581
582 private:
583 iterator m_begin;
584 iterator m_end;
585 };
586
587 /* Return a range to iterate over all breakpoint locations exactly at address
588 ADDR.
589
590 If it's needed to iterate multiple times on the same range, it's possible
591 to save the range in a local variable and use it multiple times:
592
593 auto range = all_bp_locations_at_addr (addr);
594
595 for (bp_location *loc : range)
596 // use loc
597
598 for (bp_location *loc : range)
599 // use loc
600
601 This saves a bit of time, as it avoids re-doing the binary searches to find
602 the range's boundaries. Just remember not to change the bp_locations vector
603 in the mean time, as it could make the range's iterators stale. */
604
605 static bp_locations_at_addr_range
606 all_bp_locations_at_addr (CORE_ADDR addr)
607 {
608 return bp_locations_at_addr_range (addr);
609 }
610
611 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
612 ADDRESS for the current elements of BP_LOCATIONS which get a valid
613 result from bp_location_has_shadow. You can use it for roughly
614 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
615 an address you need to read. */
616
617 static CORE_ADDR bp_locations_placed_address_before_address_max;
618
619 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
620 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
621 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
622 You can use it for roughly limiting the subrange of BP_LOCATIONS to
623 scan for shadow bytes for an address you need to read. */
624
625 static CORE_ADDR bp_locations_shadow_len_after_address_max;
626
627 /* The locations that no longer correspond to any breakpoint, unlinked
628 from the bp_locations array, but for which a hit may still be
629 reported by a target. */
630 static std::vector<bp_location *> moribund_locations;
631
632 /* Number of last breakpoint made. */
633
634 static int breakpoint_count;
635
636 /* The value of `breakpoint_count' before the last command that
637 created breakpoints. If the last (break-like) command created more
638 than one breakpoint, then the difference between BREAKPOINT_COUNT
639 and PREV_BREAKPOINT_COUNT is more than one. */
640 static int prev_breakpoint_count;
641
642 /* Number of last tracepoint made. */
643
644 static int tracepoint_count;
645
646 static struct cmd_list_element *breakpoint_set_cmdlist;
647 static struct cmd_list_element *breakpoint_show_cmdlist;
648 struct cmd_list_element *save_cmdlist;
649
650 /* Return whether a breakpoint is an active enabled breakpoint. */
651 static int
652 breakpoint_enabled (struct breakpoint *b)
653 {
654 return (b->enable_state == bp_enabled);
655 }
656
657 /* Set breakpoint count to NUM. */
658
659 static void
660 set_breakpoint_count (int num)
661 {
662 prev_breakpoint_count = breakpoint_count;
663 breakpoint_count = num;
664 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
665 }
666
667 /* Used by `start_rbreak_breakpoints' below, to record the current
668 breakpoint count before "rbreak" creates any breakpoint. */
669 static int rbreak_start_breakpoint_count;
670
671 /* Called at the start an "rbreak" command to record the first
672 breakpoint made. */
673
674 scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
675 {
676 rbreak_start_breakpoint_count = breakpoint_count;
677 }
678
679 /* Called at the end of an "rbreak" command to record the last
680 breakpoint made. */
681
682 scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
683 {
684 prev_breakpoint_count = rbreak_start_breakpoint_count;
685 }
686
687 /* Used in run_command to zero the hit count when a new run starts. */
688
689 void
690 clear_breakpoint_hit_counts (void)
691 {
692 for (breakpoint *b : all_breakpoints ())
693 b->hit_count = 0;
694 }
695
696 \f
697 /* Return the breakpoint with the specified number, or NULL
698 if the number does not refer to an existing breakpoint. */
699
700 struct breakpoint *
701 get_breakpoint (int num)
702 {
703 for (breakpoint *b : all_breakpoints ())
704 if (b->number == num)
705 return b;
706
707 return nullptr;
708 }
709
710 \f
711
712 /* Mark locations as "conditions have changed" in case the target supports
713 evaluating conditions on its side. */
714
715 static void
716 mark_breakpoint_modified (struct breakpoint *b)
717 {
718 /* This is only meaningful if the target is
719 evaluating conditions and if the user has
720 opted for condition evaluation on the target's
721 side. */
722 if (gdb_evaluates_breakpoint_condition_p ()
723 || !target_supports_evaluation_of_breakpoint_conditions ())
724 return;
725
726 if (!is_breakpoint (b))
727 return;
728
729 for (bp_location *loc : b->locations ())
730 loc->condition_changed = condition_modified;
731 }
732
733 /* Mark location as "conditions have changed" in case the target supports
734 evaluating conditions on its side. */
735
736 static void
737 mark_breakpoint_location_modified (struct bp_location *loc)
738 {
739 /* This is only meaningful if the target is
740 evaluating conditions and if the user has
741 opted for condition evaluation on the target's
742 side. */
743 if (gdb_evaluates_breakpoint_condition_p ()
744 || !target_supports_evaluation_of_breakpoint_conditions ())
745
746 return;
747
748 if (!is_breakpoint (loc->owner))
749 return;
750
751 loc->condition_changed = condition_modified;
752 }
753
754 /* Sets the condition-evaluation mode using the static global
755 condition_evaluation_mode. */
756
757 static void
758 set_condition_evaluation_mode (const char *args, int from_tty,
759 struct cmd_list_element *c)
760 {
761 const char *old_mode, *new_mode;
762
763 if ((condition_evaluation_mode_1 == condition_evaluation_target)
764 && !target_supports_evaluation_of_breakpoint_conditions ())
765 {
766 condition_evaluation_mode_1 = condition_evaluation_mode;
767 warning (_("Target does not support breakpoint condition evaluation.\n"
768 "Using host evaluation mode instead."));
769 return;
770 }
771
772 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
773 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
774
775 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
776 settings was "auto". */
777 condition_evaluation_mode = condition_evaluation_mode_1;
778
779 /* Only update the mode if the user picked a different one. */
780 if (new_mode != old_mode)
781 {
782 /* If the user switched to a different evaluation mode, we
783 need to synch the changes with the target as follows:
784
785 "host" -> "target": Send all (valid) conditions to the target.
786 "target" -> "host": Remove all the conditions from the target.
787 */
788
789 if (new_mode == condition_evaluation_target)
790 {
791 /* Mark everything modified and synch conditions with the
792 target. */
793 for (bp_location *loc : all_bp_locations ())
794 mark_breakpoint_location_modified (loc);
795 }
796 else
797 {
798 /* Manually mark non-duplicate locations to synch conditions
799 with the target. We do this to remove all the conditions the
800 target knows about. */
801 for (bp_location *loc : all_bp_locations ())
802 if (is_breakpoint (loc->owner) && loc->inserted)
803 loc->needs_update = 1;
804 }
805
806 /* Do the update. */
807 update_global_location_list (UGLL_MAY_INSERT);
808 }
809
810 return;
811 }
812
813 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
814 what "auto" is translating to. */
815
816 static void
817 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
818 struct cmd_list_element *c, const char *value)
819 {
820 if (condition_evaluation_mode == condition_evaluation_auto)
821 gdb_printf (file,
822 _("Breakpoint condition evaluation "
823 "mode is %s (currently %s).\n"),
824 value,
825 breakpoint_condition_evaluation_mode ());
826 else
827 gdb_printf (file, _("Breakpoint condition evaluation mode is %s.\n"),
828 value);
829 }
830
831 /* Parse COND_STRING in the context of LOC and set as the condition
832 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
833 the number of LOC within its owner. In case of parsing error, mark
834 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
835
836 static void
837 set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
838 int bp_num, int loc_num)
839 {
840 bool has_junk = false;
841 try
842 {
843 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
844 block_for_pc (loc->address), 0);
845 if (*cond_string != 0)
846 has_junk = true;
847 else
848 {
849 loc->cond = std::move (new_exp);
850 if (loc->disabled_by_cond && loc->enabled)
851 gdb_printf (_("Breakpoint %d's condition is now valid at "
852 "location %d, enabling.\n"),
853 bp_num, loc_num);
854
855 loc->disabled_by_cond = false;
856 }
857 }
858 catch (const gdb_exception_error &e)
859 {
860 if (loc->enabled)
861 {
862 /* Warn if a user-enabled location is now becoming disabled-by-cond.
863 BP_NUM is 0 if the breakpoint is being defined for the first
864 time using the "break ... if ..." command, and non-zero if
865 already defined. */
866 if (bp_num != 0)
867 warning (_("failed to validate condition at location %d.%d, "
868 "disabling:\n %s"), bp_num, loc_num, e.what ());
869 else
870 warning (_("failed to validate condition at location %d, "
871 "disabling:\n %s"), loc_num, e.what ());
872 }
873
874 loc->disabled_by_cond = true;
875 }
876
877 if (has_junk)
878 error (_("Garbage '%s' follows condition"), cond_string);
879 }
880
881 void
882 set_breakpoint_condition (struct breakpoint *b, const char *exp,
883 int from_tty, bool force)
884 {
885 if (*exp == 0)
886 {
887 b->cond_string.reset ();
888
889 if (is_watchpoint (b))
890 static_cast<watchpoint *> (b)->cond_exp.reset ();
891 else
892 {
893 int loc_num = 1;
894 for (bp_location *loc : b->locations ())
895 {
896 loc->cond.reset ();
897 if (loc->disabled_by_cond && loc->enabled)
898 gdb_printf (_("Breakpoint %d's condition is now valid at "
899 "location %d, enabling.\n"),
900 b->number, loc_num);
901 loc->disabled_by_cond = false;
902 loc_num++;
903
904 /* No need to free the condition agent expression
905 bytecode (if we have one). We will handle this
906 when we go through update_global_location_list. */
907 }
908 }
909
910 if (from_tty)
911 gdb_printf (_("Breakpoint %d now unconditional.\n"), b->number);
912 }
913 else
914 {
915 if (is_watchpoint (b))
916 {
917 innermost_block_tracker tracker;
918 const char *arg = exp;
919 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
920 if (*arg != 0)
921 error (_("Junk at end of expression"));
922 watchpoint *w = static_cast<watchpoint *> (b);
923 w->cond_exp = std::move (new_exp);
924 w->cond_exp_valid_block = tracker.block ();
925 }
926 else
927 {
928 /* Parse and set condition expressions. We make two passes.
929 In the first, we parse the condition string to see if it
930 is valid in at least one location. If so, the condition
931 would be accepted. So we go ahead and set the locations'
932 conditions. In case no valid case is found, we throw
933 the error and the condition string will be rejected.
934 This two-pass approach is taken to avoid setting the
935 state of locations in case of a reject. */
936 for (bp_location *loc : b->locations ())
937 {
938 try
939 {
940 const char *arg = exp;
941 parse_exp_1 (&arg, loc->address,
942 block_for_pc (loc->address), 0);
943 if (*arg != 0)
944 error (_("Junk at end of expression"));
945 break;
946 }
947 catch (const gdb_exception_error &e)
948 {
949 /* Condition string is invalid. If this happens to
950 be the last loc, abandon (if not forced) or continue
951 (if forced). */
952 if (loc->next == nullptr && !force)
953 throw;
954 }
955 }
956
957 /* If we reach here, the condition is valid at some locations. */
958 int loc_num = 1;
959 for (bp_location *loc : b->locations ())
960 {
961 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
962 loc_num++;
963 }
964 }
965
966 /* We know that the new condition parsed successfully. The
967 condition string of the breakpoint can be safely updated. */
968 b->cond_string = make_unique_xstrdup (exp);
969 b->condition_not_parsed = 0;
970 }
971 mark_breakpoint_modified (b);
972
973 gdb::observers::breakpoint_modified.notify (b);
974 }
975
976 /* See breakpoint.h. */
977
978 void
979 set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
980 bool force)
981 {
982 for (breakpoint *b : all_breakpoints ())
983 if (b->number == bpnum)
984 {
985 /* Check if this breakpoint has a "stop" method implemented in an
986 extension language. This method and conditions entered into GDB
987 from the CLI are mutually exclusive. */
988 const struct extension_language_defn *extlang
989 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
990
991 if (extlang != NULL)
992 {
993 error (_("Only one stop condition allowed. There is currently"
994 " a %s stop condition defined for this breakpoint."),
995 ext_lang_capitalized_name (extlang));
996 }
997 set_breakpoint_condition (b, exp, from_tty, force);
998
999 if (is_breakpoint (b))
1000 update_global_location_list (UGLL_MAY_INSERT);
1001
1002 return;
1003 }
1004
1005 error (_("No breakpoint number %d."), bpnum);
1006 }
1007
1008 /* The options for the "condition" command. */
1009
1010 struct condition_command_opts
1011 {
1012 /* For "-force". */
1013 bool force_condition = false;
1014 };
1015
1016 static const gdb::option::option_def condition_command_option_defs[] = {
1017
1018 gdb::option::flag_option_def<condition_command_opts> {
1019 "force",
1020 [] (condition_command_opts *opts) { return &opts->force_condition; },
1021 N_("Set the condition even if it is invalid for all current locations."),
1022 },
1023
1024 };
1025
1026 /* Create an option_def_group for the "condition" options, with
1027 CC_OPTS as context. */
1028
1029 static inline gdb::option::option_def_group
1030 make_condition_command_options_def_group (condition_command_opts *cc_opts)
1031 {
1032 return {{condition_command_option_defs}, cc_opts};
1033 }
1034
1035 /* Completion for the "condition" command. */
1036
1037 static void
1038 condition_completer (struct cmd_list_element *cmd,
1039 completion_tracker &tracker,
1040 const char *text, const char * /*word*/)
1041 {
1042 bool has_no_arguments = (*text == '\0');
1043 condition_command_opts cc_opts;
1044 const auto group = make_condition_command_options_def_group (&cc_opts);
1045 if (gdb::option::complete_options
1046 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1047 return;
1048
1049 text = skip_spaces (text);
1050 const char *space = skip_to_space (text);
1051 if (*space == '\0')
1052 {
1053 int len;
1054
1055 if (text[0] == '$')
1056 {
1057 tracker.advance_custom_word_point_by (1);
1058 /* We don't support completion of history indices. */
1059 if (!isdigit (text[1]))
1060 complete_internalvar (tracker, &text[1]);
1061 return;
1062 }
1063
1064 /* Suggest the "-force" flag if no arguments are given. If
1065 arguments were passed, they either already include the flag,
1066 or we are beyond the point of suggesting it because it's
1067 positionally the first argument. */
1068 if (has_no_arguments)
1069 gdb::option::complete_on_all_options (tracker, group);
1070
1071 /* We're completing the breakpoint number. */
1072 len = strlen (text);
1073
1074 for (breakpoint *b : all_breakpoints ())
1075 {
1076 char number[50];
1077
1078 xsnprintf (number, sizeof (number), "%d", b->number);
1079
1080 if (strncmp (number, text, len) == 0)
1081 tracker.add_completion (make_unique_xstrdup (number));
1082 }
1083
1084 return;
1085 }
1086
1087 /* We're completing the expression part. Skip the breakpoint num. */
1088 const char *exp_start = skip_spaces (space);
1089 tracker.advance_custom_word_point_by (exp_start - text);
1090 text = exp_start;
1091 const char *word = advance_to_expression_complete_word_point (tracker, text);
1092 expression_completer (cmd, tracker, text, word);
1093 }
1094
1095 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1096
1097 static void
1098 condition_command (const char *arg, int from_tty)
1099 {
1100 const char *p;
1101 int bnum;
1102
1103 if (arg == 0)
1104 error_no_arg (_("breakpoint number"));
1105
1106 p = arg;
1107
1108 /* Check if the "-force" flag was passed. */
1109 condition_command_opts cc_opts;
1110 const auto group = make_condition_command_options_def_group (&cc_opts);
1111 gdb::option::process_options
1112 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
1113
1114 bnum = get_number (&p);
1115 if (bnum == 0)
1116 error (_("Bad breakpoint argument: '%s'"), arg);
1117
1118 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
1119 }
1120
1121 /* Check that COMMAND do not contain commands that are suitable
1122 only for tracepoints and not suitable for ordinary breakpoints.
1123 Throw if any such commands is found. */
1124
1125 static void
1126 check_no_tracepoint_commands (struct command_line *commands)
1127 {
1128 struct command_line *c;
1129
1130 for (c = commands; c; c = c->next)
1131 {
1132 if (c->control_type == while_stepping_control)
1133 error (_("The 'while-stepping' command can "
1134 "only be used for tracepoints"));
1135
1136 check_no_tracepoint_commands (c->body_list_0.get ());
1137 check_no_tracepoint_commands (c->body_list_1.get ());
1138
1139 /* Not that command parsing removes leading whitespace and comment
1140 lines and also empty lines. So, we only need to check for
1141 command directly. */
1142 if (strstr (c->line, "collect ") == c->line)
1143 error (_("The 'collect' command can only be used for tracepoints"));
1144
1145 if (strstr (c->line, "teval ") == c->line)
1146 error (_("The 'teval' command can only be used for tracepoints"));
1147 }
1148 }
1149
1150 struct longjmp_breakpoint : public momentary_breakpoint
1151 {
1152 ~longjmp_breakpoint () override;
1153 };
1154
1155 /* Encapsulate tests for different types of tracepoints. */
1156
1157 static bool
1158 is_tracepoint_type (bptype type)
1159 {
1160 return (type == bp_tracepoint
1161 || type == bp_fast_tracepoint
1162 || type == bp_static_tracepoint);
1163 }
1164
1165 /* See breakpoint.h. */
1166
1167 bool
1168 is_tracepoint (const struct breakpoint *b)
1169 {
1170 return is_tracepoint_type (b->type);
1171 }
1172
1173 /* Factory function to create an appropriate instance of breakpoint given
1174 TYPE. */
1175
1176 static std::unique_ptr<breakpoint>
1177 new_breakpoint_from_type (bptype type)
1178 {
1179 breakpoint *b;
1180
1181 switch (type)
1182 {
1183 case bp_breakpoint:
1184 case bp_hardware_breakpoint:
1185 b = new ordinary_breakpoint ();
1186 break;
1187
1188 case bp_fast_tracepoint:
1189 case bp_static_tracepoint:
1190 case bp_tracepoint:
1191 b = new tracepoint ();
1192 break;
1193
1194 case bp_dprintf:
1195 b = new dprintf_breakpoint ();
1196 break;
1197
1198 case bp_overlay_event:
1199 case bp_longjmp_master:
1200 case bp_std_terminate_master:
1201 case bp_exception_master:
1202 case bp_thread_event:
1203 case bp_jit_event:
1204 case bp_shlib_event:
1205 b = new internal_breakpoint ();
1206 break;
1207
1208 case bp_longjmp:
1209 case bp_exception:
1210 b = new longjmp_breakpoint ();
1211 break;
1212
1213 case bp_watchpoint_scope:
1214 case bp_finish:
1215 case bp_gnu_ifunc_resolver_return:
1216 case bp_step_resume:
1217 case bp_hp_step_resume:
1218 case bp_longjmp_resume:
1219 case bp_longjmp_call_dummy:
1220 case bp_exception_resume:
1221 case bp_call_dummy:
1222 case bp_until:
1223 case bp_std_terminate:
1224 b = new momentary_breakpoint ();
1225 break;
1226
1227 default:
1228 gdb_assert_not_reached ("invalid type");
1229 }
1230
1231 return std::unique_ptr<breakpoint> (b);
1232 }
1233
1234 /* A helper function that validates that COMMANDS are valid for a
1235 breakpoint. This function will throw an exception if a problem is
1236 found. */
1237
1238 static void
1239 validate_commands_for_breakpoint (struct breakpoint *b,
1240 struct command_line *commands)
1241 {
1242 if (is_tracepoint (b))
1243 {
1244 struct tracepoint *t = (struct tracepoint *) b;
1245 struct command_line *c;
1246 struct command_line *while_stepping = 0;
1247
1248 /* Reset the while-stepping step count. The previous commands
1249 might have included a while-stepping action, while the new
1250 ones might not. */
1251 t->step_count = 0;
1252
1253 /* We need to verify that each top-level element of commands is
1254 valid for tracepoints, that there's at most one
1255 while-stepping element, and that the while-stepping's body
1256 has valid tracing commands excluding nested while-stepping.
1257 We also need to validate the tracepoint action line in the
1258 context of the tracepoint --- validate_actionline actually
1259 has side effects, like setting the tracepoint's
1260 while-stepping STEP_COUNT, in addition to checking if the
1261 collect/teval actions parse and make sense in the
1262 tracepoint's context. */
1263 for (c = commands; c; c = c->next)
1264 {
1265 if (c->control_type == while_stepping_control)
1266 {
1267 if (b->type == bp_fast_tracepoint)
1268 error (_("The 'while-stepping' command "
1269 "cannot be used for fast tracepoint"));
1270 else if (b->type == bp_static_tracepoint)
1271 error (_("The 'while-stepping' command "
1272 "cannot be used for static tracepoint"));
1273
1274 if (while_stepping)
1275 error (_("The 'while-stepping' command "
1276 "can be used only once"));
1277 else
1278 while_stepping = c;
1279 }
1280
1281 validate_actionline (c->line, b);
1282 }
1283 if (while_stepping)
1284 {
1285 struct command_line *c2;
1286
1287 gdb_assert (while_stepping->body_list_1 == nullptr);
1288 c2 = while_stepping->body_list_0.get ();
1289 for (; c2; c2 = c2->next)
1290 {
1291 if (c2->control_type == while_stepping_control)
1292 error (_("The 'while-stepping' command cannot be nested"));
1293 }
1294 }
1295 }
1296 else
1297 {
1298 check_no_tracepoint_commands (commands);
1299 }
1300 }
1301
1302 /* Return a vector of all the static tracepoints set at ADDR. The
1303 caller is responsible for releasing the vector. */
1304
1305 std::vector<breakpoint *>
1306 static_tracepoints_here (CORE_ADDR addr)
1307 {
1308 std::vector<breakpoint *> found;
1309
1310 for (breakpoint *b : all_breakpoints ())
1311 if (b->type == bp_static_tracepoint)
1312 {
1313 for (bp_location *loc : b->locations ())
1314 if (loc->address == addr)
1315 found.push_back (b);
1316 }
1317
1318 return found;
1319 }
1320
1321 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1322 validate that only allowed commands are included. */
1323
1324 void
1325 breakpoint_set_commands (struct breakpoint *b,
1326 counted_command_line &&commands)
1327 {
1328 validate_commands_for_breakpoint (b, commands.get ());
1329
1330 b->commands = std::move (commands);
1331 gdb::observers::breakpoint_modified.notify (b);
1332 }
1333
1334 /* Set the internal `silent' flag on the breakpoint. Note that this
1335 is not the same as the "silent" that may appear in the breakpoint's
1336 commands. */
1337
1338 void
1339 breakpoint_set_silent (struct breakpoint *b, int silent)
1340 {
1341 int old_silent = b->silent;
1342
1343 b->silent = silent;
1344 if (old_silent != silent)
1345 gdb::observers::breakpoint_modified.notify (b);
1346 }
1347
1348 /* Set the thread for this breakpoint. If THREAD is -1, make the
1349 breakpoint work for any thread. */
1350
1351 void
1352 breakpoint_set_thread (struct breakpoint *b, int thread)
1353 {
1354 int old_thread = b->thread;
1355
1356 b->thread = thread;
1357 if (old_thread != thread)
1358 gdb::observers::breakpoint_modified.notify (b);
1359 }
1360
1361 /* Set the task for this breakpoint. If TASK is 0, make the
1362 breakpoint work for any task. */
1363
1364 void
1365 breakpoint_set_task (struct breakpoint *b, int task)
1366 {
1367 int old_task = b->task;
1368
1369 b->task = task;
1370 if (old_task != task)
1371 gdb::observers::breakpoint_modified.notify (b);
1372 }
1373
1374 static void
1375 commands_command_1 (const char *arg, int from_tty,
1376 struct command_line *control)
1377 {
1378 counted_command_line cmd;
1379 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1380 NULL after the call to read_command_lines if the user provides an empty
1381 list of command by just typing "end". */
1382 bool cmd_read = false;
1383
1384 std::string new_arg;
1385
1386 if (arg == NULL || !*arg)
1387 {
1388 /* Argument not explicitly given. Synthesize it. */
1389 if (breakpoint_count - prev_breakpoint_count > 1)
1390 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1391 breakpoint_count);
1392 else if (breakpoint_count > 0)
1393 new_arg = string_printf ("%d", breakpoint_count);
1394 }
1395 else
1396 {
1397 /* Create a copy of ARG. This is needed because the "commands"
1398 command may be coming from a script. In that case, the read
1399 line buffer is going to be overwritten in the lambda of
1400 'map_breakpoint_numbers' below when reading the next line
1401 before we are are done parsing the breakpoint numbers. */
1402 new_arg = arg;
1403 }
1404 arg = new_arg.c_str ();
1405
1406 map_breakpoint_numbers
1407 (arg, [&] (breakpoint *b)
1408 {
1409 if (!cmd_read)
1410 {
1411 gdb_assert (cmd == NULL);
1412 if (control != NULL)
1413 cmd = control->body_list_0;
1414 else
1415 {
1416 std::string str
1417 = string_printf (_("Type commands for breakpoint(s) "
1418 "%s, one per line."),
1419 arg);
1420
1421 auto do_validate = [=] (const char *line)
1422 {
1423 validate_actionline (line, b);
1424 };
1425 gdb::function_view<void (const char *)> validator;
1426 if (is_tracepoint (b))
1427 validator = do_validate;
1428
1429 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
1430 }
1431 cmd_read = true;
1432 }
1433
1434 /* If a breakpoint was on the list more than once, we don't need to
1435 do anything. */
1436 if (b->commands != cmd)
1437 {
1438 validate_commands_for_breakpoint (b, cmd.get ());
1439 b->commands = cmd;
1440 gdb::observers::breakpoint_modified.notify (b);
1441 }
1442 });
1443 }
1444
1445 static void
1446 commands_command (const char *arg, int from_tty)
1447 {
1448 commands_command_1 (arg, from_tty, NULL);
1449 }
1450
1451 /* Like commands_command, but instead of reading the commands from
1452 input stream, takes them from an already parsed command structure.
1453
1454 This is used by cli-script.c to DTRT with breakpoint commands
1455 that are part of if and while bodies. */
1456 enum command_control_type
1457 commands_from_control_command (const char *arg, struct command_line *cmd)
1458 {
1459 commands_command_1 (arg, 0, cmd);
1460 return simple_control;
1461 }
1462
1463 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1464
1465 static int
1466 bp_location_has_shadow (struct bp_location *bl)
1467 {
1468 if (bl->loc_type != bp_loc_software_breakpoint)
1469 return 0;
1470 if (!bl->inserted)
1471 return 0;
1472 if (bl->target_info.shadow_len == 0)
1473 /* BL isn't valid, or doesn't shadow memory. */
1474 return 0;
1475 return 1;
1476 }
1477
1478 /* Update BUF, which is LEN bytes read from the target address
1479 MEMADDR, by replacing a memory breakpoint with its shadowed
1480 contents.
1481
1482 If READBUF is not NULL, this buffer must not overlap with the of
1483 the breakpoint location's shadow_contents buffer. Otherwise, a
1484 failed assertion internal error will be raised. */
1485
1486 static void
1487 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1488 const gdb_byte *writebuf_org,
1489 ULONGEST memaddr, LONGEST len,
1490 struct bp_target_info *target_info,
1491 struct gdbarch *gdbarch)
1492 {
1493 /* Now do full processing of the found relevant range of elements. */
1494 CORE_ADDR bp_addr = 0;
1495 int bp_size = 0;
1496 int bptoffset = 0;
1497
1498 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1499 current_program_space->aspace, 0))
1500 {
1501 /* The breakpoint is inserted in a different address space. */
1502 return;
1503 }
1504
1505 /* Addresses and length of the part of the breakpoint that
1506 we need to copy. */
1507 bp_addr = target_info->placed_address;
1508 bp_size = target_info->shadow_len;
1509
1510 if (bp_addr + bp_size <= memaddr)
1511 {
1512 /* The breakpoint is entirely before the chunk of memory we are
1513 reading. */
1514 return;
1515 }
1516
1517 if (bp_addr >= memaddr + len)
1518 {
1519 /* The breakpoint is entirely after the chunk of memory we are
1520 reading. */
1521 return;
1522 }
1523
1524 /* Offset within shadow_contents. */
1525 if (bp_addr < memaddr)
1526 {
1527 /* Only copy the second part of the breakpoint. */
1528 bp_size -= memaddr - bp_addr;
1529 bptoffset = memaddr - bp_addr;
1530 bp_addr = memaddr;
1531 }
1532
1533 if (bp_addr + bp_size > memaddr + len)
1534 {
1535 /* Only copy the first part of the breakpoint. */
1536 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1537 }
1538
1539 if (readbuf != NULL)
1540 {
1541 /* Verify that the readbuf buffer does not overlap with the
1542 shadow_contents buffer. */
1543 gdb_assert (target_info->shadow_contents >= readbuf + len
1544 || readbuf >= (target_info->shadow_contents
1545 + target_info->shadow_len));
1546
1547 /* Update the read buffer with this inserted breakpoint's
1548 shadow. */
1549 memcpy (readbuf + bp_addr - memaddr,
1550 target_info->shadow_contents + bptoffset, bp_size);
1551 }
1552 else
1553 {
1554 const unsigned char *bp;
1555 CORE_ADDR addr = target_info->reqstd_address;
1556 int placed_size;
1557
1558 /* Update the shadow with what we want to write to memory. */
1559 memcpy (target_info->shadow_contents + bptoffset,
1560 writebuf_org + bp_addr - memaddr, bp_size);
1561
1562 /* Determine appropriate breakpoint contents and size for this
1563 address. */
1564 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1565
1566 /* Update the final write buffer with this inserted
1567 breakpoint's INSN. */
1568 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1569 }
1570 }
1571
1572 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1573 by replacing any memory breakpoints with their shadowed contents.
1574
1575 If READBUF is not NULL, this buffer must not overlap with any of
1576 the breakpoint location's shadow_contents buffers. Otherwise,
1577 a failed assertion internal error will be raised.
1578
1579 The range of shadowed area by each bp_location is:
1580 bl->address - bp_locations_placed_address_before_address_max
1581 up to bl->address + bp_locations_shadow_len_after_address_max
1582 The range we were requested to resolve shadows for is:
1583 memaddr ... memaddr + len
1584 Thus the safe cutoff boundaries for performance optimization are
1585 memaddr + len <= (bl->address
1586 - bp_locations_placed_address_before_address_max)
1587 and:
1588 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1589
1590 void
1591 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1592 const gdb_byte *writebuf_org,
1593 ULONGEST memaddr, LONGEST len)
1594 {
1595 /* Left boundary, right boundary and median element of our binary
1596 search. */
1597 unsigned bc_l, bc_r, bc;
1598
1599 /* Find BC_L which is a leftmost element which may affect BUF
1600 content. It is safe to report lower value but a failure to
1601 report higher one. */
1602
1603 bc_l = 0;
1604 bc_r = bp_locations.size ();
1605 while (bc_l + 1 < bc_r)
1606 {
1607 struct bp_location *bl;
1608
1609 bc = (bc_l + bc_r) / 2;
1610 bl = bp_locations[bc];
1611
1612 /* Check first BL->ADDRESS will not overflow due to the added
1613 constant. Then advance the left boundary only if we are sure
1614 the BC element can in no way affect the BUF content (MEMADDR
1615 to MEMADDR + LEN range).
1616
1617 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1618 offset so that we cannot miss a breakpoint with its shadow
1619 range tail still reaching MEMADDR. */
1620
1621 if ((bl->address + bp_locations_shadow_len_after_address_max
1622 >= bl->address)
1623 && (bl->address + bp_locations_shadow_len_after_address_max
1624 <= memaddr))
1625 bc_l = bc;
1626 else
1627 bc_r = bc;
1628 }
1629
1630 /* Due to the binary search above, we need to make sure we pick the
1631 first location that's at BC_L's address. E.g., if there are
1632 multiple locations at the same address, BC_L may end up pointing
1633 at a duplicate location, and miss the "master"/"inserted"
1634 location. Say, given locations L1, L2 and L3 at addresses A and
1635 B:
1636
1637 L1@A, L2@A, L3@B, ...
1638
1639 BC_L could end up pointing at location L2, while the "master"
1640 location could be L1. Since the `loc->inserted' flag is only set
1641 on "master" locations, we'd forget to restore the shadow of L1
1642 and L2. */
1643 while (bc_l > 0
1644 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1645 bc_l--;
1646
1647 /* Now do full processing of the found relevant range of elements. */
1648
1649 for (bc = bc_l; bc < bp_locations.size (); bc++)
1650 {
1651 struct bp_location *bl = bp_locations[bc];
1652
1653 /* bp_location array has BL->OWNER always non-NULL. */
1654 if (bl->owner->type == bp_none)
1655 warning (_("reading through apparently deleted breakpoint #%d?"),
1656 bl->owner->number);
1657
1658 /* Performance optimization: any further element can no longer affect BUF
1659 content. */
1660
1661 if (bl->address >= bp_locations_placed_address_before_address_max
1662 && (memaddr + len
1663 <= (bl->address
1664 - bp_locations_placed_address_before_address_max)))
1665 break;
1666
1667 if (!bp_location_has_shadow (bl))
1668 continue;
1669
1670 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1671 memaddr, len, &bl->target_info, bl->gdbarch);
1672 }
1673 }
1674
1675 /* See breakpoint.h. */
1676
1677 bool
1678 is_breakpoint (const struct breakpoint *bpt)
1679 {
1680 return (bpt->type == bp_breakpoint
1681 || bpt->type == bp_hardware_breakpoint
1682 || bpt->type == bp_dprintf);
1683 }
1684
1685 /* Return true if BPT is of any hardware watchpoint kind. */
1686
1687 static bool
1688 is_hardware_watchpoint (const struct breakpoint *bpt)
1689 {
1690 return (bpt->type == bp_hardware_watchpoint
1691 || bpt->type == bp_read_watchpoint
1692 || bpt->type == bp_access_watchpoint);
1693 }
1694
1695 /* See breakpoint.h. */
1696
1697 bool
1698 is_watchpoint (const struct breakpoint *bpt)
1699 {
1700 return (is_hardware_watchpoint (bpt)
1701 || bpt->type == bp_watchpoint);
1702 }
1703
1704 /* Returns true if the current thread and its running state are safe
1705 to evaluate or update watchpoint B. Watchpoints on local
1706 expressions need to be evaluated in the context of the thread that
1707 was current when the watchpoint was created, and, that thread needs
1708 to be stopped to be able to select the correct frame context.
1709 Watchpoints on global expressions can be evaluated on any thread,
1710 and in any state. It is presently left to the target allowing
1711 memory accesses when threads are running. */
1712
1713 static int
1714 watchpoint_in_thread_scope (struct watchpoint *b)
1715 {
1716 return (b->pspace == current_program_space
1717 && (b->watchpoint_thread == null_ptid
1718 || (inferior_ptid == b->watchpoint_thread
1719 && !inferior_thread ()->executing ())));
1720 }
1721
1722 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1723 associated bp_watchpoint_scope breakpoint. */
1724
1725 static void
1726 watchpoint_del_at_next_stop (struct watchpoint *w)
1727 {
1728 if (w->related_breakpoint != w)
1729 {
1730 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1731 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1732 w->related_breakpoint->disposition = disp_del_at_next_stop;
1733 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1734 w->related_breakpoint = w;
1735 }
1736 w->disposition = disp_del_at_next_stop;
1737 }
1738
1739 /* Extract a bitfield value from value VAL using the bit parameters contained in
1740 watchpoint W. */
1741
1742 static struct value *
1743 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1744 {
1745 struct value *bit_val;
1746
1747 if (val == NULL)
1748 return NULL;
1749
1750 bit_val = allocate_value (value_type (val));
1751
1752 unpack_value_bitfield (bit_val,
1753 w->val_bitpos,
1754 w->val_bitsize,
1755 value_contents_for_printing (val).data (),
1756 value_offset (val),
1757 val);
1758
1759 return bit_val;
1760 }
1761
1762 /* Allocate a dummy location and add it to B, which must be a software
1763 watchpoint. This is required because even if a software watchpoint
1764 is not watching any memory, bpstat_stop_status requires a location
1765 to be able to report stops. */
1766
1767 static void
1768 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1769 struct program_space *pspace)
1770 {
1771 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1772
1773 b->loc = allocate_bp_location (b);
1774 b->loc->pspace = pspace;
1775 b->loc->address = -1;
1776 b->loc->length = -1;
1777 }
1778
1779 /* Returns true if B is a software watchpoint that is not watching any
1780 memory (e.g., "watch $pc"). */
1781
1782 static bool
1783 is_no_memory_software_watchpoint (struct breakpoint *b)
1784 {
1785 return (b->type == bp_watchpoint
1786 && b->loc != NULL
1787 && b->loc->next == NULL
1788 && b->loc->address == -1
1789 && b->loc->length == -1);
1790 }
1791
1792 /* Assuming that B is a watchpoint:
1793 - Reparse watchpoint expression, if REPARSE is non-zero
1794 - Evaluate expression and store the result in B->val
1795 - Evaluate the condition if there is one, and store the result
1796 in b->loc->cond.
1797 - Update the list of values that must be watched in B->loc.
1798
1799 If the watchpoint disposition is disp_del_at_next_stop, then do
1800 nothing. If this is local watchpoint that is out of scope, delete
1801 it.
1802
1803 Even with `set breakpoint always-inserted on' the watchpoints are
1804 removed + inserted on each stop here. Normal breakpoints must
1805 never be removed because they might be missed by a running thread
1806 when debugging in non-stop mode. On the other hand, hardware
1807 watchpoints (is_hardware_watchpoint; processed here) are specific
1808 to each LWP since they are stored in each LWP's hardware debug
1809 registers. Therefore, such LWP must be stopped first in order to
1810 be able to modify its hardware watchpoints.
1811
1812 Hardware watchpoints must be reset exactly once after being
1813 presented to the user. It cannot be done sooner, because it would
1814 reset the data used to present the watchpoint hit to the user. And
1815 it must not be done later because it could display the same single
1816 watchpoint hit during multiple GDB stops. Note that the latter is
1817 relevant only to the hardware watchpoint types bp_read_watchpoint
1818 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1819 not user-visible - its hit is suppressed if the memory content has
1820 not changed.
1821
1822 The following constraints influence the location where we can reset
1823 hardware watchpoints:
1824
1825 * target_stopped_by_watchpoint and target_stopped_data_address are
1826 called several times when GDB stops.
1827
1828 [linux]
1829 * Multiple hardware watchpoints can be hit at the same time,
1830 causing GDB to stop. GDB only presents one hardware watchpoint
1831 hit at a time as the reason for stopping, and all the other hits
1832 are presented later, one after the other, each time the user
1833 requests the execution to be resumed. Execution is not resumed
1834 for the threads still having pending hit event stored in
1835 LWP_INFO->STATUS. While the watchpoint is already removed from
1836 the inferior on the first stop the thread hit event is kept being
1837 reported from its cached value by linux_nat_stopped_data_address
1838 until the real thread resume happens after the watchpoint gets
1839 presented and thus its LWP_INFO->STATUS gets reset.
1840
1841 Therefore the hardware watchpoint hit can get safely reset on the
1842 watchpoint removal from inferior. */
1843
1844 static void
1845 update_watchpoint (struct watchpoint *b, int reparse)
1846 {
1847 int within_current_scope;
1848 struct frame_id saved_frame_id;
1849 int frame_saved;
1850
1851 /* If this is a local watchpoint, we only want to check if the
1852 watchpoint frame is in scope if the current thread is the thread
1853 that was used to create the watchpoint. */
1854 if (!watchpoint_in_thread_scope (b))
1855 return;
1856
1857 if (b->disposition == disp_del_at_next_stop)
1858 return;
1859
1860 frame_saved = 0;
1861
1862 /* Determine if the watchpoint is within scope. */
1863 if (b->exp_valid_block == NULL)
1864 within_current_scope = 1;
1865 else
1866 {
1867 struct frame_info *fi = get_current_frame ();
1868 struct gdbarch *frame_arch = get_frame_arch (fi);
1869 CORE_ADDR frame_pc = get_frame_pc (fi);
1870
1871 /* If we're at a point where the stack has been destroyed
1872 (e.g. in a function epilogue), unwinding may not work
1873 properly. Do not attempt to recreate locations at this
1874 point. See similar comments in watchpoint_check. */
1875 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1876 return;
1877
1878 /* Save the current frame's ID so we can restore it after
1879 evaluating the watchpoint expression on its own frame. */
1880 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1881 took a frame parameter, so that we didn't have to change the
1882 selected frame. */
1883 frame_saved = 1;
1884 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1885
1886 fi = frame_find_by_id (b->watchpoint_frame);
1887 within_current_scope = (fi != NULL);
1888 if (within_current_scope)
1889 select_frame (fi);
1890 }
1891
1892 /* We don't free locations. They are stored in the bp_location array
1893 and update_global_location_list will eventually delete them and
1894 remove breakpoints if needed. */
1895 b->loc = NULL;
1896
1897 if (within_current_scope && reparse)
1898 {
1899 const char *s;
1900
1901 b->exp.reset ();
1902 s = (b->exp_string_reparse
1903 ? b->exp_string_reparse.get ()
1904 : b->exp_string.get ());
1905 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1906 /* If the meaning of expression itself changed, the old value is
1907 no longer relevant. We don't want to report a watchpoint hit
1908 to the user when the old value and the new value may actually
1909 be completely different objects. */
1910 b->val = NULL;
1911 b->val_valid = false;
1912
1913 /* Note that unlike with breakpoints, the watchpoint's condition
1914 expression is stored in the breakpoint object, not in the
1915 locations (re)created below. */
1916 if (b->cond_string != NULL)
1917 {
1918 b->cond_exp.reset ();
1919
1920 s = b->cond_string.get ();
1921 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1922 }
1923 }
1924
1925 /* If we failed to parse the expression, for example because
1926 it refers to a global variable in a not-yet-loaded shared library,
1927 don't try to insert watchpoint. We don't automatically delete
1928 such watchpoint, though, since failure to parse expression
1929 is different from out-of-scope watchpoint. */
1930 if (!target_has_execution ())
1931 {
1932 /* Without execution, memory can't change. No use to try and
1933 set watchpoint locations. The watchpoint will be reset when
1934 the target gains execution, through breakpoint_re_set. */
1935 if (!can_use_hw_watchpoints)
1936 {
1937 if (b->ops->works_in_software_mode (b))
1938 b->type = bp_watchpoint;
1939 else
1940 error (_("Can't set read/access watchpoint when "
1941 "hardware watchpoints are disabled."));
1942 }
1943 }
1944 else if (within_current_scope && b->exp)
1945 {
1946 std::vector<value_ref_ptr> val_chain;
1947 struct value *v, *result;
1948 struct program_space *frame_pspace;
1949
1950 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
1951 &val_chain, false);
1952
1953 /* Avoid setting b->val if it's already set. The meaning of
1954 b->val is 'the last value' user saw, and we should update
1955 it only if we reported that last value to user. As it
1956 happens, the code that reports it updates b->val directly.
1957 We don't keep track of the memory value for masked
1958 watchpoints. */
1959 if (!b->val_valid && !is_masked_watchpoint (b))
1960 {
1961 if (b->val_bitsize != 0)
1962 v = extract_bitfield_from_watchpoint_value (b, v);
1963 b->val = release_value (v);
1964 b->val_valid = true;
1965 }
1966
1967 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1968
1969 /* Look at each value on the value chain. */
1970 gdb_assert (!val_chain.empty ());
1971 for (const value_ref_ptr &iter : val_chain)
1972 {
1973 v = iter.get ();
1974
1975 /* If it's a memory location, and GDB actually needed
1976 its contents to evaluate the expression, then we
1977 must watch it. If the first value returned is
1978 still lazy, that means an error occurred reading it;
1979 watch it anyway in case it becomes readable. */
1980 if (VALUE_LVAL (v) == lval_memory
1981 && (v == val_chain[0] || ! value_lazy (v)))
1982 {
1983 struct type *vtype = check_typedef (value_type (v));
1984
1985 /* We only watch structs and arrays if user asked
1986 for it explicitly, never if they just happen to
1987 appear in the middle of some value chain. */
1988 if (v == result
1989 || (vtype->code () != TYPE_CODE_STRUCT
1990 && vtype->code () != TYPE_CODE_ARRAY))
1991 {
1992 CORE_ADDR addr;
1993 enum target_hw_bp_type type;
1994 struct bp_location *loc, **tmp;
1995 int bitpos = 0, bitsize = 0;
1996
1997 if (value_bitsize (v) != 0)
1998 {
1999 /* Extract the bit parameters out from the bitfield
2000 sub-expression. */
2001 bitpos = value_bitpos (v);
2002 bitsize = value_bitsize (v);
2003 }
2004 else if (v == result && b->val_bitsize != 0)
2005 {
2006 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2007 lvalue whose bit parameters are saved in the fields
2008 VAL_BITPOS and VAL_BITSIZE. */
2009 bitpos = b->val_bitpos;
2010 bitsize = b->val_bitsize;
2011 }
2012
2013 addr = value_address (v);
2014 if (bitsize != 0)
2015 {
2016 /* Skip the bytes that don't contain the bitfield. */
2017 addr += bitpos / 8;
2018 }
2019
2020 type = hw_write;
2021 if (b->type == bp_read_watchpoint)
2022 type = hw_read;
2023 else if (b->type == bp_access_watchpoint)
2024 type = hw_access;
2025
2026 loc = allocate_bp_location (b);
2027 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
2028 ;
2029 *tmp = loc;
2030 loc->gdbarch = value_type (v)->arch ();
2031
2032 loc->pspace = frame_pspace;
2033 loc->address = address_significant (loc->gdbarch, addr);
2034
2035 if (bitsize != 0)
2036 {
2037 /* Just cover the bytes that make up the bitfield. */
2038 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2039 }
2040 else
2041 loc->length = TYPE_LENGTH (value_type (v));
2042
2043 loc->watchpoint_type = type;
2044 }
2045 }
2046 }
2047
2048 /* Change the type of breakpoint between hardware assisted or
2049 an ordinary watchpoint depending on the hardware support
2050 and free hardware slots. REPARSE is set when the inferior
2051 is started. */
2052 if (reparse)
2053 {
2054 int reg_cnt;
2055 enum bp_loc_type loc_type;
2056
2057 reg_cnt = can_use_hardware_watchpoint (val_chain);
2058
2059 if (reg_cnt)
2060 {
2061 int i, target_resources_ok, other_type_used;
2062 enum bptype type;
2063
2064 /* Use an exact watchpoint when there's only one memory region to be
2065 watched, and only one debug register is needed to watch it. */
2066 b->exact = target_exact_watchpoints && reg_cnt == 1;
2067
2068 /* We need to determine how many resources are already
2069 used for all other hardware watchpoints plus this one
2070 to see if we still have enough resources to also fit
2071 this watchpoint in as well. */
2072
2073 /* If this is a software watchpoint, we try to turn it
2074 to a hardware one -- count resources as if B was of
2075 hardware watchpoint type. */
2076 type = b->type;
2077 if (type == bp_watchpoint)
2078 type = bp_hardware_watchpoint;
2079
2080 /* This watchpoint may or may not have been placed on
2081 the list yet at this point (it won't be in the list
2082 if we're trying to create it for the first time,
2083 through watch_command), so always account for it
2084 manually. */
2085
2086 /* Count resources used by all watchpoints except B. */
2087 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
2088
2089 /* Add in the resources needed for B. */
2090 i += hw_watchpoint_use_count (b);
2091
2092 target_resources_ok
2093 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2094 if (target_resources_ok <= 0)
2095 {
2096 int sw_mode = b->ops->works_in_software_mode (b);
2097
2098 if (target_resources_ok == 0 && !sw_mode)
2099 error (_("Target does not support this type of "
2100 "hardware watchpoint."));
2101 else if (target_resources_ok < 0 && !sw_mode)
2102 error (_("There are not enough available hardware "
2103 "resources for this watchpoint."));
2104
2105 /* Downgrade to software watchpoint. */
2106 b->type = bp_watchpoint;
2107 }
2108 else
2109 {
2110 /* If this was a software watchpoint, we've just
2111 found we have enough resources to turn it to a
2112 hardware watchpoint. Otherwise, this is a
2113 nop. */
2114 b->type = type;
2115 }
2116 }
2117 else if (!b->ops->works_in_software_mode (b))
2118 {
2119 if (!can_use_hw_watchpoints)
2120 error (_("Can't set read/access watchpoint when "
2121 "hardware watchpoints are disabled."));
2122 else
2123 error (_("Expression cannot be implemented with "
2124 "read/access watchpoint."));
2125 }
2126 else
2127 b->type = bp_watchpoint;
2128
2129 loc_type = (b->type == bp_watchpoint? bp_loc_other
2130 : bp_loc_hardware_watchpoint);
2131 for (bp_location *bl : b->locations ())
2132 bl->loc_type = loc_type;
2133 }
2134
2135 /* If a software watchpoint is not watching any memory, then the
2136 above left it without any location set up. But,
2137 bpstat_stop_status requires a location to be able to report
2138 stops, so make sure there's at least a dummy one. */
2139 if (b->type == bp_watchpoint && b->loc == NULL)
2140 software_watchpoint_add_no_memory_location (b, frame_pspace);
2141 }
2142 else if (!within_current_scope)
2143 {
2144 gdb_printf (_("\
2145 Watchpoint %d deleted because the program has left the block\n\
2146 in which its expression is valid.\n"),
2147 b->number);
2148 watchpoint_del_at_next_stop (b);
2149 }
2150
2151 /* Restore the selected frame. */
2152 if (frame_saved)
2153 select_frame (frame_find_by_id (saved_frame_id));
2154 }
2155
2156
2157 /* Returns 1 iff breakpoint location should be
2158 inserted in the inferior. We don't differentiate the type of BL's owner
2159 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2160 breakpoint_ops is not defined, because in insert_bp_location,
2161 tracepoint's insert_location will not be called. */
2162 static int
2163 should_be_inserted (struct bp_location *bl)
2164 {
2165 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2166 return 0;
2167
2168 if (bl->owner->disposition == disp_del_at_next_stop)
2169 return 0;
2170
2171 if (!bl->enabled || bl->disabled_by_cond
2172 || bl->shlib_disabled || bl->duplicate)
2173 return 0;
2174
2175 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2176 return 0;
2177
2178 /* This is set for example, when we're attached to the parent of a
2179 vfork, and have detached from the child. The child is running
2180 free, and we expect it to do an exec or exit, at which point the
2181 OS makes the parent schedulable again (and the target reports
2182 that the vfork is done). Until the child is done with the shared
2183 memory region, do not insert breakpoints in the parent, otherwise
2184 the child could still trip on the parent's breakpoints. Since
2185 the parent is blocked anyway, it won't miss any breakpoint. */
2186 if (bl->pspace->breakpoints_not_allowed)
2187 return 0;
2188
2189 /* Don't insert a breakpoint if we're trying to step past its
2190 location, except if the breakpoint is a single-step breakpoint,
2191 and the breakpoint's thread is the thread which is stepping past
2192 a breakpoint. */
2193 if ((bl->loc_type == bp_loc_software_breakpoint
2194 || bl->loc_type == bp_loc_hardware_breakpoint)
2195 && stepping_past_instruction_at (bl->pspace->aspace,
2196 bl->address)
2197 /* The single-step breakpoint may be inserted at the location
2198 we're trying to step if the instruction branches to itself.
2199 However, the instruction won't be executed at all and it may
2200 break the semantics of the instruction, for example, the
2201 instruction is a conditional branch or updates some flags.
2202 We can't fix it unless GDB is able to emulate the instruction
2203 or switch to displaced stepping. */
2204 && !(bl->owner->type == bp_single_step
2205 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2206 {
2207 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2208 paddress (bl->gdbarch, bl->address));
2209 return 0;
2210 }
2211
2212 /* Don't insert watchpoints if we're trying to step past the
2213 instruction that triggered one. */
2214 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2215 && stepping_past_nonsteppable_watchpoint ())
2216 {
2217 infrun_debug_printf ("stepping past non-steppable watchpoint. "
2218 "skipping watchpoint at %s:%d",
2219 paddress (bl->gdbarch, bl->address), bl->length);
2220 return 0;
2221 }
2222
2223 return 1;
2224 }
2225
2226 /* Same as should_be_inserted but does the check assuming
2227 that the location is not duplicated. */
2228
2229 static int
2230 unduplicated_should_be_inserted (struct bp_location *bl)
2231 {
2232 int result;
2233 const int save_duplicate = bl->duplicate;
2234
2235 bl->duplicate = 0;
2236 result = should_be_inserted (bl);
2237 bl->duplicate = save_duplicate;
2238 return result;
2239 }
2240
2241 /* Parses a conditional described by an expression COND into an
2242 agent expression bytecode suitable for evaluation
2243 by the bytecode interpreter. Return NULL if there was
2244 any error during parsing. */
2245
2246 static agent_expr_up
2247 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2248 {
2249 if (cond == NULL)
2250 return NULL;
2251
2252 agent_expr_up aexpr;
2253
2254 /* We don't want to stop processing, so catch any errors
2255 that may show up. */
2256 try
2257 {
2258 aexpr = gen_eval_for_expr (scope, cond);
2259 }
2260
2261 catch (const gdb_exception_error &ex)
2262 {
2263 /* If we got here, it means the condition could not be parsed to a valid
2264 bytecode expression and thus can't be evaluated on the target's side.
2265 It's no use iterating through the conditions. */
2266 }
2267
2268 /* We have a valid agent expression. */
2269 return aexpr;
2270 }
2271
2272 /* Based on location BL, create a list of breakpoint conditions to be
2273 passed on to the target. If we have duplicated locations with different
2274 conditions, we will add such conditions to the list. The idea is that the
2275 target will evaluate the list of conditions and will only notify GDB when
2276 one of them is true. */
2277
2278 static void
2279 build_target_condition_list (struct bp_location *bl)
2280 {
2281 int null_condition_or_parse_error = 0;
2282 int modified = bl->needs_update;
2283
2284 /* Release conditions left over from a previous insert. */
2285 bl->target_info.conditions.clear ();
2286
2287 /* This is only meaningful if the target is
2288 evaluating conditions and if the user has
2289 opted for condition evaluation on the target's
2290 side. */
2291 if (gdb_evaluates_breakpoint_condition_p ()
2292 || !target_supports_evaluation_of_breakpoint_conditions ())
2293 return;
2294
2295 auto loc_range = all_bp_locations_at_addr (bl->address);
2296
2297 /* Do a first pass to check for locations with no assigned
2298 conditions or conditions that fail to parse to a valid agent
2299 expression bytecode. If any of these happen, then it's no use to
2300 send conditions to the target since this location will always
2301 trigger and generate a response back to GDB. Note we consider
2302 all locations at the same address irrespective of type, i.e.,
2303 even if the locations aren't considered duplicates (e.g.,
2304 software breakpoint and hardware breakpoint at the same
2305 address). */
2306 for (bp_location *loc : loc_range)
2307 {
2308 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2309 {
2310 if (modified)
2311 {
2312 /* Re-parse the conditions since something changed. In that
2313 case we already freed the condition bytecodes (see
2314 force_breakpoint_reinsertion). We just
2315 need to parse the condition to bytecodes again. */
2316 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2317 loc->cond.get ());
2318 }
2319
2320 /* If we have a NULL bytecode expression, it means something
2321 went wrong or we have a null condition expression. */
2322 if (!loc->cond_bytecode)
2323 {
2324 null_condition_or_parse_error = 1;
2325 break;
2326 }
2327 }
2328 }
2329
2330 /* If any of these happened, it means we will have to evaluate the conditions
2331 for the location's address on gdb's side. It is no use keeping bytecodes
2332 for all the other duplicate locations, thus we free all of them here.
2333
2334 This is so we have a finer control over which locations' conditions are
2335 being evaluated by GDB or the remote stub. */
2336 if (null_condition_or_parse_error)
2337 {
2338 for (bp_location *loc : loc_range)
2339 {
2340 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2341 {
2342 /* Only go as far as the first NULL bytecode is
2343 located. */
2344 if (!loc->cond_bytecode)
2345 return;
2346
2347 loc->cond_bytecode.reset ();
2348 }
2349 }
2350 }
2351
2352 /* No NULL conditions or failed bytecode generation. Build a
2353 condition list for this location's address. If we have software
2354 and hardware locations at the same address, they aren't
2355 considered duplicates, but we still marge all the conditions
2356 anyway, as it's simpler, and doesn't really make a practical
2357 difference. */
2358 for (bp_location *loc : loc_range)
2359 if (loc->cond
2360 && is_breakpoint (loc->owner)
2361 && loc->pspace->num == bl->pspace->num
2362 && loc->owner->enable_state == bp_enabled
2363 && loc->enabled
2364 && !loc->disabled_by_cond)
2365 {
2366 /* Add the condition to the vector. This will be used later
2367 to send the conditions to the target. */
2368 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2369 }
2370
2371 return;
2372 }
2373
2374 /* Parses a command described by string CMD into an agent expression
2375 bytecode suitable for evaluation by the bytecode interpreter.
2376 Return NULL if there was any error during parsing. */
2377
2378 static agent_expr_up
2379 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2380 {
2381 const char *cmdrest;
2382 const char *format_start, *format_end;
2383 struct gdbarch *gdbarch = get_current_arch ();
2384
2385 if (cmd == NULL)
2386 return NULL;
2387
2388 cmdrest = cmd;
2389
2390 if (*cmdrest == ',')
2391 ++cmdrest;
2392 cmdrest = skip_spaces (cmdrest);
2393
2394 if (*cmdrest++ != '"')
2395 error (_("No format string following the location"));
2396
2397 format_start = cmdrest;
2398
2399 format_pieces fpieces (&cmdrest);
2400
2401 format_end = cmdrest;
2402
2403 if (*cmdrest++ != '"')
2404 error (_("Bad format string, non-terminated '\"'."));
2405
2406 cmdrest = skip_spaces (cmdrest);
2407
2408 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2409 error (_("Invalid argument syntax"));
2410
2411 if (*cmdrest == ',')
2412 cmdrest++;
2413 cmdrest = skip_spaces (cmdrest);
2414
2415 /* For each argument, make an expression. */
2416
2417 std::vector<struct expression *> argvec;
2418 while (*cmdrest != '\0')
2419 {
2420 const char *cmd1;
2421
2422 cmd1 = cmdrest;
2423 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2424 argvec.push_back (expr.release ());
2425 cmdrest = cmd1;
2426 if (*cmdrest == ',')
2427 ++cmdrest;
2428 }
2429
2430 agent_expr_up aexpr;
2431
2432 /* We don't want to stop processing, so catch any errors
2433 that may show up. */
2434 try
2435 {
2436 aexpr = gen_printf (scope, gdbarch, 0, 0,
2437 format_start, format_end - format_start,
2438 argvec.size (), argvec.data ());
2439 }
2440 catch (const gdb_exception_error &ex)
2441 {
2442 /* If we got here, it means the command could not be parsed to a valid
2443 bytecode expression and thus can't be evaluated on the target's side.
2444 It's no use iterating through the other commands. */
2445 }
2446
2447 /* We have a valid agent expression, return it. */
2448 return aexpr;
2449 }
2450
2451 /* Based on location BL, create a list of breakpoint commands to be
2452 passed on to the target. If we have duplicated locations with
2453 different commands, we will add any such to the list. */
2454
2455 static void
2456 build_target_command_list (struct bp_location *bl)
2457 {
2458 int null_command_or_parse_error = 0;
2459 int modified = bl->needs_update;
2460
2461 /* Clear commands left over from a previous insert. */
2462 bl->target_info.tcommands.clear ();
2463
2464 if (!target_can_run_breakpoint_commands ())
2465 return;
2466
2467 /* For now, limit to agent-style dprintf breakpoints. */
2468 if (dprintf_style != dprintf_style_agent)
2469 return;
2470
2471 auto loc_range = all_bp_locations_at_addr (bl->address);
2472
2473 /* For now, if we have any location at the same address that isn't a
2474 dprintf, don't install the target-side commands, as that would
2475 make the breakpoint not be reported to the core, and we'd lose
2476 control. */
2477 for (bp_location *loc : loc_range)
2478 if (is_breakpoint (loc->owner)
2479 && loc->pspace->num == bl->pspace->num
2480 && loc->owner->type != bp_dprintf)
2481 return;
2482
2483 /* Do a first pass to check for locations with no assigned
2484 conditions or conditions that fail to parse to a valid agent expression
2485 bytecode. If any of these happen, then it's no use to send conditions
2486 to the target since this location will always trigger and generate a
2487 response back to GDB. */
2488 for (bp_location *loc : loc_range)
2489 {
2490 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2491 {
2492 if (modified)
2493 {
2494 /* Re-parse the commands since something changed. In that
2495 case we already freed the command bytecodes (see
2496 force_breakpoint_reinsertion). We just
2497 need to parse the command to bytecodes again. */
2498 loc->cmd_bytecode
2499 = parse_cmd_to_aexpr (bl->address,
2500 loc->owner->extra_string.get ());
2501 }
2502
2503 /* If we have a NULL bytecode expression, it means something
2504 went wrong or we have a null command expression. */
2505 if (!loc->cmd_bytecode)
2506 {
2507 null_command_or_parse_error = 1;
2508 break;
2509 }
2510 }
2511 }
2512
2513 /* If anything failed, then we're not doing target-side commands,
2514 and so clean up. */
2515 if (null_command_or_parse_error)
2516 {
2517 for (bp_location *loc : loc_range)
2518 if (is_breakpoint (loc->owner)
2519 && loc->pspace->num == bl->pspace->num)
2520 {
2521 /* Only go as far as the first NULL bytecode is
2522 located. */
2523 if (loc->cmd_bytecode == NULL)
2524 return;
2525
2526 loc->cmd_bytecode.reset ();
2527 }
2528 }
2529
2530 /* No NULL commands or failed bytecode generation. Build a command
2531 list for all duplicate locations at this location's address.
2532 Note that here we must care for whether the breakpoint location
2533 types are considered duplicates, otherwise, say, if we have a
2534 software and hardware location at the same address, the target
2535 could end up running the commands twice. For the moment, we only
2536 support targets-side commands with dprintf, but it doesn't hurt
2537 to be pedantically correct in case that changes. */
2538 for (bp_location *loc : loc_range)
2539 if (breakpoint_locations_match (bl, loc)
2540 && loc->owner->extra_string
2541 && is_breakpoint (loc->owner)
2542 && loc->pspace->num == bl->pspace->num
2543 && loc->owner->enable_state == bp_enabled
2544 && loc->enabled
2545 && !loc->disabled_by_cond)
2546 {
2547 /* Add the command to the vector. This will be used later
2548 to send the commands to the target. */
2549 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2550 }
2551
2552 bl->target_info.persist = 0;
2553 /* Maybe flag this location as persistent. */
2554 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2555 bl->target_info.persist = 1;
2556 }
2557
2558 /* Return the kind of breakpoint on address *ADDR. Get the kind
2559 of breakpoint according to ADDR except single-step breakpoint.
2560 Get the kind of single-step breakpoint according to the current
2561 registers state. */
2562
2563 static int
2564 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2565 {
2566 if (bl->owner->type == bp_single_step)
2567 {
2568 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2569 struct regcache *regcache;
2570
2571 regcache = get_thread_regcache (thr);
2572
2573 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2574 regcache, addr);
2575 }
2576 else
2577 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2578 }
2579
2580 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2581 location. Any error messages are printed to TMP_ERROR_STREAM; and
2582 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2583 Returns 0 for success, 1 if the bp_location type is not supported or
2584 -1 for failure.
2585
2586 NOTE drow/2003-09-09: This routine could be broken down to an
2587 object-style method for each breakpoint or catchpoint type. */
2588 static int
2589 insert_bp_location (struct bp_location *bl,
2590 struct ui_file *tmp_error_stream,
2591 int *disabled_breaks,
2592 int *hw_breakpoint_error,
2593 int *hw_bp_error_explained_already)
2594 {
2595 gdb_exception bp_excpt;
2596
2597 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2598 return 0;
2599
2600 /* Note we don't initialize bl->target_info, as that wipes out
2601 the breakpoint location's shadow_contents if the breakpoint
2602 is still inserted at that location. This in turn breaks
2603 target_read_memory which depends on these buffers when
2604 a memory read is requested at the breakpoint location:
2605 Once the target_info has been wiped, we fail to see that
2606 we have a breakpoint inserted at that address and thus
2607 read the breakpoint instead of returning the data saved in
2608 the breakpoint location's shadow contents. */
2609 bl->target_info.reqstd_address = bl->address;
2610 bl->target_info.placed_address_space = bl->pspace->aspace;
2611 bl->target_info.length = bl->length;
2612
2613 /* When working with target-side conditions, we must pass all the conditions
2614 for the same breakpoint address down to the target since GDB will not
2615 insert those locations. With a list of breakpoint conditions, the target
2616 can decide when to stop and notify GDB. */
2617
2618 if (is_breakpoint (bl->owner))
2619 {
2620 build_target_condition_list (bl);
2621 build_target_command_list (bl);
2622 /* Reset the modification marker. */
2623 bl->needs_update = 0;
2624 }
2625
2626 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2627 set at a read-only address, then a breakpoint location will have
2628 been changed to hardware breakpoint before we get here. If it is
2629 "off" however, error out before actually trying to insert the
2630 breakpoint, with a nicer error message. */
2631 if (bl->loc_type == bp_loc_software_breakpoint
2632 && !automatic_hardware_breakpoints)
2633 {
2634 mem_region *mr = lookup_mem_region (bl->address);
2635
2636 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2637 {
2638 gdb_printf (tmp_error_stream,
2639 _("Cannot insert breakpoint %d.\n"
2640 "Cannot set software breakpoint "
2641 "at read-only address %s\n"),
2642 bl->owner->number,
2643 paddress (bl->gdbarch, bl->address));
2644 return 1;
2645 }
2646 }
2647
2648 if (bl->loc_type == bp_loc_software_breakpoint
2649 || bl->loc_type == bp_loc_hardware_breakpoint)
2650 {
2651 /* First check to see if we have to handle an overlay. */
2652 if (overlay_debugging == ovly_off
2653 || bl->section == NULL
2654 || !(section_is_overlay (bl->section)))
2655 {
2656 /* No overlay handling: just set the breakpoint. */
2657 try
2658 {
2659 int val;
2660
2661 val = bl->owner->ops->insert_location (bl);
2662 if (val)
2663 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2664 }
2665 catch (gdb_exception &e)
2666 {
2667 bp_excpt = std::move (e);
2668 }
2669 }
2670 else
2671 {
2672 /* This breakpoint is in an overlay section.
2673 Shall we set a breakpoint at the LMA? */
2674 if (!overlay_events_enabled)
2675 {
2676 /* Yes -- overlay event support is not active,
2677 so we must try to set a breakpoint at the LMA.
2678 This will not work for a hardware breakpoint. */
2679 if (bl->loc_type == bp_loc_hardware_breakpoint)
2680 warning (_("hardware breakpoint %d not supported in overlay!"),
2681 bl->owner->number);
2682 else
2683 {
2684 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2685 bl->section);
2686 /* Set a software (trap) breakpoint at the LMA. */
2687 bl->overlay_target_info = bl->target_info;
2688 bl->overlay_target_info.reqstd_address = addr;
2689
2690 /* No overlay handling: just set the breakpoint. */
2691 try
2692 {
2693 int val;
2694
2695 bl->overlay_target_info.kind
2696 = breakpoint_kind (bl, &addr);
2697 bl->overlay_target_info.placed_address = addr;
2698 val = target_insert_breakpoint (bl->gdbarch,
2699 &bl->overlay_target_info);
2700 if (val)
2701 bp_excpt
2702 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2703 }
2704 catch (gdb_exception &e)
2705 {
2706 bp_excpt = std::move (e);
2707 }
2708
2709 if (bp_excpt.reason != 0)
2710 gdb_printf (tmp_error_stream,
2711 "Overlay breakpoint %d "
2712 "failed: in ROM?\n",
2713 bl->owner->number);
2714 }
2715 }
2716 /* Shall we set a breakpoint at the VMA? */
2717 if (section_is_mapped (bl->section))
2718 {
2719 /* Yes. This overlay section is mapped into memory. */
2720 try
2721 {
2722 int val;
2723
2724 val = bl->owner->ops->insert_location (bl);
2725 if (val)
2726 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
2727 }
2728 catch (gdb_exception &e)
2729 {
2730 bp_excpt = std::move (e);
2731 }
2732 }
2733 else
2734 {
2735 /* No. This breakpoint will not be inserted.
2736 No error, but do not mark the bp as 'inserted'. */
2737 return 0;
2738 }
2739 }
2740
2741 if (bp_excpt.reason != 0)
2742 {
2743 /* Can't set the breakpoint. */
2744
2745 /* If the target has closed then it will have deleted any
2746 breakpoints inserted within the target inferior, as a result
2747 any further attempts to interact with the breakpoint objects
2748 is not possible. Just rethrow the error. */
2749 if (bp_excpt.error == TARGET_CLOSE_ERROR)
2750 throw bp_excpt;
2751 gdb_assert (bl->owner != nullptr);
2752
2753 /* In some cases, we might not be able to insert a
2754 breakpoint in a shared library that has already been
2755 removed, but we have not yet processed the shlib unload
2756 event. Unfortunately, some targets that implement
2757 breakpoint insertion themselves can't tell why the
2758 breakpoint insertion failed (e.g., the remote target
2759 doesn't define error codes), so we must treat generic
2760 errors as memory errors. */
2761 if (bp_excpt.reason == RETURN_ERROR
2762 && (bp_excpt.error == GENERIC_ERROR
2763 || bp_excpt.error == MEMORY_ERROR)
2764 && bl->loc_type == bp_loc_software_breakpoint
2765 && (solib_name_from_address (bl->pspace, bl->address)
2766 || shared_objfile_contains_address_p (bl->pspace,
2767 bl->address)))
2768 {
2769 /* See also: disable_breakpoints_in_shlibs. */
2770 bl->shlib_disabled = 1;
2771 gdb::observers::breakpoint_modified.notify (bl->owner);
2772 if (!*disabled_breaks)
2773 {
2774 gdb_printf (tmp_error_stream,
2775 "Cannot insert breakpoint %d.\n",
2776 bl->owner->number);
2777 gdb_printf (tmp_error_stream,
2778 "Temporarily disabling shared "
2779 "library breakpoints:\n");
2780 }
2781 *disabled_breaks = 1;
2782 gdb_printf (tmp_error_stream,
2783 "breakpoint #%d\n", bl->owner->number);
2784 return 0;
2785 }
2786 else
2787 {
2788 if (bl->loc_type == bp_loc_hardware_breakpoint)
2789 {
2790 *hw_breakpoint_error = 1;
2791 *hw_bp_error_explained_already = bp_excpt.message != NULL;
2792 gdb_printf (tmp_error_stream,
2793 "Cannot insert hardware breakpoint %d%s",
2794 bl->owner->number,
2795 bp_excpt.message ? ":" : ".\n");
2796 if (bp_excpt.message != NULL)
2797 gdb_printf (tmp_error_stream, "%s.\n",
2798 bp_excpt.what ());
2799 }
2800 else
2801 {
2802 if (bp_excpt.message == NULL)
2803 {
2804 std::string message
2805 = memory_error_message (TARGET_XFER_E_IO,
2806 bl->gdbarch, bl->address);
2807
2808 gdb_printf (tmp_error_stream,
2809 "Cannot insert breakpoint %d.\n"
2810 "%s\n",
2811 bl->owner->number, message.c_str ());
2812 }
2813 else
2814 {
2815 gdb_printf (tmp_error_stream,
2816 "Cannot insert breakpoint %d: %s\n",
2817 bl->owner->number,
2818 bp_excpt.what ());
2819 }
2820 }
2821 return 1;
2822
2823 }
2824 }
2825 else
2826 bl->inserted = 1;
2827
2828 return 0;
2829 }
2830
2831 else if (bl->loc_type == bp_loc_hardware_watchpoint
2832 /* NOTE drow/2003-09-08: This state only exists for removing
2833 watchpoints. It's not clear that it's necessary... */
2834 && bl->owner->disposition != disp_del_at_next_stop)
2835 {
2836 int val;
2837
2838 gdb_assert (bl->owner->ops != NULL
2839 && bl->owner->ops->insert_location != NULL);
2840
2841 val = bl->owner->ops->insert_location (bl);
2842
2843 /* If trying to set a read-watchpoint, and it turns out it's not
2844 supported, try emulating one with an access watchpoint. */
2845 if (val == 1 && bl->watchpoint_type == hw_read)
2846 {
2847 /* But don't try to insert it, if there's already another
2848 hw_access location that would be considered a duplicate
2849 of this one. */
2850 for (bp_location *loc : all_bp_locations ())
2851 if (loc != bl
2852 && loc->watchpoint_type == hw_access
2853 && watchpoint_locations_match (bl, loc))
2854 {
2855 bl->duplicate = 1;
2856 bl->inserted = 1;
2857 bl->target_info = loc->target_info;
2858 bl->watchpoint_type = hw_access;
2859 val = 0;
2860 break;
2861 }
2862
2863 if (val == 1)
2864 {
2865 bl->watchpoint_type = hw_access;
2866 val = bl->owner->ops->insert_location (bl);
2867
2868 if (val)
2869 /* Back to the original value. */
2870 bl->watchpoint_type = hw_read;
2871 }
2872 }
2873
2874 bl->inserted = (val == 0);
2875 }
2876
2877 else if (bl->owner->type == bp_catchpoint)
2878 {
2879 int val;
2880
2881 gdb_assert (bl->owner->ops != NULL
2882 && bl->owner->ops->insert_location != NULL);
2883
2884 val = bl->owner->ops->insert_location (bl);
2885 if (val)
2886 {
2887 bl->owner->enable_state = bp_disabled;
2888
2889 if (val == 1)
2890 warning (_("\
2891 Error inserting catchpoint %d: Your system does not support this type\n\
2892 of catchpoint."), bl->owner->number);
2893 else
2894 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2895 }
2896
2897 bl->inserted = (val == 0);
2898
2899 /* We've already printed an error message if there was a problem
2900 inserting this catchpoint, and we've disabled the catchpoint,
2901 so just return success. */
2902 return 0;
2903 }
2904
2905 return 0;
2906 }
2907
2908 /* This function is called when program space PSPACE is about to be
2909 deleted. It takes care of updating breakpoints to not reference
2910 PSPACE anymore. */
2911
2912 void
2913 breakpoint_program_space_exit (struct program_space *pspace)
2914 {
2915 /* Remove any breakpoint that was set through this program space. */
2916 for (breakpoint *b : all_breakpoints_safe ())
2917 if (b->pspace == pspace)
2918 delete_breakpoint (b);
2919
2920 /* Breakpoints set through other program spaces could have locations
2921 bound to PSPACE as well. Remove those. */
2922 for (bp_location *loc : all_bp_locations ())
2923 {
2924 struct bp_location *tmp;
2925
2926 if (loc->pspace == pspace)
2927 {
2928 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2929 if (loc->owner->loc == loc)
2930 loc->owner->loc = loc->next;
2931 else
2932 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2933 if (tmp->next == loc)
2934 {
2935 tmp->next = loc->next;
2936 break;
2937 }
2938 }
2939 }
2940
2941 /* Now update the global location list to permanently delete the
2942 removed locations above. */
2943 update_global_location_list (UGLL_DONT_INSERT);
2944 }
2945
2946 /* Make sure all breakpoints are inserted in inferior.
2947 Throws exception on any error.
2948 A breakpoint that is already inserted won't be inserted
2949 again, so calling this function twice is safe. */
2950 void
2951 insert_breakpoints (void)
2952 {
2953 for (breakpoint *bpt : all_breakpoints ())
2954 if (is_hardware_watchpoint (bpt))
2955 {
2956 struct watchpoint *w = (struct watchpoint *) bpt;
2957
2958 update_watchpoint (w, 0 /* don't reparse. */);
2959 }
2960
2961 /* Updating watchpoints creates new locations, so update the global
2962 location list. Explicitly tell ugll to insert locations and
2963 ignore breakpoints_always_inserted_mode. Also,
2964 update_global_location_list tries to "upgrade" software
2965 breakpoints to hardware breakpoints to handle "set breakpoint
2966 auto-hw", so we need to call it even if we don't have new
2967 locations. */
2968 update_global_location_list (UGLL_INSERT);
2969 }
2970
2971 /* This is used when we need to synch breakpoint conditions between GDB and the
2972 target. It is the case with deleting and disabling of breakpoints when using
2973 always-inserted mode. */
2974
2975 static void
2976 update_inserted_breakpoint_locations (void)
2977 {
2978 int error_flag = 0;
2979 int val = 0;
2980 int disabled_breaks = 0;
2981 int hw_breakpoint_error = 0;
2982 int hw_bp_details_reported = 0;
2983
2984 string_file tmp_error_stream;
2985
2986 /* Explicitly mark the warning -- this will only be printed if
2987 there was an error. */
2988 tmp_error_stream.puts ("Warning:\n");
2989
2990 scoped_restore_current_pspace_and_thread restore_pspace_thread;
2991
2992 for (bp_location *bl : all_bp_locations ())
2993 {
2994 /* We only want to update software breakpoints and hardware
2995 breakpoints. */
2996 if (!is_breakpoint (bl->owner))
2997 continue;
2998
2999 /* We only want to update locations that are already inserted
3000 and need updating. This is to avoid unwanted insertion during
3001 deletion of breakpoints. */
3002 if (!bl->inserted || !bl->needs_update)
3003 continue;
3004
3005 switch_to_program_space_and_thread (bl->pspace);
3006
3007 /* For targets that support global breakpoints, there's no need
3008 to select an inferior to insert breakpoint to. In fact, even
3009 if we aren't attached to any process yet, we should still
3010 insert breakpoints. */
3011 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3012 && (inferior_ptid == null_ptid || !target_has_execution ()))
3013 continue;
3014
3015 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3016 &hw_breakpoint_error, &hw_bp_details_reported);
3017 if (val)
3018 error_flag = val;
3019 }
3020
3021 if (error_flag)
3022 {
3023 target_terminal::ours_for_output ();
3024 error_stream (tmp_error_stream);
3025 }
3026 }
3027
3028 /* Used when starting or continuing the program. */
3029
3030 static void
3031 insert_breakpoint_locations (void)
3032 {
3033 int error_flag = 0;
3034 int val = 0;
3035 int disabled_breaks = 0;
3036 int hw_breakpoint_error = 0;
3037 int hw_bp_error_explained_already = 0;
3038
3039 string_file tmp_error_stream;
3040
3041 /* Explicitly mark the warning -- this will only be printed if
3042 there was an error. */
3043 tmp_error_stream.puts ("Warning:\n");
3044
3045 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3046
3047 for (bp_location *bl : all_bp_locations ())
3048 {
3049 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3050 continue;
3051
3052 /* There is no point inserting thread-specific breakpoints if
3053 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3054 has BL->OWNER always non-NULL. */
3055 if (bl->owner->thread != -1
3056 && !valid_global_thread_id (bl->owner->thread))
3057 continue;
3058
3059 switch_to_program_space_and_thread (bl->pspace);
3060
3061 /* For targets that support global breakpoints, there's no need
3062 to select an inferior to insert breakpoint to. In fact, even
3063 if we aren't attached to any process yet, we should still
3064 insert breakpoints. */
3065 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3066 && (inferior_ptid == null_ptid || !target_has_execution ()))
3067 continue;
3068
3069 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3070 &hw_breakpoint_error, &hw_bp_error_explained_already);
3071 if (val)
3072 error_flag = val;
3073 }
3074
3075 /* If we failed to insert all locations of a watchpoint, remove
3076 them, as half-inserted watchpoint is of limited use. */
3077 for (breakpoint *bpt : all_breakpoints ())
3078 {
3079 int some_failed = 0;
3080
3081 if (!is_hardware_watchpoint (bpt))
3082 continue;
3083
3084 if (!breakpoint_enabled (bpt))
3085 continue;
3086
3087 if (bpt->disposition == disp_del_at_next_stop)
3088 continue;
3089
3090 for (bp_location *loc : bpt->locations ())
3091 if (!loc->inserted && should_be_inserted (loc))
3092 {
3093 some_failed = 1;
3094 break;
3095 }
3096
3097 if (some_failed)
3098 {
3099 for (bp_location *loc : bpt->locations ())
3100 if (loc->inserted)
3101 remove_breakpoint (loc);
3102
3103 hw_breakpoint_error = 1;
3104 tmp_error_stream.printf ("Could not insert "
3105 "hardware watchpoint %d.\n",
3106 bpt->number);
3107 error_flag = -1;
3108 }
3109 }
3110
3111 if (error_flag)
3112 {
3113 /* If a hardware breakpoint or watchpoint was inserted, add a
3114 message about possibly exhausted resources. */
3115 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3116 {
3117 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3118 You may have requested too many hardware breakpoints/watchpoints.\n");
3119 }
3120 target_terminal::ours_for_output ();
3121 error_stream (tmp_error_stream);
3122 }
3123 }
3124
3125 /* Used when the program stops.
3126 Returns zero if successful, or non-zero if there was a problem
3127 removing a breakpoint location. */
3128
3129 int
3130 remove_breakpoints (void)
3131 {
3132 int val = 0;
3133
3134 for (bp_location *bl : all_bp_locations ())
3135 if (bl->inserted && !is_tracepoint (bl->owner))
3136 val |= remove_breakpoint (bl);
3137
3138 return val;
3139 }
3140
3141 /* When a thread exits, remove breakpoints that are related to
3142 that thread. */
3143
3144 static void
3145 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3146 {
3147 for (breakpoint *b : all_breakpoints_safe ())
3148 {
3149 if (b->thread == tp->global_num && user_breakpoint_p (b))
3150 {
3151 b->disposition = disp_del_at_next_stop;
3152
3153 gdb_printf (_("\
3154 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3155 b->number, print_thread_id (tp));
3156
3157 /* Hide it from the user. */
3158 b->number = 0;
3159 }
3160 }
3161 }
3162
3163 /* See breakpoint.h. */
3164
3165 void
3166 remove_breakpoints_inf (inferior *inf)
3167 {
3168 int val;
3169
3170 for (bp_location *bl : all_bp_locations ())
3171 {
3172 if (bl->pspace != inf->pspace)
3173 continue;
3174
3175 if (bl->inserted && !bl->target_info.persist)
3176 {
3177 val = remove_breakpoint (bl);
3178 if (val != 0)
3179 return;
3180 }
3181 }
3182 }
3183
3184 static int internal_breakpoint_number = -1;
3185
3186 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3187 If INTERNAL is non-zero, the breakpoint number will be populated
3188 from internal_breakpoint_number and that variable decremented.
3189 Otherwise the breakpoint number will be populated from
3190 breakpoint_count and that value incremented. Internal breakpoints
3191 do not set the internal var bpnum. */
3192 static void
3193 set_breakpoint_number (int internal, struct breakpoint *b)
3194 {
3195 if (internal)
3196 b->number = internal_breakpoint_number--;
3197 else
3198 {
3199 set_breakpoint_count (breakpoint_count + 1);
3200 b->number = breakpoint_count;
3201 }
3202 }
3203
3204 static struct breakpoint *
3205 create_internal_breakpoint (struct gdbarch *gdbarch,
3206 CORE_ADDR address, enum bptype type,
3207 const struct breakpoint_ops *ops)
3208 {
3209 symtab_and_line sal;
3210 sal.pc = address;
3211 sal.section = find_pc_overlay (sal.pc);
3212 sal.pspace = current_program_space;
3213
3214 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
3215 b->number = internal_breakpoint_number--;
3216 b->disposition = disp_donttouch;
3217
3218 return b;
3219 }
3220
3221 static const char *const longjmp_names[] =
3222 {
3223 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3224 };
3225 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3226
3227 /* Per-objfile data private to breakpoint.c. */
3228 struct breakpoint_objfile_data
3229 {
3230 /* Minimal symbol for "_ovly_debug_event" (if any). */
3231 struct bound_minimal_symbol overlay_msym;
3232
3233 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3234 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3235
3236 /* True if we have looked for longjmp probes. */
3237 int longjmp_searched = 0;
3238
3239 /* SystemTap probe points for longjmp (if any). These are non-owning
3240 references. */
3241 std::vector<probe *> longjmp_probes;
3242
3243 /* Minimal symbol for "std::terminate()" (if any). */
3244 struct bound_minimal_symbol terminate_msym;
3245
3246 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3247 struct bound_minimal_symbol exception_msym;
3248
3249 /* True if we have looked for exception probes. */
3250 int exception_searched = 0;
3251
3252 /* SystemTap probe points for unwinding (if any). These are non-owning
3253 references. */
3254 std::vector<probe *> exception_probes;
3255 };
3256
3257 static const struct objfile_key<breakpoint_objfile_data>
3258 breakpoint_objfile_key;
3259
3260 /* Minimal symbol not found sentinel. */
3261 static struct minimal_symbol msym_not_found;
3262
3263 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3264
3265 static int
3266 msym_not_found_p (const struct minimal_symbol *msym)
3267 {
3268 return msym == &msym_not_found;
3269 }
3270
3271 /* Return per-objfile data needed by breakpoint.c.
3272 Allocate the data if necessary. */
3273
3274 static struct breakpoint_objfile_data *
3275 get_breakpoint_objfile_data (struct objfile *objfile)
3276 {
3277 struct breakpoint_objfile_data *bp_objfile_data;
3278
3279 bp_objfile_data = breakpoint_objfile_key.get (objfile);
3280 if (bp_objfile_data == NULL)
3281 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
3282 return bp_objfile_data;
3283 }
3284
3285 static void
3286 create_overlay_event_breakpoint (void)
3287 {
3288 const char *const func_name = "_ovly_debug_event";
3289
3290 for (objfile *objfile : current_program_space->objfiles ())
3291 {
3292 struct breakpoint *b;
3293 struct breakpoint_objfile_data *bp_objfile_data;
3294 CORE_ADDR addr;
3295 struct explicit_location explicit_loc;
3296
3297 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3298
3299 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3300 continue;
3301
3302 if (bp_objfile_data->overlay_msym.minsym == NULL)
3303 {
3304 struct bound_minimal_symbol m;
3305
3306 m = lookup_minimal_symbol_text (func_name, objfile);
3307 if (m.minsym == NULL)
3308 {
3309 /* Avoid future lookups in this objfile. */
3310 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3311 continue;
3312 }
3313 bp_objfile_data->overlay_msym = m;
3314 }
3315
3316 addr = bp_objfile_data->overlay_msym.value_address ();
3317 b = create_internal_breakpoint (objfile->arch (), addr,
3318 bp_overlay_event,
3319 &internal_breakpoint_ops);
3320 initialize_explicit_location (&explicit_loc);
3321 explicit_loc.function_name = ASTRDUP (func_name);
3322 b->location = new_explicit_location (&explicit_loc);
3323
3324 if (overlay_debugging == ovly_auto)
3325 {
3326 b->enable_state = bp_enabled;
3327 overlay_events_enabled = 1;
3328 }
3329 else
3330 {
3331 b->enable_state = bp_disabled;
3332 overlay_events_enabled = 0;
3333 }
3334 }
3335 }
3336
3337 /* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3338 true if a breakpoint was installed. */
3339
3340 static bool
3341 create_longjmp_master_breakpoint_probe (objfile *objfile)
3342 {
3343 struct gdbarch *gdbarch = objfile->arch ();
3344 struct breakpoint_objfile_data *bp_objfile_data
3345 = get_breakpoint_objfile_data (objfile);
3346
3347 if (!bp_objfile_data->longjmp_searched)
3348 {
3349 std::vector<probe *> ret
3350 = find_probes_in_objfile (objfile, "libc", "longjmp");
3351
3352 if (!ret.empty ())
3353 {
3354 /* We are only interested in checking one element. */
3355 probe *p = ret[0];
3356
3357 if (!p->can_evaluate_arguments ())
3358 {
3359 /* We cannot use the probe interface here,
3360 because it does not know how to evaluate
3361 arguments. */
3362 ret.clear ();
3363 }
3364 }
3365 bp_objfile_data->longjmp_probes = ret;
3366 bp_objfile_data->longjmp_searched = 1;
3367 }
3368
3369 if (bp_objfile_data->longjmp_probes.empty ())
3370 return false;
3371
3372 for (probe *p : bp_objfile_data->longjmp_probes)
3373 {
3374 struct breakpoint *b;
3375
3376 b = create_internal_breakpoint (gdbarch,
3377 p->get_relocated_address (objfile),
3378 bp_longjmp_master,
3379 &internal_breakpoint_ops);
3380 b->location = new_probe_location ("-probe-stap libc:longjmp");
3381 b->enable_state = bp_disabled;
3382 }
3383
3384 return true;
3385 }
3386
3387 /* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3388 Return true if at least one breakpoint was installed. */
3389
3390 static bool
3391 create_longjmp_master_breakpoint_names (objfile *objfile)
3392 {
3393 struct gdbarch *gdbarch = objfile->arch ();
3394 if (!gdbarch_get_longjmp_target_p (gdbarch))
3395 return false;
3396
3397 struct breakpoint_objfile_data *bp_objfile_data
3398 = get_breakpoint_objfile_data (objfile);
3399 unsigned int installed_bp = 0;
3400
3401 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3402 {
3403 struct breakpoint *b;
3404 const char *func_name;
3405 CORE_ADDR addr;
3406 struct explicit_location explicit_loc;
3407
3408 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3409 continue;
3410
3411 func_name = longjmp_names[i];
3412 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3413 {
3414 struct bound_minimal_symbol m;
3415
3416 m = lookup_minimal_symbol_text (func_name, objfile);
3417 if (m.minsym == NULL)
3418 {
3419 /* Prevent future lookups in this objfile. */
3420 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3421 continue;
3422 }
3423 bp_objfile_data->longjmp_msym[i] = m;
3424 }
3425
3426 addr = bp_objfile_data->longjmp_msym[i].value_address ();
3427 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3428 &internal_breakpoint_ops);
3429 initialize_explicit_location (&explicit_loc);
3430 explicit_loc.function_name = ASTRDUP (func_name);
3431 b->location = new_explicit_location (&explicit_loc);
3432 b->enable_state = bp_disabled;
3433 installed_bp++;
3434 }
3435
3436 return installed_bp > 0;
3437 }
3438
3439 /* Create a master longjmp breakpoint. */
3440
3441 static void
3442 create_longjmp_master_breakpoint (void)
3443 {
3444 scoped_restore_current_program_space restore_pspace;
3445
3446 for (struct program_space *pspace : program_spaces)
3447 {
3448 set_current_program_space (pspace);
3449
3450 for (objfile *obj : current_program_space->objfiles ())
3451 {
3452 /* Skip separate debug object, it's handled in the loop below. */
3453 if (obj->separate_debug_objfile_backlink != nullptr)
3454 continue;
3455
3456 /* Try a probe kind breakpoint on main objfile. */
3457 if (create_longjmp_master_breakpoint_probe (obj))
3458 continue;
3459
3460 /* Try longjmp_names kind breakpoints on main and separate_debug
3461 objfiles. */
3462 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3463 if (create_longjmp_master_breakpoint_names (debug_objfile))
3464 break;
3465 }
3466 }
3467 }
3468
3469 /* Create a master std::terminate breakpoint. */
3470 static void
3471 create_std_terminate_master_breakpoint (void)
3472 {
3473 const char *const func_name = "std::terminate()";
3474
3475 scoped_restore_current_program_space restore_pspace;
3476
3477 for (struct program_space *pspace : program_spaces)
3478 {
3479 CORE_ADDR addr;
3480
3481 set_current_program_space (pspace);
3482
3483 for (objfile *objfile : current_program_space->objfiles ())
3484 {
3485 struct breakpoint *b;
3486 struct breakpoint_objfile_data *bp_objfile_data;
3487 struct explicit_location explicit_loc;
3488
3489 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3490
3491 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3492 continue;
3493
3494 if (bp_objfile_data->terminate_msym.minsym == NULL)
3495 {
3496 struct bound_minimal_symbol m;
3497
3498 m = lookup_minimal_symbol (func_name, NULL, objfile);
3499 if (m.minsym == NULL || (m.minsym->type () != mst_text
3500 && m.minsym->type () != mst_file_text))
3501 {
3502 /* Prevent future lookups in this objfile. */
3503 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3504 continue;
3505 }
3506 bp_objfile_data->terminate_msym = m;
3507 }
3508
3509 addr = bp_objfile_data->terminate_msym.value_address ();
3510 b = create_internal_breakpoint (objfile->arch (), addr,
3511 bp_std_terminate_master,
3512 &internal_breakpoint_ops);
3513 initialize_explicit_location (&explicit_loc);
3514 explicit_loc.function_name = ASTRDUP (func_name);
3515 b->location = new_explicit_location (&explicit_loc);
3516 b->enable_state = bp_disabled;
3517 }
3518 }
3519 }
3520
3521 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3522 probe. Return true if a breakpoint was installed. */
3523
3524 static bool
3525 create_exception_master_breakpoint_probe (objfile *objfile)
3526 {
3527 struct breakpoint *b;
3528 struct gdbarch *gdbarch;
3529 struct breakpoint_objfile_data *bp_objfile_data;
3530
3531 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3532
3533 /* We prefer the SystemTap probe point if it exists. */
3534 if (!bp_objfile_data->exception_searched)
3535 {
3536 std::vector<probe *> ret
3537 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3538
3539 if (!ret.empty ())
3540 {
3541 /* We are only interested in checking one element. */
3542 probe *p = ret[0];
3543
3544 if (!p->can_evaluate_arguments ())
3545 {
3546 /* We cannot use the probe interface here, because it does
3547 not know how to evaluate arguments. */
3548 ret.clear ();
3549 }
3550 }
3551 bp_objfile_data->exception_probes = ret;
3552 bp_objfile_data->exception_searched = 1;
3553 }
3554
3555 if (bp_objfile_data->exception_probes.empty ())
3556 return false;
3557
3558 gdbarch = objfile->arch ();
3559
3560 for (probe *p : bp_objfile_data->exception_probes)
3561 {
3562 b = create_internal_breakpoint (gdbarch,
3563 p->get_relocated_address (objfile),
3564 bp_exception_master,
3565 &internal_breakpoint_ops);
3566 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3567 b->enable_state = bp_disabled;
3568 }
3569
3570 return true;
3571 }
3572
3573 /* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3574 _Unwind_DebugHook. Return true if a breakpoint was installed. */
3575
3576 static bool
3577 create_exception_master_breakpoint_hook (objfile *objfile)
3578 {
3579 const char *const func_name = "_Unwind_DebugHook";
3580 struct breakpoint *b;
3581 struct gdbarch *gdbarch;
3582 struct breakpoint_objfile_data *bp_objfile_data;
3583 CORE_ADDR addr;
3584 struct explicit_location explicit_loc;
3585
3586 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3587
3588 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3589 return false;
3590
3591 gdbarch = objfile->arch ();
3592
3593 if (bp_objfile_data->exception_msym.minsym == NULL)
3594 {
3595 struct bound_minimal_symbol debug_hook;
3596
3597 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3598 if (debug_hook.minsym == NULL)
3599 {
3600 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3601 return false;
3602 }
3603
3604 bp_objfile_data->exception_msym = debug_hook;
3605 }
3606
3607 addr = bp_objfile_data->exception_msym.value_address ();
3608 addr = gdbarch_convert_from_func_ptr_addr
3609 (gdbarch, addr, current_inferior ()->top_target ());
3610 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3611 &internal_breakpoint_ops);
3612 initialize_explicit_location (&explicit_loc);
3613 explicit_loc.function_name = ASTRDUP (func_name);
3614 b->location = new_explicit_location (&explicit_loc);
3615 b->enable_state = bp_disabled;
3616
3617 return true;
3618 }
3619
3620 /* Install a master breakpoint on the unwinder's debug hook. */
3621
3622 static void
3623 create_exception_master_breakpoint (void)
3624 {
3625 for (objfile *obj : current_program_space->objfiles ())
3626 {
3627 /* Skip separate debug object. */
3628 if (obj->separate_debug_objfile_backlink)
3629 continue;
3630
3631 /* Try a probe kind breakpoint. */
3632 if (create_exception_master_breakpoint_probe (obj))
3633 continue;
3634
3635 /* Iterate over main and separate debug objects and try an
3636 _Unwind_DebugHook kind breakpoint. */
3637 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3638 if (create_exception_master_breakpoint_hook (debug_objfile))
3639 break;
3640 }
3641 }
3642
3643 /* Does B have a location spec? */
3644
3645 static int
3646 breakpoint_event_location_empty_p (const struct breakpoint *b)
3647 {
3648 return b->location != NULL && event_location_empty_p (b->location.get ());
3649 }
3650
3651 void
3652 update_breakpoints_after_exec (void)
3653 {
3654 /* We're about to delete breakpoints from GDB's lists. If the
3655 INSERTED flag is true, GDB will try to lift the breakpoints by
3656 writing the breakpoints' "shadow contents" back into memory. The
3657 "shadow contents" are NOT valid after an exec, so GDB should not
3658 do that. Instead, the target is responsible from marking
3659 breakpoints out as soon as it detects an exec. We don't do that
3660 here instead, because there may be other attempts to delete
3661 breakpoints after detecting an exec and before reaching here. */
3662 for (bp_location *bploc : all_bp_locations ())
3663 if (bploc->pspace == current_program_space)
3664 gdb_assert (!bploc->inserted);
3665
3666 for (breakpoint *b : all_breakpoints_safe ())
3667 {
3668 if (b->pspace != current_program_space)
3669 continue;
3670
3671 /* Solib breakpoints must be explicitly reset after an exec(). */
3672 if (b->type == bp_shlib_event)
3673 {
3674 delete_breakpoint (b);
3675 continue;
3676 }
3677
3678 /* JIT breakpoints must be explicitly reset after an exec(). */
3679 if (b->type == bp_jit_event)
3680 {
3681 delete_breakpoint (b);
3682 continue;
3683 }
3684
3685 /* Thread event breakpoints must be set anew after an exec(),
3686 as must overlay event and longjmp master breakpoints. */
3687 if (b->type == bp_thread_event || b->type == bp_overlay_event
3688 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3689 || b->type == bp_exception_master)
3690 {
3691 delete_breakpoint (b);
3692 continue;
3693 }
3694
3695 /* Step-resume breakpoints are meaningless after an exec(). */
3696 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3697 {
3698 delete_breakpoint (b);
3699 continue;
3700 }
3701
3702 /* Just like single-step breakpoints. */
3703 if (b->type == bp_single_step)
3704 {
3705 delete_breakpoint (b);
3706 continue;
3707 }
3708
3709 /* Longjmp and longjmp-resume breakpoints are also meaningless
3710 after an exec. */
3711 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3712 || b->type == bp_longjmp_call_dummy
3713 || b->type == bp_exception || b->type == bp_exception_resume)
3714 {
3715 delete_breakpoint (b);
3716 continue;
3717 }
3718
3719 if (b->type == bp_catchpoint)
3720 {
3721 /* For now, none of the bp_catchpoint breakpoints need to
3722 do anything at this point. In the future, if some of
3723 the catchpoints need to something, we will need to add
3724 a new method, and call this method from here. */
3725 continue;
3726 }
3727
3728 /* bp_finish is a special case. The only way we ought to be able
3729 to see one of these when an exec() has happened, is if the user
3730 caught a vfork, and then said "finish". Ordinarily a finish just
3731 carries them to the call-site of the current callee, by setting
3732 a temporary bp there and resuming. But in this case, the finish
3733 will carry them entirely through the vfork & exec.
3734
3735 We don't want to allow a bp_finish to remain inserted now. But
3736 we can't safely delete it, 'cause finish_command has a handle to
3737 the bp on a bpstat, and will later want to delete it. There's a
3738 chance (and I've seen it happen) that if we delete the bp_finish
3739 here, that its storage will get reused by the time finish_command
3740 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3741 We really must allow finish_command to delete a bp_finish.
3742
3743 In the absence of a general solution for the "how do we know
3744 it's safe to delete something others may have handles to?"
3745 problem, what we'll do here is just uninsert the bp_finish, and
3746 let finish_command delete it.
3747
3748 (We know the bp_finish is "doomed" in the sense that it's
3749 momentary, and will be deleted as soon as finish_command sees
3750 the inferior stopped. So it doesn't matter that the bp's
3751 address is probably bogus in the new a.out, unlike e.g., the
3752 solib breakpoints.) */
3753
3754 if (b->type == bp_finish)
3755 {
3756 continue;
3757 }
3758
3759 /* Without a symbolic address, we have little hope of the
3760 pre-exec() address meaning the same thing in the post-exec()
3761 a.out. */
3762 if (breakpoint_event_location_empty_p (b))
3763 {
3764 delete_breakpoint (b);
3765 continue;
3766 }
3767 }
3768 }
3769
3770 int
3771 detach_breakpoints (ptid_t ptid)
3772 {
3773 int val = 0;
3774 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
3775 struct inferior *inf = current_inferior ();
3776
3777 if (ptid.pid () == inferior_ptid.pid ())
3778 error (_("Cannot detach breakpoints of inferior_ptid"));
3779
3780 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3781 inferior_ptid = ptid;
3782 for (bp_location *bl : all_bp_locations ())
3783 {
3784 if (bl->pspace != inf->pspace)
3785 continue;
3786
3787 /* This function must physically remove breakpoints locations
3788 from the specified ptid, without modifying the breakpoint
3789 package's state. Locations of type bp_loc_other are only
3790 maintained at GDB side. So, there is no need to remove
3791 these bp_loc_other locations. Moreover, removing these
3792 would modify the breakpoint package's state. */
3793 if (bl->loc_type == bp_loc_other)
3794 continue;
3795
3796 if (bl->inserted)
3797 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3798 }
3799
3800 return val;
3801 }
3802
3803 /* Remove the breakpoint location BL from the current address space.
3804 Note that this is used to detach breakpoints from a child fork.
3805 When we get here, the child isn't in the inferior list, and neither
3806 do we have objects to represent its address space --- we should
3807 *not* look at bl->pspace->aspace here. */
3808
3809 static int
3810 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3811 {
3812 int val;
3813
3814 /* BL is never in moribund_locations by our callers. */
3815 gdb_assert (bl->owner != NULL);
3816
3817 /* The type of none suggests that owner is actually deleted.
3818 This should not ever happen. */
3819 gdb_assert (bl->owner->type != bp_none);
3820
3821 if (bl->loc_type == bp_loc_software_breakpoint
3822 || bl->loc_type == bp_loc_hardware_breakpoint)
3823 {
3824 /* "Normal" instruction breakpoint: either the standard
3825 trap-instruction bp (bp_breakpoint), or a
3826 bp_hardware_breakpoint. */
3827
3828 /* First check to see if we have to handle an overlay. */
3829 if (overlay_debugging == ovly_off
3830 || bl->section == NULL
3831 || !(section_is_overlay (bl->section)))
3832 {
3833 /* No overlay handling: just remove the breakpoint. */
3834
3835 /* If we're trying to uninsert a memory breakpoint that we
3836 know is set in a dynamic object that is marked
3837 shlib_disabled, then either the dynamic object was
3838 removed with "remove-symbol-file" or with
3839 "nosharedlibrary". In the former case, we don't know
3840 whether another dynamic object might have loaded over the
3841 breakpoint's address -- the user might well let us know
3842 about it next with add-symbol-file (the whole point of
3843 add-symbol-file is letting the user manually maintain a
3844 list of dynamically loaded objects). If we have the
3845 breakpoint's shadow memory, that is, this is a software
3846 breakpoint managed by GDB, check whether the breakpoint
3847 is still inserted in memory, to avoid overwriting wrong
3848 code with stale saved shadow contents. Note that HW
3849 breakpoints don't have shadow memory, as they're
3850 implemented using a mechanism that is not dependent on
3851 being able to modify the target's memory, and as such
3852 they should always be removed. */
3853 if (bl->shlib_disabled
3854 && bl->target_info.shadow_len != 0
3855 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3856 val = 0;
3857 else
3858 val = bl->owner->ops->remove_location (bl, reason);
3859 }
3860 else
3861 {
3862 /* This breakpoint is in an overlay section.
3863 Did we set a breakpoint at the LMA? */
3864 if (!overlay_events_enabled)
3865 {
3866 /* Yes -- overlay event support is not active, so we
3867 should have set a breakpoint at the LMA. Remove it.
3868 */
3869 /* Ignore any failures: if the LMA is in ROM, we will
3870 have already warned when we failed to insert it. */
3871 if (bl->loc_type == bp_loc_hardware_breakpoint)
3872 target_remove_hw_breakpoint (bl->gdbarch,
3873 &bl->overlay_target_info);
3874 else
3875 target_remove_breakpoint (bl->gdbarch,
3876 &bl->overlay_target_info,
3877 reason);
3878 }
3879 /* Did we set a breakpoint at the VMA?
3880 If so, we will have marked the breakpoint 'inserted'. */
3881 if (bl->inserted)
3882 {
3883 /* Yes -- remove it. Previously we did not bother to
3884 remove the breakpoint if the section had been
3885 unmapped, but let's not rely on that being safe. We
3886 don't know what the overlay manager might do. */
3887
3888 /* However, we should remove *software* breakpoints only
3889 if the section is still mapped, or else we overwrite
3890 wrong code with the saved shadow contents. */
3891 if (bl->loc_type == bp_loc_hardware_breakpoint
3892 || section_is_mapped (bl->section))
3893 val = bl->owner->ops->remove_location (bl, reason);
3894 else
3895 val = 0;
3896 }
3897 else
3898 {
3899 /* No -- not inserted, so no need to remove. No error. */
3900 val = 0;
3901 }
3902 }
3903
3904 /* In some cases, we might not be able to remove a breakpoint in
3905 a shared library that has already been removed, but we have
3906 not yet processed the shlib unload event. Similarly for an
3907 unloaded add-symbol-file object - the user might not yet have
3908 had the chance to remove-symbol-file it. shlib_disabled will
3909 be set if the library/object has already been removed, but
3910 the breakpoint hasn't been uninserted yet, e.g., after
3911 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3912 always-inserted mode. */
3913 if (val
3914 && (bl->loc_type == bp_loc_software_breakpoint
3915 && (bl->shlib_disabled
3916 || solib_name_from_address (bl->pspace, bl->address)
3917 || shared_objfile_contains_address_p (bl->pspace,
3918 bl->address))))
3919 val = 0;
3920
3921 if (val)
3922 return val;
3923 bl->inserted = (reason == DETACH_BREAKPOINT);
3924 }
3925 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3926 {
3927 gdb_assert (bl->owner->ops != NULL
3928 && bl->owner->ops->remove_location != NULL);
3929
3930 bl->inserted = (reason == DETACH_BREAKPOINT);
3931 bl->owner->ops->remove_location (bl, reason);
3932
3933 /* Failure to remove any of the hardware watchpoints comes here. */
3934 if (reason == REMOVE_BREAKPOINT && bl->inserted)
3935 warning (_("Could not remove hardware watchpoint %d."),
3936 bl->owner->number);
3937 }
3938 else if (bl->owner->type == bp_catchpoint
3939 && breakpoint_enabled (bl->owner)
3940 && !bl->duplicate)
3941 {
3942 gdb_assert (bl->owner->ops != NULL
3943 && bl->owner->ops->remove_location != NULL);
3944
3945 val = bl->owner->ops->remove_location (bl, reason);
3946 if (val)
3947 return val;
3948
3949 bl->inserted = (reason == DETACH_BREAKPOINT);
3950 }
3951
3952 return 0;
3953 }
3954
3955 static int
3956 remove_breakpoint (struct bp_location *bl)
3957 {
3958 /* BL is never in moribund_locations by our callers. */
3959 gdb_assert (bl->owner != NULL);
3960
3961 /* The type of none suggests that owner is actually deleted.
3962 This should not ever happen. */
3963 gdb_assert (bl->owner->type != bp_none);
3964
3965 scoped_restore_current_pspace_and_thread restore_pspace_thread;
3966
3967 switch_to_program_space_and_thread (bl->pspace);
3968
3969 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
3970 }
3971
3972 /* Clear the "inserted" flag in all breakpoints. */
3973
3974 void
3975 mark_breakpoints_out (void)
3976 {
3977 for (bp_location *bl : all_bp_locations ())
3978 if (bl->pspace == current_program_space)
3979 bl->inserted = 0;
3980 }
3981
3982 /* Clear the "inserted" flag in all breakpoints and delete any
3983 breakpoints which should go away between runs of the program.
3984
3985 Plus other such housekeeping that has to be done for breakpoints
3986 between runs.
3987
3988 Note: this function gets called at the end of a run (by
3989 generic_mourn_inferior) and when a run begins (by
3990 init_wait_for_inferior). */
3991
3992
3993
3994 void
3995 breakpoint_init_inferior (enum inf_context context)
3996 {
3997 struct program_space *pspace = current_program_space;
3998
3999 /* If breakpoint locations are shared across processes, then there's
4000 nothing to do. */
4001 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4002 return;
4003
4004 mark_breakpoints_out ();
4005
4006 for (breakpoint *b : all_breakpoints_safe ())
4007 {
4008 if (b->loc && b->loc->pspace != pspace)
4009 continue;
4010
4011 switch (b->type)
4012 {
4013 case bp_call_dummy:
4014 case bp_longjmp_call_dummy:
4015
4016 /* If the call dummy breakpoint is at the entry point it will
4017 cause problems when the inferior is rerun, so we better get
4018 rid of it. */
4019
4020 case bp_watchpoint_scope:
4021
4022 /* Also get rid of scope breakpoints. */
4023
4024 case bp_shlib_event:
4025
4026 /* Also remove solib event breakpoints. Their addresses may
4027 have changed since the last time we ran the program.
4028 Actually we may now be debugging against different target;
4029 and so the solib backend that installed this breakpoint may
4030 not be used in by the target. E.g.,
4031
4032 (gdb) file prog-linux
4033 (gdb) run # native linux target
4034 ...
4035 (gdb) kill
4036 (gdb) file prog-win.exe
4037 (gdb) tar rem :9999 # remote Windows gdbserver.
4038 */
4039
4040 case bp_step_resume:
4041
4042 /* Also remove step-resume breakpoints. */
4043
4044 case bp_single_step:
4045
4046 /* Also remove single-step breakpoints. */
4047
4048 delete_breakpoint (b);
4049 break;
4050
4051 case bp_watchpoint:
4052 case bp_hardware_watchpoint:
4053 case bp_read_watchpoint:
4054 case bp_access_watchpoint:
4055 {
4056 struct watchpoint *w = (struct watchpoint *) b;
4057
4058 /* Likewise for watchpoints on local expressions. */
4059 if (w->exp_valid_block != NULL)
4060 delete_breakpoint (b);
4061 else
4062 {
4063 /* Get rid of existing locations, which are no longer
4064 valid. New ones will be created in
4065 update_watchpoint, when the inferior is restarted.
4066 The next update_global_location_list call will
4067 garbage collect them. */
4068 b->loc = NULL;
4069
4070 if (context == inf_starting)
4071 {
4072 /* Reset val field to force reread of starting value in
4073 insert_breakpoints. */
4074 w->val.reset (nullptr);
4075 w->val_valid = false;
4076 }
4077 }
4078 }
4079 break;
4080 default:
4081 break;
4082 }
4083 }
4084
4085 /* Get rid of the moribund locations. */
4086 for (bp_location *bl : moribund_locations)
4087 decref_bp_location (&bl);
4088 moribund_locations.clear ();
4089 }
4090
4091 /* These functions concern about actual breakpoints inserted in the
4092 target --- to e.g. check if we need to do decr_pc adjustment or if
4093 we need to hop over the bkpt --- so we check for address space
4094 match, not program space. */
4095
4096 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4097 exists at PC. It returns ordinary_breakpoint_here if it's an
4098 ordinary breakpoint, or permanent_breakpoint_here if it's a
4099 permanent breakpoint.
4100 - When continuing from a location with an ordinary breakpoint, we
4101 actually single step once before calling insert_breakpoints.
4102 - When continuing from a location with a permanent breakpoint, we
4103 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4104 the target, to advance the PC past the breakpoint. */
4105
4106 enum breakpoint_here
4107 breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4108 {
4109 int any_breakpoint_here = 0;
4110
4111 for (bp_location *bl : all_bp_locations ())
4112 {
4113 if (bl->loc_type != bp_loc_software_breakpoint
4114 && bl->loc_type != bp_loc_hardware_breakpoint)
4115 continue;
4116
4117 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4118 if ((breakpoint_enabled (bl->owner)
4119 || bl->permanent)
4120 && breakpoint_location_address_match (bl, aspace, pc))
4121 {
4122 if (overlay_debugging
4123 && section_is_overlay (bl->section)
4124 && !section_is_mapped (bl->section))
4125 continue; /* unmapped overlay -- can't be a match */
4126 else if (bl->permanent)
4127 return permanent_breakpoint_here;
4128 else
4129 any_breakpoint_here = 1;
4130 }
4131 }
4132
4133 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4134 }
4135
4136 /* See breakpoint.h. */
4137
4138 int
4139 breakpoint_in_range_p (const address_space *aspace,
4140 CORE_ADDR addr, ULONGEST len)
4141 {
4142 for (bp_location *bl : all_bp_locations ())
4143 {
4144 if (bl->loc_type != bp_loc_software_breakpoint
4145 && bl->loc_type != bp_loc_hardware_breakpoint)
4146 continue;
4147
4148 if ((breakpoint_enabled (bl->owner)
4149 || bl->permanent)
4150 && breakpoint_location_address_range_overlap (bl, aspace,
4151 addr, len))
4152 {
4153 if (overlay_debugging
4154 && section_is_overlay (bl->section)
4155 && !section_is_mapped (bl->section))
4156 {
4157 /* Unmapped overlay -- can't be a match. */
4158 continue;
4159 }
4160
4161 return 1;
4162 }
4163 }
4164
4165 return 0;
4166 }
4167
4168 /* Return true if there's a moribund breakpoint at PC. */
4169
4170 int
4171 moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
4172 {
4173 for (bp_location *loc : moribund_locations)
4174 if (breakpoint_location_address_match (loc, aspace, pc))
4175 return 1;
4176
4177 return 0;
4178 }
4179
4180 /* Returns non-zero iff BL is inserted at PC, in address space
4181 ASPACE. */
4182
4183 static int
4184 bp_location_inserted_here_p (struct bp_location *bl,
4185 const address_space *aspace, CORE_ADDR pc)
4186 {
4187 if (bl->inserted
4188 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4189 aspace, pc))
4190 {
4191 if (overlay_debugging
4192 && section_is_overlay (bl->section)
4193 && !section_is_mapped (bl->section))
4194 return 0; /* unmapped overlay -- can't be a match */
4195 else
4196 return 1;
4197 }
4198 return 0;
4199 }
4200
4201 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4202
4203 int
4204 breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
4205 {
4206 for (bp_location *bl : all_bp_locations_at_addr (pc))
4207 {
4208 if (bl->loc_type != bp_loc_software_breakpoint
4209 && bl->loc_type != bp_loc_hardware_breakpoint)
4210 continue;
4211
4212 if (bp_location_inserted_here_p (bl, aspace, pc))
4213 return 1;
4214 }
4215 return 0;
4216 }
4217
4218 /* This function returns non-zero iff there is a software breakpoint
4219 inserted at PC. */
4220
4221 int
4222 software_breakpoint_inserted_here_p (const address_space *aspace,
4223 CORE_ADDR pc)
4224 {
4225 for (bp_location *bl : all_bp_locations_at_addr (pc))
4226 {
4227 if (bl->loc_type != bp_loc_software_breakpoint)
4228 continue;
4229
4230 if (bp_location_inserted_here_p (bl, aspace, pc))
4231 return 1;
4232 }
4233
4234 return 0;
4235 }
4236
4237 /* See breakpoint.h. */
4238
4239 int
4240 hardware_breakpoint_inserted_here_p (const address_space *aspace,
4241 CORE_ADDR pc)
4242 {
4243 for (bp_location *bl : all_bp_locations_at_addr (pc))
4244 {
4245 if (bl->loc_type != bp_loc_hardware_breakpoint)
4246 continue;
4247
4248 if (bp_location_inserted_here_p (bl, aspace, pc))
4249 return 1;
4250 }
4251
4252 return 0;
4253 }
4254
4255 int
4256 hardware_watchpoint_inserted_in_range (const address_space *aspace,
4257 CORE_ADDR addr, ULONGEST len)
4258 {
4259 for (breakpoint *bpt : all_breakpoints ())
4260 {
4261 if (bpt->type != bp_hardware_watchpoint
4262 && bpt->type != bp_access_watchpoint)
4263 continue;
4264
4265 if (!breakpoint_enabled (bpt))
4266 continue;
4267
4268 for (bp_location *loc : bpt->locations ())
4269 if (loc->pspace->aspace == aspace && loc->inserted)
4270 {
4271 CORE_ADDR l, h;
4272
4273 /* Check for intersection. */
4274 l = std::max<CORE_ADDR> (loc->address, addr);
4275 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4276 if (l < h)
4277 return 1;
4278 }
4279 }
4280 return 0;
4281 }
4282
4283 /* See breakpoint.h. */
4284
4285 bool
4286 is_catchpoint (struct breakpoint *b)
4287 {
4288 return (b->type == bp_catchpoint);
4289 }
4290
4291 /* Clear a bpstat so that it says we are not at any breakpoint.
4292 Also free any storage that is part of a bpstat. */
4293
4294 void
4295 bpstat_clear (bpstat **bsp)
4296 {
4297 bpstat *p;
4298 bpstat *q;
4299
4300 if (bsp == 0)
4301 return;
4302 p = *bsp;
4303 while (p != NULL)
4304 {
4305 q = p->next;
4306 delete p;
4307 p = q;
4308 }
4309 *bsp = NULL;
4310 }
4311
4312 bpstat::bpstat (const bpstat &other)
4313 : next (NULL),
4314 bp_location_at (other.bp_location_at),
4315 breakpoint_at (other.breakpoint_at),
4316 commands (other.commands),
4317 print (other.print),
4318 stop (other.stop),
4319 print_it (other.print_it)
4320 {
4321 if (other.old_val != NULL)
4322 old_val = release_value (value_copy (other.old_val.get ()));
4323 }
4324
4325 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4326 is part of the bpstat is copied as well. */
4327
4328 bpstat *
4329 bpstat_copy (bpstat *bs)
4330 {
4331 bpstat *p = nullptr;
4332 bpstat *tmp;
4333 bpstat *retval = nullptr;
4334
4335 if (bs == NULL)
4336 return bs;
4337
4338 for (; bs != NULL; bs = bs->next)
4339 {
4340 tmp = new bpstat (*bs);
4341
4342 if (p == NULL)
4343 /* This is the first thing in the chain. */
4344 retval = tmp;
4345 else
4346 p->next = tmp;
4347 p = tmp;
4348 }
4349 p->next = NULL;
4350 return retval;
4351 }
4352
4353 /* Find the bpstat associated with this breakpoint. */
4354
4355 bpstat *
4356 bpstat_find_breakpoint (bpstat *bsp, struct breakpoint *breakpoint)
4357 {
4358 if (bsp == NULL)
4359 return NULL;
4360
4361 for (; bsp != NULL; bsp = bsp->next)
4362 {
4363 if (bsp->breakpoint_at == breakpoint)
4364 return bsp;
4365 }
4366 return NULL;
4367 }
4368
4369 /* See breakpoint.h. */
4370
4371 bool
4372 bpstat_explains_signal (bpstat *bsp, enum gdb_signal sig)
4373 {
4374 for (; bsp != NULL; bsp = bsp->next)
4375 {
4376 if (bsp->breakpoint_at == NULL)
4377 {
4378 /* A moribund location can never explain a signal other than
4379 GDB_SIGNAL_TRAP. */
4380 if (sig == GDB_SIGNAL_TRAP)
4381 return true;
4382 }
4383 else
4384 {
4385 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4386 sig))
4387 return true;
4388 }
4389 }
4390
4391 return false;
4392 }
4393
4394 /* Put in *NUM the breakpoint number of the first breakpoint we are
4395 stopped at. *BSP upon return is a bpstat which points to the
4396 remaining breakpoints stopped at (but which is not guaranteed to be
4397 good for anything but further calls to bpstat_num).
4398
4399 Return 0 if passed a bpstat which does not indicate any breakpoints.
4400 Return -1 if stopped at a breakpoint that has been deleted since
4401 we set it.
4402 Return 1 otherwise. */
4403
4404 int
4405 bpstat_num (bpstat **bsp, int *num)
4406 {
4407 struct breakpoint *b;
4408
4409 if ((*bsp) == NULL)
4410 return 0; /* No more breakpoint values */
4411
4412 /* We assume we'll never have several bpstats that correspond to a
4413 single breakpoint -- otherwise, this function might return the
4414 same number more than once and this will look ugly. */
4415 b = (*bsp)->breakpoint_at;
4416 *bsp = (*bsp)->next;
4417 if (b == NULL)
4418 return -1; /* breakpoint that's been deleted since */
4419
4420 *num = b->number; /* We have its number */
4421 return 1;
4422 }
4423
4424 /* See breakpoint.h. */
4425
4426 void
4427 bpstat_clear_actions (void)
4428 {
4429 bpstat *bs;
4430
4431 if (inferior_ptid == null_ptid)
4432 return;
4433
4434 thread_info *tp = inferior_thread ();
4435 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4436 {
4437 bs->commands = NULL;
4438 bs->old_val.reset (nullptr);
4439 }
4440 }
4441
4442 /* Called when a command is about to proceed the inferior. */
4443
4444 static void
4445 breakpoint_about_to_proceed (void)
4446 {
4447 if (inferior_ptid != null_ptid)
4448 {
4449 struct thread_info *tp = inferior_thread ();
4450
4451 /* Allow inferior function calls in breakpoint commands to not
4452 interrupt the command list. When the call finishes
4453 successfully, the inferior will be standing at the same
4454 breakpoint as if nothing happened. */
4455 if (tp->control.in_infcall)
4456 return;
4457 }
4458
4459 breakpoint_proceeded = 1;
4460 }
4461
4462 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4463 or its equivalent. */
4464
4465 static int
4466 command_line_is_silent (struct command_line *cmd)
4467 {
4468 return cmd && (strcmp ("silent", cmd->line) == 0);
4469 }
4470
4471 /* Execute all the commands associated with all the breakpoints at
4472 this location. Any of these commands could cause the process to
4473 proceed beyond this point, etc. We look out for such changes by
4474 checking the global "breakpoint_proceeded" after each command.
4475
4476 Returns true if a breakpoint command resumed the inferior. In that
4477 case, it is the caller's responsibility to recall it again with the
4478 bpstat of the current thread. */
4479
4480 static int
4481 bpstat_do_actions_1 (bpstat **bsp)
4482 {
4483 bpstat *bs;
4484 int again = 0;
4485
4486 /* Avoid endless recursion if a `source' command is contained
4487 in bs->commands. */
4488 if (executing_breakpoint_commands)
4489 return 0;
4490
4491 scoped_restore save_executing
4492 = make_scoped_restore (&executing_breakpoint_commands, 1);
4493
4494 scoped_restore preventer = prevent_dont_repeat ();
4495
4496 /* This pointer will iterate over the list of bpstat's. */
4497 bs = *bsp;
4498
4499 breakpoint_proceeded = 0;
4500 for (; bs != NULL; bs = bs->next)
4501 {
4502 struct command_line *cmd = NULL;
4503
4504 /* Take ownership of the BSP's command tree, if it has one.
4505
4506 The command tree could legitimately contain commands like
4507 'step' and 'next', which call clear_proceed_status, which
4508 frees stop_bpstat's command tree. To make sure this doesn't
4509 free the tree we're executing out from under us, we need to
4510 take ownership of the tree ourselves. Since a given bpstat's
4511 commands are only executed once, we don't need to copy it; we
4512 can clear the pointer in the bpstat, and make sure we free
4513 the tree when we're done. */
4514 counted_command_line ccmd = bs->commands;
4515 bs->commands = NULL;
4516 if (ccmd != NULL)
4517 cmd = ccmd.get ();
4518 if (command_line_is_silent (cmd))
4519 {
4520 /* The action has been already done by bpstat_stop_status. */
4521 cmd = cmd->next;
4522 }
4523
4524 while (cmd != NULL)
4525 {
4526 execute_control_command (cmd);
4527
4528 if (breakpoint_proceeded)
4529 break;
4530 else
4531 cmd = cmd->next;
4532 }
4533
4534 if (breakpoint_proceeded)
4535 {
4536 if (current_ui->async)
4537 /* If we are in async mode, then the target might be still
4538 running, not stopped at any breakpoint, so nothing for
4539 us to do here -- just return to the event loop. */
4540 ;
4541 else
4542 /* In sync mode, when execute_control_command returns
4543 we're already standing on the next breakpoint.
4544 Breakpoint commands for that stop were not run, since
4545 execute_command does not run breakpoint commands --
4546 only command_line_handler does, but that one is not
4547 involved in execution of breakpoint commands. So, we
4548 can now execute breakpoint commands. It should be
4549 noted that making execute_command do bpstat actions is
4550 not an option -- in this case we'll have recursive
4551 invocation of bpstat for each breakpoint with a
4552 command, and can easily blow up GDB stack. Instead, we
4553 return true, which will trigger the caller to recall us
4554 with the new stop_bpstat. */
4555 again = 1;
4556 break;
4557 }
4558 }
4559 return again;
4560 }
4561
4562 /* Helper for bpstat_do_actions. Get the current thread, if there's
4563 one, is alive and has execution. Return NULL otherwise. */
4564
4565 static thread_info *
4566 get_bpstat_thread ()
4567 {
4568 if (inferior_ptid == null_ptid || !target_has_execution ())
4569 return NULL;
4570
4571 thread_info *tp = inferior_thread ();
4572 if (tp->state == THREAD_EXITED || tp->executing ())
4573 return NULL;
4574 return tp;
4575 }
4576
4577 void
4578 bpstat_do_actions (void)
4579 {
4580 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
4581 thread_info *tp;
4582
4583 /* Do any commands attached to breakpoint we are stopped at. */
4584 while ((tp = get_bpstat_thread ()) != NULL)
4585 {
4586 /* Since in sync mode, bpstat_do_actions may resume the
4587 inferior, and only return when it is stopped at the next
4588 breakpoint, we keep doing breakpoint actions until it returns
4589 false to indicate the inferior was not resumed. */
4590 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4591 break;
4592 }
4593
4594 cleanup_if_error.release ();
4595 }
4596
4597 /* Print out the (old or new) value associated with a watchpoint. */
4598
4599 static void
4600 watchpoint_value_print (struct value *val, struct ui_file *stream)
4601 {
4602 if (val == NULL)
4603 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
4604 else
4605 {
4606 struct value_print_options opts;
4607 get_user_print_options (&opts);
4608 value_print (val, stream, &opts);
4609 }
4610 }
4611
4612 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4613 debugging multiple threads. */
4614
4615 void
4616 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4617 {
4618 if (uiout->is_mi_like_p ())
4619 return;
4620
4621 uiout->text ("\n");
4622
4623 if (show_thread_that_caused_stop ())
4624 {
4625 struct thread_info *thr = inferior_thread ();
4626
4627 uiout->text ("Thread ");
4628 uiout->field_string ("thread-id", print_thread_id (thr));
4629
4630 const char *name = thread_name (thr);
4631 if (name != NULL)
4632 {
4633 uiout->text (" \"");
4634 uiout->field_string ("name", name);
4635 uiout->text ("\"");
4636 }
4637
4638 uiout->text (" hit ");
4639 }
4640 }
4641
4642 /* Generic routine for printing messages indicating why we
4643 stopped. The behavior of this function depends on the value
4644 'print_it' in the bpstat structure. Under some circumstances we
4645 may decide not to print anything here and delegate the task to
4646 normal_stop(). */
4647
4648 static enum print_stop_action
4649 print_bp_stop_message (bpstat *bs)
4650 {
4651 switch (bs->print_it)
4652 {
4653 case print_it_noop:
4654 /* Nothing should be printed for this bpstat entry. */
4655 return PRINT_UNKNOWN;
4656 break;
4657
4658 case print_it_done:
4659 /* We still want to print the frame, but we already printed the
4660 relevant messages. */
4661 return PRINT_SRC_AND_LOC;
4662 break;
4663
4664 case print_it_normal:
4665 {
4666 struct breakpoint *b = bs->breakpoint_at;
4667
4668 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4669 which has since been deleted. */
4670 if (b == NULL)
4671 return PRINT_UNKNOWN;
4672
4673 /* Normal case. Call the breakpoint's print_it method. */
4674 return b->ops->print_it (bs);
4675 }
4676 break;
4677
4678 default:
4679 internal_error (__FILE__, __LINE__,
4680 _("print_bp_stop_message: unrecognized enum value"));
4681 break;
4682 }
4683 }
4684
4685 /* See breakpoint.h. */
4686
4687 void
4688 print_solib_event (bool is_catchpoint)
4689 {
4690 bool any_deleted = !current_program_space->deleted_solibs.empty ();
4691 bool any_added = !current_program_space->added_solibs.empty ();
4692
4693 if (!is_catchpoint)
4694 {
4695 if (any_added || any_deleted)
4696 current_uiout->text (_("Stopped due to shared library event:\n"));
4697 else
4698 current_uiout->text (_("Stopped due to shared library event (no "
4699 "libraries added or removed)\n"));
4700 }
4701
4702 if (current_uiout->is_mi_like_p ())
4703 current_uiout->field_string ("reason",
4704 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4705
4706 if (any_deleted)
4707 {
4708 current_uiout->text (_(" Inferior unloaded "));
4709 ui_out_emit_list list_emitter (current_uiout, "removed");
4710 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
4711 {
4712 const std::string &name = current_program_space->deleted_solibs[ix];
4713
4714 if (ix > 0)
4715 current_uiout->text (" ");
4716 current_uiout->field_string ("library", name);
4717 current_uiout->text ("\n");
4718 }
4719 }
4720
4721 if (any_added)
4722 {
4723 current_uiout->text (_(" Inferior loaded "));
4724 ui_out_emit_list list_emitter (current_uiout, "added");
4725 bool first = true;
4726 for (so_list *iter : current_program_space->added_solibs)
4727 {
4728 if (!first)
4729 current_uiout->text (" ");
4730 first = false;
4731 current_uiout->field_string ("library", iter->so_name);
4732 current_uiout->text ("\n");
4733 }
4734 }
4735 }
4736
4737 /* Print a message indicating what happened. This is called from
4738 normal_stop(). The input to this routine is the head of the bpstat
4739 list - a list of the eventpoints that caused this stop. KIND is
4740 the target_waitkind for the stopping event. This
4741 routine calls the generic print routine for printing a message
4742 about reasons for stopping. This will print (for example) the
4743 "Breakpoint n," part of the output. The return value of this
4744 routine is one of:
4745
4746 PRINT_UNKNOWN: Means we printed nothing.
4747 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4748 code to print the location. An example is
4749 "Breakpoint 1, " which should be followed by
4750 the location.
4751 PRINT_SRC_ONLY: Means we printed something, but there is no need
4752 to also print the location part of the message.
4753 An example is the catch/throw messages, which
4754 don't require a location appended to the end.
4755 PRINT_NOTHING: We have done some printing and we don't need any
4756 further info to be printed. */
4757
4758 enum print_stop_action
4759 bpstat_print (bpstat *bs, int kind)
4760 {
4761 enum print_stop_action val;
4762
4763 /* Maybe another breakpoint in the chain caused us to stop.
4764 (Currently all watchpoints go on the bpstat whether hit or not.
4765 That probably could (should) be changed, provided care is taken
4766 with respect to bpstat_explains_signal). */
4767 for (; bs; bs = bs->next)
4768 {
4769 val = print_bp_stop_message (bs);
4770 if (val == PRINT_SRC_ONLY
4771 || val == PRINT_SRC_AND_LOC
4772 || val == PRINT_NOTHING)
4773 return val;
4774 }
4775
4776 /* If we had hit a shared library event breakpoint,
4777 print_bp_stop_message would print out this message. If we hit an
4778 OS-level shared library event, do the same thing. */
4779 if (kind == TARGET_WAITKIND_LOADED)
4780 {
4781 print_solib_event (false);
4782 return PRINT_NOTHING;
4783 }
4784
4785 /* We reached the end of the chain, or we got a null BS to start
4786 with and nothing was printed. */
4787 return PRINT_UNKNOWN;
4788 }
4789
4790 /* Evaluate the boolean expression EXP and return the result. */
4791
4792 static bool
4793 breakpoint_cond_eval (expression *exp)
4794 {
4795 struct value *mark = value_mark ();
4796 bool res = value_true (evaluate_expression (exp));
4797
4798 value_free_to_mark (mark);
4799 return res;
4800 }
4801
4802 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4803
4804 bpstat::bpstat (struct bp_location *bl, bpstat ***bs_link_pointer)
4805 : next (NULL),
4806 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
4807 breakpoint_at (bl->owner),
4808 commands (NULL),
4809 print (0),
4810 stop (0),
4811 print_it (print_it_normal)
4812 {
4813 **bs_link_pointer = this;
4814 *bs_link_pointer = &next;
4815 }
4816
4817 bpstat::bpstat ()
4818 : next (NULL),
4819 breakpoint_at (NULL),
4820 commands (NULL),
4821 print (0),
4822 stop (0),
4823 print_it (print_it_normal)
4824 {
4825 }
4826 \f
4827 /* The target has stopped with waitstatus WS. Check if any hardware
4828 watchpoints have triggered, according to the target. */
4829
4830 int
4831 watchpoints_triggered (const target_waitstatus &ws)
4832 {
4833 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
4834 CORE_ADDR addr;
4835
4836 if (!stopped_by_watchpoint)
4837 {
4838 /* We were not stopped by a watchpoint. Mark all watchpoints
4839 as not triggered. */
4840 for (breakpoint *b : all_breakpoints ())
4841 if (is_hardware_watchpoint (b))
4842 {
4843 struct watchpoint *w = (struct watchpoint *) b;
4844
4845 w->watchpoint_triggered = watch_triggered_no;
4846 }
4847
4848 return 0;
4849 }
4850
4851 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
4852 {
4853 /* We were stopped by a watchpoint, but we don't know where.
4854 Mark all watchpoints as unknown. */
4855 for (breakpoint *b : all_breakpoints ())
4856 if (is_hardware_watchpoint (b))
4857 {
4858 struct watchpoint *w = (struct watchpoint *) b;
4859
4860 w->watchpoint_triggered = watch_triggered_unknown;
4861 }
4862
4863 return 1;
4864 }
4865
4866 /* The target could report the data address. Mark watchpoints
4867 affected by this data address as triggered, and all others as not
4868 triggered. */
4869
4870 for (breakpoint *b : all_breakpoints ())
4871 if (is_hardware_watchpoint (b))
4872 {
4873 struct watchpoint *w = (struct watchpoint *) b;
4874
4875 w->watchpoint_triggered = watch_triggered_no;
4876 for (bp_location *loc : b->locations ())
4877 {
4878 if (is_masked_watchpoint (b))
4879 {
4880 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4881 CORE_ADDR start = loc->address & w->hw_wp_mask;
4882
4883 if (newaddr == start)
4884 {
4885 w->watchpoint_triggered = watch_triggered_yes;
4886 break;
4887 }
4888 }
4889 /* Exact match not required. Within range is sufficient. */
4890 else if (target_watchpoint_addr_within_range
4891 (current_inferior ()->top_target (), addr, loc->address,
4892 loc->length))
4893 {
4894 w->watchpoint_triggered = watch_triggered_yes;
4895 break;
4896 }
4897 }
4898 }
4899
4900 return 1;
4901 }
4902
4903 /* Possible return values for watchpoint_check. */
4904 enum wp_check_result
4905 {
4906 /* The watchpoint has been deleted. */
4907 WP_DELETED = 1,
4908
4909 /* The value has changed. */
4910 WP_VALUE_CHANGED = 2,
4911
4912 /* The value has not changed. */
4913 WP_VALUE_NOT_CHANGED = 3,
4914
4915 /* Ignore this watchpoint, no matter if the value changed or not. */
4916 WP_IGNORE = 4,
4917 };
4918
4919 #define BP_TEMPFLAG 1
4920 #define BP_HARDWAREFLAG 2
4921
4922 /* Evaluate watchpoint condition expression and check if its value
4923 changed. */
4924
4925 static wp_check_result
4926 watchpoint_check (bpstat *bs)
4927 {
4928 struct watchpoint *b;
4929 struct frame_info *fr;
4930 int within_current_scope;
4931
4932 /* BS is built from an existing struct breakpoint. */
4933 gdb_assert (bs->breakpoint_at != NULL);
4934 b = (struct watchpoint *) bs->breakpoint_at;
4935
4936 /* If this is a local watchpoint, we only want to check if the
4937 watchpoint frame is in scope if the current thread is the thread
4938 that was used to create the watchpoint. */
4939 if (!watchpoint_in_thread_scope (b))
4940 return WP_IGNORE;
4941
4942 if (b->exp_valid_block == NULL)
4943 within_current_scope = 1;
4944 else
4945 {
4946 struct frame_info *frame = get_current_frame ();
4947 struct gdbarch *frame_arch = get_frame_arch (frame);
4948 CORE_ADDR frame_pc = get_frame_pc (frame);
4949
4950 /* stack_frame_destroyed_p() returns a non-zero value if we're
4951 still in the function but the stack frame has already been
4952 invalidated. Since we can't rely on the values of local
4953 variables after the stack has been destroyed, we are treating
4954 the watchpoint in that state as `not changed' without further
4955 checking. Don't mark watchpoints as changed if the current
4956 frame is in an epilogue - even if they are in some other
4957 frame, our view of the stack is likely to be wrong and
4958 frame_find_by_id could error out. */
4959 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
4960 return WP_IGNORE;
4961
4962 fr = frame_find_by_id (b->watchpoint_frame);
4963 within_current_scope = (fr != NULL);
4964
4965 /* If we've gotten confused in the unwinder, we might have
4966 returned a frame that can't describe this variable. */
4967 if (within_current_scope)
4968 {
4969 struct symbol *function;
4970
4971 function = get_frame_function (fr);
4972 if (function == NULL
4973 || !contained_in (b->exp_valid_block, function->value_block ()))
4974 within_current_scope = 0;
4975 }
4976
4977 if (within_current_scope)
4978 /* If we end up stopping, the current frame will get selected
4979 in normal_stop. So this call to select_frame won't affect
4980 the user. */
4981 select_frame (fr);
4982 }
4983
4984 if (within_current_scope)
4985 {
4986 /* We use value_{,free_to_}mark because it could be a *long*
4987 time before we return to the command level and call
4988 free_all_values. We can't call free_all_values because we
4989 might be in the middle of evaluating a function call. */
4990
4991 struct value *mark;
4992 struct value *new_val;
4993
4994 if (is_masked_watchpoint (b))
4995 /* Since we don't know the exact trigger address (from
4996 stopped_data_address), just tell the user we've triggered
4997 a mask watchpoint. */
4998 return WP_VALUE_CHANGED;
4999
5000 mark = value_mark ();
5001 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
5002 NULL, NULL, false);
5003
5004 if (b->val_bitsize != 0)
5005 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5006
5007 /* We use value_equal_contents instead of value_equal because
5008 the latter coerces an array to a pointer, thus comparing just
5009 the address of the array instead of its contents. This is
5010 not what we want. */
5011 if ((b->val != NULL) != (new_val != NULL)
5012 || (b->val != NULL && !value_equal_contents (b->val.get (),
5013 new_val)))
5014 {
5015 bs->old_val = b->val;
5016 b->val = release_value (new_val);
5017 b->val_valid = true;
5018 if (new_val != NULL)
5019 value_free_to_mark (mark);
5020 return WP_VALUE_CHANGED;
5021 }
5022 else
5023 {
5024 /* Nothing changed. */
5025 value_free_to_mark (mark);
5026 return WP_VALUE_NOT_CHANGED;
5027 }
5028 }
5029 else
5030 {
5031 /* This seems like the only logical thing to do because
5032 if we temporarily ignored the watchpoint, then when
5033 we reenter the block in which it is valid it contains
5034 garbage (in the case of a function, it may have two
5035 garbage values, one before and one after the prologue).
5036 So we can't even detect the first assignment to it and
5037 watch after that (since the garbage may or may not equal
5038 the first value assigned). */
5039 /* We print all the stop information in
5040 breakpoint_ops->print_it, but in this case, by the time we
5041 call breakpoint_ops->print_it this bp will be deleted
5042 already. So we have no choice but print the information
5043 here. */
5044
5045 SWITCH_THRU_ALL_UIS ()
5046 {
5047 struct ui_out *uiout = current_uiout;
5048
5049 if (uiout->is_mi_like_p ())
5050 uiout->field_string
5051 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5052 uiout->message ("\nWatchpoint %pF deleted because the program has "
5053 "left the block in\n"
5054 "which its expression is valid.\n",
5055 signed_field ("wpnum", b->number));
5056 }
5057
5058 /* Make sure the watchpoint's commands aren't executed. */
5059 b->commands = NULL;
5060 watchpoint_del_at_next_stop (b);
5061
5062 return WP_DELETED;
5063 }
5064 }
5065
5066 /* Return true if it looks like target has stopped due to hitting
5067 breakpoint location BL. This function does not check if we should
5068 stop, only if BL explains the stop. */
5069
5070 static int
5071 bpstat_check_location (const struct bp_location *bl,
5072 const address_space *aspace, CORE_ADDR bp_addr,
5073 const target_waitstatus &ws)
5074 {
5075 struct breakpoint *b = bl->owner;
5076
5077 /* BL is from an existing breakpoint. */
5078 gdb_assert (b != NULL);
5079
5080 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5081 }
5082
5083 /* Determine if the watched values have actually changed, and we
5084 should stop. If not, set BS->stop to 0. */
5085
5086 static void
5087 bpstat_check_watchpoint (bpstat *bs)
5088 {
5089 const struct bp_location *bl;
5090 struct watchpoint *b;
5091
5092 /* BS is built for existing struct breakpoint. */
5093 bl = bs->bp_location_at.get ();
5094 gdb_assert (bl != NULL);
5095 b = (struct watchpoint *) bs->breakpoint_at;
5096 gdb_assert (b != NULL);
5097
5098 {
5099 int must_check_value = 0;
5100
5101 if (b->type == bp_watchpoint)
5102 /* For a software watchpoint, we must always check the
5103 watched value. */
5104 must_check_value = 1;
5105 else if (b->watchpoint_triggered == watch_triggered_yes)
5106 /* We have a hardware watchpoint (read, write, or access)
5107 and the target earlier reported an address watched by
5108 this watchpoint. */
5109 must_check_value = 1;
5110 else if (b->watchpoint_triggered == watch_triggered_unknown
5111 && b->type == bp_hardware_watchpoint)
5112 /* We were stopped by a hardware watchpoint, but the target could
5113 not report the data address. We must check the watchpoint's
5114 value. Access and read watchpoints are out of luck; without
5115 a data address, we can't figure it out. */
5116 must_check_value = 1;
5117
5118 if (must_check_value)
5119 {
5120 wp_check_result e;
5121
5122 try
5123 {
5124 e = watchpoint_check (bs);
5125 }
5126 catch (const gdb_exception &ex)
5127 {
5128 exception_fprintf (gdb_stderr, ex,
5129 "Error evaluating expression "
5130 "for watchpoint %d\n",
5131 b->number);
5132
5133 SWITCH_THRU_ALL_UIS ()
5134 {
5135 gdb_printf (_("Watchpoint %d deleted.\n"),
5136 b->number);
5137 }
5138 watchpoint_del_at_next_stop (b);
5139 e = WP_DELETED;
5140 }
5141
5142 switch (e)
5143 {
5144 case WP_DELETED:
5145 /* We've already printed what needs to be printed. */
5146 bs->print_it = print_it_done;
5147 /* Stop. */
5148 break;
5149 case WP_IGNORE:
5150 bs->print_it = print_it_noop;
5151 bs->stop = 0;
5152 break;
5153 case WP_VALUE_CHANGED:
5154 if (b->type == bp_read_watchpoint)
5155 {
5156 /* There are two cases to consider here:
5157
5158 1. We're watching the triggered memory for reads.
5159 In that case, trust the target, and always report
5160 the watchpoint hit to the user. Even though
5161 reads don't cause value changes, the value may
5162 have changed since the last time it was read, and
5163 since we're not trapping writes, we will not see
5164 those, and as such we should ignore our notion of
5165 old value.
5166
5167 2. We're watching the triggered memory for both
5168 reads and writes. There are two ways this may
5169 happen:
5170
5171 2.1. This is a target that can't break on data
5172 reads only, but can break on accesses (reads or
5173 writes), such as e.g., x86. We detect this case
5174 at the time we try to insert read watchpoints.
5175
5176 2.2. Otherwise, the target supports read
5177 watchpoints, but, the user set an access or write
5178 watchpoint watching the same memory as this read
5179 watchpoint.
5180
5181 If we're watching memory writes as well as reads,
5182 ignore watchpoint hits when we find that the
5183 value hasn't changed, as reads don't cause
5184 changes. This still gives false positives when
5185 the program writes the same value to memory as
5186 what there was already in memory (we will confuse
5187 it for a read), but it's much better than
5188 nothing. */
5189
5190 int other_write_watchpoint = 0;
5191
5192 if (bl->watchpoint_type == hw_read)
5193 {
5194 for (breakpoint *other_b : all_breakpoints ())
5195 if (other_b->type == bp_hardware_watchpoint
5196 || other_b->type == bp_access_watchpoint)
5197 {
5198 struct watchpoint *other_w =
5199 (struct watchpoint *) other_b;
5200
5201 if (other_w->watchpoint_triggered
5202 == watch_triggered_yes)
5203 {
5204 other_write_watchpoint = 1;
5205 break;
5206 }
5207 }
5208 }
5209
5210 if (other_write_watchpoint
5211 || bl->watchpoint_type == hw_access)
5212 {
5213 /* We're watching the same memory for writes,
5214 and the value changed since the last time we
5215 updated it, so this trap must be for a write.
5216 Ignore it. */
5217 bs->print_it = print_it_noop;
5218 bs->stop = 0;
5219 }
5220 }
5221 break;
5222 case WP_VALUE_NOT_CHANGED:
5223 if (b->type == bp_hardware_watchpoint
5224 || b->type == bp_watchpoint)
5225 {
5226 /* Don't stop: write watchpoints shouldn't fire if
5227 the value hasn't changed. */
5228 bs->print_it = print_it_noop;
5229 bs->stop = 0;
5230 }
5231 /* Stop. */
5232 break;
5233 default:
5234 /* Can't happen. */
5235 break;
5236 }
5237 }
5238 else /* must_check_value == 0 */
5239 {
5240 /* This is a case where some watchpoint(s) triggered, but
5241 not at the address of this watchpoint, or else no
5242 watchpoint triggered after all. So don't print
5243 anything for this watchpoint. */
5244 bs->print_it = print_it_noop;
5245 bs->stop = 0;
5246 }
5247 }
5248 }
5249
5250 /* For breakpoints that are currently marked as telling gdb to stop,
5251 check conditions (condition proper, frame, thread and ignore count)
5252 of breakpoint referred to by BS. If we should not stop for this
5253 breakpoint, set BS->stop to 0. */
5254
5255 static void
5256 bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread)
5257 {
5258 const struct bp_location *bl;
5259 struct breakpoint *b;
5260 /* Assume stop. */
5261 bool condition_result = true;
5262 struct expression *cond;
5263
5264 gdb_assert (bs->stop);
5265
5266 /* BS is built for existing struct breakpoint. */
5267 bl = bs->bp_location_at.get ();
5268 gdb_assert (bl != NULL);
5269 b = bs->breakpoint_at;
5270 gdb_assert (b != NULL);
5271
5272 /* Even if the target evaluated the condition on its end and notified GDB, we
5273 need to do so again since GDB does not know if we stopped due to a
5274 breakpoint or a single step breakpoint. */
5275
5276 if (frame_id_p (b->frame_id)
5277 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5278 {
5279 bs->stop = 0;
5280 return;
5281 }
5282
5283 /* If this is a thread/task-specific breakpoint, don't waste cpu
5284 evaluating the condition if this isn't the specified
5285 thread/task. */
5286 if ((b->thread != -1 && b->thread != thread->global_num)
5287 || (b->task != 0 && b->task != ada_get_task_number (thread)))
5288 {
5289 bs->stop = 0;
5290 return;
5291 }
5292
5293 /* Evaluate extension language breakpoints that have a "stop" method
5294 implemented. */
5295 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5296
5297 if (is_watchpoint (b))
5298 {
5299 struct watchpoint *w = (struct watchpoint *) b;
5300
5301 cond = w->cond_exp.get ();
5302 }
5303 else
5304 cond = bl->cond.get ();
5305
5306 if (cond && b->disposition != disp_del_at_next_stop)
5307 {
5308 int within_current_scope = 1;
5309 struct watchpoint * w;
5310
5311 /* We use value_mark and value_free_to_mark because it could
5312 be a long time before we return to the command level and
5313 call free_all_values. We can't call free_all_values
5314 because we might be in the middle of evaluating a
5315 function call. */
5316 struct value *mark = value_mark ();
5317
5318 if (is_watchpoint (b))
5319 w = (struct watchpoint *) b;
5320 else
5321 w = NULL;
5322
5323 /* Need to select the frame, with all that implies so that
5324 the conditions will have the right context. Because we
5325 use the frame, we will not see an inlined function's
5326 variables when we arrive at a breakpoint at the start
5327 of the inlined function; the current frame will be the
5328 call site. */
5329 if (w == NULL || w->cond_exp_valid_block == NULL)
5330 select_frame (get_current_frame ());
5331 else
5332 {
5333 struct frame_info *frame;
5334
5335 /* For local watchpoint expressions, which particular
5336 instance of a local is being watched matters, so we
5337 keep track of the frame to evaluate the expression
5338 in. To evaluate the condition however, it doesn't
5339 really matter which instantiation of the function
5340 where the condition makes sense triggers the
5341 watchpoint. This allows an expression like "watch
5342 global if q > 10" set in `func', catch writes to
5343 global on all threads that call `func', or catch
5344 writes on all recursive calls of `func' by a single
5345 thread. We simply always evaluate the condition in
5346 the innermost frame that's executing where it makes
5347 sense to evaluate the condition. It seems
5348 intuitive. */
5349 frame = block_innermost_frame (w->cond_exp_valid_block);
5350 if (frame != NULL)
5351 select_frame (frame);
5352 else
5353 within_current_scope = 0;
5354 }
5355 if (within_current_scope)
5356 {
5357 try
5358 {
5359 condition_result = breakpoint_cond_eval (cond);
5360 }
5361 catch (const gdb_exception &ex)
5362 {
5363 exception_fprintf (gdb_stderr, ex,
5364 "Error in testing breakpoint condition:\n");
5365 }
5366 }
5367 else
5368 {
5369 warning (_("Watchpoint condition cannot be tested "
5370 "in the current scope"));
5371 /* If we failed to set the right context for this
5372 watchpoint, unconditionally report it. */
5373 }
5374 /* FIXME-someday, should give breakpoint #. */
5375 value_free_to_mark (mark);
5376 }
5377
5378 if (cond && !condition_result)
5379 {
5380 bs->stop = 0;
5381 }
5382 else if (b->ignore_count > 0)
5383 {
5384 b->ignore_count--;
5385 bs->stop = 0;
5386 /* Increase the hit count even though we don't stop. */
5387 ++(b->hit_count);
5388 gdb::observers::breakpoint_modified.notify (b);
5389 }
5390 }
5391
5392 /* Returns true if we need to track moribund locations of LOC's type
5393 on the current target. */
5394
5395 static int
5396 need_moribund_for_location_type (struct bp_location *loc)
5397 {
5398 return ((loc->loc_type == bp_loc_software_breakpoint
5399 && !target_supports_stopped_by_sw_breakpoint ())
5400 || (loc->loc_type == bp_loc_hardware_breakpoint
5401 && !target_supports_stopped_by_hw_breakpoint ()));
5402 }
5403
5404 /* See breakpoint.h. */
5405
5406 bpstat *
5407 build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
5408 const target_waitstatus &ws)
5409 {
5410 bpstat *bs_head = nullptr, **bs_link = &bs_head;
5411
5412 for (breakpoint *b : all_breakpoints ())
5413 {
5414 if (!breakpoint_enabled (b))
5415 continue;
5416
5417 for (bp_location *bl : b->locations ())
5418 {
5419 /* For hardware watchpoints, we look only at the first
5420 location. The watchpoint_check function will work on the
5421 entire expression, not the individual locations. For
5422 read watchpoints, the watchpoints_triggered function has
5423 checked all locations already. */
5424 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5425 break;
5426
5427 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
5428 continue;
5429
5430 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5431 continue;
5432
5433 /* Come here if it's a watchpoint, or if the break address
5434 matches. */
5435
5436 bpstat *bs = new bpstat (bl, &bs_link); /* Alloc a bpstat to
5437 explain stop. */
5438
5439 /* Assume we stop. Should we find a watchpoint that is not
5440 actually triggered, or if the condition of the breakpoint
5441 evaluates as false, we'll reset 'stop' to 0. */
5442 bs->stop = 1;
5443 bs->print = 1;
5444
5445 /* If this is a scope breakpoint, mark the associated
5446 watchpoint as triggered so that we will handle the
5447 out-of-scope event. We'll get to the watchpoint next
5448 iteration. */
5449 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5450 {
5451 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5452
5453 w->watchpoint_triggered = watch_triggered_yes;
5454 }
5455 }
5456 }
5457
5458 /* Check if a moribund breakpoint explains the stop. */
5459 if (!target_supports_stopped_by_sw_breakpoint ()
5460 || !target_supports_stopped_by_hw_breakpoint ())
5461 {
5462 for (bp_location *loc : moribund_locations)
5463 {
5464 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5465 && need_moribund_for_location_type (loc))
5466 {
5467 bpstat *bs = new bpstat (loc, &bs_link);
5468 /* For hits of moribund locations, we should just proceed. */
5469 bs->stop = 0;
5470 bs->print = 0;
5471 bs->print_it = print_it_noop;
5472 }
5473 }
5474 }
5475
5476 return bs_head;
5477 }
5478
5479 /* See breakpoint.h. */
5480
5481 bpstat *
5482 bpstat_stop_status (const address_space *aspace,
5483 CORE_ADDR bp_addr, thread_info *thread,
5484 const target_waitstatus &ws,
5485 bpstat *stop_chain)
5486 {
5487 struct breakpoint *b = NULL;
5488 /* First item of allocated bpstat's. */
5489 bpstat *bs_head = stop_chain;
5490 bpstat *bs;
5491 int need_remove_insert;
5492 int removed_any;
5493
5494 /* First, build the bpstat chain with locations that explain a
5495 target stop, while being careful to not set the target running,
5496 as that may invalidate locations (in particular watchpoint
5497 locations are recreated). Resuming will happen here with
5498 breakpoint conditions or watchpoint expressions that include
5499 inferior function calls. */
5500 if (bs_head == NULL)
5501 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5502
5503 /* A bit of special processing for shlib breakpoints. We need to
5504 process solib loading here, so that the lists of loaded and
5505 unloaded libraries are correct before we handle "catch load" and
5506 "catch unload". */
5507 for (bs = bs_head; bs != NULL; bs = bs->next)
5508 {
5509 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5510 {
5511 handle_solib_event ();
5512 break;
5513 }
5514 }
5515
5516 /* Now go through the locations that caused the target to stop, and
5517 check whether we're interested in reporting this stop to higher
5518 layers, or whether we should resume the target transparently. */
5519
5520 removed_any = 0;
5521
5522 for (bs = bs_head; bs != NULL; bs = bs->next)
5523 {
5524 if (!bs->stop)
5525 continue;
5526
5527 b = bs->breakpoint_at;
5528 b->ops->check_status (bs);
5529 if (bs->stop)
5530 {
5531 bpstat_check_breakpoint_conditions (bs, thread);
5532
5533 if (bs->stop)
5534 {
5535 ++(b->hit_count);
5536
5537 /* We will stop here. */
5538 if (b->disposition == disp_disable)
5539 {
5540 --(b->enable_count);
5541 if (b->enable_count <= 0)
5542 b->enable_state = bp_disabled;
5543 removed_any = 1;
5544 }
5545 gdb::observers::breakpoint_modified.notify (b);
5546 if (b->silent)
5547 bs->print = 0;
5548 bs->commands = b->commands;
5549 if (command_line_is_silent (bs->commands
5550 ? bs->commands.get () : NULL))
5551 bs->print = 0;
5552
5553 b->ops->after_condition_true (bs);
5554 }
5555
5556 }
5557
5558 /* Print nothing for this entry if we don't stop or don't
5559 print. */
5560 if (!bs->stop || !bs->print)
5561 bs->print_it = print_it_noop;
5562 }
5563
5564 /* If we aren't stopping, the value of some hardware watchpoint may
5565 not have changed, but the intermediate memory locations we are
5566 watching may have. Don't bother if we're stopping; this will get
5567 done later. */
5568 need_remove_insert = 0;
5569 if (! bpstat_causes_stop (bs_head))
5570 for (bs = bs_head; bs != NULL; bs = bs->next)
5571 if (!bs->stop
5572 && bs->breakpoint_at
5573 && is_hardware_watchpoint (bs->breakpoint_at))
5574 {
5575 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5576
5577 update_watchpoint (w, 0 /* don't reparse. */);
5578 need_remove_insert = 1;
5579 }
5580
5581 if (need_remove_insert)
5582 update_global_location_list (UGLL_MAY_INSERT);
5583 else if (removed_any)
5584 update_global_location_list (UGLL_DONT_INSERT);
5585
5586 return bs_head;
5587 }
5588
5589 /* See breakpoint.h. */
5590
5591 bpstat *
5592 bpstat_stop_status_nowatch (const address_space *aspace, CORE_ADDR bp_addr,
5593 thread_info *thread, const target_waitstatus &ws)
5594 {
5595 gdb_assert (!target_stopped_by_watchpoint ());
5596
5597 /* Clear all watchpoints' 'watchpoint_triggered' value from a
5598 previous stop to avoid confusing bpstat_stop_status. */
5599 watchpoints_triggered (ws);
5600
5601 return bpstat_stop_status (aspace, bp_addr, thread, ws);
5602 }
5603
5604 static void
5605 handle_jit_event (CORE_ADDR address)
5606 {
5607 struct gdbarch *gdbarch;
5608
5609 infrun_debug_printf ("handling bp_jit_event");
5610
5611 /* Switch terminal for any messages produced by
5612 breakpoint_re_set. */
5613 target_terminal::ours_for_output ();
5614
5615 gdbarch = get_frame_arch (get_current_frame ());
5616 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5617 thus it is expected that its objectfile can be found through
5618 minimal symbol lookup. If it doesn't work (and assert fails), it
5619 most likely means that `jit_breakpoint_re_set` was changes and this
5620 function needs to be updated too. */
5621 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5622 gdb_assert (jit_bp_sym.objfile != nullptr);
5623 jit_event_handler (gdbarch, jit_bp_sym.objfile);
5624
5625 target_terminal::inferior ();
5626 }
5627
5628 /* Prepare WHAT final decision for infrun. */
5629
5630 /* Decide what infrun needs to do with this bpstat. */
5631
5632 struct bpstat_what
5633 bpstat_what (bpstat *bs_head)
5634 {
5635 struct bpstat_what retval;
5636 bpstat *bs;
5637
5638 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5639 retval.call_dummy = STOP_NONE;
5640 retval.is_longjmp = false;
5641
5642 for (bs = bs_head; bs != NULL; bs = bs->next)
5643 {
5644 /* Extract this BS's action. After processing each BS, we check
5645 if its action overrides all we've seem so far. */
5646 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5647 enum bptype bptype;
5648
5649 if (bs->breakpoint_at == NULL)
5650 {
5651 /* I suspect this can happen if it was a momentary
5652 breakpoint which has since been deleted. */
5653 bptype = bp_none;
5654 }
5655 else
5656 bptype = bs->breakpoint_at->type;
5657
5658 switch (bptype)
5659 {
5660 case bp_none:
5661 break;
5662 case bp_breakpoint:
5663 case bp_hardware_breakpoint:
5664 case bp_single_step:
5665 case bp_until:
5666 case bp_finish:
5667 case bp_shlib_event:
5668 if (bs->stop)
5669 {
5670 if (bs->print)
5671 this_action = BPSTAT_WHAT_STOP_NOISY;
5672 else
5673 this_action = BPSTAT_WHAT_STOP_SILENT;
5674 }
5675 else
5676 this_action = BPSTAT_WHAT_SINGLE;
5677 break;
5678 case bp_watchpoint:
5679 case bp_hardware_watchpoint:
5680 case bp_read_watchpoint:
5681 case bp_access_watchpoint:
5682 if (bs->stop)
5683 {
5684 if (bs->print)
5685 this_action = BPSTAT_WHAT_STOP_NOISY;
5686 else
5687 this_action = BPSTAT_WHAT_STOP_SILENT;
5688 }
5689 else
5690 {
5691 /* There was a watchpoint, but we're not stopping.
5692 This requires no further action. */
5693 }
5694 break;
5695 case bp_longjmp:
5696 case bp_longjmp_call_dummy:
5697 case bp_exception:
5698 if (bs->stop)
5699 {
5700 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5701 retval.is_longjmp = bptype != bp_exception;
5702 }
5703 else
5704 this_action = BPSTAT_WHAT_SINGLE;
5705 break;
5706 case bp_longjmp_resume:
5707 case bp_exception_resume:
5708 if (bs->stop)
5709 {
5710 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5711 retval.is_longjmp = bptype == bp_longjmp_resume;
5712 }
5713 else
5714 this_action = BPSTAT_WHAT_SINGLE;
5715 break;
5716 case bp_step_resume:
5717 if (bs->stop)
5718 this_action = BPSTAT_WHAT_STEP_RESUME;
5719 else
5720 {
5721 /* It is for the wrong frame. */
5722 this_action = BPSTAT_WHAT_SINGLE;
5723 }
5724 break;
5725 case bp_hp_step_resume:
5726 if (bs->stop)
5727 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5728 else
5729 {
5730 /* It is for the wrong frame. */
5731 this_action = BPSTAT_WHAT_SINGLE;
5732 }
5733 break;
5734 case bp_watchpoint_scope:
5735 case bp_thread_event:
5736 case bp_overlay_event:
5737 case bp_longjmp_master:
5738 case bp_std_terminate_master:
5739 case bp_exception_master:
5740 this_action = BPSTAT_WHAT_SINGLE;
5741 break;
5742 case bp_catchpoint:
5743 if (bs->stop)
5744 {
5745 if (bs->print)
5746 this_action = BPSTAT_WHAT_STOP_NOISY;
5747 else
5748 this_action = BPSTAT_WHAT_STOP_SILENT;
5749 }
5750 else
5751 {
5752 /* Some catchpoints are implemented with breakpoints.
5753 For those, we need to step over the breakpoint. */
5754 if (bs->bp_location_at->loc_type != bp_loc_other)
5755 this_action = BPSTAT_WHAT_SINGLE;
5756 }
5757 break;
5758 case bp_jit_event:
5759 this_action = BPSTAT_WHAT_SINGLE;
5760 break;
5761 case bp_call_dummy:
5762 /* Make sure the action is stop (silent or noisy),
5763 so infrun.c pops the dummy frame. */
5764 retval.call_dummy = STOP_STACK_DUMMY;
5765 this_action = BPSTAT_WHAT_STOP_SILENT;
5766 break;
5767 case bp_std_terminate:
5768 /* Make sure the action is stop (silent or noisy),
5769 so infrun.c pops the dummy frame. */
5770 retval.call_dummy = STOP_STD_TERMINATE;
5771 this_action = BPSTAT_WHAT_STOP_SILENT;
5772 break;
5773 case bp_tracepoint:
5774 case bp_fast_tracepoint:
5775 case bp_static_tracepoint:
5776 /* Tracepoint hits should not be reported back to GDB, and
5777 if one got through somehow, it should have been filtered
5778 out already. */
5779 internal_error (__FILE__, __LINE__,
5780 _("bpstat_what: tracepoint encountered"));
5781 break;
5782 case bp_gnu_ifunc_resolver:
5783 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5784 this_action = BPSTAT_WHAT_SINGLE;
5785 break;
5786 case bp_gnu_ifunc_resolver_return:
5787 /* The breakpoint will be removed, execution will restart from the
5788 PC of the former breakpoint. */
5789 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5790 break;
5791
5792 case bp_dprintf:
5793 if (bs->stop)
5794 this_action = BPSTAT_WHAT_STOP_SILENT;
5795 else
5796 this_action = BPSTAT_WHAT_SINGLE;
5797 break;
5798
5799 default:
5800 internal_error (__FILE__, __LINE__,
5801 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5802 }
5803
5804 retval.main_action = std::max (retval.main_action, this_action);
5805 }
5806
5807 return retval;
5808 }
5809
5810 void
5811 bpstat_run_callbacks (bpstat *bs_head)
5812 {
5813 bpstat *bs;
5814
5815 for (bs = bs_head; bs != NULL; bs = bs->next)
5816 {
5817 struct breakpoint *b = bs->breakpoint_at;
5818
5819 if (b == NULL)
5820 continue;
5821 switch (b->type)
5822 {
5823 case bp_jit_event:
5824 handle_jit_event (bs->bp_location_at->address);
5825 break;
5826 case bp_gnu_ifunc_resolver:
5827 gnu_ifunc_resolver_stop (b);
5828 break;
5829 case bp_gnu_ifunc_resolver_return:
5830 gnu_ifunc_resolver_return_stop (b);
5831 break;
5832 }
5833 }
5834 }
5835
5836 /* See breakpoint.h. */
5837
5838 bool
5839 bpstat_should_step ()
5840 {
5841 for (breakpoint *b : all_breakpoints ())
5842 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5843 return true;
5844
5845 return false;
5846 }
5847
5848 /* See breakpoint.h. */
5849
5850 bool
5851 bpstat_causes_stop (bpstat *bs)
5852 {
5853 for (; bs != NULL; bs = bs->next)
5854 if (bs->stop)
5855 return true;
5856
5857 return false;
5858 }
5859
5860 \f
5861
5862 /* Compute a number of spaces suitable to indent the next line
5863 so it starts at the position corresponding to the table column
5864 named COL_NAME in the currently active table of UIOUT. */
5865
5866 static int
5867 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5868 {
5869 int i, total_width, width, align;
5870 const char *text;
5871
5872 total_width = 0;
5873 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
5874 {
5875 if (strcmp (text, col_name) == 0)
5876 return total_width;
5877
5878 total_width += width + 1;
5879 }
5880
5881 return 0;
5882 }
5883
5884 /* Determine if the locations of this breakpoint will have their conditions
5885 evaluated by the target, host or a mix of both. Returns the following:
5886
5887 "host": Host evals condition.
5888 "host or target": Host or Target evals condition.
5889 "target": Target evals condition.
5890 */
5891
5892 static const char *
5893 bp_condition_evaluator (struct breakpoint *b)
5894 {
5895 char host_evals = 0;
5896 char target_evals = 0;
5897
5898 if (!b)
5899 return NULL;
5900
5901 if (!is_breakpoint (b))
5902 return NULL;
5903
5904 if (gdb_evaluates_breakpoint_condition_p ()
5905 || !target_supports_evaluation_of_breakpoint_conditions ())
5906 return condition_evaluation_host;
5907
5908 for (bp_location *bl : b->locations ())
5909 {
5910 if (bl->cond_bytecode)
5911 target_evals++;
5912 else
5913 host_evals++;
5914 }
5915
5916 if (host_evals && target_evals)
5917 return condition_evaluation_both;
5918 else if (target_evals)
5919 return condition_evaluation_target;
5920 else
5921 return condition_evaluation_host;
5922 }
5923
5924 /* Determine the breakpoint location's condition evaluator. This is
5925 similar to bp_condition_evaluator, but for locations. */
5926
5927 static const char *
5928 bp_location_condition_evaluator (struct bp_location *bl)
5929 {
5930 if (bl && !is_breakpoint (bl->owner))
5931 return NULL;
5932
5933 if (gdb_evaluates_breakpoint_condition_p ()
5934 || !target_supports_evaluation_of_breakpoint_conditions ())
5935 return condition_evaluation_host;
5936
5937 if (bl && bl->cond_bytecode)
5938 return condition_evaluation_target;
5939 else
5940 return condition_evaluation_host;
5941 }
5942
5943 /* Print the LOC location out of the list of B->LOC locations. */
5944
5945 static void
5946 print_breakpoint_location (struct breakpoint *b,
5947 struct bp_location *loc)
5948 {
5949 struct ui_out *uiout = current_uiout;
5950
5951 scoped_restore_current_program_space restore_pspace;
5952
5953 if (loc != NULL && loc->shlib_disabled)
5954 loc = NULL;
5955
5956 if (loc != NULL)
5957 set_current_program_space (loc->pspace);
5958
5959 if (b->display_canonical)
5960 uiout->field_string ("what", event_location_to_string (b->location.get ()));
5961 else if (loc && loc->symtab)
5962 {
5963 const struct symbol *sym = loc->symbol;
5964
5965 if (sym)
5966 {
5967 uiout->text ("in ");
5968 uiout->field_string ("func", sym->print_name (),
5969 function_name_style.style ());
5970 uiout->text (" ");
5971 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5972 uiout->text ("at ");
5973 }
5974 uiout->field_string ("file",
5975 symtab_to_filename_for_display (loc->symtab),
5976 file_name_style.style ());
5977 uiout->text (":");
5978
5979 if (uiout->is_mi_like_p ())
5980 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
5981
5982 uiout->field_signed ("line", loc->line_number);
5983 }
5984 else if (loc)
5985 {
5986 string_file stb;
5987
5988 print_address_symbolic (loc->gdbarch, loc->address, &stb,
5989 demangle, "");
5990 uiout->field_stream ("at", stb);
5991 }
5992 else
5993 {
5994 uiout->field_string ("pending",
5995 event_location_to_string (b->location.get ()));
5996 /* If extra_string is available, it could be holding a condition
5997 or dprintf arguments. In either case, make sure it is printed,
5998 too, but only for non-MI streams. */
5999 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6000 {
6001 if (b->type == bp_dprintf)
6002 uiout->text (",");
6003 else
6004 uiout->text (" ");
6005 uiout->text (b->extra_string.get ());
6006 }
6007 }
6008
6009 if (loc && is_breakpoint (b)
6010 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6011 && bp_condition_evaluator (b) == condition_evaluation_both)
6012 {
6013 uiout->text (" (");
6014 uiout->field_string ("evaluated-by",
6015 bp_location_condition_evaluator (loc));
6016 uiout->text (")");
6017 }
6018 }
6019
6020 static const char *
6021 bptype_string (enum bptype type)
6022 {
6023 struct ep_type_description
6024 {
6025 enum bptype type;
6026 const char *description;
6027 };
6028 static struct ep_type_description bptypes[] =
6029 {
6030 {bp_none, "?deleted?"},
6031 {bp_breakpoint, "breakpoint"},
6032 {bp_hardware_breakpoint, "hw breakpoint"},
6033 {bp_single_step, "sw single-step"},
6034 {bp_until, "until"},
6035 {bp_finish, "finish"},
6036 {bp_watchpoint, "watchpoint"},
6037 {bp_hardware_watchpoint, "hw watchpoint"},
6038 {bp_read_watchpoint, "read watchpoint"},
6039 {bp_access_watchpoint, "acc watchpoint"},
6040 {bp_longjmp, "longjmp"},
6041 {bp_longjmp_resume, "longjmp resume"},
6042 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6043 {bp_exception, "exception"},
6044 {bp_exception_resume, "exception resume"},
6045 {bp_step_resume, "step resume"},
6046 {bp_hp_step_resume, "high-priority step resume"},
6047 {bp_watchpoint_scope, "watchpoint scope"},
6048 {bp_call_dummy, "call dummy"},
6049 {bp_std_terminate, "std::terminate"},
6050 {bp_shlib_event, "shlib events"},
6051 {bp_thread_event, "thread events"},
6052 {bp_overlay_event, "overlay events"},
6053 {bp_longjmp_master, "longjmp master"},
6054 {bp_std_terminate_master, "std::terminate master"},
6055 {bp_exception_master, "exception master"},
6056 {bp_catchpoint, "catchpoint"},
6057 {bp_tracepoint, "tracepoint"},
6058 {bp_fast_tracepoint, "fast tracepoint"},
6059 {bp_static_tracepoint, "static tracepoint"},
6060 {bp_dprintf, "dprintf"},
6061 {bp_jit_event, "jit events"},
6062 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6063 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6064 };
6065
6066 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6067 || ((int) type != bptypes[(int) type].type))
6068 internal_error (__FILE__, __LINE__,
6069 _("bptypes table does not describe type #%d."),
6070 (int) type);
6071
6072 return bptypes[(int) type].description;
6073 }
6074
6075 /* For MI, output a field named 'thread-groups' with a list as the value.
6076 For CLI, prefix the list with the string 'inf'. */
6077
6078 static void
6079 output_thread_groups (struct ui_out *uiout,
6080 const char *field_name,
6081 const std::vector<int> &inf_nums,
6082 int mi_only)
6083 {
6084 int is_mi = uiout->is_mi_like_p ();
6085
6086 /* For backward compatibility, don't display inferiors in CLI unless
6087 there are several. Always display them for MI. */
6088 if (!is_mi && mi_only)
6089 return;
6090
6091 ui_out_emit_list list_emitter (uiout, field_name);
6092
6093 for (size_t i = 0; i < inf_nums.size (); i++)
6094 {
6095 if (is_mi)
6096 {
6097 char mi_group[10];
6098
6099 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
6100 uiout->field_string (NULL, mi_group);
6101 }
6102 else
6103 {
6104 if (i == 0)
6105 uiout->text (" inf ");
6106 else
6107 uiout->text (", ");
6108
6109 uiout->text (plongest (inf_nums[i]));
6110 }
6111 }
6112 }
6113
6114 /* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6115 instead of going via breakpoint_ops::print_one. This makes "maint
6116 info breakpoints" show the software breakpoint locations of
6117 catchpoints, which are considered internal implementation
6118 detail. Returns true if RAW_LOC is false and if the breakpoint's
6119 print_one method did something; false otherwise. */
6120
6121 static bool
6122 print_one_breakpoint_location (struct breakpoint *b,
6123 struct bp_location *loc,
6124 int loc_number,
6125 struct bp_location **last_loc,
6126 int allflag, bool raw_loc)
6127 {
6128 struct command_line *l;
6129 static char bpenables[] = "nynny";
6130
6131 struct ui_out *uiout = current_uiout;
6132 int header_of_multiple = 0;
6133 int part_of_multiple = (loc != NULL);
6134 struct value_print_options opts;
6135
6136 get_user_print_options (&opts);
6137
6138 gdb_assert (!loc || loc_number != 0);
6139 /* See comment in print_one_breakpoint concerning treatment of
6140 breakpoints with single disabled location. */
6141 if (loc == NULL
6142 && (b->loc != NULL
6143 && (b->loc->next != NULL
6144 || !b->loc->enabled || b->loc->disabled_by_cond)))
6145 header_of_multiple = 1;
6146 if (loc == NULL)
6147 loc = b->loc;
6148
6149 annotate_record ();
6150
6151 /* 1 */
6152 annotate_field (0);
6153 if (part_of_multiple)
6154 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
6155 else
6156 uiout->field_signed ("number", b->number);
6157
6158 /* 2 */
6159 annotate_field (1);
6160 if (part_of_multiple)
6161 uiout->field_skip ("type");
6162 else
6163 uiout->field_string ("type", bptype_string (b->type));
6164
6165 /* 3 */
6166 annotate_field (2);
6167 if (part_of_multiple)
6168 uiout->field_skip ("disp");
6169 else
6170 uiout->field_string ("disp", bpdisp_text (b->disposition));
6171
6172 /* 4 */
6173 annotate_field (3);
6174 /* For locations that are disabled because of an invalid condition,
6175 display "N*" on CLI, where "*" refers to a footnote below the
6176 table. For MI, simply display a "N" without a footnote. */
6177 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
6178 if (part_of_multiple)
6179 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
6180 : (loc->enabled ? "y" : "n")));
6181 else
6182 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6183
6184 /* 5 and 6 */
6185 bool result = false;
6186 if (!raw_loc && b->ops != NULL && b->ops->print_one (b, last_loc))
6187 result = true;
6188 else
6189 {
6190 if (is_watchpoint (b))
6191 {
6192 struct watchpoint *w = (struct watchpoint *) b;
6193
6194 /* Field 4, the address, is omitted (which makes the columns
6195 not line up too nicely with the headers, but the effect
6196 is relatively readable). */
6197 if (opts.addressprint)
6198 uiout->field_skip ("addr");
6199 annotate_field (5);
6200 uiout->field_string ("what", w->exp_string.get ());
6201 }
6202 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6203 || is_ada_exception_catchpoint (b))
6204 {
6205 if (opts.addressprint)
6206 {
6207 annotate_field (4);
6208 if (header_of_multiple)
6209 uiout->field_string ("addr", "<MULTIPLE>",
6210 metadata_style.style ());
6211 else if (b->loc == NULL || loc->shlib_disabled)
6212 uiout->field_string ("addr", "<PENDING>",
6213 metadata_style.style ());
6214 else
6215 uiout->field_core_addr ("addr",
6216 loc->gdbarch, loc->address);
6217 }
6218 annotate_field (5);
6219 if (!header_of_multiple)
6220 print_breakpoint_location (b, loc);
6221 if (b->loc)
6222 *last_loc = b->loc;
6223 }
6224 }
6225
6226 if (loc != NULL && !header_of_multiple)
6227 {
6228 std::vector<int> inf_nums;
6229 int mi_only = 1;
6230
6231 for (inferior *inf : all_inferiors ())
6232 {
6233 if (inf->pspace == loc->pspace)
6234 inf_nums.push_back (inf->num);
6235 }
6236
6237 /* For backward compatibility, don't display inferiors in CLI unless
6238 there are several. Always display for MI. */
6239 if (allflag
6240 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6241 && (program_spaces.size () > 1
6242 || number_of_inferiors () > 1)
6243 /* LOC is for existing B, it cannot be in
6244 moribund_locations and thus having NULL OWNER. */
6245 && loc->owner->type != bp_catchpoint))
6246 mi_only = 0;
6247 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6248 }
6249
6250 if (!part_of_multiple)
6251 {
6252 if (b->thread != -1)
6253 {
6254 /* FIXME: This seems to be redundant and lost here; see the
6255 "stop only in" line a little further down. */
6256 uiout->text (" thread ");
6257 uiout->field_signed ("thread", b->thread);
6258 }
6259 else if (b->task != 0)
6260 {
6261 uiout->text (" task ");
6262 uiout->field_signed ("task", b->task);
6263 }
6264 }
6265
6266 uiout->text ("\n");
6267
6268 if (!part_of_multiple)
6269 b->ops->print_one_detail (b, uiout);
6270
6271 if (part_of_multiple && frame_id_p (b->frame_id))
6272 {
6273 annotate_field (6);
6274 uiout->text ("\tstop only in stack frame at ");
6275 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6276 the frame ID. */
6277 uiout->field_core_addr ("frame",
6278 b->gdbarch, b->frame_id.stack_addr);
6279 uiout->text ("\n");
6280 }
6281
6282 if (!part_of_multiple && b->cond_string)
6283 {
6284 annotate_field (7);
6285 if (is_tracepoint (b))
6286 uiout->text ("\ttrace only if ");
6287 else
6288 uiout->text ("\tstop only if ");
6289 uiout->field_string ("cond", b->cond_string.get ());
6290
6291 /* Print whether the target is doing the breakpoint's condition
6292 evaluation. If GDB is doing the evaluation, don't print anything. */
6293 if (is_breakpoint (b)
6294 && breakpoint_condition_evaluation_mode ()
6295 == condition_evaluation_target)
6296 {
6297 uiout->message (" (%pF evals)",
6298 string_field ("evaluated-by",
6299 bp_condition_evaluator (b)));
6300 }
6301 uiout->text ("\n");
6302 }
6303
6304 if (!part_of_multiple && b->thread != -1)
6305 {
6306 /* FIXME should make an annotation for this. */
6307 uiout->text ("\tstop only in thread ");
6308 if (uiout->is_mi_like_p ())
6309 uiout->field_signed ("thread", b->thread);
6310 else
6311 {
6312 struct thread_info *thr = find_thread_global_id (b->thread);
6313
6314 uiout->field_string ("thread", print_thread_id (thr));
6315 }
6316 uiout->text ("\n");
6317 }
6318
6319 if (!part_of_multiple)
6320 {
6321 if (b->hit_count)
6322 {
6323 /* FIXME should make an annotation for this. */
6324 if (is_catchpoint (b))
6325 uiout->text ("\tcatchpoint");
6326 else if (is_tracepoint (b))
6327 uiout->text ("\ttracepoint");
6328 else
6329 uiout->text ("\tbreakpoint");
6330 uiout->text (" already hit ");
6331 uiout->field_signed ("times", b->hit_count);
6332 if (b->hit_count == 1)
6333 uiout->text (" time\n");
6334 else
6335 uiout->text (" times\n");
6336 }
6337 else
6338 {
6339 /* Output the count also if it is zero, but only if this is mi. */
6340 if (uiout->is_mi_like_p ())
6341 uiout->field_signed ("times", b->hit_count);
6342 }
6343 }
6344
6345 if (!part_of_multiple && b->ignore_count)
6346 {
6347 annotate_field (8);
6348 uiout->message ("\tignore next %pF hits\n",
6349 signed_field ("ignore", b->ignore_count));
6350 }
6351
6352 /* Note that an enable count of 1 corresponds to "enable once"
6353 behavior, which is reported by the combination of enablement and
6354 disposition, so we don't need to mention it here. */
6355 if (!part_of_multiple && b->enable_count > 1)
6356 {
6357 annotate_field (8);
6358 uiout->text ("\tdisable after ");
6359 /* Tweak the wording to clarify that ignore and enable counts
6360 are distinct, and have additive effect. */
6361 if (b->ignore_count)
6362 uiout->text ("additional ");
6363 else
6364 uiout->text ("next ");
6365 uiout->field_signed ("enable", b->enable_count);
6366 uiout->text (" hits\n");
6367 }
6368
6369 if (!part_of_multiple && is_tracepoint (b))
6370 {
6371 struct tracepoint *tp = (struct tracepoint *) b;
6372
6373 if (tp->traceframe_usage)
6374 {
6375 uiout->text ("\ttrace buffer usage ");
6376 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
6377 uiout->text (" bytes\n");
6378 }
6379 }
6380
6381 l = b->commands ? b->commands.get () : NULL;
6382 if (!part_of_multiple && l)
6383 {
6384 annotate_field (9);
6385 ui_out_emit_tuple tuple_emitter (uiout, "script");
6386 print_command_lines (uiout, l, 4);
6387 }
6388
6389 if (is_tracepoint (b))
6390 {
6391 struct tracepoint *t = (struct tracepoint *) b;
6392
6393 if (!part_of_multiple && t->pass_count)
6394 {
6395 annotate_field (10);
6396 uiout->text ("\tpass count ");
6397 uiout->field_signed ("pass", t->pass_count);
6398 uiout->text (" \n");
6399 }
6400
6401 /* Don't display it when tracepoint or tracepoint location is
6402 pending. */
6403 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6404 {
6405 annotate_field (11);
6406
6407 if (uiout->is_mi_like_p ())
6408 uiout->field_string ("installed",
6409 loc->inserted ? "y" : "n");
6410 else
6411 {
6412 if (loc->inserted)
6413 uiout->text ("\t");
6414 else
6415 uiout->text ("\tnot ");
6416 uiout->text ("installed on target\n");
6417 }
6418 }
6419 }
6420
6421 if (uiout->is_mi_like_p () && !part_of_multiple)
6422 {
6423 if (is_watchpoint (b))
6424 {
6425 struct watchpoint *w = (struct watchpoint *) b;
6426
6427 uiout->field_string ("original-location", w->exp_string.get ());
6428 }
6429 else if (b->location != NULL
6430 && event_location_to_string (b->location.get ()) != NULL)
6431 uiout->field_string ("original-location",
6432 event_location_to_string (b->location.get ()));
6433 }
6434
6435 return result;
6436 }
6437
6438 /* See breakpoint.h. */
6439
6440 bool fix_multi_location_breakpoint_output_globally = false;
6441
6442 static void
6443 print_one_breakpoint (struct breakpoint *b,
6444 struct bp_location **last_loc,
6445 int allflag)
6446 {
6447 struct ui_out *uiout = current_uiout;
6448 bool use_fixed_output
6449 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6450 || fix_multi_location_breakpoint_output_globally);
6451
6452 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
6453 bool printed = print_one_breakpoint_location (b, NULL, 0, last_loc,
6454 allflag, false);
6455
6456 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6457 are outside. */
6458 if (!use_fixed_output)
6459 bkpt_tuple_emitter.reset ();
6460
6461 /* If this breakpoint has custom print function,
6462 it's already printed. Otherwise, print individual
6463 locations, if any. */
6464 if (!printed || allflag)
6465 {
6466 /* If breakpoint has a single location that is disabled, we
6467 print it as if it had several locations, since otherwise it's
6468 hard to represent "breakpoint enabled, location disabled"
6469 situation.
6470
6471 Note that while hardware watchpoints have several locations
6472 internally, that's not a property exposed to users.
6473
6474 Likewise, while catchpoints may be implemented with
6475 breakpoints (e.g., catch throw), that's not a property
6476 exposed to users. We do however display the internal
6477 breakpoint locations with "maint info breakpoints". */
6478 if (!is_hardware_watchpoint (b)
6479 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6480 || is_ada_exception_catchpoint (b))
6481 && (allflag
6482 || (b->loc && (b->loc->next
6483 || !b->loc->enabled
6484 || b->loc->disabled_by_cond))))
6485 {
6486 gdb::optional<ui_out_emit_list> locations_list;
6487
6488 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6489 MI record. For later versions, place breakpoint locations in a
6490 list. */
6491 if (uiout->is_mi_like_p () && use_fixed_output)
6492 locations_list.emplace (uiout, "locations");
6493
6494 int n = 1;
6495 for (bp_location *loc : b->locations ())
6496 {
6497 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
6498 print_one_breakpoint_location (b, loc, n, last_loc,
6499 allflag, allflag);
6500 n++;
6501 }
6502 }
6503 }
6504 }
6505
6506 static int
6507 breakpoint_address_bits (struct breakpoint *b)
6508 {
6509 int print_address_bits = 0;
6510
6511 /* Software watchpoints that aren't watching memory don't have an
6512 address to print. */
6513 if (is_no_memory_software_watchpoint (b))
6514 return 0;
6515
6516 for (bp_location *loc : b->locations ())
6517 {
6518 int addr_bit;
6519
6520 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6521 if (addr_bit > print_address_bits)
6522 print_address_bits = addr_bit;
6523 }
6524
6525 return print_address_bits;
6526 }
6527
6528 /* See breakpoint.h. */
6529
6530 void
6531 print_breakpoint (breakpoint *b)
6532 {
6533 struct bp_location *dummy_loc = NULL;
6534 print_one_breakpoint (b, &dummy_loc, 0);
6535 }
6536
6537 /* Return true if this breakpoint was set by the user, false if it is
6538 internal or momentary. */
6539
6540 int
6541 user_breakpoint_p (struct breakpoint *b)
6542 {
6543 return b->number > 0;
6544 }
6545
6546 /* See breakpoint.h. */
6547
6548 int
6549 pending_breakpoint_p (struct breakpoint *b)
6550 {
6551 return b->loc == NULL;
6552 }
6553
6554 /* Print information on breakpoints (including watchpoints and tracepoints).
6555
6556 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6557 understood by number_or_range_parser. Only breakpoints included in this
6558 list are then printed.
6559
6560 If SHOW_INTERNAL is true, print internal breakpoints.
6561
6562 If FILTER is non-NULL, call it on each breakpoint and only include the
6563 ones for which it returns true.
6564
6565 Return the total number of breakpoints listed. */
6566
6567 static int
6568 breakpoint_1 (const char *bp_num_list, bool show_internal,
6569 bool (*filter) (const struct breakpoint *))
6570 {
6571 struct bp_location *last_loc = NULL;
6572 int nr_printable_breakpoints;
6573 struct value_print_options opts;
6574 int print_address_bits = 0;
6575 int print_type_col_width = 14;
6576 struct ui_out *uiout = current_uiout;
6577 bool has_disabled_by_cond_location = false;
6578
6579 get_user_print_options (&opts);
6580
6581 /* Compute the number of rows in the table, as well as the size
6582 required for address fields. */
6583 nr_printable_breakpoints = 0;
6584 for (breakpoint *b : all_breakpoints ())
6585 {
6586 /* If we have a filter, only list the breakpoints it accepts. */
6587 if (filter && !filter (b))
6588 continue;
6589
6590 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6591 accept. Skip the others. */
6592 if (bp_num_list != NULL && *bp_num_list != '\0')
6593 {
6594 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
6595 continue;
6596 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
6597 continue;
6598 }
6599
6600 if (show_internal || user_breakpoint_p (b))
6601 {
6602 int addr_bit, type_len;
6603
6604 addr_bit = breakpoint_address_bits (b);
6605 if (addr_bit > print_address_bits)
6606 print_address_bits = addr_bit;
6607
6608 type_len = strlen (bptype_string (b->type));
6609 if (type_len > print_type_col_width)
6610 print_type_col_width = type_len;
6611
6612 nr_printable_breakpoints++;
6613 }
6614 }
6615
6616 {
6617 ui_out_emit_table table_emitter (uiout,
6618 opts.addressprint ? 6 : 5,
6619 nr_printable_breakpoints,
6620 "BreakpointTable");
6621
6622 if (nr_printable_breakpoints > 0)
6623 annotate_breakpoints_headers ();
6624 if (nr_printable_breakpoints > 0)
6625 annotate_field (0);
6626 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6627 if (nr_printable_breakpoints > 0)
6628 annotate_field (1);
6629 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6630 if (nr_printable_breakpoints > 0)
6631 annotate_field (2);
6632 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6633 if (nr_printable_breakpoints > 0)
6634 annotate_field (3);
6635 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6636 if (opts.addressprint)
6637 {
6638 if (nr_printable_breakpoints > 0)
6639 annotate_field (4);
6640 if (print_address_bits <= 32)
6641 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6642 else
6643 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6644 }
6645 if (nr_printable_breakpoints > 0)
6646 annotate_field (5);
6647 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6648 uiout->table_body ();
6649 if (nr_printable_breakpoints > 0)
6650 annotate_breakpoints_table ();
6651
6652 for (breakpoint *b : all_breakpoints ())
6653 {
6654 QUIT;
6655 /* If we have a filter, only list the breakpoints it accepts. */
6656 if (filter && !filter (b))
6657 continue;
6658
6659 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
6660 accept. Skip the others. */
6661
6662 if (bp_num_list != NULL && *bp_num_list != '\0')
6663 {
6664 if (show_internal) /* maintenance info breakpoint */
6665 {
6666 if (parse_and_eval_long (bp_num_list) != b->number)
6667 continue;
6668 }
6669 else /* all others */
6670 {
6671 if (!number_is_in_list (bp_num_list, b->number))
6672 continue;
6673 }
6674 }
6675 /* We only print out user settable breakpoints unless the
6676 show_internal is set. */
6677 if (show_internal || user_breakpoint_p (b))
6678 {
6679 print_one_breakpoint (b, &last_loc, show_internal);
6680 for (bp_location *loc : b->locations ())
6681 if (loc->disabled_by_cond)
6682 has_disabled_by_cond_location = true;
6683 }
6684 }
6685 }
6686
6687 if (nr_printable_breakpoints == 0)
6688 {
6689 /* If there's a filter, let the caller decide how to report
6690 empty list. */
6691 if (!filter)
6692 {
6693 if (bp_num_list == NULL || *bp_num_list == '\0')
6694 uiout->message ("No breakpoints or watchpoints.\n");
6695 else
6696 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6697 bp_num_list);
6698 }
6699 }
6700 else
6701 {
6702 if (last_loc && !server_command)
6703 set_next_address (last_loc->gdbarch, last_loc->address);
6704
6705 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
6706 uiout->message (_("(*): Breakpoint condition is invalid at this "
6707 "location.\n"));
6708 }
6709
6710 /* FIXME? Should this be moved up so that it is only called when
6711 there have been breakpoints? */
6712 annotate_breakpoints_table_end ();
6713
6714 return nr_printable_breakpoints;
6715 }
6716
6717 /* Display the value of default-collect in a way that is generally
6718 compatible with the breakpoint list. */
6719
6720 static void
6721 default_collect_info (void)
6722 {
6723 struct ui_out *uiout = current_uiout;
6724
6725 /* If it has no value (which is frequently the case), say nothing; a
6726 message like "No default-collect." gets in user's face when it's
6727 not wanted. */
6728 if (default_collect.empty ())
6729 return;
6730
6731 /* The following phrase lines up nicely with per-tracepoint collect
6732 actions. */
6733 uiout->text ("default collect ");
6734 uiout->field_string ("default-collect", default_collect);
6735 uiout->text (" \n");
6736 }
6737
6738 static void
6739 info_breakpoints_command (const char *args, int from_tty)
6740 {
6741 breakpoint_1 (args, false, NULL);
6742
6743 default_collect_info ();
6744 }
6745
6746 static void
6747 info_watchpoints_command (const char *args, int from_tty)
6748 {
6749 int num_printed = breakpoint_1 (args, false, is_watchpoint);
6750 struct ui_out *uiout = current_uiout;
6751
6752 if (num_printed == 0)
6753 {
6754 if (args == NULL || *args == '\0')
6755 uiout->message ("No watchpoints.\n");
6756 else
6757 uiout->message ("No watchpoint matching '%s'.\n", args);
6758 }
6759 }
6760
6761 static void
6762 maintenance_info_breakpoints (const char *args, int from_tty)
6763 {
6764 breakpoint_1 (args, true, NULL);
6765
6766 default_collect_info ();
6767 }
6768
6769 static int
6770 breakpoint_has_pc (struct breakpoint *b,
6771 struct program_space *pspace,
6772 CORE_ADDR pc, struct obj_section *section)
6773 {
6774 for (bp_location *bl : b->locations ())
6775 {
6776 if (bl->pspace == pspace
6777 && bl->address == pc
6778 && (!overlay_debugging || bl->section == section))
6779 return 1;
6780 }
6781 return 0;
6782 }
6783
6784 /* Print a message describing any user-breakpoints set at PC. This
6785 concerns with logical breakpoints, so we match program spaces, not
6786 address spaces. */
6787
6788 static void
6789 describe_other_breakpoints (struct gdbarch *gdbarch,
6790 struct program_space *pspace, CORE_ADDR pc,
6791 struct obj_section *section, int thread)
6792 {
6793 int others = 0;
6794
6795 for (breakpoint *b : all_breakpoints ())
6796 others += (user_breakpoint_p (b)
6797 && breakpoint_has_pc (b, pspace, pc, section));
6798
6799 if (others > 0)
6800 {
6801 if (others == 1)
6802 gdb_printf (_("Note: breakpoint "));
6803 else /* if (others == ???) */
6804 gdb_printf (_("Note: breakpoints "));
6805 for (breakpoint *b : all_breakpoints ())
6806 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6807 {
6808 others--;
6809 gdb_printf ("%d", b->number);
6810 if (b->thread == -1 && thread != -1)
6811 gdb_printf (" (all threads)");
6812 else if (b->thread != -1)
6813 gdb_printf (" (thread %d)", b->thread);
6814 gdb_printf ("%s%s ",
6815 ((b->enable_state == bp_disabled
6816 || b->enable_state == bp_call_disabled)
6817 ? " (disabled)"
6818 : ""),
6819 (others > 1) ? ","
6820 : ((others == 1) ? " and" : ""));
6821 }
6822 current_uiout->message (_("also set at pc %ps.\n"),
6823 styled_string (address_style.style (),
6824 paddress (gdbarch, pc)));
6825 }
6826 }
6827 \f
6828
6829 /* Return true iff it is meaningful to use the address member of LOC.
6830 For some breakpoint types, the locations' address members are
6831 irrelevant and it makes no sense to attempt to compare them to
6832 other addresses (or use them for any other purpose either).
6833
6834 More specifically, software watchpoints and catchpoints that are
6835 not backed by breakpoints always have a zero valued location
6836 address and we don't want to mark breakpoints of any of these types
6837 to be a duplicate of an actual breakpoint location at address
6838 zero. */
6839
6840 static bool
6841 bl_address_is_meaningful (bp_location *loc)
6842 {
6843 return loc->loc_type != bp_loc_other;
6844 }
6845
6846 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6847 true if LOC1 and LOC2 represent the same watchpoint location. */
6848
6849 static int
6850 watchpoint_locations_match (struct bp_location *loc1,
6851 struct bp_location *loc2)
6852 {
6853 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6854 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6855
6856 /* Both of them must exist. */
6857 gdb_assert (w1 != NULL);
6858 gdb_assert (w2 != NULL);
6859
6860 /* If the target can evaluate the condition expression in hardware,
6861 then we we need to insert both watchpoints even if they are at
6862 the same place. Otherwise the watchpoint will only trigger when
6863 the condition of whichever watchpoint was inserted evaluates to
6864 true, not giving a chance for GDB to check the condition of the
6865 other watchpoint. */
6866 if ((w1->cond_exp
6867 && target_can_accel_watchpoint_condition (loc1->address,
6868 loc1->length,
6869 loc1->watchpoint_type,
6870 w1->cond_exp.get ()))
6871 || (w2->cond_exp
6872 && target_can_accel_watchpoint_condition (loc2->address,
6873 loc2->length,
6874 loc2->watchpoint_type,
6875 w2->cond_exp.get ())))
6876 return 0;
6877
6878 /* Note that this checks the owner's type, not the location's. In
6879 case the target does not support read watchpoints, but does
6880 support access watchpoints, we'll have bp_read_watchpoint
6881 watchpoints with hw_access locations. Those should be considered
6882 duplicates of hw_read locations. The hw_read locations will
6883 become hw_access locations later. */
6884 return (loc1->owner->type == loc2->owner->type
6885 && loc1->pspace->aspace == loc2->pspace->aspace
6886 && loc1->address == loc2->address
6887 && loc1->length == loc2->length);
6888 }
6889
6890 /* See breakpoint.h. */
6891
6892 int
6893 breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6894 const address_space *aspace2, CORE_ADDR addr2)
6895 {
6896 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6897 || aspace1 == aspace2)
6898 && addr1 == addr2);
6899 }
6900
6901 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6902 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6903 matches ASPACE2. On targets that have global breakpoints, the address
6904 space doesn't really matter. */
6905
6906 static int
6907 breakpoint_address_match_range (const address_space *aspace1,
6908 CORE_ADDR addr1,
6909 int len1, const address_space *aspace2,
6910 CORE_ADDR addr2)
6911 {
6912 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6913 || aspace1 == aspace2)
6914 && addr2 >= addr1 && addr2 < addr1 + len1);
6915 }
6916
6917 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6918 a ranged breakpoint. In most targets, a match happens only if ASPACE
6919 matches the breakpoint's address space. On targets that have global
6920 breakpoints, the address space doesn't really matter. */
6921
6922 static int
6923 breakpoint_location_address_match (struct bp_location *bl,
6924 const address_space *aspace,
6925 CORE_ADDR addr)
6926 {
6927 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6928 aspace, addr)
6929 || (bl->length
6930 && breakpoint_address_match_range (bl->pspace->aspace,
6931 bl->address, bl->length,
6932 aspace, addr)));
6933 }
6934
6935 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6936 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6937 match happens only if ASPACE matches the breakpoint's address
6938 space. On targets that have global breakpoints, the address space
6939 doesn't really matter. */
6940
6941 static int
6942 breakpoint_location_address_range_overlap (struct bp_location *bl,
6943 const address_space *aspace,
6944 CORE_ADDR addr, int len)
6945 {
6946 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6947 || bl->pspace->aspace == aspace)
6948 {
6949 int bl_len = bl->length != 0 ? bl->length : 1;
6950
6951 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6952 return 1;
6953 }
6954 return 0;
6955 }
6956
6957 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6958 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6959 true, otherwise returns false. */
6960
6961 static int
6962 tracepoint_locations_match (struct bp_location *loc1,
6963 struct bp_location *loc2)
6964 {
6965 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6966 /* Since tracepoint locations are never duplicated with others', tracepoint
6967 locations at the same address of different tracepoints are regarded as
6968 different locations. */
6969 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6970 else
6971 return 0;
6972 }
6973
6974 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6975 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
6976 the same location. If SW_HW_BPS_MATCH is true, then software
6977 breakpoint locations and hardware breakpoint locations match,
6978 otherwise they don't. */
6979
6980 static int
6981 breakpoint_locations_match (struct bp_location *loc1,
6982 struct bp_location *loc2,
6983 bool sw_hw_bps_match)
6984 {
6985 int hw_point1, hw_point2;
6986
6987 /* Both of them must not be in moribund_locations. */
6988 gdb_assert (loc1->owner != NULL);
6989 gdb_assert (loc2->owner != NULL);
6990
6991 hw_point1 = is_hardware_watchpoint (loc1->owner);
6992 hw_point2 = is_hardware_watchpoint (loc2->owner);
6993
6994 if (hw_point1 != hw_point2)
6995 return 0;
6996 else if (hw_point1)
6997 return watchpoint_locations_match (loc1, loc2);
6998 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6999 return tracepoint_locations_match (loc1, loc2);
7000 else
7001 /* We compare bp_location.length in order to cover ranged
7002 breakpoints. Keep this in sync with
7003 bp_location_is_less_than. */
7004 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7005 loc2->pspace->aspace, loc2->address)
7006 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
7007 && loc1->length == loc2->length);
7008 }
7009
7010 static void
7011 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7012 int bnum, int have_bnum)
7013 {
7014 /* The longest string possibly returned by hex_string_custom
7015 is 50 chars. These must be at least that big for safety. */
7016 char astr1[64];
7017 char astr2[64];
7018
7019 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7020 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7021 if (have_bnum)
7022 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7023 bnum, astr1, astr2);
7024 else
7025 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7026 }
7027
7028 /* Adjust a breakpoint's address to account for architectural
7029 constraints on breakpoint placement. Return the adjusted address.
7030 Note: Very few targets require this kind of adjustment. For most
7031 targets, this function is simply the identity function. */
7032
7033 static CORE_ADDR
7034 adjust_breakpoint_address (struct gdbarch *gdbarch,
7035 CORE_ADDR bpaddr, enum bptype bptype)
7036 {
7037 if (bptype == bp_watchpoint
7038 || bptype == bp_hardware_watchpoint
7039 || bptype == bp_read_watchpoint
7040 || bptype == bp_access_watchpoint
7041 || bptype == bp_catchpoint)
7042 {
7043 /* Watchpoints and the various bp_catch_* eventpoints should not
7044 have their addresses modified. */
7045 return bpaddr;
7046 }
7047 else if (bptype == bp_single_step)
7048 {
7049 /* Single-step breakpoints should not have their addresses
7050 modified. If there's any architectural constrain that
7051 applies to this address, then it should have already been
7052 taken into account when the breakpoint was created in the
7053 first place. If we didn't do this, stepping through e.g.,
7054 Thumb-2 IT blocks would break. */
7055 return bpaddr;
7056 }
7057 else
7058 {
7059 CORE_ADDR adjusted_bpaddr = bpaddr;
7060
7061 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7062 {
7063 /* Some targets have architectural constraints on the placement
7064 of breakpoint instructions. Obtain the adjusted address. */
7065 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7066 }
7067
7068 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
7069
7070 /* An adjusted breakpoint address can significantly alter
7071 a user's expectations. Print a warning if an adjustment
7072 is required. */
7073 if (adjusted_bpaddr != bpaddr)
7074 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7075
7076 return adjusted_bpaddr;
7077 }
7078 }
7079
7080 static bp_loc_type
7081 bp_location_from_bp_type (bptype type)
7082 {
7083 switch (type)
7084 {
7085 case bp_breakpoint:
7086 case bp_single_step:
7087 case bp_until:
7088 case bp_finish:
7089 case bp_longjmp:
7090 case bp_longjmp_resume:
7091 case bp_longjmp_call_dummy:
7092 case bp_exception:
7093 case bp_exception_resume:
7094 case bp_step_resume:
7095 case bp_hp_step_resume:
7096 case bp_watchpoint_scope:
7097 case bp_call_dummy:
7098 case bp_std_terminate:
7099 case bp_shlib_event:
7100 case bp_thread_event:
7101 case bp_overlay_event:
7102 case bp_jit_event:
7103 case bp_longjmp_master:
7104 case bp_std_terminate_master:
7105 case bp_exception_master:
7106 case bp_gnu_ifunc_resolver:
7107 case bp_gnu_ifunc_resolver_return:
7108 case bp_dprintf:
7109 return bp_loc_software_breakpoint;
7110 case bp_hardware_breakpoint:
7111 return bp_loc_hardware_breakpoint;
7112 case bp_hardware_watchpoint:
7113 case bp_read_watchpoint:
7114 case bp_access_watchpoint:
7115 return bp_loc_hardware_watchpoint;
7116 case bp_watchpoint:
7117 case bp_catchpoint:
7118 case bp_tracepoint:
7119 case bp_fast_tracepoint:
7120 case bp_static_tracepoint:
7121 return bp_loc_other;
7122 default:
7123 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7124 }
7125 }
7126
7127 bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7128 {
7129 this->owner = owner;
7130 this->cond_bytecode = NULL;
7131 this->shlib_disabled = 0;
7132 this->enabled = 1;
7133 this->disabled_by_cond = false;
7134
7135 this->loc_type = type;
7136
7137 if (this->loc_type == bp_loc_software_breakpoint
7138 || this->loc_type == bp_loc_hardware_breakpoint)
7139 mark_breakpoint_location_modified (this);
7140
7141 incref ();
7142 }
7143
7144 bp_location::bp_location (breakpoint *owner)
7145 : bp_location::bp_location (owner,
7146 bp_location_from_bp_type (owner->type))
7147 {
7148 }
7149
7150 /* Allocate a struct bp_location. */
7151
7152 static struct bp_location *
7153 allocate_bp_location (struct breakpoint *bpt)
7154 {
7155 return bpt->ops->allocate_location (bpt);
7156 }
7157
7158 /* Decrement reference count. If the reference count reaches 0,
7159 destroy the bp_location. Sets *BLP to NULL. */
7160
7161 static void
7162 decref_bp_location (struct bp_location **blp)
7163 {
7164 bp_location_ref_policy::decref (*blp);
7165 *blp = NULL;
7166 }
7167
7168 /* Add breakpoint B at the end of the global breakpoint chain. */
7169
7170 static breakpoint *
7171 add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
7172 {
7173 struct breakpoint *b1;
7174 struct breakpoint *result = b.get ();
7175
7176 /* Add this breakpoint to the end of the chain so that a list of
7177 breakpoints will come out in order of increasing numbers. */
7178
7179 b1 = breakpoint_chain;
7180 if (b1 == 0)
7181 breakpoint_chain = b.release ();
7182 else
7183 {
7184 while (b1->next)
7185 b1 = b1->next;
7186 b1->next = b.release ();
7187 }
7188
7189 return result;
7190 }
7191
7192 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7193
7194 static void
7195 init_raw_breakpoint_without_location (struct breakpoint *b,
7196 struct gdbarch *gdbarch,
7197 enum bptype bptype,
7198 const struct breakpoint_ops *ops)
7199 {
7200 gdb_assert (ops != NULL);
7201
7202 b->ops = ops;
7203 b->type = bptype;
7204 b->gdbarch = gdbarch;
7205 b->language = current_language->la_language;
7206 b->input_radix = input_radix;
7207 b->related_breakpoint = b;
7208 }
7209
7210 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7211 that has type BPTYPE and has no locations as yet. */
7212
7213 static struct breakpoint *
7214 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7215 enum bptype bptype,
7216 const struct breakpoint_ops *ops)
7217 {
7218 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7219
7220 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7221 return add_to_breakpoint_chain (std::move (b));
7222 }
7223
7224 /* Initialize loc->function_name. */
7225
7226 static void
7227 set_breakpoint_location_function (struct bp_location *loc)
7228 {
7229 gdb_assert (loc->owner != NULL);
7230
7231 if (loc->owner->type == bp_breakpoint
7232 || loc->owner->type == bp_hardware_breakpoint
7233 || is_tracepoint (loc->owner))
7234 {
7235 const char *function_name;
7236
7237 if (loc->msymbol != NULL
7238 && (loc->msymbol->type () == mst_text_gnu_ifunc
7239 || loc->msymbol->type () == mst_data_gnu_ifunc))
7240 {
7241 struct breakpoint *b = loc->owner;
7242
7243 function_name = loc->msymbol->linkage_name ();
7244
7245 if (b->type == bp_breakpoint && b->loc == loc
7246 && loc->next == NULL && b->related_breakpoint == b)
7247 {
7248 /* Create only the whole new breakpoint of this type but do not
7249 mess more complicated breakpoints with multiple locations. */
7250 b->type = bp_gnu_ifunc_resolver;
7251 /* Remember the resolver's address for use by the return
7252 breakpoint. */
7253 loc->related_address = loc->address;
7254 }
7255 }
7256 else
7257 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
7258
7259 if (function_name)
7260 loc->function_name = make_unique_xstrdup (function_name);
7261 }
7262 }
7263
7264 /* Attempt to determine architecture of location identified by SAL. */
7265 struct gdbarch *
7266 get_sal_arch (struct symtab_and_line sal)
7267 {
7268 if (sal.section)
7269 return sal.section->objfile->arch ();
7270 if (sal.symtab)
7271 return sal.symtab->compunit ()->objfile ()->arch ();
7272
7273 return NULL;
7274 }
7275
7276 /* Low level routine for partially initializing a breakpoint of type
7277 BPTYPE. The newly created breakpoint's address, section, source
7278 file name, and line number are provided by SAL.
7279
7280 It is expected that the caller will complete the initialization of
7281 the newly created breakpoint struct as well as output any status
7282 information regarding the creation of a new breakpoint. */
7283
7284 static void
7285 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7286 struct symtab_and_line sal, enum bptype bptype,
7287 const struct breakpoint_ops *ops)
7288 {
7289 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7290
7291 add_location_to_breakpoint (b, &sal);
7292
7293 if (bptype != bp_catchpoint)
7294 gdb_assert (sal.pspace != NULL);
7295
7296 /* Store the program space that was used to set the breakpoint,
7297 except for ordinary breakpoints, which are independent of the
7298 program space. */
7299 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7300 b->pspace = sal.pspace;
7301 }
7302
7303 /* set_raw_breakpoint is a low level routine for allocating and
7304 partially initializing a breakpoint of type BPTYPE. The newly
7305 created breakpoint's address, section, source file name, and line
7306 number are provided by SAL. The newly created and partially
7307 initialized breakpoint is added to the breakpoint chain and
7308 is also returned as the value of this function.
7309
7310 It is expected that the caller will complete the initialization of
7311 the newly created breakpoint struct as well as output any status
7312 information regarding the creation of a new breakpoint. In
7313 particular, set_raw_breakpoint does NOT set the breakpoint
7314 number! Care should be taken to not allow an error to occur
7315 prior to completing the initialization of the breakpoint. If this
7316 should happen, a bogus breakpoint will be left on the chain. */
7317
7318 static struct breakpoint *
7319 set_raw_breakpoint (struct gdbarch *gdbarch,
7320 struct symtab_and_line sal, enum bptype bptype,
7321 const struct breakpoint_ops *ops)
7322 {
7323 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
7324
7325 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7326 return add_to_breakpoint_chain (std::move (b));
7327 }
7328
7329 /* Call this routine when stepping and nexting to enable a breakpoint
7330 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7331 initiated the operation. */
7332
7333 void
7334 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7335 {
7336 int thread = tp->global_num;
7337
7338 /* To avoid having to rescan all objfile symbols at every step,
7339 we maintain a list of continually-inserted but always disabled
7340 longjmp "master" breakpoints. Here, we simply create momentary
7341 clones of those and enable them for the requested thread. */
7342 for (breakpoint *b : all_breakpoints_safe ())
7343 if (b->pspace == current_program_space
7344 && (b->type == bp_longjmp_master
7345 || b->type == bp_exception_master))
7346 {
7347 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7348 struct breakpoint *clone;
7349
7350 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7351 after their removal. */
7352 clone = momentary_breakpoint_from_master (b, type,
7353 &momentary_breakpoint_ops, 1);
7354 clone->thread = thread;
7355 }
7356
7357 tp->initiating_frame = frame;
7358 }
7359
7360 /* Delete all longjmp breakpoints from THREAD. */
7361 void
7362 delete_longjmp_breakpoint (int thread)
7363 {
7364 for (breakpoint *b : all_breakpoints_safe ())
7365 if (b->type == bp_longjmp || b->type == bp_exception)
7366 {
7367 if (b->thread == thread)
7368 delete_breakpoint (b);
7369 }
7370 }
7371
7372 void
7373 delete_longjmp_breakpoint_at_next_stop (int thread)
7374 {
7375 for (breakpoint *b : all_breakpoints_safe ())
7376 if (b->type == bp_longjmp || b->type == bp_exception)
7377 {
7378 if (b->thread == thread)
7379 b->disposition = disp_del_at_next_stop;
7380 }
7381 }
7382
7383 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7384 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7385 pointer to any of them. Return NULL if this system cannot place longjmp
7386 breakpoints. */
7387
7388 struct breakpoint *
7389 set_longjmp_breakpoint_for_call_dummy (void)
7390 {
7391 breakpoint *retval = nullptr;
7392
7393 for (breakpoint *b : all_breakpoints ())
7394 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7395 {
7396 struct breakpoint *new_b;
7397
7398 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7399 &momentary_breakpoint_ops,
7400 1);
7401 new_b->thread = inferior_thread ()->global_num;
7402
7403 /* Link NEW_B into the chain of RETVAL breakpoints. */
7404
7405 gdb_assert (new_b->related_breakpoint == new_b);
7406 if (retval == NULL)
7407 retval = new_b;
7408 new_b->related_breakpoint = retval;
7409 while (retval->related_breakpoint != new_b->related_breakpoint)
7410 retval = retval->related_breakpoint;
7411 retval->related_breakpoint = new_b;
7412 }
7413
7414 return retval;
7415 }
7416
7417 /* Verify all existing dummy frames and their associated breakpoints for
7418 TP. Remove those which can no longer be found in the current frame
7419 stack.
7420
7421 If the unwind fails then there is not sufficient information to discard
7422 dummy frames. In this case, elide the clean up and the dummy frames will
7423 be cleaned up next time this function is called from a location where
7424 unwinding is possible. */
7425
7426 void
7427 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7428 {
7429 struct breakpoint *b, *b_tmp;
7430
7431 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7432 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7433 {
7434 struct breakpoint *dummy_b = b->related_breakpoint;
7435
7436 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7437 chained off b->related_breakpoint. */
7438 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7439 dummy_b = dummy_b->related_breakpoint;
7440
7441 /* If there was no bp_call_dummy breakpoint then there's nothing
7442 more to do. Or, if the dummy frame associated with the
7443 bp_call_dummy is still on the stack then we need to leave this
7444 bp_call_dummy in place. */
7445 if (dummy_b->type != bp_call_dummy
7446 || frame_find_by_id (dummy_b->frame_id) != NULL)
7447 continue;
7448
7449 /* We didn't find the dummy frame on the stack, this could be
7450 because we have longjmp'd to a stack frame that is previous to
7451 the dummy frame, or it could be because the stack unwind is
7452 broken at some point between the longjmp frame and the dummy
7453 frame.
7454
7455 Next we figure out why the stack unwind stopped. If it looks
7456 like the unwind is complete then we assume the dummy frame has
7457 been jumped over, however, if the unwind stopped for an
7458 unexpected reason then we assume the stack unwind is currently
7459 broken, and that we will (eventually) return to the dummy
7460 frame.
7461
7462 It might be tempting to consider using frame_id_inner here, but
7463 that is not safe. There is no guarantee that the stack frames
7464 we are looking at here are even on the same stack as the
7465 original dummy frame, hence frame_id_inner can't be used. See
7466 the comments on frame_id_inner for more details. */
7467 bool unwind_finished_unexpectedly = false;
7468 for (struct frame_info *fi = get_current_frame (); fi != nullptr; )
7469 {
7470 struct frame_info *prev = get_prev_frame (fi);
7471 if (prev == nullptr)
7472 {
7473 /* FI is the last stack frame. Why did this frame not
7474 unwind further? */
7475 auto stop_reason = get_frame_unwind_stop_reason (fi);
7476 if (stop_reason != UNWIND_NO_REASON
7477 && stop_reason != UNWIND_OUTERMOST)
7478 unwind_finished_unexpectedly = true;
7479 }
7480 fi = prev;
7481 }
7482 if (unwind_finished_unexpectedly)
7483 continue;
7484
7485 dummy_frame_discard (dummy_b->frame_id, tp);
7486
7487 while (b->related_breakpoint != b)
7488 {
7489 if (b_tmp == b->related_breakpoint)
7490 b_tmp = b->related_breakpoint->next;
7491 delete_breakpoint (b->related_breakpoint);
7492 }
7493 delete_breakpoint (b);
7494 }
7495 }
7496
7497 void
7498 enable_overlay_breakpoints (void)
7499 {
7500 for (breakpoint *b : all_breakpoints ())
7501 if (b->type == bp_overlay_event)
7502 {
7503 b->enable_state = bp_enabled;
7504 update_global_location_list (UGLL_MAY_INSERT);
7505 overlay_events_enabled = 1;
7506 }
7507 }
7508
7509 void
7510 disable_overlay_breakpoints (void)
7511 {
7512 for (breakpoint *b : all_breakpoints ())
7513 if (b->type == bp_overlay_event)
7514 {
7515 b->enable_state = bp_disabled;
7516 update_global_location_list (UGLL_DONT_INSERT);
7517 overlay_events_enabled = 0;
7518 }
7519 }
7520
7521 /* Set an active std::terminate breakpoint for each std::terminate
7522 master breakpoint. */
7523 void
7524 set_std_terminate_breakpoint (void)
7525 {
7526 for (breakpoint *b : all_breakpoints_safe ())
7527 if (b->pspace == current_program_space
7528 && b->type == bp_std_terminate_master)
7529 {
7530 momentary_breakpoint_from_master (b, bp_std_terminate,
7531 &momentary_breakpoint_ops, 1);
7532 }
7533 }
7534
7535 /* Delete all the std::terminate breakpoints. */
7536 void
7537 delete_std_terminate_breakpoint (void)
7538 {
7539 for (breakpoint *b : all_breakpoints_safe ())
7540 if (b->type == bp_std_terminate)
7541 delete_breakpoint (b);
7542 }
7543
7544 struct breakpoint *
7545 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7546 {
7547 struct breakpoint *b;
7548
7549 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7550 &internal_breakpoint_ops);
7551
7552 b->enable_state = bp_enabled;
7553 /* location has to be used or breakpoint_re_set will delete me. */
7554 b->location = new_address_location (b->loc->address, NULL, 0);
7555
7556 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7557
7558 return b;
7559 }
7560
7561 struct lang_and_radix
7562 {
7563 enum language lang;
7564 int radix;
7565 };
7566
7567 /* Create a breakpoint for JIT code registration and unregistration. */
7568
7569 struct breakpoint *
7570 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7571 {
7572 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7573 &internal_breakpoint_ops);
7574 }
7575
7576 /* Remove JIT code registration and unregistration breakpoint(s). */
7577
7578 void
7579 remove_jit_event_breakpoints (void)
7580 {
7581 for (breakpoint *b : all_breakpoints_safe ())
7582 if (b->type == bp_jit_event
7583 && b->loc->pspace == current_program_space)
7584 delete_breakpoint (b);
7585 }
7586
7587 void
7588 remove_solib_event_breakpoints (void)
7589 {
7590 for (breakpoint *b : all_breakpoints_safe ())
7591 if (b->type == bp_shlib_event
7592 && b->loc->pspace == current_program_space)
7593 delete_breakpoint (b);
7594 }
7595
7596 /* See breakpoint.h. */
7597
7598 void
7599 remove_solib_event_breakpoints_at_next_stop (void)
7600 {
7601 for (breakpoint *b : all_breakpoints_safe ())
7602 if (b->type == bp_shlib_event
7603 && b->loc->pspace == current_program_space)
7604 b->disposition = disp_del_at_next_stop;
7605 }
7606
7607 /* Helper for create_solib_event_breakpoint /
7608 create_and_insert_solib_event_breakpoint. Allows specifying which
7609 INSERT_MODE to pass through to update_global_location_list. */
7610
7611 static struct breakpoint *
7612 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7613 enum ugll_insert_mode insert_mode)
7614 {
7615 struct breakpoint *b;
7616
7617 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7618 &internal_breakpoint_ops);
7619 update_global_location_list_nothrow (insert_mode);
7620 return b;
7621 }
7622
7623 struct breakpoint *
7624 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7625 {
7626 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7627 }
7628
7629 /* See breakpoint.h. */
7630
7631 struct breakpoint *
7632 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7633 {
7634 struct breakpoint *b;
7635
7636 /* Explicitly tell update_global_location_list to insert
7637 locations. */
7638 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7639 if (!b->loc->inserted)
7640 {
7641 delete_breakpoint (b);
7642 return NULL;
7643 }
7644 return b;
7645 }
7646
7647 /* Disable any breakpoints that are on code in shared libraries. Only
7648 apply to enabled breakpoints, disabled ones can just stay disabled. */
7649
7650 void
7651 disable_breakpoints_in_shlibs (void)
7652 {
7653 for (bp_location *loc : all_bp_locations ())
7654 {
7655 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7656 struct breakpoint *b = loc->owner;
7657
7658 /* We apply the check to all breakpoints, including disabled for
7659 those with loc->duplicate set. This is so that when breakpoint
7660 becomes enabled, or the duplicate is removed, gdb will try to
7661 insert all breakpoints. If we don't set shlib_disabled here,
7662 we'll try to insert those breakpoints and fail. */
7663 if (((b->type == bp_breakpoint)
7664 || (b->type == bp_jit_event)
7665 || (b->type == bp_hardware_breakpoint)
7666 || (is_tracepoint (b)))
7667 && loc->pspace == current_program_space
7668 && !loc->shlib_disabled
7669 && solib_name_from_address (loc->pspace, loc->address)
7670 )
7671 {
7672 loc->shlib_disabled = 1;
7673 }
7674 }
7675 }
7676
7677 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7678 notification of unloaded_shlib. Only apply to enabled breakpoints,
7679 disabled ones can just stay disabled. */
7680
7681 static void
7682 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7683 {
7684 int disabled_shlib_breaks = 0;
7685
7686 for (bp_location *loc : all_bp_locations ())
7687 {
7688 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7689 struct breakpoint *b = loc->owner;
7690
7691 if (solib->pspace == loc->pspace
7692 && !loc->shlib_disabled
7693 && (((b->type == bp_breakpoint
7694 || b->type == bp_jit_event
7695 || b->type == bp_hardware_breakpoint)
7696 && (loc->loc_type == bp_loc_hardware_breakpoint
7697 || loc->loc_type == bp_loc_software_breakpoint))
7698 || is_tracepoint (b))
7699 && solib_contains_address_p (solib, loc->address))
7700 {
7701 loc->shlib_disabled = 1;
7702 /* At this point, we cannot rely on remove_breakpoint
7703 succeeding so we must mark the breakpoint as not inserted
7704 to prevent future errors occurring in remove_breakpoints. */
7705 loc->inserted = 0;
7706
7707 /* This may cause duplicate notifications for the same breakpoint. */
7708 gdb::observers::breakpoint_modified.notify (b);
7709
7710 if (!disabled_shlib_breaks)
7711 {
7712 target_terminal::ours_for_output ();
7713 warning (_("Temporarily disabling breakpoints "
7714 "for unloaded shared library \"%s\""),
7715 solib->so_name);
7716 }
7717 disabled_shlib_breaks = 1;
7718 }
7719 }
7720 }
7721
7722 /* Disable any breakpoints and tracepoints in OBJFILE upon
7723 notification of free_objfile. Only apply to enabled breakpoints,
7724 disabled ones can just stay disabled. */
7725
7726 static void
7727 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7728 {
7729 if (objfile == NULL)
7730 return;
7731
7732 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7733 managed by the user with add-symbol-file/remove-symbol-file.
7734 Similarly to how breakpoints in shared libraries are handled in
7735 response to "nosharedlibrary", mark breakpoints in such modules
7736 shlib_disabled so they end up uninserted on the next global
7737 location list update. Shared libraries not loaded by the user
7738 aren't handled here -- they're already handled in
7739 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7740 solib_unloaded observer. We skip objfiles that are not
7741 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7742 main objfile). */
7743 if ((objfile->flags & OBJF_SHARED) == 0
7744 || (objfile->flags & OBJF_USERLOADED) == 0)
7745 return;
7746
7747 for (breakpoint *b : all_breakpoints ())
7748 {
7749 int bp_modified = 0;
7750
7751 if (!is_breakpoint (b) && !is_tracepoint (b))
7752 continue;
7753
7754 for (bp_location *loc : b->locations ())
7755 {
7756 CORE_ADDR loc_addr = loc->address;
7757
7758 if (loc->loc_type != bp_loc_hardware_breakpoint
7759 && loc->loc_type != bp_loc_software_breakpoint)
7760 continue;
7761
7762 if (loc->shlib_disabled != 0)
7763 continue;
7764
7765 if (objfile->pspace != loc->pspace)
7766 continue;
7767
7768 if (loc->loc_type != bp_loc_hardware_breakpoint
7769 && loc->loc_type != bp_loc_software_breakpoint)
7770 continue;
7771
7772 if (is_addr_in_objfile (loc_addr, objfile))
7773 {
7774 loc->shlib_disabled = 1;
7775 /* At this point, we don't know whether the object was
7776 unmapped from the inferior or not, so leave the
7777 inserted flag alone. We'll handle failure to
7778 uninsert quietly, in case the object was indeed
7779 unmapped. */
7780
7781 mark_breakpoint_location_modified (loc);
7782
7783 bp_modified = 1;
7784 }
7785 }
7786
7787 if (bp_modified)
7788 gdb::observers::breakpoint_modified.notify (b);
7789 }
7790 }
7791
7792 /* See breakpoint.h. */
7793
7794 void
7795 init_catchpoint (struct breakpoint *b,
7796 struct gdbarch *gdbarch, bool temp,
7797 const char *cond_string,
7798 const struct breakpoint_ops *ops)
7799 {
7800 symtab_and_line sal;
7801 sal.pspace = current_program_space;
7802
7803 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
7804
7805 if (cond_string == nullptr)
7806 b->cond_string.reset ();
7807 else
7808 b->cond_string = make_unique_xstrdup (cond_string);
7809 b->disposition = temp ? disp_del : disp_donttouch;
7810 }
7811
7812 void
7813 install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
7814 {
7815 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
7816 set_breakpoint_number (internal, b);
7817 if (is_tracepoint (b))
7818 set_tracepoint_count (breakpoint_count);
7819 if (!internal)
7820 mention (b);
7821 gdb::observers::breakpoint_created.notify (b);
7822
7823 if (update_gll)
7824 update_global_location_list (UGLL_MAY_INSERT);
7825 }
7826
7827 static int
7828 hw_breakpoint_used_count (void)
7829 {
7830 int i = 0;
7831
7832 for (breakpoint *b : all_breakpoints ())
7833 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
7834 for (bp_location *bl : b->locations ())
7835 {
7836 /* Special types of hardware breakpoints may use more than
7837 one register. */
7838 i += b->ops->resources_needed (bl);
7839 }
7840
7841 return i;
7842 }
7843
7844 /* Returns the resources B would use if it were a hardware
7845 watchpoint. */
7846
7847 static int
7848 hw_watchpoint_use_count (struct breakpoint *b)
7849 {
7850 int i = 0;
7851
7852 if (!breakpoint_enabled (b))
7853 return 0;
7854
7855 for (bp_location *bl : b->locations ())
7856 {
7857 /* Special types of hardware watchpoints may use more than
7858 one register. */
7859 i += b->ops->resources_needed (bl);
7860 }
7861
7862 return i;
7863 }
7864
7865 /* Returns the sum the used resources of all hardware watchpoints of
7866 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
7867 the sum of the used resources of all hardware watchpoints of other
7868 types _not_ TYPE. */
7869
7870 static int
7871 hw_watchpoint_used_count_others (struct breakpoint *except,
7872 enum bptype type, int *other_type_used)
7873 {
7874 int i = 0;
7875
7876 *other_type_used = 0;
7877 for (breakpoint *b : all_breakpoints ())
7878 {
7879 if (b == except)
7880 continue;
7881 if (!breakpoint_enabled (b))
7882 continue;
7883
7884 if (b->type == type)
7885 i += hw_watchpoint_use_count (b);
7886 else if (is_hardware_watchpoint (b))
7887 *other_type_used = 1;
7888 }
7889
7890 return i;
7891 }
7892
7893 void
7894 disable_watchpoints_before_interactive_call_start (void)
7895 {
7896 for (breakpoint *b : all_breakpoints ())
7897 if (is_watchpoint (b) && breakpoint_enabled (b))
7898 {
7899 b->enable_state = bp_call_disabled;
7900 update_global_location_list (UGLL_DONT_INSERT);
7901 }
7902 }
7903
7904 void
7905 enable_watchpoints_after_interactive_call_stop (void)
7906 {
7907 for (breakpoint *b : all_breakpoints ())
7908 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
7909 {
7910 b->enable_state = bp_enabled;
7911 update_global_location_list (UGLL_MAY_INSERT);
7912 }
7913 }
7914
7915 void
7916 disable_breakpoints_before_startup (void)
7917 {
7918 current_program_space->executing_startup = 1;
7919 update_global_location_list (UGLL_DONT_INSERT);
7920 }
7921
7922 void
7923 enable_breakpoints_after_startup (void)
7924 {
7925 current_program_space->executing_startup = 0;
7926 breakpoint_re_set ();
7927 }
7928
7929 /* Create a new single-step breakpoint for thread THREAD, with no
7930 locations. */
7931
7932 static struct breakpoint *
7933 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
7934 {
7935 std::unique_ptr<breakpoint> b (new breakpoint ());
7936
7937 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7938 &momentary_breakpoint_ops);
7939
7940 b->disposition = disp_donttouch;
7941 b->frame_id = null_frame_id;
7942
7943 b->thread = thread;
7944 gdb_assert (b->thread != 0);
7945
7946 return add_to_breakpoint_chain (std::move (b));
7947 }
7948
7949 /* Set a momentary breakpoint of type TYPE at address specified by
7950 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
7951 frame. */
7952
7953 breakpoint_up
7954 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
7955 struct frame_id frame_id, enum bptype type)
7956 {
7957 struct breakpoint *b;
7958
7959 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
7960 tail-called one. */
7961 gdb_assert (!frame_id_artificial_p (frame_id));
7962
7963 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
7964 b->enable_state = bp_enabled;
7965 b->disposition = disp_donttouch;
7966 b->frame_id = frame_id;
7967
7968 b->thread = inferior_thread ()->global_num;
7969
7970 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7971
7972 return breakpoint_up (b);
7973 }
7974
7975 /* Make a momentary breakpoint based on the master breakpoint ORIG.
7976 The new breakpoint will have type TYPE, use OPS as its
7977 breakpoint_ops, and will set enabled to LOC_ENABLED. */
7978
7979 static struct breakpoint *
7980 momentary_breakpoint_from_master (struct breakpoint *orig,
7981 enum bptype type,
7982 const struct breakpoint_ops *ops,
7983 int loc_enabled)
7984 {
7985 struct breakpoint *copy;
7986
7987 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
7988 copy->loc = allocate_bp_location (copy);
7989 set_breakpoint_location_function (copy->loc);
7990
7991 copy->loc->gdbarch = orig->loc->gdbarch;
7992 copy->loc->requested_address = orig->loc->requested_address;
7993 copy->loc->address = orig->loc->address;
7994 copy->loc->section = orig->loc->section;
7995 copy->loc->pspace = orig->loc->pspace;
7996 copy->loc->probe = orig->loc->probe;
7997 copy->loc->line_number = orig->loc->line_number;
7998 copy->loc->symtab = orig->loc->symtab;
7999 copy->loc->enabled = loc_enabled;
8000 copy->frame_id = orig->frame_id;
8001 copy->thread = orig->thread;
8002 copy->pspace = orig->pspace;
8003
8004 copy->enable_state = bp_enabled;
8005 copy->disposition = disp_donttouch;
8006 copy->number = internal_breakpoint_number--;
8007
8008 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8009 return copy;
8010 }
8011
8012 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8013 ORIG is NULL. */
8014
8015 struct breakpoint *
8016 clone_momentary_breakpoint (struct breakpoint *orig)
8017 {
8018 /* If there's nothing to clone, then return nothing. */
8019 if (orig == NULL)
8020 return NULL;
8021
8022 gdb_assert (orig->ops == &momentary_breakpoint_ops);
8023 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8024 }
8025
8026 breakpoint_up
8027 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8028 enum bptype type)
8029 {
8030 struct symtab_and_line sal;
8031
8032 sal = find_pc_line (pc, 0);
8033 sal.pc = pc;
8034 sal.section = find_pc_overlay (pc);
8035 sal.explicit_pc = 1;
8036
8037 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8038 }
8039 \f
8040
8041 /* Tell the user we have just set a breakpoint B. */
8042
8043 static void
8044 mention (struct breakpoint *b)
8045 {
8046 b->ops->print_mention (b);
8047 current_uiout->text ("\n");
8048 }
8049 \f
8050
8051 static bool bp_loc_is_permanent (struct bp_location *loc);
8052
8053 /* Handle "set breakpoint auto-hw on".
8054
8055 If the explicitly specified breakpoint type is not hardware
8056 breakpoint, check the memory map to see whether the breakpoint
8057 address is in read-only memory.
8058
8059 - location type is not hardware breakpoint, memory is read-only.
8060 We change the type of the location to hardware breakpoint.
8061
8062 - location type is hardware breakpoint, memory is read-write. This
8063 means we've previously made the location hardware one, but then the
8064 memory map changed, so we undo.
8065 */
8066
8067 static void
8068 handle_automatic_hardware_breakpoints (bp_location *bl)
8069 {
8070 if (automatic_hardware_breakpoints
8071 && bl->owner->type != bp_hardware_breakpoint
8072 && (bl->loc_type == bp_loc_software_breakpoint
8073 || bl->loc_type == bp_loc_hardware_breakpoint))
8074 {
8075 /* When breakpoints are removed, remove_breakpoints will use
8076 location types we've just set here, the only possible problem
8077 is that memory map has changed during running program, but
8078 it's not going to work anyway with current gdb. */
8079 mem_region *mr = lookup_mem_region (bl->address);
8080
8081 if (mr != nullptr)
8082 {
8083 enum bp_loc_type new_type;
8084
8085 if (mr->attrib.mode != MEM_RW)
8086 new_type = bp_loc_hardware_breakpoint;
8087 else
8088 new_type = bp_loc_software_breakpoint;
8089
8090 if (new_type != bl->loc_type)
8091 {
8092 static bool said = false;
8093
8094 bl->loc_type = new_type;
8095 if (!said)
8096 {
8097 gdb_printf (_("Note: automatically using "
8098 "hardware breakpoints for "
8099 "read-only addresses.\n"));
8100 said = true;
8101 }
8102 }
8103 }
8104 }
8105 }
8106
8107 static struct bp_location *
8108 add_location_to_breakpoint (struct breakpoint *b,
8109 const struct symtab_and_line *sal)
8110 {
8111 struct bp_location *loc, **tmp;
8112 CORE_ADDR adjusted_address;
8113 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8114
8115 if (loc_gdbarch == NULL)
8116 loc_gdbarch = b->gdbarch;
8117
8118 /* Adjust the breakpoint's address prior to allocating a location.
8119 Once we call allocate_bp_location(), that mostly uninitialized
8120 location will be placed on the location chain. Adjustment of the
8121 breakpoint may cause target_read_memory() to be called and we do
8122 not want its scan of the location chain to find a breakpoint and
8123 location that's only been partially initialized. */
8124 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8125 sal->pc, b->type);
8126
8127 /* Sort the locations by their ADDRESS. */
8128 loc = allocate_bp_location (b);
8129 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8130 tmp = &((*tmp)->next))
8131 ;
8132 loc->next = *tmp;
8133 *tmp = loc;
8134
8135 loc->requested_address = sal->pc;
8136 loc->address = adjusted_address;
8137 loc->pspace = sal->pspace;
8138 loc->probe.prob = sal->prob;
8139 loc->probe.objfile = sal->objfile;
8140 gdb_assert (loc->pspace != NULL);
8141 loc->section = sal->section;
8142 loc->gdbarch = loc_gdbarch;
8143 loc->line_number = sal->line;
8144 loc->symtab = sal->symtab;
8145 loc->symbol = sal->symbol;
8146 loc->msymbol = sal->msymbol;
8147 loc->objfile = sal->objfile;
8148
8149 set_breakpoint_location_function (loc);
8150
8151 /* While by definition, permanent breakpoints are already present in the
8152 code, we don't mark the location as inserted. Normally one would expect
8153 that GDB could rely on that breakpoint instruction to stop the program,
8154 thus removing the need to insert its own breakpoint, except that executing
8155 the breakpoint instruction can kill the target instead of reporting a
8156 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8157 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8158 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8159 breakpoint be inserted normally results in QEMU knowing about the GDB
8160 breakpoint, and thus trap before the breakpoint instruction is executed.
8161 (If GDB later needs to continue execution past the permanent breakpoint,
8162 it manually increments the PC, thus avoiding executing the breakpoint
8163 instruction.) */
8164 if (bp_loc_is_permanent (loc))
8165 loc->permanent = 1;
8166
8167 return loc;
8168 }
8169 \f
8170
8171 /* Return true if LOC is pointing to a permanent breakpoint,
8172 return false otherwise. */
8173
8174 static bool
8175 bp_loc_is_permanent (struct bp_location *loc)
8176 {
8177 gdb_assert (loc != NULL);
8178
8179 /* If we have a non-breakpoint-backed catchpoint or a software
8180 watchpoint, just return 0. We should not attempt to read from
8181 the addresses the locations of these breakpoint types point to.
8182 gdbarch_program_breakpoint_here_p, below, will attempt to read
8183 memory. */
8184 if (!bl_address_is_meaningful (loc))
8185 return false;
8186
8187 scoped_restore_current_pspace_and_thread restore_pspace_thread;
8188 switch_to_program_space_and_thread (loc->pspace);
8189 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
8190 }
8191
8192 /* Build a command list for the dprintf corresponding to the current
8193 settings of the dprintf style options. */
8194
8195 static void
8196 update_dprintf_command_list (struct breakpoint *b)
8197 {
8198 const char *dprintf_args = b->extra_string.get ();
8199 gdb::unique_xmalloc_ptr<char> printf_line = nullptr;
8200
8201 if (!dprintf_args)
8202 return;
8203
8204 dprintf_args = skip_spaces (dprintf_args);
8205
8206 /* Allow a comma, as it may have terminated a location, but don't
8207 insist on it. */
8208 if (*dprintf_args == ',')
8209 ++dprintf_args;
8210 dprintf_args = skip_spaces (dprintf_args);
8211
8212 if (*dprintf_args != '"')
8213 error (_("Bad format string, missing '\"'."));
8214
8215 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8216 printf_line = xstrprintf ("printf %s", dprintf_args);
8217 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8218 {
8219 if (dprintf_function.empty ())
8220 error (_("No function supplied for dprintf call"));
8221
8222 if (!dprintf_channel.empty ())
8223 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8224 dprintf_function.c_str (),
8225 dprintf_channel.c_str (),
8226 dprintf_args);
8227 else
8228 printf_line = xstrprintf ("call (void) %s (%s)",
8229 dprintf_function.c_str (),
8230 dprintf_args);
8231 }
8232 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8233 {
8234 if (target_can_run_breakpoint_commands ())
8235 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8236 else
8237 {
8238 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8239 printf_line = xstrprintf ("printf %s", dprintf_args);
8240 }
8241 }
8242 else
8243 internal_error (__FILE__, __LINE__,
8244 _("Invalid dprintf style."));
8245
8246 gdb_assert (printf_line != NULL);
8247
8248 /* Manufacture a printf sequence. */
8249 struct command_line *printf_cmd_line
8250 = new struct command_line (simple_control, printf_line.release ());
8251 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8252 command_lines_deleter ()));
8253 }
8254
8255 /* Update all dprintf commands, making their command lists reflect
8256 current style settings. */
8257
8258 static void
8259 update_dprintf_commands (const char *args, int from_tty,
8260 struct cmd_list_element *c)
8261 {
8262 for (breakpoint *b : all_breakpoints ())
8263 if (b->type == bp_dprintf)
8264 update_dprintf_command_list (b);
8265 }
8266
8267 /* Create a breakpoint with SAL as location. Use LOCATION
8268 as a description of the location, and COND_STRING
8269 as condition expression. If LOCATION is NULL then create an
8270 "address location" from the address in the SAL. */
8271
8272 static void
8273 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8274 gdb::array_view<const symtab_and_line> sals,
8275 event_location_up &&location,
8276 gdb::unique_xmalloc_ptr<char> filter,
8277 gdb::unique_xmalloc_ptr<char> cond_string,
8278 gdb::unique_xmalloc_ptr<char> extra_string,
8279 enum bptype type, enum bpdisp disposition,
8280 int thread, int task, int ignore_count,
8281 const struct breakpoint_ops *ops, int from_tty,
8282 int enabled, int internal, unsigned flags,
8283 int display_canonical)
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 for (const auto &sal : sals)
8304 {
8305 struct bp_location *loc;
8306
8307 if (from_tty)
8308 {
8309 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8310 if (!loc_gdbarch)
8311 loc_gdbarch = gdbarch;
8312
8313 describe_other_breakpoints (loc_gdbarch,
8314 sal.pspace, sal.pc, sal.section, thread);
8315 }
8316
8317 if (&sal == &sals[0])
8318 {
8319 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8320 b->thread = thread;
8321 b->task = task;
8322
8323 b->cond_string = std::move (cond_string);
8324 b->extra_string = std::move (extra_string);
8325 b->ignore_count = ignore_count;
8326 b->enable_state = enabled ? bp_enabled : bp_disabled;
8327 b->disposition = disposition;
8328
8329 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8330 b->loc->inserted = 1;
8331
8332 if (type == bp_static_tracepoint)
8333 {
8334 struct tracepoint *t = (struct tracepoint *) b;
8335 struct static_tracepoint_marker marker;
8336
8337 if (strace_marker_p (b))
8338 {
8339 /* We already know the marker exists, otherwise, we
8340 wouldn't see a sal for it. */
8341 const char *p
8342 = &event_location_to_string (b->location.get ())[3];
8343 const char *endp;
8344
8345 p = skip_spaces (p);
8346
8347 endp = skip_to_space (p);
8348
8349 t->static_trace_marker_id.assign (p, endp - p);
8350
8351 gdb_printf (_("Probed static tracepoint "
8352 "marker \"%s\"\n"),
8353 t->static_trace_marker_id.c_str ());
8354 }
8355 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8356 {
8357 t->static_trace_marker_id = std::move (marker.str_id);
8358
8359 gdb_printf (_("Probed static tracepoint "
8360 "marker \"%s\"\n"),
8361 t->static_trace_marker_id.c_str ());
8362 }
8363 else
8364 warning (_("Couldn't determine the static "
8365 "tracepoint marker to probe"));
8366 }
8367
8368 loc = b->loc;
8369 }
8370 else
8371 {
8372 loc = add_location_to_breakpoint (b, &sal);
8373 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8374 loc->inserted = 1;
8375 }
8376
8377 /* Do not set breakpoint locations conditions yet. As locations
8378 are inserted, they get sorted based on their addresses. Let
8379 the list stabilize to have reliable location numbers. */
8380
8381 /* Dynamic printf requires and uses additional arguments on the
8382 command line, otherwise it's an error. */
8383 if (type == bp_dprintf)
8384 {
8385 if (b->extra_string)
8386 update_dprintf_command_list (b);
8387 else
8388 error (_("Format string required"));
8389 }
8390 else if (b->extra_string)
8391 error (_("Garbage '%s' at end of command"), b->extra_string.get ());
8392 }
8393
8394
8395 /* The order of the locations is now stable. Set the location
8396 condition using the location's number. */
8397 int loc_num = 1;
8398 for (bp_location *loc : b->locations ())
8399 {
8400 if (b->cond_string != nullptr)
8401 set_breakpoint_location_condition (b->cond_string.get (), loc,
8402 b->number, loc_num);
8403
8404 ++loc_num;
8405 }
8406
8407 b->display_canonical = display_canonical;
8408 if (location != NULL)
8409 b->location = std::move (location);
8410 else
8411 b->location = new_address_location (b->loc->address, NULL, 0);
8412 b->filter = std::move (filter);
8413 }
8414
8415 static void
8416 create_breakpoint_sal (struct gdbarch *gdbarch,
8417 gdb::array_view<const symtab_and_line> sals,
8418 event_location_up &&location,
8419 gdb::unique_xmalloc_ptr<char> filter,
8420 gdb::unique_xmalloc_ptr<char> cond_string,
8421 gdb::unique_xmalloc_ptr<char> extra_string,
8422 enum bptype type, enum bpdisp disposition,
8423 int thread, int task, int ignore_count,
8424 const struct breakpoint_ops *ops, int from_tty,
8425 int enabled, int internal, unsigned flags,
8426 int display_canonical)
8427 {
8428 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
8429
8430 init_breakpoint_sal (b.get (), gdbarch,
8431 sals, std::move (location),
8432 std::move (filter),
8433 std::move (cond_string),
8434 std::move (extra_string),
8435 type, disposition,
8436 thread, task, ignore_count,
8437 ops, from_tty,
8438 enabled, internal, flags,
8439 display_canonical);
8440
8441 install_breakpoint (internal, std::move (b), 0);
8442 }
8443
8444 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8445 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8446 value. COND_STRING, if not NULL, specified the condition to be
8447 used for all breakpoints. Essentially the only case where
8448 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8449 function. In that case, it's still not possible to specify
8450 separate conditions for different overloaded functions, so
8451 we take just a single condition string.
8452
8453 NOTE: If the function succeeds, the caller is expected to cleanup
8454 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8455 array contents). If the function fails (error() is called), the
8456 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8457 COND and SALS arrays and each of those arrays contents. */
8458
8459 static void
8460 create_breakpoints_sal (struct gdbarch *gdbarch,
8461 struct linespec_result *canonical,
8462 gdb::unique_xmalloc_ptr<char> cond_string,
8463 gdb::unique_xmalloc_ptr<char> extra_string,
8464 enum bptype type, enum bpdisp disposition,
8465 int thread, int task, int ignore_count,
8466 const struct breakpoint_ops *ops, int from_tty,
8467 int enabled, int internal, unsigned flags)
8468 {
8469 if (canonical->pre_expanded)
8470 gdb_assert (canonical->lsals.size () == 1);
8471
8472 for (const auto &lsal : canonical->lsals)
8473 {
8474 /* Note that 'location' can be NULL in the case of a plain
8475 'break', without arguments. */
8476 event_location_up location
8477 = (canonical->location != NULL
8478 ? copy_event_location (canonical->location.get ()) : NULL);
8479 gdb::unique_xmalloc_ptr<char> filter_string
8480 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
8481
8482 create_breakpoint_sal (gdbarch, lsal.sals,
8483 std::move (location),
8484 std::move (filter_string),
8485 std::move (cond_string),
8486 std::move (extra_string),
8487 type, disposition,
8488 thread, task, ignore_count, ops,
8489 from_tty, enabled, internal, flags,
8490 canonical->special_display);
8491 }
8492 }
8493
8494 /* Parse LOCATION which is assumed to be a SAL specification possibly
8495 followed by conditionals. On return, SALS contains an array of SAL
8496 addresses found. LOCATION points to the end of the SAL (for
8497 linespec locations).
8498
8499 The array and the line spec strings are allocated on the heap, it is
8500 the caller's responsibility to free them. */
8501
8502 static void
8503 parse_breakpoint_sals (struct event_location *location,
8504 struct linespec_result *canonical)
8505 {
8506 struct symtab_and_line cursal;
8507
8508 if (event_location_type (location) == LINESPEC_LOCATION)
8509 {
8510 const char *spec = get_linespec_location (location)->spec_string;
8511
8512 if (spec == NULL)
8513 {
8514 /* The last displayed codepoint, if it's valid, is our default
8515 breakpoint address. */
8516 if (last_displayed_sal_is_valid ())
8517 {
8518 /* Set sal's pspace, pc, symtab, and line to the values
8519 corresponding to the last call to print_frame_info.
8520 Be sure to reinitialize LINE with NOTCURRENT == 0
8521 as the breakpoint line number is inappropriate otherwise.
8522 find_pc_line would adjust PC, re-set it back. */
8523 symtab_and_line sal = get_last_displayed_sal ();
8524 CORE_ADDR pc = sal.pc;
8525
8526 sal = find_pc_line (pc, 0);
8527
8528 /* "break" without arguments is equivalent to "break *PC"
8529 where PC is the last displayed codepoint's address. So
8530 make sure to set sal.explicit_pc to prevent GDB from
8531 trying to expand the list of sals to include all other
8532 instances with the same symtab and line. */
8533 sal.pc = pc;
8534 sal.explicit_pc = 1;
8535
8536 struct linespec_sals lsal;
8537 lsal.sals = {sal};
8538 lsal.canonical = NULL;
8539
8540 canonical->lsals.push_back (std::move (lsal));
8541 return;
8542 }
8543 else
8544 error (_("No default breakpoint address now."));
8545 }
8546 }
8547
8548 /* Force almost all breakpoints to be in terms of the
8549 current_source_symtab (which is decode_line_1's default).
8550 This should produce the results we want almost all of the
8551 time while leaving default_breakpoint_* alone.
8552
8553 ObjC: However, don't match an Objective-C method name which
8554 may have a '+' or '-' succeeded by a '['. */
8555 cursal = get_current_source_symtab_and_line ();
8556 if (last_displayed_sal_is_valid ())
8557 {
8558 const char *spec = NULL;
8559
8560 if (event_location_type (location) == LINESPEC_LOCATION)
8561 spec = get_linespec_location (location)->spec_string;
8562
8563 if (!cursal.symtab
8564 || (spec != NULL
8565 && strchr ("+-", spec[0]) != NULL
8566 && spec[1] != '['))
8567 {
8568 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
8569 get_last_displayed_symtab (),
8570 get_last_displayed_line (),
8571 canonical, NULL, NULL);
8572 return;
8573 }
8574 }
8575
8576 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
8577 cursal.symtab, cursal.line, canonical, NULL, NULL);
8578 }
8579
8580
8581 /* Convert each SAL into a real PC. Verify that the PC can be
8582 inserted as a breakpoint. If it can't throw an error. */
8583
8584 static void
8585 breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
8586 {
8587 for (auto &sal : sals)
8588 resolve_sal_pc (&sal);
8589 }
8590
8591 /* Fast tracepoints may have restrictions on valid locations. For
8592 instance, a fast tracepoint using a jump instead of a trap will
8593 likely have to overwrite more bytes than a trap would, and so can
8594 only be placed where the instruction is longer than the jump, or a
8595 multi-instruction sequence does not have a jump into the middle of
8596 it, etc. */
8597
8598 static void
8599 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8600 gdb::array_view<const symtab_and_line> sals)
8601 {
8602 for (const auto &sal : sals)
8603 {
8604 struct gdbarch *sarch;
8605
8606 sarch = get_sal_arch (sal);
8607 /* We fall back to GDBARCH if there is no architecture
8608 associated with SAL. */
8609 if (sarch == NULL)
8610 sarch = gdbarch;
8611 std::string msg;
8612 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
8613 error (_("May not have a fast tracepoint at %s%s"),
8614 paddress (sarch, sal.pc), msg.c_str ());
8615 }
8616 }
8617
8618 /* Given TOK, a string specification of condition and thread, as
8619 accepted by the 'break' command, extract the condition
8620 string and thread number and set *COND_STRING and *THREAD.
8621 PC identifies the context at which the condition should be parsed.
8622 If no condition is found, *COND_STRING is set to NULL.
8623 If no thread is found, *THREAD is set to -1. */
8624
8625 static void
8626 find_condition_and_thread (const char *tok, CORE_ADDR pc,
8627 gdb::unique_xmalloc_ptr<char> *cond_string,
8628 int *thread, int *task,
8629 gdb::unique_xmalloc_ptr<char> *rest)
8630 {
8631 cond_string->reset ();
8632 *thread = -1;
8633 *task = 0;
8634 rest->reset ();
8635 bool force = false;
8636
8637 while (tok && *tok)
8638 {
8639 const char *end_tok;
8640 int toklen;
8641 const char *cond_start = NULL;
8642 const char *cond_end = NULL;
8643
8644 tok = skip_spaces (tok);
8645
8646 if ((*tok == '"' || *tok == ',') && rest)
8647 {
8648 rest->reset (savestring (tok, strlen (tok)));
8649 return;
8650 }
8651
8652 end_tok = skip_to_space (tok);
8653
8654 toklen = end_tok - tok;
8655
8656 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8657 {
8658 tok = cond_start = end_tok + 1;
8659 try
8660 {
8661 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
8662 }
8663 catch (const gdb_exception_error &)
8664 {
8665 if (!force)
8666 throw;
8667 else
8668 tok = tok + strlen (tok);
8669 }
8670 cond_end = tok;
8671 cond_string->reset (savestring (cond_start, cond_end - cond_start));
8672 }
8673 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
8674 {
8675 tok = tok + toklen;
8676 force = true;
8677 }
8678 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8679 {
8680 const char *tmptok;
8681 struct thread_info *thr;
8682
8683 tok = end_tok + 1;
8684 thr = parse_thread_id (tok, &tmptok);
8685 if (tok == tmptok)
8686 error (_("Junk after thread keyword."));
8687 *thread = thr->global_num;
8688 tok = tmptok;
8689 }
8690 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8691 {
8692 char *tmptok;
8693
8694 tok = end_tok + 1;
8695 *task = strtol (tok, &tmptok, 0);
8696 if (tok == tmptok)
8697 error (_("Junk after task keyword."));
8698 if (!valid_task_id (*task))
8699 error (_("Unknown task %d."), *task);
8700 tok = tmptok;
8701 }
8702 else if (rest)
8703 {
8704 rest->reset (savestring (tok, strlen (tok)));
8705 return;
8706 }
8707 else
8708 error (_("Junk at end of arguments."));
8709 }
8710 }
8711
8712 /* Call 'find_condition_and_thread' for each sal in SALS until a parse
8713 succeeds. The parsed values are written to COND_STRING, THREAD,
8714 TASK, and REST. See the comment of 'find_condition_and_thread'
8715 for the description of these parameters and INPUT. */
8716
8717 static void
8718 find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
8719 const char *input,
8720 gdb::unique_xmalloc_ptr<char> *cond_string,
8721 int *thread, int *task,
8722 gdb::unique_xmalloc_ptr<char> *rest)
8723 {
8724 int num_failures = 0;
8725 for (auto &sal : sals)
8726 {
8727 gdb::unique_xmalloc_ptr<char> cond;
8728 int thread_id = 0;
8729 int task_id = 0;
8730 gdb::unique_xmalloc_ptr<char> remaining;
8731
8732 /* Here we want to parse 'arg' to separate condition from thread
8733 number. But because parsing happens in a context and the
8734 contexts of sals might be different, try each until there is
8735 success. Finding one successful parse is sufficient for our
8736 goal. When setting the breakpoint we'll re-parse the
8737 condition in the context of each sal. */
8738 try
8739 {
8740 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
8741 &task_id, &remaining);
8742 *cond_string = std::move (cond);
8743 *thread = thread_id;
8744 *task = task_id;
8745 *rest = std::move (remaining);
8746 break;
8747 }
8748 catch (const gdb_exception_error &e)
8749 {
8750 num_failures++;
8751 /* If no sal remains, do not continue. */
8752 if (num_failures == sals.size ())
8753 throw;
8754 }
8755 }
8756 }
8757
8758 /* Decode a static tracepoint marker spec. */
8759
8760 static std::vector<symtab_and_line>
8761 decode_static_tracepoint_spec (const char **arg_p)
8762 {
8763 const char *p = &(*arg_p)[3];
8764 const char *endp;
8765
8766 p = skip_spaces (p);
8767
8768 endp = skip_to_space (p);
8769
8770 std::string marker_str (p, endp - p);
8771
8772 std::vector<static_tracepoint_marker> markers
8773 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
8774 if (markers.empty ())
8775 error (_("No known static tracepoint marker named %s"),
8776 marker_str.c_str ());
8777
8778 std::vector<symtab_and_line> sals;
8779 sals.reserve (markers.size ());
8780
8781 for (const static_tracepoint_marker &marker : markers)
8782 {
8783 symtab_and_line sal = find_pc_line (marker.address, 0);
8784 sal.pc = marker.address;
8785 sals.push_back (sal);
8786 }
8787
8788 *arg_p = endp;
8789 return sals;
8790 }
8791
8792 /* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
8793 according to IS_TRACEPOINT. */
8794
8795 static const struct breakpoint_ops *
8796 breakpoint_ops_for_event_location_type (enum event_location_type location_type,
8797 bool is_tracepoint)
8798 {
8799 if (is_tracepoint)
8800 {
8801 if (location_type == PROBE_LOCATION)
8802 return &tracepoint_probe_breakpoint_ops;
8803 else
8804 return &vtable_breakpoint_ops;
8805 }
8806 else
8807 {
8808 if (location_type == PROBE_LOCATION)
8809 return &bkpt_probe_breakpoint_ops;
8810 else
8811 return &bkpt_breakpoint_ops;
8812 }
8813 }
8814
8815 /* See breakpoint.h. */
8816
8817 const struct breakpoint_ops *
8818 breakpoint_ops_for_event_location (const struct event_location *location,
8819 bool is_tracepoint)
8820 {
8821 if (location != nullptr)
8822 return breakpoint_ops_for_event_location_type
8823 (event_location_type (location), is_tracepoint);
8824 return is_tracepoint ? &vtable_breakpoint_ops : &bkpt_breakpoint_ops;
8825 }
8826
8827 /* See breakpoint.h. */
8828
8829 int
8830 create_breakpoint (struct gdbarch *gdbarch,
8831 struct event_location *location,
8832 const char *cond_string,
8833 int thread, const char *extra_string,
8834 bool force_condition, int parse_extra,
8835 int tempflag, enum bptype type_wanted,
8836 int ignore_count,
8837 enum auto_boolean pending_break_support,
8838 const struct breakpoint_ops *ops,
8839 int from_tty, int enabled, int internal,
8840 unsigned flags)
8841 {
8842 struct linespec_result canonical;
8843 int pending = 0;
8844 int task = 0;
8845 int prev_bkpt_count = breakpoint_count;
8846
8847 gdb_assert (ops != NULL);
8848
8849 /* If extra_string isn't useful, set it to NULL. */
8850 if (extra_string != NULL && *extra_string == '\0')
8851 extra_string = NULL;
8852
8853 try
8854 {
8855 ops->create_sals_from_location (location, &canonical, type_wanted);
8856 }
8857 catch (const gdb_exception_error &e)
8858 {
8859 /* If caller is interested in rc value from parse, set
8860 value. */
8861 if (e.error == NOT_FOUND_ERROR)
8862 {
8863 /* If pending breakpoint support is turned off, throw
8864 error. */
8865
8866 if (pending_break_support == AUTO_BOOLEAN_FALSE)
8867 throw;
8868
8869 exception_print (gdb_stderr, e);
8870
8871 /* If pending breakpoint support is auto query and the user
8872 selects no, then simply return the error code. */
8873 if (pending_break_support == AUTO_BOOLEAN_AUTO
8874 && !nquery (_("Make %s pending on future shared library load? "),
8875 bptype_string (type_wanted)))
8876 return 0;
8877
8878 /* At this point, either the user was queried about setting
8879 a pending breakpoint and selected yes, or pending
8880 breakpoint behavior is on and thus a pending breakpoint
8881 is defaulted on behalf of the user. */
8882 pending = 1;
8883 }
8884 else
8885 throw;
8886 }
8887
8888 if (!pending && canonical.lsals.empty ())
8889 return 0;
8890
8891 /* Resolve all line numbers to PC's and verify that the addresses
8892 are ok for the target. */
8893 if (!pending)
8894 {
8895 for (auto &lsal : canonical.lsals)
8896 breakpoint_sals_to_pc (lsal.sals);
8897 }
8898
8899 /* Fast tracepoints may have additional restrictions on location. */
8900 if (!pending && type_wanted == bp_fast_tracepoint)
8901 {
8902 for (const auto &lsal : canonical.lsals)
8903 check_fast_tracepoint_sals (gdbarch, lsal.sals);
8904 }
8905
8906 /* Verify that condition can be parsed, before setting any
8907 breakpoints. Allocate a separate condition expression for each
8908 breakpoint. */
8909 if (!pending)
8910 {
8911 gdb::unique_xmalloc_ptr<char> cond_string_copy;
8912 gdb::unique_xmalloc_ptr<char> extra_string_copy;
8913
8914 if (parse_extra)
8915 {
8916 gdb::unique_xmalloc_ptr<char> rest;
8917 gdb::unique_xmalloc_ptr<char> cond;
8918
8919 const linespec_sals &lsal = canonical.lsals[0];
8920
8921 find_condition_and_thread_for_sals (lsal.sals, extra_string,
8922 &cond, &thread, &task, &rest);
8923 cond_string_copy = std::move (cond);
8924 extra_string_copy = std::move (rest);
8925 }
8926 else
8927 {
8928 if (type_wanted != bp_dprintf
8929 && extra_string != NULL && *extra_string != '\0')
8930 error (_("Garbage '%s' at end of location"), extra_string);
8931
8932 /* Check the validity of the condition. We should error out
8933 if the condition is invalid at all of the locations and
8934 if it is not forced. In the PARSE_EXTRA case above, this
8935 check is done when parsing the EXTRA_STRING. */
8936 if (cond_string != nullptr && !force_condition)
8937 {
8938 int num_failures = 0;
8939 const linespec_sals &lsal = canonical.lsals[0];
8940 for (const auto &sal : lsal.sals)
8941 {
8942 const char *cond = cond_string;
8943 try
8944 {
8945 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
8946 /* One success is sufficient to keep going. */
8947 break;
8948 }
8949 catch (const gdb_exception_error &)
8950 {
8951 num_failures++;
8952 /* If this is the last sal, error out. */
8953 if (num_failures == lsal.sals.size ())
8954 throw;
8955 }
8956 }
8957 }
8958
8959 /* Create a private copy of condition string. */
8960 if (cond_string)
8961 cond_string_copy.reset (xstrdup (cond_string));
8962 /* Create a private copy of any extra string. */
8963 if (extra_string)
8964 extra_string_copy.reset (xstrdup (extra_string));
8965 }
8966
8967 ops->create_breakpoints_sal (gdbarch, &canonical,
8968 std::move (cond_string_copy),
8969 std::move (extra_string_copy),
8970 type_wanted,
8971 tempflag ? disp_del : disp_donttouch,
8972 thread, task, ignore_count, ops,
8973 from_tty, enabled, internal, flags);
8974 }
8975 else
8976 {
8977 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
8978
8979 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
8980 b->location = copy_event_location (location);
8981
8982 if (parse_extra)
8983 b->cond_string = NULL;
8984 else
8985 {
8986 /* Create a private copy of condition string. */
8987 b->cond_string.reset (cond_string != NULL
8988 ? xstrdup (cond_string)
8989 : NULL);
8990 b->thread = thread;
8991 }
8992
8993 /* Create a private copy of any extra string. */
8994 b->extra_string.reset (extra_string != NULL
8995 ? xstrdup (extra_string)
8996 : NULL);
8997 b->ignore_count = ignore_count;
8998 b->disposition = tempflag ? disp_del : disp_donttouch;
8999 b->condition_not_parsed = 1;
9000 b->enable_state = enabled ? bp_enabled : bp_disabled;
9001 if ((type_wanted != bp_breakpoint
9002 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9003 b->pspace = current_program_space;
9004
9005 install_breakpoint (internal, std::move (b), 0);
9006 }
9007
9008 if (canonical.lsals.size () > 1)
9009 {
9010 warning (_("Multiple breakpoints were set.\nUse the "
9011 "\"delete\" command to delete unwanted breakpoints."));
9012 prev_breakpoint_count = prev_bkpt_count;
9013 }
9014
9015 update_global_location_list (UGLL_MAY_INSERT);
9016
9017 return 1;
9018 }
9019
9020 /* Set a breakpoint.
9021 ARG is a string describing breakpoint address,
9022 condition, and thread.
9023 FLAG specifies if a breakpoint is hardware on,
9024 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9025 and BP_TEMPFLAG. */
9026
9027 static void
9028 break_command_1 (const char *arg, int flag, int from_tty)
9029 {
9030 int tempflag = flag & BP_TEMPFLAG;
9031 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9032 ? bp_hardware_breakpoint
9033 : bp_breakpoint);
9034
9035 event_location_up location = string_to_event_location (&arg, current_language);
9036 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9037 (location.get (), false /* is_tracepoint */);
9038
9039 create_breakpoint (get_current_arch (),
9040 location.get (),
9041 NULL, 0, arg, false, 1 /* parse arg */,
9042 tempflag, type_wanted,
9043 0 /* Ignore count */,
9044 pending_break_support,
9045 ops,
9046 from_tty,
9047 1 /* enabled */,
9048 0 /* internal */,
9049 0);
9050 }
9051
9052 /* Helper function for break_command_1 and disassemble_command. */
9053
9054 void
9055 resolve_sal_pc (struct symtab_and_line *sal)
9056 {
9057 CORE_ADDR pc;
9058
9059 if (sal->pc == 0 && sal->symtab != NULL)
9060 {
9061 if (!find_line_pc (sal->symtab, sal->line, &pc))
9062 error (_("No line %d in file \"%s\"."),
9063 sal->line, symtab_to_filename_for_display (sal->symtab));
9064 sal->pc = pc;
9065
9066 /* If this SAL corresponds to a breakpoint inserted using a line
9067 number, then skip the function prologue if necessary. */
9068 if (sal->explicit_line)
9069 skip_prologue_sal (sal);
9070 }
9071
9072 if (sal->section == 0 && sal->symtab != NULL)
9073 {
9074 const struct blockvector *bv;
9075 const struct block *b;
9076 struct symbol *sym;
9077
9078 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9079 sal->symtab->compunit ());
9080 if (bv != NULL)
9081 {
9082 sym = block_linkage_function (b);
9083 if (sym != NULL)
9084 {
9085 fixup_symbol_section (sym, sal->symtab->compunit ()->objfile ());
9086 sal->section
9087 = sym->obj_section (sal->symtab->compunit ()->objfile ());
9088 }
9089 else
9090 {
9091 /* It really is worthwhile to have the section, so we'll
9092 just have to look harder. This case can be executed
9093 if we have line numbers but no functions (as can
9094 happen in assembly source). */
9095
9096 scoped_restore_current_pspace_and_thread restore_pspace_thread;
9097 switch_to_program_space_and_thread (sal->pspace);
9098
9099 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
9100 if (msym.minsym)
9101 sal->section = msym.obj_section ();
9102 }
9103 }
9104 }
9105 }
9106
9107 void
9108 break_command (const char *arg, int from_tty)
9109 {
9110 break_command_1 (arg, 0, from_tty);
9111 }
9112
9113 void
9114 tbreak_command (const char *arg, int from_tty)
9115 {
9116 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9117 }
9118
9119 static void
9120 hbreak_command (const char *arg, int from_tty)
9121 {
9122 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9123 }
9124
9125 static void
9126 thbreak_command (const char *arg, int from_tty)
9127 {
9128 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9129 }
9130
9131 /* The dynamic printf command is mostly like a regular breakpoint, but
9132 with a prewired command list consisting of a single output command,
9133 built from extra arguments supplied on the dprintf command
9134 line. */
9135
9136 static void
9137 dprintf_command (const char *arg, int from_tty)
9138 {
9139 event_location_up location = string_to_event_location (&arg, current_language);
9140
9141 /* If non-NULL, ARG should have been advanced past the location;
9142 the next character must be ','. */
9143 if (arg != NULL)
9144 {
9145 if (arg[0] != ',' || arg[1] == '\0')
9146 error (_("Format string required"));
9147 else
9148 {
9149 /* Skip the comma. */
9150 ++arg;
9151 }
9152 }
9153
9154 create_breakpoint (get_current_arch (),
9155 location.get (),
9156 NULL, 0, arg, false, 1 /* parse arg */,
9157 0, bp_dprintf,
9158 0 /* Ignore count */,
9159 pending_break_support,
9160 &dprintf_breakpoint_ops,
9161 from_tty,
9162 1 /* enabled */,
9163 0 /* internal */,
9164 0);
9165 }
9166
9167 static void
9168 agent_printf_command (const char *arg, int from_tty)
9169 {
9170 error (_("May only run agent-printf on the target"));
9171 }
9172
9173 /* Implement the "breakpoint_hit" breakpoint_ops method for
9174 ranged breakpoints. */
9175
9176 static int
9177 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9178 const address_space *aspace,
9179 CORE_ADDR bp_addr,
9180 const target_waitstatus &ws)
9181 {
9182 if (ws.kind () != TARGET_WAITKIND_STOPPED
9183 || ws.sig () != GDB_SIGNAL_TRAP)
9184 return 0;
9185
9186 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9187 bl->length, aspace, bp_addr);
9188 }
9189
9190 /* Implement the "resources_needed" breakpoint_ops method for
9191 ranged breakpoints. */
9192
9193 static int
9194 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9195 {
9196 return target_ranged_break_num_registers ();
9197 }
9198
9199 /* Implement the "print_it" breakpoint_ops method for
9200 ranged breakpoints. */
9201
9202 static enum print_stop_action
9203 print_it_ranged_breakpoint (bpstat *bs)
9204 {
9205 struct breakpoint *b = bs->breakpoint_at;
9206 struct bp_location *bl = b->loc;
9207 struct ui_out *uiout = current_uiout;
9208
9209 gdb_assert (b->type == bp_hardware_breakpoint);
9210
9211 /* Ranged breakpoints have only one location. */
9212 gdb_assert (bl && bl->next == NULL);
9213
9214 annotate_breakpoint (b->number);
9215
9216 maybe_print_thread_hit_breakpoint (uiout);
9217
9218 if (b->disposition == disp_del)
9219 uiout->text ("Temporary ranged breakpoint ");
9220 else
9221 uiout->text ("Ranged breakpoint ");
9222 if (uiout->is_mi_like_p ())
9223 {
9224 uiout->field_string ("reason",
9225 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9226 uiout->field_string ("disp", bpdisp_text (b->disposition));
9227 }
9228 uiout->field_signed ("bkptno", b->number);
9229 uiout->text (", ");
9230
9231 return PRINT_SRC_AND_LOC;
9232 }
9233
9234 /* Implement the "print_one" breakpoint_ops method for
9235 ranged breakpoints. */
9236
9237 static bool
9238 print_one_ranged_breakpoint (struct breakpoint *b,
9239 struct bp_location **last_loc)
9240 {
9241 struct bp_location *bl = b->loc;
9242 struct value_print_options opts;
9243 struct ui_out *uiout = current_uiout;
9244
9245 /* Ranged breakpoints have only one location. */
9246 gdb_assert (bl && bl->next == NULL);
9247
9248 get_user_print_options (&opts);
9249
9250 if (opts.addressprint)
9251 /* We don't print the address range here, it will be printed later
9252 by print_one_detail_ranged_breakpoint. */
9253 uiout->field_skip ("addr");
9254 annotate_field (5);
9255 print_breakpoint_location (b, bl);
9256 *last_loc = bl;
9257
9258 return true;
9259 }
9260
9261 /* Implement the "print_one_detail" breakpoint_ops method for
9262 ranged breakpoints. */
9263
9264 static void
9265 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9266 struct ui_out *uiout)
9267 {
9268 CORE_ADDR address_start, address_end;
9269 struct bp_location *bl = b->loc;
9270 string_file stb;
9271
9272 gdb_assert (bl);
9273
9274 address_start = bl->address;
9275 address_end = address_start + bl->length - 1;
9276
9277 uiout->text ("\taddress range: ");
9278 stb.printf ("[%s, %s]",
9279 print_core_address (bl->gdbarch, address_start),
9280 print_core_address (bl->gdbarch, address_end));
9281 uiout->field_stream ("addr", stb);
9282 uiout->text ("\n");
9283 }
9284
9285 /* Implement the "print_mention" breakpoint_ops method for
9286 ranged breakpoints. */
9287
9288 static void
9289 print_mention_ranged_breakpoint (struct breakpoint *b)
9290 {
9291 struct bp_location *bl = b->loc;
9292 struct ui_out *uiout = current_uiout;
9293
9294 gdb_assert (bl);
9295 gdb_assert (b->type == bp_hardware_breakpoint);
9296
9297 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9298 b->number, paddress (bl->gdbarch, bl->address),
9299 paddress (bl->gdbarch, bl->address + bl->length - 1));
9300 }
9301
9302 /* Implement the "print_recreate" breakpoint_ops method for
9303 ranged breakpoints. */
9304
9305 static void
9306 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9307 {
9308 gdb_printf (fp, "break-range %s, %s",
9309 event_location_to_string (b->location.get ()),
9310 event_location_to_string (b->location_range_end.get ()));
9311 print_recreate_thread (b, fp);
9312 }
9313
9314 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9315
9316 static struct breakpoint_ops ranged_breakpoint_ops;
9317
9318 /* Find the address where the end of the breakpoint range should be
9319 placed, given the SAL of the end of the range. This is so that if
9320 the user provides a line number, the end of the range is set to the
9321 last instruction of the given line. */
9322
9323 static CORE_ADDR
9324 find_breakpoint_range_end (struct symtab_and_line sal)
9325 {
9326 CORE_ADDR end;
9327
9328 /* If the user provided a PC value, use it. Otherwise,
9329 find the address of the end of the given location. */
9330 if (sal.explicit_pc)
9331 end = sal.pc;
9332 else
9333 {
9334 int ret;
9335 CORE_ADDR start;
9336
9337 ret = find_line_pc_range (sal, &start, &end);
9338 if (!ret)
9339 error (_("Could not find location of the end of the range."));
9340
9341 /* find_line_pc_range returns the start of the next line. */
9342 end--;
9343 }
9344
9345 return end;
9346 }
9347
9348 /* Implement the "break-range" CLI command. */
9349
9350 static void
9351 break_range_command (const char *arg, int from_tty)
9352 {
9353 const char *arg_start;
9354 struct linespec_result canonical_start, canonical_end;
9355 int bp_count, can_use_bp, length;
9356 CORE_ADDR end;
9357 struct breakpoint *b;
9358
9359 /* We don't support software ranged breakpoints. */
9360 if (target_ranged_break_num_registers () < 0)
9361 error (_("This target does not support hardware ranged breakpoints."));
9362
9363 bp_count = hw_breakpoint_used_count ();
9364 bp_count += target_ranged_break_num_registers ();
9365 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9366 bp_count, 0);
9367 if (can_use_bp < 0)
9368 error (_("Hardware breakpoints used exceeds limit."));
9369
9370 arg = skip_spaces (arg);
9371 if (arg == NULL || arg[0] == '\0')
9372 error(_("No address range specified."));
9373
9374 arg_start = arg;
9375 event_location_up start_location = string_to_event_location (&arg,
9376 current_language);
9377 parse_breakpoint_sals (start_location.get (), &canonical_start);
9378
9379 if (arg[0] != ',')
9380 error (_("Too few arguments."));
9381 else if (canonical_start.lsals.empty ())
9382 error (_("Could not find location of the beginning of the range."));
9383
9384 const linespec_sals &lsal_start = canonical_start.lsals[0];
9385
9386 if (canonical_start.lsals.size () > 1
9387 || lsal_start.sals.size () != 1)
9388 error (_("Cannot create a ranged breakpoint with multiple locations."));
9389
9390 const symtab_and_line &sal_start = lsal_start.sals[0];
9391 std::string addr_string_start (arg_start, arg - arg_start);
9392
9393 arg++; /* Skip the comma. */
9394 arg = skip_spaces (arg);
9395
9396 /* Parse the end location. */
9397
9398 arg_start = arg;
9399
9400 /* We call decode_line_full directly here instead of using
9401 parse_breakpoint_sals because we need to specify the start location's
9402 symtab and line as the default symtab and line for the end of the
9403 range. This makes it possible to have ranges like "foo.c:27, +14",
9404 where +14 means 14 lines from the start location. */
9405 event_location_up end_location = string_to_event_location (&arg,
9406 current_language);
9407 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
9408 sal_start.symtab, sal_start.line,
9409 &canonical_end, NULL, NULL);
9410
9411 if (canonical_end.lsals.empty ())
9412 error (_("Could not find location of the end of the range."));
9413
9414 const linespec_sals &lsal_end = canonical_end.lsals[0];
9415 if (canonical_end.lsals.size () > 1
9416 || lsal_end.sals.size () != 1)
9417 error (_("Cannot create a ranged breakpoint with multiple locations."));
9418
9419 const symtab_and_line &sal_end = lsal_end.sals[0];
9420
9421 end = find_breakpoint_range_end (sal_end);
9422 if (sal_start.pc > end)
9423 error (_("Invalid address range, end precedes start."));
9424
9425 length = end - sal_start.pc + 1;
9426 if (length < 0)
9427 /* Length overflowed. */
9428 error (_("Address range too large."));
9429 else if (length == 1)
9430 {
9431 /* This range is simple enough to be handled by
9432 the `hbreak' command. */
9433 hbreak_command (&addr_string_start[0], 1);
9434
9435 return;
9436 }
9437
9438 /* Now set up the breakpoint. */
9439 b = set_raw_breakpoint (get_current_arch (), sal_start,
9440 bp_hardware_breakpoint, &ranged_breakpoint_ops);
9441 set_breakpoint_count (breakpoint_count + 1);
9442 b->number = breakpoint_count;
9443 b->disposition = disp_donttouch;
9444 b->location = std::move (start_location);
9445 b->location_range_end = std::move (end_location);
9446 b->loc->length = length;
9447
9448 mention (b);
9449 gdb::observers::breakpoint_created.notify (b);
9450 update_global_location_list (UGLL_MAY_INSERT);
9451 }
9452
9453 /* Return non-zero if EXP is verified as constant. Returned zero
9454 means EXP is variable. Also the constant detection may fail for
9455 some constant expressions and in such case still falsely return
9456 zero. */
9457
9458 static bool
9459 watchpoint_exp_is_const (const struct expression *exp)
9460 {
9461 return exp->op->constant_p ();
9462 }
9463
9464 /* Implement the "re_set" method for watchpoints. */
9465
9466 void
9467 watchpoint::re_set ()
9468 {
9469 /* Watchpoint can be either on expression using entirely global
9470 variables, or it can be on local variables.
9471
9472 Watchpoints of the first kind are never auto-deleted, and even
9473 persist across program restarts. Since they can use variables
9474 from shared libraries, we need to reparse expression as libraries
9475 are loaded and unloaded.
9476
9477 Watchpoints on local variables can also change meaning as result
9478 of solib event. For example, if a watchpoint uses both a local
9479 and a global variables in expression, it's a local watchpoint,
9480 but unloading of a shared library will make the expression
9481 invalid. This is not a very common use case, but we still
9482 re-evaluate expression, to avoid surprises to the user.
9483
9484 Note that for local watchpoints, we re-evaluate it only if
9485 watchpoints frame id is still valid. If it's not, it means the
9486 watchpoint is out of scope and will be deleted soon. In fact,
9487 I'm not sure we'll ever be called in this case.
9488
9489 If a local watchpoint's frame id is still valid, then
9490 exp_valid_block is likewise valid, and we can safely use it.
9491
9492 Don't do anything about disabled watchpoints, since they will be
9493 reevaluated again when enabled. */
9494 update_watchpoint (this, 1 /* reparse */);
9495 }
9496
9497 /* Implement the "insert" method for hardware watchpoints. */
9498
9499 int
9500 watchpoint::insert_location (struct bp_location *bl)
9501 {
9502 int length = exact ? 1 : bl->length;
9503
9504 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9505 cond_exp.get ());
9506 }
9507
9508 /* Implement the "remove" method for hardware watchpoints. */
9509
9510 int
9511 watchpoint::remove_location (struct bp_location *bl,
9512 enum remove_bp_reason reason)
9513 {
9514 int length = exact ? 1 : bl->length;
9515
9516 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9517 cond_exp.get ());
9518 }
9519
9520 int
9521 watchpoint::breakpoint_hit (const struct bp_location *bl,
9522 const address_space *aspace, CORE_ADDR bp_addr,
9523 const target_waitstatus &ws)
9524 {
9525 struct breakpoint *b = bl->owner;
9526
9527 /* Continuable hardware watchpoints are treated as non-existent if the
9528 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9529 some data address). Otherwise gdb won't stop on a break instruction
9530 in the code (not from a breakpoint) when a hardware watchpoint has
9531 been defined. Also skip watchpoints which we know did not trigger
9532 (did not match the data address). */
9533 if (is_hardware_watchpoint (b)
9534 && watchpoint_triggered == watch_triggered_no)
9535 return 0;
9536
9537 return 1;
9538 }
9539
9540 void
9541 watchpoint::check_status (bpstat *bs)
9542 {
9543 gdb_assert (is_watchpoint (bs->breakpoint_at));
9544
9545 bpstat_check_watchpoint (bs);
9546 }
9547
9548 /* Implement the "resources_needed" method for hardware
9549 watchpoints. */
9550
9551 int
9552 watchpoint::resources_needed (const struct bp_location *bl)
9553 {
9554 int length = exact? 1 : bl->length;
9555
9556 return target_region_ok_for_hw_watchpoint (bl->address, length);
9557 }
9558
9559 /* Implement the "works_in_software_mode" method for hardware
9560 watchpoints. */
9561
9562 int
9563 watchpoint::works_in_software_mode () const
9564 {
9565 /* Read and access watchpoints only work with hardware support. */
9566 return type == bp_watchpoint || type == bp_hardware_watchpoint;
9567 }
9568
9569 enum print_stop_action
9570 watchpoint::print_it (bpstat *bs)
9571 {
9572 struct breakpoint *b;
9573 enum print_stop_action result;
9574 struct ui_out *uiout = current_uiout;
9575
9576 gdb_assert (bs->bp_location_at != NULL);
9577
9578 b = bs->breakpoint_at;
9579
9580 annotate_watchpoint (b->number);
9581 maybe_print_thread_hit_breakpoint (uiout);
9582
9583 string_file stb;
9584
9585 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9586 switch (b->type)
9587 {
9588 case bp_watchpoint:
9589 case bp_hardware_watchpoint:
9590 if (uiout->is_mi_like_p ())
9591 uiout->field_string
9592 ("reason", async_reason_lookup (EXEC_ASYNC_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 watchpoint_value_print (val.get (), &stb);
9600 uiout->field_stream ("new", stb);
9601 uiout->text ("\n");
9602 /* More than one watchpoint may have been triggered. */
9603 result = PRINT_UNKNOWN;
9604 break;
9605
9606 case bp_read_watchpoint:
9607 if (uiout->is_mi_like_p ())
9608 uiout->field_string
9609 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9610 mention (b);
9611 tuple_emitter.emplace (uiout, "value");
9612 uiout->text ("\nValue = ");
9613 watchpoint_value_print (val.get (), &stb);
9614 uiout->field_stream ("value", stb);
9615 uiout->text ("\n");
9616 result = PRINT_UNKNOWN;
9617 break;
9618
9619 case bp_access_watchpoint:
9620 if (bs->old_val != NULL)
9621 {
9622 if (uiout->is_mi_like_p ())
9623 uiout->field_string
9624 ("reason",
9625 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9626 mention (b);
9627 tuple_emitter.emplace (uiout, "value");
9628 uiout->text ("\nOld value = ");
9629 watchpoint_value_print (bs->old_val.get (), &stb);
9630 uiout->field_stream ("old", stb);
9631 uiout->text ("\nNew value = ");
9632 }
9633 else
9634 {
9635 mention (b);
9636 if (uiout->is_mi_like_p ())
9637 uiout->field_string
9638 ("reason",
9639 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9640 tuple_emitter.emplace (uiout, "value");
9641 uiout->text ("\nValue = ");
9642 }
9643 watchpoint_value_print (val.get (), &stb);
9644 uiout->field_stream ("new", stb);
9645 uiout->text ("\n");
9646 result = PRINT_UNKNOWN;
9647 break;
9648 default:
9649 result = PRINT_UNKNOWN;
9650 }
9651
9652 return result;
9653 }
9654
9655 /* Implement the "print_mention" method for hardware watchpoints. */
9656
9657 void
9658 watchpoint::print_mention ()
9659 {
9660 struct ui_out *uiout = current_uiout;
9661 const char *tuple_name;
9662
9663 switch (type)
9664 {
9665 case bp_watchpoint:
9666 uiout->text ("Watchpoint ");
9667 tuple_name = "wpt";
9668 break;
9669 case bp_hardware_watchpoint:
9670 uiout->text ("Hardware watchpoint ");
9671 tuple_name = "wpt";
9672 break;
9673 case bp_read_watchpoint:
9674 uiout->text ("Hardware read watchpoint ");
9675 tuple_name = "hw-rwpt";
9676 break;
9677 case bp_access_watchpoint:
9678 uiout->text ("Hardware access (read/write) watchpoint ");
9679 tuple_name = "hw-awpt";
9680 break;
9681 default:
9682 internal_error (__FILE__, __LINE__,
9683 _("Invalid hardware watchpoint type."));
9684 }
9685
9686 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9687 uiout->field_signed ("number", number);
9688 uiout->text (": ");
9689 uiout->field_string ("exp", exp_string.get ());
9690 }
9691
9692 /* Implement the "print_recreate" method for watchpoints. */
9693
9694 void
9695 watchpoint::print_recreate (struct ui_file *fp)
9696 {
9697 switch (type)
9698 {
9699 case bp_watchpoint:
9700 case bp_hardware_watchpoint:
9701 gdb_printf (fp, "watch");
9702 break;
9703 case bp_read_watchpoint:
9704 gdb_printf (fp, "rwatch");
9705 break;
9706 case bp_access_watchpoint:
9707 gdb_printf (fp, "awatch");
9708 break;
9709 default:
9710 internal_error (__FILE__, __LINE__,
9711 _("Invalid watchpoint type."));
9712 }
9713
9714 gdb_printf (fp, " %s", exp_string.get ());
9715 print_recreate_thread (this, fp);
9716 }
9717
9718 /* Implement the "explains_signal" method for watchpoints. */
9719
9720 int
9721 watchpoint::explains_signal (enum gdb_signal sig)
9722 {
9723 /* A software watchpoint cannot cause a signal other than
9724 GDB_SIGNAL_TRAP. */
9725 if (type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
9726 return 0;
9727
9728 return 1;
9729 }
9730
9731 struct masked_watchpoint : public watchpoint
9732 {
9733 int insert_location (struct bp_location *) override;
9734 int remove_location (struct bp_location *,
9735 enum remove_bp_reason reason) override;
9736 int resources_needed (const struct bp_location *) override;
9737 int works_in_software_mode () const override;
9738 enum print_stop_action print_it (struct bpstat *bs) override;
9739 void print_one_detail (struct ui_out *) const override;
9740 void print_mention () override;
9741 void print_recreate (struct ui_file *fp) override;
9742 };
9743
9744 /* Implement the "insert" method for masked hardware watchpoints. */
9745
9746 int
9747 masked_watchpoint::insert_location (struct bp_location *bl)
9748 {
9749 return target_insert_mask_watchpoint (bl->address, hw_wp_mask,
9750 bl->watchpoint_type);
9751 }
9752
9753 /* Implement the "remove" method for masked hardware watchpoints. */
9754
9755 int
9756 masked_watchpoint::remove_location (struct bp_location *bl,
9757 enum remove_bp_reason reason)
9758 {
9759 return target_remove_mask_watchpoint (bl->address, hw_wp_mask,
9760 bl->watchpoint_type);
9761 }
9762
9763 /* Implement the "resources_needed" method for masked hardware
9764 watchpoints. */
9765
9766 int
9767 masked_watchpoint::resources_needed (const struct bp_location *bl)
9768 {
9769 return target_masked_watch_num_registers (bl->address, hw_wp_mask);
9770 }
9771
9772 /* Implement the "works_in_software_mode" method for masked hardware
9773 watchpoints. */
9774
9775 int
9776 masked_watchpoint::works_in_software_mode () const
9777 {
9778 return 0;
9779 }
9780
9781 /* Implement the "print_it" method for masked hardware
9782 watchpoints. */
9783
9784 enum print_stop_action
9785 masked_watchpoint::print_it (bpstat *bs)
9786 {
9787 struct breakpoint *b = bs->breakpoint_at;
9788 struct ui_out *uiout = current_uiout;
9789
9790 /* Masked watchpoints have only one location. */
9791 gdb_assert (b->loc && b->loc->next == NULL);
9792
9793 annotate_watchpoint (b->number);
9794 maybe_print_thread_hit_breakpoint (uiout);
9795
9796 switch (b->type)
9797 {
9798 case bp_hardware_watchpoint:
9799 if (uiout->is_mi_like_p ())
9800 uiout->field_string
9801 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
9802 break;
9803
9804 case bp_read_watchpoint:
9805 if (uiout->is_mi_like_p ())
9806 uiout->field_string
9807 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
9808 break;
9809
9810 case bp_access_watchpoint:
9811 if (uiout->is_mi_like_p ())
9812 uiout->field_string
9813 ("reason",
9814 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
9815 break;
9816 default:
9817 internal_error (__FILE__, __LINE__,
9818 _("Invalid hardware watchpoint type."));
9819 }
9820
9821 mention (b);
9822 uiout->text (_("\n\
9823 Check the underlying instruction at PC for the memory\n\
9824 address and value which triggered this watchpoint.\n"));
9825 uiout->text ("\n");
9826
9827 /* More than one watchpoint may have been triggered. */
9828 return PRINT_UNKNOWN;
9829 }
9830
9831 /* Implement the "print_one_detail" method for masked hardware
9832 watchpoints. */
9833
9834 void
9835 masked_watchpoint::print_one_detail (struct ui_out *uiout) const
9836 {
9837 /* Masked watchpoints have only one location. */
9838 gdb_assert (loc && loc->next == NULL);
9839
9840 uiout->text ("\tmask ");
9841 uiout->field_core_addr ("mask", loc->gdbarch, hw_wp_mask);
9842 uiout->text ("\n");
9843 }
9844
9845 /* Implement the "print_mention" method for masked hardware
9846 watchpoints. */
9847
9848 void
9849 masked_watchpoint::print_mention ()
9850 {
9851 struct ui_out *uiout = current_uiout;
9852 const char *tuple_name;
9853
9854 switch (type)
9855 {
9856 case bp_hardware_watchpoint:
9857 uiout->text ("Masked hardware watchpoint ");
9858 tuple_name = "wpt";
9859 break;
9860 case bp_read_watchpoint:
9861 uiout->text ("Masked hardware read watchpoint ");
9862 tuple_name = "hw-rwpt";
9863 break;
9864 case bp_access_watchpoint:
9865 uiout->text ("Masked hardware access (read/write) watchpoint ");
9866 tuple_name = "hw-awpt";
9867 break;
9868 default:
9869 internal_error (__FILE__, __LINE__,
9870 _("Invalid hardware watchpoint type."));
9871 }
9872
9873 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
9874 uiout->field_signed ("number", number);
9875 uiout->text (": ");
9876 uiout->field_string ("exp", exp_string.get ());
9877 }
9878
9879 /* Implement the "print_recreate" method for masked hardware
9880 watchpoints. */
9881
9882 void
9883 masked_watchpoint::print_recreate (struct ui_file *fp)
9884 {
9885 switch (type)
9886 {
9887 case bp_hardware_watchpoint:
9888 gdb_printf (fp, "watch");
9889 break;
9890 case bp_read_watchpoint:
9891 gdb_printf (fp, "rwatch");
9892 break;
9893 case bp_access_watchpoint:
9894 gdb_printf (fp, "awatch");
9895 break;
9896 default:
9897 internal_error (__FILE__, __LINE__,
9898 _("Invalid hardware watchpoint type."));
9899 }
9900
9901 gdb_printf (fp, " %s mask 0x%s", exp_string.get (),
9902 phex (hw_wp_mask, sizeof (CORE_ADDR)));
9903 print_recreate_thread (this, fp);
9904 }
9905
9906 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
9907
9908 static bool
9909 is_masked_watchpoint (const struct breakpoint *b)
9910 {
9911 return dynamic_cast<const masked_watchpoint *> (b) != nullptr;
9912 }
9913
9914 /* accessflag: hw_write: watch write,
9915 hw_read: watch read,
9916 hw_access: watch access (read or write) */
9917 static void
9918 watch_command_1 (const char *arg, int accessflag, int from_tty,
9919 bool just_location, bool internal)
9920 {
9921 struct breakpoint *scope_breakpoint = NULL;
9922 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
9923 struct value *result;
9924 int saved_bitpos = 0, saved_bitsize = 0;
9925 const char *exp_start = NULL;
9926 const char *exp_end = NULL;
9927 const char *tok, *end_tok;
9928 int toklen = -1;
9929 const char *cond_start = NULL;
9930 const char *cond_end = NULL;
9931 enum bptype bp_type;
9932 int thread = -1;
9933 /* Flag to indicate whether we are going to use masks for
9934 the hardware watchpoint. */
9935 bool use_mask = false;
9936 CORE_ADDR mask = 0;
9937 int task = 0;
9938
9939 /* Make sure that we actually have parameters to parse. */
9940 if (arg != NULL && arg[0] != '\0')
9941 {
9942 const char *value_start;
9943
9944 exp_end = arg + strlen (arg);
9945
9946 /* Look for "parameter value" pairs at the end
9947 of the arguments string. */
9948 for (tok = exp_end - 1; tok > arg; tok--)
9949 {
9950 /* Skip whitespace at the end of the argument list. */
9951 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9952 tok--;
9953
9954 /* Find the beginning of the last token.
9955 This is the value of the parameter. */
9956 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9957 tok--;
9958 value_start = tok + 1;
9959
9960 /* Skip whitespace. */
9961 while (tok > arg && (*tok == ' ' || *tok == '\t'))
9962 tok--;
9963
9964 end_tok = tok;
9965
9966 /* Find the beginning of the second to last token.
9967 This is the parameter itself. */
9968 while (tok > arg && (*tok != ' ' && *tok != '\t'))
9969 tok--;
9970 tok++;
9971 toklen = end_tok - tok + 1;
9972
9973 if (toklen == 6 && startswith (tok, "thread"))
9974 {
9975 struct thread_info *thr;
9976 /* At this point we've found a "thread" token, which means
9977 the user is trying to set a watchpoint that triggers
9978 only in a specific thread. */
9979 const char *endp;
9980
9981 if (thread != -1)
9982 error(_("You can specify only one thread."));
9983
9984 /* Extract the thread ID from the next token. */
9985 thr = parse_thread_id (value_start, &endp);
9986
9987 /* Check if the user provided a valid thread ID. */
9988 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
9989 invalid_thread_id_error (value_start);
9990
9991 thread = thr->global_num;
9992 }
9993 else if (toklen == 4 && startswith (tok, "task"))
9994 {
9995 char *tmp;
9996
9997 task = strtol (value_start, &tmp, 0);
9998 if (tmp == value_start)
9999 error (_("Junk after task keyword."));
10000 if (!valid_task_id (task))
10001 error (_("Unknown task %d."), task);
10002 }
10003 else if (toklen == 4 && startswith (tok, "mask"))
10004 {
10005 /* We've found a "mask" token, which means the user wants to
10006 create a hardware watchpoint that is going to have the mask
10007 facility. */
10008 struct value *mask_value, *mark;
10009
10010 if (use_mask)
10011 error(_("You can specify only one mask."));
10012
10013 use_mask = just_location = true;
10014
10015 mark = value_mark ();
10016 mask_value = parse_to_comma_and_eval (&value_start);
10017 mask = value_as_address (mask_value);
10018 value_free_to_mark (mark);
10019 }
10020 else
10021 /* We didn't recognize what we found. We should stop here. */
10022 break;
10023
10024 /* Truncate the string and get rid of the "parameter value" pair before
10025 the arguments string is parsed by the parse_exp_1 function. */
10026 exp_end = tok;
10027 }
10028 }
10029 else
10030 exp_end = arg;
10031
10032 /* Parse the rest of the arguments. From here on out, everything
10033 is in terms of a newly allocated string instead of the original
10034 ARG. */
10035 std::string expression (arg, exp_end - arg);
10036 exp_start = arg = expression.c_str ();
10037 innermost_block_tracker tracker;
10038 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
10039 exp_end = arg;
10040 /* Remove trailing whitespace from the expression before saving it.
10041 This makes the eventual display of the expression string a bit
10042 prettier. */
10043 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10044 --exp_end;
10045
10046 /* Checking if the expression is not constant. */
10047 if (watchpoint_exp_is_const (exp.get ()))
10048 {
10049 int len;
10050
10051 len = exp_end - exp_start;
10052 while (len > 0 && isspace (exp_start[len - 1]))
10053 len--;
10054 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10055 }
10056
10057 exp_valid_block = tracker.block ();
10058 struct value *mark = value_mark ();
10059 struct value *val_as_value = nullptr;
10060 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10061 just_location);
10062
10063 if (val_as_value != NULL && just_location)
10064 {
10065 saved_bitpos = value_bitpos (val_as_value);
10066 saved_bitsize = value_bitsize (val_as_value);
10067 }
10068
10069 value_ref_ptr val;
10070 if (just_location)
10071 {
10072 int ret;
10073
10074 exp_valid_block = NULL;
10075 val = release_value (value_addr (result));
10076 value_free_to_mark (mark);
10077
10078 if (use_mask)
10079 {
10080 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
10081 mask);
10082 if (ret == -1)
10083 error (_("This target does not support masked watchpoints."));
10084 else if (ret == -2)
10085 error (_("Invalid mask or memory region."));
10086 }
10087 }
10088 else if (val_as_value != NULL)
10089 val = release_value (val_as_value);
10090
10091 tok = skip_spaces (arg);
10092 end_tok = skip_to_space (tok);
10093
10094 toklen = end_tok - tok;
10095 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10096 {
10097 tok = cond_start = end_tok + 1;
10098 innermost_block_tracker if_tracker;
10099 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
10100
10101 /* The watchpoint expression may not be local, but the condition
10102 may still be. E.g.: `watch global if local > 0'. */
10103 cond_exp_valid_block = if_tracker.block ();
10104
10105 cond_end = tok;
10106 }
10107 if (*tok)
10108 error (_("Junk at end of command."));
10109
10110 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10111
10112 /* Save this because create_internal_breakpoint below invalidates
10113 'wp_frame'. */
10114 frame_id watchpoint_frame = get_frame_id (wp_frame);
10115
10116 /* If the expression is "local", then set up a "watchpoint scope"
10117 breakpoint at the point where we've left the scope of the watchpoint
10118 expression. Create the scope breakpoint before the watchpoint, so
10119 that we will encounter it first in bpstat_stop_status. */
10120 if (exp_valid_block != NULL && wp_frame != NULL)
10121 {
10122 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10123
10124 if (frame_id_p (caller_frame_id))
10125 {
10126 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10127 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10128
10129 scope_breakpoint
10130 = create_internal_breakpoint (caller_arch, caller_pc,
10131 bp_watchpoint_scope,
10132 &momentary_breakpoint_ops);
10133
10134 /* create_internal_breakpoint could invalidate WP_FRAME. */
10135 wp_frame = NULL;
10136
10137 scope_breakpoint->enable_state = bp_enabled;
10138
10139 /* Automatically delete the breakpoint when it hits. */
10140 scope_breakpoint->disposition = disp_del;
10141
10142 /* Only break in the proper frame (help with recursion). */
10143 scope_breakpoint->frame_id = caller_frame_id;
10144
10145 /* Set the address at which we will stop. */
10146 scope_breakpoint->loc->gdbarch = caller_arch;
10147 scope_breakpoint->loc->requested_address = caller_pc;
10148 scope_breakpoint->loc->address
10149 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10150 scope_breakpoint->loc->requested_address,
10151 scope_breakpoint->type);
10152 }
10153 }
10154
10155 /* Now set up the breakpoint. We create all watchpoints as hardware
10156 watchpoints here even if hardware watchpoints are turned off, a call
10157 to update_watchpoint later in this function will cause the type to
10158 drop back to bp_watchpoint (software watchpoint) if required. */
10159
10160 if (accessflag == hw_read)
10161 bp_type = bp_read_watchpoint;
10162 else if (accessflag == hw_access)
10163 bp_type = bp_access_watchpoint;
10164 else
10165 bp_type = bp_hardware_watchpoint;
10166
10167 std::unique_ptr<watchpoint> w;
10168 if (use_mask)
10169 w.reset (new masked_watchpoint ());
10170 else
10171 w.reset (new watchpoint ());
10172 init_raw_breakpoint_without_location (w.get (), nullptr, bp_type,
10173 &vtable_breakpoint_ops);
10174
10175 w->thread = thread;
10176 w->task = task;
10177 w->disposition = disp_donttouch;
10178 w->pspace = current_program_space;
10179 w->exp = std::move (exp);
10180 w->exp_valid_block = exp_valid_block;
10181 w->cond_exp_valid_block = cond_exp_valid_block;
10182 if (just_location)
10183 {
10184 struct type *t = value_type (val.get ());
10185 CORE_ADDR addr = value_as_address (val.get ());
10186
10187 w->exp_string_reparse
10188 = current_language->watch_location_expression (t, addr);
10189
10190 w->exp_string = xstrprintf ("-location %.*s",
10191 (int) (exp_end - exp_start), exp_start);
10192 }
10193 else
10194 w->exp_string.reset (savestring (exp_start, exp_end - exp_start));
10195
10196 if (use_mask)
10197 {
10198 w->hw_wp_mask = mask;
10199 }
10200 else
10201 {
10202 w->val = val;
10203 w->val_bitpos = saved_bitpos;
10204 w->val_bitsize = saved_bitsize;
10205 w->val_valid = true;
10206 }
10207
10208 if (cond_start)
10209 w->cond_string.reset (savestring (cond_start, cond_end - cond_start));
10210 else
10211 w->cond_string = 0;
10212
10213 if (frame_id_p (watchpoint_frame))
10214 {
10215 w->watchpoint_frame = watchpoint_frame;
10216 w->watchpoint_thread = inferior_ptid;
10217 }
10218 else
10219 {
10220 w->watchpoint_frame = null_frame_id;
10221 w->watchpoint_thread = null_ptid;
10222 }
10223
10224 if (scope_breakpoint != NULL)
10225 {
10226 /* The scope breakpoint is related to the watchpoint. We will
10227 need to act on them together. */
10228 w->related_breakpoint = scope_breakpoint;
10229 scope_breakpoint->related_breakpoint = w.get ();
10230 }
10231
10232 if (!just_location)
10233 value_free_to_mark (mark);
10234
10235 /* Finally update the new watchpoint. This creates the locations
10236 that should be inserted. */
10237 update_watchpoint (w.get (), 1);
10238
10239 install_breakpoint (internal, std::move (w), 1);
10240 }
10241
10242 /* Return count of debug registers needed to watch the given expression.
10243 If the watchpoint cannot be handled in hardware return zero. */
10244
10245 static int
10246 can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
10247 {
10248 int found_memory_cnt = 0;
10249
10250 /* Did the user specifically forbid us to use hardware watchpoints? */
10251 if (!can_use_hw_watchpoints)
10252 return 0;
10253
10254 gdb_assert (!vals.empty ());
10255 struct value *head = vals[0].get ();
10256
10257 /* Make sure that the value of the expression depends only upon
10258 memory contents, and values computed from them within GDB. If we
10259 find any register references or function calls, we can't use a
10260 hardware watchpoint.
10261
10262 The idea here is that evaluating an expression generates a series
10263 of values, one holding the value of every subexpression. (The
10264 expression a*b+c has five subexpressions: a, b, a*b, c, and
10265 a*b+c.) GDB's values hold almost enough information to establish
10266 the criteria given above --- they identify memory lvalues,
10267 register lvalues, computed values, etcetera. So we can evaluate
10268 the expression, and then scan the chain of values that leaves
10269 behind to decide whether we can detect any possible change to the
10270 expression's final value using only hardware watchpoints.
10271
10272 However, I don't think that the values returned by inferior
10273 function calls are special in any way. So this function may not
10274 notice that an expression involving an inferior function call
10275 can't be watched with hardware watchpoints. FIXME. */
10276 for (const value_ref_ptr &iter : vals)
10277 {
10278 struct value *v = iter.get ();
10279
10280 if (VALUE_LVAL (v) == lval_memory)
10281 {
10282 if (v != head && value_lazy (v))
10283 /* A lazy memory lvalue in the chain is one that GDB never
10284 needed to fetch; we either just used its address (e.g.,
10285 `a' in `a.b') or we never needed it at all (e.g., `a'
10286 in `a,b'). This doesn't apply to HEAD; if that is
10287 lazy then it was not readable, but watch it anyway. */
10288 ;
10289 else
10290 {
10291 /* Ahh, memory we actually used! Check if we can cover
10292 it with hardware watchpoints. */
10293 struct type *vtype = check_typedef (value_type (v));
10294
10295 /* We only watch structs and arrays if user asked for it
10296 explicitly, never if they just happen to appear in a
10297 middle of some value chain. */
10298 if (v == head
10299 || (vtype->code () != TYPE_CODE_STRUCT
10300 && vtype->code () != TYPE_CODE_ARRAY))
10301 {
10302 CORE_ADDR vaddr = value_address (v);
10303 int len;
10304 int num_regs;
10305
10306 len = (target_exact_watchpoints
10307 && is_scalar_type_recursive (vtype))?
10308 1 : TYPE_LENGTH (value_type (v));
10309
10310 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10311 if (!num_regs)
10312 return 0;
10313 else
10314 found_memory_cnt += num_regs;
10315 }
10316 }
10317 }
10318 else if (VALUE_LVAL (v) != not_lval
10319 && deprecated_value_modifiable (v) == 0)
10320 return 0; /* These are values from the history (e.g., $1). */
10321 else if (VALUE_LVAL (v) == lval_register)
10322 return 0; /* Cannot watch a register with a HW watchpoint. */
10323 }
10324
10325 /* The expression itself looks suitable for using a hardware
10326 watchpoint, but give the target machine a chance to reject it. */
10327 return found_memory_cnt;
10328 }
10329
10330 void
10331 watch_command_wrapper (const char *arg, int from_tty, bool internal)
10332 {
10333 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10334 }
10335
10336 /* Options for the watch, awatch, and rwatch commands. */
10337
10338 struct watch_options
10339 {
10340 /* For -location. */
10341 bool location = false;
10342 };
10343
10344 /* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10345
10346 Historically GDB always accepted both '-location' and '-l' flags for
10347 these commands (both flags being synonyms). When converting to the
10348 newer option scheme only '-location' is added here. That's fine (for
10349 backward compatibility) as any non-ambiguous prefix of a flag will be
10350 accepted, so '-l', '-loc', are now all accepted.
10351
10352 What this means is that, if in the future, we add any new flag here
10353 that starts with '-l' then this will break backward compatibility, so
10354 please, don't do that! */
10355
10356 static const gdb::option::option_def watch_option_defs[] = {
10357 gdb::option::flag_option_def<watch_options> {
10358 "location",
10359 [] (watch_options *opt) { return &opt->location; },
10360 N_("\
10361 This evaluates EXPRESSION and watches the memory to which is refers.\n\
10362 -l can be used as a short form of -location."),
10363 },
10364 };
10365
10366 /* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10367 commands. */
10368
10369 static gdb::option::option_def_group
10370 make_watch_options_def_group (watch_options *opts)
10371 {
10372 return {{watch_option_defs}, opts};
10373 }
10374
10375 /* A helper function that looks for the "-location" argument and then
10376 calls watch_command_1. */
10377
10378 static void
10379 watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
10380 {
10381 watch_options opts;
10382 auto grp = make_watch_options_def_group (&opts);
10383 gdb::option::process_options
10384 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10385 if (arg != nullptr && *arg == '\0')
10386 arg = nullptr;
10387
10388 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10389 }
10390
10391 /* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10392 static void
10393 watch_command_completer (struct cmd_list_element *ignore,
10394 completion_tracker &tracker,
10395 const char *text, const char * /*word*/)
10396 {
10397 const auto group = make_watch_options_def_group (nullptr);
10398 if (gdb::option::complete_options
10399 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10400 return;
10401
10402 const char *word = advance_to_expression_complete_word_point (tracker, text);
10403 expression_completer (ignore, tracker, text, word);
10404 }
10405
10406 static void
10407 watch_command (const char *arg, int from_tty)
10408 {
10409 watch_maybe_just_location (arg, hw_write, from_tty);
10410 }
10411
10412 void
10413 rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
10414 {
10415 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10416 }
10417
10418 static void
10419 rwatch_command (const char *arg, int from_tty)
10420 {
10421 watch_maybe_just_location (arg, hw_read, from_tty);
10422 }
10423
10424 void
10425 awatch_command_wrapper (const char *arg, int from_tty, bool internal)
10426 {
10427 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10428 }
10429
10430 static void
10431 awatch_command (const char *arg, int from_tty)
10432 {
10433 watch_maybe_just_location (arg, hw_access, from_tty);
10434 }
10435 \f
10436
10437 /* Data for the FSM that manages the until(location)/advance commands
10438 in infcmd.c. Here because it uses the mechanisms of
10439 breakpoints. */
10440
10441 struct until_break_fsm : public thread_fsm
10442 {
10443 /* The thread that was current when the command was executed. */
10444 int thread;
10445
10446 /* The breakpoint set at the return address in the caller frame,
10447 plus breakpoints at all the destination locations. */
10448 std::vector<breakpoint_up> breakpoints;
10449
10450 until_break_fsm (struct interp *cmd_interp, int thread,
10451 std::vector<breakpoint_up> &&breakpoints)
10452 : thread_fsm (cmd_interp),
10453 thread (thread),
10454 breakpoints (std::move (breakpoints))
10455 {
10456 }
10457
10458 void clean_up (struct thread_info *thread) override;
10459 bool should_stop (struct thread_info *thread) override;
10460 enum async_reply_reason do_async_reply_reason () override;
10461 };
10462
10463 /* Implementation of the 'should_stop' FSM method for the
10464 until(location)/advance commands. */
10465
10466 bool
10467 until_break_fsm::should_stop (struct thread_info *tp)
10468 {
10469 for (const breakpoint_up &bp : breakpoints)
10470 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
10471 bp.get ()) != NULL)
10472 {
10473 set_finished ();
10474 break;
10475 }
10476
10477 return true;
10478 }
10479
10480 /* Implementation of the 'clean_up' FSM method for the
10481 until(location)/advance commands. */
10482
10483 void
10484 until_break_fsm::clean_up (struct thread_info *)
10485 {
10486 /* Clean up our temporary breakpoints. */
10487 breakpoints.clear ();
10488 delete_longjmp_breakpoint (thread);
10489 }
10490
10491 /* Implementation of the 'async_reply_reason' FSM method for the
10492 until(location)/advance commands. */
10493
10494 enum async_reply_reason
10495 until_break_fsm::do_async_reply_reason ()
10496 {
10497 return EXEC_ASYNC_LOCATION_REACHED;
10498 }
10499
10500 void
10501 until_break_command (const char *arg, int from_tty, int anywhere)
10502 {
10503 struct frame_info *frame;
10504 struct gdbarch *frame_gdbarch;
10505 struct frame_id stack_frame_id;
10506 struct frame_id caller_frame_id;
10507 int thread;
10508 struct thread_info *tp;
10509
10510 clear_proceed_status (0);
10511
10512 /* Set a breakpoint where the user wants it and at return from
10513 this function. */
10514
10515 event_location_up location = string_to_event_location (&arg, current_language);
10516
10517 std::vector<symtab_and_line> sals
10518 = (last_displayed_sal_is_valid ()
10519 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10520 get_last_displayed_symtab (),
10521 get_last_displayed_line ())
10522 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
10523 NULL, NULL, 0));
10524
10525 if (sals.empty ())
10526 error (_("Couldn't get information on specified line."));
10527
10528 if (*arg)
10529 error (_("Junk at end of arguments."));
10530
10531 tp = inferior_thread ();
10532 thread = tp->global_num;
10533
10534 /* Note linespec handling above invalidates the frame chain.
10535 Installing a breakpoint also invalidates the frame chain (as it
10536 may need to switch threads), so do any frame handling before
10537 that. */
10538
10539 frame = get_selected_frame (NULL);
10540 frame_gdbarch = get_frame_arch (frame);
10541 stack_frame_id = get_stack_frame_id (frame);
10542 caller_frame_id = frame_unwind_caller_id (frame);
10543
10544 /* Keep within the current frame, or in frames called by the current
10545 one. */
10546
10547 std::vector<breakpoint_up> breakpoints;
10548
10549 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
10550
10551 if (frame_id_p (caller_frame_id))
10552 {
10553 struct symtab_and_line sal2;
10554 struct gdbarch *caller_gdbarch;
10555
10556 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10557 sal2.pc = frame_unwind_caller_pc (frame);
10558 caller_gdbarch = frame_unwind_caller_arch (frame);
10559
10560 breakpoint_up caller_breakpoint
10561 = set_momentary_breakpoint (caller_gdbarch, sal2,
10562 caller_frame_id, bp_until);
10563 breakpoints.emplace_back (std::move (caller_breakpoint));
10564
10565 set_longjmp_breakpoint (tp, caller_frame_id);
10566 lj_deleter.emplace (thread);
10567 }
10568
10569 /* set_momentary_breakpoint could invalidate FRAME. */
10570 frame = NULL;
10571
10572 /* If the user told us to continue until a specified location, we
10573 don't specify a frame at which we need to stop. Otherwise,
10574 specify the selected frame, because we want to stop only at the
10575 very same frame. */
10576 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
10577
10578 for (symtab_and_line &sal : sals)
10579 {
10580 resolve_sal_pc (&sal);
10581
10582 breakpoint_up location_breakpoint
10583 = set_momentary_breakpoint (frame_gdbarch, sal,
10584 stop_frame_id, bp_until);
10585 breakpoints.emplace_back (std::move (location_breakpoint));
10586 }
10587
10588 tp->set_thread_fsm
10589 (std::unique_ptr<thread_fsm>
10590 (new until_break_fsm (command_interp (), tp->global_num,
10591 std::move (breakpoints))));
10592
10593 if (lj_deleter)
10594 lj_deleter->release ();
10595
10596 proceed (-1, GDB_SIGNAL_DEFAULT);
10597 }
10598
10599 void
10600 init_ada_exception_breakpoint (struct breakpoint *b,
10601 struct gdbarch *gdbarch,
10602 struct symtab_and_line sal,
10603 const char *addr_string,
10604 const struct breakpoint_ops *ops,
10605 int tempflag,
10606 int enabled,
10607 int from_tty)
10608 {
10609 if (from_tty)
10610 {
10611 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
10612 if (!loc_gdbarch)
10613 loc_gdbarch = gdbarch;
10614
10615 describe_other_breakpoints (loc_gdbarch,
10616 sal.pspace, sal.pc, sal.section, -1);
10617 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
10618 version for exception catchpoints, because two catchpoints
10619 used for different exception names will use the same address.
10620 In this case, a "breakpoint ... also set at..." warning is
10621 unproductive. Besides, the warning phrasing is also a bit
10622 inappropriate, we should use the word catchpoint, and tell
10623 the user what type of catchpoint it is. The above is good
10624 enough for now, though. */
10625 }
10626
10627 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
10628
10629 b->enable_state = enabled ? bp_enabled : bp_disabled;
10630 b->disposition = tempflag ? disp_del : disp_donttouch;
10631 b->location = string_to_event_location (&addr_string,
10632 language_def (language_ada));
10633 b->language = language_ada;
10634 }
10635
10636 \f
10637
10638 /* Compare two breakpoints and return a strcmp-like result. */
10639
10640 static int
10641 compare_breakpoints (const breakpoint *a, const breakpoint *b)
10642 {
10643 uintptr_t ua = (uintptr_t) a;
10644 uintptr_t ub = (uintptr_t) b;
10645
10646 if (a->number < b->number)
10647 return -1;
10648 else if (a->number > b->number)
10649 return 1;
10650
10651 /* Now sort by address, in case we see, e..g, two breakpoints with
10652 the number 0. */
10653 if (ua < ub)
10654 return -1;
10655 return ua > ub ? 1 : 0;
10656 }
10657
10658 /* Delete breakpoints by address or line. */
10659
10660 static void
10661 clear_command (const char *arg, int from_tty)
10662 {
10663 int default_match;
10664
10665 std::vector<symtab_and_line> decoded_sals;
10666 symtab_and_line last_sal;
10667 gdb::array_view<symtab_and_line> sals;
10668 if (arg)
10669 {
10670 decoded_sals
10671 = decode_line_with_current_source (arg,
10672 (DECODE_LINE_FUNFIRSTLINE
10673 | DECODE_LINE_LIST_MODE));
10674 default_match = 0;
10675 sals = decoded_sals;
10676 }
10677 else
10678 {
10679 /* Set sal's line, symtab, pc, and pspace to the values
10680 corresponding to the last call to print_frame_info. If the
10681 codepoint is not valid, this will set all the fields to 0. */
10682 last_sal = get_last_displayed_sal ();
10683 if (last_sal.symtab == 0)
10684 error (_("No source file specified."));
10685
10686 default_match = 1;
10687 sals = last_sal;
10688 }
10689
10690 /* We don't call resolve_sal_pc here. That's not as bad as it
10691 seems, because all existing breakpoints typically have both
10692 file/line and pc set. So, if clear is given file/line, we can
10693 match this to existing breakpoint without obtaining pc at all.
10694
10695 We only support clearing given the address explicitly
10696 present in breakpoint table. Say, we've set breakpoint
10697 at file:line. There were several PC values for that file:line,
10698 due to optimization, all in one block.
10699
10700 We've picked one PC value. If "clear" is issued with another
10701 PC corresponding to the same file:line, the breakpoint won't
10702 be cleared. We probably can still clear the breakpoint, but
10703 since the other PC value is never presented to user, user
10704 can only find it by guessing, and it does not seem important
10705 to support that. */
10706
10707 /* For each line spec given, delete bps which correspond to it. Do
10708 it in two passes, solely to preserve the current behavior that
10709 from_tty is forced true if we delete more than one
10710 breakpoint. */
10711
10712 std::vector<struct breakpoint *> found;
10713 for (const auto &sal : sals)
10714 {
10715 const char *sal_fullname;
10716
10717 /* If exact pc given, clear bpts at that pc.
10718 If line given (pc == 0), clear all bpts on specified line.
10719 If defaulting, clear all bpts on default line
10720 or at default pc.
10721
10722 defaulting sal.pc != 0 tests to do
10723
10724 0 1 pc
10725 1 1 pc _and_ line
10726 0 0 line
10727 1 0 <can't happen> */
10728
10729 sal_fullname = (sal.symtab == NULL
10730 ? NULL : symtab_to_fullname (sal.symtab));
10731
10732 /* Find all matching breakpoints and add them to 'found'. */
10733 for (breakpoint *b : all_breakpoints ())
10734 {
10735 int match = 0;
10736 /* Are we going to delete b? */
10737 if (b->type != bp_none && !is_watchpoint (b)
10738 && user_breakpoint_p (b))
10739 {
10740 for (bp_location *loc : b->locations ())
10741 {
10742 /* If the user specified file:line, don't allow a PC
10743 match. This matches historical gdb behavior. */
10744 int pc_match = (!sal.explicit_line
10745 && sal.pc
10746 && (loc->pspace == sal.pspace)
10747 && (loc->address == sal.pc)
10748 && (!section_is_overlay (loc->section)
10749 || loc->section == sal.section));
10750 int line_match = 0;
10751
10752 if ((default_match || sal.explicit_line)
10753 && loc->symtab != NULL
10754 && sal_fullname != NULL
10755 && sal.pspace == loc->pspace
10756 && loc->line_number == sal.line
10757 && filename_cmp (symtab_to_fullname (loc->symtab),
10758 sal_fullname) == 0)
10759 line_match = 1;
10760
10761 if (pc_match || line_match)
10762 {
10763 match = 1;
10764 break;
10765 }
10766 }
10767 }
10768
10769 if (match)
10770 found.push_back (b);
10771 }
10772 }
10773
10774 /* Now go thru the 'found' chain and delete them. */
10775 if (found.empty ())
10776 {
10777 if (arg)
10778 error (_("No breakpoint at %s."), arg);
10779 else
10780 error (_("No breakpoint at this line."));
10781 }
10782
10783 /* Remove duplicates from the vec. */
10784 std::sort (found.begin (), found.end (),
10785 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10786 {
10787 return compare_breakpoints (bp_a, bp_b) < 0;
10788 });
10789 found.erase (std::unique (found.begin (), found.end (),
10790 [] (const breakpoint *bp_a, const breakpoint *bp_b)
10791 {
10792 return compare_breakpoints (bp_a, bp_b) == 0;
10793 }),
10794 found.end ());
10795
10796 if (found.size () > 1)
10797 from_tty = 1; /* Always report if deleted more than one. */
10798 if (from_tty)
10799 {
10800 if (found.size () == 1)
10801 gdb_printf (_("Deleted breakpoint "));
10802 else
10803 gdb_printf (_("Deleted breakpoints "));
10804 }
10805
10806 for (breakpoint *iter : found)
10807 {
10808 if (from_tty)
10809 gdb_printf ("%d ", iter->number);
10810 delete_breakpoint (iter);
10811 }
10812 if (from_tty)
10813 gdb_putc ('\n');
10814 }
10815 \f
10816 /* Delete breakpoint in BS if they are `delete' breakpoints and
10817 all breakpoints that are marked for deletion, whether hit or not.
10818 This is called after any breakpoint is hit, or after errors. */
10819
10820 void
10821 breakpoint_auto_delete (bpstat *bs)
10822 {
10823 for (; bs; bs = bs->next)
10824 if (bs->breakpoint_at
10825 && bs->breakpoint_at->disposition == disp_del
10826 && bs->stop)
10827 delete_breakpoint (bs->breakpoint_at);
10828
10829 for (breakpoint *b : all_breakpoints_safe ())
10830 if (b->disposition == disp_del_at_next_stop)
10831 delete_breakpoint (b);
10832 }
10833
10834 /* A comparison function for bp_location AP and BP being interfaced to
10835 std::sort. Sort elements primarily by their ADDRESS (no matter what
10836 bl_address_is_meaningful says), secondarily by ordering first
10837 permanent elements and terciarily just ensuring the array is sorted
10838 stable way despite std::sort being an unstable algorithm. */
10839
10840 static int
10841 bp_location_is_less_than (const bp_location *a, const bp_location *b)
10842 {
10843 if (a->address != b->address)
10844 return a->address < b->address;
10845
10846 /* Sort locations at the same address by their pspace number, keeping
10847 locations of the same inferior (in a multi-inferior environment)
10848 grouped. */
10849
10850 if (a->pspace->num != b->pspace->num)
10851 return a->pspace->num < b->pspace->num;
10852
10853 /* Sort permanent breakpoints first. */
10854 if (a->permanent != b->permanent)
10855 return a->permanent > b->permanent;
10856
10857 /* Sort by type in order to make duplicate determination easier.
10858 See update_global_location_list. This is kept in sync with
10859 breakpoint_locations_match. */
10860 if (a->loc_type < b->loc_type)
10861 return true;
10862
10863 /* Likewise, for range-breakpoints, sort by length. */
10864 if (a->loc_type == bp_loc_hardware_breakpoint
10865 && b->loc_type == bp_loc_hardware_breakpoint
10866 && a->length < b->length)
10867 return true;
10868
10869 /* Make the internal GDB representation stable across GDB runs
10870 where A and B memory inside GDB can differ. Breakpoint locations of
10871 the same type at the same address can be sorted in arbitrary order. */
10872
10873 if (a->owner->number != b->owner->number)
10874 return a->owner->number < b->owner->number;
10875
10876 return a < b;
10877 }
10878
10879 /* Set bp_locations_placed_address_before_address_max and
10880 bp_locations_shadow_len_after_address_max according to the current
10881 content of the bp_locations array. */
10882
10883 static void
10884 bp_locations_target_extensions_update (void)
10885 {
10886 bp_locations_placed_address_before_address_max = 0;
10887 bp_locations_shadow_len_after_address_max = 0;
10888
10889 for (bp_location *bl : all_bp_locations ())
10890 {
10891 CORE_ADDR start, end, addr;
10892
10893 if (!bp_location_has_shadow (bl))
10894 continue;
10895
10896 start = bl->target_info.placed_address;
10897 end = start + bl->target_info.shadow_len;
10898
10899 gdb_assert (bl->address >= start);
10900 addr = bl->address - start;
10901 if (addr > bp_locations_placed_address_before_address_max)
10902 bp_locations_placed_address_before_address_max = addr;
10903
10904 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
10905
10906 gdb_assert (bl->address < end);
10907 addr = end - bl->address;
10908 if (addr > bp_locations_shadow_len_after_address_max)
10909 bp_locations_shadow_len_after_address_max = addr;
10910 }
10911 }
10912
10913 /* Download tracepoint locations if they haven't been. */
10914
10915 static void
10916 download_tracepoint_locations (void)
10917 {
10918 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
10919
10920 scoped_restore_current_pspace_and_thread restore_pspace_thread;
10921
10922 for (breakpoint *b : all_tracepoints ())
10923 {
10924 struct tracepoint *t;
10925 int bp_location_downloaded = 0;
10926
10927 if ((b->type == bp_fast_tracepoint
10928 ? !may_insert_fast_tracepoints
10929 : !may_insert_tracepoints))
10930 continue;
10931
10932 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
10933 {
10934 if (target_can_download_tracepoint ())
10935 can_download_tracepoint = TRIBOOL_TRUE;
10936 else
10937 can_download_tracepoint = TRIBOOL_FALSE;
10938 }
10939
10940 if (can_download_tracepoint == TRIBOOL_FALSE)
10941 break;
10942
10943 for (bp_location *bl : b->locations ())
10944 {
10945 /* In tracepoint, locations are _never_ duplicated, so
10946 should_be_inserted is equivalent to
10947 unduplicated_should_be_inserted. */
10948 if (!should_be_inserted (bl) || bl->inserted)
10949 continue;
10950
10951 switch_to_program_space_and_thread (bl->pspace);
10952
10953 target_download_tracepoint (bl);
10954
10955 bl->inserted = 1;
10956 bp_location_downloaded = 1;
10957 }
10958 t = (struct tracepoint *) b;
10959 t->number_on_target = b->number;
10960 if (bp_location_downloaded)
10961 gdb::observers::breakpoint_modified.notify (b);
10962 }
10963 }
10964
10965 /* Swap the insertion/duplication state between two locations. */
10966
10967 static void
10968 swap_insertion (struct bp_location *left, struct bp_location *right)
10969 {
10970 const int left_inserted = left->inserted;
10971 const int left_duplicate = left->duplicate;
10972 const int left_needs_update = left->needs_update;
10973 const struct bp_target_info left_target_info = left->target_info;
10974
10975 /* Locations of tracepoints can never be duplicated. */
10976 if (is_tracepoint (left->owner))
10977 gdb_assert (!left->duplicate);
10978 if (is_tracepoint (right->owner))
10979 gdb_assert (!right->duplicate);
10980
10981 left->inserted = right->inserted;
10982 left->duplicate = right->duplicate;
10983 left->needs_update = right->needs_update;
10984 left->target_info = right->target_info;
10985 right->inserted = left_inserted;
10986 right->duplicate = left_duplicate;
10987 right->needs_update = left_needs_update;
10988 right->target_info = left_target_info;
10989 }
10990
10991 /* Force the re-insertion of the locations at ADDRESS. This is called
10992 once a new/deleted/modified duplicate location is found and we are evaluating
10993 conditions on the target's side. Such conditions need to be updated on
10994 the target. */
10995
10996 static void
10997 force_breakpoint_reinsertion (struct bp_location *bl)
10998 {
10999 CORE_ADDR address = 0;
11000 int pspace_num;
11001
11002 address = bl->address;
11003 pspace_num = bl->pspace->num;
11004
11005 /* This is only meaningful if the target is
11006 evaluating conditions and if the user has
11007 opted for condition evaluation on the target's
11008 side. */
11009 if (gdb_evaluates_breakpoint_condition_p ()
11010 || !target_supports_evaluation_of_breakpoint_conditions ())
11011 return;
11012
11013 /* Flag all breakpoint locations with this address and
11014 the same program space as the location
11015 as "its condition has changed". We need to
11016 update the conditions on the target's side. */
11017 for (bp_location *loc : all_bp_locations_at_addr (address))
11018 {
11019 if (!is_breakpoint (loc->owner)
11020 || pspace_num != loc->pspace->num)
11021 continue;
11022
11023 /* Flag the location appropriately. We use a different state to
11024 let everyone know that we already updated the set of locations
11025 with addr bl->address and program space bl->pspace. This is so
11026 we don't have to keep calling these functions just to mark locations
11027 that have already been marked. */
11028 loc->condition_changed = condition_updated;
11029
11030 /* Free the agent expression bytecode as well. We will compute
11031 it later on. */
11032 loc->cond_bytecode.reset ();
11033 }
11034 }
11035
11036 /* Called whether new breakpoints are created, or existing breakpoints
11037 deleted, to update the global location list and recompute which
11038 locations are duplicate of which.
11039
11040 The INSERT_MODE flag determines whether locations may not, may, or
11041 shall be inserted now. See 'enum ugll_insert_mode' for more
11042 info. */
11043
11044 static void
11045 update_global_location_list (enum ugll_insert_mode insert_mode)
11046 {
11047 /* Last breakpoint location address that was marked for update. */
11048 CORE_ADDR last_addr = 0;
11049 /* Last breakpoint location program space that was marked for update. */
11050 int last_pspace_num = -1;
11051
11052 /* Used in the duplicates detection below. When iterating over all
11053 bp_locations, points to the first bp_location of a given address.
11054 Breakpoints and watchpoints of different types are never
11055 duplicates of each other. Keep one pointer for each type of
11056 breakpoint/watchpoint, so we only need to loop over all locations
11057 once. */
11058 struct bp_location *bp_loc_first; /* breakpoint */
11059 struct bp_location *wp_loc_first; /* hardware watchpoint */
11060 struct bp_location *awp_loc_first; /* access watchpoint */
11061 struct bp_location *rwp_loc_first; /* read watchpoint */
11062
11063 /* Saved former bp_locations array which we compare against the newly
11064 built bp_locations from the current state of ALL_BREAKPOINTS. */
11065 std::vector<bp_location *> old_locations = std::move (bp_locations);
11066 bp_locations.clear ();
11067
11068 for (breakpoint *b : all_breakpoints ())
11069 for (bp_location *loc : b->locations ())
11070 bp_locations.push_back (loc);
11071
11072 /* See if we need to "upgrade" a software breakpoint to a hardware
11073 breakpoint. Do this before deciding whether locations are
11074 duplicates. Also do this before sorting because sorting order
11075 depends on location type. */
11076 for (bp_location *loc : bp_locations)
11077 if (!loc->inserted && should_be_inserted (loc))
11078 handle_automatic_hardware_breakpoints (loc);
11079
11080 std::sort (bp_locations.begin (), bp_locations.end (),
11081 bp_location_is_less_than);
11082
11083 bp_locations_target_extensions_update ();
11084
11085 /* Identify bp_location instances that are no longer present in the
11086 new list, and therefore should be freed. Note that it's not
11087 necessary that those locations should be removed from inferior --
11088 if there's another location at the same address (previously
11089 marked as duplicate), we don't need to remove/insert the
11090 location.
11091
11092 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11093 and former bp_location array state respectively. */
11094
11095 size_t loc_i = 0;
11096 for (bp_location *old_loc : old_locations)
11097 {
11098 /* Tells if 'old_loc' is found among the new locations. If
11099 not, we have to free it. */
11100 int found_object = 0;
11101 /* Tells if the location should remain inserted in the target. */
11102 int keep_in_target = 0;
11103 int removed = 0;
11104
11105 /* Skip LOCP entries which will definitely never be needed.
11106 Stop either at or being the one matching OLD_LOC. */
11107 while (loc_i < bp_locations.size ()
11108 && bp_locations[loc_i]->address < old_loc->address)
11109 loc_i++;
11110
11111 for (size_t loc2_i = loc_i;
11112 (loc2_i < bp_locations.size ()
11113 && bp_locations[loc2_i]->address == old_loc->address);
11114 loc2_i++)
11115 {
11116 /* Check if this is a new/duplicated location or a duplicated
11117 location that had its condition modified. If so, we want to send
11118 its condition to the target if evaluation of conditions is taking
11119 place there. */
11120 if (bp_locations[loc2_i]->condition_changed == condition_modified
11121 && (last_addr != old_loc->address
11122 || last_pspace_num != old_loc->pspace->num))
11123 {
11124 force_breakpoint_reinsertion (bp_locations[loc2_i]);
11125 last_pspace_num = old_loc->pspace->num;
11126 }
11127
11128 if (bp_locations[loc2_i] == old_loc)
11129 found_object = 1;
11130 }
11131
11132 /* We have already handled this address, update it so that we don't
11133 have to go through updates again. */
11134 last_addr = old_loc->address;
11135
11136 /* Target-side condition evaluation: Handle deleted locations. */
11137 if (!found_object)
11138 force_breakpoint_reinsertion (old_loc);
11139
11140 /* If this location is no longer present, and inserted, look if
11141 there's maybe a new location at the same address. If so,
11142 mark that one inserted, and don't remove this one. This is
11143 needed so that we don't have a time window where a breakpoint
11144 at certain location is not inserted. */
11145
11146 if (old_loc->inserted)
11147 {
11148 /* If the location is inserted now, we might have to remove
11149 it. */
11150
11151 if (found_object && should_be_inserted (old_loc))
11152 {
11153 /* The location is still present in the location list,
11154 and still should be inserted. Don't do anything. */
11155 keep_in_target = 1;
11156 }
11157 else
11158 {
11159 /* This location still exists, but it won't be kept in the
11160 target since it may have been disabled. We proceed to
11161 remove its target-side condition. */
11162
11163 /* The location is either no longer present, or got
11164 disabled. See if there's another location at the
11165 same address, in which case we don't need to remove
11166 this one from the target. */
11167
11168 /* OLD_LOC comes from existing struct breakpoint. */
11169 if (bl_address_is_meaningful (old_loc))
11170 {
11171 for (size_t loc2_i = loc_i;
11172 (loc2_i < bp_locations.size ()
11173 && bp_locations[loc2_i]->address == old_loc->address);
11174 loc2_i++)
11175 {
11176 bp_location *loc2 = bp_locations[loc2_i];
11177
11178 if (loc2 == old_loc)
11179 continue;
11180
11181 if (breakpoint_locations_match (loc2, old_loc))
11182 {
11183 /* Read watchpoint locations are switched to
11184 access watchpoints, if the former are not
11185 supported, but the latter are. */
11186 if (is_hardware_watchpoint (old_loc->owner))
11187 {
11188 gdb_assert (is_hardware_watchpoint (loc2->owner));
11189 loc2->watchpoint_type = old_loc->watchpoint_type;
11190 }
11191
11192 /* loc2 is a duplicated location. We need to check
11193 if it should be inserted in case it will be
11194 unduplicated. */
11195 if (unduplicated_should_be_inserted (loc2))
11196 {
11197 swap_insertion (old_loc, loc2);
11198 keep_in_target = 1;
11199 break;
11200 }
11201 }
11202 }
11203 }
11204 }
11205
11206 if (!keep_in_target)
11207 {
11208 if (remove_breakpoint (old_loc))
11209 {
11210 /* This is just about all we can do. We could keep
11211 this location on the global list, and try to
11212 remove it next time, but there's no particular
11213 reason why we will succeed next time.
11214
11215 Note that at this point, old_loc->owner is still
11216 valid, as delete_breakpoint frees the breakpoint
11217 only after calling us. */
11218 gdb_printf (_("warning: Error removing "
11219 "breakpoint %d\n"),
11220 old_loc->owner->number);
11221 }
11222 removed = 1;
11223 }
11224 }
11225
11226 if (!found_object)
11227 {
11228 if (removed && target_is_non_stop_p ()
11229 && need_moribund_for_location_type (old_loc))
11230 {
11231 /* This location was removed from the target. In
11232 non-stop mode, a race condition is possible where
11233 we've removed a breakpoint, but stop events for that
11234 breakpoint are already queued and will arrive later.
11235 We apply an heuristic to be able to distinguish such
11236 SIGTRAPs from other random SIGTRAPs: we keep this
11237 breakpoint location for a bit, and will retire it
11238 after we see some number of events. The theory here
11239 is that reporting of events should, "on the average",
11240 be fair, so after a while we'll see events from all
11241 threads that have anything of interest, and no longer
11242 need to keep this breakpoint location around. We
11243 don't hold locations forever so to reduce chances of
11244 mistaking a non-breakpoint SIGTRAP for a breakpoint
11245 SIGTRAP.
11246
11247 The heuristic failing can be disastrous on
11248 decr_pc_after_break targets.
11249
11250 On decr_pc_after_break targets, like e.g., x86-linux,
11251 if we fail to recognize a late breakpoint SIGTRAP,
11252 because events_till_retirement has reached 0 too
11253 soon, we'll fail to do the PC adjustment, and report
11254 a random SIGTRAP to the user. When the user resumes
11255 the inferior, it will most likely immediately crash
11256 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
11257 corrupted, because of being resumed e.g., in the
11258 middle of a multi-byte instruction, or skipped a
11259 one-byte instruction. This was actually seen happen
11260 on native x86-linux, and should be less rare on
11261 targets that do not support new thread events, like
11262 remote, due to the heuristic depending on
11263 thread_count.
11264
11265 Mistaking a random SIGTRAP for a breakpoint trap
11266 causes similar symptoms (PC adjustment applied when
11267 it shouldn't), but then again, playing with SIGTRAPs
11268 behind the debugger's back is asking for trouble.
11269
11270 Since hardware watchpoint traps are always
11271 distinguishable from other traps, so we don't need to
11272 apply keep hardware watchpoint moribund locations
11273 around. We simply always ignore hardware watchpoint
11274 traps we can no longer explain. */
11275
11276 process_stratum_target *proc_target = nullptr;
11277 for (inferior *inf : all_inferiors ())
11278 if (inf->pspace == old_loc->pspace)
11279 {
11280 proc_target = inf->process_target ();
11281 break;
11282 }
11283 if (proc_target != nullptr)
11284 old_loc->events_till_retirement
11285 = 3 * (thread_count (proc_target) + 1);
11286 else
11287 old_loc->events_till_retirement = 1;
11288 old_loc->owner = NULL;
11289
11290 moribund_locations.push_back (old_loc);
11291 }
11292 else
11293 {
11294 old_loc->owner = NULL;
11295 decref_bp_location (&old_loc);
11296 }
11297 }
11298 }
11299
11300 /* Rescan breakpoints at the same address and section, marking the
11301 first one as "first" and any others as "duplicates". This is so
11302 that the bpt instruction is only inserted once. If we have a
11303 permanent breakpoint at the same place as BPT, make that one the
11304 official one, and the rest as duplicates. Permanent breakpoints
11305 are sorted first for the same address.
11306
11307 Do the same for hardware watchpoints, but also considering the
11308 watchpoint's type (regular/access/read) and length. */
11309
11310 bp_loc_first = NULL;
11311 wp_loc_first = NULL;
11312 awp_loc_first = NULL;
11313 rwp_loc_first = NULL;
11314
11315 for (bp_location *loc : all_bp_locations ())
11316 {
11317 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
11318 non-NULL. */
11319 struct bp_location **loc_first_p;
11320 breakpoint *b = loc->owner;
11321
11322 if (!unduplicated_should_be_inserted (loc)
11323 || !bl_address_is_meaningful (loc)
11324 /* Don't detect duplicate for tracepoint locations because they are
11325 never duplicated. See the comments in field `duplicate' of
11326 `struct bp_location'. */
11327 || is_tracepoint (b))
11328 {
11329 /* Clear the condition modification flag. */
11330 loc->condition_changed = condition_unchanged;
11331 continue;
11332 }
11333
11334 if (b->type == bp_hardware_watchpoint)
11335 loc_first_p = &wp_loc_first;
11336 else if (b->type == bp_read_watchpoint)
11337 loc_first_p = &rwp_loc_first;
11338 else if (b->type == bp_access_watchpoint)
11339 loc_first_p = &awp_loc_first;
11340 else
11341 loc_first_p = &bp_loc_first;
11342
11343 if (*loc_first_p == NULL
11344 || (overlay_debugging && loc->section != (*loc_first_p)->section)
11345 || !breakpoint_locations_match (loc, *loc_first_p))
11346 {
11347 *loc_first_p = loc;
11348 loc->duplicate = 0;
11349
11350 if (is_breakpoint (loc->owner) && loc->condition_changed)
11351 {
11352 loc->needs_update = 1;
11353 /* Clear the condition modification flag. */
11354 loc->condition_changed = condition_unchanged;
11355 }
11356 continue;
11357 }
11358
11359
11360 /* This and the above ensure the invariant that the first location
11361 is not duplicated, and is the inserted one.
11362 All following are marked as duplicated, and are not inserted. */
11363 if (loc->inserted)
11364 swap_insertion (loc, *loc_first_p);
11365 loc->duplicate = 1;
11366
11367 /* Clear the condition modification flag. */
11368 loc->condition_changed = condition_unchanged;
11369 }
11370
11371 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
11372 {
11373 if (insert_mode != UGLL_DONT_INSERT)
11374 insert_breakpoint_locations ();
11375 else
11376 {
11377 /* Even though the caller told us to not insert new
11378 locations, we may still need to update conditions on the
11379 target's side of breakpoints that were already inserted
11380 if the target is evaluating breakpoint conditions. We
11381 only update conditions for locations that are marked
11382 "needs_update". */
11383 update_inserted_breakpoint_locations ();
11384 }
11385 }
11386
11387 if (insert_mode != UGLL_DONT_INSERT)
11388 download_tracepoint_locations ();
11389 }
11390
11391 void
11392 breakpoint_retire_moribund (void)
11393 {
11394 for (int ix = 0; ix < moribund_locations.size (); ++ix)
11395 {
11396 struct bp_location *loc = moribund_locations[ix];
11397 if (--(loc->events_till_retirement) == 0)
11398 {
11399 decref_bp_location (&loc);
11400 unordered_remove (moribund_locations, ix);
11401 --ix;
11402 }
11403 }
11404 }
11405
11406 static void
11407 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
11408 {
11409
11410 try
11411 {
11412 update_global_location_list (insert_mode);
11413 }
11414 catch (const gdb_exception_error &e)
11415 {
11416 }
11417 }
11418
11419 /* Clear BKP from a BPS. */
11420
11421 static void
11422 bpstat_remove_bp_location (bpstat *bps, struct breakpoint *bpt)
11423 {
11424 bpstat *bs;
11425
11426 for (bs = bps; bs; bs = bs->next)
11427 if (bs->breakpoint_at == bpt)
11428 {
11429 bs->breakpoint_at = NULL;
11430 bs->old_val = NULL;
11431 /* bs->commands will be freed later. */
11432 }
11433 }
11434
11435 /* Callback for iterate_over_threads. */
11436 static int
11437 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
11438 {
11439 struct breakpoint *bpt = (struct breakpoint *) data;
11440
11441 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
11442 return 0;
11443 }
11444
11445 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
11446 callbacks. */
11447
11448 static void
11449 say_where (struct breakpoint *b)
11450 {
11451 struct value_print_options opts;
11452
11453 get_user_print_options (&opts);
11454
11455 /* i18n: cagney/2005-02-11: Below needs to be merged into a
11456 single string. */
11457 if (b->loc == NULL)
11458 {
11459 /* For pending locations, the output differs slightly based
11460 on b->extra_string. If this is non-NULL, it contains either
11461 a condition or dprintf arguments. */
11462 if (b->extra_string == NULL)
11463 {
11464 gdb_printf (_(" (%s) pending."),
11465 event_location_to_string (b->location.get ()));
11466 }
11467 else if (b->type == bp_dprintf)
11468 {
11469 gdb_printf (_(" (%s,%s) pending."),
11470 event_location_to_string (b->location.get ()),
11471 b->extra_string.get ());
11472 }
11473 else
11474 {
11475 gdb_printf (_(" (%s %s) pending."),
11476 event_location_to_string (b->location.get ()),
11477 b->extra_string.get ());
11478 }
11479 }
11480 else
11481 {
11482 if (opts.addressprint || b->loc->symtab == NULL)
11483 gdb_printf (" at %ps",
11484 styled_string (address_style.style (),
11485 paddress (b->loc->gdbarch,
11486 b->loc->address)));
11487 if (b->loc->symtab != NULL)
11488 {
11489 /* If there is a single location, we can print the location
11490 more nicely. */
11491 if (b->loc->next == NULL)
11492 {
11493 const char *filename
11494 = symtab_to_filename_for_display (b->loc->symtab);
11495 gdb_printf (": file %ps, line %d.",
11496 styled_string (file_name_style.style (),
11497 filename),
11498 b->loc->line_number);
11499 }
11500 else
11501 /* This is not ideal, but each location may have a
11502 different file name, and this at least reflects the
11503 real situation somewhat. */
11504 gdb_printf (": %s.",
11505 event_location_to_string (b->location.get ()));
11506 }
11507
11508 if (b->loc->next)
11509 {
11510 struct bp_location *loc = b->loc;
11511 int n = 0;
11512 for (; loc; loc = loc->next)
11513 ++n;
11514 gdb_printf (" (%d locations)", n);
11515 }
11516 }
11517 }
11518
11519 /* See breakpoint.h. */
11520
11521 bp_location_range breakpoint::locations ()
11522 {
11523 return bp_location_range (this->loc);
11524 }
11525
11526 static struct bp_location *
11527 base_breakpoint_allocate_location (struct breakpoint *self)
11528 {
11529 return new bp_location (self);
11530 }
11531
11532 struct bp_location *
11533 breakpoint::allocate_location ()
11534 {
11535 return new bp_location (this);
11536 }
11537
11538 static void
11539 base_breakpoint_re_set (struct breakpoint *b)
11540 {
11541 /* Nothing to re-set. */
11542 }
11543
11544 #define internal_error_pure_virtual_called() \
11545 gdb_assert_not_reached ("pure virtual function called")
11546
11547 static int
11548 base_breakpoint_insert_location (struct bp_location *bl)
11549 {
11550 internal_error_pure_virtual_called ();
11551 }
11552
11553 int
11554 breakpoint::insert_location (struct bp_location *bl)
11555 {
11556 internal_error_pure_virtual_called ();
11557 }
11558
11559 static int
11560 base_breakpoint_remove_location (struct bp_location *bl,
11561 enum remove_bp_reason reason)
11562 {
11563 internal_error_pure_virtual_called ();
11564 }
11565
11566 int
11567 breakpoint::remove_location (struct bp_location *bl,
11568 enum remove_bp_reason reason)
11569 {
11570 internal_error_pure_virtual_called ();
11571 }
11572
11573 static int
11574 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
11575 const address_space *aspace,
11576 CORE_ADDR bp_addr,
11577 const target_waitstatus &ws)
11578 {
11579 internal_error_pure_virtual_called ();
11580 }
11581
11582 int
11583 breakpoint::breakpoint_hit (const struct bp_location *bl,
11584 const address_space *aspace,
11585 CORE_ADDR bp_addr,
11586 const target_waitstatus &ws)
11587 {
11588 internal_error_pure_virtual_called ();
11589 }
11590
11591 static void
11592 base_breakpoint_check_status (bpstat *bs)
11593 {
11594 /* Always stop. */
11595 }
11596
11597 /* A "works_in_software_mode" breakpoint_ops method that just internal
11598 errors. */
11599
11600 static int
11601 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
11602 {
11603 internal_error_pure_virtual_called ();
11604 }
11605
11606 int
11607 breakpoint::works_in_software_mode () const
11608 {
11609 internal_error_pure_virtual_called ();
11610 }
11611
11612 /* A "resources_needed" breakpoint_ops method that just internal
11613 errors. */
11614
11615 static int
11616 base_breakpoint_resources_needed (const struct bp_location *bl)
11617 {
11618 internal_error_pure_virtual_called ();
11619 }
11620
11621 int
11622 breakpoint::resources_needed (const struct bp_location *bl)
11623 {
11624 internal_error_pure_virtual_called ();
11625 }
11626
11627 static enum print_stop_action
11628 base_breakpoint_print_it (bpstat *bs)
11629 {
11630 internal_error_pure_virtual_called ();
11631 }
11632
11633 enum print_stop_action
11634 breakpoint::print_it (bpstat *bs)
11635 {
11636 internal_error_pure_virtual_called ();
11637 }
11638
11639 static bool
11640 base_breakpoint_print_one (struct breakpoint *, struct bp_location **)
11641 {
11642 return false;
11643 }
11644
11645 static void
11646 base_breakpoint_print_one_detail (const struct breakpoint *self,
11647 struct ui_out *uiout)
11648 {
11649 /* nothing */
11650 }
11651
11652 static void
11653 base_breakpoint_print_mention (struct breakpoint *b)
11654 {
11655 internal_error_pure_virtual_called ();
11656 }
11657
11658 void
11659 breakpoint::print_mention ()
11660 {
11661 internal_error_pure_virtual_called ();
11662 }
11663
11664 static void
11665 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
11666 {
11667 internal_error_pure_virtual_called ();
11668 }
11669
11670 void
11671 breakpoint::print_recreate (struct ui_file *fp)
11672 {
11673 internal_error_pure_virtual_called ();
11674 }
11675
11676 static void
11677 base_breakpoint_create_sals_from_location
11678 (struct event_location *location,
11679 struct linespec_result *canonical,
11680 enum bptype type_wanted)
11681 {
11682 internal_error_pure_virtual_called ();
11683 }
11684
11685 static void
11686 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
11687 struct linespec_result *c,
11688 gdb::unique_xmalloc_ptr<char> cond_string,
11689 gdb::unique_xmalloc_ptr<char> extra_string,
11690 enum bptype type_wanted,
11691 enum bpdisp disposition,
11692 int thread,
11693 int task, int ignore_count,
11694 const struct breakpoint_ops *o,
11695 int from_tty, int enabled,
11696 int internal, unsigned flags)
11697 {
11698 internal_error_pure_virtual_called ();
11699 }
11700
11701 static std::vector<symtab_and_line>
11702 base_breakpoint_decode_location (struct breakpoint *b,
11703 struct event_location *location,
11704 struct program_space *search_pspace)
11705 {
11706 internal_error_pure_virtual_called ();
11707 }
11708
11709 std::vector<symtab_and_line>
11710 breakpoint::decode_location (struct event_location *location,
11711 struct program_space *search_pspace)
11712 {
11713 internal_error_pure_virtual_called ();
11714 }
11715
11716 /* The default 'explains_signal' method. */
11717
11718 static int
11719 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
11720 {
11721 return 1;
11722 }
11723
11724 /* The default "after_condition_true" method. */
11725
11726 static void
11727 base_breakpoint_after_condition_true (struct bpstat *bs)
11728 {
11729 /* Nothing to do. */
11730 }
11731
11732 struct breakpoint_ops base_breakpoint_ops =
11733 {
11734 base_breakpoint_allocate_location,
11735 base_breakpoint_re_set,
11736 base_breakpoint_insert_location,
11737 base_breakpoint_remove_location,
11738 base_breakpoint_breakpoint_hit,
11739 base_breakpoint_check_status,
11740 base_breakpoint_resources_needed,
11741 base_breakpoint_works_in_software_mode,
11742 base_breakpoint_print_it,
11743 base_breakpoint_print_one,
11744 base_breakpoint_print_one_detail,
11745 base_breakpoint_print_mention,
11746 base_breakpoint_print_recreate,
11747 base_breakpoint_create_sals_from_location,
11748 base_breakpoint_create_breakpoints_sal,
11749 base_breakpoint_decode_location,
11750 base_breakpoint_explains_signal,
11751 base_breakpoint_after_condition_true,
11752 };
11753
11754 struct breakpoint_ops vtable_breakpoint_ops =
11755 {
11756 [] (struct breakpoint *b) { return b->allocate_location (); },
11757 [] (struct breakpoint *b) { b->re_set (); },
11758 [] (struct bp_location *l)
11759 {
11760 return l->owner->insert_location (l);
11761 },
11762 [] (struct bp_location *l, enum remove_bp_reason reason)
11763 {
11764 return l->owner->remove_location (l, reason);
11765 },
11766 [] (const struct bp_location *bl,
11767 const address_space *aspace,
11768 CORE_ADDR bp_addr,
11769 const target_waitstatus &ws)
11770 {
11771 return bl->owner->breakpoint_hit (bl, aspace, bp_addr, ws);
11772 },
11773 [] (struct bpstat *bs) { bs->breakpoint_at->check_status (bs); },
11774 [] (const struct bp_location *bl)
11775 { return bl->owner->resources_needed (bl); },
11776 [] (const struct breakpoint *b)
11777 { return b->works_in_software_mode (); },
11778 [] (struct bpstat *bs)
11779 { return bs->breakpoint_at->print_it (bs); },
11780 [] (struct breakpoint *b, struct bp_location **bl)
11781 { return b->print_one (bl); },
11782 [] (const struct breakpoint *b, struct ui_out *out)
11783 { b->print_one_detail (out); },
11784 [] (struct breakpoint *b) { b->print_mention (); },
11785 [] (struct breakpoint *b, struct ui_file *fp)
11786 { b->print_recreate (fp); },
11787 create_sals_from_location_default,
11788 create_breakpoints_sal_default,
11789 [] (struct breakpoint *b,
11790 struct event_location *location,
11791 struct program_space *search_pspace)
11792 { return b->decode_location (location, search_pspace); },
11793 [] (struct breakpoint *b, enum gdb_signal s)
11794 { return b->explains_signal (s); },
11795 [] (struct bpstat *bs)
11796 { bs->breakpoint_at->after_condition_true (bs); }
11797 };
11798
11799 /* Default breakpoint_ops methods. */
11800
11801 static void
11802 bkpt_re_set (struct breakpoint *b)
11803 {
11804 /* FIXME: is this still reachable? */
11805 if (breakpoint_event_location_empty_p (b))
11806 {
11807 /* Anything without a location can't be re-set. */
11808 delete_breakpoint (b);
11809 return;
11810 }
11811
11812 breakpoint_re_set_default (b);
11813 }
11814
11815 static int
11816 bkpt_insert_location (struct bp_location *bl)
11817 {
11818 CORE_ADDR addr = bl->target_info.reqstd_address;
11819
11820 bl->target_info.kind = breakpoint_kind (bl, &addr);
11821 bl->target_info.placed_address = addr;
11822
11823 if (bl->loc_type == bp_loc_hardware_breakpoint)
11824 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
11825 else
11826 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
11827 }
11828
11829 static int
11830 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
11831 {
11832 if (bl->loc_type == bp_loc_hardware_breakpoint)
11833 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
11834 else
11835 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
11836 }
11837
11838 static int
11839 bkpt_breakpoint_hit (const struct bp_location *bl,
11840 const address_space *aspace, CORE_ADDR bp_addr,
11841 const target_waitstatus &ws)
11842 {
11843 if (ws.kind () != TARGET_WAITKIND_STOPPED
11844 || ws.sig () != GDB_SIGNAL_TRAP)
11845 return 0;
11846
11847 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
11848 aspace, bp_addr))
11849 return 0;
11850
11851 if (overlay_debugging /* unmapped overlay section */
11852 && section_is_overlay (bl->section)
11853 && !section_is_mapped (bl->section))
11854 return 0;
11855
11856 return 1;
11857 }
11858
11859 static int
11860 dprintf_breakpoint_hit (const struct bp_location *bl,
11861 const address_space *aspace, CORE_ADDR bp_addr,
11862 const target_waitstatus &ws)
11863 {
11864 if (dprintf_style == dprintf_style_agent
11865 && target_can_run_breakpoint_commands ())
11866 {
11867 /* An agent-style dprintf never causes a stop. If we see a trap
11868 for this address it must be for a breakpoint that happens to
11869 be set at the same address. */
11870 return 0;
11871 }
11872
11873 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
11874 }
11875
11876 static int
11877 bkpt_resources_needed (const struct bp_location *bl)
11878 {
11879 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
11880
11881 return 1;
11882 }
11883
11884 static enum print_stop_action
11885 bkpt_print_it (bpstat *bs)
11886 {
11887 struct breakpoint *b;
11888 const struct bp_location *bl;
11889 int bp_temp;
11890 struct ui_out *uiout = current_uiout;
11891
11892 gdb_assert (bs->bp_location_at != NULL);
11893
11894 bl = bs->bp_location_at.get ();
11895 b = bs->breakpoint_at;
11896
11897 bp_temp = b->disposition == disp_del;
11898 if (bl->address != bl->requested_address)
11899 breakpoint_adjustment_warning (bl->requested_address,
11900 bl->address,
11901 b->number, 1);
11902 annotate_breakpoint (b->number);
11903 maybe_print_thread_hit_breakpoint (uiout);
11904
11905 if (uiout->is_mi_like_p ())
11906 {
11907 uiout->field_string ("reason",
11908 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11909 uiout->field_string ("disp", bpdisp_text (b->disposition));
11910 }
11911 if (bp_temp)
11912 uiout->message ("Temporary breakpoint %pF, ",
11913 signed_field ("bkptno", b->number));
11914 else
11915 uiout->message ("Breakpoint %pF, ",
11916 signed_field ("bkptno", b->number));
11917
11918 return PRINT_SRC_AND_LOC;
11919 }
11920
11921 static void
11922 bkpt_print_mention (struct breakpoint *b)
11923 {
11924 if (current_uiout->is_mi_like_p ())
11925 return;
11926
11927 switch (b->type)
11928 {
11929 case bp_breakpoint:
11930 case bp_gnu_ifunc_resolver:
11931 if (b->disposition == disp_del)
11932 gdb_printf (_("Temporary breakpoint"));
11933 else
11934 gdb_printf (_("Breakpoint"));
11935 gdb_printf (_(" %d"), b->number);
11936 if (b->type == bp_gnu_ifunc_resolver)
11937 gdb_printf (_(" at gnu-indirect-function resolver"));
11938 break;
11939 case bp_hardware_breakpoint:
11940 gdb_printf (_("Hardware assisted breakpoint %d"), b->number);
11941 break;
11942 case bp_dprintf:
11943 gdb_printf (_("Dprintf %d"), b->number);
11944 break;
11945 }
11946
11947 say_where (b);
11948 }
11949
11950 static void
11951 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
11952 {
11953 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
11954 gdb_printf (fp, "tbreak");
11955 else if (tp->type == bp_breakpoint)
11956 gdb_printf (fp, "break");
11957 else if (tp->type == bp_hardware_breakpoint
11958 && tp->disposition == disp_del)
11959 gdb_printf (fp, "thbreak");
11960 else if (tp->type == bp_hardware_breakpoint)
11961 gdb_printf (fp, "hbreak");
11962 else
11963 internal_error (__FILE__, __LINE__,
11964 _("unhandled breakpoint type %d"), (int) tp->type);
11965
11966 gdb_printf (fp, " %s",
11967 event_location_to_string (tp->location.get ()));
11968
11969 /* Print out extra_string if this breakpoint is pending. It might
11970 contain, for example, conditions that were set by the user. */
11971 if (tp->loc == NULL && tp->extra_string != NULL)
11972 gdb_printf (fp, " %s", tp->extra_string.get ());
11973
11974 print_recreate_thread (tp, fp);
11975 }
11976
11977 static std::vector<symtab_and_line>
11978 bkpt_decode_location (struct breakpoint *b,
11979 struct event_location *location,
11980 struct program_space *search_pspace)
11981 {
11982 return decode_location_default (b, location, search_pspace);
11983 }
11984
11985 /* Virtual table for internal breakpoints. */
11986
11987 static void
11988 internal_bkpt_re_set (struct breakpoint *b)
11989 {
11990 switch (b->type)
11991 {
11992 /* Delete overlay event and longjmp master breakpoints; they
11993 will be reset later by breakpoint_re_set. */
11994 case bp_overlay_event:
11995 case bp_longjmp_master:
11996 case bp_std_terminate_master:
11997 case bp_exception_master:
11998 delete_breakpoint (b);
11999 break;
12000
12001 /* This breakpoint is special, it's set up when the inferior
12002 starts and we really don't want to touch it. */
12003 case bp_shlib_event:
12004
12005 /* Like bp_shlib_event, this breakpoint type is special. Once
12006 it is set up, we do not want to touch it. */
12007 case bp_thread_event:
12008 break;
12009 }
12010 }
12011
12012 static void
12013 internal_bkpt_check_status (bpstat *bs)
12014 {
12015 if (bs->breakpoint_at->type == bp_shlib_event)
12016 {
12017 /* If requested, stop when the dynamic linker notifies GDB of
12018 events. This allows the user to get control and place
12019 breakpoints in initializer routines for dynamically loaded
12020 objects (among other things). */
12021 bs->stop = stop_on_solib_events;
12022 bs->print = stop_on_solib_events;
12023 }
12024 else
12025 bs->stop = 0;
12026 }
12027
12028 static enum print_stop_action
12029 internal_bkpt_print_it (bpstat *bs)
12030 {
12031 struct breakpoint *b;
12032
12033 b = bs->breakpoint_at;
12034
12035 switch (b->type)
12036 {
12037 case bp_shlib_event:
12038 /* Did we stop because the user set the stop_on_solib_events
12039 variable? (If so, we report this as a generic, "Stopped due
12040 to shlib event" message.) */
12041 print_solib_event (false);
12042 break;
12043
12044 case bp_thread_event:
12045 /* Not sure how we will get here.
12046 GDB should not stop for these breakpoints. */
12047 gdb_printf (_("Thread Event Breakpoint: gdb should not stop!\n"));
12048 break;
12049
12050 case bp_overlay_event:
12051 /* By analogy with the thread event, GDB should not stop for these. */
12052 gdb_printf (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12053 break;
12054
12055 case bp_longjmp_master:
12056 /* These should never be enabled. */
12057 gdb_printf (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12058 break;
12059
12060 case bp_std_terminate_master:
12061 /* These should never be enabled. */
12062 gdb_printf (_("std::terminate Master Breakpoint: "
12063 "gdb should not stop!\n"));
12064 break;
12065
12066 case bp_exception_master:
12067 /* These should never be enabled. */
12068 gdb_printf (_("Exception Master Breakpoint: "
12069 "gdb should not stop!\n"));
12070 break;
12071 }
12072
12073 return PRINT_NOTHING;
12074 }
12075
12076 static void
12077 internal_bkpt_print_mention (struct breakpoint *b)
12078 {
12079 /* Nothing to mention. These breakpoints are internal. */
12080 }
12081
12082 /* Virtual table for momentary breakpoints */
12083
12084 static void
12085 momentary_bkpt_re_set (struct breakpoint *b)
12086 {
12087 /* Keep temporary breakpoints, which can be encountered when we step
12088 over a dlopen call and solib_add is resetting the breakpoints.
12089 Otherwise these should have been blown away via the cleanup chain
12090 or by breakpoint_init_inferior when we rerun the executable. */
12091 }
12092
12093 static void
12094 momentary_bkpt_check_status (bpstat *bs)
12095 {
12096 /* Nothing. The point of these breakpoints is causing a stop. */
12097 }
12098
12099 static enum print_stop_action
12100 momentary_bkpt_print_it (bpstat *bs)
12101 {
12102 return PRINT_UNKNOWN;
12103 }
12104
12105 static void
12106 momentary_bkpt_print_mention (struct breakpoint *b)
12107 {
12108 /* Nothing to mention. These breakpoints are internal. */
12109 }
12110
12111 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12112
12113 It gets cleared already on the removal of the first one of such placed
12114 breakpoints. This is OK as they get all removed altogether. */
12115
12116 longjmp_breakpoint::~longjmp_breakpoint ()
12117 {
12118 thread_info *tp = find_thread_global_id (this->thread);
12119
12120 if (tp != NULL)
12121 tp->initiating_frame = null_frame_id;
12122 }
12123
12124 /* Specific methods for probe breakpoints. */
12125
12126 static int
12127 bkpt_probe_insert_location (struct bp_location *bl)
12128 {
12129 int v = bkpt_insert_location (bl);
12130
12131 if (v == 0)
12132 {
12133 /* The insertion was successful, now let's set the probe's semaphore
12134 if needed. */
12135 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
12136 }
12137
12138 return v;
12139 }
12140
12141 static int
12142 bkpt_probe_remove_location (struct bp_location *bl,
12143 enum remove_bp_reason reason)
12144 {
12145 /* Let's clear the semaphore before removing the location. */
12146 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
12147
12148 return bkpt_remove_location (bl, reason);
12149 }
12150
12151 static void
12152 bkpt_probe_create_sals_from_location (struct event_location *location,
12153 struct linespec_result *canonical,
12154 enum bptype type_wanted)
12155 {
12156 struct linespec_sals lsal;
12157
12158 lsal.sals = parse_probes (location, NULL, canonical);
12159 lsal.canonical
12160 = xstrdup (event_location_to_string (canonical->location.get ()));
12161 canonical->lsals.push_back (std::move (lsal));
12162 }
12163
12164 static std::vector<symtab_and_line>
12165 bkpt_probe_decode_location (struct breakpoint *b,
12166 struct event_location *location,
12167 struct program_space *search_pspace)
12168 {
12169 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12170 if (sals.empty ())
12171 error (_("probe not found"));
12172 return sals;
12173 }
12174
12175 /* The breakpoint_ops structure to be used in tracepoints. */
12176
12177 void
12178 tracepoint::re_set ()
12179 {
12180 breakpoint_re_set_default (this);
12181 }
12182
12183 int
12184 tracepoint::breakpoint_hit (const struct bp_location *bl,
12185 const address_space *aspace, CORE_ADDR bp_addr,
12186 const target_waitstatus &ws)
12187 {
12188 /* By definition, the inferior does not report stops at
12189 tracepoints. */
12190 return 0;
12191 }
12192
12193 void
12194 tracepoint::print_one_detail (struct ui_out *uiout) const
12195 {
12196 if (!static_trace_marker_id.empty ())
12197 {
12198 gdb_assert (type == bp_static_tracepoint);
12199
12200 uiout->message ("\tmarker id is %pF\n",
12201 string_field ("static-tracepoint-marker-string-id",
12202 static_trace_marker_id.c_str ()));
12203 }
12204 }
12205
12206 void
12207 tracepoint::print_mention ()
12208 {
12209 if (current_uiout->is_mi_like_p ())
12210 return;
12211
12212 switch (type)
12213 {
12214 case bp_tracepoint:
12215 gdb_printf (_("Tracepoint"));
12216 gdb_printf (_(" %d"), number);
12217 break;
12218 case bp_fast_tracepoint:
12219 gdb_printf (_("Fast tracepoint"));
12220 gdb_printf (_(" %d"), number);
12221 break;
12222 case bp_static_tracepoint:
12223 gdb_printf (_("Static tracepoint"));
12224 gdb_printf (_(" %d"), number);
12225 break;
12226 default:
12227 internal_error (__FILE__, __LINE__,
12228 _("unhandled tracepoint type %d"), (int) type);
12229 }
12230
12231 say_where (this);
12232 }
12233
12234 void
12235 tracepoint::print_recreate (struct ui_file *fp)
12236 {
12237 if (type == bp_fast_tracepoint)
12238 gdb_printf (fp, "ftrace");
12239 else if (type == bp_static_tracepoint)
12240 gdb_printf (fp, "strace");
12241 else if (type == bp_tracepoint)
12242 gdb_printf (fp, "trace");
12243 else
12244 internal_error (__FILE__, __LINE__,
12245 _("unhandled tracepoint type %d"), (int) type);
12246
12247 gdb_printf (fp, " %s", event_location_to_string (location.get ()));
12248 print_recreate_thread (this, fp);
12249
12250 if (pass_count)
12251 gdb_printf (fp, " passcount %d\n", pass_count);
12252 }
12253
12254 std::vector<symtab_and_line>
12255 tracepoint::decode_location (struct event_location *location,
12256 struct program_space *search_pspace)
12257 {
12258 return decode_location_default (this, location, search_pspace);
12259 }
12260
12261 /* Virtual table for tracepoints on static probes. */
12262
12263 static void
12264 tracepoint_probe_create_sals_from_location
12265 (struct event_location *location,
12266 struct linespec_result *canonical,
12267 enum bptype type_wanted)
12268 {
12269 /* We use the same method for breakpoint on probes. */
12270 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
12271 }
12272
12273 static std::vector<symtab_and_line>
12274 tracepoint_probe_decode_location (struct breakpoint *b,
12275 struct event_location *location,
12276 struct program_space *search_pspace)
12277 {
12278 /* We use the same method for breakpoint on probes. */
12279 return bkpt_probe_decode_location (b, location, search_pspace);
12280 }
12281
12282 /* Dprintf breakpoint_ops methods. */
12283
12284 static void
12285 dprintf_re_set (struct breakpoint *b)
12286 {
12287 breakpoint_re_set_default (b);
12288
12289 /* extra_string should never be non-NULL for dprintf. */
12290 gdb_assert (b->extra_string != NULL);
12291
12292 /* 1 - connect to target 1, that can run breakpoint commands.
12293 2 - create a dprintf, which resolves fine.
12294 3 - disconnect from target 1
12295 4 - connect to target 2, that can NOT run breakpoint commands.
12296
12297 After steps #3/#4, you'll want the dprintf command list to
12298 be updated, because target 1 and 2 may well return different
12299 answers for target_can_run_breakpoint_commands().
12300 Given absence of finer grained resetting, we get to do
12301 it all the time. */
12302 if (b->extra_string != NULL)
12303 update_dprintf_command_list (b);
12304 }
12305
12306 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12307
12308 static void
12309 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12310 {
12311 gdb_printf (fp, "dprintf %s,%s",
12312 event_location_to_string (tp->location.get ()),
12313 tp->extra_string.get ());
12314 print_recreate_thread (tp, fp);
12315 }
12316
12317 /* Implement the "after_condition_true" breakpoint_ops method for
12318 dprintf.
12319
12320 dprintf's are implemented with regular commands in their command
12321 list, but we run the commands here instead of before presenting the
12322 stop to the user, as dprintf's don't actually cause a stop. This
12323 also makes it so that the commands of multiple dprintfs at the same
12324 address are all handled. */
12325
12326 static void
12327 dprintf_after_condition_true (struct bpstat *bs)
12328 {
12329 /* dprintf's never cause a stop. This wasn't set in the
12330 check_status hook instead because that would make the dprintf's
12331 condition not be evaluated. */
12332 bs->stop = 0;
12333
12334 /* Run the command list here. Take ownership of it instead of
12335 copying. We never want these commands to run later in
12336 bpstat_do_actions, if a breakpoint that causes a stop happens to
12337 be set at same address as this dprintf, or even if running the
12338 commands here throws. */
12339 counted_command_line cmds = std::move (bs->commands);
12340 gdb_assert (cmds != nullptr);
12341 execute_control_commands (cmds.get (), 0);
12342 }
12343
12344 /* The breakpoint_ops structure to be used on static tracepoints with
12345 markers (`-m'). */
12346
12347 static void
12348 strace_marker_create_sals_from_location (struct event_location *location,
12349 struct linespec_result *canonical,
12350 enum bptype type_wanted)
12351 {
12352 struct linespec_sals lsal;
12353 const char *arg_start, *arg;
12354
12355 arg = arg_start = get_linespec_location (location)->spec_string;
12356 lsal.sals = decode_static_tracepoint_spec (&arg);
12357
12358 std::string str (arg_start, arg - arg_start);
12359 const char *ptr = str.c_str ();
12360 canonical->location
12361 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
12362
12363 lsal.canonical
12364 = xstrdup (event_location_to_string (canonical->location.get ()));
12365 canonical->lsals.push_back (std::move (lsal));
12366 }
12367
12368 static void
12369 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12370 struct linespec_result *canonical,
12371 gdb::unique_xmalloc_ptr<char> cond_string,
12372 gdb::unique_xmalloc_ptr<char> extra_string,
12373 enum bptype type_wanted,
12374 enum bpdisp disposition,
12375 int thread,
12376 int task, int ignore_count,
12377 const struct breakpoint_ops *ops,
12378 int from_tty, int enabled,
12379 int internal, unsigned flags)
12380 {
12381 const linespec_sals &lsal = canonical->lsals[0];
12382
12383 /* If the user is creating a static tracepoint by marker id
12384 (strace -m MARKER_ID), then store the sals index, so that
12385 breakpoint_re_set can try to match up which of the newly
12386 found markers corresponds to this one, and, don't try to
12387 expand multiple locations for each sal, given than SALS
12388 already should contain all sals for MARKER_ID. */
12389
12390 for (size_t i = 0; i < lsal.sals.size (); i++)
12391 {
12392 event_location_up location
12393 = copy_event_location (canonical->location.get ());
12394
12395 std::unique_ptr<tracepoint> tp (new tracepoint ());
12396 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
12397 std::move (location), NULL,
12398 std::move (cond_string),
12399 std::move (extra_string),
12400 type_wanted, disposition,
12401 thread, task, ignore_count, ops,
12402 from_tty, enabled, internal, flags,
12403 canonical->special_display);
12404 /* Given that its possible to have multiple markers with
12405 the same string id, if the user is creating a static
12406 tracepoint by marker id ("strace -m MARKER_ID"), then
12407 store the sals index, so that breakpoint_re_set can
12408 try to match up which of the newly found markers
12409 corresponds to this one */
12410 tp->static_trace_marker_id_idx = i;
12411
12412 install_breakpoint (internal, std::move (tp), 0);
12413 }
12414 }
12415
12416 static std::vector<symtab_and_line>
12417 strace_marker_decode_location (struct breakpoint *b,
12418 struct event_location *location,
12419 struct program_space *search_pspace)
12420 {
12421 struct tracepoint *tp = (struct tracepoint *) b;
12422 const char *s = get_linespec_location (location)->spec_string;
12423
12424 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
12425 if (sals.size () > tp->static_trace_marker_id_idx)
12426 {
12427 sals[0] = sals[tp->static_trace_marker_id_idx];
12428 sals.resize (1);
12429 return sals;
12430 }
12431 else
12432 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
12433 }
12434
12435 static struct breakpoint_ops strace_marker_breakpoint_ops;
12436
12437 static int
12438 strace_marker_p (struct breakpoint *b)
12439 {
12440 return b->ops == &strace_marker_breakpoint_ops;
12441 }
12442
12443 /* Delete a breakpoint and clean up all traces of it in the data
12444 structures. */
12445
12446 void
12447 delete_breakpoint (struct breakpoint *bpt)
12448 {
12449 gdb_assert (bpt != NULL);
12450
12451 /* Has this bp already been deleted? This can happen because
12452 multiple lists can hold pointers to bp's. bpstat lists are
12453 especial culprits.
12454
12455 One example of this happening is a watchpoint's scope bp. When
12456 the scope bp triggers, we notice that the watchpoint is out of
12457 scope, and delete it. We also delete its scope bp. But the
12458 scope bp is marked "auto-deleting", and is already on a bpstat.
12459 That bpstat is then checked for auto-deleting bp's, which are
12460 deleted.
12461
12462 A real solution to this problem might involve reference counts in
12463 bp's, and/or giving them pointers back to their referencing
12464 bpstat's, and teaching delete_breakpoint to only free a bp's
12465 storage when no more references were extent. A cheaper bandaid
12466 was chosen. */
12467 if (bpt->type == bp_none)
12468 return;
12469
12470 /* At least avoid this stale reference until the reference counting
12471 of breakpoints gets resolved. */
12472 if (bpt->related_breakpoint != bpt)
12473 {
12474 struct breakpoint *related;
12475 struct watchpoint *w;
12476
12477 if (bpt->type == bp_watchpoint_scope)
12478 w = (struct watchpoint *) bpt->related_breakpoint;
12479 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
12480 w = (struct watchpoint *) bpt;
12481 else
12482 w = NULL;
12483 if (w != NULL)
12484 watchpoint_del_at_next_stop (w);
12485
12486 /* Unlink bpt from the bpt->related_breakpoint ring. */
12487 for (related = bpt; related->related_breakpoint != bpt;
12488 related = related->related_breakpoint);
12489 related->related_breakpoint = bpt->related_breakpoint;
12490 bpt->related_breakpoint = bpt;
12491 }
12492
12493 /* watch_command_1 creates a watchpoint but only sets its number if
12494 update_watchpoint succeeds in creating its bp_locations. If there's
12495 a problem in that process, we'll be asked to delete the half-created
12496 watchpoint. In that case, don't announce the deletion. */
12497 if (bpt->number)
12498 gdb::observers::breakpoint_deleted.notify (bpt);
12499
12500 if (breakpoint_chain == bpt)
12501 breakpoint_chain = bpt->next;
12502
12503 for (breakpoint *b : all_breakpoints ())
12504 if (b->next == bpt)
12505 {
12506 b->next = bpt->next;
12507 break;
12508 }
12509
12510 /* Be sure no bpstat's are pointing at the breakpoint after it's
12511 been freed. */
12512 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
12513 in all threads for now. Note that we cannot just remove bpstats
12514 pointing at bpt from the stop_bpstat list entirely, as breakpoint
12515 commands are associated with the bpstat; if we remove it here,
12516 then the later call to bpstat_do_actions (&stop_bpstat); in
12517 event-top.c won't do anything, and temporary breakpoints with
12518 commands won't work. */
12519
12520 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
12521
12522 /* Now that breakpoint is removed from breakpoint list, update the
12523 global location list. This will remove locations that used to
12524 belong to this breakpoint. Do this before freeing the breakpoint
12525 itself, since remove_breakpoint looks at location's owner. It
12526 might be better design to have location completely
12527 self-contained, but it's not the case now. */
12528 update_global_location_list (UGLL_DONT_INSERT);
12529
12530 /* On the chance that someone will soon try again to delete this
12531 same bp, we mark it as deleted before freeing its storage. */
12532 bpt->type = bp_none;
12533 delete bpt;
12534 }
12535
12536 /* Iterator function to call a user-provided callback function once
12537 for each of B and its related breakpoints. */
12538
12539 static void
12540 iterate_over_related_breakpoints (struct breakpoint *b,
12541 gdb::function_view<void (breakpoint *)> function)
12542 {
12543 struct breakpoint *related;
12544
12545 related = b;
12546 do
12547 {
12548 struct breakpoint *next;
12549
12550 /* FUNCTION may delete RELATED. */
12551 next = related->related_breakpoint;
12552
12553 if (next == related)
12554 {
12555 /* RELATED is the last ring entry. */
12556 function (related);
12557
12558 /* FUNCTION may have deleted it, so we'd never reach back to
12559 B. There's nothing left to do anyway, so just break
12560 out. */
12561 break;
12562 }
12563 else
12564 function (related);
12565
12566 related = next;
12567 }
12568 while (related != b);
12569 }
12570
12571 static void
12572 delete_command (const char *arg, int from_tty)
12573 {
12574 dont_repeat ();
12575
12576 if (arg == 0)
12577 {
12578 int breaks_to_delete = 0;
12579
12580 /* Delete all breakpoints if no argument. Do not delete
12581 internal breakpoints, these have to be deleted with an
12582 explicit breakpoint number argument. */
12583 for (breakpoint *b : all_breakpoints ())
12584 if (user_breakpoint_p (b))
12585 {
12586 breaks_to_delete = 1;
12587 break;
12588 }
12589
12590 /* Ask user only if there are some breakpoints to delete. */
12591 if (!from_tty
12592 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
12593 for (breakpoint *b : all_breakpoints_safe ())
12594 if (user_breakpoint_p (b))
12595 delete_breakpoint (b);
12596 }
12597 else
12598 map_breakpoint_numbers
12599 (arg, [&] (breakpoint *br)
12600 {
12601 iterate_over_related_breakpoints (br, delete_breakpoint);
12602 });
12603 }
12604
12605 /* Return true if all locations of B bound to PSPACE are pending. If
12606 PSPACE is NULL, all locations of all program spaces are
12607 considered. */
12608
12609 static int
12610 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
12611 {
12612 for (bp_location *loc : b->locations ())
12613 if ((pspace == NULL
12614 || loc->pspace == pspace)
12615 && !loc->shlib_disabled
12616 && !loc->pspace->executing_startup)
12617 return 0;
12618 return 1;
12619 }
12620
12621 /* Subroutine of update_breakpoint_locations to simplify it.
12622 Return non-zero if multiple fns in list LOC have the same name.
12623 Null names are ignored. */
12624
12625 static int
12626 ambiguous_names_p (struct bp_location *loc)
12627 {
12628 struct bp_location *l;
12629 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
12630 xcalloc, xfree));
12631
12632 for (l = loc; l != NULL; l = l->next)
12633 {
12634 const char **slot;
12635 const char *name = l->function_name.get ();
12636
12637 /* Allow for some names to be NULL, ignore them. */
12638 if (name == NULL)
12639 continue;
12640
12641 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
12642 INSERT);
12643 /* NOTE: We can assume slot != NULL here because xcalloc never
12644 returns NULL. */
12645 if (*slot != NULL)
12646 return 1;
12647 *slot = name;
12648 }
12649
12650 return 0;
12651 }
12652
12653 /* When symbols change, it probably means the sources changed as well,
12654 and it might mean the static tracepoint markers are no longer at
12655 the same address or line numbers they used to be at last we
12656 checked. Losing your static tracepoints whenever you rebuild is
12657 undesirable. This function tries to resync/rematch gdb static
12658 tracepoints with the markers on the target, for static tracepoints
12659 that have not been set by marker id. Static tracepoint that have
12660 been set by marker id are reset by marker id in breakpoint_re_set.
12661 The heuristic is:
12662
12663 1) For a tracepoint set at a specific address, look for a marker at
12664 the old PC. If one is found there, assume to be the same marker.
12665 If the name / string id of the marker found is different from the
12666 previous known name, assume that means the user renamed the marker
12667 in the sources, and output a warning.
12668
12669 2) For a tracepoint set at a given line number, look for a marker
12670 at the new address of the old line number. If one is found there,
12671 assume to be the same marker. If the name / string id of the
12672 marker found is different from the previous known name, assume that
12673 means the user renamed the marker in the sources, and output a
12674 warning.
12675
12676 3) If a marker is no longer found at the same address or line, it
12677 may mean the marker no longer exists. But it may also just mean
12678 the code changed a bit. Maybe the user added a few lines of code
12679 that made the marker move up or down (in line number terms). Ask
12680 the target for info about the marker with the string id as we knew
12681 it. If found, update line number and address in the matching
12682 static tracepoint. This will get confused if there's more than one
12683 marker with the same ID (possible in UST, although unadvised
12684 precisely because it confuses tools). */
12685
12686 static struct symtab_and_line
12687 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
12688 {
12689 struct tracepoint *tp = (struct tracepoint *) b;
12690 struct static_tracepoint_marker marker;
12691 CORE_ADDR pc;
12692
12693 pc = sal.pc;
12694 if (sal.line)
12695 find_line_pc (sal.symtab, sal.line, &pc);
12696
12697 if (target_static_tracepoint_marker_at (pc, &marker))
12698 {
12699 if (tp->static_trace_marker_id != marker.str_id)
12700 warning (_("static tracepoint %d changed probed marker from %s to %s"),
12701 b->number, tp->static_trace_marker_id.c_str (),
12702 marker.str_id.c_str ());
12703
12704 tp->static_trace_marker_id = std::move (marker.str_id);
12705
12706 return sal;
12707 }
12708
12709 /* Old marker wasn't found on target at lineno. Try looking it up
12710 by string ID. */
12711 if (!sal.explicit_pc
12712 && sal.line != 0
12713 && sal.symtab != NULL
12714 && !tp->static_trace_marker_id.empty ())
12715 {
12716 std::vector<static_tracepoint_marker> markers
12717 = target_static_tracepoint_markers_by_strid
12718 (tp->static_trace_marker_id.c_str ());
12719
12720 if (!markers.empty ())
12721 {
12722 struct symbol *sym;
12723 struct static_tracepoint_marker *tpmarker;
12724 struct ui_out *uiout = current_uiout;
12725 struct explicit_location explicit_loc;
12726
12727 tpmarker = &markers[0];
12728
12729 tp->static_trace_marker_id = std::move (tpmarker->str_id);
12730
12731 warning (_("marker for static tracepoint %d (%s) not "
12732 "found at previous line number"),
12733 b->number, tp->static_trace_marker_id.c_str ());
12734
12735 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
12736 sym = find_pc_sect_function (tpmarker->address, NULL);
12737 uiout->text ("Now in ");
12738 if (sym)
12739 {
12740 uiout->field_string ("func", sym->print_name (),
12741 function_name_style.style ());
12742 uiout->text (" at ");
12743 }
12744 uiout->field_string ("file",
12745 symtab_to_filename_for_display (sal2.symtab),
12746 file_name_style.style ());
12747 uiout->text (":");
12748
12749 if (uiout->is_mi_like_p ())
12750 {
12751 const char *fullname = symtab_to_fullname (sal2.symtab);
12752
12753 uiout->field_string ("fullname", fullname);
12754 }
12755
12756 uiout->field_signed ("line", sal2.line);
12757 uiout->text ("\n");
12758
12759 b->loc->line_number = sal2.line;
12760 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
12761
12762 b->location.reset (NULL);
12763 initialize_explicit_location (&explicit_loc);
12764 explicit_loc.source_filename
12765 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
12766 explicit_loc.line_offset.offset = b->loc->line_number;
12767 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
12768 b->location = new_explicit_location (&explicit_loc);
12769
12770 /* Might be nice to check if function changed, and warn if
12771 so. */
12772 }
12773 }
12774 return sal;
12775 }
12776
12777 /* Returns 1 iff locations A and B are sufficiently same that
12778 we don't need to report breakpoint as changed. */
12779
12780 static int
12781 locations_are_equal (struct bp_location *a, struct bp_location *b)
12782 {
12783 while (a && b)
12784 {
12785 if (a->address != b->address)
12786 return 0;
12787
12788 if (a->shlib_disabled != b->shlib_disabled)
12789 return 0;
12790
12791 if (a->enabled != b->enabled)
12792 return 0;
12793
12794 if (a->disabled_by_cond != b->disabled_by_cond)
12795 return 0;
12796
12797 a = a->next;
12798 b = b->next;
12799 }
12800
12801 if ((a == NULL) != (b == NULL))
12802 return 0;
12803
12804 return 1;
12805 }
12806
12807 /* Split all locations of B that are bound to PSPACE out of B's
12808 location list to a separate list and return that list's head. If
12809 PSPACE is NULL, hoist out all locations of B. */
12810
12811 static struct bp_location *
12812 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
12813 {
12814 struct bp_location head;
12815 struct bp_location *i = b->loc;
12816 struct bp_location **i_link = &b->loc;
12817 struct bp_location *hoisted = &head;
12818
12819 if (pspace == NULL)
12820 {
12821 i = b->loc;
12822 b->loc = NULL;
12823 return i;
12824 }
12825
12826 head.next = NULL;
12827
12828 while (i != NULL)
12829 {
12830 if (i->pspace == pspace)
12831 {
12832 *i_link = i->next;
12833 i->next = NULL;
12834 hoisted->next = i;
12835 hoisted = i;
12836 }
12837 else
12838 i_link = &i->next;
12839 i = *i_link;
12840 }
12841
12842 return head.next;
12843 }
12844
12845 /* Create new breakpoint locations for B (a hardware or software
12846 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
12847 zero, then B is a ranged breakpoint. Only recreates locations for
12848 FILTER_PSPACE. Locations of other program spaces are left
12849 untouched. */
12850
12851 void
12852 update_breakpoint_locations (struct breakpoint *b,
12853 struct program_space *filter_pspace,
12854 gdb::array_view<const symtab_and_line> sals,
12855 gdb::array_view<const symtab_and_line> sals_end)
12856 {
12857 struct bp_location *existing_locations;
12858
12859 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
12860 {
12861 /* Ranged breakpoints have only one start location and one end
12862 location. */
12863 b->enable_state = bp_disabled;
12864 gdb_printf (gdb_stderr,
12865 _("Could not reset ranged breakpoint %d: "
12866 "multiple locations found\n"),
12867 b->number);
12868 return;
12869 }
12870
12871 /* If there's no new locations, and all existing locations are
12872 pending, don't do anything. This optimizes the common case where
12873 all locations are in the same shared library, that was unloaded.
12874 We'd like to retain the location, so that when the library is
12875 loaded again, we don't loose the enabled/disabled status of the
12876 individual locations. */
12877 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
12878 return;
12879
12880 existing_locations = hoist_existing_locations (b, filter_pspace);
12881
12882 for (const auto &sal : sals)
12883 {
12884 struct bp_location *new_loc;
12885
12886 switch_to_program_space_and_thread (sal.pspace);
12887
12888 new_loc = add_location_to_breakpoint (b, &sal);
12889
12890 /* Reparse conditions, they might contain references to the
12891 old symtab. */
12892 if (b->cond_string != NULL)
12893 {
12894 const char *s;
12895
12896 s = b->cond_string.get ();
12897 try
12898 {
12899 new_loc->cond = parse_exp_1 (&s, sal.pc,
12900 block_for_pc (sal.pc),
12901 0);
12902 }
12903 catch (const gdb_exception_error &e)
12904 {
12905 new_loc->disabled_by_cond = true;
12906 }
12907 }
12908
12909 if (!sals_end.empty ())
12910 {
12911 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
12912
12913 new_loc->length = end - sals[0].pc + 1;
12914 }
12915 }
12916
12917 /* If possible, carry over 'disable' status from existing
12918 breakpoints. */
12919 {
12920 struct bp_location *e = existing_locations;
12921 /* If there are multiple breakpoints with the same function name,
12922 e.g. for inline functions, comparing function names won't work.
12923 Instead compare pc addresses; this is just a heuristic as things
12924 may have moved, but in practice it gives the correct answer
12925 often enough until a better solution is found. */
12926 int have_ambiguous_names = ambiguous_names_p (b->loc);
12927
12928 for (; e; e = e->next)
12929 {
12930 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
12931 {
12932 if (have_ambiguous_names)
12933 {
12934 for (bp_location *l : b->locations ())
12935 {
12936 /* Ignore software vs hardware location type at
12937 this point, because with "set breakpoint
12938 auto-hw", after a re-set, locations that were
12939 hardware can end up as software, or vice versa.
12940 As mentioned above, this is an heuristic and in
12941 practice should give the correct answer often
12942 enough. */
12943 if (breakpoint_locations_match (e, l, true))
12944 {
12945 l->enabled = e->enabled;
12946 l->disabled_by_cond = e->disabled_by_cond;
12947 break;
12948 }
12949 }
12950 }
12951 else
12952 {
12953 for (bp_location *l : b->locations ())
12954 if (l->function_name
12955 && strcmp (e->function_name.get (),
12956 l->function_name.get ()) == 0)
12957 {
12958 l->enabled = e->enabled;
12959 l->disabled_by_cond = e->disabled_by_cond;
12960 break;
12961 }
12962 }
12963 }
12964 }
12965 }
12966
12967 if (!locations_are_equal (existing_locations, b->loc))
12968 gdb::observers::breakpoint_modified.notify (b);
12969 }
12970
12971 /* Find the SaL locations corresponding to the given LOCATION.
12972 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
12973
12974 static std::vector<symtab_and_line>
12975 location_to_sals (struct breakpoint *b, struct event_location *location,
12976 struct program_space *search_pspace, int *found)
12977 {
12978 struct gdb_exception exception;
12979
12980 gdb_assert (b->ops != NULL);
12981
12982 std::vector<symtab_and_line> sals;
12983
12984 try
12985 {
12986 sals = b->ops->decode_location (b, location, search_pspace);
12987 }
12988 catch (gdb_exception_error &e)
12989 {
12990 int not_found_and_ok = 0;
12991
12992 /* For pending breakpoints, it's expected that parsing will
12993 fail until the right shared library is loaded. User has
12994 already told to create pending breakpoints and don't need
12995 extra messages. If breakpoint is in bp_shlib_disabled
12996 state, then user already saw the message about that
12997 breakpoint being disabled, and don't want to see more
12998 errors. */
12999 if (e.error == NOT_FOUND_ERROR
13000 && (b->condition_not_parsed
13001 || (b->loc != NULL
13002 && search_pspace != NULL
13003 && b->loc->pspace != search_pspace)
13004 || (b->loc && b->loc->shlib_disabled)
13005 || (b->loc && b->loc->pspace->executing_startup)
13006 || b->enable_state == bp_disabled))
13007 not_found_and_ok = 1;
13008
13009 if (!not_found_and_ok)
13010 {
13011 /* We surely don't want to warn about the same breakpoint
13012 10 times. One solution, implemented here, is disable
13013 the breakpoint on error. Another solution would be to
13014 have separate 'warning emitted' flag. Since this
13015 happens only when a binary has changed, I don't know
13016 which approach is better. */
13017 b->enable_state = bp_disabled;
13018 throw;
13019 }
13020
13021 exception = std::move (e);
13022 }
13023
13024 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
13025 {
13026 for (auto &sal : sals)
13027 resolve_sal_pc (&sal);
13028 if (b->condition_not_parsed && b->extra_string != NULL)
13029 {
13030 gdb::unique_xmalloc_ptr<char> cond_string, extra_string;
13031 int thread, task;
13032
13033 find_condition_and_thread_for_sals (sals, b->extra_string.get (),
13034 &cond_string, &thread,
13035 &task, &extra_string);
13036 gdb_assert (b->cond_string == NULL);
13037 if (cond_string)
13038 b->cond_string = std::move (cond_string);
13039 b->thread = thread;
13040 b->task = task;
13041 if (extra_string)
13042 b->extra_string = std::move (extra_string);
13043 b->condition_not_parsed = 0;
13044 }
13045
13046 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13047 sals[0] = update_static_tracepoint (b, sals[0]);
13048
13049 *found = 1;
13050 }
13051 else
13052 *found = 0;
13053
13054 return sals;
13055 }
13056
13057 /* The default re_set method, for typical hardware or software
13058 breakpoints. Reevaluate the breakpoint and recreate its
13059 locations. */
13060
13061 static void
13062 breakpoint_re_set_default (struct breakpoint *b)
13063 {
13064 struct program_space *filter_pspace = current_program_space;
13065 std::vector<symtab_and_line> expanded, expanded_end;
13066
13067 int found;
13068 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13069 filter_pspace, &found);
13070 if (found)
13071 expanded = std::move (sals);
13072
13073 if (b->location_range_end != NULL)
13074 {
13075 std::vector<symtab_and_line> sals_end
13076 = location_to_sals (b, b->location_range_end.get (),
13077 filter_pspace, &found);
13078 if (found)
13079 expanded_end = std::move (sals_end);
13080 }
13081
13082 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
13083 }
13084
13085 /* Default method for creating SALs from an address string. It basically
13086 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13087
13088 static void
13089 create_sals_from_location_default (struct event_location *location,
13090 struct linespec_result *canonical,
13091 enum bptype type_wanted)
13092 {
13093 parse_breakpoint_sals (location, canonical);
13094 }
13095
13096 /* Call create_breakpoints_sal for the given arguments. This is the default
13097 function for the `create_breakpoints_sal' method of
13098 breakpoint_ops. */
13099
13100 static void
13101 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13102 struct linespec_result *canonical,
13103 gdb::unique_xmalloc_ptr<char> cond_string,
13104 gdb::unique_xmalloc_ptr<char> extra_string,
13105 enum bptype type_wanted,
13106 enum bpdisp disposition,
13107 int thread,
13108 int task, int ignore_count,
13109 const struct breakpoint_ops *ops,
13110 int from_tty, int enabled,
13111 int internal, unsigned flags)
13112 {
13113 create_breakpoints_sal (gdbarch, canonical,
13114 std::move (cond_string),
13115 std::move (extra_string),
13116 type_wanted, disposition,
13117 thread, task, ignore_count, ops, from_tty,
13118 enabled, internal, flags);
13119 }
13120
13121 /* Decode the line represented by S by calling decode_line_full. This is the
13122 default function for the `decode_location' method of breakpoint_ops. */
13123
13124 static std::vector<symtab_and_line>
13125 decode_location_default (struct breakpoint *b,
13126 struct event_location *location,
13127 struct program_space *search_pspace)
13128 {
13129 struct linespec_result canonical;
13130
13131 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
13132 NULL, 0, &canonical, multiple_symbols_all,
13133 b->filter.get ());
13134
13135 /* We should get 0 or 1 resulting SALs. */
13136 gdb_assert (canonical.lsals.size () < 2);
13137
13138 if (!canonical.lsals.empty ())
13139 {
13140 const linespec_sals &lsal = canonical.lsals[0];
13141 return std::move (lsal.sals);
13142 }
13143 return {};
13144 }
13145
13146 /* Reset a breakpoint. */
13147
13148 static void
13149 breakpoint_re_set_one (breakpoint *b)
13150 {
13151 input_radix = b->input_radix;
13152 set_language (b->language);
13153
13154 b->ops->re_set (b);
13155 }
13156
13157 /* Re-set breakpoint locations for the current program space.
13158 Locations bound to other program spaces are left untouched. */
13159
13160 void
13161 breakpoint_re_set (void)
13162 {
13163 {
13164 scoped_restore_current_language save_language;
13165 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
13166 scoped_restore_current_pspace_and_thread restore_pspace_thread;
13167
13168 /* breakpoint_re_set_one sets the current_language to the language
13169 of the breakpoint it is resetting (see prepare_re_set_context)
13170 before re-evaluating the breakpoint's location. This change can
13171 unfortunately get undone by accident if the language_mode is set
13172 to auto, and we either switch frames, or more likely in this context,
13173 we select the current frame.
13174
13175 We prevent this by temporarily turning the language_mode to
13176 language_mode_manual. We restore it once all breakpoints
13177 have been reset. */
13178 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13179 language_mode = language_mode_manual;
13180
13181 /* Note: we must not try to insert locations until after all
13182 breakpoints have been re-set. Otherwise, e.g., when re-setting
13183 breakpoint 1, we'd insert the locations of breakpoint 2, which
13184 hadn't been re-set yet, and thus may have stale locations. */
13185
13186 for (breakpoint *b : all_breakpoints_safe ())
13187 {
13188 try
13189 {
13190 breakpoint_re_set_one (b);
13191 }
13192 catch (const gdb_exception &ex)
13193 {
13194 exception_fprintf (gdb_stderr, ex,
13195 "Error in re-setting breakpoint %d: ",
13196 b->number);
13197 }
13198 }
13199
13200 jit_breakpoint_re_set ();
13201 }
13202
13203 create_overlay_event_breakpoint ();
13204 create_longjmp_master_breakpoint ();
13205 create_std_terminate_master_breakpoint ();
13206 create_exception_master_breakpoint ();
13207
13208 /* Now we can insert. */
13209 update_global_location_list (UGLL_MAY_INSERT);
13210 }
13211 \f
13212 /* Reset the thread number of this breakpoint:
13213
13214 - If the breakpoint is for all threads, leave it as-is.
13215 - Else, reset it to the current thread for inferior_ptid. */
13216 void
13217 breakpoint_re_set_thread (struct breakpoint *b)
13218 {
13219 if (b->thread != -1)
13220 {
13221 b->thread = inferior_thread ()->global_num;
13222
13223 /* We're being called after following a fork. The new fork is
13224 selected as current, and unless this was a vfork will have a
13225 different program space from the original thread. Reset that
13226 as well. */
13227 b->loc->pspace = current_program_space;
13228 }
13229 }
13230
13231 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13232 If from_tty is nonzero, it prints a message to that effect,
13233 which ends with a period (no newline). */
13234
13235 void
13236 set_ignore_count (int bptnum, int count, int from_tty)
13237 {
13238 if (count < 0)
13239 count = 0;
13240
13241 for (breakpoint *b : all_breakpoints ())
13242 if (b->number == bptnum)
13243 {
13244 if (is_tracepoint (b))
13245 {
13246 if (from_tty && count != 0)
13247 gdb_printf (_("Ignore count ignored for tracepoint %d."),
13248 bptnum);
13249 return;
13250 }
13251
13252 b->ignore_count = count;
13253 if (from_tty)
13254 {
13255 if (count == 0)
13256 gdb_printf (_("Will stop next time "
13257 "breakpoint %d is reached."),
13258 bptnum);
13259 else if (count == 1)
13260 gdb_printf (_("Will ignore next crossing of breakpoint %d."),
13261 bptnum);
13262 else
13263 gdb_printf (_("Will ignore next %d "
13264 "crossings of breakpoint %d."),
13265 count, bptnum);
13266 }
13267 gdb::observers::breakpoint_modified.notify (b);
13268 return;
13269 }
13270
13271 error (_("No breakpoint number %d."), bptnum);
13272 }
13273
13274 /* Command to set ignore-count of breakpoint N to COUNT. */
13275
13276 static void
13277 ignore_command (const char *args, int from_tty)
13278 {
13279 const char *p = args;
13280 int num;
13281
13282 if (p == 0)
13283 error_no_arg (_("a breakpoint number"));
13284
13285 num = get_number (&p);
13286 if (num == 0)
13287 error (_("bad breakpoint number: '%s'"), args);
13288 if (*p == 0)
13289 error (_("Second argument (specified ignore-count) is missing."));
13290
13291 set_ignore_count (num,
13292 longest_to_int (value_as_long (parse_and_eval (p))),
13293 from_tty);
13294 if (from_tty)
13295 gdb_printf ("\n");
13296 }
13297 \f
13298
13299 /* Call FUNCTION on each of the breakpoints with numbers in the range
13300 defined by BP_NUM_RANGE (an inclusive range). */
13301
13302 static void
13303 map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13304 gdb::function_view<void (breakpoint *)> function)
13305 {
13306 if (bp_num_range.first == 0)
13307 {
13308 warning (_("bad breakpoint number at or near '%d'"),
13309 bp_num_range.first);
13310 }
13311 else
13312 {
13313 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
13314 {
13315 bool match = false;
13316
13317 for (breakpoint *b : all_breakpoints_safe ())
13318 if (b->number == i)
13319 {
13320 match = true;
13321 function (b);
13322 break;
13323 }
13324 if (!match)
13325 gdb_printf (_("No breakpoint number %d.\n"), i);
13326 }
13327 }
13328 }
13329
13330 /* Call FUNCTION on each of the breakpoints whose numbers are given in
13331 ARGS. */
13332
13333 static void
13334 map_breakpoint_numbers (const char *args,
13335 gdb::function_view<void (breakpoint *)> function)
13336 {
13337 if (args == NULL || *args == '\0')
13338 error_no_arg (_("one or more breakpoint numbers"));
13339
13340 number_or_range_parser parser (args);
13341
13342 while (!parser.finished ())
13343 {
13344 int num = parser.get_number ();
13345 map_breakpoint_number_range (std::make_pair (num, num), function);
13346 }
13347 }
13348
13349 /* Return the breakpoint location structure corresponding to the
13350 BP_NUM and LOC_NUM values. */
13351
13352 static struct bp_location *
13353 find_location_by_number (int bp_num, int loc_num)
13354 {
13355 breakpoint *b = get_breakpoint (bp_num);
13356
13357 if (!b || b->number != bp_num)
13358 error (_("Bad breakpoint number '%d'"), bp_num);
13359
13360 if (loc_num == 0)
13361 error (_("Bad breakpoint location number '%d'"), loc_num);
13362
13363 int n = 0;
13364 for (bp_location *loc : b->locations ())
13365 if (++n == loc_num)
13366 return loc;
13367
13368 error (_("Bad breakpoint location number '%d'"), loc_num);
13369 }
13370
13371 /* Modes of operation for extract_bp_num. */
13372 enum class extract_bp_kind
13373 {
13374 /* Extracting a breakpoint number. */
13375 bp,
13376
13377 /* Extracting a location number. */
13378 loc,
13379 };
13380
13381 /* Extract a breakpoint or location number (as determined by KIND)
13382 from the string starting at START. TRAILER is a character which
13383 can be found after the number. If you don't want a trailer, use
13384 '\0'. If END_OUT is not NULL, it is set to point after the parsed
13385 string. This always returns a positive integer. */
13386
13387 static int
13388 extract_bp_num (extract_bp_kind kind, const char *start,
13389 int trailer, const char **end_out = NULL)
13390 {
13391 const char *end = start;
13392 int num = get_number_trailer (&end, trailer);
13393 if (num < 0)
13394 error (kind == extract_bp_kind::bp
13395 ? _("Negative breakpoint number '%.*s'")
13396 : _("Negative breakpoint location number '%.*s'"),
13397 int (end - start), start);
13398 if (num == 0)
13399 error (kind == extract_bp_kind::bp
13400 ? _("Bad breakpoint number '%.*s'")
13401 : _("Bad breakpoint location number '%.*s'"),
13402 int (end - start), start);
13403
13404 if (end_out != NULL)
13405 *end_out = end;
13406 return num;
13407 }
13408
13409 /* Extract a breakpoint or location range (as determined by KIND) in
13410 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
13411 representing the (inclusive) range. The returned pair's elements
13412 are always positive integers. */
13413
13414 static std::pair<int, int>
13415 extract_bp_or_bp_range (extract_bp_kind kind,
13416 const std::string &arg,
13417 std::string::size_type arg_offset)
13418 {
13419 std::pair<int, int> range;
13420 const char *bp_loc = &arg[arg_offset];
13421 std::string::size_type dash = arg.find ('-', arg_offset);
13422 if (dash != std::string::npos)
13423 {
13424 /* bp_loc is a range (x-z). */
13425 if (arg.length () == dash + 1)
13426 error (kind == extract_bp_kind::bp
13427 ? _("Bad breakpoint number at or near: '%s'")
13428 : _("Bad breakpoint location number at or near: '%s'"),
13429 bp_loc);
13430
13431 const char *end;
13432 const char *start_first = bp_loc;
13433 const char *start_second = &arg[dash + 1];
13434 range.first = extract_bp_num (kind, start_first, '-');
13435 range.second = extract_bp_num (kind, start_second, '\0', &end);
13436
13437 if (range.first > range.second)
13438 error (kind == extract_bp_kind::bp
13439 ? _("Inverted breakpoint range at '%.*s'")
13440 : _("Inverted breakpoint location range at '%.*s'"),
13441 int (end - start_first), start_first);
13442 }
13443 else
13444 {
13445 /* bp_loc is a single value. */
13446 range.first = extract_bp_num (kind, bp_loc, '\0');
13447 range.second = range.first;
13448 }
13449 return range;
13450 }
13451
13452 /* Extract the breakpoint/location range specified by ARG. Returns
13453 the breakpoint range in BP_NUM_RANGE, and the location range in
13454 BP_LOC_RANGE.
13455
13456 ARG may be in any of the following forms:
13457
13458 x where 'x' is a breakpoint number.
13459 x-y where 'x' and 'y' specify a breakpoint numbers range.
13460 x.y where 'x' is a breakpoint number and 'y' a location number.
13461 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
13462 location number range.
13463 */
13464
13465 static void
13466 extract_bp_number_and_location (const std::string &arg,
13467 std::pair<int, int> &bp_num_range,
13468 std::pair<int, int> &bp_loc_range)
13469 {
13470 std::string::size_type dot = arg.find ('.');
13471
13472 if (dot != std::string::npos)
13473 {
13474 /* Handle 'x.y' and 'x.y-z' cases. */
13475
13476 if (arg.length () == dot + 1 || dot == 0)
13477 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
13478
13479 bp_num_range.first
13480 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
13481 bp_num_range.second = bp_num_range.first;
13482
13483 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
13484 arg, dot + 1);
13485 }
13486 else
13487 {
13488 /* Handle x and x-y cases. */
13489
13490 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
13491 bp_loc_range.first = 0;
13492 bp_loc_range.second = 0;
13493 }
13494 }
13495
13496 /* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
13497 specifies whether to enable or disable. */
13498
13499 static void
13500 enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
13501 {
13502 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
13503 if (loc != NULL)
13504 {
13505 if (loc->disabled_by_cond && enable)
13506 error (_("Breakpoint %d's condition is invalid at location %d, "
13507 "cannot enable."), bp_num, loc_num);
13508
13509 if (loc->enabled != enable)
13510 {
13511 loc->enabled = enable;
13512 mark_breakpoint_location_modified (loc);
13513 }
13514 if (target_supports_enable_disable_tracepoint ()
13515 && current_trace_status ()->running && loc->owner
13516 && is_tracepoint (loc->owner))
13517 target_disable_tracepoint (loc);
13518 }
13519 update_global_location_list (UGLL_DONT_INSERT);
13520
13521 gdb::observers::breakpoint_modified.notify (loc->owner);
13522 }
13523
13524 /* Enable or disable a range of breakpoint locations. BP_NUM is the
13525 number of the breakpoint, and BP_LOC_RANGE specifies the
13526 (inclusive) range of location numbers of that breakpoint to
13527 enable/disable. ENABLE specifies whether to enable or disable the
13528 location. */
13529
13530 static void
13531 enable_disable_breakpoint_location_range (int bp_num,
13532 std::pair<int, int> &bp_loc_range,
13533 bool enable)
13534 {
13535 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
13536 enable_disable_bp_num_loc (bp_num, i, enable);
13537 }
13538
13539 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13540 If from_tty is nonzero, it prints a message to that effect,
13541 which ends with a period (no newline). */
13542
13543 void
13544 disable_breakpoint (struct breakpoint *bpt)
13545 {
13546 /* Never disable a watchpoint scope breakpoint; we want to
13547 hit them when we leave scope so we can delete both the
13548 watchpoint and its scope breakpoint at that time. */
13549 if (bpt->type == bp_watchpoint_scope)
13550 return;
13551
13552 bpt->enable_state = bp_disabled;
13553
13554 /* Mark breakpoint locations modified. */
13555 mark_breakpoint_modified (bpt);
13556
13557 if (target_supports_enable_disable_tracepoint ()
13558 && current_trace_status ()->running && is_tracepoint (bpt))
13559 {
13560 for (bp_location *location : bpt->locations ())
13561 target_disable_tracepoint (location);
13562 }
13563
13564 update_global_location_list (UGLL_DONT_INSERT);
13565
13566 gdb::observers::breakpoint_modified.notify (bpt);
13567 }
13568
13569 /* Enable or disable the breakpoint(s) or breakpoint location(s)
13570 specified in ARGS. ARGS may be in any of the formats handled by
13571 extract_bp_number_and_location. ENABLE specifies whether to enable
13572 or disable the breakpoints/locations. */
13573
13574 static void
13575 enable_disable_command (const char *args, int from_tty, bool enable)
13576 {
13577 if (args == 0)
13578 {
13579 for (breakpoint *bpt : all_breakpoints ())
13580 if (user_breakpoint_p (bpt))
13581 {
13582 if (enable)
13583 enable_breakpoint (bpt);
13584 else
13585 disable_breakpoint (bpt);
13586 }
13587 }
13588 else
13589 {
13590 std::string num = extract_arg (&args);
13591
13592 while (!num.empty ())
13593 {
13594 std::pair<int, int> bp_num_range, bp_loc_range;
13595
13596 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
13597
13598 if (bp_loc_range.first == bp_loc_range.second
13599 && bp_loc_range.first == 0)
13600 {
13601 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
13602 map_breakpoint_number_range (bp_num_range,
13603 enable
13604 ? enable_breakpoint
13605 : disable_breakpoint);
13606 }
13607 else
13608 {
13609 /* Handle breakpoint ids with formats 'x.y' or
13610 'x.y-z'. */
13611 enable_disable_breakpoint_location_range
13612 (bp_num_range.first, bp_loc_range, enable);
13613 }
13614 num = extract_arg (&args);
13615 }
13616 }
13617 }
13618
13619 /* The disable command disables the specified breakpoints/locations
13620 (or all defined breakpoints) so they're no longer effective in
13621 stopping the inferior. ARGS may be in any of the forms defined in
13622 extract_bp_number_and_location. */
13623
13624 static void
13625 disable_command (const char *args, int from_tty)
13626 {
13627 enable_disable_command (args, from_tty, false);
13628 }
13629
13630 static void
13631 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
13632 int count)
13633 {
13634 int target_resources_ok;
13635
13636 if (bpt->type == bp_hardware_breakpoint)
13637 {
13638 int i;
13639 i = hw_breakpoint_used_count ();
13640 target_resources_ok =
13641 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
13642 i + 1, 0);
13643 if (target_resources_ok == 0)
13644 error (_("No hardware breakpoint support in the target."));
13645 else if (target_resources_ok < 0)
13646 error (_("Hardware breakpoints used exceeds limit."));
13647 }
13648
13649 if (is_watchpoint (bpt))
13650 {
13651 /* Initialize it just to avoid a GCC false warning. */
13652 enum enable_state orig_enable_state = bp_disabled;
13653
13654 try
13655 {
13656 struct watchpoint *w = (struct watchpoint *) bpt;
13657
13658 orig_enable_state = bpt->enable_state;
13659 bpt->enable_state = bp_enabled;
13660 update_watchpoint (w, 1 /* reparse */);
13661 }
13662 catch (const gdb_exception &e)
13663 {
13664 bpt->enable_state = orig_enable_state;
13665 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
13666 bpt->number);
13667 return;
13668 }
13669 }
13670
13671 bpt->enable_state = bp_enabled;
13672
13673 /* Mark breakpoint locations modified. */
13674 mark_breakpoint_modified (bpt);
13675
13676 if (target_supports_enable_disable_tracepoint ()
13677 && current_trace_status ()->running && is_tracepoint (bpt))
13678 {
13679 for (bp_location *location : bpt->locations ())
13680 target_enable_tracepoint (location);
13681 }
13682
13683 bpt->disposition = disposition;
13684 bpt->enable_count = count;
13685 update_global_location_list (UGLL_MAY_INSERT);
13686
13687 gdb::observers::breakpoint_modified.notify (bpt);
13688 }
13689
13690
13691 void
13692 enable_breakpoint (struct breakpoint *bpt)
13693 {
13694 enable_breakpoint_disp (bpt, bpt->disposition, 0);
13695 }
13696
13697 /* The enable command enables the specified breakpoints/locations (or
13698 all defined breakpoints) so they once again become (or continue to
13699 be) effective in stopping the inferior. ARGS may be in any of the
13700 forms defined in extract_bp_number_and_location. */
13701
13702 static void
13703 enable_command (const char *args, int from_tty)
13704 {
13705 enable_disable_command (args, from_tty, true);
13706 }
13707
13708 static void
13709 enable_once_command (const char *args, int from_tty)
13710 {
13711 map_breakpoint_numbers
13712 (args, [&] (breakpoint *b)
13713 {
13714 iterate_over_related_breakpoints
13715 (b, [&] (breakpoint *bpt)
13716 {
13717 enable_breakpoint_disp (bpt, disp_disable, 1);
13718 });
13719 });
13720 }
13721
13722 static void
13723 enable_count_command (const char *args, int from_tty)
13724 {
13725 int count;
13726
13727 if (args == NULL)
13728 error_no_arg (_("hit count"));
13729
13730 count = get_number (&args);
13731
13732 map_breakpoint_numbers
13733 (args, [&] (breakpoint *b)
13734 {
13735 iterate_over_related_breakpoints
13736 (b, [&] (breakpoint *bpt)
13737 {
13738 enable_breakpoint_disp (bpt, disp_disable, count);
13739 });
13740 });
13741 }
13742
13743 static void
13744 enable_delete_command (const char *args, int from_tty)
13745 {
13746 map_breakpoint_numbers
13747 (args, [&] (breakpoint *b)
13748 {
13749 iterate_over_related_breakpoints
13750 (b, [&] (breakpoint *bpt)
13751 {
13752 enable_breakpoint_disp (bpt, disp_del, 1);
13753 });
13754 });
13755 }
13756 \f
13757 /* Invalidate last known value of any hardware watchpoint if
13758 the memory which that value represents has been written to by
13759 GDB itself. */
13760
13761 static void
13762 invalidate_bp_value_on_memory_change (struct inferior *inferior,
13763 CORE_ADDR addr, ssize_t len,
13764 const bfd_byte *data)
13765 {
13766 for (breakpoint *bp : all_breakpoints ())
13767 if (bp->enable_state == bp_enabled
13768 && bp->type == bp_hardware_watchpoint)
13769 {
13770 struct watchpoint *wp = (struct watchpoint *) bp;
13771
13772 if (wp->val_valid && wp->val != nullptr)
13773 {
13774 for (bp_location *loc : bp->locations ())
13775 if (loc->loc_type == bp_loc_hardware_watchpoint
13776 && loc->address + loc->length > addr
13777 && addr + len > loc->address)
13778 {
13779 wp->val = NULL;
13780 wp->val_valid = false;
13781 }
13782 }
13783 }
13784 }
13785
13786 /* Create and insert a breakpoint for software single step. */
13787
13788 void
13789 insert_single_step_breakpoint (struct gdbarch *gdbarch,
13790 const address_space *aspace,
13791 CORE_ADDR next_pc)
13792 {
13793 struct thread_info *tp = inferior_thread ();
13794 struct symtab_and_line sal;
13795 CORE_ADDR pc = next_pc;
13796
13797 if (tp->control.single_step_breakpoints == NULL)
13798 {
13799 tp->control.single_step_breakpoints
13800 = new_single_step_breakpoint (tp->global_num, gdbarch);
13801 }
13802
13803 sal = find_pc_line (pc, 0);
13804 sal.pc = pc;
13805 sal.section = find_pc_overlay (pc);
13806 sal.explicit_pc = 1;
13807 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
13808
13809 update_global_location_list (UGLL_INSERT);
13810 }
13811
13812 /* Insert single step breakpoints according to the current state. */
13813
13814 int
13815 insert_single_step_breakpoints (struct gdbarch *gdbarch)
13816 {
13817 struct regcache *regcache = get_current_regcache ();
13818 std::vector<CORE_ADDR> next_pcs;
13819
13820 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
13821
13822 if (!next_pcs.empty ())
13823 {
13824 struct frame_info *frame = get_current_frame ();
13825 const address_space *aspace = get_frame_address_space (frame);
13826
13827 for (CORE_ADDR pc : next_pcs)
13828 insert_single_step_breakpoint (gdbarch, aspace, pc);
13829
13830 return 1;
13831 }
13832 else
13833 return 0;
13834 }
13835
13836 /* See breakpoint.h. */
13837
13838 int
13839 breakpoint_has_location_inserted_here (struct breakpoint *bp,
13840 const address_space *aspace,
13841 CORE_ADDR pc)
13842 {
13843 for (bp_location *loc : bp->locations ())
13844 if (loc->inserted
13845 && breakpoint_location_address_match (loc, aspace, pc))
13846 return 1;
13847
13848 return 0;
13849 }
13850
13851 /* Check whether a software single-step breakpoint is inserted at
13852 PC. */
13853
13854 int
13855 single_step_breakpoint_inserted_here_p (const address_space *aspace,
13856 CORE_ADDR pc)
13857 {
13858 for (breakpoint *bpt : all_breakpoints ())
13859 {
13860 if (bpt->type == bp_single_step
13861 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
13862 return 1;
13863 }
13864 return 0;
13865 }
13866
13867 /* Tracepoint-specific operations. */
13868
13869 /* Set tracepoint count to NUM. */
13870 static void
13871 set_tracepoint_count (int num)
13872 {
13873 tracepoint_count = num;
13874 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
13875 }
13876
13877 static void
13878 trace_command (const char *arg, int from_tty)
13879 {
13880 event_location_up location = string_to_event_location (&arg,
13881 current_language);
13882 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
13883 (location.get (), true /* is_tracepoint */);
13884
13885 create_breakpoint (get_current_arch (),
13886 location.get (),
13887 NULL, 0, arg, false, 1 /* parse arg */,
13888 0 /* tempflag */,
13889 bp_tracepoint /* type_wanted */,
13890 0 /* Ignore count */,
13891 pending_break_support,
13892 ops,
13893 from_tty,
13894 1 /* enabled */,
13895 0 /* internal */, 0);
13896 }
13897
13898 static void
13899 ftrace_command (const char *arg, int from_tty)
13900 {
13901 event_location_up location = string_to_event_location (&arg,
13902 current_language);
13903 create_breakpoint (get_current_arch (),
13904 location.get (),
13905 NULL, 0, arg, false, 1 /* parse arg */,
13906 0 /* tempflag */,
13907 bp_fast_tracepoint /* type_wanted */,
13908 0 /* Ignore count */,
13909 pending_break_support,
13910 &vtable_breakpoint_ops,
13911 from_tty,
13912 1 /* enabled */,
13913 0 /* internal */, 0);
13914 }
13915
13916 /* strace command implementation. Creates a static tracepoint. */
13917
13918 static void
13919 strace_command (const char *arg, int from_tty)
13920 {
13921 struct breakpoint_ops *ops;
13922 event_location_up location;
13923
13924 /* Decide if we are dealing with a static tracepoint marker (`-m'),
13925 or with a normal static tracepoint. */
13926 if (arg && startswith (arg, "-m") && isspace (arg[2]))
13927 {
13928 ops = &strace_marker_breakpoint_ops;
13929 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
13930 }
13931 else
13932 {
13933 ops = &vtable_breakpoint_ops;
13934 location = string_to_event_location (&arg, current_language);
13935 }
13936
13937 create_breakpoint (get_current_arch (),
13938 location.get (),
13939 NULL, 0, arg, false, 1 /* parse arg */,
13940 0 /* tempflag */,
13941 bp_static_tracepoint /* type_wanted */,
13942 0 /* Ignore count */,
13943 pending_break_support,
13944 ops,
13945 from_tty,
13946 1 /* enabled */,
13947 0 /* internal */, 0);
13948 }
13949
13950 /* Set up a fake reader function that gets command lines from a linked
13951 list that was acquired during tracepoint uploading. */
13952
13953 static struct uploaded_tp *this_utp;
13954 static int next_cmd;
13955
13956 static char *
13957 read_uploaded_action (void)
13958 {
13959 char *rslt = nullptr;
13960
13961 if (next_cmd < this_utp->cmd_strings.size ())
13962 {
13963 rslt = this_utp->cmd_strings[next_cmd].get ();
13964 next_cmd++;
13965 }
13966
13967 return rslt;
13968 }
13969
13970 /* Given information about a tracepoint as recorded on a target (which
13971 can be either a live system or a trace file), attempt to create an
13972 equivalent GDB tracepoint. This is not a reliable process, since
13973 the target does not necessarily have all the information used when
13974 the tracepoint was originally defined. */
13975
13976 struct tracepoint *
13977 create_tracepoint_from_upload (struct uploaded_tp *utp)
13978 {
13979 const char *addr_str;
13980 char small_buf[100];
13981 struct tracepoint *tp;
13982
13983 if (utp->at_string)
13984 addr_str = utp->at_string.get ();
13985 else
13986 {
13987 /* In the absence of a source location, fall back to raw
13988 address. Since there is no way to confirm that the address
13989 means the same thing as when the trace was started, warn the
13990 user. */
13991 warning (_("Uploaded tracepoint %d has no "
13992 "source location, using raw address"),
13993 utp->number);
13994 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
13995 addr_str = small_buf;
13996 }
13997
13998 /* There's not much we can do with a sequence of bytecodes. */
13999 if (utp->cond && !utp->cond_string)
14000 warning (_("Uploaded tracepoint %d condition "
14001 "has no source form, ignoring it"),
14002 utp->number);
14003
14004 event_location_up location = string_to_event_location (&addr_str,
14005 current_language);
14006 if (!create_breakpoint (get_current_arch (),
14007 location.get (),
14008 utp->cond_string.get (), -1, addr_str,
14009 false /* force_condition */,
14010 0 /* parse cond/thread */,
14011 0 /* tempflag */,
14012 utp->type /* type_wanted */,
14013 0 /* Ignore count */,
14014 pending_break_support,
14015 &vtable_breakpoint_ops,
14016 0 /* from_tty */,
14017 utp->enabled /* enabled */,
14018 0 /* internal */,
14019 CREATE_BREAKPOINT_FLAGS_INSERTED))
14020 return NULL;
14021
14022 /* Get the tracepoint we just created. */
14023 tp = get_tracepoint (tracepoint_count);
14024 gdb_assert (tp != NULL);
14025
14026 if (utp->pass > 0)
14027 {
14028 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
14029 tp->number);
14030
14031 trace_pass_command (small_buf, 0);
14032 }
14033
14034 /* If we have uploaded versions of the original commands, set up a
14035 special-purpose "reader" function and call the usual command line
14036 reader, then pass the result to the breakpoint command-setting
14037 function. */
14038 if (!utp->cmd_strings.empty ())
14039 {
14040 counted_command_line cmd_list;
14041
14042 this_utp = utp;
14043 next_cmd = 0;
14044
14045 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
14046
14047 breakpoint_set_commands (tp, std::move (cmd_list));
14048 }
14049 else if (!utp->actions.empty ()
14050 || !utp->step_actions.empty ())
14051 warning (_("Uploaded tracepoint %d actions "
14052 "have no source form, ignoring them"),
14053 utp->number);
14054
14055 /* Copy any status information that might be available. */
14056 tp->hit_count = utp->hit_count;
14057 tp->traceframe_usage = utp->traceframe_usage;
14058
14059 return tp;
14060 }
14061
14062 /* Print information on tracepoint number TPNUM_EXP, or all if
14063 omitted. */
14064
14065 static void
14066 info_tracepoints_command (const char *args, int from_tty)
14067 {
14068 struct ui_out *uiout = current_uiout;
14069 int num_printed;
14070
14071 num_printed = breakpoint_1 (args, false, is_tracepoint);
14072
14073 if (num_printed == 0)
14074 {
14075 if (args == NULL || *args == '\0')
14076 uiout->message ("No tracepoints.\n");
14077 else
14078 uiout->message ("No tracepoint matching '%s'.\n", args);
14079 }
14080
14081 default_collect_info ();
14082 }
14083
14084 /* The 'enable trace' command enables tracepoints.
14085 Not supported by all targets. */
14086 static void
14087 enable_trace_command (const char *args, int from_tty)
14088 {
14089 enable_command (args, from_tty);
14090 }
14091
14092 /* The 'disable trace' command disables tracepoints.
14093 Not supported by all targets. */
14094 static void
14095 disable_trace_command (const char *args, int from_tty)
14096 {
14097 disable_command (args, from_tty);
14098 }
14099
14100 /* Remove a tracepoint (or all if no argument). */
14101 static void
14102 delete_trace_command (const char *arg, int from_tty)
14103 {
14104 dont_repeat ();
14105
14106 if (arg == 0)
14107 {
14108 int breaks_to_delete = 0;
14109
14110 /* Delete all breakpoints if no argument.
14111 Do not delete internal or call-dummy breakpoints, these
14112 have to be deleted with an explicit breakpoint number
14113 argument. */
14114 for (breakpoint *tp : all_tracepoints ())
14115 if (is_tracepoint (tp) && user_breakpoint_p (tp))
14116 {
14117 breaks_to_delete = 1;
14118 break;
14119 }
14120
14121 /* Ask user only if there are some breakpoints to delete. */
14122 if (!from_tty
14123 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14124 {
14125 for (breakpoint *b : all_breakpoints_safe ())
14126 if (is_tracepoint (b) && user_breakpoint_p (b))
14127 delete_breakpoint (b);
14128 }
14129 }
14130 else
14131 map_breakpoint_numbers
14132 (arg, [&] (breakpoint *br)
14133 {
14134 iterate_over_related_breakpoints (br, delete_breakpoint);
14135 });
14136 }
14137
14138 /* Helper function for trace_pass_command. */
14139
14140 static void
14141 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14142 {
14143 tp->pass_count = count;
14144 gdb::observers::breakpoint_modified.notify (tp);
14145 if (from_tty)
14146 gdb_printf (_("Setting tracepoint %d's passcount to %d\n"),
14147 tp->number, count);
14148 }
14149
14150 /* Set passcount for tracepoint.
14151
14152 First command argument is passcount, second is tracepoint number.
14153 If tracepoint number omitted, apply to most recently defined.
14154 Also accepts special argument "all". */
14155
14156 static void
14157 trace_pass_command (const char *args, int from_tty)
14158 {
14159 struct tracepoint *t1;
14160 ULONGEST count;
14161
14162 if (args == 0 || *args == 0)
14163 error (_("passcount command requires an "
14164 "argument (count + optional TP num)"));
14165
14166 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
14167
14168 args = skip_spaces (args);
14169 if (*args && strncasecmp (args, "all", 3) == 0)
14170 {
14171 args += 3; /* Skip special argument "all". */
14172 if (*args)
14173 error (_("Junk at end of arguments."));
14174
14175 for (breakpoint *b : all_tracepoints ())
14176 {
14177 t1 = (struct tracepoint *) b;
14178 trace_pass_set_count (t1, count, from_tty);
14179 }
14180 }
14181 else if (*args == '\0')
14182 {
14183 t1 = get_tracepoint_by_number (&args, NULL);
14184 if (t1)
14185 trace_pass_set_count (t1, count, from_tty);
14186 }
14187 else
14188 {
14189 number_or_range_parser parser (args);
14190 while (!parser.finished ())
14191 {
14192 t1 = get_tracepoint_by_number (&args, &parser);
14193 if (t1)
14194 trace_pass_set_count (t1, count, from_tty);
14195 }
14196 }
14197 }
14198
14199 struct tracepoint *
14200 get_tracepoint (int num)
14201 {
14202 for (breakpoint *t : all_tracepoints ())
14203 if (t->number == num)
14204 return (struct tracepoint *) t;
14205
14206 return NULL;
14207 }
14208
14209 /* Find the tracepoint with the given target-side number (which may be
14210 different from the tracepoint number after disconnecting and
14211 reconnecting). */
14212
14213 struct tracepoint *
14214 get_tracepoint_by_number_on_target (int num)
14215 {
14216 for (breakpoint *b : all_tracepoints ())
14217 {
14218 struct tracepoint *t = (struct tracepoint *) b;
14219
14220 if (t->number_on_target == num)
14221 return t;
14222 }
14223
14224 return NULL;
14225 }
14226
14227 /* Utility: parse a tracepoint number and look it up in the list.
14228 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14229 If the argument is missing, the most recent tracepoint
14230 (tracepoint_count) is returned. */
14231
14232 struct tracepoint *
14233 get_tracepoint_by_number (const char **arg,
14234 number_or_range_parser *parser)
14235 {
14236 int tpnum;
14237 const char *instring = arg == NULL ? NULL : *arg;
14238
14239 if (parser != NULL)
14240 {
14241 gdb_assert (!parser->finished ());
14242 tpnum = parser->get_number ();
14243 }
14244 else if (arg == NULL || *arg == NULL || ! **arg)
14245 tpnum = tracepoint_count;
14246 else
14247 tpnum = get_number (arg);
14248
14249 if (tpnum <= 0)
14250 {
14251 if (instring && *instring)
14252 gdb_printf (_("bad tracepoint number at or near '%s'\n"),
14253 instring);
14254 else
14255 gdb_printf (_("No previous tracepoint\n"));
14256 return NULL;
14257 }
14258
14259 for (breakpoint *t : all_tracepoints ())
14260 if (t->number == tpnum)
14261 return (struct tracepoint *) t;
14262
14263 gdb_printf ("No tracepoint number %d.\n", tpnum);
14264 return NULL;
14265 }
14266
14267 void
14268 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14269 {
14270 if (b->thread != -1)
14271 gdb_printf (fp, " thread %d", b->thread);
14272
14273 if (b->task != 0)
14274 gdb_printf (fp, " task %d", b->task);
14275
14276 gdb_printf (fp, "\n");
14277 }
14278
14279 /* Save information on user settable breakpoints (watchpoints, etc) to
14280 a new script file named FILENAME. If FILTER is non-NULL, call it
14281 on each breakpoint and only include the ones for which it returns
14282 true. */
14283
14284 static void
14285 save_breakpoints (const char *filename, int from_tty,
14286 bool (*filter) (const struct breakpoint *))
14287 {
14288 int any = 0;
14289 int extra_trace_bits = 0;
14290
14291 if (filename == 0 || *filename == 0)
14292 error (_("Argument required (file name in which to save)"));
14293
14294 /* See if we have anything to save. */
14295 for (breakpoint *tp : all_breakpoints ())
14296 {
14297 /* Skip internal and momentary breakpoints. */
14298 if (!user_breakpoint_p (tp))
14299 continue;
14300
14301 /* If we have a filter, only save the breakpoints it accepts. */
14302 if (filter && !filter (tp))
14303 continue;
14304
14305 any = 1;
14306
14307 if (is_tracepoint (tp))
14308 {
14309 extra_trace_bits = 1;
14310
14311 /* We can stop searching. */
14312 break;
14313 }
14314 }
14315
14316 if (!any)
14317 {
14318 warning (_("Nothing to save."));
14319 return;
14320 }
14321
14322 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
14323
14324 stdio_file fp;
14325
14326 if (!fp.open (expanded_filename.get (), "w"))
14327 error (_("Unable to open file '%s' for saving (%s)"),
14328 expanded_filename.get (), safe_strerror (errno));
14329
14330 if (extra_trace_bits)
14331 save_trace_state_variables (&fp);
14332
14333 for (breakpoint *tp : all_breakpoints ())
14334 {
14335 /* Skip internal and momentary breakpoints. */
14336 if (!user_breakpoint_p (tp))
14337 continue;
14338
14339 /* If we have a filter, only save the breakpoints it accepts. */
14340 if (filter && !filter (tp))
14341 continue;
14342
14343 tp->ops->print_recreate (tp, &fp);
14344
14345 /* Note, we can't rely on tp->number for anything, as we can't
14346 assume the recreated breakpoint numbers will match. Use $bpnum
14347 instead. */
14348
14349 if (tp->cond_string)
14350 fp.printf (" condition $bpnum %s\n", tp->cond_string.get ());
14351
14352 if (tp->ignore_count)
14353 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
14354
14355 if (tp->type != bp_dprintf && tp->commands)
14356 {
14357 fp.puts (" commands\n");
14358
14359 current_uiout->redirect (&fp);
14360 try
14361 {
14362 print_command_lines (current_uiout, tp->commands.get (), 2);
14363 }
14364 catch (const gdb_exception &ex)
14365 {
14366 current_uiout->redirect (NULL);
14367 throw;
14368 }
14369
14370 current_uiout->redirect (NULL);
14371 fp.puts (" end\n");
14372 }
14373
14374 if (tp->enable_state == bp_disabled)
14375 fp.puts ("disable $bpnum\n");
14376
14377 /* If this is a multi-location breakpoint, check if the locations
14378 should be individually disabled. Watchpoint locations are
14379 special, and not user visible. */
14380 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
14381 {
14382 int n = 1;
14383
14384 for (bp_location *loc : tp->locations ())
14385 {
14386 if (!loc->enabled)
14387 fp.printf ("disable $bpnum.%d\n", n);
14388
14389 n++;
14390 }
14391 }
14392 }
14393
14394 if (extra_trace_bits && !default_collect.empty ())
14395 fp.printf ("set default-collect %s\n", default_collect.c_str ());
14396
14397 if (from_tty)
14398 gdb_printf (_("Saved to file '%s'.\n"), expanded_filename.get ());
14399 }
14400
14401 /* The `save breakpoints' command. */
14402
14403 static void
14404 save_breakpoints_command (const char *args, int from_tty)
14405 {
14406 save_breakpoints (args, from_tty, NULL);
14407 }
14408
14409 /* The `save tracepoints' command. */
14410
14411 static void
14412 save_tracepoints_command (const char *args, int from_tty)
14413 {
14414 save_breakpoints (args, from_tty, is_tracepoint);
14415 }
14416
14417 \f
14418 /* This help string is used to consolidate all the help string for specifying
14419 locations used by several commands. */
14420
14421 #define LOCATION_HELP_STRING \
14422 "Linespecs are colon-separated lists of location parameters, such as\n\
14423 source filename, function name, label name, and line number.\n\
14424 Example: To specify the start of a label named \"the_top\" in the\n\
14425 function \"fact\" in the file \"factorial.c\", use\n\
14426 \"factorial.c:fact:the_top\".\n\
14427 \n\
14428 Address locations begin with \"*\" and specify an exact address in the\n\
14429 program. Example: To specify the fourth byte past the start function\n\
14430 \"main\", use \"*main + 4\".\n\
14431 \n\
14432 Explicit locations are similar to linespecs but use an option/argument\n\
14433 syntax to specify location parameters.\n\
14434 Example: To specify the start of the label named \"the_top\" in the\n\
14435 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
14436 -function fact -label the_top\".\n\
14437 \n\
14438 By default, a specified function is matched against the program's\n\
14439 functions in all scopes. For C++, this means in all namespaces and\n\
14440 classes. For Ada, this means in all packages. E.g., in C++,\n\
14441 \"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
14442 \"-qualified\" flag overrides this behavior, making GDB interpret the\n\
14443 specified name as a complete fully-qualified name instead."
14444
14445 /* This help string is used for the break, hbreak, tbreak and thbreak
14446 commands. It is defined as a macro to prevent duplication.
14447 COMMAND should be a string constant containing the name of the
14448 command. */
14449
14450 #define BREAK_ARGS_HELP(command) \
14451 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
14452 \t[-force-condition] [if CONDITION]\n\
14453 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
14454 probe point. Accepted values are `-probe' (for a generic, automatically\n\
14455 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
14456 `-probe-dtrace' (for a DTrace probe).\n\
14457 LOCATION may be a linespec, address, or explicit location as described\n\
14458 below.\n\
14459 \n\
14460 With no LOCATION, uses current execution address of the selected\n\
14461 stack frame. This is useful for breaking on return to a stack frame.\n\
14462 \n\
14463 THREADNUM is the number from \"info threads\".\n\
14464 CONDITION is a boolean expression.\n\
14465 \n\
14466 With the \"-force-condition\" flag, the condition is defined even when\n\
14467 it is invalid for all current locations.\n\
14468 \n" LOCATION_HELP_STRING "\n\n\
14469 Multiple breakpoints at one place are permitted, and useful if their\n\
14470 conditions are different.\n\
14471 \n\
14472 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
14473
14474 /* List of subcommands for "catch". */
14475 static struct cmd_list_element *catch_cmdlist;
14476
14477 /* List of subcommands for "tcatch". */
14478 static struct cmd_list_element *tcatch_cmdlist;
14479
14480 void
14481 add_catch_command (const char *name, const char *docstring,
14482 cmd_func_ftype *func,
14483 completer_ftype *completer,
14484 void *user_data_catch,
14485 void *user_data_tcatch)
14486 {
14487 struct cmd_list_element *command;
14488
14489 command = add_cmd (name, class_breakpoint, docstring,
14490 &catch_cmdlist);
14491 command->func = func;
14492 command->set_context (user_data_catch);
14493 set_cmd_completer (command, completer);
14494
14495 command = add_cmd (name, class_breakpoint, docstring,
14496 &tcatch_cmdlist);
14497 command->func = func;
14498 command->set_context (user_data_tcatch);
14499 set_cmd_completer (command, completer);
14500 }
14501
14502 /* Zero if any of the breakpoint's locations could be a location where
14503 functions have been inlined, nonzero otherwise. */
14504
14505 static int
14506 is_non_inline_function (struct breakpoint *b)
14507 {
14508 /* The shared library event breakpoint is set on the address of a
14509 non-inline function. */
14510 if (b->type == bp_shlib_event)
14511 return 1;
14512
14513 return 0;
14514 }
14515
14516 /* Nonzero if the specified PC cannot be a location where functions
14517 have been inlined. */
14518
14519 int
14520 pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
14521 const target_waitstatus &ws)
14522 {
14523 for (breakpoint *b : all_breakpoints ())
14524 {
14525 if (!is_non_inline_function (b))
14526 continue;
14527
14528 for (bp_location *bl : b->locations ())
14529 {
14530 if (!bl->shlib_disabled
14531 && bpstat_check_location (bl, aspace, pc, ws))
14532 return 1;
14533 }
14534 }
14535
14536 return 0;
14537 }
14538
14539 /* Remove any references to OBJFILE which is going to be freed. */
14540
14541 void
14542 breakpoint_free_objfile (struct objfile *objfile)
14543 {
14544 for (bp_location *loc : all_bp_locations ())
14545 if (loc->symtab != NULL && loc->symtab->compunit ()->objfile () == objfile)
14546 loc->symtab = NULL;
14547 }
14548
14549 void
14550 initialize_breakpoint_ops (void)
14551 {
14552 static int initialized = 0;
14553
14554 struct breakpoint_ops *ops;
14555
14556 if (initialized)
14557 return;
14558 initialized = 1;
14559
14560 /* The breakpoint_ops structure to be inherit by all kinds of
14561 breakpoints (real breakpoints, i.e., user "break" breakpoints,
14562 internal and momentary breakpoints, etc.). */
14563 ops = &bkpt_base_breakpoint_ops;
14564 *ops = base_breakpoint_ops;
14565 ops->re_set = bkpt_re_set;
14566 ops->insert_location = bkpt_insert_location;
14567 ops->remove_location = bkpt_remove_location;
14568 ops->breakpoint_hit = bkpt_breakpoint_hit;
14569 ops->create_sals_from_location = create_sals_from_location_default;
14570 ops->create_breakpoints_sal = create_breakpoints_sal_default;
14571 ops->decode_location = bkpt_decode_location;
14572
14573 /* The breakpoint_ops structure to be used in regular breakpoints. */
14574 ops = &bkpt_breakpoint_ops;
14575 *ops = bkpt_base_breakpoint_ops;
14576 ops->re_set = bkpt_re_set;
14577 ops->resources_needed = bkpt_resources_needed;
14578 ops->print_it = bkpt_print_it;
14579 ops->print_mention = bkpt_print_mention;
14580 ops->print_recreate = bkpt_print_recreate;
14581
14582 /* Ranged breakpoints. */
14583 ops = &ranged_breakpoint_ops;
14584 *ops = bkpt_breakpoint_ops;
14585 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
14586 ops->resources_needed = resources_needed_ranged_breakpoint;
14587 ops->print_it = print_it_ranged_breakpoint;
14588 ops->print_one = print_one_ranged_breakpoint;
14589 ops->print_one_detail = print_one_detail_ranged_breakpoint;
14590 ops->print_mention = print_mention_ranged_breakpoint;
14591 ops->print_recreate = print_recreate_ranged_breakpoint;
14592
14593 /* Internal breakpoints. */
14594 ops = &internal_breakpoint_ops;
14595 *ops = bkpt_base_breakpoint_ops;
14596 ops->re_set = internal_bkpt_re_set;
14597 ops->check_status = internal_bkpt_check_status;
14598 ops->print_it = internal_bkpt_print_it;
14599 ops->print_mention = internal_bkpt_print_mention;
14600
14601 /* Momentary breakpoints. */
14602 ops = &momentary_breakpoint_ops;
14603 *ops = bkpt_base_breakpoint_ops;
14604 ops->re_set = momentary_bkpt_re_set;
14605 ops->check_status = momentary_bkpt_check_status;
14606 ops->print_it = momentary_bkpt_print_it;
14607 ops->print_mention = momentary_bkpt_print_mention;
14608
14609 /* Probe breakpoints. */
14610 ops = &bkpt_probe_breakpoint_ops;
14611 *ops = bkpt_breakpoint_ops;
14612 ops->insert_location = bkpt_probe_insert_location;
14613 ops->remove_location = bkpt_probe_remove_location;
14614 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
14615 ops->decode_location = bkpt_probe_decode_location;
14616
14617 /* Probe tracepoints. */
14618 ops = &tracepoint_probe_breakpoint_ops;
14619 *ops = vtable_breakpoint_ops;
14620 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
14621 ops->decode_location = tracepoint_probe_decode_location;
14622
14623 /* Static tracepoints with marker (`-m'). */
14624 ops = &strace_marker_breakpoint_ops;
14625 *ops = vtable_breakpoint_ops;
14626 ops->create_sals_from_location = strace_marker_create_sals_from_location;
14627 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
14628 ops->decode_location = strace_marker_decode_location;
14629
14630 ops = &dprintf_breakpoint_ops;
14631 *ops = bkpt_base_breakpoint_ops;
14632 ops->re_set = dprintf_re_set;
14633 ops->resources_needed = bkpt_resources_needed;
14634 ops->print_it = bkpt_print_it;
14635 ops->print_mention = bkpt_print_mention;
14636 ops->print_recreate = dprintf_print_recreate;
14637 ops->after_condition_true = dprintf_after_condition_true;
14638 ops->breakpoint_hit = dprintf_breakpoint_hit;
14639 }
14640
14641 /* Chain containing all defined "enable breakpoint" subcommands. */
14642
14643 static struct cmd_list_element *enablebreaklist = NULL;
14644
14645 /* See breakpoint.h. */
14646
14647 cmd_list_element *commands_cmd_element = nullptr;
14648
14649 void _initialize_breakpoint ();
14650 void
14651 _initialize_breakpoint ()
14652 {
14653 struct cmd_list_element *c;
14654
14655 initialize_breakpoint_ops ();
14656
14657 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
14658 "breakpoint");
14659 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
14660 "breakpoint");
14661 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
14662 "breakpoint");
14663
14664 breakpoint_chain = 0;
14665 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
14666 before a breakpoint is set. */
14667 breakpoint_count = 0;
14668
14669 tracepoint_count = 0;
14670
14671 add_com ("ignore", class_breakpoint, ignore_command, _("\
14672 Set ignore-count of breakpoint number N to COUNT.\n\
14673 Usage is `ignore N COUNT'."));
14674
14675 commands_cmd_element = add_com ("commands", class_breakpoint,
14676 commands_command, _("\
14677 Set commands to be executed when the given breakpoints are hit.\n\
14678 Give a space-separated breakpoint list as argument after \"commands\".\n\
14679 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
14680 (e.g. `5-7').\n\
14681 With no argument, the targeted breakpoint is the last one set.\n\
14682 The commands themselves follow starting on the next line.\n\
14683 Type a line containing \"end\" to indicate the end of them.\n\
14684 Give \"silent\" as the first line to make the breakpoint silent;\n\
14685 then no output is printed when it is hit, except what the commands print."));
14686
14687 const auto cc_opts = make_condition_command_options_def_group (nullptr);
14688 static std::string condition_command_help
14689 = gdb::option::build_help (_("\
14690 Specify breakpoint number N to break only if COND is true.\n\
14691 Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
14692 is an expression to be evaluated whenever breakpoint N is reached.\n\
14693 \n\
14694 Options:\n\
14695 %OPTIONS%"), cc_opts);
14696
14697 c = add_com ("condition", class_breakpoint, condition_command,
14698 condition_command_help.c_str ());
14699 set_cmd_completer_handle_brkchars (c, condition_completer);
14700
14701 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
14702 Set a temporary breakpoint.\n\
14703 Like \"break\" except the breakpoint is only temporary,\n\
14704 so it will be deleted when hit. Equivalent to \"break\" followed\n\
14705 by using \"enable delete\" on the breakpoint number.\n\
14706 \n"
14707 BREAK_ARGS_HELP ("tbreak")));
14708 set_cmd_completer (c, location_completer);
14709
14710 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
14711 Set a hardware assisted breakpoint.\n\
14712 Like \"break\" except the breakpoint requires hardware support,\n\
14713 some target hardware may not have this support.\n\
14714 \n"
14715 BREAK_ARGS_HELP ("hbreak")));
14716 set_cmd_completer (c, location_completer);
14717
14718 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
14719 Set a temporary hardware assisted breakpoint.\n\
14720 Like \"hbreak\" except the breakpoint is only temporary,\n\
14721 so it will be deleted when hit.\n\
14722 \n"
14723 BREAK_ARGS_HELP ("thbreak")));
14724 set_cmd_completer (c, location_completer);
14725
14726 cmd_list_element *enable_cmd
14727 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
14728 Enable all or some breakpoints.\n\
14729 Usage: enable [BREAKPOINTNUM]...\n\
14730 Give breakpoint numbers (separated by spaces) as arguments.\n\
14731 With no subcommand, breakpoints are enabled until you command otherwise.\n\
14732 This is used to cancel the effect of the \"disable\" command.\n\
14733 With a subcommand you can enable temporarily."),
14734 &enablelist, 1, &cmdlist);
14735
14736 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
14737
14738 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
14739 Enable all or some breakpoints.\n\
14740 Usage: enable breakpoints [BREAKPOINTNUM]...\n\
14741 Give breakpoint numbers (separated by spaces) as arguments.\n\
14742 This is used to cancel the effect of the \"disable\" command.\n\
14743 May be abbreviated to simply \"enable\"."),
14744 &enablebreaklist, 1, &enablelist);
14745
14746 add_cmd ("once", no_class, enable_once_command, _("\
14747 Enable some breakpoints for one hit.\n\
14748 Usage: enable breakpoints once BREAKPOINTNUM...\n\
14749 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14750 &enablebreaklist);
14751
14752 add_cmd ("delete", no_class, enable_delete_command, _("\
14753 Enable some breakpoints and delete when hit.\n\
14754 Usage: enable breakpoints delete BREAKPOINTNUM...\n\
14755 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14756 &enablebreaklist);
14757
14758 add_cmd ("count", no_class, enable_count_command, _("\
14759 Enable some breakpoints for COUNT hits.\n\
14760 Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
14761 If a breakpoint is hit while enabled in this fashion,\n\
14762 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14763 &enablebreaklist);
14764
14765 add_cmd ("delete", no_class, enable_delete_command, _("\
14766 Enable some breakpoints and delete when hit.\n\
14767 Usage: enable delete BREAKPOINTNUM...\n\
14768 If a breakpoint is hit while enabled in this fashion, it is deleted."),
14769 &enablelist);
14770
14771 add_cmd ("once", no_class, enable_once_command, _("\
14772 Enable some breakpoints for one hit.\n\
14773 Usage: enable once BREAKPOINTNUM...\n\
14774 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
14775 &enablelist);
14776
14777 add_cmd ("count", no_class, enable_count_command, _("\
14778 Enable some breakpoints for COUNT hits.\n\
14779 Usage: enable count COUNT BREAKPOINTNUM...\n\
14780 If a breakpoint is hit while enabled in this fashion,\n\
14781 the count is decremented; when it reaches zero, the breakpoint is disabled."),
14782 &enablelist);
14783
14784 cmd_list_element *disable_cmd
14785 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
14786 Disable all or some breakpoints.\n\
14787 Usage: disable [BREAKPOINTNUM]...\n\
14788 Arguments are breakpoint numbers with spaces in between.\n\
14789 To disable all breakpoints, give no argument.\n\
14790 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
14791 &disablelist, 1, &cmdlist);
14792 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
14793 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
14794
14795 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
14796 Disable all or some breakpoints.\n\
14797 Usage: disable breakpoints [BREAKPOINTNUM]...\n\
14798 Arguments are breakpoint numbers with spaces in between.\n\
14799 To disable all breakpoints, give no argument.\n\
14800 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
14801 This command may be abbreviated \"disable\"."),
14802 &disablelist);
14803
14804 cmd_list_element *delete_cmd
14805 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
14806 Delete all or some breakpoints.\n\
14807 Usage: delete [BREAKPOINTNUM]...\n\
14808 Arguments are breakpoint numbers with spaces in between.\n\
14809 To delete all breakpoints, give no argument.\n\
14810 \n\
14811 Also a prefix command for deletion of other GDB objects."),
14812 &deletelist, 1, &cmdlist);
14813 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
14814 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
14815
14816 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
14817 Delete all or some breakpoints or auto-display expressions.\n\
14818 Usage: delete breakpoints [BREAKPOINTNUM]...\n\
14819 Arguments are breakpoint numbers with spaces in between.\n\
14820 To delete all breakpoints, give no argument.\n\
14821 This command may be abbreviated \"delete\"."),
14822 &deletelist);
14823
14824 cmd_list_element *clear_cmd
14825 = add_com ("clear", class_breakpoint, clear_command, _("\
14826 Clear breakpoint at specified location.\n\
14827 Argument may be a linespec, explicit, or address location as described below.\n\
14828 \n\
14829 With no argument, clears all breakpoints in the line that the selected frame\n\
14830 is executing in.\n"
14831 "\n" LOCATION_HELP_STRING "\n\n\
14832 See also the \"delete\" command which clears breakpoints by number."));
14833 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
14834
14835 cmd_list_element *break_cmd
14836 = add_com ("break", class_breakpoint, break_command, _("\
14837 Set breakpoint at specified location.\n"
14838 BREAK_ARGS_HELP ("break")));
14839 set_cmd_completer (break_cmd, location_completer);
14840
14841 add_com_alias ("b", break_cmd, class_run, 1);
14842 add_com_alias ("br", break_cmd, class_run, 1);
14843 add_com_alias ("bre", break_cmd, class_run, 1);
14844 add_com_alias ("brea", break_cmd, class_run, 1);
14845
14846 cmd_list_element *info_breakpoints_cmd
14847 = add_info ("breakpoints", info_breakpoints_command, _("\
14848 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
14849 The \"Type\" column indicates one of:\n\
14850 \tbreakpoint - normal breakpoint\n\
14851 \twatchpoint - watchpoint\n\
14852 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14853 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14854 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14855 address and file/line number respectively.\n\
14856 \n\
14857 Convenience variable \"$_\" and default examine address for \"x\"\n\
14858 are set to the address of the last breakpoint listed unless the command\n\
14859 is prefixed with \"server \".\n\n\
14860 Convenience variable \"$bpnum\" contains the number of the last\n\
14861 breakpoint set."));
14862
14863 add_info_alias ("b", info_breakpoints_cmd, 1);
14864
14865 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
14866 Status of all breakpoints, or breakpoint number NUMBER.\n\
14867 The \"Type\" column indicates one of:\n\
14868 \tbreakpoint - normal breakpoint\n\
14869 \twatchpoint - watchpoint\n\
14870 \tlongjmp - internal breakpoint used to step through longjmp()\n\
14871 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
14872 \tuntil - internal breakpoint used by the \"until\" command\n\
14873 \tfinish - internal breakpoint used by the \"finish\" command\n\
14874 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
14875 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
14876 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
14877 address and file/line number respectively.\n\
14878 \n\
14879 Convenience variable \"$_\" and default examine address for \"x\"\n\
14880 are set to the address of the last breakpoint listed unless the command\n\
14881 is prefixed with \"server \".\n\n\
14882 Convenience variable \"$bpnum\" contains the number of the last\n\
14883 breakpoint set."),
14884 &maintenanceinfolist);
14885
14886 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
14887 Set catchpoints to catch events."),
14888 &catch_cmdlist,
14889 0/*allow-unknown*/, &cmdlist);
14890
14891 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
14892 Set temporary catchpoints to catch events."),
14893 &tcatch_cmdlist,
14894 0/*allow-unknown*/, &cmdlist);
14895
14896 const auto opts = make_watch_options_def_group (nullptr);
14897
14898 static const std::string watch_help = gdb::option::build_help (_("\
14899 Set a watchpoint for EXPRESSION.\n\
14900 Usage: watch [-location] EXPRESSION\n\
14901 \n\
14902 Options:\n\
14903 %OPTIONS%\n\
14904 \n\
14905 A watchpoint stops execution of your program whenever the value of\n\
14906 an expression changes."), opts);
14907 c = add_com ("watch", class_breakpoint, watch_command,
14908 watch_help.c_str ());
14909 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14910
14911 static const std::string rwatch_help = gdb::option::build_help (_("\
14912 Set a read watchpoint for EXPRESSION.\n\
14913 Usage: rwatch [-location] EXPRESSION\n\
14914 \n\
14915 Options:\n\
14916 %OPTIONS%\n\
14917 \n\
14918 A read watchpoint stops execution of your program whenever the value of\n\
14919 an expression is read."), opts);
14920 c = add_com ("rwatch", class_breakpoint, rwatch_command,
14921 rwatch_help.c_str ());
14922 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14923
14924 static const std::string awatch_help = gdb::option::build_help (_("\
14925 Set an access watchpoint for EXPRESSION.\n\
14926 Usage: awatch [-location] EXPRESSION\n\
14927 \n\
14928 Options:\n\
14929 %OPTIONS%\n\
14930 \n\
14931 An access watchpoint stops execution of your program whenever the value\n\
14932 of an expression is either read or written."), opts);
14933 c = add_com ("awatch", class_breakpoint, awatch_command,
14934 awatch_help.c_str ());
14935 set_cmd_completer_handle_brkchars (c, watch_command_completer);
14936
14937 add_info ("watchpoints", info_watchpoints_command, _("\
14938 Status of specified watchpoints (all watchpoints if no argument)."));
14939
14940 /* XXX: cagney/2005-02-23: This should be a boolean, and should
14941 respond to changes - contrary to the description. */
14942 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
14943 &can_use_hw_watchpoints, _("\
14944 Set debugger's willingness to use watchpoint hardware."), _("\
14945 Show debugger's willingness to use watchpoint hardware."), _("\
14946 If zero, gdb will not use hardware for new watchpoints, even if\n\
14947 such is available. (However, any hardware watchpoints that were\n\
14948 created before setting this to nonzero, will continue to use watchpoint\n\
14949 hardware.)"),
14950 NULL,
14951 show_can_use_hw_watchpoints,
14952 &setlist, &showlist);
14953
14954 can_use_hw_watchpoints = 1;
14955
14956 /* Tracepoint manipulation commands. */
14957
14958 cmd_list_element *trace_cmd
14959 = add_com ("trace", class_breakpoint, trace_command, _("\
14960 Set a tracepoint at specified location.\n\
14961 \n"
14962 BREAK_ARGS_HELP ("trace") "\n\
14963 Do \"help tracepoints\" for info on other tracepoint commands."));
14964 set_cmd_completer (trace_cmd, location_completer);
14965
14966 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
14967 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
14968 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
14969 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
14970
14971 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
14972 Set a fast tracepoint at specified location.\n\
14973 \n"
14974 BREAK_ARGS_HELP ("ftrace") "\n\
14975 Do \"help tracepoints\" for info on other tracepoint commands."));
14976 set_cmd_completer (c, location_completer);
14977
14978 c = add_com ("strace", class_breakpoint, strace_command, _("\
14979 Set a static tracepoint at location or marker.\n\
14980 \n\
14981 strace [LOCATION] [if CONDITION]\n\
14982 LOCATION may be a linespec, explicit, or address location (described below) \n\
14983 or -m MARKER_ID.\n\n\
14984 If a marker id is specified, probe the marker with that name. With\n\
14985 no LOCATION, uses current execution address of the selected stack frame.\n\
14986 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
14987 This collects arbitrary user data passed in the probe point call to the\n\
14988 tracing library. You can inspect it when analyzing the trace buffer,\n\
14989 by printing the $_sdata variable like any other convenience variable.\n\
14990 \n\
14991 CONDITION is a boolean expression.\n\
14992 \n" LOCATION_HELP_STRING "\n\n\
14993 Multiple tracepoints at one place are permitted, and useful if their\n\
14994 conditions are different.\n\
14995 \n\
14996 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
14997 Do \"help tracepoints\" for info on other tracepoint commands."));
14998 set_cmd_completer (c, location_completer);
14999
15000 cmd_list_element *info_tracepoints_cmd
15001 = add_info ("tracepoints", info_tracepoints_command, _("\
15002 Status of specified tracepoints (all tracepoints if no argument).\n\
15003 Convenience variable \"$tpnum\" contains the number of the\n\
15004 last tracepoint set."));
15005
15006 add_info_alias ("tp", info_tracepoints_cmd, 1);
15007
15008 cmd_list_element *delete_tracepoints_cmd
15009 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15010 Delete specified tracepoints.\n\
15011 Arguments are tracepoint numbers, separated by spaces.\n\
15012 No argument means delete all tracepoints."),
15013 &deletelist);
15014 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
15015
15016 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15017 Disable specified tracepoints.\n\
15018 Arguments are tracepoint numbers, separated by spaces.\n\
15019 No argument means disable all tracepoints."),
15020 &disablelist);
15021 deprecate_cmd (c, "disable");
15022
15023 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15024 Enable specified tracepoints.\n\
15025 Arguments are tracepoint numbers, separated by spaces.\n\
15026 No argument means enable all tracepoints."),
15027 &enablelist);
15028 deprecate_cmd (c, "enable");
15029
15030 add_com ("passcount", class_trace, trace_pass_command, _("\
15031 Set the passcount for a tracepoint.\n\
15032 The trace will end when the tracepoint has been passed 'count' times.\n\
15033 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15034 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15035
15036 add_basic_prefix_cmd ("save", class_breakpoint,
15037 _("Save breakpoint definitions as a script."),
15038 &save_cmdlist,
15039 0/*allow-unknown*/, &cmdlist);
15040
15041 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15042 Save current breakpoint definitions as a script.\n\
15043 This includes all types of breakpoints (breakpoints, watchpoints,\n\
15044 catchpoints, tracepoints). Use the 'source' command in another debug\n\
15045 session to restore them."),
15046 &save_cmdlist);
15047 set_cmd_completer (c, filename_completer);
15048
15049 cmd_list_element *save_tracepoints_cmd
15050 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
15051 Save current tracepoint definitions as a script.\n\
15052 Use the 'source' command in another debug session to restore them."),
15053 &save_cmdlist);
15054 set_cmd_completer (save_tracepoints_cmd, filename_completer);
15055
15056 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
15057 deprecate_cmd (c, "save tracepoints");
15058
15059 add_setshow_prefix_cmd ("breakpoint", class_maintenance,
15060 _("\
15061 Breakpoint specific settings.\n\
15062 Configure various breakpoint-specific variables such as\n\
15063 pending breakpoint behavior."),
15064 _("\
15065 Breakpoint specific settings.\n\
15066 Configure various breakpoint-specific variables such as\n\
15067 pending breakpoint behavior."),
15068 &breakpoint_set_cmdlist, &breakpoint_show_cmdlist,
15069 &setlist, &showlist);
15070
15071 add_setshow_auto_boolean_cmd ("pending", no_class,
15072 &pending_break_support, _("\
15073 Set debugger's behavior regarding pending breakpoints."), _("\
15074 Show debugger's behavior regarding pending breakpoints."), _("\
15075 If on, an unrecognized breakpoint location will cause gdb to create a\n\
15076 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15077 an error. If auto, an unrecognized breakpoint location results in a\n\
15078 user-query to see if a pending breakpoint should be created."),
15079 NULL,
15080 show_pending_break_support,
15081 &breakpoint_set_cmdlist,
15082 &breakpoint_show_cmdlist);
15083
15084 pending_break_support = AUTO_BOOLEAN_AUTO;
15085
15086 add_setshow_boolean_cmd ("auto-hw", no_class,
15087 &automatic_hardware_breakpoints, _("\
15088 Set automatic usage of hardware breakpoints."), _("\
15089 Show automatic usage of hardware breakpoints."), _("\
15090 If set, the debugger will automatically use hardware breakpoints for\n\
15091 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15092 a warning will be emitted for such breakpoints."),
15093 NULL,
15094 show_automatic_hardware_breakpoints,
15095 &breakpoint_set_cmdlist,
15096 &breakpoint_show_cmdlist);
15097
15098 add_setshow_boolean_cmd ("always-inserted", class_support,
15099 &always_inserted_mode, _("\
15100 Set mode for inserting breakpoints."), _("\
15101 Show mode for inserting breakpoints."), _("\
15102 When this mode is on, breakpoints are inserted immediately as soon as\n\
15103 they're created, kept inserted even when execution stops, and removed\n\
15104 only when the user deletes them. When this mode is off (the default),\n\
15105 breakpoints are inserted only when execution continues, and removed\n\
15106 when execution stops."),
15107 NULL,
15108 &show_always_inserted_mode,
15109 &breakpoint_set_cmdlist,
15110 &breakpoint_show_cmdlist);
15111
15112 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15113 condition_evaluation_enums,
15114 &condition_evaluation_mode_1, _("\
15115 Set mode of breakpoint condition evaluation."), _("\
15116 Show mode of breakpoint condition evaluation."), _("\
15117 When this is set to \"host\", breakpoint conditions will be\n\
15118 evaluated on the host's side by GDB. When it is set to \"target\",\n\
15119 breakpoint conditions will be downloaded to the target (if the target\n\
15120 supports such feature) and conditions will be evaluated on the target's side.\n\
15121 If this is set to \"auto\" (default), this will be automatically set to\n\
15122 \"target\" if it supports condition evaluation, otherwise it will\n\
15123 be set to \"host\"."),
15124 &set_condition_evaluation_mode,
15125 &show_condition_evaluation_mode,
15126 &breakpoint_set_cmdlist,
15127 &breakpoint_show_cmdlist);
15128
15129 add_com ("break-range", class_breakpoint, break_range_command, _("\
15130 Set a breakpoint for an address range.\n\
15131 break-range START-LOCATION, END-LOCATION\n\
15132 where START-LOCATION and END-LOCATION can be one of the following:\n\
15133 LINENUM, for that line in the current file,\n\
15134 FILE:LINENUM, for that line in that file,\n\
15135 +OFFSET, for that number of lines after the current line\n\
15136 or the start of the range\n\
15137 FUNCTION, for the first line in that function,\n\
15138 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15139 *ADDRESS, for the instruction at that address.\n\
15140 \n\
15141 The breakpoint will stop execution of the inferior whenever it executes\n\
15142 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15143 range (including START-LOCATION and END-LOCATION)."));
15144
15145 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
15146 Set a dynamic printf at specified location.\n\
15147 dprintf location,format string,arg1,arg2,...\n\
15148 location may be a linespec, explicit, or address location.\n"
15149 "\n" LOCATION_HELP_STRING));
15150 set_cmd_completer (c, location_completer);
15151
15152 add_setshow_enum_cmd ("dprintf-style", class_support,
15153 dprintf_style_enums, &dprintf_style, _("\
15154 Set the style of usage for dynamic printf."), _("\
15155 Show the style of usage for dynamic printf."), _("\
15156 This setting chooses how GDB will do a dynamic printf.\n\
15157 If the value is \"gdb\", then the printing is done by GDB to its own\n\
15158 console, as with the \"printf\" command.\n\
15159 If the value is \"call\", the print is done by calling a function in your\n\
15160 program; by default printf(), but you can choose a different function or\n\
15161 output stream by setting dprintf-function and dprintf-channel."),
15162 update_dprintf_commands, NULL,
15163 &setlist, &showlist);
15164
15165 add_setshow_string_cmd ("dprintf-function", class_support,
15166 &dprintf_function, _("\
15167 Set the function to use for dynamic printf."), _("\
15168 Show the function to use for dynamic printf."), NULL,
15169 update_dprintf_commands, NULL,
15170 &setlist, &showlist);
15171
15172 add_setshow_string_cmd ("dprintf-channel", class_support,
15173 &dprintf_channel, _("\
15174 Set the channel to use for dynamic printf."), _("\
15175 Show the channel to use for dynamic printf."), NULL,
15176 update_dprintf_commands, NULL,
15177 &setlist, &showlist);
15178
15179 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
15180 &disconnected_dprintf, _("\
15181 Set whether dprintf continues after GDB disconnects."), _("\
15182 Show whether dprintf continues after GDB disconnects."), _("\
15183 Use this to let dprintf commands continue to hit and produce output\n\
15184 even if GDB disconnects or detaches from the target."),
15185 NULL,
15186 NULL,
15187 &setlist, &showlist);
15188
15189 add_com ("agent-printf", class_vars, agent_printf_command, _("\
15190 Target agent only formatted printing, like the C \"printf\" function.\n\
15191 Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
15192 This supports most C printf format specifications, like %s, %d, etc.\n\
15193 This is useful for formatted output in user-defined commands."));
15194
15195 automatic_hardware_breakpoints = true;
15196
15197 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
15198 "breakpoint");
15199 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
15200 "breakpoint");
15201 }